ping.c 28 KB

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