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>
29 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
30 #include <net/netfilter/nf_log.h>
32 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
33 struct nf_conntrack_tuple *tuple)
38 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
39 sizeof(_addrs), _addrs);
43 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
44 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
49 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
50 const struct nf_conntrack_tuple *orig)
52 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
53 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
58 static int ipv6_print_tuple(struct seq_file *s,
59 const struct nf_conntrack_tuple *tuple)
61 return seq_printf(s, "src=%pI6 dst=%pI6 ",
62 tuple->src.u3.ip6, tuple->dst.u3.ip6);
66 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
68 * This function parses (probably truncated) exthdr set "hdr"
69 * of length "len". "nexthdrp" initially points to some place,
70 * where type of the first header can be found.
72 * It skips all well-known exthdrs, and returns pointer to the start
73 * of unparsable area i.e. the first header with unknown type.
74 * if success, *nexthdr is updated by type/protocol of this header.
76 * NOTES: - it may return pointer pointing beyond end of packet,
77 * if the last recognized header is truncated in the middle.
78 * - if packet is truncated, so that all parsed headers are skipped,
80 * - if packet is fragmented, return pointer of the fragment header.
81 * - ESP is unparsable for now and considered like
82 * normal payload protocol.
83 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
86 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
87 u8 *nexthdrp, int len)
89 u8 nexthdr = *nexthdrp;
91 while (ipv6_ext_hdr(nexthdr)) {
92 struct ipv6_opt_hdr hdr;
95 if (len < (int)sizeof(struct ipv6_opt_hdr))
97 if (nexthdr == NEXTHDR_NONE)
99 if (nexthdr == NEXTHDR_FRAGMENT)
101 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
103 if (nexthdr == NEXTHDR_AUTH)
104 hdrlen = (hdr.hdrlen+2)<<2;
106 hdrlen = ipv6_optlen(&hdr);
108 nexthdr = hdr.nexthdr;
117 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
118 unsigned int *dataoff, u_int8_t *protonum)
120 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
124 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
125 &pnum, sizeof(pnum)) != 0) {
126 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
129 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
131 * (protoff == skb->len) mean that the packet doesn't have no data
132 * except of IPv6 & ext headers. but it's tracked anyway. - YK
134 if ((protoff < 0) || (protoff > skb->len)) {
135 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
144 static unsigned int ipv6_confirm(unsigned int hooknum,
146 const struct net_device *in,
147 const struct net_device *out,
148 int (*okfn)(struct sk_buff *))
151 const struct nf_conn_help *help;
152 const struct nf_conntrack_helper *helper;
153 enum ip_conntrack_info ctinfo;
154 unsigned int ret, protoff;
155 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
156 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
159 /* This is where we call the helper: as the packet goes out. */
160 ct = nf_ct_get(skb, &ctinfo);
161 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
164 help = nfct_help(ct);
167 /* rcu_read_lock()ed by nf_hook_slow */
168 helper = rcu_dereference(help->helper);
172 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
174 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
175 pr_debug("proto header not found\n");
179 ret = helper->help(skb, protoff, ct, ctinfo);
180 if (ret != NF_ACCEPT) {
181 nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
182 "nf_ct_%s: dropping packet", helper->name);
186 /* We've seen it coming out the other side: confirm it */
187 return nf_conntrack_confirm(skb);
190 static unsigned int ipv6_defrag(unsigned int hooknum,
192 const struct net_device *in,
193 const struct net_device *out,
194 int (*okfn)(struct sk_buff *))
196 struct sk_buff *reasm;
198 /* Previously seen (loopback)? */
202 reasm = nf_ct_frag6_gather(skb);
208 /* error occured or not fragmented */
212 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
213 (struct net_device *)out, okfn);
218 static unsigned int __ipv6_conntrack_in(struct net *net,
219 unsigned int hooknum,
221 int (*okfn)(struct sk_buff *))
223 struct sk_buff *reasm = skb->nfct_reasm;
225 /* This packet is fragmented and has reassembled packet. */
227 /* Reassembled packet isn't parsed yet ? */
231 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
232 if (ret != NF_ACCEPT)
235 nf_conntrack_get(reasm->nfct);
236 skb->nfct = reasm->nfct;
237 skb->nfctinfo = reasm->nfctinfo;
241 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
244 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
246 const struct net_device *in,
247 const struct net_device *out,
248 int (*okfn)(struct sk_buff *))
250 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
253 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
255 const struct net_device *in,
256 const struct net_device *out,
257 int (*okfn)(struct sk_buff *))
259 /* root is playing with raw sockets. */
260 if (skb->len < sizeof(struct ipv6hdr)) {
262 printk("ipv6_conntrack_local: packet too short\n");
265 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
268 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
271 .owner = THIS_MODULE,
273 .hooknum = NF_INET_PRE_ROUTING,
274 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
277 .hook = ipv6_conntrack_in,
278 .owner = THIS_MODULE,
280 .hooknum = NF_INET_PRE_ROUTING,
281 .priority = NF_IP6_PRI_CONNTRACK,
284 .hook = ipv6_conntrack_local,
285 .owner = THIS_MODULE,
287 .hooknum = NF_INET_LOCAL_OUT,
288 .priority = NF_IP6_PRI_CONNTRACK,
292 .owner = THIS_MODULE,
294 .hooknum = NF_INET_LOCAL_OUT,
295 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
298 .hook = ipv6_confirm,
299 .owner = THIS_MODULE,
301 .hooknum = NF_INET_POST_ROUTING,
302 .priority = NF_IP6_PRI_LAST,
305 .hook = ipv6_confirm,
306 .owner = THIS_MODULE,
308 .hooknum = NF_INET_LOCAL_IN,
309 .priority = NF_IP6_PRI_LAST-1,
313 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
315 #include <linux/netfilter/nfnetlink.h>
316 #include <linux/netfilter/nfnetlink_conntrack.h>
318 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
319 const struct nf_conntrack_tuple *tuple)
321 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
323 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
331 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
332 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
333 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
336 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
337 struct nf_conntrack_tuple *t)
339 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
342 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
343 sizeof(u_int32_t) * 4);
344 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
345 sizeof(u_int32_t) * 4);
350 static int ipv6_nlattr_tuple_size(void)
352 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
356 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
359 .pkt_to_tuple = ipv6_pkt_to_tuple,
360 .invert_tuple = ipv6_invert_tuple,
361 .print_tuple = ipv6_print_tuple,
362 .get_l4proto = ipv6_get_l4proto,
363 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
364 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
365 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
366 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
367 .nla_policy = ipv6_nla_policy,
370 .ctl_table_path = nf_net_netfilter_sysctl_path,
371 .ctl_table = nf_ct_ipv6_sysctl_table,
376 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
377 MODULE_LICENSE("GPL");
378 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
380 static int __init nf_conntrack_l3proto_ipv6_init(void)
386 ret = nf_ct_frag6_init();
388 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
391 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
393 printk("nf_conntrack_ipv6: can't register tcp.\n");
397 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
399 printk("nf_conntrack_ipv6: can't register udp.\n");
403 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
405 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
409 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
411 printk("nf_conntrack_ipv6: can't register ipv6\n");
415 ret = nf_register_hooks(ipv6_conntrack_ops,
416 ARRAY_SIZE(ipv6_conntrack_ops));
418 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
425 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
427 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
429 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
431 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
433 nf_ct_frag6_cleanup();
437 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
440 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
441 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
442 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
443 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
444 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
445 nf_ct_frag6_cleanup();
448 module_init(nf_conntrack_l3proto_ipv6_init);
449 module_exit(nf_conntrack_l3proto_ipv6_fini);