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 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/init.h>
18 #include <linux/ipv6.h>
19 #include <linux/notifier.h>
20 #include <linux/netdevice.h>
21 #include <linux/netfilter.h>
22 #include <linux/netlink.h>
23 #include <linux/spinlock.h>
24 #include <linux/sysctl.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/mutex.h>
28 #include <net/net_namespace.h>
31 #include <net/ip6_route.h>
32 #include <net/netfilter/nf_queue.h>
33 #include <linux/netfilter_ipv4/ip_queue.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
37 #define IPQ_QMAX_DEFAULT 1024
38 #define IPQ_PROC_FS_NAME "ip6_queue"
39 #define NET_IPQ_QMAX 2088
40 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
42 struct ipq_queue_entry {
43 struct list_head list;
48 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
50 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
51 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
52 static DEFINE_RWLOCK(queue_lock);
53 static int peer_pid __read_mostly;
54 static unsigned int copy_range __read_mostly;
55 static unsigned int queue_total;
56 static unsigned int queue_dropped = 0;
57 static unsigned int queue_user_dropped = 0;
58 static struct sock *ipqnl __read_mostly;
59 static LIST_HEAD(queue_list);
60 static DEFINE_MUTEX(ipqnl_mutex);
63 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
66 nf_reinject(entry->skb, entry->info, verdict);
72 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
74 list_add_tail(&entry->list, &queue_list);
79 __ipq_set_mode(unsigned char mode, unsigned int range)
93 if (copy_range > 0xFFFF)
104 static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data);
110 net_disable_timestamp();
111 __ipq_set_mode(IPQ_COPY_NONE, 0);
112 __ipq_flush(NULL, 0);
115 static struct ipq_queue_entry *
116 ipq_find_dequeue_entry(unsigned long id)
118 struct ipq_queue_entry *entry = NULL, *i;
120 write_lock_bh(&queue_lock);
122 list_for_each_entry(i, &queue_list, list) {
123 if ((unsigned long)i == id) {
130 list_del(&entry->list);
134 write_unlock_bh(&queue_lock);
139 __ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
141 struct ipq_queue_entry *entry, *next;
143 list_for_each_entry_safe(entry, next, &queue_list, list) {
144 if (!cmpfn || cmpfn(entry, data)) {
145 list_del(&entry->list);
147 ipq_issue_verdict(entry, NF_DROP);
153 ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
155 write_lock_bh(&queue_lock);
156 __ipq_flush(cmpfn, data);
157 write_unlock_bh(&queue_lock);
160 static struct sk_buff *
161 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
163 sk_buff_data_t old_tail;
167 struct ipq_packet_msg *pmsg;
168 struct nlmsghdr *nlh;
171 read_lock_bh(&queue_lock);
176 size = NLMSG_SPACE(sizeof(*pmsg));
180 case IPQ_COPY_PACKET:
181 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
182 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
183 (*errp = skb_checksum_help(entry->skb))) {
184 read_unlock_bh(&queue_lock);
187 if (copy_range == 0 || copy_range > entry->skb->len)
188 data_len = entry->skb->len;
190 data_len = copy_range;
192 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
197 read_unlock_bh(&queue_lock);
201 read_unlock_bh(&queue_lock);
203 skb = alloc_skb(size, GFP_ATOMIC);
207 old_tail = skb->tail;
208 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
209 pmsg = NLMSG_DATA(nlh);
210 memset(pmsg, 0, sizeof(*pmsg));
212 pmsg->packet_id = (unsigned long )entry;
213 pmsg->data_len = data_len;
214 tv = ktime_to_timeval(entry->skb->tstamp);
215 pmsg->timestamp_sec = tv.tv_sec;
216 pmsg->timestamp_usec = tv.tv_usec;
217 pmsg->mark = entry->skb->mark;
218 pmsg->hook = entry->info->hook;
219 pmsg->hw_protocol = entry->skb->protocol;
221 if (entry->info->indev)
222 strcpy(pmsg->indev_name, entry->info->indev->name);
224 pmsg->indev_name[0] = '\0';
226 if (entry->info->outdev)
227 strcpy(pmsg->outdev_name, entry->info->outdev->name);
229 pmsg->outdev_name[0] = '\0';
231 if (entry->info->indev && entry->skb->dev) {
232 pmsg->hw_type = entry->skb->dev->type;
233 pmsg->hw_addrlen = dev_parse_header(entry->skb, pmsg->hw_addr);
237 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
240 nlh->nlmsg_len = skb->tail - old_tail;
247 printk(KERN_ERR "ip6_queue: error creating packet message\n");
252 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
253 unsigned int queuenum)
255 int status = -EINVAL;
256 struct sk_buff *nskb;
257 struct ipq_queue_entry *entry;
259 if (copy_mode == IPQ_COPY_NONE)
262 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
264 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
271 nskb = ipq_build_packet_message(entry, &status);
275 write_lock_bh(&queue_lock);
278 goto err_out_free_nskb;
280 if (queue_total >= queue_maxlen) {
284 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
285 "dropping packet(s). Dropped: %d\n", queue_total,
287 goto err_out_free_nskb;
290 /* netlink_unicast will either free the nskb or attach it to a socket */
291 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
293 queue_user_dropped++;
297 __ipq_enqueue_entry(entry);
299 write_unlock_bh(&queue_lock);
306 write_unlock_bh(&queue_lock);
314 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
318 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
320 if (v->data_len < sizeof(*user_iph))
322 diff = v->data_len - e->skb->len;
324 if (pskb_trim(e->skb, v->data_len))
326 } else if (diff > 0) {
327 if (v->data_len > 0xFFFF)
329 if (diff > skb_tailroom(e->skb)) {
330 err = pskb_expand_head(e->skb, 0,
331 diff - skb_tailroom(e->skb),
334 printk(KERN_WARNING "ip6_queue: OOM "
335 "in mangle, dropping packet\n");
339 skb_put(e->skb, diff);
341 if (!skb_make_writable(e->skb, v->data_len))
343 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
344 e->skb->ip_summed = CHECKSUM_NONE;
350 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
352 struct ipq_queue_entry *entry;
354 if (vmsg->value > NF_MAX_VERDICT)
357 entry = ipq_find_dequeue_entry(vmsg->id);
361 int verdict = vmsg->value;
363 if (vmsg->data_len && vmsg->data_len == len)
364 if (ipq_mangle_ipv6(vmsg, entry) < 0)
367 ipq_issue_verdict(entry, verdict);
373 ipq_set_mode(unsigned char mode, unsigned int range)
377 write_lock_bh(&queue_lock);
378 status = __ipq_set_mode(mode, range);
379 write_unlock_bh(&queue_lock);
384 ipq_receive_peer(struct ipq_peer_msg *pmsg,
385 unsigned char type, unsigned int len)
389 if (len < sizeof(*pmsg))
394 status = ipq_set_mode(pmsg->msg.mode.value,
395 pmsg->msg.mode.range);
399 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
402 status = ipq_set_verdict(&pmsg->msg.verdict,
403 len - sizeof(*pmsg));
412 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
414 if (entry->info->indev)
415 if (entry->info->indev->ifindex == ifindex)
418 if (entry->info->outdev)
419 if (entry->info->outdev->ifindex == ifindex)
421 #ifdef CONFIG_BRIDGE_NETFILTER
422 if (entry->skb->nf_bridge) {
423 if (entry->skb->nf_bridge->physindev &&
424 entry->skb->nf_bridge->physindev->ifindex == ifindex)
426 if (entry->skb->nf_bridge->physoutdev &&
427 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
435 ipq_dev_drop(int ifindex)
437 ipq_flush(dev_cmp, ifindex);
440 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
443 __ipq_rcv_skb(struct sk_buff *skb)
445 int status, type, pid, flags, nlmsglen, skblen;
446 struct nlmsghdr *nlh;
449 if (skblen < sizeof(*nlh))
452 nlh = nlmsg_hdr(skb);
453 nlmsglen = nlh->nlmsg_len;
454 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
457 pid = nlh->nlmsg_pid;
458 flags = nlh->nlmsg_flags;
460 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
461 RCV_SKB_FAIL(-EINVAL);
463 if (flags & MSG_TRUNC)
464 RCV_SKB_FAIL(-ECOMM);
466 type = nlh->nlmsg_type;
467 if (type < NLMSG_NOOP || type >= IPQM_MAX)
468 RCV_SKB_FAIL(-EINVAL);
470 if (type <= IPQM_BASE)
473 if (security_netlink_recv(skb, CAP_NET_ADMIN))
474 RCV_SKB_FAIL(-EPERM);
476 write_lock_bh(&queue_lock);
479 if (peer_pid != pid) {
480 write_unlock_bh(&queue_lock);
481 RCV_SKB_FAIL(-EBUSY);
484 net_enable_timestamp();
488 write_unlock_bh(&queue_lock);
490 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
491 nlmsglen - NLMSG_LENGTH(0));
493 RCV_SKB_FAIL(status);
495 if (flags & NLM_F_ACK)
496 netlink_ack(skb, nlh, 0);
501 ipq_rcv_skb(struct sk_buff *skb)
503 mutex_lock(&ipqnl_mutex);
505 mutex_unlock(&ipqnl_mutex);
509 ipq_rcv_dev_event(struct notifier_block *this,
510 unsigned long event, void *ptr)
512 struct net_device *dev = ptr;
514 if (dev->nd_net != &init_net)
517 /* Drop any packets associated with the downed device */
518 if (event == NETDEV_DOWN)
519 ipq_dev_drop(dev->ifindex);
523 static struct notifier_block ipq_dev_notifier = {
524 .notifier_call = ipq_rcv_dev_event,
528 ipq_rcv_nl_event(struct notifier_block *this,
529 unsigned long event, void *ptr)
531 struct netlink_notify *n = ptr;
533 if (event == NETLINK_URELEASE &&
534 n->protocol == NETLINK_IP6_FW && n->pid) {
535 write_lock_bh(&queue_lock);
536 if ((n->net == &init_net) && (n->pid == peer_pid))
538 write_unlock_bh(&queue_lock);
543 static struct notifier_block ipq_nl_notifier = {
544 .notifier_call = ipq_rcv_nl_event,
547 static struct ctl_table_header *ipq_sysctl_header;
549 static ctl_table ipq_table[] = {
551 .ctl_name = NET_IPQ_QMAX,
552 .procname = NET_IPQ_QMAX_NAME,
553 .data = &queue_maxlen,
554 .maxlen = sizeof(queue_maxlen),
556 .proc_handler = proc_dointvec
561 static ctl_table ipq_dir_table[] = {
563 .ctl_name = NET_IPV6,
571 static ctl_table ipq_root_table[] = {
576 .child = ipq_dir_table
581 static int ip6_queue_show(struct seq_file *m, void *v)
583 read_lock_bh(&queue_lock);
589 "Queue length : %u\n"
590 "Queue max. length : %u\n"
591 "Queue dropped : %u\n"
592 "Netfilter dropped : %u\n",
601 read_unlock_bh(&queue_lock);
605 static int ip6_queue_open(struct inode *inode, struct file *file)
607 return single_open(file, ip6_queue_show, NULL);
610 static const struct file_operations ip6_queue_proc_fops = {
611 .open = ip6_queue_open,
614 .release = single_release,
615 .owner = THIS_MODULE,
618 static const struct nf_queue_handler nfqh = {
620 .outfn = &ipq_enqueue_packet,
623 static int __init ip6_queue_init(void)
625 int status = -ENOMEM;
626 struct proc_dir_entry *proc;
628 netlink_register_notifier(&ipq_nl_notifier);
629 ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
630 ipq_rcv_skb, NULL, THIS_MODULE);
632 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
633 goto cleanup_netlink_notifier;
636 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
638 proc->owner = THIS_MODULE;
639 proc->proc_fops = &ip6_queue_proc_fops;
641 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
645 register_netdevice_notifier(&ipq_dev_notifier);
646 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
648 status = nf_register_queue_handler(PF_INET6, &nfqh);
650 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
656 unregister_sysctl_table(ipq_sysctl_header);
657 unregister_netdevice_notifier(&ipq_dev_notifier);
658 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
661 sock_release(ipqnl->sk_socket);
662 mutex_lock(&ipqnl_mutex);
663 mutex_unlock(&ipqnl_mutex);
665 cleanup_netlink_notifier:
666 netlink_unregister_notifier(&ipq_nl_notifier);
670 static void __exit ip6_queue_fini(void)
672 nf_unregister_queue_handlers(&nfqh);
676 unregister_sysctl_table(ipq_sysctl_header);
677 unregister_netdevice_notifier(&ipq_dev_notifier);
678 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
680 sock_release(ipqnl->sk_socket);
681 mutex_lock(&ipqnl_mutex);
682 mutex_unlock(&ipqnl_mutex);
684 netlink_unregister_notifier(&ipq_nl_notifier);
687 MODULE_DESCRIPTION("IPv6 packet queue handler");
688 MODULE_LICENSE("GPL");
690 module_init(ip6_queue_init);
691 module_exit(ip6_queue_fini);