2 * Copyright (C)2004 USAGI/WIDE Project
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.
9 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
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>
18 * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
19 * - add get_features() to support various size of conntrack
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>
33 #include <linux/netfilter_ipv6.h>
34 #include <net/netfilter/nf_conntrack.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_protocol.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_core.h>
43 #define DEBUGP(format, args...)
46 DECLARE_PER_CPU(struct ip_conntrack_stat
, nf_conntrack_stat
);
48 static int ipv6_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
49 struct nf_conntrack_tuple
*tuple
)
51 u_int32_t _addrs
[8], *ap
;
53 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct ipv6hdr
, saddr
),
54 sizeof(_addrs
), _addrs
);
58 memcpy(tuple
->src
.u3
.ip6
, ap
, sizeof(tuple
->src
.u3
.ip6
));
59 memcpy(tuple
->dst
.u3
.ip6
, ap
+ 4, sizeof(tuple
->dst
.u3
.ip6
));
64 static int ipv6_invert_tuple(struct nf_conntrack_tuple
*tuple
,
65 const struct nf_conntrack_tuple
*orig
)
67 memcpy(tuple
->src
.u3
.ip6
, orig
->dst
.u3
.ip6
, sizeof(tuple
->src
.u3
.ip6
));
68 memcpy(tuple
->dst
.u3
.ip6
, orig
->src
.u3
.ip6
, sizeof(tuple
->dst
.u3
.ip6
));
73 static int ipv6_print_tuple(struct seq_file
*s
,
74 const struct nf_conntrack_tuple
*tuple
)
76 return seq_printf(s
, "src=" NIP6_FMT
" dst=" NIP6_FMT
" ",
77 NIP6(*((struct in6_addr
*)tuple
->src
.u3
.ip6
)),
78 NIP6(*((struct in6_addr
*)tuple
->dst
.u3
.ip6
)));
81 static int ipv6_print_conntrack(struct seq_file
*s
,
82 const struct nf_conn
*conntrack
)
88 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
90 * This function parses (probably truncated) exthdr set "hdr"
91 * of length "len". "nexthdrp" initially points to some place,
92 * where type of the first header can be found.
94 * It skips all well-known exthdrs, and returns pointer to the start
95 * of unparsable area i.e. the first header with unknown type.
96 * if success, *nexthdr is updated by type/protocol of this header.
98 * NOTES: - it may return pointer pointing beyond end of packet,
99 * if the last recognized header is truncated in the middle.
100 * - if packet is truncated, so that all parsed headers are skipped,
102 * - if packet is fragmented, return pointer of the fragment header.
103 * - ESP is unparsable for now and considered like
104 * normal payload protocol.
105 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
108 int nf_ct_ipv6_skip_exthdr(struct sk_buff
*skb
, int start
, u8
*nexthdrp
,
111 u8 nexthdr
= *nexthdrp
;
113 while (ipv6_ext_hdr(nexthdr
)) {
114 struct ipv6_opt_hdr hdr
;
117 if (len
< (int)sizeof(struct ipv6_opt_hdr
))
119 if (nexthdr
== NEXTHDR_NONE
)
121 if (nexthdr
== NEXTHDR_FRAGMENT
)
123 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
125 if (nexthdr
== NEXTHDR_AUTH
)
126 hdrlen
= (hdr
.hdrlen
+2)<<2;
128 hdrlen
= ipv6_optlen(&hdr
);
130 nexthdr
= hdr
.nexthdr
;
140 ipv6_prepare(struct sk_buff
**pskb
, unsigned int hooknum
, unsigned int *dataoff
,
147 extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1) - (*pskb
)->data
;
148 pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
150 protoff
= nf_ct_ipv6_skip_exthdr(*pskb
, extoff
, &pnum
,
151 (*pskb
)->len
- extoff
);
154 * (protoff == (*pskb)->len) mean that the packet doesn't have no data
155 * except of IPv6 & ext headers. but it's tracked anyway. - YK
157 if ((protoff
< 0) || (protoff
> (*pskb
)->len
)) {
158 DEBUGP("ip6_conntrack_core: can't find proto in pkt\n");
159 NF_CT_STAT_INC(error
);
160 NF_CT_STAT_INC(invalid
);
169 static u_int32_t
ipv6_get_features(const struct nf_conntrack_tuple
*tuple
)
171 return NF_CT_F_BASIC
;
174 static unsigned int ipv6_confirm(unsigned int hooknum
,
175 struct sk_buff
**pskb
,
176 const struct net_device
*in
,
177 const struct net_device
*out
,
178 int (*okfn
)(struct sk_buff
*))
181 struct nf_conn_help
*help
;
182 enum ip_conntrack_info ctinfo
;
183 unsigned int ret
, protoff
;
184 unsigned int extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1)
186 unsigned char pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
189 /* This is where we call the helper: as the packet goes out. */
190 ct
= nf_ct_get(*pskb
, &ctinfo
);
191 if (!ct
|| ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
)
194 help
= nfct_help(ct
);
195 if (!help
|| !help
->helper
)
198 protoff
= nf_ct_ipv6_skip_exthdr(*pskb
, extoff
, &pnum
,
199 (*pskb
)->len
- extoff
);
200 if (protoff
< 0 || protoff
> (*pskb
)->len
||
201 pnum
== NEXTHDR_FRAGMENT
) {
202 DEBUGP("proto header not found\n");
206 ret
= help
->helper
->help(pskb
, protoff
, ct
, ctinfo
);
207 if (ret
!= NF_ACCEPT
)
210 /* We've seen it coming out the other side: confirm it */
211 return nf_conntrack_confirm(pskb
);
214 extern struct sk_buff
*nf_ct_frag6_gather(struct sk_buff
*skb
);
215 extern void nf_ct_frag6_output(unsigned int hooknum
, struct sk_buff
*skb
,
216 struct net_device
*in
,
217 struct net_device
*out
,
218 int (*okfn
)(struct sk_buff
*));
219 static unsigned int ipv6_defrag(unsigned int hooknum
,
220 struct sk_buff
**pskb
,
221 const struct net_device
*in
,
222 const struct net_device
*out
,
223 int (*okfn
)(struct sk_buff
*))
225 struct sk_buff
*reasm
;
227 /* Previously seen (loopback)? */
231 reasm
= nf_ct_frag6_gather(*pskb
);
237 /* error occured or not fragmented */
241 nf_ct_frag6_output(hooknum
, reasm
, (struct net_device
*)in
,
242 (struct net_device
*)out
, okfn
);
247 static unsigned int ipv6_conntrack_in(unsigned int hooknum
,
248 struct sk_buff
**pskb
,
249 const struct net_device
*in
,
250 const struct net_device
*out
,
251 int (*okfn
)(struct sk_buff
*))
253 struct sk_buff
*reasm
= (*pskb
)->nfct_reasm
;
255 /* This packet is fragmented and has reassembled packet. */
257 /* Reassembled packet isn't parsed yet ? */
261 ret
= nf_conntrack_in(PF_INET6
, hooknum
, &reasm
);
262 if (ret
!= NF_ACCEPT
)
265 nf_conntrack_get(reasm
->nfct
);
266 (*pskb
)->nfct
= reasm
->nfct
;
270 return nf_conntrack_in(PF_INET6
, hooknum
, pskb
);
273 static unsigned int ipv6_conntrack_local(unsigned int hooknum
,
274 struct sk_buff
**pskb
,
275 const struct net_device
*in
,
276 const struct net_device
*out
,
277 int (*okfn
)(struct sk_buff
*))
279 /* root is playing with raw sockets. */
280 if ((*pskb
)->len
< sizeof(struct ipv6hdr
)) {
282 printk("ipv6_conntrack_local: packet too short\n");
285 return ipv6_conntrack_in(hooknum
, pskb
, in
, out
, okfn
);
288 static struct nf_hook_ops ipv6_conntrack_ops
[] = {
291 .owner
= THIS_MODULE
,
293 .hooknum
= NF_IP6_PRE_ROUTING
,
294 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
297 .hook
= ipv6_conntrack_in
,
298 .owner
= THIS_MODULE
,
300 .hooknum
= NF_IP6_PRE_ROUTING
,
301 .priority
= NF_IP6_PRI_CONNTRACK
,
304 .hook
= ipv6_conntrack_local
,
305 .owner
= THIS_MODULE
,
307 .hooknum
= NF_IP6_LOCAL_OUT
,
308 .priority
= NF_IP6_PRI_CONNTRACK
,
312 .owner
= THIS_MODULE
,
314 .hooknum
= NF_IP6_LOCAL_OUT
,
315 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
318 .hook
= ipv6_confirm
,
319 .owner
= THIS_MODULE
,
321 .hooknum
= NF_IP6_POST_ROUTING
,
322 .priority
= NF_IP6_PRI_LAST
,
325 .hook
= ipv6_confirm
,
326 .owner
= THIS_MODULE
,
328 .hooknum
= NF_IP6_LOCAL_IN
,
329 .priority
= NF_IP6_PRI_LAST
-1,
335 /* From nf_conntrack_proto_icmpv6.c */
336 extern unsigned int nf_ct_icmpv6_timeout
;
338 /* From nf_conntrack_reasm.c */
339 extern unsigned int nf_ct_frag6_timeout
;
340 extern unsigned int nf_ct_frag6_low_thresh
;
341 extern unsigned int nf_ct_frag6_high_thresh
;
343 static struct ctl_table_header
*nf_ct_ipv6_sysctl_header
;
345 static ctl_table nf_ct_sysctl_table
[] = {
347 .ctl_name
= NET_NF_CONNTRACK_ICMPV6_TIMEOUT
,
348 .procname
= "nf_conntrack_icmpv6_timeout",
349 .data
= &nf_ct_icmpv6_timeout
,
350 .maxlen
= sizeof(unsigned int),
352 .proc_handler
= &proc_dointvec_jiffies
,
355 .ctl_name
= NET_NF_CONNTRACK_FRAG6_TIMEOUT
,
356 .procname
= "nf_conntrack_frag6_timeout",
357 .data
= &nf_ct_frag6_timeout
,
358 .maxlen
= sizeof(unsigned int),
360 .proc_handler
= &proc_dointvec_jiffies
,
363 .ctl_name
= NET_NF_CONNTRACK_FRAG6_LOW_THRESH
,
364 .procname
= "nf_conntrack_frag6_low_thresh",
365 .data
= &nf_ct_frag6_low_thresh
,
366 .maxlen
= sizeof(unsigned int),
368 .proc_handler
= &proc_dointvec
,
371 .ctl_name
= NET_NF_CONNTRACK_FRAG6_HIGH_THRESH
,
372 .procname
= "nf_conntrack_frag6_high_thresh",
373 .data
= &nf_ct_frag6_high_thresh
,
374 .maxlen
= sizeof(unsigned int),
376 .proc_handler
= &proc_dointvec
,
381 static ctl_table nf_ct_netfilter_table
[] = {
383 .ctl_name
= NET_NETFILTER
,
384 .procname
= "netfilter",
386 .child
= nf_ct_sysctl_table
,
391 static ctl_table nf_ct_net_table
[] = {
396 .child
= nf_ct_netfilter_table
,
402 #if defined(CONFIG_NF_CT_NETLINK) || \
403 defined(CONFIG_NF_CT_NETLINK_MODULE)
405 #include <linux/netfilter/nfnetlink.h>
406 #include <linux/netfilter/nfnetlink_conntrack.h>
408 static int ipv6_tuple_to_nfattr(struct sk_buff
*skb
,
409 const struct nf_conntrack_tuple
*tuple
)
411 NFA_PUT(skb
, CTA_IP_V6_SRC
, sizeof(u_int32_t
) * 4,
413 NFA_PUT(skb
, CTA_IP_V6_DST
, sizeof(u_int32_t
) * 4,
421 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
422 [CTA_IP_V6_SRC
-1] = sizeof(u_int32_t
)*4,
423 [CTA_IP_V6_DST
-1] = sizeof(u_int32_t
)*4,
426 static int ipv6_nfattr_to_tuple(struct nfattr
*tb
[],
427 struct nf_conntrack_tuple
*t
)
429 if (!tb
[CTA_IP_V6_SRC
-1] || !tb
[CTA_IP_V6_DST
-1])
432 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
435 memcpy(&t
->src
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_SRC
-1]),
436 sizeof(u_int32_t
) * 4);
437 memcpy(&t
->dst
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_DST
-1]),
438 sizeof(u_int32_t
) * 4);
444 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6
= {
447 .pkt_to_tuple
= ipv6_pkt_to_tuple
,
448 .invert_tuple
= ipv6_invert_tuple
,
449 .print_tuple
= ipv6_print_tuple
,
450 .print_conntrack
= ipv6_print_conntrack
,
451 .prepare
= ipv6_prepare
,
452 #if defined(CONFIG_NF_CT_NETLINK) || \
453 defined(CONFIG_NF_CT_NETLINK_MODULE)
454 .tuple_to_nfattr
= ipv6_tuple_to_nfattr
,
455 .nfattr_to_tuple
= ipv6_nfattr_to_tuple
,
457 .get_features
= ipv6_get_features
,
461 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp6
;
462 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp6
;
463 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmpv6
;
464 extern int nf_ct_frag6_init(void);
465 extern void nf_ct_frag6_cleanup(void);
467 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6
));
468 MODULE_LICENSE("GPL");
469 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
471 static int __init
nf_conntrack_l3proto_ipv6_init(void)
477 ret
= nf_ct_frag6_init();
479 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
482 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp6
);
484 printk("nf_conntrack_ipv6: can't register tcp.\n");
488 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_udp6
);
490 printk("nf_conntrack_ipv6: can't register udp.\n");
494 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_icmpv6
);
496 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
500 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6
);
502 printk("nf_conntrack_ipv6: can't register ipv6\n");
506 ret
= nf_register_hooks(ipv6_conntrack_ops
,
507 ARRAY_SIZE(ipv6_conntrack_ops
));
509 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
514 nf_ct_ipv6_sysctl_header
= register_sysctl_table(nf_ct_net_table
, 0);
515 if (nf_ct_ipv6_sysctl_header
== NULL
) {
516 printk("nf_conntrack: can't register to sysctl.\n");
525 nf_unregister_hooks(ipv6_conntrack_ops
, ARRAY_SIZE(ipv6_conntrack_ops
));
528 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6
);
530 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmpv6
);
532 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp6
);
534 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp6
);
536 nf_ct_frag6_cleanup();
540 static void __exit
nf_conntrack_l3proto_ipv6_fini(void)
544 unregister_sysctl_table(nf_ct_ipv6_sysctl_header
);
546 nf_unregister_hooks(ipv6_conntrack_ops
, ARRAY_SIZE(ipv6_conntrack_ops
));
547 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6
);
548 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmpv6
);
549 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp6
);
550 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp6
);
551 nf_ct_frag6_cleanup();
554 module_init(nf_conntrack_l3proto_ipv6_init
);
555 module_exit(nf_conntrack_l3proto_ipv6_fini
);