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_bridge.h>
24 #include <linux/netfilter_ipv6.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_l3proto.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
31 #include <net/netfilter/nf_log.h>
33 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34 struct nf_conntrack_tuple *tuple)
39 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
40 sizeof(_addrs), _addrs);
44 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
45 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
50 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
51 const struct nf_conntrack_tuple *orig)
53 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
54 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
59 static int ipv6_print_tuple(struct seq_file *s,
60 const struct nf_conntrack_tuple *tuple)
62 return seq_printf(s, "src=%pI6 dst=%pI6 ",
63 tuple->src.u3.ip6, tuple->dst.u3.ip6);
67 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
69 * This function parses (probably truncated) exthdr set "hdr"
70 * of length "len". "nexthdrp" initially points to some place,
71 * where type of the first header can be found.
73 * It skips all well-known exthdrs, and returns pointer to the start
74 * of unparsable area i.e. the first header with unknown type.
75 * if success, *nexthdr is updated by type/protocol of this header.
77 * NOTES: - it may return pointer pointing beyond end of packet,
78 * if the last recognized header is truncated in the middle.
79 * - if packet is truncated, so that all parsed headers are skipped,
81 * - if packet is fragmented, return pointer of the fragment header.
82 * - ESP is unparsable for now and considered like
83 * normal payload protocol.
84 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
87 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
88 u8 *nexthdrp, int len)
90 u8 nexthdr = *nexthdrp;
92 while (ipv6_ext_hdr(nexthdr)) {
93 struct ipv6_opt_hdr hdr;
96 if (len < (int)sizeof(struct ipv6_opt_hdr))
98 if (nexthdr == NEXTHDR_NONE)
100 if (nexthdr == NEXTHDR_FRAGMENT)
102 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
104 if (nexthdr == NEXTHDR_AUTH)
105 hdrlen = (hdr.hdrlen+2)<<2;
107 hdrlen = ipv6_optlen(&hdr);
109 nexthdr = hdr.nexthdr;
118 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
119 unsigned int *dataoff, u_int8_t *protonum)
121 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
125 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
126 &pnum, sizeof(pnum)) != 0) {
127 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
130 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
132 * (protoff == skb->len) mean that the packet doesn't have no data
133 * except of IPv6 & ext headers. but it's tracked anyway. - YK
135 if ((protoff < 0) || (protoff > skb->len)) {
136 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
145 static unsigned int ipv6_confirm(unsigned int hooknum,
147 const struct net_device *in,
148 const struct net_device *out,
149 int (*okfn)(struct sk_buff *))
152 const struct nf_conn_help *help;
153 const struct nf_conntrack_helper *helper;
154 enum ip_conntrack_info ctinfo;
155 unsigned int ret, protoff;
156 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
157 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
160 /* This is where we call the helper: as the packet goes out. */
161 ct = nf_ct_get(skb, &ctinfo);
162 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
165 help = nfct_help(ct);
168 /* rcu_read_lock()ed by nf_hook_slow */
169 helper = rcu_dereference(help->helper);
173 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
175 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
176 pr_debug("proto header not found\n");
180 ret = helper->help(skb, protoff, ct, ctinfo);
181 if (ret != NF_ACCEPT) {
182 nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
183 "nf_ct_%s: dropping packet", helper->name);
187 /* We've seen it coming out the other side: confirm it */
188 return nf_conntrack_confirm(skb);
191 static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
194 #ifdef CONFIG_BRIDGE_NETFILTER
195 if (skb->nf_bridge &&
196 skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)
197 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN;
199 if (hooknum == NF_INET_PRE_ROUTING)
200 return IP6_DEFRAG_CONNTRACK_IN;
202 return IP6_DEFRAG_CONNTRACK_OUT;
206 static unsigned int ipv6_defrag(unsigned int hooknum,
208 const struct net_device *in,
209 const struct net_device *out,
210 int (*okfn)(struct sk_buff *))
212 struct sk_buff *reasm;
214 /* Previously seen (loopback)? */
218 reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(hooknum, skb));
223 /* error occured or not fragmented */
227 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
228 (struct net_device *)out, okfn);
233 static unsigned int __ipv6_conntrack_in(struct net *net,
234 unsigned int hooknum,
236 int (*okfn)(struct sk_buff *))
238 struct sk_buff *reasm = skb->nfct_reasm;
240 /* This packet is fragmented and has reassembled packet. */
242 /* Reassembled packet isn't parsed yet ? */
246 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
247 if (ret != NF_ACCEPT)
250 nf_conntrack_get(reasm->nfct);
251 skb->nfct = reasm->nfct;
252 skb->nfctinfo = reasm->nfctinfo;
256 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
259 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
261 const struct net_device *in,
262 const struct net_device *out,
263 int (*okfn)(struct sk_buff *))
265 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
268 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
270 const struct net_device *in,
271 const struct net_device *out,
272 int (*okfn)(struct sk_buff *))
274 /* root is playing with raw sockets. */
275 if (skb->len < sizeof(struct ipv6hdr)) {
277 printk("ipv6_conntrack_local: packet too short\n");
280 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
283 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
286 .owner = THIS_MODULE,
288 .hooknum = NF_INET_PRE_ROUTING,
289 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
292 .hook = ipv6_conntrack_in,
293 .owner = THIS_MODULE,
295 .hooknum = NF_INET_PRE_ROUTING,
296 .priority = NF_IP6_PRI_CONNTRACK,
299 .hook = ipv6_conntrack_local,
300 .owner = THIS_MODULE,
302 .hooknum = NF_INET_LOCAL_OUT,
303 .priority = NF_IP6_PRI_CONNTRACK,
307 .owner = THIS_MODULE,
309 .hooknum = NF_INET_LOCAL_OUT,
310 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
313 .hook = ipv6_confirm,
314 .owner = THIS_MODULE,
316 .hooknum = NF_INET_POST_ROUTING,
317 .priority = NF_IP6_PRI_LAST,
320 .hook = ipv6_confirm,
321 .owner = THIS_MODULE,
323 .hooknum = NF_INET_LOCAL_IN,
324 .priority = NF_IP6_PRI_LAST-1,
328 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
330 #include <linux/netfilter/nfnetlink.h>
331 #include <linux/netfilter/nfnetlink_conntrack.h>
333 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
334 const struct nf_conntrack_tuple *tuple)
336 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
338 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
346 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
347 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
348 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
351 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
352 struct nf_conntrack_tuple *t)
354 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
357 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
358 sizeof(u_int32_t) * 4);
359 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
360 sizeof(u_int32_t) * 4);
365 static int ipv6_nlattr_tuple_size(void)
367 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
371 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
374 .pkt_to_tuple = ipv6_pkt_to_tuple,
375 .invert_tuple = ipv6_invert_tuple,
376 .print_tuple = ipv6_print_tuple,
377 .get_l4proto = ipv6_get_l4proto,
378 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
379 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
380 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
381 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
382 .nla_policy = ipv6_nla_policy,
385 .ctl_table_path = nf_net_netfilter_sysctl_path,
386 .ctl_table = nf_ct_ipv6_sysctl_table,
391 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
392 MODULE_LICENSE("GPL");
393 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
395 static int __init nf_conntrack_l3proto_ipv6_init(void)
401 ret = nf_ct_frag6_init();
403 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
406 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
408 printk("nf_conntrack_ipv6: can't register tcp.\n");
412 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
414 printk("nf_conntrack_ipv6: can't register udp.\n");
418 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
420 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
424 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
426 printk("nf_conntrack_ipv6: can't register ipv6\n");
430 ret = nf_register_hooks(ipv6_conntrack_ops,
431 ARRAY_SIZE(ipv6_conntrack_ops));
433 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
440 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
442 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
444 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
446 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
448 nf_ct_frag6_cleanup();
452 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
455 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
456 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
457 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
458 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
459 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
460 nf_ct_frag6_cleanup();
463 module_init(nf_conntrack_l3proto_ipv6_init);
464 module_exit(nf_conntrack_l3proto_ipv6_fini);