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