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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/etherdevice.h>
18 #include <linux/netlink.h>
19 #include <asm/byteorder.h>
22 #if !defined(NLMSG_HDRLEN)
23 #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
26 #define ND_MAX_GROUP 30
27 #define ND_IFINDEX_LEN sizeof(int)
28 #define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
29 #define ND_NLMSG_DATA(nlh) \
30 ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN))
31 #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
32 #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
33 #define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
34 #define ND_MAX_MSG_LEN 8096
36 #if defined(DEFINE_MUTEX)
37 static DEFINE_MUTEX(netlink_mutex);
39 static struct semaphore netlink_mutex;
40 #define mutex_lock(x) down(x)
41 #define mutex_unlock(x) up(x)
44 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
46 static void netlink_rcv_cb(struct sk_buff *skb)
49 struct net_device *dev;
54 if (skb->len >= NLMSG_SPACE(0)) {
55 nlh = (struct nlmsghdr *)skb->data;
57 if (skb->len < nlh->nlmsg_len ||
58 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
59 netdev_err(skb->dev, "Invalid length (%d,%d)\n",
60 skb->len, nlh->nlmsg_len);
64 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
65 msg = ND_NLMSG_DATA(nlh);
66 mlen = ND_NLMSG_R_LEN(nlh);
69 dev = dev_get_by_index(&init_net, ifindex);
71 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
75 "dev_get_by_index(%d) is not found.\n",
78 netdev_err(skb->dev, "Unregistered Callback\n");
82 static void netlink_rcv(struct sk_buff *skb)
84 mutex_lock(&netlink_mutex);
86 mutex_unlock(&netlink_mutex);
89 struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
93 struct netlink_kernel_cfg cfg = {
97 #if !defined(DEFINE_MUTEX)
98 init_MUTEX(&netlink_mutex);
101 sock = netlink_kernel_create(&init_net, unit, &cfg);
109 void netlink_exit(struct sock *sock)
111 netlink_kernel_release(sock);
114 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
117 struct sk_buff *skb = NULL;
118 struct nlmsghdr *nlh;
121 if (group > ND_MAX_GROUP) {
122 pr_err("Group %d is invalied.\n", group);
123 pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
127 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
129 pr_err("netlink_broadcast ret=%d\n", ret);
134 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
139 memcpy(nlmsg_data(nlh), msg, len);
141 NETLINK_CB(skb).portid = 0;
142 NETLINK_CB(skb).dst_group = 0;
144 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
150 pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
151 group, type, len, ret);