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/version.h>
17 #include <linux/export.h>
18 #include <linux/etherdevice.h>
19 #include <linux/netlink.h>
20 #include <asm/byteorder.h>
23 #include "netlink_k.h"
25 #if defined(DEFINE_MUTEX)
26 static DEFINE_MUTEX(netlink_mutex);
28 static struct semaphore netlink_mutex;
29 #define mutex_lock(x) down(x)
30 #define mutex_unlock(x) up(x)
33 #define ND_MAX_GROUP 30
34 #define ND_IFINDEX_LEN sizeof(int)
35 #define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
36 #define ND_NLMSG_DATA(nlh) ((void *)((char *)NLMSG_DATA(nlh) + ND_IFINDEX_LEN))
37 #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
38 #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
39 #define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
40 #define ND_MAX_MSG_LEN (1024 * 32)
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;
53 pr_err("nl cb - unregistered\n");
57 if (skb->len < NLMSG_SPACE(0)) {
58 pr_err("nl cb - invalid skb length\n");
62 nlh = (struct nlmsghdr *)skb->data;
64 if (skb->len < nlh->nlmsg_len || nlh->nlmsg_len > ND_MAX_MSG_LEN) {
65 pr_err("nl cb - invalid length (%d,%d)\n",
66 skb->len, nlh->nlmsg_len);
70 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
71 msg = ND_NLMSG_DATA(nlh);
72 mlen = ND_NLMSG_R_LEN(nlh);
74 dev = dev_get_by_index(&init_net, ifindex);
76 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
79 pr_err("nl cb - dev (%d) not found\n", ifindex);
83 static void netlink_rcv(struct sk_buff *skb)
85 mutex_lock(&netlink_mutex);
87 mutex_unlock(&netlink_mutex);
90 struct sock *netlink_init(int unit,
91 void (*cb)(struct net_device *dev, u16 type, void *msg, int len))
94 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
95 struct netlink_kernel_cfg cfg = {
100 #if !defined(DEFINE_MUTEX)
101 init_MUTEX(&netlink_mutex);
104 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
105 sock = netlink_kernel_create(&init_net, unit, 0, netlink_rcv, NULL,
107 #elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
108 sock = netlink_kernel_create(&init_net, unit, THIS_MODULE, &cfg);
110 sock = netlink_kernel_create(&init_net, unit, &cfg);
119 void netlink_exit(struct sock *sock)
121 sock_release(sock->sk_socket);
124 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
127 struct sk_buff *skb = NULL;
128 struct nlmsghdr *nlh;
131 if (group > ND_MAX_GROUP)
134 if (!netlink_has_listeners(sock, group+1))
137 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
143 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
144 memcpy(NLMSG_DATA(nlh), msg, len);
145 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
146 NETLINK_CB(skb).pid = 0;
148 NETLINK_CB(skb).portid = 0;
150 NETLINK_CB(skb).dst_group = 0;
152 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
157 pr_err("nl broadcast g=%d, t=%d, l=%d, r=%d\n",
158 group, type, len, ret);
159 else if (netlink_has_listeners(sock, group+1))