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