[NETNS]: CLONE_NEWNET don't use the same clone flag as the pid namespace.
[deliverable/linux.git] / net / netfilter / nfnetlink_queue.c
CommitLineData
7af4cc3f
HW
1/*
2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 *
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
20#include <linux/notifier.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
838ab636 23#include <linux/proc_fs.h>
7af4cc3f
HW
24#include <linux/netfilter_ipv4.h>
25#include <linux/netfilter_ipv6.h>
26#include <linux/netfilter/nfnetlink.h>
27#include <linux/netfilter/nfnetlink_queue.h>
28#include <linux/list.h>
29#include <net/sock.h>
30
31#include <asm/atomic.h>
32
fbcd923c
HW
33#ifdef CONFIG_BRIDGE_NETFILTER
34#include "../bridge/br_private.h"
35#endif
36
7af4cc3f
HW
37#define NFQNL_QMAX_DEFAULT 1024
38
39#if 0
40#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
41 __FILE__, __LINE__, __FUNCTION__, \
42 ## args)
43#else
44#define QDEBUG(x, ...)
45#endif
46
47struct nfqnl_queue_entry {
48 struct list_head list;
49 struct nf_info *info;
50 struct sk_buff *skb;
51 unsigned int id;
52};
53
54struct nfqnl_instance {
55 struct hlist_node hlist; /* global list of queues */
838ab636 56 atomic_t use;
7af4cc3f
HW
57
58 int peer_pid;
59 unsigned int queue_maxlen;
60 unsigned int copy_range;
61 unsigned int queue_total;
62 unsigned int queue_dropped;
63 unsigned int queue_user_dropped;
64
65 atomic_t id_sequence; /* 'sequence' of pkt ids */
66
67 u_int16_t queue_num; /* number of this queue */
68 u_int8_t copy_mode;
69
70 spinlock_t lock;
71
72 struct list_head queue_list; /* packets in queue */
73};
74
75typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
76
77static DEFINE_RWLOCK(instances_lock);
78
7af4cc3f
HW
79#define INSTANCE_BUCKETS 16
80static struct hlist_head instance_table[INSTANCE_BUCKETS];
81
82static inline u_int8_t instance_hashfn(u_int16_t queue_num)
83{
84 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
85}
86
87static struct nfqnl_instance *
88__instance_lookup(u_int16_t queue_num)
89{
90 struct hlist_head *head;
91 struct hlist_node *pos;
92 struct nfqnl_instance *inst;
93
94 head = &instance_table[instance_hashfn(queue_num)];
95 hlist_for_each_entry(inst, pos, head, hlist) {
96 if (inst->queue_num == queue_num)
97 return inst;
98 }
99 return NULL;
100}
101
102static struct nfqnl_instance *
838ab636 103instance_lookup_get(u_int16_t queue_num)
7af4cc3f
HW
104{
105 struct nfqnl_instance *inst;
106
107 read_lock_bh(&instances_lock);
108 inst = __instance_lookup(queue_num);
838ab636
HW
109 if (inst)
110 atomic_inc(&inst->use);
7af4cc3f
HW
111 read_unlock_bh(&instances_lock);
112
113 return inst;
114}
115
838ab636
HW
116static void
117instance_put(struct nfqnl_instance *inst)
118{
119 if (inst && atomic_dec_and_test(&inst->use)) {
120 QDEBUG("kfree(inst=%p)\n", inst);
121 kfree(inst);
122 }
123}
124
7af4cc3f
HW
125static struct nfqnl_instance *
126instance_create(u_int16_t queue_num, int pid)
127{
128 struct nfqnl_instance *inst;
129
130 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
131
601e68e1 132 write_lock_bh(&instances_lock);
7af4cc3f
HW
133 if (__instance_lookup(queue_num)) {
134 inst = NULL;
135 QDEBUG("aborting, instance already exists\n");
136 goto out_unlock;
137 }
138
10dfdc69 139 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
7af4cc3f
HW
140 if (!inst)
141 goto out_unlock;
142
7af4cc3f
HW
143 inst->queue_num = queue_num;
144 inst->peer_pid = pid;
145 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
146 inst->copy_range = 0xfffff;
147 inst->copy_mode = NFQNL_COPY_NONE;
148 atomic_set(&inst->id_sequence, 0);
838ab636
HW
149 /* needs to be two, since we _put() after creation */
150 atomic_set(&inst->use, 2);
181a46a5 151 spin_lock_init(&inst->lock);
7af4cc3f
HW
152 INIT_LIST_HEAD(&inst->queue_list);
153
154 if (!try_module_get(THIS_MODULE))
155 goto out_free;
156
601e68e1 157 hlist_add_head(&inst->hlist,
7af4cc3f
HW
158 &instance_table[instance_hashfn(queue_num)]);
159
160 write_unlock_bh(&instances_lock);
161
162 QDEBUG("successfully created new instance\n");
163
164 return inst;
165
166out_free:
167 kfree(inst);
168out_unlock:
169 write_unlock_bh(&instances_lock);
170 return NULL;
171}
172
173static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
174
175static void
176_instance_destroy2(struct nfqnl_instance *inst, int lock)
177{
178 /* first pull it out of the global list */
179 if (lock)
180 write_lock_bh(&instances_lock);
181
182 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
183 inst, inst->queue_num);
184 hlist_del(&inst->hlist);
185
186 if (lock)
187 write_unlock_bh(&instances_lock);
188
189 /* then flush all pending skbs from the queue */
190 nfqnl_flush(inst, NF_DROP);
191
838ab636
HW
192 /* and finally put the refcount */
193 instance_put(inst);
7af4cc3f
HW
194
195 module_put(THIS_MODULE);
196}
197
198static inline void
199__instance_destroy(struct nfqnl_instance *inst)
200{
201 _instance_destroy2(inst, 0);
202}
203
204static inline void
205instance_destroy(struct nfqnl_instance *inst)
206{
207 _instance_destroy2(inst, 1);
208}
209
210
211
212static void
213issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
214{
215 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
216
217 /* TCP input path (and probably other bits) assume to be called
218 * from softirq context, not from syscall, like issue_verdict is
219 * called. TCP input path deadlocks with locks taken from timer
220 * softirq, e.g. We therefore emulate this by local_bh_disable() */
221
222 local_bh_disable();
223 nf_reinject(entry->skb, entry->info, verdict);
224 local_bh_enable();
225
226 kfree(entry);
227}
228
229static inline void
230__enqueue_entry(struct nfqnl_instance *queue,
231 struct nfqnl_queue_entry *entry)
232{
233 list_add(&entry->list, &queue->queue_list);
234 queue->queue_total++;
235}
236
237/*
238 * Find and return a queued entry matched by cmpfn, or return the last
239 * entry if cmpfn is NULL.
240 */
241static inline struct nfqnl_queue_entry *
601e68e1 242__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
7af4cc3f
HW
243 unsigned long data)
244{
245 struct list_head *p;
246
247 list_for_each_prev(p, &queue->queue_list) {
248 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
601e68e1 249
7af4cc3f
HW
250 if (!cmpfn || cmpfn(entry, data))
251 return entry;
252 }
253 return NULL;
254}
255
256static inline void
257__dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
258{
259 list_del(&entry->list);
260 q->queue_total--;
261}
262
263static inline struct nfqnl_queue_entry *
264__find_dequeue_entry(struct nfqnl_instance *queue,
265 nfqnl_cmpfn cmpfn, unsigned long data)
266{
267 struct nfqnl_queue_entry *entry;
268
269 entry = __find_entry(queue, cmpfn, data);
270 if (entry == NULL)
271 return NULL;
272
273 __dequeue_entry(queue, entry);
274 return entry;
275}
276
277
278static inline void
279__nfqnl_flush(struct nfqnl_instance *queue, int verdict)
280{
281 struct nfqnl_queue_entry *entry;
601e68e1 282
7af4cc3f
HW
283 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
284 issue_verdict(entry, verdict);
285}
286
287static inline int
288__nfqnl_set_mode(struct nfqnl_instance *queue,
289 unsigned char mode, unsigned int range)
290{
291 int status = 0;
601e68e1 292
7af4cc3f
HW
293 switch (mode) {
294 case NFQNL_COPY_NONE:
295 case NFQNL_COPY_META:
296 queue->copy_mode = mode;
297 queue->copy_range = 0;
298 break;
601e68e1 299
7af4cc3f
HW
300 case NFQNL_COPY_PACKET:
301 queue->copy_mode = mode;
302 /* we're using struct nfattr which has 16bit nfa_len */
303 if (range > 0xffff)
304 queue->copy_range = 0xffff;
305 else
306 queue->copy_range = range;
307 break;
601e68e1 308
7af4cc3f
HW
309 default:
310 status = -EINVAL;
311
312 }
313 return status;
314}
315
316static struct nfqnl_queue_entry *
317find_dequeue_entry(struct nfqnl_instance *queue,
318 nfqnl_cmpfn cmpfn, unsigned long data)
319{
320 struct nfqnl_queue_entry *entry;
601e68e1 321
7af4cc3f
HW
322 spin_lock_bh(&queue->lock);
323 entry = __find_dequeue_entry(queue, cmpfn, data);
324 spin_unlock_bh(&queue->lock);
325
326 return entry;
327}
328
329static void
330nfqnl_flush(struct nfqnl_instance *queue, int verdict)
331{
332 spin_lock_bh(&queue->lock);
333 __nfqnl_flush(queue, verdict);
334 spin_unlock_bh(&queue->lock);
335}
336
337static struct sk_buff *
338nfqnl_build_packet_message(struct nfqnl_instance *queue,
339 struct nfqnl_queue_entry *entry, int *errp)
340{
27a884dc 341 sk_buff_data_t old_tail;
7af4cc3f
HW
342 size_t size;
343 size_t data_len = 0;
344 struct sk_buff *skb;
345 struct nfqnl_msg_packet_hdr pmsg;
346 struct nlmsghdr *nlh;
347 struct nfgenmsg *nfmsg;
3e4ead4f
JJ
348 struct nf_info *entinf = entry->info;
349 struct sk_buff *entskb = entry->skb;
350 struct net_device *indev;
351 struct net_device *outdev;
98a4a861 352 __be32 tmp_uint;
7af4cc3f
HW
353
354 QDEBUG("entered\n");
355
356 /* all macros expand to constant values at compile time */
f0d83583
PNA
357 size = NLMSG_SPACE(sizeof(struct nfgenmsg)) +
358 + NFA_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
359 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
360 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
fbcd923c 361#ifdef CONFIG_BRIDGE_NETFILTER
f0d83583
PNA
362 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
363 + NFA_SPACE(sizeof(u_int32_t)) /* ifindex */
fbcd923c 364#endif
f0d83583
PNA
365 + NFA_SPACE(sizeof(u_int32_t)) /* mark */
366 + NFA_SPACE(sizeof(struct nfqnl_msg_packet_hw))
367 + NFA_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
7af4cc3f 368
3e4ead4f
JJ
369 outdev = entinf->outdev;
370
7af4cc3f 371 spin_lock_bh(&queue->lock);
601e68e1 372
7af4cc3f
HW
373 switch (queue->copy_mode) {
374 case NFQNL_COPY_META:
375 case NFQNL_COPY_NONE:
376 data_len = 0;
377 break;
601e68e1 378
7af4cc3f 379 case NFQNL_COPY_PACKET:
84fa7933
PM
380 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
381 entskb->ip_summed == CHECKSUM_COMPLETE) &&
382 (*errp = skb_checksum_help(entskb))) {
e7dfb09a
PM
383 spin_unlock_bh(&queue->lock);
384 return NULL;
385 }
601e68e1 386 if (queue->copy_range == 0
3e4ead4f
JJ
387 || queue->copy_range > entskb->len)
388 data_len = entskb->len;
7af4cc3f
HW
389 else
390 data_len = queue->copy_range;
601e68e1 391
f0d83583 392 size += NFA_SPACE(data_len);
7af4cc3f 393 break;
601e68e1 394
7af4cc3f
HW
395 default:
396 *errp = -EINVAL;
397 spin_unlock_bh(&queue->lock);
398 return NULL;
399 }
400
401 spin_unlock_bh(&queue->lock);
402
403 skb = alloc_skb(size, GFP_ATOMIC);
404 if (!skb)
405 goto nlmsg_failure;
601e68e1 406
27a884dc 407 old_tail = skb->tail;
601e68e1 408 nlh = NLMSG_PUT(skb, 0, 0,
7af4cc3f
HW
409 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
410 sizeof(struct nfgenmsg));
411 nfmsg = NLMSG_DATA(nlh);
3e4ead4f 412 nfmsg->nfgen_family = entinf->pf;
7af4cc3f
HW
413 nfmsg->version = NFNETLINK_V0;
414 nfmsg->res_id = htons(queue->queue_num);
415
416 pmsg.packet_id = htonl(entry->id);
febf0a43 417 pmsg.hw_protocol = entskb->protocol;
3e4ead4f 418 pmsg.hook = entinf->hook;
7af4cc3f
HW
419
420 NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
421
3e4ead4f
JJ
422 indev = entinf->indev;
423 if (indev) {
424 tmp_uint = htonl(indev->ifindex);
fbcd923c 425#ifndef CONFIG_BRIDGE_NETFILTER
7af4cc3f 426 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
fbcd923c 427#else
3e4ead4f 428 if (entinf->pf == PF_BRIDGE) {
fbcd923c 429 /* Case 1: indev is physical input device, we need to
601e68e1 430 * look for bridge group (when called from
fbcd923c 431 * netfilter_bridge) */
601e68e1 432 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
fbcd923c
HW
433 &tmp_uint);
434 /* this is the bridge group "brX" */
3e4ead4f 435 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
fbcd923c
HW
436 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
437 &tmp_uint);
438 } else {
439 /* Case 2: indev is bridge group, we need to look for
440 * physical device (when called from ipv4) */
441 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
442 &tmp_uint);
3e4ead4f
JJ
443 if (entskb->nf_bridge
444 && entskb->nf_bridge->physindev) {
445 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
fbcd923c
HW
446 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
447 sizeof(tmp_uint), &tmp_uint);
448 }
449 }
450#endif
7af4cc3f
HW
451 }
452
3e4ead4f
JJ
453 if (outdev) {
454 tmp_uint = htonl(outdev->ifindex);
fbcd923c 455#ifndef CONFIG_BRIDGE_NETFILTER
7af4cc3f 456 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
fbcd923c 457#else
3e4ead4f 458 if (entinf->pf == PF_BRIDGE) {
fbcd923c 459 /* Case 1: outdev is physical output device, we need to
601e68e1 460 * look for bridge group (when called from
fbcd923c
HW
461 * netfilter_bridge) */
462 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
463 &tmp_uint);
464 /* this is the bridge group "brX" */
3e4ead4f 465 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
fbcd923c
HW
466 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
467 &tmp_uint);
468 } else {
469 /* Case 2: outdev is bridge group, we need to look for
470 * physical output device (when called from ipv4) */
471 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
472 &tmp_uint);
3e4ead4f
JJ
473 if (entskb->nf_bridge
474 && entskb->nf_bridge->physoutdev) {
475 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
fbcd923c
HW
476 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
477 sizeof(tmp_uint), &tmp_uint);
478 }
479 }
480#endif
7af4cc3f
HW
481 }
482
82e91ffe
TG
483 if (entskb->mark) {
484 tmp_uint = htonl(entskb->mark);
7af4cc3f
HW
485 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
486 }
487
b95cce35 488 if (indev && entskb->dev) {
7af4cc3f 489 struct nfqnl_msg_packet_hw phw;
b95cce35
SH
490 int len = dev_parse_header(entskb, phw.hw_addr);
491 if (len) {
492 phw.hw_addrlen = htons(len);
493 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
494 }
7af4cc3f
HW
495 }
496
b7aa0bf7 497 if (entskb->tstamp.tv64) {
7af4cc3f 498 struct nfqnl_msg_packet_timestamp ts;
b7aa0bf7
ED
499 struct timeval tv = ktime_to_timeval(entskb->tstamp);
500 ts.sec = cpu_to_be64(tv.tv_sec);
501 ts.usec = cpu_to_be64(tv.tv_usec);
7af4cc3f
HW
502
503 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
504 }
505
506 if (data_len) {
507 struct nfattr *nfa;
508 int size = NFA_LENGTH(data_len);
509
510 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
511 printk(KERN_WARNING "nf_queue: no tailroom!\n");
512 goto nlmsg_failure;
513 }
514
515 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
516 nfa->nfa_type = NFQA_PAYLOAD;
517 nfa->nfa_len = size;
518
3e4ead4f 519 if (skb_copy_bits(entskb, 0, NFA_DATA(nfa), data_len))
7af4cc3f
HW
520 BUG();
521 }
601e68e1 522
7af4cc3f
HW
523 nlh->nlmsg_len = skb->tail - old_tail;
524 return skb;
525
526nlmsg_failure:
527nfattr_failure:
528 if (skb)
529 kfree_skb(skb);
530 *errp = -EINVAL;
531 if (net_ratelimit())
532 printk(KERN_ERR "nf_queue: error creating packet message\n");
533 return NULL;
534}
535
536static int
601e68e1 537nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
7af4cc3f
HW
538 unsigned int queuenum, void *data)
539{
540 int status = -EINVAL;
541 struct sk_buff *nskb;
542 struct nfqnl_instance *queue;
543 struct nfqnl_queue_entry *entry;
544
545 QDEBUG("entered\n");
546
838ab636 547 queue = instance_lookup_get(queuenum);
7af4cc3f
HW
548 if (!queue) {
549 QDEBUG("no queue instance matching\n");
550 return -EINVAL;
551 }
552
553 if (queue->copy_mode == NFQNL_COPY_NONE) {
554 QDEBUG("mode COPY_NONE, aborting\n");
838ab636
HW
555 status = -EAGAIN;
556 goto err_out_put;
7af4cc3f
HW
557 }
558
559 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
560 if (entry == NULL) {
561 if (net_ratelimit())
601e68e1 562 printk(KERN_ERR
7af4cc3f 563 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
838ab636
HW
564 status = -ENOMEM;
565 goto err_out_put;
7af4cc3f
HW
566 }
567
568 entry->info = info;
569 entry->skb = skb;
570 entry->id = atomic_inc_return(&queue->id_sequence);
571
572 nskb = nfqnl_build_packet_message(queue, entry, &status);
573 if (nskb == NULL)
574 goto err_out_free;
601e68e1 575
7af4cc3f 576 spin_lock_bh(&queue->lock);
601e68e1 577
7af4cc3f 578 if (!queue->peer_pid)
601e68e1 579 goto err_out_free_nskb;
7af4cc3f
HW
580
581 if (queue->queue_total >= queue->queue_maxlen) {
601e68e1 582 queue->queue_dropped++;
7af4cc3f
HW
583 status = -ENOSPC;
584 if (net_ratelimit())
601e68e1
YH
585 printk(KERN_WARNING "nf_queue: full at %d entries, "
586 "dropping packets(s). Dropped: %d\n",
7af4cc3f
HW
587 queue->queue_total, queue->queue_dropped);
588 goto err_out_free_nskb;
589 }
590
591 /* nfnetlink_unicast will either free the nskb or add it to a socket */
592 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
593 if (status < 0) {
601e68e1 594 queue->queue_user_dropped++;
7af4cc3f
HW
595 goto err_out_unlock;
596 }
597
598 __enqueue_entry(queue, entry);
599
600 spin_unlock_bh(&queue->lock);
838ab636 601 instance_put(queue);
7af4cc3f
HW
602 return status;
603
604err_out_free_nskb:
601e68e1
YH
605 kfree_skb(nskb);
606
7af4cc3f
HW
607err_out_unlock:
608 spin_unlock_bh(&queue->lock);
609
610err_out_free:
611 kfree(entry);
838ab636
HW
612err_out_put:
613 instance_put(queue);
7af4cc3f
HW
614 return status;
615}
616
617static int
618nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
619{
620 int diff;
621
622 diff = data_len - e->skb->len;
d8a585d7
PM
623 if (diff < 0) {
624 if (pskb_trim(e->skb, data_len))
625 return -ENOMEM;
626 } else if (diff > 0) {
7af4cc3f
HW
627 if (data_len > 0xFFFF)
628 return -EINVAL;
629 if (diff > skb_tailroom(e->skb)) {
630 struct sk_buff *newskb;
601e68e1 631
7af4cc3f 632 newskb = skb_copy_expand(e->skb,
601e68e1
YH
633 skb_headroom(e->skb),
634 diff,
635 GFP_ATOMIC);
7af4cc3f 636 if (newskb == NULL) {
1158ba27 637 printk(KERN_WARNING "nf_queue: OOM "
7af4cc3f
HW
638 "in mangle, dropping packet\n");
639 return -ENOMEM;
640 }
641 if (e->skb->sk)
642 skb_set_owner_w(newskb, e->skb->sk);
643 kfree_skb(e->skb);
644 e->skb = newskb;
645 }
646 skb_put(e->skb, diff);
647 }
648 if (!skb_make_writable(&e->skb, data_len))
649 return -ENOMEM;
27d7ff46 650 skb_copy_to_linear_data(e->skb, data, data_len);
e7dfb09a 651 e->skb->ip_summed = CHECKSUM_NONE;
7af4cc3f
HW
652 return 0;
653}
654
655static inline int
656id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
657{
658 return (id == e->id);
659}
660
661static int
662nfqnl_set_mode(struct nfqnl_instance *queue,
663 unsigned char mode, unsigned int range)
664{
665 int status;
666
667 spin_lock_bh(&queue->lock);
668 status = __nfqnl_set_mode(queue, mode, range);
669 spin_unlock_bh(&queue->lock);
670
671 return status;
672}
673
674static int
675dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
676{
3e4ead4f 677 struct nf_info *entinf = entry->info;
601e68e1 678
3e4ead4f
JJ
679 if (entinf->indev)
680 if (entinf->indev->ifindex == ifindex)
7af4cc3f 681 return 1;
3e4ead4f
JJ
682 if (entinf->outdev)
683 if (entinf->outdev->ifindex == ifindex)
7af4cc3f 684 return 1;
ef47c6a7
PM
685#ifdef CONFIG_BRIDGE_NETFILTER
686 if (entry->skb->nf_bridge) {
687 if (entry->skb->nf_bridge->physindev &&
688 entry->skb->nf_bridge->physindev->ifindex == ifindex)
689 return 1;
690 if (entry->skb->nf_bridge->physoutdev &&
691 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
692 return 1;
693 }
694#endif
7af4cc3f
HW
695 return 0;
696}
697
698/* drop all packets with either indev or outdev == ifindex from all queue
699 * instances */
700static void
701nfqnl_dev_drop(int ifindex)
702{
703 int i;
601e68e1 704
7af4cc3f
HW
705 QDEBUG("entering for ifindex %u\n", ifindex);
706
707 /* this only looks like we have to hold the readlock for a way too long
708 * time, issue_verdict(), nf_reinject(), ... - but we always only
709 * issue NF_DROP, which is processed directly in nf_reinject() */
710 read_lock_bh(&instances_lock);
711
712 for (i = 0; i < INSTANCE_BUCKETS; i++) {
713 struct hlist_node *tmp;
714 struct nfqnl_instance *inst;
715 struct hlist_head *head = &instance_table[i];
716
717 hlist_for_each_entry(inst, tmp, head, hlist) {
718 struct nfqnl_queue_entry *entry;
601e68e1 719 while ((entry = find_dequeue_entry(inst, dev_cmp,
7af4cc3f
HW
720 ifindex)) != NULL)
721 issue_verdict(entry, NF_DROP);
722 }
723 }
724
725 read_unlock_bh(&instances_lock);
726}
727
728#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
729
730static int
731nfqnl_rcv_dev_event(struct notifier_block *this,
732 unsigned long event, void *ptr)
733{
734 struct net_device *dev = ptr;
735
e9dc8653
EB
736 if (dev->nd_net != &init_net)
737 return NOTIFY_DONE;
738
7af4cc3f
HW
739 /* Drop any packets associated with the downed device */
740 if (event == NETDEV_DOWN)
741 nfqnl_dev_drop(dev->ifindex);
742 return NOTIFY_DONE;
743}
744
745static struct notifier_block nfqnl_dev_notifier = {
746 .notifier_call = nfqnl_rcv_dev_event,
747};
748
749static int
750nfqnl_rcv_nl_event(struct notifier_block *this,
751 unsigned long event, void *ptr)
752{
753 struct netlink_notify *n = ptr;
754
755 if (event == NETLINK_URELEASE &&
756 n->protocol == NETLINK_NETFILTER && n->pid) {
757 int i;
758
759 /* destroy all instances for this pid */
760 write_lock_bh(&instances_lock);
761 for (i = 0; i < INSTANCE_BUCKETS; i++) {
762 struct hlist_node *tmp, *t2;
763 struct nfqnl_instance *inst;
764 struct hlist_head *head = &instance_table[i];
765
766 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
b4b51029
EB
767 if ((n->net == &init_net) &&
768 (n->pid == inst->peer_pid))
7af4cc3f
HW
769 __instance_destroy(inst);
770 }
771 }
772 write_unlock_bh(&instances_lock);
773 }
774 return NOTIFY_DONE;
775}
776
777static struct notifier_block nfqnl_rtnl_notifier = {
778 .notifier_call = nfqnl_rcv_nl_event,
779};
780
838ab636
HW
781static const int nfqa_verdict_min[NFQA_MAX] = {
782 [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr),
783 [NFQA_MARK-1] = sizeof(u_int32_t),
784 [NFQA_PAYLOAD-1] = 0,
785};
786
7af4cc3f
HW
787static int
788nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 789 struct nlmsghdr *nlh, struct nfattr *nfqa[])
7af4cc3f
HW
790{
791 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
792 u_int16_t queue_num = ntohs(nfmsg->res_id);
793
794 struct nfqnl_msg_verdict_hdr *vhdr;
795 struct nfqnl_instance *queue;
796 unsigned int verdict;
797 struct nfqnl_queue_entry *entry;
838ab636 798 int err;
7af4cc3f 799
838ab636
HW
800 if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
801 QDEBUG("bad attribute size\n");
802 return -EINVAL;
803 }
804
805 queue = instance_lookup_get(queue_num);
7af4cc3f
HW
806 if (!queue)
807 return -ENODEV;
808
838ab636
HW
809 if (queue->peer_pid != NETLINK_CB(skb).pid) {
810 err = -EPERM;
811 goto err_out_put;
812 }
7af4cc3f 813
838ab636
HW
814 if (!nfqa[NFQA_VERDICT_HDR-1]) {
815 err = -EINVAL;
816 goto err_out_put;
817 }
7af4cc3f
HW
818
819 vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
820 verdict = ntohl(vhdr->verdict);
821
838ab636
HW
822 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
823 err = -EINVAL;
824 goto err_out_put;
825 }
7af4cc3f
HW
826
827 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
838ab636
HW
828 if (entry == NULL) {
829 err = -ENOENT;
830 goto err_out_put;
831 }
7af4cc3f
HW
832
833 if (nfqa[NFQA_PAYLOAD-1]) {
834 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
835 NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
836 verdict = NF_DROP;
837 }
838
839 if (nfqa[NFQA_MARK-1])
82e91ffe 840 entry->skb->mark = ntohl(*(__be32 *)
601e68e1
YH
841 NFA_DATA(nfqa[NFQA_MARK-1]));
842
7af4cc3f 843 issue_verdict(entry, verdict);
838ab636 844 instance_put(queue);
7af4cc3f 845 return 0;
838ab636
HW
846
847err_out_put:
848 instance_put(queue);
849 return err;
7af4cc3f
HW
850}
851
852static int
853nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 854 struct nlmsghdr *nlh, struct nfattr *nfqa[])
7af4cc3f
HW
855{
856 return -ENOTSUPP;
857}
858
838ab636
HW
859static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
860 [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd),
861 [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params),
862};
863
bbd86b9f
HW
864static struct nf_queue_handler nfqh = {
865 .name = "nf_queue",
866 .outfn = &nfqnl_enqueue_packet,
867};
868
7af4cc3f
HW
869static int
870nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
1d00a4eb 871 struct nlmsghdr *nlh, struct nfattr *nfqa[])
7af4cc3f
HW
872{
873 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
874 u_int16_t queue_num = ntohs(nfmsg->res_id);
875 struct nfqnl_instance *queue;
838ab636 876 int ret = 0;
7af4cc3f
HW
877
878 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
879
838ab636
HW
880 if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
881 QDEBUG("bad attribute size\n");
882 return -EINVAL;
883 }
884
885 queue = instance_lookup_get(queue_num);
7af4cc3f
HW
886 if (nfqa[NFQA_CFG_CMD-1]) {
887 struct nfqnl_msg_config_cmd *cmd;
888 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
889 QDEBUG("found CFG_CMD\n");
890
891 switch (cmd->command) {
892 case NFQNL_CFG_CMD_BIND:
893 if (queue)
894 return -EBUSY;
895
896 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
897 if (!queue)
898 return -EINVAL;
899 break;
900 case NFQNL_CFG_CMD_UNBIND:
901 if (!queue)
902 return -ENODEV;
903
838ab636
HW
904 if (queue->peer_pid != NETLINK_CB(skb).pid) {
905 ret = -EPERM;
906 goto out_put;
907 }
7af4cc3f
HW
908
909 instance_destroy(queue);
910 break;
911 case NFQNL_CFG_CMD_PF_BIND:
912 QDEBUG("registering queue handler for pf=%u\n",
913 ntohs(cmd->pf));
bbd86b9f 914 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
7af4cc3f
HW
915 break;
916 case NFQNL_CFG_CMD_PF_UNBIND:
917 QDEBUG("unregistering queue handler for pf=%u\n",
918 ntohs(cmd->pf));
ce7663d8 919 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
7af4cc3f
HW
920 break;
921 default:
838ab636
HW
922 ret = -EINVAL;
923 break;
7af4cc3f
HW
924 }
925 } else {
926 if (!queue) {
927 QDEBUG("no config command, and no instance ENOENT\n");
838ab636
HW
928 ret = -ENOENT;
929 goto out_put;
7af4cc3f
HW
930 }
931
932 if (queue->peer_pid != NETLINK_CB(skb).pid) {
933 QDEBUG("no config command, and wrong pid\n");
838ab636
HW
934 ret = -EPERM;
935 goto out_put;
7af4cc3f
HW
936 }
937 }
938
939 if (nfqa[NFQA_CFG_PARAMS-1]) {
940 struct nfqnl_msg_config_params *params;
7af4cc3f 941
406dbfc9
PM
942 if (!queue) {
943 ret = -ENOENT;
944 goto out_put;
945 }
946 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
7af4cc3f
HW
947 nfqnl_set_mode(queue, params->copy_mode,
948 ntohl(params->copy_range));
949 }
950
829e17a1
EL
951 if (nfqa[NFQA_CFG_QUEUE_MAXLEN-1]) {
952 __be32 *queue_maxlen;
953 queue_maxlen = NFA_DATA(nfqa[NFQA_CFG_QUEUE_MAXLEN-1]);
954 spin_lock_bh(&queue->lock);
955 queue->queue_maxlen = ntohl(*queue_maxlen);
956 spin_unlock_bh(&queue->lock);
957 }
958
838ab636
HW
959out_put:
960 instance_put(queue);
961 return ret;
7af4cc3f
HW
962}
963
964static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
965 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
37d2e7a2 966 .attr_count = NFQA_MAX, },
7af4cc3f 967 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
37d2e7a2 968 .attr_count = NFQA_MAX, },
7af4cc3f 969 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
37d2e7a2 970 .attr_count = NFQA_CFG_MAX, },
7af4cc3f
HW
971};
972
973static struct nfnetlink_subsystem nfqnl_subsys = {
974 .name = "nf_queue",
975 .subsys_id = NFNL_SUBSYS_QUEUE,
976 .cb_count = NFQNL_MSG_MAX,
7af4cc3f
HW
977 .cb = nfqnl_cb,
978};
979
838ab636
HW
980#ifdef CONFIG_PROC_FS
981struct iter_state {
982 unsigned int bucket;
983};
984
985static struct hlist_node *get_first(struct seq_file *seq)
986{
987 struct iter_state *st = seq->private;
988
989 if (!st)
990 return NULL;
991
992 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
993 if (!hlist_empty(&instance_table[st->bucket]))
994 return instance_table[st->bucket].first;
995 }
996 return NULL;
997}
998
999static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1000{
1001 struct iter_state *st = seq->private;
1002
1003 h = h->next;
1004 while (!h) {
1005 if (++st->bucket >= INSTANCE_BUCKETS)
1006 return NULL;
1007
1008 h = instance_table[st->bucket].first;
1009 }
1010 return h;
1011}
1012
1013static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1014{
1015 struct hlist_node *head;
1016 head = get_first(seq);
1017
1018 if (head)
1019 while (pos && (head = get_next(seq, head)))
1020 pos--;
1021 return pos ? NULL : head;
1022}
1023
1024static void *seq_start(struct seq_file *seq, loff_t *pos)
1025{
1026 read_lock_bh(&instances_lock);
1027 return get_idx(seq, *pos);
1028}
1029
1030static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1031{
1032 (*pos)++;
1033 return get_next(s, v);
1034}
1035
1036static void seq_stop(struct seq_file *s, void *v)
1037{
1038 read_unlock_bh(&instances_lock);
1039}
1040
1041static int seq_show(struct seq_file *s, void *v)
1042{
1043 const struct nfqnl_instance *inst = v;
1044
1045 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1046 inst->queue_num,
1047 inst->peer_pid, inst->queue_total,
1048 inst->copy_mode, inst->copy_range,
1049 inst->queue_dropped, inst->queue_user_dropped,
1050 atomic_read(&inst->id_sequence),
1051 atomic_read(&inst->use));
1052}
1053
56b3d975 1054static const struct seq_operations nfqnl_seq_ops = {
838ab636
HW
1055 .start = seq_start,
1056 .next = seq_next,
1057 .stop = seq_stop,
1058 .show = seq_show,
1059};
1060
1061static int nfqnl_open(struct inode *inode, struct file *file)
1062{
1063 struct seq_file *seq;
1064 struct iter_state *is;
1065 int ret;
1066
10dfdc69 1067 is = kzalloc(sizeof(*is), GFP_KERNEL);
838ab636
HW
1068 if (!is)
1069 return -ENOMEM;
838ab636
HW
1070 ret = seq_open(file, &nfqnl_seq_ops);
1071 if (ret < 0)
1072 goto out_free;
1073 seq = file->private_data;
1074 seq->private = is;
1075 return ret;
1076out_free:
1077 kfree(is);
1078 return ret;
1079}
1080
da7071d7 1081static const struct file_operations nfqnl_file_ops = {
838ab636
HW
1082 .owner = THIS_MODULE,
1083 .open = nfqnl_open,
1084 .read = seq_read,
1085 .llseek = seq_lseek,
1086 .release = seq_release_private,
1087};
1088
1089#endif /* PROC_FS */
1090
32292a7f 1091static int __init nfnetlink_queue_init(void)
7af4cc3f 1092{
838ab636
HW
1093 int i, status = -ENOMEM;
1094#ifdef CONFIG_PROC_FS
1095 struct proc_dir_entry *proc_nfqueue;
1096#endif
601e68e1 1097
838ab636
HW
1098 for (i = 0; i < INSTANCE_BUCKETS; i++)
1099 INIT_HLIST_HEAD(&instance_table[i]);
1100
7af4cc3f
HW
1101 netlink_register_notifier(&nfqnl_rtnl_notifier);
1102 status = nfnetlink_subsys_register(&nfqnl_subsys);
1103 if (status < 0) {
1104 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1105 goto cleanup_netlink_notifier;
1106 }
1107
838ab636
HW
1108#ifdef CONFIG_PROC_FS
1109 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1110 proc_net_netfilter);
1111 if (!proc_nfqueue)
1112 goto cleanup_subsys;
1113 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1114#endif
1115
7af4cc3f
HW
1116 register_netdevice_notifier(&nfqnl_dev_notifier);
1117 return status;
1118
838ab636
HW
1119#ifdef CONFIG_PROC_FS
1120cleanup_subsys:
7af4cc3f 1121 nfnetlink_subsys_unregister(&nfqnl_subsys);
32292a7f 1122#endif
7af4cc3f
HW
1123cleanup_netlink_notifier:
1124 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1125 return status;
1126}
1127
65b4b4e8 1128static void __exit nfnetlink_queue_fini(void)
7af4cc3f 1129{
32292a7f
PM
1130 nf_unregister_queue_handlers(&nfqh);
1131 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1132#ifdef CONFIG_PROC_FS
1133 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1134#endif
1135 nfnetlink_subsys_unregister(&nfqnl_subsys);
1136 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
7af4cc3f
HW
1137}
1138
1139MODULE_DESCRIPTION("netfilter packet queue handler");
1140MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1141MODULE_LICENSE("GPL");
1142MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1143
65b4b4e8
AM
1144module_init(nfnetlink_queue_init);
1145module_exit(nfnetlink_queue_fini);
This page took 0.363768 seconds and 5 git commands to generate.