ping.c 28 KB

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