ping.c 29 KB

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