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/config.h>
24 #include <linux/types.h>
25 #include <linux/ipv6.h>
26 #include <linux/in6.h>
27 #include <linux/netfilter.h>
28 #include <linux/module.h>
29 #include <linux/skbuff.h>
30 #include <linux/icmp.h>
31 #include <linux/sysctl.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_protocol.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_core.h>
44 #define DEBUGP(format, args...)
47 DECLARE_PER_CPU(struct ip_conntrack_stat
, nf_conntrack_stat
);
49 static int ipv6_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
50 struct nf_conntrack_tuple
*tuple
)
52 u_int32_t _addrs
[8], *ap
;
54 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct ipv6hdr
, saddr
),
55 sizeof(_addrs
), _addrs
);
59 memcpy(tuple
->src
.u3
.ip6
, ap
, sizeof(tuple
->src
.u3
.ip6
));
60 memcpy(tuple
->dst
.u3
.ip6
, ap
+ 4, sizeof(tuple
->dst
.u3
.ip6
));
65 static int ipv6_invert_tuple(struct nf_conntrack_tuple
*tuple
,
66 const struct nf_conntrack_tuple
*orig
)
68 memcpy(tuple
->src
.u3
.ip6
, orig
->dst
.u3
.ip6
, sizeof(tuple
->src
.u3
.ip6
));
69 memcpy(tuple
->dst
.u3
.ip6
, orig
->src
.u3
.ip6
, sizeof(tuple
->dst
.u3
.ip6
));
74 static int ipv6_print_tuple(struct seq_file
*s
,
75 const struct nf_conntrack_tuple
*tuple
)
77 return seq_printf(s
, "src=%x:%x:%x:%x:%x:%x:%x:%x dst=%x:%x:%x:%x:%x:%x:%x:%x ",
78 NIP6(*((struct in6_addr
*)tuple
->src
.u3
.ip6
)),
79 NIP6(*((struct in6_addr
*)tuple
->dst
.u3
.ip6
)));
82 static int ipv6_print_conntrack(struct seq_file
*s
,
83 const struct nf_conn
*conntrack
)
89 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
91 * This function parses (probably truncated) exthdr set "hdr"
92 * of length "len". "nexthdrp" initially points to some place,
93 * where type of the first header can be found.
95 * It skips all well-known exthdrs, and returns pointer to the start
96 * of unparsable area i.e. the first header with unknown type.
97 * if success, *nexthdr is updated by type/protocol of this header.
99 * NOTES: - it may return pointer pointing beyond end of packet,
100 * if the last recognized header is truncated in the middle.
101 * - if packet is truncated, so that all parsed headers are skipped,
103 * - if packet is fragmented, return pointer of the fragment header.
104 * - ESP is unparsable for now and considered like
105 * normal payload protocol.
106 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
109 int nf_ct_ipv6_skip_exthdr(struct sk_buff
*skb
, int start
, u8
*nexthdrp
,
112 u8 nexthdr
= *nexthdrp
;
114 while (ipv6_ext_hdr(nexthdr
)) {
115 struct ipv6_opt_hdr hdr
;
118 if (len
< (int)sizeof(struct ipv6_opt_hdr
))
120 if (nexthdr
== NEXTHDR_NONE
)
122 if (nexthdr
== NEXTHDR_FRAGMENT
)
124 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
126 if (nexthdr
== NEXTHDR_AUTH
)
127 hdrlen
= (hdr
.hdrlen
+2)<<2;
129 hdrlen
= ipv6_optlen(&hdr
);
131 nexthdr
= hdr
.nexthdr
;
141 ipv6_prepare(struct sk_buff
**pskb
, unsigned int hooknum
, unsigned int *dataoff
,
148 extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1) - (*pskb
)->data
;
149 pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
151 protoff
= nf_ct_ipv6_skip_exthdr(*pskb
, extoff
, &pnum
,
152 (*pskb
)->len
- extoff
);
155 * (protoff == (*pskb)->len) mean that the packet doesn't have no data
156 * except of IPv6 & ext headers. but it's tracked anyway. - YK
158 if ((protoff
< 0) || (protoff
> (*pskb
)->len
)) {
159 DEBUGP("ip6_conntrack_core: can't find proto in pkt\n");
160 NF_CT_STAT_INC(error
);
161 NF_CT_STAT_INC(invalid
);
170 static u_int32_t
ipv6_get_features(const struct nf_conntrack_tuple
*tuple
)
172 return NF_CT_F_BASIC
;
175 static unsigned int ipv6_confirm(unsigned int hooknum
,
176 struct sk_buff
**pskb
,
177 const struct net_device
*in
,
178 const struct net_device
*out
,
179 int (*okfn
)(struct sk_buff
*))
182 enum ip_conntrack_info ctinfo
;
184 /* This is where we call the helper: as the packet goes out. */
185 ct
= nf_ct_get(*pskb
, &ctinfo
);
186 if (ct
&& ct
->helper
) {
187 unsigned int ret
, protoff
;
188 unsigned int extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1)
190 unsigned char pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
192 protoff
= nf_ct_ipv6_skip_exthdr(*pskb
, extoff
, &pnum
,
193 (*pskb
)->len
- extoff
);
194 if (protoff
< 0 || protoff
> (*pskb
)->len
||
195 pnum
== NEXTHDR_FRAGMENT
) {
196 DEBUGP("proto header not found\n");
200 ret
= ct
->helper
->help(pskb
, protoff
, ct
, ctinfo
);
201 if (ret
!= NF_ACCEPT
)
205 /* We've seen it coming out the other side: confirm it */
207 return nf_conntrack_confirm(pskb
);
210 extern struct sk_buff
*nf_ct_frag6_gather(struct sk_buff
*skb
);
211 extern void nf_ct_frag6_output(unsigned int hooknum
, struct sk_buff
*skb
,
212 struct net_device
*in
,
213 struct net_device
*out
,
214 int (*okfn
)(struct sk_buff
*));
215 static unsigned int ipv6_defrag(unsigned int hooknum
,
216 struct sk_buff
**pskb
,
217 const struct net_device
*in
,
218 const struct net_device
*out
,
219 int (*okfn
)(struct sk_buff
*))
221 struct sk_buff
*reasm
;
223 /* Previously seen (loopback)? */
227 reasm
= nf_ct_frag6_gather(*pskb
);
233 /* error occured or not fragmented */
237 nf_ct_frag6_output(hooknum
, reasm
, (struct net_device
*)in
,
238 (struct net_device
*)out
, okfn
);
243 static unsigned int ipv6_conntrack_in(unsigned int hooknum
,
244 struct sk_buff
**pskb
,
245 const struct net_device
*in
,
246 const struct net_device
*out
,
247 int (*okfn
)(struct sk_buff
*))
249 struct sk_buff
*reasm
= (*pskb
)->nfct_reasm
;
251 /* This packet is fragmented and has reassembled packet. */
253 /* Reassembled packet isn't parsed yet ? */
257 ret
= nf_conntrack_in(PF_INET6
, hooknum
, &reasm
);
258 if (ret
!= NF_ACCEPT
)
261 nf_conntrack_get(reasm
->nfct
);
262 (*pskb
)->nfct
= reasm
->nfct
;
266 return nf_conntrack_in(PF_INET6
, hooknum
, pskb
);
269 static unsigned int ipv6_conntrack_local(unsigned int hooknum
,
270 struct sk_buff
**pskb
,
271 const struct net_device
*in
,
272 const struct net_device
*out
,
273 int (*okfn
)(struct sk_buff
*))
275 /* root is playing with raw sockets. */
276 if ((*pskb
)->len
< sizeof(struct ipv6hdr
)) {
278 printk("ipv6_conntrack_local: packet too short\n");
281 return ipv6_conntrack_in(hooknum
, pskb
, in
, out
, okfn
);
284 /* Connection tracking may drop packets, but never alters them, so
285 make it the first hook. */
286 static struct nf_hook_ops ipv6_conntrack_defrag_ops
= {
288 .owner
= THIS_MODULE
,
290 .hooknum
= NF_IP6_PRE_ROUTING
,
291 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
294 static struct nf_hook_ops ipv6_conntrack_in_ops
= {
295 .hook
= ipv6_conntrack_in
,
296 .owner
= THIS_MODULE
,
298 .hooknum
= NF_IP6_PRE_ROUTING
,
299 .priority
= NF_IP6_PRI_CONNTRACK
,
302 static struct nf_hook_ops ipv6_conntrack_local_out_ops
= {
303 .hook
= ipv6_conntrack_local
,
304 .owner
= THIS_MODULE
,
306 .hooknum
= NF_IP6_LOCAL_OUT
,
307 .priority
= NF_IP6_PRI_CONNTRACK
,
310 static struct nf_hook_ops ipv6_conntrack_defrag_local_out_ops
= {
312 .owner
= THIS_MODULE
,
314 .hooknum
= NF_IP6_LOCAL_OUT
,
315 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
318 /* Refragmenter; last chance. */
319 static struct nf_hook_ops ipv6_conntrack_out_ops
= {
320 .hook
= ipv6_confirm
,
321 .owner
= THIS_MODULE
,
323 .hooknum
= NF_IP6_POST_ROUTING
,
324 .priority
= NF_IP6_PRI_LAST
,
327 static struct nf_hook_ops ipv6_conntrack_local_in_ops
= {
328 .hook
= ipv6_confirm
,
329 .owner
= THIS_MODULE
,
331 .hooknum
= NF_IP6_LOCAL_IN
,
332 .priority
= NF_IP6_PRI_LAST
-1,
337 /* From nf_conntrack_proto_icmpv6.c */
338 extern unsigned int nf_ct_icmpv6_timeout
;
340 /* From nf_conntrack_frag6.c */
341 extern unsigned int nf_ct_frag6_timeout
;
342 extern unsigned int nf_ct_frag6_low_thresh
;
343 extern unsigned int nf_ct_frag6_high_thresh
;
345 static struct ctl_table_header
*nf_ct_ipv6_sysctl_header
;
347 static ctl_table nf_ct_sysctl_table
[] = {
349 .ctl_name
= NET_NF_CONNTRACK_ICMPV6_TIMEOUT
,
350 .procname
= "nf_conntrack_icmpv6_timeout",
351 .data
= &nf_ct_icmpv6_timeout
,
352 .maxlen
= sizeof(unsigned int),
354 .proc_handler
= &proc_dointvec_jiffies
,
357 .ctl_name
= NET_NF_CONNTRACK_FRAG6_TIMEOUT
,
358 .procname
= "nf_conntrack_frag6_timeout",
359 .data
= &nf_ct_frag6_timeout
,
360 .maxlen
= sizeof(unsigned int),
362 .proc_handler
= &proc_dointvec_jiffies
,
365 .ctl_name
= NET_NF_CONNTRACK_FRAG6_LOW_THRESH
,
366 .procname
= "nf_conntrack_frag6_low_thresh",
367 .data
= &nf_ct_frag6_low_thresh
,
368 .maxlen
= sizeof(unsigned int),
370 .proc_handler
= &proc_dointvec
,
373 .ctl_name
= NET_NF_CONNTRACK_FRAG6_HIGH_THRESH
,
374 .procname
= "nf_conntrack_frag6_high_thresh",
375 .data
= &nf_ct_frag6_high_thresh
,
376 .maxlen
= sizeof(unsigned int),
378 .proc_handler
= &proc_dointvec
,
383 static ctl_table nf_ct_netfilter_table
[] = {
385 .ctl_name
= NET_NETFILTER
,
386 .procname
= "netfilter",
388 .child
= nf_ct_sysctl_table
,
393 static ctl_table nf_ct_net_table
[] = {
398 .child
= nf_ct_netfilter_table
,
404 #if defined(CONFIG_NF_CT_NETLINK) || \
405 defined(CONFIG_NF_CT_NETLINK_MODULE)
407 #include <linux/netfilter/nfnetlink.h>
408 #include <linux/netfilter/nfnetlink_conntrack.h>
410 static int ipv6_tuple_to_nfattr(struct sk_buff
*skb
,
411 const struct nf_conntrack_tuple
*tuple
)
413 NFA_PUT(skb
, CTA_IP_V6_SRC
, sizeof(u_int32_t
) * 4,
415 NFA_PUT(skb
, CTA_IP_V6_DST
, sizeof(u_int32_t
) * 4,
423 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
424 [CTA_IP_V6_SRC
-1] = sizeof(u_int32_t
)*4,
425 [CTA_IP_V6_DST
-1] = sizeof(u_int32_t
)*4,
428 static int ipv6_nfattr_to_tuple(struct nfattr
*tb
[],
429 struct nf_conntrack_tuple
*t
)
431 if (!tb
[CTA_IP_V6_SRC
-1] || !tb
[CTA_IP_V6_DST
-1])
434 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
437 memcpy(&t
->src
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_SRC
-1]),
438 sizeof(u_int32_t
) * 4);
439 memcpy(&t
->dst
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_DST
-1]),
440 sizeof(u_int32_t
) * 4);
446 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6
= {
449 .pkt_to_tuple
= ipv6_pkt_to_tuple
,
450 .invert_tuple
= ipv6_invert_tuple
,
451 .print_tuple
= ipv6_print_tuple
,
452 .print_conntrack
= ipv6_print_conntrack
,
453 .prepare
= ipv6_prepare
,
454 #if defined(CONFIG_NF_CT_NETLINK) || \
455 defined(CONFIG_NF_CT_NETLINK_MODULE)
456 .tuple_to_nfattr
= ipv6_tuple_to_nfattr
,
457 .nfattr_to_tuple
= ipv6_nfattr_to_tuple
,
459 .get_features
= ipv6_get_features
,
463 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp6
;
464 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp6
;
465 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmpv6
;
466 extern int nf_ct_frag6_init(void);
467 extern void nf_ct_frag6_cleanup(void);
468 static int init_or_cleanup(int init
)
472 if (!init
) goto cleanup
;
474 ret
= nf_ct_frag6_init();
476 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
477 goto cleanup_nothing
;
479 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp6
);
481 printk("nf_conntrack_ipv6: can't register tcp.\n");
485 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_udp6
);
487 printk("nf_conntrack_ipv6: can't register udp.\n");
491 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_icmpv6
);
493 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
497 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6
);
499 printk("nf_conntrack_ipv6: can't register ipv6\n");
503 ret
= nf_register_hook(&ipv6_conntrack_defrag_ops
);
505 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
510 ret
= nf_register_hook(&ipv6_conntrack_defrag_local_out_ops
);
512 printk("nf_conntrack_ipv6: can't register local_out defrag "
514 goto cleanup_defragops
;
517 ret
= nf_register_hook(&ipv6_conntrack_in_ops
);
519 printk("nf_conntrack_ipv6: can't register pre-routing hook.\n");
520 goto cleanup_defraglocalops
;
523 ret
= nf_register_hook(&ipv6_conntrack_local_out_ops
);
525 printk("nf_conntrack_ipv6: can't register local out hook.\n");
529 ret
= nf_register_hook(&ipv6_conntrack_out_ops
);
531 printk("nf_conntrack_ipv6: can't register post-routing hook.\n");
532 goto cleanup_inandlocalops
;
535 ret
= nf_register_hook(&ipv6_conntrack_local_in_ops
);
537 printk("nf_conntrack_ipv6: can't register local in hook.\n");
538 goto cleanup_inoutandlocalops
;
542 nf_ct_ipv6_sysctl_header
= register_sysctl_table(nf_ct_net_table
, 0);
543 if (nf_ct_ipv6_sysctl_header
== NULL
) {
544 printk("nf_conntrack: can't register to sysctl.\n");
546 goto cleanup_localinops
;
554 unregister_sysctl_table(nf_ct_ipv6_sysctl_header
);
557 nf_unregister_hook(&ipv6_conntrack_local_in_ops
);
558 cleanup_inoutandlocalops
:
559 nf_unregister_hook(&ipv6_conntrack_out_ops
);
560 cleanup_inandlocalops
:
561 nf_unregister_hook(&ipv6_conntrack_local_out_ops
);
563 nf_unregister_hook(&ipv6_conntrack_in_ops
);
564 cleanup_defraglocalops
:
565 nf_unregister_hook(&ipv6_conntrack_defrag_local_out_ops
);
567 nf_unregister_hook(&ipv6_conntrack_defrag_ops
);
569 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6
);
571 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmpv6
);
573 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp6
);
575 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp6
);
577 nf_ct_frag6_cleanup();
582 MODULE_LICENSE("GPL");
583 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
585 static int __init
init(void)
588 return init_or_cleanup(1);
591 static void __exit
fini(void)
599 void need_ip6_conntrack(void)
603 EXPORT_SYMBOL(need_ip6_conntrack
);