• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4  * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
5  */
6 
7 #include <linux/types.h>
8 #include <linux/timer.h>
9 #include <linux/netfilter.h>
10 #include <linux/in.h>
11 #include <linux/icmp.h>
12 #include <linux/seq_file.h>
13 #include <net/ip.h>
14 #include <net/checksum.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <net/netfilter/nf_conntrack_tuple.h>
17 #include <net/netfilter/nf_conntrack_l4proto.h>
18 #include <net/netfilter/nf_conntrack_core.h>
19 #include <net/netfilter/nf_conntrack_timeout.h>
20 #include <net/netfilter/nf_conntrack_zones.h>
21 #include <net/netfilter/nf_log.h>
22 
23 #include "nf_internals.h"
24 
25 static const unsigned int nf_ct_icmp_timeout = 30*HZ;
26 
icmp_pkt_to_tuple(const struct sk_buff * skb,unsigned int dataoff,struct net * net,struct nf_conntrack_tuple * tuple)27 bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
28 		       struct net *net, struct nf_conntrack_tuple *tuple)
29 {
30 	const struct icmphdr *hp;
31 	struct icmphdr _hdr;
32 
33 	hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
34 	if (hp == NULL)
35 		return false;
36 
37 	tuple->dst.u.icmp.type = hp->type;
38 	tuple->src.u.icmp.id = hp->un.echo.id;
39 	tuple->dst.u.icmp.code = hp->code;
40 
41 	return true;
42 }
43 
44 /* Add 1; spaces filled with 0. */
45 static const u_int8_t invmap[] = {
46 	[ICMP_ECHO] = ICMP_ECHOREPLY + 1,
47 	[ICMP_ECHOREPLY] = ICMP_ECHO + 1,
48 	[ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
49 	[ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
50 	[ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
51 	[ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
52 	[ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
53 	[ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
54 };
55 
nf_conntrack_invert_icmp_tuple(struct nf_conntrack_tuple * tuple,const struct nf_conntrack_tuple * orig)56 bool nf_conntrack_invert_icmp_tuple(struct nf_conntrack_tuple *tuple,
57 				    const struct nf_conntrack_tuple *orig)
58 {
59 	if (orig->dst.u.icmp.type >= sizeof(invmap) ||
60 	    !invmap[orig->dst.u.icmp.type])
61 		return false;
62 
63 	tuple->src.u.icmp.id = orig->src.u.icmp.id;
64 	tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
65 	tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
66 	return true;
67 }
68 
69 /* Returns verdict for packet, or -1 for invalid. */
nf_conntrack_icmp_packet(struct nf_conn * ct,struct sk_buff * skb,enum ip_conntrack_info ctinfo,const struct nf_hook_state * state)70 int nf_conntrack_icmp_packet(struct nf_conn *ct,
71 			     struct sk_buff *skb,
72 			     enum ip_conntrack_info ctinfo,
73 			     const struct nf_hook_state *state)
74 {
75 	/* Do not immediately delete the connection after the first
76 	   successful reply to avoid excessive conntrackd traffic
77 	   and also to handle correctly ICMP echo reply duplicates. */
78 	unsigned int *timeout = nf_ct_timeout_lookup(ct);
79 	static const u_int8_t valid_new[] = {
80 		[ICMP_ECHO] = 1,
81 		[ICMP_TIMESTAMP] = 1,
82 		[ICMP_INFO_REQUEST] = 1,
83 		[ICMP_ADDRESS] = 1
84 	};
85 
86 	if (state->pf != NFPROTO_IPV4)
87 		return -NF_ACCEPT;
88 
89 	if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
90 	    !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
91 		/* Can't create a new ICMP `conn' with this. */
92 		pr_debug("icmp: can't create new conn with type %u\n",
93 			 ct->tuplehash[0].tuple.dst.u.icmp.type);
94 		nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
95 		return -NF_ACCEPT;
96 	}
97 
98 	if (!timeout)
99 		timeout = &nf_icmp_pernet(nf_ct_net(ct))->timeout;
100 
101 	nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
102 	return NF_ACCEPT;
103 }
104 
105 /* Check inner header is related to any of the existing connections */
nf_conntrack_inet_error(struct nf_conn * tmpl,struct sk_buff * skb,unsigned int dataoff,const struct nf_hook_state * state,u8 l4proto,union nf_inet_addr * outer_daddr)106 int nf_conntrack_inet_error(struct nf_conn *tmpl, struct sk_buff *skb,
107 			    unsigned int dataoff,
108 			    const struct nf_hook_state *state,
109 			    u8 l4proto, union nf_inet_addr *outer_daddr)
110 {
111 	struct nf_conntrack_tuple innertuple, origtuple;
112 	const struct nf_conntrack_tuple_hash *h;
113 	const struct nf_conntrack_zone *zone;
114 	enum ip_conntrack_info ctinfo;
115 	struct nf_conntrack_zone tmp;
116 	union nf_inet_addr *ct_daddr;
117 	enum ip_conntrack_dir dir;
118 	struct nf_conn *ct;
119 
120 	WARN_ON(skb_nfct(skb));
121 	zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
122 
123 	/* Are they talking about one of our connections? */
124 	if (!nf_ct_get_tuplepr(skb, dataoff,
125 			       state->pf, state->net, &origtuple))
126 		return -NF_ACCEPT;
127 
128 	/* Ordinarily, we'd expect the inverted tupleproto, but it's
129 	   been preserved inside the ICMP. */
130 	if (!nf_ct_invert_tuple(&innertuple, &origtuple))
131 		return -NF_ACCEPT;
132 
133 	h = nf_conntrack_find_get(state->net, zone, &innertuple);
134 	if (!h)
135 		return -NF_ACCEPT;
136 
137 	/* Consider: A -> T (=This machine) -> B
138 	 *   Conntrack entry will look like this:
139 	 *      Original:  A->B
140 	 *      Reply:     B->T (SNAT case) OR A
141 	 *
142 	 * When this function runs, we got packet that looks like this:
143 	 * iphdr|icmphdr|inner_iphdr|l4header (tcp, udp, ..).
144 	 *
145 	 * Above nf_conntrack_find_get() makes lookup based on inner_hdr,
146 	 * so we should expect that destination of the found connection
147 	 * matches outer header destination address.
148 	 *
149 	 * In above example, we can consider these two cases:
150 	 *  1. Error coming in reply direction from B or M (middle box) to
151 	 *     T (SNAT case) or A.
152 	 *     Inner saddr will be B, dst will be T or A.
153 	 *     The found conntrack will be reply tuple (B->T/A).
154 	 *  2. Error coming in original direction from A or M to B.
155 	 *     Inner saddr will be A, inner daddr will be B.
156 	 *     The found conntrack will be original tuple (A->B).
157 	 *
158 	 * In both cases, conntrack[dir].dst == inner.dst.
159 	 *
160 	 * A bogus packet could look like this:
161 	 *   Inner: B->T
162 	 *   Outer: B->X (other machine reachable by T).
163 	 *
164 	 * In this case, lookup yields connection A->B and will
165 	 * set packet from B->X as *RELATED*, even though no connection
166 	 * from X was ever seen.
167 	 */
168 	ct = nf_ct_tuplehash_to_ctrack(h);
169 	dir = NF_CT_DIRECTION(h);
170 	ct_daddr = &ct->tuplehash[dir].tuple.dst.u3;
171 	if (!nf_inet_addr_cmp(outer_daddr, ct_daddr)) {
172 		if (state->pf == AF_INET) {
173 			nf_l4proto_log_invalid(skb, state->net, state->pf,
174 					       l4proto,
175 					       "outer daddr %pI4 != inner %pI4",
176 					       &outer_daddr->ip, &ct_daddr->ip);
177 		} else if (state->pf == AF_INET6) {
178 			nf_l4proto_log_invalid(skb, state->net, state->pf,
179 					       l4proto,
180 					       "outer daddr %pI6 != inner %pI6",
181 					       &outer_daddr->ip6, &ct_daddr->ip6);
182 		}
183 		nf_ct_put(ct);
184 		return -NF_ACCEPT;
185 	}
186 
187 	ctinfo = IP_CT_RELATED;
188 	if (dir == IP_CT_DIR_REPLY)
189 		ctinfo += IP_CT_IS_REPLY;
190 
191 	/* Update skb to refer to this connection */
192 	nf_ct_set(skb, ct, ctinfo);
193 	return NF_ACCEPT;
194 }
195 
icmp_error_log(const struct sk_buff * skb,const struct nf_hook_state * state,const char * msg)196 static void icmp_error_log(const struct sk_buff *skb,
197 			   const struct nf_hook_state *state,
198 			   const char *msg)
199 {
200 	nf_l4proto_log_invalid(skb, state->net, state->pf,
201 			       IPPROTO_ICMP, "%s", msg);
202 }
203 
204 /* Small and modified version of icmp_rcv */
nf_conntrack_icmpv4_error(struct nf_conn * tmpl,struct sk_buff * skb,unsigned int dataoff,const struct nf_hook_state * state)205 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
206 			      struct sk_buff *skb, unsigned int dataoff,
207 			      const struct nf_hook_state *state)
208 {
209 	union nf_inet_addr outer_daddr;
210 	const struct icmphdr *icmph;
211 	struct icmphdr _ih;
212 
213 	/* Not enough header? */
214 	icmph = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
215 	if (icmph == NULL) {
216 		icmp_error_log(skb, state, "short packet");
217 		return -NF_ACCEPT;
218 	}
219 
220 	/* See nf_conntrack_proto_tcp.c */
221 	if (state->net->ct.sysctl_checksum &&
222 	    state->hook == NF_INET_PRE_ROUTING &&
223 	    nf_ip_checksum(skb, state->hook, dataoff, IPPROTO_ICMP)) {
224 		icmp_error_log(skb, state, "bad hw icmp checksum");
225 		return -NF_ACCEPT;
226 	}
227 
228 	/*
229 	 *	18 is the highest 'known' ICMP type. Anything else is a mystery
230 	 *
231 	 *	RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
232 	 *		  discarded.
233 	 */
234 	if (icmph->type > NR_ICMP_TYPES) {
235 		icmp_error_log(skb, state, "invalid icmp type");
236 		return -NF_ACCEPT;
237 	}
238 
239 	/* Need to track icmp error message? */
240 	if (!icmp_is_err(icmph->type))
241 		return NF_ACCEPT;
242 
243 	memset(&outer_daddr, 0, sizeof(outer_daddr));
244 	outer_daddr.ip = ip_hdr(skb)->daddr;
245 
246 	dataoff += sizeof(*icmph);
247 	return nf_conntrack_inet_error(tmpl, skb, dataoff, state,
248 				       IPPROTO_ICMP, &outer_daddr);
249 }
250 
251 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
252 
253 #include <linux/netfilter/nfnetlink.h>
254 #include <linux/netfilter/nfnetlink_conntrack.h>
255 
icmp_tuple_to_nlattr(struct sk_buff * skb,const struct nf_conntrack_tuple * t)256 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
257 				const struct nf_conntrack_tuple *t)
258 {
259 	if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
260 	    nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
261 	    nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
262 		goto nla_put_failure;
263 	return 0;
264 
265 nla_put_failure:
266 	return -1;
267 }
268 
269 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
270 	[CTA_PROTO_ICMP_TYPE]	= { .type = NLA_U8 },
271 	[CTA_PROTO_ICMP_CODE]	= { .type = NLA_U8 },
272 	[CTA_PROTO_ICMP_ID]	= { .type = NLA_U16 },
273 };
274 
icmp_nlattr_to_tuple(struct nlattr * tb[],struct nf_conntrack_tuple * tuple,u_int32_t flags)275 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
276 				struct nf_conntrack_tuple *tuple,
277 				u_int32_t flags)
278 {
279 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_TYPE)) {
280 		if (!tb[CTA_PROTO_ICMP_TYPE])
281 			return -EINVAL;
282 
283 		tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
284 		if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
285 		    !invmap[tuple->dst.u.icmp.type])
286 			return -EINVAL;
287 	}
288 
289 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_CODE)) {
290 		if (!tb[CTA_PROTO_ICMP_CODE])
291 			return -EINVAL;
292 
293 		tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
294 	}
295 
296 	if (flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_ID)) {
297 		if (!tb[CTA_PROTO_ICMP_ID])
298 			return -EINVAL;
299 
300 		tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
301 	}
302 
303 	return 0;
304 }
305 
icmp_nlattr_tuple_size(void)306 static unsigned int icmp_nlattr_tuple_size(void)
307 {
308 	static unsigned int size __read_mostly;
309 
310 	if (!size)
311 		size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
312 
313 	return size;
314 }
315 #endif
316 
317 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
318 
319 #include <linux/netfilter/nfnetlink.h>
320 #include <linux/netfilter/nfnetlink_cttimeout.h>
321 
icmp_timeout_nlattr_to_obj(struct nlattr * tb[],struct net * net,void * data)322 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
323 				      struct net *net, void *data)
324 {
325 	unsigned int *timeout = data;
326 	struct nf_icmp_net *in = nf_icmp_pernet(net);
327 
328 	if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
329 		if (!timeout)
330 			timeout = &in->timeout;
331 		*timeout =
332 			ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
333 	} else if (timeout) {
334 		/* Set default ICMP timeout. */
335 		*timeout = in->timeout;
336 	}
337 	return 0;
338 }
339 
340 static int
icmp_timeout_obj_to_nlattr(struct sk_buff * skb,const void * data)341 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
342 {
343 	const unsigned int *timeout = data;
344 
345 	if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
346 		goto nla_put_failure;
347 	return 0;
348 
349 nla_put_failure:
350 	return -ENOSPC;
351 }
352 
353 static const struct nla_policy
354 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
355 	[CTA_TIMEOUT_ICMP_TIMEOUT]	= { .type = NLA_U32 },
356 };
357 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
358 
nf_conntrack_icmp_init_net(struct net * net)359 void nf_conntrack_icmp_init_net(struct net *net)
360 {
361 	struct nf_icmp_net *in = nf_icmp_pernet(net);
362 
363 	in->timeout = nf_ct_icmp_timeout;
364 }
365 
366 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
367 {
368 	.l4proto		= IPPROTO_ICMP,
369 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
370 	.tuple_to_nlattr	= icmp_tuple_to_nlattr,
371 	.nlattr_tuple_size	= icmp_nlattr_tuple_size,
372 	.nlattr_to_tuple	= icmp_nlattr_to_tuple,
373 	.nla_policy		= icmp_nla_policy,
374 #endif
375 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
376 	.ctnl_timeout		= {
377 		.nlattr_to_obj	= icmp_timeout_nlattr_to_obj,
378 		.obj_to_nlattr	= icmp_timeout_obj_to_nlattr,
379 		.nlattr_max	= CTA_TIMEOUT_ICMP_MAX,
380 		.obj_size	= sizeof(unsigned int),
381 		.nla_policy	= icmp_timeout_nla_policy,
382 	},
383 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
384 };
385