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 * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
13 * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
14 * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian
16 * 2000-08-01: Added Nick Williams' MAC support.
17 * 2002-06-25: Code cleanup.
18 * 2005-01-10: Added /proc counter for dropped packets; fixed so
19 * packets aren't delivered to user space if they're going
21 * 2005-05-26: local_bh_{disable,enable} around nf_reinject (Harald Welte)
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/init.h>
28 #include <linux/notifier.h>
29 #include <linux/netdevice.h>
30 #include <linux/netfilter.h>
31 #include <linux/netfilter_ipv4/ip_queue.h>
32 #include <linux/netfilter_ipv4/ip_tables.h>
33 #include <linux/netlink.h>
34 #include <linux/spinlock.h>
35 #include <linux/sysctl.h>
36 #include <linux/proc_fs.h>
37 #include <linux/security.h>
39 #include <net/route.h>
41 #define IPQ_QMAX_DEFAULT 1024
42 #define IPQ_PROC_FS_NAME "ip_queue"
43 #define NET_IPQ_QMAX 2088
44 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
52 struct ipq_queue_entry {
53 struct list_head list;
56 struct ipq_rt_info rt_info;
59 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
61 static unsigned char copy_mode = IPQ_COPY_NONE;
62 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
63 static DEFINE_RWLOCK(queue_lock);
65 static unsigned int copy_range;
66 static unsigned int queue_total;
67 static unsigned int queue_dropped = 0;
68 static unsigned int queue_user_dropped = 0;
69 static struct sock *ipqnl;
70 static LIST_HEAD(queue_list);
71 static DECLARE_MUTEX(ipqnl_sem);
74 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
76 /* TCP input path (and probably other bits) assume to be called
77 * from softirq context, not from syscall, like ipq_issue_verdict is
78 * called. TCP input path deadlocks with locks taken from timer
79 * softirq, e.g. We therefore emulate this by local_bh_disable() */
82 nf_reinject(entry->skb, entry->info, verdict);
89 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
91 list_add(&entry->list, &queue_list);
96 * Find and return a queued entry matched by cmpfn, or return the last
97 * entry if cmpfn is NULL.
99 static inline struct ipq_queue_entry *
100 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
104 list_for_each_prev(p, &queue_list) {
105 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
107 if (!cmpfn || cmpfn(entry, data))
114 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
116 list_del(&entry->list);
120 static inline struct ipq_queue_entry *
121 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
123 struct ipq_queue_entry *entry;
125 entry = __ipq_find_entry(cmpfn, data);
129 __ipq_dequeue_entry(entry);
135 __ipq_flush(int verdict)
137 struct ipq_queue_entry *entry;
139 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
140 ipq_issue_verdict(entry, verdict);
144 __ipq_set_mode(unsigned char mode, unsigned int range)
155 case IPQ_COPY_PACKET:
158 if (copy_range > 0xFFFF)
173 net_disable_timestamp();
174 __ipq_set_mode(IPQ_COPY_NONE, 0);
175 __ipq_flush(NF_DROP);
178 static struct ipq_queue_entry *
179 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
181 struct ipq_queue_entry *entry;
183 write_lock_bh(&queue_lock);
184 entry = __ipq_find_dequeue_entry(cmpfn, data);
185 write_unlock_bh(&queue_lock);
190 ipq_flush(int verdict)
192 write_lock_bh(&queue_lock);
193 __ipq_flush(verdict);
194 write_unlock_bh(&queue_lock);
197 static struct sk_buff *
198 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
200 unsigned char *old_tail;
204 struct ipq_packet_msg *pmsg;
205 struct nlmsghdr *nlh;
207 read_lock_bh(&queue_lock);
212 size = NLMSG_SPACE(sizeof(*pmsg));
216 case IPQ_COPY_PACKET:
217 if (copy_range == 0 || copy_range > entry->skb->len)
218 data_len = entry->skb->len;
220 data_len = copy_range;
222 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
227 read_unlock_bh(&queue_lock);
231 read_unlock_bh(&queue_lock);
233 skb = alloc_skb(size, GFP_ATOMIC);
238 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
239 pmsg = NLMSG_DATA(nlh);
240 memset(pmsg, 0, sizeof(*pmsg));
242 pmsg->packet_id = (unsigned long )entry;
243 pmsg->data_len = data_len;
244 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
245 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
246 pmsg->mark = entry->skb->nfmark;
247 pmsg->hook = entry->info->hook;
248 pmsg->hw_protocol = entry->skb->protocol;
250 if (entry->info->indev)
251 strcpy(pmsg->indev_name, entry->info->indev->name);
253 pmsg->indev_name[0] = '\0';
255 if (entry->info->outdev)
256 strcpy(pmsg->outdev_name, entry->info->outdev->name);
258 pmsg->outdev_name[0] = '\0';
260 if (entry->info->indev && entry->skb->dev) {
261 pmsg->hw_type = entry->skb->dev->type;
262 if (entry->skb->dev->hard_header_parse)
264 entry->skb->dev->hard_header_parse(entry->skb,
269 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
272 nlh->nlmsg_len = skb->tail - old_tail;
279 printk(KERN_ERR "ip_queue: error creating packet message\n");
284 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
286 int status = -EINVAL;
287 struct sk_buff *nskb;
288 struct ipq_queue_entry *entry;
290 if (copy_mode == IPQ_COPY_NONE)
293 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
295 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
302 if (entry->info->hook == NF_IP_LOCAL_OUT) {
303 struct iphdr *iph = skb->nh.iph;
305 entry->rt_info.tos = iph->tos;
306 entry->rt_info.daddr = iph->daddr;
307 entry->rt_info.saddr = iph->saddr;
310 nskb = ipq_build_packet_message(entry, &status);
314 write_lock_bh(&queue_lock);
317 goto err_out_free_nskb;
319 if (queue_total >= queue_maxlen) {
323 printk (KERN_WARNING "ip_queue: full at %d entries, "
324 "dropping packets(s). Dropped: %d\n", queue_total,
326 goto err_out_free_nskb;
329 /* netlink_unicast will either free the nskb or attach it to a socket */
330 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
332 queue_user_dropped++;
336 __ipq_enqueue_entry(entry);
338 write_unlock_bh(&queue_lock);
345 write_unlock_bh(&queue_lock);
353 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
356 struct iphdr *user_iph = (struct iphdr *)v->payload;
358 if (v->data_len < sizeof(*user_iph))
360 diff = v->data_len - e->skb->len;
362 skb_trim(e->skb, v->data_len);
364 if (v->data_len > 0xFFFF)
366 if (diff > skb_tailroom(e->skb)) {
367 struct sk_buff *newskb;
369 newskb = skb_copy_expand(e->skb,
370 skb_headroom(e->skb),
373 if (newskb == NULL) {
374 printk(KERN_WARNING "ip_queue: OOM "
375 "in mangle, dropping packet\n");
379 skb_set_owner_w(newskb, e->skb->sk);
383 skb_put(e->skb, diff);
385 if (!skb_ip_make_writable(&e->skb, v->data_len))
387 memcpy(e->skb->data, v->payload, v->data_len);
388 e->skb->nfcache |= NFC_ALTERED;
391 * Extra routing may needed on local out, as the QUEUE target never
392 * returns control to the table.
394 if (e->info->hook == NF_IP_LOCAL_OUT) {
395 struct iphdr *iph = e->skb->nh.iph;
397 if (!(iph->tos == e->rt_info.tos
398 && iph->daddr == e->rt_info.daddr
399 && iph->saddr == e->rt_info.saddr))
400 return ip_route_me_harder(&e->skb);
406 id_cmp(struct ipq_queue_entry *e, unsigned long id)
408 return (id == (unsigned long )e);
412 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
414 struct ipq_queue_entry *entry;
416 if (vmsg->value > NF_MAX_VERDICT)
419 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
423 int verdict = vmsg->value;
425 if (vmsg->data_len && vmsg->data_len == len)
426 if (ipq_mangle_ipv4(vmsg, entry) < 0)
429 ipq_issue_verdict(entry, verdict);
435 ipq_set_mode(unsigned char mode, unsigned int range)
439 write_lock_bh(&queue_lock);
440 status = __ipq_set_mode(mode, range);
441 write_unlock_bh(&queue_lock);
446 ipq_receive_peer(struct ipq_peer_msg *pmsg,
447 unsigned char type, unsigned int len)
451 if (len < sizeof(*pmsg))
456 status = ipq_set_mode(pmsg->msg.mode.value,
457 pmsg->msg.mode.range);
461 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
464 status = ipq_set_verdict(&pmsg->msg.verdict,
465 len - sizeof(*pmsg));
474 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
476 if (entry->info->indev)
477 if (entry->info->indev->ifindex == ifindex)
480 if (entry->info->outdev)
481 if (entry->info->outdev->ifindex == ifindex)
488 ipq_dev_drop(int ifindex)
490 struct ipq_queue_entry *entry;
492 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
493 ipq_issue_verdict(entry, NF_DROP);
496 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
499 ipq_rcv_skb(struct sk_buff *skb)
501 int status, type, pid, flags, nlmsglen, skblen;
502 struct nlmsghdr *nlh;
505 if (skblen < sizeof(*nlh))
508 nlh = (struct nlmsghdr *)skb->data;
509 nlmsglen = nlh->nlmsg_len;
510 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
513 pid = nlh->nlmsg_pid;
514 flags = nlh->nlmsg_flags;
516 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
517 RCV_SKB_FAIL(-EINVAL);
519 if (flags & MSG_TRUNC)
520 RCV_SKB_FAIL(-ECOMM);
522 type = nlh->nlmsg_type;
523 if (type < NLMSG_NOOP || type >= IPQM_MAX)
524 RCV_SKB_FAIL(-EINVAL);
526 if (type <= IPQM_BASE)
529 if (security_netlink_recv(skb))
530 RCV_SKB_FAIL(-EPERM);
532 write_lock_bh(&queue_lock);
535 if (peer_pid != pid) {
536 write_unlock_bh(&queue_lock);
537 RCV_SKB_FAIL(-EBUSY);
540 net_enable_timestamp();
544 write_unlock_bh(&queue_lock);
546 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
547 skblen - NLMSG_LENGTH(0));
549 RCV_SKB_FAIL(status);
551 if (flags & NLM_F_ACK)
552 netlink_ack(skb, nlh, 0);
557 ipq_rcv_sk(struct sock *sk, int len)
564 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
565 skb = skb_dequeue(&sk->sk_receive_queue);
574 ipq_rcv_dev_event(struct notifier_block *this,
575 unsigned long event, void *ptr)
577 struct net_device *dev = ptr;
579 /* Drop any packets associated with the downed device */
580 if (event == NETDEV_DOWN)
581 ipq_dev_drop(dev->ifindex);
585 static struct notifier_block ipq_dev_notifier = {
586 .notifier_call = ipq_rcv_dev_event,
590 ipq_rcv_nl_event(struct notifier_block *this,
591 unsigned long event, void *ptr)
593 struct netlink_notify *n = ptr;
595 if (event == NETLINK_URELEASE &&
596 n->protocol == NETLINK_FIREWALL && n->pid) {
597 write_lock_bh(&queue_lock);
598 if (n->pid == peer_pid)
600 write_unlock_bh(&queue_lock);
605 static struct notifier_block ipq_nl_notifier = {
606 .notifier_call = ipq_rcv_nl_event,
609 static struct ctl_table_header *ipq_sysctl_header;
611 static ctl_table ipq_table[] = {
613 .ctl_name = NET_IPQ_QMAX,
614 .procname = NET_IPQ_QMAX_NAME,
615 .data = &queue_maxlen,
616 .maxlen = sizeof(queue_maxlen),
618 .proc_handler = proc_dointvec
623 static ctl_table ipq_dir_table[] = {
625 .ctl_name = NET_IPV4,
633 static ctl_table ipq_root_table[] = {
638 .child = ipq_dir_table
643 #ifdef CONFIG_PROC_FS
645 ipq_get_info(char *buffer, char **start, off_t offset, int length)
649 read_lock_bh(&queue_lock);
651 len = sprintf(buffer,
655 "Queue length : %u\n"
656 "Queue max. length : %u\n"
657 "Queue dropped : %u\n"
658 "Netlink dropped : %u\n",
667 read_unlock_bh(&queue_lock);
669 *start = buffer + offset;
677 #endif /* CONFIG_PROC_FS */
680 init_or_cleanup(int init)
682 int status = -ENOMEM;
683 struct proc_dir_entry *proc;
688 netlink_register_notifier(&ipq_nl_notifier);
689 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
691 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
692 goto cleanup_netlink_notifier;
695 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
697 proc->owner = THIS_MODULE;
699 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
703 register_netdevice_notifier(&ipq_dev_notifier);
704 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
706 status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
708 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
714 nf_unregister_queue_handler(PF_INET);
719 unregister_sysctl_table(ipq_sysctl_header);
720 unregister_netdevice_notifier(&ipq_dev_notifier);
721 proc_net_remove(IPQ_PROC_FS_NAME);
724 sock_release(ipqnl->sk_socket);
728 cleanup_netlink_notifier:
729 netlink_unregister_notifier(&ipq_nl_notifier);
733 static int __init init(void)
736 return init_or_cleanup(1);
739 static void __exit fini(void)
744 MODULE_DESCRIPTION("IPv4 packet queue handler");
745 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
746 MODULE_LICENSE("GPL");