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/mutex.h>
29 #include <net/ip6_route.h>
30 #include <linux/netfilter_ipv4/ip_queue.h>
31 #include <linux/netfilter_ipv4/ip_tables.h>
32 #include <linux/netfilter_ipv6/ip6_tables.h>
34 #define IPQ_QMAX_DEFAULT 1024
35 #define IPQ_PROC_FS_NAME "ip6_queue"
36 #define NET_IPQ_QMAX 2088
37 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
39 struct ipq_queue_entry {
40 struct list_head list;
45 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
47 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
48 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
49 static DEFINE_RWLOCK(queue_lock);
50 static int peer_pid __read_mostly;
51 static unsigned int copy_range __read_mostly;
52 static unsigned int queue_total;
53 static unsigned int queue_dropped = 0;
54 static unsigned int queue_user_dropped = 0;
55 static struct sock *ipqnl __read_mostly;
56 static LIST_HEAD(queue_list);
57 static DEFINE_MUTEX(ipqnl_mutex);
60 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
63 nf_reinject(entry->skb, entry->info, verdict);
69 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
71 list_add(&entry->list, &queue_list);
76 * Find and return a queued entry matched by cmpfn, or return the last
77 * entry if cmpfn is NULL.
79 static inline struct ipq_queue_entry *
80 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
84 list_for_each_prev(p, &queue_list) {
85 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
87 if (!cmpfn || cmpfn(entry, data))
94 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
96 list_del(&entry->list);
100 static inline struct ipq_queue_entry *
101 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
103 struct ipq_queue_entry *entry;
105 entry = __ipq_find_entry(cmpfn, data);
109 __ipq_dequeue_entry(entry);
115 __ipq_flush(int verdict)
117 struct ipq_queue_entry *entry;
119 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
120 ipq_issue_verdict(entry, verdict);
124 __ipq_set_mode(unsigned char mode, unsigned int range)
135 case IPQ_COPY_PACKET:
138 if (copy_range > 0xFFFF)
153 net_disable_timestamp();
154 __ipq_set_mode(IPQ_COPY_NONE, 0);
155 __ipq_flush(NF_DROP);
158 static struct ipq_queue_entry *
159 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
161 struct ipq_queue_entry *entry;
163 write_lock_bh(&queue_lock);
164 entry = __ipq_find_dequeue_entry(cmpfn, data);
165 write_unlock_bh(&queue_lock);
170 ipq_flush(int verdict)
172 write_lock_bh(&queue_lock);
173 __ipq_flush(verdict);
174 write_unlock_bh(&queue_lock);
177 static struct sk_buff *
178 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
180 sk_buff_data_t old_tail;
184 struct ipq_packet_msg *pmsg;
185 struct nlmsghdr *nlh;
188 read_lock_bh(&queue_lock);
193 size = NLMSG_SPACE(sizeof(*pmsg));
197 case IPQ_COPY_PACKET:
198 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
199 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
200 (*errp = skb_checksum_help(entry->skb))) {
201 read_unlock_bh(&queue_lock);
204 if (copy_range == 0 || copy_range > entry->skb->len)
205 data_len = entry->skb->len;
207 data_len = copy_range;
209 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
214 read_unlock_bh(&queue_lock);
218 read_unlock_bh(&queue_lock);
220 skb = alloc_skb(size, GFP_ATOMIC);
224 old_tail = skb->tail;
225 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
226 pmsg = NLMSG_DATA(nlh);
227 memset(pmsg, 0, sizeof(*pmsg));
229 pmsg->packet_id = (unsigned long )entry;
230 pmsg->data_len = data_len;
231 tv = ktime_to_timeval(entry->skb->tstamp);
232 pmsg->timestamp_sec = tv.tv_sec;
233 pmsg->timestamp_usec = tv.tv_usec;
234 pmsg->mark = entry->skb->mark;
235 pmsg->hook = entry->info->hook;
236 pmsg->hw_protocol = entry->skb->protocol;
238 if (entry->info->indev)
239 strcpy(pmsg->indev_name, entry->info->indev->name);
241 pmsg->indev_name[0] = '\0';
243 if (entry->info->outdev)
244 strcpy(pmsg->outdev_name, entry->info->outdev->name);
246 pmsg->outdev_name[0] = '\0';
248 if (entry->info->indev && entry->skb->dev) {
249 pmsg->hw_type = entry->skb->dev->type;
250 if (entry->skb->dev->hard_header_parse)
252 entry->skb->dev->hard_header_parse(entry->skb,
257 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
260 nlh->nlmsg_len = skb->tail - old_tail;
267 printk(KERN_ERR "ip6_queue: error creating packet message\n");
272 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
273 unsigned int queuenum, void *data)
275 int status = -EINVAL;
276 struct sk_buff *nskb;
277 struct ipq_queue_entry *entry;
279 if (copy_mode == IPQ_COPY_NONE)
282 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
284 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
291 nskb = ipq_build_packet_message(entry, &status);
295 write_lock_bh(&queue_lock);
298 goto err_out_free_nskb;
300 if (queue_total >= queue_maxlen) {
304 printk (KERN_WARNING "ip6_queue: fill at %d entries, "
305 "dropping packet(s). Dropped: %d\n", queue_total,
307 goto err_out_free_nskb;
310 /* netlink_unicast will either free the nskb or attach it to a socket */
311 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
313 queue_user_dropped++;
317 __ipq_enqueue_entry(entry);
319 write_unlock_bh(&queue_lock);
326 write_unlock_bh(&queue_lock);
334 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
337 struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
339 if (v->data_len < sizeof(*user_iph))
341 diff = v->data_len - e->skb->len;
343 if (pskb_trim(e->skb, v->data_len))
345 } else if (diff > 0) {
346 if (v->data_len > 0xFFFF)
348 if (diff > skb_tailroom(e->skb)) {
349 struct sk_buff *newskb;
351 newskb = skb_copy_expand(e->skb,
352 skb_headroom(e->skb),
355 if (newskb == NULL) {
356 printk(KERN_WARNING "ip6_queue: OOM "
357 "in mangle, dropping packet\n");
361 skb_set_owner_w(newskb, e->skb->sk);
365 skb_put(e->skb, diff);
367 if (!skb_make_writable(&e->skb, v->data_len))
369 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
370 e->skb->ip_summed = CHECKSUM_NONE;
376 id_cmp(struct ipq_queue_entry *e, unsigned long id)
378 return (id == (unsigned long )e);
382 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
384 struct ipq_queue_entry *entry;
386 if (vmsg->value > NF_MAX_VERDICT)
389 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
393 int verdict = vmsg->value;
395 if (vmsg->data_len && vmsg->data_len == len)
396 if (ipq_mangle_ipv6(vmsg, entry) < 0)
399 ipq_issue_verdict(entry, verdict);
405 ipq_set_mode(unsigned char mode, unsigned int range)
409 write_lock_bh(&queue_lock);
410 status = __ipq_set_mode(mode, range);
411 write_unlock_bh(&queue_lock);
416 ipq_receive_peer(struct ipq_peer_msg *pmsg,
417 unsigned char type, unsigned int len)
421 if (len < sizeof(*pmsg))
426 status = ipq_set_mode(pmsg->msg.mode.value,
427 pmsg->msg.mode.range);
431 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
434 status = ipq_set_verdict(&pmsg->msg.verdict,
435 len - sizeof(*pmsg));
444 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
446 if (entry->info->indev)
447 if (entry->info->indev->ifindex == ifindex)
450 if (entry->info->outdev)
451 if (entry->info->outdev->ifindex == ifindex)
458 ipq_dev_drop(int ifindex)
460 struct ipq_queue_entry *entry;
462 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
463 ipq_issue_verdict(entry, NF_DROP);
466 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
469 ipq_rcv_skb(struct sk_buff *skb)
471 int status, type, pid, flags, nlmsglen, skblen;
472 struct nlmsghdr *nlh;
475 if (skblen < sizeof(*nlh))
478 nlh = nlmsg_hdr(skb);
479 nlmsglen = nlh->nlmsg_len;
480 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
483 pid = nlh->nlmsg_pid;
484 flags = nlh->nlmsg_flags;
486 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
487 RCV_SKB_FAIL(-EINVAL);
489 if (flags & MSG_TRUNC)
490 RCV_SKB_FAIL(-ECOMM);
492 type = nlh->nlmsg_type;
493 if (type < NLMSG_NOOP || type >= IPQM_MAX)
494 RCV_SKB_FAIL(-EINVAL);
496 if (type <= IPQM_BASE)
499 if (security_netlink_recv(skb, CAP_NET_ADMIN))
500 RCV_SKB_FAIL(-EPERM);
502 write_lock_bh(&queue_lock);
505 if (peer_pid != pid) {
506 write_unlock_bh(&queue_lock);
507 RCV_SKB_FAIL(-EBUSY);
510 net_enable_timestamp();
514 write_unlock_bh(&queue_lock);
516 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
517 nlmsglen - NLMSG_LENGTH(0));
519 RCV_SKB_FAIL(status);
521 if (flags & NLM_F_ACK)
522 netlink_ack(skb, nlh, 0);
527 ipq_rcv_sk(struct sock *sk, int len)
532 mutex_lock(&ipqnl_mutex);
534 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
535 skb = skb_dequeue(&sk->sk_receive_queue);
540 mutex_unlock(&ipqnl_mutex);
544 ipq_rcv_dev_event(struct notifier_block *this,
545 unsigned long event, void *ptr)
547 struct net_device *dev = ptr;
549 /* Drop any packets associated with the downed device */
550 if (event == NETDEV_DOWN)
551 ipq_dev_drop(dev->ifindex);
555 static struct notifier_block ipq_dev_notifier = {
556 .notifier_call = ipq_rcv_dev_event,
560 ipq_rcv_nl_event(struct notifier_block *this,
561 unsigned long event, void *ptr)
563 struct netlink_notify *n = ptr;
565 if (event == NETLINK_URELEASE &&
566 n->protocol == NETLINK_IP6_FW && n->pid) {
567 write_lock_bh(&queue_lock);
568 if (n->pid == peer_pid)
570 write_unlock_bh(&queue_lock);
575 static struct notifier_block ipq_nl_notifier = {
576 .notifier_call = ipq_rcv_nl_event,
579 static struct ctl_table_header *ipq_sysctl_header;
581 static ctl_table ipq_table[] = {
583 .ctl_name = NET_IPQ_QMAX,
584 .procname = NET_IPQ_QMAX_NAME,
585 .data = &queue_maxlen,
586 .maxlen = sizeof(queue_maxlen),
588 .proc_handler = proc_dointvec
593 static ctl_table ipq_dir_table[] = {
595 .ctl_name = NET_IPV6,
603 static ctl_table ipq_root_table[] = {
608 .child = ipq_dir_table
613 #ifdef CONFIG_PROC_FS
615 ipq_get_info(char *buffer, char **start, off_t offset, int length)
619 read_lock_bh(&queue_lock);
621 len = sprintf(buffer,
625 "Queue length : %u\n"
626 "Queue max. length : %u\n"
627 "Queue dropped : %u\n"
628 "Netfilter dropped : %u\n",
637 read_unlock_bh(&queue_lock);
639 *start = buffer + offset;
647 #endif /* CONFIG_PROC_FS */
649 static struct nf_queue_handler nfqh = {
651 .outfn = &ipq_enqueue_packet,
654 static int __init ip6_queue_init(void)
656 int status = -ENOMEM;
657 struct proc_dir_entry *proc;
659 netlink_register_notifier(&ipq_nl_notifier);
660 ipqnl = netlink_kernel_create(NETLINK_IP6_FW, 0, ipq_rcv_sk, NULL,
663 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
664 goto cleanup_netlink_notifier;
667 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
669 proc->owner = THIS_MODULE;
671 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
675 register_netdevice_notifier(&ipq_dev_notifier);
676 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
678 status = nf_register_queue_handler(PF_INET6, &nfqh);
680 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
686 unregister_sysctl_table(ipq_sysctl_header);
687 unregister_netdevice_notifier(&ipq_dev_notifier);
688 proc_net_remove(IPQ_PROC_FS_NAME);
691 sock_release(ipqnl->sk_socket);
692 mutex_lock(&ipqnl_mutex);
693 mutex_unlock(&ipqnl_mutex);
695 cleanup_netlink_notifier:
696 netlink_unregister_notifier(&ipq_nl_notifier);
700 static void __exit ip6_queue_fini(void)
702 nf_unregister_queue_handlers(&nfqh);
706 unregister_sysctl_table(ipq_sysctl_header);
707 unregister_netdevice_notifier(&ipq_dev_notifier);
708 proc_net_remove(IPQ_PROC_FS_NAME);
710 sock_release(ipqnl->sk_socket);
711 mutex_lock(&ipqnl_mutex);
712 mutex_unlock(&ipqnl_mutex);
714 netlink_unregister_notifier(&ipq_nl_notifier);
717 MODULE_DESCRIPTION("IPv6 packet queue handler");
718 MODULE_LICENSE("GPL");
720 module_init(ip6_queue_init);
721 module_exit(ip6_queue_fini);