ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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  */
24 #include <linux/module.h>
25 #include <linux/skbuff.h>
26 #include <linux/init.h>
27 #include <linux/ipv6.h>
28 #include <linux/notifier.h>
29 #include <linux/netdevice.h>
30 #include <linux/netfilter.h>
31 #include <linux/netlink.h>
32 #include <linux/spinlock.h>
33 #include <linux/sysctl.h>
34 #include <linux/proc_fs.h>
35 #include <net/sock.h>
36 #include <net/ipv6.h>
37 #include <net/ip6_route.h>
38 #include <linux/netfilter_ipv4/ip_queue.h>
39 #include <linux/netfilter_ipv4/ip_tables.h>
40 #include <linux/netfilter_ipv6/ip6_tables.h>
41
42 #define IPQ_QMAX_DEFAULT 1024
43 #define IPQ_PROC_FS_NAME "ip6_queue"
44 #define NET_IPQ_QMAX 2088
45 #define NET_IPQ_QMAX_NAME "ip6_queue_maxlen"
46
47 struct ipq_rt_info {
48         struct in6_addr daddr;
49         struct in6_addr saddr;
50 };
51
52 struct ipq_queue_entry {
53         struct list_head list;
54         struct nf_info *info;
55         struct sk_buff *skb;
56         struct ipq_rt_info rt_info;
57 };
58
59 typedef int (*ipq_cmpfn)(struct ipq_queue_entry *, unsigned long);
60
61 static unsigned char copy_mode = IPQ_COPY_NONE;
62 static unsigned int queue_maxlen = IPQ_QMAX_DEFAULT;
63 static rwlock_t queue_lock = RW_LOCK_UNLOCKED;
64 static int peer_pid;
65 static unsigned int copy_range;
66 static unsigned int queue_total;
67 static struct sock *ipqnl;
68 static LIST_HEAD(queue_list);
69 static DECLARE_MUTEX(ipqnl_sem);
70
71 static void
72 ipq_issue_verdict(struct ipq_queue_entry *entry, int verdict)
73 {
74         nf_reinject(entry->skb, entry->info, verdict);
75         kfree(entry);
76 }
77
78 static inline int
79 __ipq_enqueue_entry(struct ipq_queue_entry *entry)
80 {
81        if (queue_total >= queue_maxlen) {
82                if (net_ratelimit()) 
83                        printk(KERN_WARNING "ip6_queue: full at %d entries, "
84                               "dropping packet(s).\n", queue_total);
85                return -ENOSPC;
86        }
87        list_add(&entry->list, &queue_list);
88        queue_total++;
89        return 0;
90 }
91
92 /*
93  * Find and return a queued entry matched by cmpfn, or return the last
94  * entry if cmpfn is NULL.
95  */
96 static inline struct ipq_queue_entry *
97 __ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
98 {
99         struct list_head *p;
100
101         list_for_each_prev(p, &queue_list) {
102                 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
103                 
104                 if (!cmpfn || cmpfn(entry, data))
105                         return entry;
106         }
107         return NULL;
108 }
109
110 static inline void
111 __ipq_dequeue_entry(struct ipq_queue_entry *entry)
112 {
113         list_del(&entry->list);
114         queue_total--;
115 }
116
117 static inline struct ipq_queue_entry *
118 __ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
119 {
120         struct ipq_queue_entry *entry;
121
122         entry = __ipq_find_entry(cmpfn, data);
123         if (entry == NULL)
124                 return NULL;
125
126         __ipq_dequeue_entry(entry);
127         return entry;
128 }
129
130
131 static inline void
132 __ipq_flush(int verdict)
133 {
134         struct ipq_queue_entry *entry;
135         
136         while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
137                 ipq_issue_verdict(entry, verdict);
138 }
139
140 static inline int
141 __ipq_set_mode(unsigned char mode, unsigned int range)
142 {
143         int status = 0;
144         
145         switch(mode) {
146         case IPQ_COPY_NONE:
147         case IPQ_COPY_META:
148                 copy_mode = mode;
149                 copy_range = 0;
150                 break;
151                 
152         case IPQ_COPY_PACKET:
153                 copy_mode = mode;
154                 copy_range = range;
155                 if (copy_range > 0xFFFF)
156                         copy_range = 0xFFFF;
157                 break;
158                 
159         default:
160                 status = -EINVAL;
161
162         }
163         return status;
164 }
165
166 static inline void
167 __ipq_reset(void)
168 {
169         peer_pid = 0;
170         __ipq_set_mode(IPQ_COPY_NONE, 0);
171         __ipq_flush(NF_DROP);
172 }
173
174 static struct ipq_queue_entry *
175 ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
176 {
177         struct ipq_queue_entry *entry;
178         
179         write_lock_bh(&queue_lock);
180         entry = __ipq_find_dequeue_entry(cmpfn, data);
181         write_unlock_bh(&queue_lock);
182         return entry;
183 }
184
185 static void
186 ipq_flush(int verdict)
187 {
188         write_lock_bh(&queue_lock);
189         __ipq_flush(verdict);
190         write_unlock_bh(&queue_lock);
191 }
192
193 static struct sk_buff *
194 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
195 {
196         unsigned char *old_tail;
197         size_t size = 0;
198         size_t data_len = 0;
199         struct sk_buff *skb;
200         struct ipq_packet_msg *pmsg;
201         struct nlmsghdr *nlh;
202
203         read_lock_bh(&queue_lock);
204         
205         switch (copy_mode) {
206         case IPQ_COPY_META:
207         case IPQ_COPY_NONE:
208                 size = NLMSG_SPACE(sizeof(*pmsg));
209                 data_len = 0;
210                 break;
211         
212         case IPQ_COPY_PACKET:
213                 if (copy_range == 0 || copy_range > entry->skb->len)
214                         data_len = entry->skb->len;
215                 else
216                         data_len = copy_range;
217                 
218                 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
219                 break;
220         
221         default:
222                 *errp = -EINVAL;
223                 read_unlock_bh(&queue_lock);
224                 return NULL;
225         }
226
227         read_unlock_bh(&queue_lock);
228
229         skb = alloc_skb(size, GFP_ATOMIC);
230         if (!skb)
231                 goto nlmsg_failure;
232                 
233         old_tail= skb->tail;
234         nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
235         pmsg = NLMSG_DATA(nlh);
236         memset(pmsg, 0, sizeof(*pmsg));
237
238         pmsg->packet_id       = (unsigned long )entry;
239         pmsg->data_len        = data_len;
240         pmsg->timestamp_sec   = entry->skb->stamp.tv_sec;
241         pmsg->timestamp_usec  = entry->skb->stamp.tv_usec;
242         pmsg->mark            = entry->skb->nfmark;
243         pmsg->hook            = entry->info->hook;
244         pmsg->hw_protocol     = entry->skb->protocol;
245         
246         if (entry->info->indev)
247                 strcpy(pmsg->indev_name, entry->info->indev->name);
248         else
249                 pmsg->indev_name[0] = '\0';
250         
251         if (entry->info->outdev)
252                 strcpy(pmsg->outdev_name, entry->info->outdev->name);
253         else
254                 pmsg->outdev_name[0] = '\0';
255         
256         if (entry->info->indev && entry->skb->dev) {
257                 pmsg->hw_type = entry->skb->dev->type;
258                 if (entry->skb->dev->hard_header_parse)
259                         pmsg->hw_addrlen =
260                                 entry->skb->dev->hard_header_parse(entry->skb,
261                                                                    pmsg->hw_addr);
262         }
263         
264         if (data_len)
265                 memcpy(pmsg->payload, entry->skb->data, data_len);
266                 
267         nlh->nlmsg_len = skb->tail - old_tail;
268         return skb;
269
270 nlmsg_failure:
271         if (skb)
272                 kfree_skb(skb);
273         *errp = -EINVAL;
274         printk(KERN_ERR "ip6_queue: error creating packet message\n");
275         return NULL;
276 }
277
278 static int
279 ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info, void *data)
280 {
281         int status = -EINVAL;
282         struct sk_buff *nskb;
283         struct ipq_queue_entry *entry;
284
285         if (copy_mode == IPQ_COPY_NONE)
286                 return -EAGAIN;
287
288         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
289         if (entry == NULL) {
290                 printk(KERN_ERR "ip6_queue: OOM in ipq_enqueue_packet()\n");
291                 return -ENOMEM;
292         }
293
294         entry->info = info;
295         entry->skb = skb;
296
297         if (entry->info->hook == NF_IP_LOCAL_OUT) {
298                 struct ipv6hdr *iph = skb->nh.ipv6h;
299
300                 entry->rt_info.daddr = iph->daddr;
301                 entry->rt_info.saddr = iph->saddr;
302         }
303
304         nskb = ipq_build_packet_message(entry, &status);
305         if (nskb == NULL)
306                 goto err_out_free;
307                 
308         write_lock_bh(&queue_lock);
309         
310         if (!peer_pid)
311                 goto err_out_free_nskb; 
312
313         /* netlink_unicast will either free the nskb or attach it to a socket */ 
314         status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
315         if (status < 0)
316                 goto err_out_unlock;
317         
318         status = __ipq_enqueue_entry(entry);
319         if (status < 0)
320                 goto err_out_unlock;
321
322         write_unlock_bh(&queue_lock);
323         return status;
324         
325 err_out_free_nskb:
326         kfree_skb(nskb); 
327         
328 err_out_unlock:
329         write_unlock_bh(&queue_lock);
330
331 err_out_free:
332         kfree(entry);
333         return status;
334 }
335
336 static int
337 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
338 {
339         int diff;
340         struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
341
342         if (v->data_len < sizeof(*user_iph))
343                 return 0;
344         diff = v->data_len - e->skb->len;
345         if (diff < 0)
346                 skb_trim(e->skb, v->data_len);
347         else if (diff > 0) {
348                 if (v->data_len > 0xFFFF)
349                         return -EINVAL;
350                 if (diff > skb_tailroom(e->skb)) {
351                         struct sk_buff *newskb;
352                         
353                         newskb = skb_copy_expand(e->skb,
354                                                  skb_headroom(e->skb),
355                                                  diff,
356                                                  GFP_ATOMIC);
357                         if (newskb == NULL) {
358                                 printk(KERN_WARNING "ip6_queue: OOM "
359                                       "in mangle, dropping packet\n");
360                                 return -ENOMEM;
361                         }
362                         if (e->skb->sk)
363                                 skb_set_owner_w(newskb, e->skb->sk);
364                         kfree_skb(e->skb);
365                         e->skb = newskb;
366                 }
367                 skb_put(e->skb, diff);
368         }
369         memcpy(e->skb->data, v->payload, v->data_len);
370         e->skb->nfcache |= NFC_ALTERED;
371
372         /*
373          * Extra routing may needed on local out, as the QUEUE target never
374          * returns control to the table.
375          * Not a nice way to cmp, but works
376          */
377         if (e->info->hook == NF_IP_LOCAL_OUT) {
378                 struct ipv6hdr *iph = e->skb->nh.ipv6h;
379                 if (ipv6_addr_cmp(&iph->daddr, &e->rt_info.daddr) ||
380                     ipv6_addr_cmp(&iph->saddr, &e->rt_info.saddr))
381                         return ip6_route_me_harder(e->skb);
382         }
383         return 0;
384 }
385
386 static inline int
387 id_cmp(struct ipq_queue_entry *e, unsigned long id)
388 {
389         return (id == (unsigned long )e);
390 }
391
392 static int
393 ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
394 {
395         struct ipq_queue_entry *entry;
396
397         if (vmsg->value > NF_MAX_VERDICT)
398                 return -EINVAL;
399
400         entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
401         if (entry == NULL)
402                 return -ENOENT;
403         else {
404                 int verdict = vmsg->value;
405                 
406                 if (vmsg->data_len && vmsg->data_len == len)
407                         if (ipq_mangle_ipv6(vmsg, entry) < 0)
408                                 verdict = NF_DROP;
409                 
410                 ipq_issue_verdict(entry, verdict);
411                 return 0;
412         }
413 }
414
415 static int
416 ipq_set_mode(unsigned char mode, unsigned int range)
417 {
418         int status;
419
420         write_lock_bh(&queue_lock);
421         status = __ipq_set_mode(mode, range);
422         write_unlock_bh(&queue_lock);
423         return status;
424 }
425
426 static int
427 ipq_receive_peer(struct ipq_peer_msg *pmsg,
428                  unsigned char type, unsigned int len)
429 {
430         int status = 0;
431
432         if (len < sizeof(*pmsg))
433                 return -EINVAL;
434
435         switch (type) {
436         case IPQM_MODE:
437                 status = ipq_set_mode(pmsg->msg.mode.value,
438                                       pmsg->msg.mode.range);
439                 break;
440                 
441         case IPQM_VERDICT:
442                 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
443                         status = -EINVAL;
444                 else
445                         status = ipq_set_verdict(&pmsg->msg.verdict,
446                                                  len - sizeof(*pmsg));
447                         break;
448         default:
449                 status = -EINVAL;
450         }
451         return status;
452 }
453
454 static int
455 dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
456 {
457         if (entry->info->indev)
458                 if (entry->info->indev->ifindex == ifindex)
459                         return 1;
460                         
461         if (entry->info->outdev)
462                 if (entry->info->outdev->ifindex == ifindex)
463                         return 1;
464
465         return 0;
466 }
467
468 static void
469 ipq_dev_drop(int ifindex)
470 {
471         struct ipq_queue_entry *entry;
472         
473         while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
474                 ipq_issue_verdict(entry, NF_DROP);
475 }
476
477 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
478
479 static inline void
480 ipq_rcv_skb(struct sk_buff *skb)
481 {
482         int status, type, pid, flags, nlmsglen, skblen;
483         struct nlmsghdr *nlh;
484
485         skblen = skb->len;
486         if (skblen < sizeof(*nlh))
487                 return;
488
489         nlh = (struct nlmsghdr *)skb->data;
490         nlmsglen = nlh->nlmsg_len;
491         if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
492                 return;
493
494         pid = nlh->nlmsg_pid;
495         flags = nlh->nlmsg_flags;
496         
497         if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
498                 RCV_SKB_FAIL(-EINVAL);
499                 
500         if (flags & MSG_TRUNC)
501                 RCV_SKB_FAIL(-ECOMM);
502                 
503         type = nlh->nlmsg_type;
504         if (type < NLMSG_NOOP || type >= IPQM_MAX)
505                 RCV_SKB_FAIL(-EINVAL);
506                 
507         if (type <= IPQM_BASE)
508                 return;
509         
510         if (security_netlink_recv(skb))
511                 RCV_SKB_FAIL(-EPERM);   
512
513         write_lock_bh(&queue_lock);
514         
515         if (peer_pid) {
516                 if (peer_pid != pid) {
517                         write_unlock_bh(&queue_lock);
518                         RCV_SKB_FAIL(-EBUSY);
519                 }
520         }
521         else
522                 peer_pid = pid;
523                 
524         write_unlock_bh(&queue_lock);
525         
526         status = ipq_receive_peer(NLMSG_DATA(nlh), type,
527                                   skblen - NLMSG_LENGTH(0));
528         if (status < 0)
529                 RCV_SKB_FAIL(status);
530                 
531         if (flags & NLM_F_ACK)
532                 netlink_ack(skb, nlh, 0);
533         return;
534 }
535
536 static void
537 ipq_rcv_sk(struct sock *sk, int len)
538 {
539         do {
540                 struct sk_buff *skb;
541
542                 if (down_trylock(&ipqnl_sem))
543                         return;
544                         
545                 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
546                         ipq_rcv_skb(skb);
547                         kfree_skb(skb);
548                 }
549                 
550                 up(&ipqnl_sem);
551
552         } while (ipqnl && ipqnl->sk_receive_queue.qlen);
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 static int
626 ipq_get_info(char *buffer, char **start, off_t offset, int length)
627 {
628         int len;
629
630         read_lock_bh(&queue_lock);
631         
632         len = sprintf(buffer,
633                       "Peer PID          : %d\n"
634                       "Copy mode         : %hu\n"
635                       "Copy range        : %u\n"
636                       "Queue length      : %u\n"
637                       "Queue max. length : %u\n",
638                       peer_pid,
639                       copy_mode,
640                       copy_range,
641                       queue_total,
642                       queue_maxlen);
643
644         read_unlock_bh(&queue_lock);
645         
646         *start = buffer + offset;
647         len -= offset;
648         if (len > length)
649                 len = length;
650         else if (len < 0)
651                 len = 0;
652         return len;
653 }
654
655 static int
656 init_or_cleanup(int init)
657 {
658         int status = -ENOMEM;
659         struct proc_dir_entry *proc;
660         
661         if (!init)
662                 goto cleanup;
663
664         netlink_register_notifier(&ipq_nl_notifier);
665         ipqnl = netlink_kernel_create(NETLINK_IP6_FW, ipq_rcv_sk);
666         if (ipqnl == NULL) {
667                 printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
668                 goto cleanup_netlink_notifier;
669         }
670
671         proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
672         if (proc)
673                 proc->owner = THIS_MODULE;
674         else {
675                 printk(KERN_ERR "ip6_queue: failed to create proc entry\n");
676                 goto cleanup_ipqnl;
677         }
678         
679         register_netdevice_notifier(&ipq_dev_notifier);
680         ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
681         
682         status = nf_register_queue_handler(PF_INET6, ipq_enqueue_packet, NULL);
683         if (status < 0) {
684                 printk(KERN_ERR "ip6_queue: failed to register queue handler\n");
685                 goto cleanup_sysctl;
686         }
687         return status;
688
689 cleanup:
690         nf_unregister_queue_handler(PF_INET6);
691         synchronize_net();
692         ipq_flush(NF_DROP);
693         
694 cleanup_sysctl:
695         unregister_sysctl_table(ipq_sysctl_header);
696         unregister_netdevice_notifier(&ipq_dev_notifier);
697         proc_net_remove(IPQ_PROC_FS_NAME);
698         
699 cleanup_ipqnl:
700         sock_release(ipqnl->sk_socket);
701         down(&ipqnl_sem);
702         up(&ipqnl_sem);
703         
704 cleanup_netlink_notifier:
705         netlink_unregister_notifier(&ipq_nl_notifier);
706         return status;
707 }
708
709 static int __init init(void)
710 {
711         
712         return init_or_cleanup(1);
713 }
714
715 static void __exit fini(void)
716 {
717         init_or_cleanup(0);
718 }
719
720 MODULE_DESCRIPTION("IPv6 packet queue handler");
721 MODULE_LICENSE("GPL");
722
723 module_init(init);
724 module_exit(fini);