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>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * 2000-03-27: Simplified code (thanks to Andi Kleen for clues).
12 * 2000-05-20: Fixed notifier problems (following Miguel Freitas' report).
13 * 2000-06-19: Fixed so nfmark is copied to metadata (reported by Sebastian
15 * 2000-08-01: Added Nick Williams' MAC support.
16 * 2002-06-25: Code cleanup.
17 * 2005-01-10: Added /proc counter for dropped packets; fixed so
18 * packets aren't delivered to user space if they're going
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
26 #include <linux/notifier.h>
27 #include <linux/netdevice.h>
28 #include <linux/netfilter.h>
29 #include <linux/netfilter_ipv4/ip_queue.h>
30 #include <linux/netfilter_ipv4/ip_tables.h>
31 #include <linux/netlink.h>
32 #include <linux/spinlock.h>
33 #include <linux/sysctl.h>
34 #include <linux/proc_fs.h>
35 #include <linux/security.h>
37 #include <net/route.h>
39 #define IPQ_QMAX_DEFAULT 1024
40 #define IPQ_PROC_FS_NAME "ip_queue"
41 #define NET_IPQ_QMAX 2088
42 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
50 struct ipq_queue_entry {
51 struct list_head list;
54 struct ipq_rt_info rt_info;
57 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
59 static unsigned char copy_mode = IPQ_COPY_NONE;
60 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
61 static DEFINE_RWLOCK(queue_lock);
63 static unsigned int copy_range;
64 static unsigned int queue_total;
65 static unsigned int queue_dropped = 0;
66 static unsigned int queue_user_dropped = 0;
67 static struct sock *ipqnl;
68 static LIST_HEAD(queue_list);
69 static DECLARE_MUTEX(ipqnl_sem);
72 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
74 nf_reinject(entry->skb, entry->info, verdict);
79 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
81 list_add(&entry->list, &queue_list);
86 * Find and return a queued entry matched by cmpfn, or return the last
87 * entry if cmpfn is NULL.
89 static inline struct ipq_queue_entry *
90 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
94 list_for_each_prev(p, &queue_list) {
95 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
97 if (!cmpfn || cmpfn(entry, data))
104 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
106 list_del(&entry->list);
110 static inline struct ipq_queue_entry *
111 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
113 struct ipq_queue_entry *entry;
115 entry = __ipq_find_entry(cmpfn, data);
119 __ipq_dequeue_entry(entry);
125 __ipq_flush(int verdict)
127 struct ipq_queue_entry *entry;
129 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
130 ipq_issue_verdict(entry, verdict);
134 __ipq_set_mode(unsigned char mode, unsigned int range)
145 case IPQ_COPY_PACKET:
148 if (copy_range > 0xFFFF)
163 net_disable_timestamp();
164 __ipq_set_mode(IPQ_COPY_NONE, 0);
165 __ipq_flush(NF_DROP);
168 static struct ipq_queue_entry *
169 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
171 struct ipq_queue_entry *entry;
173 write_lock_bh(&queue_lock);
174 entry = __ipq_find_dequeue_entry(cmpfn, data);
175 write_unlock_bh(&queue_lock);
180 ipq_flush(int verdict)
182 write_lock_bh(&queue_lock);
183 __ipq_flush(verdict);
184 write_unlock_bh(&queue_lock);
187 static struct sk_buff *
188 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
190 unsigned char *old_tail;
194 struct ipq_packet_msg *pmsg;
195 struct nlmsghdr *nlh;
197 read_lock_bh(&queue_lock);
202 size = NLMSG_SPACE(sizeof(*pmsg));
206 case IPQ_COPY_PACKET:
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);
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 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
235 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
236 pmsg->mark = entry->skb->nfmark;
237 pmsg->hook = entry->info->hook;
238 pmsg->hw_protocol = entry->skb->protocol;
240 if (entry->info->indev)
241 strcpy(pmsg->indev_name, entry->info->indev->name);
243 pmsg->indev_name[0] = '\0';
245 if (entry->info->outdev)
246 strcpy(pmsg->outdev_name, entry->info->outdev->name);
248 pmsg->outdev_name[0] = '\0';
250 if (entry->info->indev && entry->skb->dev) {
251 pmsg->hw_type = entry->skb->dev->type;
252 if (entry->skb->dev->hard_header_parse)
254 entry->skb->dev->hard_header_parse(entry->skb,
259 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
262 nlh->nlmsg_len = skb->tail - old_tail;
269 printk(KERN_ERR "ip_queue: error creating packet message\n");
274 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, 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 if (entry->info->hook == NF_IP_LOCAL_OUT) {
293 struct iphdr *iph = skb->nh.iph;
295 entry->rt_info.tos = iph->tos;
296 entry->rt_info.daddr = iph->daddr;
297 entry->rt_info.saddr = iph->saddr;
300 nskb = ipq_build_packet_message(entry, &status);
304 write_lock_bh(&queue_lock);
307 goto err_out_free_nskb;
309 if (queue_total >= queue_maxlen) {
313 printk (KERN_WARNING "ip_queue: full at %d entries, "
314 "dropping packets(s). Dropped: %d\n", queue_total,
316 goto err_out_free_nskb;
319 /* netlink_unicast will either free the nskb or attach it to a socket */
320 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
322 queue_user_dropped++;
326 __ipq_enqueue_entry(entry);
328 write_unlock_bh(&queue_lock);
335 write_unlock_bh(&queue_lock);
343 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
346 struct iphdr *user_iph = (struct iphdr *)v->payload;
348 if (v->data_len < sizeof(*user_iph))
350 diff = v->data_len - e->skb->len;
352 skb_trim(e->skb, v->data_len);
354 if (v->data_len > 0xFFFF)
356 if (diff > skb_tailroom(e->skb)) {
357 struct sk_buff *newskb;
359 newskb = skb_copy_expand(e->skb,
360 skb_headroom(e->skb),
363 if (newskb == NULL) {
364 printk(KERN_WARNING "ip_queue: OOM "
365 "in mangle, dropping packet\n");
369 skb_set_owner_w(newskb, e->skb->sk);
373 skb_put(e->skb, diff);
375 if (!skb_ip_make_writable(&e->skb, v->data_len))
377 memcpy(e->skb->data, v->payload, v->data_len);
378 e->skb->nfcache |= NFC_ALTERED;
381 * Extra routing may needed on local out, as the QUEUE target never
382 * returns control to the table.
384 if (e->info->hook == NF_IP_LOCAL_OUT) {
385 struct iphdr *iph = e->skb->nh.iph;
387 if (!(iph->tos == e->rt_info.tos
388 && iph->daddr == e->rt_info.daddr
389 && iph->saddr == e->rt_info.saddr))
390 return ip_route_me_harder(&e->skb);
396 id_cmp(struct ipq_queue_entry *e, unsigned long id)
398 return (id == (unsigned long )e);
402 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
404 struct ipq_queue_entry *entry;
406 if (vmsg->value > NF_MAX_VERDICT)
409 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
413 int verdict = vmsg->value;
415 if (vmsg->data_len && vmsg->data_len == len)
416 if (ipq_mangle_ipv4(vmsg, entry) < 0)
419 ipq_issue_verdict(entry, verdict);
425 ipq_set_mode(unsigned char mode, unsigned int range)
429 write_lock_bh(&queue_lock);
430 status = __ipq_set_mode(mode, range);
431 write_unlock_bh(&queue_lock);
436 ipq_receive_peer(struct ipq_peer_msg *pmsg,
437 unsigned char type, unsigned int len)
441 if (len < sizeof(*pmsg))
446 status = ipq_set_mode(pmsg->msg.mode.value,
447 pmsg->msg.mode.range);
451 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
454 status = ipq_set_verdict(&pmsg->msg.verdict,
455 len - sizeof(*pmsg));
464 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
466 if (entry->info->indev)
467 if (entry->info->indev->ifindex == ifindex)
470 if (entry->info->outdev)
471 if (entry->info->outdev->ifindex == ifindex)
478 ipq_dev_drop(int ifindex)
480 struct ipq_queue_entry *entry;
482 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
483 ipq_issue_verdict(entry, NF_DROP);
486 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
489 ipq_rcv_skb(struct sk_buff *skb)
491 int status, type, pid, flags, nlmsglen, skblen;
492 struct nlmsghdr *nlh;
495 if (skblen < sizeof(*nlh))
498 nlh = (struct nlmsghdr *)skb->data;
499 nlmsglen = nlh->nlmsg_len;
500 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
503 pid = nlh->nlmsg_pid;
504 flags = nlh->nlmsg_flags;
506 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
507 RCV_SKB_FAIL(-EINVAL);
509 if (flags & MSG_TRUNC)
510 RCV_SKB_FAIL(-ECOMM);
512 type = nlh->nlmsg_type;
513 if (type < NLMSG_NOOP || type >= IPQM_MAX)
514 RCV_SKB_FAIL(-EINVAL);
516 if (type <= IPQM_BASE)
519 if (security_netlink_recv(skb))
520 RCV_SKB_FAIL(-EPERM);
522 write_lock_bh(&queue_lock);
525 if (peer_pid != pid) {
526 write_unlock_bh(&queue_lock);
527 RCV_SKB_FAIL(-EBUSY);
530 net_enable_timestamp();
534 write_unlock_bh(&queue_lock);
536 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
537 skblen - NLMSG_LENGTH(0));
539 RCV_SKB_FAIL(status);
541 if (flags & NLM_F_ACK)
542 netlink_ack(skb, nlh, 0);
547 ipq_rcv_sk(struct sock *sk, int len)
554 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
555 skb = skb_dequeue(&sk->sk_receive_queue);
564 ipq_rcv_dev_event(struct notifier_block *this,
565 unsigned long event, void *ptr)
567 struct net_device *dev = ptr;
569 /* Drop any packets associated with the downed device */
570 if (event == NETDEV_DOWN)
571 ipq_dev_drop(dev->ifindex);
575 static struct notifier_block ipq_dev_notifier = {
576 .notifier_call = ipq_rcv_dev_event,
580 ipq_rcv_nl_event(struct notifier_block *this,
581 unsigned long event, void *ptr)
583 struct netlink_notify *n = ptr;
585 if (event == NETLINK_URELEASE &&
586 n->protocol == NETLINK_FIREWALL && n->pid) {
587 write_lock_bh(&queue_lock);
588 if (n->pid == peer_pid)
590 write_unlock_bh(&queue_lock);
595 static struct notifier_block ipq_nl_notifier = {
596 .notifier_call = ipq_rcv_nl_event,
599 static struct ctl_table_header *ipq_sysctl_header;
601 static ctl_table ipq_table[] = {
603 .ctl_name = NET_IPQ_QMAX,
604 .procname = NET_IPQ_QMAX_NAME,
605 .data = &queue_maxlen,
606 .maxlen = sizeof(queue_maxlen),
608 .proc_handler = proc_dointvec
613 static ctl_table ipq_dir_table[] = {
615 .ctl_name = NET_IPV4,
623 static ctl_table ipq_root_table[] = {
628 .child = ipq_dir_table
633 #ifdef CONFIG_PROC_FS
635 ipq_get_info(char *buffer, char **start, off_t offset, int length)
639 read_lock_bh(&queue_lock);
641 len = sprintf(buffer,
645 "Queue length : %u\n"
646 "Queue max. length : %u\n"
647 "Queue dropped : %u\n"
648 "Netlink dropped : %u\n",
657 read_unlock_bh(&queue_lock);
659 *start = buffer + offset;
667 #endif /* CONFIG_PROC_FS */
670 init_or_cleanup(int init)
672 int status = -ENOMEM;
673 struct proc_dir_entry *proc;
678 netlink_register_notifier(&ipq_nl_notifier);
679 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
681 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
682 goto cleanup_netlink_notifier;
685 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
687 proc->owner = THIS_MODULE;
689 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
693 register_netdevice_notifier(&ipq_dev_notifier);
694 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
696 status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
698 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
704 nf_unregister_queue_handler(PF_INET);
709 unregister_sysctl_table(ipq_sysctl_header);
710 unregister_netdevice_notifier(&ipq_dev_notifier);
711 proc_net_remove(IPQ_PROC_FS_NAME);
714 sock_release(ipqnl->sk_socket);
718 cleanup_netlink_notifier:
719 netlink_unregister_notifier(&ipq_nl_notifier);
723 static int __init init(void)
726 return init_or_cleanup(1);
729 static void __exit fini(void)
734 MODULE_DESCRIPTION("IPv4 packet queue handler");
735 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
736 MODULE_LICENSE("GPL");