78892980f42c5320c70eb46cb18e6395e09f94bd
[pandora-kernel.git] / net / ipv4 / netfilter / ip_queue.c
1 /*
2  * This is a module which is used for queueing IPv4 packets and
3  * communicating with userspace via netlink.
4  *
5  * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
6  * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
7  *
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.
11  *
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 
15  *             Zander).
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 
20  *             to be dropped. 
21  * 2005-05-26: local_bh_{disable,enable} around nf_reinject (Harald Welte)
22  *
23  */
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/init.h>
27 #include <linux/ip.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>
38 #include <net/sock.h>
39 #include <net/route.h>
40
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"
45
46 struct ipq_queue_entry {
47         struct list_head list;
48         struct nf_info *info;
49         struct sk_buff *skb;
50 };
51
52 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
53
54 static unsigned char copy_mode = IPQ_COPY_NONE;
55 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
56 static DEFINE_RWLOCK(queue_lock);
57 static int peer_pid;
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);
65
66 static void
67 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
68 {
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() */
73
74         local_bh_disable();
75         nf_reinject(entry->skb, entry->info, verdict);
76         local_bh_enable();
77
78         kfree(entry);
79 }
80
81 static inline void
82 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
83 {
84        list_add(&entry->list, &queue_list);
85        queue_total++;
86 }
87
88 /*
89  * Find and return a queued entry matched by cmpfn, or return the last
90  * entry if cmpfn is NULL.
91  */
92 static inline struct ipq_queue_entry *
93 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
94 {
95         struct list_head *p;
96
97         list_for_each_prev(p, &queue_list) {
98                 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
99                 
100                 if (!cmpfn || cmpfn(entry, data))
101                         return entry;
102         }
103         return NULL;
104 }
105
106 static inline void
107 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
108 {
109         list_del(&entry->list);
110         queue_total--;
111 }
112
113 static inline struct ipq_queue_entry *
114 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
115 {
116         struct ipq_queue_entry *entry;
117
118         entry = __ipq_find_entry(cmpfn, data);
119         if (entry == NULL)
120                 return NULL;
121
122         __ipq_dequeue_entry(entry);
123         return entry;
124 }
125
126
127 static inline void
128 __ipq_flush(int verdict)
129 {
130         struct ipq_queue_entry *entry;
131         
132         while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
133                 ipq_issue_verdict(entry, verdict);
134 }
135
136 static inline int
137 __ipq_set_mode(unsigned char mode, unsigned int range)
138 {
139         int status = 0;
140         
141         switch(mode) {
142         case IPQ_COPY_NONE:
143         case IPQ_COPY_META:
144                 copy_mode = mode;
145                 copy_range = 0;
146                 break;
147                 
148         case IPQ_COPY_PACKET:
149                 copy_mode = mode;
150                 copy_range = range;
151                 if (copy_range > 0xFFFF)
152                         copy_range = 0xFFFF;
153                 break;
154                 
155         default:
156                 status = -EINVAL;
157
158         }
159         return status;
160 }
161
162 static inline void
163 __ipq_reset(void)
164 {
165         peer_pid = 0;
166         net_disable_timestamp();
167         __ipq_set_mode(IPQ_COPY_NONE, 0);
168         __ipq_flush(NF_DROP);
169 }
170
171 static struct ipq_queue_entry *
172 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
173 {
174         struct ipq_queue_entry *entry;
175         
176         write_lock_bh(&queue_lock);
177         entry = __ipq_find_dequeue_entry(cmpfn, data);
178         write_unlock_bh(&queue_lock);
179         return entry;
180 }
181
182 static void
183 ipq_flush(int verdict)
184 {
185         write_lock_bh(&queue_lock);
186         __ipq_flush(verdict);
187         write_unlock_bh(&queue_lock);
188 }
189
190 static struct sk_buff *
191 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
192 {
193         unsigned char *old_tail;
194         size_t size = 0;
195         size_t data_len = 0;
196         struct sk_buff *skb;
197         struct ipq_packet_msg *pmsg;
198         struct nlmsghdr *nlh;
199
200         read_lock_bh(&queue_lock);
201         
202         switch (copy_mode) {
203         case IPQ_COPY_META:
204         case IPQ_COPY_NONE:
205                 size = NLMSG_SPACE(sizeof(*pmsg));
206                 data_len = 0;
207                 break;
208         
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);
214                         return NULL;
215                 }
216                 if (copy_range == 0 || copy_range > entry->skb->len)
217                         data_len = entry->skb->len;
218                 else
219                         data_len = copy_range;
220                 
221                 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
222                 break;
223         
224         default:
225                 *errp = -EINVAL;
226                 read_unlock_bh(&queue_lock);
227                 return NULL;
228         }
229
230         read_unlock_bh(&queue_lock);
231
232         skb = alloc_skb(size, GFP_ATOMIC);
233         if (!skb)
234                 goto nlmsg_failure;
235                 
236         old_tail= skb->tail;
237         nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
238         pmsg = NLMSG_DATA(nlh);
239         memset(pmsg, 0, sizeof(*pmsg));
240
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;
248         
249         if (entry->info->indev)
250                 strcpy(pmsg->indev_name, entry->info->indev->name);
251         else
252                 pmsg->indev_name[0] = '\0';
253         
254         if (entry->info->outdev)
255                 strcpy(pmsg->outdev_name, entry->info->outdev->name);
256         else
257                 pmsg->outdev_name[0] = '\0';
258         
259         if (entry->info->indev && entry->skb->dev) {
260                 pmsg->hw_type = entry->skb->dev->type;
261                 if (entry->skb->dev->hard_header_parse)
262                         pmsg->hw_addrlen =
263                                 entry->skb->dev->hard_header_parse(entry->skb,
264                                                                    pmsg->hw_addr);
265         }
266         
267         if (data_len)
268                 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
269                         BUG();
270                 
271         nlh->nlmsg_len = skb->tail - old_tail;
272         return skb;
273
274 nlmsg_failure:
275         if (skb)
276                 kfree_skb(skb);
277         *errp = -EINVAL;
278         printk(KERN_ERR "ip_queue: error creating packet message\n");
279         return NULL;
280 }
281
282 static int
283 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
284 {
285         int status = -EINVAL;
286         struct sk_buff *nskb;
287         struct ipq_queue_entry *entry;
288
289         if (copy_mode == IPQ_COPY_NONE)
290                 return -EAGAIN;
291
292         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
293         if (entry == NULL) {
294                 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
295                 return -ENOMEM;
296         }
297
298         entry->info = info;
299         entry->skb = skb;
300
301         nskb = ipq_build_packet_message(entry, &status);
302         if (nskb == NULL)
303                 goto err_out_free;
304                 
305         write_lock_bh(&queue_lock);
306         
307         if (!peer_pid)
308                 goto err_out_free_nskb; 
309
310         if (queue_total >= queue_maxlen) {
311                 queue_dropped++;
312                 status = -ENOSPC;
313                 if (net_ratelimit())
314                           printk (KERN_WARNING "ip_queue: full at %d entries, "
315                                   "dropping packets(s). Dropped: %d\n", queue_total,
316                                   queue_dropped);
317                 goto err_out_free_nskb;
318         }
319
320         /* netlink_unicast will either free the nskb or attach it to a socket */ 
321         status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
322         if (status < 0) {
323                 queue_user_dropped++;
324                 goto err_out_unlock;
325         }
326
327         __ipq_enqueue_entry(entry);
328
329         write_unlock_bh(&queue_lock);
330         return status;
331
332 err_out_free_nskb:
333         kfree_skb(nskb); 
334         
335 err_out_unlock:
336         write_unlock_bh(&queue_lock);
337
338 err_out_free:
339         kfree(entry);
340         return status;
341 }
342
343 static int
344 ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
345 {
346         int diff;
347         struct iphdr *user_iph = (struct iphdr *)v->payload;
348
349         if (v->data_len < sizeof(*user_iph))
350                 return 0;
351         diff = v->data_len - e->skb->len;
352         if (diff < 0)
353                 skb_trim(e->skb, v->data_len);
354         else if (diff > 0) {
355                 if (v->data_len > 0xFFFF)
356                         return -EINVAL;
357                 if (diff > skb_tailroom(e->skb)) {
358                         struct sk_buff *newskb;
359                         
360                         newskb = skb_copy_expand(e->skb,
361                                                  skb_headroom(e->skb),
362                                                  diff,
363                                                  GFP_ATOMIC);
364                         if (newskb == NULL) {
365                                 printk(KERN_WARNING "ip_queue: OOM "
366                                       "in mangle, dropping packet\n");
367                                 return -ENOMEM;
368                         }
369                         if (e->skb->sk)
370                                 skb_set_owner_w(newskb, e->skb->sk);
371                         kfree_skb(e->skb);
372                         e->skb = newskb;
373                 }
374                 skb_put(e->skb, diff);
375         }
376         if (!skb_make_writable(&e->skb, v->data_len))
377                 return -ENOMEM;
378         memcpy(e->skb->data, v->payload, v->data_len);
379         e->skb->ip_summed = CHECKSUM_NONE;
380
381         return 0;
382 }
383
384 static inline int
385 id_cmp(struct ipq_queue_entry *e, unsigned long id)
386 {
387         return (id == (unsigned long )e);
388 }
389
390 static int
391 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
392 {
393         struct ipq_queue_entry *entry;
394
395         if (vmsg->value > NF_MAX_VERDICT)
396                 return -EINVAL;
397
398         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
399         if (entry == NULL)
400                 return -ENOENT;
401         else {
402                 int verdict = vmsg->value;
403                 
404                 if (vmsg->data_len && vmsg->data_len == len)
405                         if (ipq_mangle_ipv4(vmsg, entry) < 0)
406                                 verdict = NF_DROP;
407                 
408                 ipq_issue_verdict(entry, verdict);
409                 return 0;
410         }
411 }
412
413 static int
414 ipq_set_mode(unsigned char mode, unsigned int range)
415 {
416         int status;
417
418         write_lock_bh(&queue_lock);
419         status = __ipq_set_mode(mode, range);
420         write_unlock_bh(&queue_lock);
421         return status;
422 }
423
424 static int
425 ipq_receive_peer(struct ipq_peer_msg *pmsg,
426                  unsigned char type, unsigned int len)
427 {
428         int status = 0;
429
430         if (len < sizeof(*pmsg))
431                 return -EINVAL;
432
433         switch (type) {
434         case IPQM_MODE:
435                 status = ipq_set_mode(pmsg->msg.mode.value,
436                                       pmsg->msg.mode.range);
437                 break;
438                 
439         case IPQM_VERDICT:
440                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
441                         status = -EINVAL;
442                 else
443                         status = ipq_set_verdict(&pmsg->msg.verdict,
444                                                  len - sizeof(*pmsg));
445                         break;
446         default:
447                 status = -EINVAL;
448         }
449         return status;
450 }
451
452 static int
453 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
454 {
455         if (entry->info->indev)
456                 if (entry->info->indev->ifindex == ifindex)
457                         return 1;
458                         
459         if (entry->info->outdev)
460                 if (entry->info->outdev->ifindex == ifindex)
461                         return 1;
462
463         return 0;
464 }
465
466 static void
467 ipq_dev_drop(int ifindex)
468 {
469         struct ipq_queue_entry *entry;
470         
471         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
472                 ipq_issue_verdict(entry, NF_DROP);
473 }
474
475 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
476
477 static inline void
478 ipq_rcv_skb(struct sk_buff *skb)
479 {
480         int status, type, pid, flags, nlmsglen, skblen;
481         struct nlmsghdr *nlh;
482
483         skblen = skb->len;
484         if (skblen < sizeof(*nlh))
485                 return;
486
487         nlh = (struct nlmsghdr *)skb->data;
488         nlmsglen = nlh->nlmsg_len;
489         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
490                 return;
491
492         pid = nlh->nlmsg_pid;
493         flags = nlh->nlmsg_flags;
494         
495         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
496                 RCV_SKB_FAIL(-EINVAL);
497                 
498         if (flags & MSG_TRUNC)
499                 RCV_SKB_FAIL(-ECOMM);
500                 
501         type = nlh->nlmsg_type;
502         if (type < NLMSG_NOOP || type >= IPQM_MAX)
503                 RCV_SKB_FAIL(-EINVAL);
504                 
505         if (type <= IPQM_BASE)
506                 return;
507                 
508         if (security_netlink_recv(skb))
509                 RCV_SKB_FAIL(-EPERM);
510         
511         write_lock_bh(&queue_lock);
512         
513         if (peer_pid) {
514                 if (peer_pid != pid) {
515                         write_unlock_bh(&queue_lock);
516                         RCV_SKB_FAIL(-EBUSY);
517                 }
518         } else {
519                 net_enable_timestamp();
520                 peer_pid = pid;
521         }
522                 
523         write_unlock_bh(&queue_lock);
524         
525         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
526                                   skblen - NLMSG_LENGTH(0));
527         if (status < 0)
528                 RCV_SKB_FAIL(status);
529                 
530         if (flags & NLM_F_ACK)
531                 netlink_ack(skb, nlh, 0);
532         return;
533 }
534
535 static void
536 ipq_rcv_sk(struct sock *sk, int len)
537 {
538         struct sk_buff *skb;
539         unsigned int qlen;
540
541         down(&ipqnl_sem);
542                         
543         for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
544                 skb = skb_dequeue(&sk->sk_receive_queue);
545                 ipq_rcv_skb(skb);
546                 kfree_skb(skb);
547         }
548                 
549         up(&ipqnl_sem);
550 }
551
552 static int
553 ipq_rcv_dev_event(struct notifier_block *this,
554                   unsigned long event, void *ptr)
555 {
556         struct net_device *dev = ptr;
557
558         /* Drop any packets associated with the downed device */
559         if (event == NETDEV_DOWN)
560                 ipq_dev_drop(dev->ifindex);
561         return NOTIFY_DONE;
562 }
563
564 static struct notifier_block ipq_dev_notifier = {
565         .notifier_call  = ipq_rcv_dev_event,
566 };
567
568 static int
569 ipq_rcv_nl_event(struct notifier_block *this,
570                  unsigned long event, void *ptr)
571 {
572         struct netlink_notify *n = ptr;
573
574         if (event == NETLINK_URELEASE &&
575             n->protocol == NETLINK_FIREWALL && n->pid) {
576                 write_lock_bh(&queue_lock);
577                 if (n->pid == peer_pid)
578                         __ipq_reset();
579                 write_unlock_bh(&queue_lock);
580         }
581         return NOTIFY_DONE;
582 }
583
584 static struct notifier_block ipq_nl_notifier = {
585         .notifier_call  = ipq_rcv_nl_event,
586 };
587
588 static struct ctl_table_header *ipq_sysctl_header;
589
590 static ctl_table ipq_table[] = {
591         {
592                 .ctl_name       = NET_IPQ_QMAX,
593                 .procname       = NET_IPQ_QMAX_NAME,
594                 .data           = &queue_maxlen,
595                 .maxlen         = sizeof(queue_maxlen),
596                 .mode           = 0644,
597                 .proc_handler   = proc_dointvec
598         },
599         { .ctl_name = 0 }
600 };
601
602 static ctl_table ipq_dir_table[] = {
603         {
604                 .ctl_name       = NET_IPV4,
605                 .procname       = "ipv4",
606                 .mode           = 0555,
607                 .child          = ipq_table
608         },
609         { .ctl_name = 0 }
610 };
611
612 static ctl_table ipq_root_table[] = {
613         {
614                 .ctl_name       = CTL_NET,
615                 .procname       = "net",
616                 .mode           = 0555,
617                 .child          = ipq_dir_table
618         },
619         { .ctl_name = 0 }
620 };
621
622 #ifdef CONFIG_PROC_FS
623 static int
624 ipq_get_info(char *buffer, char **start, off_t offset, int length)
625 {
626         int len;
627
628         read_lock_bh(&queue_lock);
629         
630         len = sprintf(buffer,
631                       "Peer PID          : %d\n"
632                       "Copy mode         : %hu\n"
633                       "Copy range        : %u\n"
634                       "Queue length      : %u\n"
635                       "Queue max. length : %u\n"
636                       "Queue dropped     : %u\n"
637                       "Netlink dropped   : %u\n",
638                       peer_pid,
639                       copy_mode,
640                       copy_range,
641                       queue_total,
642                       queue_maxlen,
643                       queue_dropped,
644                       queue_user_dropped);
645
646         read_unlock_bh(&queue_lock);
647         
648         *start = buffer + offset;
649         len -= offset;
650         if (len > length)
651                 len = length;
652         else if (len < 0)
653                 len = 0;
654         return len;
655 }
656 #endif /* CONFIG_PROC_FS */
657
658 static int
659 init_or_cleanup(int init)
660 {
661         int status = -ENOMEM;
662         struct proc_dir_entry *proc;
663         
664         if (!init)
665                 goto cleanup;
666
667         netlink_register_notifier(&ipq_nl_notifier);
668         ipqnl = netlink_kernel_create(NETLINK_FIREWALL, ipq_rcv_sk,
669                                       THIS_MODULE);
670         if (ipqnl == NULL) {
671                 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
672                 goto cleanup_netlink_notifier;
673         }
674
675         proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
676         if (proc)
677                 proc->owner = THIS_MODULE;
678         else {
679                 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
680                 goto cleanup_ipqnl;
681         }
682         
683         register_netdevice_notifier(&ipq_dev_notifier);
684         ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
685         
686         status = nf_register_queue_handler(PF_INET, ipq_enqueue_packet, NULL);
687         if (status < 0) {
688                 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
689                 goto cleanup_sysctl;
690         }
691         return status;
692
693 cleanup:
694         nf_unregister_queue_handler(PF_INET);
695         synchronize_net();
696         ipq_flush(NF_DROP);
697         
698 cleanup_sysctl:
699         unregister_sysctl_table(ipq_sysctl_header);
700         unregister_netdevice_notifier(&ipq_dev_notifier);
701         proc_net_remove(IPQ_PROC_FS_NAME);
702         
703 cleanup_ipqnl:
704         sock_release(ipqnl->sk_socket);
705         down(&ipqnl_sem);
706         up(&ipqnl_sem);
707         
708 cleanup_netlink_notifier:
709         netlink_unregister_notifier(&ipq_nl_notifier);
710         return status;
711 }
712
713 static int __init init(void)
714 {
715         
716         return init_or_cleanup(1);
717 }
718
719 static void __exit fini(void)
720 {
721         init_or_cleanup(0);
722 }
723
724 MODULE_DESCRIPTION("IPv4 packet queue handler");
725 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
726 MODULE_LICENSE("GPL");
727
728 module_init(init);
729 module_exit(fini);