[NETFILTER]: nfnetlink_queue: remove useless debugging
[linux-3.10.git] / net / netfilter / nfnetlink_queue.c
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>
23 #include <linux/proc_fs.h>
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 #include <net/netfilter/nf_queue.h>
31
32 #include <asm/atomic.h>
33
34 #ifdef CONFIG_BRIDGE_NETFILTER
35 #include "../bridge/br_private.h"
36 #endif
37
38 #define NFQNL_QMAX_DEFAULT 1024
39
40 struct nfqnl_instance {
41         struct hlist_node hlist;                /* global list of queues */
42         struct rcu_head rcu;
43
44         int peer_pid;
45         unsigned int queue_maxlen;
46         unsigned int copy_range;
47         unsigned int queue_total;
48         unsigned int queue_dropped;
49         unsigned int queue_user_dropped;
50
51         unsigned int id_sequence;               /* 'sequence' of pkt ids */
52
53         u_int16_t queue_num;                    /* number of this queue */
54         u_int8_t copy_mode;
55
56         spinlock_t lock;
57
58         struct list_head queue_list;            /* packets in queue */
59 };
60
61 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
62
63 static DEFINE_SPINLOCK(instances_lock);
64
65 #define INSTANCE_BUCKETS        16
66 static struct hlist_head instance_table[INSTANCE_BUCKETS];
67
68 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
69 {
70         return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
71 }
72
73 static struct nfqnl_instance *
74 instance_lookup(u_int16_t queue_num)
75 {
76         struct hlist_head *head;
77         struct hlist_node *pos;
78         struct nfqnl_instance *inst;
79
80         head = &instance_table[instance_hashfn(queue_num)];
81         hlist_for_each_entry_rcu(inst, pos, head, hlist) {
82                 if (inst->queue_num == queue_num)
83                         return inst;
84         }
85         return NULL;
86 }
87
88 static struct nfqnl_instance *
89 instance_create(u_int16_t queue_num, int pid)
90 {
91         struct nfqnl_instance *inst = NULL;
92         unsigned int h;
93
94         spin_lock(&instances_lock);
95         if (instance_lookup(queue_num))
96                 goto out_unlock;
97
98         inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
99         if (!inst)
100                 goto out_unlock;
101
102         inst->queue_num = queue_num;
103         inst->peer_pid = pid;
104         inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
105         inst->copy_range = 0xfffff;
106         inst->copy_mode = NFQNL_COPY_NONE;
107         spin_lock_init(&inst->lock);
108         INIT_LIST_HEAD(&inst->queue_list);
109         INIT_RCU_HEAD(&inst->rcu);
110
111         if (!try_module_get(THIS_MODULE))
112                 goto out_free;
113
114         h = instance_hashfn(queue_num);
115         hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
116
117         spin_unlock(&instances_lock);
118
119         return inst;
120
121 out_free:
122         kfree(inst);
123 out_unlock:
124         spin_unlock(&instances_lock);
125         return NULL;
126 }
127
128 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
129                         unsigned long data);
130
131 static void
132 instance_destroy_rcu(struct rcu_head *head)
133 {
134         struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
135                                                    rcu);
136
137         nfqnl_flush(inst, NULL, 0);
138         kfree(inst);
139         module_put(THIS_MODULE);
140 }
141
142 static void
143 __instance_destroy(struct nfqnl_instance *inst)
144 {
145         hlist_del_rcu(&inst->hlist);
146         call_rcu(&inst->rcu, instance_destroy_rcu);
147 }
148
149 static void
150 instance_destroy(struct nfqnl_instance *inst)
151 {
152         spin_lock(&instances_lock);
153         __instance_destroy(inst);
154         spin_unlock(&instances_lock);
155 }
156
157 static inline void
158 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
159 {
160        list_add_tail(&entry->list, &queue->queue_list);
161        queue->queue_total++;
162 }
163
164 static struct nf_queue_entry *
165 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
166 {
167         struct nf_queue_entry *entry = NULL, *i;
168
169         spin_lock_bh(&queue->lock);
170
171         list_for_each_entry(i, &queue->queue_list, list) {
172                 if (i->id == id) {
173                         entry = i;
174                         break;
175                 }
176         }
177
178         if (entry) {
179                 list_del(&entry->list);
180                 queue->queue_total--;
181         }
182
183         spin_unlock_bh(&queue->lock);
184
185         return entry;
186 }
187
188 static void
189 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
190 {
191         struct nf_queue_entry *entry, *next;
192
193         spin_lock_bh(&queue->lock);
194         list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
195                 if (!cmpfn || cmpfn(entry, data)) {
196                         list_del(&entry->list);
197                         queue->queue_total--;
198                         nf_reinject(entry, NF_DROP);
199                 }
200         }
201         spin_unlock_bh(&queue->lock);
202 }
203
204 static struct sk_buff *
205 nfqnl_build_packet_message(struct nfqnl_instance *queue,
206                            struct nf_queue_entry *entry, int *errp)
207 {
208         sk_buff_data_t old_tail;
209         size_t size;
210         size_t data_len = 0;
211         struct sk_buff *skb;
212         struct nfqnl_msg_packet_hdr pmsg;
213         struct nlmsghdr *nlh;
214         struct nfgenmsg *nfmsg;
215         struct sk_buff *entskb = entry->skb;
216         struct net_device *indev;
217         struct net_device *outdev;
218         __be32 tmp_uint;
219
220         size =    NLMSG_ALIGN(sizeof(struct nfgenmsg))
221                 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
222                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
223                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
224 #ifdef CONFIG_BRIDGE_NETFILTER
225                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
226                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
227 #endif
228                 + nla_total_size(sizeof(u_int32_t))     /* mark */
229                 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
230                 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
231
232         outdev = entry->outdev;
233
234         spin_lock_bh(&queue->lock);
235
236         switch (queue->copy_mode) {
237         case NFQNL_COPY_META:
238         case NFQNL_COPY_NONE:
239                 data_len = 0;
240                 break;
241
242         case NFQNL_COPY_PACKET:
243                 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
244                      entskb->ip_summed == CHECKSUM_COMPLETE) &&
245                     (*errp = skb_checksum_help(entskb))) {
246                         spin_unlock_bh(&queue->lock);
247                         return NULL;
248                 }
249                 if (queue->copy_range == 0
250                     || queue->copy_range > entskb->len)
251                         data_len = entskb->len;
252                 else
253                         data_len = queue->copy_range;
254
255                 size += nla_total_size(data_len);
256                 break;
257
258         default:
259                 *errp = -EINVAL;
260                 spin_unlock_bh(&queue->lock);
261                 return NULL;
262         }
263
264         entry->id = queue->id_sequence++;
265
266         spin_unlock_bh(&queue->lock);
267
268         skb = alloc_skb(size, GFP_ATOMIC);
269         if (!skb)
270                 goto nlmsg_failure;
271
272         old_tail = skb->tail;
273         nlh = NLMSG_PUT(skb, 0, 0,
274                         NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
275                         sizeof(struct nfgenmsg));
276         nfmsg = NLMSG_DATA(nlh);
277         nfmsg->nfgen_family = entry->pf;
278         nfmsg->version = NFNETLINK_V0;
279         nfmsg->res_id = htons(queue->queue_num);
280
281         pmsg.packet_id          = htonl(entry->id);
282         pmsg.hw_protocol        = entskb->protocol;
283         pmsg.hook               = entry->hook;
284
285         NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
286
287         indev = entry->indev;
288         if (indev) {
289                 tmp_uint = htonl(indev->ifindex);
290 #ifndef CONFIG_BRIDGE_NETFILTER
291                 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
292 #else
293                 if (entry->pf == PF_BRIDGE) {
294                         /* Case 1: indev is physical input device, we need to
295                          * look for bridge group (when called from
296                          * netfilter_bridge) */
297                         NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
298                                 &tmp_uint);
299                         /* this is the bridge group "brX" */
300                         tmp_uint = htonl(indev->br_port->br->dev->ifindex);
301                         NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
302                                 &tmp_uint);
303                 } else {
304                         /* Case 2: indev is bridge group, we need to look for
305                          * physical device (when called from ipv4) */
306                         NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
307                                 &tmp_uint);
308                         if (entskb->nf_bridge
309                             && entskb->nf_bridge->physindev) {
310                                 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
311                                 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
312                                         sizeof(tmp_uint), &tmp_uint);
313                         }
314                 }
315 #endif
316         }
317
318         if (outdev) {
319                 tmp_uint = htonl(outdev->ifindex);
320 #ifndef CONFIG_BRIDGE_NETFILTER
321                 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
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(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
328                                 &tmp_uint);
329                         /* this is the bridge group "brX" */
330                         tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
331                         NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
332                                 &tmp_uint);
333                 } else {
334                         /* Case 2: outdev is bridge group, we need to look for
335                          * physical output device (when called from ipv4) */
336                         NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
337                                 &tmp_uint);
338                         if (entskb->nf_bridge
339                             && entskb->nf_bridge->physoutdev) {
340                                 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
341                                 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
342                                         sizeof(tmp_uint), &tmp_uint);
343                         }
344                 }
345 #endif
346         }
347
348         if (entskb->mark) {
349                 tmp_uint = htonl(entskb->mark);
350                 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
351         }
352
353         if (indev && entskb->dev) {
354                 struct nfqnl_msg_packet_hw phw;
355                 int len = dev_parse_header(entskb, phw.hw_addr);
356                 if (len) {
357                         phw.hw_addrlen = htons(len);
358                         NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
359                 }
360         }
361
362         if (entskb->tstamp.tv64) {
363                 struct nfqnl_msg_packet_timestamp ts;
364                 struct timeval tv = ktime_to_timeval(entskb->tstamp);
365                 ts.sec = cpu_to_be64(tv.tv_sec);
366                 ts.usec = cpu_to_be64(tv.tv_usec);
367
368                 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
369         }
370
371         if (data_len) {
372                 struct nlattr *nla;
373                 int size = nla_attr_size(data_len);
374
375                 if (skb_tailroom(skb) < nla_total_size(data_len)) {
376                         printk(KERN_WARNING "nf_queue: no tailroom!\n");
377                         goto nlmsg_failure;
378                 }
379
380                 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
381                 nla->nla_type = NFQA_PAYLOAD;
382                 nla->nla_len = size;
383
384                 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
385                         BUG();
386         }
387
388         nlh->nlmsg_len = skb->tail - old_tail;
389         return skb;
390
391 nlmsg_failure:
392 nla_put_failure:
393         if (skb)
394                 kfree_skb(skb);
395         *errp = -EINVAL;
396         if (net_ratelimit())
397                 printk(KERN_ERR "nf_queue: error creating packet message\n");
398         return NULL;
399 }
400
401 static int
402 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
403 {
404         int status = -EINVAL;
405         struct sk_buff *nskb;
406         struct nfqnl_instance *queue;
407
408         /* rcu_read_lock()ed by nf_hook_slow() */
409         queue = instance_lookup(queuenum);
410         if (!queue)
411                 return -EINVAL;
412
413         if (queue->copy_mode == NFQNL_COPY_NONE)
414                 return -EAGAIN;
415
416         nskb = nfqnl_build_packet_message(queue, entry, &status);
417         if (nskb == NULL)
418                 return status;
419
420         spin_lock_bh(&queue->lock);
421
422         if (!queue->peer_pid)
423                 goto err_out_free_nskb;
424
425         if (queue->queue_total >= queue->queue_maxlen) {
426                 queue->queue_dropped++;
427                 status = -ENOSPC;
428                 if (net_ratelimit())
429                           printk(KERN_WARNING "nf_queue: full at %d entries, "
430                                  "dropping packets(s). Dropped: %d\n",
431                                  queue->queue_total, queue->queue_dropped);
432                 goto err_out_free_nskb;
433         }
434
435         /* nfnetlink_unicast will either free the nskb or add it to a socket */
436         status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
437         if (status < 0) {
438                 queue->queue_user_dropped++;
439                 goto err_out_unlock;
440         }
441
442         __enqueue_entry(queue, entry);
443
444         spin_unlock_bh(&queue->lock);
445         return status;
446
447 err_out_free_nskb:
448         kfree_skb(nskb);
449
450 err_out_unlock:
451         spin_unlock_bh(&queue->lock);
452         return status;
453 }
454
455 static int
456 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
457 {
458         int diff;
459         int err;
460
461         diff = data_len - e->skb->len;
462         if (diff < 0) {
463                 if (pskb_trim(e->skb, data_len))
464                         return -ENOMEM;
465         } else if (diff > 0) {
466                 if (data_len > 0xFFFF)
467                         return -EINVAL;
468                 if (diff > skb_tailroom(e->skb)) {
469                         err = pskb_expand_head(e->skb, 0,
470                                                diff - skb_tailroom(e->skb),
471                                                GFP_ATOMIC);
472                         if (err) {
473                                 printk(KERN_WARNING "nf_queue: OOM "
474                                       "in mangle, dropping packet\n");
475                                 return err;
476                         }
477                 }
478                 skb_put(e->skb, diff);
479         }
480         if (!skb_make_writable(e->skb, data_len))
481                 return -ENOMEM;
482         skb_copy_to_linear_data(e->skb, data, data_len);
483         e->skb->ip_summed = CHECKSUM_NONE;
484         return 0;
485 }
486
487 static int
488 nfqnl_set_mode(struct nfqnl_instance *queue,
489                unsigned char mode, unsigned int range)
490 {
491         int status = 0;
492
493         spin_lock_bh(&queue->lock);
494         switch (mode) {
495         case NFQNL_COPY_NONE:
496         case NFQNL_COPY_META:
497                 queue->copy_mode = mode;
498                 queue->copy_range = 0;
499                 break;
500
501         case NFQNL_COPY_PACKET:
502                 queue->copy_mode = mode;
503                 /* we're using struct nlattr which has 16bit nla_len */
504                 if (range > 0xffff)
505                         queue->copy_range = 0xffff;
506                 else
507                         queue->copy_range = range;
508                 break;
509
510         default:
511                 status = -EINVAL;
512
513         }
514         spin_unlock_bh(&queue->lock);
515
516         return status;
517 }
518
519 static int
520 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
521 {
522         if (entry->indev)
523                 if (entry->indev->ifindex == ifindex)
524                         return 1;
525         if (entry->outdev)
526                 if (entry->outdev->ifindex == ifindex)
527                         return 1;
528 #ifdef CONFIG_BRIDGE_NETFILTER
529         if (entry->skb->nf_bridge) {
530                 if (entry->skb->nf_bridge->physindev &&
531                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
532                         return 1;
533                 if (entry->skb->nf_bridge->physoutdev &&
534                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
535                         return 1;
536         }
537 #endif
538         return 0;
539 }
540
541 /* drop all packets with either indev or outdev == ifindex from all queue
542  * instances */
543 static void
544 nfqnl_dev_drop(int ifindex)
545 {
546         int i;
547
548         rcu_read_lock();
549
550         for (i = 0; i < INSTANCE_BUCKETS; i++) {
551                 struct hlist_node *tmp;
552                 struct nfqnl_instance *inst;
553                 struct hlist_head *head = &instance_table[i];
554
555                 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
556                         nfqnl_flush(inst, dev_cmp, ifindex);
557         }
558
559         rcu_read_unlock();
560 }
561
562 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
563
564 static int
565 nfqnl_rcv_dev_event(struct notifier_block *this,
566                     unsigned long event, void *ptr)
567 {
568         struct net_device *dev = ptr;
569
570         if (dev->nd_net != &init_net)
571                 return NOTIFY_DONE;
572
573         /* Drop any packets associated with the downed device */
574         if (event == NETDEV_DOWN)
575                 nfqnl_dev_drop(dev->ifindex);
576         return NOTIFY_DONE;
577 }
578
579 static struct notifier_block nfqnl_dev_notifier = {
580         .notifier_call  = nfqnl_rcv_dev_event,
581 };
582
583 static int
584 nfqnl_rcv_nl_event(struct notifier_block *this,
585                    unsigned long event, void *ptr)
586 {
587         struct netlink_notify *n = ptr;
588
589         if (event == NETLINK_URELEASE &&
590             n->protocol == NETLINK_NETFILTER && n->pid) {
591                 int i;
592
593                 /* destroy all instances for this pid */
594                 spin_lock(&instances_lock);
595                 for (i = 0; i < INSTANCE_BUCKETS; i++) {
596                         struct hlist_node *tmp, *t2;
597                         struct nfqnl_instance *inst;
598                         struct hlist_head *head = &instance_table[i];
599
600                         hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
601                                 if ((n->net == &init_net) &&
602                                     (n->pid == inst->peer_pid))
603                                         __instance_destroy(inst);
604                         }
605                 }
606                 spin_unlock(&instances_lock);
607         }
608         return NOTIFY_DONE;
609 }
610
611 static struct notifier_block nfqnl_rtnl_notifier = {
612         .notifier_call  = nfqnl_rcv_nl_event,
613 };
614
615 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
616         [NFQA_VERDICT_HDR]      = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
617         [NFQA_MARK]             = { .type = NLA_U32 },
618         [NFQA_PAYLOAD]          = { .type = NLA_UNSPEC },
619 };
620
621 static int
622 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
623                    struct nlmsghdr *nlh, struct nlattr *nfqa[])
624 {
625         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
626         u_int16_t queue_num = ntohs(nfmsg->res_id);
627
628         struct nfqnl_msg_verdict_hdr *vhdr;
629         struct nfqnl_instance *queue;
630         unsigned int verdict;
631         struct nf_queue_entry *entry;
632         int err;
633
634         rcu_read_lock();
635         queue = instance_lookup(queue_num);
636         if (!queue) {
637                 err = -ENODEV;
638                 goto err_out_unlock;
639         }
640
641         if (queue->peer_pid != NETLINK_CB(skb).pid) {
642                 err = -EPERM;
643                 goto err_out_unlock;
644         }
645
646         if (!nfqa[NFQA_VERDICT_HDR]) {
647                 err = -EINVAL;
648                 goto err_out_unlock;
649         }
650
651         vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
652         verdict = ntohl(vhdr->verdict);
653
654         if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
655                 err = -EINVAL;
656                 goto err_out_unlock;
657         }
658
659         entry = find_dequeue_entry(queue, ntohl(vhdr->id));
660         if (entry == NULL) {
661                 err = -ENOENT;
662                 goto err_out_unlock;
663         }
664         rcu_read_unlock();
665
666         if (nfqa[NFQA_PAYLOAD]) {
667                 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
668                                  nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
669                         verdict = NF_DROP;
670         }
671
672         if (nfqa[NFQA_MARK])
673                 entry->skb->mark = ntohl(*(__be32 *)
674                                          nla_data(nfqa[NFQA_MARK]));
675
676         nf_reinject(entry, verdict);
677         return 0;
678
679 err_out_unlock:
680         rcu_read_unlock();
681         return err;
682 }
683
684 static int
685 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
686                   struct nlmsghdr *nlh, struct nlattr *nfqa[])
687 {
688         return -ENOTSUPP;
689 }
690
691 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
692         [NFQA_CFG_CMD]          = { .len = sizeof(struct nfqnl_msg_config_cmd) },
693         [NFQA_CFG_PARAMS]       = { .len = sizeof(struct nfqnl_msg_config_params) },
694 };
695
696 static const struct nf_queue_handler nfqh = {
697         .name   = "nf_queue",
698         .outfn  = &nfqnl_enqueue_packet,
699 };
700
701 static int
702 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
703                   struct nlmsghdr *nlh, struct nlattr *nfqa[])
704 {
705         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
706         u_int16_t queue_num = ntohs(nfmsg->res_id);
707         struct nfqnl_instance *queue;
708         struct nfqnl_msg_config_cmd *cmd = NULL;
709         int ret = 0;
710
711         if (nfqa[NFQA_CFG_CMD]) {
712                 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
713
714                 /* Commands without queue context - might sleep */
715                 switch (cmd->command) {
716                 case NFQNL_CFG_CMD_PF_BIND:
717                         ret = nf_register_queue_handler(ntohs(cmd->pf),
718                                                         &nfqh);
719                         break;
720                 case NFQNL_CFG_CMD_PF_UNBIND:
721                         ret = nf_unregister_queue_handler(ntohs(cmd->pf),
722                                                           &nfqh);
723                         break;
724                 default:
725                         break;
726                 }
727
728                 if (ret < 0)
729                         return ret;
730         }
731
732         rcu_read_lock();
733         queue = instance_lookup(queue_num);
734         if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
735                 ret = -EPERM;
736                 goto err_out_unlock;
737         }
738
739         if (cmd != NULL) {
740                 switch (cmd->command) {
741                 case NFQNL_CFG_CMD_BIND:
742                         if (queue) {
743                                 ret = -EBUSY;
744                                 goto err_out_unlock;
745                         }
746                         queue = instance_create(queue_num, NETLINK_CB(skb).pid);
747                         if (!queue) {
748                                 ret = -EINVAL;
749                                 goto err_out_unlock;
750                         }
751                         break;
752                 case NFQNL_CFG_CMD_UNBIND:
753                         if (!queue) {
754                                 ret = -ENODEV;
755                                 goto err_out_unlock;
756                         }
757                         instance_destroy(queue);
758                         break;
759                 case NFQNL_CFG_CMD_PF_BIND:
760                 case NFQNL_CFG_CMD_PF_UNBIND:
761                         break;
762                 default:
763                         ret = -EINVAL;
764                         break;
765                 }
766         }
767
768         if (nfqa[NFQA_CFG_PARAMS]) {
769                 struct nfqnl_msg_config_params *params;
770
771                 if (!queue) {
772                         ret = -ENODEV;
773                         goto err_out_unlock;
774                 }
775                 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
776                 nfqnl_set_mode(queue, params->copy_mode,
777                                 ntohl(params->copy_range));
778         }
779
780         if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
781                 __be32 *queue_maxlen;
782
783                 if (!queue) {
784                         ret = -ENODEV;
785                         goto err_out_unlock;
786                 }
787                 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
788                 spin_lock_bh(&queue->lock);
789                 queue->queue_maxlen = ntohl(*queue_maxlen);
790                 spin_unlock_bh(&queue->lock);
791         }
792
793 err_out_unlock:
794         rcu_read_unlock();
795         return ret;
796 }
797
798 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
799         [NFQNL_MSG_PACKET]      = { .call = nfqnl_recv_unsupp,
800                                     .attr_count = NFQA_MAX, },
801         [NFQNL_MSG_VERDICT]     = { .call = nfqnl_recv_verdict,
802                                     .attr_count = NFQA_MAX,
803                                     .policy = nfqa_verdict_policy },
804         [NFQNL_MSG_CONFIG]      = { .call = nfqnl_recv_config,
805                                     .attr_count = NFQA_CFG_MAX,
806                                     .policy = nfqa_cfg_policy },
807 };
808
809 static const struct nfnetlink_subsystem nfqnl_subsys = {
810         .name           = "nf_queue",
811         .subsys_id      = NFNL_SUBSYS_QUEUE,
812         .cb_count       = NFQNL_MSG_MAX,
813         .cb             = nfqnl_cb,
814 };
815
816 #ifdef CONFIG_PROC_FS
817 struct iter_state {
818         unsigned int bucket;
819 };
820
821 static struct hlist_node *get_first(struct seq_file *seq)
822 {
823         struct iter_state *st = seq->private;
824
825         if (!st)
826                 return NULL;
827
828         for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
829                 if (!hlist_empty(&instance_table[st->bucket]))
830                         return instance_table[st->bucket].first;
831         }
832         return NULL;
833 }
834
835 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
836 {
837         struct iter_state *st = seq->private;
838
839         h = h->next;
840         while (!h) {
841                 if (++st->bucket >= INSTANCE_BUCKETS)
842                         return NULL;
843
844                 h = instance_table[st->bucket].first;
845         }
846         return h;
847 }
848
849 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
850 {
851         struct hlist_node *head;
852         head = get_first(seq);
853
854         if (head)
855                 while (pos && (head = get_next(seq, head)))
856                         pos--;
857         return pos ? NULL : head;
858 }
859
860 static void *seq_start(struct seq_file *seq, loff_t *pos)
861 {
862         spin_lock(&instances_lock);
863         return get_idx(seq, *pos);
864 }
865
866 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
867 {
868         (*pos)++;
869         return get_next(s, v);
870 }
871
872 static void seq_stop(struct seq_file *s, void *v)
873 {
874         spin_unlock(&instances_lock);
875 }
876
877 static int seq_show(struct seq_file *s, void *v)
878 {
879         const struct nfqnl_instance *inst = v;
880
881         return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
882                           inst->queue_num,
883                           inst->peer_pid, inst->queue_total,
884                           inst->copy_mode, inst->copy_range,
885                           inst->queue_dropped, inst->queue_user_dropped,
886                           inst->id_sequence, 1);
887 }
888
889 static const struct seq_operations nfqnl_seq_ops = {
890         .start  = seq_start,
891         .next   = seq_next,
892         .stop   = seq_stop,
893         .show   = seq_show,
894 };
895
896 static int nfqnl_open(struct inode *inode, struct file *file)
897 {
898         return seq_open_private(file, &nfqnl_seq_ops,
899                         sizeof(struct iter_state));
900 }
901
902 static const struct file_operations nfqnl_file_ops = {
903         .owner   = THIS_MODULE,
904         .open    = nfqnl_open,
905         .read    = seq_read,
906         .llseek  = seq_lseek,
907         .release = seq_release_private,
908 };
909
910 #endif /* PROC_FS */
911
912 static int __init nfnetlink_queue_init(void)
913 {
914         int i, status = -ENOMEM;
915 #ifdef CONFIG_PROC_FS
916         struct proc_dir_entry *proc_nfqueue;
917 #endif
918
919         for (i = 0; i < INSTANCE_BUCKETS; i++)
920                 INIT_HLIST_HEAD(&instance_table[i]);
921
922         netlink_register_notifier(&nfqnl_rtnl_notifier);
923         status = nfnetlink_subsys_register(&nfqnl_subsys);
924         if (status < 0) {
925                 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
926                 goto cleanup_netlink_notifier;
927         }
928
929 #ifdef CONFIG_PROC_FS
930         proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
931                                          proc_net_netfilter);
932         if (!proc_nfqueue)
933                 goto cleanup_subsys;
934         proc_nfqueue->proc_fops = &nfqnl_file_ops;
935 #endif
936
937         register_netdevice_notifier(&nfqnl_dev_notifier);
938         return status;
939
940 #ifdef CONFIG_PROC_FS
941 cleanup_subsys:
942         nfnetlink_subsys_unregister(&nfqnl_subsys);
943 #endif
944 cleanup_netlink_notifier:
945         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
946         return status;
947 }
948
949 static void __exit nfnetlink_queue_fini(void)
950 {
951         nf_unregister_queue_handlers(&nfqh);
952         unregister_netdevice_notifier(&nfqnl_dev_notifier);
953 #ifdef CONFIG_PROC_FS
954         remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
955 #endif
956         nfnetlink_subsys_unregister(&nfqnl_subsys);
957         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
958 }
959
960 MODULE_DESCRIPTION("netfilter packet queue handler");
961 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
962 MODULE_LICENSE("GPL");
963 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
964
965 module_init(nfnetlink_queue_init);
966 module_exit(nfnetlink_queue_fini);