2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/notifier.h>
21 #include <linux/netdevice.h>
22 #include <linux/netfilter.h>
23 #include <linux/proc_fs.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/netfilter/nfnetlink.h>
27 #include <linux/netfilter/nfnetlink_queue.h>
28 #include <linux/list.h>
30 #include <net/netfilter/nf_queue.h>
32 #include <asm/atomic.h>
34 #ifdef CONFIG_BRIDGE_NETFILTER
35 #include "../bridge/br_private.h"
38 #define NFQNL_QMAX_DEFAULT 1024
41 #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
42 __FILE__, __LINE__, __FUNCTION__, \
45 #define QDEBUG(x, ...)
48 struct nfqnl_instance
{
49 struct hlist_node hlist
; /* global list of queues */
53 unsigned int queue_maxlen
;
54 unsigned int copy_range
;
55 unsigned int queue_total
;
56 unsigned int queue_dropped
;
57 unsigned int queue_user_dropped
;
59 unsigned int id_sequence
; /* 'sequence' of pkt ids */
61 u_int16_t queue_num
; /* number of this queue */
66 struct list_head queue_list
; /* packets in queue */
69 typedef int (*nfqnl_cmpfn
)(struct nf_queue_entry
*, unsigned long);
71 static DEFINE_SPINLOCK(instances_lock
);
73 #define INSTANCE_BUCKETS 16
74 static struct hlist_head instance_table
[INSTANCE_BUCKETS
];
76 static inline u_int8_t
instance_hashfn(u_int16_t queue_num
)
78 return ((queue_num
>> 8) | queue_num
) % INSTANCE_BUCKETS
;
81 static struct nfqnl_instance
*
82 instance_lookup(u_int16_t queue_num
)
84 struct hlist_head
*head
;
85 struct hlist_node
*pos
;
86 struct nfqnl_instance
*inst
;
88 head
= &instance_table
[instance_hashfn(queue_num
)];
89 hlist_for_each_entry_rcu(inst
, pos
, head
, hlist
) {
90 if (inst
->queue_num
== queue_num
)
96 static struct nfqnl_instance
*
97 instance_create(u_int16_t queue_num
, int pid
)
99 struct nfqnl_instance
*inst
;
102 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num
, pid
);
104 spin_lock(&instances_lock
);
105 if (instance_lookup(queue_num
)) {
107 QDEBUG("aborting, instance already exists\n");
111 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
115 inst
->queue_num
= queue_num
;
116 inst
->peer_pid
= pid
;
117 inst
->queue_maxlen
= NFQNL_QMAX_DEFAULT
;
118 inst
->copy_range
= 0xfffff;
119 inst
->copy_mode
= NFQNL_COPY_NONE
;
120 spin_lock_init(&inst
->lock
);
121 INIT_LIST_HEAD(&inst
->queue_list
);
122 INIT_RCU_HEAD(&inst
->rcu
);
124 if (!try_module_get(THIS_MODULE
))
127 h
= instance_hashfn(queue_num
);
128 hlist_add_head_rcu(&inst
->hlist
, &instance_table
[h
]);
130 spin_unlock(&instances_lock
);
132 QDEBUG("successfully created new instance\n");
139 spin_unlock(&instances_lock
);
143 static void nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
,
147 instance_destroy_rcu(struct rcu_head
*head
)
149 struct nfqnl_instance
*inst
= container_of(head
, struct nfqnl_instance
,
152 nfqnl_flush(inst
, NULL
, 0);
154 module_put(THIS_MODULE
);
158 __instance_destroy(struct nfqnl_instance
*inst
)
160 hlist_del_rcu(&inst
->hlist
);
161 call_rcu(&inst
->rcu
, instance_destroy_rcu
);
165 instance_destroy(struct nfqnl_instance
*inst
)
167 spin_lock(&instances_lock
);
168 __instance_destroy(inst
);
169 spin_unlock(&instances_lock
);
173 __enqueue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
175 list_add_tail(&entry
->list
, &queue
->queue_list
);
176 queue
->queue_total
++;
179 static struct nf_queue_entry
*
180 find_dequeue_entry(struct nfqnl_instance
*queue
, unsigned int id
)
182 struct nf_queue_entry
*entry
= NULL
, *i
;
184 spin_lock_bh(&queue
->lock
);
186 list_for_each_entry(i
, &queue
->queue_list
, list
) {
194 list_del(&entry
->list
);
195 queue
->queue_total
--;
198 spin_unlock_bh(&queue
->lock
);
204 nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
, unsigned long data
)
206 struct nf_queue_entry
*entry
, *next
;
208 spin_lock_bh(&queue
->lock
);
209 list_for_each_entry_safe(entry
, next
, &queue
->queue_list
, list
) {
210 if (!cmpfn
|| cmpfn(entry
, data
)) {
211 list_del(&entry
->list
);
212 queue
->queue_total
--;
213 nf_reinject(entry
, NF_DROP
);
216 spin_unlock_bh(&queue
->lock
);
219 static struct sk_buff
*
220 nfqnl_build_packet_message(struct nfqnl_instance
*queue
,
221 struct nf_queue_entry
*entry
, int *errp
)
223 sk_buff_data_t old_tail
;
227 struct nfqnl_msg_packet_hdr pmsg
;
228 struct nlmsghdr
*nlh
;
229 struct nfgenmsg
*nfmsg
;
230 struct sk_buff
*entskb
= entry
->skb
;
231 struct net_device
*indev
;
232 struct net_device
*outdev
;
237 size
= NLMSG_ALIGN(sizeof(struct nfgenmsg
))
238 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr
))
239 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
240 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
241 #ifdef CONFIG_BRIDGE_NETFILTER
242 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
243 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
245 + nla_total_size(sizeof(u_int32_t
)) /* mark */
246 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw
))
247 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp
));
249 outdev
= entry
->outdev
;
251 spin_lock_bh(&queue
->lock
);
253 switch (queue
->copy_mode
) {
254 case NFQNL_COPY_META
:
255 case NFQNL_COPY_NONE
:
259 case NFQNL_COPY_PACKET
:
260 if ((entskb
->ip_summed
== CHECKSUM_PARTIAL
||
261 entskb
->ip_summed
== CHECKSUM_COMPLETE
) &&
262 (*errp
= skb_checksum_help(entskb
))) {
263 spin_unlock_bh(&queue
->lock
);
266 if (queue
->copy_range
== 0
267 || queue
->copy_range
> entskb
->len
)
268 data_len
= entskb
->len
;
270 data_len
= queue
->copy_range
;
272 size
+= nla_total_size(data_len
);
277 spin_unlock_bh(&queue
->lock
);
281 entry
->id
= queue
->id_sequence
++;
283 spin_unlock_bh(&queue
->lock
);
285 skb
= alloc_skb(size
, GFP_ATOMIC
);
289 old_tail
= skb
->tail
;
290 nlh
= NLMSG_PUT(skb
, 0, 0,
291 NFNL_SUBSYS_QUEUE
<< 8 | NFQNL_MSG_PACKET
,
292 sizeof(struct nfgenmsg
));
293 nfmsg
= NLMSG_DATA(nlh
);
294 nfmsg
->nfgen_family
= entry
->pf
;
295 nfmsg
->version
= NFNETLINK_V0
;
296 nfmsg
->res_id
= htons(queue
->queue_num
);
298 pmsg
.packet_id
= htonl(entry
->id
);
299 pmsg
.hw_protocol
= entskb
->protocol
;
300 pmsg
.hook
= entry
->hook
;
302 NLA_PUT(skb
, NFQA_PACKET_HDR
, sizeof(pmsg
), &pmsg
);
304 indev
= entry
->indev
;
306 tmp_uint
= htonl(indev
->ifindex
);
307 #ifndef CONFIG_BRIDGE_NETFILTER
308 NLA_PUT(skb
, NFQA_IFINDEX_INDEV
, sizeof(tmp_uint
), &tmp_uint
);
310 if (entry
->pf
== PF_BRIDGE
) {
311 /* Case 1: indev is physical input device, we need to
312 * look for bridge group (when called from
313 * netfilter_bridge) */
314 NLA_PUT(skb
, NFQA_IFINDEX_PHYSINDEV
, sizeof(tmp_uint
),
316 /* this is the bridge group "brX" */
317 tmp_uint
= htonl(indev
->br_port
->br
->dev
->ifindex
);
318 NLA_PUT(skb
, NFQA_IFINDEX_INDEV
, sizeof(tmp_uint
),
321 /* Case 2: indev is bridge group, we need to look for
322 * physical device (when called from ipv4) */
323 NLA_PUT(skb
, NFQA_IFINDEX_INDEV
, sizeof(tmp_uint
),
325 if (entskb
->nf_bridge
326 && entskb
->nf_bridge
->physindev
) {
327 tmp_uint
= htonl(entskb
->nf_bridge
->physindev
->ifindex
);
328 NLA_PUT(skb
, NFQA_IFINDEX_PHYSINDEV
,
329 sizeof(tmp_uint
), &tmp_uint
);
336 tmp_uint
= htonl(outdev
->ifindex
);
337 #ifndef CONFIG_BRIDGE_NETFILTER
338 NLA_PUT(skb
, NFQA_IFINDEX_OUTDEV
, sizeof(tmp_uint
), &tmp_uint
);
340 if (entry
->pf
== PF_BRIDGE
) {
341 /* Case 1: outdev is physical output device, we need to
342 * look for bridge group (when called from
343 * netfilter_bridge) */
344 NLA_PUT(skb
, NFQA_IFINDEX_PHYSOUTDEV
, sizeof(tmp_uint
),
346 /* this is the bridge group "brX" */
347 tmp_uint
= htonl(outdev
->br_port
->br
->dev
->ifindex
);
348 NLA_PUT(skb
, NFQA_IFINDEX_OUTDEV
, sizeof(tmp_uint
),
351 /* Case 2: outdev is bridge group, we need to look for
352 * physical output device (when called from ipv4) */
353 NLA_PUT(skb
, NFQA_IFINDEX_OUTDEV
, sizeof(tmp_uint
),
355 if (entskb
->nf_bridge
356 && entskb
->nf_bridge
->physoutdev
) {
357 tmp_uint
= htonl(entskb
->nf_bridge
->physoutdev
->ifindex
);
358 NLA_PUT(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
359 sizeof(tmp_uint
), &tmp_uint
);
366 tmp_uint
= htonl(entskb
->mark
);
367 NLA_PUT(skb
, NFQA_MARK
, sizeof(u_int32_t
), &tmp_uint
);
370 if (indev
&& entskb
->dev
) {
371 struct nfqnl_msg_packet_hw phw
;
372 int len
= dev_parse_header(entskb
, phw
.hw_addr
);
374 phw
.hw_addrlen
= htons(len
);
375 NLA_PUT(skb
, NFQA_HWADDR
, sizeof(phw
), &phw
);
379 if (entskb
->tstamp
.tv64
) {
380 struct nfqnl_msg_packet_timestamp ts
;
381 struct timeval tv
= ktime_to_timeval(entskb
->tstamp
);
382 ts
.sec
= cpu_to_be64(tv
.tv_sec
);
383 ts
.usec
= cpu_to_be64(tv
.tv_usec
);
385 NLA_PUT(skb
, NFQA_TIMESTAMP
, sizeof(ts
), &ts
);
390 int size
= nla_attr_size(data_len
);
392 if (skb_tailroom(skb
) < nla_total_size(data_len
)) {
393 printk(KERN_WARNING
"nf_queue: no tailroom!\n");
397 nla
= (struct nlattr
*)skb_put(skb
, nla_total_size(data_len
));
398 nla
->nla_type
= NFQA_PAYLOAD
;
401 if (skb_copy_bits(entskb
, 0, nla_data(nla
), data_len
))
405 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
414 printk(KERN_ERR
"nf_queue: error creating packet message\n");
419 nfqnl_enqueue_packet(struct nf_queue_entry
*entry
, unsigned int queuenum
)
421 int status
= -EINVAL
;
422 struct sk_buff
*nskb
;
423 struct nfqnl_instance
*queue
;
427 /* rcu_read_lock()ed by nf_hook_slow() */
428 queue
= instance_lookup(queuenum
);
430 QDEBUG("no queue instance matching\n");
434 if (queue
->copy_mode
== NFQNL_COPY_NONE
) {
435 QDEBUG("mode COPY_NONE, aborting\n");
439 nskb
= nfqnl_build_packet_message(queue
, entry
, &status
);
443 spin_lock_bh(&queue
->lock
);
445 if (!queue
->peer_pid
)
446 goto err_out_free_nskb
;
448 if (queue
->queue_total
>= queue
->queue_maxlen
) {
449 queue
->queue_dropped
++;
452 printk(KERN_WARNING
"nf_queue: full at %d entries, "
453 "dropping packets(s). Dropped: %d\n",
454 queue
->queue_total
, queue
->queue_dropped
);
455 goto err_out_free_nskb
;
458 /* nfnetlink_unicast will either free the nskb or add it to a socket */
459 status
= nfnetlink_unicast(nskb
, queue
->peer_pid
, MSG_DONTWAIT
);
461 queue
->queue_user_dropped
++;
465 __enqueue_entry(queue
, entry
);
467 spin_unlock_bh(&queue
->lock
);
474 spin_unlock_bh(&queue
->lock
);
479 nfqnl_mangle(void *data
, int data_len
, struct nf_queue_entry
*e
)
484 diff
= data_len
- e
->skb
->len
;
486 if (pskb_trim(e
->skb
, data_len
))
488 } else if (diff
> 0) {
489 if (data_len
> 0xFFFF)
491 if (diff
> skb_tailroom(e
->skb
)) {
492 err
= pskb_expand_head(e
->skb
, 0,
493 diff
- skb_tailroom(e
->skb
),
496 printk(KERN_WARNING
"nf_queue: OOM "
497 "in mangle, dropping packet\n");
501 skb_put(e
->skb
, diff
);
503 if (!skb_make_writable(e
->skb
, data_len
))
505 skb_copy_to_linear_data(e
->skb
, data
, data_len
);
506 e
->skb
->ip_summed
= CHECKSUM_NONE
;
511 nfqnl_set_mode(struct nfqnl_instance
*queue
,
512 unsigned char mode
, unsigned int range
)
516 spin_lock_bh(&queue
->lock
);
518 case NFQNL_COPY_NONE
:
519 case NFQNL_COPY_META
:
520 queue
->copy_mode
= mode
;
521 queue
->copy_range
= 0;
524 case NFQNL_COPY_PACKET
:
525 queue
->copy_mode
= mode
;
526 /* we're using struct nlattr which has 16bit nla_len */
528 queue
->copy_range
= 0xffff;
530 queue
->copy_range
= range
;
537 spin_unlock_bh(&queue
->lock
);
543 dev_cmp(struct nf_queue_entry
*entry
, unsigned long ifindex
)
546 if (entry
->indev
->ifindex
== ifindex
)
549 if (entry
->outdev
->ifindex
== ifindex
)
551 #ifdef CONFIG_BRIDGE_NETFILTER
552 if (entry
->skb
->nf_bridge
) {
553 if (entry
->skb
->nf_bridge
->physindev
&&
554 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
556 if (entry
->skb
->nf_bridge
->physoutdev
&&
557 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
564 /* drop all packets with either indev or outdev == ifindex from all queue
567 nfqnl_dev_drop(int ifindex
)
571 QDEBUG("entering for ifindex %u\n", ifindex
);
575 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
576 struct hlist_node
*tmp
;
577 struct nfqnl_instance
*inst
;
578 struct hlist_head
*head
= &instance_table
[i
];
580 hlist_for_each_entry_rcu(inst
, tmp
, head
, hlist
)
581 nfqnl_flush(inst
, dev_cmp
, ifindex
);
587 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
590 nfqnl_rcv_dev_event(struct notifier_block
*this,
591 unsigned long event
, void *ptr
)
593 struct net_device
*dev
= ptr
;
595 if (dev
->nd_net
!= &init_net
)
598 /* Drop any packets associated with the downed device */
599 if (event
== NETDEV_DOWN
)
600 nfqnl_dev_drop(dev
->ifindex
);
604 static struct notifier_block nfqnl_dev_notifier
= {
605 .notifier_call
= nfqnl_rcv_dev_event
,
609 nfqnl_rcv_nl_event(struct notifier_block
*this,
610 unsigned long event
, void *ptr
)
612 struct netlink_notify
*n
= ptr
;
614 if (event
== NETLINK_URELEASE
&&
615 n
->protocol
== NETLINK_NETFILTER
&& n
->pid
) {
618 /* destroy all instances for this pid */
619 spin_lock(&instances_lock
);
620 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
621 struct hlist_node
*tmp
, *t2
;
622 struct nfqnl_instance
*inst
;
623 struct hlist_head
*head
= &instance_table
[i
];
625 hlist_for_each_entry_safe(inst
, tmp
, t2
, head
, hlist
) {
626 if ((n
->net
== &init_net
) &&
627 (n
->pid
== inst
->peer_pid
))
628 __instance_destroy(inst
);
631 spin_unlock(&instances_lock
);
636 static struct notifier_block nfqnl_rtnl_notifier
= {
637 .notifier_call
= nfqnl_rcv_nl_event
,
640 static const struct nla_policy nfqa_verdict_policy
[NFQA_MAX
+1] = {
641 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
642 [NFQA_MARK
] = { .type
= NLA_U32
},
643 [NFQA_PAYLOAD
] = { .type
= NLA_UNSPEC
},
647 nfqnl_recv_verdict(struct sock
*ctnl
, struct sk_buff
*skb
,
648 struct nlmsghdr
*nlh
, struct nlattr
*nfqa
[])
650 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
651 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
653 struct nfqnl_msg_verdict_hdr
*vhdr
;
654 struct nfqnl_instance
*queue
;
655 unsigned int verdict
;
656 struct nf_queue_entry
*entry
;
660 queue
= instance_lookup(queue_num
);
666 if (queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
671 if (!nfqa
[NFQA_VERDICT_HDR
]) {
676 vhdr
= nla_data(nfqa
[NFQA_VERDICT_HDR
]);
677 verdict
= ntohl(vhdr
->verdict
);
679 if ((verdict
& NF_VERDICT_MASK
) > NF_MAX_VERDICT
) {
684 entry
= find_dequeue_entry(queue
, ntohl(vhdr
->id
));
691 if (nfqa
[NFQA_PAYLOAD
]) {
692 if (nfqnl_mangle(nla_data(nfqa
[NFQA_PAYLOAD
]),
693 nla_len(nfqa
[NFQA_PAYLOAD
]), entry
) < 0)
698 entry
->skb
->mark
= ntohl(*(__be32
*)
699 nla_data(nfqa
[NFQA_MARK
]));
701 nf_reinject(entry
, verdict
);
710 nfqnl_recv_unsupp(struct sock
*ctnl
, struct sk_buff
*skb
,
711 struct nlmsghdr
*nlh
, struct nlattr
*nfqa
[])
716 static const struct nla_policy nfqa_cfg_policy
[NFQA_CFG_MAX
+1] = {
717 [NFQA_CFG_CMD
] = { .len
= sizeof(struct nfqnl_msg_config_cmd
) },
718 [NFQA_CFG_PARAMS
] = { .len
= sizeof(struct nfqnl_msg_config_params
) },
721 static const struct nf_queue_handler nfqh
= {
723 .outfn
= &nfqnl_enqueue_packet
,
727 nfqnl_recv_config(struct sock
*ctnl
, struct sk_buff
*skb
,
728 struct nlmsghdr
*nlh
, struct nlattr
*nfqa
[])
730 struct nfgenmsg
*nfmsg
= NLMSG_DATA(nlh
);
731 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
732 struct nfqnl_instance
*queue
;
733 struct nfqnl_msg_config_cmd
*cmd
= NULL
;
736 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh
->nlmsg_type
));
738 if (nfqa
[NFQA_CFG_CMD
]) {
739 cmd
= nla_data(nfqa
[NFQA_CFG_CMD
]);
741 /* Commands without queue context - might sleep */
742 switch (cmd
->command
) {
743 case NFQNL_CFG_CMD_PF_BIND
:
744 ret
= nf_register_queue_handler(ntohs(cmd
->pf
),
747 case NFQNL_CFG_CMD_PF_UNBIND
:
748 ret
= nf_unregister_queue_handler(ntohs(cmd
->pf
),
760 queue
= instance_lookup(queue_num
);
761 if (queue
&& queue
->peer_pid
!= NETLINK_CB(skb
).pid
) {
767 switch (cmd
->command
) {
768 case NFQNL_CFG_CMD_BIND
:
773 queue
= instance_create(queue_num
, NETLINK_CB(skb
).pid
);
779 case NFQNL_CFG_CMD_UNBIND
:
784 instance_destroy(queue
);
786 case NFQNL_CFG_CMD_PF_BIND
:
787 case NFQNL_CFG_CMD_PF_UNBIND
:
795 if (nfqa
[NFQA_CFG_PARAMS
]) {
796 struct nfqnl_msg_config_params
*params
;
802 params
= nla_data(nfqa
[NFQA_CFG_PARAMS
]);
803 nfqnl_set_mode(queue
, params
->copy_mode
,
804 ntohl(params
->copy_range
));
807 if (nfqa
[NFQA_CFG_QUEUE_MAXLEN
]) {
808 __be32
*queue_maxlen
;
814 queue_maxlen
= nla_data(nfqa
[NFQA_CFG_QUEUE_MAXLEN
]);
815 spin_lock_bh(&queue
->lock
);
816 queue
->queue_maxlen
= ntohl(*queue_maxlen
);
817 spin_unlock_bh(&queue
->lock
);
825 static const struct nfnl_callback nfqnl_cb
[NFQNL_MSG_MAX
] = {
826 [NFQNL_MSG_PACKET
] = { .call
= nfqnl_recv_unsupp
,
827 .attr_count
= NFQA_MAX
, },
828 [NFQNL_MSG_VERDICT
] = { .call
= nfqnl_recv_verdict
,
829 .attr_count
= NFQA_MAX
,
830 .policy
= nfqa_verdict_policy
},
831 [NFQNL_MSG_CONFIG
] = { .call
= nfqnl_recv_config
,
832 .attr_count
= NFQA_CFG_MAX
,
833 .policy
= nfqa_cfg_policy
},
836 static const struct nfnetlink_subsystem nfqnl_subsys
= {
838 .subsys_id
= NFNL_SUBSYS_QUEUE
,
839 .cb_count
= NFQNL_MSG_MAX
,
843 #ifdef CONFIG_PROC_FS
848 static struct hlist_node
*get_first(struct seq_file
*seq
)
850 struct iter_state
*st
= seq
->private;
855 for (st
->bucket
= 0; st
->bucket
< INSTANCE_BUCKETS
; st
->bucket
++) {
856 if (!hlist_empty(&instance_table
[st
->bucket
]))
857 return instance_table
[st
->bucket
].first
;
862 static struct hlist_node
*get_next(struct seq_file
*seq
, struct hlist_node
*h
)
864 struct iter_state
*st
= seq
->private;
868 if (++st
->bucket
>= INSTANCE_BUCKETS
)
871 h
= instance_table
[st
->bucket
].first
;
876 static struct hlist_node
*get_idx(struct seq_file
*seq
, loff_t pos
)
878 struct hlist_node
*head
;
879 head
= get_first(seq
);
882 while (pos
&& (head
= get_next(seq
, head
)))
884 return pos
? NULL
: head
;
887 static void *seq_start(struct seq_file
*seq
, loff_t
*pos
)
889 spin_lock(&instances_lock
);
890 return get_idx(seq
, *pos
);
893 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
896 return get_next(s
, v
);
899 static void seq_stop(struct seq_file
*s
, void *v
)
901 spin_unlock(&instances_lock
);
904 static int seq_show(struct seq_file
*s
, void *v
)
906 const struct nfqnl_instance
*inst
= v
;
908 return seq_printf(s
, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
910 inst
->peer_pid
, inst
->queue_total
,
911 inst
->copy_mode
, inst
->copy_range
,
912 inst
->queue_dropped
, inst
->queue_user_dropped
,
913 inst
->id_sequence
, 1);
916 static const struct seq_operations nfqnl_seq_ops
= {
923 static int nfqnl_open(struct inode
*inode
, struct file
*file
)
925 return seq_open_private(file
, &nfqnl_seq_ops
,
926 sizeof(struct iter_state
));
929 static const struct file_operations nfqnl_file_ops
= {
930 .owner
= THIS_MODULE
,
934 .release
= seq_release_private
,
939 static int __init
nfnetlink_queue_init(void)
941 int i
, status
= -ENOMEM
;
942 #ifdef CONFIG_PROC_FS
943 struct proc_dir_entry
*proc_nfqueue
;
946 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
947 INIT_HLIST_HEAD(&instance_table
[i
]);
949 netlink_register_notifier(&nfqnl_rtnl_notifier
);
950 status
= nfnetlink_subsys_register(&nfqnl_subsys
);
952 printk(KERN_ERR
"nf_queue: failed to create netlink socket\n");
953 goto cleanup_netlink_notifier
;
956 #ifdef CONFIG_PROC_FS
957 proc_nfqueue
= create_proc_entry("nfnetlink_queue", 0440,
961 proc_nfqueue
->proc_fops
= &nfqnl_file_ops
;
964 register_netdevice_notifier(&nfqnl_dev_notifier
);
967 #ifdef CONFIG_PROC_FS
969 nfnetlink_subsys_unregister(&nfqnl_subsys
);
971 cleanup_netlink_notifier
:
972 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
976 static void __exit
nfnetlink_queue_fini(void)
978 nf_unregister_queue_handlers(&nfqh
);
979 unregister_netdevice_notifier(&nfqnl_dev_notifier
);
980 #ifdef CONFIG_PROC_FS
981 remove_proc_entry("nfnetlink_queue", proc_net_netfilter
);
983 nfnetlink_subsys_unregister(&nfqnl_subsys
);
984 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
987 MODULE_DESCRIPTION("netfilter packet queue handler");
988 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
989 MODULE_LICENSE("GPL");
990 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE
);
992 module_init(nfnetlink_queue_init
);
993 module_exit(nfnetlink_queue_fini
);