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