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