ping.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 u32 ping_hashfn(const struct net *net, u32 num, u32 mask)
  62. {
  63. u32 res = (num + net_hash_mix(net)) & mask;
  64. pr_debug("hash(%u) = %u\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. if (sk->sk_family == AF_INET6)
  226. sk->sk_ipv6only = 1;
  227. inet_get_ping_group_range_net(net, &low, &high);
  228. if (gid_lte(low, group) && gid_lte(group, high))
  229. return 0;
  230. group_info = get_current_groups();
  231. count = group_info->ngroups;
  232. for (i = 0; i < group_info->nblocks; i++) {
  233. int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
  234. for (j = 0; j < cp_count; j++) {
  235. kgid_t gid = group_info->blocks[i][j];
  236. if (gid_lte(low, gid) && gid_lte(gid, high))
  237. goto out_release_group;
  238. }
  239. count -= cp_count;
  240. }
  241. ret = -EACCES;
  242. out_release_group:
  243. put_group_info(group_info);
  244. return ret;
  245. }
  246. EXPORT_SYMBOL_GPL(ping_init_sock);
  247. void ping_close(struct sock *sk, long timeout)
  248. {
  249. pr_debug("ping_close(sk=%p,sk->num=%u)\n",
  250. inet_sk(sk), inet_sk(sk)->inet_num);
  251. pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
  252. sk_common_release(sk);
  253. }
  254. EXPORT_SYMBOL_GPL(ping_close);
  255. /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
  256. static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
  257. struct sockaddr *uaddr, int addr_len) {
  258. struct net *net = sock_net(sk);
  259. if (sk->sk_family == AF_INET) {
  260. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  261. int chk_addr_ret;
  262. if (addr_len < sizeof(*addr))
  263. return -EINVAL;
  264. if (addr->sin_family != AF_INET &&
  265. !(addr->sin_family == AF_UNSPEC &&
  266. addr->sin_addr.s_addr == htonl(INADDR_ANY)))
  267. return -EAFNOSUPPORT;
  268. pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
  269. sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
  270. chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
  271. if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
  272. chk_addr_ret = RTN_LOCAL;
  273. if ((net->ipv4.sysctl_ip_nonlocal_bind == 0 &&
  274. isk->freebind == 0 && isk->transparent == 0 &&
  275. chk_addr_ret != RTN_LOCAL) ||
  276. chk_addr_ret == RTN_MULTICAST ||
  277. chk_addr_ret == RTN_BROADCAST)
  278. return -EADDRNOTAVAIL;
  279. #if IS_ENABLED(CONFIG_IPV6)
  280. } else if (sk->sk_family == AF_INET6) {
  281. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
  282. int addr_type, scoped, has_addr;
  283. struct net_device *dev = NULL;
  284. if (addr_len < sizeof(*addr))
  285. return -EINVAL;
  286. if (addr->sin6_family != AF_INET6)
  287. return -EAFNOSUPPORT;
  288. pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
  289. sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
  290. addr_type = ipv6_addr_type(&addr->sin6_addr);
  291. scoped = __ipv6_addr_needs_scope_id(addr_type);
  292. if ((addr_type != IPV6_ADDR_ANY &&
  293. !(addr_type & IPV6_ADDR_UNICAST)) ||
  294. (scoped && !addr->sin6_scope_id))
  295. return -EINVAL;
  296. rcu_read_lock();
  297. if (addr->sin6_scope_id) {
  298. dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
  299. if (!dev) {
  300. rcu_read_unlock();
  301. return -ENODEV;
  302. }
  303. }
  304. has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
  305. scoped);
  306. rcu_read_unlock();
  307. if (!(isk->freebind || isk->transparent || has_addr ||
  308. addr_type == IPV6_ADDR_ANY))
  309. return -EADDRNOTAVAIL;
  310. if (scoped)
  311. sk->sk_bound_dev_if = addr->sin6_scope_id;
  312. #endif
  313. } else {
  314. return -EAFNOSUPPORT;
  315. }
  316. return 0;
  317. }
  318. static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
  319. {
  320. if (saddr->sa_family == AF_INET) {
  321. struct inet_sock *isk = inet_sk(sk);
  322. struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
  323. isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
  324. #if IS_ENABLED(CONFIG_IPV6)
  325. } else if (saddr->sa_family == AF_INET6) {
  326. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
  327. struct ipv6_pinfo *np = inet6_sk(sk);
  328. sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
  329. #endif
  330. }
  331. }
  332. static void ping_clear_saddr(struct sock *sk, int dif)
  333. {
  334. sk->sk_bound_dev_if = dif;
  335. if (sk->sk_family == AF_INET) {
  336. struct inet_sock *isk = inet_sk(sk);
  337. isk->inet_rcv_saddr = isk->inet_saddr = 0;
  338. #if IS_ENABLED(CONFIG_IPV6)
  339. } else if (sk->sk_family == AF_INET6) {
  340. struct ipv6_pinfo *np = inet6_sk(sk);
  341. memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
  342. memset(&np->saddr, 0, sizeof(np->saddr));
  343. #endif
  344. }
  345. }
  346. /*
  347. * We need our own bind because there are no privileged id's == local ports.
  348. * Moreover, we don't allow binding to multi- and broadcast addresses.
  349. */
  350. int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  351. {
  352. struct inet_sock *isk = inet_sk(sk);
  353. unsigned short snum;
  354. int err;
  355. int dif = sk->sk_bound_dev_if;
  356. err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
  357. if (err)
  358. return err;
  359. lock_sock(sk);
  360. err = -EINVAL;
  361. if (isk->inet_num != 0)
  362. goto out;
  363. err = -EADDRINUSE;
  364. ping_set_saddr(sk, uaddr);
  365. snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
  366. if (ping_get_port(sk, snum) != 0) {
  367. ping_clear_saddr(sk, dif);
  368. goto out;
  369. }
  370. pr_debug("after bind(): num = %d, dif = %d\n",
  371. (int)isk->inet_num,
  372. (int)sk->sk_bound_dev_if);
  373. err = 0;
  374. if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
  375. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  376. #if IS_ENABLED(CONFIG_IPV6)
  377. if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  378. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  379. #endif
  380. if (snum)
  381. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  382. isk->inet_sport = htons(isk->inet_num);
  383. isk->inet_daddr = 0;
  384. isk->inet_dport = 0;
  385. #if IS_ENABLED(CONFIG_IPV6)
  386. if (sk->sk_family == AF_INET6)
  387. memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
  388. #endif
  389. sk_dst_reset(sk);
  390. out:
  391. release_sock(sk);
  392. pr_debug("ping_v4_bind -> %d\n", err);
  393. return err;
  394. }
  395. EXPORT_SYMBOL_GPL(ping_bind);
  396. /*
  397. * Is this a supported type of ICMP message?
  398. */
  399. static inline int ping_supported(int family, int type, int code)
  400. {
  401. return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
  402. (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
  403. }
  404. /*
  405. * This routine is called by the ICMP module when it gets some
  406. * sort of error condition.
  407. */
  408. void ping_err(struct sk_buff *skb, int offset, u32 info)
  409. {
  410. int family;
  411. struct icmphdr *icmph;
  412. struct inet_sock *inet_sock;
  413. int type;
  414. int code;
  415. struct net *net = dev_net(skb->dev);
  416. struct sock *sk;
  417. int harderr;
  418. int err;
  419. if (skb->protocol == htons(ETH_P_IP)) {
  420. family = AF_INET;
  421. type = icmp_hdr(skb)->type;
  422. code = icmp_hdr(skb)->code;
  423. icmph = (struct icmphdr *)(skb->data + offset);
  424. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  425. family = AF_INET6;
  426. type = icmp6_hdr(skb)->icmp6_type;
  427. code = icmp6_hdr(skb)->icmp6_code;
  428. icmph = (struct icmphdr *) (skb->data + offset);
  429. } else {
  430. BUG();
  431. }
  432. /* We assume the packet has already been checked by icmp_unreach */
  433. if (!ping_supported(family, icmph->type, icmph->code))
  434. return;
  435. pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
  436. skb->protocol, type, code, ntohs(icmph->un.echo.id),
  437. ntohs(icmph->un.echo.sequence));
  438. sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
  439. if (sk == NULL) {
  440. pr_debug("no socket, dropping\n");
  441. return; /* No socket for error */
  442. }
  443. pr_debug("err on socket %p\n", sk);
  444. err = 0;
  445. harderr = 0;
  446. inet_sock = inet_sk(sk);
  447. if (skb->protocol == htons(ETH_P_IP)) {
  448. switch (type) {
  449. default:
  450. case ICMP_TIME_EXCEEDED:
  451. err = EHOSTUNREACH;
  452. break;
  453. case ICMP_SOURCE_QUENCH:
  454. /* This is not a real error but ping wants to see it.
  455. * Report it with some fake errno.
  456. */
  457. err = EREMOTEIO;
  458. break;
  459. case ICMP_PARAMETERPROB:
  460. err = EPROTO;
  461. harderr = 1;
  462. break;
  463. case ICMP_DEST_UNREACH:
  464. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  465. ipv4_sk_update_pmtu(skb, sk, info);
  466. if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
  467. err = EMSGSIZE;
  468. harderr = 1;
  469. break;
  470. }
  471. goto out;
  472. }
  473. err = EHOSTUNREACH;
  474. if (code <= NR_ICMP_UNREACH) {
  475. harderr = icmp_err_convert[code].fatal;
  476. err = icmp_err_convert[code].errno;
  477. }
  478. break;
  479. case ICMP_REDIRECT:
  480. /* See ICMP_SOURCE_QUENCH */
  481. ipv4_sk_redirect(skb, sk);
  482. err = EREMOTEIO;
  483. break;
  484. }
  485. #if IS_ENABLED(CONFIG_IPV6)
  486. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  487. harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
  488. #endif
  489. }
  490. /*
  491. * RFC1122: OK. Passes ICMP errors back to application, as per
  492. * 4.1.3.3.
  493. */
  494. if ((family == AF_INET && !inet_sock->recverr) ||
  495. (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
  496. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  497. goto out;
  498. } else {
  499. if (family == AF_INET) {
  500. ip_icmp_error(sk, skb, err, 0 /* no remote port */,
  501. info, (u8 *)icmph);
  502. #if IS_ENABLED(CONFIG_IPV6)
  503. } else if (family == AF_INET6) {
  504. pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
  505. info, (u8 *)icmph);
  506. #endif
  507. }
  508. }
  509. sk->sk_err = err;
  510. sk->sk_error_report(sk);
  511. out:
  512. sock_put(sk);
  513. }
  514. EXPORT_SYMBOL_GPL(ping_err);
  515. /*
  516. * Copy and checksum an ICMP Echo packet from user space into a buffer
  517. * starting from the payload.
  518. */
  519. int ping_getfrag(void *from, char *to,
  520. int offset, int fraglen, int odd, struct sk_buff *skb)
  521. {
  522. struct pingfakehdr *pfh = (struct pingfakehdr *)from;
  523. if (offset == 0) {
  524. fraglen -= sizeof(struct icmphdr);
  525. if (fraglen < 0)
  526. BUG();
  527. if (csum_and_copy_from_iter(to + sizeof(struct icmphdr),
  528. fraglen, &pfh->wcheck,
  529. &pfh->msg->msg_iter) != fraglen)
  530. return -EFAULT;
  531. } else if (offset < sizeof(struct icmphdr)) {
  532. BUG();
  533. } else {
  534. if (csum_and_copy_from_iter(to, fraglen, &pfh->wcheck,
  535. &pfh->msg->msg_iter) != fraglen)
  536. return -EFAULT;
  537. }
  538. #if IS_ENABLED(CONFIG_IPV6)
  539. /* For IPv6, checksum each skb as we go along, as expected by
  540. * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
  541. * wcheck, it will be finalized in ping_v4_push_pending_frames.
  542. */
  543. if (pfh->family == AF_INET6) {
  544. skb->csum = pfh->wcheck;
  545. skb->ip_summed = CHECKSUM_NONE;
  546. pfh->wcheck = 0;
  547. }
  548. #endif
  549. return 0;
  550. }
  551. EXPORT_SYMBOL_GPL(ping_getfrag);
  552. static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
  553. struct flowi4 *fl4)
  554. {
  555. struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
  556. pfh->wcheck = csum_partial((char *)&pfh->icmph,
  557. sizeof(struct icmphdr), pfh->wcheck);
  558. pfh->icmph.checksum = csum_fold(pfh->wcheck);
  559. memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
  560. skb->ip_summed = CHECKSUM_NONE;
  561. return ip_push_pending_frames(sk, fl4);
  562. }
  563. int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
  564. void *user_icmph, size_t icmph_len) {
  565. u8 type, code;
  566. if (len > 0xFFFF)
  567. return -EMSGSIZE;
  568. /*
  569. * Check the flags.
  570. */
  571. /* Mirror BSD error message compatibility */
  572. if (msg->msg_flags & MSG_OOB)
  573. return -EOPNOTSUPP;
  574. /*
  575. * Fetch the ICMP header provided by the userland.
  576. * iovec is modified! The ICMP header is consumed.
  577. */
  578. if (memcpy_from_msg(user_icmph, msg, icmph_len))
  579. return -EFAULT;
  580. if (family == AF_INET) {
  581. type = ((struct icmphdr *) user_icmph)->type;
  582. code = ((struct icmphdr *) user_icmph)->code;
  583. #if IS_ENABLED(CONFIG_IPV6)
  584. } else if (family == AF_INET6) {
  585. type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
  586. code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
  587. #endif
  588. } else {
  589. BUG();
  590. }
  591. if (!ping_supported(family, type, code))
  592. return -EINVAL;
  593. return 0;
  594. }
  595. EXPORT_SYMBOL_GPL(ping_common_sendmsg);
  596. static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  597. {
  598. struct net *net = sock_net(sk);
  599. struct flowi4 fl4;
  600. struct inet_sock *inet = inet_sk(sk);
  601. struct ipcm_cookie ipc;
  602. struct icmphdr user_icmph;
  603. struct pingfakehdr pfh;
  604. struct rtable *rt = NULL;
  605. struct ip_options_data opt_copy;
  606. int free = 0;
  607. __be32 saddr, daddr, faddr;
  608. u8 tos;
  609. int err;
  610. pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
  611. err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
  612. sizeof(user_icmph));
  613. if (err)
  614. return err;
  615. /*
  616. * Get and verify the address.
  617. */
  618. if (msg->msg_name) {
  619. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  620. if (msg->msg_namelen < sizeof(*usin))
  621. return -EINVAL;
  622. if (usin->sin_family != AF_INET)
  623. return -EAFNOSUPPORT;
  624. daddr = usin->sin_addr.s_addr;
  625. /* no remote port */
  626. } else {
  627. if (sk->sk_state != TCP_ESTABLISHED)
  628. return -EDESTADDRREQ;
  629. daddr = inet->inet_daddr;
  630. /* no remote port */
  631. }
  632. ipc.addr = inet->inet_saddr;
  633. ipc.opt = NULL;
  634. ipc.oif = sk->sk_bound_dev_if;
  635. ipc.tx_flags = 0;
  636. ipc.ttl = 0;
  637. ipc.tos = -1;
  638. sock_tx_timestamp(sk, &ipc.tx_flags);
  639. if (msg->msg_controllen) {
  640. err = ip_cmsg_send(sock_net(sk), msg, &ipc, false);
  641. if (err)
  642. return err;
  643. if (ipc.opt)
  644. free = 1;
  645. }
  646. if (!ipc.opt) {
  647. struct ip_options_rcu *inet_opt;
  648. rcu_read_lock();
  649. inet_opt = rcu_dereference(inet->inet_opt);
  650. if (inet_opt) {
  651. memcpy(&opt_copy, inet_opt,
  652. sizeof(*inet_opt) + inet_opt->opt.optlen);
  653. ipc.opt = &opt_copy.opt;
  654. }
  655. rcu_read_unlock();
  656. }
  657. saddr = ipc.addr;
  658. ipc.addr = faddr = daddr;
  659. if (ipc.opt && ipc.opt->opt.srr) {
  660. if (!daddr)
  661. return -EINVAL;
  662. faddr = ipc.opt->opt.faddr;
  663. }
  664. tos = get_rttos(&ipc, inet);
  665. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  666. (msg->msg_flags & MSG_DONTROUTE) ||
  667. (ipc.opt && ipc.opt->opt.is_strictroute)) {
  668. tos |= RTO_ONLINK;
  669. }
  670. if (ipv4_is_multicast(daddr)) {
  671. if (!ipc.oif)
  672. ipc.oif = inet->mc_index;
  673. if (!saddr)
  674. saddr = inet->mc_addr;
  675. } else if (!ipc.oif)
  676. ipc.oif = inet->uc_index;
  677. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  678. RT_SCOPE_UNIVERSE, sk->sk_protocol,
  679. inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
  680. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  681. rt = ip_route_output_flow(net, &fl4, sk);
  682. if (IS_ERR(rt)) {
  683. err = PTR_ERR(rt);
  684. rt = NULL;
  685. if (err == -ENETUNREACH)
  686. IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
  687. goto out;
  688. }
  689. err = -EACCES;
  690. if ((rt->rt_flags & RTCF_BROADCAST) &&
  691. !sock_flag(sk, SOCK_BROADCAST))
  692. goto out;
  693. if (msg->msg_flags & MSG_CONFIRM)
  694. goto do_confirm;
  695. back_from_confirm:
  696. if (!ipc.addr)
  697. ipc.addr = fl4.daddr;
  698. lock_sock(sk);
  699. pfh.icmph.type = user_icmph.type; /* already checked */
  700. pfh.icmph.code = user_icmph.code; /* ditto */
  701. pfh.icmph.checksum = 0;
  702. pfh.icmph.un.echo.id = inet->inet_sport;
  703. pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
  704. pfh.msg = msg;
  705. pfh.wcheck = 0;
  706. pfh.family = AF_INET;
  707. err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
  708. 0, &ipc, &rt, msg->msg_flags);
  709. if (err)
  710. ip_flush_pending_frames(sk);
  711. else
  712. err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
  713. release_sock(sk);
  714. out:
  715. ip_rt_put(rt);
  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. dst_confirm(&rt->dst);
  725. if (!(msg->msg_flags & MSG_PROBE) || len)
  726. goto back_from_confirm;
  727. err = 0;
  728. goto out;
  729. }
  730. int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
  731. int flags, int *addr_len)
  732. {
  733. struct inet_sock *isk = inet_sk(sk);
  734. int family = sk->sk_family;
  735. struct sk_buff *skb;
  736. int copied, err;
  737. pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
  738. err = -EOPNOTSUPP;
  739. if (flags & MSG_OOB)
  740. goto out;
  741. if (flags & MSG_ERRQUEUE)
  742. return inet_recv_error(sk, msg, len, addr_len);
  743. skb = skb_recv_datagram(sk, flags, noblock, &err);
  744. if (!skb)
  745. goto out;
  746. copied = skb->len;
  747. if (copied > len) {
  748. msg->msg_flags |= MSG_TRUNC;
  749. copied = len;
  750. }
  751. /* Don't bother checking the checksum */
  752. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  753. if (err)
  754. goto done;
  755. sock_recv_timestamp(msg, sk, skb);
  756. /* Copy the address and add cmsg data. */
  757. if (family == AF_INET) {
  758. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  759. if (sin) {
  760. sin->sin_family = AF_INET;
  761. sin->sin_port = 0 /* skb->h.uh->source */;
  762. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  763. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  764. *addr_len = sizeof(*sin);
  765. }
  766. if (isk->cmsg_flags)
  767. ip_cmsg_recv(msg, skb);
  768. #if IS_ENABLED(CONFIG_IPV6)
  769. } else if (family == AF_INET6) {
  770. struct ipv6_pinfo *np = inet6_sk(sk);
  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 (np->sndflow)
  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) && isk->cmsg_flags)
  791. ip_cmsg_recv(msg, skb);
  792. #endif
  793. } else {
  794. BUG();
  795. }
  796. err = copied;
  797. done:
  798. skb_free_datagram(sk, skb);
  799. out:
  800. pr_debug("ping_recvmsg -> %d\n", err);
  801. return err;
  802. }
  803. EXPORT_SYMBOL_GPL(ping_recvmsg);
  804. int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  805. {
  806. pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
  807. inet_sk(sk), inet_sk(sk)->inet_num, skb);
  808. if (sock_queue_rcv_skb(sk, skb) < 0) {
  809. kfree_skb(skb);
  810. pr_debug("ping_queue_rcv_skb -> failed\n");
  811. return -1;
  812. }
  813. return 0;
  814. }
  815. EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
  816. /*
  817. * All we need to do is get the socket.
  818. */
  819. bool ping_rcv(struct sk_buff *skb)
  820. {
  821. struct sock *sk;
  822. struct net *net = dev_net(skb->dev);
  823. struct icmphdr *icmph = icmp_hdr(skb);
  824. /* We assume the packet has already been checked by icmp_rcv */
  825. pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
  826. skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
  827. /* Push ICMP header back */
  828. skb_push(skb, skb->data - (u8 *)icmph);
  829. sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
  830. if (sk != NULL) {
  831. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  832. pr_debug("rcv on socket %p\n", sk);
  833. if (skb2)
  834. ping_queue_rcv_skb(sk, skb2);
  835. sock_put(sk);
  836. return true;
  837. }
  838. pr_debug("no socket, dropping\n");
  839. return false;
  840. }
  841. EXPORT_SYMBOL_GPL(ping_rcv);
  842. struct proto ping_prot = {
  843. .name = "PING",
  844. .owner = THIS_MODULE,
  845. .init = ping_init_sock,
  846. .close = ping_close,
  847. .connect = ip4_datagram_connect,
  848. .disconnect = udp_disconnect,
  849. .setsockopt = ip_setsockopt,
  850. .getsockopt = ip_getsockopt,
  851. .sendmsg = ping_v4_sendmsg,
  852. .recvmsg = ping_recvmsg,
  853. .bind = ping_bind,
  854. .backlog_rcv = ping_queue_rcv_skb,
  855. .release_cb = ip4_datagram_release_cb,
  856. .hash = ping_hash,
  857. .unhash = ping_unhash,
  858. .get_port = ping_get_port,
  859. .obj_size = sizeof(struct inet_sock),
  860. };
  861. EXPORT_SYMBOL(ping_prot);
  862. #ifdef CONFIG_PROC_FS
  863. static struct sock *ping_get_first(struct seq_file *seq, int start)
  864. {
  865. struct sock *sk;
  866. struct ping_iter_state *state = seq->private;
  867. struct net *net = seq_file_net(seq);
  868. for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
  869. ++state->bucket) {
  870. struct hlist_nulls_node *node;
  871. struct hlist_nulls_head *hslot;
  872. hslot = &ping_table.hash[state->bucket];
  873. if (hlist_nulls_empty(hslot))
  874. continue;
  875. sk_nulls_for_each(sk, node, hslot) {
  876. if (net_eq(sock_net(sk), net) &&
  877. sk->sk_family == state->family)
  878. goto found;
  879. }
  880. }
  881. sk = NULL;
  882. found:
  883. return sk;
  884. }
  885. static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
  886. {
  887. struct ping_iter_state *state = seq->private;
  888. struct net *net = seq_file_net(seq);
  889. do {
  890. sk = sk_nulls_next(sk);
  891. } while (sk && (!net_eq(sock_net(sk), net)));
  892. if (!sk)
  893. return ping_get_first(seq, state->bucket + 1);
  894. return sk;
  895. }
  896. static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
  897. {
  898. struct sock *sk = ping_get_first(seq, 0);
  899. if (sk)
  900. while (pos && (sk = ping_get_next(seq, sk)) != NULL)
  901. --pos;
  902. return pos ? NULL : sk;
  903. }
  904. void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
  905. {
  906. struct ping_iter_state *state = seq->private;
  907. state->bucket = 0;
  908. state->family = family;
  909. read_lock_bh(&ping_table.lock);
  910. return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  911. }
  912. EXPORT_SYMBOL_GPL(ping_seq_start);
  913. static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
  914. {
  915. return ping_seq_start(seq, pos, AF_INET);
  916. }
  917. void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  918. {
  919. struct sock *sk;
  920. if (v == SEQ_START_TOKEN)
  921. sk = ping_get_idx(seq, 0);
  922. else
  923. sk = ping_get_next(seq, v);
  924. ++*pos;
  925. return sk;
  926. }
  927. EXPORT_SYMBOL_GPL(ping_seq_next);
  928. void ping_seq_stop(struct seq_file *seq, void *v)
  929. {
  930. read_unlock_bh(&ping_table.lock);
  931. }
  932. EXPORT_SYMBOL_GPL(ping_seq_stop);
  933. static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
  934. int bucket)
  935. {
  936. struct inet_sock *inet = inet_sk(sp);
  937. __be32 dest = inet->inet_daddr;
  938. __be32 src = inet->inet_rcv_saddr;
  939. __u16 destp = ntohs(inet->inet_dport);
  940. __u16 srcp = ntohs(inet->inet_sport);
  941. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  942. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
  943. bucket, src, srcp, dest, destp, sp->sk_state,
  944. sk_wmem_alloc_get(sp),
  945. sk_rmem_alloc_get(sp),
  946. 0, 0L, 0,
  947. from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
  948. 0, sock_i_ino(sp),
  949. atomic_read(&sp->sk_refcnt), sp,
  950. atomic_read(&sp->sk_drops));
  951. }
  952. static int ping_v4_seq_show(struct seq_file *seq, void *v)
  953. {
  954. seq_setwidth(seq, 127);
  955. if (v == SEQ_START_TOKEN)
  956. seq_puts(seq, " sl local_address rem_address st tx_queue "
  957. "rx_queue tr tm->when retrnsmt uid timeout "
  958. "inode ref pointer drops");
  959. else {
  960. struct ping_iter_state *state = seq->private;
  961. ping_v4_format_sock(v, seq, state->bucket);
  962. }
  963. seq_pad(seq, '\n');
  964. return 0;
  965. }
  966. static const struct seq_operations ping_v4_seq_ops = {
  967. .show = ping_v4_seq_show,
  968. .start = ping_v4_seq_start,
  969. .next = ping_seq_next,
  970. .stop = ping_seq_stop,
  971. };
  972. static int ping_seq_open(struct inode *inode, struct file *file)
  973. {
  974. struct ping_seq_afinfo *afinfo = PDE_DATA(inode);
  975. return seq_open_net(inode, file, &afinfo->seq_ops,
  976. sizeof(struct ping_iter_state));
  977. }
  978. const struct file_operations ping_seq_fops = {
  979. .open = ping_seq_open,
  980. .read = seq_read,
  981. .llseek = seq_lseek,
  982. .release = seq_release_net,
  983. };
  984. EXPORT_SYMBOL_GPL(ping_seq_fops);
  985. static struct ping_seq_afinfo ping_v4_seq_afinfo = {
  986. .name = "icmp",
  987. .family = AF_INET,
  988. .seq_fops = &ping_seq_fops,
  989. .seq_ops = {
  990. .start = ping_v4_seq_start,
  991. .show = ping_v4_seq_show,
  992. .next = ping_seq_next,
  993. .stop = ping_seq_stop,
  994. },
  995. };
  996. int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo)
  997. {
  998. struct proc_dir_entry *p;
  999. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1000. afinfo->seq_fops, afinfo);
  1001. if (!p)
  1002. return -ENOMEM;
  1003. return 0;
  1004. }
  1005. EXPORT_SYMBOL_GPL(ping_proc_register);
  1006. void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo)
  1007. {
  1008. remove_proc_entry(afinfo->name, net->proc_net);
  1009. }
  1010. EXPORT_SYMBOL_GPL(ping_proc_unregister);
  1011. static int __net_init ping_v4_proc_init_net(struct net *net)
  1012. {
  1013. return ping_proc_register(net, &ping_v4_seq_afinfo);
  1014. }
  1015. static void __net_exit ping_v4_proc_exit_net(struct net *net)
  1016. {
  1017. ping_proc_unregister(net, &ping_v4_seq_afinfo);
  1018. }
  1019. static struct pernet_operations ping_v4_net_ops = {
  1020. .init = ping_v4_proc_init_net,
  1021. .exit = ping_v4_proc_exit_net,
  1022. };
  1023. int __init ping_proc_init(void)
  1024. {
  1025. return register_pernet_subsys(&ping_v4_net_ops);
  1026. }
  1027. void ping_proc_exit(void)
  1028. {
  1029. unregister_pernet_subsys(&ping_v4_net_ops);
  1030. }
  1031. #endif
  1032. void __init ping_init(void)
  1033. {
  1034. int i;
  1035. for (i = 0; i < PING_HTABLE_SIZE; i++)
  1036. INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
  1037. rwlock_init(&ping_table.lock);
  1038. }