1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C)2003,2004 USAGI/WIDE Project
4  *
5  * Author:
6  *	Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
7  */
8 
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/module.h>
12 #include <linux/netfilter.h>
13 #include <linux/in6.h>
14 #include <linux/icmpv6.h>
15 #include <linux/ipv6.h>
16 #include <net/ipv6.h>
17 #include <net/ip6_checksum.h>
18 #include <linux/seq_file.h>
19 #include <linux/netfilter_ipv6.h>
20 #include <net/netfilter/nf_conntrack_tuple.h>
21 #include <net/netfilter/nf_conntrack_l4proto.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_timeout.h>
24 #include <net/netfilter/nf_conntrack_zones.h>
25 #include <net/netfilter/nf_log.h>
26 
27 #include "nf_internals.h"
28 
29 static const unsigned int nf_ct_icmpv6_timeout = 30*HZ;
30 
icmpv6_pkt_to_tuple(const struct sk_buff * skb,unsigned int dataoff,struct net * net,struct nf_conntrack_tuple * tuple)31 bool icmpv6_pkt_to_tuple(const struct sk_buff *skb,
32 			 unsigned int dataoff,
33 			 struct net *net,
34 			 struct nf_conntrack_tuple *tuple)
35 {
36 	const struct icmp6hdr *hp;
37 	struct icmp6hdr _hdr;
38 
39 	hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
40 	if (hp == NULL)
41 		return false;
42 	tuple->dst.u.icmp.type = hp->icmp6_type;
43 	tuple->src.u.icmp.id = hp->icmp6_identifier;
44 	tuple->dst.u.icmp.code = hp->icmp6_code;
45 
46 	return true;
47 }
48 
49 /* Add 1; spaces filled with 0. */
50 static const u_int8_t invmap[] = {
51 	[ICMPV6_ECHO_REQUEST - 128]	= ICMPV6_ECHO_REPLY + 1,
52 	[ICMPV6_ECHO_REPLY - 128]	= ICMPV6_ECHO_REQUEST + 1,
53 	[ICMPV6_NI_QUERY - 128]		= ICMPV6_NI_REPLY + 1,
54 	[ICMPV6_NI_REPLY - 128]		= ICMPV6_NI_QUERY + 1
55 };
56 
57 static const u_int8_t noct_valid_new[] = {
58 	[ICMPV6_MGM_QUERY - 130] = 1,
59 	[ICMPV6_MGM_REPORT - 130] = 1,
60 	[ICMPV6_MGM_REDUCTION - 130] = 1,
61 	[NDISC_ROUTER_SOLICITATION - 130] = 1,
62 	[NDISC_ROUTER_ADVERTISEMENT - 130] = 1,
63 	[NDISC_NEIGHBOUR_SOLICITATION - 130] = 1,
64 	[NDISC_NEIGHBOUR_ADVERTISEMENT - 130] = 1,
65 	[ICMPV6_MLD2_REPORT - 130] = 1
66 };
67 
nf_conntrack_invert_icmpv6_tuple(struct nf_conntrack_tuple * tuple,const struct nf_conntrack_tuple * orig)68 bool nf_conntrack_invert_icmpv6_tuple(struct nf_conntrack_tuple *tuple,
69 				      const struct nf_conntrack_tuple *orig)
70 {
71 	int type = orig->dst.u.icmp.type - 128;
72 	if (type < 0 || type >= sizeof(invmap) || !invmap[type])
73 		return false;
74 
75 	tuple->src.u.icmp.id   = orig->src.u.icmp.id;
76 	tuple->dst.u.icmp.type = invmap[type] - 1;
77 	tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
78 	return true;
79 }
80 
icmpv6_get_timeouts(struct net * net)81 static unsigned int *icmpv6_get_timeouts(struct net *net)
82 {
83 	return &nf_icmpv6_pernet(net)->timeout;
84 }
85 
86 /* Returns verdict for packet, or -1 for invalid. */
nf_conntrack_icmpv6_packet(struct nf_conn * ct,struct sk_buff * skb,enum ip_conntrack_info ctinfo,const struct nf_hook_state * state)87 int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
88 			       struct sk_buff *skb,
89 			       enum ip_conntrack_info ctinfo,
90 			       const struct nf_hook_state *state)
91 {
92 	unsigned int *timeout = nf_ct_timeout_lookup(ct);
93 	static const u8 valid_new[] = {
94 		[ICMPV6_ECHO_REQUEST - 128] = 1,
95 		[ICMPV6_NI_QUERY - 128] = 1
96 	};
97 
98 	if (state->pf != NFPROTO_IPV6)
99 		return -NF_ACCEPT;
100 
101 	if (!nf_ct_is_confirmed(ct)) {
102 		int type = ct->tuplehash[0].tuple.dst.u.icmp.type - 128;
103 
104 		if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) {
105 			/* Can't create a new ICMPv6 `conn' with this. */
106 			pr_debug("icmpv6: can't create new conn with type %u\n",
107 				 type + 128);
108 			nf_ct_dump_tuple_ipv6(&ct->tuplehash[0].tuple);
109 			return -NF_ACCEPT;
110 		}
111 	}
112 
113 	if (!timeout)
114 		timeout = icmpv6_get_timeouts(nf_ct_net(ct));
115 
116 	/* Do not immediately delete the connection after the first
117 	   successful reply to avoid excessive conntrackd traffic
118 	   and also to handle correctly ICMP echo reply duplicates. */
119 	nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
120 
121 	return NF_ACCEPT;
122 }
123 
124 
icmpv6_error_log(const struct sk_buff * skb,const struct nf_hook_state * state,const char * msg)125 static void icmpv6_error_log(const struct sk_buff *skb,
126 			     const struct nf_hook_state *state,
127 			     const char *msg)
128 {
129 	nf_l4proto_log_invalid(skb, state, IPPROTO_ICMPV6, "%s", msg);
130 }
131 
nf_conntrack_icmpv6_error(struct nf_conn * tmpl,struct sk_buff * skb,unsigned int dataoff,const struct nf_hook_state * state)132 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
133 			      struct sk_buff *skb,
134 			      unsigned int dataoff,
135 			      const struct nf_hook_state *state)
136 {
137 	union nf_inet_addr outer_daddr;
138 	const struct icmp6hdr *icmp6h;
139 	struct icmp6hdr _ih;
140 	int type;
141 
142 	icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
143 	if (icmp6h == NULL) {
144 		icmpv6_error_log(skb, state, "short packet");
145 		return -NF_ACCEPT;
146 	}
147 
148 	if (state->hook == NF_INET_PRE_ROUTING &&
149 	    state->net->ct.sysctl_checksum &&
150 	    nf_ip6_checksum(skb, state->hook, dataoff, IPPROTO_ICMPV6)) {
151 		icmpv6_error_log(skb, state, "ICMPv6 checksum failed");
152 		return -NF_ACCEPT;
153 	}
154 
155 	type = icmp6h->icmp6_type - 130;
156 	if (type >= 0 && type < sizeof(noct_valid_new) &&
157 	    noct_valid_new[type]) {
158 		nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
159 		return NF_ACCEPT;
160 	}
161 
162 	/* is not error message ? */
163 	if (icmp6h->icmp6_type >= 128)
164 		return NF_ACCEPT;
165 
166 	memcpy(&outer_daddr.ip6, &ipv6_hdr(skb)->daddr,
167 	       sizeof(outer_daddr.ip6));
168 	dataoff += sizeof(*icmp6h);
169 	return nf_conntrack_inet_error(tmpl, skb, dataoff, state,
170 				       IPPROTO_ICMPV6, &outer_daddr);
171 }
172 
173 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
174 
175 #include <linux/netfilter/nfnetlink.h>
176 #include <linux/netfilter/nfnetlink_conntrack.h>
icmpv6_tuple_to_nlattr(struct sk_buff * skb,const struct nf_conntrack_tuple * t)177 static int icmpv6_tuple_to_nlattr(struct sk_buff *skb,
178 				  const struct nf_conntrack_tuple *t)
179 {
180 	if (nla_put_be16(skb, CTA_PROTO_ICMPV6_ID, t->src.u.icmp.id) ||
181 	    nla_put_u8(skb, CTA_PROTO_ICMPV6_TYPE, t->dst.u.icmp.type) ||
182 	    nla_put_u8(skb, CTA_PROTO_ICMPV6_CODE, t->dst.u.icmp.code))
183 		goto nla_put_failure;
184 	return 0;
185 
186 nla_put_failure:
187 	return -1;
188 }
189 
190 static const struct nla_policy icmpv6_nla_policy[CTA_PROTO_MAX+1] = {
191 	[CTA_PROTO_ICMPV6_TYPE]	= { .type = NLA_U8 },
192 	[CTA_PROTO_ICMPV6_CODE]	= { .type = NLA_U8 },
193 	[CTA_PROTO_ICMPV6_ID]	= { .type = NLA_U16 },
194 };
195 
icmpv6_nlattr_to_tuple(struct nlattr * tb[],struct nf_conntrack_tuple * tuple,u_int32_t flags)196 static int icmpv6_nlattr_to_tuple(struct nlattr *tb[],
197 				struct nf_conntrack_tuple *tuple,
198 				u_int32_t flags)
199 {
200 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_TYPE)) {
201 		if (!tb[CTA_PROTO_ICMPV6_TYPE])
202 			return -EINVAL;
203 
204 		tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]);
205 		if (tuple->dst.u.icmp.type < 128 ||
206 		    tuple->dst.u.icmp.type - 128 >= sizeof(invmap) ||
207 		    !invmap[tuple->dst.u.icmp.type - 128])
208 			return -EINVAL;
209 	}
210 
211 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_CODE)) {
212 		if (!tb[CTA_PROTO_ICMPV6_CODE])
213 			return -EINVAL;
214 
215 		tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]);
216 	}
217 
218 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_ID)) {
219 		if (!tb[CTA_PROTO_ICMPV6_ID])
220 			return -EINVAL;
221 
222 		tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMPV6_ID]);
223 	}
224 
225 	return 0;
226 }
227 
icmpv6_nlattr_tuple_size(void)228 static unsigned int icmpv6_nlattr_tuple_size(void)
229 {
230 	static unsigned int size __read_mostly;
231 
232 	if (!size)
233 		size = nla_policy_len(icmpv6_nla_policy, CTA_PROTO_MAX + 1);
234 
235 	return size;
236 }
237 #endif
238 
239 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
240 
241 #include <linux/netfilter/nfnetlink.h>
242 #include <linux/netfilter/nfnetlink_cttimeout.h>
243 
icmpv6_timeout_nlattr_to_obj(struct nlattr * tb[],struct net * net,void * data)244 static int icmpv6_timeout_nlattr_to_obj(struct nlattr *tb[],
245 					struct net *net, void *data)
246 {
247 	unsigned int *timeout = data;
248 	struct nf_icmp_net *in = nf_icmpv6_pernet(net);
249 
250 	if (!timeout)
251 		timeout = icmpv6_get_timeouts(net);
252 	if (tb[CTA_TIMEOUT_ICMPV6_TIMEOUT]) {
253 		*timeout =
254 		    ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMPV6_TIMEOUT])) * HZ;
255 	} else {
256 		/* Set default ICMPv6 timeout. */
257 		*timeout = in->timeout;
258 	}
259 	return 0;
260 }
261 
262 static int
icmpv6_timeout_obj_to_nlattr(struct sk_buff * skb,const void * data)263 icmpv6_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
264 {
265 	const unsigned int *timeout = data;
266 
267 	if (nla_put_be32(skb, CTA_TIMEOUT_ICMPV6_TIMEOUT, htonl(*timeout / HZ)))
268 		goto nla_put_failure;
269 	return 0;
270 
271 nla_put_failure:
272 	return -ENOSPC;
273 }
274 
275 static const struct nla_policy
276 icmpv6_timeout_nla_policy[CTA_TIMEOUT_ICMPV6_MAX+1] = {
277 	[CTA_TIMEOUT_ICMPV6_TIMEOUT]	= { .type = NLA_U32 },
278 };
279 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
280 
nf_conntrack_icmpv6_init_net(struct net * net)281 void nf_conntrack_icmpv6_init_net(struct net *net)
282 {
283 	struct nf_icmp_net *in = nf_icmpv6_pernet(net);
284 
285 	in->timeout = nf_ct_icmpv6_timeout;
286 }
287 
288 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 =
289 {
290 	.l4proto		= IPPROTO_ICMPV6,
291 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
292 	.tuple_to_nlattr	= icmpv6_tuple_to_nlattr,
293 	.nlattr_tuple_size	= icmpv6_nlattr_tuple_size,
294 	.nlattr_to_tuple	= icmpv6_nlattr_to_tuple,
295 	.nla_policy		= icmpv6_nla_policy,
296 #endif
297 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
298 	.ctnl_timeout		= {
299 		.nlattr_to_obj	= icmpv6_timeout_nlattr_to_obj,
300 		.obj_to_nlattr	= icmpv6_timeout_obj_to_nlattr,
301 		.nlattr_max	= CTA_TIMEOUT_ICMP_MAX,
302 		.obj_size	= sizeof(unsigned int),
303 		.nla_policy	= icmpv6_timeout_nla_policy,
304 	},
305 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
306 };
307