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 <net/netlink.h>
19 #include <asm/byteorder.h>
21 #include "netlink_k.h"
23 #if !defined(NLMSG_HDRLEN)
24 #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
27 #define ND_MAX_GROUP 30
28 #define ND_IFINDEX_LEN sizeof(int)
29 #define ND_NLMSG_SPACE(len) (nlmsg_total_size(len) + ND_IFINDEX_LEN)
30 #define ND_NLMSG_DATA(nlh) \
31 ((void *)((char *)nlmsg_data(nlh) + ND_IFINDEX_LEN))
32 #define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
33 #define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
34 #define ND_NLMSG_IFIDX(nlh) nlmsg_data(nlh)
35 #define ND_MAX_MSG_LEN 8096
37 #if defined(DEFINE_MUTEX)
38 static DEFINE_MUTEX(netlink_mutex);
40 static struct semaphore netlink_mutex;
41 #define mutex_lock(x) down(x)
42 #define mutex_unlock(x) up(x)
45 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
47 static void netlink_rcv_cb(struct sk_buff *skb)
50 struct net_device *dev;
55 if (skb->len >= NLMSG_HDRLEN) {
56 nlh = (struct nlmsghdr *)skb->data;
58 if (skb->len < nlh->nlmsg_len ||
59 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
60 netdev_err(skb->dev, "Invalid length (%d,%d)\n",
61 skb->len, nlh->nlmsg_len);
65 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
66 msg = ND_NLMSG_DATA(nlh);
67 mlen = ND_NLMSG_R_LEN(nlh);
70 dev = dev_get_by_index(&init_net, ifindex);
72 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
76 "dev_get_by_index(%d) is not found.\n",
79 netdev_err(skb->dev, "Unregistered Callback\n");
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, void (*cb)(struct net_device *dev, u16 type,
94 struct netlink_kernel_cfg cfg = {
98 #if !defined(DEFINE_MUTEX)
99 init_MUTEX(&netlink_mutex);
102 sock = netlink_kernel_create(&init_net, unit, &cfg);
110 void netlink_exit(struct sock *sock)
112 netlink_kernel_release(sock);
115 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
118 struct sk_buff *skb = NULL;
119 struct nlmsghdr *nlh;
122 if (group > ND_MAX_GROUP) {
123 pr_err("Group %d is invalied.\n", group);
124 pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
128 skb = nlmsg_new(len, GFP_ATOMIC);
130 pr_err("netlink_broadcast ret=%d\n", ret);
135 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
140 memcpy(nlmsg_data(nlh), msg, len);
142 NETLINK_CB(skb).portid = 0;
143 NETLINK_CB(skb).dst_group = 0;
145 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
151 pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
152 group, type, len, ret);