]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - net/netfilter/nfnetlink.c
b0ed5798184766182dccc7986397bad87a678cb1
[linux-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/config.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
21 #include <linux/kernel.h>
22 #include <linux/major.h>
23 #include <linux/sched.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/skbuff.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <net/sock.h>
33 #include <linux/init.h>
34 #include <linux/spinlock.h>
35
36 #include <linux/netfilter.h>
37 #include <linux/netlink.h>
38 #include <linux/netfilter/nfnetlink.h>
39
40 MODULE_LICENSE("GPL");
41
42 static char __initdata nfversion[] = "0.30";
43
44 #if 0
45 #define DEBUGP printk
46 #else
47 #define DEBUGP(format, args...)
48 #endif
49
50 static struct sock *nfnl = NULL;
51 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
52 DECLARE_MUTEX(nfnl_sem);
53
54 void nfnl_lock(void)
55 {
56         nfnl_shlock();
57 }
58
59 void nfnl_unlock(void)
60 {
61         nfnl_shunlock();
62 }
63
64 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
65 {
66         DEBUGP("registering subsystem ID %u\n", n->subsys_id);
67
68         /* If the netlink socket wasn't created, then fail */
69         if (!nfnl)
70                 return -1;
71
72         nfnl_lock();
73         subsys_table[n->subsys_id] = n;
74         nfnl_unlock();
75
76         return 0;
77 }
78
79 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
80 {
81         DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
82
83         nfnl_lock();
84         subsys_table[n->subsys_id] = NULL;
85         nfnl_unlock();
86
87         return 0;
88 }
89
90 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
91 {
92         u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
93
94         if (subsys_id >= NFNL_SUBSYS_COUNT
95             || subsys_table[subsys_id] == NULL)
96                 return NULL;
97
98         return subsys_table[subsys_id];
99 }
100
101 static inline struct nfnl_callback *
102 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
103 {
104         u_int8_t cb_id = NFNL_MSG_TYPE(type);
105         
106         if (cb_id >= ss->cb_count) {
107                 DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
108                 return NULL;
109         }
110
111         return &ss->cb[cb_id];
112 }
113
114 void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
115                 const void *data)
116 {
117         struct nfattr *nfa;
118         int size = NFA_LENGTH(attrlen);
119
120         nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
121         nfa->nfa_type = attrtype;
122         nfa->nfa_len  = size;
123         memcpy(NFA_DATA(nfa), data, attrlen);
124         memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
125 }
126
127 int nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
128 {
129         memset(tb, 0, sizeof(struct nfattr *) * maxattr);
130
131         while (NFA_OK(nfa, len)) {
132                 unsigned flavor = nfa->nfa_type;
133                 if (flavor && flavor <= maxattr)
134                         tb[flavor-1] = nfa;
135                 nfa = NFA_NEXT(nfa, len);
136         }
137
138         return 0;
139 }
140
141 /**
142  * nfnetlink_check_attributes - check and parse nfnetlink attributes
143  *
144  * subsys: nfnl subsystem for which this message is to be parsed
145  * nlmsghdr: netlink message to be checked/parsed
146  * cda: array of pointers, needs to be at least subsys->attr_count big
147  *
148  */
149 static int
150 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
151                            struct nlmsghdr *nlh, struct nfattr *cda[])
152 {
153         int min_len;
154
155         memset(cda, 0, sizeof(struct nfattr *) * subsys->attr_count);
156
157         /* check attribute lengths. */
158         min_len = NLMSG_ALIGN(sizeof(struct nfgenmsg));
159         if (nlh->nlmsg_len < min_len)
160                 return -EINVAL;
161
162         if (nlh->nlmsg_len > min_len) {
163                 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
164                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
165
166                 while (NFA_OK(attr, attrlen)) {
167                         unsigned flavor = attr->nfa_type;
168                         if (flavor) {
169                                 if (flavor > subsys->attr_count)
170                                         return -EINVAL;
171                                 cda[flavor - 1] = attr;
172                         }
173                         attr = NFA_NEXT(attr, attrlen);
174                 }
175         } else
176                 return -EINVAL;
177
178         return 0;
179 }
180
181 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
182 {
183         int allocation = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
184         int err = 0;
185
186         NETLINK_CB(skb).dst_groups = group;
187         if (echo)
188                 atomic_inc(&skb->users);
189         netlink_broadcast(nfnl, skb, pid, group, allocation);
190         if (echo)
191                 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
192
193         return err;
194 }
195
196 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
197 {
198         return netlink_unicast(nfnl, skb, pid, flags);
199 }
200
201 /* Process one complete nfnetlink message. */
202 static inline int nfnetlink_rcv_msg(struct sk_buff *skb,
203                                     struct nlmsghdr *nlh, int *errp)
204 {
205         struct nfnl_callback *nc;
206         struct nfnetlink_subsystem *ss;
207         int type, err = 0;
208
209         DEBUGP("entered; subsys=%u, msgtype=%u\n",
210                  NFNL_SUBSYS_ID(nlh->nlmsg_type),
211                  NFNL_MSG_TYPE(nlh->nlmsg_type));
212
213         /* Only requests are handled by kernel now. */
214         if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
215                 DEBUGP("received non-request message\n");
216                 return 0;
217         }
218
219         /* All the messages must at least contain nfgenmsg */
220         if (nlh->nlmsg_len < 
221                         NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg)))) {
222                 DEBUGP("received message was too short\n");
223                 return 0;
224         }
225
226         type = nlh->nlmsg_type;
227         ss = nfnetlink_get_subsys(type);
228         if (!ss)
229                 goto err_inval;
230
231         nc = nfnetlink_find_client(type, ss);
232         if (!nc) {
233                 DEBUGP("unable to find client for type %d\n", type);
234                 goto err_inval;
235         }
236
237         if (nc->cap_required && 
238             !cap_raised(NETLINK_CB(skb).eff_cap, nc->cap_required)) {
239                 DEBUGP("permission denied for type %d\n", type);
240                 *errp = -EPERM;
241                 return -1;
242         }
243
244         {
245                 struct nfattr *cda[ss->attr_count];
246
247                 memset(cda, 0, ss->attr_count*sizeof(struct nfattr *));
248                 
249                 err = nfnetlink_check_attributes(ss, nlh, cda);
250                 if (err < 0)
251                         goto err_inval;
252
253                 err = nc->call(nfnl, skb, nlh, cda, errp);
254                 *errp = err;
255                 return err;
256         }
257
258 err_inval:
259         *errp = -EINVAL;
260         return -1;
261 }
262
263 /* Process one packet of messages. */
264 static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
265 {
266         int err;
267         struct nlmsghdr *nlh;
268
269         while (skb->len >= NLMSG_SPACE(0)) {
270                 u32 rlen;
271
272                 nlh = (struct nlmsghdr *)skb->data;
273                 if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
274                     || skb->len < nlh->nlmsg_len)
275                         return 0;
276                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
277                 if (rlen > skb->len)
278                         rlen = skb->len;
279                 if (nfnetlink_rcv_msg(skb, nlh, &err)) {
280                         if (!err)
281                                 return -1;
282                         netlink_ack(skb, nlh, err);
283                 } else
284                         if (nlh->nlmsg_flags & NLM_F_ACK)
285                                 netlink_ack(skb, nlh, 0);
286                 skb_pull(skb, rlen);
287         }
288
289         return 0;
290 }
291
292 static void nfnetlink_rcv(struct sock *sk, int len)
293 {
294         do {
295                 struct sk_buff *skb;
296
297                 if (nfnl_shlock_nowait())
298                         return;
299
300                 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
301                         if (nfnetlink_rcv_skb(skb)) {
302                                 if (skb->len)
303                                         skb_queue_head(&sk->sk_receive_queue,
304                                                        skb);
305                                 else
306                                         kfree_skb(skb);
307                                 break;
308                         }
309                         kfree_skb(skb);
310                 }
311
312                 up(&nfnl_sem);
313         } while(nfnl && nfnl->sk_receive_queue.qlen);
314 }
315
316 void __exit nfnetlink_exit(void)
317 {
318         printk("Removing netfilter NETLINK layer.\n");
319         sock_release(nfnl->sk_socket);
320         return;
321 }
322
323 int __init nfnetlink_init(void)
324 {
325         printk("Netfilter messages via NETLINK v%s.\n", nfversion);
326
327         nfnl = netlink_kernel_create(NETLINK_NETFILTER, nfnetlink_rcv);
328         if (!nfnl) {
329                 printk(KERN_ERR "cannot initialize nfnetlink!\n");
330                 return -1;
331         }
332
333         return 0;
334 }
335
336 module_init(nfnetlink_init);
337 module_exit(nfnetlink_exit);
338
339 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
340 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
341 EXPORT_SYMBOL_GPL(nfnetlink_send);
342 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
343 EXPORT_SYMBOL_GPL(nfattr_parse);
344 EXPORT_SYMBOL_GPL(__nfa_fill);