7865a47c981e8610fedaec1cc174fab36a88dff6
[sfrench/cifs-2.6.git] / net / netfilter / nfnetlink.c
1 /* Netfilter messages via netlink socket. Allows for user space
2  * protocol helpers and general trouble making from userspace.
3  *
4  * (C) 2001 by Jay Schulist <jschlst@samba.org>,
5  * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
6  * (C) 2005 by Pablo Neira Ayuso <pablo@eurodev.net>
7  *
8  * Initial netfilter messages via netlink development funded and
9  * generally made possible by Network Robots, Inc. (www.networkrobots.com)
10  *
11  * Further development of this code funded by Astaro AG (http://www.astaro.com)
12  *
13  * This software may be used and distributed according to the terms
14  * of the GNU General Public License, incorporated herein by reference.
15  */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/timer.h>
23 #include <linux/string.h>
24 #include <linux/sockios.h>
25 #include <linux/net.h>
26 #include <linux/fcntl.h>
27 #include <linux/skbuff.h>
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <net/sock.h>
31 #include <net/netlink.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34
35 #include <linux/netfilter.h>
36 #include <linux/netlink.h>
37 #include <linux/netfilter/nfnetlink.h>
38
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
41 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
42
43 static char __initdata nfversion[] = "0.30";
44
45 #if 0
46 #define DEBUGP(format, args...) \
47                 printk(KERN_DEBUG "%s(%d):%s(): " format, __FILE__, \
48                         __LINE__, __FUNCTION__, ## args)
49 #else
50 #define DEBUGP(format, args...)
51 #endif
52
53 static struct sock *nfnl = NULL;
54 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
55 static DEFINE_MUTEX(nfnl_mutex);
56
57 static void nfnl_lock(void)
58 {
59         mutex_lock(&nfnl_mutex);
60 }
61
62 static int nfnl_trylock(void)
63 {
64         return !mutex_trylock(&nfnl_mutex);
65 }
66
67 static void __nfnl_unlock(void)
68 {
69         mutex_unlock(&nfnl_mutex);
70 }
71
72 static void nfnl_unlock(void)
73 {
74         mutex_unlock(&nfnl_mutex);
75         if (nfnl->sk_receive_queue.qlen)
76                 nfnl->sk_data_ready(nfnl, 0);
77 }
78
79 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
80 {
81         DEBUGP("registering subsystem ID %u\n", n->subsys_id);
82
83         nfnl_lock();
84         if (subsys_table[n->subsys_id]) {
85                 nfnl_unlock();
86                 return -EBUSY;
87         }
88         subsys_table[n->subsys_id] = n;
89         nfnl_unlock();
90
91         return 0;
92 }
93
94 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
95 {
96         DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
97
98         nfnl_lock();
99         subsys_table[n->subsys_id] = NULL;
100         nfnl_unlock();
101
102         return 0;
103 }
104
105 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
106 {
107         u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
108
109         if (subsys_id >= NFNL_SUBSYS_COUNT
110             || subsys_table[subsys_id] == NULL)
111                 return NULL;
112
113         return subsys_table[subsys_id];
114 }
115
116 static inline struct nfnl_callback *
117 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
118 {
119         u_int8_t cb_id = NFNL_MSG_TYPE(type);
120
121         if (cb_id >= ss->cb_count) {
122                 DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
123                 return NULL;
124         }
125
126         return &ss->cb[cb_id];
127 }
128
129 void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
130                 const void *data)
131 {
132         struct nfattr *nfa;
133         int size = NFA_LENGTH(attrlen);
134
135         nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
136         nfa->nfa_type = attrtype;
137         nfa->nfa_len  = size;
138         memcpy(NFA_DATA(nfa), data, attrlen);
139         memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
140 }
141
142 void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
143 {
144         memset(tb, 0, sizeof(struct nfattr *) * maxattr);
145
146         while (NFA_OK(nfa, len)) {
147                 unsigned flavor = NFA_TYPE(nfa);
148                 if (flavor && flavor <= maxattr)
149                         tb[flavor-1] = nfa;
150                 nfa = NFA_NEXT(nfa, len);
151         }
152 }
153
154 /**
155  * nfnetlink_check_attributes - check and parse nfnetlink attributes
156  *
157  * subsys: nfnl subsystem for which this message is to be parsed
158  * nlmsghdr: netlink message to be checked/parsed
159  * cda: array of pointers, needs to be at least subsys->attr_count big
160  *
161  */
162 static int
163 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
164                            struct nlmsghdr *nlh, struct nfattr *cda[])
165 {
166         int min_len;
167         u_int16_t attr_count;
168         u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
169
170         if (unlikely(cb_id >= subsys->cb_count)) {
171                 DEBUGP("msgtype %u >= %u, returning\n",
172                         cb_id, subsys->cb_count);
173                 return -EINVAL;
174         }
175
176         min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
177         if (unlikely(nlh->nlmsg_len < min_len))
178                 return -EINVAL;
179
180         attr_count = subsys->cb[cb_id].attr_count;
181         memset(cda, 0, sizeof(struct nfattr *) * attr_count);
182
183         /* check attribute lengths. */
184         if (likely(nlh->nlmsg_len > min_len)) {
185                 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
186                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
187
188                 while (NFA_OK(attr, attrlen)) {
189                         unsigned flavor = NFA_TYPE(attr);
190                         if (flavor) {
191                                 if (flavor > attr_count)
192                                         return -EINVAL;
193                                 cda[flavor - 1] = attr;
194                         }
195                         attr = NFA_NEXT(attr, attrlen);
196                 }
197         }
198
199         /* implicit: if nlmsg_len == min_len, we return 0, and an empty
200          * (zeroed) cda[] array. The message is valid, but empty. */
201
202         return 0;
203 }
204
205 int nfnetlink_has_listeners(unsigned int group)
206 {
207         return netlink_has_listeners(nfnl, group);
208 }
209 EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
210
211 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
212 {
213         int err = 0;
214
215         NETLINK_CB(skb).dst_group = group;
216         if (echo)
217                 atomic_inc(&skb->users);
218         netlink_broadcast(nfnl, skb, pid, group, gfp_any());
219         if (echo)
220                 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
221
222         return err;
223 }
224
225 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
226 {
227         return netlink_unicast(nfnl, skb, pid, flags);
228 }
229
230 /* Process one complete nfnetlink message. */
231 static int nfnetlink_rcv_msg(struct sk_buff *skb,
232                                     struct nlmsghdr *nlh, int *errp)
233 {
234         struct nfnl_callback *nc;
235         struct nfnetlink_subsystem *ss;
236         int type, err = 0;
237
238         DEBUGP("entered; subsys=%u, msgtype=%u\n",
239                  NFNL_SUBSYS_ID(nlh->nlmsg_type),
240                  NFNL_MSG_TYPE(nlh->nlmsg_type));
241
242         if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
243                 DEBUGP("missing CAP_NET_ADMIN\n");
244                 *errp = -EPERM;
245                 return -1;
246         }
247
248         /* Only requests are handled by kernel now. */
249         if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
250                 DEBUGP("received non-request message\n");
251                 return 0;
252         }
253
254         /* All the messages must at least contain nfgenmsg */
255         if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg))) {
256                 DEBUGP("received message was too short\n");
257                 return 0;
258         }
259
260         type = nlh->nlmsg_type;
261         ss = nfnetlink_get_subsys(type);
262         if (!ss) {
263 #ifdef CONFIG_KMOD
264                 /* don't call nfnl_unlock, since it would reenter
265                  * with further packet processing */
266                 __nfnl_unlock();
267                 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
268                 nfnl_lock();
269                 ss = nfnetlink_get_subsys(type);
270                 if (!ss)
271 #endif
272                         goto err_inval;
273         }
274
275         nc = nfnetlink_find_client(type, ss);
276         if (!nc) {
277                 DEBUGP("unable to find client for type %d\n", type);
278                 goto err_inval;
279         }
280
281         {
282                 u_int16_t attr_count =
283                         ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
284                 struct nfattr *cda[attr_count];
285
286                 memset(cda, 0, sizeof(struct nfattr *) * attr_count);
287
288                 err = nfnetlink_check_attributes(ss, nlh, cda);
289                 if (err < 0)
290                         goto err_inval;
291
292                 DEBUGP("calling handler\n");
293                 err = nc->call(nfnl, skb, nlh, cda, errp);
294                 *errp = err;
295                 return err;
296         }
297
298 err_inval:
299         DEBUGP("returning -EINVAL\n");
300         *errp = -EINVAL;
301         return -1;
302 }
303
304 /* Process one packet of messages. */
305 static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
306 {
307         int err;
308         struct nlmsghdr *nlh;
309
310         while (skb->len >= NLMSG_SPACE(0)) {
311                 u32 rlen;
312
313                 nlh = (struct nlmsghdr *)skb->data;
314                 if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
315                     || skb->len < nlh->nlmsg_len)
316                         return 0;
317                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
318                 if (rlen > skb->len)
319                         rlen = skb->len;
320                 if (nfnetlink_rcv_msg(skb, nlh, &err)) {
321                         if (!err)
322                                 return -1;
323                         netlink_ack(skb, nlh, err);
324                 } else
325                         if (nlh->nlmsg_flags & NLM_F_ACK)
326                                 netlink_ack(skb, nlh, 0);
327                 skb_pull(skb, rlen);
328         }
329
330         return 0;
331 }
332
333 static void nfnetlink_rcv(struct sock *sk, int len)
334 {
335         do {
336                 struct sk_buff *skb;
337
338                 if (nfnl_trylock())
339                         return;
340
341                 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
342                         if (nfnetlink_rcv_skb(skb)) {
343                                 if (skb->len)
344                                         skb_queue_head(&sk->sk_receive_queue,
345                                                        skb);
346                                 else
347                                         kfree_skb(skb);
348                                 break;
349                         }
350                         kfree_skb(skb);
351                 }
352
353                 /* don't call nfnl_unlock, since it would reenter
354                  * with further packet processing */
355                 __nfnl_unlock();
356         } while(nfnl && nfnl->sk_receive_queue.qlen);
357 }
358
359 static void __exit nfnetlink_exit(void)
360 {
361         printk("Removing netfilter NETLINK layer.\n");
362         sock_release(nfnl->sk_socket);
363         return;
364 }
365
366 static int __init nfnetlink_init(void)
367 {
368         printk("Netfilter messages via NETLINK v%s.\n", nfversion);
369
370         nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
371                                      nfnetlink_rcv, THIS_MODULE);
372         if (!nfnl) {
373                 printk(KERN_ERR "cannot initialize nfnetlink!\n");
374                 return -1;
375         }
376
377         return 0;
378 }
379
380 module_init(nfnetlink_init);
381 module_exit(nfnetlink_exit);
382
383 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
384 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
385 EXPORT_SYMBOL_GPL(nfnetlink_send);
386 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
387 EXPORT_SYMBOL_GPL(nfattr_parse);
388 EXPORT_SYMBOL_GPL(__nfa_fill);