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)
72 nf_reinject(entry->skb, entry->info, verdict);
78 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
80 if (queue_total >= queue_maxlen) {
82 printk(KERN_WARNING "ip6_queue: full at %d entries, "
83 "dropping packet(s).\n", queue_total);
86 list_add(&entry->list, &queue_list);
92 * Find and return a queued entry matched by cmpfn, or return the last
93 * entry if cmpfn is NULL.
95 static inline struct ipq_queue_entry *
96 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
100 list_for_each_prev(p, &queue_list) {
101 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
103 if (!cmpfn || cmpfn(entry, data))
110 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
112 list_del(&entry->list);
116 static inline struct ipq_queue_entry *
117 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
119 struct ipq_queue_entry *entry;
121 entry = __ipq_find_entry(cmpfn, data);
125 __ipq_dequeue_entry(entry);
131 __ipq_flush(int verdict)
133 struct ipq_queue_entry *entry;
135 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
136 ipq_issue_verdict(entry, verdict);
140 __ipq_set_mode(unsigned char mode, unsigned int range)
151 case IPQ_COPY_PACKET:
154 if (copy_range > 0xFFFF)
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 memcpy(pmsg->payload, entry->skb->data, data_len);
266 nlh->nlmsg_len = skb->tail - old_tail;
273 printk(KERN_ERR "ip6_queue: error creating packet message\n");
278 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
280 int status = -EINVAL;
281 struct sk_buff *nskb;
282 struct ipq_queue_entry *entry;
284 if (copy_mode == IPQ_COPY_NONE)
287 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
289 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
296 if (entry->info->hook == NF_IP_LOCAL_OUT) {
297 struct ipv6hdr *iph = skb->nh.ipv6h;
299 entry->rt_info.daddr = iph->daddr;
300 entry->rt_info.saddr = iph->saddr;
303 nskb = ipq_build_packet_message(entry, &status);
307 write_lock_bh(&queue_lock);
310 goto err_out_free_nskb;
312 /* netlink_unicast will either free the nskb or attach it to a socket */
313 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
317 status = __ipq_enqueue_entry(entry);
321 write_unlock_bh(&queue_lock);
328 write_unlock_bh(&queue_lock);
336 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
339 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
341 if (v->data_len < sizeof(*user_iph))
343 diff = v->data_len - e->skb->len;
345 skb_trim(e->skb, v->data_len);
347 if (v->data_len > 0xFFFF)
349 if (diff > skb_tailroom(e->skb)) {
350 struct sk_buff *newskb;
352 newskb = skb_copy_expand(e->skb,
353 skb_headroom(e->skb),
356 if (newskb == NULL) {
357 printk(KERN_WARNING "ip6_queue: OOM "
358 "in mangle, dropping packet\n");
362 skb_set_owner_w(newskb, e->skb->sk);
366 skb_put(e->skb, diff);
368 memcpy(e->skb->data, v->payload, v->data_len);
369 e->skb->nfcache |= NFC_ALTERED;
372 * Extra routing may needed on local out, as the QUEUE target never
373 * returns control to the table.
374 * Not a nice way to cmp, but works
376 if (e->info->hook == NF_IP_LOCAL_OUT) {
377 struct ipv6hdr *iph = e->skb->nh.ipv6h;
378 if (ipv6_addr_cmp(&iph->daddr, &e->rt_info.daddr) ||
379 ipv6_addr_cmp(&iph->saddr, &e->rt_info.saddr))
380 return ip6_route_me_harder(e->skb);
386 id_cmp(struct ipq_queue_entry *e, unsigned long id)
388 return (id == (unsigned long )e);
392 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
394 struct ipq_queue_entry *entry;
396 if (vmsg->value > NF_MAX_VERDICT)
399 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
403 int verdict = vmsg->value;
405 if (vmsg->data_len && vmsg->data_len == len)
406 if (ipq_mangle_ipv6(vmsg, entry) < 0)
409 ipq_issue_verdict(entry, verdict);
415 ipq_set_mode(unsigned char mode, unsigned int range)
419 write_lock_bh(&queue_lock);
420 status = __ipq_set_mode(mode, range);
421 write_unlock_bh(&queue_lock);
426 ipq_receive_peer(struct ipq_peer_msg *pmsg,
427 unsigned char type, unsigned int len)
431 if (len < sizeof(*pmsg))
436 status = ipq_set_mode(pmsg->msg.mode.value,
437 pmsg->msg.mode.range);
441 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
444 status = ipq_set_verdict(&pmsg->msg.verdict,
445 len - sizeof(*pmsg));
454 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
456 if (entry->info->indev)
457 if (entry->info->indev->ifindex == ifindex)
460 if (entry->info->outdev)
461 if (entry->info->outdev->ifindex == ifindex)
468 ipq_dev_drop(int ifindex)
470 struct ipq_queue_entry *entry;
472 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
473 ipq_issue_verdict(entry, NF_DROP);
476 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
479 ipq_rcv_skb(struct sk_buff *skb)
481 int status, type, pid, flags, nlmsglen, skblen;
482 struct nlmsghdr *nlh;
485 if (skblen < sizeof(*nlh))
488 nlh = (struct nlmsghdr *)skb->data;
489 nlmsglen = nlh->nlmsg_len;
490 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
493 pid = nlh->nlmsg_pid;
494 flags = nlh->nlmsg_flags;
496 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
497 RCV_SKB_FAIL(-EINVAL);
499 if (flags & MSG_TRUNC)
500 RCV_SKB_FAIL(-ECOMM);
502 type = nlh->nlmsg_type;
503 if (type < NLMSG_NOOP || type >= IPQM_MAX)
504 RCV_SKB_FAIL(-EINVAL);
506 if (type <= IPQM_BASE)
509 if(!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
510 RCV_SKB_FAIL(-EPERM);
512 write_lock_bh(&queue_lock);
515 if (peer_pid != pid) {
516 write_unlock_bh(&queue_lock);
517 RCV_SKB_FAIL(-EBUSY);
523 write_unlock_bh(&queue_lock);
525 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
526 nlmsglen - NLMSG_LENGTH(0));
528 RCV_SKB_FAIL(status);
530 if (flags & NLM_F_ACK)
531 netlink_ack(skb, nlh, 0);
536 ipq_rcv_sk(struct sock *sk, int len)
541 if (down_trylock(&ipqnl_sem))
544 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
551 } while (ipqnl && ipqnl->receive_queue.qlen);
555 ipq_rcv_dev_event(struct notifier_block *this,
556 unsigned long event, void *ptr)
558 struct net_device *dev = ptr;
560 /* Drop any packets associated with the downed device */
561 if (event == NETDEV_DOWN)
562 ipq_dev_drop(dev->ifindex);
566 static struct notifier_block ipq_dev_notifier = {
573 ipq_rcv_nl_event(struct notifier_block *this,
574 unsigned long event, void *ptr)
576 struct netlink_notify *n = ptr;
578 if (event == NETLINK_URELEASE &&
579 n->protocol == NETLINK_IP6_FW && n->pid) {
580 write_lock_bh(&queue_lock);
581 if (n->pid == peer_pid)
583 write_unlock_bh(&queue_lock);
588 static struct notifier_block ipq_nl_notifier = {
594 static struct ctl_table_header *ipq_sysctl_header;
596 static ctl_table ipq_table[] = {
597 { NET_IPQ_QMAX, NET_IPQ_QMAX_NAME, &queue_maxlen,
598 sizeof(queue_maxlen), 0644, NULL, proc_dointvec },
602 static ctl_table ipq_dir_table[] = {
603 {NET_IPV6, "ipv6", NULL, 0, 0555, ipq_table, 0, 0, 0, 0, 0},
607 static ctl_table ipq_root_table[] = {
608 {CTL_NET, "net", NULL, 0, 0555, ipq_dir_table, 0, 0, 0, 0, 0},
613 ipq_get_info(char *buffer, char **start, off_t offset, int length)
617 read_lock_bh(&queue_lock);
619 len = sprintf(buffer,
623 "Queue length : %u\n"
624 "Queue max. length : %u\n",
631 read_unlock_bh(&queue_lock);
633 *start = buffer + offset;
643 init_or_cleanup(int init)
645 int status = -ENOMEM;
646 struct proc_dir_entry *proc;
651 netlink_register_notifier(&ipq_nl_notifier);
652 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
654 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
655 goto cleanup_netlink_notifier;
658 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
660 proc->owner = THIS_MODULE;
662 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
666 register_netdevice_notifier(&ipq_dev_notifier);
667 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
669 status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
671 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
677 nf_unregister_queue_handler(PF_INET6);
678 br_write_lock_bh(BR_NETPROTO_LOCK);
679 br_write_unlock_bh(BR_NETPROTO_LOCK);
683 unregister_sysctl_table(ipq_sysctl_header);
684 unregister_netdevice_notifier(&ipq_dev_notifier);
685 proc_net_remove(IPQ_PROC_FS_NAME);
688 sock_release(ipqnl->socket);
692 cleanup_netlink_notifier:
693 netlink_unregister_notifier(&ipq_nl_notifier);
697 static int __init init(void)
700 return init_or_cleanup(1);
703 static void __exit fini(void)
708 MODULE_DESCRIPTION("IPv6 packet queue handler");
709 MODULE_LICENSE("GPL");