2 * This is a module which is used for queueing IPv6 packets and
3 * communicating with userspace via netlink.
5 * (C) 2001 Fernando Anton, this code is GPL.
6 * IPv64 Project - Work based in IPv64 draft by Arturo Azcorra.
7 * Universidad Carlos III de Madrid - Leganes (Madrid) - Spain
8 * Universidad Politecnica de Alcala de Henares - Alcala de H. (Madrid) - Spain
9 * email: fanton@it.uc3m.es
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.
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/init.h>
18 #include <linux/ipv6.h>
19 #include <linux/notifier.h>
20 #include <linux/netdevice.h>
21 #include <linux/netfilter.h>
22 #include <linux/netlink.h>
23 #include <linux/spinlock.h>
24 #include <linux/sysctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/mutex.h>
28 #include <net/net_namespace.h>
31 #include <net/ip6_route.h>
32 #include <net/netfilter/nf_queue.h>
33 #include <linux/netfilter_ipv4/ip_queue.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
37 #define IPQ_QMAX_DEFAULT 1024
38 #define IPQ_PROC_FS_NAME "ip6_queue"
39 #define NET_IPQ_QMAX 2088
40 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
42 struct ipq_queue_entry
{
43 struct list_head list
;
48 typedef int (*ipq_cmpfn
)(struct ipq_queue_entry
*, unsigned long);
50 static unsigned char copy_mode __read_mostly
= IPQ_COPY_NONE
;
51 static unsigned int queue_maxlen __read_mostly
= IPQ_QMAX_DEFAULT
;
52 static DEFINE_RWLOCK(queue_lock
);
53 static int peer_pid __read_mostly
;
54 static unsigned int copy_range __read_mostly
;
55 static unsigned int queue_total
;
56 static unsigned int queue_dropped
= 0;
57 static unsigned int queue_user_dropped
= 0;
58 static struct sock
*ipqnl __read_mostly
;
59 static LIST_HEAD(queue_list
);
60 static DEFINE_MUTEX(ipqnl_mutex
);
63 ipq_issue_verdict(struct ipq_queue_entry
*entry
, int verdict
)
66 nf_reinject(entry
->skb
, entry
->info
, verdict
);
72 __ipq_enqueue_entry(struct ipq_queue_entry
*entry
)
74 list_add_tail(&entry
->list
, &queue_list
);
79 * Find and return a queued entry matched by cmpfn, or return the last
80 * entry if cmpfn is NULL.
82 static inline struct ipq_queue_entry
*
83 __ipq_find_entry(ipq_cmpfn cmpfn
, unsigned long data
)
85 struct ipq_queue_entry
*entry
;
87 list_for_each_entry(entry
, &queue_list
, list
) {
88 if (!cmpfn
|| cmpfn(entry
, data
))
95 __ipq_dequeue_entry(struct ipq_queue_entry
*entry
)
97 list_del(&entry
->list
);
101 static inline struct ipq_queue_entry
*
102 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
104 struct ipq_queue_entry
*entry
;
106 entry
= __ipq_find_entry(cmpfn
, data
);
110 __ipq_dequeue_entry(entry
);
116 __ipq_flush(int verdict
)
118 struct ipq_queue_entry
*entry
;
120 while ((entry
= __ipq_find_dequeue_entry(NULL
, 0)))
121 ipq_issue_verdict(entry
, verdict
);
125 __ipq_set_mode(unsigned char mode
, unsigned int range
)
136 case IPQ_COPY_PACKET
:
139 if (copy_range
> 0xFFFF)
154 net_disable_timestamp();
155 __ipq_set_mode(IPQ_COPY_NONE
, 0);
156 __ipq_flush(NF_DROP
);
159 static struct ipq_queue_entry
*
160 ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
162 struct ipq_queue_entry
*entry
;
164 write_lock_bh(&queue_lock
);
165 entry
= __ipq_find_dequeue_entry(cmpfn
, data
);
166 write_unlock_bh(&queue_lock
);
171 ipq_flush(int verdict
)
173 write_lock_bh(&queue_lock
);
174 __ipq_flush(verdict
);
175 write_unlock_bh(&queue_lock
);
178 static struct sk_buff
*
179 ipq_build_packet_message(struct ipq_queue_entry
*entry
, int *errp
)
181 sk_buff_data_t old_tail
;
185 struct ipq_packet_msg
*pmsg
;
186 struct nlmsghdr
*nlh
;
189 read_lock_bh(&queue_lock
);
194 size
= NLMSG_SPACE(sizeof(*pmsg
));
198 case IPQ_COPY_PACKET
:
199 if ((entry
->skb
->ip_summed
== CHECKSUM_PARTIAL
||
200 entry
->skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
201 (*errp
= skb_checksum_help(entry
->skb
))) {
202 read_unlock_bh(&queue_lock
);
205 if (copy_range
== 0 || copy_range
> entry
->skb
->len
)
206 data_len
= entry
->skb
->len
;
208 data_len
= copy_range
;
210 size
= NLMSG_SPACE(sizeof(*pmsg
) + data_len
);
215 read_unlock_bh(&queue_lock
);
219 read_unlock_bh(&queue_lock
);
221 skb
= alloc_skb(size
, GFP_ATOMIC
);
225 old_tail
= skb
->tail
;
226 nlh
= NLMSG_PUT(skb
, 0, 0, IPQM_PACKET
, size
- sizeof(*nlh
));
227 pmsg
= NLMSG_DATA(nlh
);
228 memset(pmsg
, 0, sizeof(*pmsg
));
230 pmsg
->packet_id
= (unsigned long )entry
;
231 pmsg
->data_len
= data_len
;
232 tv
= ktime_to_timeval(entry
->skb
->tstamp
);
233 pmsg
->timestamp_sec
= tv
.tv_sec
;
234 pmsg
->timestamp_usec
= tv
.tv_usec
;
235 pmsg
->mark
= entry
->skb
->mark
;
236 pmsg
->hook
= entry
->info
->hook
;
237 pmsg
->hw_protocol
= entry
->skb
->protocol
;
239 if (entry
->info
->indev
)
240 strcpy(pmsg
->indev_name
, entry
->info
->indev
->name
);
242 pmsg
->indev_name
[0] = '\0';
244 if (entry
->info
->outdev
)
245 strcpy(pmsg
->outdev_name
, entry
->info
->outdev
->name
);
247 pmsg
->outdev_name
[0] = '\0';
249 if (entry
->info
->indev
&& entry
->skb
->dev
) {
250 pmsg
->hw_type
= entry
->skb
->dev
->type
;
251 pmsg
->hw_addrlen
= dev_parse_header(entry
->skb
, pmsg
->hw_addr
);
255 if (skb_copy_bits(entry
->skb
, 0, pmsg
->payload
, data_len
))
258 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
265 printk(KERN_ERR
"ip6_queue: error creating packet message\n");
270 ipq_enqueue_packet(struct sk_buff
*skb
, struct nf_info
*info
,
271 unsigned int queuenum
)
273 int status
= -EINVAL
;
274 struct sk_buff
*nskb
;
275 struct ipq_queue_entry
*entry
;
277 if (copy_mode
== IPQ_COPY_NONE
)
280 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
282 printk(KERN_ERR
"ip6_queue: OOM in ipq_enqueue_packet()\n");
289 nskb
= ipq_build_packet_message(entry
, &status
);
293 write_lock_bh(&queue_lock
);
296 goto err_out_free_nskb
;
298 if (queue_total
>= queue_maxlen
) {
302 printk (KERN_WARNING
"ip6_queue: fill at %d entries, "
303 "dropping packet(s). Dropped: %d\n", queue_total
,
305 goto err_out_free_nskb
;
308 /* netlink_unicast will either free the nskb or attach it to a socket */
309 status
= netlink_unicast(ipqnl
, nskb
, peer_pid
, MSG_DONTWAIT
);
311 queue_user_dropped
++;
315 __ipq_enqueue_entry(entry
);
317 write_unlock_bh(&queue_lock
);
324 write_unlock_bh(&queue_lock
);
332 ipq_mangle_ipv6(ipq_verdict_msg_t
*v
, struct ipq_queue_entry
*e
)
336 struct ipv6hdr
*user_iph
= (struct ipv6hdr
*)v
->payload
;
338 if (v
->data_len
< sizeof(*user_iph
))
340 diff
= v
->data_len
- e
->skb
->len
;
342 if (pskb_trim(e
->skb
, v
->data_len
))
344 } else if (diff
> 0) {
345 if (v
->data_len
> 0xFFFF)
347 if (diff
> skb_tailroom(e
->skb
)) {
348 err
= pskb_expand_head(e
->skb
, 0,
349 diff
- skb_tailroom(e
->skb
),
352 printk(KERN_WARNING
"ip6_queue: OOM "
353 "in mangle, dropping packet\n");
357 skb_put(e
->skb
, diff
);
359 if (!skb_make_writable(e
->skb
, v
->data_len
))
361 skb_copy_to_linear_data(e
->skb
, v
->payload
, v
->data_len
);
362 e
->skb
->ip_summed
= CHECKSUM_NONE
;
368 id_cmp(struct ipq_queue_entry
*e
, unsigned long id
)
370 return (id
== (unsigned long )e
);
374 ipq_set_verdict(struct ipq_verdict_msg
*vmsg
, unsigned int len
)
376 struct ipq_queue_entry
*entry
;
378 if (vmsg
->value
> NF_MAX_VERDICT
)
381 entry
= ipq_find_dequeue_entry(id_cmp
, vmsg
->id
);
385 int verdict
= vmsg
->value
;
387 if (vmsg
->data_len
&& vmsg
->data_len
== len
)
388 if (ipq_mangle_ipv6(vmsg
, entry
) < 0)
391 ipq_issue_verdict(entry
, verdict
);
397 ipq_set_mode(unsigned char mode
, unsigned int range
)
401 write_lock_bh(&queue_lock
);
402 status
= __ipq_set_mode(mode
, range
);
403 write_unlock_bh(&queue_lock
);
408 ipq_receive_peer(struct ipq_peer_msg
*pmsg
,
409 unsigned char type
, unsigned int len
)
413 if (len
< sizeof(*pmsg
))
418 status
= ipq_set_mode(pmsg
->msg
.mode
.value
,
419 pmsg
->msg
.mode
.range
);
423 if (pmsg
->msg
.verdict
.value
> NF_MAX_VERDICT
)
426 status
= ipq_set_verdict(&pmsg
->msg
.verdict
,
427 len
- sizeof(*pmsg
));
436 dev_cmp(struct ipq_queue_entry
*entry
, unsigned long ifindex
)
438 if (entry
->info
->indev
)
439 if (entry
->info
->indev
->ifindex
== ifindex
)
442 if (entry
->info
->outdev
)
443 if (entry
->info
->outdev
->ifindex
== ifindex
)
450 ipq_dev_drop(int ifindex
)
452 struct ipq_queue_entry
*entry
;
454 while ((entry
= ipq_find_dequeue_entry(dev_cmp
, ifindex
)) != NULL
)
455 ipq_issue_verdict(entry
, NF_DROP
);
458 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
461 __ipq_rcv_skb(struct sk_buff
*skb
)
463 int status
, type
, pid
, flags
, nlmsglen
, skblen
;
464 struct nlmsghdr
*nlh
;
467 if (skblen
< sizeof(*nlh
))
470 nlh
= nlmsg_hdr(skb
);
471 nlmsglen
= nlh
->nlmsg_len
;
472 if (nlmsglen
< sizeof(*nlh
) || skblen
< nlmsglen
)
475 pid
= nlh
->nlmsg_pid
;
476 flags
= nlh
->nlmsg_flags
;
478 if(pid
<= 0 || !(flags
& NLM_F_REQUEST
) || flags
& NLM_F_MULTI
)
479 RCV_SKB_FAIL(-EINVAL
);
481 if (flags
& MSG_TRUNC
)
482 RCV_SKB_FAIL(-ECOMM
);
484 type
= nlh
->nlmsg_type
;
485 if (type
< NLMSG_NOOP
|| type
>= IPQM_MAX
)
486 RCV_SKB_FAIL(-EINVAL
);
488 if (type
<= IPQM_BASE
)
491 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
492 RCV_SKB_FAIL(-EPERM
);
494 write_lock_bh(&queue_lock
);
497 if (peer_pid
!= pid
) {
498 write_unlock_bh(&queue_lock
);
499 RCV_SKB_FAIL(-EBUSY
);
502 net_enable_timestamp();
506 write_unlock_bh(&queue_lock
);
508 status
= ipq_receive_peer(NLMSG_DATA(nlh
), type
,
509 nlmsglen
- NLMSG_LENGTH(0));
511 RCV_SKB_FAIL(status
);
513 if (flags
& NLM_F_ACK
)
514 netlink_ack(skb
, nlh
, 0);
519 ipq_rcv_skb(struct sk_buff
*skb
)
521 mutex_lock(&ipqnl_mutex
);
523 mutex_unlock(&ipqnl_mutex
);
527 ipq_rcv_dev_event(struct notifier_block
*this,
528 unsigned long event
, void *ptr
)
530 struct net_device
*dev
= ptr
;
532 if (dev
->nd_net
!= &init_net
)
535 /* Drop any packets associated with the downed device */
536 if (event
== NETDEV_DOWN
)
537 ipq_dev_drop(dev
->ifindex
);
541 static struct notifier_block ipq_dev_notifier
= {
542 .notifier_call
= ipq_rcv_dev_event
,
546 ipq_rcv_nl_event(struct notifier_block
*this,
547 unsigned long event
, void *ptr
)
549 struct netlink_notify
*n
= ptr
;
551 if (event
== NETLINK_URELEASE
&&
552 n
->protocol
== NETLINK_IP6_FW
&& n
->pid
) {
553 write_lock_bh(&queue_lock
);
554 if ((n
->net
== &init_net
) && (n
->pid
== peer_pid
))
556 write_unlock_bh(&queue_lock
);
561 static struct notifier_block ipq_nl_notifier
= {
562 .notifier_call
= ipq_rcv_nl_event
,
565 static struct ctl_table_header
*ipq_sysctl_header
;
567 static ctl_table ipq_table
[] = {
569 .ctl_name
= NET_IPQ_QMAX
,
570 .procname
= NET_IPQ_QMAX_NAME
,
571 .data
= &queue_maxlen
,
572 .maxlen
= sizeof(queue_maxlen
),
574 .proc_handler
= proc_dointvec
579 static ctl_table ipq_dir_table
[] = {
581 .ctl_name
= NET_IPV6
,
589 static ctl_table ipq_root_table
[] = {
594 .child
= ipq_dir_table
599 static int ip6_queue_show(struct seq_file
*m
, void *v
)
601 read_lock_bh(&queue_lock
);
607 "Queue length : %u\n"
608 "Queue max. length : %u\n"
609 "Queue dropped : %u\n"
610 "Netfilter dropped : %u\n",
619 read_unlock_bh(&queue_lock
);
623 static int ip6_queue_open(struct inode
*inode
, struct file
*file
)
625 return single_open(file
, ip6_queue_show
, NULL
);
628 static const struct file_operations ip6_queue_proc_fops
= {
629 .open
= ip6_queue_open
,
632 .release
= single_release
,
633 .owner
= THIS_MODULE
,
636 static const struct nf_queue_handler nfqh
= {
638 .outfn
= &ipq_enqueue_packet
,
641 static int __init
ip6_queue_init(void)
643 int status
= -ENOMEM
;
644 struct proc_dir_entry
*proc
;
646 netlink_register_notifier(&ipq_nl_notifier
);
647 ipqnl
= netlink_kernel_create(&init_net
, NETLINK_IP6_FW
, 0,
648 ipq_rcv_skb
, NULL
, THIS_MODULE
);
650 printk(KERN_ERR
"ip6_queue: failed to create netlink socket\n");
651 goto cleanup_netlink_notifier
;
654 proc
= create_proc_entry(IPQ_PROC_FS_NAME
, 0, init_net
.proc_net
);
656 proc
->owner
= THIS_MODULE
;
657 proc
->proc_fops
= &ip6_queue_proc_fops
;
659 printk(KERN_ERR
"ip6_queue: failed to create proc entry\n");
663 register_netdevice_notifier(&ipq_dev_notifier
);
664 ipq_sysctl_header
= register_sysctl_table(ipq_root_table
);
666 status
= nf_register_queue_handler(PF_INET6
, &nfqh
);
668 printk(KERN_ERR
"ip6_queue: failed to register queue handler\n");
674 unregister_sysctl_table(ipq_sysctl_header
);
675 unregister_netdevice_notifier(&ipq_dev_notifier
);
676 proc_net_remove(&init_net
, IPQ_PROC_FS_NAME
);
679 sock_release(ipqnl
->sk_socket
);
680 mutex_lock(&ipqnl_mutex
);
681 mutex_unlock(&ipqnl_mutex
);
683 cleanup_netlink_notifier
:
684 netlink_unregister_notifier(&ipq_nl_notifier
);
688 static void __exit
ip6_queue_fini(void)
690 nf_unregister_queue_handlers(&nfqh
);
694 unregister_sysctl_table(ipq_sysctl_header
);
695 unregister_netdevice_notifier(&ipq_dev_notifier
);
696 proc_net_remove(&init_net
, IPQ_PROC_FS_NAME
);
698 sock_release(ipqnl
->sk_socket
);
699 mutex_lock(&ipqnl_mutex
);
700 mutex_unlock(&ipqnl_mutex
);
702 netlink_unregister_notifier(&ipq_nl_notifier
);
705 MODULE_DESCRIPTION("IPv6 packet queue handler");
706 MODULE_LICENSE("GPL");
708 module_init(ip6_queue_init
);
709 module_exit(ip6_queue_fini
);