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_total
;
50 unsigned int queue_dropped
;
51 unsigned int queue_user_dropped
;
53 unsigned int id_sequence
; /* 'sequence' of pkt ids */
55 u_int16_t queue_num
; /* number of this queue */
60 struct list_head queue_list
; /* packets in queue */
63 typedef int (*nfqnl_cmpfn
)(struct nf_queue_entry
*, unsigned long);
65 static DEFINE_SPINLOCK(instances_lock
);
67 #define INSTANCE_BUCKETS 16
68 static struct hlist_head instance_table
[INSTANCE_BUCKETS
] __read_mostly
;
70 static inline u_int8_t
instance_hashfn(u_int16_t queue_num
)
72 return ((queue_num
>> 8) | queue_num
) % INSTANCE_BUCKETS
;
75 static struct nfqnl_instance
*
76 instance_lookup(u_int16_t queue_num
)
78 struct hlist_head
*head
;
79 struct hlist_node
*pos
;
80 struct nfqnl_instance
*inst
;
82 head
= &instance_table
[instance_hashfn(queue_num
)];
83 hlist_for_each_entry_rcu(inst
, pos
, head
, hlist
) {
84 if (inst
->queue_num
== queue_num
)
90 static struct nfqnl_instance
*
91 instance_create(u_int16_t queue_num
, int pid
)
93 struct nfqnl_instance
*inst
;
97 spin_lock(&instances_lock
);
98 if (instance_lookup(queue_num
)) {
103 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
109 inst
->queue_num
= queue_num
;
110 inst
->peer_pid
= pid
;
111 inst
->queue_maxlen
= NFQNL_QMAX_DEFAULT
;
112 inst
->copy_range
= 0xfffff;
113 inst
->copy_mode
= NFQNL_COPY_NONE
;
114 spin_lock_init(&inst
->lock
);
115 INIT_LIST_HEAD(&inst
->queue_list
);
117 if (!try_module_get(THIS_MODULE
)) {
122 h
= instance_hashfn(queue_num
);
123 hlist_add_head_rcu(&inst
->hlist
, &instance_table
[h
]);
125 spin_unlock(&instances_lock
);
132 spin_unlock(&instances_lock
);
136 static void nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
,
140 instance_destroy_rcu(struct rcu_head
*head
)
142 struct nfqnl_instance
*inst
= container_of(head
, struct nfqnl_instance
,
145 nfqnl_flush(inst
, NULL
, 0);
147 module_put(THIS_MODULE
);
151 __instance_destroy(struct nfqnl_instance
*inst
)
153 hlist_del_rcu(&inst
->hlist
);
154 call_rcu(&inst
->rcu
, instance_destroy_rcu
);
158 instance_destroy(struct nfqnl_instance
*inst
)
160 spin_lock(&instances_lock
);
161 __instance_destroy(inst
);
162 spin_unlock(&instances_lock
);
166 __enqueue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
168 list_add_tail(&entry
->list
, &queue
->queue_list
);
169 queue
->queue_total
++;
172 static struct nf_queue_entry
*
173 find_dequeue_entry(struct nfqnl_instance
*queue
, unsigned int id
)
175 struct nf_queue_entry
*entry
= NULL
, *i
;
177 spin_lock_bh(&queue
->lock
);
179 list_for_each_entry(i
, &queue
->queue_list
, list
) {
187 list_del(&entry
->list
);
188 queue
->queue_total
--;
191 spin_unlock_bh(&queue
->lock
);
197 nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
, unsigned long data
)
199 struct nf_queue_entry
*entry
, *next
;
201 spin_lock_bh(&queue
->lock
);
202 list_for_each_entry_safe(entry
, next
, &queue
->queue_list
, list
) {
203 if (!cmpfn
|| cmpfn(entry
, data
)) {
204 list_del(&entry
->list
);
205 queue
->queue_total
--;
206 nf_reinject(entry
, NF_DROP
);
209 spin_unlock_bh(&queue
->lock
);
212 static struct sk_buff
*
213 nfqnl_build_packet_message(struct nfqnl_instance
*queue
,
214 struct nf_queue_entry
*entry
)
216 sk_buff_data_t old_tail
;
220 struct nfqnl_msg_packet_hdr pmsg
;
221 struct nlmsghdr
*nlh
;
222 struct nfgenmsg
*nfmsg
;
223 struct sk_buff
*entskb
= entry
->skb
;
224 struct net_device
*indev
;
225 struct net_device
*outdev
;
227 size
= NLMSG_SPACE(sizeof(struct nfgenmsg
))
228 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr
))
229 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
230 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
231 #ifdef CONFIG_BRIDGE_NETFILTER
232 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
233 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
235 + nla_total_size(sizeof(u_int32_t
)) /* mark */
236 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw
))
237 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp
));
239 outdev
= entry
->outdev
;
241 spin_lock_bh(&queue
->lock
);
243 switch ((enum nfqnl_config_mode
)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 entskb
->ip_summed
== CHECKSUM_COMPLETE
) &&
251 skb_checksum_help(entskb
)) {
252 spin_unlock_bh(&queue
->lock
);
255 if (queue
->copy_range
== 0
256 || queue
->copy_range
> entskb
->len
)
257 data_len
= entskb
->len
;
259 data_len
= queue
->copy_range
;
261 size
+= nla_total_size(data_len
);
265 entry
->id
= queue
->id_sequence
++;
267 spin_unlock_bh(&queue
->lock
);
269 skb
= alloc_skb(size
, GFP_ATOMIC
);
273 old_tail
= skb
->tail
;
274 nlh
= NLMSG_PUT(skb
, 0, 0,
275 NFNL_SUBSYS_QUEUE
<< 8 | NFQNL_MSG_PACKET
,
276 sizeof(struct nfgenmsg
));
277 nfmsg
= NLMSG_DATA(nlh
);
278 nfmsg
->nfgen_family
= entry
->pf
;
279 nfmsg
->version
= NFNETLINK_V0
;
280 nfmsg
->res_id
= htons(queue
->queue_num
);
282 pmsg
.packet_id
= htonl(entry
->id
);
283 pmsg
.hw_protocol
= entskb
->protocol
;
284 pmsg
.hook
= entry
->hook
;
286 NLA_PUT(skb
, NFQA_PACKET_HDR
, sizeof(pmsg
), &pmsg
);
288 indev
= entry
->indev
;
290 #ifndef CONFIG_BRIDGE_NETFILTER
291 NLA_PUT_BE32(skb
, NFQA_IFINDEX_INDEV
, htonl(indev
->ifindex
));
293 if (entry
->pf
== PF_BRIDGE
) {
294 /* Case 1: indev is physical input device, we need to
295 * look for bridge group (when called from
296 * netfilter_bridge) */
297 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSINDEV
,
298 htonl(indev
->ifindex
));
299 /* this is the bridge group "brX" */
300 NLA_PUT_BE32(skb
, NFQA_IFINDEX_INDEV
,
301 htonl(indev
->br_port
->br
->dev
->ifindex
));
303 /* Case 2: indev is bridge group, we need to look for
304 * physical device (when called from ipv4) */
305 NLA_PUT_BE32(skb
, NFQA_IFINDEX_INDEV
,
306 htonl(indev
->ifindex
));
307 if (entskb
->nf_bridge
&& entskb
->nf_bridge
->physindev
)
308 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSINDEV
,
309 htonl(entskb
->nf_bridge
->physindev
->ifindex
));
315 #ifndef CONFIG_BRIDGE_NETFILTER
316 NLA_PUT_BE32(skb
, NFQA_IFINDEX_OUTDEV
, htonl(outdev
->ifindex
));
318 if (entry
->pf
== PF_BRIDGE
) {
319 /* Case 1: outdev is physical output device, we need to
320 * look for bridge group (when called from
321 * netfilter_bridge) */
322 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
323 htonl(outdev
->ifindex
));
324 /* this is the bridge group "brX" */
325 NLA_PUT_BE32(skb
, NFQA_IFINDEX_OUTDEV
,
326 htonl(outdev
->br_port
->br
->dev
->ifindex
));
328 /* Case 2: outdev is bridge group, we need to look for
329 * physical output device (when called from ipv4) */
330 NLA_PUT_BE32(skb
, NFQA_IFINDEX_OUTDEV
,
331 htonl(outdev
->ifindex
));
332 if (entskb
->nf_bridge
&& entskb
->nf_bridge
->physoutdev
)
333 NLA_PUT_BE32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
334 htonl(entskb
->nf_bridge
->physoutdev
->ifindex
));
340 NLA_PUT_BE32(skb
, NFQA_MARK
, htonl(entskb
->mark
));
342 if (indev
&& entskb
->dev
) {
343 struct nfqnl_msg_packet_hw phw
;
344 int len
= dev_parse_header(entskb
, phw
.hw_addr
);
346 phw
.hw_addrlen
= htons(len
);
347 NLA_PUT(skb
, NFQA_HWADDR
, sizeof(phw
), &phw
);
351 if (entskb
->tstamp
.tv64
) {
352 struct nfqnl_msg_packet_timestamp ts
;
353 struct timeval tv
= ktime_to_timeval(entskb
->tstamp
);
354 ts
.sec
= cpu_to_be64(tv
.tv_sec
);
355 ts
.usec
= cpu_to_be64(tv
.tv_usec
);
357 NLA_PUT(skb
, NFQA_TIMESTAMP
, sizeof(ts
), &ts
);
362 int sz
= nla_attr_size(data_len
);
364 if (skb_tailroom(skb
) < nla_total_size(data_len
)) {
365 printk(KERN_WARNING
"nf_queue: no tailroom!\n");
369 nla
= (struct nlattr
*)skb_put(skb
, nla_total_size(data_len
));
370 nla
->nla_type
= NFQA_PAYLOAD
;
373 if (skb_copy_bits(entskb
, 0, nla_data(nla
), data_len
))
377 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
385 printk(KERN_ERR
"nf_queue: error creating packet message\n");
390 nfqnl_enqueue_packet(struct nf_queue_entry
*entry
, unsigned int queuenum
)
392 struct sk_buff
*nskb
;
393 struct nfqnl_instance
*queue
;
396 /* rcu_read_lock()ed by nf_hook_slow() */
397 queue
= instance_lookup(queuenum
);
401 if (queue
->copy_mode
== NFQNL_COPY_NONE
)
404 nskb
= nfqnl_build_packet_message(queue
, entry
);
408 spin_lock_bh(&queue
->lock
);
410 if (!queue
->peer_pid
)
411 goto err_out_free_nskb
;
413 if (queue
->queue_total
>= queue
->queue_maxlen
) {
414 queue
->queue_dropped
++;
416 printk(KERN_WARNING
"nf_queue: full at %d entries, "
417 "dropping packets(s).\n",
419 goto err_out_free_nskb
;
422 /* nfnetlink_unicast will either free the nskb or add it to a socket */
423 err
= nfnetlink_unicast(nskb
, &init_net
, queue
->peer_pid
, MSG_DONTWAIT
);
425 queue
->queue_user_dropped
++;
429 __enqueue_entry(queue
, entry
);
431 spin_unlock_bh(&queue
->lock
);
437 spin_unlock_bh(&queue
->lock
);
443 nfqnl_mangle(void *data
, int data_len
, struct nf_queue_entry
*e
)
445 struct sk_buff
*nskb
;
448 diff
= data_len
- e
->skb
->len
;
450 if (pskb_trim(e
->skb
, data_len
))
452 } else if (diff
> 0) {
453 if (data_len
> 0xFFFF)
455 if (diff
> skb_tailroom(e
->skb
)) {
456 nskb
= skb_copy_expand(e
->skb
, skb_headroom(e
->skb
),
459 printk(KERN_WARNING
"nf_queue: OOM "
460 "in mangle, dropping packet\n");
466 skb_put(e
->skb
, diff
);
468 if (!skb_make_writable(e
->skb
, data_len
))
470 skb_copy_to_linear_data(e
->skb
, data
, data_len
);
471 e
->skb
->ip_summed
= CHECKSUM_NONE
;
476 nfqnl_set_mode(struct nfqnl_instance
*queue
,
477 unsigned char mode
, unsigned int range
)
481 spin_lock_bh(&queue
->lock
);
483 case NFQNL_COPY_NONE
:
484 case NFQNL_COPY_META
:
485 queue
->copy_mode
= mode
;
486 queue
->copy_range
= 0;
489 case NFQNL_COPY_PACKET
:
490 queue
->copy_mode
= mode
;
491 /* we're using struct nlattr which has 16bit nla_len */
493 queue
->copy_range
= 0xffff;
495 queue
->copy_range
= range
;
502 spin_unlock_bh(&queue
->lock
);
508 dev_cmp(struct nf_queue_entry
*entry
, unsigned long ifindex
)
511 if (entry
->indev
->ifindex
== ifindex
)
514 if (entry
->outdev
->ifindex
== ifindex
)
516 #ifdef CONFIG_BRIDGE_NETFILTER
517 if (entry
->skb
->nf_bridge
) {
518 if (entry
->skb
->nf_bridge
->physindev
&&
519 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
521 if (entry
->skb
->nf_bridge
->physoutdev
&&
522 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
529 /* drop all packets with either indev or outdev == ifindex from all queue
532 nfqnl_dev_drop(int ifindex
)
538 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
539 struct hlist_node
*tmp
;
540 struct nfqnl_instance
*inst
;
541 struct hlist_head
*head
= &instance_table
[i
];
543 hlist_for_each_entry_rcu(inst
, tmp
, head
, hlist
)
544 nfqnl_flush(inst
, dev_cmp
, ifindex
);
550 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
553 nfqnl_rcv_dev_event(struct notifier_block
*this,
554 unsigned long event
, void *ptr
)
556 struct net_device
*dev
= ptr
;
558 if (!net_eq(dev_net(dev
), &init_net
))
561 /* Drop any packets associated with the downed device */
562 if (event
== NETDEV_DOWN
)
563 nfqnl_dev_drop(dev
->ifindex
);
567 static struct notifier_block nfqnl_dev_notifier
= {
568 .notifier_call
= nfqnl_rcv_dev_event
,
572 nfqnl_rcv_nl_event(struct notifier_block
*this,
573 unsigned long event
, void *ptr
)
575 struct netlink_notify
*n
= ptr
;
577 if (event
== NETLINK_URELEASE
&& n
->protocol
== NETLINK_NETFILTER
) {
580 /* destroy all instances for this pid */
581 spin_lock(&instances_lock
);
582 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
583 struct hlist_node
*tmp
, *t2
;
584 struct nfqnl_instance
*inst
;
585 struct hlist_head
*head
= &instance_table
[i
];
587 hlist_for_each_entry_safe(inst
, tmp
, t2
, head
, hlist
) {
588 if ((n
->net
== &init_net
) &&
589 (n
->pid
== inst
->peer_pid
))
590 __instance_destroy(inst
);
593 spin_unlock(&instances_lock
);
598 static struct notifier_block nfqnl_rtnl_notifier
= {
599 .notifier_call
= nfqnl_rcv_nl_event
,
602 static const struct nla_policy nfqa_verdict_policy
[NFQA_MAX
+1] = {
603 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
604 [NFQA_MARK
] = { .type
= NLA_U32
},
605 [NFQA_PAYLOAD
] = { .type
= NLA_UNSPEC
},
609 nfqnl_recv_verdict(struct sock
*ctnl
, struct sk_buff
*skb
,
610 const struct nlmsghdr
*nlh
,
611 const struct nlattr
* const nfqa
[])
613 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
614 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
616 struct nfqnl_msg_verdict_hdr
*vhdr
;
617 struct nfqnl_instance
*queue
;
618 unsigned int verdict
;
619 struct nf_queue_entry
*entry
;
623 queue
= instance_lookup(queue_num
);
629 if (queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
634 if (!nfqa
[NFQA_VERDICT_HDR
]) {
639 vhdr
= nla_data(nfqa
[NFQA_VERDICT_HDR
]);
640 verdict
= ntohl(vhdr
->verdict
);
642 if ((verdict
& NF_VERDICT_MASK
) > NF_MAX_VERDICT
) {
647 entry
= find_dequeue_entry(queue
, ntohl(vhdr
->id
));
654 if (nfqa
[NFQA_PAYLOAD
]) {
655 if (nfqnl_mangle(nla_data(nfqa
[NFQA_PAYLOAD
]),
656 nla_len(nfqa
[NFQA_PAYLOAD
]), entry
) < 0)
661 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
663 nf_reinject(entry
, verdict
);
672 nfqnl_recv_unsupp(struct sock
*ctnl
, struct sk_buff
*skb
,
673 const struct nlmsghdr
*nlh
,
674 const struct nlattr
* const nfqa
[])
679 static const struct nla_policy nfqa_cfg_policy
[NFQA_CFG_MAX
+1] = {
680 [NFQA_CFG_CMD
] = { .len
= sizeof(struct nfqnl_msg_config_cmd
) },
681 [NFQA_CFG_PARAMS
] = { .len
= sizeof(struct nfqnl_msg_config_params
) },
684 static const struct nf_queue_handler nfqh
= {
686 .outfn
= &nfqnl_enqueue_packet
,
690 nfqnl_recv_config(struct sock
*ctnl
, struct sk_buff
*skb
,
691 const struct nlmsghdr
*nlh
,
692 const struct nlattr
* const nfqa
[])
694 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
695 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
696 struct nfqnl_instance
*queue
;
697 struct nfqnl_msg_config_cmd
*cmd
= NULL
;
700 if (nfqa
[NFQA_CFG_CMD
]) {
701 cmd
= nla_data(nfqa
[NFQA_CFG_CMD
]);
703 /* Commands without queue context - might sleep */
704 switch (cmd
->command
) {
705 case NFQNL_CFG_CMD_PF_BIND
:
706 return nf_register_queue_handler(ntohs(cmd
->pf
),
708 case NFQNL_CFG_CMD_PF_UNBIND
:
709 return nf_unregister_queue_handler(ntohs(cmd
->pf
),
715 queue
= instance_lookup(queue_num
);
716 if (queue
&& queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
722 switch (cmd
->command
) {
723 case NFQNL_CFG_CMD_BIND
:
728 queue
= instance_create(queue_num
, NETLINK_CB(skb
).pid
);
730 ret
= PTR_ERR(queue
);
734 case NFQNL_CFG_CMD_UNBIND
:
739 instance_destroy(queue
);
741 case NFQNL_CFG_CMD_PF_BIND
:
742 case NFQNL_CFG_CMD_PF_UNBIND
:
750 if (nfqa
[NFQA_CFG_PARAMS
]) {
751 struct nfqnl_msg_config_params
*params
;
757 params
= nla_data(nfqa
[NFQA_CFG_PARAMS
]);
758 nfqnl_set_mode(queue
, params
->copy_mode
,
759 ntohl(params
->copy_range
));
762 if (nfqa
[NFQA_CFG_QUEUE_MAXLEN
]) {
763 __be32
*queue_maxlen
;
769 queue_maxlen
= nla_data(nfqa
[NFQA_CFG_QUEUE_MAXLEN
]);
770 spin_lock_bh(&queue
->lock
);
771 queue
->queue_maxlen
= ntohl(*queue_maxlen
);
772 spin_unlock_bh(&queue
->lock
);
780 static const struct nfnl_callback nfqnl_cb
[NFQNL_MSG_MAX
] = {
781 [NFQNL_MSG_PACKET
] = { .call
= nfqnl_recv_unsupp
,
782 .attr_count
= NFQA_MAX
, },
783 [NFQNL_MSG_VERDICT
] = { .call
= nfqnl_recv_verdict
,
784 .attr_count
= NFQA_MAX
,
785 .policy
= nfqa_verdict_policy
},
786 [NFQNL_MSG_CONFIG
] = { .call
= nfqnl_recv_config
,
787 .attr_count
= NFQA_CFG_MAX
,
788 .policy
= nfqa_cfg_policy
},
791 static const struct nfnetlink_subsystem nfqnl_subsys
= {
793 .subsys_id
= NFNL_SUBSYS_QUEUE
,
794 .cb_count
= NFQNL_MSG_MAX
,
798 #ifdef CONFIG_PROC_FS
803 static struct hlist_node
*get_first(struct seq_file
*seq
)
805 struct iter_state
*st
= seq
->private;
810 for (st
->bucket
= 0; st
->bucket
< INSTANCE_BUCKETS
; st
->bucket
++) {
811 if (!hlist_empty(&instance_table
[st
->bucket
]))
812 return instance_table
[st
->bucket
].first
;
817 static struct hlist_node
*get_next(struct seq_file
*seq
, struct hlist_node
*h
)
819 struct iter_state
*st
= seq
->private;
823 if (++st
->bucket
>= INSTANCE_BUCKETS
)
826 h
= instance_table
[st
->bucket
].first
;
831 static struct hlist_node
*get_idx(struct seq_file
*seq
, loff_t pos
)
833 struct hlist_node
*head
;
834 head
= get_first(seq
);
837 while (pos
&& (head
= get_next(seq
, head
)))
839 return pos
? NULL
: head
;
842 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
843 __acquires(instances_lock
)
845 spin_lock(&instances_lock
);
846 return get_idx(seq
, *pos
);
849 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
852 return get_next(s
, v
);
855 static void seq_stop(struct seq_file
*s
, void *v
)
856 __releases(instances_lock
)
858 spin_unlock(&instances_lock
);
861 static int seq_show(struct seq_file
*s
, void *v
)
863 const struct nfqnl_instance
*inst
= v
;
865 return seq_printf(s
, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
867 inst
->peer_pid
, inst
->queue_total
,
868 inst
->copy_mode
, inst
->copy_range
,
869 inst
->queue_dropped
, inst
->queue_user_dropped
,
870 inst
->id_sequence
, 1);
873 static const struct seq_operations nfqnl_seq_ops
= {
880 static int nfqnl_open(struct inode
*inode
, struct file
*file
)
882 return seq_open_private(file
, &nfqnl_seq_ops
,
883 sizeof(struct iter_state
));
886 static const struct file_operations nfqnl_file_ops
= {
887 .owner
= THIS_MODULE
,
891 .release
= seq_release_private
,
896 static int __init
nfnetlink_queue_init(void)
898 int i
, status
= -ENOMEM
;
900 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
901 INIT_HLIST_HEAD(&instance_table
[i
]);
903 netlink_register_notifier(&nfqnl_rtnl_notifier
);
904 status
= nfnetlink_subsys_register(&nfqnl_subsys
);
906 printk(KERN_ERR
"nf_queue: failed to create netlink socket\n");
907 goto cleanup_netlink_notifier
;
910 #ifdef CONFIG_PROC_FS
911 if (!proc_create("nfnetlink_queue", 0440,
912 proc_net_netfilter
, &nfqnl_file_ops
))
916 register_netdevice_notifier(&nfqnl_dev_notifier
);
919 #ifdef CONFIG_PROC_FS
921 nfnetlink_subsys_unregister(&nfqnl_subsys
);
923 cleanup_netlink_notifier
:
924 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
928 static void __exit
nfnetlink_queue_fini(void)
930 nf_unregister_queue_handlers(&nfqh
);
931 unregister_netdevice_notifier(&nfqnl_dev_notifier
);
932 #ifdef CONFIG_PROC_FS
933 remove_proc_entry("nfnetlink_queue", proc_net_netfilter
);
935 nfnetlink_subsys_unregister(&nfqnl_subsys
);
936 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
938 rcu_barrier(); /* Wait for completion of call_rcu()'s */
941 MODULE_DESCRIPTION("netfilter packet queue handler");
942 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
943 MODULE_LICENSE("GPL");
944 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE
);
946 module_init(nfnetlink_queue_init
);
947 module_exit(nfnetlink_queue_fini
);