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