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 <linux/netfilter_ipv4/ip_queue.h>
33 #include <linux/netfilter_ipv4/ip_tables.h>
34 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #define IPQ_QMAX_DEFAULT 1024
37 #define IPQ_PROC_FS_NAME "ip6_queue"
38 #define NET_IPQ_QMAX 2088
39 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
41 struct ipq_queue_entry {
42 struct list_head list;
47 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
49 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
50 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
51 static DEFINE_RWLOCK(queue_lock);
52 static int peer_pid __read_mostly;
53 static unsigned int copy_range __read_mostly;
54 static unsigned int queue_total;
55 static unsigned int queue_dropped = 0;
56 static unsigned int queue_user_dropped = 0;
57 static struct sock *ipqnl __read_mostly;
58 static LIST_HEAD(queue_list);
59 static DEFINE_MUTEX(ipqnl_mutex);
62 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
65 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 pmsg->hw_addrlen = dev_parse_header(entry->skb, pmsg->hw_addr);
256 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
259 nlh->nlmsg_len = skb->tail - old_tail;
266 printk(KERN_ERR "ip6_queue: error creating packet message\n");
271 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
272 unsigned int queuenum, void *data)
274 int status = -EINVAL;
275 struct sk_buff *nskb;
276 struct ipq_queue_entry *entry;
278 if (copy_mode == IPQ_COPY_NONE)
281 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
283 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
290 nskb = ipq_build_packet_message(entry, &status);
294 write_lock_bh(&queue_lock);
297 goto err_out_free_nskb;
299 if (queue_total >= queue_maxlen) {
303 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
304 "dropping packet(s). Dropped: %d\n", queue_total,
306 goto err_out_free_nskb;
309 /* netlink_unicast will either free the nskb or attach it to a socket */
310 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
312 queue_user_dropped++;
316 __ipq_enqueue_entry(entry);
318 write_unlock_bh(&queue_lock);
325 write_unlock_bh(&queue_lock);
333 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
337 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
339 if (v->data_len < sizeof(*user_iph))
341 diff = v->data_len - e->skb->len;
343 if (pskb_trim(e->skb, v->data_len))
345 } else if (diff > 0) {
346 if (v->data_len > 0xFFFF)
348 if (diff > skb_tailroom(e->skb)) {
349 err = pskb_expand_head(e->skb, 0,
350 diff - skb_tailroom(e->skb),
353 printk(KERN_WARNING "ip6_queue: OOM "
354 "in mangle, dropping packet\n");
358 skb_put(e->skb, diff);
360 if (!skb_make_writable(e->skb, v->data_len))
362 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
363 e->skb->ip_summed = CHECKSUM_NONE;
369 id_cmp(struct ipq_queue_entry *e, unsigned long id)
371 return (id == (unsigned long )e);
375 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
377 struct ipq_queue_entry *entry;
379 if (vmsg->value > NF_MAX_VERDICT)
382 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
386 int verdict = vmsg->value;
388 if (vmsg->data_len && vmsg->data_len == len)
389 if (ipq_mangle_ipv6(vmsg, entry) < 0)
392 ipq_issue_verdict(entry, verdict);
398 ipq_set_mode(unsigned char mode, unsigned int range)
402 write_lock_bh(&queue_lock);
403 status = __ipq_set_mode(mode, range);
404 write_unlock_bh(&queue_lock);
409 ipq_receive_peer(struct ipq_peer_msg *pmsg,
410 unsigned char type, unsigned int len)
414 if (len < sizeof(*pmsg))
419 status = ipq_set_mode(pmsg->msg.mode.value,
420 pmsg->msg.mode.range);
424 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
427 status = ipq_set_verdict(&pmsg->msg.verdict,
428 len - sizeof(*pmsg));
437 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
439 if (entry->info->indev)
440 if (entry->info->indev->ifindex == ifindex)
443 if (entry->info->outdev)
444 if (entry->info->outdev->ifindex == ifindex)
451 ipq_dev_drop(int ifindex)
453 struct ipq_queue_entry *entry;
455 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
456 ipq_issue_verdict(entry, NF_DROP);
459 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
462 __ipq_rcv_skb(struct sk_buff *skb)
464 int status, type, pid, flags, nlmsglen, skblen;
465 struct nlmsghdr *nlh;
468 if (skblen < sizeof(*nlh))
471 nlh = nlmsg_hdr(skb);
472 nlmsglen = nlh->nlmsg_len;
473 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
476 pid = nlh->nlmsg_pid;
477 flags = nlh->nlmsg_flags;
479 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
480 RCV_SKB_FAIL(-EINVAL);
482 if (flags & MSG_TRUNC)
483 RCV_SKB_FAIL(-ECOMM);
485 type = nlh->nlmsg_type;
486 if (type < NLMSG_NOOP || type >= IPQM_MAX)
487 RCV_SKB_FAIL(-EINVAL);
489 if (type <= IPQM_BASE)
492 if (security_netlink_recv(skb, CAP_NET_ADMIN))
493 RCV_SKB_FAIL(-EPERM);
495 write_lock_bh(&queue_lock);
498 if (peer_pid != pid) {
499 write_unlock_bh(&queue_lock);
500 RCV_SKB_FAIL(-EBUSY);
503 net_enable_timestamp();
507 write_unlock_bh(&queue_lock);
509 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
510 nlmsglen - NLMSG_LENGTH(0));
512 RCV_SKB_FAIL(status);
514 if (flags & NLM_F_ACK)
515 netlink_ack(skb, nlh, 0);
520 ipq_rcv_skb(struct sk_buff *skb)
522 mutex_lock(&ipqnl_mutex);
524 mutex_unlock(&ipqnl_mutex);
528 ipq_rcv_dev_event(struct notifier_block *this,
529 unsigned long event, void *ptr)
531 struct net_device *dev = ptr;
533 if (dev->nd_net != &init_net)
536 /* Drop any packets associated with the downed device */
537 if (event == NETDEV_DOWN)
538 ipq_dev_drop(dev->ifindex);
542 static struct notifier_block ipq_dev_notifier = {
543 .notifier_call = ipq_rcv_dev_event,
547 ipq_rcv_nl_event(struct notifier_block *this,
548 unsigned long event, void *ptr)
550 struct netlink_notify *n = ptr;
552 if (event == NETLINK_URELEASE &&
553 n->protocol == NETLINK_IP6_FW && n->pid) {
554 write_lock_bh(&queue_lock);
555 if ((n->net == &init_net) && (n->pid == peer_pid))
557 write_unlock_bh(&queue_lock);
562 static struct notifier_block ipq_nl_notifier = {
563 .notifier_call = ipq_rcv_nl_event,
566 static struct ctl_table_header *ipq_sysctl_header;
568 static ctl_table ipq_table[] = {
570 .ctl_name = NET_IPQ_QMAX,
571 .procname = NET_IPQ_QMAX_NAME,
572 .data = &queue_maxlen,
573 .maxlen = sizeof(queue_maxlen),
575 .proc_handler = proc_dointvec
580 static ctl_table ipq_dir_table[] = {
582 .ctl_name = NET_IPV6,
590 static ctl_table ipq_root_table[] = {
595 .child = ipq_dir_table
600 static int ip6_queue_show(struct seq_file *m, void *v)
602 read_lock_bh(&queue_lock);
608 "Queue length : %u\n"
609 "Queue max. length : %u\n"
610 "Queue dropped : %u\n"
611 "Netfilter dropped : %u\n",
620 read_unlock_bh(&queue_lock);
624 static int ip6_queue_open(struct inode *inode, struct file *file)
626 return single_open(file, ip6_queue_show, NULL);
629 static const struct file_operations ip6_queue_proc_fops = {
630 .open = ip6_queue_open,
633 .release = single_release,
634 .owner = THIS_MODULE,
637 static struct nf_queue_handler nfqh = {
639 .outfn = &ipq_enqueue_packet,
642 static int __init ip6_queue_init(void)
644 int status = -ENOMEM;
645 struct proc_dir_entry *proc;
647 netlink_register_notifier(&ipq_nl_notifier);
648 ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
649 ipq_rcv_skb, NULL, THIS_MODULE);
651 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
652 goto cleanup_netlink_notifier;
655 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
657 proc->owner = THIS_MODULE;
658 proc->proc_fops = &ip6_queue_proc_fops;
660 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
664 register_netdevice_notifier(&ipq_dev_notifier);
665 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
667 status = nf_register_queue_handler(PF_INET6, &nfqh);
669 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
675 unregister_sysctl_table(ipq_sysctl_header);
676 unregister_netdevice_notifier(&ipq_dev_notifier);
677 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
680 sock_release(ipqnl->sk_socket);
681 mutex_lock(&ipqnl_mutex);
682 mutex_unlock(&ipqnl_mutex);
684 cleanup_netlink_notifier:
685 netlink_unregister_notifier(&ipq_nl_notifier);
689 static void __exit ip6_queue_fini(void)
691 nf_unregister_queue_handlers(&nfqh);
695 unregister_sysctl_table(ipq_sysctl_header);
696 unregister_netdevice_notifier(&ipq_dev_notifier);
697 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
699 sock_release(ipqnl->sk_socket);
700 mutex_lock(&ipqnl_mutex);
701 mutex_unlock(&ipqnl_mutex);
703 netlink_unregister_notifier(&ipq_nl_notifier);
706 MODULE_DESCRIPTION("IPv6 packet queue handler");
707 MODULE_LICENSE("GPL");
709 module_init(ip6_queue_init);
710 module_exit(ip6_queue_fini);