[NETFILTER] remove bogus hand-coded htonll() from nenetlink_queue
[pandora-kernel.git] / net / netfilter / nfnetlink_queue.c
1 /*
2  * This is a module which is used for queueing packets and communicating with
3  * userspace via nfetlink.
4  *
5  * (C) 2005 by Harald Welte <laforge@netfilter.org>
6  *
7  * Based on the old ipv4-only ip_queue.c:
8  * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9  * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  */
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/notifier.h>
21 #include <linux/netdevice.h>
22 #include <linux/netfilter.h>
23 #include <linux/proc_fs.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/netfilter/nfnetlink.h>
27 #include <linux/netfilter/nfnetlink_queue.h>
28 #include <linux/list.h>
29 #include <net/sock.h>
30
31 #include <asm/atomic.h>
32
33 #ifdef CONFIG_BRIDGE_NETFILTER
34 #include "../bridge/br_private.h"
35 #endif
36
37 #define NFQNL_QMAX_DEFAULT 1024
38
39 #if 0
40 #define QDEBUG(x, args ...)     printk(KERN_DEBUG "%s(%d):%s(): " x,       \
41                                         __FILE__, __LINE__, __FUNCTION__,  \
42                                         ## args)
43 #else
44 #define QDEBUG(x, ...)
45 #endif
46
47 struct nfqnl_queue_entry {
48         struct list_head list;
49         struct nf_info *info;
50         struct sk_buff *skb;
51         unsigned int id;
52 };
53
54 struct nfqnl_instance {
55         struct hlist_node hlist;                /* global list of queues */
56         atomic_t use;
57
58         int peer_pid;
59         unsigned int queue_maxlen;
60         unsigned int copy_range;
61         unsigned int queue_total;
62         unsigned int queue_dropped;
63         unsigned int queue_user_dropped;
64
65         atomic_t id_sequence;                   /* 'sequence' of pkt ids */
66
67         u_int16_t queue_num;                    /* number of this queue */
68         u_int8_t copy_mode;
69
70         spinlock_t lock;
71
72         struct list_head queue_list;            /* packets in queue */
73 };
74
75 typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
76
77 static DEFINE_RWLOCK(instances_lock);
78
79 #define INSTANCE_BUCKETS        16
80 static struct hlist_head instance_table[INSTANCE_BUCKETS];
81
82 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
83 {
84         return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
85 }
86
87 static struct nfqnl_instance *
88 __instance_lookup(u_int16_t queue_num)
89 {
90         struct hlist_head *head;
91         struct hlist_node *pos;
92         struct nfqnl_instance *inst;
93
94         head = &instance_table[instance_hashfn(queue_num)];
95         hlist_for_each_entry(inst, pos, head, hlist) {
96                 if (inst->queue_num == queue_num)
97                         return inst;
98         }
99         return NULL;
100 }
101
102 static struct nfqnl_instance *
103 instance_lookup_get(u_int16_t queue_num)
104 {
105         struct nfqnl_instance *inst;
106
107         read_lock_bh(&instances_lock);
108         inst = __instance_lookup(queue_num);
109         if (inst)
110                 atomic_inc(&inst->use);
111         read_unlock_bh(&instances_lock);
112
113         return inst;
114 }
115
116 static void
117 instance_put(struct nfqnl_instance *inst)
118 {
119         if (inst && atomic_dec_and_test(&inst->use)) {
120                 QDEBUG("kfree(inst=%p)\n", inst);
121                 kfree(inst);
122         }
123 }
124
125 static struct nfqnl_instance *
126 instance_create(u_int16_t queue_num, int pid)
127 {
128         struct nfqnl_instance *inst;
129
130         QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
131
132         write_lock_bh(&instances_lock); 
133         if (__instance_lookup(queue_num)) {
134                 inst = NULL;
135                 QDEBUG("aborting, instance already exists\n");
136                 goto out_unlock;
137         }
138
139         inst = kmalloc(sizeof(*inst), GFP_ATOMIC);
140         if (!inst)
141                 goto out_unlock;
142
143         memset(inst, 0, sizeof(*inst));
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         inst->lock = SPIN_LOCK_UNLOCKED;
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(&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 list_head *p;
247
248         list_for_each_prev(p, &queue->queue_list) {
249                 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
250                 
251                 if (!cmpfn || cmpfn(entry, data))
252                         return entry;
253         }
254         return NULL;
255 }
256
257 static inline void
258 __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
259 {
260         list_del(&entry->list);
261         q->queue_total--;
262 }
263
264 static inline struct nfqnl_queue_entry *
265 __find_dequeue_entry(struct nfqnl_instance *queue,
266                      nfqnl_cmpfn cmpfn, unsigned long data)
267 {
268         struct nfqnl_queue_entry *entry;
269
270         entry = __find_entry(queue, cmpfn, data);
271         if (entry == NULL)
272                 return NULL;
273
274         __dequeue_entry(queue, entry);
275         return entry;
276 }
277
278
279 static inline void
280 __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
281 {
282         struct nfqnl_queue_entry *entry;
283         
284         while ((entry = __find_dequeue_entry(queue, NULL, 0)))
285                 issue_verdict(entry, verdict);
286 }
287
288 static inline int
289 __nfqnl_set_mode(struct nfqnl_instance *queue,
290                  unsigned char mode, unsigned int range)
291 {
292         int status = 0;
293         
294         switch (mode) {
295         case NFQNL_COPY_NONE:
296         case NFQNL_COPY_META:
297                 queue->copy_mode = mode;
298                 queue->copy_range = 0;
299                 break;
300                 
301         case NFQNL_COPY_PACKET:
302                 queue->copy_mode = mode;
303                 /* we're using struct nfattr which has 16bit nfa_len */
304                 if (range > 0xffff)
305                         queue->copy_range = 0xffff;
306                 else
307                         queue->copy_range = range;
308                 break;
309                 
310         default:
311                 status = -EINVAL;
312
313         }
314         return status;
315 }
316
317 static struct nfqnl_queue_entry *
318 find_dequeue_entry(struct nfqnl_instance *queue,
319                          nfqnl_cmpfn cmpfn, unsigned long data)
320 {
321         struct nfqnl_queue_entry *entry;
322         
323         spin_lock_bh(&queue->lock);
324         entry = __find_dequeue_entry(queue, cmpfn, data);
325         spin_unlock_bh(&queue->lock);
326
327         return entry;
328 }
329
330 static void
331 nfqnl_flush(struct nfqnl_instance *queue, int verdict)
332 {
333         spin_lock_bh(&queue->lock);
334         __nfqnl_flush(queue, verdict);
335         spin_unlock_bh(&queue->lock);
336 }
337
338 static struct sk_buff *
339 nfqnl_build_packet_message(struct nfqnl_instance *queue,
340                            struct nfqnl_queue_entry *entry, int *errp)
341 {
342         unsigned char *old_tail;
343         size_t size;
344         size_t data_len = 0;
345         struct sk_buff *skb;
346         struct nfqnl_msg_packet_hdr pmsg;
347         struct nlmsghdr *nlh;
348         struct nfgenmsg *nfmsg;
349         unsigned int tmp_uint;
350
351         QDEBUG("entered\n");
352
353         /* all macros expand to constant values at compile time */
354         size =    NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
355                 + NLMSG_SPACE(sizeof(u_int32_t))        /* ifindex */
356                 + NLMSG_SPACE(sizeof(u_int32_t))        /* ifindex */
357 #ifdef CONFIG_BRIDGE_NETFILTER
358                 + NLMSG_SPACE(sizeof(u_int32_t))        /* ifindex */
359                 + NLMSG_SPACE(sizeof(u_int32_t))        /* ifindex */
360 #endif
361                 + NLMSG_SPACE(sizeof(u_int32_t))        /* mark */
362                 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
363                 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
364
365         spin_lock_bh(&queue->lock);
366         
367         switch (queue->copy_mode) {
368         case NFQNL_COPY_META:
369         case NFQNL_COPY_NONE:
370                 data_len = 0;
371                 break;
372         
373         case NFQNL_COPY_PACKET:
374                 if (queue->copy_range == 0 
375                     || queue->copy_range > entry->skb->len)
376                         data_len = entry->skb->len;
377                 else
378                         data_len = queue->copy_range;
379                 
380                 size += NLMSG_SPACE(data_len);
381                 break;
382         
383         default:
384                 *errp = -EINVAL;
385                 spin_unlock_bh(&queue->lock);
386                 return NULL;
387         }
388
389         spin_unlock_bh(&queue->lock);
390
391         skb = alloc_skb(size, GFP_ATOMIC);
392         if (!skb)
393                 goto nlmsg_failure;
394                 
395         old_tail= skb->tail;
396         nlh = NLMSG_PUT(skb, 0, 0, 
397                         NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
398                         sizeof(struct nfgenmsg));
399         nfmsg = NLMSG_DATA(nlh);
400         nfmsg->nfgen_family = entry->info->pf;
401         nfmsg->version = NFNETLINK_V0;
402         nfmsg->res_id = htons(queue->queue_num);
403
404         pmsg.packet_id          = htonl(entry->id);
405         pmsg.hw_protocol        = htons(entry->skb->protocol);
406         pmsg.hook               = entry->info->hook;
407
408         NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
409
410         if (entry->info->indev) {
411                 tmp_uint = htonl(entry->info->indev->ifindex);
412 #ifndef CONFIG_BRIDGE_NETFILTER
413                 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
414 #else
415                 if (entry->info->pf == PF_BRIDGE) {
416                         /* Case 1: indev is physical input device, we need to
417                          * look for bridge group (when called from 
418                          * netfilter_bridge) */
419                         NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint), 
420                                 &tmp_uint);
421                         /* this is the bridge group "brX" */
422                         tmp_uint = htonl(entry->info->indev->br_port->br->dev->ifindex);
423                         NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
424                                 &tmp_uint);
425                 } else {
426                         /* Case 2: indev is bridge group, we need to look for
427                          * physical device (when called from ipv4) */
428                         NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
429                                 &tmp_uint);
430                         if (entry->skb->nf_bridge
431                             && entry->skb->nf_bridge->physindev) {
432                                 tmp_uint = htonl(entry->skb->nf_bridge->physindev->ifindex);
433                                 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
434                                         sizeof(tmp_uint), &tmp_uint);
435                         }
436                 }
437 #endif
438         }
439
440         if (entry->info->outdev) {
441                 tmp_uint = htonl(entry->info->outdev->ifindex);
442 #ifndef CONFIG_BRIDGE_NETFILTER
443                 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
444 #else
445                 if (entry->info->pf == PF_BRIDGE) {
446                         /* Case 1: outdev is physical output device, we need to
447                          * look for bridge group (when called from 
448                          * netfilter_bridge) */
449                         NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
450                                 &tmp_uint);
451                         /* this is the bridge group "brX" */
452                         tmp_uint = htonl(entry->info->outdev->br_port->br->dev->ifindex);
453                         NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
454                                 &tmp_uint);
455                 } else {
456                         /* Case 2: outdev is bridge group, we need to look for
457                          * physical output device (when called from ipv4) */
458                         NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
459                                 &tmp_uint);
460                         if (entry->skb->nf_bridge
461                             && entry->skb->nf_bridge->physoutdev) {
462                                 tmp_uint = htonl(entry->skb->nf_bridge->physoutdev->ifindex);
463                                 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
464                                         sizeof(tmp_uint), &tmp_uint);
465                         }
466                 }
467 #endif
468         }
469
470         if (entry->skb->nfmark) {
471                 tmp_uint = htonl(entry->skb->nfmark);
472                 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
473         }
474
475         if (entry->info->indev && entry->skb->dev
476             && entry->skb->dev->hard_header_parse) {
477                 struct nfqnl_msg_packet_hw phw;
478
479                 phw.hw_addrlen =
480                         entry->skb->dev->hard_header_parse(entry->skb,
481                                                            phw.hw_addr);
482                 phw.hw_addrlen = htons(phw.hw_addrlen);
483                 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
484         }
485
486         if (entry->skb->tstamp.off_sec) {
487                 struct nfqnl_msg_packet_timestamp ts;
488
489                 ts.sec = cpu_to_be64(skb_tv_base.tv_sec + entry->skb->tstamp.off_sec);
490                 ts.usec = cpu_to_be64(skb_tv_base.tv_usec + entry->skb->tstamp.off_usec);
491
492                 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
493         }
494
495         if (data_len) {
496                 struct nfattr *nfa;
497                 int size = NFA_LENGTH(data_len);
498
499                 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
500                         printk(KERN_WARNING "nf_queue: no tailroom!\n");
501                         goto nlmsg_failure;
502                 }
503
504                 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
505                 nfa->nfa_type = NFQA_PAYLOAD;
506                 nfa->nfa_len = size;
507
508                 if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len))
509                         BUG();
510         }
511                 
512         nlh->nlmsg_len = skb->tail - old_tail;
513         return skb;
514
515 nlmsg_failure:
516 nfattr_failure:
517         if (skb)
518                 kfree_skb(skb);
519         *errp = -EINVAL;
520         if (net_ratelimit())
521                 printk(KERN_ERR "nf_queue: error creating packet message\n");
522         return NULL;
523 }
524
525 static int
526 nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info, 
527                      unsigned int queuenum, void *data)
528 {
529         int status = -EINVAL;
530         struct sk_buff *nskb;
531         struct nfqnl_instance *queue;
532         struct nfqnl_queue_entry *entry;
533
534         QDEBUG("entered\n");
535
536         queue = instance_lookup_get(queuenum);
537         if (!queue) {
538                 QDEBUG("no queue instance matching\n");
539                 return -EINVAL;
540         }
541
542         if (queue->copy_mode == NFQNL_COPY_NONE) {
543                 QDEBUG("mode COPY_NONE, aborting\n");
544                 status = -EAGAIN;
545                 goto err_out_put;
546         }
547
548         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
549         if (entry == NULL) {
550                 if (net_ratelimit())
551                         printk(KERN_ERR 
552                                 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
553                 status = -ENOMEM;
554                 goto err_out_put;
555         }
556
557         entry->info = info;
558         entry->skb = skb;
559         entry->id = atomic_inc_return(&queue->id_sequence);
560
561         nskb = nfqnl_build_packet_message(queue, entry, &status);
562         if (nskb == NULL)
563                 goto err_out_free;
564                 
565         spin_lock_bh(&queue->lock);
566         
567         if (!queue->peer_pid)
568                 goto err_out_free_nskb; 
569
570         if (queue->queue_total >= queue->queue_maxlen) {
571                 queue->queue_dropped++;
572                 status = -ENOSPC;
573                 if (net_ratelimit())
574                           printk(KERN_WARNING "ip_queue: full at %d entries, "
575                                  "dropping packets(s). Dropped: %d\n", 
576                                  queue->queue_total, queue->queue_dropped);
577                 goto err_out_free_nskb;
578         }
579
580         /* nfnetlink_unicast will either free the nskb or add it to a socket */
581         status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
582         if (status < 0) {
583                 queue->queue_user_dropped++;
584                 goto err_out_unlock;
585         }
586
587         __enqueue_entry(queue, entry);
588
589         spin_unlock_bh(&queue->lock);
590         instance_put(queue);
591         return status;
592
593 err_out_free_nskb:
594         kfree_skb(nskb); 
595         
596 err_out_unlock:
597         spin_unlock_bh(&queue->lock);
598
599 err_out_free:
600         kfree(entry);
601 err_out_put:
602         instance_put(queue);
603         return status;
604 }
605
606 static int
607 nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
608 {
609         int diff;
610
611         diff = data_len - e->skb->len;
612         if (diff < 0)
613                 skb_trim(e->skb, data_len);
614         else if (diff > 0) {
615                 if (data_len > 0xFFFF)
616                         return -EINVAL;
617                 if (diff > skb_tailroom(e->skb)) {
618                         struct sk_buff *newskb;
619                         
620                         newskb = skb_copy_expand(e->skb,
621                                                  skb_headroom(e->skb),
622                                                  diff,
623                                                  GFP_ATOMIC);
624                         if (newskb == NULL) {
625                                 printk(KERN_WARNING "ip_queue: OOM "
626                                       "in mangle, dropping packet\n");
627                                 return -ENOMEM;
628                         }
629                         if (e->skb->sk)
630                                 skb_set_owner_w(newskb, e->skb->sk);
631                         kfree_skb(e->skb);
632                         e->skb = newskb;
633                 }
634                 skb_put(e->skb, diff);
635         }
636         if (!skb_make_writable(&e->skb, data_len))
637                 return -ENOMEM;
638         memcpy(e->skb->data, data, data_len);
639
640         return 0;
641 }
642
643 static inline int
644 id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
645 {
646         return (id == e->id);
647 }
648
649 static int
650 nfqnl_set_mode(struct nfqnl_instance *queue,
651                unsigned char mode, unsigned int range)
652 {
653         int status;
654
655         spin_lock_bh(&queue->lock);
656         status = __nfqnl_set_mode(queue, mode, range);
657         spin_unlock_bh(&queue->lock);
658
659         return status;
660 }
661
662 static int
663 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
664 {
665         if (entry->info->indev)
666                 if (entry->info->indev->ifindex == ifindex)
667                         return 1;
668                         
669         if (entry->info->outdev)
670                 if (entry->info->outdev->ifindex == ifindex)
671                         return 1;
672
673         return 0;
674 }
675
676 /* drop all packets with either indev or outdev == ifindex from all queue
677  * instances */
678 static void
679 nfqnl_dev_drop(int ifindex)
680 {
681         int i;
682         
683         QDEBUG("entering for ifindex %u\n", ifindex);
684
685         /* this only looks like we have to hold the readlock for a way too long
686          * time, issue_verdict(),  nf_reinject(), ... - but we always only
687          * issue NF_DROP, which is processed directly in nf_reinject() */
688         read_lock_bh(&instances_lock);
689
690         for  (i = 0; i < INSTANCE_BUCKETS; i++) {
691                 struct hlist_node *tmp;
692                 struct nfqnl_instance *inst;
693                 struct hlist_head *head = &instance_table[i];
694
695                 hlist_for_each_entry(inst, tmp, head, hlist) {
696                         struct nfqnl_queue_entry *entry;
697                         while ((entry = find_dequeue_entry(inst, dev_cmp, 
698                                                            ifindex)) != NULL)
699                                 issue_verdict(entry, NF_DROP);
700                 }
701         }
702
703         read_unlock_bh(&instances_lock);
704 }
705
706 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
707
708 static int
709 nfqnl_rcv_dev_event(struct notifier_block *this,
710                     unsigned long event, void *ptr)
711 {
712         struct net_device *dev = ptr;
713
714         /* Drop any packets associated with the downed device */
715         if (event == NETDEV_DOWN)
716                 nfqnl_dev_drop(dev->ifindex);
717         return NOTIFY_DONE;
718 }
719
720 static struct notifier_block nfqnl_dev_notifier = {
721         .notifier_call  = nfqnl_rcv_dev_event,
722 };
723
724 static int
725 nfqnl_rcv_nl_event(struct notifier_block *this,
726                    unsigned long event, void *ptr)
727 {
728         struct netlink_notify *n = ptr;
729
730         if (event == NETLINK_URELEASE &&
731             n->protocol == NETLINK_NETFILTER && n->pid) {
732                 int i;
733
734                 /* destroy all instances for this pid */
735                 write_lock_bh(&instances_lock);
736                 for  (i = 0; i < INSTANCE_BUCKETS; i++) {
737                         struct hlist_node *tmp, *t2;
738                         struct nfqnl_instance *inst;
739                         struct hlist_head *head = &instance_table[i];
740
741                         hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
742                                 if (n->pid == inst->peer_pid)
743                                         __instance_destroy(inst);
744                         }
745                 }
746                 write_unlock_bh(&instances_lock);
747         }
748         return NOTIFY_DONE;
749 }
750
751 static struct notifier_block nfqnl_rtnl_notifier = {
752         .notifier_call  = nfqnl_rcv_nl_event,
753 };
754
755 static const int nfqa_verdict_min[NFQA_MAX] = {
756         [NFQA_VERDICT_HDR-1]    = sizeof(struct nfqnl_msg_verdict_hdr),
757         [NFQA_MARK-1]           = sizeof(u_int32_t),
758         [NFQA_PAYLOAD-1]        = 0,
759 };
760
761 static int
762 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
763                    struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
764 {
765         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
766         u_int16_t queue_num = ntohs(nfmsg->res_id);
767
768         struct nfqnl_msg_verdict_hdr *vhdr;
769         struct nfqnl_instance *queue;
770         unsigned int verdict;
771         struct nfqnl_queue_entry *entry;
772         int err;
773
774         if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
775                 QDEBUG("bad attribute size\n");
776                 return -EINVAL;
777         }
778
779         queue = instance_lookup_get(queue_num);
780         if (!queue)
781                 return -ENODEV;
782
783         if (queue->peer_pid != NETLINK_CB(skb).pid) {
784                 err = -EPERM;
785                 goto err_out_put;
786         }
787
788         if (!nfqa[NFQA_VERDICT_HDR-1]) {
789                 err = -EINVAL;
790                 goto err_out_put;
791         }
792
793         vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
794         verdict = ntohl(vhdr->verdict);
795
796         if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
797                 err = -EINVAL;
798                 goto err_out_put;
799         }
800
801         entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
802         if (entry == NULL) {
803                 err = -ENOENT;
804                 goto err_out_put;
805         }
806
807         if (nfqa[NFQA_PAYLOAD-1]) {
808                 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
809                                  NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
810                         verdict = NF_DROP;
811         }
812
813         if (nfqa[NFQA_MARK-1])
814                 skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1]));
815                 
816         issue_verdict(entry, verdict);
817         instance_put(queue);
818         return 0;
819
820 err_out_put:
821         instance_put(queue);
822         return err;
823 }
824
825 static int
826 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
827                   struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
828 {
829         return -ENOTSUPP;
830 }
831
832 static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
833         [NFQA_CFG_CMD-1]        = sizeof(struct nfqnl_msg_config_cmd),
834         [NFQA_CFG_PARAMS-1]     = sizeof(struct nfqnl_msg_config_params),
835 };
836
837 static struct nf_queue_handler nfqh = {
838         .name   = "nf_queue",
839         .outfn  = &nfqnl_enqueue_packet,
840 };
841
842 static int
843 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
844                   struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
845 {
846         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
847         u_int16_t queue_num = ntohs(nfmsg->res_id);
848         struct nfqnl_instance *queue;
849         int ret = 0;
850
851         QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
852
853         if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
854                 QDEBUG("bad attribute size\n");
855                 return -EINVAL;
856         }
857
858         queue = instance_lookup_get(queue_num);
859         if (nfqa[NFQA_CFG_CMD-1]) {
860                 struct nfqnl_msg_config_cmd *cmd;
861                 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
862                 QDEBUG("found CFG_CMD\n");
863
864                 switch (cmd->command) {
865                 case NFQNL_CFG_CMD_BIND:
866                         if (queue)
867                                 return -EBUSY;
868
869                         queue = instance_create(queue_num, NETLINK_CB(skb).pid);
870                         if (!queue)
871                                 return -EINVAL;
872                         break;
873                 case NFQNL_CFG_CMD_UNBIND:
874                         if (!queue)
875                                 return -ENODEV;
876
877                         if (queue->peer_pid != NETLINK_CB(skb).pid) {
878                                 ret = -EPERM;
879                                 goto out_put;
880                         }
881
882                         instance_destroy(queue);
883                         break;
884                 case NFQNL_CFG_CMD_PF_BIND:
885                         QDEBUG("registering queue handler for pf=%u\n",
886                                 ntohs(cmd->pf));
887                         ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
888                         break;
889                 case NFQNL_CFG_CMD_PF_UNBIND:
890                         QDEBUG("unregistering queue handler for pf=%u\n",
891                                 ntohs(cmd->pf));
892                         /* This is a bug and a feature.  We can unregister
893                          * other handlers(!) */
894                         ret = nf_unregister_queue_handler(ntohs(cmd->pf));
895                         break;
896                 default:
897                         ret = -EINVAL;
898                         break;
899                 }
900         } else {
901                 if (!queue) {
902                         QDEBUG("no config command, and no instance ENOENT\n");
903                         ret = -ENOENT;
904                         goto out_put;
905                 }
906
907                 if (queue->peer_pid != NETLINK_CB(skb).pid) {
908                         QDEBUG("no config command, and wrong pid\n");
909                         ret = -EPERM;
910                         goto out_put;
911                 }
912         }
913
914         if (nfqa[NFQA_CFG_PARAMS-1]) {
915                 struct nfqnl_msg_config_params *params;
916                 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
917
918                 nfqnl_set_mode(queue, params->copy_mode,
919                                 ntohl(params->copy_range));
920         }
921
922 out_put:
923         instance_put(queue);
924         return ret;
925 }
926
927 static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
928         [NFQNL_MSG_PACKET]      = { .call = nfqnl_recv_unsupp,
929                                     .attr_count = NFQA_MAX,
930                                     .cap_required = CAP_NET_ADMIN },
931         [NFQNL_MSG_VERDICT]     = { .call = nfqnl_recv_verdict,
932                                     .attr_count = NFQA_MAX,
933                                     .cap_required = CAP_NET_ADMIN },
934         [NFQNL_MSG_CONFIG]      = { .call = nfqnl_recv_config,
935                                     .attr_count = NFQA_CFG_MAX,
936                                     .cap_required = CAP_NET_ADMIN },
937 };
938
939 static struct nfnetlink_subsystem nfqnl_subsys = {
940         .name           = "nf_queue",
941         .subsys_id      = NFNL_SUBSYS_QUEUE,
942         .cb_count       = NFQNL_MSG_MAX,
943         .cb             = nfqnl_cb,
944 };
945
946 #ifdef CONFIG_PROC_FS
947 struct iter_state {
948         unsigned int bucket;
949 };
950
951 static struct hlist_node *get_first(struct seq_file *seq)
952 {
953         struct iter_state *st = seq->private;
954
955         if (!st)
956                 return NULL;
957
958         for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
959                 if (!hlist_empty(&instance_table[st->bucket]))
960                         return instance_table[st->bucket].first;
961         }
962         return NULL;
963 }
964
965 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
966 {
967         struct iter_state *st = seq->private;
968
969         h = h->next;
970         while (!h) {
971                 if (++st->bucket >= INSTANCE_BUCKETS)
972                         return NULL;
973
974                 h = instance_table[st->bucket].first;
975         }
976         return h;
977 }
978
979 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
980 {
981         struct hlist_node *head;
982         head = get_first(seq);
983
984         if (head)
985                 while (pos && (head = get_next(seq, head)))
986                         pos--;
987         return pos ? NULL : head;
988 }
989
990 static void *seq_start(struct seq_file *seq, loff_t *pos)
991 {
992         read_lock_bh(&instances_lock);
993         return get_idx(seq, *pos);
994 }
995
996 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
997 {
998         (*pos)++;
999         return get_next(s, v);
1000 }
1001
1002 static void seq_stop(struct seq_file *s, void *v)
1003 {
1004         read_unlock_bh(&instances_lock);
1005 }
1006
1007 static int seq_show(struct seq_file *s, void *v)
1008 {
1009         const struct nfqnl_instance *inst = v;
1010
1011         return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1012                           inst->queue_num,
1013                           inst->peer_pid, inst->queue_total,
1014                           inst->copy_mode, inst->copy_range,
1015                           inst->queue_dropped, inst->queue_user_dropped,
1016                           atomic_read(&inst->id_sequence),
1017                           atomic_read(&inst->use));
1018 }
1019
1020 static struct seq_operations nfqnl_seq_ops = {
1021         .start  = seq_start,
1022         .next   = seq_next,
1023         .stop   = seq_stop,
1024         .show   = seq_show,
1025 };
1026
1027 static int nfqnl_open(struct inode *inode, struct file *file)
1028 {
1029         struct seq_file *seq;
1030         struct iter_state *is;
1031         int ret;
1032
1033         is = kmalloc(sizeof(*is), GFP_KERNEL);
1034         if (!is)
1035                 return -ENOMEM;
1036         memset(is, 0, sizeof(*is));
1037         ret = seq_open(file, &nfqnl_seq_ops);
1038         if (ret < 0)
1039                 goto out_free;
1040         seq = file->private_data;
1041         seq->private = is;
1042         return ret;
1043 out_free:
1044         kfree(is);
1045         return ret;
1046 }
1047
1048 static struct file_operations nfqnl_file_ops = {
1049         .owner   = THIS_MODULE,
1050         .open    = nfqnl_open,
1051         .read    = seq_read,
1052         .llseek  = seq_lseek,
1053         .release = seq_release_private,
1054 };
1055
1056 #endif /* PROC_FS */
1057
1058 static int
1059 init_or_cleanup(int init)
1060 {
1061         int i, status = -ENOMEM;
1062 #ifdef CONFIG_PROC_FS
1063         struct proc_dir_entry *proc_nfqueue;
1064 #endif
1065         
1066         if (!init)
1067                 goto cleanup;
1068
1069         for (i = 0; i < INSTANCE_BUCKETS; i++)
1070                 INIT_HLIST_HEAD(&instance_table[i]);
1071
1072         netlink_register_notifier(&nfqnl_rtnl_notifier);
1073         status = nfnetlink_subsys_register(&nfqnl_subsys);
1074         if (status < 0) {
1075                 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1076                 goto cleanup_netlink_notifier;
1077         }
1078
1079 #ifdef CONFIG_PROC_FS
1080         proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1081                                          proc_net_netfilter);
1082         if (!proc_nfqueue)
1083                 goto cleanup_subsys;
1084         proc_nfqueue->proc_fops = &nfqnl_file_ops;
1085 #endif
1086
1087         register_netdevice_notifier(&nfqnl_dev_notifier);
1088
1089         return status;
1090
1091 cleanup:
1092         nf_unregister_queue_handlers(&nfqh);
1093         unregister_netdevice_notifier(&nfqnl_dev_notifier);
1094 #ifdef CONFIG_PROC_FS
1095         remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1096 cleanup_subsys:
1097 #endif  
1098         nfnetlink_subsys_unregister(&nfqnl_subsys);
1099 cleanup_netlink_notifier:
1100         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1101         return status;
1102 }
1103
1104 static int __init init(void)
1105 {
1106         
1107         return init_or_cleanup(1);
1108 }
1109
1110 static void __exit fini(void)
1111 {
1112         init_or_cleanup(0);
1113 }
1114
1115 MODULE_DESCRIPTION("netfilter packet queue handler");
1116 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1117 MODULE_LICENSE("GPL");
1118 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1119
1120 module_init(init);
1121 module_exit(fini);