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