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