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 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_queue.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netlink.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/security.h>
26 #include <linux/mutex.h>
28 #include <net/route.h>
30 #define IPQ_QMAX_DEFAULT 1024
31 #define IPQ_PROC_FS_NAME "ip_queue"
32 #define NET_IPQ_QMAX 2088
33 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
35 struct ipq_queue_entry
{
36 struct list_head list
;
41 typedef int (*ipq_cmpfn
)(struct ipq_queue_entry
*, unsigned long);
43 static unsigned char copy_mode __read_mostly
= IPQ_COPY_NONE
;
44 static unsigned int queue_maxlen __read_mostly
= IPQ_QMAX_DEFAULT
;
45 static DEFINE_RWLOCK(queue_lock
);
46 static int peer_pid __read_mostly
;
47 static unsigned int copy_range __read_mostly
;
48 static unsigned int queue_total
;
49 static unsigned int queue_dropped
= 0;
50 static unsigned int queue_user_dropped
= 0;
51 static struct sock
*ipqnl __read_mostly
;
52 static LIST_HEAD(queue_list
);
53 static DEFINE_MUTEX(ipqnl_mutex
);
56 ipq_issue_verdict(struct ipq_queue_entry
*entry
, int verdict
)
58 /* TCP input path (and probably other bits) assume to be called
59 * from softirq context, not from syscall, like ipq_issue_verdict is
60 * called. TCP input path deadlocks with locks taken from timer
61 * softirq, e.g. We therefore emulate this by local_bh_disable() */
64 nf_reinject(entry
->skb
, entry
->info
, verdict
);
71 __ipq_enqueue_entry(struct ipq_queue_entry
*entry
)
73 list_add(&entry
->list
, &queue_list
);
78 * Find and return a queued entry matched by cmpfn, or return the last
79 * entry if cmpfn is NULL.
81 static inline struct ipq_queue_entry
*
82 __ipq_find_entry(ipq_cmpfn cmpfn
, unsigned long data
)
86 list_for_each_prev(p
, &queue_list
) {
87 struct ipq_queue_entry
*entry
= (struct ipq_queue_entry
*)p
;
89 if (!cmpfn
|| cmpfn(entry
, data
))
96 __ipq_dequeue_entry(struct ipq_queue_entry
*entry
)
98 list_del(&entry
->list
);
102 static inline struct ipq_queue_entry
*
103 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
105 struct ipq_queue_entry
*entry
;
107 entry
= __ipq_find_entry(cmpfn
, data
);
111 __ipq_dequeue_entry(entry
);
117 __ipq_flush(int verdict
)
119 struct ipq_queue_entry
*entry
;
121 while ((entry
= __ipq_find_dequeue_entry(NULL
, 0)))
122 ipq_issue_verdict(entry
, verdict
);
126 __ipq_set_mode(unsigned char mode
, unsigned int range
)
137 case IPQ_COPY_PACKET
:
140 if (copy_range
> 0xFFFF)
155 net_disable_timestamp();
156 __ipq_set_mode(IPQ_COPY_NONE
, 0);
157 __ipq_flush(NF_DROP
);
160 static struct ipq_queue_entry
*
161 ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
163 struct ipq_queue_entry
*entry
;
165 write_lock_bh(&queue_lock
);
166 entry
= __ipq_find_dequeue_entry(cmpfn
, data
);
167 write_unlock_bh(&queue_lock
);
172 ipq_flush(int verdict
)
174 write_lock_bh(&queue_lock
);
175 __ipq_flush(verdict
);
176 write_unlock_bh(&queue_lock
);
179 static struct sk_buff
*
180 ipq_build_packet_message(struct ipq_queue_entry
*entry
, int *errp
)
182 sk_buff_data_t old_tail
;
186 struct ipq_packet_msg
*pmsg
;
187 struct nlmsghdr
*nlh
;
190 read_lock_bh(&queue_lock
);
195 size
= NLMSG_SPACE(sizeof(*pmsg
));
199 case IPQ_COPY_PACKET
:
200 if ((entry
->skb
->ip_summed
== CHECKSUM_PARTIAL
||
201 entry
->skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
202 (*errp
= skb_checksum_help(entry
->skb
))) {
203 read_unlock_bh(&queue_lock
);
206 if (copy_range
== 0 || copy_range
> entry
->skb
->len
)
207 data_len
= entry
->skb
->len
;
209 data_len
= copy_range
;
211 size
= NLMSG_SPACE(sizeof(*pmsg
) + data_len
);
216 read_unlock_bh(&queue_lock
);
220 read_unlock_bh(&queue_lock
);
222 skb
= alloc_skb(size
, GFP_ATOMIC
);
226 old_tail
= skb
->tail
;
227 nlh
= NLMSG_PUT(skb
, 0, 0, IPQM_PACKET
, size
- sizeof(*nlh
));
228 pmsg
= NLMSG_DATA(nlh
);
229 memset(pmsg
, 0, sizeof(*pmsg
));
231 pmsg
->packet_id
= (unsigned long )entry
;
232 pmsg
->data_len
= data_len
;
233 tv
= ktime_to_timeval(entry
->skb
->tstamp
);
234 pmsg
->timestamp_sec
= tv
.tv_sec
;
235 pmsg
->timestamp_usec
= tv
.tv_usec
;
236 pmsg
->mark
= entry
->skb
->mark
;
237 pmsg
->hook
= entry
->info
->hook
;
238 pmsg
->hw_protocol
= entry
->skb
->protocol
;
240 if (entry
->info
->indev
)
241 strcpy(pmsg
->indev_name
, entry
->info
->indev
->name
);
243 pmsg
->indev_name
[0] = '\0';
245 if (entry
->info
->outdev
)
246 strcpy(pmsg
->outdev_name
, entry
->info
->outdev
->name
);
248 pmsg
->outdev_name
[0] = '\0';
250 if (entry
->info
->indev
&& entry
->skb
->dev
) {
251 pmsg
->hw_type
= entry
->skb
->dev
->type
;
252 if (entry
->skb
->dev
->hard_header_parse
)
254 entry
->skb
->dev
->hard_header_parse(entry
->skb
,
259 if (skb_copy_bits(entry
->skb
, 0, pmsg
->payload
, data_len
))
262 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
269 printk(KERN_ERR
"ip_queue: error creating packet message\n");
274 ipq_enqueue_packet(struct sk_buff
*skb
, struct nf_info
*info
,
275 unsigned int queuenum
, void *data
)
277 int status
= -EINVAL
;
278 struct sk_buff
*nskb
;
279 struct ipq_queue_entry
*entry
;
281 if (copy_mode
== IPQ_COPY_NONE
)
284 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
286 printk(KERN_ERR
"ip_queue: OOM in ipq_enqueue_packet()\n");
293 nskb
= ipq_build_packet_message(entry
, &status
);
297 write_lock_bh(&queue_lock
);
300 goto err_out_free_nskb
;
302 if (queue_total
>= queue_maxlen
) {
306 printk (KERN_WARNING
"ip_queue: full at %d entries, "
307 "dropping packets(s). Dropped: %d\n", queue_total
,
309 goto err_out_free_nskb
;
312 /* netlink_unicast will either free the nskb or attach it to a socket */
313 status
= netlink_unicast(ipqnl
, nskb
, peer_pid
, MSG_DONTWAIT
);
315 queue_user_dropped
++;
319 __ipq_enqueue_entry(entry
);
321 write_unlock_bh(&queue_lock
);
328 write_unlock_bh(&queue_lock
);
336 ipq_mangle_ipv4(ipq_verdict_msg_t
*v
, struct ipq_queue_entry
*e
)
340 struct iphdr
*user_iph
= (struct iphdr
*)v
->payload
;
342 if (v
->data_len
< sizeof(*user_iph
))
344 diff
= v
->data_len
- e
->skb
->len
;
346 if (pskb_trim(e
->skb
, v
->data_len
))
348 } else if (diff
> 0) {
349 if (v
->data_len
> 0xFFFF)
351 if (diff
> skb_tailroom(e
->skb
)) {
352 err
= pskb_expand_head(e
->skb
, 0,
353 diff
- skb_tailroom(e
->skb
),
356 printk(KERN_WARNING
"ip_queue: error "
357 "in mangle, dropping packet: %d\n", -err
);
361 skb_put(e
->skb
, diff
);
363 if (!skb_make_writable(e
->skb
, v
->data_len
))
365 skb_copy_to_linear_data(e
->skb
, v
->payload
, v
->data_len
);
366 e
->skb
->ip_summed
= CHECKSUM_NONE
;
372 id_cmp(struct ipq_queue_entry
*e
, unsigned long id
)
374 return (id
== (unsigned long )e
);
378 ipq_set_verdict(struct ipq_verdict_msg
*vmsg
, unsigned int len
)
380 struct ipq_queue_entry
*entry
;
382 if (vmsg
->value
> NF_MAX_VERDICT
)
385 entry
= ipq_find_dequeue_entry(id_cmp
, vmsg
->id
);
389 int verdict
= vmsg
->value
;
391 if (vmsg
->data_len
&& vmsg
->data_len
== len
)
392 if (ipq_mangle_ipv4(vmsg
, entry
) < 0)
395 ipq_issue_verdict(entry
, verdict
);
401 ipq_set_mode(unsigned char mode
, unsigned int range
)
405 write_lock_bh(&queue_lock
);
406 status
= __ipq_set_mode(mode
, range
);
407 write_unlock_bh(&queue_lock
);
412 ipq_receive_peer(struct ipq_peer_msg
*pmsg
,
413 unsigned char type
, unsigned int len
)
417 if (len
< sizeof(*pmsg
))
422 status
= ipq_set_mode(pmsg
->msg
.mode
.value
,
423 pmsg
->msg
.mode
.range
);
427 if (pmsg
->msg
.verdict
.value
> NF_MAX_VERDICT
)
430 status
= ipq_set_verdict(&pmsg
->msg
.verdict
,
431 len
- sizeof(*pmsg
));
440 dev_cmp(struct ipq_queue_entry
*entry
, unsigned long ifindex
)
442 if (entry
->info
->indev
)
443 if (entry
->info
->indev
->ifindex
== ifindex
)
445 if (entry
->info
->outdev
)
446 if (entry
->info
->outdev
->ifindex
== ifindex
)
448 #ifdef CONFIG_BRIDGE_NETFILTER
449 if (entry
->skb
->nf_bridge
) {
450 if (entry
->skb
->nf_bridge
->physindev
&&
451 entry
->skb
->nf_bridge
->physindev
->ifindex
== ifindex
)
453 if (entry
->skb
->nf_bridge
->physoutdev
&&
454 entry
->skb
->nf_bridge
->physoutdev
->ifindex
== ifindex
)
462 ipq_dev_drop(int ifindex
)
464 struct ipq_queue_entry
*entry
;
466 while ((entry
= ipq_find_dequeue_entry(dev_cmp
, ifindex
)) != NULL
)
467 ipq_issue_verdict(entry
, NF_DROP
);
470 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
473 __ipq_rcv_skb(struct sk_buff
*skb
)
475 int status
, type
, pid
, flags
, nlmsglen
, skblen
;
476 struct nlmsghdr
*nlh
;
479 if (skblen
< sizeof(*nlh
))
482 nlh
= nlmsg_hdr(skb
);
483 nlmsglen
= nlh
->nlmsg_len
;
484 if (nlmsglen
< sizeof(*nlh
) || skblen
< nlmsglen
)
487 pid
= nlh
->nlmsg_pid
;
488 flags
= nlh
->nlmsg_flags
;
490 if(pid
<= 0 || !(flags
& NLM_F_REQUEST
) || flags
& NLM_F_MULTI
)
491 RCV_SKB_FAIL(-EINVAL
);
493 if (flags
& MSG_TRUNC
)
494 RCV_SKB_FAIL(-ECOMM
);
496 type
= nlh
->nlmsg_type
;
497 if (type
< NLMSG_NOOP
|| type
>= IPQM_MAX
)
498 RCV_SKB_FAIL(-EINVAL
);
500 if (type
<= IPQM_BASE
)
503 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
504 RCV_SKB_FAIL(-EPERM
);
506 write_lock_bh(&queue_lock
);
509 if (peer_pid
!= pid
) {
510 write_unlock_bh(&queue_lock
);
511 RCV_SKB_FAIL(-EBUSY
);
514 net_enable_timestamp();
518 write_unlock_bh(&queue_lock
);
520 status
= ipq_receive_peer(NLMSG_DATA(nlh
), type
,
521 nlmsglen
- NLMSG_LENGTH(0));
523 RCV_SKB_FAIL(status
);
525 if (flags
& NLM_F_ACK
)
526 netlink_ack(skb
, nlh
, 0);
531 ipq_rcv_skb(struct sk_buff
*skb
)
533 mutex_lock(&ipqnl_mutex
);
535 mutex_unlock(&ipqnl_mutex
);
539 ipq_rcv_dev_event(struct notifier_block
*this,
540 unsigned long event
, void *ptr
)
542 struct net_device
*dev
= ptr
;
544 /* Drop any packets associated with the downed device */
545 if (event
== NETDEV_DOWN
)
546 ipq_dev_drop(dev
->ifindex
);
550 static struct notifier_block ipq_dev_notifier
= {
551 .notifier_call
= ipq_rcv_dev_event
,
555 ipq_rcv_nl_event(struct notifier_block
*this,
556 unsigned long event
, void *ptr
)
558 struct netlink_notify
*n
= ptr
;
560 if (event
== NETLINK_URELEASE
&&
561 n
->protocol
== NETLINK_FIREWALL
&& n
->pid
) {
562 write_lock_bh(&queue_lock
);
563 if (n
->pid
== peer_pid
)
565 write_unlock_bh(&queue_lock
);
570 static struct notifier_block ipq_nl_notifier
= {
571 .notifier_call
= ipq_rcv_nl_event
,
574 static struct ctl_table_header
*ipq_sysctl_header
;
576 static ctl_table ipq_table
[] = {
578 .ctl_name
= NET_IPQ_QMAX
,
579 .procname
= NET_IPQ_QMAX_NAME
,
580 .data
= &queue_maxlen
,
581 .maxlen
= sizeof(queue_maxlen
),
583 .proc_handler
= proc_dointvec
588 static ctl_table ipq_dir_table
[] = {
590 .ctl_name
= NET_IPV4
,
598 static ctl_table ipq_root_table
[] = {
603 .child
= ipq_dir_table
608 #ifdef CONFIG_PROC_FS
610 ipq_get_info(char *buffer
, char **start
, off_t offset
, int length
)
614 read_lock_bh(&queue_lock
);
616 len
= sprintf(buffer
,
620 "Queue length : %u\n"
621 "Queue max. length : %u\n"
622 "Queue dropped : %u\n"
623 "Netlink dropped : %u\n",
632 read_unlock_bh(&queue_lock
);
634 *start
= buffer
+ offset
;
642 #endif /* CONFIG_PROC_FS */
644 static struct nf_queue_handler nfqh
= {
646 .outfn
= &ipq_enqueue_packet
,
649 static int __init
ip_queue_init(void)
651 int status
= -ENOMEM
;
652 struct proc_dir_entry
*proc
;
654 netlink_register_notifier(&ipq_nl_notifier
);
655 ipqnl
= netlink_kernel_create(NETLINK_FIREWALL
, 0, ipq_rcv_skb
,
658 printk(KERN_ERR
"ip_queue: failed to create netlink socket\n");
659 goto cleanup_netlink_notifier
;
662 proc
= proc_net_create(IPQ_PROC_FS_NAME
, 0, ipq_get_info
);
664 proc
->owner
= THIS_MODULE
;
666 printk(KERN_ERR
"ip_queue: failed to create proc entry\n");
670 register_netdevice_notifier(&ipq_dev_notifier
);
671 ipq_sysctl_header
= register_sysctl_table(ipq_root_table
);
673 status
= nf_register_queue_handler(PF_INET
, &nfqh
);
675 printk(KERN_ERR
"ip_queue: failed to register queue handler\n");
681 unregister_sysctl_table(ipq_sysctl_header
);
682 unregister_netdevice_notifier(&ipq_dev_notifier
);
683 proc_net_remove(IPQ_PROC_FS_NAME
);
686 sock_release(ipqnl
->sk_socket
);
687 mutex_lock(&ipqnl_mutex
);
688 mutex_unlock(&ipqnl_mutex
);
690 cleanup_netlink_notifier
:
691 netlink_unregister_notifier(&ipq_nl_notifier
);
695 static void __exit
ip_queue_fini(void)
697 nf_unregister_queue_handlers(&nfqh
);
701 unregister_sysctl_table(ipq_sysctl_header
);
702 unregister_netdevice_notifier(&ipq_dev_notifier
);
703 proc_net_remove(IPQ_PROC_FS_NAME
);
705 sock_release(ipqnl
->sk_socket
);
706 mutex_lock(&ipqnl_mutex
);
707 mutex_unlock(&ipqnl_mutex
);
709 netlink_unregister_notifier(&ipq_nl_notifier
);
712 MODULE_DESCRIPTION("IPv4 packet queue handler");
713 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
714 MODULE_LICENSE("GPL");
716 module_init(ip_queue_init
);
717 module_exit(ip_queue_fini
);