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]
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/init.h>
27 #include <linux/ipv6.h>
28 #include <linux/notifier.h>
29 #include <linux/netdevice.h>
30 #include <linux/netfilter.h>
31 #include <linux/netlink.h>
32 #include <linux/spinlock.h>
33 #include <linux/sysctl.h>
34 #include <linux/proc_fs.h>
37 #include <net/ip6_route.h>
38 #include <linux/netfilter_ipv4/ip_queue.h>
39 #include <linux/netfilter_ipv4/ip_tables.h>
40 #include <linux/netfilter_ipv6/ip6_tables.h>
42 #define IPQ_QMAX_DEFAULT 1024
43 #define IPQ_PROC_FS_NAME "ip6_queue"
44 #define NET_IPQ_QMAX 2088
45 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
48 struct in6_addr daddr;
49 struct in6_addr saddr;
52 struct ipq_queue_entry {
53 struct list_head list;
56 struct ipq_rt_info rt_info;
59 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
61 static unsigned char copy_mode = IPQ_COPY_NONE;
62 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
63 static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
65 static unsigned int copy_range;
66 static unsigned int queue_total;
67 static struct sock *ipqnl;
68 static LIST_HEAD(queue_list);
69 static DECLARE_MUTEX(ipqnl_sem);
72 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
74 nf_reinject(entry->skb, entry->info, verdict);
79 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
81 if (queue_total >= queue_maxlen) {
83 printk(KERN_WARNING "ip6_queue: full at %d entries, "
84 "dropping packet(s).\n", queue_total);
87 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 __ipq_set_mode(IPQ_COPY_NONE, 0);
171 __ipq_flush(NF_DROP);
174 static struct ipq_queue_entry *
175 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
177 struct ipq_queue_entry *entry;
179 write_lock_bh(&queue_lock);
180 entry = __ipq_find_dequeue_entry(cmpfn, data);
181 write_unlock_bh(&queue_lock);
186 ipq_flush(int verdict)
188 write_lock_bh(&queue_lock);
189 __ipq_flush(verdict);
190 write_unlock_bh(&queue_lock);
193 static struct sk_buff *
194 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
196 unsigned char *old_tail;
200 struct ipq_packet_msg *pmsg;
201 struct nlmsghdr *nlh;
203 read_lock_bh(&queue_lock);
208 size = NLMSG_SPACE(sizeof(*pmsg));
212 case IPQ_COPY_PACKET:
213 if (copy_range == 0 || copy_range > entry->skb->len)
214 data_len = entry->skb->len;
216 data_len = copy_range;
218 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
223 read_unlock_bh(&queue_lock);
227 read_unlock_bh(&queue_lock);
229 skb = alloc_skb(size, GFP_ATOMIC);
234 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
235 pmsg = NLMSG_DATA(nlh);
236 memset(pmsg, 0, sizeof(*pmsg));
238 pmsg->packet_id = (unsigned long )entry;
239 pmsg->data_len = data_len;
240 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
241 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
242 pmsg->mark = entry->skb->nfmark;
243 pmsg->hook = entry->info->hook;
244 pmsg->hw_protocol = entry->skb->protocol;
246 if (entry->info->indev)
247 strcpy(pmsg->indev_name, entry->info->indev->name);
249 pmsg->indev_name[0] = '\0';
251 if (entry->info->outdev)
252 strcpy(pmsg->outdev_name, entry->info->outdev->name);
254 pmsg->outdev_name[0] = '\0';
256 if (entry->info->indev && entry->skb->dev) {
257 pmsg->hw_type = entry->skb->dev->type;
258 if (entry->skb->dev->hard_header_parse)
260 entry->skb->dev->hard_header_parse(entry->skb,
265 memcpy(pmsg->payload, entry->skb->data, 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 /* netlink_unicast will either free the nskb or attach it to a socket */
314 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
318 status = __ipq_enqueue_entry(entry);
322 write_unlock_bh(&queue_lock);
329 write_unlock_bh(&queue_lock);
337 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
340 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
342 if (v->data_len < sizeof(*user_iph))
344 diff = v->data_len - e->skb->len;
346 skb_trim(e->skb, v->data_len);
348 if (v->data_len > 0xFFFF)
350 if (diff > skb_tailroom(e->skb)) {
351 struct sk_buff *newskb;
353 newskb = skb_copy_expand(e->skb,
354 skb_headroom(e->skb),
357 if (newskb == NULL) {
358 printk(KERN_WARNING "ip6_queue: OOM "
359 "in mangle, dropping packet\n");
363 skb_set_owner_w(newskb, e->skb->sk);
367 skb_put(e->skb, diff);
369 memcpy(e->skb->data, v->payload, v->data_len);
370 e->skb->nfcache |= NFC_ALTERED;
373 * Extra routing may needed on local out, as the QUEUE target never
374 * returns control to the table.
375 * Not a nice way to cmp, but works
377 if (e->info->hook == NF_IP_LOCAL_OUT) {
378 struct ipv6hdr *iph = e->skb->nh.ipv6h;
379 if (ipv6_addr_cmp(&iph->daddr, &e->rt_info.daddr) ||
380 ipv6_addr_cmp(&iph->saddr, &e->rt_info.saddr))
381 return ip6_route_me_harder(e->skb);
387 id_cmp(struct ipq_queue_entry *e, unsigned long id)
389 return (id == (unsigned long )e);
393 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
395 struct ipq_queue_entry *entry;
397 if (vmsg->value > NF_MAX_VERDICT)
400 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
404 int verdict = vmsg->value;
406 if (vmsg->data_len && vmsg->data_len == len)
407 if (ipq_mangle_ipv6(vmsg, entry) < 0)
410 ipq_issue_verdict(entry, verdict);
416 ipq_set_mode(unsigned char mode, unsigned int range)
420 write_lock_bh(&queue_lock);
421 status = __ipq_set_mode(mode, range);
422 write_unlock_bh(&queue_lock);
427 ipq_receive_peer(struct ipq_peer_msg *pmsg,
428 unsigned char type, unsigned int len)
432 if (len < sizeof(*pmsg))
437 status = ipq_set_mode(pmsg->msg.mode.value,
438 pmsg->msg.mode.range);
442 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
445 status = ipq_set_verdict(&pmsg->msg.verdict,
446 len - sizeof(*pmsg));
455 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
457 if (entry->info->indev)
458 if (entry->info->indev->ifindex == ifindex)
461 if (entry->info->outdev)
462 if (entry->info->outdev->ifindex == ifindex)
469 ipq_dev_drop(int ifindex)
471 struct ipq_queue_entry *entry;
473 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
474 ipq_issue_verdict(entry, NF_DROP);
477 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
480 ipq_rcv_skb(struct sk_buff *skb)
482 int status, type, pid, flags, nlmsglen, skblen;
483 struct nlmsghdr *nlh;
486 if (skblen < sizeof(*nlh))
489 nlh = (struct nlmsghdr *)skb->data;
490 nlmsglen = nlh->nlmsg_len;
491 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
494 pid = nlh->nlmsg_pid;
495 flags = nlh->nlmsg_flags;
497 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
498 RCV_SKB_FAIL(-EINVAL);
500 if (flags & MSG_TRUNC)
501 RCV_SKB_FAIL(-ECOMM);
503 type = nlh->nlmsg_type;
504 if (type < NLMSG_NOOP || type >= IPQM_MAX)
505 RCV_SKB_FAIL(-EINVAL);
507 if (type <= IPQM_BASE)
510 if (security_netlink_recv(skb))
511 RCV_SKB_FAIL(-EPERM);
513 write_lock_bh(&queue_lock);
516 if (peer_pid != pid) {
517 write_unlock_bh(&queue_lock);
518 RCV_SKB_FAIL(-EBUSY);
524 write_unlock_bh(&queue_lock);
526 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
527 skblen - NLMSG_LENGTH(0));
529 RCV_SKB_FAIL(status);
531 if (flags & NLM_F_ACK)
532 netlink_ack(skb, nlh, 0);
537 ipq_rcv_sk(struct sock *sk, int len)
542 if (down_trylock(&ipqnl_sem))
545 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
552 } while (ipqnl && ipqnl->sk_receive_queue.qlen);
556 ipq_rcv_dev_event(struct notifier_block *this,
557 unsigned long event, void *ptr)
559 struct net_device *dev = ptr;
561 /* Drop any packets associated with the downed device */
562 if (event == NETDEV_DOWN)
563 ipq_dev_drop(dev->ifindex);
567 static struct notifier_block ipq_dev_notifier = {
568 .notifier_call = ipq_rcv_dev_event,
572 ipq_rcv_nl_event(struct notifier_block *this,
573 unsigned long event, void *ptr)
575 struct netlink_notify *n = ptr;
577 if (event == NETLINK_URELEASE &&
578 n->protocol == NETLINK_IP6_FW && n->pid) {
579 write_lock_bh(&queue_lock);
580 if (n->pid == peer_pid)
582 write_unlock_bh(&queue_lock);
587 static struct notifier_block ipq_nl_notifier = {
588 .notifier_call = ipq_rcv_nl_event,
591 static struct ctl_table_header *ipq_sysctl_header;
593 static ctl_table ipq_table[] = {
595 .ctl_name = NET_IPQ_QMAX,
596 .procname = NET_IPQ_QMAX_NAME,
597 .data = &queue_maxlen,
598 .maxlen = sizeof(queue_maxlen),
600 .proc_handler = proc_dointvec
605 static ctl_table ipq_dir_table[] = {
607 .ctl_name = NET_IPV6,
615 static ctl_table ipq_root_table[] = {
620 .child = ipq_dir_table
626 ipq_get_info(char *buffer, char **start, off_t offset, int length)
630 read_lock_bh(&queue_lock);
632 len = sprintf(buffer,
636 "Queue length : %u\n"
637 "Queue max. length : %u\n",
644 read_unlock_bh(&queue_lock);
646 *start = buffer + offset;
656 init_or_cleanup(int init)
658 int status = -ENOMEM;
659 struct proc_dir_entry *proc;
664 netlink_register_notifier(&ipq_nl_notifier);
665 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
667 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
668 goto cleanup_netlink_notifier;
671 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
673 proc->owner = THIS_MODULE;
675 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
679 register_netdevice_notifier(&ipq_dev_notifier);
680 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
682 status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
684 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
690 nf_unregister_queue_handler(PF_INET6);
695 unregister_sysctl_table(ipq_sysctl_header);
696 unregister_netdevice_notifier(&ipq_dev_notifier);
697 proc_net_remove(IPQ_PROC_FS_NAME);
700 sock_release(ipqnl->sk_socket);
704 cleanup_netlink_notifier:
705 netlink_unregister_notifier(&ipq_nl_notifier);
709 static int __init init(void)
712 return init_or_cleanup(1);
715 static void __exit fini(void)
720 MODULE_DESCRIPTION("IPv6 packet queue handler");
721 MODULE_LICENSE("GPL");