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>
34 #include <asm/atomic.h>
36 #ifdef CONFIG_BRIDGE_NETFILTER
37 #include "../bridge/br_private.h"
40 #define NFQNL_QMAX_DEFAULT 1024
42 struct nfqnl_instance
{
43 struct hlist_node hlist
; /* global list of queues */
47 unsigned int queue_maxlen
;
48 unsigned int copy_range
;
49 unsigned int queue_dropped
;
50 unsigned int queue_user_dropped
;
53 u_int16_t queue_num
; /* number of this queue */
56 * Following fields are dirtied for each queued packet,
57 * keep them in same cache line if possible.
60 unsigned int queue_total
;
61 atomic_t id_sequence
; /* 'sequence' of pkt ids */
62 struct list_head queue_list
; /* packets in queue */
65 typedef int (*nfqnl_cmpfn
)(struct nf_queue_entry
*, unsigned long);
67 static DEFINE_SPINLOCK(instances_lock
);
69 #define INSTANCE_BUCKETS 16
70 static struct hlist_head instance_table
[INSTANCE_BUCKETS
] __read_mostly
;
72 static inline u_int8_t
instance_hashfn(u_int16_t queue_num
)
74 return ((queue_num
>> 8) | queue_num
) % INSTANCE_BUCKETS
;
77 static struct nfqnl_instance
*
78 instance_lookup(u_int16_t queue_num
)
80 struct hlist_head
*head
;
81 struct hlist_node
*pos
;
82 struct nfqnl_instance
*inst
;
84 head
= &instance_table
[instance_hashfn(queue_num
)];
85 hlist_for_each_entry_rcu(inst
, pos
, head
, hlist
) {
86 if (inst
->queue_num
== queue_num
)
92 static struct nfqnl_instance
*
93 instance_create(u_int16_t queue_num
, int pid
)
95 struct nfqnl_instance
*inst
;
99 spin_lock(&instances_lock
);
100 if (instance_lookup(queue_num
)) {
105 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
111 inst
->queue_num
= queue_num
;
112 inst
->peer_pid
= pid
;
113 inst
->queue_maxlen
= NFQNL_QMAX_DEFAULT
;
114 inst
->copy_range
= 0xfffff;
115 inst
->copy_mode
= NFQNL_COPY_NONE
;
116 spin_lock_init(&inst
->lock
);
117 INIT_LIST_HEAD(&inst
->queue_list
);
119 if (!try_module_get(THIS_MODULE
)) {
124 h
= instance_hashfn(queue_num
);
125 hlist_add_head_rcu(&inst
->hlist
, &instance_table
[h
]);
127 spin_unlock(&instances_lock
);
134 spin_unlock(&instances_lock
);
138 static void nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
,
142 instance_destroy_rcu(struct rcu_head
*head
)
144 struct nfqnl_instance
*inst
= container_of(head
, struct nfqnl_instance
,
147 nfqnl_flush(inst
, NULL
, 0);
149 module_put(THIS_MODULE
);
153 __instance_destroy(struct nfqnl_instance
*inst
)
155 hlist_del_rcu(&inst
->hlist
);
156 call_rcu(&inst
->rcu
, instance_destroy_rcu
);
160 instance_destroy(struct nfqnl_instance
*inst
)
162 spin_lock(&instances_lock
);
163 __instance_destroy(inst
);
164 spin_unlock(&instances_lock
);
168 __enqueue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
170 list_add_tail(&entry
->list
, &queue
->queue_list
);
171 queue
->queue_total
++;
174 static struct nf_queue_entry
*
175 find_dequeue_entry(struct nfqnl_instance
*queue
, unsigned int id
)
177 struct nf_queue_entry
*entry
= NULL
, *i
;
179 spin_lock_bh(&queue
->lock
);
181 list_for_each_entry(i
, &queue
->queue_list
, list
) {
189 list_del(&entry
->list
);
190 queue
->queue_total
--;
193 spin_unlock_bh(&queue
->lock
);
199 nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
, unsigned long data
)
201 struct nf_queue_entry
*entry
, *next
;
203 spin_lock_bh(&queue
->lock
);
204 list_for_each_entry_safe(entry
, next
, &queue
->queue_list
, list
) {
205 if (!cmpfn
|| cmpfn(entry
, data
)) {
206 list_del(&entry
->list
);
207 queue
->queue_total
--;
208 nf_reinject(entry
, NF_DROP
);
211 spin_unlock_bh(&queue
->lock
);
214 static struct sk_buff
*
215 nfqnl_build_packet_message(struct nfqnl_instance
*queue
,
216 struct nf_queue_entry
*entry
)
218 sk_buff_data_t old_tail
;
222 struct nfqnl_msg_packet_hdr pmsg
;
223 struct nlmsghdr
*nlh
;
224 struct nfgenmsg
*nfmsg
;
225 struct sk_buff
*entskb
= entry
->skb
;
226 struct net_device
*indev
;
227 struct net_device
*outdev
;
229 size
= NLMSG_SPACE(sizeof(struct nfgenmsg
))
230 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr
))
231 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
232 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
233 #ifdef CONFIG_BRIDGE_NETFILTER
234 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
235 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
237 + nla_total_size(sizeof(u_int32_t
)) /* mark */
238 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw
))
239 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp
));
241 outdev
= entry
->outdev
;
243 switch ((enum nfqnl_config_mode
)ACCESS_ONCE(queue
->copy_mode
)) {
244 case NFQNL_COPY_META
:
245 case NFQNL_COPY_NONE
:
248 case NFQNL_COPY_PACKET
:
249 if (entskb
->ip_summed
== CHECKSUM_PARTIAL
&&
250 skb_checksum_help(entskb
))
253 data_len
= ACCESS_ONCE(queue
->copy_range
);
254 if (data_len
== 0 || data_len
> entskb
->len
)
255 data_len
= entskb
->len
;
257 size
+= nla_total_size(data_len
);
262 skb
= alloc_skb(size
, GFP_ATOMIC
);
266 old_tail
= skb
->tail
;
267 nlh
= NLMSG_PUT(skb
, 0, 0,
268 NFNL_SUBSYS_QUEUE
<< 8 | NFQNL_MSG_PACKET
,
269 sizeof(struct nfgenmsg
));
270 nfmsg
= NLMSG_DATA(nlh
);
271 nfmsg
->nfgen_family
= entry
->pf
;
272 nfmsg
->version
= NFNETLINK_V0
;
273 nfmsg
->res_id
= htons(queue
->queue_num
);
275 entry
->id
= atomic_inc_return(&queue
->id_sequence
);
276 pmsg
.packet_id
= htonl(entry
->id
);
277 pmsg
.hw_protocol
= entskb
->protocol
;
278 pmsg
.hook
= entry
->hook
;
280 NLA_PUT(skb
, NFQA_PACKET_HDR
, sizeof(pmsg
), &pmsg
);
282 indev
= entry
->indev
;
284 #ifndef CONFIG_BRIDGE_NETFILTER
285 NLA_PUT_BE32(skb
, NFQA_IFINDEX_INDEV
, htonl(indev
->ifindex
));
287 if (entry
->pf
== PF_BRIDGE
) {
288 /* Case 1: indev is physical input device, we need to
289 * look for bridge group (when called from
290 * netfilter_bridge) */
291 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSINDEV
,
292 htonl(indev
->ifindex
));
293 /* this is the bridge group "brX" */
294 /* rcu_read_lock()ed by __nf_queue */
295 NLA_PUT_BE32(skb
, NFQA_IFINDEX_INDEV
,
296 htonl(br_port_get_rcu(indev
)->br
->dev
->ifindex
));
298 /* Case 2: indev is bridge group, we need to look for
299 * physical device (when called from ipv4) */
300 NLA_PUT_BE32(skb
, NFQA_IFINDEX_INDEV
,
301 htonl(indev
->ifindex
));
302 if (entskb
->nf_bridge
&& entskb
->nf_bridge
->physindev
)
303 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSINDEV
,
304 htonl(entskb
->nf_bridge
->physindev
->ifindex
));
310 #ifndef CONFIG_BRIDGE_NETFILTER
311 NLA_PUT_BE32(skb
, NFQA_IFINDEX_OUTDEV
, htonl(outdev
->ifindex
));
313 if (entry
->pf
== PF_BRIDGE
) {
314 /* Case 1: outdev is physical output device, we need to
315 * look for bridge group (when called from
316 * netfilter_bridge) */
317 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
318 htonl(outdev
->ifindex
));
319 /* this is the bridge group "brX" */
320 /* rcu_read_lock()ed by __nf_queue */
321 NLA_PUT_BE32(skb
, NFQA_IFINDEX_OUTDEV
,
322 htonl(br_port_get_rcu(outdev
)->br
->dev
->ifindex
));
324 /* Case 2: outdev is bridge group, we need to look for
325 * physical output device (when called from ipv4) */
326 NLA_PUT_BE32(skb
, NFQA_IFINDEX_OUTDEV
,
327 htonl(outdev
->ifindex
));
328 if (entskb
->nf_bridge
&& entskb
->nf_bridge
->physoutdev
)
329 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
330 htonl(entskb
->nf_bridge
->physoutdev
->ifindex
));
336 NLA_PUT_BE32(skb
, NFQA_MARK
, htonl(entskb
->mark
));
338 if (indev
&& entskb
->dev
&&
339 entskb
->mac_header
!= entskb
->network_header
) {
340 struct nfqnl_msg_packet_hw phw
;
341 int len
= dev_parse_header(entskb
, phw
.hw_addr
);
343 phw
.hw_addrlen
= htons(len
);
344 NLA_PUT(skb
, NFQA_HWADDR
, sizeof(phw
), &phw
);
348 if (entskb
->tstamp
.tv64
) {
349 struct nfqnl_msg_packet_timestamp ts
;
350 struct timeval tv
= ktime_to_timeval(entskb
->tstamp
);
351 ts
.sec
= cpu_to_be64(tv
.tv_sec
);
352 ts
.usec
= cpu_to_be64(tv
.tv_usec
);
354 NLA_PUT(skb
, NFQA_TIMESTAMP
, sizeof(ts
), &ts
);
359 int sz
= nla_attr_size(data_len
);
361 if (skb_tailroom(skb
) < nla_total_size(data_len
)) {
362 printk(KERN_WARNING
"nf_queue: no tailroom!\n");
366 nla
= (struct nlattr
*)skb_put(skb
, nla_total_size(data_len
));
367 nla
->nla_type
= NFQA_PAYLOAD
;
370 if (skb_copy_bits(entskb
, 0, nla_data(nla
), data_len
))
374 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
382 printk(KERN_ERR
"nf_queue: error creating packet message\n");
387 nfqnl_enqueue_packet(struct nf_queue_entry
*entry
, unsigned int queuenum
)
389 struct sk_buff
*nskb
;
390 struct nfqnl_instance
*queue
;
393 /* rcu_read_lock()ed by nf_hook_slow() */
394 queue
= instance_lookup(queuenum
);
400 if (queue
->copy_mode
== NFQNL_COPY_NONE
) {
405 nskb
= nfqnl_build_packet_message(queue
, entry
);
410 spin_lock_bh(&queue
->lock
);
412 if (!queue
->peer_pid
) {
414 goto err_out_free_nskb
;
416 if (queue
->queue_total
>= queue
->queue_maxlen
) {
417 queue
->queue_dropped
++;
419 printk(KERN_WARNING
"nf_queue: full at %d entries, "
420 "dropping packets(s).\n",
422 goto err_out_free_nskb
;
425 /* nfnetlink_unicast will either free the nskb or add it to a socket */
426 err
= nfnetlink_unicast(nskb
, &init_net
, queue
->peer_pid
, MSG_DONTWAIT
);
428 queue
->queue_user_dropped
++;
432 __enqueue_entry(queue
, entry
);
434 spin_unlock_bh(&queue
->lock
);
440 spin_unlock_bh(&queue
->lock
);
446 nfqnl_mangle(void *data
, int data_len
, struct nf_queue_entry
*e
)
448 struct sk_buff
*nskb
;
451 diff
= data_len
- e
->skb
->len
;
453 if (pskb_trim(e
->skb
, data_len
))
455 } else if (diff
> 0) {
456 if (data_len
> 0xFFFF)
458 if (diff
> skb_tailroom(e
->skb
)) {
459 nskb
= skb_copy_expand(e
->skb
, skb_headroom(e
->skb
),
462 printk(KERN_WARNING
"nf_queue: OOM "
463 "in mangle, dropping packet\n");
469 skb_put(e
->skb
, diff
);
471 if (!skb_make_writable(e
->skb
, data_len
))
473 skb_copy_to_linear_data(e
->skb
, data
, data_len
);
474 e
->skb
->ip_summed
= CHECKSUM_NONE
;
479 nfqnl_set_mode(struct nfqnl_instance
*queue
,
480 unsigned char mode
, unsigned int range
)
484 spin_lock_bh(&queue
->lock
);
486 case NFQNL_COPY_NONE
:
487 case NFQNL_COPY_META
:
488 queue
->copy_mode
= mode
;
489 queue
->copy_range
= 0;
492 case NFQNL_COPY_PACKET
:
493 queue
->copy_mode
= mode
;
494 /* we're using struct nlattr which has 16bit nla_len */
496 queue
->copy_range
= 0xffff;
498 queue
->copy_range
= range
;
505 spin_unlock_bh(&queue
->lock
);
511 dev_cmp(struct nf_queue_entry
*entry
, unsigned long ifindex
)
514 if (entry
->indev
->ifindex
== ifindex
)
517 if (entry
->outdev
->ifindex
== ifindex
)
519 #ifdef CONFIG_BRIDGE_NETFILTER
520 if (entry
->skb
->nf_bridge
) {
521 if (entry
->skb
->nf_bridge
->physindev
&&
522 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
524 if (entry
->skb
->nf_bridge
->physoutdev
&&
525 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
532 /* drop all packets with either indev or outdev == ifindex from all queue
535 nfqnl_dev_drop(int ifindex
)
541 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
542 struct hlist_node
*tmp
;
543 struct nfqnl_instance
*inst
;
544 struct hlist_head
*head
= &instance_table
[i
];
546 hlist_for_each_entry_rcu(inst
, tmp
, head
, hlist
)
547 nfqnl_flush(inst
, dev_cmp
, ifindex
);
553 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
556 nfqnl_rcv_dev_event(struct notifier_block
*this,
557 unsigned long event
, void *ptr
)
559 struct net_device
*dev
= ptr
;
561 if (!net_eq(dev_net(dev
), &init_net
))
564 /* Drop any packets associated with the downed device */
565 if (event
== NETDEV_DOWN
)
566 nfqnl_dev_drop(dev
->ifindex
);
570 static struct notifier_block nfqnl_dev_notifier
= {
571 .notifier_call
= nfqnl_rcv_dev_event
,
575 nfqnl_rcv_nl_event(struct notifier_block
*this,
576 unsigned long event
, void *ptr
)
578 struct netlink_notify
*n
= ptr
;
580 if (event
== NETLINK_URELEASE
&& n
->protocol
== NETLINK_NETFILTER
) {
583 /* destroy all instances for this pid */
584 spin_lock(&instances_lock
);
585 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
586 struct hlist_node
*tmp
, *t2
;
587 struct nfqnl_instance
*inst
;
588 struct hlist_head
*head
= &instance_table
[i
];
590 hlist_for_each_entry_safe(inst
, tmp
, t2
, head
, hlist
) {
591 if ((n
->net
== &init_net
) &&
592 (n
->pid
== inst
->peer_pid
))
593 __instance_destroy(inst
);
596 spin_unlock(&instances_lock
);
601 static struct notifier_block nfqnl_rtnl_notifier
= {
602 .notifier_call
= nfqnl_rcv_nl_event
,
605 static const struct nla_policy nfqa_verdict_policy
[NFQA_MAX
+1] = {
606 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
607 [NFQA_MARK
] = { .type
= NLA_U32
},
608 [NFQA_PAYLOAD
] = { .type
= NLA_UNSPEC
},
612 nfqnl_recv_verdict(struct sock
*ctnl
, struct sk_buff
*skb
,
613 const struct nlmsghdr
*nlh
,
614 const struct nlattr
* const nfqa
[])
616 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
617 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
619 struct nfqnl_msg_verdict_hdr
*vhdr
;
620 struct nfqnl_instance
*queue
;
621 unsigned int verdict
;
622 struct nf_queue_entry
*entry
;
626 queue
= instance_lookup(queue_num
);
632 if (queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
637 if (!nfqa
[NFQA_VERDICT_HDR
]) {
642 vhdr
= nla_data(nfqa
[NFQA_VERDICT_HDR
]);
643 verdict
= ntohl(vhdr
->verdict
);
645 if ((verdict
& NF_VERDICT_MASK
) > NF_MAX_VERDICT
) {
650 entry
= find_dequeue_entry(queue
, ntohl(vhdr
->id
));
657 if (nfqa
[NFQA_PAYLOAD
]) {
658 if (nfqnl_mangle(nla_data(nfqa
[NFQA_PAYLOAD
]),
659 nla_len(nfqa
[NFQA_PAYLOAD
]), entry
) < 0)
664 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
666 nf_reinject(entry
, verdict
);
675 nfqnl_recv_unsupp(struct sock
*ctnl
, struct sk_buff
*skb
,
676 const struct nlmsghdr
*nlh
,
677 const struct nlattr
* const nfqa
[])
682 static const struct nla_policy nfqa_cfg_policy
[NFQA_CFG_MAX
+1] = {
683 [NFQA_CFG_CMD
] = { .len
= sizeof(struct nfqnl_msg_config_cmd
) },
684 [NFQA_CFG_PARAMS
] = { .len
= sizeof(struct nfqnl_msg_config_params
) },
687 static const struct nf_queue_handler nfqh
= {
689 .outfn
= &nfqnl_enqueue_packet
,
693 nfqnl_recv_config(struct sock
*ctnl
, struct sk_buff
*skb
,
694 const struct nlmsghdr
*nlh
,
695 const struct nlattr
* const nfqa
[])
697 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
698 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
699 struct nfqnl_instance
*queue
;
700 struct nfqnl_msg_config_cmd
*cmd
= NULL
;
703 if (nfqa
[NFQA_CFG_CMD
]) {
704 cmd
= nla_data(nfqa
[NFQA_CFG_CMD
]);
706 /* Commands without queue context - might sleep */
707 switch (cmd
->command
) {
708 case NFQNL_CFG_CMD_PF_BIND
:
709 return nf_register_queue_handler(ntohs(cmd
->pf
),
711 case NFQNL_CFG_CMD_PF_UNBIND
:
712 return nf_unregister_queue_handler(ntohs(cmd
->pf
),
718 queue
= instance_lookup(queue_num
);
719 if (queue
&& queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
725 switch (cmd
->command
) {
726 case NFQNL_CFG_CMD_BIND
:
731 queue
= instance_create(queue_num
, NETLINK_CB(skb
).pid
);
733 ret
= PTR_ERR(queue
);
737 case NFQNL_CFG_CMD_UNBIND
:
742 instance_destroy(queue
);
744 case NFQNL_CFG_CMD_PF_BIND
:
745 case NFQNL_CFG_CMD_PF_UNBIND
:
753 if (nfqa
[NFQA_CFG_PARAMS
]) {
754 struct nfqnl_msg_config_params
*params
;
760 params
= nla_data(nfqa
[NFQA_CFG_PARAMS
]);
761 nfqnl_set_mode(queue
, params
->copy_mode
,
762 ntohl(params
->copy_range
));
765 if (nfqa
[NFQA_CFG_QUEUE_MAXLEN
]) {
766 __be32
*queue_maxlen
;
772 queue_maxlen
= nla_data(nfqa
[NFQA_CFG_QUEUE_MAXLEN
]);
773 spin_lock_bh(&queue
->lock
);
774 queue
->queue_maxlen
= ntohl(*queue_maxlen
);
775 spin_unlock_bh(&queue
->lock
);
783 static const struct nfnl_callback nfqnl_cb
[NFQNL_MSG_MAX
] = {
784 [NFQNL_MSG_PACKET
] = { .call
= nfqnl_recv_unsupp
,
785 .attr_count
= NFQA_MAX
, },
786 [NFQNL_MSG_VERDICT
] = { .call
= nfqnl_recv_verdict
,
787 .attr_count
= NFQA_MAX
,
788 .policy
= nfqa_verdict_policy
},
789 [NFQNL_MSG_CONFIG
] = { .call
= nfqnl_recv_config
,
790 .attr_count
= NFQA_CFG_MAX
,
791 .policy
= nfqa_cfg_policy
},
794 static const struct nfnetlink_subsystem nfqnl_subsys
= {
796 .subsys_id
= NFNL_SUBSYS_QUEUE
,
797 .cb_count
= NFQNL_MSG_MAX
,
801 #ifdef CONFIG_PROC_FS
806 static struct hlist_node
*get_first(struct seq_file
*seq
)
808 struct iter_state
*st
= seq
->private;
813 for (st
->bucket
= 0; st
->bucket
< INSTANCE_BUCKETS
; st
->bucket
++) {
814 if (!hlist_empty(&instance_table
[st
->bucket
]))
815 return instance_table
[st
->bucket
].first
;
820 static struct hlist_node
*get_next(struct seq_file
*seq
, struct hlist_node
*h
)
822 struct iter_state
*st
= seq
->private;
826 if (++st
->bucket
>= INSTANCE_BUCKETS
)
829 h
= instance_table
[st
->bucket
].first
;
834 static struct hlist_node
*get_idx(struct seq_file
*seq
, loff_t pos
)
836 struct hlist_node
*head
;
837 head
= get_first(seq
);
840 while (pos
&& (head
= get_next(seq
, head
)))
842 return pos
? NULL
: head
;
845 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
846 __acquires(instances_lock
)
848 spin_lock(&instances_lock
);
849 return get_idx(seq
, *pos
);
852 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
855 return get_next(s
, v
);
858 static void seq_stop(struct seq_file
*s
, void *v
)
859 __releases(instances_lock
)
861 spin_unlock(&instances_lock
);
864 static int seq_show(struct seq_file
*s
, void *v
)
866 const struct nfqnl_instance
*inst
= v
;
868 return seq_printf(s
, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
870 inst
->peer_pid
, inst
->queue_total
,
871 inst
->copy_mode
, inst
->copy_range
,
872 inst
->queue_dropped
, inst
->queue_user_dropped
,
873 atomic_read(&inst
->id_sequence
), 1);
876 static const struct seq_operations nfqnl_seq_ops
= {
883 static int nfqnl_open(struct inode
*inode
, struct file
*file
)
885 return seq_open_private(file
, &nfqnl_seq_ops
,
886 sizeof(struct iter_state
));
889 static const struct file_operations nfqnl_file_ops
= {
890 .owner
= THIS_MODULE
,
894 .release
= seq_release_private
,
899 static int __init
nfnetlink_queue_init(void)
901 int i
, status
= -ENOMEM
;
903 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
904 INIT_HLIST_HEAD(&instance_table
[i
]);
906 netlink_register_notifier(&nfqnl_rtnl_notifier
);
907 status
= nfnetlink_subsys_register(&nfqnl_subsys
);
909 printk(KERN_ERR
"nf_queue: failed to create netlink socket\n");
910 goto cleanup_netlink_notifier
;
913 #ifdef CONFIG_PROC_FS
914 if (!proc_create("nfnetlink_queue", 0440,
915 proc_net_netfilter
, &nfqnl_file_ops
))
919 register_netdevice_notifier(&nfqnl_dev_notifier
);
922 #ifdef CONFIG_PROC_FS
924 nfnetlink_subsys_unregister(&nfqnl_subsys
);
926 cleanup_netlink_notifier
:
927 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
931 static void __exit
nfnetlink_queue_fini(void)
933 nf_unregister_queue_handlers(&nfqh
);
934 unregister_netdevice_notifier(&nfqnl_dev_notifier
);
935 #ifdef CONFIG_PROC_FS
936 remove_proc_entry("nfnetlink_queue", proc_net_netfilter
);
938 nfnetlink_subsys_unregister(&nfqnl_subsys
);
939 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
941 rcu_barrier(); /* Wait for completion of call_rcu()'s */
944 MODULE_DESCRIPTION("netfilter packet queue handler");
945 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
946 MODULE_LICENSE("GPL");
947 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE
);
949 module_init(nfnetlink_queue_init
);
950 module_exit(nfnetlink_queue_fini
);