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 * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
17 * HEAVILY based in ipqueue.c by James Morris. It's just
18 * a little modified version of it, so he's nearly the
20 * Few changes needed, mainly the hard_routing code and
21 * the netlink socket protocol (we're NETLINK_IP6_FW).
22 * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
23 * 2005-02-04: Added /proc counter for dropped packets; fixed so
24 * packets aren't delivered to user space if they're going
27 #include <linux/module.h>
28 #include <linux/skbuff.h>
29 #include <linux/init.h>
30 #include <linux/ipv6.h>
31 #include <linux/notifier.h>
32 #include <linux/netdevice.h>
33 #include <linux/netfilter.h>
34 #include <linux/netlink.h>
35 #include <linux/spinlock.h>
36 #include <linux/sysctl.h>
37 #include <linux/proc_fs.h>
40 #include <net/ip6_route.h>
41 #include <linux/netfilter_ipv4/ip_queue.h>
42 #include <linux/netfilter_ipv4/ip_tables.h>
43 #include <linux/netfilter_ipv6/ip6_tables.h>
45 #define IPQ_QMAX_DEFAULT 1024
46 #define IPQ_PROC_FS_NAME "ip6_queue"
47 #define NET_IPQ_QMAX 2088
48 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
51 struct in6_addr daddr
;
52 struct in6_addr saddr
;
55 struct ipq_queue_entry
{
56 struct list_head list
;
59 struct ipq_rt_info rt_info
;
62 typedef int (*ipq_cmpfn
)(struct ipq_queue_entry
*, unsigned long);
64 static unsigned char copy_mode
= IPQ_COPY_NONE
;
65 static unsigned int queue_maxlen
= IPQ_QMAX_DEFAULT
;
66 static DEFINE_RWLOCK(queue_lock
);
68 static unsigned int copy_range
;
69 static unsigned int queue_total
;
70 static unsigned int queue_dropped
= 0;
71 static unsigned int queue_user_dropped
= 0;
72 static struct sock
*ipqnl
;
73 static LIST_HEAD(queue_list
);
74 static DECLARE_MUTEX(ipqnl_sem
);
77 ipq_issue_verdict(struct ipq_queue_entry
*entry
, int verdict
)
80 nf_reinject(entry
->skb
, entry
->info
, verdict
);
86 __ipq_enqueue_entry(struct ipq_queue_entry
*entry
)
88 list_add(&entry
->list
, &queue_list
);
93 * Find and return a queued entry matched by cmpfn, or return the last
94 * entry if cmpfn is NULL.
96 static inline struct ipq_queue_entry
*
97 __ipq_find_entry(ipq_cmpfn cmpfn
, unsigned long data
)
101 list_for_each_prev(p
, &queue_list
) {
102 struct ipq_queue_entry
*entry
= (struct ipq_queue_entry
*)p
;
104 if (!cmpfn
|| cmpfn(entry
, data
))
111 __ipq_dequeue_entry(struct ipq_queue_entry
*entry
)
113 list_del(&entry
->list
);
117 static inline struct ipq_queue_entry
*
118 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
120 struct ipq_queue_entry
*entry
;
122 entry
= __ipq_find_entry(cmpfn
, data
);
126 __ipq_dequeue_entry(entry
);
132 __ipq_flush(int verdict
)
134 struct ipq_queue_entry
*entry
;
136 while ((entry
= __ipq_find_dequeue_entry(NULL
, 0)))
137 ipq_issue_verdict(entry
, verdict
);
141 __ipq_set_mode(unsigned char mode
, unsigned int range
)
152 case IPQ_COPY_PACKET
:
155 if (copy_range
> 0xFFFF)
170 net_disable_timestamp();
171 __ipq_set_mode(IPQ_COPY_NONE
, 0);
172 __ipq_flush(NF_DROP
);
175 static struct ipq_queue_entry
*
176 ipq_find_dequeue_entry(ipq_cmpfn cmpfn
, unsigned long data
)
178 struct ipq_queue_entry
*entry
;
180 write_lock_bh(&queue_lock
);
181 entry
= __ipq_find_dequeue_entry(cmpfn
, data
);
182 write_unlock_bh(&queue_lock
);
187 ipq_flush(int verdict
)
189 write_lock_bh(&queue_lock
);
190 __ipq_flush(verdict
);
191 write_unlock_bh(&queue_lock
);
194 static struct sk_buff
*
195 ipq_build_packet_message(struct ipq_queue_entry
*entry
, int *errp
)
197 unsigned char *old_tail
;
201 struct ipq_packet_msg
*pmsg
;
202 struct nlmsghdr
*nlh
;
204 read_lock_bh(&queue_lock
);
209 size
= NLMSG_SPACE(sizeof(*pmsg
));
213 case IPQ_COPY_PACKET
:
214 if (entry
->skb
->ip_summed
== CHECKSUM_HW
&&
215 (*errp
= skb_checksum_help(entry
->skb
,
216 entry
->info
->outdev
== NULL
))) {
217 read_unlock_bh(&queue_lock
);
220 if (copy_range
== 0 || copy_range
> entry
->skb
->len
)
221 data_len
= entry
->skb
->len
;
223 data_len
= copy_range
;
225 size
= NLMSG_SPACE(sizeof(*pmsg
) + data_len
);
230 read_unlock_bh(&queue_lock
);
234 read_unlock_bh(&queue_lock
);
236 skb
= alloc_skb(size
, GFP_ATOMIC
);
241 nlh
= NLMSG_PUT(skb
, 0, 0, IPQM_PACKET
, size
- sizeof(*nlh
));
242 pmsg
= NLMSG_DATA(nlh
);
243 memset(pmsg
, 0, sizeof(*pmsg
));
245 pmsg
->packet_id
= (unsigned long )entry
;
246 pmsg
->data_len
= data_len
;
247 pmsg
->timestamp_sec
= entry
->skb
->stamp
.tv_sec
;
248 pmsg
->timestamp_usec
= entry
->skb
->stamp
.tv_usec
;
249 pmsg
->mark
= entry
->skb
->nfmark
;
250 pmsg
->hook
= entry
->info
->hook
;
251 pmsg
->hw_protocol
= entry
->skb
->protocol
;
253 if (entry
->info
->indev
)
254 strcpy(pmsg
->indev_name
, entry
->info
->indev
->name
);
256 pmsg
->indev_name
[0] = '\0';
258 if (entry
->info
->outdev
)
259 strcpy(pmsg
->outdev_name
, entry
->info
->outdev
->name
);
261 pmsg
->outdev_name
[0] = '\0';
263 if (entry
->info
->indev
&& entry
->skb
->dev
) {
264 pmsg
->hw_type
= entry
->skb
->dev
->type
;
265 if (entry
->skb
->dev
->hard_header_parse
)
267 entry
->skb
->dev
->hard_header_parse(entry
->skb
,
272 if (skb_copy_bits(entry
->skb
, 0, pmsg
->payload
, data_len
))
275 nlh
->nlmsg_len
= skb
->tail
- old_tail
;
282 printk(KERN_ERR
"ip6_queue: error creating packet message\n");
287 ipq_enqueue_packet(struct sk_buff
*skb
, struct nf_info
*info
, void *data
)
289 int status
= -EINVAL
;
290 struct sk_buff
*nskb
;
291 struct ipq_queue_entry
*entry
;
293 if (copy_mode
== IPQ_COPY_NONE
)
296 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
298 printk(KERN_ERR
"ip6_queue: OOM in ipq_enqueue_packet()\n");
305 if (entry
->info
->hook
== NF_IP_LOCAL_OUT
) {
306 struct ipv6hdr
*iph
= skb
->nh
.ipv6h
;
308 entry
->rt_info
.daddr
= iph
->daddr
;
309 entry
->rt_info
.saddr
= iph
->saddr
;
312 nskb
= ipq_build_packet_message(entry
, &status
);
316 write_lock_bh(&queue_lock
);
319 goto err_out_free_nskb
;
321 if (queue_total
>= queue_maxlen
) {
325 printk (KERN_WARNING
"ip6_queue: fill at %d entries, "
326 "dropping packet(s). Dropped: %d\n", queue_total
,
328 goto err_out_free_nskb
;
331 /* netlink_unicast will either free the nskb or attach it to a socket */
332 status
= netlink_unicast(ipqnl
, nskb
, peer_pid
, MSG_DONTWAIT
);
334 queue_user_dropped
++;
338 __ipq_enqueue_entry(entry
);
340 write_unlock_bh(&queue_lock
);
347 write_unlock_bh(&queue_lock
);
355 ipq_mangle_ipv6(ipq_verdict_msg_t
*v
, struct ipq_queue_entry
*e
)
358 struct ipv6hdr
*user_iph
= (struct ipv6hdr
*)v
->payload
;
360 if (v
->data_len
< sizeof(*user_iph
))
362 diff
= v
->data_len
- e
->skb
->len
;
364 skb_trim(e
->skb
, v
->data_len
);
366 if (v
->data_len
> 0xFFFF)
368 if (diff
> skb_tailroom(e
->skb
)) {
369 struct sk_buff
*newskb
;
371 newskb
= skb_copy_expand(e
->skb
,
372 skb_headroom(e
->skb
),
375 if (newskb
== NULL
) {
376 printk(KERN_WARNING
"ip6_queue: OOM "
377 "in mangle, dropping packet\n");
381 skb_set_owner_w(newskb
, e
->skb
->sk
);
385 skb_put(e
->skb
, diff
);
387 if (!skb_ip_make_writable(&e
->skb
, v
->data_len
))
389 memcpy(e
->skb
->data
, v
->payload
, v
->data_len
);
390 e
->skb
->ip_summed
= CHECKSUM_NONE
;
391 e
->skb
->nfcache
|= NFC_ALTERED
;
394 * Extra routing may needed on local out, as the QUEUE target never
395 * returns control to the table.
396 * Not a nice way to cmp, but works
398 if (e
->info
->hook
== NF_IP_LOCAL_OUT
) {
399 struct ipv6hdr
*iph
= e
->skb
->nh
.ipv6h
;
400 if (!ipv6_addr_equal(&iph
->daddr
, &e
->rt_info
.daddr
) ||
401 !ipv6_addr_equal(&iph
->saddr
, &e
->rt_info
.saddr
))
402 return ip6_route_me_harder(e
->skb
);
408 id_cmp(struct ipq_queue_entry
*e
, unsigned long id
)
410 return (id
== (unsigned long )e
);
414 ipq_set_verdict(struct ipq_verdict_msg
*vmsg
, unsigned int len
)
416 struct ipq_queue_entry
*entry
;
418 if (vmsg
->value
> NF_MAX_VERDICT
)
421 entry
= ipq_find_dequeue_entry(id_cmp
, vmsg
->id
);
425 int verdict
= vmsg
->value
;
427 if (vmsg
->data_len
&& vmsg
->data_len
== len
)
428 if (ipq_mangle_ipv6(vmsg
, entry
) < 0)
431 ipq_issue_verdict(entry
, verdict
);
437 ipq_set_mode(unsigned char mode
, unsigned int range
)
441 write_lock_bh(&queue_lock
);
442 status
= __ipq_set_mode(mode
, range
);
443 write_unlock_bh(&queue_lock
);
448 ipq_receive_peer(struct ipq_peer_msg
*pmsg
,
449 unsigned char type
, unsigned int len
)
453 if (len
< sizeof(*pmsg
))
458 status
= ipq_set_mode(pmsg
->msg
.mode
.value
,
459 pmsg
->msg
.mode
.range
);
463 if (pmsg
->msg
.verdict
.value
> NF_MAX_VERDICT
)
466 status
= ipq_set_verdict(&pmsg
->msg
.verdict
,
467 len
- sizeof(*pmsg
));
476 dev_cmp(struct ipq_queue_entry
*entry
, unsigned long ifindex
)
478 if (entry
->info
->indev
)
479 if (entry
->info
->indev
->ifindex
== ifindex
)
482 if (entry
->info
->outdev
)
483 if (entry
->info
->outdev
->ifindex
== ifindex
)
490 ipq_dev_drop(int ifindex
)
492 struct ipq_queue_entry
*entry
;
494 while ((entry
= ipq_find_dequeue_entry(dev_cmp
, ifindex
)) != NULL
)
495 ipq_issue_verdict(entry
, NF_DROP
);
498 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
501 ipq_rcv_skb(struct sk_buff
*skb
)
503 int status
, type
, pid
, flags
, nlmsglen
, skblen
;
504 struct nlmsghdr
*nlh
;
507 if (skblen
< sizeof(*nlh
))
510 nlh
= (struct nlmsghdr
*)skb
->data
;
511 nlmsglen
= nlh
->nlmsg_len
;
512 if (nlmsglen
< sizeof(*nlh
) || skblen
< nlmsglen
)
515 pid
= nlh
->nlmsg_pid
;
516 flags
= nlh
->nlmsg_flags
;
518 if(pid
<= 0 || !(flags
& NLM_F_REQUEST
) || flags
& NLM_F_MULTI
)
519 RCV_SKB_FAIL(-EINVAL
);
521 if (flags
& MSG_TRUNC
)
522 RCV_SKB_FAIL(-ECOMM
);
524 type
= nlh
->nlmsg_type
;
525 if (type
< NLMSG_NOOP
|| type
>= IPQM_MAX
)
526 RCV_SKB_FAIL(-EINVAL
);
528 if (type
<= IPQM_BASE
)
531 if (security_netlink_recv(skb
))
532 RCV_SKB_FAIL(-EPERM
);
534 write_lock_bh(&queue_lock
);
537 if (peer_pid
!= pid
) {
538 write_unlock_bh(&queue_lock
);
539 RCV_SKB_FAIL(-EBUSY
);
542 net_enable_timestamp();
546 write_unlock_bh(&queue_lock
);
548 status
= ipq_receive_peer(NLMSG_DATA(nlh
), type
,
549 skblen
- NLMSG_LENGTH(0));
551 RCV_SKB_FAIL(status
);
553 if (flags
& NLM_F_ACK
)
554 netlink_ack(skb
, nlh
, 0);
559 ipq_rcv_sk(struct sock
*sk
, int len
)
566 for (qlen
= skb_queue_len(&sk
->sk_receive_queue
); qlen
; qlen
--) {
567 skb
= skb_dequeue(&sk
->sk_receive_queue
);
576 ipq_rcv_dev_event(struct notifier_block
*this,
577 unsigned long event
, void *ptr
)
579 struct net_device
*dev
= ptr
;
581 /* Drop any packets associated with the downed device */
582 if (event
== NETDEV_DOWN
)
583 ipq_dev_drop(dev
->ifindex
);
587 static struct notifier_block ipq_dev_notifier
= {
588 .notifier_call
= ipq_rcv_dev_event
,
592 ipq_rcv_nl_event(struct notifier_block
*this,
593 unsigned long event
, void *ptr
)
595 struct netlink_notify
*n
= ptr
;
597 if (event
== NETLINK_URELEASE
&&
598 n
->protocol
== NETLINK_IP6_FW
&& n
->pid
) {
599 write_lock_bh(&queue_lock
);
600 if (n
->pid
== peer_pid
)
602 write_unlock_bh(&queue_lock
);
607 static struct notifier_block ipq_nl_notifier
= {
608 .notifier_call
= ipq_rcv_nl_event
,
611 static struct ctl_table_header
*ipq_sysctl_header
;
613 static ctl_table ipq_table
[] = {
615 .ctl_name
= NET_IPQ_QMAX
,
616 .procname
= NET_IPQ_QMAX_NAME
,
617 .data
= &queue_maxlen
,
618 .maxlen
= sizeof(queue_maxlen
),
620 .proc_handler
= proc_dointvec
625 static ctl_table ipq_dir_table
[] = {
627 .ctl_name
= NET_IPV6
,
635 static ctl_table ipq_root_table
[] = {
640 .child
= ipq_dir_table
646 ipq_get_info(char *buffer
, char **start
, off_t offset
, int length
)
650 read_lock_bh(&queue_lock
);
652 len
= sprintf(buffer
,
656 "Queue length : %u\n"
657 "Queue max. length : %u\n"
658 "Queue dropped : %u\n"
659 "Netfilter dropped : %u\n",
668 read_unlock_bh(&queue_lock
);
670 *start
= buffer
+ offset
;
680 init_or_cleanup(int init
)
682 int status
= -ENOMEM
;
683 struct proc_dir_entry
*proc
;
688 netlink_register_notifier(&ipq_nl_notifier
);
689 ipqnl
= netlink_kernel_create(NETLINK_IP6_FW
, ipq_rcv_sk
);
691 printk(KERN_ERR
"ip6_queue: failed to create netlink socket\n");
692 goto cleanup_netlink_notifier
;
695 proc
= proc_net_create(IPQ_PROC_FS_NAME
, 0, ipq_get_info
);
697 proc
->owner
= THIS_MODULE
;
699 printk(KERN_ERR
"ip6_queue: failed to create proc entry\n");
703 register_netdevice_notifier(&ipq_dev_notifier
);
704 ipq_sysctl_header
= register_sysctl_table(ipq_root_table
, 0);
706 status
= nf_register_queue_handler(PF_INET6
, ipq_enqueue_packet
, NULL
);
708 printk(KERN_ERR
"ip6_queue: failed to register queue handler\n");
714 nf_unregister_queue_handler(PF_INET6
);
719 unregister_sysctl_table(ipq_sysctl_header
);
720 unregister_netdevice_notifier(&ipq_dev_notifier
);
721 proc_net_remove(IPQ_PROC_FS_NAME
);
724 sock_release(ipqnl
->sk_socket
);
728 cleanup_netlink_notifier
:
729 netlink_unregister_notifier(&ipq_nl_notifier
);
733 static int __init
init(void)
736 return init_or_cleanup(1);
739 static void __exit
fini(void)
744 MODULE_DESCRIPTION("IPv6 packet queue handler");
745 MODULE_LICENSE("GPL");