ping.c 28.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		"Ping" sockets
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Based on ipv4/udp.c code.
 *
 * Authors:	Vasiliy Kulikov / Openwall (for Linux 2.6),
 *		Pavel Kankovsky (for Linux 2.4.32)
 *
 * Pavel gave all rights to bugs to Vasiliy,
 * none of the bugs are Pavel's now.
 *
 */

#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <net/snmp.h>
#include <net/ip.h>
#include <net/icmp.h>
#include <net/protocol.h>
#include <linux/skbuff.h>
#include <linux/proc_fs.h>
40
#include <linux/export.h>
41 42 43 44 45 46 47
#include <net/sock.h>
#include <net/ping.h>
#include <net/udp.h>
#include <net/route.h>
#include <net/inet_common.h>
#include <net/checksum.h>

48 49 50 51 52 53 54 55
#if IS_ENABLED(CONFIG_IPV6)
#include <linux/in6.h>
#include <linux/icmpv6.h>
#include <net/addrconf.h>
#include <net/ipv6.h>
#include <net/transp_v6.h>
#endif

56 57 58 59
struct ping_table {
	struct hlist_nulls_head	hash[PING_HTABLE_SIZE];
	rwlock_t		lock;
};
60

61
static struct ping_table ping_table;
62 63
struct pingv6_ops pingv6_ops;
EXPORT_SYMBOL_GPL(pingv6_ops);
64

E
Eric Dumazet 已提交
65
static u16 ping_port_rover;
66

67
static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)
68
{
69
	u32 res = (num + net_hash_mix(net)) & mask;
70

71
	pr_debug("hash(%u) = %u\n", num, res);
72 73
	return res;
}
74
EXPORT_SYMBOL_GPL(ping_hash);
75 76

static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
77
					     struct net *net, unsigned int num)
78 79 80 81
{
	return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
}

82
int ping_get_port(struct sock *sk, unsigned short ident)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
	struct hlist_nulls_node *node;
	struct hlist_nulls_head *hlist;
	struct inet_sock *isk, *isk2;
	struct sock *sk2 = NULL;

	isk = inet_sk(sk);
	write_lock_bh(&ping_table.lock);
	if (ident == 0) {
		u32 i;
		u16 result = ping_port_rover + 1;

		for (i = 0; i < (1L << 16); i++, result++) {
			if (!result)
				result++; /* avoid zero */
			hlist = ping_hashslot(&ping_table, sock_net(sk),
					    result);
			ping_portaddr_for_each_entry(sk2, node, hlist) {
				isk2 = inet_sk(sk2);

				if (isk2->inet_num == result)
					goto next_port;
			}

			/* found */
			ping_port_rover = ident = result;
			break;
next_port:
			;
		}
		if (i >= (1L << 16))
			goto fail;
	} else {
		hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
		ping_portaddr_for_each_entry(sk2, node, hlist) {
			isk2 = inet_sk(sk2);

120 121 122 123
			/* BUG? Why is this reuse and not reuseaddr? ping.c
			 * doesn't turn off SO_REUSEADDR, and it doesn't expect
			 * that other ping processes can steal its packets.
			 */
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
			if ((isk2->inet_num == ident) &&
			    (sk2 != sk) &&
			    (!sk2->sk_reuse || !sk->sk_reuse))
				goto fail;
		}
	}

	pr_debug("found port/ident = %d\n", ident);
	isk->inet_num = ident;
	if (sk_unhashed(sk)) {
		pr_debug("was not hashed\n");
		sock_hold(sk);
		hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
	}
	write_unlock_bh(&ping_table.lock);
	return 0;

fail:
	write_unlock_bh(&ping_table.lock);
	return 1;
}
146
EXPORT_SYMBOL_GPL(ping_get_port);
147

148
int ping_hash(struct sock *sk)
149
{
150
	pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
151
	BUG(); /* "Please do not press this button again." */
152 153

	return 0;
154 155
}

156
void ping_unhash(struct sock *sk)
157 158
{
	struct inet_sock *isk = inet_sk(sk);
E
Eric Dumazet 已提交
159

160
	pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
E
Eric Dumazet 已提交
161
	write_lock_bh(&ping_table.lock);
162 163
	if (sk_hashed(sk)) {
		hlist_nulls_del(&sk->sk_nulls_node);
164
		sk_nulls_node_init(&sk->sk_nulls_node);
165
		sock_put(sk);
E
Eric Dumazet 已提交
166 167
		isk->inet_num = 0;
		isk->inet_sport = 0;
168 169
		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
	}
E
Eric Dumazet 已提交
170
	write_unlock_bh(&ping_table.lock);
171
}
172
EXPORT_SYMBOL_GPL(ping_unhash);
173

