[NETFILTER]: Add ctnetlink port for nf_conntrack
[linux-2.6.git] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
1 /*
2  * Copyright (C)2004 USAGI/WIDE Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Author:
9  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10  *
11  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
12  *      - support Layer 3 protocol independent connection tracking.
13  *        Based on the original ip_conntrack code which had the following
14  *        copyright information:
15  *              (C) 1999-2001 Paul `Rusty' Russell
16  *              (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
17  *
18  * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
19  *      - add get_features() to support various size of conntrack
20  *        structures.
21  */
22
23 #include <linux/config.h>
24 #include <linux/types.h>
25 #include <linux/ipv6.h>
26 #include <linux/in6.h>
27 #include <linux/netfilter.h>
28 #include <linux/module.h>
29 #include <linux/skbuff.h>
30 #include <linux/icmp.h>
31 #include <linux/sysctl.h>
32 #include <net/ipv6.h>
33
34 #include <linux/netfilter_ipv6.h>
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_protocol.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_core.h>
40
41 #if 0
42 #define DEBUGP printk
43 #else
44 #define DEBUGP(format, args...)
45 #endif
46
47 DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
48
49 static int ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
50                              struct nf_conntrack_tuple *tuple)
51 {
52         u_int32_t _addrs[8], *ap;
53
54         ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
55                                 sizeof(_addrs), _addrs);
56         if (ap == NULL)
57                 return 0;
58
59         memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
60         memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
61
62         return 1;
63 }
64
65 static int ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
66                              const struct nf_conntrack_tuple *orig)
67 {
68         memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
69         memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
70
71         return 1;
72 }
73
74 static int ipv6_print_tuple(struct seq_file *s,
75                             const struct nf_conntrack_tuple *tuple)
76 {
77         return seq_printf(s, "src=%x:%x:%x:%x:%x:%x:%x:%x dst=%x:%x:%x:%x:%x:%x:%x:%x ",
78                           NIP6(*((struct in6_addr *)tuple->src.u3.ip6)),
79                           NIP6(*((struct in6_addr *)tuple->dst.u3.ip6)));
80 }
81
82 static int ipv6_print_conntrack(struct seq_file *s,
83                                 const struct nf_conn *conntrack)
84 {
85         return 0;
86 }
87
88 /*
89  * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
90  *
91  * This function parses (probably truncated) exthdr set "hdr"
92  * of length "len". "nexthdrp" initially points to some place,
93  * where type of the first header can be found.
94  *
95  * It skips all well-known exthdrs, and returns pointer to the start
96  * of unparsable area i.e. the first header with unknown type.
97  * if success, *nexthdr is updated by type/protocol of this header.
98  *
99  * NOTES: - it may return pointer pointing beyond end of packet,
100  *          if the last recognized header is truncated in the middle.
101  *        - if packet is truncated, so that all parsed headers are skipped,
102  *          it returns -1.
103  *        - if packet is fragmented, return pointer of the fragment header.
104  *        - ESP is unparsable for now and considered like
105  *          normal payload protocol.
106  *        - Note also special handling of AUTH header. Thanks to IPsec wizards.
107  */
108
109 int nf_ct_ipv6_skip_exthdr(struct sk_buff *skb, int start, u8 *nexthdrp,
110                            int len)
111 {
112         u8 nexthdr = *nexthdrp;
113
114         while (ipv6_ext_hdr(nexthdr)) {
115                 struct ipv6_opt_hdr hdr;
116                 int hdrlen;
117
118                 if (len < (int)sizeof(struct ipv6_opt_hdr))
119                         return -1;
120                 if (nexthdr == NEXTHDR_NONE)
121                         break;
122                 if (nexthdr == NEXTHDR_FRAGMENT)
123                         break;
124                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
125                         BUG();
126                 if (nexthdr == NEXTHDR_AUTH)
127                         hdrlen = (hdr.hdrlen+2)<<2;
128                 else
129                         hdrlen = ipv6_optlen(&hdr);
130
131                 nexthdr = hdr.nexthdr;
132                 len -= hdrlen;
133                 start += hdrlen;
134         }
135
136         *nexthdrp = nexthdr;
137         return start;
138 }
139
140 static int
141 ipv6_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
142              u_int8_t *protonum)
143 {
144         unsigned int extoff;
145         unsigned char pnum;
146         int protoff;
147
148         extoff = (u8*)((*pskb)->nh.ipv6h + 1) - (*pskb)->data;
149         pnum = (*pskb)->nh.ipv6h->nexthdr;
150
151         protoff = nf_ct_ipv6_skip_exthdr(*pskb, extoff, &pnum,
152                                          (*pskb)->len - extoff);
153
154         /*
155          * (protoff == (*pskb)->len) mean that the packet doesn't have no data
156          * except of IPv6 & ext headers. but it's tracked anyway. - YK
157          */
158         if ((protoff < 0) || (protoff > (*pskb)->len)) {
159                 DEBUGP("ip6_conntrack_core: can't find proto in pkt\n");
160                 NF_CT_STAT_INC(error);
161                 NF_CT_STAT_INC(invalid);
162                 return -NF_ACCEPT;
163         }
164
165         *dataoff = protoff;
166         *protonum = pnum;
167         return NF_ACCEPT;
168 }
169
170 static u_int32_t ipv6_get_features(const struct nf_conntrack_tuple *tuple)
171 {
172         return NF_CT_F_BASIC;
173 }
174
175 static unsigned int ipv6_confirm(unsigned int hooknum,
176                                  struct sk_buff **pskb,
177                                  const struct net_device *in,
178                                  const struct net_device *out,
179                                  int (*okfn)(struct sk_buff *))
180 {
181         struct nf_conn *ct;
182         enum ip_conntrack_info ctinfo;
183
184         /* This is where we call the helper: as the packet goes out. */
185         ct = nf_ct_get(*pskb, &ctinfo);
186         if (ct && ct->helper) {
187                 unsigned int ret, protoff;
188                 unsigned int extoff = (u8*)((*pskb)->nh.ipv6h + 1)
189                                       - (*pskb)->data;
190                 unsigned char pnum = (*pskb)->nh.ipv6h->nexthdr;
191
192                 protoff = nf_ct_ipv6_skip_exthdr(*pskb, extoff, &pnum,
193                                                  (*pskb)->len - extoff);
194                 if (protoff < 0 || protoff > (*pskb)->len ||
195                     pnum == NEXTHDR_FRAGMENT) {
196                         DEBUGP("proto header not found\n");
197                         return NF_ACCEPT;
198                 }
199
200                 ret = ct->helper->help(pskb, protoff, ct, ctinfo);
201                 if (ret != NF_ACCEPT)
202                         return ret;
203         }
204
205         /* We've seen it coming out the other side: confirm it */
206
207         return nf_conntrack_confirm(pskb);
208 }
209
210 extern struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb);
211 extern void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
212                                struct net_device *in,
213                                struct net_device *out,
214                                int (*okfn)(struct sk_buff *));
215 static unsigned int ipv6_defrag(unsigned int hooknum,
216                                 struct sk_buff **pskb,
217                                 const struct net_device *in,
218                                 const struct net_device *out,
219                                 int (*okfn)(struct sk_buff *))
220 {
221         struct sk_buff *reasm;
222
223         /* Previously seen (loopback)?  */
224         if ((*pskb)->nfct)
225                 return NF_ACCEPT;
226
227         reasm = nf_ct_frag6_gather(*pskb);
228
229         /* queued */
230         if (reasm == NULL)
231                 return NF_STOLEN;
232
233         /* error occured or not fragmented */
234         if (reasm == *pskb)
235                 return NF_ACCEPT;
236
237         nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
238                            (struct net_device *)out, okfn);
239
240         return NF_STOLEN;
241 }
242
243 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
244                                       struct sk_buff **pskb,
245                                       const struct net_device *in,
246                                       const struct net_device *out,
247                                       int (*okfn)(struct sk_buff *))
248 {
249         struct sk_buff *reasm = (*pskb)->nfct_reasm;
250
251         /* This packet is fragmented and has reassembled packet. */
252         if (reasm) {
253                 /* Reassembled packet isn't parsed yet ? */
254                 if (!reasm->nfct) {
255                         unsigned int ret;
256
257                         ret = nf_conntrack_in(PF_INET6, hooknum, &reasm);
258                         if (ret != NF_ACCEPT)
259                                 return ret;
260                 }
261                 nf_conntrack_get(reasm->nfct);
262                 (*pskb)->nfct = reasm->nfct;
263                 return NF_ACCEPT;
264         }
265
266         return nf_conntrack_in(PF_INET6, hooknum, pskb);
267 }
268
269 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
270                                          struct sk_buff **pskb,
271                                          const struct net_device *in,
272                                          const struct net_device *out,
273                                          int (*okfn)(struct sk_buff *))
274 {
275         /* root is playing with raw sockets. */
276         if ((*pskb)->len < sizeof(struct ipv6hdr)) {
277                 if (net_ratelimit())
278                         printk("ipv6_conntrack_local: packet too short\n");
279                 return NF_ACCEPT;
280         }
281         return ipv6_conntrack_in(hooknum, pskb, in, out, okfn);
282 }
283
284 /* Connection tracking may drop packets, but never alters them, so
285    make it the first hook. */
286 static struct nf_hook_ops ipv6_conntrack_defrag_ops = {
287         .hook           = ipv6_defrag,
288         .owner          = THIS_MODULE,
289         .pf             = PF_INET6,
290         .hooknum        = NF_IP6_PRE_ROUTING,
291         .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
292 };
293
294 static struct nf_hook_ops ipv6_conntrack_in_ops = {
295         .hook           = ipv6_conntrack_in,
296         .owner          = THIS_MODULE,
297         .pf             = PF_INET6,
298         .hooknum        = NF_IP6_PRE_ROUTING,
299         .priority       = NF_IP6_PRI_CONNTRACK,
300 };
301
302 static struct nf_hook_ops ipv6_conntrack_local_out_ops = {
303         .hook           = ipv6_conntrack_local,
304         .owner          = THIS_MODULE,
305         .pf             = PF_INET6,
306         .hooknum        = NF_IP6_LOCAL_OUT,
307         .priority       = NF_IP6_PRI_CONNTRACK,
308 };
309
310 static struct nf_hook_ops ipv6_conntrack_defrag_local_out_ops = {
311         .hook           = ipv6_defrag,
312         .owner          = THIS_MODULE,
313         .pf             = PF_INET6,
314         .hooknum        = NF_IP6_LOCAL_OUT,
315         .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
316 };
317
318 /* Refragmenter; last chance. */
319 static struct nf_hook_ops ipv6_conntrack_out_ops = {
320         .hook           = ipv6_confirm,
321         .owner          = THIS_MODULE,
322         .pf             = PF_INET6,
323         .hooknum        = NF_IP6_POST_ROUTING,
324         .priority       = NF_IP6_PRI_LAST,
325 };
326
327 static struct nf_hook_ops ipv6_conntrack_local_in_ops = {
328         .hook           = ipv6_confirm,
329         .owner          = THIS_MODULE,
330         .pf             = PF_INET6,
331         .hooknum        = NF_IP6_LOCAL_IN,
332         .priority       = NF_IP6_PRI_LAST-1,
333 };
334
335 #ifdef CONFIG_SYSCTL
336
337 /* From nf_conntrack_proto_icmpv6.c */
338 extern unsigned long nf_ct_icmpv6_timeout;
339
340 /* From nf_conntrack_frag6.c */
341 extern unsigned long nf_ct_frag6_timeout;
342 extern unsigned int nf_ct_frag6_low_thresh;
343 extern unsigned int nf_ct_frag6_high_thresh;
344
345 static struct ctl_table_header *nf_ct_ipv6_sysctl_header;
346
347 static ctl_table nf_ct_sysctl_table[] = {
348         {
349                 .ctl_name       = NET_NF_CONNTRACK_ICMPV6_TIMEOUT,
350                 .procname       = "nf_conntrack_icmpv6_timeout",
351                 .data           = &nf_ct_icmpv6_timeout,
352                 .maxlen         = sizeof(unsigned int),
353                 .mode           = 0644,
354                 .proc_handler   = &proc_dointvec_jiffies,
355         },
356         {
357                 .ctl_name       = NET_NF_CONNTRACK_FRAG6_TIMEOUT,
358                 .procname       = "nf_conntrack_frag6_timeout",
359                 .data           = &nf_ct_frag6_timeout,
360                 .maxlen         = sizeof(unsigned int),
361                 .mode           = 0644,
362                 .proc_handler   = &proc_dointvec_jiffies,
363         },
364         {
365                 .ctl_name       = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
366                 .procname       = "nf_conntrack_frag6_low_thresh",
367                 .data           = &nf_ct_frag6_low_thresh,
368                 .maxlen         = sizeof(unsigned int),
369                 .mode           = 0644,
370                 .proc_handler   = &proc_dointvec,
371         },
372         {
373                 .ctl_name       = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
374                 .procname       = "nf_conntrack_frag6_high_thresh",
375                 .data           = &nf_ct_frag6_high_thresh,
376                 .maxlen         = sizeof(unsigned int),
377                 .mode           = 0644,
378                 .proc_handler   = &proc_dointvec,
379         },
380         { .ctl_name = 0 }
381 };
382
383 static ctl_table nf_ct_netfilter_table[] = {
384         {
385                 .ctl_name       = NET_NETFILTER,
386                 .procname       = "netfilter",
387                 .mode           = 0555,
388                 .child          = nf_ct_sysctl_table,
389         },
390         { .ctl_name = 0 }
391 };
392
393 static ctl_table nf_ct_net_table[] = {
394         {
395                 .ctl_name       = CTL_NET,
396                 .procname       = "net",
397                 .mode           = 0555,
398                 .child          = nf_ct_netfilter_table,
399         },
400         { .ctl_name = 0 }
401 };
402 #endif
403
404 #if defined(CONFIG_NF_CT_NETLINK) || \
405     defined(CONFIG_NF_CT_NETLINK_MODULE)
406
407 #include <linux/netfilter/nfnetlink.h>
408 #include <linux/netfilter/nfnetlink_conntrack.h>
409
410 static int ipv6_tuple_to_nfattr(struct sk_buff *skb,
411                                 const struct nf_conntrack_tuple *tuple)
412 {
413         NFA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
414                 &tuple->src.u3.ip6);
415         NFA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
416                 &tuple->dst.u3.ip6);
417         return 0;
418
419 nfattr_failure:
420         return -1;
421 }
422
423 static const size_t cta_min_ip[CTA_IP_MAX] = {
424         [CTA_IP_V6_SRC-1]       = sizeof(u_int32_t)*4,
425         [CTA_IP_V6_DST-1]       = sizeof(u_int32_t)*4,
426 };
427
428 static int ipv6_nfattr_to_tuple(struct nfattr *tb[],
429                                 struct nf_conntrack_tuple *t)
430 {
431         if (!tb[CTA_IP_V6_SRC-1] || !tb[CTA_IP_V6_DST-1])
432                 return -EINVAL;
433
434         if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
435                 return -EINVAL;
436
437         memcpy(&t->src.u3.ip6, NFA_DATA(tb[CTA_IP_V6_SRC-1]), 
438                sizeof(u_int32_t) * 4);
439         memcpy(&t->dst.u3.ip6, NFA_DATA(tb[CTA_IP_V6_DST-1]),
440                sizeof(u_int32_t) * 4);
441
442         return 0;
443 }
444 #endif
445
446 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 = {
447         .l3proto                = PF_INET6,
448         .name                   = "ipv6",
449         .pkt_to_tuple           = ipv6_pkt_to_tuple,
450         .invert_tuple           = ipv6_invert_tuple,
451         .print_tuple            = ipv6_print_tuple,
452         .print_conntrack        = ipv6_print_conntrack,
453         .prepare                = ipv6_prepare,
454 #if defined(CONFIG_NF_CT_NETLINK) || \
455     defined(CONFIG_NF_CT_NETLINK_MODULE)
456         .tuple_to_nfattr        = ipv6_tuple_to_nfattr,
457         .nfattr_to_tuple        = ipv6_nfattr_to_tuple,
458 #endif
459         .get_features           = ipv6_get_features,
460         .me                     = THIS_MODULE,
461 };
462
463 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp6;
464 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp6;
465 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmpv6;
466 extern int nf_ct_frag6_init(void);
467 extern void nf_ct_frag6_cleanup(void);
468 static int init_or_cleanup(int init)
469 {
470         int ret = 0;
471
472         if (!init) goto cleanup;
473
474         ret = nf_ct_frag6_init();
475         if (ret < 0) {
476                 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
477                 goto cleanup_nothing;
478         }
479         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp6);
480         if (ret < 0) {
481                 printk("nf_conntrack_ipv6: can't register tcp.\n");
482                 goto cleanup_frag6;
483         }
484
485         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_udp6);
486         if (ret < 0) {
487                 printk("nf_conntrack_ipv6: can't register udp.\n");
488                 goto cleanup_tcp;
489         }
490
491         ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_icmpv6);
492         if (ret < 0) {
493                 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
494                 goto cleanup_udp;
495         }
496
497         ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
498         if (ret < 0) {
499                 printk("nf_conntrack_ipv6: can't register ipv6\n");
500                 goto cleanup_icmpv6;
501         }
502
503         ret = nf_register_hook(&ipv6_conntrack_defrag_ops);
504         if (ret < 0) {
505                 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
506                        "hook.\n");
507                 goto cleanup_ipv6;
508         }
509
510         ret = nf_register_hook(&ipv6_conntrack_defrag_local_out_ops);
511         if (ret < 0) {
512                 printk("nf_conntrack_ipv6: can't register local_out defrag "
513                        "hook.\n");
514                 goto cleanup_defragops;
515         }
516
517         ret = nf_register_hook(&ipv6_conntrack_in_ops);
518         if (ret < 0) {
519                 printk("nf_conntrack_ipv6: can't register pre-routing hook.\n");
520                 goto cleanup_defraglocalops;
521         }
522
523         ret = nf_register_hook(&ipv6_conntrack_local_out_ops);
524         if (ret < 0) {
525                 printk("nf_conntrack_ipv6: can't register local out hook.\n");
526                 goto cleanup_inops;
527         }
528
529         ret = nf_register_hook(&ipv6_conntrack_out_ops);
530         if (ret < 0) {
531                 printk("nf_conntrack_ipv6: can't register post-routing hook.\n");
532                 goto cleanup_inandlocalops;
533         }
534
535         ret = nf_register_hook(&ipv6_conntrack_local_in_ops);
536         if (ret < 0) {
537                 printk("nf_conntrack_ipv6: can't register local in hook.\n");
538                 goto cleanup_inoutandlocalops;
539         }
540
541 #ifdef CONFIG_SYSCTL
542         nf_ct_ipv6_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
543         if (nf_ct_ipv6_sysctl_header == NULL) {
544                 printk("nf_conntrack: can't register to sysctl.\n");
545                 ret = -ENOMEM;
546                 goto cleanup_localinops;
547         }
548 #endif
549         return ret;
550
551  cleanup:
552         synchronize_net();
553 #ifdef CONFIG_SYSCTL
554         unregister_sysctl_table(nf_ct_ipv6_sysctl_header);
555  cleanup_localinops:
556 #endif
557         nf_unregister_hook(&ipv6_conntrack_local_in_ops);
558  cleanup_inoutandlocalops:
559         nf_unregister_hook(&ipv6_conntrack_out_ops);
560  cleanup_inandlocalops:
561         nf_unregister_hook(&ipv6_conntrack_local_out_ops);
562  cleanup_inops:
563         nf_unregister_hook(&ipv6_conntrack_in_ops);
564  cleanup_defraglocalops:
565         nf_unregister_hook(&ipv6_conntrack_defrag_local_out_ops);
566  cleanup_defragops:
567         nf_unregister_hook(&ipv6_conntrack_defrag_ops);
568  cleanup_ipv6:
569         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
570  cleanup_icmpv6:
571         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmpv6);
572  cleanup_udp:
573         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp6);
574  cleanup_tcp:
575         nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp6);
576  cleanup_frag6:
577         nf_ct_frag6_cleanup();
578  cleanup_nothing:
579         return ret;
580 }
581
582 MODULE_LICENSE("GPL");
583 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
584
585 static int __init init(void)
586 {
587         need_nf_conntrack();
588         return init_or_cleanup(1);
589 }
590
591 static void __exit fini(void)
592 {
593         init_or_cleanup(0);
594 }
595
596 module_init(init);
597 module_exit(fini);
598
599 void need_ip6_conntrack(void)
600 {
601 }
602
603 EXPORT_SYMBOL(need_ip6_conntrack);