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 pmsg->hw_addrlen = dev_parse_header(entry->skb,
258 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
261 nlh->nlmsg_len = skb->tail - old_tail;
268 printk(KERN_ERR "ip_queue: error creating packet message\n");
273 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
274 unsigned int queuenum, void *data)
276 int status = -EINVAL;
277 struct sk_buff *nskb;
278 struct ipq_queue_entry *entry;
280 if (copy_mode == IPQ_COPY_NONE)
283 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
285 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
292 nskb = ipq_build_packet_message(entry, &status);
296 write_lock_bh(&queue_lock);
299 goto err_out_free_nskb;
301 if (queue_total >= queue_maxlen) {
305 printk (KERN_WARNING "ip_queue: full at %d entries, "
306 "dropping packets(s). Dropped: %d\n", queue_total,
308 goto err_out_free_nskb;
311 /* netlink_unicast will either free the nskb or attach it to a socket */
312 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
314 queue_user_dropped++;
318 __ipq_enqueue_entry(entry);
320 write_unlock_bh(&queue_lock);
327 write_unlock_bh(&queue_lock);
335 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
338 struct iphdr *user_iph = (struct iphdr *)v->payload;
340 if (v->data_len < sizeof(*user_iph))
342 diff = v->data_len - e->skb->len;
344 if (pskb_trim(e->skb, v->data_len))
346 } else if (diff > 0) {
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 "ip_queue: OOM "
358 "in mangle, dropping packet\n");
362 skb_set_owner_w(newskb, e->skb->sk);
366 skb_put(e->skb, diff);
368 if (!skb_make_writable(&e->skb, v->data_len))
370 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
371 e->skb->ip_summed = CHECKSUM_NONE;
377 id_cmp(struct ipq_queue_entry *e, unsigned long id)
379 return (id == (unsigned long )e);
383 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
385 struct ipq_queue_entry *entry;
387 if (vmsg->value > NF_MAX_VERDICT)
390 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
394 int verdict = vmsg->value;
396 if (vmsg->data_len && vmsg->data_len == len)
397 if (ipq_mangle_ipv4(vmsg, entry) < 0)
400 ipq_issue_verdict(entry, verdict);
406 ipq_set_mode(unsigned char mode, unsigned int range)
410 write_lock_bh(&queue_lock);
411 status = __ipq_set_mode(mode, range);
412 write_unlock_bh(&queue_lock);
417 ipq_receive_peer(struct ipq_peer_msg *pmsg,
418 unsigned char type, unsigned int len)
422 if (len < sizeof(*pmsg))
427 status = ipq_set_mode(pmsg->msg.mode.value,
428 pmsg->msg.mode.range);
432 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
435 status = ipq_set_verdict(&pmsg->msg.verdict,
436 len - sizeof(*pmsg));
445 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
447 if (entry->info->indev)
448 if (entry->info->indev->ifindex == ifindex)
450 if (entry->info->outdev)
451 if (entry->info->outdev->ifindex == ifindex)
453 #ifdef CONFIG_BRIDGE_NETFILTER
454 if (entry->skb->nf_bridge) {
455 if (entry->skb->nf_bridge->physindev &&
456 entry->skb->nf_bridge->physindev->ifindex == ifindex)
458 if (entry->skb->nf_bridge->physoutdev &&
459 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
467 ipq_dev_drop(int ifindex)
469 struct ipq_queue_entry *entry;
471 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
472 ipq_issue_verdict(entry, NF_DROP);
475 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
478 __ipq_rcv_skb(struct sk_buff *skb)
480 int status, type, pid, flags, nlmsglen, skblen;
481 struct nlmsghdr *nlh;
484 if (skblen < sizeof(*nlh))
487 nlh = nlmsg_hdr(skb);
488 nlmsglen = nlh->nlmsg_len;
489 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
492 pid = nlh->nlmsg_pid;
493 flags = nlh->nlmsg_flags;
495 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
496 RCV_SKB_FAIL(-EINVAL);
498 if (flags & MSG_TRUNC)
499 RCV_SKB_FAIL(-ECOMM);
501 type = nlh->nlmsg_type;
502 if (type < NLMSG_NOOP || type >= IPQM_MAX)
503 RCV_SKB_FAIL(-EINVAL);
505 if (type <= IPQM_BASE)
508 if (security_netlink_recv(skb, CAP_NET_ADMIN))
509 RCV_SKB_FAIL(-EPERM);
511 write_lock_bh(&queue_lock);
514 if (peer_pid != pid) {
515 write_unlock_bh(&queue_lock);
516 RCV_SKB_FAIL(-EBUSY);
519 net_enable_timestamp();
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_skb(struct sk_buff *skb)
538 mutex_lock(&ipqnl_mutex);
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 if (dev->nd_net != &init_net)
552 /* Drop any packets associated with the downed device */
553 if (event == NETDEV_DOWN)
554 ipq_dev_drop(dev->ifindex);
558 static struct notifier_block ipq_dev_notifier = {
559 .notifier_call = ipq_rcv_dev_event,
563 ipq_rcv_nl_event(struct notifier_block *this,
564 unsigned long event, void *ptr)
566 struct netlink_notify *n = ptr;
568 if (event == NETLINK_URELEASE &&
569 n->protocol == NETLINK_FIREWALL && n->pid) {
570 write_lock_bh(&queue_lock);
571 if ((n->net == &init_net) && (n->pid == peer_pid))
573 write_unlock_bh(&queue_lock);
578 static struct notifier_block ipq_nl_notifier = {
579 .notifier_call = ipq_rcv_nl_event,
582 static struct ctl_table_header *ipq_sysctl_header;
584 static ctl_table ipq_table[] = {
586 .ctl_name = NET_IPQ_QMAX,
587 .procname = NET_IPQ_QMAX_NAME,
588 .data = &queue_maxlen,
589 .maxlen = sizeof(queue_maxlen),
591 .proc_handler = proc_dointvec
596 static ctl_table ipq_dir_table[] = {
598 .ctl_name = NET_IPV4,
606 static ctl_table ipq_root_table[] = {
611 .child = ipq_dir_table
616 #ifdef CONFIG_PROC_FS
618 ipq_get_info(char *buffer, char **start, off_t offset, int length)
622 read_lock_bh(&queue_lock);
624 len = sprintf(buffer,
628 "Queue length : %u\n"
629 "Queue max. length : %u\n"
630 "Queue dropped : %u\n"
631 "Netlink dropped : %u\n",
640 read_unlock_bh(&queue_lock);
642 *start = buffer + offset;
650 #endif /* CONFIG_PROC_FS */
652 static struct nf_queue_handler nfqh = {
654 .outfn = &ipq_enqueue_packet,
657 static int __init ip_queue_init(void)
659 int status = -ENOMEM;
660 struct proc_dir_entry *proc;
662 netlink_register_notifier(&ipq_nl_notifier);
663 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
664 ipq_rcv_skb, NULL, THIS_MODULE);
666 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
667 goto cleanup_netlink_notifier;
670 proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
672 proc->owner = THIS_MODULE;
674 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
678 register_netdevice_notifier(&ipq_dev_notifier);
679 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
681 status = nf_register_queue_handler(PF_INET, &nfqh);
683 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
689 unregister_sysctl_table(ipq_sysctl_header);
690 unregister_netdevice_notifier(&ipq_dev_notifier);
691 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
693 sock_release(ipqnl->sk_socket);
694 mutex_lock(&ipqnl_mutex);
695 mutex_unlock(&ipqnl_mutex);
697 cleanup_netlink_notifier:
698 netlink_unregister_notifier(&ipq_nl_notifier);
702 static void __exit ip_queue_fini(void)
704 nf_unregister_queue_handlers(&nfqh);
708 unregister_sysctl_table(ipq_sysctl_header);
709 unregister_netdevice_notifier(&ipq_dev_notifier);
710 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
712 sock_release(ipqnl->sk_socket);
713 mutex_lock(&ipqnl_mutex);
714 mutex_unlock(&ipqnl_mutex);
716 netlink_unregister_notifier(&ipq_nl_notifier);
719 MODULE_DESCRIPTION("IPv4 packet queue handler");
720 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
721 MODULE_LICENSE("GPL");
723 module_init(ip_queue_init);
724 module_exit(ip_queue_fini);