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