Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[sfrench/cifs-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
12 #include <linux/types.h>
13 #include <linux/ipv6.h>
14 #include <linux/in6.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/icmp.h>
19 #include <linux/sysctl.h>
20 #include <net/ipv6.h>
21 #include <net/inet_frag.h>
22
23 #include <linux/netfilter_ipv6.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_core.h>
29
30 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
31                               struct nf_conntrack_tuple *tuple)
32 {
33         const u_int32_t *ap;
34         u_int32_t _addrs[8];
35
36         ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
37                                 sizeof(_addrs), _addrs);
38         if (ap == NULL)
39                 return false;
40
41         memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
42         memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
43
44         return true;
45 }
46
47 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
48                               const struct nf_conntrack_tuple *orig)
49 {
50         memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
51         memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
52
53         return true;
54 }
55
56 static int ipv6_print_tuple(struct seq_file *s,
57                             const struct nf_conntrack_tuple *tuple)
58 {
59         return seq_printf(s, "src=%pI6 dst=%pI6 ",
60                           tuple->src.u3.ip6, tuple->dst.u3.ip6);
61 }
62
63 /*
64  * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
65  *
66  * This function parses (probably truncated) exthdr set "hdr"
67  * of length "len". "nexthdrp" initially points to some place,
68  * where type of the first header can be found.
69  *
70  * It skips all well-known exthdrs, and returns pointer to the start
71  * of unparsable area i.e. the first header with unknown type.
72  * if success, *nexthdr is updated by type/protocol of this header.
73  *
74  * NOTES: - it may return pointer pointing beyond end of packet,
75  *          if the last recognized header is truncated in the middle.
76  *        - if packet is truncated, so that all parsed headers are skipped,
77  *          it returns -1.
78  *        - if packet is fragmented, return pointer of the fragment header.
79  *        - ESP is unparsable for now and considered like
80  *          normal payload protocol.
81  *        - Note also special handling of AUTH header. Thanks to IPsec wizards.
82  */
83
84 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
85                                   u8 *nexthdrp, int len)
86 {
87         u8 nexthdr = *nexthdrp;
88
89         while (ipv6_ext_hdr(nexthdr)) {
90                 struct ipv6_opt_hdr hdr;
91                 int hdrlen;
92
93                 if (len < (int)sizeof(struct ipv6_opt_hdr))
94                         return -1;
95                 if (nexthdr == NEXTHDR_NONE)
96                         break;
97                 if (nexthdr == NEXTHDR_FRAGMENT)
98                         break;
99                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
100                         BUG();
101                 if (nexthdr == NEXTHDR_AUTH)
102                         hdrlen = (hdr.hdrlen+2)<<2;
103                 else
104                         hdrlen = ipv6_optlen(&hdr);
105
106                 nexthdr = hdr.nexthdr;
107                 len -= hdrlen;
108                 start += hdrlen;
109         }
110
111         *nexthdrp = nexthdr;
112         return start;
113 }
114
115 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
116                             unsigned int *dataoff, u_int8_t *protonum)
117 {
118         unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
119         unsigned char pnum;
120         int protoff;
121
122         if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
123                           &pnum, sizeof(pnum)) != 0) {
124                 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
125                 return -NF_ACCEPT;
126         }
127         protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
128         /*
129          * (protoff == skb->len) mean that the packet doesn't have no data
130          * except of IPv6 & ext headers. but it's tracked anyway. - YK
131          */
132         if ((protoff < 0) || (protoff > skb->len)) {
133                 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
134                 return -NF_ACCEPT;
135         }
136
137         *dataoff = protoff;
138         *protonum = pnum;
139         return NF_ACCEPT;
140 }
141
142 static unsigned int ipv6_confirm(unsigned int hooknum,
143                                  struct sk_buff *skb,
144                                  const struct net_device *in,
145                                  const struct net_device *out,
146                                  int (*okfn)(struct sk_buff *))
147 {
148         struct nf_conn *ct;
149         const struct nf_conn_help *help;
150         const struct nf_conntrack_helper *helper;
151         enum ip_conntrack_info ctinfo;
152         unsigned int ret, protoff;
153         unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
154         unsigned char pnum = ipv6_hdr(skb)->nexthdr;
155
156
157         /* This is where we call the helper: as the packet goes out. */
158         ct = nf_ct_get(skb, &ctinfo);
159         if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
160                 goto out;
161
162         help = nfct_help(ct);
163         if (!help)
164                 goto out;
165         /* rcu_read_lock()ed by nf_hook_slow */
166         helper = rcu_dereference(help->helper);
167         if (!helper)
168                 goto out;
169
170         protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
171                                          skb->len - extoff);
172         if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
173                 pr_debug("proto header not found\n");
174                 return NF_ACCEPT;
175         }
176
177         ret = helper->help(skb, protoff, ct, ctinfo);
178         if (ret != NF_ACCEPT)
179                 return ret;
180 out:
181         /* We've seen it coming out the other side: confirm it */
182         return nf_conntrack_confirm(skb);
183 }
184
185 static unsigned int ipv6_defrag(unsigned int hooknum,
186                                 struct sk_buff *skb,
187                                 const struct net_device *in,
188                                 const struct net_device *out,
189                                 int (*okfn)(struct sk_buff *))
190 {
191         struct sk_buff *reasm;
192
193         /* Previously seen (loopback)?  */
194         if (skb->nfct)
195                 return NF_ACCEPT;
196
197         reasm = nf_ct_frag6_gather(skb);
198
199         /* queued */
200         if (reasm == NULL)
201                 return NF_STOLEN;
202
203         /* error occured or not fragmented */
204         if (reasm == skb)
205                 return NF_ACCEPT;
206
207         nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
208                            (struct net_device *)out, okfn);
209
210         return NF_STOLEN;
211 }
212
213 static unsigned int __ipv6_conntrack_in(struct net *net,
214                                         unsigned int hooknum,
215                                         struct sk_buff *skb,
216                                         int (*okfn)(struct sk_buff *))
217 {
218         struct sk_buff *reasm = skb->nfct_reasm;
219
220         /* This packet is fragmented and has reassembled packet. */
221         if (reasm) {
222                 /* Reassembled packet isn't parsed yet ? */
223                 if (!reasm->nfct) {
224                         unsigned int ret;
225
226                         ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
227                         if (ret != NF_ACCEPT)
228                                 return ret;
229                 }
230                 nf_conntrack_get(reasm->nfct);
231                 skb->nfct = reasm->nfct;
232                 skb->nfctinfo = reasm->nfctinfo;
233                 return NF_ACCEPT;
234         }
235
236         return nf_conntrack_in(net, PF_INET6, hooknum, skb);
237 }
238
239 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
240                                       struct sk_buff *skb,
241                                       const struct net_device *in,
242                                       const struct net_device *out,
243                                       int (*okfn)(struct sk_buff *))
244 {
245         return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
246 }
247
248 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
249                                          struct sk_buff *skb,
250                                          const struct net_device *in,
251                                          const struct net_device *out,
252                                          int (*okfn)(struct sk_buff *))
253 {
254         /* root is playing with raw sockets. */
255         if (skb->len < sizeof(struct ipv6hdr)) {
256                 if (net_ratelimit())
257                         printk("ipv6_conntrack_local: packet too short\n");
258                 return NF_ACCEPT;
259         }
260         return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
261 }
262
263 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
264         {
265                 .hook           = ipv6_defrag,
266                 .owner          = THIS_MODULE,
267                 .pf             = PF_INET6,
268                 .hooknum        = NF_INET_PRE_ROUTING,
269                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
270         },
271         {
272                 .hook           = ipv6_conntrack_in,
273                 .owner          = THIS_MODULE,
274                 .pf             = PF_INET6,
275                 .hooknum        = NF_INET_PRE_ROUTING,
276                 .priority       = NF_IP6_PRI_CONNTRACK,
277         },
278         {
279                 .hook           = ipv6_conntrack_local,
280                 .owner          = THIS_MODULE,
281                 .pf             = PF_INET6,
282                 .hooknum        = NF_INET_LOCAL_OUT,
283                 .priority       = NF_IP6_PRI_CONNTRACK,
284         },
285         {
286                 .hook           = ipv6_defrag,
287                 .owner          = THIS_MODULE,
288                 .pf             = PF_INET6,
289                 .hooknum        = NF_INET_LOCAL_OUT,
290                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
291         },
292         {
293                 .hook           = ipv6_confirm,
294                 .owner          = THIS_MODULE,
295                 .pf             = PF_INET6,
296                 .hooknum        = NF_INET_POST_ROUTING,
297                 .priority       = NF_IP6_PRI_LAST,
298         },
299         {
300                 .hook           = ipv6_confirm,
301                 .owner          = THIS_MODULE,
302                 .pf             = PF_INET6,
303                 .hooknum        = NF_INET_LOCAL_IN,
304                 .priority       = NF_IP6_PRI_LAST-1,
305         },
306 };
307
308 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
309
310 #include <linux/netfilter/nfnetlink.h>
311 #include <linux/netfilter/nfnetlink_conntrack.h>
312
313 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
314                                 const struct nf_conntrack_tuple *tuple)
315 {
316         NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
317                 &tuple->src.u3.ip6);
318         NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
319                 &tuple->dst.u3.ip6);
320         return 0;
321
322 nla_put_failure:
323         return -1;
324 }
325
326 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
327         [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
328         [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
329 };
330
331 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
332                                 struct nf_conntrack_tuple *t)
333 {
334         if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
335                 return -EINVAL;
336
337         memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
338                sizeof(u_int32_t) * 4);
339         memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
340                sizeof(u_int32_t) * 4);
341
342         return 0;
343 }
344 #endif
345
346 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
347         .l3proto                = PF_INET6,
348         .name                   = "ipv6",
349         .pkt_to_tuple           = ipv6_pkt_to_tuple,
350         .invert_tuple           = ipv6_invert_tuple,
351         .print_tuple            = ipv6_print_tuple,
352         .get_l4proto            = ipv6_get_l4proto,
353 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
354         .tuple_to_nlattr        = ipv6_tuple_to_nlattr,
355         .nlattr_to_tuple        = ipv6_nlattr_to_tuple,
356         .nla_policy             = ipv6_nla_policy,
357 #endif
358 #ifdef CONFIG_SYSCTL
359         .ctl_table_path         = nf_net_netfilter_sysctl_path,
360         .ctl_table              = nf_ct_ipv6_sysctl_table,
361 #endif
362         .me                     = THIS_MODULE,
363 };
364
365 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
366 MODULE_LICENSE("GPL");
367 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
368
369 static int __init nf_conntrack_l3proto_ipv6_init(void)
370 {
371         int ret = 0;
372
373         need_conntrack();
374
375         ret = nf_ct_frag6_init();
376         if (ret < 0) {
377                 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
378                 return ret;
379         }
380         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
381         if (ret < 0) {
382                 printk("nf_conntrack_ipv6: can't register tcp.\n");
383                 goto cleanup_frag6;
384         }
385
386         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
387         if (ret < 0) {
388                 printk("nf_conntrack_ipv6: can't register udp.\n");
389                 goto cleanup_tcp;
390         }
391
392         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
393         if (ret < 0) {
394                 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
395                 goto cleanup_udp;
396         }
397
398         ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
399         if (ret < 0) {
400                 printk("nf_conntrack_ipv6: can't register ipv6\n");
401                 goto cleanup_icmpv6;
402         }
403
404         ret = nf_register_hooks(ipv6_conntrack_ops,
405                                 ARRAY_SIZE(ipv6_conntrack_ops));
406         if (ret < 0) {
407                 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
408                        "hook.\n");
409                 goto cleanup_ipv6;
410         }
411         return ret;
412
413  cleanup_ipv6:
414         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
415  cleanup_icmpv6:
416         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
417  cleanup_udp:
418         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
419  cleanup_tcp:
420         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
421  cleanup_frag6:
422         nf_ct_frag6_cleanup();
423         return ret;
424 }
425
426 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
427 {
428         synchronize_net();
429         nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
430         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
431         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
432         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
433         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
434         nf_ct_frag6_cleanup();
435 }
436
437 module_init(nf_conntrack_l3proto_ipv6_init);
438 module_exit(nf_conntrack_l3proto_ipv6_fini);