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