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)
79 nf_reinject(entry->skb, entry->info, verdict);
84 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
86 list_add(&entry->list, &queue_list);
91 * Find and return a queued entry matched by cmpfn, or return the last
92 * entry if cmpfn is NULL.
94 static inline struct ipq_queue_entry *
95 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
99 list_for_each_prev(p, &queue_list) {
100 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
102 if (!cmpfn || cmpfn(entry, data))
109 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
111 list_del(&entry->list);
115 static inline struct ipq_queue_entry *
116 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
118 struct ipq_queue_entry *entry;
120 entry = __ipq_find_entry(cmpfn, data);
124 __ipq_dequeue_entry(entry);
130 __ipq_flush(int verdict)
132 struct ipq_queue_entry *entry;
134 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
135 ipq_issue_verdict(entry, verdict);
139 __ipq_set_mode(unsigned char mode, unsigned int range)
150 case IPQ_COPY_PACKET:
153 if (copy_range > 0xFFFF)
168 net_disable_timestamp();
169 __ipq_set_mode(IPQ_COPY_NONE, 0);
170 __ipq_flush(NF_DROP);
173 static struct ipq_queue_entry *
174 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
176 struct ipq_queue_entry *entry;
178 write_lock_bh(&queue_lock);
179 entry = __ipq_find_dequeue_entry(cmpfn, data);
180 write_unlock_bh(&queue_lock);
185 ipq_flush(int verdict)
187 write_lock_bh(&queue_lock);
188 __ipq_flush(verdict);
189 write_unlock_bh(&queue_lock);
192 static struct sk_buff *
193 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
195 unsigned char *old_tail;
199 struct ipq_packet_msg *pmsg;
200 struct nlmsghdr *nlh;
202 read_lock_bh(&queue_lock);
207 size = NLMSG_SPACE(sizeof(*pmsg));
211 case IPQ_COPY_PACKET:
212 if (copy_range == 0 || copy_range > entry->skb->len)
213 data_len = entry->skb->len;
215 data_len = copy_range;
217 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
222 read_unlock_bh(&queue_lock);
226 read_unlock_bh(&queue_lock);
228 skb = alloc_skb(size, GFP_ATOMIC);
233 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
234 pmsg = NLMSG_DATA(nlh);
235 memset(pmsg, 0, sizeof(*pmsg));
237 pmsg->packet_id = (unsigned long )entry;
238 pmsg->data_len = data_len;
239 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
240 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
241 pmsg->mark = entry->skb->nfmark;
242 pmsg->hook = entry->info->hook;
243 pmsg->hw_protocol = entry->skb->protocol;
245 if (entry->info->indev)
246 strcpy(pmsg->indev_name, entry->info->indev->name);
248 pmsg->indev_name[0] = '\0';
250 if (entry->info->outdev)
251 strcpy(pmsg->outdev_name, entry->info->outdev->name);
253 pmsg->outdev_name[0] = '\0';
255 if (entry->info->indev && entry->skb->dev) {
256 pmsg->hw_type = entry->skb->dev->type;
257 if (entry->skb->dev->hard_header_parse)
259 entry->skb->dev->hard_header_parse(entry->skb,
264 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
267 nlh->nlmsg_len = skb->tail - old_tail;
274 printk(KERN_ERR "ip6_queue: error creating packet message\n");
279 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
281 int status = -EINVAL;
282 struct sk_buff *nskb;
283 struct ipq_queue_entry *entry;
285 if (copy_mode == IPQ_COPY_NONE)
288 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
290 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
297 if (entry->info->hook == NF_IP_LOCAL_OUT) {
298 struct ipv6hdr *iph = skb->nh.ipv6h;
300 entry->rt_info.daddr = iph->daddr;
301 entry->rt_info.saddr = iph->saddr;
304 nskb = ipq_build_packet_message(entry, &status);
308 write_lock_bh(&queue_lock);
311 goto err_out_free_nskb;
313 if (queue_total >= queue_maxlen) {
317 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
318 "dropping packet(s). Dropped: %d\n", queue_total,
320 goto err_out_free_nskb;
323 /* netlink_unicast will either free the nskb or attach it to a socket */
324 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
326 queue_user_dropped++;
330 __ipq_enqueue_entry(entry);
332 write_unlock_bh(&queue_lock);
339 write_unlock_bh(&queue_lock);
347 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
350 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
352 if (v->data_len < sizeof(*user_iph))
354 diff = v->data_len - e->skb->len;
356 skb_trim(e->skb, v->data_len);
358 if (v->data_len > 0xFFFF)
360 if (diff > skb_tailroom(e->skb)) {
361 struct sk_buff *newskb;
363 newskb = skb_copy_expand(e->skb,
364 skb_headroom(e->skb),
367 if (newskb == NULL) {
368 printk(KERN_WARNING "ip6_queue: OOM "
369 "in mangle, dropping packet\n");
373 skb_set_owner_w(newskb, e->skb->sk);
377 skb_put(e->skb, diff);
379 if (!skb_ip_make_writable(&e->skb, v->data_len))
381 memcpy(e->skb->data, v->payload, v->data_len);
382 e->skb->nfcache |= NFC_ALTERED;
385 * Extra routing may needed on local out, as the QUEUE target never
386 * returns control to the table.
387 * Not a nice way to cmp, but works
389 if (e->info->hook == NF_IP_LOCAL_OUT) {
390 struct ipv6hdr *iph = e->skb->nh.ipv6h;
391 if (!ipv6_addr_equal(&iph->daddr, &e->rt_info.daddr) ||
392 !ipv6_addr_equal(&iph->saddr, &e->rt_info.saddr))
393 return ip6_route_me_harder(e->skb);
399 id_cmp(struct ipq_queue_entry *e, unsigned long id)
401 return (id == (unsigned long )e);
405 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
407 struct ipq_queue_entry *entry;
409 if (vmsg->value > NF_MAX_VERDICT)
412 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
416 int verdict = vmsg->value;
418 if (vmsg->data_len && vmsg->data_len == len)
419 if (ipq_mangle_ipv6(vmsg, entry) < 0)
422 ipq_issue_verdict(entry, verdict);
428 ipq_set_mode(unsigned char mode, unsigned int range)
432 write_lock_bh(&queue_lock);
433 status = __ipq_set_mode(mode, range);
434 write_unlock_bh(&queue_lock);
439 ipq_receive_peer(struct ipq_peer_msg *pmsg,
440 unsigned char type, unsigned int len)
444 if (len < sizeof(*pmsg))
449 status = ipq_set_mode(pmsg->msg.mode.value,
450 pmsg->msg.mode.range);
454 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
457 status = ipq_set_verdict(&pmsg->msg.verdict,
458 len - sizeof(*pmsg));
467 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
469 if (entry->info->indev)
470 if (entry->info->indev->ifindex == ifindex)
473 if (entry->info->outdev)
474 if (entry->info->outdev->ifindex == ifindex)
481 ipq_dev_drop(int ifindex)
483 struct ipq_queue_entry *entry;
485 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
486 ipq_issue_verdict(entry, NF_DROP);
489 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
492 ipq_rcv_skb(struct sk_buff *skb)
494 int status, type, pid, flags, nlmsglen, skblen;
495 struct nlmsghdr *nlh;
498 if (skblen < sizeof(*nlh))
501 nlh = (struct nlmsghdr *)skb->data;
502 nlmsglen = nlh->nlmsg_len;
503 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
506 pid = nlh->nlmsg_pid;
507 flags = nlh->nlmsg_flags;
509 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
510 RCV_SKB_FAIL(-EINVAL);
512 if (flags & MSG_TRUNC)
513 RCV_SKB_FAIL(-ECOMM);
515 type = nlh->nlmsg_type;
516 if (type < NLMSG_NOOP || type >= IPQM_MAX)
517 RCV_SKB_FAIL(-EINVAL);
519 if (type <= IPQM_BASE)
522 if (security_netlink_recv(skb))
523 RCV_SKB_FAIL(-EPERM);
525 write_lock_bh(&queue_lock);
528 if (peer_pid != pid) {
529 write_unlock_bh(&queue_lock);
530 RCV_SKB_FAIL(-EBUSY);
533 net_enable_timestamp();
537 write_unlock_bh(&queue_lock);
539 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
540 skblen - NLMSG_LENGTH(0));
542 RCV_SKB_FAIL(status);
544 if (flags & NLM_F_ACK)
545 netlink_ack(skb, nlh, 0);
550 ipq_rcv_sk(struct sock *sk, int len)
557 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
558 skb = skb_dequeue(&sk->sk_receive_queue);
567 ipq_rcv_dev_event(struct notifier_block *this,
568 unsigned long event, void *ptr)
570 struct net_device *dev = ptr;
572 /* Drop any packets associated with the downed device */
573 if (event == NETDEV_DOWN)
574 ipq_dev_drop(dev->ifindex);
578 static struct notifier_block ipq_dev_notifier = {
579 .notifier_call = ipq_rcv_dev_event,
583 ipq_rcv_nl_event(struct notifier_block *this,
584 unsigned long event, void *ptr)
586 struct netlink_notify *n = ptr;
588 if (event == NETLINK_URELEASE &&
589 n->protocol == NETLINK_IP6_FW && n->pid) {
590 write_lock_bh(&queue_lock);
591 if (n->pid == peer_pid)
593 write_unlock_bh(&queue_lock);
598 static struct notifier_block ipq_nl_notifier = {
599 .notifier_call = ipq_rcv_nl_event,
602 static struct ctl_table_header *ipq_sysctl_header;
604 static ctl_table ipq_table[] = {
606 .ctl_name = NET_IPQ_QMAX,
607 .procname = NET_IPQ_QMAX_NAME,
608 .data = &queue_maxlen,
609 .maxlen = sizeof(queue_maxlen),
611 .proc_handler = proc_dointvec
616 static ctl_table ipq_dir_table[] = {
618 .ctl_name = NET_IPV6,
626 static ctl_table ipq_root_table[] = {
631 .child = ipq_dir_table
637 ipq_get_info(char *buffer, char **start, off_t offset, int length)
641 read_lock_bh(&queue_lock);
643 len = sprintf(buffer,
647 "Queue length : %u\n"
648 "Queue max. length : %u\n"
649 "Queue dropped : %u\n"
650 "Netfilter dropped : %u\n",
659 read_unlock_bh(&queue_lock);
661 *start = buffer + offset;
671 init_or_cleanup(int init)
673 int status = -ENOMEM;
674 struct proc_dir_entry *proc;
679 netlink_register_notifier(&ipq_nl_notifier);
680 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
682 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
683 goto cleanup_netlink_notifier;
686 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
688 proc->owner = THIS_MODULE;
690 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
694 register_netdevice_notifier(&ipq_dev_notifier);
695 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
697 status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
699 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
705 nf_unregister_queue_handler(PF_INET6);
710 unregister_sysctl_table(ipq_sysctl_header);
711 unregister_netdevice_notifier(&ipq_dev_notifier);
712 proc_net_remove(IPQ_PROC_FS_NAME);
715 sock_release(ipqnl->sk_socket);
719 cleanup_netlink_notifier:
720 netlink_unregister_notifier(&ipq_nl_notifier);
724 static int __init init(void)
727 return init_or_cleanup(1);
730 static void __exit fini(void)
735 MODULE_DESCRIPTION("IPv6 packet queue handler");
736 MODULE_LICENSE("GPL");