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(&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)
87 list_for_each_prev(p, &queue_list) {
88 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
90 if (!cmpfn || cmpfn(entry, data))
97 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
99 list_del(&entry->list);
103 static inline struct ipq_queue_entry *
104 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
106 struct ipq_queue_entry *entry;
108 entry = __ipq_find_entry(cmpfn, data);
112 __ipq_dequeue_entry(entry);
118 __ipq_flush(int verdict)
120 struct ipq_queue_entry *entry;
122 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
123 ipq_issue_verdict(entry, verdict);
127 __ipq_set_mode(unsigned char mode, unsigned int range)
138 case IPQ_COPY_PACKET:
141 if (copy_range > 0xFFFF)
156 net_disable_timestamp();
157 __ipq_set_mode(IPQ_COPY_NONE, 0);
158 __ipq_flush(NF_DROP);
161 static struct ipq_queue_entry *
162 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
164 struct ipq_queue_entry *entry;
166 write_lock_bh(&queue_lock);
167 entry = __ipq_find_dequeue_entry(cmpfn, data);
168 write_unlock_bh(&queue_lock);
173 ipq_flush(int verdict)
175 write_lock_bh(&queue_lock);
176 __ipq_flush(verdict);
177 write_unlock_bh(&queue_lock);
180 static struct sk_buff *
181 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
183 sk_buff_data_t old_tail;
187 struct ipq_packet_msg *pmsg;
188 struct nlmsghdr *nlh;
191 read_lock_bh(&queue_lock);
196 size = NLMSG_SPACE(sizeof(*pmsg));
200 case IPQ_COPY_PACKET:
201 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
202 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
203 (*errp = skb_checksum_help(entry->skb))) {
204 read_unlock_bh(&queue_lock);
207 if (copy_range == 0 || copy_range > entry->skb->len)
208 data_len = entry->skb->len;
210 data_len = copy_range;
212 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
217 read_unlock_bh(&queue_lock);
221 read_unlock_bh(&queue_lock);
223 skb = alloc_skb(size, GFP_ATOMIC);
227 old_tail = skb->tail;
228 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
229 pmsg = NLMSG_DATA(nlh);
230 memset(pmsg, 0, sizeof(*pmsg));
232 pmsg->packet_id = (unsigned long )entry;
233 pmsg->data_len = data_len;
234 tv = ktime_to_timeval(entry->skb->tstamp);
235 pmsg->timestamp_sec = tv.tv_sec;
236 pmsg->timestamp_usec = tv.tv_usec;
237 pmsg->mark = entry->skb->mark;
238 pmsg->hook = entry->info->hook;
239 pmsg->hw_protocol = entry->skb->protocol;
241 if (entry->info->indev)
242 strcpy(pmsg->indev_name, entry->info->indev->name);
244 pmsg->indev_name[0] = '\0';
246 if (entry->info->outdev)
247 strcpy(pmsg->outdev_name, entry->info->outdev->name);
249 pmsg->outdev_name[0] = '\0';
251 if (entry->info->indev && entry->skb->dev) {
252 pmsg->hw_type = entry->skb->dev->type;
253 pmsg->hw_addrlen = dev_parse_header(entry->skb, pmsg->hw_addr);
257 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
260 nlh->nlmsg_len = skb->tail - old_tail;
267 printk(KERN_ERR "ip6_queue: error creating packet message\n");
272 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
273 unsigned int queuenum)
275 int status = -EINVAL;
276 struct sk_buff *nskb;
277 struct ipq_queue_entry *entry;
279 if (copy_mode == IPQ_COPY_NONE)
282 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
284 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
291 nskb = ipq_build_packet_message(entry, &status);
295 write_lock_bh(&queue_lock);
298 goto err_out_free_nskb;
300 if (queue_total >= queue_maxlen) {
304 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
305 "dropping packet(s). Dropped: %d\n", queue_total,
307 goto err_out_free_nskb;
310 /* netlink_unicast will either free the nskb or attach it to a socket */
311 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
313 queue_user_dropped++;
317 __ipq_enqueue_entry(entry);
319 write_unlock_bh(&queue_lock);
326 write_unlock_bh(&queue_lock);
334 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
338 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
340 if (v->data_len < sizeof(*user_iph))
342 diff = v->data_len - e->skb->len;
344 if (pskb_trim(e->skb, v->data_len))
346 } else if (diff > 0) {
347 if (v->data_len > 0xFFFF)
349 if (diff > skb_tailroom(e->skb)) {
350 err = pskb_expand_head(e->skb, 0,
351 diff - skb_tailroom(e->skb),
354 printk(KERN_WARNING "ip6_queue: OOM "
355 "in mangle, dropping packet\n");
359 skb_put(e->skb, diff);
361 if (!skb_make_writable(e->skb, v->data_len))
363 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
364 e->skb->ip_summed = CHECKSUM_NONE;
370 id_cmp(struct ipq_queue_entry *e, unsigned long id)
372 return (id == (unsigned long )e);
376 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
378 struct ipq_queue_entry *entry;
380 if (vmsg->value > NF_MAX_VERDICT)
383 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
387 int verdict = vmsg->value;
389 if (vmsg->data_len && vmsg->data_len == len)
390 if (ipq_mangle_ipv6(vmsg, entry) < 0)
393 ipq_issue_verdict(entry, verdict);
399 ipq_set_mode(unsigned char mode, unsigned int range)
403 write_lock_bh(&queue_lock);
404 status = __ipq_set_mode(mode, range);
405 write_unlock_bh(&queue_lock);
410 ipq_receive_peer(struct ipq_peer_msg *pmsg,
411 unsigned char type, unsigned int len)
415 if (len < sizeof(*pmsg))
420 status = ipq_set_mode(pmsg->msg.mode.value,
421 pmsg->msg.mode.range);
425 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
428 status = ipq_set_verdict(&pmsg->msg.verdict,
429 len - sizeof(*pmsg));
438 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
440 if (entry->info->indev)
441 if (entry->info->indev->ifindex == ifindex)
444 if (entry->info->outdev)
445 if (entry->info->outdev->ifindex == ifindex)
452 ipq_dev_drop(int ifindex)
454 struct ipq_queue_entry *entry;
456 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
457 ipq_issue_verdict(entry, NF_DROP);
460 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
463 __ipq_rcv_skb(struct sk_buff *skb)
465 int status, type, pid, flags, nlmsglen, skblen;
466 struct nlmsghdr *nlh;
469 if (skblen < sizeof(*nlh))
472 nlh = nlmsg_hdr(skb);
473 nlmsglen = nlh->nlmsg_len;
474 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
477 pid = nlh->nlmsg_pid;
478 flags = nlh->nlmsg_flags;
480 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
481 RCV_SKB_FAIL(-EINVAL);
483 if (flags & MSG_TRUNC)
484 RCV_SKB_FAIL(-ECOMM);
486 type = nlh->nlmsg_type;
487 if (type < NLMSG_NOOP || type >= IPQM_MAX)
488 RCV_SKB_FAIL(-EINVAL);
490 if (type <= IPQM_BASE)
493 if (security_netlink_recv(skb, CAP_NET_ADMIN))
494 RCV_SKB_FAIL(-EPERM);
496 write_lock_bh(&queue_lock);
499 if (peer_pid != pid) {
500 write_unlock_bh(&queue_lock);
501 RCV_SKB_FAIL(-EBUSY);
504 net_enable_timestamp();
508 write_unlock_bh(&queue_lock);
510 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
511 nlmsglen - NLMSG_LENGTH(0));
513 RCV_SKB_FAIL(status);
515 if (flags & NLM_F_ACK)
516 netlink_ack(skb, nlh, 0);
521 ipq_rcv_skb(struct sk_buff *skb)
523 mutex_lock(&ipqnl_mutex);
525 mutex_unlock(&ipqnl_mutex);
529 ipq_rcv_dev_event(struct notifier_block *this,
530 unsigned long event, void *ptr)
532 struct net_device *dev = ptr;
534 if (dev->nd_net != &init_net)
537 /* Drop any packets associated with the downed device */
538 if (event == NETDEV_DOWN)
539 ipq_dev_drop(dev->ifindex);
543 static struct notifier_block ipq_dev_notifier = {
544 .notifier_call = ipq_rcv_dev_event,
548 ipq_rcv_nl_event(struct notifier_block *this,
549 unsigned long event, void *ptr)
551 struct netlink_notify *n = ptr;
553 if (event == NETLINK_URELEASE &&
554 n->protocol == NETLINK_IP6_FW && n->pid) {
555 write_lock_bh(&queue_lock);
556 if ((n->net == &init_net) && (n->pid == peer_pid))
558 write_unlock_bh(&queue_lock);
563 static struct notifier_block ipq_nl_notifier = {
564 .notifier_call = ipq_rcv_nl_event,
567 static struct ctl_table_header *ipq_sysctl_header;
569 static ctl_table ipq_table[] = {
571 .ctl_name = NET_IPQ_QMAX,
572 .procname = NET_IPQ_QMAX_NAME,
573 .data = &queue_maxlen,
574 .maxlen = sizeof(queue_maxlen),
576 .proc_handler = proc_dointvec
581 static ctl_table ipq_dir_table[] = {
583 .ctl_name = NET_IPV6,
591 static ctl_table ipq_root_table[] = {
596 .child = ipq_dir_table
601 static int ip6_queue_show(struct seq_file *m, void *v)
603 read_lock_bh(&queue_lock);
609 "Queue length : %u\n"
610 "Queue max. length : %u\n"
611 "Queue dropped : %u\n"
612 "Netfilter dropped : %u\n",
621 read_unlock_bh(&queue_lock);
625 static int ip6_queue_open(struct inode *inode, struct file *file)
627 return single_open(file, ip6_queue_show, NULL);
630 static const struct file_operations ip6_queue_proc_fops = {
631 .open = ip6_queue_open,
634 .release = single_release,
635 .owner = THIS_MODULE,
638 static const struct nf_queue_handler nfqh = {
640 .outfn = &ipq_enqueue_packet,
643 static int __init ip6_queue_init(void)
645 int status = -ENOMEM;
646 struct proc_dir_entry *proc;
648 netlink_register_notifier(&ipq_nl_notifier);
649 ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
650 ipq_rcv_skb, NULL, THIS_MODULE);
652 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
653 goto cleanup_netlink_notifier;
656 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
658 proc->owner = THIS_MODULE;
659 proc->proc_fops = &ip6_queue_proc_fops;
661 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
665 register_netdevice_notifier(&ipq_dev_notifier);
666 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
668 status = nf_register_queue_handler(PF_INET6, &nfqh);
670 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
676 unregister_sysctl_table(ipq_sysctl_header);
677 unregister_netdevice_notifier(&ipq_dev_notifier);
678 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
681 sock_release(ipqnl->sk_socket);
682 mutex_lock(&ipqnl_mutex);
683 mutex_unlock(&ipqnl_mutex);
685 cleanup_netlink_notifier:
686 netlink_unregister_notifier(&ipq_nl_notifier);
690 static void __exit ip6_queue_fini(void)
692 nf_unregister_queue_handlers(&nfqh);
696 unregister_sysctl_table(ipq_sysctl_header);
697 unregister_netdevice_notifier(&ipq_dev_notifier);
698 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
700 sock_release(ipqnl->sk_socket);
701 mutex_lock(&ipqnl_mutex);
702 mutex_unlock(&ipqnl_mutex);
704 netlink_unregister_notifier(&ipq_nl_notifier);
707 MODULE_DESCRIPTION("IPv6 packet queue handler");
708 MODULE_LICENSE("GPL");
710 module_init(ip6_queue_init);
711 module_exit(ip6_queue_fini);