Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / netfilter / ip_queue.c
blob9e40dffc204f3438e0dbabc1ed89be0031f13449
1 /*
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
14 * Zander).
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
19 * to be dropped.
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/ip.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>
36 #include <net/sock.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"
44 struct ipq_rt_info {
45 __u8 tos;
46 __u32 daddr;
47 __u32 saddr;
50 struct ipq_queue_entry {
51 struct list_head list;
52 struct nf_info *info;
53 struct sk_buff *skb;
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);
62 static int peer_pid;
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);
71 static void
72 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
74 nf_reinject(entry->skb, entry->info, verdict);
75 kfree(entry);
78 static inline void
79 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
81 list_add(&entry->list, &queue_list);
82 queue_total++;
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)
92 struct list_head *p;
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))
98 return entry;
100 return NULL;
103 static inline void
104 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
106 list_del(&entry->list);
107 queue_total--;
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);
116 if (entry == NULL)
117 return NULL;
119 __ipq_dequeue_entry(entry);
120 return entry;
124 static inline void
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);
133 static inline int
134 __ipq_set_mode(unsigned char mode, unsigned int range)
136 int status = 0;
138 switch(mode) {
139 case IPQ_COPY_NONE:
140 case IPQ_COPY_META:
141 copy_mode = mode;
142 copy_range = 0;
143 break;
145 case IPQ_COPY_PACKET:
146 copy_mode = mode;
147 copy_range = range;
148 if (copy_range > 0xFFFF)
149 copy_range = 0xFFFF;
150 break;
152 default:
153 status = -EINVAL;
156 return status;
159 static inline void
160 __ipq_reset(void)
162 peer_pid = 0;
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);
176 return entry;
179 static void
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;
191 size_t size = 0;
192 size_t data_len = 0;
193 struct sk_buff *skb;
194 struct ipq_packet_msg *pmsg;
195 struct nlmsghdr *nlh;
197 read_lock_bh(&queue_lock);
199 switch (copy_mode) {
200 case IPQ_COPY_META:
201 case IPQ_COPY_NONE:
202 size = NLMSG_SPACE(sizeof(*pmsg));
203 data_len = 0;
204 break;
206 case IPQ_COPY_PACKET:
207 if (copy_range == 0 || copy_range > entry->skb->len)
208 data_len = entry->skb->len;
209 else
210 data_len = copy_range;
212 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
213 break;
215 default:
216 *errp = -EINVAL;
217 read_unlock_bh(&queue_lock);
218 return NULL;
221 read_unlock_bh(&queue_lock);
223 skb = alloc_skb(size, GFP_ATOMIC);
224 if (!skb)
225 goto nlmsg_failure;
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 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);
242 else
243 pmsg->indev_name[0] = '\0';
245 if (entry->info->outdev)
246 strcpy(pmsg->outdev_name, entry->info->outdev->name);
247 else
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)
253 pmsg->hw_addrlen =
254 entry->skb->dev->hard_header_parse(entry->skb,
255 pmsg->hw_addr);
258 if (data_len)
259 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
260 BUG();
262 nlh->nlmsg_len = skb->tail - old_tail;
263 return skb;
265 nlmsg_failure:
266 if (skb)
267 kfree_skb(skb);
268 *errp = -EINVAL;
269 printk(KERN_ERR "ip_queue: error creating packet message\n");
270 return NULL;
273 static int
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)
281 return -EAGAIN;
283 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
284 if (entry == NULL) {
285 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
286 return -ENOMEM;
289 entry->info = info;
290 entry->skb = skb;
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);
301 if (nskb == NULL)
302 goto err_out_free;
304 write_lock_bh(&queue_lock);
306 if (!peer_pid)
307 goto err_out_free_nskb;
309 if (queue_total >= queue_maxlen) {
310 queue_dropped++;
311 status = -ENOSPC;
312 if (net_ratelimit())
313 printk (KERN_WARNING "ip_queue: full at %d entries, "
314 "dropping packets(s). Dropped: %d\n", queue_total,
315 queue_dropped);
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);
321 if (status < 0) {
322 queue_user_dropped++;
323 goto err_out_unlock;
326 __ipq_enqueue_entry(entry);
328 write_unlock_bh(&queue_lock);
329 return status;
331 err_out_free_nskb:
332 kfree_skb(nskb);
334 err_out_unlock:
335 write_unlock_bh(&queue_lock);
337 err_out_free:
338 kfree(entry);
339 return status;
342 static int
343 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
345 int diff;
346 struct iphdr *user_iph = (struct iphdr *)v->payload;
348 if (v->data_len < sizeof(*user_iph))
349 return 0;
350 diff = v->data_len - e->skb->len;
351 if (diff < 0)
352 skb_trim(e->skb, v->data_len);
353 else if (diff > 0) {
354 if (v->data_len > 0xFFFF)
355 return -EINVAL;
356 if (diff > skb_tailroom(e->skb)) {
357 struct sk_buff *newskb;
359 newskb = skb_copy_expand(e->skb,
360 skb_headroom(e->skb),
361 diff,
362 GFP_ATOMIC);
363 if (newskb == NULL) {
364 printk(KERN_WARNING "ip_queue: OOM "
365 "in mangle, dropping packet\n");
366 return -ENOMEM;
368 if (e->skb->sk)
369 skb_set_owner_w(newskb, e->skb->sk);
370 kfree_skb(e->skb);
371 e->skb = newskb;
373 skb_put(e->skb, diff);
375 if (!skb_ip_make_writable(&e->skb, v->data_len))
376 return -ENOMEM;
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);
392 return 0;
395 static inline int
396 id_cmp(struct ipq_queue_entry *e, unsigned long id)
398 return (id == (unsigned long )e);
401 static int
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)
407 return -EINVAL;
409 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
410 if (entry == NULL)
411 return -ENOENT;
412 else {
413 int verdict = vmsg->value;
415 if (vmsg->data_len && vmsg->data_len == len)
416 if (ipq_mangle_ipv4(vmsg, entry) < 0)
417 verdict = NF_DROP;
419 ipq_issue_verdict(entry, verdict);
420 return 0;
424 static int
425 ipq_set_mode(unsigned char mode, unsigned int range)
427 int status;
429 write_lock_bh(&queue_lock);
430 status = __ipq_set_mode(mode, range);
431 write_unlock_bh(&queue_lock);
432 return status;
435 static int
436 ipq_receive_peer(struct ipq_peer_msg *pmsg,
437 unsigned char type, unsigned int len)
439 int status = 0;
441 if (len < sizeof(*pmsg))
442 return -EINVAL;
444 switch (type) {
445 case IPQM_MODE:
446 status = ipq_set_mode(pmsg->msg.mode.value,
447 pmsg->msg.mode.range);
448 break;
450 case IPQM_VERDICT:
451 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
452 status = -EINVAL;
453 else
454 status = ipq_set_verdict(&pmsg->msg.verdict,
455 len - sizeof(*pmsg));
456 break;
457 default:
458 status = -EINVAL;
460 return status;
463 static int
464 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
466 if (entry->info->indev)
467 if (entry->info->indev->ifindex == ifindex)
468 return 1;
470 if (entry->info->outdev)
471 if (entry->info->outdev->ifindex == ifindex)
472 return 1;
474 return 0;
477 static void
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)
488 static inline void
489 ipq_rcv_skb(struct sk_buff *skb)
491 int status, type, pid, flags, nlmsglen, skblen;
492 struct nlmsghdr *nlh;
494 skblen = skb->len;
495 if (skblen < sizeof(*nlh))
496 return;
498 nlh = (struct nlmsghdr *)skb->data;
499 nlmsglen = nlh->nlmsg_len;
500 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
501 return;
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)
517 return;
519 if (security_netlink_recv(skb))
520 RCV_SKB_FAIL(-EPERM);
522 write_lock_bh(&queue_lock);
524 if (peer_pid) {
525 if (peer_pid != pid) {
526 write_unlock_bh(&queue_lock);
527 RCV_SKB_FAIL(-EBUSY);
529 } else {
530 net_enable_timestamp();
531 peer_pid = pid;
534 write_unlock_bh(&queue_lock);
536 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
537 skblen - NLMSG_LENGTH(0));
538 if (status < 0)
539 RCV_SKB_FAIL(status);
541 if (flags & NLM_F_ACK)
542 netlink_ack(skb, nlh, 0);
543 return;
546 static void
547 ipq_rcv_sk(struct sock *sk, int len)
549 do {
550 struct sk_buff *skb;
552 if (down_trylock(&ipqnl_sem))
553 return;
555 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
556 ipq_rcv_skb(skb);
557 kfree_skb(skb);
560 up(&ipqnl_sem);
562 } while (ipqnl && ipqnl->sk_receive_queue.qlen);
565 static int
566 ipq_rcv_dev_event(struct notifier_block *this,
567 unsigned long event, void *ptr)
569 struct net_device *dev = ptr;
571 /* Drop any packets associated with the downed device */
572 if (event == NETDEV_DOWN)
573 ipq_dev_drop(dev->ifindex);
574 return NOTIFY_DONE;
577 static struct notifier_block ipq_dev_notifier = {
578 .notifier_call = ipq_rcv_dev_event,
581 static int
582 ipq_rcv_nl_event(struct notifier_block *this,
583 unsigned long event, void *ptr)
585 struct netlink_notify *n = ptr;
587 if (event == NETLINK_URELEASE &&
588 n->protocol == NETLINK_FIREWALL && n->pid) {
589 write_lock_bh(&queue_lock);
590 if (n->pid == peer_pid)
591 __ipq_reset();
592 write_unlock_bh(&queue_lock);
594 return NOTIFY_DONE;
597 static struct notifier_block ipq_nl_notifier = {
598 .notifier_call = ipq_rcv_nl_event,
601 static struct ctl_table_header *ipq_sysctl_header;
603 static ctl_table ipq_table[] = {
605 .ctl_name = NET_IPQ_QMAX,
606 .procname = NET_IPQ_QMAX_NAME,
607 .data = &queue_maxlen,
608 .maxlen = sizeof(queue_maxlen),
609 .mode = 0644,
610 .proc_handler = proc_dointvec
612 { .ctl_name = 0 }
615 static ctl_table ipq_dir_table[] = {
617 .ctl_name = NET_IPV4,
618 .procname = "ipv4",
619 .mode = 0555,
620 .child = ipq_table
622 { .ctl_name = 0 }
625 static ctl_table ipq_root_table[] = {
627 .ctl_name = CTL_NET,
628 .procname = "net",
629 .mode = 0555,
630 .child = ipq_dir_table
632 { .ctl_name = 0 }
635 #ifdef CONFIG_PROC_FS
636 static int
637 ipq_get_info(char *buffer, char **start, off_t offset, int length)
639 int len;
641 read_lock_bh(&queue_lock);
643 len = sprintf(buffer,
644 "Peer PID : %d\n"
645 "Copy mode : %hu\n"
646 "Copy range : %u\n"
647 "Queue length : %u\n"
648 "Queue max. length : %u\n"
649 "Queue dropped : %u\n"
650 "Netlink dropped : %u\n",
651 peer_pid,
652 copy_mode,
653 copy_range,
654 queue_total,
655 queue_maxlen,
656 queue_dropped,
657 queue_user_dropped);
659 read_unlock_bh(&queue_lock);
661 *start = buffer + offset;
662 len -= offset;
663 if (len > length)
664 len = length;
665 else if (len < 0)
666 len = 0;
667 return len;
669 #endif /* CONFIG_PROC_FS */
671 static int
672 init_or_cleanup(int init)
674 int status = -ENOMEM;
675 struct proc_dir_entry *proc;
677 if (!init)
678 goto cleanup;
680 netlink_register_notifier(&ipq_nl_notifier);
681 ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk);
682 if (ipqnl == NULL) {
683 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
684 goto cleanup_netlink_notifier;
687 proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
688 if (proc)
689 proc->owner = THIS_MODULE;
690 else {
691 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
692 goto cleanup_ipqnl;
695 register_netdevice_notifier(&ipq_dev_notifier);
696 ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
698 status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
699 if (status < 0) {
700 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
701 goto cleanup_sysctl;
703 return status;
705 cleanup:
706 nf_unregister_queue_handler(PF_INET);
707 synchronize_net();
708 ipq_flush(NF_DROP);
710 cleanup_sysctl:
711 unregister_sysctl_table(ipq_sysctl_header);
712 unregister_netdevice_notifier(&ipq_dev_notifier);
713 proc_net_remove(IPQ_PROC_FS_NAME);
715 cleanup_ipqnl:
716 sock_release(ipqnl->sk_socket);
717 down(&ipqnl_sem);
718 up(&ipqnl_sem);
720 cleanup_netlink_notifier:
721 netlink_unregister_notifier(&ipq_nl_notifier);
722 return status;
725 static int __init init(void)
728 return init_or_cleanup(1);
731 static void __exit fini(void)
733 init_or_cleanup(0);
736 MODULE_DESCRIPTION("IPv4 packet queue handler");
737 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
738 MODULE_LICENSE("GPL");
740 module_init(init);
741 module_exit(fini);