2 * This is a module which is used for queueing IPv4 packets and
3 * communicating with userspace via netlink.
5 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
6 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
13 * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
14 * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian
16 * 2000-08-01: Added Nick Williams' MAC support.
17 * 2002-06-25: Code cleanup.
18 * 2005-01-10: Added /proc counter for dropped packets; fixed so
19 * packets aren't delivered to user space if they're going
21 * 2005-05-26: local_bh_{disable,enable} around nf_reinject (Harald Welte)
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/init.h>
28 #include <linux/notifier.h>
29 #include <linux/netdevice.h>
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4/ip_queue.h>
32 #include <linux/netfilter_ipv4/ip_tables.h>
33 #include <linux/netlink.h>
34 #include <linux/spinlock.h>
35 #include <linux/sysctl.h>
36 #include <linux/proc_fs.h>
37 #include <linux/security.h>
39 #include <net/route.h>
41 #define IPQ_QMAX_DEFAULT 1024
42 #define IPQ_PROC_FS_NAME "ip_queue"
43 #define NET_IPQ_QMAX 2088
44 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
46 struct ipq_queue_entry
{
47 struct list_head list
;
52 typedef int (*ipq_cmpfn
)(struct ipq_queue_entry
*, unsigned long);
54 static unsigned char copy_mode
= IPQ_COPY_NONE
;
55 static unsigned int queue_maxlen
= IPQ_QMAX_DEFAULT
;
56 static DEFINE_RWLOCK(queue_lock
);
58 static unsigned int copy_range
;
59 static unsigned int queue_total
;
60 static unsigned int queue_dropped
= 0;
61 static unsigned int queue_user_dropped
= 0;
62 static struct sock
*ipqnl
;
63 static LIST_HEAD(queue_list
);
64 static DECLARE_MUTEX(ipqnl_sem
);
67 ipq_issue_verdict(struct ipq_queue_entry
*entry
, int verdict
)
69 /* TCP input path (and probably other bits) assume to be called
70 * from softirq context, not from syscall, like ipq_issue_verdict is
71 * called. TCP input path deadlocks with locks taken from timer
72 * softirq, e.g. We therefore emulate this by local_bh_disable() */
75 nf_reinject(entry
->skb
, entry
->info
, verdict
);
82 __ipq_enqueue_entry(struct ipq_queue_entry
*entry
)
84 list_add(&entry
->list
, &queue_list
);
89 * Find and return a queued entry matched by cmpfn, or return the last
90 * entry if cmpfn is NULL.
92 static inline struct ipq_queue_entry
*
93 __ipq_find_entry(ipq_cmpfn cmpfn
, unsigned long data
)
97 list_for_each_prev(p
, &queue_list
) {
98 struct ipq_queue_entry
*entry
= (struct ipq_queue_entry
*)p
;
100 if (!cmpfn
|| cmpfn(entry
, data
))
107 __ipq_dequeue_entry(struct ipq_queue_entry
*entry
)
109 list_del(&entry
->list
);
113 static inline struct ipq_queue_entry
*
114 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
116 struct ipq_queue_entry
*entry
;
118 entry
= __ipq_find_entry(cmpfn
, data
);
122 __ipq_dequeue_entry(entry
);
128 __ipq_flush(int verdict
)
130 struct ipq_queue_entry
*entry
;
132 while ((entry
= __ipq_find_dequeue_entry(NULL
, 0)))
133 ipq_issue_verdict(entry
, verdict
);
137 __ipq_set_mode(unsigned char mode
, unsigned int range
)
148 case IPQ_COPY_PACKET
:
151 if (copy_range
> 0xFFFF)
166 net_disable_timestamp();
167 __ipq_set_mode(IPQ_COPY_NONE
, 0);
168 __ipq_flush(NF_DROP
);
171 static struct ipq_queue_entry
*
172 ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
174 struct ipq_queue_entry
*entry
;
176 write_lock_bh(&queue_lock
);
177 entry
= __ipq_find_dequeue_entry(cmpfn
, data
);
178 write_unlock_bh(&queue_lock
);
183 ipq_flush(int verdict
)
185 write_lock_bh(&queue_lock
);
186 __ipq_flush(verdict
);
187 write_unlock_bh(&queue_lock
);
190 static struct sk_buff
*
191 ipq_build_packet_message(struct ipq_queue_entry
*entry
, int *errp
)
193 unsigned char *old_tail
;
197 struct ipq_packet_msg
*pmsg
;
198 struct nlmsghdr
*nlh
;
200 read_lock_bh(&queue_lock
);
205 size
= NLMSG_SPACE(sizeof(*pmsg
));
209 case IPQ_COPY_PACKET
:
210 if (entry
->skb
->ip_summed
== CHECKSUM_HW
&&
211 (*errp
= skb_checksum_help(entry
->skb
,
212 entry
->info
->outdev
== NULL
))) {
213 read_unlock_bh(&queue_lock
);
216 if (copy_range
== 0 || copy_range
> entry
->skb
->len
)
217 data_len
= entry
->skb
->len
;
219 data_len
= copy_range
;
221 size
= NLMSG_SPACE(sizeof(*pmsg
) + data_len
);
226 read_unlock_bh(&queue_lock
);
230 read_unlock_bh(&queue_lock
);
232 skb
= alloc_skb(size
, GFP_ATOMIC
);
237 nlh
= NLMSG_PUT(skb
, 0, 0, IPQM_PACKET
, size
- sizeof(*nlh
));
238 pmsg
= NLMSG_DATA(nlh
);
239 memset(pmsg
, 0, sizeof(*pmsg
));
241 pmsg
->packet_id
= (unsigned long )entry
;
242 pmsg
->data_len
= data_len
;
243 pmsg
->timestamp_sec
= entry
->skb
->stamp
.tv_sec
;
244 pmsg
->timestamp_usec
= entry
->skb
->stamp
.tv_usec
;
245 pmsg
->mark
= entry
->skb
->nfmark
;
246 pmsg
->hook
= entry
->info
->hook
;
247 pmsg
->hw_protocol
= entry
->skb
->protocol
;
249 if (entry
->info
->indev
)
250 strcpy(pmsg
->indev_name
, entry
->info
->indev
->name
);
252 pmsg
->indev_name
[0] = '\0';
254 if (entry
->info
->outdev
)
255 strcpy(pmsg
->outdev_name
, entry
->info
->outdev
->name
);
257 pmsg
->outdev_name
[0] = '\0';
259 if (entry
->info
->indev
&& entry
->skb
->dev
) {
260 pmsg
->hw_type
= entry
->skb
->dev
->type
;
261 if (entry
->skb
->dev
->hard_header_parse
)
263 entry
->skb
->dev
->hard_header_parse(entry
->skb
,
268 if (skb_copy_bits(entry
->skb
, 0, pmsg
->payload
, data_len
))
271 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
278 printk(KERN_ERR
"ip_queue: error creating packet message\n");
283 ipq_enqueue_packet(struct sk_buff
*skb
, struct nf_info
*info
,
284 unsigned int queuenum
, void *data
)
286 int status
= -EINVAL
;
287 struct sk_buff
*nskb
;
288 struct ipq_queue_entry
*entry
;
290 if (copy_mode
== IPQ_COPY_NONE
)
293 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
295 printk(KERN_ERR
"ip_queue: OOM in ipq_enqueue_packet()\n");
302 nskb
= ipq_build_packet_message(entry
, &status
);
306 write_lock_bh(&queue_lock
);
309 goto err_out_free_nskb
;
311 if (queue_total
>= queue_maxlen
) {
315 printk (KERN_WARNING
"ip_queue: full at %d entries, "
316 "dropping packets(s). Dropped: %d\n", queue_total
,
318 goto err_out_free_nskb
;
321 /* netlink_unicast will either free the nskb or attach it to a socket */
322 status
= netlink_unicast(ipqnl
, nskb
, peer_pid
, MSG_DONTWAIT
);
324 queue_user_dropped
++;
328 __ipq_enqueue_entry(entry
);
330 write_unlock_bh(&queue_lock
);
337 write_unlock_bh(&queue_lock
);
345 ipq_mangle_ipv4(ipq_verdict_msg_t
*v
, struct ipq_queue_entry
*e
)
348 struct iphdr
*user_iph
= (struct iphdr
*)v
->payload
;
350 if (v
->data_len
< sizeof(*user_iph
))
352 diff
= v
->data_len
- e
->skb
->len
;
354 skb_trim(e
->skb
, v
->data_len
);
356 if (v
->data_len
> 0xFFFF)
358 if (diff
> skb_tailroom(e
->skb
)) {
359 struct sk_buff
*newskb
;
361 newskb
= skb_copy_expand(e
->skb
,
362 skb_headroom(e
->skb
),
365 if (newskb
== NULL
) {
366 printk(KERN_WARNING
"ip_queue: OOM "
367 "in mangle, dropping packet\n");
371 skb_set_owner_w(newskb
, e
->skb
->sk
);
375 skb_put(e
->skb
, diff
);
377 if (!skb_make_writable(&e
->skb
, v
->data_len
))
379 memcpy(e
->skb
->data
, v
->payload
, v
->data_len
);
380 e
->skb
->ip_summed
= CHECKSUM_NONE
;
386 id_cmp(struct ipq_queue_entry
*e
, unsigned long id
)
388 return (id
== (unsigned long )e
);
392 ipq_set_verdict(struct ipq_verdict_msg
*vmsg
, unsigned int len
)
394 struct ipq_queue_entry
*entry
;
396 if (vmsg
->value
> NF_MAX_VERDICT
)
399 entry
= ipq_find_dequeue_entry(id_cmp
, vmsg
->id
);
403 int verdict
= vmsg
->value
;
405 if (vmsg
->data_len
&& vmsg
->data_len
== len
)
406 if (ipq_mangle_ipv4(vmsg
, entry
) < 0)
409 ipq_issue_verdict(entry
, verdict
);
415 ipq_set_mode(unsigned char mode
, unsigned int range
)
419 write_lock_bh(&queue_lock
);
420 status
= __ipq_set_mode(mode
, range
);
421 write_unlock_bh(&queue_lock
);
426 ipq_receive_peer(struct ipq_peer_msg
*pmsg
,
427 unsigned char type
, unsigned int len
)
431 if (len
< sizeof(*pmsg
))
436 status
= ipq_set_mode(pmsg
->msg
.mode
.value
,
437 pmsg
->msg
.mode
.range
);
441 if (pmsg
->msg
.verdict
.value
> NF_MAX_VERDICT
)
444 status
= ipq_set_verdict(&pmsg
->msg
.verdict
,
445 len
- sizeof(*pmsg
));
454 dev_cmp(struct ipq_queue_entry
*entry
, unsigned long ifindex
)
456 if (entry
->info
->indev
)
457 if (entry
->info
->indev
->ifindex
== ifindex
)
460 if (entry
->info
->outdev
)
461 if (entry
->info
->outdev
->ifindex
== ifindex
)
468 ipq_dev_drop(int ifindex
)
470 struct ipq_queue_entry
*entry
;
472 while ((entry
= ipq_find_dequeue_entry(dev_cmp
, ifindex
)) != NULL
)
473 ipq_issue_verdict(entry
, NF_DROP
);
476 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
479 ipq_rcv_skb(struct sk_buff
*skb
)
481 int status
, type
, pid
, flags
, nlmsglen
, skblen
;
482 struct nlmsghdr
*nlh
;
485 if (skblen
< sizeof(*nlh
))
488 nlh
= (struct nlmsghdr
*)skb
->data
;
489 nlmsglen
= nlh
->nlmsg_len
;
490 if (nlmsglen
< sizeof(*nlh
) || skblen
< nlmsglen
)
493 pid
= nlh
->nlmsg_pid
;
494 flags
= nlh
->nlmsg_flags
;
496 if(pid
<= 0 || !(flags
& NLM_F_REQUEST
) || flags
& NLM_F_MULTI
)
497 RCV_SKB_FAIL(-EINVAL
);
499 if (flags
& MSG_TRUNC
)
500 RCV_SKB_FAIL(-ECOMM
);
502 type
= nlh
->nlmsg_type
;
503 if (type
< NLMSG_NOOP
|| type
>= IPQM_MAX
)
504 RCV_SKB_FAIL(-EINVAL
);
506 if (type
<= IPQM_BASE
)
509 if (security_netlink_recv(skb
))
510 RCV_SKB_FAIL(-EPERM
);
512 write_lock_bh(&queue_lock
);
515 if (peer_pid
!= pid
) {
516 write_unlock_bh(&queue_lock
);
517 RCV_SKB_FAIL(-EBUSY
);
520 net_enable_timestamp();
524 write_unlock_bh(&queue_lock
);
526 status
= ipq_receive_peer(NLMSG_DATA(nlh
), type
,
527 skblen
- NLMSG_LENGTH(0));
529 RCV_SKB_FAIL(status
);
531 if (flags
& NLM_F_ACK
)
532 netlink_ack(skb
, nlh
, 0);
537 ipq_rcv_sk(struct sock
*sk
, int len
)
544 for (qlen
= skb_queue_len(&sk
->sk_receive_queue
); qlen
; qlen
--) {
545 skb
= skb_dequeue(&sk
->sk_receive_queue
);
554 ipq_rcv_dev_event(struct notifier_block
*this,
555 unsigned long event
, void *ptr
)
557 struct net_device
*dev
= ptr
;
559 /* Drop any packets associated with the downed device */
560 if (event
== NETDEV_DOWN
)
561 ipq_dev_drop(dev
->ifindex
);
565 static struct notifier_block ipq_dev_notifier
= {
566 .notifier_call
= ipq_rcv_dev_event
,
570 ipq_rcv_nl_event(struct notifier_block
*this,
571 unsigned long event
, void *ptr
)
573 struct netlink_notify
*n
= ptr
;
575 if (event
== NETLINK_URELEASE
&&
576 n
->protocol
== NETLINK_FIREWALL
&& n
->pid
) {
577 write_lock_bh(&queue_lock
);
578 if (n
->pid
== peer_pid
)
580 write_unlock_bh(&queue_lock
);
585 static struct notifier_block ipq_nl_notifier
= {
586 .notifier_call
= ipq_rcv_nl_event
,
589 static struct ctl_table_header
*ipq_sysctl_header
;
591 static ctl_table ipq_table
[] = {
593 .ctl_name
= NET_IPQ_QMAX
,
594 .procname
= NET_IPQ_QMAX_NAME
,
595 .data
= &queue_maxlen
,
596 .maxlen
= sizeof(queue_maxlen
),
598 .proc_handler
= proc_dointvec
603 static ctl_table ipq_dir_table
[] = {
605 .ctl_name
= NET_IPV4
,
613 static ctl_table ipq_root_table
[] = {
618 .child
= ipq_dir_table
623 #ifdef CONFIG_PROC_FS
625 ipq_get_info(char *buffer
, char **start
, off_t offset
, int length
)
629 read_lock_bh(&queue_lock
);
631 len
= sprintf(buffer
,
635 "Queue length : %u\n"
636 "Queue max. length : %u\n"
637 "Queue dropped : %u\n"
638 "Netlink dropped : %u\n",
647 read_unlock_bh(&queue_lock
);
649 *start
= buffer
+ offset
;
657 #endif /* CONFIG_PROC_FS */
660 init_or_cleanup(int init
)
662 int status
= -ENOMEM
;
663 struct proc_dir_entry
*proc
;
668 netlink_register_notifier(&ipq_nl_notifier
);
669 ipqnl
= netlink_kernel_create(NETLINK_FIREWALL
, ipq_rcv_sk
,
672 printk(KERN_ERR
"ip_queue: failed to create netlink socket\n");
673 goto cleanup_netlink_notifier
;
676 proc
= proc_net_create(IPQ_PROC_FS_NAME
, 0, ipq_get_info
);
678 proc
->owner
= THIS_MODULE
;
680 printk(KERN_ERR
"ip_queue: failed to create proc entry\n");
684 register_netdevice_notifier(&ipq_dev_notifier
);
685 ipq_sysctl_header
= register_sysctl_table(ipq_root_table
, 0);
687 status
= nf_register_queue_handler(PF_INET
, ipq_enqueue_packet
, NULL
);
689 printk(KERN_ERR
"ip_queue: failed to register queue handler\n");
695 nf_unregister_queue_handler(PF_INET
);
700 unregister_sysctl_table(ipq_sysctl_header
);
701 unregister_netdevice_notifier(&ipq_dev_notifier
);
702 proc_net_remove(IPQ_PROC_FS_NAME
);
705 sock_release(ipqnl
->sk_socket
);
709 cleanup_netlink_notifier
:
710 netlink_unregister_notifier(&ipq_nl_notifier
);
714 static int __init
init(void)
717 return init_or_cleanup(1);
720 static void __exit
fini(void)
725 MODULE_DESCRIPTION("IPv4 packet queue handler");
726 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
727 MODULE_LICENSE("GPL");