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 * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
13 * HEAVILY based in ipqueue.c by James Morris. It's just
14 * a little modified version of it, so he's nearly the
16 * Few changes needed, mainly the hard_routing code and
17 * the netlink socket protocol (we're NETLINK_IP6_FW).
18 * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
20 #include <linux/module.h>
21 #include <linux/skbuff.h>
22 #include <linux/init.h>
23 #include <linux/ipv6.h>
24 #include <linux/notifier.h>
25 #include <linux/netdevice.h>
26 #include <linux/netfilter.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/brlock.h>
30 #include <linux/sysctl.h>
31 #include <linux/proc_fs.h>
34 #include <net/ip6_route.h>
35 #include <linux/netfilter_ipv4/ip_queue.h>
36 #include <linux/netfilter_ipv4/ip_tables.h>
37 #include <linux/netfilter_ipv6/ip6_tables.h>
39 #define IPQ_QMAX_DEFAULT 1024
40 #define IPQ_PROC_FS_NAME "ip6_queue"
41 #define NET_IPQ_QMAX 2088
42 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
45 struct in6_addr daddr;
46 struct in6_addr saddr;
49 struct ipq_queue_entry {
50 struct list_head list;
53 struct ipq_rt_info rt_info;
56 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
58 static unsigned char copy_mode = IPQ_COPY_NONE;
59 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
60 static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
62 static unsigned int copy_range;
63 static unsigned int queue_total;
64 static struct sock *ipqnl;
65 static LIST_HEAD(queue_list);
66 static DECLARE_MUTEX(ipqnl_sem);
69 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
71 nf_reinject(entry->skb, entry->info, verdict);
76 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
78 if (queue_total >= queue_maxlen) {
80 printk(KERN_WARNING "ip6_queue: full at %d entries, "
81 "dropping packet(s).\n", queue_total);
84 list_add(&entry->list, &queue_list);
90 * Find and return a queued entry matched by cmpfn, or return the last
91 * entry if cmpfn is NULL.
93 static inline struct ipq_queue_entry *
94 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
98 list_for_each_prev(p, &queue_list) {
99 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
101 if (!cmpfn || cmpfn(entry, data))
108 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
110 list_del(&entry->list);
114 static inline struct ipq_queue_entry *
115 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
117 struct ipq_queue_entry *entry;
119 entry = __ipq_find_entry(cmpfn, data);
123 __ipq_dequeue_entry(entry);
129 __ipq_flush(int verdict)
131 struct ipq_queue_entry *entry;
133 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
134 ipq_issue_verdict(entry, verdict);
138 __ipq_set_mode(unsigned char mode, unsigned int range)
149 case IPQ_COPY_PACKET:
152 if (copy_range > 0xFFFF)
167 __ipq_set_mode(IPQ_COPY_NONE, 0);
168 __ipq_flush(NF_DROP);
171 static struct ipq_queue_entry *
172 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
174 struct ipq_queue_entry *entry;
176 write_lock_bh(&queue_lock);
177 entry = __ipq_find_dequeue_entry(cmpfn, data);
178 write_unlock_bh(&queue_lock);
183 ipq_flush(int verdict)
185 write_lock_bh(&queue_lock);
186 __ipq_flush(verdict);
187 write_unlock_bh(&queue_lock);
190 static struct sk_buff *
191 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
193 unsigned char *old_tail;
197 struct ipq_packet_msg *pmsg;
198 struct nlmsghdr *nlh;
200 read_lock_bh(&queue_lock);
205 size = NLMSG_SPACE(sizeof(*pmsg));
209 case IPQ_COPY_PACKET:
210 if (copy_range == 0 || copy_range > entry->skb->len)
211 data_len = entry->skb->len;
213 data_len = copy_range;
215 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
220 read_unlock_bh(&queue_lock);
224 read_unlock_bh(&queue_lock);
226 skb = alloc_skb(size, GFP_ATOMIC);
231 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
232 pmsg = NLMSG_DATA(nlh);
233 memset(pmsg, 0, sizeof(*pmsg));
235 pmsg->packet_id = (unsigned long )entry;
236 pmsg->data_len = data_len;
237 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
238 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
239 pmsg->mark = entry->skb->nfmark;
240 pmsg->hook = entry->info->hook;
241 pmsg->hw_protocol = entry->skb->protocol;
243 if (entry->info->indev)
244 strcpy(pmsg->indev_name, entry->info->indev->name);
246 pmsg->indev_name[0] = '\0';
248 if (entry->info->outdev)
249 strcpy(pmsg->outdev_name, entry->info->outdev->name);
251 pmsg->outdev_name[0] = '\0';
253 if (entry->info->indev && entry->skb->dev) {
254 pmsg->hw_type = entry->skb->dev->type;
255 if (entry->skb->dev->hard_header_parse)
257 entry->skb->dev->hard_header_parse(entry->skb,
262 memcpy(pmsg->payload, entry->skb->data, data_len);
264 nlh->nlmsg_len = skb->tail - old_tail;
271 printk(KERN_ERR "ip6_queue: error creating packet message\n");
276 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
278 int status = -EINVAL;
279 struct sk_buff *nskb;
280 struct ipq_queue_entry *entry;
282 if (copy_mode == IPQ_COPY_NONE)
285 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
287 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
294 if (entry->info->hook == NF_IP_LOCAL_OUT) {
295 struct ipv6hdr *iph = skb->nh.ipv6h;
297 entry->rt_info.daddr = iph->daddr;
298 entry->rt_info.saddr = iph->saddr;
301 nskb = ipq_build_packet_message(entry, &status);
305 write_lock_bh(&queue_lock);
310 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
314 status = __ipq_enqueue_entry(entry);
318 write_unlock_bh(&queue_lock);
322 write_unlock_bh(&queue_lock);
330 * Taken from net/ipv6/ip6_output.c
332 * We should use the one there, but is defined static
333 * so we put this just here and let the things as
336 * If that one is modified, this one should be modified too.
339 route6_me_harder(struct sk_buff *skb)
341 struct ipv6hdr *iph = skb->nh.ipv6h;
342 struct dst_entry *dst;
345 fl.proto = iph->nexthdr;
346 fl.fl6_dst = &iph->daddr;
347 fl.fl6_src = &iph->saddr;
348 fl.oif = skb->sk ? skb->sk->bound_dev_if : 0;
349 fl.fl6_flowlabel = 0;
350 fl.uli_u.ports.dport = 0;
351 fl.uli_u.ports.sport = 0;
353 dst = ip6_route_output(skb->sk, &fl);
357 printk(KERN_DEBUG "route6_me_harder: No more route.\n");
361 /* Drop old route. */
362 dst_release(skb->dst);
369 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
372 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
374 if (v->data_len < sizeof(*user_iph))
376 diff = v->data_len - e->skb->len;
378 skb_trim(e->skb, v->data_len);
380 if (v->data_len > 0xFFFF)
382 if (diff > skb_tailroom(e->skb)) {
383 struct sk_buff *newskb;
385 newskb = skb_copy_expand(e->skb,
386 skb_headroom(e->skb),
389 if (newskb == NULL) {
390 printk(KERN_WARNING "ip6_queue: OOM "
391 "in mangle, dropping packet\n");
395 skb_set_owner_w(newskb, e->skb->sk);
399 skb_put(e->skb, diff);
401 memcpy(e->skb->data, v->payload, v->data_len);
402 e->skb->nfcache |= NFC_ALTERED;
405 * Extra routing may needed on local out, as the QUEUE target never
406 * returns control to the table.
407 * Not a nice way to cmp, but works
409 if (e->info->hook == NF_IP_LOCAL_OUT) {
410 struct ipv6hdr *iph = e->skb->nh.ipv6h;
411 if (ipv6_addr_cmp(&iph->daddr, &e->rt_info.daddr) ||
412 ipv6_addr_cmp(&iph->saddr, &e->rt_info.saddr))
413 return route6_me_harder(e->skb);
419 id_cmp(struct ipq_queue_entry *e, unsigned long id)
421 return (id == (unsigned long )e);
425 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
427 struct ipq_queue_entry *entry;
429 if (vmsg->value > NF_MAX_VERDICT)
432 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
436 int verdict = vmsg->value;
438 if (vmsg->data_len && vmsg->data_len == len)
439 if (ipq_mangle_ipv6(vmsg, entry) < 0)
442 ipq_issue_verdict(entry, verdict);
448 ipq_set_mode(unsigned char mode, unsigned int range)
452 write_lock_bh(&queue_lock);
453 status = __ipq_set_mode(mode, range);
454 write_unlock_bh(&queue_lock);
459 ipq_receive_peer(struct ipq_peer_msg *pmsg,
460 unsigned char type, unsigned int len)
464 if (len < sizeof(*pmsg))
469 status = ipq_set_mode(pmsg->msg.mode.value,
470 pmsg->msg.mode.range);
474 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
477 status = ipq_set_verdict(&pmsg->msg.verdict,
478 len - sizeof(*pmsg));
487 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
489 if (entry->info->indev)
490 if (entry->info->indev->ifindex == ifindex)
493 if (entry->info->outdev)
494 if (entry->info->outdev->ifindex == ifindex)
501 ipq_dev_drop(int ifindex)
503 struct ipq_queue_entry *entry;
505 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
506 ipq_issue_verdict(entry, NF_DROP);
509 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
512 ipq_rcv_skb(struct sk_buff *skb)
514 int status, type, pid, flags, nlmsglen, skblen;
515 struct nlmsghdr *nlh;
518 if (skblen < sizeof(*nlh))
521 nlh = (struct nlmsghdr *)skb->data;
522 nlmsglen = nlh->nlmsg_len;
523 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
526 pid = nlh->nlmsg_pid;
527 flags = nlh->nlmsg_flags;
529 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
530 RCV_SKB_FAIL(-EINVAL);
532 if (flags & MSG_TRUNC)
533 RCV_SKB_FAIL(-ECOMM);
535 type = nlh->nlmsg_type;
536 if (type < NLMSG_NOOP || type >= IPQM_MAX)
537 RCV_SKB_FAIL(-EINVAL);
539 if (type <= IPQM_BASE)
542 if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
543 RCV_SKB_FAIL(-EPERM);
545 write_lock_bh(&queue_lock);
548 if (peer_pid != pid) {
549 write_unlock_bh(&queue_lock);
550 RCV_SKB_FAIL(-EBUSY);
556 write_unlock_bh(&queue_lock);
558 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
559 skblen - NLMSG_LENGTH(0));
561 RCV_SKB_FAIL(status);
563 if (flags & NLM_F_ACK)
564 netlink_ack(skb, nlh, 0);
569 ipq_rcv_sk(struct sock *sk, int len)
574 if (down_trylock(&ipqnl_sem))
577 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
584 } while (ipqnl && ipqnl->receive_queue.qlen);
588 ipq_rcv_dev_event(struct notifier_block *this,
589 unsigned long event, void *ptr)
591 struct net_device *dev = ptr;
593 /* Drop any packets associated with the downed device */
594 if (event == NETDEV_DOWN)
595 ipq_dev_drop(dev->ifindex);
599 static struct notifier_block ipq_dev_notifier = {
606 ipq_rcv_nl_event(struct notifier_block *this,
607 unsigned long event, void *ptr)
609 struct netlink_notify *n = ptr;
611 if (event == NETLINK_URELEASE &&
612 n->protocol == NETLINK_IP6_FW && n->pid) {
613 write_lock_bh(&queue_lock);
614 if (n->pid == peer_pid)
616 write_unlock_bh(&queue_lock);
621 static struct notifier_block ipq_nl_notifier = {
627 static int sysctl_maxlen = IPQ_QMAX_DEFAULT;
628 static struct ctl_table_header *ipq_sysctl_header;
630 static ctl_table ipq_table[] = {
631 { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &sysctl_maxlen,
632 sizeof(sysctl_maxlen), 0644, NULL, proc_dointvec },
636 static ctl_table ipq_dir_table[] = {
637 {NET_IPV6, "ipv6", NULL, 0, 0555, ipq_table, 0, 0, 0, 0, 0},
641 static ctl_table ipq_root_table[] = {
642 {CTL_NET, "net", NULL, 0, 0555, ipq_dir_table, 0, 0, 0, 0, 0},
647 ipq_get_info(char *buffer, char **start, off_t offset, int length)
651 read_lock_bh(&queue_lock);
653 len = sprintf(buffer,
657 "Queue length : %u\n"
658 "Queue max. length : %u\n",
665 read_unlock_bh(&queue_lock);
667 *start = buffer + offset;
677 init_or_cleanup(int init)
679 int status = -ENOMEM;
680 struct proc_dir_entry *proc;
685 netlink_register_notifier(&ipq_nl_notifier);
686 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
688 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
689 goto cleanup_netlink_notifier;
692 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
694 proc->owner = THIS_MODULE;
696 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
700 register_netdevice_notifier(&ipq_dev_notifier);
701 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
703 status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
705 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
711 nf_unregister_queue_handler(PF_INET6);
712 br_write_lock_bh(BR_NETPROTO_LOCK);
713 br_write_unlock_bh(BR_NETPROTO_LOCK);
717 unregister_sysctl_table(ipq_sysctl_header);
718 unregister_netdevice_notifier(&ipq_dev_notifier);
719 proc_net_remove(IPQ_PROC_FS_NAME);
722 sock_release(ipqnl->socket);
726 cleanup_netlink_notifier:
727 netlink_unregister_notifier(&ipq_nl_notifier);
731 static int __init init(void)
734 return init_or_cleanup(1);
737 static void __exit fini(void)
742 MODULE_DESCRIPTION("IPv6 packet queue handler");
743 MODULE_LICENSE("GPL");