[NET]: Support multiple network namespaces with netlink
[linux-2.6/s3c2410-cpufreq.git] / net / ipv4 / netfilter / ip_queue.c
blob82fda92e6b97b106d0dad050a7f63774d0551483
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>
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 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/init.h>
15 #include <linux/ip.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4/ip_queue.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netlink.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysctl.h>
24 #include <linux/proc_fs.h>
25 #include <linux/security.h>
26 #include <linux/mutex.h>
27 #include <net/net_namespace.h>
28 #include <net/sock.h>
29 #include <net/route.h>
31 #define IPQ_QMAX_DEFAULT 1024
32 #define IPQ_PROC_FS_NAME "ip_queue"
33 #define NET_IPQ_QMAX 2088
34 #define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
36 struct ipq_queue_entry {
37 struct list_head list;
38 struct nf_info *info;
39 struct sk_buff *skb;
42 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
44 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
45 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
46 static DEFINE_RWLOCK(queue_lock);
47 static int peer_pid __read_mostly;
48 static unsigned int copy_range __read_mostly;
49 static unsigned int queue_total;
50 static unsigned int queue_dropped = 0;
51 static unsigned int queue_user_dropped = 0;
52 static struct sock *ipqnl __read_mostly;
53 static LIST_HEAD(queue_list);
54 static DEFINE_MUTEX(ipqnl_mutex);
56 static void
57 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
59 /* TCP input path (and probably other bits) assume to be called
60 * from softirq context, not from syscall, like ipq_issue_verdict is
61 * called. TCP input path deadlocks with locks taken from timer
62 * softirq, e.g. We therefore emulate this by local_bh_disable() */
64 local_bh_disable();
65 nf_reinject(entry->skb, entry->info, verdict);
66 local_bh_enable();
68 kfree(entry);
71 static inline void
72 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
74 list_add(&entry->list, &queue_list);
75 queue_total++;
79 * Find and return a queued entry matched by cmpfn, or return the last
80 * entry if cmpfn is NULL.
82 static inline struct ipq_queue_entry *
83 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
85 struct list_head *p;
87 list_for_each_prev(p, &queue_list) {
88 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
90 if (!cmpfn || cmpfn(entry, data))
91 return entry;
93 return NULL;
96 static inline void
97 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
99 list_del(&entry->list);
100 queue_total--;
103 static inline struct ipq_queue_entry *
104 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
106 struct ipq_queue_entry *entry;
108 entry = __ipq_find_entry(cmpfn, data);
109 if (entry == NULL)
110 return NULL;
112 __ipq_dequeue_entry(entry);
113 return entry;
117 static inline void
118 __ipq_flush(int verdict)
120 struct ipq_queue_entry *entry;
122 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
123 ipq_issue_verdict(entry, verdict);
126 static inline int
127 __ipq_set_mode(unsigned char mode, unsigned int range)
129 int status = 0;
131 switch(mode) {
132 case IPQ_COPY_NONE:
133 case IPQ_COPY_META:
134 copy_mode = mode;
135 copy_range = 0;
136 break;
138 case IPQ_COPY_PACKET:
139 copy_mode = mode;
140 copy_range = range;
141 if (copy_range > 0xFFFF)
142 copy_range = 0xFFFF;
143 break;
145 default:
146 status = -EINVAL;
149 return status;
152 static inline void
153 __ipq_reset(void)
155 peer_pid = 0;
156 net_disable_timestamp();
157 __ipq_set_mode(IPQ_COPY_NONE, 0);
158 __ipq_flush(NF_DROP);
161 static struct ipq_queue_entry *
162 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
164 struct ipq_queue_entry *entry;
166 write_lock_bh(&queue_lock);
167 entry = __ipq_find_dequeue_entry(cmpfn, data);
168 write_unlock_bh(&queue_lock);
169 return entry;
172 static void
173 ipq_flush(int verdict)
175 write_lock_bh(&queue_lock);
176 __ipq_flush(verdict);
177 write_unlock_bh(&queue_lock);
180 static struct sk_buff *
181 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
183 sk_buff_data_t old_tail;
184 size_t size = 0;
185 size_t data_len = 0;
186 struct sk_buff *skb;
187 struct ipq_packet_msg *pmsg;
188 struct nlmsghdr *nlh;
189 struct timeval tv;
191 read_lock_bh(&queue_lock);
193 switch (copy_mode) {
194 case IPQ_COPY_META:
195 case IPQ_COPY_NONE:
196 size = NLMSG_SPACE(sizeof(*pmsg));
197 data_len = 0;
198 break;
200 case IPQ_COPY_PACKET:
201 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
202 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
203 (*errp = skb_checksum_help(entry->skb))) {
204 read_unlock_bh(&queue_lock);
205 return NULL;
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 tv = ktime_to_timeval(entry->skb->tstamp);
235 pmsg->timestamp_sec = tv.tv_sec;
236 pmsg->timestamp_usec = tv.tv_usec;
237 pmsg->mark = entry->skb->mark;
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);
243 else
244 pmsg->indev_name[0] = '\0';
246 if (entry->info->outdev)
247 strcpy(pmsg->outdev_name, entry->info->outdev->name);
248 else
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)
254 pmsg->hw_addrlen =
255 entry->skb->dev->hard_header_parse(entry->skb,
256 pmsg->hw_addr);
259 if (data_len)
260 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
261 BUG();
263 nlh->nlmsg_len = skb->tail - old_tail;
264 return skb;
266 nlmsg_failure:
267 if (skb)
268 kfree_skb(skb);
269 *errp = -EINVAL;
270 printk(KERN_ERR "ip_queue: error creating packet message\n");
271 return NULL;
274 static int
275 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
276 unsigned int queuenum, void *data)
278 int status = -EINVAL;
279 struct sk_buff *nskb;
280 struct ipq_queue_entry *entry;
282 if (copy_mode == IPQ_COPY_NONE)
283 return -EAGAIN;
285 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
286 if (entry == NULL) {
287 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
288 return -ENOMEM;
291 entry->info = info;
292 entry->skb = skb;
294 nskb = ipq_build_packet_message(entry, &status);
295 if (nskb == NULL)
296 goto err_out_free;
298 write_lock_bh(&queue_lock);
300 if (!peer_pid)
301 goto err_out_free_nskb;
303 if (queue_total >= queue_maxlen) {
304 queue_dropped++;
305 status = -ENOSPC;
306 if (net_ratelimit())
307 printk (KERN_WARNING "ip_queue: full at %d entries, "
308 "dropping packets(s). Dropped: %d\n", queue_total,
309 queue_dropped);
310 goto err_out_free_nskb;
313 /* netlink_unicast will either free the nskb or attach it to a socket */
314 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
315 if (status < 0) {
316 queue_user_dropped++;
317 goto err_out_unlock;
320 __ipq_enqueue_entry(entry);
322 write_unlock_bh(&queue_lock);
323 return status;
325 err_out_free_nskb:
326 kfree_skb(nskb);
328 err_out_unlock:
329 write_unlock_bh(&queue_lock);
331 err_out_free:
332 kfree(entry);
333 return status;
336 static int
337 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
339 int diff;
340 struct iphdr *user_iph = (struct iphdr *)v->payload;
342 if (v->data_len < sizeof(*user_iph))
343 return 0;
344 diff = v->data_len - e->skb->len;
345 if (diff < 0) {
346 if (pskb_trim(e->skb, v->data_len))
347 return -ENOMEM;
348 } else if (diff > 0) {
349 if (v->data_len > 0xFFFF)
350 return -EINVAL;
351 if (diff > skb_tailroom(e->skb)) {
352 struct sk_buff *newskb;
354 newskb = skb_copy_expand(e->skb,
355 skb_headroom(e->skb),
356 diff,
357 GFP_ATOMIC);
358 if (newskb == NULL) {
359 printk(KERN_WARNING "ip_queue: OOM "
360 "in mangle, dropping packet\n");
361 return -ENOMEM;
363 if (e->skb->sk)
364 skb_set_owner_w(newskb, e->skb->sk);
365 kfree_skb(e->skb);
366 e->skb = newskb;
368 skb_put(e->skb, diff);
370 if (!skb_make_writable(&e->skb, v->data_len))
371 return -ENOMEM;
372 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
373 e->skb->ip_summed = CHECKSUM_NONE;
375 return 0;
378 static inline int
379 id_cmp(struct ipq_queue_entry *e, unsigned long id)
381 return (id == (unsigned long )e);
384 static int
385 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
387 struct ipq_queue_entry *entry;
389 if (vmsg->value > NF_MAX_VERDICT)
390 return -EINVAL;
392 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
393 if (entry == NULL)
394 return -ENOENT;
395 else {
396 int verdict = vmsg->value;
398 if (vmsg->data_len && vmsg->data_len == len)
399 if (ipq_mangle_ipv4(vmsg, entry) < 0)
400 verdict = NF_DROP;
402 ipq_issue_verdict(entry, verdict);
403 return 0;
407 static int
408 ipq_set_mode(unsigned char mode, unsigned int range)
410 int status;
412 write_lock_bh(&queue_lock);
413 status = __ipq_set_mode(mode, range);
414 write_unlock_bh(&queue_lock);
415 return status;
418 static int
419 ipq_receive_peer(struct ipq_peer_msg *pmsg,
420 unsigned char type, unsigned int len)
422 int status = 0;
424 if (len < sizeof(*pmsg))
425 return -EINVAL;
427 switch (type) {
428 case IPQM_MODE:
429 status = ipq_set_mode(pmsg->msg.mode.value,
430 pmsg->msg.mode.range);
431 break;
433 case IPQM_VERDICT:
434 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
435 status = -EINVAL;
436 else
437 status = ipq_set_verdict(&pmsg->msg.verdict,
438 len - sizeof(*pmsg));
439 break;
440 default:
441 status = -EINVAL;
443 return status;
446 static int
447 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
449 if (entry->info->indev)
450 if (entry->info->indev->ifindex == ifindex)
451 return 1;
452 if (entry->info->outdev)
453 if (entry->info->outdev->ifindex == ifindex)
454 return 1;
455 #ifdef CONFIG_BRIDGE_NETFILTER
456 if (entry->skb->nf_bridge) {
457 if (entry->skb->nf_bridge->physindev &&
458 entry->skb->nf_bridge->physindev->ifindex == ifindex)
459 return 1;
460 if (entry->skb->nf_bridge->physoutdev &&
461 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
462 return 1;
464 #endif
465 return 0;
468 static void
469 ipq_dev_drop(int ifindex)
471 struct ipq_queue_entry *entry;
473 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
474 ipq_issue_verdict(entry, NF_DROP);
477 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
479 static inline void
480 ipq_rcv_skb(struct sk_buff *skb)
482 int status, type, pid, flags, nlmsglen, skblen;
483 struct nlmsghdr *nlh;
485 skblen = skb->len;
486 if (skblen < sizeof(*nlh))
487 return;
489 nlh = nlmsg_hdr(skb);
490 nlmsglen = nlh->nlmsg_len;
491 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
492 return;
494 pid = nlh->nlmsg_pid;
495 flags = nlh->nlmsg_flags;
497 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
498 RCV_SKB_FAIL(-EINVAL);
500 if (flags & MSG_TRUNC)
501 RCV_SKB_FAIL(-ECOMM);
503 type = nlh->nlmsg_type;
504 if (type < NLMSG_NOOP || type >= IPQM_MAX)
505 RCV_SKB_FAIL(-EINVAL);
507 if (type <= IPQM_BASE)
508 return;
510 if (security_netlink_recv(skb, CAP_NET_ADMIN))
511 RCV_SKB_FAIL(-EPERM);
513 write_lock_bh(&queue_lock);
515 if (peer_pid) {
516 if (peer_pid != pid) {
517 write_unlock_bh(&queue_lock);
518 RCV_SKB_FAIL(-EBUSY);
520 } else {
521 net_enable_timestamp();
522 peer_pid = pid;
525 write_unlock_bh(&queue_lock);
527 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
528 nlmsglen - NLMSG_LENGTH(0));
529 if (status < 0)
530 RCV_SKB_FAIL(status);
532 if (flags & NLM_F_ACK)
533 netlink_ack(skb, nlh, 0);
534 return;
537 static void
538 ipq_rcv_sk(struct sock *sk, int len)
540 struct sk_buff *skb;
541 unsigned int qlen;
543 mutex_lock(&ipqnl_mutex);
545 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
546 skb = skb_dequeue(&sk->sk_receive_queue);
547 ipq_rcv_skb(skb);
548 kfree_skb(skb);
551 mutex_unlock(&ipqnl_mutex);
554 static int
555 ipq_rcv_dev_event(struct notifier_block *this,
556 unsigned long event, void *ptr)
558 struct net_device *dev = ptr;
560 if (dev->nd_net != &init_net)
561 return NOTIFY_DONE;
563 /* Drop any packets associated with the downed device */
564 if (event == NETDEV_DOWN)
565 ipq_dev_drop(dev->ifindex);
566 return NOTIFY_DONE;
569 static struct notifier_block ipq_dev_notifier = {
570 .notifier_call = ipq_rcv_dev_event,
573 static int
574 ipq_rcv_nl_event(struct notifier_block *this,
575 unsigned long event, void *ptr)
577 struct netlink_notify *n = ptr;
579 if (event == NETLINK_URELEASE &&
580 n->protocol == NETLINK_FIREWALL && n->pid) {
581 write_lock_bh(&queue_lock);
582 if ((n->net == &init_net) && (n->pid == peer_pid))
583 __ipq_reset();
584 write_unlock_bh(&queue_lock);
586 return NOTIFY_DONE;
589 static struct notifier_block ipq_nl_notifier = {
590 .notifier_call = ipq_rcv_nl_event,
593 static struct ctl_table_header *ipq_sysctl_header;
595 static ctl_table ipq_table[] = {
597 .ctl_name = NET_IPQ_QMAX,
598 .procname = NET_IPQ_QMAX_NAME,
599 .data = &queue_maxlen,
600 .maxlen = sizeof(queue_maxlen),
601 .mode = 0644,
602 .proc_handler = proc_dointvec
604 { .ctl_name = 0 }
607 static ctl_table ipq_dir_table[] = {
609 .ctl_name = NET_IPV4,
610 .procname = "ipv4",
611 .mode = 0555,
612 .child = ipq_table
614 { .ctl_name = 0 }
617 static ctl_table ipq_root_table[] = {
619 .ctl_name = CTL_NET,
620 .procname = "net",
621 .mode = 0555,
622 .child = ipq_dir_table
624 { .ctl_name = 0 }
627 #ifdef CONFIG_PROC_FS
628 static int
629 ipq_get_info(char *buffer, char **start, off_t offset, int length)
631 int len;
633 read_lock_bh(&queue_lock);
635 len = sprintf(buffer,
636 "Peer PID : %d\n"
637 "Copy mode : %hu\n"
638 "Copy range : %u\n"
639 "Queue length : %u\n"
640 "Queue max. length : %u\n"
641 "Queue dropped : %u\n"
642 "Netlink dropped : %u\n",
643 peer_pid,
644 copy_mode,
645 copy_range,
646 queue_total,
647 queue_maxlen,
648 queue_dropped,
649 queue_user_dropped);
651 read_unlock_bh(&queue_lock);
653 *start = buffer + offset;
654 len -= offset;
655 if (len > length)
656 len = length;
657 else if (len < 0)
658 len = 0;
659 return len;
661 #endif /* CONFIG_PROC_FS */
663 static struct nf_queue_handler nfqh = {
664 .name = "ip_queue",
665 .outfn = &ipq_enqueue_packet,
668 static int __init ip_queue_init(void)
670 int status = -ENOMEM;
671 struct proc_dir_entry *proc;
673 netlink_register_notifier(&ipq_nl_notifier);
674 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
675 ipq_rcv_sk, NULL, THIS_MODULE);
676 if (ipqnl == NULL) {
677 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
678 goto cleanup_netlink_notifier;
681 proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
682 if (proc)
683 proc->owner = THIS_MODULE;
684 else {
685 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
686 goto cleanup_ipqnl;
689 register_netdevice_notifier(&ipq_dev_notifier);
690 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
692 status = nf_register_queue_handler(PF_INET, &nfqh);
693 if (status < 0) {
694 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
695 goto cleanup_sysctl;
697 return status;
699 cleanup_sysctl:
700 unregister_sysctl_table(ipq_sysctl_header);
701 unregister_netdevice_notifier(&ipq_dev_notifier);
702 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
703 cleanup_ipqnl:
704 sock_release(ipqnl->sk_socket);
705 mutex_lock(&ipqnl_mutex);
706 mutex_unlock(&ipqnl_mutex);
708 cleanup_netlink_notifier:
709 netlink_unregister_notifier(&ipq_nl_notifier);
710 return status;
713 static void __exit ip_queue_fini(void)
715 nf_unregister_queue_handlers(&nfqh);
716 synchronize_net();
717 ipq_flush(NF_DROP);
719 unregister_sysctl_table(ipq_sysctl_header);
720 unregister_netdevice_notifier(&ipq_dev_notifier);
721 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
723 sock_release(ipqnl->sk_socket);
724 mutex_lock(&ipqnl_mutex);
725 mutex_unlock(&ipqnl_mutex);
727 netlink_unregister_notifier(&ipq_nl_notifier);
730 MODULE_DESCRIPTION("IPv4 packet queue handler");
731 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
732 MODULE_LICENSE("GPL");
734 module_init(ip_queue_init);
735 module_exit(ip_queue_fini);