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.
19 #include <linux/module.h>
20 #include <linux/skbuff.h>
21 #include <linux/init.h>
23 #include <linux/notifier.h>
24 #include <linux/netdevice.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter_ipv4/ip_queue.h>
27 #include <linux/netfilter_ipv4/ip_tables.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/sysctl.h>
31 #include <linux/proc_fs.h>
32 #include <linux/security.h>
34 #include <net/route.h>
36 #define IPQ_QMAX_DEFAULT 1024
37 #define IPQ_PROC_FS_NAME "ip_queue"
38 #define NET_IPQ_QMAX 2088
39 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
47 struct ipq_queue_entry {
48 struct list_head list;
51 struct ipq_rt_info rt_info;
54 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
56 static unsigned char copy_mode = IPQ_COPY_NONE;
57 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
58 static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
60 static unsigned int copy_range;
61 static unsigned int queue_total;
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 nf_reinject(entry->skb, entry->info, verdict);
74 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
76 if (queue_total >= queue_maxlen) {
78 printk(KERN_WARNING "ip_queue: full at %d entries, "
79 "dropping packet(s).\n", queue_total);
82 list_add(&entry->list, &queue_list);
88 * Find and return a queued entry matched by cmpfn, or return the last
89 * entry if cmpfn is NULL.
91 static inline struct ipq_queue_entry *
92 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
96 list_for_each_prev(p, &queue_list) {
97 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
99 if (!cmpfn || cmpfn(entry, data))
106 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
108 list_del(&entry->list);
112 static inline struct ipq_queue_entry *
113 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
115 struct ipq_queue_entry *entry;
117 entry = __ipq_find_entry(cmpfn, data);
121 __ipq_dequeue_entry(entry);
127 __ipq_flush(int verdict)
129 struct ipq_queue_entry *entry;
131 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
132 ipq_issue_verdict(entry, verdict);
136 __ipq_set_mode(unsigned char mode, unsigned int range)
147 case IPQ_COPY_PACKET:
150 if (copy_range > 0xFFFF)
165 __ipq_set_mode(IPQ_COPY_NONE, 0);
166 __ipq_flush(NF_DROP);
169 static struct ipq_queue_entry *
170 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
172 struct ipq_queue_entry *entry;
174 write_lock_bh(&queue_lock);
175 entry = __ipq_find_dequeue_entry(cmpfn, data);
176 write_unlock_bh(&queue_lock);
181 ipq_flush(int verdict)
183 write_lock_bh(&queue_lock);
184 __ipq_flush(verdict);
185 write_unlock_bh(&queue_lock);
188 static struct sk_buff *
189 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
191 unsigned char *old_tail;
195 struct ipq_packet_msg *pmsg;
196 struct nlmsghdr *nlh;
198 read_lock_bh(&queue_lock);
203 size = NLMSG_SPACE(sizeof(*pmsg));
207 case IPQ_COPY_PACKET:
208 if (copy_range == 0 || copy_range > entry->skb->len)
209 data_len = entry->skb->len;
211 data_len = copy_range;
213 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
218 read_unlock_bh(&queue_lock);
222 read_unlock_bh(&queue_lock);
224 skb = alloc_skb(size, GFP_ATOMIC);
229 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
230 pmsg = NLMSG_DATA(nlh);
231 memset(pmsg, 0, sizeof(*pmsg));
233 pmsg->packet_id = (unsigned long )entry;
234 pmsg->data_len = data_len;
235 pmsg->timestamp_sec = entry->skb->stamp.tv_sec;
236 pmsg->timestamp_usec = entry->skb->stamp.tv_usec;
237 pmsg->mark = entry->skb->nfmark;
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 if (entry->skb->dev->hard_header_parse)
255 entry->skb->dev->hard_header_parse(entry->skb,
260 memcpy(pmsg->payload, entry->skb->data, 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 /* netlink_unicast will either free the nskb or attach it to a socket */
310 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
314 status = __ipq_enqueue_entry(entry);
318 write_unlock_bh(&queue_lock);
325 write_unlock_bh(&queue_lock);
333 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
336 struct iphdr *user_iph = (struct iphdr *)v->payload;
338 if (v->data_len < sizeof(*user_iph))
340 diff = v->data_len - e->skb->len;
342 skb_trim(e->skb, v->data_len);
344 if (v->data_len > 0xFFFF)
346 if (diff > skb_tailroom(e->skb)) {
347 struct sk_buff *newskb;
349 newskb = skb_copy_expand(e->skb,
350 skb_headroom(e->skb),
353 if (newskb == NULL) {
354 printk(KERN_WARNING "ip_queue: OOM "
355 "in mangle, dropping packet\n");
359 skb_set_owner_w(newskb, e->skb->sk);
363 skb_put(e->skb, diff);
365 memcpy(e->skb->data, v->payload, v->data_len);
366 e->skb->nfcache |= NFC_ALTERED;
369 * Extra routing may needed on local out, as the QUEUE target never
370 * returns control to the table.
372 if (e->info->hook == NF_IP_LOCAL_OUT) {
373 struct iphdr *iph = e->skb->nh.iph;
375 if (!(iph->tos == e->rt_info.tos
376 && iph->daddr == e->rt_info.daddr
377 && iph->saddr == e->rt_info.saddr))
378 return ip_route_me_harder(&e->skb);
384 id_cmp(struct ipq_queue_entry *e, unsigned long id)
386 return (id == (unsigned long )e);
390 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
392 struct ipq_queue_entry *entry;
394 if (vmsg->value > NF_MAX_VERDICT)
397 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
401 int verdict = vmsg->value;
403 if (vmsg->data_len && vmsg->data_len == len)
404 if (ipq_mangle_ipv4(vmsg, entry) < 0)
407 ipq_issue_verdict(entry, verdict);
413 ipq_set_mode(unsigned char mode, unsigned int range)
417 write_lock_bh(&queue_lock);
418 status = __ipq_set_mode(mode, range);
419 write_unlock_bh(&queue_lock);
424 ipq_receive_peer(struct ipq_peer_msg *pmsg,
425 unsigned char type, unsigned int len)
429 if (len < sizeof(*pmsg))
434 status = ipq_set_mode(pmsg->msg.mode.value,
435 pmsg->msg.mode.range);
439 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
442 status = ipq_set_verdict(&pmsg->msg.verdict,
443 len - sizeof(*pmsg));
452 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
454 if (entry->info->indev)
455 if (entry->info->indev->ifindex == ifindex)
458 if (entry->info->outdev)
459 if (entry->info->outdev->ifindex == ifindex)
466 ipq_dev_drop(int ifindex)
468 struct ipq_queue_entry *entry;
470 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
471 ipq_issue_verdict(entry, NF_DROP);
474 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
477 ipq_rcv_skb(struct sk_buff *skb)
479 int status, type, pid, flags, nlmsglen, skblen;
480 struct nlmsghdr *nlh;
483 if (skblen < sizeof(*nlh))
486 nlh = (struct nlmsghdr *)skb->data;
487 nlmsglen = nlh->nlmsg_len;
488 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
491 pid = nlh->nlmsg_pid;
492 flags = nlh->nlmsg_flags;
494 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
495 RCV_SKB_FAIL(-EINVAL);
497 if (flags & MSG_TRUNC)
498 RCV_SKB_FAIL(-ECOMM);
500 type = nlh->nlmsg_type;
501 if (type < NLMSG_NOOP || type >= IPQM_MAX)
502 RCV_SKB_FAIL(-EINVAL);
504 if (type <= IPQM_BASE)
507 if (security_netlink_recv(skb))
508 RCV_SKB_FAIL(-EPERM);
510 write_lock_bh(&queue_lock);
513 if (peer_pid != pid) {
514 write_unlock_bh(&queue_lock);
515 RCV_SKB_FAIL(-EBUSY);
521 write_unlock_bh(&queue_lock);
523 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
524 skblen - NLMSG_LENGTH(0));
526 RCV_SKB_FAIL(status);
528 if (flags & NLM_F_ACK)
529 netlink_ack(skb, nlh, 0);
534 ipq_rcv_sk(struct sock *sk, int len)
539 if (down_trylock(&ipqnl_sem))
542 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
549 } while (ipqnl && ipqnl->sk_receive_queue.qlen);
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",
642 read_unlock_bh(&queue_lock);
644 *start = buffer + offset;
652 #endif /* CONFIG_PROC_FS */
655 init_or_cleanup(int init)
657 int status = -ENOMEM;
658 struct proc_dir_entry *proc;
663 netlink_register_notifier(&ipq_nl_notifier);
664 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
666 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
667 goto cleanup_netlink_notifier;
670 proc = proc_net_create(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, 0);
681 status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
683 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
689 nf_unregister_queue_handler(PF_INET);
694 unregister_sysctl_table(ipq_sysctl_header);
695 unregister_netdevice_notifier(&ipq_dev_notifier);
696 proc_net_remove(IPQ_PROC_FS_NAME);
699 sock_release(ipqnl->sk_socket);
703 cleanup_netlink_notifier:
704 netlink_unregister_notifier(&ipq_nl_notifier);
708 static int __init init(void)
711 return init_or_cleanup(1);
714 static void __exit fini(void)
719 MODULE_DESCRIPTION("IPv4 packet queue handler");
720 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
721 MODULE_LICENSE("GPL");