Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / net / ipv6 / netfilter / ip6_queue.c
1 /*
2  * This is a module which is used for queueing IPv6 packets and
3  * communicating with userspace via netlink.
4  *
5  * (C) 2001 Fernando Anton, this code is GPL.
6  *     IPv64 Project - Work based in IPv64 draft by Arturo Azcorra.
7  *     Universidad Carlos III de Madrid - Leganes (Madrid) - Spain
8  *     Universidad Politecnica de Alcala de Henares - Alcala de H. (Madrid) - Spain
9  *     email: fanton@it.uc3m.es
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
16  *             to adapt it to IPv6
17  *             HEAVILY based in ipqueue.c by James Morris. It's just
18  *             a little modified version of it, so he's nearly the
19  *             real coder of this.
20  *             Few changes needed, mainly the hard_routing code and
21  *             the netlink socket protocol (we're NETLINK_IP6_FW).
22  * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
23  * 2005-02-04: Added /proc counter for dropped packets; fixed so
24  *             packets aren't delivered to user space if they're going
25  *             to be dropped.
26  */
27 #include <linux/module.h>
28 #include <linux/skbuff.h>
29 #include <linux/init.h>
30 #include <linux/ipv6.h>
31 #include <linux/notifier.h>
32 #include <linux/netdevice.h>
33 #include <linux/netfilter.h>
34 #include <linux/netlink.h>
35 #include <linux/spinlock.h>
36 #include <linux/sysctl.h>
37 #include <linux/proc_fs.h>
38 #include <linux/mutex.h>
39 #include <net/sock.h>
40 #include <net/ipv6.h>
41 #include <net/ip6_route.h>
42 #include <linux/netfilter_ipv4/ip_queue.h>
43 #include <linux/netfilter_ipv4/ip_tables.h>
44 #include <linux/netfilter_ipv6/ip6_tables.h>
45
46 #define IPQ_QMAX_DEFAULT 1024
47 #define IPQ_PROC_FS_NAME "ip6_queue"
48 #define NET_IPQ_QMAX 2088
49 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
50
51 struct ipq_queue_entry {
52         struct list_head list;
53         struct nf_info *info;
54         struct sk_buff *skb;
55 };
56
57 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
58
59 static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
60 static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
61 static DEFINE_RWLOCK(queue_lock);
62 static int peer_pid __read_mostly;
63 static unsigned int copy_range __read_mostly;
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 __read_mostly;
68 static LIST_HEAD(queue_list);
69 static DEFINE_MUTEX(ipqnl_mutex);
70
71 static void
72 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
73 {
74         local_bh_disable();
75         nf_reinject(entry->skb, entry->info, verdict);
76         local_bh_enable();
77         kfree(entry);
78 }
79
80 static inline void
81 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
82 {
83        list_add(&entry->list, &queue_list);
84        queue_total++;
85 }
86
87 /*
88  * Find and return a queued entry matched by cmpfn, or return the last
89  * entry if cmpfn is NULL.
90  */
91 static inline struct ipq_queue_entry *
92 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
93 {
94         struct list_head *p;
95
96         list_for_each_prev(p, &queue_list) {
97                 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
98                 
99                 if (!cmpfn || cmpfn(entry, data))
100                         return entry;
101         }
102         return NULL;
103 }
104
105 static inline void
106 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
107 {
108         list_del(&entry->list);
109         queue_total--;
110 }
111
112 static inline struct ipq_queue_entry *
113 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
114 {
115         struct ipq_queue_entry *entry;
116
117         entry = __ipq_find_entry(cmpfn, data);
118         if (entry == NULL)
119                 return NULL;
120
121         __ipq_dequeue_entry(entry);
122         return entry;
123 }
124
125
126 static inline void
127 __ipq_flush(int verdict)
128 {
129         struct ipq_queue_entry *entry;
130         
131         while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
132                 ipq_issue_verdict(entry, verdict);
133 }
134
135 static inline int
136 __ipq_set_mode(unsigned char mode, unsigned int range)
137 {
138         int status = 0;
139         
140         switch(mode) {
141         case IPQ_COPY_NONE:
142         case IPQ_COPY_META:
143                 copy_mode = mode;
144                 copy_range = 0;
145                 break;
146                 
147         case IPQ_COPY_PACKET:
148                 copy_mode = mode;
149                 copy_range = range;
150                 if (copy_range > 0xFFFF)
151                         copy_range = 0xFFFF;
152                 break;
153                 
154         default:
155                 status = -EINVAL;
156
157         }
158         return status;
159 }
160
161 static inline void
162 __ipq_reset(void)
163 {
164         peer_pid = 0;
165         net_disable_timestamp();
166         __ipq_set_mode(IPQ_COPY_NONE, 0);
167         __ipq_flush(NF_DROP);
168 }
169
170 static struct ipq_queue_entry *
171 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
172 {
173         struct ipq_queue_entry *entry;
174         
175         write_lock_bh(&queue_lock);
176         entry = __ipq_find_dequeue_entry(cmpfn, data);
177         write_unlock_bh(&queue_lock);
178         return entry;
179 }
180
181 static void
182 ipq_flush(int verdict)
183 {
184         write_lock_bh(&queue_lock);
185         __ipq_flush(verdict);
186         write_unlock_bh(&queue_lock);
187 }
188
189 static struct sk_buff *
190 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
191 {
192         unsigned char *old_tail;
193         size_t size = 0;
194         size_t data_len = 0;
195         struct sk_buff *skb;
196         struct ipq_packet_msg *pmsg;
197         struct nlmsghdr *nlh;
198
199         read_lock_bh(&queue_lock);
200         
201         switch (copy_mode) {
202         case IPQ_COPY_META:
203         case IPQ_COPY_NONE:
204                 size = NLMSG_SPACE(sizeof(*pmsg));
205                 data_len = 0;
206                 break;
207         
208         case IPQ_COPY_PACKET:
209                 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
210                      entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
211                     (*errp = skb_checksum_help(entry->skb))) {
212                         read_unlock_bh(&queue_lock);
213                         return NULL;
214                 }
215                 if (copy_range == 0 || copy_range > entry->skb->len)
216                         data_len = entry->skb->len;
217                 else
218                         data_len = copy_range;
219                 
220                 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
221                 break;
222         
223         default:
224                 *errp = -EINVAL;
225                 read_unlock_bh(&queue_lock);
226                 return NULL;
227         }
228
229         read_unlock_bh(&queue_lock);
230
231         skb = alloc_skb(size, GFP_ATOMIC);
232         if (!skb)
233                 goto nlmsg_failure;
234                 
235         old_tail= skb->tail;
236         nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
237         pmsg = NLMSG_DATA(nlh);
238         memset(pmsg, 0, sizeof(*pmsg));
239
240         pmsg->packet_id       = (unsigned long )entry;
241         pmsg->data_len        = data_len;
242         pmsg->timestamp_sec   = entry->skb->tstamp.off_sec;
243         pmsg->timestamp_usec  = entry->skb->tstamp.off_usec;
244         pmsg->mark            = entry->skb->mark;
245         pmsg->hook            = entry->info->hook;
246         pmsg->hw_protocol     = entry->skb->protocol;
247         
248         if (entry->info->indev)
249                 strcpy(pmsg->indev_name, entry->info->indev->name);
250         else
251                 pmsg->indev_name[0] = '\0';
252         
253         if (entry->info->outdev)
254                 strcpy(pmsg->outdev_name, entry->info->outdev->name);
255         else
256                 pmsg->outdev_name[0] = '\0';
257         
258         if (entry->info->indev && entry->skb->dev) {
259                 pmsg->hw_type = entry->skb->dev->type;
260                 if (entry->skb->dev->hard_header_parse)
261                         pmsg->hw_addrlen =
262                                 entry->skb->dev->hard_header_parse(entry->skb,
263                                                                    pmsg->hw_addr);
264         }
265         
266         if (data_len)
267                 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
268                         BUG();
269                 
270         nlh->nlmsg_len = skb->tail - old_tail;
271         return skb;
272
273 nlmsg_failure:
274         if (skb)
275                 kfree_skb(skb);
276         *errp = -EINVAL;
277         printk(KERN_ERR "ip6_queue: error creating packet message\n");
278         return NULL;
279 }
280
281 static int
282 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, 
283                    unsigned int queuenum, 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 "ip6_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 "ip6_queue: fill at %d entries, "
315                                 "dropping packet(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_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
345 {
346         int diff;
347         struct ipv6hdr *user_iph = (struct ipv6hdr *)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                 if (pskb_trim(e->skb, v->data_len))
354                         return -ENOMEM;
355         } else if (diff > 0) {
356                 if (v->data_len > 0xFFFF)
357                         return -EINVAL;
358                 if (diff > skb_tailroom(e->skb)) {
359                         struct sk_buff *newskb;
360                         
361                         newskb = skb_copy_expand(e->skb,
362                                                  skb_headroom(e->skb),
363                                                  diff,
364                                                  GFP_ATOMIC);
365                         if (newskb == NULL) {
366                                 printk(KERN_WARNING "ip6_queue: OOM "
367                                       "in mangle, dropping packet\n");
368                                 return -ENOMEM;
369                         }
370                         if (e->skb->sk)
371                                 skb_set_owner_w(newskb, e->skb->sk);
372                         kfree_skb(e->skb);
373                         e->skb = newskb;
374                 }
375                 skb_put(e->skb, diff);
376         }
377         if (!skb_make_writable(&e->skb, v->data_len))
378                 return -ENOMEM;
379         memcpy(e->skb->data, v->payload, v->data_len);
380         e->skb->ip_summed = CHECKSUM_NONE;
381
382         return 0;
383 }
384
385 static inline int
386 id_cmp(struct ipq_queue_entry *e, unsigned long id)
387 {
388         return (id == (unsigned long )e);
389 }
390
391 static int
392 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
393 {
394         struct ipq_queue_entry *entry;
395
396         if (vmsg->value > NF_MAX_VERDICT)
397                 return -EINVAL;
398
399         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
400         if (entry == NULL)
401                 return -ENOENT;
402         else {
403                 int verdict = vmsg->value;
404                 
405                 if (vmsg->data_len && vmsg->data_len == len)
406                         if (ipq_mangle_ipv6(vmsg, entry) < 0)
407                                 verdict = NF_DROP;
408                 
409                 ipq_issue_verdict(entry, verdict);
410                 return 0;
411         }
412 }
413
414 static int
415 ipq_set_mode(unsigned char mode, unsigned int range)
416 {
417         int status;
418
419         write_lock_bh(&queue_lock);
420         status = __ipq_set_mode(mode, range);
421         write_unlock_bh(&queue_lock);
422         return status;
423 }
424
425 static int
426 ipq_receive_peer(struct ipq_peer_msg *pmsg,
427                  unsigned char type, unsigned int len)
428 {
429         int status = 0;
430
431         if (len < sizeof(*pmsg))
432                 return -EINVAL;
433
434         switch (type) {
435         case IPQM_MODE:
436                 status = ipq_set_mode(pmsg->msg.mode.value,
437                                       pmsg->msg.mode.range);
438                 break;
439                 
440         case IPQM_VERDICT:
441                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
442                         status = -EINVAL;
443                 else
444                         status = ipq_set_verdict(&pmsg->msg.verdict,
445                                                  len - sizeof(*pmsg));
446                         break;
447         default:
448                 status = -EINVAL;
449         }
450         return status;
451 }
452
453 static int
454 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
455 {
456         if (entry->info->indev)
457                 if (entry->info->indev->ifindex == ifindex)
458                         return 1;
459                         
460         if (entry->info->outdev)
461                 if (entry->info->outdev->ifindex == ifindex)
462                         return 1;
463
464         return 0;
465 }
466
467 static void
468 ipq_dev_drop(int ifindex)
469 {
470         struct ipq_queue_entry *entry;
471         
472         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
473                 ipq_issue_verdict(entry, NF_DROP);
474 }
475
476 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
477
478 static inline void
479 ipq_rcv_skb(struct sk_buff *skb)
480 {
481         int status, type, pid, flags, nlmsglen, skblen;
482         struct nlmsghdr *nlh;
483
484         skblen = skb->len;
485         if (skblen < sizeof(*nlh))
486                 return;
487
488         nlh = (struct nlmsghdr *)skb->data;
489         nlmsglen = nlh->nlmsg_len;
490         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
491                 return;
492
493         pid = nlh->nlmsg_pid;
494         flags = nlh->nlmsg_flags;
495         
496         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
497                 RCV_SKB_FAIL(-EINVAL);
498                 
499         if (flags & MSG_TRUNC)
500                 RCV_SKB_FAIL(-ECOMM);
501                 
502         type = nlh->nlmsg_type;
503         if (type < NLMSG_NOOP || type >= IPQM_MAX)
504                 RCV_SKB_FAIL(-EINVAL);
505                 
506         if (type <= IPQM_BASE)
507                 return;
508         
509         if (security_netlink_recv(skb, CAP_NET_ADMIN))
510                 RCV_SKB_FAIL(-EPERM);   
511
512         write_lock_bh(&queue_lock);
513         
514         if (peer_pid) {
515                 if (peer_pid != pid) {
516                         write_unlock_bh(&queue_lock);
517                         RCV_SKB_FAIL(-EBUSY);
518                 }
519         } else {
520                 net_enable_timestamp();
521                 peer_pid = pid;
522         }
523                 
524         write_unlock_bh(&queue_lock);
525         
526         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
527                                   nlmsglen - NLMSG_LENGTH(0));
528         if (status < 0)
529                 RCV_SKB_FAIL(status);
530                 
531         if (flags & NLM_F_ACK)
532                 netlink_ack(skb, nlh, 0);
533         return;
534 }
535
536 static void
537 ipq_rcv_sk(struct sock *sk, int len)
538 {
539         struct sk_buff *skb;
540         unsigned int qlen;
541
542         mutex_lock(&ipqnl_mutex);
543                         
544         for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
545                 skb = skb_dequeue(&sk->sk_receive_queue);
546                 ipq_rcv_skb(skb);
547                 kfree_skb(skb);
548         }
549                 
550         mutex_unlock(&ipqnl_mutex);
551 }
552
553 static int
554 ipq_rcv_dev_event(struct notifier_block *this,
555                   unsigned long event, void *ptr)
556 {
557         struct net_device *dev = ptr;
558
559         /* Drop any packets associated with the downed device */
560         if (event == NETDEV_DOWN)
561                 ipq_dev_drop(dev->ifindex);
562         return NOTIFY_DONE;
563 }
564
565 static struct notifier_block ipq_dev_notifier = {
566         .notifier_call  = ipq_rcv_dev_event,
567 };
568
569 static int
570 ipq_rcv_nl_event(struct notifier_block *this,
571                  unsigned long event, void *ptr)
572 {
573         struct netlink_notify *n = ptr;
574
575         if (event == NETLINK_URELEASE &&
576             n->protocol == NETLINK_IP6_FW && n->pid) {
577                 write_lock_bh(&queue_lock);
578                 if (n->pid == peer_pid)
579                         __ipq_reset();
580                 write_unlock_bh(&queue_lock);
581         }
582         return NOTIFY_DONE;
583 }
584
585 static struct notifier_block ipq_nl_notifier = {
586         .notifier_call  = ipq_rcv_nl_event,
587 };
588
589 static struct ctl_table_header *ipq_sysctl_header;
590
591 static ctl_table ipq_table[] = {
592         {
593                 .ctl_name       = NET_IPQ_QMAX,
594                 .procname       = NET_IPQ_QMAX_NAME,
595                 .data           = &queue_maxlen,
596                 .maxlen         = sizeof(queue_maxlen),
597                 .mode           = 0644,
598                 .proc_handler   = proc_dointvec
599         },
600         { .ctl_name = 0 }
601 };
602
603 static ctl_table ipq_dir_table[] = {
604         {
605                 .ctl_name       = NET_IPV6,
606                 .procname       = "ipv6",
607                 .mode           = 0555,
608                 .child          = ipq_table
609         },
610         { .ctl_name = 0 }
611 };
612
613 static ctl_table ipq_root_table[] = {
614         {
615                 .ctl_name       = CTL_NET,
616                 .procname       = "net",
617                 .mode           = 0555,
618                 .child          = ipq_dir_table
619         },
620         { .ctl_name = 0 }
621 };
622
623 #ifdef CONFIG_PROC_FS
624 static int
625 ipq_get_info(char *buffer, char **start, off_t offset, int length)
626 {
627         int len;
628
629         read_lock_bh(&queue_lock);
630         
631         len = sprintf(buffer,
632                       "Peer PID          : %d\n"
633                       "Copy mode         : %hu\n"
634                       "Copy range        : %u\n"
635                       "Queue length      : %u\n"
636                       "Queue max. length : %u\n"
637                       "Queue dropped     : %u\n"
638                       "Netfilter dropped : %u\n",
639                       peer_pid,
640                       copy_mode,
641                       copy_range,
642                       queue_total,
643                       queue_maxlen,
644                       queue_dropped,
645                       queue_user_dropped);
646
647         read_unlock_bh(&queue_lock);
648         
649         *start = buffer + offset;
650         len -= offset;
651         if (len > length)
652                 len = length;
653         else if (len < 0)
654                 len = 0;
655         return len;
656 }
657 #endif /* CONFIG_PROC_FS */
658
659 static struct nf_queue_handler nfqh = {
660         .name   = "ip6_queue",
661         .outfn  = &ipq_enqueue_packet,
662 };
663
664 static int __init ip6_queue_init(void)
665 {
666         int status = -ENOMEM;
667         struct proc_dir_entry *proc;
668         
669         netlink_register_notifier(&ipq_nl_notifier);
670         ipqnl = netlink_kernel_create(NETLINK_IP6_FW, 0, ipq_rcv_sk,
671                                       THIS_MODULE);
672         if (ipqnl == NULL) {
673                 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
674                 goto cleanup_netlink_notifier;
675         }
676
677         proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
678         if (proc)
679                 proc->owner = THIS_MODULE;
680         else {
681                 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
682                 goto cleanup_ipqnl;
683         }
684         
685         register_netdevice_notifier(&ipq_dev_notifier);
686         ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
687         
688         status = nf_register_queue_handler(PF_INET6, &nfqh);
689         if (status < 0) {
690                 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
691                 goto cleanup_sysctl;
692         }
693         return status;
694
695 cleanup_sysctl:
696         unregister_sysctl_table(ipq_sysctl_header);
697         unregister_netdevice_notifier(&ipq_dev_notifier);
698         proc_net_remove(IPQ_PROC_FS_NAME);
699         
700 cleanup_ipqnl:
701         sock_release(ipqnl->sk_socket);
702         mutex_lock(&ipqnl_mutex);
703         mutex_unlock(&ipqnl_mutex);
704         
705 cleanup_netlink_notifier:
706         netlink_unregister_notifier(&ipq_nl_notifier);
707         return status;
708 }
709
710 static void __exit ip6_queue_fini(void)
711 {
712         nf_unregister_queue_handlers(&nfqh);
713         synchronize_net();
714         ipq_flush(NF_DROP);
715
716         unregister_sysctl_table(ipq_sysctl_header);
717         unregister_netdevice_notifier(&ipq_dev_notifier);
718         proc_net_remove(IPQ_PROC_FS_NAME);
719
720         sock_release(ipqnl->sk_socket);
721         mutex_lock(&ipqnl_mutex);
722         mutex_unlock(&ipqnl_mutex);
723
724         netlink_unregister_notifier(&ipq_nl_notifier);
725 }
726
727 MODULE_DESCRIPTION("IPv6 packet queue handler");
728 MODULE_LICENSE("GPL");
729
730 module_init(ip6_queue_init);
731 module_exit(ip6_queue_fini);