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/module.h>
15 #include <linux/etherdevice.h>
16 #include <linux/netlink.h>
17 #include <asm/byteorder.h>
20 #if !defined(NLMSG_HDRLEN)
21 #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
24 #define ND_MAX_GROUP 30
25 #define ND_IFINDEX_LEN sizeof(int)
26 #define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
27 #define ND_NLMSG_DATA(nlh) \
28 ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN))
29 #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
30 #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
31 #define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
32 #define ND_MAX_MSG_LEN 8096
34 #if defined(DEFINE_MUTEX)
35 static DEFINE_MUTEX(netlink_mutex);
37 static struct semaphore netlink_mutex;
38 #define mutex_lock(x) down(x)
39 #define mutex_unlock(x) up(x)
42 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
44 static void netlink_rcv_cb(struct sk_buff *skb)
47 struct net_device *dev;
52 if (skb->len >= NLMSG_SPACE(0)) {
53 nlh = (struct nlmsghdr *)skb->data;
55 if (skb->len < nlh->nlmsg_len ||
56 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
57 printk(KERN_ERR "Invalid length (%d,%d)\n", skb->len,
62 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
63 msg = ND_NLMSG_DATA(nlh);
64 mlen = ND_NLMSG_R_LEN(nlh);
67 dev = dev_get_by_index(&init_net, ifindex);
69 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
72 printk(KERN_ERR "dev_get_by_index(%d) "
73 "is not found.\n", ifindex);
75 printk(KERN_ERR "Unregistered Callback\n");
79 static void netlink_rcv(struct sk_buff *skb)
81 mutex_lock(&netlink_mutex);
83 mutex_unlock(&netlink_mutex);
86 struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
90 struct netlink_kernel_cfg cfg = {
94 #if !defined(DEFINE_MUTEX)
95 init_MUTEX(&netlink_mutex);
98 sock = netlink_kernel_create(&init_net, unit, THIS_MODULE, &cfg);
106 void netlink_exit(struct sock *sock)
108 netlink_kernel_release(sock);
111 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
114 struct sk_buff *skb = NULL;
115 struct nlmsghdr *nlh;
118 if (group > ND_MAX_GROUP) {
119 printk(KERN_ERR "Group %d is invalied.\n", group);
120 printk(KERN_ERR "Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
124 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
126 printk(KERN_ERR "netlink_broadcast ret=%d\n", ret);
131 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
136 memcpy(nlmsg_data(nlh), msg, len);
138 NETLINK_CB(skb).pid = 0;
139 NETLINK_CB(skb).dst_group = 0;
141 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
147 printk(KERN_ERR "netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
148 group, type, len, ret);