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