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)));
64 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
66 * This function parses (probably truncated) exthdr set "hdr"
67 * of length "len". "nexthdrp" initially points to some place,
68 * where type of the first header can be found.
70 * It skips all well-known exthdrs, and returns pointer to the start
71 * of unparsable area i.e. the first header with unknown type.
72 * if success, *nexthdr is updated by type/protocol of this header.
74 * NOTES: - it may return pointer pointing beyond end of packet,
75 * if the last recognized header is truncated in the middle.
76 * - if packet is truncated, so that all parsed headers are skipped,
78 * - if packet is fragmented, return pointer of the fragment header.
79 * - ESP is unparsable for now and considered like
80 * normal payload protocol.
81 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
84 static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
85 u8 *nexthdrp, int len)
87 u8 nexthdr = *nexthdrp;
89 while (ipv6_ext_hdr(nexthdr)) {
90 struct ipv6_opt_hdr hdr;
93 if (len < (int)sizeof(struct ipv6_opt_hdr))
95 if (nexthdr == NEXTHDR_NONE)
97 if (nexthdr == NEXTHDR_FRAGMENT)
99 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
101 if (nexthdr == NEXTHDR_AUTH)
102 hdrlen = (hdr.hdrlen+2)<<2;
104 hdrlen = ipv6_optlen(&hdr);
106 nexthdr = hdr.nexthdr;
115 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
116 unsigned int *dataoff, u_int8_t *protonum)
118 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
122 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
123 &pnum, sizeof(pnum)) != 0) {
124 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
127 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
129 * (protoff == skb->len) mean that the packet doesn't have no data
130 * except of IPv6 & ext headers. but it's tracked anyway. - YK
132 if ((protoff < 0) || (protoff > skb->len)) {
133 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
142 static unsigned int ipv6_confirm(unsigned int hooknum,
144 const struct net_device *in,
145 const struct net_device *out,
146 int (*okfn)(struct sk_buff *))
149 struct nf_conn_help *help;
150 struct nf_conntrack_helper *helper;
151 enum ip_conntrack_info ctinfo;
152 unsigned int ret, protoff;
153 unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
154 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
157 /* This is where we call the helper: as the packet goes out. */
158 ct = nf_ct_get(skb, &ctinfo);
159 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
162 help = nfct_help(ct);
165 /* rcu_read_lock()ed by nf_hook_slow */
166 helper = rcu_dereference(help->helper);
170 protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
172 if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
173 pr_debug("proto header not found\n");
177 ret = helper->help(skb, protoff, ct, ctinfo);
178 if (ret != NF_ACCEPT)
181 /* We've seen it coming out the other side: confirm it */
182 return nf_conntrack_confirm(skb);
185 static unsigned int ipv6_defrag(unsigned int hooknum,
187 const struct net_device *in,
188 const struct net_device *out,
189 int (*okfn)(struct sk_buff *))
191 struct sk_buff *reasm;
193 /* Previously seen (loopback)? */
197 reasm = nf_ct_frag6_gather(skb);
203 /* error occured or not fragmented */
207 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
208 (struct net_device *)out, okfn);
213 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
215 const struct net_device *in,
216 const struct net_device *out,
217 int (*okfn)(struct sk_buff *))
219 struct sk_buff *reasm = skb->nfct_reasm;
221 /* This packet is fragmented and has reassembled packet. */
223 /* Reassembled packet isn't parsed yet ? */
227 ret = nf_conntrack_in(PF_INET6, hooknum, reasm);
228 if (ret != NF_ACCEPT)
231 nf_conntrack_get(reasm->nfct);
232 skb->nfct = reasm->nfct;
233 skb->nfctinfo = reasm->nfctinfo;
237 return nf_conntrack_in(PF_INET6, hooknum, skb);
240 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
242 const struct net_device *in,
243 const struct net_device *out,
244 int (*okfn)(struct sk_buff *))
246 /* root is playing with raw sockets. */
247 if (skb->len < sizeof(struct ipv6hdr)) {
249 printk("ipv6_conntrack_local: packet too short\n");
252 return ipv6_conntrack_in(hooknum, skb, in, out, okfn);
255 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
258 .owner = THIS_MODULE,
260 .hooknum = NF_INET_PRE_ROUTING,
261 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
264 .hook = ipv6_conntrack_in,
265 .owner = THIS_MODULE,
267 .hooknum = NF_INET_PRE_ROUTING,
268 .priority = NF_IP6_PRI_CONNTRACK,
271 .hook = ipv6_conntrack_local,
272 .owner = THIS_MODULE,
274 .hooknum = NF_INET_LOCAL_OUT,
275 .priority = NF_IP6_PRI_CONNTRACK,
279 .owner = THIS_MODULE,
281 .hooknum = NF_INET_LOCAL_OUT,
282 .priority = NF_IP6_PRI_CONNTRACK_DEFRAG,
285 .hook = ipv6_confirm,
286 .owner = THIS_MODULE,
288 .hooknum = NF_INET_POST_ROUTING,
289 .priority = NF_IP6_PRI_LAST,
292 .hook = ipv6_confirm,
293 .owner = THIS_MODULE,
295 .hooknum = NF_INET_LOCAL_IN,
296 .priority = NF_IP6_PRI_LAST-1,
300 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
302 #include <linux/netfilter/nfnetlink.h>
303 #include <linux/netfilter/nfnetlink_conntrack.h>
305 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
306 const struct nf_conntrack_tuple *tuple)
308 NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
310 NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
318 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
319 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
320 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
323 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
324 struct nf_conntrack_tuple *t)
326 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
329 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
330 sizeof(u_int32_t) * 4);
331 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
332 sizeof(u_int32_t) * 4);
338 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
341 .pkt_to_tuple = ipv6_pkt_to_tuple,
342 .invert_tuple = ipv6_invert_tuple,
343 .print_tuple = ipv6_print_tuple,
344 .get_l4proto = ipv6_get_l4proto,
345 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
346 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
347 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
348 .nla_policy = ipv6_nla_policy,
351 .ctl_table_path = nf_net_netfilter_sysctl_path,
352 .ctl_table = nf_ct_ipv6_sysctl_table,
357 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
361 static int __init nf_conntrack_l3proto_ipv6_init(void)
367 ret = nf_ct_frag6_init();
369 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
372 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
374 printk("nf_conntrack_ipv6: can't register tcp.\n");
378 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
380 printk("nf_conntrack_ipv6: can't register udp.\n");
384 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
386 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
390 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
392 printk("nf_conntrack_ipv6: can't register ipv6\n");
396 ret = nf_register_hooks(ipv6_conntrack_ops,
397 ARRAY_SIZE(ipv6_conntrack_ops));
399 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
406 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
408 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
410 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
412 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
414 nf_ct_frag6_cleanup();
418 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
421 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
422 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
423 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
424 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
425 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
426 nf_ct_frag6_cleanup();
429 module_init(nf_conntrack_l3proto_ipv6_init);
430 module_exit(nf_conntrack_l3proto_ipv6_fini);