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