2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/version.h>
15 #include <linux/module.h>
16 #include <linux/etherdevice.h>
17 #include <linux/netlink.h>
18 #include <asm/byteorder.h>
21 #if !defined(NLMSG_HDRLEN)
22 #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
25 #define ND_MAX_GROUP 30
26 #define ND_IFINDEX_LEN sizeof(int)
27 #define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
28 #define ND_NLMSG_DATA(nlh) \
29 ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN))
30 #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
31 #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
32 #define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
33 #define ND_MAX_MSG_LEN 8096
35 #if defined(DEFINE_MUTEX)
36 static DEFINE_MUTEX(netlink_mutex);
38 static struct semaphore netlink_mutex;
39 #define mutex_lock(x) down(x)
40 #define mutex_unlock(x) up(x)
43 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
45 static void netlink_rcv_cb(struct sk_buff *skb)
48 struct net_device *dev;
53 if (skb->len >= NLMSG_SPACE(0)) {
54 nlh = (struct nlmsghdr *)skb->data;
56 if (skb->len < nlh->nlmsg_len ||
57 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
58 printk(KERN_ERR "Invalid length (%d,%d)\n", skb->len,
63 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
64 msg = ND_NLMSG_DATA(nlh);
65 mlen = ND_NLMSG_R_LEN(nlh);
68 dev = dev_get_by_index(&init_net, ifindex);
70 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
73 printk(KERN_ERR "dev_get_by_index(%d) "
74 "is not found.\n", ifindex);
76 printk(KERN_ERR "Unregistered Callback\n");
80 static void netlink_rcv(struct sk_buff *skb)
82 mutex_lock(&netlink_mutex);
84 mutex_unlock(&netlink_mutex);
87 struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
92 #if !defined(DEFINE_MUTEX)
93 init_MUTEX(&netlink_mutex);
96 sock = netlink_kernel_create(&init_net, unit, 0, netlink_rcv, NULL,
105 void netlink_exit(struct sock *sock)
107 sock_release(sock->sk_socket);
110 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
113 struct sk_buff *skb = NULL;
114 struct nlmsghdr *nlh;
117 if (group > ND_MAX_GROUP) {
118 printk(KERN_ERR "Group %d is invalied.\n", group);
119 printk(KERN_ERR "Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
123 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
125 printk(KERN_ERR "netlink_broadcast ret=%d\n", ret);
130 nlh = NLMSG_PUT(skb, 0, seq, type, len);
131 memcpy(NLMSG_DATA(nlh), msg, len);
133 NETLINK_CB(skb).pid = 0;
134 NETLINK_CB(skb).dst_group = 0;
136 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
142 printk(KERN_ERR "netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
143 group, type, len, ret);