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/netfilter/nfnetlink_queue.h>
35 #include <linux/atomic.h>
37 #ifdef CONFIG_BRIDGE_NETFILTER
38 #include "../bridge/br_private.h"
41 #define NFQNL_QMAX_DEFAULT 1024
43 struct nfqnl_instance
{
44 struct hlist_node hlist
; /* global list of queues */
48 unsigned int queue_maxlen
;
49 unsigned int copy_range
;
50 unsigned int queue_dropped
;
51 unsigned int queue_user_dropped
;
54 u_int16_t queue_num
; /* number of this queue */
56 u_int32_t flags
; /* Set using NFQA_CFG_FLAGS */
58 * Following fields are dirtied for each queued packet,
59 * keep them in same cache line if possible.
62 unsigned int queue_total
;
63 unsigned int id_sequence
; /* 'sequence' of pkt ids */
64 struct list_head queue_list
; /* packets in queue */
67 typedef int (*nfqnl_cmpfn
)(struct nf_queue_entry
*, unsigned long);
69 static DEFINE_SPINLOCK(instances_lock
);
71 #define INSTANCE_BUCKETS 16
72 static struct hlist_head instance_table
[INSTANCE_BUCKETS
] __read_mostly
;
74 static inline u_int8_t
instance_hashfn(u_int16_t queue_num
)
76 return ((queue_num
>> 8) ^ queue_num
) % INSTANCE_BUCKETS
;
79 static struct nfqnl_instance
*
80 instance_lookup(u_int16_t queue_num
)
82 struct hlist_head
*head
;
83 struct nfqnl_instance
*inst
;
85 head
= &instance_table
[instance_hashfn(queue_num
)];
86 hlist_for_each_entry_rcu(inst
, head
, hlist
) {
87 if (inst
->queue_num
== queue_num
)
93 static struct nfqnl_instance
*
94 instance_create(u_int16_t queue_num
, int portid
)
96 struct nfqnl_instance
*inst
;
100 spin_lock(&instances_lock
);
101 if (instance_lookup(queue_num
)) {
106 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
112 inst
->queue_num
= queue_num
;
113 inst
->peer_portid
= portid
;
114 inst
->queue_maxlen
= NFQNL_QMAX_DEFAULT
;
115 inst
->copy_range
= 0xfffff;
116 inst
->copy_mode
= NFQNL_COPY_NONE
;
117 spin_lock_init(&inst
->lock
);
118 INIT_LIST_HEAD(&inst
->queue_list
);
120 if (!try_module_get(THIS_MODULE
)) {
125 h
= instance_hashfn(queue_num
);
126 hlist_add_head_rcu(&inst
->hlist
, &instance_table
[h
]);
128 spin_unlock(&instances_lock
);
135 spin_unlock(&instances_lock
);
139 static void nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
,
143 instance_destroy_rcu(struct rcu_head
*head
)
145 struct nfqnl_instance
*inst
= container_of(head
, struct nfqnl_instance
,
148 nfqnl_flush(inst
, NULL
, 0);
150 module_put(THIS_MODULE
);
154 __instance_destroy(struct nfqnl_instance
*inst
)
156 hlist_del_rcu(&inst
->hlist
);
157 call_rcu(&inst
->rcu
, instance_destroy_rcu
);
161 instance_destroy(struct nfqnl_instance
*inst
)
163 spin_lock(&instances_lock
);
164 __instance_destroy(inst
);
165 spin_unlock(&instances_lock
);
169 __enqueue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
171 list_add_tail(&entry
->list
, &queue
->queue_list
);
172 queue
->queue_total
++;
176 __dequeue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
178 list_del(&entry
->list
);
179 queue
->queue_total
--;
182 static struct nf_queue_entry
*
183 find_dequeue_entry(struct nfqnl_instance
*queue
, unsigned int id
)
185 struct nf_queue_entry
*entry
= NULL
, *i
;
187 spin_lock_bh(&queue
->lock
);
189 list_for_each_entry(i
, &queue
->queue_list
, list
) {
197 __dequeue_entry(queue
, entry
);
199 spin_unlock_bh(&queue
->lock
);
205 nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
, unsigned long data
)
207 struct nf_queue_entry
*entry
, *next
;
209 spin_lock_bh(&queue
->lock
);
210 list_for_each_entry_safe(entry
, next
, &queue
->queue_list
, list
) {
211 if (!cmpfn
|| cmpfn(entry
, data
)) {
212 list_del(&entry
->list
);
213 queue
->queue_total
--;
214 nf_reinject(entry
, NF_DROP
);
217 spin_unlock_bh(&queue
->lock
);
221 nfqnl_zcopy(struct sk_buff
*to
, const struct sk_buff
*from
, int len
, int hlen
)
224 int plen
= 0; /* length of skb->head fragment */
228 /* dont bother with small payloads */
229 if (len
<= skb_tailroom(to
)) {
230 skb_copy_bits(from
, 0, skb_put(to
, len
), len
);
235 skb_copy_bits(from
, 0, skb_put(to
, hlen
), hlen
);
238 plen
= min_t(int, skb_headlen(from
), len
);
240 page
= virt_to_head_page(from
->head
);
241 offset
= from
->data
- (unsigned char *)page_address(page
);
242 __skb_fill_page_desc(to
, 0, page
, offset
, plen
);
249 to
->truesize
+= len
+ plen
;
250 to
->len
+= len
+ plen
;
251 to
->data_len
+= len
+ plen
;
253 for (i
= 0; i
< skb_shinfo(from
)->nr_frags
; i
++) {
256 skb_shinfo(to
)->frags
[j
] = skb_shinfo(from
)->frags
[i
];
257 skb_shinfo(to
)->frags
[j
].size
= min_t(int, skb_shinfo(to
)->frags
[j
].size
, len
);
258 len
-= skb_shinfo(to
)->frags
[j
].size
;
262 skb_shinfo(to
)->nr_frags
= j
;
265 static struct sk_buff
*
266 nfqnl_build_packet_message(struct nfqnl_instance
*queue
,
267 struct nf_queue_entry
*entry
,
268 __be32
**packet_id_ptr
)
271 size_t data_len
= 0, cap_len
= 0;
275 struct nfqnl_msg_packet_hdr
*pmsg
;
276 struct nlmsghdr
*nlh
;
277 struct nfgenmsg
*nfmsg
;
278 struct sk_buff
*entskb
= entry
->skb
;
279 struct net_device
*indev
;
280 struct net_device
*outdev
;
281 struct nf_conn
*ct
= NULL
;
282 enum ip_conntrack_info
uninitialized_var(ctinfo
);
284 size
= NLMSG_SPACE(sizeof(struct nfgenmsg
))
285 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr
))
286 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
287 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
288 #ifdef CONFIG_BRIDGE_NETFILTER
289 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
290 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
292 + nla_total_size(sizeof(u_int32_t
)) /* mark */
293 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw
))
294 + nla_total_size(sizeof(u_int32_t
)); /* cap_len */
296 if (entskb
->tstamp
.tv64
)
297 size
+= nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp
));
299 outdev
= entry
->outdev
;
301 switch ((enum nfqnl_config_mode
)ACCESS_ONCE(queue
->copy_mode
)) {
302 case NFQNL_COPY_META
:
303 case NFQNL_COPY_NONE
:
306 case NFQNL_COPY_PACKET
:
307 if (entskb
->ip_summed
== CHECKSUM_PARTIAL
&&
308 skb_checksum_help(entskb
))
311 data_len
= ACCESS_ONCE(queue
->copy_range
);
312 if (data_len
== 0 || data_len
> entskb
->len
)
313 data_len
= entskb
->len
;
316 if (!entskb
->head_frag
||
317 skb_headlen(entskb
) < L1_CACHE_BYTES
||
318 skb_shinfo(entskb
)->nr_frags
>= MAX_SKB_FRAGS
)
319 hlen
= skb_headlen(entskb
);
321 if (skb_has_frag_list(entskb
))
323 hlen
= min_t(int, data_len
, hlen
);
324 size
+= sizeof(struct nlattr
) + hlen
;
325 cap_len
= entskb
->len
;
329 if (queue
->flags
& NFQA_CFG_F_CONNTRACK
)
330 ct
= nfqnl_ct_get(entskb
, &size
, &ctinfo
);
332 skb
= alloc_skb(size
, GFP_ATOMIC
);
336 nlh
= nlmsg_put(skb
, 0, 0,
337 NFNL_SUBSYS_QUEUE
<< 8 | NFQNL_MSG_PACKET
,
338 sizeof(struct nfgenmsg
), 0);
343 nfmsg
= nlmsg_data(nlh
);
344 nfmsg
->nfgen_family
= entry
->pf
;
345 nfmsg
->version
= NFNETLINK_V0
;
346 nfmsg
->res_id
= htons(queue
->queue_num
);
348 nla
= __nla_reserve(skb
, NFQA_PACKET_HDR
, sizeof(*pmsg
));
349 pmsg
= nla_data(nla
);
350 pmsg
->hw_protocol
= entskb
->protocol
;
351 pmsg
->hook
= entry
->hook
;
352 *packet_id_ptr
= &pmsg
->packet_id
;
354 indev
= entry
->indev
;
356 #ifndef CONFIG_BRIDGE_NETFILTER
357 if (nla_put_be32(skb
, NFQA_IFINDEX_INDEV
, htonl(indev
->ifindex
)))
358 goto nla_put_failure
;
360 if (entry
->pf
== PF_BRIDGE
) {
361 /* Case 1: indev is physical input device, we need to
362 * look for bridge group (when called from
363 * netfilter_bridge) */
364 if (nla_put_be32(skb
, NFQA_IFINDEX_PHYSINDEV
,
365 htonl(indev
->ifindex
)) ||
366 /* this is the bridge group "brX" */
367 /* rcu_read_lock()ed by __nf_queue */
368 nla_put_be32(skb
, NFQA_IFINDEX_INDEV
,
369 htonl(br_port_get_rcu(indev
)->br
->dev
->ifindex
)))
370 goto nla_put_failure
;
372 /* Case 2: indev is bridge group, we need to look for
373 * physical device (when called from ipv4) */
374 if (nla_put_be32(skb
, NFQA_IFINDEX_INDEV
,
375 htonl(indev
->ifindex
)))
376 goto nla_put_failure
;
377 if (entskb
->nf_bridge
&& entskb
->nf_bridge
->physindev
&&
378 nla_put_be32(skb
, NFQA_IFINDEX_PHYSINDEV
,
379 htonl(entskb
->nf_bridge
->physindev
->ifindex
)))
380 goto nla_put_failure
;
386 #ifndef CONFIG_BRIDGE_NETFILTER
387 if (nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
, htonl(outdev
->ifindex
)))
388 goto nla_put_failure
;
390 if (entry
->pf
== PF_BRIDGE
) {
391 /* Case 1: outdev is physical output device, we need to
392 * look for bridge group (when called from
393 * netfilter_bridge) */
394 if (nla_put_be32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
395 htonl(outdev
->ifindex
)) ||
396 /* this is the bridge group "brX" */
397 /* rcu_read_lock()ed by __nf_queue */
398 nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
,
399 htonl(br_port_get_rcu(outdev
)->br
->dev
->ifindex
)))
400 goto nla_put_failure
;
402 /* Case 2: outdev is bridge group, we need to look for
403 * physical output device (when called from ipv4) */
404 if (nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
,
405 htonl(outdev
->ifindex
)))
406 goto nla_put_failure
;
407 if (entskb
->nf_bridge
&& entskb
->nf_bridge
->physoutdev
&&
408 nla_put_be32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
409 htonl(entskb
->nf_bridge
->physoutdev
->ifindex
)))
410 goto nla_put_failure
;
416 nla_put_be32(skb
, NFQA_MARK
, htonl(entskb
->mark
)))
417 goto nla_put_failure
;
419 if (indev
&& entskb
->dev
&&
420 entskb
->mac_header
!= entskb
->network_header
) {
421 struct nfqnl_msg_packet_hw phw
;
422 int len
= dev_parse_header(entskb
, phw
.hw_addr
);
424 phw
.hw_addrlen
= htons(len
);
425 if (nla_put(skb
, NFQA_HWADDR
, sizeof(phw
), &phw
))
426 goto nla_put_failure
;
430 if (entskb
->tstamp
.tv64
) {
431 struct nfqnl_msg_packet_timestamp ts
;
432 struct timeval tv
= ktime_to_timeval(entskb
->tstamp
);
433 ts
.sec
= cpu_to_be64(tv
.tv_sec
);
434 ts
.usec
= cpu_to_be64(tv
.tv_usec
);
436 if (nla_put(skb
, NFQA_TIMESTAMP
, sizeof(ts
), &ts
))
437 goto nla_put_failure
;
440 if (ct
&& nfqnl_ct_put(skb
, ct
, ctinfo
) < 0)
441 goto nla_put_failure
;
443 if (cap_len
> 0 && nla_put_be32(skb
, NFQA_CAP_LEN
, htonl(cap_len
)))
444 goto nla_put_failure
;
449 if (skb_tailroom(skb
) < sizeof(*nla
) + hlen
)
450 goto nla_put_failure
;
452 nla
= (struct nlattr
*)skb_put(skb
, sizeof(*nla
));
453 nla
->nla_type
= NFQA_PAYLOAD
;
454 nla
->nla_len
= nla_attr_size(data_len
);
456 nfqnl_zcopy(skb
, entskb
, data_len
, hlen
);
459 nlh
->nlmsg_len
= skb
->len
;
464 net_err_ratelimited("nf_queue: error creating packet message\n");
469 nfqnl_enqueue_packet(struct nf_queue_entry
*entry
, unsigned int queuenum
)
471 struct sk_buff
*nskb
;
472 struct nfqnl_instance
*queue
;
474 __be32
*packet_id_ptr
;
477 /* rcu_read_lock()ed by nf_hook_slow() */
478 queue
= instance_lookup(queuenum
);
484 if (queue
->copy_mode
== NFQNL_COPY_NONE
) {
489 nskb
= nfqnl_build_packet_message(queue
, entry
, &packet_id_ptr
);
494 spin_lock_bh(&queue
->lock
);
496 if (!queue
->peer_portid
) {
498 goto err_out_free_nskb
;
500 if (queue
->queue_total
>= queue
->queue_maxlen
) {
501 if (queue
->flags
& NFQA_CFG_F_FAIL_OPEN
) {
505 queue
->queue_dropped
++;
506 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
509 goto err_out_free_nskb
;
511 entry
->id
= ++queue
->id_sequence
;
512 *packet_id_ptr
= htonl(entry
->id
);
514 /* nfnetlink_unicast will either free the nskb or add it to a socket */
515 err
= nfnetlink_unicast(nskb
, &init_net
, queue
->peer_portid
, MSG_DONTWAIT
);
517 queue
->queue_user_dropped
++;
521 __enqueue_entry(queue
, entry
);
523 spin_unlock_bh(&queue
->lock
);
529 spin_unlock_bh(&queue
->lock
);
531 nf_reinject(entry
, NF_ACCEPT
);
537 nfqnl_mangle(void *data
, int data_len
, struct nf_queue_entry
*e
, int diff
)
539 struct sk_buff
*nskb
;
542 if (pskb_trim(e
->skb
, data_len
))
544 } else if (diff
> 0) {
545 if (data_len
> 0xFFFF)
547 if (diff
> skb_tailroom(e
->skb
)) {
548 nskb
= skb_copy_expand(e
->skb
, skb_headroom(e
->skb
),
551 printk(KERN_WARNING
"nf_queue: OOM "
552 "in mangle, dropping packet\n");
558 skb_put(e
->skb
, diff
);
560 if (!skb_make_writable(e
->skb
, data_len
))
562 skb_copy_to_linear_data(e
->skb
, data
, data_len
);
563 e
->skb
->ip_summed
= CHECKSUM_NONE
;
568 nfqnl_set_mode(struct nfqnl_instance
*queue
,
569 unsigned char mode
, unsigned int range
)
573 spin_lock_bh(&queue
->lock
);
575 case NFQNL_COPY_NONE
:
576 case NFQNL_COPY_META
:
577 queue
->copy_mode
= mode
;
578 queue
->copy_range
= 0;
581 case NFQNL_COPY_PACKET
:
582 queue
->copy_mode
= mode
;
583 /* We're using struct nlattr which has 16bit nla_len. Note that
584 * nla_len includes the header length. Thus, the maximum packet
585 * length that we support is 65531 bytes. We send truncated
586 * packets if the specified length is larger than that.
588 if (range
> 0xffff - NLA_HDRLEN
)
589 queue
->copy_range
= 0xffff - NLA_HDRLEN
;
591 queue
->copy_range
= range
;
598 spin_unlock_bh(&queue
->lock
);
604 dev_cmp(struct nf_queue_entry
*entry
, unsigned long ifindex
)
607 if (entry
->indev
->ifindex
== ifindex
)
610 if (entry
->outdev
->ifindex
== ifindex
)
612 #ifdef CONFIG_BRIDGE_NETFILTER
613 if (entry
->skb
->nf_bridge
) {
614 if (entry
->skb
->nf_bridge
->physindev
&&
615 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
617 if (entry
->skb
->nf_bridge
->physoutdev
&&
618 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
625 /* drop all packets with either indev or outdev == ifindex from all queue
628 nfqnl_dev_drop(int ifindex
)
634 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
635 struct nfqnl_instance
*inst
;
636 struct hlist_head
*head
= &instance_table
[i
];
638 hlist_for_each_entry_rcu(inst
, head
, hlist
)
639 nfqnl_flush(inst
, dev_cmp
, ifindex
);
645 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
648 nfqnl_rcv_dev_event(struct notifier_block
*this,
649 unsigned long event
, void *ptr
)
651 struct net_device
*dev
= ptr
;
653 if (!net_eq(dev_net(dev
), &init_net
))
656 /* Drop any packets associated with the downed device */
657 if (event
== NETDEV_DOWN
)
658 nfqnl_dev_drop(dev
->ifindex
);
662 static struct notifier_block nfqnl_dev_notifier
= {
663 .notifier_call
= nfqnl_rcv_dev_event
,
667 nfqnl_rcv_nl_event(struct notifier_block
*this,
668 unsigned long event
, void *ptr
)
670 struct netlink_notify
*n
= ptr
;
672 if (event
== NETLINK_URELEASE
&& n
->protocol
== NETLINK_NETFILTER
) {
675 /* destroy all instances for this portid */
676 spin_lock(&instances_lock
);
677 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
678 struct hlist_node
*t2
;
679 struct nfqnl_instance
*inst
;
680 struct hlist_head
*head
= &instance_table
[i
];
682 hlist_for_each_entry_safe(inst
, t2
, head
, hlist
) {
683 if ((n
->net
== &init_net
) &&
684 (n
->portid
== inst
->peer_portid
))
685 __instance_destroy(inst
);
688 spin_unlock(&instances_lock
);
693 static struct notifier_block nfqnl_rtnl_notifier
= {
694 .notifier_call
= nfqnl_rcv_nl_event
,
697 static const struct nla_policy nfqa_verdict_policy
[NFQA_MAX
+1] = {
698 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
699 [NFQA_MARK
] = { .type
= NLA_U32
},
700 [NFQA_PAYLOAD
] = { .type
= NLA_UNSPEC
},
701 [NFQA_CT
] = { .type
= NLA_UNSPEC
},
704 static const struct nla_policy nfqa_verdict_batch_policy
[NFQA_MAX
+1] = {
705 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
706 [NFQA_MARK
] = { .type
= NLA_U32
},
709 static struct nfqnl_instance
*verdict_instance_lookup(u16 queue_num
, int nlportid
)
711 struct nfqnl_instance
*queue
;
713 queue
= instance_lookup(queue_num
);
715 return ERR_PTR(-ENODEV
);
717 if (queue
->peer_portid
!= nlportid
)
718 return ERR_PTR(-EPERM
);
723 static struct nfqnl_msg_verdict_hdr
*
724 verdicthdr_get(const struct nlattr
* const nfqa
[])
726 struct nfqnl_msg_verdict_hdr
*vhdr
;
727 unsigned int verdict
;
729 if (!nfqa
[NFQA_VERDICT_HDR
])
732 vhdr
= nla_data(nfqa
[NFQA_VERDICT_HDR
]);
733 verdict
= ntohl(vhdr
->verdict
) & NF_VERDICT_MASK
;
734 if (verdict
> NF_MAX_VERDICT
|| verdict
== NF_STOLEN
)
739 static int nfq_id_after(unsigned int id
, unsigned int max
)
741 return (int)(id
- max
) > 0;
745 nfqnl_recv_verdict_batch(struct sock
*ctnl
, struct sk_buff
*skb
,
746 const struct nlmsghdr
*nlh
,
747 const struct nlattr
* const nfqa
[])
749 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
750 struct nf_queue_entry
*entry
, *tmp
;
751 unsigned int verdict
, maxid
;
752 struct nfqnl_msg_verdict_hdr
*vhdr
;
753 struct nfqnl_instance
*queue
;
754 LIST_HEAD(batch_list
);
755 u16 queue_num
= ntohs(nfmsg
->res_id
);
757 queue
= verdict_instance_lookup(queue_num
, NETLINK_CB(skb
).portid
);
759 return PTR_ERR(queue
);
761 vhdr
= verdicthdr_get(nfqa
);
765 verdict
= ntohl(vhdr
->verdict
);
766 maxid
= ntohl(vhdr
->id
);
768 spin_lock_bh(&queue
->lock
);
770 list_for_each_entry_safe(entry
, tmp
, &queue
->queue_list
, list
) {
771 if (nfq_id_after(entry
->id
, maxid
))
773 __dequeue_entry(queue
, entry
);
774 list_add_tail(&entry
->list
, &batch_list
);
777 spin_unlock_bh(&queue
->lock
);
779 if (list_empty(&batch_list
))
782 list_for_each_entry_safe(entry
, tmp
, &batch_list
, list
) {
784 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
785 nf_reinject(entry
, verdict
);
791 nfqnl_recv_verdict(struct sock
*ctnl
, struct sk_buff
*skb
,
792 const struct nlmsghdr
*nlh
,
793 const struct nlattr
* const nfqa
[])
795 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
796 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
798 struct nfqnl_msg_verdict_hdr
*vhdr
;
799 struct nfqnl_instance
*queue
;
800 unsigned int verdict
;
801 struct nf_queue_entry
*entry
;
802 enum ip_conntrack_info
uninitialized_var(ctinfo
);
803 struct nf_conn
*ct
= NULL
;
805 queue
= instance_lookup(queue_num
);
808 queue
= verdict_instance_lookup(queue_num
, NETLINK_CB(skb
).portid
);
810 return PTR_ERR(queue
);
812 vhdr
= verdicthdr_get(nfqa
);
816 verdict
= ntohl(vhdr
->verdict
);
818 entry
= find_dequeue_entry(queue
, ntohl(vhdr
->id
));
823 if (nfqa
[NFQA_CT
] && (queue
->flags
& NFQA_CFG_F_CONNTRACK
))
824 ct
= nfqnl_ct_parse(entry
->skb
, nfqa
[NFQA_CT
], &ctinfo
);
826 if (nfqa
[NFQA_PAYLOAD
]) {
827 u16 payload_len
= nla_len(nfqa
[NFQA_PAYLOAD
]);
828 int diff
= payload_len
- entry
->skb
->len
;
830 if (nfqnl_mangle(nla_data(nfqa
[NFQA_PAYLOAD
]),
831 payload_len
, entry
, diff
) < 0)
835 nfqnl_ct_seq_adjust(skb
, ct
, ctinfo
, diff
);
840 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
842 nf_reinject(entry
, verdict
);
847 nfqnl_recv_unsupp(struct sock
*ctnl
, struct sk_buff
*skb
,
848 const struct nlmsghdr
*nlh
,
849 const struct nlattr
* const nfqa
[])
854 static const struct nla_policy nfqa_cfg_policy
[NFQA_CFG_MAX
+1] = {
855 [NFQA_CFG_CMD
] = { .len
= sizeof(struct nfqnl_msg_config_cmd
) },
856 [NFQA_CFG_PARAMS
] = { .len
= sizeof(struct nfqnl_msg_config_params
) },
859 static const struct nf_queue_handler nfqh
= {
860 .outfn
= &nfqnl_enqueue_packet
,
864 nfqnl_recv_config(struct sock
*ctnl
, struct sk_buff
*skb
,
865 const struct nlmsghdr
*nlh
,
866 const struct nlattr
* const nfqa
[])
868 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
869 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
870 struct nfqnl_instance
*queue
;
871 struct nfqnl_msg_config_cmd
*cmd
= NULL
;
874 if (nfqa
[NFQA_CFG_CMD
]) {
875 cmd
= nla_data(nfqa
[NFQA_CFG_CMD
]);
877 /* Obsolete commands without queue context */
878 switch (cmd
->command
) {
879 case NFQNL_CFG_CMD_PF_BIND
: return 0;
880 case NFQNL_CFG_CMD_PF_UNBIND
: return 0;
885 queue
= instance_lookup(queue_num
);
886 if (queue
&& queue
->peer_portid
!= NETLINK_CB(skb
).portid
) {
892 switch (cmd
->command
) {
893 case NFQNL_CFG_CMD_BIND
:
898 queue
= instance_create(queue_num
, NETLINK_CB(skb
).portid
);
900 ret
= PTR_ERR(queue
);
904 case NFQNL_CFG_CMD_UNBIND
:
909 instance_destroy(queue
);
911 case NFQNL_CFG_CMD_PF_BIND
:
912 case NFQNL_CFG_CMD_PF_UNBIND
:
920 if (nfqa
[NFQA_CFG_PARAMS
]) {
921 struct nfqnl_msg_config_params
*params
;
927 params
= nla_data(nfqa
[NFQA_CFG_PARAMS
]);
928 nfqnl_set_mode(queue
, params
->copy_mode
,
929 ntohl(params
->copy_range
));
932 if (nfqa
[NFQA_CFG_QUEUE_MAXLEN
]) {
933 __be32
*queue_maxlen
;
939 queue_maxlen
= nla_data(nfqa
[NFQA_CFG_QUEUE_MAXLEN
]);
940 spin_lock_bh(&queue
->lock
);
941 queue
->queue_maxlen
= ntohl(*queue_maxlen
);
942 spin_unlock_bh(&queue
->lock
);
945 if (nfqa
[NFQA_CFG_FLAGS
]) {
953 if (!nfqa
[NFQA_CFG_MASK
]) {
954 /* A mask is needed to specify which flags are being
961 flags
= ntohl(nla_get_be32(nfqa
[NFQA_CFG_FLAGS
]));
962 mask
= ntohl(nla_get_be32(nfqa
[NFQA_CFG_MASK
]));
964 if (flags
>= NFQA_CFG_F_MAX
) {
969 spin_lock_bh(&queue
->lock
);
970 queue
->flags
&= ~mask
;
971 queue
->flags
|= flags
& mask
;
972 spin_unlock_bh(&queue
->lock
);
980 static const struct nfnl_callback nfqnl_cb
[NFQNL_MSG_MAX
] = {
981 [NFQNL_MSG_PACKET
] = { .call_rcu
= nfqnl_recv_unsupp
,
982 .attr_count
= NFQA_MAX
, },
983 [NFQNL_MSG_VERDICT
] = { .call_rcu
= nfqnl_recv_verdict
,
984 .attr_count
= NFQA_MAX
,
985 .policy
= nfqa_verdict_policy
},
986 [NFQNL_MSG_CONFIG
] = { .call
= nfqnl_recv_config
,
987 .attr_count
= NFQA_CFG_MAX
,
988 .policy
= nfqa_cfg_policy
},
989 [NFQNL_MSG_VERDICT_BATCH
]={ .call_rcu
= nfqnl_recv_verdict_batch
,
990 .attr_count
= NFQA_MAX
,
991 .policy
= nfqa_verdict_batch_policy
},
994 static const struct nfnetlink_subsystem nfqnl_subsys
= {
996 .subsys_id
= NFNL_SUBSYS_QUEUE
,
997 .cb_count
= NFQNL_MSG_MAX
,
1001 #ifdef CONFIG_PROC_FS
1003 unsigned int bucket
;
1006 static struct hlist_node
*get_first(struct seq_file
*seq
)
1008 struct iter_state
*st
= seq
->private;
1013 for (st
->bucket
= 0; st
->bucket
< INSTANCE_BUCKETS
; st
->bucket
++) {
1014 if (!hlist_empty(&instance_table
[st
->bucket
]))
1015 return instance_table
[st
->bucket
].first
;
1020 static struct hlist_node
*get_next(struct seq_file
*seq
, struct hlist_node
*h
)
1022 struct iter_state
*st
= seq
->private;
1026 if (++st
->bucket
>= INSTANCE_BUCKETS
)
1029 h
= instance_table
[st
->bucket
].first
;
1034 static struct hlist_node
*get_idx(struct seq_file
*seq
, loff_t pos
)
1036 struct hlist_node
*head
;
1037 head
= get_first(seq
);
1040 while (pos
&& (head
= get_next(seq
, head
)))
1042 return pos
? NULL
: head
;
1045 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
1046 __acquires(instances_lock
)
1048 spin_lock(&instances_lock
);
1049 return get_idx(seq
, *pos
);
1052 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1055 return get_next(s
, v
);
1058 static void seq_stop(struct seq_file
*s
, void *v
)
1059 __releases(instances_lock
)
1061 spin_unlock(&instances_lock
);
1064 static int seq_show(struct seq_file
*s
, void *v
)
1066 const struct nfqnl_instance
*inst
= v
;
1068 return seq_printf(s
, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1070 inst
->peer_portid
, inst
->queue_total
,
1071 inst
->copy_mode
, inst
->copy_range
,
1072 inst
->queue_dropped
, inst
->queue_user_dropped
,
1073 inst
->id_sequence
, 1);
1076 static const struct seq_operations nfqnl_seq_ops
= {
1083 static int nfqnl_open(struct inode
*inode
, struct file
*file
)
1085 return seq_open_private(file
, &nfqnl_seq_ops
,
1086 sizeof(struct iter_state
));
1089 static const struct file_operations nfqnl_file_ops
= {
1090 .owner
= THIS_MODULE
,
1093 .llseek
= seq_lseek
,
1094 .release
= seq_release_private
,
1097 #endif /* PROC_FS */
1099 static int __init
nfnetlink_queue_init(void)
1101 int i
, status
= -ENOMEM
;
1103 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
1104 INIT_HLIST_HEAD(&instance_table
[i
]);
1106 netlink_register_notifier(&nfqnl_rtnl_notifier
);
1107 status
= nfnetlink_subsys_register(&nfqnl_subsys
);
1109 printk(KERN_ERR
"nf_queue: failed to create netlink socket\n");
1110 goto cleanup_netlink_notifier
;
1113 #ifdef CONFIG_PROC_FS
1114 if (!proc_create("nfnetlink_queue", 0440,
1115 proc_net_netfilter
, &nfqnl_file_ops
))
1116 goto cleanup_subsys
;
1119 register_netdevice_notifier(&nfqnl_dev_notifier
);
1120 nf_register_queue_handler(&nfqh
);
1123 #ifdef CONFIG_PROC_FS
1125 nfnetlink_subsys_unregister(&nfqnl_subsys
);
1127 cleanup_netlink_notifier
:
1128 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
1132 static void __exit
nfnetlink_queue_fini(void)
1134 nf_unregister_queue_handler();
1135 unregister_netdevice_notifier(&nfqnl_dev_notifier
);
1136 #ifdef CONFIG_PROC_FS
1137 remove_proc_entry("nfnetlink_queue", proc_net_netfilter
);
1139 nfnetlink_subsys_unregister(&nfqnl_subsys
);
1140 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
1142 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1145 MODULE_DESCRIPTION("netfilter packet queue handler");
1146 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1147 MODULE_LICENSE("GPL");
1148 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE
);
1150 module_init(nfnetlink_queue_init
);
1151 module_exit(nfnetlink_queue_fini
);