ping.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. if (sk_hashed(sk)) {
  142. write_lock_bh(&ping_table.lock);
  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. write_unlock_bh(&ping_table.lock);
  150. }
  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", sk->sk_refcnt.counter);
  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 = %d, dif = %d\n",
  369. (int)isk->inet_num,
  370. (int)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(to + sizeof(struct icmphdr),
  526. fraglen, &pfh->wcheck,
  527. &pfh->msg->msg_iter) != fraglen)
  528. return -EFAULT;
  529. } else if (offset < sizeof(struct icmphdr)) {
  530. BUG();
  531. } else {
  532. if (csum_and_copy_from_iter(to, fraglen, &pfh->wcheck,
  533. &pfh->msg->msg_iter) != fraglen)
  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. pfh->wcheck = csum_partial((char *)&pfh->icmph,
  555. sizeof(struct icmphdr), pfh->wcheck);
  556. pfh->icmph.checksum = csum_fold(pfh->wcheck);
  557. memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
  558. skb->ip_summed = CHECKSUM_NONE;
  559. return ip_push_pending_frames(sk, fl4);
  560. }
  561. int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
  562. void *user_icmph, size_t icmph_len) {
  563. u8 type, code;
  564. if (len > 0xFFFF)
  565. return -EMSGSIZE;
  566. /*
  567. * Check the flags.
  568. */
  569. /* Mirror BSD error message compatibility */
  570. if (msg->msg_flags & MSG_OOB)
  571. return -EOPNOTSUPP;
  572. /*
  573. * Fetch the ICMP header provided by the userland.
  574. * iovec is modified! The ICMP header is consumed.
  575. */
  576. if (memcpy_from_msg(user_icmph, msg, icmph_len))
  577. return -EFAULT;
  578. if (family == AF_INET) {
  579. type = ((struct icmphdr *) user_icmph)->type;
  580. code = ((struct icmphdr *) user_icmph)->code;
  581. #if IS_ENABLED(CONFIG_IPV6)
  582. } else if (family == AF_INET6) {
  583. type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
  584. code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
  585. #endif
  586. } else {
  587. BUG();
  588. }
  589. if (!ping_supported(family, type, code))
  590. return -EINVAL;
  591. return 0;
  592. }
  593. EXPORT_SYMBOL_GPL(ping_common_sendmsg);
  594. static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  595. {
  596. struct net *net = sock_net(sk);
  597. struct flowi4 fl4;
  598. struct inet_sock *inet = inet_sk(sk);
  599. struct ipcm_cookie ipc;
  600. struct icmphdr user_icmph;
  601. struct pingfakehdr pfh;
  602. struct rtable *rt = NULL;
  603. struct ip_options_data opt_copy;
  604. int free = 0;
  605. __be32 saddr, daddr, faddr;
  606. u8 tos;
  607. int err;
  608. pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
  609. err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
  610. sizeof(user_icmph));
  611. if (err)
  612. return err;
  613. /*
  614. * Get and verify the address.
  615. */
  616. if (msg->msg_name) {
  617. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  618. if (msg->msg_namelen < sizeof(*usin))
  619. return -EINVAL;
  620. if (usin->sin_family != AF_INET)
  621. return -EAFNOSUPPORT;
  622. daddr = usin->sin_addr.s_addr;
  623. /* no remote port */
  624. } else {
  625. if (sk->sk_state != TCP_ESTABLISHED)
  626. return -EDESTADDRREQ;
  627. daddr = inet->inet_daddr;
  628. /* no remote port */
  629. }
  630. ipc.sockc.tsflags = sk->sk_tsflags;
  631. ipc.addr = inet->inet_saddr;
  632. ipc.opt = NULL;
  633. ipc.oif = sk->sk_bound_dev_if;
  634. ipc.tx_flags = 0;
  635. ipc.ttl = 0;
  636. ipc.tos = -1;
  637. if (msg->msg_controllen) {
  638. err = ip_cmsg_send(sk, msg, &ipc, false);
  639. if (unlikely(err)) {
  640. kfree(ipc.opt);
  641. return err;
  642. }
  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. sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
  658. saddr = ipc.addr;
  659. ipc.addr = faddr = daddr;
  660. if (ipc.opt && ipc.opt->opt.srr) {
  661. if (!daddr)
  662. return -EINVAL;
  663. faddr = ipc.opt->opt.faddr;
  664. }
  665. tos = get_rttos(&ipc, inet);
  666. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  667. (msg->msg_flags & MSG_DONTROUTE) ||
  668. (ipc.opt && ipc.opt->opt.is_strictroute)) {
  669. tos |= RTO_ONLINK;
  670. }
  671. if (ipv4_is_multicast(daddr)) {
  672. if (!ipc.oif)
  673. ipc.oif = inet->mc_index;
  674. if (!saddr)
  675. saddr = inet->mc_addr;
  676. } else if (!ipc.oif)
  677. ipc.oif = inet->uc_index;
  678. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  679. RT_SCOPE_UNIVERSE, sk->sk_protocol,
  680. inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
  681. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  682. rt = ip_route_output_flow(net, &fl4, sk);
  683. if (IS_ERR(rt)) {
  684. err = PTR_ERR(rt);
  685. rt = NULL;
  686. if (err == -ENETUNREACH)
  687. IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
  688. goto out;
  689. }
  690. err = -EACCES;
  691. if ((rt->rt_flags & RTCF_BROADCAST) &&
  692. !sock_flag(sk, SOCK_BROADCAST))
  693. goto out;
  694. if (msg->msg_flags & MSG_CONFIRM)
  695. goto do_confirm;
  696. back_from_confirm:
  697. if (!ipc.addr)
  698. ipc.addr = fl4.daddr;
  699. lock_sock(sk);
  700. pfh.icmph.type = user_icmph.type; /* already checked */
  701. pfh.icmph.code = user_icmph.code; /* ditto */
  702. pfh.icmph.checksum = 0;
  703. pfh.icmph.un.echo.id = inet->inet_sport;
  704. pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
  705. pfh.msg = msg;
  706. pfh.wcheck = 0;
  707. pfh.family = AF_INET;
  708. err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
  709. 0, &ipc, &rt, msg->msg_flags);
  710. if (err)
  711. ip_flush_pending_frames(sk);
  712. else
  713. err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
  714. release_sock(sk);
  715. out:
  716. ip_rt_put(rt);
  717. if (free)
  718. kfree(ipc.opt);
  719. if (!err) {
  720. icmp_out_count(sock_net(sk), user_icmph.type);
  721. return len;
  722. }
  723. return err;
  724. do_confirm:
  725. dst_confirm(&rt->dst);
  726. if (!(msg->msg_flags & MSG_PROBE) || len)
  727. goto back_from_confirm;
  728. err = 0;
  729. goto out;
  730. }
  731. int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
  732. int flags, int *addr_len)
  733. {
  734. struct inet_sock *isk = inet_sk(sk);
  735. int family = sk->sk_family;
  736. struct sk_buff *skb;
  737. int copied, err;
  738. pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
  739. err = -EOPNOTSUPP;
  740. if (flags & MSG_OOB)
  741. goto out;
  742. if (flags & MSG_ERRQUEUE)
  743. return inet_recv_error(sk, msg, len, addr_len);
  744. skb = skb_recv_datagram(sk, flags, noblock, &err);
  745. if (!skb)
  746. goto out;
  747. copied = skb->len;
  748. if (copied > len) {
  749. msg->msg_flags |= MSG_TRUNC;
  750. copied = len;
  751. }
  752. /* Don't bother checking the checksum */
  753. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  754. if (err)
  755. goto done;
  756. sock_recv_timestamp(msg, sk, skb);
  757. /* Copy the address and add cmsg data. */
  758. if (family == AF_INET) {
  759. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  760. if (sin) {
  761. sin->sin_family = AF_INET;
  762. sin->sin_port = 0 /* skb->h.uh->source */;
  763. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  764. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  765. *addr_len = sizeof(*sin);
  766. }
  767. if (isk->cmsg_flags)
  768. ip_cmsg_recv(msg, skb);
  769. #if IS_ENABLED(CONFIG_IPV6)
  770. } else if (family == AF_INET6) {
  771. struct ipv6_pinfo *np = inet6_sk(sk);
  772. struct ipv6hdr *ip6 = ipv6_hdr(skb);
  773. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  774. if (sin6) {
  775. sin6->sin6_family = AF_INET6;
  776. sin6->sin6_port = 0;
  777. sin6->sin6_addr = ip6->saddr;
  778. sin6->sin6_flowinfo = 0;
  779. if (np->sndflow)
  780. sin6->sin6_flowinfo = ip6_flowinfo(ip6);
  781. sin6->sin6_scope_id =
  782. ipv6_iface_scope_id(&sin6->sin6_addr,
  783. inet6_iif(skb));
  784. *addr_len = sizeof(*sin6);
  785. }
  786. if (inet6_sk(sk)->rxopt.all)
  787. pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
  788. if (skb->protocol == htons(ETH_P_IPV6) &&
  789. inet6_sk(sk)->rxopt.all)
  790. pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
  791. else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
  792. ip_cmsg_recv(msg, skb);
  793. #endif
  794. } else {
  795. BUG();
  796. }
  797. err = copied;
  798. done:
  799. skb_free_datagram(sk, skb);
  800. out:
  801. pr_debug("ping_recvmsg -> %d\n", err);
  802. return err;
  803. }
  804. EXPORT_SYMBOL_GPL(ping_recvmsg);
  805. int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  806. {
  807. pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
  808. inet_sk(sk), inet_sk(sk)->inet_num, skb);
  809. if (sock_queue_rcv_skb(sk, skb) < 0) {
  810. kfree_skb(skb);
  811. pr_debug("ping_queue_rcv_skb -> failed\n");
  812. return -1;
  813. }
  814. return 0;
  815. }
  816. EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
  817. /*
  818. * All we need to do is get the socket.
  819. */
  820. bool ping_rcv(struct sk_buff *skb)
  821. {
  822. struct sock *sk;
  823. struct net *net = dev_net(skb->dev);
  824. struct icmphdr *icmph = icmp_hdr(skb);
  825. /* We assume the packet has already been checked by icmp_rcv */
  826. pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
  827. skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
  828. /* Push ICMP header back */
  829. skb_push(skb, skb->data - (u8 *)icmph);
  830. sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
  831. if (sk) {
  832. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  833. pr_debug("rcv on socket %p\n", sk);
  834. if (skb2)
  835. ping_queue_rcv_skb(sk, skb2);
  836. sock_put(sk);
  837. return true;
  838. }
  839. pr_debug("no socket, dropping\n");
  840. return false;
  841. }
  842. EXPORT_SYMBOL_GPL(ping_rcv);
  843. struct proto ping_prot = {
  844. .name = "PING",
  845. .owner = THIS_MODULE,
  846. .init = ping_init_sock,
  847. .close = ping_close,
  848. .connect = ip4_datagram_connect,
  849. .disconnect = udp_disconnect,
  850. .setsockopt = ip_setsockopt,
  851. .getsockopt = ip_getsockopt,
  852. .sendmsg = ping_v4_sendmsg,
  853. .recvmsg = ping_recvmsg,
  854. .bind = ping_bind,
  855. .backlog_rcv = ping_queue_rcv_skb,
  856. .release_cb = ip4_datagram_release_cb,
  857. .hash = ping_hash,
  858. .unhash = ping_unhash,
  859. .get_port = ping_get_port,
  860. .obj_size = sizeof(struct inet_sock),
  861. };
  862. EXPORT_SYMBOL(ping_prot);
  863. #ifdef CONFIG_PROC_FS
  864. static struct sock *ping_get_first(struct seq_file *seq, int start)
  865. {
  866. struct sock *sk;
  867. struct ping_iter_state *state = seq->private;
  868. struct net *net = seq_file_net(seq);
  869. for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
  870. ++state->bucket) {
  871. struct hlist_nulls_node *node;
  872. struct hlist_nulls_head *hslot;
  873. hslot = &ping_table.hash[state->bucket];
  874. if (hlist_nulls_empty(hslot))
  875. continue;
  876. sk_nulls_for_each(sk, node, hslot) {
  877. if (net_eq(sock_net(sk), net) &&
  878. sk->sk_family == state->family)
  879. goto found;
  880. }
  881. }
  882. sk = NULL;
  883. found:
  884. return sk;
  885. }
  886. static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
  887. {
  888. struct ping_iter_state *state = seq->private;
  889. struct net *net = seq_file_net(seq);
  890. do {
  891. sk = sk_nulls_next(sk);
  892. } while (sk && (!net_eq(sock_net(sk), net)));
  893. if (!sk)
  894. return ping_get_first(seq, state->bucket + 1);
  895. return sk;
  896. }
  897. static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
  898. {
  899. struct sock *sk = ping_get_first(seq, 0);
  900. if (sk)
  901. while (pos && (sk = ping_get_next(seq, sk)) != NULL)
  902. --pos;
  903. return pos ? NULL : sk;
  904. }
  905. void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
  906. __acquires(ping_table.lock)
  907. {
  908. struct ping_iter_state *state = seq->private;
  909. state->bucket = 0;
  910. state->family = family;
  911. read_lock_bh(&ping_table.lock);
  912. return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  913. }
  914. EXPORT_SYMBOL_GPL(ping_seq_start);
  915. static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
  916. {
  917. return ping_seq_start(seq, pos, AF_INET);
  918. }
  919. void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  920. {
  921. struct sock *sk;
  922. if (v == SEQ_START_TOKEN)
  923. sk = ping_get_idx(seq, 0);
  924. else
  925. sk = ping_get_next(seq, v);
  926. ++*pos;
  927. return sk;
  928. }
  929. EXPORT_SYMBOL_GPL(ping_seq_next);
  930. void ping_seq_stop(struct seq_file *seq, void *v)
  931. __releases(ping_table.lock)
  932. {
  933. read_unlock_bh(&ping_table.lock);
  934. }
  935. EXPORT_SYMBOL_GPL(ping_seq_stop);
  936. static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
  937. int bucket)
  938. {
  939. struct inet_sock *inet = inet_sk(sp);
  940. __be32 dest = inet->inet_daddr;
  941. __be32 src = inet->inet_rcv_saddr;
  942. __u16 destp = ntohs(inet->inet_dport);
  943. __u16 srcp = ntohs(inet->inet_sport);
  944. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  945. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
  946. bucket, src, srcp, dest, destp, sp->sk_state,
  947. sk_wmem_alloc_get(sp),
  948. sk_rmem_alloc_get(sp),
  949. 0, 0L, 0,
  950. from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
  951. 0, sock_i_ino(sp),
  952. atomic_read(&sp->sk_refcnt), sp,
  953. atomic_read(&sp->sk_drops));
  954. }
  955. static int ping_v4_seq_show(struct seq_file *seq, void *v)
  956. {
  957. seq_setwidth(seq, 127);
  958. if (v == SEQ_START_TOKEN)
  959. seq_puts(seq, " sl local_address rem_address st tx_queue "
  960. "rx_queue tr tm->when retrnsmt uid timeout "
  961. "inode ref pointer drops");
  962. else {
  963. struct ping_iter_state *state = seq->private;
  964. ping_v4_format_sock(v, seq, state->bucket);
  965. }
  966. seq_pad(seq, '\n');
  967. return 0;
  968. }
  969. static int ping_seq_open(struct inode *inode, struct file *file)
  970. {
  971. struct ping_seq_afinfo *afinfo = PDE_DATA(inode);
  972. return seq_open_net(inode, file, &afinfo->seq_ops,
  973. sizeof(struct ping_iter_state));
  974. }
  975. const struct file_operations ping_seq_fops = {
  976. .open = ping_seq_open,
  977. .read = seq_read,
  978. .llseek = seq_lseek,
  979. .release = seq_release_net,
  980. };
  981. EXPORT_SYMBOL_GPL(ping_seq_fops);
  982. static struct ping_seq_afinfo ping_v4_seq_afinfo = {
  983. .name = "icmp",
  984. .family = AF_INET,
  985. .seq_fops = &ping_seq_fops,
  986. .seq_ops = {
  987. .start = ping_v4_seq_start,
  988. .show = ping_v4_seq_show,
  989. .next = ping_seq_next,
  990. .stop = ping_seq_stop,
  991. },
  992. };
  993. int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo)
  994. {
  995. struct proc_dir_entry *p;
  996. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  997. afinfo->seq_fops, afinfo);
  998. if (!p)
  999. return -ENOMEM;
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL_GPL(ping_proc_register);
  1003. void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo)
  1004. {
  1005. remove_proc_entry(afinfo->name, net->proc_net);
  1006. }
  1007. EXPORT_SYMBOL_GPL(ping_proc_unregister);
  1008. static int __net_init ping_v4_proc_init_net(struct net *net)
  1009. {
  1010. return ping_proc_register(net, &ping_v4_seq_afinfo);
  1011. }
  1012. static void __net_exit ping_v4_proc_exit_net(struct net *net)
  1013. {
  1014. ping_proc_unregister(net, &ping_v4_seq_afinfo);
  1015. }
  1016. static struct pernet_operations ping_v4_net_ops = {
  1017. .init = ping_v4_proc_init_net,
  1018. .exit = ping_v4_proc_exit_net,
  1019. };
  1020. int __init ping_proc_init(void)
  1021. {
  1022. return register_pernet_subsys(&ping_v4_net_ops);
  1023. }
  1024. void ping_proc_exit(void)
  1025. {
  1026. unregister_pernet_subsys(&ping_v4_net_ops);
  1027. }
  1028. #endif
  1029. void __init ping_init(void)
  1030. {
  1031. int i;
  1032. for (i = 0; i < PING_HTABLE_SIZE; i++)
  1033. INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
  1034. rwlock_init(&ping_table.lock);
  1035. }