174
static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
175 176 177 178 179
{
	struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
	struct sock *sk = NULL;
	struct inet_sock *isk;
	struct hlist_nulls_node *hnode;
180 181 182 183 184 185 186 187 188 189 190
	int dif = skb->dev->ifindex;

	if (skb->protocol == htons(ETH_P_IP)) {
		pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
			 (int)ident, &ip_hdr(skb)->daddr, dif);
#if IS_ENABLED(CONFIG_IPV6)
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
			 (int)ident, &ipv6_hdr(skb)->daddr, dif);
#endif
	}
191 192 193 194 195 196 197 198 199

	read_lock_bh(&ping_table.lock);

	ping_portaddr_for_each_entry(sk, hnode, hslot) {
		isk = inet_sk(sk);

		pr_debug("iterate\n");
		if (isk->inet_num != ident)
			continue;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

		if (skb->protocol == htons(ETH_P_IP) &&
		    sk->sk_family == AF_INET) {
			pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
				 (int) isk->inet_num, &isk->inet_rcv_saddr,
				 sk->sk_bound_dev_if);

			if (isk->inet_rcv_saddr &&
			    isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
				continue;
#if IS_ENABLED(CONFIG_IPV6)
		} else if (skb->protocol == htons(ETH_P_IPV6) &&
			   sk->sk_family == AF_INET6) {

			pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
				 (int) isk->inet_num,
216
				 &sk->sk_v6_rcv_saddr,
217 218
				 sk->sk_bound_dev_if);

219 220
			if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
			    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr,
221 222 223
					     &ipv6_hdr(skb)->daddr))
				continue;
#endif
224 225
		} else {
			continue;
226 227
		}

228 229 230 231 232 233 234 235 236 237 238 239 240 241
		if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
			continue;

		sock_hold(sk);
		goto exit;
	}

	sk = NULL;
exit:
	read_unlock_bh(&ping_table.lock);

	return sk;
}

242 243
static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
					  kgid_t *high)
V
Vasiliy Kulikov 已提交
244
{
245
	kgid_t *data = net->ipv4.ping_group_range.range;
246 247
	unsigned int seq;

V
Vasiliy Kulikov 已提交
248
	do {
249
		seq = read_seqbegin(&net->ipv4.ping_group_range.lock);
V
Vasiliy Kulikov 已提交
250 251 252

		*low = data[0];
		*high = data[1];
253
	} while (read_seqretry(&net->ipv4.ping_group_range.lock, seq));
V
Vasiliy Kulikov 已提交
254 255 256
}


257
int ping_init_sock(struct sock *sk)
258 259
{
	struct net *net = sock_net(sk);
260
	kgid_t group = current_egid();
261
	struct group_info *group_info;
262
	int i;
263
	kgid_t low, high;
264
	int ret = 0;
265

266 267 268
	if (sk->sk_family == AF_INET6)
		sk->sk_ipv6only = 1;

269 270
	inet_get_ping_group_range_net(net, &low, &high);
	if (gid_lte(low, group) && gid_lte(group, high))
271 272
		return 0;

273
	group_info = get_current_groups();
274 275
	for (i = 0; i < group_info->ngroups; i++) {
		kgid_t gid = group_info->gid[i];
276

277 278
		if (gid_lte(low, gid) && gid_lte(gid, high))
			goto out_release_group;
279 280
	}

281 282 283 284 285
	ret = -EACCES;

out_release_group:
	put_group_info(group_info);
	return ret;
286
}
287
EXPORT_SYMBOL_GPL(ping_init_sock);
288

289
void ping_close(struct sock *sk, long timeout)
290 291
{
	pr_debug("ping_close(sk=%p,sk->num=%u)\n",
J
Joe Perches 已提交
292
		 inet_sk(sk), inet_sk(sk)->inet_num);
293
	pr_debug("isk->refcnt = %d\n", refcount_read(&sk->sk_refcnt));
294 295 296

	sk_common_release(sk);
}
297 298 299
EXPORT_SYMBOL_GPL(ping_close);

/* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
300 301
static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
				struct sockaddr *uaddr, int addr_len) {
302 303 304 305 306 307 308 309
	struct net *net = sock_net(sk);
	if (sk->sk_family == AF_INET) {
		struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
		int chk_addr_ret;

		if (addr_len < sizeof(*addr))
			return -EINVAL;

310 311 312 313 314
		if (addr->sin_family != AF_INET &&
		    !(addr->sin_family == AF_UNSPEC &&
		      addr->sin_addr.s_addr == htonl(INADDR_ANY)))
			return -EAFNOSUPPORT;

315 316 317 318 319 320 321 322
		pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
			 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));

		chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);

		if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
			chk_addr_ret = RTN_LOCAL;

323
		if ((net->ipv4.sysctl_ip_nonlocal_bind == 0 &&
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		    isk->freebind == 0 && isk->transparent == 0 &&
		     chk_addr_ret != RTN_LOCAL) ||
		    chk_addr_ret == RTN_MULTICAST ||
		    chk_addr_ret == RTN_BROADCAST)
			return -EADDRNOTAVAIL;

#if IS_ENABLED(CONFIG_IPV6)
	} else if (sk->sk_family == AF_INET6) {
		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
		int addr_type, scoped, has_addr;
		struct net_device *dev = NULL;

		if (addr_len < sizeof(*addr))
			return -EINVAL;

339
		if (addr->sin6_family != AF_INET6)
340
			return -EAFNOSUPPORT;
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
		pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
			 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));

		addr_type = ipv6_addr_type(&addr->sin6_addr);
		scoped = __ipv6_addr_needs_scope_id(addr_type);
		if ((addr_type != IPV6_ADDR_ANY &&
		     !(addr_type & IPV6_ADDR_UNICAST)) ||
		    (scoped && !addr->sin6_scope_id))
			return -EINVAL;

		rcu_read_lock();
		if (addr->sin6_scope_id) {
			dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
			if (!dev) {
				rcu_read_unlock();
				return -ENODEV;
			}
		}
		has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
						    scoped);
		rcu_read_unlock();

T
Tom Herbert 已提交
364 365
		if (!(net->ipv6.sysctl.ip_nonlocal_bind ||
		      isk->freebind || isk->transparent || has_addr ||
366 367 368 369 370 371 372 373 374 375 376 377
		      addr_type == IPV6_ADDR_ANY))
			return -EADDRNOTAVAIL;

		if (scoped)
			sk->sk_bound_dev_if = addr->sin6_scope_id;
#endif
	} else {
		return -EAFNOSUPPORT;
	}
	return 0;
}

378
static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
379 380 381 382 383 384 385 386 387
{
	if (saddr->sa_family == AF_INET) {
		struct inet_sock *isk = inet_sk(sk);
		struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
		isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
#if IS_ENABLED(CONFIG_IPV6)
	} else if (saddr->sa_family == AF_INET6) {
		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
		struct ipv6_pinfo *np = inet6_sk(sk);
388
		sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
389 390 391
#endif
	}
}
392

393
static void ping_clear_saddr(struct sock *sk, int dif)
394 395 396 397 398 399 400 401
{
	sk->sk_bound_dev_if = dif;
	if (sk->sk_family == AF_INET) {
		struct inet_sock *isk = inet_sk(sk);
		isk->inet_rcv_saddr = isk->inet_saddr = 0;
#if IS_ENABLED(CONFIG_IPV6)
	} else if (sk->sk_family == AF_INET6) {
		struct ipv6_pinfo *np = inet6_sk(sk);
402
		memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
403 404 405 406
		memset(&np->saddr, 0, sizeof(np->saddr));
#endif
	}
}
407 408 409 410 411
/*
 * We need our own bind because there are no privileged id's == local ports.
 * Moreover, we don't allow binding to multi- and broadcast addresses.
 */

412
int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
413 414 415 416
{
	struct inet_sock *isk = inet_sk(sk);
	unsigned short snum;
	int err;
417
	int dif = sk->sk_bound_dev_if;
418

419 420 421
	err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
	if (err)
		return err;
422 423 424 425 426 427 428 429

	lock_sock(sk);

	err = -EINVAL;
	if (isk->inet_num != 0)
		goto out;

	err = -EADDRINUSE;
430 431 432 433
	ping_set_saddr(sk, uaddr);
	snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
	if (ping_get_port(sk, snum) != 0) {
		ping_clear_saddr(sk, dif);
434 435 436
		goto out;
	}

437 438 439
	pr_debug("after bind(): num = %hu, dif = %d\n",
		 isk->inet_num,
		 sk->sk_bound_dev_if);
440 441

	err = 0;
442
	if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
443
		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
444 445 446 447
#if IS_ENABLED(CONFIG_IPV6)
	if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
#endif
448

449 450 451 452 453
	if (snum)
		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
	isk->inet_sport = htons(isk->inet_num);
	isk->inet_daddr = 0;
	isk->inet_dport = 0;
454 455 456

#if IS_ENABLED(CONFIG_IPV6)
	if (sk->sk_family == AF_INET6)
457
		memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
458 459
#endif

460 461 462 463 464 465
	sk_dst_reset(sk);
out:
	release_sock(sk);
	pr_debug("ping_v4_bind -> %d\n", err);
	return err;
}
466
EXPORT_SYMBOL_GPL(ping_bind);
467 468 469 470 471

