2 * This is a module which is used for queueing IPv4 packets and
3 * communicating with userspace via netlink.
5 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
6 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_queue.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netlink.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/security.h>
26 #include <linux/mutex.h>
27 #include <net/net_namespace.h>
29 #include <net/route.h>
31 #define IPQ_QMAX_DEFAULT 1024
32 #define IPQ_PROC_FS_NAME "ip_queue"
33 #define NET_IPQ_QMAX 2088
34 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
36 struct ipq_queue_entry {
37 struct list_head list;
42 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
44 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
45 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
46 static DEFINE_RWLOCK(queue_lock);
47 static int peer_pid __read_mostly;
48 static unsigned int copy_range __read_mostly;
49 static unsigned int queue_total;
50 static unsigned int queue_dropped = 0;
51 static unsigned int queue_user_dropped = 0;
52 static struct sock *ipqnl __read_mostly;
53 static LIST_HEAD(queue_list);
54 static DEFINE_MUTEX(ipqnl_mutex);
57 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
59 /* TCP input path (and probably other bits) assume to be called
60 * from softirq context, not from syscall, like ipq_issue_verdict is
61 * called. TCP input path deadlocks with locks taken from timer
62 * softirq, e.g. We therefore emulate this by local_bh_disable() */
65 nf_reinject(entry->skb, entry->info, verdict);
72 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
74 list_add(&entry->list, &queue_list);
79 * Find and return a queued entry matched by cmpfn, or return the last
80 * entry if cmpfn is NULL.
82 static inline struct ipq_queue_entry *
83 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
87 list_for_each_prev(p, &queue_list) {
88 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
90 if (!cmpfn || cmpfn(entry, data))
97 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
99 list_del(&entry->list);
103 static inline struct ipq_queue_entry *
104 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
106 struct ipq_queue_entry *entry;
108 entry = __ipq_find_entry(cmpfn, data);
112 __ipq_dequeue_entry(entry);
118 __ipq_flush(int verdict)
120 struct ipq_queue_entry *entry;
122 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
123 ipq_issue_verdict(entry, verdict);
127 __ipq_set_mode(unsigned char mode, unsigned int range)
138 case IPQ_COPY_PACKET:
141 if (copy_range > 0xFFFF)
156 net_disable_timestamp();
157 __ipq_set_mode(IPQ_COPY_NONE, 0);
158 __ipq_flush(NF_DROP);
161 static struct ipq_queue_entry *
162 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
164 struct ipq_queue_entry *entry;
166 write_lock_bh(&queue_lock);
167 entry = __ipq_find_dequeue_entry(cmpfn, data);
168 write_unlock_bh(&queue_lock);
173 ipq_flush(int verdict)
175 write_lock_bh(&queue_lock);
176 __ipq_flush(verdict);
177 write_unlock_bh(&queue_lock);
180 static struct sk_buff *
181 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
183 sk_buff_data_t old_tail;
187 struct ipq_packet_msg *pmsg;
188 struct nlmsghdr *nlh;
191 read_lock_bh(&queue_lock);
196 size = NLMSG_SPACE(sizeof(*pmsg));
200 case IPQ_COPY_PACKET:
201 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
202 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
203 (*errp = skb_checksum_help(entry->skb))) {
204 read_unlock_bh(&queue_lock);
207 if (copy_range == 0 || copy_range > entry->skb->len)
208 data_len = entry->skb->len;
210 data_len = copy_range;
212 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
217 read_unlock_bh(&queue_lock);
221 read_unlock_bh(&queue_lock);
223 skb = alloc_skb(size, GFP_ATOMIC);
227 old_tail = skb->tail;
228 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
229 pmsg = NLMSG_DATA(nlh);
230 memset(pmsg, 0, sizeof(*pmsg));
232 pmsg->packet_id = (unsigned long )entry;
233 pmsg->data_len = data_len;
234 tv = ktime_to_timeval(entry->skb->tstamp);
235 pmsg->timestamp_sec = tv.tv_sec;
236 pmsg->timestamp_usec = tv.tv_usec;
237 pmsg->mark = entry->skb->mark;
238 pmsg->hook = entry->info->hook;
239 pmsg->hw_protocol = entry->skb->protocol;
241 if (entry->info->indev)
242 strcpy(pmsg->indev_name, entry->info->indev->name);
244 pmsg->indev_name[0] = '\0';
246 if (entry->info->outdev)
247 strcpy(pmsg->outdev_name, entry->info->outdev->name);
249 pmsg->outdev_name[0] = '\0';
251 if (entry->info->indev && entry->skb->dev) {
252 pmsg->hw_type = entry->skb->dev->type;
253 if (entry->skb->dev->hard_header_parse)
255 entry->skb->dev->hard_header_parse(entry->skb,
260 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
263 nlh->nlmsg_len = skb->tail - old_tail;
270 printk(KERN_ERR "ip_queue: error creating packet message\n");
275 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
276 unsigned int queuenum, 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 "ip_queue: OOM in ipq_enqueue_packet()\n");
294 nskb = ipq_build_packet_message(entry, &status);
298 write_lock_bh(&queue_lock);
301 goto err_out_free_nskb;
303 if (queue_total >= queue_maxlen) {
307 printk (KERN_WARNING "ip_queue: full at %d entries, "
308 "dropping packets(s). Dropped: %d\n", queue_total,
310 goto err_out_free_nskb;
313 /* netlink_unicast will either free the nskb or attach it to a socket */
314 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
316 queue_user_dropped++;
320 __ipq_enqueue_entry(entry);
322 write_unlock_bh(&queue_lock);
329 write_unlock_bh(&queue_lock);
337 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
340 struct iphdr *user_iph = (struct iphdr *)v->payload;
342 if (v->data_len < sizeof(*user_iph))
344 diff = v->data_len - e->skb->len;
346 if (pskb_trim(e->skb, v->data_len))
348 } else if (diff > 0) {
349 if (v->data_len > 0xFFFF)
351 if (diff > skb_tailroom(e->skb)) {
352 struct sk_buff *newskb;
354 newskb = skb_copy_expand(e->skb,
355 skb_headroom(e->skb),
358 if (newskb == NULL) {
359 printk(KERN_WARNING "ip_queue: OOM "
360 "in mangle, dropping packet\n");
364 skb_set_owner_w(newskb, e->skb->sk);
368 skb_put(e->skb, diff);
370 if (!skb_make_writable(&e->skb, v->data_len))
372 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
373 e->skb->ip_summed = CHECKSUM_NONE;
379 id_cmp(struct ipq_queue_entry *e, unsigned long id)
381 return (id == (unsigned long )e);
385 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
387 struct ipq_queue_entry *entry;
389 if (vmsg->value > NF_MAX_VERDICT)
392 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
396 int verdict = vmsg->value;
398 if (vmsg->data_len && vmsg->data_len == len)
399 if (ipq_mangle_ipv4(vmsg, entry) < 0)
402 ipq_issue_verdict(entry, verdict);
408 ipq_set_mode(unsigned char mode, unsigned int range)
412 write_lock_bh(&queue_lock);
413 status = __ipq_set_mode(mode, range);
414 write_unlock_bh(&queue_lock);
419 ipq_receive_peer(struct ipq_peer_msg *pmsg,
420 unsigned char type, unsigned int len)
424 if (len < sizeof(*pmsg))
429 status = ipq_set_mode(pmsg->msg.mode.value,
430 pmsg->msg.mode.range);
434 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
437 status = ipq_set_verdict(&pmsg->msg.verdict,
438 len - sizeof(*pmsg));
447 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
449 if (entry->info->indev)
450 if (entry->info->indev->ifindex == ifindex)
452 if (entry->info->outdev)
453 if (entry->info->outdev->ifindex == ifindex)
455 #ifdef CONFIG_BRIDGE_NETFILTER
456 if (entry->skb->nf_bridge) {
457 if (entry->skb->nf_bridge->physindev &&
458 entry->skb->nf_bridge->physindev->ifindex == ifindex)
460 if (entry->skb->nf_bridge->physoutdev &&
461 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
469 ipq_dev_drop(int ifindex)
471 struct ipq_queue_entry *entry;
473 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
474 ipq_issue_verdict(entry, NF_DROP);
477 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
480 ipq_rcv_skb(struct sk_buff *skb)
482 int status, type, pid, flags, nlmsglen, skblen;
483 struct nlmsghdr *nlh;
486 if (skblen < sizeof(*nlh))
489 nlh = nlmsg_hdr(skb);
490 nlmsglen = nlh->nlmsg_len;
491 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
494 pid = nlh->nlmsg_pid;
495 flags = nlh->nlmsg_flags;
497 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
498 RCV_SKB_FAIL(-EINVAL);
500 if (flags & MSG_TRUNC)
501 RCV_SKB_FAIL(-ECOMM);
503 type = nlh->nlmsg_type;
504 if (type < NLMSG_NOOP || type >= IPQM_MAX)
505 RCV_SKB_FAIL(-EINVAL);
507 if (type <= IPQM_BASE)
510 if (security_netlink_recv(skb, CAP_NET_ADMIN))
511 RCV_SKB_FAIL(-EPERM);
513 write_lock_bh(&queue_lock);
516 if (peer_pid != pid) {
517 write_unlock_bh(&queue_lock);
518 RCV_SKB_FAIL(-EBUSY);
521 net_enable_timestamp();
525 write_unlock_bh(&queue_lock);
527 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
528 nlmsglen - NLMSG_LENGTH(0));
530 RCV_SKB_FAIL(status);
532 if (flags & NLM_F_ACK)
533 netlink_ack(skb, nlh, 0);
538 ipq_rcv_sk(struct sock *sk, int len)
543 mutex_lock(&ipqnl_mutex);
545 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
546 skb = skb_dequeue(&sk->sk_receive_queue);
551 mutex_unlock(&ipqnl_mutex);
555 ipq_rcv_dev_event(struct notifier_block *this,
556 unsigned long event, void *ptr)
558 struct net_device *dev = ptr;
560 if (dev->nd_net != &init_net)
563 /* Drop any packets associated with the downed device */
564 if (event == NETDEV_DOWN)
565 ipq_dev_drop(dev->ifindex);
569 static struct notifier_block ipq_dev_notifier = {
570 .notifier_call = ipq_rcv_dev_event,
574 ipq_rcv_nl_event(struct notifier_block *this,
575 unsigned long event, void *ptr)
577 struct netlink_notify *n = ptr;
579 if (event == NETLINK_URELEASE &&
580 n->protocol == NETLINK_FIREWALL && n->pid) {
581 write_lock_bh(&queue_lock);
582 if ((n->net == &init_net) && (n->pid == peer_pid))
584 write_unlock_bh(&queue_lock);
589 static struct notifier_block ipq_nl_notifier = {
590 .notifier_call = ipq_rcv_nl_event,
593 static struct ctl_table_header *ipq_sysctl_header;
595 static ctl_table ipq_table[] = {
597 .ctl_name = NET_IPQ_QMAX,
598 .procname = NET_IPQ_QMAX_NAME,
599 .data = &queue_maxlen,
600 .maxlen = sizeof(queue_maxlen),
602 .proc_handler = proc_dointvec
607 static ctl_table ipq_dir_table[] = {
609 .ctl_name = NET_IPV4,
617 static ctl_table ipq_root_table[] = {
622 .child = ipq_dir_table
627 #ifdef CONFIG_PROC_FS
629 ipq_get_info(char *buffer, char **start, off_t offset, int length)
633 read_lock_bh(&queue_lock);
635 len = sprintf(buffer,
639 "Queue length : %u\n"
640 "Queue max. length : %u\n"
641 "Queue dropped : %u\n"
642 "Netlink dropped : %u\n",
651 read_unlock_bh(&queue_lock);
653 *start = buffer + offset;
661 #endif /* CONFIG_PROC_FS */
663 static struct nf_queue_handler nfqh = {
665 .outfn = &ipq_enqueue_packet,
668 static int __init ip_queue_init(void)
670 int status = -ENOMEM;
671 struct proc_dir_entry *proc;
673 netlink_register_notifier(&ipq_nl_notifier);
674 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
675 ipq_rcv_sk, NULL, THIS_MODULE);
677 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
678 goto cleanup_netlink_notifier;
681 proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
683 proc->owner = THIS_MODULE;
685 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
689 register_netdevice_notifier(&ipq_dev_notifier);
690 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
692 status = nf_register_queue_handler(PF_INET, &nfqh);
694 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
700 unregister_sysctl_table(ipq_sysctl_header);
701 unregister_netdevice_notifier(&ipq_dev_notifier);
702 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
704 sock_release(ipqnl->sk_socket);
705 mutex_lock(&ipqnl_mutex);
706 mutex_unlock(&ipqnl_mutex);
708 cleanup_netlink_notifier:
709 netlink_unregister_notifier(&ipq_nl_notifier);
713 static void __exit ip_queue_fini(void)
715 nf_unregister_queue_handlers(&nfqh);
719 unregister_sysctl_table(ipq_sysctl_header);
720 unregister_netdevice_notifier(&ipq_dev_notifier);
721 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
723 sock_release(ipqnl->sk_socket);
724 mutex_lock(&ipqnl_mutex);
725 mutex_unlock(&ipqnl_mutex);
727 netlink_unregister_notifier(&ipq_nl_notifier);
730 MODULE_DESCRIPTION("IPv4 packet queue handler");
731 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
732 MODULE_LICENSE("GPL");
734 module_init(ip_queue_init);
735 module_exit(ip_queue_fini);