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