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