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