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)
339 struct iphdr *user_iph = (struct iphdr *)v->payload;
341 if (v->data_len < sizeof(*user_iph))
343 diff = v->data_len - e->skb->len;
345 if (pskb_trim(e->skb, v->data_len))
347 } else if (diff > 0) {
348 if (v->data_len > 0xFFFF)
350 if (diff > skb_tailroom(e->skb)) {
351 err = pskb_expand_head(e->skb, 0,
352 diff - skb_tailroom(e->skb),
355 printk(KERN_WARNING "ip_queue: error "
356 "in mangle, dropping packet: %d\n", -err);
360 skb_put(e->skb, diff);
362 if (!skb_make_writable(e->skb, v->data_len))
364 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
365 e->skb->ip_summed = CHECKSUM_NONE;
371 id_cmp(struct ipq_queue_entry *e, unsigned long id)
373 return (id == (unsigned long )e);
377 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
379 struct ipq_queue_entry *entry;
381 if (vmsg->value > NF_MAX_VERDICT)
384 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
388 int verdict = vmsg->value;
390 if (vmsg->data_len && vmsg->data_len == len)
391 if (ipq_mangle_ipv4(vmsg, entry) < 0)
394 ipq_issue_verdict(entry, verdict);
400 ipq_set_mode(unsigned char mode, unsigned int range)
404 write_lock_bh(&queue_lock);
405 status = __ipq_set_mode(mode, range);
406 write_unlock_bh(&queue_lock);
411 ipq_receive_peer(struct ipq_peer_msg *pmsg,
412 unsigned char type, unsigned int len)
416 if (len < sizeof(*pmsg))
421 status = ipq_set_mode(pmsg->msg.mode.value,
422 pmsg->msg.mode.range);
426 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
429 status = ipq_set_verdict(&pmsg->msg.verdict,
430 len - sizeof(*pmsg));
439 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
441 if (entry->info->indev)
442 if (entry->info->indev->ifindex == ifindex)
444 if (entry->info->outdev)
445 if (entry->info->outdev->ifindex == ifindex)
447 #ifdef CONFIG_BRIDGE_NETFILTER
448 if (entry->skb->nf_bridge) {
449 if (entry->skb->nf_bridge->physindev &&
450 entry->skb->nf_bridge->physindev->ifindex == ifindex)
452 if (entry->skb->nf_bridge->physoutdev &&
453 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
461 ipq_dev_drop(int ifindex)
463 struct ipq_queue_entry *entry;
465 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
466 ipq_issue_verdict(entry, NF_DROP);
469 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
472 __ipq_rcv_skb(struct sk_buff *skb)
474 int status, type, pid, flags, nlmsglen, skblen;
475 struct nlmsghdr *nlh;
478 if (skblen < sizeof(*nlh))
481 nlh = nlmsg_hdr(skb);
482 nlmsglen = nlh->nlmsg_len;
483 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
486 pid = nlh->nlmsg_pid;
487 flags = nlh->nlmsg_flags;
489 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
490 RCV_SKB_FAIL(-EINVAL);
492 if (flags & MSG_TRUNC)
493 RCV_SKB_FAIL(-ECOMM);
495 type = nlh->nlmsg_type;
496 if (type < NLMSG_NOOP || type >= IPQM_MAX)
497 RCV_SKB_FAIL(-EINVAL);
499 if (type <= IPQM_BASE)
502 if (security_netlink_recv(skb, CAP_NET_ADMIN))
503 RCV_SKB_FAIL(-EPERM);
505 write_lock_bh(&queue_lock);
508 if (peer_pid != pid) {
509 write_unlock_bh(&queue_lock);
510 RCV_SKB_FAIL(-EBUSY);
513 net_enable_timestamp();
517 write_unlock_bh(&queue_lock);
519 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
520 nlmsglen - NLMSG_LENGTH(0));
522 RCV_SKB_FAIL(status);
524 if (flags & NLM_F_ACK)
525 netlink_ack(skb, nlh, 0);
530 ipq_rcv_skb(struct sk_buff *skb)
532 mutex_lock(&ipqnl_mutex);
534 mutex_unlock(&ipqnl_mutex);
538 ipq_rcv_dev_event(struct notifier_block *this,
539 unsigned long event, void *ptr)
541 struct net_device *dev = ptr;
543 if (dev->nd_net != &init_net)
546 /* Drop any packets associated with the downed device */
547 if (event == NETDEV_DOWN)
548 ipq_dev_drop(dev->ifindex);
552 static struct notifier_block ipq_dev_notifier = {
553 .notifier_call = ipq_rcv_dev_event,
557 ipq_rcv_nl_event(struct notifier_block *this,
558 unsigned long event, void *ptr)
560 struct netlink_notify *n = ptr;
562 if (event == NETLINK_URELEASE &&
563 n->protocol == NETLINK_FIREWALL && n->pid) {
564 write_lock_bh(&queue_lock);
565 if ((n->net == &init_net) && (n->pid == peer_pid))
567 write_unlock_bh(&queue_lock);
572 static struct notifier_block ipq_nl_notifier = {
573 .notifier_call = ipq_rcv_nl_event,
576 static struct ctl_table_header *ipq_sysctl_header;
578 static ctl_table ipq_table[] = {
580 .ctl_name = NET_IPQ_QMAX,
581 .procname = NET_IPQ_QMAX_NAME,
582 .data = &queue_maxlen,
583 .maxlen = sizeof(queue_maxlen),
585 .proc_handler = proc_dointvec
590 static ctl_table ipq_dir_table[] = {
592 .ctl_name = NET_IPV4,
600 static ctl_table ipq_root_table[] = {
605 .child = ipq_dir_table
610 #ifdef CONFIG_PROC_FS
612 ipq_get_info(char *buffer, char **start, off_t offset, int length)
616 read_lock_bh(&queue_lock);
618 len = sprintf(buffer,
622 "Queue length : %u\n"
623 "Queue max. length : %u\n"
624 "Queue dropped : %u\n"
625 "Netlink dropped : %u\n",
634 read_unlock_bh(&queue_lock);
636 *start = buffer + offset;
644 #endif /* CONFIG_PROC_FS */
646 static struct nf_queue_handler nfqh = {
648 .outfn = &ipq_enqueue_packet,
651 static int __init ip_queue_init(void)
653 int status = -ENOMEM;
654 struct proc_dir_entry *proc;
656 netlink_register_notifier(&ipq_nl_notifier);
657 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
658 ipq_rcv_skb, NULL, THIS_MODULE);
660 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
661 goto cleanup_netlink_notifier;
664 proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
666 proc->owner = THIS_MODULE;
668 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
672 register_netdevice_notifier(&ipq_dev_notifier);
673 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
675 status = nf_register_queue_handler(PF_INET, &nfqh);
677 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
683 unregister_sysctl_table(ipq_sysctl_header);
684 unregister_netdevice_notifier(&ipq_dev_notifier);
685 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
687 sock_release(ipqnl->sk_socket);
688 mutex_lock(&ipqnl_mutex);
689 mutex_unlock(&ipqnl_mutex);
691 cleanup_netlink_notifier:
692 netlink_unregister_notifier(&ipq_nl_notifier);
696 static void __exit ip_queue_fini(void)
698 nf_unregister_queue_handlers(&nfqh);
702 unregister_sysctl_table(ipq_sysctl_header);
703 unregister_netdevice_notifier(&ipq_dev_notifier);
704 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
706 sock_release(ipqnl->sk_socket);
707 mutex_lock(&ipqnl_mutex);
708 mutex_unlock(&ipqnl_mutex);
710 netlink_unregister_notifier(&ipq_nl_notifier);
713 MODULE_DESCRIPTION("IPv4 packet queue handler");
714 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
715 MODULE_LICENSE("GPL");
717 module_init(ip_queue_init);
718 module_exit(ip_queue_fini);