/*
 * Is this a supported type of ICMP message?
 */

472
static inline int ping_supported(int family, int type, int code)
473
{
474 475
	return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
	       (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
476 477 478 479 480 481 482
}

/*
 * This routine is called by the ICMP module when it gets some
 * sort of error condition.
 */

483
void ping_err(struct sk_buff *skb, int offset, u32 info)
484
{
485 486
	int family;
	struct icmphdr *icmph;
487
	struct inet_sock *inet_sock;
488 489
	int type;
	int code;
490 491 492 493 494
	struct net *net = dev_net(skb->dev);
	struct sock *sk;
	int harderr;
	int err;

495 496 497 498 499 500 501 502 503 504 505 506 507 508
	if (skb->protocol == htons(ETH_P_IP)) {
		family = AF_INET;
		type = icmp_hdr(skb)->type;
		code = icmp_hdr(skb)->code;
		icmph = (struct icmphdr *)(skb->data + offset);
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		family = AF_INET6;
		type = icmp6_hdr(skb)->icmp6_type;
		code = icmp6_hdr(skb)->icmp6_code;
		icmph = (struct icmphdr *) (skb->data + offset);
	} else {
		BUG();
	}

509 510
	/* We assume the packet has already been checked by icmp_unreach */

511
	if (!ping_supported(family, icmph->type, icmph->code))
512 513
		return;

514 515 516
	pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
		 skb->protocol, type, code, ntohs(icmph->un.echo.id),
		 ntohs(icmph->un.echo.sequence));
517

518
	sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
519
	if (!sk) {
520 521 522 523 524 525 526 527 528
		pr_debug("no socket, dropping\n");
		return;	/* No socket for error */
	}
	pr_debug("err on socket %p\n", sk);

	err = 0;
	harderr = 0;
	inet_sock = inet_sk(sk);

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	if (skb->protocol == htons(ETH_P_IP)) {
		switch (type) {
		default:
		case ICMP_TIME_EXCEEDED:
			err = EHOSTUNREACH;
			break;
		case ICMP_SOURCE_QUENCH:
			/* This is not a real error but ping wants to see it.
			 * Report it with some fake errno.
			 */
			err = EREMOTEIO;
			break;
		case ICMP_PARAMETERPROB:
			err = EPROTO;
			harderr = 1;
			break;
		case ICMP_DEST_UNREACH:
			if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
				ipv4_sk_update_pmtu(skb, sk, info);
				if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
					err = EMSGSIZE;
					harderr = 1;
					break;
				}
				goto out;
554
			}
555 556 557 558 559 560 561 562 563 564 565
			err = EHOSTUNREACH;
			if (code <= NR_ICMP_UNREACH) {
				harderr = icmp_err_convert[code].fatal;
				err = icmp_err_convert[code].errno;
			}
			break;
		case ICMP_REDIRECT:
			/* See ICMP_SOURCE_QUENCH */
			ipv4_sk_redirect(skb, sk);
			err = EREMOTEIO;
			break;
566
		}
567 568 569 570
#if IS_ENABLED(CONFIG_IPV6)
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
#endif
571 572 573 574 575 576
	}

	/*
	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
	 *	4.1.3.3.
	 */
577 578
	if ((family == AF_INET && !inet_sock->recverr) ||
	    (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
579 580 581
		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
			goto out;
	} else {
582 583 584 585 586 587 588 589 590
		if (family == AF_INET) {
			ip_icmp_error(sk, skb, err, 0 /* no remote port */,
				      info, (u8 *)icmph);
#if IS_ENABLED(CONFIG_IPV6)
		} else if (family == AF_INET6) {
			pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
						   info, (u8 *)icmph);
#endif
		}
591 592 593 594 595 596
	}
	sk->sk_err = err;
	sk->sk_error_report(sk);
out:
	sock_put(sk);
}
597
EXPORT_SYMBOL_GPL(ping_err);
598 599

/*
600 601
 *	Copy and checksum an ICMP Echo packet from user space into a buffer
 *	starting from the payload.
602 603
 */

604 605
int ping_getfrag(void *from, char *to,
		 int offset, int fraglen, int odd, struct sk_buff *skb)
