Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / gdm72xx / netlink_k.c
1 /*
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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/etherdevice.h>
18 #include <net/netlink.h>
19 #include <asm/byteorder.h>
20 #include <net/sock.h>
21 #include "netlink_k.h"
22
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)
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
36
37 #if defined(DEFINE_MUTEX)
38 static DEFINE_MUTEX(netlink_mutex);
39 #else
40 static struct semaphore netlink_mutex;
41 #define mutex_lock(x)           down(x)
42 #define mutex_unlock(x)         up(x)
43 #endif
44
45 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
46
47 static 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
55         if (skb->len >= NLMSG_HDRLEN) {
56                 nlh = (struct nlmsghdr *)skb->data;
57
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);
62                         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
75                                 netdev_err(skb->dev,
76                                            "dev_get_by_index(%d) is not found.\n",
77                                            ifindex);
78                 } else {
79                         netdev_err(skb->dev, "Unregistered Callback\n");
80                 }
81         }
82 }
83
84 static 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
91 struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
92                                                void *msg, int len))
93 {
94         struct sock *sock;
95         struct netlink_kernel_cfg cfg = {
96                 .input  = netlink_rcv,
97         };
98
99 #if !defined(DEFINE_MUTEX)
100         init_MUTEX(&netlink_mutex);
101 #endif
102
103         sock = netlink_kernel_create(&init_net, unit, &cfg);
104
105         if (sock)
106                 rcv_cb = cb;
107
108         return sock;
109 }
110
111 void netlink_exit(struct sock *sock)
112 {
113         netlink_kernel_release(sock);
114 }
115
116 int 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) {
124                 pr_err("Group %d is invalied.\n", group);
125                 pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
126                 return -EINVAL;
127         }
128
129         skb = nlmsg_new(len, GFP_ATOMIC);
130         if (!skb) {
131                 pr_err("netlink_broadcast ret=%d\n", ret);
132                 return -ENOMEM;
133         }
134
135         seq++;
136         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);
142
143         NETLINK_CB(skb).portid = 0;
144         NETLINK_CB(skb).dst_group = 0;
145
146         ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
147
148         if (!ret)
149                 return len;
150         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;
155         return ret;
156 }