filter: fix va_list build error
[cascardo/linux.git] / 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
22 #if !defined(NLMSG_HDRLEN)
23 #define NLMSG_HDRLEN     ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
24 #endif
25
26 #define ND_MAX_GROUP                    30
27 #define ND_IFINDEX_LEN                  sizeof(int)
28 #define ND_NLMSG_SPACE(len)             (nlmsg_total_size(len) + ND_IFINDEX_LEN)
29 #define ND_NLMSG_DATA(nlh) \
30         ((void *)((char *)nlmsg_data(nlh) + ND_IFINDEX_LEN))
31 #define ND_NLMSG_S_LEN(len)             (len+ND_IFINDEX_LEN)
32 #define ND_NLMSG_R_LEN(nlh)             (nlh->nlmsg_len-ND_IFINDEX_LEN)
33 #define ND_NLMSG_IFIDX(nlh)             nlmsg_data(nlh)
34 #define ND_MAX_MSG_LEN                  8096
35
36 #if defined(DEFINE_MUTEX)
37 static DEFINE_MUTEX(netlink_mutex);
38 #else
39 static struct semaphore netlink_mutex;
40 #define mutex_lock(x)           down(x)
41 #define mutex_unlock(x)         up(x)
42 #endif
43
44 static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
45
46 static void netlink_rcv_cb(struct sk_buff *skb)
47 {
48         struct nlmsghdr *nlh;
49         struct net_device *dev;
50         u32 mlen;
51         void *msg;
52         int ifindex;
53
54         if (skb->len >= NLMSG_HDRLEN) {
55                 nlh = (struct nlmsghdr *)skb->data;
56
57                 if (skb->len < nlh->nlmsg_len ||
58                 nlh->nlmsg_len > ND_MAX_MSG_LEN) {
59                         netdev_err(skb->dev, "Invalid length (%d,%d)\n",
60                                    skb->len, nlh->nlmsg_len);
61                         return;
62                 }
63
64                 memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
65                 msg = ND_NLMSG_DATA(nlh);
66                 mlen = ND_NLMSG_R_LEN(nlh);
67
68                 if (rcv_cb) {
69                         dev = dev_get_by_index(&init_net, ifindex);
70                         if (dev) {
71                                 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
72                                 dev_put(dev);
73                         } else
74                                 netdev_err(skb->dev,
75                                            "dev_get_by_index(%d) is not found.\n",
76                                            ifindex);
77                 } else
78                         netdev_err(skb->dev, "Unregistered Callback\n");
79         }
80 }
81
82 static void netlink_rcv(struct sk_buff *skb)
83 {
84         mutex_lock(&netlink_mutex);
85         netlink_rcv_cb(skb);
86         mutex_unlock(&netlink_mutex);
87 }
88
89 struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
90                                                 void *msg, int len))
91 {
92         struct sock *sock;
93         struct netlink_kernel_cfg cfg = {
94                 .input  = netlink_rcv,
95         };
96
97 #if !defined(DEFINE_MUTEX)
98         init_MUTEX(&netlink_mutex);
99 #endif
100
101         sock = netlink_kernel_create(&init_net, unit, &cfg);
102
103         if (sock)
104                 rcv_cb = cb;
105
106         return sock;
107 }
108
109 void netlink_exit(struct sock *sock)
110 {
111         netlink_kernel_release(sock);
112 }
113
114 int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
115 {
116         static u32 seq;
117         struct sk_buff *skb = NULL;
118         struct nlmsghdr *nlh;
119         int ret = 0;
120
121         if (group > ND_MAX_GROUP) {
122                 pr_err("Group %d is invalied.\n", group);
123                 pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
124                 return -EINVAL;
125         }
126
127         skb = nlmsg_new(len, GFP_ATOMIC);
128         if (!skb) {
129                 pr_err("netlink_broadcast ret=%d\n", ret);
130                 return -ENOMEM;
131         }
132
133         seq++;
134         nlh = nlmsg_put(skb, 0, seq, type, len, 0);
135         if (!nlh) {
136                 kfree_skb(skb);
137                 return -EMSGSIZE;
138         }
139         memcpy(nlmsg_data(nlh), msg, len);
140
141         NETLINK_CB(skb).portid = 0;
142         NETLINK_CB(skb).dst_group = 0;
143
144         ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
145
146         if (!ret)
147                 return len;
148         else {
149                 if (ret != -ESRCH) {
150                         pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
151                                group, type, len, ret);
152                 }
153                 ret = 0;
154         }
155         return ret;
156 }