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"
50 struct ipq_queue_entry {
51 struct list_head list;
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 DEFINE_RWLOCK(queue_lock);
62 static unsigned int copy_range;
63 static unsigned int queue_total;
64 static unsigned int queue_dropped = 0;
65 static unsigned int queue_user_dropped = 0;
66 static struct sock *ipqnl;
67 static LIST_HEAD(queue_list);
68 static DECLARE_MUTEX(ipqnl_sem);
71 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
74 nf_reinject(entry->skb, entry->info, verdict);
80 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
82 list_add(&entry->list, &queue_list);
87 * Find and return a queued entry matched by cmpfn, or return the last
88 * entry if cmpfn is NULL.
90 static inline struct ipq_queue_entry *
91 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
95 list_for_each_prev(p, &queue_list) {
96 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
98 if (!cmpfn || cmpfn(entry, data))
105 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
107 list_del(&entry->list);
111 static inline struct ipq_queue_entry *
112 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
114 struct ipq_queue_entry *entry;
116 entry = __ipq_find_entry(cmpfn, data);
120 __ipq_dequeue_entry(entry);
126 __ipq_flush(int verdict)
128 struct ipq_queue_entry *entry;
130 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
131 ipq_issue_verdict(entry, verdict);
135 __ipq_set_mode(unsigned char mode, unsigned int range)
146 case IPQ_COPY_PACKET:
149 if (copy_range > 0xFFFF)
164 net_disable_timestamp();
165 __ipq_set_mode(IPQ_COPY_NONE, 0);
166 __ipq_flush(NF_DROP);
169 static struct ipq_queue_entry *
170 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
172 struct ipq_queue_entry *entry;
174 write_lock_bh(&queue_lock);
175 entry = __ipq_find_dequeue_entry(cmpfn, data);
176 write_unlock_bh(&queue_lock);
181 ipq_flush(int verdict)
183 write_lock_bh(&queue_lock);
184 __ipq_flush(verdict);
185 write_unlock_bh(&queue_lock);
188 static struct sk_buff *
189 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
191 unsigned char *old_tail;
195 struct ipq_packet_msg *pmsg;
196 struct nlmsghdr *nlh;
198 read_lock_bh(&queue_lock);
203 size = NLMSG_SPACE(sizeof(*pmsg));
207 case IPQ_COPY_PACKET:
208 if (entry->skb->ip_summed == CHECKSUM_HW &&
209 (*errp = skb_checksum_help(entry->skb,
210 entry->info->outdev == NULL))) {
211 read_unlock_bh(&queue_lock);
214 if (copy_range == 0 || copy_range > entry->skb->len)
215 data_len = entry->skb->len;
217 data_len = copy_range;
219 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
224 read_unlock_bh(&queue_lock);
228 read_unlock_bh(&queue_lock);
230 skb = alloc_skb(size, GFP_ATOMIC);
235 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
236 pmsg = NLMSG_DATA(nlh);
237 memset(pmsg, 0, sizeof(*pmsg));
239 pmsg->packet_id = (unsigned long )entry;
240 pmsg->data_len = data_len;
241 pmsg->timestamp_sec = entry->skb->tstamp.off_sec;
242 pmsg->timestamp_usec = entry->skb->tstamp.off_usec;
243 pmsg->mark = entry->skb->nfmark;
244 pmsg->hook = entry->info->hook;
245 pmsg->hw_protocol = entry->skb->protocol;
247 if (entry->info->indev)
248 strcpy(pmsg->indev_name, entry->info->indev->name);
250 pmsg->indev_name[0] = '\0';
252 if (entry->info->outdev)
253 strcpy(pmsg->outdev_name, entry->info->outdev->name);
255 pmsg->outdev_name[0] = '\0';
257 if (entry->info->indev && entry->skb->dev) {
258 pmsg->hw_type = entry->skb->dev->type;
259 if (entry->skb->dev->hard_header_parse)
261 entry->skb->dev->hard_header_parse(entry->skb,
266 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
269 nlh->nlmsg_len = skb->tail - old_tail;
276 printk(KERN_ERR "ip6_queue: error creating packet message\n");
281 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
282 unsigned int queuenum, void *data)
284 int status = -EINVAL;
285 struct sk_buff *nskb;
286 struct ipq_queue_entry *entry;
288 if (copy_mode == IPQ_COPY_NONE)
291 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
293 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
300 nskb = ipq_build_packet_message(entry, &status);
304 write_lock_bh(&queue_lock);
307 goto err_out_free_nskb;
309 if (queue_total >= queue_maxlen) {
313 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
314 "dropping packet(s). Dropped: %d\n", queue_total,
316 goto err_out_free_nskb;
319 /* netlink_unicast will either free the nskb or attach it to a socket */
320 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
322 queue_user_dropped++;
326 __ipq_enqueue_entry(entry);
328 write_unlock_bh(&queue_lock);
335 write_unlock_bh(&queue_lock);
343 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
346 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
348 if (v->data_len < sizeof(*user_iph))
350 diff = v->data_len - e->skb->len;
352 skb_trim(e->skb, v->data_len);
354 if (v->data_len > 0xFFFF)
356 if (diff > skb_tailroom(e->skb)) {
357 struct sk_buff *newskb;
359 newskb = skb_copy_expand(e->skb,
360 skb_headroom(e->skb),
363 if (newskb == NULL) {
364 printk(KERN_WARNING "ip6_queue: OOM "
365 "in mangle, dropping packet\n");
369 skb_set_owner_w(newskb, e->skb->sk);
373 skb_put(e->skb, diff);
375 if (!skb_make_writable(&e->skb, v->data_len))
377 memcpy(e->skb->data, v->payload, v->data_len);
378 e->skb->ip_summed = CHECKSUM_NONE;
384 id_cmp(struct ipq_queue_entry *e, unsigned long id)
386 return (id == (unsigned long )e);
390 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
392 struct ipq_queue_entry *entry;
394 if (vmsg->value > NF_MAX_VERDICT)
397 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
401 int verdict = vmsg->value;
403 if (vmsg->data_len && vmsg->data_len == len)
404 if (ipq_mangle_ipv6(vmsg, entry) < 0)
407 ipq_issue_verdict(entry, verdict);
413 ipq_set_mode(unsigned char mode, unsigned int range)
417 write_lock_bh(&queue_lock);
418 status = __ipq_set_mode(mode, range);
419 write_unlock_bh(&queue_lock);
424 ipq_receive_peer(struct ipq_peer_msg *pmsg,
425 unsigned char type, unsigned int len)
429 if (len < sizeof(*pmsg))
434 status = ipq_set_mode(pmsg->msg.mode.value,
435 pmsg->msg.mode.range);
439 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
442 status = ipq_set_verdict(&pmsg->msg.verdict,
443 len - sizeof(*pmsg));
452 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
454 if (entry->info->indev)
455 if (entry->info->indev->ifindex == ifindex)
458 if (entry->info->outdev)
459 if (entry->info->outdev->ifindex == ifindex)
466 ipq_dev_drop(int ifindex)
468 struct ipq_queue_entry *entry;
470 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
471 ipq_issue_verdict(entry, NF_DROP);
474 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
477 ipq_rcv_skb(struct sk_buff *skb)
479 int status, type, pid, flags, nlmsglen, skblen;
480 struct nlmsghdr *nlh;
483 if (skblen < sizeof(*nlh))
486 nlh = (struct nlmsghdr *)skb->data;
487 nlmsglen = nlh->nlmsg_len;
488 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
491 pid = nlh->nlmsg_pid;
492 flags = nlh->nlmsg_flags;
494 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
495 RCV_SKB_FAIL(-EINVAL);
497 if (flags & MSG_TRUNC)
498 RCV_SKB_FAIL(-ECOMM);
500 type = nlh->nlmsg_type;
501 if (type < NLMSG_NOOP || type >= IPQM_MAX)
502 RCV_SKB_FAIL(-EINVAL);
504 if (type <= IPQM_BASE)
507 if (security_netlink_recv(skb))
508 RCV_SKB_FAIL(-EPERM);
510 write_lock_bh(&queue_lock);
513 if (peer_pid != pid) {
514 write_unlock_bh(&queue_lock);
515 RCV_SKB_FAIL(-EBUSY);
518 net_enable_timestamp();
522 write_unlock_bh(&queue_lock);
524 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
525 skblen - NLMSG_LENGTH(0));
527 RCV_SKB_FAIL(status);
529 if (flags & NLM_F_ACK)
530 netlink_ack(skb, nlh, 0);
535 ipq_rcv_sk(struct sock *sk, int len)
542 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
543 skb = skb_dequeue(&sk->sk_receive_queue);
552 ipq_rcv_dev_event(struct notifier_block *this,
553 unsigned long event, void *ptr)
555 struct net_device *dev = ptr;
557 /* Drop any packets associated with the downed device */
558 if (event == NETDEV_DOWN)
559 ipq_dev_drop(dev->ifindex);
563 static struct notifier_block ipq_dev_notifier = {
564 .notifier_call = ipq_rcv_dev_event,
568 ipq_rcv_nl_event(struct notifier_block *this,
569 unsigned long event, void *ptr)
571 struct netlink_notify *n = ptr;
573 if (event == NETLINK_URELEASE &&
574 n->protocol == NETLINK_IP6_FW && n->pid) {
575 write_lock_bh(&queue_lock);
576 if (n->pid == peer_pid)
578 write_unlock_bh(&queue_lock);
583 static struct notifier_block ipq_nl_notifier = {
584 .notifier_call = ipq_rcv_nl_event,
587 static struct ctl_table_header *ipq_sysctl_header;
589 static ctl_table ipq_table[] = {
591 .ctl_name = NET_IPQ_QMAX,
592 .procname = NET_IPQ_QMAX_NAME,
593 .data = &queue_maxlen,
594 .maxlen = sizeof(queue_maxlen),
596 .proc_handler = proc_dointvec
601 static ctl_table ipq_dir_table[] = {
603 .ctl_name = NET_IPV6,
611 static ctl_table ipq_root_table[] = {
616 .child = ipq_dir_table
622 ipq_get_info(char *buffer, char **start, off_t offset, int length)
626 read_lock_bh(&queue_lock);
628 len = sprintf(buffer,
632 "Queue length : %u\n"
633 "Queue max. length : %u\n"
634 "Queue dropped : %u\n"
635 "Netfilter dropped : %u\n",
644 read_unlock_bh(&queue_lock);
646 *start = buffer + offset;
655 static struct nf_queue_handler nfqh = {
657 .outfn = &ipq_enqueue_packet,
661 init_or_cleanup(int init)
663 int status = -ENOMEM;
664 struct proc_dir_entry *proc;
669 netlink_register_notifier(&ipq_nl_notifier);
670 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, 0, ipq_rcv_sk,
673 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
674 goto cleanup_netlink_notifier;
677 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
679 proc->owner = THIS_MODULE;
681 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
685 register_netdevice_notifier(&ipq_dev_notifier);
686 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
688 status = nf_register_queue_handler(PF_INET6, &nfqh);
690 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
696 nf_unregister_queue_handlers(&nfqh);
701 unregister_sysctl_table(ipq_sysctl_header);
702 unregister_netdevice_notifier(&ipq_dev_notifier);
703 proc_net_remove(IPQ_PROC_FS_NAME);
706 sock_release(ipqnl->sk_socket);
710 cleanup_netlink_notifier:
711 netlink_unregister_notifier(&ipq_nl_notifier);
715 static int __init init(void)
718 return init_or_cleanup(1);
721 static void __exit fini(void)
726 MODULE_DESCRIPTION("IPv6 packet queue handler");
727 MODULE_LICENSE("GPL");