2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfnetlink.
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 * (C) 2007 by Patrick McHardy <kaber@trash.net>
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>
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.
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>
32 #include <net/netfilter/nf_queue.h>
33 #include <net/netns/generic.h>
34 #include <net/netfilter/nfnetlink_queue.h>
36 #include <linux/atomic.h>
38 #ifdef CONFIG_BRIDGE_NETFILTER
39 #include "../bridge/br_private.h"
42 #define NFQNL_QMAX_DEFAULT 1024
44 /* We're using struct nlattr which has 16bit nla_len. Note that nla_len
45 * includes the header length. Thus, the maximum packet length that we
46 * support is 65531 bytes. We send truncated packets if the specified length
47 * is larger than that. Userspace can check for presence of NFQA_CAP_LEN
48 * attribute to detect truncation.
50 #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
52 struct nfqnl_instance {
53 struct hlist_node hlist; /* global list of queues */
57 unsigned int queue_maxlen;
58 unsigned int copy_range;
59 unsigned int queue_dropped;
60 unsigned int queue_user_dropped;
63 u_int16_t queue_num; /* number of this queue */
65 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
67 * Following fields are dirtied for each queued packet,
68 * keep them in same cache line if possible.
71 unsigned int queue_total;
72 unsigned int id_sequence; /* 'sequence' of pkt ids */
73 struct list_head queue_list; /* packets in queue */
76 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
78 static int nfnl_queue_net_id __read_mostly;
80 #define INSTANCE_BUCKETS 16
81 struct nfnl_queue_net {
82 spinlock_t instances_lock;
83 struct hlist_head instance_table[INSTANCE_BUCKETS];
86 static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
88 return net_generic(net, nfnl_queue_net_id);
91 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
93 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
96 static struct nfqnl_instance *
97 instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
99 struct hlist_head *head;
100 struct nfqnl_instance *inst;
102 head = &q->instance_table[instance_hashfn(queue_num)];
103 hlist_for_each_entry_rcu(inst, head, hlist) {
104 if (inst->queue_num == queue_num)
110 static struct nfqnl_instance *
111 instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
114 struct nfqnl_instance *inst;
118 spin_lock(&q->instances_lock);
119 if (instance_lookup(q, queue_num)) {
124 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
130 inst->queue_num = queue_num;
131 inst->peer_portid = portid;
132 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
133 inst->copy_range = NFQNL_MAX_COPY_RANGE;
134 inst->copy_mode = NFQNL_COPY_NONE;
135 spin_lock_init(&inst->lock);
136 INIT_LIST_HEAD(&inst->queue_list);
138 if (!try_module_get(THIS_MODULE)) {
143 h = instance_hashfn(queue_num);
144 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
146 spin_unlock(&q->instances_lock);
153 spin_unlock(&q->instances_lock);
157 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
161 instance_destroy_rcu(struct rcu_head *head)
163 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
166 nfqnl_flush(inst, NULL, 0);
168 module_put(THIS_MODULE);
172 __instance_destroy(struct nfqnl_instance *inst)
174 hlist_del_rcu(&inst->hlist);
175 call_rcu(&inst->rcu, instance_destroy_rcu);
179 instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
181 spin_lock(&q->instances_lock);
182 __instance_destroy(inst);
183 spin_unlock(&q->instances_lock);
187 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
189 list_add_tail(&entry->list, &queue->queue_list);
190 queue->queue_total++;
194 __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
196 list_del(&entry->list);
197 queue->queue_total--;
200 static struct nf_queue_entry *
201 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
203 struct nf_queue_entry *entry = NULL, *i;
205 spin_lock_bh(&queue->lock);
207 list_for_each_entry(i, &queue->queue_list, list) {
215 __dequeue_entry(queue, entry);
217 spin_unlock_bh(&queue->lock);
223 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
225 struct nf_queue_entry *entry, *next;
227 spin_lock_bh(&queue->lock);
228 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
229 if (!cmpfn || cmpfn(entry, data)) {
230 list_del(&entry->list);
231 queue->queue_total--;
232 nf_reinject(entry, NF_DROP);
235 spin_unlock_bh(&queue->lock);
239 nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)
242 int plen = 0; /* length of skb->head fragment */
246 /* dont bother with small payloads */
247 if (len <= skb_tailroom(to)) {
248 skb_copy_bits(from, 0, skb_put(to, len), len);
253 skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
256 plen = min_t(int, skb_headlen(from), len);
258 page = virt_to_head_page(from->head);
259 offset = from->data - (unsigned char *)page_address(page);
260 __skb_fill_page_desc(to, 0, page, offset, plen);
267 to->truesize += len + plen;
268 to->len += len + plen;
269 to->data_len += len + plen;
271 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
274 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
275 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
276 len -= skb_shinfo(to)->frags[j].size;
280 skb_shinfo(to)->nr_frags = j;
283 static int nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet)
287 if (packet->ip_summed == CHECKSUM_PARTIAL)
288 flags = NFQA_SKB_CSUMNOTREADY;
289 if (skb_is_gso(packet))
290 flags |= NFQA_SKB_GSO;
292 return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
295 static struct sk_buff *
296 nfqnl_build_packet_message(struct nfqnl_instance *queue,
297 struct nf_queue_entry *entry,
298 __be32 **packet_id_ptr)
301 size_t data_len = 0, cap_len = 0;
305 struct nfqnl_msg_packet_hdr *pmsg;
306 struct nlmsghdr *nlh;
307 struct nfgenmsg *nfmsg;
308 struct sk_buff *entskb = entry->skb;
309 struct net_device *indev;
310 struct net_device *outdev;
311 struct nf_conn *ct = NULL;
312 enum ip_conntrack_info uninitialized_var(ctinfo);
314 size = nlmsg_total_size(sizeof(struct nfgenmsg))
315 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
316 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
317 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
318 #ifdef CONFIG_BRIDGE_NETFILTER
319 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
320 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
322 + nla_total_size(sizeof(u_int32_t)) /* mark */
323 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
324 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */
325 + nla_total_size(sizeof(u_int32_t)); /* cap_len */
327 if (entskb->tstamp.tv64)
328 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
330 outdev = entry->outdev;
332 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
333 case NFQNL_COPY_META:
334 case NFQNL_COPY_NONE:
337 case NFQNL_COPY_PACKET:
338 if (!(queue->flags & NFQA_CFG_F_GSO) &&
339 entskb->ip_summed == CHECKSUM_PARTIAL &&
340 skb_checksum_help(entskb))
343 data_len = ACCESS_ONCE(queue->copy_range);
344 if (data_len > entskb->len)
345 data_len = entskb->len;
347 if (!entskb->head_frag ||
348 skb_headlen(entskb) < L1_CACHE_BYTES ||
349 skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS)
350 hlen = skb_headlen(entskb);
352 if (skb_has_frag_list(entskb))
354 hlen = min_t(int, data_len, hlen);
355 size += sizeof(struct nlattr) + hlen;
356 cap_len = entskb->len;
360 if (queue->flags & NFQA_CFG_F_CONNTRACK)
361 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
363 skb = nfnetlink_alloc_skb(&init_net, size, queue->peer_portid,
368 nlh = nlmsg_put(skb, 0, 0,
369 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
370 sizeof(struct nfgenmsg), 0);
375 nfmsg = nlmsg_data(nlh);
376 nfmsg->nfgen_family = entry->pf;
377 nfmsg->version = NFNETLINK_V0;
378 nfmsg->res_id = htons(queue->queue_num);
380 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
381 pmsg = nla_data(nla);
382 pmsg->hw_protocol = entskb->protocol;
383 pmsg->hook = entry->hook;
384 *packet_id_ptr = &pmsg->packet_id;
386 indev = entry->indev;
388 #ifndef CONFIG_BRIDGE_NETFILTER
389 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
390 goto nla_put_failure;
392 if (entry->pf == PF_BRIDGE) {
393 /* Case 1: indev is physical input device, we need to
394 * look for bridge group (when called from
395 * netfilter_bridge) */
396 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
397 htonl(indev->ifindex)) ||
398 /* this is the bridge group "brX" */
399 /* rcu_read_lock()ed by __nf_queue */
400 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
401 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
402 goto nla_put_failure;
404 /* Case 2: indev is bridge group, we need to look for
405 * physical device (when called from ipv4) */
406 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
407 htonl(indev->ifindex)))
408 goto nla_put_failure;
409 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
410 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
411 htonl(entskb->nf_bridge->physindev->ifindex)))
412 goto nla_put_failure;
418 #ifndef CONFIG_BRIDGE_NETFILTER
419 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
420 goto nla_put_failure;
422 if (entry->pf == PF_BRIDGE) {
423 /* Case 1: outdev is physical output device, we need to
424 * look for bridge group (when called from
425 * netfilter_bridge) */
426 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
427 htonl(outdev->ifindex)) ||
428 /* this is the bridge group "brX" */
429 /* rcu_read_lock()ed by __nf_queue */
430 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
431 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
432 goto nla_put_failure;
434 /* Case 2: outdev is bridge group, we need to look for
435 * physical output device (when called from ipv4) */
436 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
437 htonl(outdev->ifindex)))
438 goto nla_put_failure;
439 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
440 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
441 htonl(entskb->nf_bridge->physoutdev->ifindex)))
442 goto nla_put_failure;
448 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
449 goto nla_put_failure;
451 if (indev && entskb->dev &&
452 entskb->mac_header != entskb->network_header) {
453 struct nfqnl_msg_packet_hw phw;
454 int len = dev_parse_header(entskb, phw.hw_addr);
456 phw.hw_addrlen = htons(len);
457 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
458 goto nla_put_failure;
462 if (entskb->tstamp.tv64) {
463 struct nfqnl_msg_packet_timestamp ts;
464 struct timeval tv = ktime_to_timeval(entskb->tstamp);
465 ts.sec = cpu_to_be64(tv.tv_sec);
466 ts.usec = cpu_to_be64(tv.tv_usec);
468 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
469 goto nla_put_failure;
472 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
473 goto nla_put_failure;
475 if (cap_len > data_len &&
476 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
477 goto nla_put_failure;
479 if (nfqnl_put_packet_info(skb, entskb))
480 goto nla_put_failure;
485 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
486 goto nla_put_failure;
488 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
489 nla->nla_type = NFQA_PAYLOAD;
490 nla->nla_len = nla_attr_size(data_len);
492 nfqnl_zcopy(skb, entskb, data_len, hlen);
495 nlh->nlmsg_len = skb->len;
500 net_err_ratelimited("nf_queue: error creating packet message\n");
505 __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
506 struct nf_queue_entry *entry)
508 struct sk_buff *nskb;
510 __be32 *packet_id_ptr;
513 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
518 spin_lock_bh(&queue->lock);
520 if (queue->queue_total >= queue->queue_maxlen) {
521 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
525 queue->queue_dropped++;
526 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
529 goto err_out_free_nskb;
531 entry->id = ++queue->id_sequence;
532 *packet_id_ptr = htonl(entry->id);
534 /* nfnetlink_unicast will either free the nskb or add it to a socket */
535 err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
537 queue->queue_user_dropped++;
541 __enqueue_entry(queue, entry);
543 spin_unlock_bh(&queue->lock);
549 spin_unlock_bh(&queue->lock);
551 nf_reinject(entry, NF_ACCEPT);
556 static struct nf_queue_entry *
557 nf_queue_entry_dup(struct nf_queue_entry *e)
559 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
561 if (nf_queue_entry_get_refs(entry))
568 #ifdef CONFIG_BRIDGE_NETFILTER
569 /* When called from bridge netfilter, skb->data must point to MAC header
570 * before calling skb_gso_segment(). Else, original MAC header is lost
571 * and segmented skbs will be sent to wrong destination.
573 static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
576 __skb_push(skb, skb->network_header - skb->mac_header);
579 static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
582 __skb_pull(skb, skb->network_header - skb->mac_header);
585 #define nf_bridge_adjust_skb_data(s) do {} while (0)
586 #define nf_bridge_adjust_segmented_data(s) do {} while (0)
589 static void free_entry(struct nf_queue_entry *entry)
591 nf_queue_entry_release_refs(entry);
596 __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
597 struct sk_buff *skb, struct nf_queue_entry *entry)
600 struct nf_queue_entry *entry_seg;
602 nf_bridge_adjust_segmented_data(skb);
604 if (skb->next == NULL) { /* last packet, no need to copy entry */
605 struct sk_buff *gso_skb = entry->skb;
607 ret = __nfqnl_enqueue_packet(net, queue, entry);
609 entry->skb = gso_skb;
615 entry_seg = nf_queue_entry_dup(entry);
617 entry_seg->skb = skb;
618 ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
620 free_entry(entry_seg);
626 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
629 struct nfqnl_instance *queue;
630 struct sk_buff *skb, *segs;
632 struct net *net = dev_net(entry->indev ?
633 entry->indev : entry->outdev);
634 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
636 /* rcu_read_lock()ed by nf_hook_slow() */
637 queue = instance_lookup(q, queuenum);
641 if (queue->copy_mode == NFQNL_COPY_NONE)
644 if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(entry->skb))
645 return __nfqnl_enqueue_packet(net, queue, entry);
651 skb->protocol = htons(ETH_P_IP);
654 skb->protocol = htons(ETH_P_IPV6);
658 nf_bridge_adjust_skb_data(skb);
659 segs = skb_gso_segment(skb, 0);
660 /* Does not use PTR_ERR to limit the number of error codes that can be
661 * returned by nf_queue. For instance, callers rely on -ECANCELED to
662 * mean 'ignore this hook'.
669 struct sk_buff *nskb = segs->next;
671 err = __nfqnl_enqueue_packet_gso(net, queue,
681 if (err) /* some segments are already queued */
687 nf_bridge_adjust_segmented_data(skb);
692 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
694 struct sk_buff *nskb;
697 if (pskb_trim(e->skb, data_len))
699 } else if (diff > 0) {
700 if (data_len > 0xFFFF)
702 if (diff > skb_tailroom(e->skb)) {
703 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
706 printk(KERN_WARNING "nf_queue: OOM "
707 "in mangle, dropping packet\n");
713 skb_put(e->skb, diff);
715 if (!skb_make_writable(e->skb, data_len))
717 skb_copy_to_linear_data(e->skb, data, data_len);
718 e->skb->ip_summed = CHECKSUM_NONE;
723 nfqnl_set_mode(struct nfqnl_instance *queue,
724 unsigned char mode, unsigned int range)
728 spin_lock_bh(&queue->lock);
730 case NFQNL_COPY_NONE:
731 case NFQNL_COPY_META:
732 queue->copy_mode = mode;
733 queue->copy_range = 0;
736 case NFQNL_COPY_PACKET:
737 queue->copy_mode = mode;
738 if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
739 queue->copy_range = NFQNL_MAX_COPY_RANGE;
741 queue->copy_range = range;
748 spin_unlock_bh(&queue->lock);
754 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
757 if (entry->indev->ifindex == ifindex)
760 if (entry->outdev->ifindex == ifindex)
762 #ifdef CONFIG_BRIDGE_NETFILTER
763 if (entry->skb->nf_bridge) {
764 if (entry->skb->nf_bridge->physindev &&
765 entry->skb->nf_bridge->physindev->ifindex == ifindex)
767 if (entry->skb->nf_bridge->physoutdev &&
768 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
775 /* drop all packets with either indev or outdev == ifindex from all queue
778 nfqnl_dev_drop(struct net *net, int ifindex)
781 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
785 for (i = 0; i < INSTANCE_BUCKETS; i++) {
786 struct nfqnl_instance *inst;
787 struct hlist_head *head = &q->instance_table[i];
789 hlist_for_each_entry_rcu(inst, head, hlist)
790 nfqnl_flush(inst, dev_cmp, ifindex);
796 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
799 nfqnl_rcv_dev_event(struct notifier_block *this,
800 unsigned long event, void *ptr)
802 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
804 /* Drop any packets associated with the downed device */
805 if (event == NETDEV_DOWN)
806 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
810 static struct notifier_block nfqnl_dev_notifier = {
811 .notifier_call = nfqnl_rcv_dev_event,
815 nfqnl_rcv_nl_event(struct notifier_block *this,
816 unsigned long event, void *ptr)
818 struct netlink_notify *n = ptr;
819 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
821 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
824 /* destroy all instances for this portid */
825 spin_lock(&q->instances_lock);
826 for (i = 0; i < INSTANCE_BUCKETS; i++) {
827 struct hlist_node *t2;
828 struct nfqnl_instance *inst;
829 struct hlist_head *head = &q->instance_table[i];
831 hlist_for_each_entry_safe(inst, t2, head, hlist) {
832 if (n->portid == inst->peer_portid)
833 __instance_destroy(inst);
836 spin_unlock(&q->instances_lock);
841 static struct notifier_block nfqnl_rtnl_notifier = {
842 .notifier_call = nfqnl_rcv_nl_event,
845 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
846 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
847 [NFQA_MARK] = { .type = NLA_U32 },
848 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
849 [NFQA_CT] = { .type = NLA_UNSPEC },
852 static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
853 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
854 [NFQA_MARK] = { .type = NLA_U32 },
857 static struct nfqnl_instance *
858 verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid)
860 struct nfqnl_instance *queue;
862 queue = instance_lookup(q, queue_num);
864 return ERR_PTR(-ENODEV);
866 if (queue->peer_portid != nlportid)
867 return ERR_PTR(-EPERM);
872 static struct nfqnl_msg_verdict_hdr*
873 verdicthdr_get(const struct nlattr * const nfqa[])
875 struct nfqnl_msg_verdict_hdr *vhdr;
876 unsigned int verdict;
878 if (!nfqa[NFQA_VERDICT_HDR])
881 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
882 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
883 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
888 static int nfq_id_after(unsigned int id, unsigned int max)
890 return (int)(id - max) > 0;
894 nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
895 const struct nlmsghdr *nlh,
896 const struct nlattr * const nfqa[])
898 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
899 struct nf_queue_entry *entry, *tmp;
900 unsigned int verdict, maxid;
901 struct nfqnl_msg_verdict_hdr *vhdr;
902 struct nfqnl_instance *queue;
903 LIST_HEAD(batch_list);
904 u16 queue_num = ntohs(nfmsg->res_id);
906 struct net *net = sock_net(ctnl);
907 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
909 queue = verdict_instance_lookup(q, queue_num,
910 NETLINK_CB(skb).portid);
912 return PTR_ERR(queue);
914 vhdr = verdicthdr_get(nfqa);
918 verdict = ntohl(vhdr->verdict);
919 maxid = ntohl(vhdr->id);
921 spin_lock_bh(&queue->lock);
923 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
924 if (nfq_id_after(entry->id, maxid))
926 __dequeue_entry(queue, entry);
927 list_add_tail(&entry->list, &batch_list);
930 spin_unlock_bh(&queue->lock);
932 if (list_empty(&batch_list))
935 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
937 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
938 nf_reinject(entry, verdict);
944 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
945 const struct nlmsghdr *nlh,
946 const struct nlattr * const nfqa[])
948 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
949 u_int16_t queue_num = ntohs(nfmsg->res_id);
951 struct nfqnl_msg_verdict_hdr *vhdr;
952 struct nfqnl_instance *queue;
953 unsigned int verdict;
954 struct nf_queue_entry *entry;
955 enum ip_conntrack_info uninitialized_var(ctinfo);
956 struct nf_conn *ct = NULL;
958 struct net *net = sock_net(ctnl);
959 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
961 queue = instance_lookup(q, queue_num);
963 queue = verdict_instance_lookup(q, queue_num,
964 NETLINK_CB(skb).portid);
966 return PTR_ERR(queue);
968 vhdr = verdicthdr_get(nfqa);
972 verdict = ntohl(vhdr->verdict);
974 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
979 if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
980 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
982 if (nfqa[NFQA_PAYLOAD]) {
983 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
984 int diff = payload_len - entry->skb->len;
986 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
987 payload_len, entry, diff) < 0)
991 nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
996 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
998 nf_reinject(entry, verdict);
1003 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
1004 const struct nlmsghdr *nlh,
1005 const struct nlattr * const nfqa[])
1010 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
1011 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
1012 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
1015 static const struct nf_queue_handler nfqh = {
1016 .outfn = &nfqnl_enqueue_packet,
1020 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
1021 const struct nlmsghdr *nlh,
1022 const struct nlattr * const nfqa[])
1024 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1025 u_int16_t queue_num = ntohs(nfmsg->res_id);
1026 struct nfqnl_instance *queue;
1027 struct nfqnl_msg_config_cmd *cmd = NULL;
1028 struct net *net = sock_net(ctnl);
1029 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
1032 if (nfqa[NFQA_CFG_CMD]) {
1033 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
1035 /* Obsolete commands without queue context */
1036 switch (cmd->command) {
1037 case NFQNL_CFG_CMD_PF_BIND: return 0;
1038 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
1043 queue = instance_lookup(q, queue_num);
1044 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
1046 goto err_out_unlock;
1050 switch (cmd->command) {
1051 case NFQNL_CFG_CMD_BIND:
1054 goto err_out_unlock;
1056 queue = instance_create(q, queue_num,
1057 NETLINK_CB(skb).portid);
1058 if (IS_ERR(queue)) {
1059 ret = PTR_ERR(queue);
1060 goto err_out_unlock;
1063 case NFQNL_CFG_CMD_UNBIND:
1066 goto err_out_unlock;
1068 instance_destroy(q, queue);
1070 case NFQNL_CFG_CMD_PF_BIND:
1071 case NFQNL_CFG_CMD_PF_UNBIND:
1079 if (nfqa[NFQA_CFG_PARAMS]) {
1080 struct nfqnl_msg_config_params *params;
1084 goto err_out_unlock;
1086 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
1087 nfqnl_set_mode(queue, params->copy_mode,
1088 ntohl(params->copy_range));
1091 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
1092 __be32 *queue_maxlen;
1096 goto err_out_unlock;
1098 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
1099 spin_lock_bh(&queue->lock);
1100 queue->queue_maxlen = ntohl(*queue_maxlen);
1101 spin_unlock_bh(&queue->lock);
1104 if (nfqa[NFQA_CFG_FLAGS]) {
1109 goto err_out_unlock;
1112 if (!nfqa[NFQA_CFG_MASK]) {
1113 /* A mask is needed to specify which flags are being
1117 goto err_out_unlock;
1120 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
1121 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
1123 if (flags >= NFQA_CFG_F_MAX) {
1125 goto err_out_unlock;
1128 spin_lock_bh(&queue->lock);
1129 queue->flags &= ~mask;
1130 queue->flags |= flags & mask;
1131 spin_unlock_bh(&queue->lock);
1139 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
1140 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
1141 .attr_count = NFQA_MAX, },
1142 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
1143 .attr_count = NFQA_MAX,
1144 .policy = nfqa_verdict_policy },
1145 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
1146 .attr_count = NFQA_CFG_MAX,
1147 .policy = nfqa_cfg_policy },
1148 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1149 .attr_count = NFQA_MAX,
1150 .policy = nfqa_verdict_batch_policy },
1153 static const struct nfnetlink_subsystem nfqnl_subsys = {
1155 .subsys_id = NFNL_SUBSYS_QUEUE,
1156 .cb_count = NFQNL_MSG_MAX,
1160 #ifdef CONFIG_PROC_FS
1162 struct seq_net_private p;
1163 unsigned int bucket;
1166 static struct hlist_node *get_first(struct seq_file *seq)
1168 struct iter_state *st = seq->private;
1170 struct nfnl_queue_net *q;
1175 net = seq_file_net(seq);
1176 q = nfnl_queue_pernet(net);
1177 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
1178 if (!hlist_empty(&q->instance_table[st->bucket]))
1179 return q->instance_table[st->bucket].first;
1184 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1186 struct iter_state *st = seq->private;
1187 struct net *net = seq_file_net(seq);
1191 struct nfnl_queue_net *q;
1193 if (++st->bucket >= INSTANCE_BUCKETS)
1196 q = nfnl_queue_pernet(net);
1197 h = q->instance_table[st->bucket].first;
1202 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1204 struct hlist_node *head;
1205 head = get_first(seq);
1208 while (pos && (head = get_next(seq, head)))
1210 return pos ? NULL : head;
1213 static void *seq_start(struct seq_file *s, loff_t *pos)
1214 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
1216 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1217 return get_idx(s, *pos);
1220 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1223 return get_next(s, v);
1226 static void seq_stop(struct seq_file *s, void *v)
1227 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
1229 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1232 static int seq_show(struct seq_file *s, void *v)
1234 const struct nfqnl_instance *inst = v;
1236 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1238 inst->peer_portid, inst->queue_total,
1239 inst->copy_mode, inst->copy_range,
1240 inst->queue_dropped, inst->queue_user_dropped,
1241 inst->id_sequence, 1);
1244 static const struct seq_operations nfqnl_seq_ops = {
1251 static int nfqnl_open(struct inode *inode, struct file *file)
1253 return seq_open_net(inode, file, &nfqnl_seq_ops,
1254 sizeof(struct iter_state));
1257 static const struct file_operations nfqnl_file_ops = {
1258 .owner = THIS_MODULE,
1261 .llseek = seq_lseek,
1262 .release = seq_release_net,
1265 #endif /* PROC_FS */
1267 static int __net_init nfnl_queue_net_init(struct net *net)
1270 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
1272 for (i = 0; i < INSTANCE_BUCKETS; i++)
1273 INIT_HLIST_HEAD(&q->instance_table[i]);
1275 spin_lock_init(&q->instances_lock);
1277 #ifdef CONFIG_PROC_FS
1278 if (!proc_create("nfnetlink_queue", 0440,
1279 net->nf.proc_netfilter, &nfqnl_file_ops))
1285 static void __net_exit nfnl_queue_net_exit(struct net *net)
1287 #ifdef CONFIG_PROC_FS
1288 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
1292 static struct pernet_operations nfnl_queue_net_ops = {
1293 .init = nfnl_queue_net_init,
1294 .exit = nfnl_queue_net_exit,
1295 .id = &nfnl_queue_net_id,
1296 .size = sizeof(struct nfnl_queue_net),
1299 static int __init nfnetlink_queue_init(void)
1301 int status = -ENOMEM;
1303 netlink_register_notifier(&nfqnl_rtnl_notifier);
1304 status = nfnetlink_subsys_register(&nfqnl_subsys);
1306 pr_err("nf_queue: failed to create netlink socket\n");
1307 goto cleanup_netlink_notifier;
1310 status = register_pernet_subsys(&nfnl_queue_net_ops);
1312 pr_err("nf_queue: failed to register pernet ops\n");
1313 goto cleanup_subsys;
1315 register_netdevice_notifier(&nfqnl_dev_notifier);
1316 nf_register_queue_handler(&nfqh);
1320 nfnetlink_subsys_unregister(&nfqnl_subsys);
1321 cleanup_netlink_notifier:
1322 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1326 static void __exit nfnetlink_queue_fini(void)
1328 nf_unregister_queue_handler();
1329 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1330 unregister_pernet_subsys(&nfnl_queue_net_ops);
1331 nfnetlink_subsys_unregister(&nfqnl_subsys);
1332 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1334 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1337 MODULE_DESCRIPTION("netfilter packet queue handler");
1338 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1339 MODULE_LICENSE("GPL");
1340 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1342 module_init(nfnetlink_queue_init);
1343 module_exit(nfnetlink_queue_fini);