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=" NIP6_FMT
" dst=" NIP6_FMT
" ",
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 struct nf_conn_help
*help
;
183 enum ip_conntrack_info ctinfo
;
184 unsigned int ret
, protoff
;
185 unsigned int extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1)
187 unsigned char pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
190 /* This is where we call the helper: as the packet goes out. */
191 ct
= nf_ct_get(*pskb
, &ctinfo
);
195 help
= nfct_help(ct
);
196 if (!help
|| !help
->helper
)
199 protoff
= nf_ct_ipv6_skip_exthdr(*pskb
, extoff
, &pnum
,
200 (*pskb
)->len
- extoff
);
201 if (protoff
< 0 || protoff
> (*pskb
)->len
||
202 pnum
== NEXTHDR_FRAGMENT
) {
203 DEBUGP("proto header not found\n");
207 ret
= help
->helper
->help(pskb
, protoff
, ct
, ctinfo
);
208 if (ret
!= NF_ACCEPT
)
211 /* We've seen it coming out the other side: confirm it */
212 return nf_conntrack_confirm(pskb
);
215 extern struct sk_buff
*nf_ct_frag6_gather(struct sk_buff
*skb
);
216 extern void nf_ct_frag6_output(unsigned int hooknum
, struct sk_buff
*skb
,
217 struct net_device
*in
,
218 struct net_device
*out
,
219 int (*okfn
)(struct sk_buff
*));
220 static unsigned int ipv6_defrag(unsigned int hooknum
,
221 struct sk_buff
**pskb
,
222 const struct net_device
*in
,
223 const struct net_device
*out
,
224 int (*okfn
)(struct sk_buff
*))
226 struct sk_buff
*reasm
;
228 /* Previously seen (loopback)? */
232 reasm
= nf_ct_frag6_gather(*pskb
);
238 /* error occured or not fragmented */
242 nf_ct_frag6_output(hooknum
, reasm
, (struct net_device
*)in
,
243 (struct net_device
*)out
, okfn
);
248 static unsigned int ipv6_conntrack_in(unsigned int hooknum
,
249 struct sk_buff
**pskb
,
250 const struct net_device
*in
,
251 const struct net_device
*out
,
252 int (*okfn
)(struct sk_buff
*))
254 struct sk_buff
*reasm
= (*pskb
)->nfct_reasm
;
256 /* This packet is fragmented and has reassembled packet. */
258 /* Reassembled packet isn't parsed yet ? */
262 ret
= nf_conntrack_in(PF_INET6
, hooknum
, &reasm
);
263 if (ret
!= NF_ACCEPT
)
266 nf_conntrack_get(reasm
->nfct
);
267 (*pskb
)->nfct
= reasm
->nfct
;
271 return nf_conntrack_in(PF_INET6
, hooknum
, pskb
);
274 static unsigned int ipv6_conntrack_local(unsigned int hooknum
,
275 struct sk_buff
**pskb
,
276 const struct net_device
*in
,
277 const struct net_device
*out
,
278 int (*okfn
)(struct sk_buff
*))
280 /* root is playing with raw sockets. */
281 if ((*pskb
)->len
< sizeof(struct ipv6hdr
)) {
283 printk("ipv6_conntrack_local: packet too short\n");
286 return ipv6_conntrack_in(hooknum
, pskb
, in
, out
, okfn
);
289 /* Connection tracking may drop packets, but never alters them, so
290 make it the first hook. */
291 static struct nf_hook_ops ipv6_conntrack_defrag_ops
= {
293 .owner
= THIS_MODULE
,
295 .hooknum
= NF_IP6_PRE_ROUTING
,
296 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
299 static struct nf_hook_ops ipv6_conntrack_in_ops
= {
300 .hook
= ipv6_conntrack_in
,
301 .owner
= THIS_MODULE
,
303 .hooknum
= NF_IP6_PRE_ROUTING
,
304 .priority
= NF_IP6_PRI_CONNTRACK
,
307 static struct nf_hook_ops ipv6_conntrack_local_out_ops
= {
308 .hook
= ipv6_conntrack_local
,
309 .owner
= THIS_MODULE
,
311 .hooknum
= NF_IP6_LOCAL_OUT
,
312 .priority
= NF_IP6_PRI_CONNTRACK
,
315 static struct nf_hook_ops ipv6_conntrack_defrag_local_out_ops
= {
317 .owner
= THIS_MODULE
,
319 .hooknum
= NF_IP6_LOCAL_OUT
,
320 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
323 /* Refragmenter; last chance. */
324 static struct nf_hook_ops ipv6_conntrack_out_ops
= {
325 .hook
= ipv6_confirm
,
326 .owner
= THIS_MODULE
,
328 .hooknum
= NF_IP6_POST_ROUTING
,
329 .priority
= NF_IP6_PRI_LAST
,
332 static struct nf_hook_ops ipv6_conntrack_local_in_ops
= {
333 .hook
= ipv6_confirm
,
334 .owner
= THIS_MODULE
,
336 .hooknum
= NF_IP6_LOCAL_IN
,
337 .priority
= NF_IP6_PRI_LAST
-1,
342 /* From nf_conntrack_proto_icmpv6.c */
343 extern unsigned int nf_ct_icmpv6_timeout
;
345 /* From nf_conntrack_frag6.c */
346 extern unsigned int nf_ct_frag6_timeout
;
347 extern unsigned int nf_ct_frag6_low_thresh
;
348 extern unsigned int nf_ct_frag6_high_thresh
;
350 static struct ctl_table_header
*nf_ct_ipv6_sysctl_header
;
352 static ctl_table nf_ct_sysctl_table
[] = {
354 .ctl_name
= NET_NF_CONNTRACK_ICMPV6_TIMEOUT
,
355 .procname
= "nf_conntrack_icmpv6_timeout",
356 .data
= &nf_ct_icmpv6_timeout
,
357 .maxlen
= sizeof(unsigned int),
359 .proc_handler
= &proc_dointvec_jiffies
,
362 .ctl_name
= NET_NF_CONNTRACK_FRAG6_TIMEOUT
,
363 .procname
= "nf_conntrack_frag6_timeout",
364 .data
= &nf_ct_frag6_timeout
,
365 .maxlen
= sizeof(unsigned int),
367 .proc_handler
= &proc_dointvec_jiffies
,
370 .ctl_name
= NET_NF_CONNTRACK_FRAG6_LOW_THRESH
,
371 .procname
= "nf_conntrack_frag6_low_thresh",
372 .data
= &nf_ct_frag6_low_thresh
,
373 .maxlen
= sizeof(unsigned int),
375 .proc_handler
= &proc_dointvec
,
378 .ctl_name
= NET_NF_CONNTRACK_FRAG6_HIGH_THRESH
,
379 .procname
= "nf_conntrack_frag6_high_thresh",
380 .data
= &nf_ct_frag6_high_thresh
,
381 .maxlen
= sizeof(unsigned int),
383 .proc_handler
= &proc_dointvec
,
388 static ctl_table nf_ct_netfilter_table
[] = {
390 .ctl_name
= NET_NETFILTER
,
391 .procname
= "netfilter",
393 .child
= nf_ct_sysctl_table
,
398 static ctl_table nf_ct_net_table
[] = {
403 .child
= nf_ct_netfilter_table
,
409 #if defined(CONFIG_NF_CT_NETLINK) || \
410 defined(CONFIG_NF_CT_NETLINK_MODULE)
412 #include <linux/netfilter/nfnetlink.h>
413 #include <linux/netfilter/nfnetlink_conntrack.h>
415 static int ipv6_tuple_to_nfattr(struct sk_buff
*skb
,
416 const struct nf_conntrack_tuple
*tuple
)
418 NFA_PUT(skb
, CTA_IP_V6_SRC
, sizeof(u_int32_t
) * 4,
420 NFA_PUT(skb
, CTA_IP_V6_DST
, sizeof(u_int32_t
) * 4,
428 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
429 [CTA_IP_V6_SRC
-1] = sizeof(u_int32_t
)*4,
430 [CTA_IP_V6_DST
-1] = sizeof(u_int32_t
)*4,
433 static int ipv6_nfattr_to_tuple(struct nfattr
*tb
[],
434 struct nf_conntrack_tuple
*t
)
436 if (!tb
[CTA_IP_V6_SRC
-1] || !tb
[CTA_IP_V6_DST
-1])
439 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
442 memcpy(&t
->src
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_SRC
-1]),
443 sizeof(u_int32_t
) * 4);
444 memcpy(&t
->dst
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_DST
-1]),
445 sizeof(u_int32_t
) * 4);
451 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6
= {
454 .pkt_to_tuple
= ipv6_pkt_to_tuple
,
455 .invert_tuple
= ipv6_invert_tuple
,
456 .print_tuple
= ipv6_print_tuple
,
457 .print_conntrack
= ipv6_print_conntrack
,
458 .prepare
= ipv6_prepare
,
459 #if defined(CONFIG_NF_CT_NETLINK) || \
460 defined(CONFIG_NF_CT_NETLINK_MODULE)
461 .tuple_to_nfattr
= ipv6_tuple_to_nfattr
,
462 .nfattr_to_tuple
= ipv6_nfattr_to_tuple
,
464 .get_features
= ipv6_get_features
,
468 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp6
;
469 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp6
;
470 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmpv6
;
471 extern int nf_ct_frag6_init(void);
472 extern void nf_ct_frag6_cleanup(void);
473 static int init_or_cleanup(int init
)
477 if (!init
) goto cleanup
;
479 ret
= nf_ct_frag6_init();
481 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
482 goto cleanup_nothing
;
484 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp6
);
486 printk("nf_conntrack_ipv6: can't register tcp.\n");
490 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_udp6
);
492 printk("nf_conntrack_ipv6: can't register udp.\n");
496 ret
= nf_conntrack_protocol_register(&nf_conntrack_protocol_icmpv6
);
498 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
502 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6
);
504 printk("nf_conntrack_ipv6: can't register ipv6\n");
508 ret
= nf_register_hook(&ipv6_conntrack_defrag_ops
);
510 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
515 ret
= nf_register_hook(&ipv6_conntrack_defrag_local_out_ops
);
517 printk("nf_conntrack_ipv6: can't register local_out defrag "
519 goto cleanup_defragops
;
522 ret
= nf_register_hook(&ipv6_conntrack_in_ops
);
524 printk("nf_conntrack_ipv6: can't register pre-routing hook.\n");
525 goto cleanup_defraglocalops
;
528 ret
= nf_register_hook(&ipv6_conntrack_local_out_ops
);
530 printk("nf_conntrack_ipv6: can't register local out hook.\n");
534 ret
= nf_register_hook(&ipv6_conntrack_out_ops
);
536 printk("nf_conntrack_ipv6: can't register post-routing hook.\n");
537 goto cleanup_inandlocalops
;
540 ret
= nf_register_hook(&ipv6_conntrack_local_in_ops
);
542 printk("nf_conntrack_ipv6: can't register local in hook.\n");
543 goto cleanup_inoutandlocalops
;
547 nf_ct_ipv6_sysctl_header
= register_sysctl_table(nf_ct_net_table
, 0);
548 if (nf_ct_ipv6_sysctl_header
== NULL
) {
549 printk("nf_conntrack: can't register to sysctl.\n");
551 goto cleanup_localinops
;
559 unregister_sysctl_table(nf_ct_ipv6_sysctl_header
);
562 nf_unregister_hook(&ipv6_conntrack_local_in_ops
);
563 cleanup_inoutandlocalops
:
564 nf_unregister_hook(&ipv6_conntrack_out_ops
);
565 cleanup_inandlocalops
:
566 nf_unregister_hook(&ipv6_conntrack_local_out_ops
);
568 nf_unregister_hook(&ipv6_conntrack_in_ops
);
569 cleanup_defraglocalops
:
570 nf_unregister_hook(&ipv6_conntrack_defrag_local_out_ops
);
572 nf_unregister_hook(&ipv6_conntrack_defrag_ops
);
574 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6
);
576 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmpv6
);
578 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp6
);
580 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp6
);
582 nf_ct_frag6_cleanup();
587 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6
));
588 MODULE_LICENSE("GPL");
589 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
591 static int __init
nf_conntrack_l3proto_ipv6_init(void)
594 return init_or_cleanup(1);
597 static void __exit
nf_conntrack_l3proto_ipv6_fini(void)
602 module_init(nf_conntrack_l3proto_ipv6_init
);
603 module_exit(nf_conntrack_l3proto_ipv6_fini
);