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>
12 #include <linux/types.h>
13 #include <linux/ipv6.h>
14 #include <linux/in6.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/icmp.h>
19 #include <linux/sysctl.h>
21 #include <net/inet_frag.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_l4proto.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_core.h>
30 static int ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
31 struct nf_conntrack_tuple *tuple)
33 u_int32_t _addrs[8], *ap;
35 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
36 sizeof(_addrs), _addrs);
40 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
41 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
46 static int ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
47 const struct nf_conntrack_tuple *orig)
49 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
50 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
55 static int ipv6_print_tuple(struct seq_file *s,
56 const struct nf_conntrack_tuple *tuple)
58 return seq_printf(s, "src=" NIP6_FMT " dst=" NIP6_FMT " ",
59 NIP6(*((struct in6_addr *)tuple->src.u3.ip6)),
60 NIP6(*((struct in6_addr *)tuple->dst.u3.ip6)));
63 static int ipv6_print_conntrack(struct seq_file *s,
64 const struct nf_conn *conntrack)
70 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
72 * This function parses (probably truncated) exthdr set "hdr"
73 * of length "len". "nexthdrp" initially points to some place,
74 * where type of the first header can be found.
76 * It skips all well-known exthdrs, and returns pointer to the start
77 * of unparsable area i.e. the first header with unknown type.
78 * if success, *nexthdr is updated by type/protocol of this header.
80 * NOTES: - it may return pointer pointing beyond end of packet,
81 * if the last recognized header is truncated in the middle.
82 * - if packet is truncated, so that all parsed headers are skipped,
84 * - if packet is fragmented, return pointer of the fragment header.
85 * - ESP is unparsable for now and considered like
86 * normal payload protocol.
87 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
90 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
91 u8 *nexthdrp, int len)
93 u8 nexthdr = *nexthdrp;
95 while (ipv6_ext_hdr(nexthdr)) {
96 struct ipv6_opt_hdr hdr;
99 if (len < (int)sizeof(struct ipv6_opt_hdr))
101 if (nexthdr == NEXTHDR_NONE)
103 if (nexthdr == NEXTHDR_FRAGMENT)
105 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
107 if (nexthdr == NEXTHDR_AUTH)
108 hdrlen = (hdr.hdrlen+2)<<2;
110 hdrlen = ipv6_optlen(&hdr);
112 nexthdr = hdr.nexthdr;
121 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
122 unsigned int *dataoff, u_int8_t *protonum)
124 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
128 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
129 &pnum, sizeof(pnum)) != 0) {
130 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
133 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
135 * (protoff == skb->len) mean that the packet doesn't have no data
136 * except of IPv6 & ext headers. but it's tracked anyway. - YK
138 if ((protoff < 0) || (protoff > skb->len)) {
139 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
148 static unsigned int ipv6_confirm(unsigned int hooknum,
150 const struct net_device *in,
151 const struct net_device *out,
152 int (*okfn)(struct sk_buff *))
155 struct nf_conn_help *help;
156 struct nf_conntrack_helper *helper;
157 enum ip_conntrack_info ctinfo;
158 unsigned int ret, protoff;
159 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
160 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
163 /* This is where we call the helper: as the packet goes out. */
164 ct = nf_ct_get(skb, &ctinfo);
165 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
168 help = nfct_help(ct);
171 /* rcu_read_lock()ed by nf_hook_slow */
172 helper = rcu_dereference(help->helper);
176 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
178 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
179 pr_debug("proto header not found\n");
183 ret = helper->help(skb, protoff, ct, ctinfo);
184 if (ret != NF_ACCEPT)
187 /* We've seen it coming out the other side: confirm it */
188 return nf_conntrack_confirm(skb);
191 static unsigned int ipv6_defrag(unsigned int hooknum,
193 const struct net_device *in,
194 const struct net_device *out,
195 int (*okfn)(struct sk_buff *))
197 struct sk_buff *reasm;
199 /* Previously seen (loopback)? */
203 reasm = nf_ct_frag6_gather(skb);
209 /* error occured or not fragmented */
213 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
214 (struct net_device *)out, okfn);
219 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
221 const struct net_device *in,
222 const struct net_device *out,
223 int (*okfn)(struct sk_buff *))
225 struct sk_buff *reasm = skb->nfct_reasm;
227 /* This packet is fragmented and has reassembled packet. */
229 /* Reassembled packet isn't parsed yet ? */
233 ret = nf_conntrack_in(PF_INET6, hooknum, reasm);
234 if (ret != NF_ACCEPT)
237 nf_conntrack_get(reasm->nfct);
238 skb->nfct = reasm->nfct;
239 skb->nfctinfo = reasm->nfctinfo;
243 return nf_conntrack_in(PF_INET6, hooknum, skb);
246 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
248 const struct net_device *in,
249 const struct net_device *out,
250 int (*okfn)(struct sk_buff *))
252 /* root is playing with raw sockets. */
253 if (skb->len < sizeof(struct ipv6hdr)) {
255 printk("ipv6_conntrack_local: packet too short\n");
258 return ipv6_conntrack_in(hooknum, skb, in, out, okfn);
261 static struct nf_hook_ops ipv6_conntrack_ops[] = {
264 .owner = THIS_MODULE,
266 .hooknum = NF_IP6_PRE_ROUTING,
267 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
270 .hook = ipv6_conntrack_in,
271 .owner = THIS_MODULE,
273 .hooknum = NF_IP6_PRE_ROUTING,
274 .priority = NF_IP6_PRI_CONNTRACK,
277 .hook = ipv6_conntrack_local,
278 .owner = THIS_MODULE,
280 .hooknum = NF_IP6_LOCAL_OUT,
281 .priority = NF_IP6_PRI_CONNTRACK,
285 .owner = THIS_MODULE,
287 .hooknum = NF_IP6_LOCAL_OUT,
288 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
291 .hook = ipv6_confirm,
292 .owner = THIS_MODULE,
294 .hooknum = NF_IP6_POST_ROUTING,
295 .priority = NF_IP6_PRI_LAST,
298 .hook = ipv6_confirm,
299 .owner = THIS_MODULE,
301 .hooknum = NF_IP6_LOCAL_IN,
302 .priority = NF_IP6_PRI_LAST-1,
307 static ctl_table nf_ct_ipv6_sysctl_table[] = {
309 .ctl_name = NET_NF_CONNTRACK_FRAG6_TIMEOUT,
310 .procname = "nf_conntrack_frag6_timeout",
311 .data = &nf_frags_ctl.timeout,
312 .maxlen = sizeof(unsigned int),
314 .proc_handler = &proc_dointvec_jiffies,
317 .ctl_name = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
318 .procname = "nf_conntrack_frag6_low_thresh",
319 .data = &nf_frags_ctl.low_thresh,
320 .maxlen = sizeof(unsigned int),
322 .proc_handler = &proc_dointvec,
325 .ctl_name = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
326 .procname = "nf_conntrack_frag6_high_thresh",
327 .data = &nf_frags_ctl.high_thresh,
328 .maxlen = sizeof(unsigned int),
330 .proc_handler = &proc_dointvec,
336 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
338 #include <linux/netfilter/nfnetlink.h>
339 #include <linux/netfilter/nfnetlink_conntrack.h>
341 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
342 const struct nf_conntrack_tuple *tuple)
344 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
346 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
354 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
355 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
356 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
359 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
360 struct nf_conntrack_tuple *t)
362 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
365 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
366 sizeof(u_int32_t) * 4);
367 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
368 sizeof(u_int32_t) * 4);
374 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
377 .pkt_to_tuple = ipv6_pkt_to_tuple,
378 .invert_tuple = ipv6_invert_tuple,
379 .print_tuple = ipv6_print_tuple,
380 .print_conntrack = ipv6_print_conntrack,
381 .get_l4proto = ipv6_get_l4proto,
382 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
383 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
384 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
385 .nla_policy = ipv6_nla_policy,
388 .ctl_table_path = nf_net_netfilter_sysctl_path,
389 .ctl_table = nf_ct_ipv6_sysctl_table,
394 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
395 MODULE_LICENSE("GPL");
396 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
398 static int __init nf_conntrack_l3proto_ipv6_init(void)
404 ret = nf_ct_frag6_init();
406 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
409 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
411 printk("nf_conntrack_ipv6: can't register tcp.\n");
415 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
417 printk("nf_conntrack_ipv6: can't register udp.\n");
421 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
423 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
427 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
429 printk("nf_conntrack_ipv6: can't register ipv6\n");
433 ret = nf_register_hooks(ipv6_conntrack_ops,
434 ARRAY_SIZE(ipv6_conntrack_ops));
436 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
443 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
445 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
447 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
449 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
451 nf_ct_frag6_cleanup();
455 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
458 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
459 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
460 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
461 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
462 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
463 nf_ct_frag6_cleanup();
466 module_init(nf_conntrack_l3proto_ipv6_init);
467 module_exit(nf_conntrack_l3proto_ipv6_fini);