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>
38 #include <linux/mutex.h>
40 #include <net/route.h>
42 #define IPQ_QMAX_DEFAULT 1024
43 #define IPQ_PROC_FS_NAME "ip_queue"
44 #define NET_IPQ_QMAX 2088
45 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
47 struct ipq_queue_entry
{
48 struct list_head list
;
53 typedef int (*ipq_cmpfn
)(struct ipq_queue_entry
*, unsigned long);
55 static unsigned char copy_mode __read_mostly
= IPQ_COPY_NONE
;
56 static unsigned int queue_maxlen __read_mostly
= IPQ_QMAX_DEFAULT
;
57 static DEFINE_RWLOCK(queue_lock
);
58 static int peer_pid __read_mostly
;
59 static unsigned int copy_range __read_mostly
;
60 static unsigned int queue_total
;
61 static unsigned int queue_dropped
= 0;
62 static unsigned int queue_user_dropped
= 0;
63 static struct sock
*ipqnl __read_mostly
;
64 static LIST_HEAD(queue_list
);
65 static DEFINE_MUTEX(ipqnl_mutex
);
68 ipq_issue_verdict(struct ipq_queue_entry
*entry
, int verdict
)
70 /* TCP input path (and probably other bits) assume to be called
71 * from softirq context, not from syscall, like ipq_issue_verdict is
72 * called. TCP input path deadlocks with locks taken from timer
73 * softirq, e.g. We therefore emulate this by local_bh_disable() */
76 nf_reinject(entry
->skb
, entry
->info
, verdict
);
83 __ipq_enqueue_entry(struct ipq_queue_entry
*entry
)
85 list_add(&entry
->list
, &queue_list
);
90 * Find and return a queued entry matched by cmpfn, or return the last
91 * entry if cmpfn is NULL.
93 static inline struct ipq_queue_entry
*
94 __ipq_find_entry(ipq_cmpfn cmpfn
, unsigned long data
)
98 list_for_each_prev(p
, &queue_list
) {
99 struct ipq_queue_entry
*entry
= (struct ipq_queue_entry
*)p
;
101 if (!cmpfn
|| cmpfn(entry
, data
))
108 __ipq_dequeue_entry(struct ipq_queue_entry
*entry
)
110 list_del(&entry
->list
);
114 static inline struct ipq_queue_entry
*
115 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
117 struct ipq_queue_entry
*entry
;
119 entry
= __ipq_find_entry(cmpfn
, data
);
123 __ipq_dequeue_entry(entry
);
129 __ipq_flush(int verdict
)
131 struct ipq_queue_entry
*entry
;
133 while ((entry
= __ipq_find_dequeue_entry(NULL
, 0)))
134 ipq_issue_verdict(entry
, verdict
);
138 __ipq_set_mode(unsigned char mode
, unsigned int range
)
149 case IPQ_COPY_PACKET
:
152 if (copy_range
> 0xFFFF)
167 net_disable_timestamp();
168 __ipq_set_mode(IPQ_COPY_NONE
, 0);
169 __ipq_flush(NF_DROP
);
172 static struct ipq_queue_entry
*
173 ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
175 struct ipq_queue_entry
*entry
;
177 write_lock_bh(&queue_lock
);
178 entry
= __ipq_find_dequeue_entry(cmpfn
, data
);
179 write_unlock_bh(&queue_lock
);
184 ipq_flush(int verdict
)
186 write_lock_bh(&queue_lock
);
187 __ipq_flush(verdict
);
188 write_unlock_bh(&queue_lock
);
191 static struct sk_buff
*
192 ipq_build_packet_message(struct ipq_queue_entry
*entry
, int *errp
)
194 unsigned char *old_tail
;
198 struct ipq_packet_msg
*pmsg
;
199 struct nlmsghdr
*nlh
;
201 read_lock_bh(&queue_lock
);
206 size
= NLMSG_SPACE(sizeof(*pmsg
));
210 case IPQ_COPY_PACKET
:
211 if ((entry
->skb
->ip_summed
== CHECKSUM_PARTIAL
||
212 entry
->skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
213 (*errp
= skb_checksum_help(entry
->skb
))) {
214 read_unlock_bh(&queue_lock
);
217 if (copy_range
== 0 || copy_range
> entry
->skb
->len
)
218 data_len
= entry
->skb
->len
;
220 data_len
= copy_range
;
222 size
= NLMSG_SPACE(sizeof(*pmsg
) + data_len
);
227 read_unlock_bh(&queue_lock
);
231 read_unlock_bh(&queue_lock
);
233 skb
= alloc_skb(size
, GFP_ATOMIC
);
238 nlh
= NLMSG_PUT(skb
, 0, 0, IPQM_PACKET
, size
- sizeof(*nlh
));
239 pmsg
= NLMSG_DATA(nlh
);
240 memset(pmsg
, 0, sizeof(*pmsg
));
242 pmsg
->packet_id
= (unsigned long )entry
;
243 pmsg
->data_len
= data_len
;
244 pmsg
->timestamp_sec
= entry
->skb
->tstamp
.off_sec
;
245 pmsg
->timestamp_usec
= entry
->skb
->tstamp
.off_usec
;
246 pmsg
->mark
= entry
->skb
->mark
;
247 pmsg
->hook
= entry
->info
->hook
;
248 pmsg
->hw_protocol
= entry
->skb
->protocol
;
250 if (entry
->info
->indev
)
251 strcpy(pmsg
->indev_name
, entry
->info
->indev
->name
);
253 pmsg
->indev_name
[0] = '\0';
255 if (entry
->info
->outdev
)
256 strcpy(pmsg
->outdev_name
, entry
->info
->outdev
->name
);
258 pmsg
->outdev_name
[0] = '\0';
260 if (entry
->info
->indev
&& entry
->skb
->dev
) {
261 pmsg
->hw_type
= entry
->skb
->dev
->type
;
262 if (entry
->skb
->dev
->hard_header_parse
)
264 entry
->skb
->dev
->hard_header_parse(entry
->skb
,
269 if (skb_copy_bits(entry
->skb
, 0, pmsg
->payload
, data_len
))
272 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
279 printk(KERN_ERR
"ip_queue: error creating packet message\n");
284 ipq_enqueue_packet(struct sk_buff
*skb
, struct nf_info
*info
,
285 unsigned int queuenum
, void *data
)
287 int status
= -EINVAL
;
288 struct sk_buff
*nskb
;
289 struct ipq_queue_entry
*entry
;
291 if (copy_mode
== IPQ_COPY_NONE
)
294 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
296 printk(KERN_ERR
"ip_queue: OOM in ipq_enqueue_packet()\n");
303 nskb
= ipq_build_packet_message(entry
, &status
);
307 write_lock_bh(&queue_lock
);
310 goto err_out_free_nskb
;
312 if (queue_total
>= queue_maxlen
) {
316 printk (KERN_WARNING
"ip_queue: full at %d entries, "
317 "dropping packets(s). Dropped: %d\n", queue_total
,
319 goto err_out_free_nskb
;
322 /* netlink_unicast will either free the nskb or attach it to a socket */
323 status
= netlink_unicast(ipqnl
, nskb
, peer_pid
, MSG_DONTWAIT
);
325 queue_user_dropped
++;
329 __ipq_enqueue_entry(entry
);
331 write_unlock_bh(&queue_lock
);
338 write_unlock_bh(&queue_lock
);
346 ipq_mangle_ipv4(ipq_verdict_msg_t
*v
, struct ipq_queue_entry
*e
)
349 struct iphdr
*user_iph
= (struct iphdr
*)v
->payload
;
351 if (v
->data_len
< sizeof(*user_iph
))
353 diff
= v
->data_len
- e
->skb
->len
;
355 if (pskb_trim(e
->skb
, v
->data_len
))
357 } else if (diff
> 0) {
358 if (v
->data_len
> 0xFFFF)
360 if (diff
> skb_tailroom(e
->skb
)) {
361 struct sk_buff
*newskb
;
363 newskb
= skb_copy_expand(e
->skb
,
364 skb_headroom(e
->skb
),
367 if (newskb
== NULL
) {
368 printk(KERN_WARNING
"ip_queue: OOM "
369 "in mangle, dropping packet\n");
373 skb_set_owner_w(newskb
, e
->skb
->sk
);
377 skb_put(e
->skb
, diff
);
379 if (!skb_make_writable(&e
->skb
, v
->data_len
))
381 memcpy(e
->skb
->data
, v
->payload
, v
->data_len
);
382 e
->skb
->ip_summed
= CHECKSUM_NONE
;
388 id_cmp(struct ipq_queue_entry
*e
, unsigned long id
)
390 return (id
== (unsigned long )e
);
394 ipq_set_verdict(struct ipq_verdict_msg
*vmsg
, unsigned int len
)
396 struct ipq_queue_entry
*entry
;
398 if (vmsg
->value
> NF_MAX_VERDICT
)
401 entry
= ipq_find_dequeue_entry(id_cmp
, vmsg
->id
);
405 int verdict
= vmsg
->value
;
407 if (vmsg
->data_len
&& vmsg
->data_len
== len
)
408 if (ipq_mangle_ipv4(vmsg
, entry
) < 0)
411 ipq_issue_verdict(entry
, verdict
);
417 ipq_set_mode(unsigned char mode
, unsigned int range
)
421 write_lock_bh(&queue_lock
);
422 status
= __ipq_set_mode(mode
, range
);
423 write_unlock_bh(&queue_lock
);
428 ipq_receive_peer(struct ipq_peer_msg
*pmsg
,
429 unsigned char type
, unsigned int len
)
433 if (len
< sizeof(*pmsg
))
438 status
= ipq_set_mode(pmsg
->msg
.mode
.value
,
439 pmsg
->msg
.mode
.range
);
443 if (pmsg
->msg
.verdict
.value
> NF_MAX_VERDICT
)
446 status
= ipq_set_verdict(&pmsg
->msg
.verdict
,
447 len
- sizeof(*pmsg
));
456 dev_cmp(struct ipq_queue_entry
*entry
, unsigned long ifindex
)
458 if (entry
->info
->indev
)
459 if (entry
->info
->indev
->ifindex
== ifindex
)
461 if (entry
->info
->outdev
)
462 if (entry
->info
->outdev
->ifindex
== ifindex
)
464 #ifdef CONFIG_BRIDGE_NETFILTER
465 if (entry
->skb
->nf_bridge
) {
466 if (entry
->skb
->nf_bridge
->physindev
&&
467 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
469 if (entry
->skb
->nf_bridge
->physoutdev
&&
470 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
478 ipq_dev_drop(int ifindex
)
480 struct ipq_queue_entry
*entry
;
482 while ((entry
= ipq_find_dequeue_entry(dev_cmp
, ifindex
)) != NULL
)
483 ipq_issue_verdict(entry
, NF_DROP
);
486 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
489 ipq_rcv_skb(struct sk_buff
*skb
)
491 int status
, type
, pid
, flags
, nlmsglen
, skblen
;
492 struct nlmsghdr
*nlh
;
495 if (skblen
< sizeof(*nlh
))
498 nlh
= (struct nlmsghdr
*)skb
->data
;
499 nlmsglen
= nlh
->nlmsg_len
;
500 if (nlmsglen
< sizeof(*nlh
) || skblen
< nlmsglen
)
503 pid
= nlh
->nlmsg_pid
;
504 flags
= nlh
->nlmsg_flags
;
506 if(pid
<= 0 || !(flags
& NLM_F_REQUEST
) || flags
& NLM_F_MULTI
)
507 RCV_SKB_FAIL(-EINVAL
);
509 if (flags
& MSG_TRUNC
)
510 RCV_SKB_FAIL(-ECOMM
);
512 type
= nlh
->nlmsg_type
;
513 if (type
< NLMSG_NOOP
|| type
>= IPQM_MAX
)
514 RCV_SKB_FAIL(-EINVAL
);
516 if (type
<= IPQM_BASE
)
519 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
520 RCV_SKB_FAIL(-EPERM
);
522 write_lock_bh(&queue_lock
);
525 if (peer_pid
!= pid
) {
526 write_unlock_bh(&queue_lock
);
527 RCV_SKB_FAIL(-EBUSY
);
530 net_enable_timestamp();
534 write_unlock_bh(&queue_lock
);
536 status
= ipq_receive_peer(NLMSG_DATA(nlh
), type
,
537 nlmsglen
- NLMSG_LENGTH(0));
539 RCV_SKB_FAIL(status
);
541 if (flags
& NLM_F_ACK
)
542 netlink_ack(skb
, nlh
, 0);
547 ipq_rcv_sk(struct sock
*sk
, int len
)
552 mutex_lock(&ipqnl_mutex
);
554 for (qlen
= skb_queue_len(&sk
->sk_receive_queue
); qlen
; qlen
--) {
555 skb
= skb_dequeue(&sk
->sk_receive_queue
);
560 mutex_unlock(&ipqnl_mutex
);
564 ipq_rcv_dev_event(struct notifier_block
*this,
565 unsigned long event
, void *ptr
)
567 struct net_device
*dev
= ptr
;
569 /* Drop any packets associated with the downed device */
570 if (event
== NETDEV_DOWN
)
571 ipq_dev_drop(dev
->ifindex
);
575 static struct notifier_block ipq_dev_notifier
= {
576 .notifier_call
= ipq_rcv_dev_event
,
580 ipq_rcv_nl_event(struct notifier_block
*this,
581 unsigned long event
, void *ptr
)
583 struct netlink_notify
*n
= ptr
;
585 if (event
== NETLINK_URELEASE
&&
586 n
->protocol
== NETLINK_FIREWALL
&& n
->pid
) {
587 write_lock_bh(&queue_lock
);
588 if (n
->pid
== peer_pid
)
590 write_unlock_bh(&queue_lock
);
595 static struct notifier_block ipq_nl_notifier
= {
596 .notifier_call
= ipq_rcv_nl_event
,
599 static struct ctl_table_header
*ipq_sysctl_header
;
601 static ctl_table ipq_table
[] = {
603 .ctl_name
= NET_IPQ_QMAX
,
604 .procname
= NET_IPQ_QMAX_NAME
,
605 .data
= &queue_maxlen
,
606 .maxlen
= sizeof(queue_maxlen
),
608 .proc_handler
= proc_dointvec
613 static ctl_table ipq_dir_table
[] = {
615 .ctl_name
= NET_IPV4
,
623 static ctl_table ipq_root_table
[] = {
628 .child
= ipq_dir_table
633 #ifdef CONFIG_PROC_FS
635 ipq_get_info(char *buffer
, char **start
, off_t offset
, int length
)
639 read_lock_bh(&queue_lock
);
641 len
= sprintf(buffer
,
645 "Queue length : %u\n"
646 "Queue max. length : %u\n"
647 "Queue dropped : %u\n"
648 "Netlink dropped : %u\n",
657 read_unlock_bh(&queue_lock
);
659 *start
= buffer
+ offset
;
667 #endif /* CONFIG_PROC_FS */
669 static struct nf_queue_handler nfqh
= {
671 .outfn
= &ipq_enqueue_packet
,
674 static int __init
ip_queue_init(void)
676 int status
= -ENOMEM
;
677 struct proc_dir_entry
*proc
;
679 netlink_register_notifier(&ipq_nl_notifier
);
680 ipqnl
= netlink_kernel_create(NETLINK_FIREWALL
, 0, ipq_rcv_sk
,
683 printk(KERN_ERR
"ip_queue: failed to create netlink socket\n");
684 goto cleanup_netlink_notifier
;
687 proc
= proc_net_create(IPQ_PROC_FS_NAME
, 0, ipq_get_info
);
689 proc
->owner
= THIS_MODULE
;
691 printk(KERN_ERR
"ip_queue: failed to create proc entry\n");
695 register_netdevice_notifier(&ipq_dev_notifier
);
696 ipq_sysctl_header
= register_sysctl_table(ipq_root_table
, 0);
698 status
= nf_register_queue_handler(PF_INET
, &nfqh
);
700 printk(KERN_ERR
"ip_queue: failed to register queue handler\n");
706 unregister_sysctl_table(ipq_sysctl_header
);
707 unregister_netdevice_notifier(&ipq_dev_notifier
);
708 proc_net_remove(IPQ_PROC_FS_NAME
);
711 sock_release(ipqnl
->sk_socket
);
712 mutex_lock(&ipqnl_mutex
);
713 mutex_unlock(&ipqnl_mutex
);
715 cleanup_netlink_notifier
:
716 netlink_unregister_notifier(&ipq_nl_notifier
);
720 static void __exit
ip_queue_fini(void)
722 nf_unregister_queue_handlers(&nfqh
);
726 unregister_sysctl_table(ipq_sysctl_header
);
727 unregister_netdevice_notifier(&ipq_dev_notifier
);
728 proc_net_remove(IPQ_PROC_FS_NAME
);
730 sock_release(ipqnl
->sk_socket
);
731 mutex_lock(&ipqnl_mutex
);
732 mutex_unlock(&ipqnl_mutex
);
734 netlink_unregister_notifier(&ipq_nl_notifier
);
737 MODULE_DESCRIPTION("IPv4 packet queue handler");
738 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
739 MODULE_LICENSE("GPL");
741 module_init(ip_queue_init
);
742 module_exit(ip_queue_fini
);