606 607 608 609
{
	struct pingfakehdr *pfh = (struct pingfakehdr *)from;

	if (offset == 0) {
610 611
		fraglen -= sizeof(struct icmphdr);
		if (fraglen < 0)
612
			BUG();
613
		if (!csum_and_copy_from_iter_full(to + sizeof(struct icmphdr),
614
			    fraglen, &pfh->wcheck,
615
			    &pfh->msg->msg_iter))
616
			return -EFAULT;
617 618 619
	} else if (offset < sizeof(struct icmphdr)) {
			BUG();
	} else {
620 621
		if (!csum_and_copy_from_iter_full(to, fraglen, &pfh->wcheck,
					    &pfh->msg->msg_iter))
622 623
			return -EFAULT;
	}
624

625 626 627 628 629 630 631 632 633
#if IS_ENABLED(CONFIG_IPV6)
	/* For IPv6, checksum each skb as we go along, as expected by
	 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
	 * wcheck, it will be finalized in ping_v4_push_pending_frames.
	 */
	if (pfh->family == AF_INET6) {
		skb->csum = pfh->wcheck;
		skb->ip_summed = CHECKSUM_NONE;
		pfh->wcheck = 0;
634
	}
635 636
#endif

637 638
	return 0;
}
639
EXPORT_SYMBOL_GPL(ping_getfrag);
640

641 642
static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
				       struct flowi4 *fl4)
643 644 645
{
	struct sk_buff *skb = skb_peek(&sk->sk_write_queue);

W
WANG Cong 已提交
646 647
	if (!skb)
		return 0;
648 649 650 651 652 653 654 655
	pfh->wcheck = csum_partial((char *)&pfh->icmph,
		sizeof(struct icmphdr), pfh->wcheck);
	pfh->icmph.checksum = csum_fold(pfh->wcheck);
	memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
	skb->ip_summed = CHECKSUM_NONE;
	return ip_push_pending_frames(sk, fl4);
}

656 657 658
int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
			void *user_icmph, size_t icmph_len) {
	u8 type, code;
659 660 661 662

	if (len > 0xFFFF)
		return -EMSGSIZE;

663 664 665 666
	/* Must have at least a full ICMP header. */
	if (len < icmph_len)
		return -EINVAL;

667 668 669 670 671 672 673 674 675 676
	/*
	 *	Check the flags.
	 */

	/* Mirror BSD error message compatibility */
	if (msg->msg_flags & MSG_OOB)
		return -EOPNOTSUPP;

	/*
	 *	Fetch the ICMP header provided by the userland.
677
	 *	iovec is modified! The ICMP header is consumed.
678
	 */
A
Al Viro 已提交
679
	if (memcpy_from_msg(user_icmph, msg, icmph_len))
680
		return -EFAULT;
681 682 683 684 685 686 687 688 689 690 691 692 693 694

	if (family == AF_INET) {
		type = ((struct icmphdr *) user_icmph)->type;
		code = ((struct icmphdr *) user_icmph)->code;
#if IS_ENABLED(CONFIG_IPV6)
	} else if (family == AF_INET6) {
		type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
		code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
#endif
	} else {
		BUG();
	}

	if (!ping_supported(family, type, code))
695 696
		return -EINVAL;

697 698 699 700
	return 0;
}
EXPORT_SYMBOL_GPL(ping_common_sendmsg);

701
static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
{
	struct net *net = sock_net(sk);
	struct flowi4 fl4;
	struct inet_sock *inet = inet_sk(sk);
	struct ipcm_cookie ipc;
	struct icmphdr user_icmph;
	struct pingfakehdr pfh;
	struct rtable *rt = NULL;
	struct ip_options_data opt_copy;
	int free = 0;
	__be32 saddr, daddr, faddr;
	u8  tos;
	int err;

	pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);

	err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
				  sizeof(user_icmph));
	if (err)
		return err;

723 724 725 726 727
	/*
	 *	Get and verify the address.
	 */

	if (msg->msg_name) {
728
		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
729 730 731
		if (msg->msg_namelen < sizeof(*usin))
			return -EINVAL;
		if (usin->sin_family != AF_INET)
732
			return -EAFNOSUPPORT;
733 734 735 736 737 738 739 740 741
		daddr = usin->sin_addr.s_addr;
		/* no remote port */
	} else {
		if (sk->sk_state != TCP_ESTABLISHED)
			return -EDESTADDRREQ;
		daddr = inet->inet_daddr;
		/* no remote port */
	}

742
	ipcm_init_sk(&ipc, inet);
743

