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