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