744
	if (msg->msg_controllen) {
745
		err = ip_cmsg_send(sk, msg, &ipc, false);
746 747
		if (unlikely(err)) {
			kfree(ipc.opt);
748
			return err;
749
		}
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
		if (ipc.opt)
			free = 1;
	}
	if (!ipc.opt) {
		struct ip_options_rcu *inet_opt;

		rcu_read_lock();
		inet_opt = rcu_dereference(inet->inet_opt);
		if (inet_opt) {
			memcpy(&opt_copy, inet_opt,
			       sizeof(*inet_opt) + inet_opt->opt.optlen);
			ipc.opt = &opt_copy.opt;
		}
		rcu_read_unlock();
	}

	saddr = ipc.addr;
	ipc.addr = faddr = daddr;

	if (ipc.opt && ipc.opt->opt.srr) {
770 771 772 773
		if (!daddr) {
			err = -EINVAL;
			goto out_free;
		}
774 775
		faddr = ipc.opt->opt.faddr;
	}
776
	tos = get_rttos(&ipc, inet);
777 778 779 780 781 782 783 784 785 786 787
	if (sock_flag(sk, SOCK_LOCALROUTE) ||
	    (msg->msg_flags & MSG_DONTROUTE) ||
	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
		tos |= RTO_ONLINK;
	}

	if (ipv4_is_multicast(daddr)) {
		if (!ipc.oif)
			ipc.oif = inet->mc_index;
		if (!saddr)
			saddr = inet->mc_addr;
788 789
	} else if (!ipc.oif)
		ipc.oif = inet->uc_index;
790 791 792

	flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
			   RT_SCOPE_UNIVERSE, sk->sk_protocol,
793 794
			   inet_sk_flowi_flags(sk), faddr, saddr, 0, 0,
			   sk->sk_uid);
795 796 797 798 799 800 801

	security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
	rt = ip_route_output_flow(net, &fl4, sk);
	if (IS_ERR(rt)) {
		err = PTR_ERR(rt);
		rt = NULL;
		if (err == -ENETUNREACH)
802
			IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
		goto out;
	}

	err = -EACCES;
	if ((rt->rt_flags & RTCF_BROADCAST) &&
	    !sock_flag(sk, SOCK_BROADCAST))
		goto out;

	if (msg->msg_flags & MSG_CONFIRM)
		goto do_confirm;
back_from_confirm:

	if (!ipc.addr)
		ipc.addr = fl4.daddr;

	lock_sock(sk);

	pfh.icmph.type = user_icmph.type; /* already checked */
	pfh.icmph.code = user_icmph.code; /* ditto */
	pfh.icmph.checksum = 0;
	pfh.icmph.un.echo.id = inet->inet_sport;
	pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
825
	pfh.msg = msg;
826
	pfh.wcheck = 0;
827
	pfh.family = AF_INET;
828 829 830 831 832 833

	err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
			0, &ipc, &rt, msg->msg_flags);
	if (err)
		ip_flush_pending_frames(sk);
	else
834
		err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
835 836 837 838
	release_sock(sk);

out:
	ip_rt_put(rt);
839
out_free:
840 841 842 843 844 845 846 847 848
	if (free)
		kfree(ipc.opt);
	if (!err) {
		icmp_out_count(sock_net(sk), user_icmph.type);
		return len;
	}
	return err;

do_confirm:
849 850
	if (msg->msg_flags & MSG_PROBE)
		dst_confirm_neigh(&rt->dst, &fl4.daddr);
851 852 853 854 855 856
	if (!(msg->msg_flags & MSG_PROBE) || len)
		goto back_from_confirm;
	err = 0;
	goto out;
}

857 858
int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
		 int flags, int *addr_len)
859 860
{
	struct inet_sock *isk = inet_sk(sk);
861
	int family = sk->sk_family;
862 863 864 865 866
	struct sk_buff *skb;
	int copied, err;

	pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);

867
	err = -EOPNOTSUPP;
868 869 870
	if (flags & MSG_OOB)
		goto out;

871 872
	if (flags & MSG_ERRQUEUE)
		return inet_recv_error(sk, msg, len, addr_len);
873 874 875 876 877 878 879 880 881 882 883 884

	skb = skb_recv_datagram(sk, flags, noblock, &err);
	if (!skb)
		goto out;

	copied = skb->len;
	if (copied > len) {
		msg->msg_flags |= MSG_TRUNC;
		copied = len;
	}

	/* Don't bother checking the checksum */
885
	err = skb_copy_datagram_msg(skb, 0, msg, copied);
886 887 888 889 890
	if (err)
		goto done;

	sock_recv_timestamp(msg, sk, skb);

