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