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_l4proto.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_core.h>
43 #define DEBUGP(format, args...)
46 static int ipv6_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
47 struct nf_conntrack_tuple
*tuple
)
49 u_int32_t _addrs
[8], *ap
;
51 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct ipv6hdr
, saddr
),
52 sizeof(_addrs
), _addrs
);
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
));
62 static int ipv6_invert_tuple(struct nf_conntrack_tuple
*tuple
,
63 const struct nf_conntrack_tuple
*orig
)
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
));
71 static int ipv6_print_tuple(struct seq_file
*s
,
72 const struct nf_conntrack_tuple
*tuple
)
74 return seq_printf(s
, "src=" NIP6_FMT
" dst=" NIP6_FMT
" ",
75 NIP6(*((struct in6_addr
*)tuple
->src
.u3
.ip6
)),
76 NIP6(*((struct in6_addr
*)tuple
->dst
.u3
.ip6
)));
79 static int ipv6_print_conntrack(struct seq_file
*s
,
80 const struct nf_conn
*conntrack
)
86 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
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.
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.
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,
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.
106 int nf_ct_ipv6_skip_exthdr(struct sk_buff
*skb
, int start
, u8
*nexthdrp
,
109 u8 nexthdr
= *nexthdrp
;
111 while (ipv6_ext_hdr(nexthdr
)) {
112 struct ipv6_opt_hdr hdr
;
115 if (len
< (int)sizeof(struct ipv6_opt_hdr
))
117 if (nexthdr
== NEXTHDR_NONE
)
119 if (nexthdr
== NEXTHDR_FRAGMENT
)
121 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
123 if (nexthdr
== NEXTHDR_AUTH
)
124 hdrlen
= (hdr
.hdrlen
+2)<<2;
126 hdrlen
= ipv6_optlen(&hdr
);
128 nexthdr
= hdr
.nexthdr
;
138 ipv6_prepare(struct sk_buff
**pskb
, unsigned int hooknum
, unsigned int *dataoff
,
145 extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1) - (*pskb
)->data
;
146 pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
148 protoff
= nf_ct_ipv6_skip_exthdr(*pskb
, extoff
, &pnum
,
149 (*pskb
)->len
- extoff
);
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
155 if ((protoff
< 0) || (protoff
> (*pskb
)->len
)) {
156 DEBUGP("ip6_conntrack_core: can't find proto in pkt\n");
157 NF_CT_STAT_INC_ATOMIC(error
);
158 NF_CT_STAT_INC_ATOMIC(invalid
);
167 static u_int32_t
ipv6_get_features(const struct nf_conntrack_tuple
*tuple
)
169 return NF_CT_F_BASIC
;
172 static 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
*))
179 struct nf_conn_help
*help
;
180 enum ip_conntrack_info ctinfo
;
181 unsigned int ret
, protoff
;
182 unsigned int extoff
= (u8
*)((*pskb
)->nh
.ipv6h
+ 1)
184 unsigned char pnum
= (*pskb
)->nh
.ipv6h
->nexthdr
;
187 /* This is where we call the helper: as the packet goes out. */
188 ct
= nf_ct_get(*pskb
, &ctinfo
);
189 if (!ct
|| ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
)
192 help
= nfct_help(ct
);
193 if (!help
|| !help
->helper
)
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");
204 ret
= help
->helper
->help(pskb
, protoff
, ct
, ctinfo
);
205 if (ret
!= NF_ACCEPT
)
208 /* We've seen it coming out the other side: confirm it */
209 return nf_conntrack_confirm(pskb
);
212 static 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
*))
218 struct sk_buff
*reasm
;
220 /* Previously seen (loopback)? */
224 reasm
= nf_ct_frag6_gather(*pskb
);
230 /* error occured or not fragmented */
234 nf_ct_frag6_output(hooknum
, reasm
, (struct net_device
*)in
,
235 (struct net_device
*)out
, okfn
);
240 static 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
*))
246 struct sk_buff
*reasm
= (*pskb
)->nfct_reasm
;
248 /* This packet is fragmented and has reassembled packet. */
250 /* Reassembled packet isn't parsed yet ? */
254 ret
= nf_conntrack_in(PF_INET6
, hooknum
, &reasm
);
255 if (ret
!= NF_ACCEPT
)
258 nf_conntrack_get(reasm
->nfct
);
259 (*pskb
)->nfct
= reasm
->nfct
;
263 return nf_conntrack_in(PF_INET6
, hooknum
, pskb
);
266 static 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
*))
272 /* root is playing with raw sockets. */
273 if ((*pskb
)->len
< sizeof(struct ipv6hdr
)) {
275 printk("ipv6_conntrack_local: packet too short\n");
278 return ipv6_conntrack_in(hooknum
, pskb
, in
, out
, okfn
);
281 static struct nf_hook_ops ipv6_conntrack_ops
[] = {
284 .owner
= THIS_MODULE
,
286 .hooknum
= NF_IP6_PRE_ROUTING
,
287 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
290 .hook
= ipv6_conntrack_in
,
291 .owner
= THIS_MODULE
,
293 .hooknum
= NF_IP6_PRE_ROUTING
,
294 .priority
= NF_IP6_PRI_CONNTRACK
,
297 .hook
= ipv6_conntrack_local
,
298 .owner
= THIS_MODULE
,
300 .hooknum
= NF_IP6_LOCAL_OUT
,
301 .priority
= NF_IP6_PRI_CONNTRACK
,
305 .owner
= THIS_MODULE
,
307 .hooknum
= NF_IP6_LOCAL_OUT
,
308 .priority
= NF_IP6_PRI_CONNTRACK_DEFRAG
,
311 .hook
= ipv6_confirm
,
312 .owner
= THIS_MODULE
,
314 .hooknum
= NF_IP6_POST_ROUTING
,
315 .priority
= NF_IP6_PRI_LAST
,
318 .hook
= ipv6_confirm
,
319 .owner
= THIS_MODULE
,
321 .hooknum
= NF_IP6_LOCAL_IN
,
322 .priority
= NF_IP6_PRI_LAST
-1,
327 static ctl_table nf_ct_ipv6_sysctl_table
[] = {
329 .ctl_name
= NET_NF_CONNTRACK_FRAG6_TIMEOUT
,
330 .procname
= "nf_conntrack_frag6_timeout",
331 .data
= &nf_ct_frag6_timeout
,
332 .maxlen
= sizeof(unsigned int),
334 .proc_handler
= &proc_dointvec_jiffies
,
337 .ctl_name
= NET_NF_CONNTRACK_FRAG6_LOW_THRESH
,
338 .procname
= "nf_conntrack_frag6_low_thresh",
339 .data
= &nf_ct_frag6_low_thresh
,
340 .maxlen
= sizeof(unsigned int),
342 .proc_handler
= &proc_dointvec
,
345 .ctl_name
= NET_NF_CONNTRACK_FRAG6_HIGH_THRESH
,
346 .procname
= "nf_conntrack_frag6_high_thresh",
347 .data
= &nf_ct_frag6_high_thresh
,
348 .maxlen
= sizeof(unsigned int),
350 .proc_handler
= &proc_dointvec
,
356 #if defined(CONFIG_NF_CT_NETLINK) || \
357 defined(CONFIG_NF_CT_NETLINK_MODULE)
359 #include <linux/netfilter/nfnetlink.h>
360 #include <linux/netfilter/nfnetlink_conntrack.h>
362 static int ipv6_tuple_to_nfattr(struct sk_buff
*skb
,
363 const struct nf_conntrack_tuple
*tuple
)
365 NFA_PUT(skb
, CTA_IP_V6_SRC
, sizeof(u_int32_t
) * 4,
367 NFA_PUT(skb
, CTA_IP_V6_DST
, sizeof(u_int32_t
) * 4,
375 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
376 [CTA_IP_V6_SRC
-1] = sizeof(u_int32_t
)*4,
377 [CTA_IP_V6_DST
-1] = sizeof(u_int32_t
)*4,
380 static int ipv6_nfattr_to_tuple(struct nfattr
*tb
[],
381 struct nf_conntrack_tuple
*t
)
383 if (!tb
[CTA_IP_V6_SRC
-1] || !tb
[CTA_IP_V6_DST
-1])
386 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
389 memcpy(&t
->src
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_SRC
-1]),
390 sizeof(u_int32_t
) * 4);
391 memcpy(&t
->dst
.u3
.ip6
, NFA_DATA(tb
[CTA_IP_V6_DST
-1]),
392 sizeof(u_int32_t
) * 4);
398 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6
= {
401 .pkt_to_tuple
= ipv6_pkt_to_tuple
,
402 .invert_tuple
= ipv6_invert_tuple
,
403 .print_tuple
= ipv6_print_tuple
,
404 .print_conntrack
= ipv6_print_conntrack
,
405 .prepare
= ipv6_prepare
,
406 #if defined(CONFIG_NF_CT_NETLINK) || \
407 defined(CONFIG_NF_CT_NETLINK_MODULE)
408 .tuple_to_nfattr
= ipv6_tuple_to_nfattr
,
409 .nfattr_to_tuple
= ipv6_nfattr_to_tuple
,
412 .ctl_table_path
= nf_net_netfilter_sysctl_path
,
413 .ctl_table
= nf_ct_ipv6_sysctl_table
,
415 .get_features
= ipv6_get_features
,
419 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6
));
420 MODULE_LICENSE("GPL");
421 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
423 static int __init
nf_conntrack_l3proto_ipv6_init(void)
429 ret
= nf_ct_frag6_init();
431 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
434 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6
);
436 printk("nf_conntrack_ipv6: can't register tcp.\n");
440 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6
);
442 printk("nf_conntrack_ipv6: can't register udp.\n");
446 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6
);
448 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
452 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6
);
454 printk("nf_conntrack_ipv6: can't register ipv6\n");
458 ret
= nf_register_hooks(ipv6_conntrack_ops
,
459 ARRAY_SIZE(ipv6_conntrack_ops
));
461 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
468 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6
);
470 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6
);
472 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6
);
474 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6
);
476 nf_ct_frag6_cleanup();
480 static void __exit
nf_conntrack_l3proto_ipv6_fini(void)
483 nf_unregister_hooks(ipv6_conntrack_ops
, ARRAY_SIZE(ipv6_conntrack_ops
));
484 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6
);
485 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6
);
486 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6
);
487 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6
);
488 nf_ct_frag6_cleanup();
491 module_init(nf_conntrack_l3proto_ipv6_init
);
492 module_exit(nf_conntrack_l3proto_ipv6_fini
);