blob: f3cdaa6c468cfde23797911ee611fa14fe795bb9 [file] [log] [blame]
Sage Ahn247e9cf2012-05-15 13:20:36 +09001/*
2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3 *
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.
7 *
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.
12 */
13
YAMANE Toshiakicade0fe2012-10-29 20:05:02 +090014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Sage Ahn247e9cf2012-05-15 13:20:36 +090016#include <linux/module.h>
17#include <linux/etherdevice.h>
Hong zhi guob96dc462013-03-27 06:52:17 +000018#include <net/netlink.h>
Sage Ahn247e9cf2012-05-15 13:20:36 +090019#include <asm/byteorder.h>
20#include <net/sock.h>
Peter Huewea6000532013-02-19 18:50:18 +010021#include "netlink_k.h"
Sage Ahn247e9cf2012-05-15 13:20:36 +090022
23#if !defined(NLMSG_HDRLEN)
24#define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
25#endif
26
27#define ND_MAX_GROUP 30
28#define ND_IFINDEX_LEN sizeof(int)
Hong zhi guob96dc462013-03-27 06:52:17 +000029#define ND_NLMSG_SPACE(len) (nlmsg_total_size(len) + ND_IFINDEX_LEN)
Sage Ahn247e9cf2012-05-15 13:20:36 +090030#define ND_NLMSG_DATA(nlh) \
Hong zhi guob96dc462013-03-27 06:52:17 +000031 ((void *)((char *)nlmsg_data(nlh) + ND_IFINDEX_LEN))
Sage Ahn247e9cf2012-05-15 13:20:36 +090032#define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
33#define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
Hong zhi guob96dc462013-03-27 06:52:17 +000034#define ND_NLMSG_IFIDX(nlh) nlmsg_data(nlh)
Sage Ahn247e9cf2012-05-15 13:20:36 +090035#define ND_MAX_MSG_LEN 8096
36
37#if defined(DEFINE_MUTEX)
38static DEFINE_MUTEX(netlink_mutex);
39#else
40static struct semaphore netlink_mutex;
41#define mutex_lock(x) down(x)
42#define mutex_unlock(x) up(x)
43#endif
44
45static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
46
47static void netlink_rcv_cb(struct sk_buff *skb)
48{
49 struct nlmsghdr *nlh;
50 struct net_device *dev;
51 u32 mlen;
52 void *msg;
53 int ifindex;
54
Hong zhi guob96dc462013-03-27 06:52:17 +000055 if (skb->len >= NLMSG_HDRLEN) {
Sage Ahn247e9cf2012-05-15 13:20:36 +090056 nlh = (struct nlmsghdr *)skb->data;
57
Davide Gianfortea3709f72014-05-23 22:06:44 +020058 if (skb->len < nlh->nlmsg_len ||
59 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
YAMANE Toshiakicade0fe2012-10-29 20:05:02 +090060 netdev_err(skb->dev, "Invalid length (%d,%d)\n",
61 skb->len, nlh->nlmsg_len);
Sage Ahn247e9cf2012-05-15 13:20:36 +090062 return;
63 }
64
65 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
66 msg = ND_NLMSG_DATA(nlh);
67 mlen = ND_NLMSG_R_LEN(nlh);
68
69 if (rcv_cb) {
70 dev = dev_get_by_index(&init_net, ifindex);
71 if (dev) {
72 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
73 dev_put(dev);
74 } else
YAMANE Toshiakicade0fe2012-10-29 20:05:02 +090075 netdev_err(skb->dev,
76 "dev_get_by_index(%d) is not found.\n",
77 ifindex);
Michalis Pappas71fd11e2014-05-09 18:08:25 +080078 } else {
YAMANE Toshiakicade0fe2012-10-29 20:05:02 +090079 netdev_err(skb->dev, "Unregistered Callback\n");
Michalis Pappas71fd11e2014-05-09 18:08:25 +080080 }
Sage Ahn247e9cf2012-05-15 13:20:36 +090081 }
82}
83
84static void netlink_rcv(struct sk_buff *skb)
85{
86 mutex_lock(&netlink_mutex);
87 netlink_rcv_cb(skb);
88 mutex_unlock(&netlink_mutex);
89}
90
91struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
Michalis Pappas39c511f2014-05-09 18:08:27 +080092 void *msg, int len))
Sage Ahn247e9cf2012-05-15 13:20:36 +090093{
94 struct sock *sock;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +000095 struct netlink_kernel_cfg cfg = {
96 .input = netlink_rcv,
97 };
Sage Ahn247e9cf2012-05-15 13:20:36 +090098
99#if !defined(DEFINE_MUTEX)
100 init_MUTEX(&netlink_mutex);
101#endif
102
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000103 sock = netlink_kernel_create(&init_net, unit, &cfg);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900104
105 if (sock)
106 rcv_cb = cb;
107
108 return sock;
109}
110
111void netlink_exit(struct sock *sock)
112{
Ben Chan9f771862012-06-14 13:18:56 -0700113 netlink_kernel_release(sock);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900114}
115
116int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
117{
118 static u32 seq;
119 struct sk_buff *skb = NULL;
120 struct nlmsghdr *nlh;
121 int ret = 0;
122
123 if (group > ND_MAX_GROUP) {
Masanari Iida24768162015-05-27 23:51:51 +0900124 pr_err("Group %d is invalid.\n", group);
YAMANE Toshiakicade0fe2012-10-29 20:05:02 +0900125 pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900126 return -EINVAL;
127 }
128
Hong zhi guob96dc462013-03-27 06:52:17 +0000129 skb = nlmsg_new(len, GFP_ATOMIC);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900130 if (!skb) {
YAMANE Toshiakicade0fe2012-10-29 20:05:02 +0900131 pr_err("netlink_broadcast ret=%d\n", ret);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900132 return -ENOMEM;
133 }
134
135 seq++;
David S. Millera8edf8a2012-06-26 21:20:52 -0700136 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
137 if (!nlh) {
138 kfree_skb(skb);
139 return -EMSGSIZE;
140 }
141 memcpy(nlmsg_data(nlh), msg, len);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900142
Eric W. Biederman15e47302012-09-07 20:12:54 +0000143 NETLINK_CB(skb).portid = 0;
Sage Ahn247e9cf2012-05-15 13:20:36 +0900144 NETLINK_CB(skb).dst_group = 0;
145
146 ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
147
Gulsah Kose2e98f762014-09-27 00:00:14 +0300148 if (!ret)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900149 return len;
Gulsah Kose93f509a2014-09-27 00:00:13 +0300150 if (ret != -ESRCH) {
151 pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
152 group, type, len, ret);
153 }
154 ret = 0;
Sage Ahn247e9cf2012-05-15 13:20:36 +0900155 return ret;
156}