0001
0002
0003
0004 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0005
0006 #include <linux/export.h>
0007 #include <linux/mutex.h>
0008 #include <linux/etherdevice.h>
0009 #include <linux/netlink.h>
0010 #include <asm/byteorder.h>
0011 #include <net/sock.h>
0012
0013 #include "netlink_k.h"
0014
0015 static DEFINE_MUTEX(netlink_mutex);
0016
0017 #define ND_MAX_GROUP 30
0018 #define ND_IFINDEX_LEN sizeof(int)
0019 #define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
0020 #define ND_NLMSG_DATA(nlh) ((void *)((char *)NLMSG_DATA(nlh) + \
0021 ND_IFINDEX_LEN))
0022 #define ND_NLMSG_S_LEN(len) ((len) + ND_IFINDEX_LEN)
0023 #define ND_NLMSG_R_LEN(nlh) ((nlh)->nlmsg_len - ND_IFINDEX_LEN)
0024 #define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
0025 #define ND_MAX_MSG_LEN (1024 * 32)
0026
0027 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
0028
0029 static void netlink_rcv_cb(struct sk_buff *skb)
0030 {
0031 struct nlmsghdr *nlh;
0032 struct net_device *dev;
0033 u32 mlen;
0034 void *msg;
0035 int ifindex;
0036
0037 if (!rcv_cb) {
0038 pr_err("nl cb - unregistered\n");
0039 return;
0040 }
0041
0042 if (skb->len < NLMSG_HDRLEN) {
0043 pr_err("nl cb - invalid skb length\n");
0044 return;
0045 }
0046
0047 nlh = (struct nlmsghdr *)skb->data;
0048
0049 if (skb->len < nlh->nlmsg_len || nlh->nlmsg_len > ND_MAX_MSG_LEN) {
0050 pr_err("nl cb - invalid length (%d,%d)\n",
0051 skb->len, nlh->nlmsg_len);
0052 return;
0053 }
0054
0055 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
0056 msg = ND_NLMSG_DATA(nlh);
0057 mlen = ND_NLMSG_R_LEN(nlh);
0058
0059 dev = dev_get_by_index(&init_net, ifindex);
0060 if (dev) {
0061 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
0062 dev_put(dev);
0063 } else {
0064 pr_err("nl cb - dev (%d) not found\n", ifindex);
0065 }
0066 }
0067
0068 static void netlink_rcv(struct sk_buff *skb)
0069 {
0070 mutex_lock(&netlink_mutex);
0071 netlink_rcv_cb(skb);
0072 mutex_unlock(&netlink_mutex);
0073 }
0074
0075 struct sock *netlink_init(int unit,
0076 void (*cb)(struct net_device *dev, u16 type,
0077 void *msg, int len))
0078 {
0079 struct sock *sock;
0080 struct netlink_kernel_cfg cfg = {
0081 .input = netlink_rcv,
0082 };
0083
0084 sock = netlink_kernel_create(&init_net, unit, &cfg);
0085
0086 if (sock)
0087 rcv_cb = cb;
0088
0089 return sock;
0090 }
0091
0092 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len,
0093 struct net_device *dev)
0094 {
0095 static u32 seq;
0096 struct sk_buff *skb = NULL;
0097 struct nlmsghdr *nlh;
0098 int ret = 0;
0099
0100 if (group > ND_MAX_GROUP)
0101 return -EINVAL;
0102
0103 if (!netlink_has_listeners(sock, group + 1))
0104 return -ESRCH;
0105
0106 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
0107 if (!skb)
0108 return -ENOMEM;
0109
0110 seq++;
0111
0112 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
0113 memcpy(NLMSG_DATA(nlh), msg, len);
0114 NETLINK_CB(skb).portid = 0;
0115 NETLINK_CB(skb).dst_group = 0;
0116
0117 ret = netlink_broadcast(sock, skb, 0, group + 1, GFP_ATOMIC);
0118 if (!ret)
0119 return len;
0120
0121 if (ret != -ESRCH)
0122 netdev_err(dev, "nl broadcast g=%d, t=%d, l=%d, r=%d\n",
0123 group, type, len, ret);
0124 else if (netlink_has_listeners(sock, group + 1))
0125 return -EAGAIN;
0126
0127 return ret;
0128 }