Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/jkirsher/net...
[pandora-kernel.git] / net / netfilter / nfnetlink_queue_core.c
1 /*
2  * This is a module which is used for queueing packets and communicating with
3  * userspace via nfnetlink.
4  *
5  * (C) 2005 by Harald Welte <laforge@netfilter.org>
6  * (C) 2007 by Patrick McHardy <kaber@trash.net>
7  *
8  * Based on the old ipv4-only ip_queue.c:
9  * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
10  * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  */
17 #include <linux/module.h>
18 #include <linux/skbuff.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/proc_fs.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/netfilter_ipv6.h>
28 #include <linux/netfilter/nfnetlink.h>
29 #include <linux/netfilter/nfnetlink_queue.h>
30 #include <linux/list.h>
31 #include <net/sock.h>
32 #include <net/netfilter/nf_queue.h>
33 #include <net/netfilter/nfnetlink_queue.h>
34
35 #include <linux/atomic.h>
36
37 #ifdef CONFIG_BRIDGE_NETFILTER
38 #include "../bridge/br_private.h"
39 #endif
40
41 #define NFQNL_QMAX_DEFAULT 1024
42
43 struct nfqnl_instance {
44         struct hlist_node hlist;                /* global list of queues */
45         struct rcu_head rcu;
46
47         int peer_portid;
48         unsigned int queue_maxlen;
49         unsigned int copy_range;
50         unsigned int queue_dropped;
51         unsigned int queue_user_dropped;
52
53
54         u_int16_t queue_num;                    /* number of this queue */
55         u_int8_t copy_mode;
56         u_int32_t flags;                        /* Set using NFQA_CFG_FLAGS */
57 /*
58  * Following fields are dirtied for each queued packet,
59  * keep them in same cache line if possible.
60  */
61         spinlock_t      lock;
62         unsigned int    queue_total;
63         unsigned int    id_sequence;            /* 'sequence' of pkt ids */
64         struct list_head queue_list;            /* packets in queue */
65 };
66
67 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
68
69 static DEFINE_SPINLOCK(instances_lock);
70
71 #define INSTANCE_BUCKETS        16
72 static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
73
74 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
75 {
76         return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
77 }
78
79 static struct nfqnl_instance *
80 instance_lookup(u_int16_t queue_num)
81 {
82         struct hlist_head *head;
83         struct nfqnl_instance *inst;
84
85         head = &instance_table[instance_hashfn(queue_num)];
86         hlist_for_each_entry_rcu(inst, head, hlist) {
87                 if (inst->queue_num == queue_num)
88                         return inst;
89         }
90         return NULL;
91 }
92
93 static struct nfqnl_instance *
94 instance_create(u_int16_t queue_num, int portid)
95 {
96         struct nfqnl_instance *inst;
97         unsigned int h;
98         int err;
99
100         spin_lock(&instances_lock);
101         if (instance_lookup(queue_num)) {
102                 err = -EEXIST;
103                 goto out_unlock;
104         }
105
106         inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
107         if (!inst) {
108                 err = -ENOMEM;
109                 goto out_unlock;
110         }
111
112         inst->queue_num = queue_num;
113         inst->peer_portid = portid;
114         inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
115         inst->copy_range = 0xffff;
116         inst->copy_mode = NFQNL_COPY_NONE;
117         spin_lock_init(&inst->lock);
118         INIT_LIST_HEAD(&inst->queue_list);
119
120         if (!try_module_get(THIS_MODULE)) {
121                 err = -EAGAIN;
122                 goto out_free;
123         }
124
125         h = instance_hashfn(queue_num);
126         hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
127
128         spin_unlock(&instances_lock);
129
130         return inst;
131
132 out_free:
133         kfree(inst);
134 out_unlock:
135         spin_unlock(&instances_lock);
136         return ERR_PTR(err);
137 }
138
139 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
140                         unsigned long data);
141
142 static void
143 instance_destroy_rcu(struct rcu_head *head)
144 {
145         struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
146                                                    rcu);
147
148         nfqnl_flush(inst, NULL, 0);
149         kfree(inst);
150         module_put(THIS_MODULE);
151 }
152
153 static void
154 __instance_destroy(struct nfqnl_instance *inst)
155 {
156         hlist_del_rcu(&inst->hlist);
157         call_rcu(&inst->rcu, instance_destroy_rcu);
158 }
159
160 static void
161 instance_destroy(struct nfqnl_instance *inst)
162 {
163         spin_lock(&instances_lock);
164         __instance_destroy(inst);
165         spin_unlock(&instances_lock);
166 }
167
168 static inline void
169 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
170 {
171        list_add_tail(&entry->list, &queue->queue_list);
172        queue->queue_total++;
173 }
174
175 static void
176 __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
177 {
178         list_del(&entry->list);
179         queue->queue_total--;
180 }
181
182 static struct nf_queue_entry *
183 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
184 {
185         struct nf_queue_entry *entry = NULL, *i;
186
187         spin_lock_bh(&queue->lock);
188
189         list_for_each_entry(i, &queue->queue_list, list) {
190                 if (i->id == id) {
191                         entry = i;
192                         break;
193                 }
194         }
195
196         if (entry)
197                 __dequeue_entry(queue, entry);
198
199         spin_unlock_bh(&queue->lock);
200
201         return entry;
202 }
203
204 static void
205 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
206 {
207         struct nf_queue_entry *entry, *next;
208
209         spin_lock_bh(&queue->lock);
210         list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
211                 if (!cmpfn || cmpfn(entry, data)) {
212                         list_del(&entry->list);
213                         queue->queue_total--;
214                         nf_reinject(entry, NF_DROP);
215                 }
216         }
217         spin_unlock_bh(&queue->lock);
218 }
219
220 static void
221 nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)
222 {
223         int i, j = 0;
224         int plen = 0; /* length of skb->head fragment */
225         struct page *page;
226         unsigned int offset;
227
228         /* dont bother with small payloads */
229         if (len <= skb_tailroom(to)) {
230                 skb_copy_bits(from, 0, skb_put(to, len), len);
231                 return;
232         }
233
234         if (hlen) {
235                 skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
236                 len -= hlen;
237         } else {
238                 plen = min_t(int, skb_headlen(from), len);
239                 if (plen) {
240                         page = virt_to_head_page(from->head);
241                         offset = from->data - (unsigned char *)page_address(page);
242                         __skb_fill_page_desc(to, 0, page, offset, plen);
243                         get_page(page);
244                         j = 1;
245                         len -= plen;
246                 }
247         }
248
249         to->truesize += len + plen;
250         to->len += len + plen;
251         to->data_len += len + plen;
252
253         for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
254                 if (!len)
255                         break;
256                 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
257                 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
258                 len -= skb_shinfo(to)->frags[j].size;
259                 skb_frag_ref(to, j);
260                 j++;
261         }
262         skb_shinfo(to)->nr_frags = j;
263 }
264
265 static struct sk_buff *
266 nfqnl_build_packet_message(struct nfqnl_instance *queue,
267                            struct nf_queue_entry *entry,
268                            __be32 **packet_id_ptr)
269 {
270         size_t size;
271         size_t data_len = 0, cap_len = 0;
272         int hlen = 0;
273         struct sk_buff *skb;
274         struct nlattr *nla;
275         struct nfqnl_msg_packet_hdr *pmsg;
276         struct nlmsghdr *nlh;
277         struct nfgenmsg *nfmsg;
278         struct sk_buff *entskb = entry->skb;
279         struct net_device *indev;
280         struct net_device *outdev;
281         struct nf_conn *ct = NULL;
282         enum ip_conntrack_info uninitialized_var(ctinfo);
283
284         size =    nlmsg_total_size(sizeof(struct nfgenmsg))
285                 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
286                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
287                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
288 #ifdef CONFIG_BRIDGE_NETFILTER
289                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
290                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
291 #endif
292                 + nla_total_size(sizeof(u_int32_t))     /* mark */
293                 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
294                 + nla_total_size(sizeof(u_int32_t));    /* cap_len */
295
296         if (entskb->tstamp.tv64)
297                 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
298
299         outdev = entry->outdev;
300
301         switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
302         case NFQNL_COPY_META:
303         case NFQNL_COPY_NONE:
304                 break;
305
306         case NFQNL_COPY_PACKET:
307                 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
308                     skb_checksum_help(entskb))
309                         return NULL;
310
311                 data_len = ACCESS_ONCE(queue->copy_range);
312                 if (data_len == 0 || data_len > entskb->len)
313                         data_len = entskb->len;
314
315
316                 if (!entskb->head_frag ||
317                     skb_headlen(entskb) < L1_CACHE_BYTES ||
318                     skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS)
319                         hlen = skb_headlen(entskb);
320
321                 if (skb_has_frag_list(entskb))
322                         hlen = entskb->len;
323                 hlen = min_t(int, data_len, hlen);
324                 size += sizeof(struct nlattr) + hlen;
325                 cap_len = entskb->len;
326                 break;
327         }
328
329         if (queue->flags & NFQA_CFG_F_CONNTRACK)
330                 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
331
332         skb = alloc_skb(size, GFP_ATOMIC);
333         if (!skb)
334                 return NULL;
335
336         nlh = nlmsg_put(skb, 0, 0,
337                         NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
338                         sizeof(struct nfgenmsg), 0);
339         if (!nlh) {
340                 kfree_skb(skb);
341                 return NULL;
342         }
343         nfmsg = nlmsg_data(nlh);
344         nfmsg->nfgen_family = entry->pf;
345         nfmsg->version = NFNETLINK_V0;
346         nfmsg->res_id = htons(queue->queue_num);
347
348         nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
349         pmsg = nla_data(nla);
350         pmsg->hw_protocol       = entskb->protocol;
351         pmsg->hook              = entry->hook;
352         *packet_id_ptr          = &pmsg->packet_id;
353
354         indev = entry->indev;
355         if (indev) {
356 #ifndef CONFIG_BRIDGE_NETFILTER
357                 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
358                         goto nla_put_failure;
359 #else
360                 if (entry->pf == PF_BRIDGE) {
361                         /* Case 1: indev is physical input device, we need to
362                          * look for bridge group (when called from
363                          * netfilter_bridge) */
364                         if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
365                                          htonl(indev->ifindex)) ||
366                         /* this is the bridge group "brX" */
367                         /* rcu_read_lock()ed by __nf_queue */
368                             nla_put_be32(skb, NFQA_IFINDEX_INDEV,
369                                          htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
370                                 goto nla_put_failure;
371                 } else {
372                         /* Case 2: indev is bridge group, we need to look for
373                          * physical device (when called from ipv4) */
374                         if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
375                                          htonl(indev->ifindex)))
376                                 goto nla_put_failure;
377                         if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
378                             nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
379                                          htonl(entskb->nf_bridge->physindev->ifindex)))
380                                 goto nla_put_failure;
381                 }
382 #endif
383         }
384
385         if (outdev) {
386 #ifndef CONFIG_BRIDGE_NETFILTER
387                 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
388                         goto nla_put_failure;
389 #else
390                 if (entry->pf == PF_BRIDGE) {
391                         /* Case 1: outdev is physical output device, we need to
392                          * look for bridge group (when called from
393                          * netfilter_bridge) */
394                         if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
395                                          htonl(outdev->ifindex)) ||
396                         /* this is the bridge group "brX" */
397                         /* rcu_read_lock()ed by __nf_queue */
398                             nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
399                                          htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
400                                 goto nla_put_failure;
401                 } else {
402                         /* Case 2: outdev is bridge group, we need to look for
403                          * physical output device (when called from ipv4) */
404                         if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
405                                          htonl(outdev->ifindex)))
406                                 goto nla_put_failure;
407                         if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
408                             nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
409                                          htonl(entskb->nf_bridge->physoutdev->ifindex)))
410                                 goto nla_put_failure;
411                 }
412 #endif
413         }
414
415         if (entskb->mark &&
416             nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
417                 goto nla_put_failure;
418
419         if (indev && entskb->dev &&
420             entskb->mac_header != entskb->network_header) {
421                 struct nfqnl_msg_packet_hw phw;
422                 int len = dev_parse_header(entskb, phw.hw_addr);
423                 if (len) {
424                         phw.hw_addrlen = htons(len);
425                         if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
426                                 goto nla_put_failure;
427                 }
428         }
429
430         if (entskb->tstamp.tv64) {
431                 struct nfqnl_msg_packet_timestamp ts;
432                 struct timeval tv = ktime_to_timeval(entskb->tstamp);
433                 ts.sec = cpu_to_be64(tv.tv_sec);
434                 ts.usec = cpu_to_be64(tv.tv_usec);
435
436                 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
437                         goto nla_put_failure;
438         }
439
440         if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
441                 goto nla_put_failure;
442
443         if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
444                 goto nla_put_failure;
445
446         if (data_len) {
447                 struct nlattr *nla;
448
449                 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
450                         goto nla_put_failure;
451
452                 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
453                 nla->nla_type = NFQA_PAYLOAD;
454                 nla->nla_len = nla_attr_size(data_len);
455
456                 nfqnl_zcopy(skb, entskb, data_len, hlen);
457         }
458
459         nlh->nlmsg_len = skb->len;
460         return skb;
461
462 nla_put_failure:
463         kfree_skb(skb);
464         net_err_ratelimited("nf_queue: error creating packet message\n");
465         return NULL;
466 }
467
468 static int
469 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
470 {
471         struct sk_buff *nskb;
472         struct nfqnl_instance *queue;
473         int err = -ENOBUFS;
474         __be32 *packet_id_ptr;
475         int failopen = 0;
476
477         /* rcu_read_lock()ed by nf_hook_slow() */
478         queue = instance_lookup(queuenum);
479         if (!queue) {
480                 err = -ESRCH;
481                 goto err_out;
482         }
483
484         if (queue->copy_mode == NFQNL_COPY_NONE) {
485                 err = -EINVAL;
486                 goto err_out;
487         }
488
489         nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
490         if (nskb == NULL) {
491                 err = -ENOMEM;
492                 goto err_out;
493         }
494         spin_lock_bh(&queue->lock);
495
496         if (!queue->peer_portid) {
497                 err = -EINVAL;
498                 goto err_out_free_nskb;
499         }
500         if (queue->queue_total >= queue->queue_maxlen) {
501                 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
502                         failopen = 1;
503                         err = 0;
504                 } else {
505                         queue->queue_dropped++;
506                         net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
507                                              queue->queue_total);
508                 }
509                 goto err_out_free_nskb;
510         }
511         entry->id = ++queue->id_sequence;
512         *packet_id_ptr = htonl(entry->id);
513
514         /* nfnetlink_unicast will either free the nskb or add it to a socket */
515         err = nfnetlink_unicast(nskb, &init_net, queue->peer_portid, MSG_DONTWAIT);
516         if (err < 0) {
517                 queue->queue_user_dropped++;
518                 goto err_out_unlock;
519         }
520
521         __enqueue_entry(queue, entry);
522
523         spin_unlock_bh(&queue->lock);
524         return 0;
525
526 err_out_free_nskb:
527         kfree_skb(nskb);
528 err_out_unlock:
529         spin_unlock_bh(&queue->lock);
530         if (failopen)
531                 nf_reinject(entry, NF_ACCEPT);
532 err_out:
533         return err;
534 }
535
536 static int
537 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
538 {
539         struct sk_buff *nskb;
540
541         if (diff < 0) {
542                 if (pskb_trim(e->skb, data_len))
543                         return -ENOMEM;
544         } else if (diff > 0) {
545                 if (data_len > 0xFFFF)
546                         return -EINVAL;
547                 if (diff > skb_tailroom(e->skb)) {
548                         nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
549                                                diff, GFP_ATOMIC);
550                         if (!nskb) {
551                                 printk(KERN_WARNING "nf_queue: OOM "
552                                       "in mangle, dropping packet\n");
553                                 return -ENOMEM;
554                         }
555                         kfree_skb(e->skb);
556                         e->skb = nskb;
557                 }
558                 skb_put(e->skb, diff);
559         }
560         if (!skb_make_writable(e->skb, data_len))
561                 return -ENOMEM;
562         skb_copy_to_linear_data(e->skb, data, data_len);
563         e->skb->ip_summed = CHECKSUM_NONE;
564         return 0;
565 }
566
567 static int
568 nfqnl_set_mode(struct nfqnl_instance *queue,
569                unsigned char mode, unsigned int range)
570 {
571         int status = 0;
572
573         spin_lock_bh(&queue->lock);
574         switch (mode) {
575         case NFQNL_COPY_NONE:
576         case NFQNL_COPY_META:
577                 queue->copy_mode = mode;
578                 queue->copy_range = 0;
579                 break;
580
581         case NFQNL_COPY_PACKET:
582                 queue->copy_mode = mode;
583                 /* We're using struct nlattr which has 16bit nla_len. Note that
584                  * nla_len includes the header length. Thus, the maximum packet
585                  * length that we support is 65531 bytes. We send truncated
586                  * packets if the specified length is larger than that.
587                  */
588                 if (range > 0xffff - NLA_HDRLEN)
589                         queue->copy_range = 0xffff - NLA_HDRLEN;
590                 else
591                         queue->copy_range = range;
592                 break;
593
594         default:
595                 status = -EINVAL;
596
597         }
598         spin_unlock_bh(&queue->lock);
599
600         return status;
601 }
602
603 static int
604 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
605 {
606         if (entry->indev)
607                 if (entry->indev->ifindex == ifindex)
608                         return 1;
609         if (entry->outdev)
610                 if (entry->outdev->ifindex == ifindex)
611                         return 1;
612 #ifdef CONFIG_BRIDGE_NETFILTER
613         if (entry->skb->nf_bridge) {
614                 if (entry->skb->nf_bridge->physindev &&
615                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
616                         return 1;
617                 if (entry->skb->nf_bridge->physoutdev &&
618                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
619                         return 1;
620         }
621 #endif
622         return 0;
623 }
624
625 /* drop all packets with either indev or outdev == ifindex from all queue
626  * instances */
627 static void
628 nfqnl_dev_drop(int ifindex)
629 {
630         int i;
631
632         rcu_read_lock();
633
634         for (i = 0; i < INSTANCE_BUCKETS; i++) {
635                 struct nfqnl_instance *inst;
636                 struct hlist_head *head = &instance_table[i];
637
638                 hlist_for_each_entry_rcu(inst, head, hlist)
639                         nfqnl_flush(inst, dev_cmp, ifindex);
640         }
641
642         rcu_read_unlock();
643 }
644
645 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
646
647 static int
648 nfqnl_rcv_dev_event(struct notifier_block *this,
649                     unsigned long event, void *ptr)
650 {
651         struct net_device *dev = ptr;
652
653         if (!net_eq(dev_net(dev), &init_net))
654                 return NOTIFY_DONE;
655
656         /* Drop any packets associated with the downed device */
657         if (event == NETDEV_DOWN)
658                 nfqnl_dev_drop(dev->ifindex);
659         return NOTIFY_DONE;
660 }
661
662 static struct notifier_block nfqnl_dev_notifier = {
663         .notifier_call  = nfqnl_rcv_dev_event,
664 };
665
666 static int
667 nfqnl_rcv_nl_event(struct notifier_block *this,
668                    unsigned long event, void *ptr)
669 {
670         struct netlink_notify *n = ptr;
671
672         if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
673                 int i;
674
675                 /* destroy all instances for this portid */
676                 spin_lock(&instances_lock);
677                 for (i = 0; i < INSTANCE_BUCKETS; i++) {
678                         struct hlist_node *t2;
679                         struct nfqnl_instance *inst;
680                         struct hlist_head *head = &instance_table[i];
681
682                         hlist_for_each_entry_safe(inst, t2, head, hlist) {
683                                 if ((n->net == &init_net) &&
684                                     (n->portid == inst->peer_portid))
685                                         __instance_destroy(inst);
686                         }
687                 }
688                 spin_unlock(&instances_lock);
689         }
690         return NOTIFY_DONE;
691 }
692
693 static struct notifier_block nfqnl_rtnl_notifier = {
694         .notifier_call  = nfqnl_rcv_nl_event,
695 };
696
697 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
698         [NFQA_VERDICT_HDR]      = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
699         [NFQA_MARK]             = { .type = NLA_U32 },
700         [NFQA_PAYLOAD]          = { .type = NLA_UNSPEC },
701         [NFQA_CT]               = { .type = NLA_UNSPEC },
702 };
703
704 static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
705         [NFQA_VERDICT_HDR]      = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
706         [NFQA_MARK]             = { .type = NLA_U32 },
707 };
708
709 static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlportid)
710 {
711         struct nfqnl_instance *queue;
712
713         queue = instance_lookup(queue_num);
714         if (!queue)
715                 return ERR_PTR(-ENODEV);
716
717         if (queue->peer_portid != nlportid)
718                 return ERR_PTR(-EPERM);
719
720         return queue;
721 }
722
723 static struct nfqnl_msg_verdict_hdr*
724 verdicthdr_get(const struct nlattr * const nfqa[])
725 {
726         struct nfqnl_msg_verdict_hdr *vhdr;
727         unsigned int verdict;
728
729         if (!nfqa[NFQA_VERDICT_HDR])
730                 return NULL;
731
732         vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
733         verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
734         if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
735                 return NULL;
736         return vhdr;
737 }
738
739 static int nfq_id_after(unsigned int id, unsigned int max)
740 {
741         return (int)(id - max) > 0;
742 }
743
744 static int
745 nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
746                    const struct nlmsghdr *nlh,
747                    const struct nlattr * const nfqa[])
748 {
749         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
750         struct nf_queue_entry *entry, *tmp;
751         unsigned int verdict, maxid;
752         struct nfqnl_msg_verdict_hdr *vhdr;
753         struct nfqnl_instance *queue;
754         LIST_HEAD(batch_list);
755         u16 queue_num = ntohs(nfmsg->res_id);
756
757         queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).portid);
758         if (IS_ERR(queue))
759                 return PTR_ERR(queue);
760
761         vhdr = verdicthdr_get(nfqa);
762         if (!vhdr)
763                 return -EINVAL;
764
765         verdict = ntohl(vhdr->verdict);
766         maxid = ntohl(vhdr->id);
767
768         spin_lock_bh(&queue->lock);
769
770         list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
771                 if (nfq_id_after(entry->id, maxid))
772                         break;
773                 __dequeue_entry(queue, entry);
774                 list_add_tail(&entry->list, &batch_list);
775         }
776
777         spin_unlock_bh(&queue->lock);
778
779         if (list_empty(&batch_list))
780                 return -ENOENT;
781
782         list_for_each_entry_safe(entry, tmp, &batch_list, list) {
783                 if (nfqa[NFQA_MARK])
784                         entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
785                 nf_reinject(entry, verdict);
786         }
787         return 0;
788 }
789
790 static int
791 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
792                    const struct nlmsghdr *nlh,
793                    const struct nlattr * const nfqa[])
794 {
795         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
796         u_int16_t queue_num = ntohs(nfmsg->res_id);
797
798         struct nfqnl_msg_verdict_hdr *vhdr;
799         struct nfqnl_instance *queue;
800         unsigned int verdict;
801         struct nf_queue_entry *entry;
802         enum ip_conntrack_info uninitialized_var(ctinfo);
803         struct nf_conn *ct = NULL;
804
805         queue = instance_lookup(queue_num);
806         if (!queue)
807
808         queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).portid);
809         if (IS_ERR(queue))
810                 return PTR_ERR(queue);
811
812         vhdr = verdicthdr_get(nfqa);
813         if (!vhdr)
814                 return -EINVAL;
815
816         verdict = ntohl(vhdr->verdict);
817
818         entry = find_dequeue_entry(queue, ntohl(vhdr->id));
819         if (entry == NULL)
820                 return -ENOENT;
821
822         rcu_read_lock();
823         if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
824                 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
825
826         if (nfqa[NFQA_PAYLOAD]) {
827                 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
828                 int diff = payload_len - entry->skb->len;
829
830                 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
831                                  payload_len, entry, diff) < 0)
832                         verdict = NF_DROP;
833
834                 if (ct)
835                         nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
836         }
837         rcu_read_unlock();
838
839         if (nfqa[NFQA_MARK])
840                 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
841
842         nf_reinject(entry, verdict);
843         return 0;
844 }
845
846 static int
847 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
848                   const struct nlmsghdr *nlh,
849                   const struct nlattr * const nfqa[])
850 {
851         return -ENOTSUPP;
852 }
853
854 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
855         [NFQA_CFG_CMD]          = { .len = sizeof(struct nfqnl_msg_config_cmd) },
856         [NFQA_CFG_PARAMS]       = { .len = sizeof(struct nfqnl_msg_config_params) },
857 };
858
859 static const struct nf_queue_handler nfqh = {
860         .outfn  = &nfqnl_enqueue_packet,
861 };
862
863 static int
864 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
865                   const struct nlmsghdr *nlh,
866                   const struct nlattr * const nfqa[])
867 {
868         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
869         u_int16_t queue_num = ntohs(nfmsg->res_id);
870         struct nfqnl_instance *queue;
871         struct nfqnl_msg_config_cmd *cmd = NULL;
872         int ret = 0;
873
874         if (nfqa[NFQA_CFG_CMD]) {
875                 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
876
877                 /* Obsolete commands without queue context */
878                 switch (cmd->command) {
879                 case NFQNL_CFG_CMD_PF_BIND: return 0;
880                 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
881                 }
882         }
883
884         rcu_read_lock();
885         queue = instance_lookup(queue_num);
886         if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
887                 ret = -EPERM;
888                 goto err_out_unlock;
889         }
890
891         if (cmd != NULL) {
892                 switch (cmd->command) {
893                 case NFQNL_CFG_CMD_BIND:
894                         if (queue) {
895                                 ret = -EBUSY;
896                                 goto err_out_unlock;
897                         }
898                         queue = instance_create(queue_num, NETLINK_CB(skb).portid);
899                         if (IS_ERR(queue)) {
900                                 ret = PTR_ERR(queue);
901                                 goto err_out_unlock;
902                         }
903                         break;
904                 case NFQNL_CFG_CMD_UNBIND:
905                         if (!queue) {
906                                 ret = -ENODEV;
907                                 goto err_out_unlock;
908                         }
909                         instance_destroy(queue);
910                         break;
911                 case NFQNL_CFG_CMD_PF_BIND:
912                 case NFQNL_CFG_CMD_PF_UNBIND:
913                         break;
914                 default:
915                         ret = -ENOTSUPP;
916                         break;
917                 }
918         }
919
920         if (nfqa[NFQA_CFG_PARAMS]) {
921                 struct nfqnl_msg_config_params *params;
922
923                 if (!queue) {
924                         ret = -ENODEV;
925                         goto err_out_unlock;
926                 }
927                 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
928                 nfqnl_set_mode(queue, params->copy_mode,
929                                 ntohl(params->copy_range));
930         }
931
932         if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
933                 __be32 *queue_maxlen;
934
935                 if (!queue) {
936                         ret = -ENODEV;
937                         goto err_out_unlock;
938                 }
939                 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
940                 spin_lock_bh(&queue->lock);
941                 queue->queue_maxlen = ntohl(*queue_maxlen);
942                 spin_unlock_bh(&queue->lock);
943         }
944
945         if (nfqa[NFQA_CFG_FLAGS]) {
946                 __u32 flags, mask;
947
948                 if (!queue) {
949                         ret = -ENODEV;
950                         goto err_out_unlock;
951                 }
952
953                 if (!nfqa[NFQA_CFG_MASK]) {
954                         /* A mask is needed to specify which flags are being
955                          * changed.
956                          */
957                         ret = -EINVAL;
958                         goto err_out_unlock;
959                 }
960
961                 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
962                 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
963
964                 if (flags >= NFQA_CFG_F_MAX) {
965                         ret = -EOPNOTSUPP;
966                         goto err_out_unlock;
967                 }
968
969                 spin_lock_bh(&queue->lock);
970                 queue->flags &= ~mask;
971                 queue->flags |= flags & mask;
972                 spin_unlock_bh(&queue->lock);
973         }
974
975 err_out_unlock:
976         rcu_read_unlock();
977         return ret;
978 }
979
980 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
981         [NFQNL_MSG_PACKET]      = { .call_rcu = nfqnl_recv_unsupp,
982                                     .attr_count = NFQA_MAX, },
983         [NFQNL_MSG_VERDICT]     = { .call_rcu = nfqnl_recv_verdict,
984                                     .attr_count = NFQA_MAX,
985                                     .policy = nfqa_verdict_policy },
986         [NFQNL_MSG_CONFIG]      = { .call = nfqnl_recv_config,
987                                     .attr_count = NFQA_CFG_MAX,
988                                     .policy = nfqa_cfg_policy },
989         [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
990                                     .attr_count = NFQA_MAX,
991                                     .policy = nfqa_verdict_batch_policy },
992 };
993
994 static const struct nfnetlink_subsystem nfqnl_subsys = {
995         .name           = "nf_queue",
996         .subsys_id      = NFNL_SUBSYS_QUEUE,
997         .cb_count       = NFQNL_MSG_MAX,
998         .cb             = nfqnl_cb,
999 };
1000
1001 #ifdef CONFIG_PROC_FS
1002 struct iter_state {
1003         unsigned int bucket;
1004 };
1005
1006 static struct hlist_node *get_first(struct seq_file *seq)
1007 {
1008         struct iter_state *st = seq->private;
1009
1010         if (!st)
1011                 return NULL;
1012
1013         for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
1014                 if (!hlist_empty(&instance_table[st->bucket]))
1015                         return instance_table[st->bucket].first;
1016         }
1017         return NULL;
1018 }
1019
1020 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1021 {
1022         struct iter_state *st = seq->private;
1023
1024         h = h->next;
1025         while (!h) {
1026                 if (++st->bucket >= INSTANCE_BUCKETS)
1027                         return NULL;
1028
1029                 h = instance_table[st->bucket].first;
1030         }
1031         return h;
1032 }
1033
1034 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1035 {
1036         struct hlist_node *head;
1037         head = get_first(seq);
1038
1039         if (head)
1040                 while (pos && (head = get_next(seq, head)))
1041                         pos--;
1042         return pos ? NULL : head;
1043 }
1044
1045 static void *seq_start(struct seq_file *seq, loff_t *pos)
1046         __acquires(instances_lock)
1047 {
1048         spin_lock(&instances_lock);
1049         return get_idx(seq, *pos);
1050 }
1051
1052 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1053 {
1054         (*pos)++;
1055         return get_next(s, v);
1056 }
1057
1058 static void seq_stop(struct seq_file *s, void *v)
1059         __releases(instances_lock)
1060 {
1061         spin_unlock(&instances_lock);
1062 }
1063
1064 static int seq_show(struct seq_file *s, void *v)
1065 {
1066         const struct nfqnl_instance *inst = v;
1067
1068         return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1069                           inst->queue_num,
1070                           inst->peer_portid, inst->queue_total,
1071                           inst->copy_mode, inst->copy_range,
1072                           inst->queue_dropped, inst->queue_user_dropped,
1073                           inst->id_sequence, 1);
1074 }
1075
1076 static const struct seq_operations nfqnl_seq_ops = {
1077         .start  = seq_start,
1078         .next   = seq_next,
1079         .stop   = seq_stop,
1080         .show   = seq_show,
1081 };
1082
1083 static int nfqnl_open(struct inode *inode, struct file *file)
1084 {
1085         return seq_open_private(file, &nfqnl_seq_ops,
1086                         sizeof(struct iter_state));
1087 }
1088
1089 static const struct file_operations nfqnl_file_ops = {
1090         .owner   = THIS_MODULE,
1091         .open    = nfqnl_open,
1092         .read    = seq_read,
1093         .llseek  = seq_lseek,
1094         .release = seq_release_private,
1095 };
1096
1097 #endif /* PROC_FS */
1098
1099 static int __init nfnetlink_queue_init(void)
1100 {
1101         int i, status = -ENOMEM;
1102
1103         for (i = 0; i < INSTANCE_BUCKETS; i++)
1104                 INIT_HLIST_HEAD(&instance_table[i]);
1105
1106         netlink_register_notifier(&nfqnl_rtnl_notifier);
1107         status = nfnetlink_subsys_register(&nfqnl_subsys);
1108         if (status < 0) {
1109                 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1110                 goto cleanup_netlink_notifier;
1111         }
1112
1113 #ifdef CONFIG_PROC_FS
1114         if (!proc_create("nfnetlink_queue", 0440,
1115                          proc_net_netfilter, &nfqnl_file_ops))
1116                 goto cleanup_subsys;
1117 #endif
1118
1119         register_netdevice_notifier(&nfqnl_dev_notifier);
1120         nf_register_queue_handler(&nfqh);
1121         return status;
1122
1123 #ifdef CONFIG_PROC_FS
1124 cleanup_subsys:
1125         nfnetlink_subsys_unregister(&nfqnl_subsys);
1126 #endif
1127 cleanup_netlink_notifier:
1128         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1129         return status;
1130 }
1131
1132 static void __exit nfnetlink_queue_fini(void)
1133 {
1134         nf_unregister_queue_handler();
1135         unregister_netdevice_notifier(&nfqnl_dev_notifier);
1136 #ifdef CONFIG_PROC_FS
1137         remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1138 #endif
1139         nfnetlink_subsys_unregister(&nfqnl_subsys);
1140         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1141
1142         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1143 }
1144
1145 MODULE_DESCRIPTION("netfilter packet queue handler");
1146 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1147 MODULE_LICENSE("GPL");
1148 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1149
1150 module_init(nfnetlink_queue_init);
1151 module_exit(nfnetlink_queue_fini);