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"
46 struct ipq_queue_entry {
47 struct list_head list;
52 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
54 static unsigned char copy_mode = IPQ_COPY_NONE;
55 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
56 static DEFINE_RWLOCK(queue_lock);
58 static unsigned int copy_range;
59 static unsigned int queue_total;
60 static unsigned int queue_dropped = 0;
61 static unsigned int queue_user_dropped = 0;
62 static struct sock *ipqnl;
63 static LIST_HEAD(queue_list);
64 static DECLARE_MUTEX(ipqnl_sem);
67 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
69 /* TCP input path (and probably other bits) assume to be called
70 * from softirq context, not from syscall, like ipq_issue_verdict is
71 * called. TCP input path deadlocks with locks taken from timer
72 * softirq, e.g. We therefore emulate this by local_bh_disable() */
75 nf_reinject(entry->skb, entry->info, verdict);
82 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
84 list_add(&entry->list, &queue_list);
89 * Find and return a queued entry matched by cmpfn, or return the last
90 * entry if cmpfn is NULL.
92 static inline struct ipq_queue_entry *
93 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
97 list_for_each_prev(p, &queue_list) {
98 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
100 if (!cmpfn || cmpfn(entry, data))
107 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
109 list_del(&entry->list);
113 static inline struct ipq_queue_entry *
114 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
116 struct ipq_queue_entry *entry;
118 entry = __ipq_find_entry(cmpfn, data);
122 __ipq_dequeue_entry(entry);
128 __ipq_flush(int verdict)
130 struct ipq_queue_entry *entry;
132 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
133 ipq_issue_verdict(entry, verdict);
137 __ipq_set_mode(unsigned char mode, unsigned int range)
148 case IPQ_COPY_PACKET:
151 if (copy_range > 0xFFFF)
166 net_disable_timestamp();
167 __ipq_set_mode(IPQ_COPY_NONE, 0);
168 __ipq_flush(NF_DROP);
171 static struct ipq_queue_entry *
172 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
174 struct ipq_queue_entry *entry;
176 write_lock_bh(&queue_lock);
177 entry = __ipq_find_dequeue_entry(cmpfn, data);
178 write_unlock_bh(&queue_lock);
183 ipq_flush(int verdict)
185 write_lock_bh(&queue_lock);
186 __ipq_flush(verdict);
187 write_unlock_bh(&queue_lock);
190 static struct sk_buff *
191 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
193 unsigned char *old_tail;
197 struct ipq_packet_msg *pmsg;
198 struct nlmsghdr *nlh;
200 read_lock_bh(&queue_lock);
205 size = NLMSG_SPACE(sizeof(*pmsg));
209 case IPQ_COPY_PACKET:
210 if (entry->skb->ip_summed == CHECKSUM_HW &&
211 (*errp = skb_checksum_help(entry->skb,
212 entry->info->outdev == NULL))) {
213 read_unlock_bh(&queue_lock);
216 if (copy_range == 0 || copy_range > entry->skb->len)
217 data_len = entry->skb->len;
219 data_len = copy_range;
221 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
226 read_unlock_bh(&queue_lock);
230 read_unlock_bh(&queue_lock);
232 skb = alloc_skb(size, GFP_ATOMIC);
237 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
238 pmsg = NLMSG_DATA(nlh);
239 memset(pmsg, 0, sizeof(*pmsg));
241 pmsg->packet_id = (unsigned long )entry;
242 pmsg->data_len = data_len;
243 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
244 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
245 pmsg->mark = entry->skb->nfmark;
246 pmsg->hook = entry->info->hook;
247 pmsg->hw_protocol = entry->skb->protocol;
249 if (entry->info->indev)
250 strcpy(pmsg->indev_name, entry->info->indev->name);
252 pmsg->indev_name[0] = '\0';
254 if (entry->info->outdev)
255 strcpy(pmsg->outdev_name, entry->info->outdev->name);
257 pmsg->outdev_name[0] = '\0';
259 if (entry->info->indev && entry->skb->dev) {
260 pmsg->hw_type = entry->skb->dev->type;
261 if (entry->skb->dev->hard_header_parse)
263 entry->skb->dev->hard_header_parse(entry->skb,
268 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
271 nlh->nlmsg_len = skb->tail - old_tail;
278 printk(KERN_ERR "ip_queue: error creating packet message\n");
283 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
285 int status = -EINVAL;
286 struct sk_buff *nskb;
287 struct ipq_queue_entry *entry;
289 if (copy_mode == IPQ_COPY_NONE)
292 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
294 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
301 nskb = ipq_build_packet_message(entry, &status);
305 write_lock_bh(&queue_lock);
308 goto err_out_free_nskb;
310 if (queue_total >= queue_maxlen) {
314 printk (KERN_WARNING "ip_queue: full at %d entries, "
315 "dropping packets(s). Dropped: %d\n", queue_total,
317 goto err_out_free_nskb;
320 /* netlink_unicast will either free the nskb or attach it to a socket */
321 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
323 queue_user_dropped++;
327 __ipq_enqueue_entry(entry);
329 write_unlock_bh(&queue_lock);
336 write_unlock_bh(&queue_lock);
344 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
347 struct iphdr *user_iph = (struct iphdr *)v->payload;
349 if (v->data_len < sizeof(*user_iph))
351 diff = v->data_len - e->skb->len;
353 skb_trim(e->skb, v->data_len);
355 if (v->data_len > 0xFFFF)
357 if (diff > skb_tailroom(e->skb)) {
358 struct sk_buff *newskb;
360 newskb = skb_copy_expand(e->skb,
361 skb_headroom(e->skb),
364 if (newskb == NULL) {
365 printk(KERN_WARNING "ip_queue: OOM "
366 "in mangle, dropping packet\n");
370 skb_set_owner_w(newskb, e->skb->sk);
374 skb_put(e->skb, diff);
376 if (!skb_make_writable(&e->skb, v->data_len))
378 memcpy(e->skb->data, v->payload, v->data_len);
379 e->skb->ip_summed = CHECKSUM_NONE;
385 id_cmp(struct ipq_queue_entry *e, unsigned long id)
387 return (id == (unsigned long )e);
391 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
393 struct ipq_queue_entry *entry;
395 if (vmsg->value > NF_MAX_VERDICT)
398 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
402 int verdict = vmsg->value;
404 if (vmsg->data_len && vmsg->data_len == len)
405 if (ipq_mangle_ipv4(vmsg, entry) < 0)
408 ipq_issue_verdict(entry, verdict);
414 ipq_set_mode(unsigned char mode, unsigned int range)
418 write_lock_bh(&queue_lock);
419 status = __ipq_set_mode(mode, range);
420 write_unlock_bh(&queue_lock);
425 ipq_receive_peer(struct ipq_peer_msg *pmsg,
426 unsigned char type, unsigned int len)
430 if (len < sizeof(*pmsg))
435 status = ipq_set_mode(pmsg->msg.mode.value,
436 pmsg->msg.mode.range);
440 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
443 status = ipq_set_verdict(&pmsg->msg.verdict,
444 len - sizeof(*pmsg));
453 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
455 if (entry->info->indev)
456 if (entry->info->indev->ifindex == ifindex)
459 if (entry->info->outdev)
460 if (entry->info->outdev->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 = (struct nlmsghdr *)skb->data;
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))
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 skblen - NLMSG_LENGTH(0));
528 RCV_SKB_FAIL(status);
530 if (flags & NLM_F_ACK)
531 netlink_ack(skb, nlh, 0);
536 ipq_rcv_sk(struct sock *sk, int len)
543 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
544 skb = skb_dequeue(&sk->sk_receive_queue);
553 ipq_rcv_dev_event(struct notifier_block *this,
554 unsigned long event, void *ptr)
556 struct net_device *dev = ptr;
558 /* Drop any packets associated with the downed device */
559 if (event == NETDEV_DOWN)
560 ipq_dev_drop(dev->ifindex);
564 static struct notifier_block ipq_dev_notifier = {
565 .notifier_call = ipq_rcv_dev_event,
569 ipq_rcv_nl_event(struct notifier_block *this,
570 unsigned long event, void *ptr)
572 struct netlink_notify *n = ptr;
574 if (event == NETLINK_URELEASE &&
575 n->protocol == NETLINK_FIREWALL && n->pid) {
576 write_lock_bh(&queue_lock);
577 if (n->pid == peer_pid)
579 write_unlock_bh(&queue_lock);
584 static struct notifier_block ipq_nl_notifier = {
585 .notifier_call = ipq_rcv_nl_event,
588 static struct ctl_table_header *ipq_sysctl_header;
590 static ctl_table ipq_table[] = {
592 .ctl_name = NET_IPQ_QMAX,
593 .procname = NET_IPQ_QMAX_NAME,
594 .data = &queue_maxlen,
595 .maxlen = sizeof(queue_maxlen),
597 .proc_handler = proc_dointvec
602 static ctl_table ipq_dir_table[] = {
604 .ctl_name = NET_IPV4,
612 static ctl_table ipq_root_table[] = {
617 .child = ipq_dir_table
622 #ifdef CONFIG_PROC_FS
624 ipq_get_info(char *buffer, char **start, off_t offset, int length)
628 read_lock_bh(&queue_lock);
630 len = sprintf(buffer,
634 "Queue length : %u\n"
635 "Queue max. length : %u\n"
636 "Queue dropped : %u\n"
637 "Netlink dropped : %u\n",
646 read_unlock_bh(&queue_lock);
648 *start = buffer + offset;
656 #endif /* CONFIG_PROC_FS */
659 init_or_cleanup(int init)
661 int status = -ENOMEM;
662 struct proc_dir_entry *proc;
667 netlink_register_notifier(&ipq_nl_notifier);
668 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk,
671 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
672 goto cleanup_netlink_notifier;
675 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
677 proc->owner = THIS_MODULE;
679 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
683 register_netdevice_notifier(&ipq_dev_notifier);
684 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
686 status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
688 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
694 nf_unregister_queue_handler(PF_INET);
699 unregister_sysctl_table(ipq_sysctl_header);
700 unregister_netdevice_notifier(&ipq_dev_notifier);
701 proc_net_remove(IPQ_PROC_FS_NAME);
704 sock_release(ipqnl->sk_socket);
708 cleanup_netlink_notifier:
709 netlink_unregister_notifier(&ipq_nl_notifier);
713 static int __init init(void)
716 return init_or_cleanup(1);
719 static void __exit fini(void)
724 MODULE_DESCRIPTION("IPv4 packet queue handler");
725 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
726 MODULE_LICENSE("GPL");