blob: 963ee38486755487f3f4562a01091674de0fb279 [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>
Patrick McHardy41d73ec2013-08-27 08:50:12 +020031#include <net/netfilter/nf_conntrack_seqadj.h>
Christoph Paasch9d2493f2009-03-16 15:15:35 +010032#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
Patrick McHardy58a317f2012-08-26 19:14:12 +020033#include <net/netfilter/nf_nat_helper.h>
Balazs Scheidlere97c3e22010-10-21 16:03:43 +020034#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
Patrick McHardy74f7a652009-08-25 15:33:08 +020035#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036
Jan Engelhardt8ce84392008-04-14 11:15:52 +020037static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
38 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039{
Jan Engelhardt32948582008-01-31 04:53:24 -080040 const u_int32_t *ap;
41 u_int32_t _addrs[8];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042
43 ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
44 sizeof(_addrs), _addrs);
45 if (ap == NULL)
Jan Engelhardt8ce84392008-04-14 11:15:52 +020046 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047
48 memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
49 memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
50
Jan Engelhardt8ce84392008-04-14 11:15:52 +020051 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052}
53
Jan Engelhardt8ce84392008-04-14 11:15:52 +020054static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056{
57 memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
58 memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
59
Jan Engelhardt8ce84392008-04-14 11:15:52 +020060 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061}
62
Joe Perches824f1fb2014-09-29 16:08:22 -070063static void ipv6_print_tuple(struct seq_file *s,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064 const struct nf_conntrack_tuple *tuple)
65{
Joe Perches824f1fb2014-09-29 16:08:22 -070066 seq_printf(s, "src=%pI6 dst=%pI6 ",
67 tuple->src.u3.ip6, tuple->dst.u3.ip6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068}
69
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070070static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
71 unsigned int *dataoff, u_int8_t *protonum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080072{
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070073 unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
Patrick McHardy2b60af02012-08-26 19:13:59 +020074 __be16 frag_off;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070075 int protoff;
Patrick McHardy2b60af02012-08-26 19:13:59 +020076 u8 nexthdr;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070077
78 if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
Patrick McHardy2b60af02012-08-26 19:13:59 +020079 &nexthdr, sizeof(nexthdr)) != 0) {
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070080 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
81 return -NF_ACCEPT;
82 }
Patrick McHardy2b60af02012-08-26 19:13:59 +020083 protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080084 /*
Florent Fourcotd7a769f2012-12-14 00:53:33 +000085 * (protoff == skb->len) means the packet has not data, just
86 * IPv6 and possibly extensions headers, but it is tracked anyway
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080087 */
Patrick McHardy2b60af02012-08-26 19:13:59 +020088 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -070089 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080090 return -NF_ACCEPT;
91 }
92
93 *dataoff = protoff;
Patrick McHardy2b60af02012-08-26 19:13:59 +020094 *protonum = nexthdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 return NF_ACCEPT;
96}
97
Eric W. Biederman06198b32015-09-18 14:33:06 -050098static unsigned int ipv6_helper(void *priv,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +020099 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400100 const struct nf_hook_state *state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101{
102 struct nf_conn *ct;
Jan Engelhardt32948582008-01-31 04:53:24 -0800103 const struct nf_conn_help *help;
104 const struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800105 enum ip_conntrack_info ctinfo;
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200106 __be16 frag_off;
107 int protoff;
108 u8 nexthdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109
110 /* This is where we call the helper: as the packet goes out. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700111 ct = nf_ct_get(skb, &ctinfo);
Eric Dumazetfb048832011-05-19 15:44:27 +0200112 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200113 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800114
Harald Weltedc808fe2006-03-20 17:56:32 -0800115 help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700116 if (!help)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200117 return NF_ACCEPT;
Aaron Conolee2361cb2016-09-21 11:35:04 -0400118 /* rcu_read_lock()ed by nf_hook_thresh */
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700119 helper = rcu_dereference(help->helper);
120 if (!helper)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200121 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122
Patrick McHardy4cdd34082012-08-26 19:13:58 +0200123 nexthdr = ipv6_hdr(skb)->nexthdr;
124 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
125 &frag_off);
126 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700127 pr_debug("proto header not found\n");
Harald Weltedc808fe2006-03-20 17:56:32 -0800128 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800129 }
130
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100131 return helper->help(skb, protoff, ct, ctinfo);
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200132}
133
Eric W. Biederman06198b32015-09-18 14:33:06 -0500134static unsigned int ipv6_confirm(void *priv,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200135 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400136 const struct nf_hook_state *state)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200137{
Patrick McHardy58a317f2012-08-26 19:14:12 +0200138 struct nf_conn *ct;
139 enum ip_conntrack_info ctinfo;
140 unsigned char pnum = ipv6_hdr(skb)->nexthdr;
141 int protoff;
142 __be16 frag_off;
143
144 ct = nf_ct_get(skb, &ctinfo);
145 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
146 goto out;
147
148 protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum,
149 &frag_off);
150 if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
151 pr_debug("proto header not found\n");
152 goto out;
153 }
154
155 /* adjust seqs for loopback traffic only in outgoing direction */
156 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
157 !nf_is_loopback_packet(skb)) {
Patrick McHardy41d73ec2013-08-27 08:50:12 +0200158 if (!nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) {
Patrick McHardy58a317f2012-08-26 19:14:12 +0200159 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
160 return NF_DROP;
161 }
162 }
163out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164 /* We've seen it coming out the other side: confirm it */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700165 return nf_conntrack_confirm(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166}
167
Eric W. Biederman06198b32015-09-18 14:33:06 -0500168static unsigned int ipv6_conntrack_in(void *priv,
Alexey Dobriyana702a652008-10-08 11:35:04 +0200169 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400170 const struct nf_hook_state *state)
Alexey Dobriyana702a652008-10-08 11:35:04 +0200171{
Eric W. Biederman082a7582015-09-18 14:32:56 -0500172 return nf_conntrack_in(state->net, PF_INET6, state->hook, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173}
174
Eric W. Biederman06198b32015-09-18 14:33:06 -0500175static unsigned int ipv6_conntrack_local(void *priv,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700176 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400177 const struct nf_hook_state *state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178{
179 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700180 if (skb->len < sizeof(struct ipv6hdr)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000181 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182 return NF_ACCEPT;
183 }
Eric W. Biederman082a7582015-09-18 14:32:56 -0500184 return nf_conntrack_in(state->net, PF_INET6, state->hook, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185}
186
Patrick McHardy19994142007-12-05 01:23:00 -0800187static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700188 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700189 .hook = ipv6_conntrack_in,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200190 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800191 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700192 .priority = NF_IP6_PRI_CONNTRACK,
193 },
194 {
195 .hook = ipv6_conntrack_local,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200196 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800197 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700198 .priority = NF_IP6_PRI_CONNTRACK,
199 },
200 {
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200201 .hook = ipv6_helper,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200202 .pf = NFPROTO_IPV6,
203 .hooknum = NF_INET_POST_ROUTING,
204 .priority = NF_IP6_PRI_CONNTRACK_HELPER,
205 },
206 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700207 .hook = ipv6_confirm,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200208 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800209 .hooknum = NF_INET_POST_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700210 .priority = NF_IP6_PRI_LAST,
211 },
212 {
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200213 .hook = ipv6_helper,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200214 .pf = NFPROTO_IPV6,
215 .hooknum = NF_INET_LOCAL_IN,
216 .priority = NF_IP6_PRI_CONNTRACK_HELPER,
217 },
218 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700219 .hook = ipv6_confirm,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200220 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800221 .hooknum = NF_INET_LOCAL_IN,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700222 .priority = NF_IP6_PRI_LAST-1,
223 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224};
225
Florian Westphal121d1e02012-10-30 01:08:49 +0000226static int
227ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
228{
229 const struct inet_sock *inet = inet_sk(sk);
230 const struct ipv6_pinfo *inet6 = inet6_sk(sk);
231 const struct nf_conntrack_tuple_hash *h;
232 struct sockaddr_in6 sin6;
233 struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
234 struct nf_conn *ct;
235
Eric Dumazetefe42082013-10-03 15:42:29 -0700236 tuple.src.u3.in6 = sk->sk_v6_rcv_saddr;
Florian Westphal121d1e02012-10-30 01:08:49 +0000237 tuple.src.u.tcp.port = inet->inet_sport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700238 tuple.dst.u3.in6 = sk->sk_v6_daddr;
Florian Westphal121d1e02012-10-30 01:08:49 +0000239 tuple.dst.u.tcp.port = inet->inet_dport;
240 tuple.dst.protonum = sk->sk_protocol;
241
242 if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP)
243 return -ENOPROTOOPT;
244
245 if (*len < 0 || (unsigned int) *len < sizeof(sin6))
246 return -EINVAL;
247
Daniel Borkmann308ac912015-08-08 21:40:01 +0200248 h = nf_conntrack_find_get(sock_net(sk), &nf_ct_zone_dflt, &tuple);
Florian Westphal121d1e02012-10-30 01:08:49 +0000249 if (!h) {
250 pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n",
251 &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port),
252 &tuple.dst.u3.ip6, ntohs(tuple.dst.u.tcp.port));
253 return -ENOENT;
254 }
255
256 ct = nf_ct_tuplehash_to_ctrack(h);
257
258 sin6.sin6_family = AF_INET6;
259 sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
260 sin6.sin6_flowinfo = inet6->flow_label & IPV6_FLOWINFO_MASK;
261 memcpy(&sin6.sin6_addr,
262 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6,
263 sizeof(sin6.sin6_addr));
Florian Westphal121d1e02012-10-30 01:08:49 +0000264
265 nf_ct_put(ct);
Hannes Frederic Sowad00bd3d2013-03-12 11:21:36 +0000266 sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr,
267 sk->sk_bound_dev_if);
Florian Westphal121d1e02012-10-30 01:08:49 +0000268 return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0;
269}
270
Amerigo Wang07a93622012-10-29 16:23:10 +0000271#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800272
273#include <linux/netfilter/nfnetlink.h>
274#include <linux/netfilter/nfnetlink_conntrack.h>
275
Patrick McHardyfdf70832007-09-28 14:37:41 -0700276static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800277 const struct nf_conntrack_tuple *tuple)
278{
Jiri Benc930345e2015-03-29 16:59:25 +0200279 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
280 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
David S. Millere549a6b2012-04-01 20:28:52 -0400281 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800282 return 0;
283
Patrick McHardydf6fb862007-09-28 14:37:03 -0700284nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800285 return -1;
286}
287
Patrick McHardyf73e9242007-09-28 14:39:55 -0700288static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
289 [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
290 [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800291};
292
Patrick McHardyfdf70832007-09-28 14:37:41 -0700293static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800294 struct nf_conntrack_tuple *t)
295{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700296 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800297 return -EINVAL;
298
Jiri Benc67b61f62015-03-29 16:59:26 +0200299 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
300 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800301
302 return 0;
303}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100304
305static int ipv6_nlattr_tuple_size(void)
306{
307 return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
308}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800309#endif
310
Patrick McHardy61075af2007-07-14 20:48:19 -0700311struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312 .l3proto = PF_INET6,
313 .name = "ipv6",
314 .pkt_to_tuple = ipv6_pkt_to_tuple,
315 .invert_tuple = ipv6_invert_tuple,
316 .print_tuple = ipv6_print_tuple,
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700317 .get_l4proto = ipv6_get_l4proto,
Amerigo Wang07a93622012-10-29 16:23:10 +0000318#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700319 .tuple_to_nlattr = ipv6_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100320 .nlattr_tuple_size = ipv6_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700321 .nlattr_to_tuple = ipv6_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700322 .nla_policy = ipv6_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800323#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800324 .me = THIS_MODULE,
325};
326
Patrick McHardy32292a72006-04-06 14:11:30 -0700327MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
328MODULE_LICENSE("GPL");
329MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
330
Florian Westphal121d1e02012-10-30 01:08:49 +0000331static struct nf_sockopt_ops so_getorigdst6 = {
332 .pf = NFPROTO_IPV6,
333 .get_optmin = IP6T_SO_ORIGINAL_DST,
334 .get_optmax = IP6T_SO_ORIGINAL_DST + 1,
335 .get = ipv6_getorigdst,
336 .owner = THIS_MODULE,
337};
338
Gao fenga7c439d2012-05-28 21:04:17 +0000339static int ipv6_net_init(struct net *net)
340{
341 int ret = 0;
342
Gao fengc296bb42013-01-23 12:51:10 +0100343 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000344 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100345 pr_err("nf_conntrack_tcp6: pernet registration failed\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000346 goto out;
347 }
Gao fengc296bb42013-01-23 12:51:10 +0100348 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000349 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100350 pr_err("nf_conntrack_udp6: pernet registration failed\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000351 goto cleanup_tcp6;
352 }
Gao fengc296bb42013-01-23 12:51:10 +0100353 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmpv6);
Gao fenga7c439d2012-05-28 21:04:17 +0000354 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100355 pr_err("nf_conntrack_icmp6: pernet registration failed\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000356 goto cleanup_udp6;
357 }
Gao feng63307502013-01-21 22:10:33 +0000358 ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv6);
Gao fenga7c439d2012-05-28 21:04:17 +0000359 if (ret < 0) {
Gao feng63307502013-01-21 22:10:33 +0000360 pr_err("nf_conntrack_ipv6: pernet registration failed.\n");
Gao fenga7c439d2012-05-28 21:04:17 +0000361 goto cleanup_icmpv6;
362 }
363 return 0;
364 cleanup_icmpv6:
Gao fengc296bb42013-01-23 12:51:10 +0100365 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
Gao fenga7c439d2012-05-28 21:04:17 +0000366 cleanup_udp6:
Gao fengc296bb42013-01-23 12:51:10 +0100367 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000368 cleanup_tcp6:
Gao fengc296bb42013-01-23 12:51:10 +0100369 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000370 out:
371 return ret;
372}
373
374static void ipv6_net_exit(struct net *net)
375{
Gao feng63307502013-01-21 22:10:33 +0000376 nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6);
Gao fengc296bb42013-01-23 12:51:10 +0100377 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
378 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
379 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
Gao fenga7c439d2012-05-28 21:04:17 +0000380}
381
382static struct pernet_operations ipv6_net_ops = {
383 .init = ipv6_net_init,
384 .exit = ipv6_net_exit,
385};
386
Patrick McHardy32292a72006-04-06 14:11:30 -0700387static int __init nf_conntrack_l3proto_ipv6_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800388{
389 int ret = 0;
390
Patrick McHardy32292a72006-04-06 14:11:30 -0700391 need_conntrack();
Balazs Scheidlere97c3e22010-10-21 16:03:43 +0200392 nf_defrag_ipv6_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800393
Florian Westphal121d1e02012-10-30 01:08:49 +0000394 ret = nf_register_sockopt(&so_getorigdst6);
395 if (ret < 0) {
396 pr_err("Unable to register netfilter socket option\n");
397 return ret;
398 }
399
Gao fenga7c439d2012-05-28 21:04:17 +0000400 ret = register_pernet_subsys(&ipv6_net_ops);
401 if (ret < 0)
Gao feng63307502013-01-21 22:10:33 +0000402 goto cleanup_sockopt;
403
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700404 ret = nf_register_hooks(ipv6_conntrack_ops,
405 ARRAY_SIZE(ipv6_conntrack_ops));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406 if (ret < 0) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200407 pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800408 "hook.\n");
Gao feng63307502013-01-21 22:10:33 +0000409 goto cleanup_pernet;
410 }
411
Gao fengc296bb42013-01-23 12:51:10 +0100412 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp6);
413 if (ret < 0) {
414 pr_err("nf_conntrack_ipv6: can't register tcp6 proto.\n");
415 goto cleanup_hooks;
416 }
417
418 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp6);
419 if (ret < 0) {
420 pr_err("nf_conntrack_ipv6: can't register udp6 proto.\n");
421 goto cleanup_tcp6;
422 }
423
424 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmpv6);
425 if (ret < 0) {
426 pr_err("nf_conntrack_ipv6: can't register icmpv6 proto.\n");
427 goto cleanup_udp6;
428 }
429
Gao feng63307502013-01-21 22:10:33 +0000430 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv6);
431 if (ret < 0) {
432 pr_err("nf_conntrack_ipv6: can't register ipv6 proto.\n");
Gao fengc296bb42013-01-23 12:51:10 +0100433 goto cleanup_icmpv6;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800434 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435 return ret;
436
Gao fengc296bb42013-01-23 12:51:10 +0100437 cleanup_icmpv6:
438 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
439 cleanup_udp6:
440 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
441 cleanup_tcp6:
442 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
Gao feng63307502013-01-21 22:10:33 +0000443 cleanup_hooks:
444 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
Gao fenga7c439d2012-05-28 21:04:17 +0000445 cleanup_pernet:
Gao feng63307502013-01-21 22:10:33 +0000446 unregister_pernet_subsys(&ipv6_net_ops);
447 cleanup_sockopt:
Florian Westphal121d1e02012-10-30 01:08:49 +0000448 nf_unregister_sockopt(&so_getorigdst6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800449 return ret;
450}
451
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800452static void __exit nf_conntrack_l3proto_ipv6_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800453{
Patrick McHardy32292a72006-04-06 14:11:30 -0700454 synchronize_net();
Gao feng63307502013-01-21 22:10:33 +0000455 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
Gao fengc296bb42013-01-23 12:51:10 +0100456 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
457 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
458 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
Patrick McHardy32292a72006-04-06 14:11:30 -0700459 nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
Gao fenga7c439d2012-05-28 21:04:17 +0000460 unregister_pernet_subsys(&ipv6_net_ops);
Florian Westphal121d1e02012-10-30 01:08:49 +0000461 nf_unregister_sockopt(&so_getorigdst6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462}
463
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800464module_init(nf_conntrack_l3proto_ipv6_init);
465module_exit(nf_conntrack_l3proto_ipv6_fini);