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