891 892
	/* Copy the address and add cmsg data. */
	if (family == AF_INET) {
893
		DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
894

895 896 897 898 899 900 901
		if (sin) {
			sin->sin_family = AF_INET;
			sin->sin_port = 0 /* skb->h.uh->source */;
			sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
			*addr_len = sizeof(*sin);
		}
902 903 904 905 906 907 908 909

		if (isk->cmsg_flags)
			ip_cmsg_recv(msg, skb);

#if IS_ENABLED(CONFIG_IPV6)
	} else if (family == AF_INET6) {
		struct ipv6_pinfo *np = inet6_sk(sk);
		struct ipv6hdr *ip6 = ipv6_hdr(skb);
910
		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
911

912 913 914 915 916 917 918 919 920
		if (sin6) {
			sin6->sin6_family = AF_INET6;
			sin6->sin6_port = 0;
			sin6->sin6_addr = ip6->saddr;
			sin6->sin6_flowinfo = 0;
			if (np->sndflow)
				sin6->sin6_flowinfo = ip6_flowinfo(ip6);
			sin6->sin6_scope_id =
				ipv6_iface_scope_id(&sin6->sin6_addr,
921
						    inet6_iif(skb));
922 923
			*addr_len = sizeof(*sin6);
		}
924 925

		if (inet6_sk(sk)->rxopt.all)
926 927 928 929 930 931
			pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
		if (skb->protocol == htons(ETH_P_IPV6) &&
		    inet6_sk(sk)->rxopt.all)
			pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
		else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
			ip_cmsg_recv(msg, skb);
932 933 934
#endif
	} else {
		BUG();
935
	}
936

937 938 939 940 941 942 943 944
	err = copied;

done:
	skb_free_datagram(sk, skb);
out:
	pr_debug("ping_recvmsg -> %d\n", err);
	return err;
}
945
EXPORT_SYMBOL_GPL(ping_recvmsg);
946

947
int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
948 949
{
	pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
J
Joe Perches 已提交
950
		 inet_sk(sk), inet_sk(sk)->inet_num, skb);
951 952 953 954 955 956 957
	if (sock_queue_rcv_skb(sk, skb) < 0) {
		kfree_skb(skb);
		pr_debug("ping_queue_rcv_skb -> failed\n");
		return -1;
	}
	return 0;
}
958
EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
959 960 961 962 963 964


/*
 *	All we need to do is get the socket.
 */

965
bool ping_rcv(struct sk_buff *skb)
966 967 968 969 970 971 972 973
{
	struct sock *sk;
	struct net *net = dev_net(skb->dev);
	struct icmphdr *icmph = icmp_hdr(skb);

	/* We assume the packet has already been checked by icmp_rcv */

	pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
J
Joe Perches 已提交
974
		 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
975 976 977 978

	/* Push ICMP header back */
	skb_push(skb, skb->data - (u8 *)icmph);

979
	sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
980
	if (sk) {
981 982
		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);

983
		pr_debug("rcv on socket %p\n", sk);
984 985
		if (skb2)
			ping_queue_rcv_skb(sk, skb2);
986
		sock_put(sk);
987
		return true;
988 989 990
	}
	pr_debug("no socket, dropping\n");

991
	return false;
992
}
993
EXPORT_SYMBOL_GPL(ping_rcv);
994 995 996 997 998 999 1000

struct proto ping_prot = {
	.name =		"PING",
	.owner =	THIS_MODULE,
	.init =		ping_init_sock,
	.close =	ping_close,
	.connect =	ip4_datagram_connect,
1001
	.disconnect =	__udp_disconnect,
1002 1003
	.setsockopt =	ip_setsockopt,
	.getsockopt =	ip_getsockopt,
1004
	.sendmsg =	ping_v4_sendmsg,
1005 1006 1007
	.recvmsg =	ping_recvmsg,
	.bind =		ping_bind,
	.backlog_rcv =	ping_queue_rcv_skb,
1008
	.release_cb =	ip4_datagram_release_cb,
1009 1010 1011
	.hash =		ping_hash,
	.unhash =	ping_unhash,
	.get_port =	ping_get_port,
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
	.obj_size =	sizeof(struct inet_sock),
};
EXPORT_SYMBOL(ping_prot);

#ifdef CONFIG_PROC_FS

static struct sock *ping_get_first(struct seq_file *seq, int start)
{
	struct sock *sk;
	struct ping_iter_state *state = seq->private;
	struct net *net = seq_file_net(seq);

	for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
	     ++state->bucket) {
		struct hlist_nulls_node *node;
C
Changli Gao 已提交
1027 1028 1029
		struct hlist_nulls_head *hslot;

		hslot = &ping_table.hash[state->bucket];
1030 1031 1032 1033 1034

		if (hlist_nulls_empty(hslot))
			continue;

		sk_nulls_for_each(sk, node, hslot) {
1035 1036
			if (net_eq(sock_net(sk), net) &&
			    sk->sk_family == state->family)
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
				goto found;
		}
	}
	sk = NULL;
