ping.c 29 KB

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