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_make_writable(&e->skb, v->data_len))
389 memcpy(e->skb->data, v->payload, v->data_len);
390 e->skb->ip_summed = CHECKSUM_NONE;
393 * Extra routing may needed on local out, as the QUEUE target never
394 * returns control to the table.
395 * Not a nice way to cmp, but works
397 if (e->info->hook == NF_IP_LOCAL_OUT) {
398 struct ipv6hdr *iph = e->skb->nh.ipv6h;
399 if (!ipv6_addr_equal(&iph->daddr, &e->rt_info.daddr) ||
400 !ipv6_addr_equal(&iph->saddr, &e->rt_info.saddr))
401 return ip6_route_me_harder(e->skb);
407 id_cmp(struct ipq_queue_entry *e, unsigned long id)
409 return (id == (unsigned long )e);
413 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
415 struct ipq_queue_entry *entry;
417 if (vmsg->value > NF_MAX_VERDICT)
420 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
424 int verdict = vmsg->value;
426 if (vmsg->data_len && vmsg->data_len == len)
427 if (ipq_mangle_ipv6(vmsg, entry) < 0)
430 ipq_issue_verdict(entry, verdict);
436 ipq_set_mode(unsigned char mode, unsigned int range)
440 write_lock_bh(&queue_lock);
441 status = __ipq_set_mode(mode, range);
442 write_unlock_bh(&queue_lock);
447 ipq_receive_peer(struct ipq_peer_msg *pmsg,
448 unsigned char type, unsigned int len)
452 if (len < sizeof(*pmsg))
457 status = ipq_set_mode(pmsg->msg.mode.value,
458 pmsg->msg.mode.range);
462 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
465 status = ipq_set_verdict(&pmsg->msg.verdict,
466 len - sizeof(*pmsg));
475 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
477 if (entry->info->indev)
478 if (entry->info->indev->ifindex == ifindex)
481 if (entry->info->outdev)
482 if (entry->info->outdev->ifindex == ifindex)
489 ipq_dev_drop(int ifindex)
491 struct ipq_queue_entry *entry;
493 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
494 ipq_issue_verdict(entry, NF_DROP);
497 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
500 ipq_rcv_skb(struct sk_buff *skb)
502 int status, type, pid, flags, nlmsglen, skblen;
503 struct nlmsghdr *nlh;
506 if (skblen < sizeof(*nlh))
509 nlh = (struct nlmsghdr *)skb->data;
510 nlmsglen = nlh->nlmsg_len;
511 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
514 pid = nlh->nlmsg_pid;
515 flags = nlh->nlmsg_flags;
517 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
518 RCV_SKB_FAIL(-EINVAL);
520 if (flags & MSG_TRUNC)
521 RCV_SKB_FAIL(-ECOMM);
523 type = nlh->nlmsg_type;
524 if (type < NLMSG_NOOP || type >= IPQM_MAX)
525 RCV_SKB_FAIL(-EINVAL);
527 if (type <= IPQM_BASE)
530 if (security_netlink_recv(skb))
531 RCV_SKB_FAIL(-EPERM);
533 write_lock_bh(&queue_lock);
536 if (peer_pid != pid) {
537 write_unlock_bh(&queue_lock);
538 RCV_SKB_FAIL(-EBUSY);
541 net_enable_timestamp();
545 write_unlock_bh(&queue_lock);
547 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
548 skblen - NLMSG_LENGTH(0));
550 RCV_SKB_FAIL(status);
552 if (flags & NLM_F_ACK)
553 netlink_ack(skb, nlh, 0);
558 ipq_rcv_sk(struct sock *sk, int len)
565 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
566 skb = skb_dequeue(&sk->sk_receive_queue);
575 ipq_rcv_dev_event(struct notifier_block *this,
576 unsigned long event, void *ptr)
578 struct net_device *dev = ptr;
580 /* Drop any packets associated with the downed device */
581 if (event == NETDEV_DOWN)
582 ipq_dev_drop(dev->ifindex);
586 static struct notifier_block ipq_dev_notifier = {
587 .notifier_call = ipq_rcv_dev_event,
591 ipq_rcv_nl_event(struct notifier_block *this,
592 unsigned long event, void *ptr)
594 struct netlink_notify *n = ptr;
596 if (event == NETLINK_URELEASE &&
597 n->protocol == NETLINK_IP6_FW && n->pid) {
598 write_lock_bh(&queue_lock);
599 if (n->pid == peer_pid)
601 write_unlock_bh(&queue_lock);
606 static struct notifier_block ipq_nl_notifier = {
607 .notifier_call = ipq_rcv_nl_event,
610 static struct ctl_table_header *ipq_sysctl_header;
612 static ctl_table ipq_table[] = {
614 .ctl_name = NET_IPQ_QMAX,
615 .procname = NET_IPQ_QMAX_NAME,
616 .data = &queue_maxlen,
617 .maxlen = sizeof(queue_maxlen),
619 .proc_handler = proc_dointvec
624 static ctl_table ipq_dir_table[] = {
626 .ctl_name = NET_IPV6,
634 static ctl_table ipq_root_table[] = {
639 .child = ipq_dir_table
645 ipq_get_info(char *buffer, char **start, off_t offset, int length)
649 read_lock_bh(&queue_lock);
651 len = sprintf(buffer,
655 "Queue length : %u\n"
656 "Queue max. length : %u\n"
657 "Queue dropped : %u\n"
658 "Netfilter dropped : %u\n",
667 read_unlock_bh(&queue_lock);
669 *start = buffer + offset;
679 init_or_cleanup(int init)
681 int status = -ENOMEM;
682 struct proc_dir_entry *proc;
687 netlink_register_notifier(&ipq_nl_notifier);
688 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
690 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
691 goto cleanup_netlink_notifier;
694 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
696 proc->owner = THIS_MODULE;
698 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
702 register_netdevice_notifier(&ipq_dev_notifier);
703 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
705 status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
707 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
713 nf_unregister_queue_handler(PF_INET6);
718 unregister_sysctl_table(ipq_sysctl_header);
719 unregister_netdevice_notifier(&ipq_dev_notifier);
720 proc_net_remove(IPQ_PROC_FS_NAME);
723 sock_release(ipqnl->sk_socket);
727 cleanup_netlink_notifier:
728 netlink_unregister_notifier(&ipq_nl_notifier);
732 static int __init init(void)
735 return init_or_cleanup(1);
738 static void __exit fini(void)
743 MODULE_DESCRIPTION("IPv6 packet queue handler");
744 MODULE_LICENSE("GPL");