blob: 97bcf2bae8574a1a79d5410e18826f78e2ecc5dd [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/*
2 * Copyright (C)2004 USAGI/WIDE Project
3 *
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.
7 *
8 * Author:
9 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010 */
11
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080012#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>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080019#include <net/ipv6.h>
Pavel Emelyanov04128f22007-10-15 02:33:45 -070020#include <net/inet_frag.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080021
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010022#include <linux/netfilter_bridge.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023#include <linux/netfilter_ipv6.h>
Florian Westphal121d1e02012-10-30 01:08:49 +000024#include <linux/netfilter_ipv6/ip6_tables.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_helper.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010027#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028#include <net/netfilter/nf_conntrack_l3proto.h>
29#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010030#include <net/netfilter/nf_conntrack_zones.h>
Christoph Paasch9d2493f2009-03-16 15:15:35 +010031#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
Patrick McHardy58a317f2012-08-26 19:14:12 +020032#include <net/netfilter/nf_nat_helper.h>
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020033#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
Patrick McHardy74f7a652009-08-25 15:33:08 +020034#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035
Jan Engelhardt8ce84392008-04-14 11:15:52 +020036static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
37 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038{
Jan Engelhardt32948582008-01-31 04:53:24 -080039 const u_int32_t *ap;
40 u_int32_t _addrs[8];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041
42 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
43 sizeof(_addrs), _addrs);
44 if (ap == NULL)
Jan Engelhardt8ce84392008-04-14 11:15:52 +020045 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046
47 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
48 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
49
Jan Engelhardt8ce84392008-04-14 11:15:52 +020050 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051}
52
Jan Engelhardt8ce84392008-04-14 11:15:52 +020053static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
54 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055{
56 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
57 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
58
Jan Engelhardt8ce84392008-04-14 11:15:52 +020059 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060}
61
62static int ipv6_print_tuple(struct seq_file *s,
63 const struct nf_conntrack_tuple *tuple)
64{
Harvey Harrison5b095d9892008-10-29 12:52:50 -070065 return seq_printf(s, "src=%pI6 dst=%pI6 ",
Harvey Harrison0c6ce782008-10-28 16:09:23 -070066 tuple->src.u3.ip6, tuple->dst.u3.ip6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067}
68
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070069static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
70 unsigned int *dataoff, u_int8_t *protonum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071{
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070072 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
Patrick McHardy2b60af02012-08-26 19:13:59 +020073 __be16 frag_off;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070074 int protoff;
Patrick McHardy2b60af02012-08-26 19:13:59 +020075 u8 nexthdr;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070076
77 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
Patrick McHardy2b60af02012-08-26 19:13:59 +020078 &nexthdr, sizeof(nexthdr)) != 0) {
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070079 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
80 return -NF_ACCEPT;
81 }
Patrick McHardy2b60af02012-08-26 19:13:59 +020082 protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083 /*
Florent Fourcotd7a769f2012-12-14 00:53:33 +000084 * (protoff == skb->len) means the packet has not data, just
85 * IPv6 and possibly extensions headers, but it is tracked anyway
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 */
Patrick McHardy2b60af02012-08-26 19:13:59 +020087 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -070088 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089 return -NF_ACCEPT;
90 }
91
92 *dataoff = protoff;
Patrick McHardy2b60af02012-08-26 19:13:59 +020093 *protonum = nexthdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094 return NF_ACCEPT;
95}
96
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +020097static unsigned int ipv6_helper(unsigned int hooknum,
98 struct sk_buff *skb,
99 const struct net_device *in,
100 const struct net_device *out,
101 int (*okfn)(struct sk_buff *))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102{
103 struct nf_conn *ct;
Jan Engelhardt32948582008-01-31 04:53:24 -0800104 const struct nf_conn_help *help;
105 const struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106 enum ip_conntrack_info ctinfo;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200107 __be16 frag_off;
108 int protoff;
109 u8 nexthdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800110
111 /* This is where we call the helper: as the packet goes out. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700112 ct = nf_ct_get(skb, &ctinfo);
Eric Dumazetfb048832011-05-19 15:44:27 +0200113 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200114 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115
Harald Weltedc808fe2006-03-20 17:56:32 -0800116 help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700117 if (!help)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200118 return NF_ACCEPT;
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700119 /* rcu_read_lock()ed by nf_hook_slow */
120 helper = rcu_dereference(help->helper);
121 if (!helper)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200122 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200124 nexthdr = ipv6_hdr(skb)->nexthdr;
125 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
126 &frag_off);
127 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700128 pr_debug("proto header not found\n");
Harald Weltedc808fe2006-03-20 17:56:32 -0800129 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130 }
131
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100132 return helper->help(skb, protoff, ct, ctinfo);
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200133}
134
135static unsigned int ipv6_confirm(unsigned int hooknum,
136 struct sk_buff *skb,
137 const struct net_device *in,
138 const struct net_device *out,
139 int (*okfn)(struct sk_buff *))
140{
Patrick McHardy58a317f2012-08-26 19:14:12 +0200141 struct nf_conn *ct;
142 enum ip_conntrack_info ctinfo;
143 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
144 int protoff;
145 __be16 frag_off;
146
147 ct = nf_ct_get(skb, &ctinfo);
148 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
149 goto out;
150
151 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum,
152 &frag_off);
153 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
154 pr_debug("proto header not found\n");
155 goto out;
156 }
157
158 /* adjust seqs for loopback traffic only in outgoing direction */
159 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
160 !nf_is_loopback_packet(skb)) {
161 typeof(nf_nat_seq_adjust_hook) seq_adjust;
162
163 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
164 if (!seq_adjust ||
165 !seq_adjust(skb, ct, ctinfo, protoff)) {
166 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
167 return NF_DROP;
168 }
169 }
170out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171 /* We've seen it coming out the other side: confirm it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700172 return nf_conntrack_confirm(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173}
174
Alexey Dobriyana702a652008-10-08 11:35:04 +0200175static unsigned int __ipv6_conntrack_in(struct net *net,
176 unsigned int hooknum,
177 struct sk_buff *skb,
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200178 const struct net_device *in,
179 const struct net_device *out,
Alexey Dobriyana702a652008-10-08 11:35:04 +0200180 int (*okfn)(struct sk_buff *))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800181{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700182 struct sk_buff *reasm = skb->nfct_reasm;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200183 const struct nf_conn_help *help;
184 struct nf_conn *ct;
185 enum ip_conntrack_info ctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186
187 /* This packet is fragmented and has reassembled packet. */
188 if (reasm) {
189 /* Reassembled packet isn't parsed yet ? */
190 if (!reasm->nfct) {
191 unsigned int ret;
192
Alexey Dobriyana702a652008-10-08 11:35:04 +0200193 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194 if (ret != NF_ACCEPT)
195 return ret;
196 }
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200197
198 /* Conntrack helpers need the entire reassembled packet in the
Patrick McHardy58a317f2012-08-26 19:14:12 +0200199 * POST_ROUTING hook. In case of unconfirmed connections NAT
200 * might reassign a helper, so the entire packet is also
201 * required.
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200202 */
203 ct = nf_ct_get(reasm, &ctinfo);
204 if (ct != NULL && !nf_ct_is_untracked(ct)) {
205 help = nfct_help(ct);
Patrick McHardy58a317f2012-08-26 19:14:12 +0200206 if ((help && help->helper) || !nf_ct_is_confirmed(ct)) {
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200207 nf_conntrack_get_reasm(skb);
208 NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, reasm,
209 (struct net_device *)in,
210 (struct net_device *)out,
211 okfn, NF_IP6_PRI_CONNTRACK + 1);
212 return NF_DROP_ERR(-ECANCELED);
213 }
214 }
215
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800216 nf_conntrack_get(reasm->nfct);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700217 skb->nfct = reasm->nfct;
218 skb->nfctinfo = reasm->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219 return NF_ACCEPT;
220 }
221
Alexey Dobriyana702a652008-10-08 11:35:04 +0200222 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
223}
224
225static unsigned int ipv6_conntrack_in(unsigned int hooknum,
226 struct sk_buff *skb,
227 const struct net_device *in,
228 const struct net_device *out,
229 int (*okfn)(struct sk_buff *))
230{
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200231 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, in, out, okfn);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232}
233
234static unsigned int ipv6_conntrack_local(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700235 struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800236 const struct net_device *in,
237 const struct net_device *out,
238 int (*okfn)(struct sk_buff *))
239{
240 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700241 if (skb->len < sizeof(struct ipv6hdr)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000242 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800243 return NF_ACCEPT;
244 }
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200245 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, in, out, okfn);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800246}
247
Patrick McHardy19994142007-12-05 01:23:00 -0800248static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700249 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700250 .hook = ipv6_conntrack_in,
251 .owner = THIS_MODULE,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200252 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800253 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700254 .priority = NF_IP6_PRI_CONNTRACK,
255 },
256 {
257 .hook = ipv6_conntrack_local,
258 .owner = THIS_MODULE,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200259 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800260 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700261 .priority = NF_IP6_PRI_CONNTRACK,
262 },
263 {
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200264 .hook = ipv6_helper,
265 .owner = THIS_MODULE,
266 .pf = NFPROTO_IPV6,
267 .hooknum = NF_INET_POST_ROUTING,
268 .priority = NF_IP6_PRI_CONNTRACK_HELPER,
269 },
270 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700271 .hook = ipv6_confirm,
272 .owner = THIS_MODULE,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200273 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800274 .hooknum = NF_INET_POST_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700275 .priority = NF_IP6_PRI_LAST,
276 },
277 {
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200278 .hook = ipv6_helper,
279 .owner = THIS_MODULE,
280 .pf = NFPROTO_IPV6,
281 .hooknum = NF_INET_LOCAL_IN,
282 .priority = NF_IP6_PRI_CONNTRACK_HELPER,
283 },
284 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700285 .hook = ipv6_confirm,
286 .owner = THIS_MODULE,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200287 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800288 .hooknum = NF_INET_LOCAL_IN,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700289 .priority = NF_IP6_PRI_LAST-1,
290 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291};
292
Florian Westphal121d1e02012-10-30 01:08:49 +0000293static int
294ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
295{
296 const struct inet_sock *inet = inet_sk(sk);
297 const struct ipv6_pinfo *inet6 = inet6_sk(sk);
298 const struct nf_conntrack_tuple_hash *h;
299 struct sockaddr_in6 sin6;
300 struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
301 struct nf_conn *ct;
302
303 tuple.src.u3.in6 = inet6->rcv_saddr;
304 tuple.src.u.tcp.port = inet->inet_sport;
305 tuple.dst.u3.in6 = inet6->daddr;
306 tuple.dst.u.tcp.port = inet->inet_dport;
307 tuple.dst.protonum = sk->sk_protocol;
308
309 if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP)
310 return -ENOPROTOOPT;
311
312 if (*len < 0 || (unsigned int) *len < sizeof(sin6))
313 return -EINVAL;
314
315 h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple);
316 if (!h) {
317 pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n",
318 &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port),
319 &tuple.dst.u3.ip6, ntohs(tuple.dst.u.tcp.port));
320 return -ENOENT;
321 }
322
323 ct = nf_ct_tuplehash_to_ctrack(h);
324
325 sin6.sin6_family = AF_INET6;
326 sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
327 sin6.sin6_flowinfo = inet6->flow_label & IPV6_FLOWINFO_MASK;
328 memcpy(&sin6.sin6_addr,
329 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6,
330 sizeof(sin6.sin6_addr));
Florian Westphal121d1e02012-10-30 01:08:49 +0000331
332 nf_ct_put(ct);
Hannes Frederic Sowad00bd3d2013-03-12 11:21:36 +0000333 sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr,
334 sk->sk_bound_dev_if);
Florian Westphal121d1e02012-10-30 01:08:49 +0000335 return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0;
336}
337
Amerigo Wang07a93622012-10-29 16:23:10 +0000338#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800339
340#include <linux/netfilter/nfnetlink.h>
341#include <linux/netfilter/nfnetlink_conntrack.h>
342
Patrick McHardyfdf70832007-09-28 14:37:41 -0700343static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800344 const struct nf_conntrack_tuple *tuple)
345{
David S. Millere549a6b2012-04-01 20:28:52 -0400346 if (nla_put(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
347 &tuple->src.u3.ip6) ||
348 nla_put(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
349 &tuple->dst.u3.ip6))
350 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800351 return 0;
352
Patrick McHardydf6fb862007-09-28 14:37:03 -0700353nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800354 return -1;
355}
356
Patrick McHardyf73e9242007-09-28 14:39:55 -0700357static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
358 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
359 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800360};
361
Patrick McHardyfdf70832007-09-28 14:37:41 -0700362static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800363 struct nf_conntrack_tuple *t)
364{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700365 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800366 return -EINVAL;
367
Patrick McHardydf6fb862007-09-28 14:37:03 -0700368 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800369 sizeof(u_int32_t) * 4);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700370 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800371 sizeof(u_int32_t) * 4);
372
373 return 0;
374}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100375
376static int ipv6_nlattr_tuple_size(void)
377{
378 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
379}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800380#endif
381
Patrick McHardy61075af2007-07-14 20:48:19 -0700382struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800383 .l3proto = PF_INET6,
384 .name = "ipv6",
385 .pkt_to_tuple = ipv6_pkt_to_tuple,
386 .invert_tuple = ipv6_invert_tuple,
387 .print_tuple = ipv6_print_tuple,
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700388 .get_l4proto = ipv6_get_l4proto,
Amerigo Wang07a93622012-10-29 16:23:10 +0000389#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700390 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100391 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700392 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700393 .nla_policy = ipv6_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800394#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800395 .me = THIS_MODULE,
396};
397
Patrick McHardy32292a72006-04-06 14:11:30 -0700398MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
399MODULE_LICENSE("GPL");
400MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
401
Florian Westphal121d1e02012-10-30 01:08:49 +0000402static struct nf_sockopt_ops so_getorigdst6 = {
403 .pf = NFPROTO_IPV6,
404 .get_optmin = IP6T_SO_ORIGINAL_DST,
405 .get_optmax = IP6T_SO_ORIGINAL_DST + 1,
406 .get = ipv6_getorigdst,
407 .owner = THIS_MODULE,
408};
409
Gao fenga7c439d2012-05-28 21:04:17 +0000410static int ipv6_net_init(struct net *net)
411{
412 int ret = 0;
413
Gao fengc296bb42013-01-23 12:51:10 +0100414 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000415 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100416 pr_err("nf_conntrack_tcp6: pernet registration failed\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000417 goto out;
418 }
Gao fengc296bb42013-01-23 12:51:10 +0100419 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000420 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100421 pr_err("nf_conntrack_udp6: pernet registration failed\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000422 goto cleanup_tcp6;
423 }
Gao fengc296bb42013-01-23 12:51:10 +0100424 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmpv6);
Gao fenga7c439d2012-05-28 21:04:17 +0000425 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100426 pr_err("nf_conntrack_icmp6: pernet registration failed\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000427 goto cleanup_udp6;
428 }
Gao feng63307502013-01-21 22:10:33 +0000429 ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv6);
Gao fenga7c439d2012-05-28 21:04:17 +0000430 if (ret < 0) {
Gao feng63307502013-01-21 22:10:33 +0000431 pr_err("nf_conntrack_ipv6: pernet registration failed.\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000432 goto cleanup_icmpv6;
433 }
434 return 0;
435 cleanup_icmpv6:
Gao fengc296bb42013-01-23 12:51:10 +0100436 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
Gao fenga7c439d2012-05-28 21:04:17 +0000437 cleanup_udp6:
Gao fengc296bb42013-01-23 12:51:10 +0100438 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000439 cleanup_tcp6:
Gao fengc296bb42013-01-23 12:51:10 +0100440 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000441 out:
442 return ret;
443}
444
445static void ipv6_net_exit(struct net *net)
446{
Gao feng63307502013-01-21 22:10:33 +0000447 nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6);
Gao fengc296bb42013-01-23 12:51:10 +0100448 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
449 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
450 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000451}
452
453static struct pernet_operations ipv6_net_ops = {
454 .init = ipv6_net_init,
455 .exit = ipv6_net_exit,
456};
457
Patrick McHardy32292a72006-04-06 14:11:30 -0700458static int __init nf_conntrack_l3proto_ipv6_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459{
460 int ret = 0;
461
Patrick McHardy32292a72006-04-06 14:11:30 -0700462 need_conntrack();
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200463 nf_defrag_ipv6_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800464
Florian Westphal121d1e02012-10-30 01:08:49 +0000465 ret = nf_register_sockopt(&so_getorigdst6);
466 if (ret < 0) {
467 pr_err("Unable to register netfilter socket option\n");
468 return ret;
469 }
470
Gao fenga7c439d2012-05-28 21:04:17 +0000471 ret = register_pernet_subsys(&ipv6_net_ops);
472 if (ret < 0)
Gao feng63307502013-01-21 22:10:33 +0000473 goto cleanup_sockopt;
474
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700475 ret = nf_register_hooks(ipv6_conntrack_ops,
476 ARRAY_SIZE(ipv6_conntrack_ops));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 if (ret < 0) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200478 pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800479 "hook.\n");
Gao feng63307502013-01-21 22:10:33 +0000480 goto cleanup_pernet;
481 }
482
Gao fengc296bb42013-01-23 12:51:10 +0100483 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp6);
484 if (ret < 0) {
485 pr_err("nf_conntrack_ipv6: can't register tcp6 proto.\n");
486 goto cleanup_hooks;
487 }
488
489 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp6);
490 if (ret < 0) {
491 pr_err("nf_conntrack_ipv6: can't register udp6 proto.\n");
492 goto cleanup_tcp6;
493 }
494
495 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmpv6);
496 if (ret < 0) {
497 pr_err("nf_conntrack_ipv6: can't register icmpv6 proto.\n");
498 goto cleanup_udp6;
499 }
500
Gao feng63307502013-01-21 22:10:33 +0000501 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv6);
502 if (ret < 0) {
503 pr_err("nf_conntrack_ipv6: can't register ipv6 proto.\n");
Gao fengc296bb42013-01-23 12:51:10 +0100504 goto cleanup_icmpv6;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800506 return ret;
507
Gao fengc296bb42013-01-23 12:51:10 +0100508 cleanup_icmpv6:
509 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
510 cleanup_udp6:
511 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
512 cleanup_tcp6:
513 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
Gao feng63307502013-01-21 22:10:33 +0000514 cleanup_hooks:
515 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
Gao fenga7c439d2012-05-28 21:04:17 +0000516 cleanup_pernet:
Gao feng63307502013-01-21 22:10:33 +0000517 unregister_pernet_subsys(&ipv6_net_ops);
518 cleanup_sockopt:
Florian Westphal121d1e02012-10-30 01:08:49 +0000519 nf_unregister_sockopt(&so_getorigdst6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800520 return ret;
521}
522
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800523static void __exit nf_conntrack_l3proto_ipv6_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800524{
Patrick McHardy32292a72006-04-06 14:11:30 -0700525 synchronize_net();
Gao feng63307502013-01-21 22:10:33 +0000526 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
Gao fengc296bb42013-01-23 12:51:10 +0100527 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
528 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
529 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
Patrick McHardy32292a72006-04-06 14:11:30 -0700530 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
Gao fenga7c439d2012-05-28 21:04:17 +0000531 unregister_pernet_subsys(&ipv6_net_ops);
Florian Westphal121d1e02012-10-30 01:08:49 +0000532 nf_unregister_sockopt(&so_getorigdst6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800533}
534
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800535module_init(nf_conntrack_l3proto_ipv6_init);
536module_exit(nf_conntrack_l3proto_ipv6_fini);