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 hlist_node
*pos
;
84 struct nfqnl_instance
*inst
;
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
)
94 static struct nfqnl_instance
*
95 instance_create(u_int16_t queue_num
, int pid
)
97 struct nfqnl_instance
*inst
;
101 spin_lock(&instances_lock
);
102 if (instance_lookup(queue_num
)) {
107 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
113 inst
->queue_num
= queue_num
;
114 inst
->peer_pid
= pid
;
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
);
121 if (!try_module_get(THIS_MODULE
)) {
126 h
= instance_hashfn(queue_num
);
127 hlist_add_head_rcu(&inst
->hlist
, &instance_table
[h
]);
129 spin_unlock(&instances_lock
);
136 spin_unlock(&instances_lock
);
140 static void nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
,
144 instance_destroy_rcu(struct rcu_head
*head
)
146 struct nfqnl_instance
*inst
= container_of(head
, struct nfqnl_instance
,
149 nfqnl_flush(inst
, NULL
, 0);
151 module_put(THIS_MODULE
);
155 __instance_destroy(struct nfqnl_instance
*inst
)
157 hlist_del_rcu(&inst
->hlist
);
158 call_rcu(&inst
->rcu
, instance_destroy_rcu
);
162 instance_destroy(struct nfqnl_instance
*inst
)
164 spin_lock(&instances_lock
);
165 __instance_destroy(inst
);
166 spin_unlock(&instances_lock
);
170 __enqueue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
172 list_add_tail(&entry
->list
, &queue
->queue_list
);
173 queue
->queue_total
++;
177 __dequeue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
179 list_del(&entry
->list
);
180 queue
->queue_total
--;
183 static struct nf_queue_entry
*
184 find_dequeue_entry(struct nfqnl_instance
*queue
, unsigned int id
)
186 struct nf_queue_entry
*entry
= NULL
, *i
;
188 spin_lock_bh(&queue
->lock
);
190 list_for_each_entry(i
, &queue
->queue_list
, list
) {
198 __dequeue_entry(queue
, entry
);
200 spin_unlock_bh(&queue
->lock
);
206 nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
, unsigned long data
)
208 struct nf_queue_entry
*entry
, *next
;
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
);
218 spin_unlock_bh(&queue
->lock
);
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
)
226 sk_buff_data_t old_tail
;
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
);
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 */
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
));
252 outdev
= entry
->outdev
;
254 switch ((enum nfqnl_config_mode
)ACCESS_ONCE(queue
->copy_mode
)) {
255 case NFQNL_COPY_META
:
256 case NFQNL_COPY_NONE
:
259 case NFQNL_COPY_PACKET
:
260 if (entskb
->ip_summed
== CHECKSUM_PARTIAL
&&
261 skb_checksum_help(entskb
))
264 data_len
= ACCESS_ONCE(queue
->copy_range
);
265 if (data_len
== 0 || data_len
> entskb
->len
)
266 data_len
= entskb
->len
;
268 size
+= nla_total_size(data_len
);
272 if (queue
->flags
& NFQA_CFG_F_CONNTRACK
)
273 ct
= nfqnl_ct_get(entskb
, &size
, &ctinfo
);
275 skb
= alloc_skb(size
, GFP_ATOMIC
);
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);
287 nfmsg
= nlmsg_data(nlh
);
288 nfmsg
->nfgen_family
= entry
->pf
;
289 nfmsg
->version
= NFNETLINK_V0
;
290 nfmsg
->res_id
= htons(queue
->queue_num
);
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
;
298 indev
= entry
->indev
;
300 #ifndef CONFIG_BRIDGE_NETFILTER
301 if (nla_put_be32(skb
, NFQA_IFINDEX_INDEV
, htonl(indev
->ifindex
)))
302 goto nla_put_failure
;
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
;
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
;
330 #ifndef CONFIG_BRIDGE_NETFILTER
331 if (nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
, htonl(outdev
->ifindex
)))
332 goto nla_put_failure
;
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
;
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
;
360 nla_put_be32(skb
, NFQA_MARK
, htonl(entskb
->mark
)))
361 goto nla_put_failure
;
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
);
368 phw
.hw_addrlen
= htons(len
);
369 if (nla_put(skb
, NFQA_HWADDR
, sizeof(phw
), &phw
))
370 goto nla_put_failure
;
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
);
380 if (nla_put(skb
, NFQA_TIMESTAMP
, sizeof(ts
), &ts
))
381 goto nla_put_failure
;
386 int sz
= nla_attr_size(data_len
);
388 if (skb_tailroom(skb
) < nla_total_size(data_len
)) {
389 printk(KERN_WARNING
"nf_queue: no tailroom!\n");
394 nla
= (struct nlattr
*)skb_put(skb
, nla_total_size(data_len
));
395 nla
->nla_type
= NFQA_PAYLOAD
;
398 if (skb_copy_bits(entskb
, 0, nla_data(nla
), data_len
))
402 if (ct
&& nfqnl_ct_put(skb
, ct
, ctinfo
) < 0)
403 goto nla_put_failure
;
405 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
411 net_err_ratelimited("nf_queue: error creating packet message\n");
416 nfqnl_enqueue_packet(struct nf_queue_entry
*entry
, unsigned int queuenum
)
418 struct sk_buff
*nskb
;
419 struct nfqnl_instance
*queue
;
421 __be32
*packet_id_ptr
;
424 /* rcu_read_lock()ed by nf_hook_slow() */
425 queue
= instance_lookup(queuenum
);
431 if (queue
->copy_mode
== NFQNL_COPY_NONE
) {
436 nskb
= nfqnl_build_packet_message(queue
, entry
, &packet_id_ptr
);
441 spin_lock_bh(&queue
->lock
);
443 if (!queue
->peer_pid
) {
445 goto err_out_free_nskb
;
447 if (queue
->queue_total
>= queue
->queue_maxlen
) {
448 if (queue
->flags
& NFQA_CFG_F_FAIL_OPEN
) {
452 queue
->queue_dropped
++;
453 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
456 goto err_out_free_nskb
;
458 entry
->id
= ++queue
->id_sequence
;
459 *packet_id_ptr
= htonl(entry
->id
);
461 /* nfnetlink_unicast will either free the nskb or add it to a socket */
462 err
= nfnetlink_unicast(nskb
, &init_net
, queue
->peer_pid
, MSG_DONTWAIT
);
464 queue
->queue_user_dropped
++;
468 __enqueue_entry(queue
, entry
);
470 spin_unlock_bh(&queue
->lock
);
476 spin_unlock_bh(&queue
->lock
);
478 nf_reinject(entry
, NF_ACCEPT
);
484 nfqnl_mangle(void *data
, int data_len
, struct nf_queue_entry
*e
, int diff
)
486 struct sk_buff
*nskb
;
489 if (pskb_trim(e
->skb
, data_len
))
491 } else if (diff
> 0) {
492 if (data_len
> 0xFFFF)
494 if (diff
> skb_tailroom(e
->skb
)) {
495 nskb
= skb_copy_expand(e
->skb
, skb_headroom(e
->skb
),
498 printk(KERN_WARNING
"nf_queue: OOM "
499 "in mangle, dropping packet\n");
505 skb_put(e
->skb
, diff
);
507 if (!skb_make_writable(e
->skb
, data_len
))
509 skb_copy_to_linear_data(e
->skb
, data
, data_len
);
510 e
->skb
->ip_summed
= CHECKSUM_NONE
;
515 nfqnl_set_mode(struct nfqnl_instance
*queue
,
516 unsigned char mode
, unsigned int range
)
520 spin_lock_bh(&queue
->lock
);
522 case NFQNL_COPY_NONE
:
523 case NFQNL_COPY_META
:
524 queue
->copy_mode
= mode
;
525 queue
->copy_range
= 0;
528 case NFQNL_COPY_PACKET
:
529 queue
->copy_mode
= mode
;
530 /* we're using struct nlattr which has 16bit nla_len */
532 queue
->copy_range
= 0xffff;
534 queue
->copy_range
= range
;
541 spin_unlock_bh(&queue
->lock
);
547 dev_cmp(struct nf_queue_entry
*entry
, unsigned long ifindex
)
550 if (entry
->indev
->ifindex
== ifindex
)
553 if (entry
->outdev
->ifindex
== ifindex
)
555 #ifdef CONFIG_BRIDGE_NETFILTER
556 if (entry
->skb
->nf_bridge
) {
557 if (entry
->skb
->nf_bridge
->physindev
&&
558 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
560 if (entry
->skb
->nf_bridge
->physoutdev
&&
561 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
568 /* drop all packets with either indev or outdev == ifindex from all queue
571 nfqnl_dev_drop(int ifindex
)
577 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
578 struct hlist_node
*tmp
;
579 struct nfqnl_instance
*inst
;
580 struct hlist_head
*head
= &instance_table
[i
];
582 hlist_for_each_entry_rcu(inst
, tmp
, head
, hlist
)
583 nfqnl_flush(inst
, dev_cmp
, ifindex
);
589 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
592 nfqnl_rcv_dev_event(struct notifier_block
*this,
593 unsigned long event
, void *ptr
)
595 struct net_device
*dev
= ptr
;
597 if (!net_eq(dev_net(dev
), &init_net
))
600 /* Drop any packets associated with the downed device */
601 if (event
== NETDEV_DOWN
)
602 nfqnl_dev_drop(dev
->ifindex
);
606 static struct notifier_block nfqnl_dev_notifier
= {
607 .notifier_call
= nfqnl_rcv_dev_event
,
611 nfqnl_rcv_nl_event(struct notifier_block
*this,
612 unsigned long event
, void *ptr
)
614 struct netlink_notify
*n
= ptr
;
616 if (event
== NETLINK_URELEASE
&& n
->protocol
== NETLINK_NETFILTER
) {
619 /* destroy all instances for this pid */
620 spin_lock(&instances_lock
);
621 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
622 struct hlist_node
*tmp
, *t2
;
623 struct nfqnl_instance
*inst
;
624 struct hlist_head
*head
= &instance_table
[i
];
626 hlist_for_each_entry_safe(inst
, tmp
, t2
, head
, hlist
) {
627 if ((n
->net
== &init_net
) &&
628 (n
->pid
== inst
->peer_pid
))
629 __instance_destroy(inst
);
632 spin_unlock(&instances_lock
);
637 static struct notifier_block nfqnl_rtnl_notifier
= {
638 .notifier_call
= nfqnl_rcv_nl_event
,
641 static const struct nla_policy nfqa_verdict_policy
[NFQA_MAX
+1] = {
642 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
643 [NFQA_MARK
] = { .type
= NLA_U32
},
644 [NFQA_PAYLOAD
] = { .type
= NLA_UNSPEC
},
645 [NFQA_CT
] = { .type
= NLA_UNSPEC
},
648 static const struct nla_policy nfqa_verdict_batch_policy
[NFQA_MAX
+1] = {
649 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
650 [NFQA_MARK
] = { .type
= NLA_U32
},
653 static struct nfqnl_instance
*verdict_instance_lookup(u16 queue_num
, int nlpid
)
655 struct nfqnl_instance
*queue
;
657 queue
= instance_lookup(queue_num
);
659 return ERR_PTR(-ENODEV
);
661 if (queue
->peer_pid
!= nlpid
)
662 return ERR_PTR(-EPERM
);
667 static struct nfqnl_msg_verdict_hdr
*
668 verdicthdr_get(const struct nlattr
* const nfqa
[])
670 struct nfqnl_msg_verdict_hdr
*vhdr
;
671 unsigned int verdict
;
673 if (!nfqa
[NFQA_VERDICT_HDR
])
676 vhdr
= nla_data(nfqa
[NFQA_VERDICT_HDR
]);
677 verdict
= ntohl(vhdr
->verdict
) & NF_VERDICT_MASK
;
678 if (verdict
> NF_MAX_VERDICT
|| verdict
== NF_STOLEN
)
683 static int nfq_id_after(unsigned int id
, unsigned int max
)
685 return (int)(id
- max
) > 0;
689 nfqnl_recv_verdict_batch(struct sock
*ctnl
, struct sk_buff
*skb
,
690 const struct nlmsghdr
*nlh
,
691 const struct nlattr
* const nfqa
[])
693 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
694 struct nf_queue_entry
*entry
, *tmp
;
695 unsigned int verdict
, maxid
;
696 struct nfqnl_msg_verdict_hdr
*vhdr
;
697 struct nfqnl_instance
*queue
;
698 LIST_HEAD(batch_list
);
699 u16 queue_num
= ntohs(nfmsg
->res_id
);
701 queue
= verdict_instance_lookup(queue_num
, NETLINK_CB(skb
).pid
);
703 return PTR_ERR(queue
);
705 vhdr
= verdicthdr_get(nfqa
);
709 verdict
= ntohl(vhdr
->verdict
);
710 maxid
= ntohl(vhdr
->id
);
712 spin_lock_bh(&queue
->lock
);
714 list_for_each_entry_safe(entry
, tmp
, &queue
->queue_list
, list
) {
715 if (nfq_id_after(entry
->id
, maxid
))
717 __dequeue_entry(queue
, entry
);
718 list_add_tail(&entry
->list
, &batch_list
);
721 spin_unlock_bh(&queue
->lock
);
723 if (list_empty(&batch_list
))
726 list_for_each_entry_safe(entry
, tmp
, &batch_list
, list
) {
728 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
729 nf_reinject(entry
, verdict
);
735 nfqnl_recv_verdict(struct sock
*ctnl
, struct sk_buff
*skb
,
736 const struct nlmsghdr
*nlh
,
737 const struct nlattr
* const nfqa
[])
739 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
740 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
742 struct nfqnl_msg_verdict_hdr
*vhdr
;
743 struct nfqnl_instance
*queue
;
744 unsigned int verdict
;
745 struct nf_queue_entry
*entry
;
746 enum ip_conntrack_info
uninitialized_var(ctinfo
);
747 struct nf_conn
*ct
= NULL
;
749 queue
= instance_lookup(queue_num
);
752 queue
= verdict_instance_lookup(queue_num
, NETLINK_CB(skb
).pid
);
754 return PTR_ERR(queue
);
756 vhdr
= verdicthdr_get(nfqa
);
760 verdict
= ntohl(vhdr
->verdict
);
762 entry
= find_dequeue_entry(queue
, ntohl(vhdr
->id
));
767 if (nfqa
[NFQA_CT
] && (queue
->flags
& NFQA_CFG_F_CONNTRACK
))
768 ct
= nfqnl_ct_parse(entry
->skb
, nfqa
[NFQA_CT
], &ctinfo
);
770 if (nfqa
[NFQA_PAYLOAD
]) {
771 u16 payload_len
= nla_len(nfqa
[NFQA_PAYLOAD
]);
772 int diff
= payload_len
- entry
->skb
->len
;
774 if (nfqnl_mangle(nla_data(nfqa
[NFQA_PAYLOAD
]),
775 payload_len
, entry
, diff
) < 0)
779 nfqnl_ct_seq_adjust(skb
, ct
, ctinfo
, diff
);
784 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
786 nf_reinject(entry
, verdict
);
791 nfqnl_recv_unsupp(struct sock
*ctnl
, struct sk_buff
*skb
,
792 const struct nlmsghdr
*nlh
,
793 const struct nlattr
* const nfqa
[])
798 static const struct nla_policy nfqa_cfg_policy
[NFQA_CFG_MAX
+1] = {
799 [NFQA_CFG_CMD
] = { .len
= sizeof(struct nfqnl_msg_config_cmd
) },
800 [NFQA_CFG_PARAMS
] = { .len
= sizeof(struct nfqnl_msg_config_params
) },
803 static const struct nf_queue_handler nfqh
= {
805 .outfn
= &nfqnl_enqueue_packet
,
809 nfqnl_recv_config(struct sock
*ctnl
, struct sk_buff
*skb
,
810 const struct nlmsghdr
*nlh
,
811 const struct nlattr
* const nfqa
[])
813 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
814 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
815 struct nfqnl_instance
*queue
;
816 struct nfqnl_msg_config_cmd
*cmd
= NULL
;
819 if (nfqa
[NFQA_CFG_CMD
]) {
820 cmd
= nla_data(nfqa
[NFQA_CFG_CMD
]);
822 /* Commands without queue context - might sleep */
823 switch (cmd
->command
) {
824 case NFQNL_CFG_CMD_PF_BIND
:
825 return nf_register_queue_handler(ntohs(cmd
->pf
),
827 case NFQNL_CFG_CMD_PF_UNBIND
:
828 return nf_unregister_queue_handler(ntohs(cmd
->pf
),
834 queue
= instance_lookup(queue_num
);
835 if (queue
&& queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
841 switch (cmd
->command
) {
842 case NFQNL_CFG_CMD_BIND
:
847 queue
= instance_create(queue_num
, NETLINK_CB(skb
).pid
);
849 ret
= PTR_ERR(queue
);
853 case NFQNL_CFG_CMD_UNBIND
:
858 instance_destroy(queue
);
860 case NFQNL_CFG_CMD_PF_BIND
:
861 case NFQNL_CFG_CMD_PF_UNBIND
:
869 if (nfqa
[NFQA_CFG_PARAMS
]) {
870 struct nfqnl_msg_config_params
*params
;
876 params
= nla_data(nfqa
[NFQA_CFG_PARAMS
]);
877 nfqnl_set_mode(queue
, params
->copy_mode
,
878 ntohl(params
->copy_range
));
881 if (nfqa
[NFQA_CFG_QUEUE_MAXLEN
]) {
882 __be32
*queue_maxlen
;
888 queue_maxlen
= nla_data(nfqa
[NFQA_CFG_QUEUE_MAXLEN
]);
889 spin_lock_bh(&queue
->lock
);
890 queue
->queue_maxlen
= ntohl(*queue_maxlen
);
891 spin_unlock_bh(&queue
->lock
);
894 if (nfqa
[NFQA_CFG_FLAGS
]) {
902 if (!nfqa
[NFQA_CFG_MASK
]) {
903 /* A mask is needed to specify which flags are being
910 flags
= ntohl(nla_get_be32(nfqa
[NFQA_CFG_FLAGS
]));
911 mask
= ntohl(nla_get_be32(nfqa
[NFQA_CFG_MASK
]));
913 if (flags
>= NFQA_CFG_F_MAX
) {
918 spin_lock_bh(&queue
->lock
);
919 queue
->flags
&= ~mask
;
920 queue
->flags
|= flags
& mask
;
921 spin_unlock_bh(&queue
->lock
);
929 static const struct nfnl_callback nfqnl_cb
[NFQNL_MSG_MAX
] = {
930 [NFQNL_MSG_PACKET
] = { .call_rcu
= nfqnl_recv_unsupp
,
931 .attr_count
= NFQA_MAX
, },
932 [NFQNL_MSG_VERDICT
] = { .call_rcu
= nfqnl_recv_verdict
,
933 .attr_count
= NFQA_MAX
,
934 .policy
= nfqa_verdict_policy
},
935 [NFQNL_MSG_CONFIG
] = { .call
= nfqnl_recv_config
,
936 .attr_count
= NFQA_CFG_MAX
,
937 .policy
= nfqa_cfg_policy
},
938 [NFQNL_MSG_VERDICT_BATCH
]={ .call_rcu
= nfqnl_recv_verdict_batch
,
939 .attr_count
= NFQA_MAX
,
940 .policy
= nfqa_verdict_batch_policy
},
943 static const struct nfnetlink_subsystem nfqnl_subsys
= {
945 .subsys_id
= NFNL_SUBSYS_QUEUE
,
946 .cb_count
= NFQNL_MSG_MAX
,
950 #ifdef CONFIG_PROC_FS
955 static struct hlist_node
*get_first(struct seq_file
*seq
)
957 struct iter_state
*st
= seq
->private;
962 for (st
->bucket
= 0; st
->bucket
< INSTANCE_BUCKETS
; st
->bucket
++) {
963 if (!hlist_empty(&instance_table
[st
->bucket
]))
964 return instance_table
[st
->bucket
].first
;
969 static struct hlist_node
*get_next(struct seq_file
*seq
, struct hlist_node
*h
)
971 struct iter_state
*st
= seq
->private;
975 if (++st
->bucket
>= INSTANCE_BUCKETS
)
978 h
= instance_table
[st
->bucket
].first
;
983 static struct hlist_node
*get_idx(struct seq_file
*seq
, loff_t pos
)
985 struct hlist_node
*head
;
986 head
= get_first(seq
);
989 while (pos
&& (head
= get_next(seq
, head
)))
991 return pos
? NULL
: head
;
994 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
995 __acquires(instances_lock
)
997 spin_lock(&instances_lock
);
998 return get_idx(seq
, *pos
);
1001 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1004 return get_next(s
, v
);
1007 static void seq_stop(struct seq_file
*s
, void *v
)
1008 __releases(instances_lock
)
1010 spin_unlock(&instances_lock
);
1013 static int seq_show(struct seq_file
*s
, void *v
)
1015 const struct nfqnl_instance
*inst
= v
;
1017 return seq_printf(s
, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1019 inst
->peer_pid
, inst
->queue_total
,
1020 inst
->copy_mode
, inst
->copy_range
,
1021 inst
->queue_dropped
, inst
->queue_user_dropped
,
1022 inst
->id_sequence
, 1);
1025 static const struct seq_operations nfqnl_seq_ops
= {
1032 static int nfqnl_open(struct inode
*inode
, struct file
*file
)
1034 return seq_open_private(file
, &nfqnl_seq_ops
,
1035 sizeof(struct iter_state
));
1038 static const struct file_operations nfqnl_file_ops
= {
1039 .owner
= THIS_MODULE
,
1042 .llseek
= seq_lseek
,
1043 .release
= seq_release_private
,
1046 #endif /* PROC_FS */
1048 static int __init
nfnetlink_queue_init(void)
1050 int i
, status
= -ENOMEM
;
1052 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
1053 INIT_HLIST_HEAD(&instance_table
[i
]);
1055 netlink_register_notifier(&nfqnl_rtnl_notifier
);
1056 status
= nfnetlink_subsys_register(&nfqnl_subsys
);
1058 printk(KERN_ERR
"nf_queue: failed to create netlink socket\n");
1059 goto cleanup_netlink_notifier
;
1062 #ifdef CONFIG_PROC_FS
1063 if (!proc_create("nfnetlink_queue", 0440,
1064 proc_net_netfilter
, &nfqnl_file_ops
))
1065 goto cleanup_subsys
;
1068 register_netdevice_notifier(&nfqnl_dev_notifier
);
1071 #ifdef CONFIG_PROC_FS
1073 nfnetlink_subsys_unregister(&nfqnl_subsys
);
1075 cleanup_netlink_notifier
:
1076 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
1080 static void __exit
nfnetlink_queue_fini(void)
1082 nf_unregister_queue_handlers(&nfqh
);
1083 unregister_netdevice_notifier(&nfqnl_dev_notifier
);
1084 #ifdef CONFIG_PROC_FS
1085 remove_proc_entry("nfnetlink_queue", proc_net_netfilter
);
1087 nfnetlink_subsys_unregister(&nfqnl_subsys
);
1088 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
1090 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1093 MODULE_DESCRIPTION("netfilter packet queue handler");
1094 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1095 MODULE_LICENSE("GPL");
1096 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE
);
1098 module_init(nfnetlink_queue_init
);
1099 module_exit(nfnetlink_queue_fini
);