Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / netfilter / nfnetlink_queue.c
blob22ef6545802857406bdb4b484950da5c1794e965
1 /*
2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 * (C) 2007 by Patrick McHardy <kaber@trash.net>
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>
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.
17 #include <linux/module.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/notifier.h>
22 #include <linux/netdevice.h>
23 #include <linux/netfilter.h>
24 #include <linux/proc_fs.h>
25 #include <linux/netfilter_ipv4.h>
26 #include <linux/netfilter_ipv6.h>
27 #include <linux/netfilter/nfnetlink.h>
28 #include <linux/netfilter/nfnetlink_queue.h>
29 #include <linux/list.h>
30 #include <net/sock.h>
31 #include <net/netfilter/nf_queue.h>
33 #include <asm/atomic.h>
35 #ifdef CONFIG_BRIDGE_NETFILTER
36 #include "../bridge/br_private.h"
37 #endif
39 #define NFQNL_QMAX_DEFAULT 1024
41 struct nfqnl_instance {
42 struct hlist_node hlist; /* global list of queues */
43 struct rcu_head rcu;
45 int peer_pid;
46 unsigned int queue_maxlen;
47 unsigned int copy_range;
48 unsigned int queue_total;
49 unsigned int queue_dropped;
50 unsigned int queue_user_dropped;
52 unsigned int id_sequence; /* 'sequence' of pkt ids */
54 u_int16_t queue_num; /* number of this queue */
55 u_int8_t copy_mode;
57 spinlock_t lock;
59 struct list_head queue_list; /* packets in queue */
62 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
64 static DEFINE_SPINLOCK(instances_lock);
66 #define INSTANCE_BUCKETS 16
67 static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
69 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
71 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
74 static struct nfqnl_instance *
75 instance_lookup(u_int16_t queue_num)
77 struct hlist_head *head;
78 struct hlist_node *pos;
79 struct nfqnl_instance *inst;
81 head = &instance_table[instance_hashfn(queue_num)];
82 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
83 if (inst->queue_num == queue_num)
84 return inst;
86 return NULL;
89 static struct nfqnl_instance *
90 instance_create(u_int16_t queue_num, int pid)
92 struct nfqnl_instance *inst;
93 unsigned int h;
94 int err;
96 spin_lock(&instances_lock);
97 if (instance_lookup(queue_num)) {
98 err = -EEXIST;
99 goto out_unlock;
102 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
103 if (!inst) {
104 err = -ENOMEM;
105 goto out_unlock;
108 inst->queue_num = queue_num;
109 inst->peer_pid = pid;
110 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
111 inst->copy_range = 0xfffff;
112 inst->copy_mode = NFQNL_COPY_NONE;
113 spin_lock_init(&inst->lock);
114 INIT_LIST_HEAD(&inst->queue_list);
115 INIT_RCU_HEAD(&inst->rcu);
117 if (!try_module_get(THIS_MODULE)) {
118 err = -EAGAIN;
119 goto out_free;
122 h = instance_hashfn(queue_num);
123 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
125 spin_unlock(&instances_lock);
127 return inst;
129 out_free:
130 kfree(inst);
131 out_unlock:
132 spin_unlock(&instances_lock);
133 return ERR_PTR(err);
136 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
137 unsigned long data);
139 static void
140 instance_destroy_rcu(struct rcu_head *head)
142 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
143 rcu);
145 nfqnl_flush(inst, NULL, 0);
146 kfree(inst);
147 module_put(THIS_MODULE);
150 static void
151 __instance_destroy(struct nfqnl_instance *inst)
153 hlist_del_rcu(&inst->hlist);
154 call_rcu(&inst->rcu, instance_destroy_rcu);
157 static void
158 instance_destroy(struct nfqnl_instance *inst)
160 spin_lock(&instances_lock);
161 __instance_destroy(inst);
162 spin_unlock(&instances_lock);
165 static inline void
166 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
168 list_add_tail(&entry->list, &queue->queue_list);
169 queue->queue_total++;
172 static struct nf_queue_entry *
173 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
175 struct nf_queue_entry *entry = NULL, *i;
177 spin_lock_bh(&queue->lock);
179 list_for_each_entry(i, &queue->queue_list, list) {
180 if (i->id == id) {
181 entry = i;
182 break;
186 if (entry) {
187 list_del(&entry->list);
188 queue->queue_total--;
191 spin_unlock_bh(&queue->lock);
193 return entry;
196 static void
197 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
199 struct nf_queue_entry *entry, *next;
201 spin_lock_bh(&queue->lock);
202 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
203 if (!cmpfn || cmpfn(entry, data)) {
204 list_del(&entry->list);
205 queue->queue_total--;
206 nf_reinject(entry, NF_DROP);
209 spin_unlock_bh(&queue->lock);
212 static struct sk_buff *
213 nfqnl_build_packet_message(struct nfqnl_instance *queue,
214 struct nf_queue_entry *entry)
216 sk_buff_data_t old_tail;
217 size_t size;
218 size_t data_len = 0;
219 struct sk_buff *skb;
220 struct nfqnl_msg_packet_hdr pmsg;
221 struct nlmsghdr *nlh;
222 struct nfgenmsg *nfmsg;
223 struct sk_buff *entskb = entry->skb;
224 struct net_device *indev;
225 struct net_device *outdev;
227 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
228 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
229 =======
230 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
231 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
232 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
233 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
234 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
235 #ifdef CONFIG_BRIDGE_NETFILTER
236 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
237 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
238 #endif
239 + nla_total_size(sizeof(u_int32_t)) /* mark */
240 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
241 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
243 outdev = entry->outdev;
245 spin_lock_bh(&queue->lock);
247 switch ((enum nfqnl_config_mode)queue->copy_mode) {
248 case NFQNL_COPY_META:
249 case NFQNL_COPY_NONE:
250 data_len = 0;
251 break;
253 case NFQNL_COPY_PACKET:
254 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
255 entskb->ip_summed == CHECKSUM_COMPLETE) &&
256 skb_checksum_help(entskb)) {
257 spin_unlock_bh(&queue->lock);
258 return NULL;
260 if (queue->copy_range == 0
261 || queue->copy_range > entskb->len)
262 data_len = entskb->len;
263 else
264 data_len = queue->copy_range;
266 size += nla_total_size(data_len);
267 break;
270 entry->id = queue->id_sequence++;
272 spin_unlock_bh(&queue->lock);
274 skb = alloc_skb(size, GFP_ATOMIC);
275 if (!skb)
276 goto nlmsg_failure;
278 old_tail = skb->tail;
279 nlh = NLMSG_PUT(skb, 0, 0,
280 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
281 sizeof(struct nfgenmsg));
282 nfmsg = NLMSG_DATA(nlh);
283 nfmsg->nfgen_family = entry->pf;
284 nfmsg->version = NFNETLINK_V0;
285 nfmsg->res_id = htons(queue->queue_num);
287 pmsg.packet_id = htonl(entry->id);
288 pmsg.hw_protocol = entskb->protocol;
289 pmsg.hook = entry->hook;
291 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
293 indev = entry->indev;
294 if (indev) {
295 #ifndef CONFIG_BRIDGE_NETFILTER
296 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex));
297 #else
298 if (entry->pf == PF_BRIDGE) {
299 /* Case 1: indev is physical input device, we need to
300 * look for bridge group (when called from
301 * netfilter_bridge) */
302 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
303 htonl(indev->ifindex));
304 /* this is the bridge group "brX" */
305 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
306 htonl(indev->br_port->br->dev->ifindex));
307 } else {
308 /* Case 2: indev is bridge group, we need to look for
309 * physical device (when called from ipv4) */
310 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
311 htonl(indev->ifindex));
312 if (entskb->nf_bridge && entskb->nf_bridge->physindev)
313 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
314 htonl(entskb->nf_bridge->physindev->ifindex));
316 #endif
319 if (outdev) {
320 #ifndef CONFIG_BRIDGE_NETFILTER
321 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex));
322 #else
323 if (entry->pf == PF_BRIDGE) {
324 /* Case 1: outdev is physical output device, we need to
325 * look for bridge group (when called from
326 * netfilter_bridge) */
327 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
328 htonl(outdev->ifindex));
329 /* this is the bridge group "brX" */
330 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
331 htonl(outdev->br_port->br->dev->ifindex));
332 } else {
333 /* Case 2: outdev is bridge group, we need to look for
334 * physical output device (when called from ipv4) */
335 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
336 htonl(outdev->ifindex));
337 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev)
338 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
339 htonl(entskb->nf_bridge->physoutdev->ifindex));
341 #endif
344 if (entskb->mark)
345 NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark));
347 if (indev && entskb->dev) {
348 struct nfqnl_msg_packet_hw phw;
349 int len = dev_parse_header(entskb, phw.hw_addr);
350 if (len) {
351 phw.hw_addrlen = htons(len);
352 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
356 if (entskb->tstamp.tv64) {
357 struct nfqnl_msg_packet_timestamp ts;
358 struct timeval tv = ktime_to_timeval(entskb->tstamp);
359 ts.sec = cpu_to_be64(tv.tv_sec);
360 ts.usec = cpu_to_be64(tv.tv_usec);
362 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
365 if (data_len) {
366 struct nlattr *nla;
367 int sz = nla_attr_size(data_len);
369 if (skb_tailroom(skb) < nla_total_size(data_len)) {
370 printk(KERN_WARNING "nf_queue: no tailroom!\n");
371 goto nlmsg_failure;
374 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
375 nla->nla_type = NFQA_PAYLOAD;
376 nla->nla_len = sz;
378 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
379 BUG();
382 nlh->nlmsg_len = skb->tail - old_tail;
383 return skb;
385 nlmsg_failure:
386 nla_put_failure:
387 if (skb)
388 kfree_skb(skb);
389 if (net_ratelimit())
390 printk(KERN_ERR "nf_queue: error creating packet message\n");
391 return NULL;
394 static int
395 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
397 struct sk_buff *nskb;
398 struct nfqnl_instance *queue;
399 int err;
401 /* rcu_read_lock()ed by nf_hook_slow() */
402 queue = instance_lookup(queuenum);
403 if (!queue)
404 goto err_out;
406 if (queue->copy_mode == NFQNL_COPY_NONE)
407 goto err_out;
409 nskb = nfqnl_build_packet_message(queue, entry);
410 if (nskb == NULL)
411 goto err_out;
413 spin_lock_bh(&queue->lock);
415 if (!queue->peer_pid)
416 goto err_out_free_nskb;
418 if (queue->queue_total >= queue->queue_maxlen) {
419 queue->queue_dropped++;
420 if (net_ratelimit())
421 printk(KERN_WARNING "nf_queue: full at %d entries, "
422 "dropping packets(s). Dropped: %d\n",
423 queue->queue_total, queue->queue_dropped);
424 goto err_out_free_nskb;
427 /* nfnetlink_unicast will either free the nskb or add it to a socket */
428 err = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
429 if (err < 0) {
430 queue->queue_user_dropped++;
431 goto err_out_unlock;
434 __enqueue_entry(queue, entry);
436 spin_unlock_bh(&queue->lock);
437 return 0;
439 err_out_free_nskb:
440 kfree_skb(nskb);
441 err_out_unlock:
442 spin_unlock_bh(&queue->lock);
443 err_out:
444 return -1;
447 static int
448 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
450 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
451 =======
452 struct sk_buff *nskb;
453 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
454 int diff;
455 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
456 int err;
457 =======
458 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
460 diff = data_len - e->skb->len;
461 if (diff < 0) {
462 if (pskb_trim(e->skb, data_len))
463 return -ENOMEM;
464 } else if (diff > 0) {
465 if (data_len > 0xFFFF)
466 return -EINVAL;
467 if (diff > skb_tailroom(e->skb)) {
468 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
469 err = pskb_expand_head(e->skb, 0,
470 =======
471 nskb = skb_copy_expand(e->skb, 0,
472 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
473 diff - skb_tailroom(e->skb),
474 GFP_ATOMIC);
475 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
476 if (err) {
477 =======
478 if (!nskb) {
479 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
480 printk(KERN_WARNING "nf_queue: OOM "
481 "in mangle, dropping packet\n");
482 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
483 return err;
484 =======
485 return -ENOMEM;
486 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
488 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
489 =======
490 kfree_skb(e->skb);
491 e->skb = nskb;
492 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
494 skb_put(e->skb, diff);
496 if (!skb_make_writable(e->skb, data_len))
497 return -ENOMEM;
498 skb_copy_to_linear_data(e->skb, data, data_len);
499 e->skb->ip_summed = CHECKSUM_NONE;
500 return 0;
503 static int
504 nfqnl_set_mode(struct nfqnl_instance *queue,
505 unsigned char mode, unsigned int range)
507 int status = 0;
509 spin_lock_bh(&queue->lock);
510 switch (mode) {
511 case NFQNL_COPY_NONE:
512 case NFQNL_COPY_META:
513 queue->copy_mode = mode;
514 queue->copy_range = 0;
515 break;
517 case NFQNL_COPY_PACKET:
518 queue->copy_mode = mode;
519 /* we're using struct nlattr which has 16bit nla_len */
520 if (range > 0xffff)
521 queue->copy_range = 0xffff;
522 else
523 queue->copy_range = range;
524 break;
526 default:
527 status = -EINVAL;
530 spin_unlock_bh(&queue->lock);
532 return status;
535 static int
536 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
538 if (entry->indev)
539 if (entry->indev->ifindex == ifindex)
540 return 1;
541 if (entry->outdev)
542 if (entry->outdev->ifindex == ifindex)
543 return 1;
544 #ifdef CONFIG_BRIDGE_NETFILTER
545 if (entry->skb->nf_bridge) {
546 if (entry->skb->nf_bridge->physindev &&
547 entry->skb->nf_bridge->physindev->ifindex == ifindex)
548 return 1;
549 if (entry->skb->nf_bridge->physoutdev &&
550 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
551 return 1;
553 #endif
554 return 0;
557 /* drop all packets with either indev or outdev == ifindex from all queue
558 * instances */
559 static void
560 nfqnl_dev_drop(int ifindex)
562 int i;
564 rcu_read_lock();
566 for (i = 0; i < INSTANCE_BUCKETS; i++) {
567 struct hlist_node *tmp;
568 struct nfqnl_instance *inst;
569 struct hlist_head *head = &instance_table[i];
571 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
572 nfqnl_flush(inst, dev_cmp, ifindex);
575 rcu_read_unlock();
578 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
580 static int
581 nfqnl_rcv_dev_event(struct notifier_block *this,
582 unsigned long event, void *ptr)
584 struct net_device *dev = ptr;
586 if (dev->nd_net != &init_net)
587 return NOTIFY_DONE;
589 /* Drop any packets associated with the downed device */
590 if (event == NETDEV_DOWN)
591 nfqnl_dev_drop(dev->ifindex);
592 return NOTIFY_DONE;
595 static struct notifier_block nfqnl_dev_notifier = {
596 .notifier_call = nfqnl_rcv_dev_event,
599 static int
600 nfqnl_rcv_nl_event(struct notifier_block *this,
601 unsigned long event, void *ptr)
603 struct netlink_notify *n = ptr;
605 if (event == NETLINK_URELEASE &&
606 n->protocol == NETLINK_NETFILTER && n->pid) {
607 int i;
609 /* destroy all instances for this pid */
610 spin_lock(&instances_lock);
611 for (i = 0; i < INSTANCE_BUCKETS; i++) {
612 struct hlist_node *tmp, *t2;
613 struct nfqnl_instance *inst;
614 struct hlist_head *head = &instance_table[i];
616 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
617 if ((n->net == &init_net) &&
618 (n->pid == inst->peer_pid))
619 __instance_destroy(inst);
622 spin_unlock(&instances_lock);
624 return NOTIFY_DONE;
627 static struct notifier_block nfqnl_rtnl_notifier = {
628 .notifier_call = nfqnl_rcv_nl_event,
631 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
632 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
633 [NFQA_MARK] = { .type = NLA_U32 },
634 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
637 static int
638 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
639 struct nlmsghdr *nlh, struct nlattr *nfqa[])
641 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
642 u_int16_t queue_num = ntohs(nfmsg->res_id);
644 struct nfqnl_msg_verdict_hdr *vhdr;
645 struct nfqnl_instance *queue;
646 unsigned int verdict;
647 struct nf_queue_entry *entry;
648 int err;
650 rcu_read_lock();
651 queue = instance_lookup(queue_num);
652 if (!queue) {
653 err = -ENODEV;
654 goto err_out_unlock;
657 if (queue->peer_pid != NETLINK_CB(skb).pid) {
658 err = -EPERM;
659 goto err_out_unlock;
662 if (!nfqa[NFQA_VERDICT_HDR]) {
663 err = -EINVAL;
664 goto err_out_unlock;
667 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
668 verdict = ntohl(vhdr->verdict);
670 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
671 err = -EINVAL;
672 goto err_out_unlock;
675 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
676 if (entry == NULL) {
677 err = -ENOENT;
678 goto err_out_unlock;
680 rcu_read_unlock();
682 if (nfqa[NFQA_PAYLOAD]) {
683 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
684 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
685 verdict = NF_DROP;
688 if (nfqa[NFQA_MARK])
689 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
691 nf_reinject(entry, verdict);
692 return 0;
694 err_out_unlock:
695 rcu_read_unlock();
696 return err;
699 static int
700 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
701 struct nlmsghdr *nlh, struct nlattr *nfqa[])
703 return -ENOTSUPP;
706 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
707 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
708 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
711 static const struct nf_queue_handler nfqh = {
712 .name = "nf_queue",
713 .outfn = &nfqnl_enqueue_packet,
716 static int
717 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
718 struct nlmsghdr *nlh, struct nlattr *nfqa[])
720 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
721 u_int16_t queue_num = ntohs(nfmsg->res_id);
722 struct nfqnl_instance *queue;
723 struct nfqnl_msg_config_cmd *cmd = NULL;
724 int ret = 0;
726 if (nfqa[NFQA_CFG_CMD]) {
727 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
729 /* Commands without queue context - might sleep */
730 switch (cmd->command) {
731 case NFQNL_CFG_CMD_PF_BIND:
732 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
733 ret = nf_register_queue_handler(ntohs(cmd->pf),
734 &nfqh);
735 break;
736 =======
737 return nf_register_queue_handler(ntohs(cmd->pf),
738 &nfqh);
739 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
740 case NFQNL_CFG_CMD_PF_UNBIND:
741 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
742 ret = nf_unregister_queue_handler(ntohs(cmd->pf),
743 &nfqh);
744 break;
745 default:
746 break;
747 =======
748 return nf_unregister_queue_handler(ntohs(cmd->pf),
749 &nfqh);
750 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
752 <<<<<<< HEAD:net/netfilter/nfnetlink_queue.c
754 if (ret < 0)
755 return ret;
756 =======
757 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/nfnetlink_queue.c
760 rcu_read_lock();
761 queue = instance_lookup(queue_num);
762 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
763 ret = -EPERM;
764 goto err_out_unlock;
767 if (cmd != NULL) {
768 switch (cmd->command) {
769 case NFQNL_CFG_CMD_BIND:
770 if (queue) {
771 ret = -EBUSY;
772 goto err_out_unlock;
774 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
775 if (IS_ERR(queue)) {
776 ret = PTR_ERR(queue);
777 goto err_out_unlock;
779 break;
780 case NFQNL_CFG_CMD_UNBIND:
781 if (!queue) {
782 ret = -ENODEV;
783 goto err_out_unlock;
785 instance_destroy(queue);
786 break;
787 case NFQNL_CFG_CMD_PF_BIND:
788 case NFQNL_CFG_CMD_PF_UNBIND:
789 break;
790 default:
791 ret = -ENOTSUPP;
792 break;
796 if (nfqa[NFQA_CFG_PARAMS]) {
797 struct nfqnl_msg_config_params *params;
799 if (!queue) {
800 ret = -ENODEV;
801 goto err_out_unlock;
803 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
804 nfqnl_set_mode(queue, params->copy_mode,
805 ntohl(params->copy_range));
808 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
809 __be32 *queue_maxlen;
811 if (!queue) {
812 ret = -ENODEV;
813 goto err_out_unlock;
815 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
816 spin_lock_bh(&queue->lock);
817 queue->queue_maxlen = ntohl(*queue_maxlen);
818 spin_unlock_bh(&queue->lock);
821 err_out_unlock:
822 rcu_read_unlock();
823 return ret;
826 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
827 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
828 .attr_count = NFQA_MAX, },
829 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
830 .attr_count = NFQA_MAX,
831 .policy = nfqa_verdict_policy },
832 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
833 .attr_count = NFQA_CFG_MAX,
834 .policy = nfqa_cfg_policy },
837 static const struct nfnetlink_subsystem nfqnl_subsys = {
838 .name = "nf_queue",
839 .subsys_id = NFNL_SUBSYS_QUEUE,
840 .cb_count = NFQNL_MSG_MAX,
841 .cb = nfqnl_cb,
844 #ifdef CONFIG_PROC_FS
845 struct iter_state {
846 unsigned int bucket;
849 static struct hlist_node *get_first(struct seq_file *seq)
851 struct iter_state *st = seq->private;
853 if (!st)
854 return NULL;
856 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
857 if (!hlist_empty(&instance_table[st->bucket]))
858 return instance_table[st->bucket].first;
860 return NULL;
863 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
865 struct iter_state *st = seq->private;
867 h = h->next;
868 while (!h) {
869 if (++st->bucket >= INSTANCE_BUCKETS)
870 return NULL;
872 h = instance_table[st->bucket].first;
874 return h;
877 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
879 struct hlist_node *head;
880 head = get_first(seq);
882 if (head)
883 while (pos && (head = get_next(seq, head)))
884 pos--;
885 return pos ? NULL : head;
888 static void *seq_start(struct seq_file *seq, loff_t *pos)
889 __acquires(instances_lock)
891 spin_lock(&instances_lock);
892 return get_idx(seq, *pos);
895 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
897 (*pos)++;
898 return get_next(s, v);
901 static void seq_stop(struct seq_file *s, void *v)
902 __releases(instances_lock)
904 spin_unlock(&instances_lock);
907 static int seq_show(struct seq_file *s, void *v)
909 const struct nfqnl_instance *inst = v;
911 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
912 inst->queue_num,
913 inst->peer_pid, inst->queue_total,
914 inst->copy_mode, inst->copy_range,
915 inst->queue_dropped, inst->queue_user_dropped,
916 inst->id_sequence, 1);
919 static const struct seq_operations nfqnl_seq_ops = {
920 .start = seq_start,
921 .next = seq_next,
922 .stop = seq_stop,
923 .show = seq_show,
926 static int nfqnl_open(struct inode *inode, struct file *file)
928 return seq_open_private(file, &nfqnl_seq_ops,
929 sizeof(struct iter_state));
932 static const struct file_operations nfqnl_file_ops = {
933 .owner = THIS_MODULE,
934 .open = nfqnl_open,
935 .read = seq_read,
936 .llseek = seq_lseek,
937 .release = seq_release_private,
940 #endif /* PROC_FS */
942 static int __init nfnetlink_queue_init(void)
944 int i, status = -ENOMEM;
945 #ifdef CONFIG_PROC_FS
946 struct proc_dir_entry *proc_nfqueue;
947 #endif
949 for (i = 0; i < INSTANCE_BUCKETS; i++)
950 INIT_HLIST_HEAD(&instance_table[i]);
952 netlink_register_notifier(&nfqnl_rtnl_notifier);
953 status = nfnetlink_subsys_register(&nfqnl_subsys);
954 if (status < 0) {
955 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
956 goto cleanup_netlink_notifier;
959 #ifdef CONFIG_PROC_FS
960 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
961 proc_net_netfilter);
962 if (!proc_nfqueue)
963 goto cleanup_subsys;
964 proc_nfqueue->proc_fops = &nfqnl_file_ops;
965 #endif
967 register_netdevice_notifier(&nfqnl_dev_notifier);
968 return status;
970 #ifdef CONFIG_PROC_FS
971 cleanup_subsys:
972 nfnetlink_subsys_unregister(&nfqnl_subsys);
973 #endif
974 cleanup_netlink_notifier:
975 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
976 return status;
979 static void __exit nfnetlink_queue_fini(void)
981 nf_unregister_queue_handlers(&nfqh);
982 unregister_netdevice_notifier(&nfqnl_dev_notifier);
983 #ifdef CONFIG_PROC_FS
984 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
985 #endif
986 nfnetlink_subsys_unregister(&nfqnl_subsys);
987 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
990 MODULE_DESCRIPTION("netfilter packet queue handler");
991 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
992 MODULE_LICENSE("GPL");
993 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
995 module_init(nfnetlink_queue_init);
996 module_exit(nfnetlink_queue_fini);