ping.c 29 KB

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