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