/net/ipv4/ping.c

http://github.com/mirrors/linux · C · 1182 lines · 930 code · 181 blank · 71 comment · 240 complexity · c9215a7fe8b3004e6af1dbc7393d62b6 MD5 · raw file

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