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 long nf_ct_icmpv6_timeout;
340 /* From nf_conntrack_frag6.c */
341 extern unsigned long nf_ct_frag6_timeout;
342 extern unsigned long nf_ct_frag6_low_thresh;
343 extern unsigned long 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_jiffies,
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_jiffies,
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 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 = {
407 .pkt_to_tuple = ipv6_pkt_to_tuple,
408 .invert_tuple = ipv6_invert_tuple,
409 .print_tuple = ipv6_print_tuple,
410 .print_conntrack = ipv6_print_conntrack,
411 .prepare = ipv6_prepare,
412 .get_features = ipv6_get_features,
416 extern struct nf_conntrack_protocol nf_conntrack_protocol_tcp6;
417 extern struct nf_conntrack_protocol nf_conntrack_protocol_udp6;
418 extern struct nf_conntrack_protocol nf_conntrack_protocol_icmpv6;
419 extern int nf_ct_frag6_init(void);
420 extern void nf_ct_frag6_cleanup(void);
421 static int init_or_cleanup(int init)
425 if (!init) goto cleanup;
427 ret = nf_ct_frag6_init();
429 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
430 goto cleanup_nothing;
432 ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_tcp6);
434 printk("nf_conntrack_ipv6: can't register tcp.\n");
438 ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_udp6);
440 printk("nf_conntrack_ipv6: can't register udp.\n");
444 ret = nf_conntrack_protocol_register(&nf_conntrack_protocol_icmpv6);
446 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
450 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
452 printk("nf_conntrack_ipv6: can't register ipv6\n");
456 ret = nf_register_hook(&ipv6_conntrack_defrag_ops);
458 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
463 ret = nf_register_hook(&ipv6_conntrack_defrag_local_out_ops);
465 printk("nf_conntrack_ipv6: can't register local_out defrag "
467 goto cleanup_defragops;
470 ret = nf_register_hook(&ipv6_conntrack_in_ops);
472 printk("nf_conntrack_ipv6: can't register pre-routing hook.\n");
473 goto cleanup_defraglocalops;
476 ret = nf_register_hook(&ipv6_conntrack_local_out_ops);
478 printk("nf_conntrack_ipv6: can't register local out hook.\n");
482 ret = nf_register_hook(&ipv6_conntrack_out_ops);
484 printk("nf_conntrack_ipv6: can't register post-routing hook.\n");
485 goto cleanup_inandlocalops;
488 ret = nf_register_hook(&ipv6_conntrack_local_in_ops);
490 printk("nf_conntrack_ipv6: can't register local in hook.\n");
491 goto cleanup_inoutandlocalops;
495 nf_ct_ipv6_sysctl_header = register_sysctl_table(nf_ct_net_table, 0);
496 if (nf_ct_ipv6_sysctl_header == NULL) {
497 printk("nf_conntrack: can't register to sysctl.\n");
499 goto cleanup_localinops;
507 unregister_sysctl_table(nf_ct_ipv6_sysctl_header);
510 nf_unregister_hook(&ipv6_conntrack_local_in_ops);
511 cleanup_inoutandlocalops:
512 nf_unregister_hook(&ipv6_conntrack_out_ops);
513 cleanup_inandlocalops:
514 nf_unregister_hook(&ipv6_conntrack_local_out_ops);
516 nf_unregister_hook(&ipv6_conntrack_in_ops);
517 cleanup_defraglocalops:
518 nf_unregister_hook(&ipv6_conntrack_defrag_local_out_ops);
520 nf_unregister_hook(&ipv6_conntrack_defrag_ops);
522 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
524 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_icmpv6);
526 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_udp6);
528 nf_conntrack_protocol_unregister(&nf_conntrack_protocol_tcp6);
530 nf_ct_frag6_cleanup();
535 MODULE_LICENSE("GPL");
536 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
538 static int __init init(void)
541 return init_or_cleanup(1);
544 static void __exit fini(void)
552 void need_ip6_conntrack(void)
556 EXPORT_SYMBOL(need_ip6_conntrack);