ping.c 29 KB

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