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