xref: /openbmc/linux/net/ipv4/ping.c (revision e8254a8e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		"Ping" sockets
8  *
9  * Based on ipv4/udp.c code.
10  *
11  * Authors:	Vasiliy Kulikov / Openwall (for Linux 2.6),
12  *		Pavel Kankovsky (for Linux 2.4.32)
13  *
14  * Pavel gave all rights to bugs to Vasiliy,
15  * none of the bugs are Pavel's now.
16  */
17 
18 #include <linux/uaccess.h>
19 #include <linux/types.h>
20 #include <linux/fcntl.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
23 #include <linux/in.h>
24 #include <linux/errno.h>
25 #include <linux/timer.h>
26 #include <linux/mm.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <net/snmp.h>
30 #include <net/ip.h>
31 #include <net/icmp.h>
32 #include <net/protocol.h>
33 #include <linux/skbuff.h>
34 #include <linux/proc_fs.h>
35 #include <linux/export.h>
36 #include <linux/bpf-cgroup.h>
37 #include <net/sock.h>
38 #include <net/ping.h>
39 #include <net/udp.h>
40 #include <net/route.h>
41 #include <net/inet_common.h>
42 #include <net/checksum.h>
43 
44 #if IS_ENABLED(CONFIG_IPV6)
45 #include <linux/in6.h>
46 #include <linux/icmpv6.h>
47 #include <net/addrconf.h>
48 #include <net/ipv6.h>
49 #include <net/transp_v6.h>
50 #endif
51 
52 #define ping_portaddr_for_each_entry(__sk, node, list) \
53 	hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
54 #define ping_portaddr_for_each_entry_rcu(__sk, node, list) \
55 	hlist_nulls_for_each_entry_rcu(__sk, node, list, sk_nulls_node)
56 
57 struct ping_table {
58 	struct hlist_nulls_head	hash[PING_HTABLE_SIZE];
59 	spinlock_t		lock;
60 };
61 
62 static struct ping_table ping_table;
63 struct pingv6_ops pingv6_ops;
64 EXPORT_SYMBOL_GPL(pingv6_ops);
65 
66 static u16 ping_port_rover;
67 
68 static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)
69 {
70 	u32 res = (num + net_hash_mix(net)) & mask;
71 
72 	pr_debug("hash(%u) = %u\n", num, res);
73 	return res;
74 }
75 EXPORT_SYMBOL_GPL(ping_hash);
76 
77 static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
78 					     struct net *net, unsigned int num)
79 {
80 	return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
81 }
82 
83 int ping_get_port(struct sock *sk, unsigned short ident)
84 {
85 	struct hlist_nulls_node *node;
86 	struct hlist_nulls_head *hlist;
87 	struct inet_sock *isk, *isk2;
88 	struct sock *sk2 = NULL;
89 
90 	isk = inet_sk(sk);
91 	spin_lock(&ping_table.lock);
92 	if (ident == 0) {
93 		u32 i;
94 		u16 result = ping_port_rover + 1;
95 
96 		for (i = 0; i < (1L << 16); i++, result++) {
97 			if (!result)
98 				result++; /* avoid zero */
99 			hlist = ping_hashslot(&ping_table, sock_net(sk),
100 					    result);
101 			ping_portaddr_for_each_entry(sk2, node, hlist) {
102 				isk2 = inet_sk(sk2);
103 
104 				if (isk2->inet_num == result)
105 					goto next_port;
106 			}
107 
108 			/* found */
109 			ping_port_rover = ident = result;
110 			break;
111 next_port:
112 			;
113 		}
114 		if (i >= (1L << 16))
115 			goto fail;
116 	} else {
117 		hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
118 		ping_portaddr_for_each_entry(sk2, node, hlist) {
119 			isk2 = inet_sk(sk2);
120 
121 			/* BUG? Why is this reuse and not reuseaddr? ping.c
122 			 * doesn't turn off SO_REUSEADDR, and it doesn't expect
123 			 * that other ping processes can steal its packets.
124 			 */
125 			if ((isk2->inet_num == ident) &&
126 			    (sk2 != sk) &&
127 			    (!sk2->sk_reuse || !sk->sk_reuse))
128 				goto fail;
129 		}
130 	}
131 
132 	pr_debug("found port/ident = %d\n", ident);
133 	isk->inet_num = ident;
134 	if (sk_unhashed(sk)) {
135 		pr_debug("was not hashed\n");
136 		sock_hold(sk);
137 		sock_set_flag(sk, SOCK_RCU_FREE);
138 		hlist_nulls_add_head_rcu(&sk->sk_nulls_node, hlist);
139 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
140 	}
141 	spin_unlock(&ping_table.lock);
142 	return 0;
143 
144 fail:
145 	spin_unlock(&ping_table.lock);
146 	return -EADDRINUSE;
147 }
148 EXPORT_SYMBOL_GPL(ping_get_port);
149 
150 int ping_hash(struct sock *sk)
151 {
152 	pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
153 	BUG(); /* "Please do not press this button again." */
154 
155 	return 0;
156 }
157 
158 void ping_unhash(struct sock *sk)
159 {
160 	struct inet_sock *isk = inet_sk(sk);
161 
162 	pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
163 	spin_lock(&ping_table.lock);
164 	if (sk_hashed(sk)) {
165 		hlist_nulls_del_init_rcu(&sk->sk_nulls_node);
166 		sock_put(sk);
167 		isk->inet_num = 0;
168 		isk->inet_sport = 0;
169 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
170 	}
171 	spin_unlock(&ping_table.lock);
172 }
173 EXPORT_SYMBOL_GPL(ping_unhash);
174 
175 /* Called under rcu_read_lock() */
176 static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
177 {
178 	struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
179 	struct sock *sk = NULL;
180 	struct inet_sock *isk;
181 	struct hlist_nulls_node *hnode;
182 	int dif, sdif;
183 
184 	if (skb->protocol == htons(ETH_P_IP)) {
185 		dif = inet_iif(skb);
186 		sdif = inet_sdif(skb);
187 		pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
188 			 (int)ident, &ip_hdr(skb)->daddr, dif);
189 #if IS_ENABLED(CONFIG_IPV6)
190 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
191 		dif = inet6_iif(skb);
192 		sdif = inet6_sdif(skb);
193 		pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
194 			 (int)ident, &ipv6_hdr(skb)->daddr, dif);
195 #endif
196 	} else {
197 		return NULL;
198 	}
199 
200 	ping_portaddr_for_each_entry_rcu(sk, hnode, hslot) {
201 		isk = inet_sk(sk);
202 
203 		pr_debug("iterate\n");
204 		if (isk->inet_num != ident)
205 			continue;
206 
207 		if (skb->protocol == htons(ETH_P_IP) &&
208 		    sk->sk_family == AF_INET) {
209 			pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
210 				 (int) isk->inet_num, &isk->inet_rcv_saddr,
211 				 sk->sk_bound_dev_if);
212 
213 			if (isk->inet_rcv_saddr &&
214 			    isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
215 				continue;
216 #if IS_ENABLED(CONFIG_IPV6)
217 		} else if (skb->protocol == htons(ETH_P_IPV6) &&
218 			   sk->sk_family == AF_INET6) {
219 
220 			pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
221 				 (int) isk->inet_num,
222 				 &sk->sk_v6_rcv_saddr,
223 				 sk->sk_bound_dev_if);
224 
225 			if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
226 			    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr,
227 					     &ipv6_hdr(skb)->daddr))
228 				continue;
229 #endif
230 		} else {
231 			continue;
232 		}
233 
234 		if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif &&
235 		    sk->sk_bound_dev_if != sdif)
236 			continue;
237 
238 		goto exit;
239 	}
240 
241 	sk = NULL;
242 exit:
243 
244 	return sk;
245 }
246 
247 static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
248 					  kgid_t *high)
249 {
250 	kgid_t *data = net->ipv4.ping_group_range.range;
251 	unsigned int seq;
252 
253 	do {
254 		seq = read_seqbegin(&net->ipv4.ping_group_range.lock);
255 
256 		*low = data[0];
257 		*high = data[1];
258 	} while (read_seqretry(&net->ipv4.ping_group_range.lock, seq));
259 }
260 
261 
262 int ping_init_sock(struct sock *sk)
263 {
264 	struct net *net = sock_net(sk);
265 	kgid_t group = current_egid();
266 	struct group_info *group_info;
267 	int i;
268 	kgid_t low, high;
269 	int ret = 0;
270 
271 	if (sk->sk_family == AF_INET6)
272 		sk->sk_ipv6only = 1;
273 
274 	inet_get_ping_group_range_net(net, &low, &high);
275 	if (gid_lte(low, group) && gid_lte(group, high))
276 		return 0;
277 
278 	group_info = get_current_groups();
279 	for (i = 0; i < group_info->ngroups; i++) {
280 		kgid_t gid = group_info->gid[i];
281 
282 		if (gid_lte(low, gid) && gid_lte(gid, high))
283 			goto out_release_group;
284 	}
285 
286 	ret = -EACCES;
287 
288 out_release_group:
289 	put_group_info(group_info);
290 	return ret;
291 }
292 EXPORT_SYMBOL_GPL(ping_init_sock);
293 
294 void ping_close(struct sock *sk, long timeout)
295 {
296 	pr_debug("ping_close(sk=%p,sk->num=%u)\n",
297 		 inet_sk(sk), inet_sk(sk)->inet_num);
298 	pr_debug("isk->refcnt = %d\n", refcount_read(&sk->sk_refcnt));
299 
300 	sk_common_release(sk);
301 }
302 EXPORT_SYMBOL_GPL(ping_close);
303 
304 static int ping_pre_connect(struct sock *sk, struct sockaddr *uaddr,
305 			    int addr_len)
306 {
307 	/* This check is replicated from __ip4_datagram_connect() and
308 	 * intended to prevent BPF program called below from accessing bytes
309 	 * that are out of the bound specified by user in addr_len.
310 	 */
311 	if (addr_len < sizeof(struct sockaddr_in))
312 		return -EINVAL;
313 
314 	return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr);
315 }
316 
317 /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
318 static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
319 				struct sockaddr *uaddr, int addr_len)
320 {
321 	struct net *net = sock_net(sk);
322 	if (sk->sk_family == AF_INET) {
323 		struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
324 		u32 tb_id = RT_TABLE_LOCAL;
325 		int chk_addr_ret;
326 
327 		if (addr_len < sizeof(*addr))
328 			return -EINVAL;
329 
330 		if (addr->sin_family != AF_INET &&
331 		    !(addr->sin_family == AF_UNSPEC &&
332 		      addr->sin_addr.s_addr == htonl(INADDR_ANY)))
333 			return -EAFNOSUPPORT;
334 
335 		pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
336 			 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
337 
338 		if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
339 			return 0;
340 
341 		tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
342 		chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
343 
344 		if (chk_addr_ret == RTN_MULTICAST ||
345 		    chk_addr_ret == RTN_BROADCAST ||
346 		    (chk_addr_ret != RTN_LOCAL &&
347 		     !inet_can_nonlocal_bind(net, isk)))
348 			return -EADDRNOTAVAIL;
349 
350 #if IS_ENABLED(CONFIG_IPV6)
351 	} else if (sk->sk_family == AF_INET6) {
352 		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
353 		int addr_type, scoped, has_addr;
354 		struct net_device *dev = NULL;
355 
356 		if (addr_len < sizeof(*addr))
357 			return -EINVAL;
358 
359 		if (addr->sin6_family != AF_INET6)
360 			return -EAFNOSUPPORT;
361 
362 		pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
363 			 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
364 
365 		addr_type = ipv6_addr_type(&addr->sin6_addr);
366 		scoped = __ipv6_addr_needs_scope_id(addr_type);
367 		if ((addr_type != IPV6_ADDR_ANY &&
368 		     !(addr_type & IPV6_ADDR_UNICAST)) ||
369 		    (scoped && !addr->sin6_scope_id))
370 			return -EINVAL;
371 
372 		rcu_read_lock();
373 		if (addr->sin6_scope_id) {
374 			dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
375 			if (!dev) {
376 				rcu_read_unlock();
377 				return -ENODEV;
378 			}
379 		}
380 
381 		if (!dev && sk->sk_bound_dev_if) {
382 			dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
383 			if (!dev) {
384 				rcu_read_unlock();
385 				return -ENODEV;
386 			}
387 		}
388 		has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
389 						    scoped);
390 		rcu_read_unlock();
391 
392 		if (!(ipv6_can_nonlocal_bind(net, isk) || has_addr ||
393 		      addr_type == IPV6_ADDR_ANY))
394 			return -EADDRNOTAVAIL;
395 
396 		if (scoped)
397 			sk->sk_bound_dev_if = addr->sin6_scope_id;
398 #endif
399 	} else {
400 		return -EAFNOSUPPORT;
401 	}
402 	return 0;
403 }
404 
405 static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
406 {
407 	if (saddr->sa_family == AF_INET) {
408 		struct inet_sock *isk = inet_sk(sk);
409 		struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
410 		isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
411 #if IS_ENABLED(CONFIG_IPV6)
412 	} else if (saddr->sa_family == AF_INET6) {
413 		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
414 		struct ipv6_pinfo *np = inet6_sk(sk);
415 		sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
416 #endif
417 	}
418 }
419 
420 /*
421  * We need our own bind because there are no privileged id's == local ports.
422  * Moreover, we don't allow binding to multi- and broadcast addresses.
423  */
424 
425 int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
426 {
427 	struct inet_sock *isk = inet_sk(sk);
428 	unsigned short snum;
429 	int err;
430 	int dif = sk->sk_bound_dev_if;
431 
432 	err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
433 	if (err)
434 		return err;
435 
436 	lock_sock(sk);
437 
438 	err = -EINVAL;
439 	if (isk->inet_num != 0)
440 		goto out;
441 
442 	err = -EADDRINUSE;
443 	snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
444 	if (ping_get_port(sk, snum) != 0) {
445 		/* Restore possibly modified sk->sk_bound_dev_if by ping_check_bind_addr(). */
446 		sk->sk_bound_dev_if = dif;
447 		goto out;
448 	}
449 	ping_set_saddr(sk, uaddr);
450 
451 	pr_debug("after bind(): num = %hu, dif = %d\n",
452 		 isk->inet_num,
453 		 sk->sk_bound_dev_if);
454 
455 	err = 0;
456 	if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
457 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
458 #if IS_ENABLED(CONFIG_IPV6)
459 	if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
460 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
461 #endif
462 
463 	if (snum)
464 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
465 	isk->inet_sport = htons(isk->inet_num);
466 	isk->inet_daddr = 0;
467 	isk->inet_dport = 0;
468 
469 #if IS_ENABLED(CONFIG_IPV6)
470 	if (sk->sk_family == AF_INET6)
471 		memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
472 #endif
473 
474 	sk_dst_reset(sk);
475 out:
476 	release_sock(sk);
477 	pr_debug("ping_v4_bind -> %d\n", err);
478 	return err;
479 }
480 EXPORT_SYMBOL_GPL(ping_bind);
481 
482 /*
483  * Is this a supported type of ICMP message?
484  */
485 
486 static inline int ping_supported(int family, int type, int code)
487 {
488 	return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
489 	       (family == AF_INET && type == ICMP_EXT_ECHO && code == 0) ||
490 	       (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0) ||
491 	       (family == AF_INET6 && type == ICMPV6_EXT_ECHO_REQUEST && code == 0);
492 }
493 
494 /*
495  * This routine is called by the ICMP module when it gets some
496  * sort of error condition.
497  */
498 
499 void ping_err(struct sk_buff *skb, int offset, u32 info)
500 {
501 	int family;
502 	struct icmphdr *icmph;
503 	struct inet_sock *inet_sock;
504 	int type;
505 	int code;
506 	struct net *net = dev_net(skb->dev);
507 	struct sock *sk;
508 	int harderr;
509 	int err;
510 
511 	if (skb->protocol == htons(ETH_P_IP)) {
512 		family = AF_INET;
513 		type = icmp_hdr(skb)->type;
514 		code = icmp_hdr(skb)->code;
515 		icmph = (struct icmphdr *)(skb->data + offset);
516 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
517 		family = AF_INET6;
518 		type = icmp6_hdr(skb)->icmp6_type;
519 		code = icmp6_hdr(skb)->icmp6_code;
520 		icmph = (struct icmphdr *) (skb->data + offset);
521 	} else {
522 		BUG();
523 	}
524 
525 	/* We assume the packet has already been checked by icmp_unreach */
526 
527 	if (!ping_supported(family, icmph->type, icmph->code))
528 		return;
529 
530 	pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
531 		 skb->protocol, type, code, ntohs(icmph->un.echo.id),
532 		 ntohs(icmph->un.echo.sequence));
533 
534 	sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
535 	if (!sk) {
536 		pr_debug("no socket, dropping\n");
537 		return;	/* No socket for error */
538 	}
539 	pr_debug("err on socket %p\n", sk);
540 
541 	err = 0;
542 	harderr = 0;
543 	inet_sock = inet_sk(sk);
544 
545 	if (skb->protocol == htons(ETH_P_IP)) {
546 		switch (type) {
547 		default:
548 		case ICMP_TIME_EXCEEDED:
549 			err = EHOSTUNREACH;
550 			break;
551 		case ICMP_SOURCE_QUENCH:
552 			/* This is not a real error but ping wants to see it.
553 			 * Report it with some fake errno.
554 			 */
555 			err = EREMOTEIO;
556 			break;
557 		case ICMP_PARAMETERPROB:
558 			err = EPROTO;
559 			harderr = 1;
560 			break;
561 		case ICMP_DEST_UNREACH:
562 			if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
563 				ipv4_sk_update_pmtu(skb, sk, info);
564 				if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
565 					err = EMSGSIZE;
566 					harderr = 1;
567 					break;
568 				}
569 				goto out;
570 			}
571 			err = EHOSTUNREACH;
572 			if (code <= NR_ICMP_UNREACH) {
573 				harderr = icmp_err_convert[code].fatal;
574 				err = icmp_err_convert[code].errno;
575 			}
576 			break;
577 		case ICMP_REDIRECT:
578 			/* See ICMP_SOURCE_QUENCH */
579 			ipv4_sk_redirect(skb, sk);
580 			err = EREMOTEIO;
581 			break;
582 		}
583 #if IS_ENABLED(CONFIG_IPV6)
584 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
585 		harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
586 #endif
587 	}
588 
589 	/*
590 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
591 	 *	4.1.3.3.
592 	 */
593 	if ((family == AF_INET && !inet_sock->recverr) ||
594 	    (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
595 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
596 			goto out;
597 	} else {
598 		if (family == AF_INET) {
599 			ip_icmp_error(sk, skb, err, 0 /* no remote port */,
600 				      info, (u8 *)icmph);
601 #if IS_ENABLED(CONFIG_IPV6)
602 		} else if (family == AF_INET6) {
603 			pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
604 						   info, (u8 *)icmph);
605 #endif
606 		}
607 	}
608 	sk->sk_err = err;
609 	sk_error_report(sk);
610 out:
611 	return;
612 }
613 EXPORT_SYMBOL_GPL(ping_err);
614 
615 /*
616  *	Copy and checksum an ICMP Echo packet from user space into a buffer
617  *	starting from the payload.
618  */
619 
620 int ping_getfrag(void *from, char *to,
621 		 int offset, int fraglen, int odd, struct sk_buff *skb)
622 {
623 	struct pingfakehdr *pfh = from;
624 
625 	if (!csum_and_copy_from_iter_full(to, fraglen, &pfh->wcheck,
626 					  &pfh->msg->msg_iter))
627 		return -EFAULT;
628 
629 #if IS_ENABLED(CONFIG_IPV6)
630 	/* For IPv6, checksum each skb as we go along, as expected by
631 	 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
632 	 * wcheck, it will be finalized in ping_v4_push_pending_frames.
633 	 */
634 	if (pfh->family == AF_INET6) {
635 		skb->csum = csum_block_add(skb->csum, pfh->wcheck, odd);
636 		skb->ip_summed = CHECKSUM_NONE;
637 		pfh->wcheck = 0;
638 	}
639 #endif
640 
641 	return 0;
642 }
643 EXPORT_SYMBOL_GPL(ping_getfrag);
644 
645 static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
646 				       struct flowi4 *fl4)
647 {
648 	struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
649 
650 	if (!skb)
651 		return 0;
652 	pfh->wcheck = csum_partial((char *)&pfh->icmph,
653 		sizeof(struct icmphdr), pfh->wcheck);
654 	pfh->icmph.checksum = csum_fold(pfh->wcheck);
655 	memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
656 	skb->ip_summed = CHECKSUM_NONE;
657 	return ip_push_pending_frames(sk, fl4);
658 }
659 
660 int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
661 			void *user_icmph, size_t icmph_len)
662 {
663 	u8 type, code;
664 
665 	if (len > 0xFFFF)
666 		return -EMSGSIZE;
667 
668 	/* Must have at least a full ICMP header. */
669 	if (len < icmph_len)
670 		return -EINVAL;
671 
672 	/*
673 	 *	Check the flags.
674 	 */
675 
676 	/* Mirror BSD error message compatibility */
677 	if (msg->msg_flags & MSG_OOB)
678 		return -EOPNOTSUPP;
679 
680 	/*
681 	 *	Fetch the ICMP header provided by the userland.
682 	 *	iovec is modified! The ICMP header is consumed.
683 	 */
684 	if (memcpy_from_msg(user_icmph, msg, icmph_len))
685 		return -EFAULT;
686 
687 	if (family == AF_INET) {
688 		type = ((struct icmphdr *) user_icmph)->type;
689 		code = ((struct icmphdr *) user_icmph)->code;
690 #if IS_ENABLED(CONFIG_IPV6)
691 	} else if (family == AF_INET6) {
692 		type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
693 		code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
694 #endif
695 	} else {
696 		BUG();
697 	}
698 
699 	if (!ping_supported(family, type, code))
700 		return -EINVAL;
701 
702 	return 0;
703 }
704 EXPORT_SYMBOL_GPL(ping_common_sendmsg);
705 
706 static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
707 {
708 	struct net *net = sock_net(sk);
709 	struct flowi4 fl4;
710 	struct inet_sock *inet = inet_sk(sk);
711 	struct ipcm_cookie ipc;
712 	struct icmphdr user_icmph;
713 	struct pingfakehdr pfh;
714 	struct rtable *rt = NULL;
715 	struct ip_options_data opt_copy;
716 	int free = 0;
717 	__be32 saddr, daddr, faddr;
718 	u8  tos;
719 	int err;
720 
721 	pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
722 
723 	err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
724 				  sizeof(user_icmph));
725 	if (err)
726 		return err;
727 
728 	/*
729 	 *	Get and verify the address.
730 	 */
731 
732 	if (msg->msg_name) {
733 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
734 		if (msg->msg_namelen < sizeof(*usin))
735 			return -EINVAL;
736 		if (usin->sin_family != AF_INET)
737 			return -EAFNOSUPPORT;
738 		daddr = usin->sin_addr.s_addr;
739 		/* no remote port */
740 	} else {
741 		if (sk->sk_state != TCP_ESTABLISHED)
742 			return -EDESTADDRREQ;
743 		daddr = inet->inet_daddr;
744 		/* no remote port */
745 	}
746 
747 	ipcm_init_sk(&ipc, inet);
748 
749 	if (msg->msg_controllen) {
750 		err = ip_cmsg_send(sk, msg, &ipc, false);
751 		if (unlikely(err)) {
752 			kfree(ipc.opt);
753 			return err;
754 		}
755 		if (ipc.opt)
756 			free = 1;
757 	}
758 	if (!ipc.opt) {
759 		struct ip_options_rcu *inet_opt;
760 
761 		rcu_read_lock();
762 		inet_opt = rcu_dereference(inet->inet_opt);
763 		if (inet_opt) {
764 			memcpy(&opt_copy, inet_opt,
765 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
766 			ipc.opt = &opt_copy.opt;
767 		}
768 		rcu_read_unlock();
769 	}
770 
771 	saddr = ipc.addr;
772 	ipc.addr = faddr = daddr;
773 
774 	if (ipc.opt && ipc.opt->opt.srr) {
775 		if (!daddr) {
776 			err = -EINVAL;
777 			goto out_free;
778 		}
779 		faddr = ipc.opt->opt.faddr;
780 	}
781 	tos = get_rttos(&ipc, inet);
782 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
783 	    (msg->msg_flags & MSG_DONTROUTE) ||
784 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
785 		tos |= RTO_ONLINK;
786 	}
787 
788 	if (ipv4_is_multicast(daddr)) {
789 		if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
790 			ipc.oif = inet->mc_index;
791 		if (!saddr)
792 			saddr = inet->mc_addr;
793 	} else if (!ipc.oif)
794 		ipc.oif = inet->uc_index;
795 
796 	flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos,
797 			   RT_SCOPE_UNIVERSE, sk->sk_protocol,
798 			   inet_sk_flowi_flags(sk), faddr, saddr, 0, 0,
799 			   sk->sk_uid);
800 
801 	fl4.fl4_icmp_type = user_icmph.type;
802 	fl4.fl4_icmp_code = user_icmph.code;
803 
804 	security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4));
805 	rt = ip_route_output_flow(net, &fl4, sk);
806 	if (IS_ERR(rt)) {
807 		err = PTR_ERR(rt);
808 		rt = NULL;
809 		if (err == -ENETUNREACH)
810 			IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
811 		goto out;
812 	}
813 
814 	err = -EACCES;
815 	if ((rt->rt_flags & RTCF_BROADCAST) &&
816 	    !sock_flag(sk, SOCK_BROADCAST))
817 		goto out;
818 
819 	if (msg->msg_flags & MSG_CONFIRM)
820 		goto do_confirm;
821 back_from_confirm:
822 
823 	if (!ipc.addr)
824 		ipc.addr = fl4.daddr;
825 
826 	lock_sock(sk);
827 
828 	pfh.icmph.type = user_icmph.type; /* already checked */
829 	pfh.icmph.code = user_icmph.code; /* ditto */
830 	pfh.icmph.checksum = 0;
831 	pfh.icmph.un.echo.id = inet->inet_sport;
832 	pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
833 	pfh.msg = msg;
834 	pfh.wcheck = 0;
835 	pfh.family = AF_INET;
836 
837 	err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
838 			     sizeof(struct icmphdr), &ipc, &rt,
839 			     msg->msg_flags);
840 	if (err)
841 		ip_flush_pending_frames(sk);
842 	else
843 		err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
844 	release_sock(sk);
845 
846 out:
847 	ip_rt_put(rt);
848 out_free:
849 	if (free)
850 		kfree(ipc.opt);
851 	if (!err) {
852 		icmp_out_count(sock_net(sk), user_icmph.type);
853 		return len;
854 	}
855 	return err;
856 
857 do_confirm:
858 	if (msg->msg_flags & MSG_PROBE)
859 		dst_confirm_neigh(&rt->dst, &fl4.daddr);
860 	if (!(msg->msg_flags & MSG_PROBE) || len)
861 		goto back_from_confirm;
862 	err = 0;
863 	goto out;
864 }
865 
866 int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags,
867 		 int *addr_len)
868 {
869 	struct inet_sock *isk = inet_sk(sk);
870 	int family = sk->sk_family;
871 	struct sk_buff *skb;
872 	int copied, err;
873 
874 	pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
875 
876 	err = -EOPNOTSUPP;
877 	if (flags & MSG_OOB)
878 		goto out;
879 
880 	if (flags & MSG_ERRQUEUE)
881 		return inet_recv_error(sk, msg, len, addr_len);
882 
883 	skb = skb_recv_datagram(sk, flags, &err);
884 	if (!skb)
885 		goto out;
886 
887 	copied = skb->len;
888 	if (copied > len) {
889 		msg->msg_flags |= MSG_TRUNC;
890 		copied = len;
891 	}
892 
893 	/* Don't bother checking the checksum */
894 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
895 	if (err)
896 		goto done;
897 
898 	sock_recv_timestamp(msg, sk, skb);
899 
900 	/* Copy the address and add cmsg data. */
901 	if (family == AF_INET) {
902 		DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
903 
904 		if (sin) {
905 			sin->sin_family = AF_INET;
906 			sin->sin_port = 0 /* skb->h.uh->source */;
907 			sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
908 			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
909 			*addr_len = sizeof(*sin);
910 		}
911 
912 		if (isk->cmsg_flags)
913 			ip_cmsg_recv(msg, skb);
914 
915 #if IS_ENABLED(CONFIG_IPV6)
916 	} else if (family == AF_INET6) {
917 		struct ipv6_pinfo *np = inet6_sk(sk);
918 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
919 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
920 
921 		if (sin6) {
922 			sin6->sin6_family = AF_INET6;
923 			sin6->sin6_port = 0;
924 			sin6->sin6_addr = ip6->saddr;
925 			sin6->sin6_flowinfo = 0;
926 			if (np->sndflow)
927 				sin6->sin6_flowinfo = ip6_flowinfo(ip6);
928 			sin6->sin6_scope_id =
929 				ipv6_iface_scope_id(&sin6->sin6_addr,
930 						    inet6_iif(skb));
931 			*addr_len = sizeof(*sin6);
932 		}
933 
934 		if (inet6_sk(sk)->rxopt.all)
935 			pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
936 		if (skb->protocol == htons(ETH_P_IPV6) &&
937 		    inet6_sk(sk)->rxopt.all)
938 			pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
939 		else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
940 			ip_cmsg_recv(msg, skb);
941 #endif
942 	} else {
943 		BUG();
944 	}
945 
946 	err = copied;
947 
948 done:
949 	skb_free_datagram(sk, skb);
950 out:
951 	pr_debug("ping_recvmsg -> %d\n", err);
952 	return err;
953 }
954 EXPORT_SYMBOL_GPL(ping_recvmsg);
955 
956 static enum skb_drop_reason __ping_queue_rcv_skb(struct sock *sk,
957 						 struct sk_buff *skb)
958 {
959 	enum skb_drop_reason reason;
960 
961 	pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
962 		 inet_sk(sk), inet_sk(sk)->inet_num, skb);
963 	if (sock_queue_rcv_skb_reason(sk, skb, &reason) < 0) {
964 		kfree_skb_reason(skb, reason);
965 		pr_debug("ping_queue_rcv_skb -> failed\n");
966 		return reason;
967 	}
968 	return SKB_NOT_DROPPED_YET;
969 }
970 
971 int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
972 {
973 	return __ping_queue_rcv_skb(sk, skb) ? -1 : 0;
974 }
975 EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
976 
977 
978 /*
979  *	All we need to do is get the socket.
980  */
981 
982 enum skb_drop_reason ping_rcv(struct sk_buff *skb)
983 {
984 	enum skb_drop_reason reason = SKB_DROP_REASON_NO_SOCKET;
985 	struct sock *sk;
986 	struct net *net = dev_net(skb->dev);
987 	struct icmphdr *icmph = icmp_hdr(skb);
988 
989 	/* We assume the packet has already been checked by icmp_rcv */
990 
991 	pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
992 		 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
993 
994 	/* Push ICMP header back */
995 	skb_push(skb, skb->data - (u8 *)icmph);
996 
997 	sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
998 	if (sk) {
999 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1000 
1001 		pr_debug("rcv on socket %p\n", sk);
1002 		if (skb2)
1003 			reason = __ping_queue_rcv_skb(sk, skb2);
1004 		else
1005 			reason = SKB_DROP_REASON_NOMEM;
1006 	}
1007 
1008 	if (reason)
1009 		pr_debug("no socket, dropping\n");
1010 
1011 	return reason;
1012 }
1013 EXPORT_SYMBOL_GPL(ping_rcv);
1014 
1015 struct proto ping_prot = {
1016 	.name =		"PING",
1017 	.owner =	THIS_MODULE,
1018 	.init =		ping_init_sock,
1019 	.close =	ping_close,
1020 	.pre_connect =	ping_pre_connect,
1021 	.connect =	ip4_datagram_connect,
1022 	.disconnect =	__udp_disconnect,
1023 	.setsockopt =	ip_setsockopt,
1024 	.getsockopt =	ip_getsockopt,
1025 	.sendmsg =	ping_v4_sendmsg,
1026 	.recvmsg =	ping_recvmsg,
1027 	.bind =		ping_bind,
1028 	.backlog_rcv =	ping_queue_rcv_skb,
1029 	.release_cb =	ip4_datagram_release_cb,
1030 	.hash =		ping_hash,
1031 	.unhash =	ping_unhash,
1032 	.get_port =	ping_get_port,
1033 	.put_port =	ping_unhash,
1034 	.obj_size =	sizeof(struct inet_sock),
1035 };
1036 EXPORT_SYMBOL(ping_prot);
1037 
1038 #ifdef CONFIG_PROC_FS
1039 
1040 static struct sock *ping_get_first(struct seq_file *seq, int start)
1041 {
1042 	struct sock *sk;
1043 	struct ping_iter_state *state = seq->private;
1044 	struct net *net = seq_file_net(seq);
1045 
1046 	for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
1047 	     ++state->bucket) {
1048 		struct hlist_nulls_node *node;
1049 		struct hlist_nulls_head *hslot;
1050 
1051 		hslot = &ping_table.hash[state->bucket];
1052 
1053 		if (hlist_nulls_empty(hslot))
1054 			continue;
1055 
1056 		sk_nulls_for_each(sk, node, hslot) {
1057 			if (net_eq(sock_net(sk), net) &&
1058 			    sk->sk_family == state->family)
1059 				goto found;
1060 		}
1061 	}
1062 	sk = NULL;
1063 found:
1064 	return sk;
1065 }
1066 
1067 static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1068 {
1069 	struct ping_iter_state *state = seq->private;
1070 	struct net *net = seq_file_net(seq);
1071 
1072 	do {
1073 		sk = sk_nulls_next(sk);
1074 	} while (sk && (!net_eq(sock_net(sk), net)));
1075 
1076 	if (!sk)
1077 		return ping_get_first(seq, state->bucket + 1);
1078 	return sk;
1079 }
1080 
1081 static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1082 {
1083 	struct sock *sk = ping_get_first(seq, 0);
1084 
1085 	if (sk)
1086 		while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1087 			--pos;
1088 	return pos ? NULL : sk;
1089 }
1090 
1091 void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
1092 	__acquires(RCU)
1093 {
1094 	struct ping_iter_state *state = seq->private;
1095 	state->bucket = 0;
1096 	state->family = family;
1097 
1098 	rcu_read_lock();
1099 
1100 	return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1101 }
1102 EXPORT_SYMBOL_GPL(ping_seq_start);
1103 
1104 static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
1105 {
1106 	return ping_seq_start(seq, pos, AF_INET);
1107 }
1108 
1109 void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1110 {
1111 	struct sock *sk;
1112 
1113 	if (v == SEQ_START_TOKEN)
1114 		sk = ping_get_idx(seq, 0);
1115 	else
1116 		sk = ping_get_next(seq, v);
1117 
1118 	++*pos;
1119 	return sk;
1120 }
1121 EXPORT_SYMBOL_GPL(ping_seq_next);
1122 
1123 void ping_seq_stop(struct seq_file *seq, void *v)
1124 	__releases(RCU)
1125 {
1126 	rcu_read_unlock();
1127 }
1128 EXPORT_SYMBOL_GPL(ping_seq_stop);
1129 
1130 static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1131 		int bucket)
1132 {
1133 	struct inet_sock *inet = inet_sk(sp);
1134 	__be32 dest = inet->inet_daddr;
1135 	__be32 src = inet->inet_rcv_saddr;
1136 	__u16 destp = ntohs(inet->inet_dport);
1137 	__u16 srcp = ntohs(inet->inet_sport);
1138 
1139 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1140 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u",
1141 		bucket, src, srcp, dest, destp, sp->sk_state,
1142 		sk_wmem_alloc_get(sp),
1143 		sk_rmem_alloc_get(sp),
1144 		0, 0L, 0,
1145 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
1146 		0, sock_i_ino(sp),
1147 		refcount_read(&sp->sk_refcnt), sp,
1148 		atomic_read(&sp->sk_drops));
1149 }
1150 
1151 static int ping_v4_seq_show(struct seq_file *seq, void *v)
1152 {
1153 	seq_setwidth(seq, 127);
1154 	if (v == SEQ_START_TOKEN)
1155 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
1156 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
1157 			   "inode ref pointer drops");
1158 	else {
1159 		struct ping_iter_state *state = seq->private;
1160 
1161 		ping_v4_format_sock(v, seq, state->bucket);
1162 	}
1163 	seq_pad(seq, '\n');
1164 	return 0;
1165 }
1166 
1167 static const struct seq_operations ping_v4_seq_ops = {
1168 	.start		= ping_v4_seq_start,
1169 	.show		= ping_v4_seq_show,
1170 	.next		= ping_seq_next,
1171 	.stop		= ping_seq_stop,
1172 };
1173 
1174 static int __net_init ping_v4_proc_init_net(struct net *net)
1175 {
1176 	if (!proc_create_net("icmp", 0444, net->proc_net, &ping_v4_seq_ops,
1177 			sizeof(struct ping_iter_state)))
1178 		return -ENOMEM;
1179 	return 0;
1180 }
1181 
1182 static void __net_exit ping_v4_proc_exit_net(struct net *net)
1183 {
1184 	remove_proc_entry("icmp", net->proc_net);
1185 }
1186 
1187 static struct pernet_operations ping_v4_net_ops = {
1188 	.init = ping_v4_proc_init_net,
1189 	.exit = ping_v4_proc_exit_net,
1190 };
1191 
1192 int __init ping_proc_init(void)
1193 {
1194 	return register_pernet_subsys(&ping_v4_net_ops);
1195 }
1196 
1197 void ping_proc_exit(void)
1198 {
1199 	unregister_pernet_subsys(&ping_v4_net_ops);
1200 }
1201 
1202 #endif
1203 
1204 void __init ping_init(void)
1205 {
1206 	int i;
1207 
1208 	for (i = 0; i < PING_HTABLE_SIZE; i++)
1209 		INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1210 	spin_lock_init(&ping_table.lock);
1211 }
1212