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/nf_conntrack_zones.h>
31 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
32 #include <net/netfilter/nf_log.h>
34 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
35 struct nf_conntrack_tuple *tuple)
40 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
41 sizeof(_addrs), _addrs);
45 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
46 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
51 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
52 const struct nf_conntrack_tuple *orig)
54 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
55 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
60 static int ipv6_print_tuple(struct seq_file *s,
61 const struct nf_conntrack_tuple *tuple)
63 return seq_printf(s, "src=%pI6 dst=%pI6 ",
64 tuple->src.u3.ip6, tuple->dst.u3.ip6);
68 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
70 * This function parses (probably truncated) exthdr set "hdr"
71 * of length "len". "nexthdrp" initially points to some place,
72 * where type of the first header can be found.
74 * It skips all well-known exthdrs, and returns pointer to the start
75 * of unparsable area i.e. the first header with unknown type.
76 * if success, *nexthdr is updated by type/protocol of this header.
78 * NOTES: - it may return pointer pointing beyond end of packet,
79 * if the last recognized header is truncated in the middle.
80 * - if packet is truncated, so that all parsed headers are skipped,
82 * - if packet is fragmented, return pointer of the fragment header.
83 * - ESP is unparsable for now and considered like
84 * normal payload protocol.
85 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
88 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
89 u8 *nexthdrp, int len)
91 u8 nexthdr = *nexthdrp;
93 while (ipv6_ext_hdr(nexthdr)) {
94 struct ipv6_opt_hdr hdr;
97 if (len < (int)sizeof(struct ipv6_opt_hdr))
99 if (nexthdr == NEXTHDR_NONE)
101 if (nexthdr == NEXTHDR_FRAGMENT)
103 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
105 if (nexthdr == NEXTHDR_AUTH)
106 hdrlen = (hdr.hdrlen+2)<<2;
108 hdrlen = ipv6_optlen(&hdr);
110 nexthdr = hdr.nexthdr;
119 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
120 unsigned int *dataoff, u_int8_t *protonum)
122 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
126 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
127 &pnum, sizeof(pnum)) != 0) {
128 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
131 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
133 * (protoff == skb->len) mean that the packet doesn't have no data
134 * except of IPv6 & ext headers. but it's tracked anyway. - YK
136 if ((protoff < 0) || (protoff > skb->len)) {
137 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
146 static unsigned int ipv6_confirm(unsigned int hooknum,
148 const struct net_device *in,
149 const struct net_device *out,
150 int (*okfn)(struct sk_buff *))
153 const struct nf_conn_help *help;
154 const struct nf_conntrack_helper *helper;
155 enum ip_conntrack_info ctinfo;
156 unsigned int ret, protoff;
157 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
158 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
161 /* This is where we call the helper: as the packet goes out. */
162 ct = nf_ct_get(skb, &ctinfo);
163 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
166 help = nfct_help(ct);
169 /* rcu_read_lock()ed by nf_hook_slow */
170 helper = rcu_dereference(help->helper);
174 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
176 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
177 pr_debug("proto header not found\n");
181 ret = helper->help(skb, protoff, ct, ctinfo);
182 if (ret != NF_ACCEPT) {
183 nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
184 "nf_ct_%s: dropping packet", helper->name);
188 /* We've seen it coming out the other side: confirm it */
189 return nf_conntrack_confirm(skb);
192 static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
195 u16 zone = NF_CT_DEFAULT_ZONE;
198 zone = nf_ct_zone((struct nf_conn *)skb->nfct);
200 #ifdef CONFIG_BRIDGE_NETFILTER
201 if (skb->nf_bridge &&
202 skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)
203 return IP6_DEFRAG_CONNTRACK_BRIDGE_IN + zone;
205 if (hooknum == NF_INET_PRE_ROUTING)
206 return IP6_DEFRAG_CONNTRACK_IN + zone;
208 return IP6_DEFRAG_CONNTRACK_OUT + zone;
212 static unsigned int ipv6_defrag(unsigned int hooknum,
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)? */
221 if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
224 reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(hooknum, skb));
229 /* error occured or not fragmented */
233 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
234 (struct net_device *)out, okfn);
239 static unsigned int __ipv6_conntrack_in(struct net *net,
240 unsigned int hooknum,
242 int (*okfn)(struct sk_buff *))
244 struct sk_buff *reasm = skb->nfct_reasm;
246 /* This packet is fragmented and has reassembled packet. */
248 /* Reassembled packet isn't parsed yet ? */
252 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
253 if (ret != NF_ACCEPT)
256 nf_conntrack_get(reasm->nfct);
257 skb->nfct = reasm->nfct;
258 skb->nfctinfo = reasm->nfctinfo;
262 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
265 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
267 const struct net_device *in,
268 const struct net_device *out,
269 int (*okfn)(struct sk_buff *))
271 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, okfn);
274 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
276 const struct net_device *in,
277 const struct net_device *out,
278 int (*okfn)(struct sk_buff *))
280 /* root is playing with raw sockets. */
281 if (skb->len < sizeof(struct ipv6hdr)) {
283 pr_notice("ipv6_conntrack_local: packet too short\n");
286 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, okfn);
289 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
292 .owner = THIS_MODULE,
294 .hooknum = NF_INET_PRE_ROUTING,
295 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
298 .hook = ipv6_conntrack_in,
299 .owner = THIS_MODULE,
301 .hooknum = NF_INET_PRE_ROUTING,
302 .priority = NF_IP6_PRI_CONNTRACK,
305 .hook = ipv6_conntrack_local,
306 .owner = THIS_MODULE,
308 .hooknum = NF_INET_LOCAL_OUT,
309 .priority = NF_IP6_PRI_CONNTRACK,
313 .owner = THIS_MODULE,
315 .hooknum = NF_INET_LOCAL_OUT,
316 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
319 .hook = ipv6_confirm,
320 .owner = THIS_MODULE,
322 .hooknum = NF_INET_POST_ROUTING,
323 .priority = NF_IP6_PRI_LAST,
326 .hook = ipv6_confirm,
327 .owner = THIS_MODULE,
329 .hooknum = NF_INET_LOCAL_IN,
330 .priority = NF_IP6_PRI_LAST-1,
334 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
336 #include <linux/netfilter/nfnetlink.h>
337 #include <linux/netfilter/nfnetlink_conntrack.h>
339 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
340 const struct nf_conntrack_tuple *tuple)
342 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
344 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
352 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
353 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
354 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
357 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
358 struct nf_conntrack_tuple *t)
360 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
363 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
364 sizeof(u_int32_t) * 4);
365 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
366 sizeof(u_int32_t) * 4);
371 static int ipv6_nlattr_tuple_size(void)
373 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
377 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
380 .pkt_to_tuple = ipv6_pkt_to_tuple,
381 .invert_tuple = ipv6_invert_tuple,
382 .print_tuple = ipv6_print_tuple,
383 .get_l4proto = ipv6_get_l4proto,
384 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
385 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
386 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
387 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
388 .nla_policy = ipv6_nla_policy,
391 .ctl_table_path = nf_net_netfilter_sysctl_path,
392 .ctl_table = nf_ct_ipv6_sysctl_table,
397 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
398 MODULE_LICENSE("GPL");
399 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
401 static int __init nf_conntrack_l3proto_ipv6_init(void)
407 ret = nf_ct_frag6_init();
409 pr_err("nf_conntrack_ipv6: can't initialize frag6.\n");
412 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
414 pr_err("nf_conntrack_ipv6: can't register tcp.\n");
418 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
420 pr_err("nf_conntrack_ipv6: can't register udp.\n");
424 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
426 pr_err("nf_conntrack_ipv6: can't register icmpv6.\n");
430 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
432 pr_err("nf_conntrack_ipv6: can't register ipv6\n");
436 ret = nf_register_hooks(ipv6_conntrack_ops,
437 ARRAY_SIZE(ipv6_conntrack_ops));
439 pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
446 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
448 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
450 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
452 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
454 nf_ct_frag6_cleanup();
458 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
461 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
462 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
463 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
464 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
465 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
466 nf_ct_frag6_cleanup();
469 module_init(nf_conntrack_l3proto_ipv6_init);
470 module_exit(nf_conntrack_l3proto_ipv6_fini);