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