9c50cb19b39b92f845866e59ad094b81c371dab9
[safe/jmp/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 <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 #ifdef CONFIG_BRIDGE_NETFILTER
422         if (entry->skb->nf_bridge) {
423                 if (entry->skb->nf_bridge->physindev &&
424                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
425                         return 1;
426                 if (entry->skb->nf_bridge->physoutdev &&
427                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
428                         return 1;
429         }
430 #endif
431         return 0;
432 }
433
434 static void
435 ipq_dev_drop(int ifindex)
436 {
437         ipq_flush(dev_cmp, ifindex);
438 }
439
440 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
441
442 static inline void
443 __ipq_rcv_skb(struct sk_buff *skb)
444 {
445         int status, type, pid, flags, nlmsglen, skblen;
446         struct nlmsghdr *nlh;
447
448         skblen = skb->len;
449         if (skblen < sizeof(*nlh))
450                 return;
451
452         nlh = nlmsg_hdr(skb);
453         nlmsglen = nlh->nlmsg_len;
454         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
455                 return;
456
457         pid = nlh->nlmsg_pid;
458         flags = nlh->nlmsg_flags;
459
460         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
461                 RCV_SKB_FAIL(-EINVAL);
462
463         if (flags & MSG_TRUNC)
464                 RCV_SKB_FAIL(-ECOMM);
465
466         type = nlh->nlmsg_type;
467         if (type < NLMSG_NOOP || type >= IPQM_MAX)
468                 RCV_SKB_FAIL(-EINVAL);
469
470         if (type <= IPQM_BASE)
471                 return;
472
473         if (security_netlink_recv(skb, CAP_NET_ADMIN))
474                 RCV_SKB_FAIL(-EPERM);
475
476         write_lock_bh(&queue_lock);
477
478         if (peer_pid) {
479                 if (peer_pid != pid) {
480                         write_unlock_bh(&queue_lock);
481                         RCV_SKB_FAIL(-EBUSY);
482                 }
483         } else {
484                 net_enable_timestamp();
485                 peer_pid = pid;
486         }
487
488         write_unlock_bh(&queue_lock);
489
490         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
491                                   nlmsglen - NLMSG_LENGTH(0));
492         if (status < 0)
493                 RCV_SKB_FAIL(status);
494
495         if (flags & NLM_F_ACK)
496                 netlink_ack(skb, nlh, 0);
497         return;
498 }
499
500 static void
501 ipq_rcv_skb(struct sk_buff *skb)
502 {
503         mutex_lock(&ipqnl_mutex);
504         __ipq_rcv_skb(skb);
505         mutex_unlock(&ipqnl_mutex);
506 }
507
508 static int
509 ipq_rcv_dev_event(struct notifier_block *this,
510                   unsigned long event, void *ptr)
511 {
512         struct net_device *dev = ptr;
513
514         if (dev->nd_net != &init_net)
515                 return NOTIFY_DONE;
516
517         /* Drop any packets associated with the downed device */
518         if (event == NETDEV_DOWN)
519                 ipq_dev_drop(dev->ifindex);
520         return NOTIFY_DONE;
521 }
522
523 static struct notifier_block ipq_dev_notifier = {
524         .notifier_call  = ipq_rcv_dev_event,
525 };
526
527 static int
528 ipq_rcv_nl_event(struct notifier_block *this,
529                  unsigned long event, void *ptr)
530 {
531         struct netlink_notify *n = ptr;
532
533         if (event == NETLINK_URELEASE &&
534             n->protocol == NETLINK_IP6_FW && n->pid) {
535                 write_lock_bh(&queue_lock);
536                 if ((n->net == &init_net) && (n->pid == peer_pid))
537                         __ipq_reset();
538                 write_unlock_bh(&queue_lock);
539         }
540         return NOTIFY_DONE;
541 }
542
543 static struct notifier_block ipq_nl_notifier = {
544         .notifier_call  = ipq_rcv_nl_event,
545 };
546
547 static struct ctl_table_header *ipq_sysctl_header;
548
549 static ctl_table ipq_table[] = {
550         {
551                 .ctl_name       = NET_IPQ_QMAX,
552                 .procname       = NET_IPQ_QMAX_NAME,
553                 .data           = &queue_maxlen,
554                 .maxlen         = sizeof(queue_maxlen),
555                 .mode           = 0644,
556                 .proc_handler   = proc_dointvec
557         },
558         { .ctl_name = 0 }
559 };
560
561 static ctl_table ipq_dir_table[] = {
562         {
563                 .ctl_name       = NET_IPV6,
564                 .procname       = "ipv6",
565                 .mode           = 0555,
566                 .child          = ipq_table
567         },
568         { .ctl_name = 0 }
569 };
570
571 static ctl_table ipq_root_table[] = {
572         {
573                 .ctl_name       = CTL_NET,
574                 .procname       = "net",
575                 .mode           = 0555,
576                 .child          = ipq_dir_table
577         },
578         { .ctl_name = 0 }
579 };
580
581 static int ip6_queue_show(struct seq_file *m, void *v)
582 {
583         read_lock_bh(&queue_lock);
584
585         seq_printf(m,
586                       "Peer PID          : %d\n"
587                       "Copy mode         : %hu\n"
588                       "Copy range        : %u\n"
589                       "Queue length      : %u\n"
590                       "Queue max. length : %u\n"
591                       "Queue dropped     : %u\n"
592                       "Netfilter dropped : %u\n",
593                       peer_pid,
594                       copy_mode,
595                       copy_range,
596                       queue_total,
597                       queue_maxlen,
598                       queue_dropped,
599                       queue_user_dropped);
600
601         read_unlock_bh(&queue_lock);
602         return 0;
603 }
604
605 static int ip6_queue_open(struct inode *inode, struct file *file)
606 {
607         return single_open(file, ip6_queue_show, NULL);
608 }
609
610 static const struct file_operations ip6_queue_proc_fops = {
611         .open           = ip6_queue_open,
612         .read           = seq_read,
613         .llseek         = seq_lseek,
614         .release        = single_release,
615         .owner          = THIS_MODULE,
616 };
617
618 static const struct nf_queue_handler nfqh = {
619         .name   = "ip6_queue",
620         .outfn  = &ipq_enqueue_packet,
621 };
622
623 static int __init ip6_queue_init(void)
624 {
625         int status = -ENOMEM;
626         struct proc_dir_entry *proc;
627
628         netlink_register_notifier(&ipq_nl_notifier);
629         ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
630                                       ipq_rcv_skb, NULL, THIS_MODULE);
631         if (ipqnl == NULL) {
632                 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
633                 goto cleanup_netlink_notifier;
634         }
635
636         proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
637         if (proc) {
638                 proc->owner = THIS_MODULE;
639                 proc->proc_fops = &ip6_queue_proc_fops;
640         } else {
641                 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
642                 goto cleanup_ipqnl;
643         }
644
645         register_netdevice_notifier(&ipq_dev_notifier);
646         ipq_sysctl_header = register_sysctl_table(ipq_root_table);
647
648         status = nf_register_queue_handler(PF_INET6, &nfqh);
649         if (status < 0) {
650                 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
651                 goto cleanup_sysctl;
652         }
653         return status;
654
655 cleanup_sysctl:
656         unregister_sysctl_table(ipq_sysctl_header);
657         unregister_netdevice_notifier(&ipq_dev_notifier);
658         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
659
660 cleanup_ipqnl:
661         sock_release(ipqnl->sk_socket);
662         mutex_lock(&ipqnl_mutex);
663         mutex_unlock(&ipqnl_mutex);
664
665 cleanup_netlink_notifier:
666         netlink_unregister_notifier(&ipq_nl_notifier);
667         return status;
668 }
669
670 static void __exit ip6_queue_fini(void)
671 {
672         nf_unregister_queue_handlers(&nfqh);
673         synchronize_net();
674         ipq_flush(NULL, 0);
675
676         unregister_sysctl_table(ipq_sysctl_header);
677         unregister_netdevice_notifier(&ipq_dev_notifier);
678         proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
679
680         sock_release(ipqnl->sk_socket);
681         mutex_lock(&ipqnl_mutex);
682         mutex_unlock(&ipqnl_mutex);
683
684         netlink_unregister_notifier(&ipq_nl_notifier);
685 }
686
687 MODULE_DESCRIPTION("IPv6 packet queue handler");
688 MODULE_LICENSE("GPL");
689
690 module_init(ip6_queue_init);
691 module_exit(ip6_queue_fini);