found:
	return sk;
}

static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
{
	struct ping_iter_state *state = seq->private;
	struct net *net = seq_file_net(seq);

	do {
		sk = sk_nulls_next(sk);
	} while (sk && (!net_eq(sock_net(sk), net)));

	if (!sk)
		return ping_get_first(seq, state->bucket + 1);
	return sk;
}

static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
{
	struct sock *sk = ping_get_first(seq, 0);

	if (sk)
		while (pos && (sk = ping_get_next(seq, sk)) != NULL)
			--pos;
	return pos ? NULL : sk;
}

1069
void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
1070
	__acquires(ping_table.lock)
1071 1072 1073
{
	struct ping_iter_state *state = seq->private;
	state->bucket = 0;
1074
	state->family = family;
1075 1076 1077 1078 1079

	read_lock_bh(&ping_table.lock);

	return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
}
1080
EXPORT_SYMBOL_GPL(ping_seq_start);
1081

1082 1083 1084 1085 1086
static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
{
	return ping_seq_start(seq, pos, AF_INET);
}

1087
void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
{
	struct sock *sk;

	if (v == SEQ_START_TOKEN)
		sk = ping_get_idx(seq, 0);
	else
		sk = ping_get_next(seq, v);

	++*pos;
	return sk;
}
1099
EXPORT_SYMBOL_GPL(ping_seq_next);
1100

1101
void ping_seq_stop(struct seq_file *seq, void *v)
1102
	__releases(ping_table.lock)
1103 1104 1105
{
	read_unlock_bh(&ping_table.lock);
}
1106
EXPORT_SYMBOL_GPL(ping_seq_stop);
1107

1108
static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1109
		int bucket)
1110 1111 1112 1113 1114 1115 1116 1117
{
	struct inet_sock *inet = inet_sk(sp);
	__be32 dest = inet->inet_daddr;
	__be32 src = inet->inet_rcv_saddr;
	__u16 destp = ntohs(inet->inet_dport);
	__u16 srcp = ntohs(inet->inet_sport);

	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1118
		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
1119 1120 1121
		bucket, src, srcp, dest, destp, sp->sk_state,
		sk_wmem_alloc_get(sp),
		sk_rmem_alloc_get(sp),
1122 1123 1124
		0, 0L, 0,
		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
		0, sock_i_ino(sp),
1125
		refcount_read(&sp->sk_refcnt), sp,
1126
		atomic_read(&sp->sk_drops));
1127 1128
}

1129
static int ping_v4_seq_show(struct seq_file *seq, void *v)
1130
{
1131
	seq_setwidth(seq, 127);
1132
	if (v == SEQ_START_TOKEN)
1133
		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
1134 1135 1136 1137 1138
			   "rx_queue tr tm->when retrnsmt   uid  timeout "
			   "inode ref pointer drops");
	else {
		struct ping_iter_state *state = seq->private;

1139
		ping_v4_format_sock(v, seq, state->bucket);
1140
	}
1141
	seq_pad(seq, '\n');
1142 1143 1144
	return 0;
}

1145 1146 1147 1148 1149 1150 1151 1152
static const struct seq_operations ping_v4_seq_ops = {
	.start		= ping_v4_seq_start,
	.show		= ping_v4_seq_show,
	.next		= ping_seq_next,
	.stop		= ping_seq_stop,
};

static int __net_init ping_v4_proc_init_net(struct net *net)
1153
{
1154 1155
	if (!proc_create_net("icmp", 0444, net->proc_net, &ping_v4_seq_ops,
			sizeof(struct ping_iter_state)))
1156 1157
		return -ENOMEM;
	return 0;
1158 1159
}

1160
static void __net_exit ping_v4_proc_exit_net(struct net *net)
1161
{
1162
	remove_proc_entry("icmp", net->proc_net);
1163 1164
}

1165 1166 1167
static struct pernet_operations ping_v4_net_ops = {
	.init = ping_v4_proc_init_net,
	.exit = ping_v4_proc_exit_net,
1168 1169 1170 1171
};

int __init ping_proc_init(void)
{
1172
	return register_pernet_subsys(&ping_v4_net_ops);
1173 1174 1175 1176
}

void ping_proc_exit(void)
{
1177
	unregister_pernet_subsys(&ping_v4_net_ops);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
}

#endif

void __init ping_init(void)
{
	int i;

	for (i = 0; i < PING_HTABLE_SIZE; i++)
		INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
	rwlock_init(&ping_table.lock);
}