ping.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. fl4.fl4_icmp_type = user_icmph.type;
  681. fl4.fl4_icmp_code = user_icmph.code;
  682. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  683. rt = ip_route_output_flow(net, &fl4, sk);
  684. if (IS_ERR(rt)) {
  685. err = PTR_ERR(rt);
  686. rt = NULL;
  687. if (err == -ENETUNREACH)
  688. IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
  689. goto out;
  690. }
  691. err = -EACCES;
  692. if ((rt->rt_flags & RTCF_BROADCAST) &&
  693. !sock_flag(sk, SOCK_BROADCAST))
  694. goto out;
  695. if (msg->msg_flags & MSG_CONFIRM)
  696. goto do_confirm;
  697. back_from_confirm:
  698. if (!ipc.addr)
  699. ipc.addr = fl4.daddr;
  700. lock_sock(sk);
  701. pfh.icmph.type = user_icmph.type; /* already checked */
  702. pfh.icmph.code = user_icmph.code; /* ditto */
  703. pfh.icmph.checksum = 0;
  704. pfh.icmph.un.echo.id = inet->inet_sport;
  705. pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
  706. pfh.msg = msg;
  707. pfh.wcheck = 0;
  708. pfh.family = AF_INET;
  709. err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
  710. 0, &ipc, &rt, msg->msg_flags);
  711. if (err)
  712. ip_flush_pending_frames(sk);
  713. else
  714. err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
  715. release_sock(sk);
  716. out:
  717. ip_rt_put(rt);
  718. out_free:
  719. if (free)
  720. kfree(ipc.opt);
  721. if (!err) {
  722. icmp_out_count(sock_net(sk), user_icmph.type);
  723. return len;
  724. }
  725. return err;
  726. do_confirm:
  727. if (msg->msg_flags & MSG_PROBE)
  728. dst_confirm_neigh(&rt->dst, &fl4.daddr);
  729. if (!(msg->msg_flags & MSG_PROBE) || len)
  730. goto back_from_confirm;
  731. err = 0;
  732. goto out;
  733. }
  734. int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
  735. int flags, int *addr_len)
  736. {
  737. struct inet_sock *isk = inet_sk(sk);
  738. int family = sk->sk_family;
  739. struct sk_buff *skb;
  740. int copied, err;
  741. pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
  742. err = -EOPNOTSUPP;
  743. if (flags & MSG_OOB)
  744. goto out;
  745. if (flags & MSG_ERRQUEUE)
  746. return inet_recv_error(sk, msg, len, addr_len);
  747. skb = skb_recv_datagram(sk, flags, noblock, &err);
  748. if (!skb)
  749. goto out;
  750. copied = skb->len;
  751. if (copied > len) {
  752. msg->msg_flags |= MSG_TRUNC;
  753. copied = len;
  754. }
  755. /* Don't bother checking the checksum */
  756. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  757. if (err)
  758. goto done;
  759. sock_recv_timestamp(msg, sk, skb);
  760. /* Copy the address and add cmsg data. */
  761. if (family == AF_INET) {
  762. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  763. if (sin) {
  764. sin->sin_family = AF_INET;
  765. sin->sin_port = 0 /* skb->h.uh->source */;
  766. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  767. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  768. *addr_len = sizeof(*sin);
  769. }
  770. if (isk->cmsg_flags)
  771. ip_cmsg_recv(msg, skb);
  772. #if IS_ENABLED(CONFIG_IPV6)
  773. } else if (family == AF_INET6) {
  774. struct ipv6_pinfo *np = inet6_sk(sk);
  775. struct ipv6hdr *ip6 = ipv6_hdr(skb);
  776. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  777. if (sin6) {
  778. sin6->sin6_family = AF_INET6;
  779. sin6->sin6_port = 0;
  780. sin6->sin6_addr = ip6->saddr;
  781. sin6->sin6_flowinfo = 0;
  782. if (np->sndflow)
  783. sin6->sin6_flowinfo = ip6_flowinfo(ip6);
  784. sin6->sin6_scope_id =
  785. ipv6_iface_scope_id(&sin6->sin6_addr,
  786. inet6_iif(skb));
  787. *addr_len = sizeof(*sin6);
  788. }
  789. if (inet6_sk(sk)->rxopt.all)
  790. pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
  791. if (skb->protocol == htons(ETH_P_IPV6) &&
  792. inet6_sk(sk)->rxopt.all)
  793. pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
  794. else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
  795. ip_cmsg_recv(msg, skb);
  796. #endif
  797. } else {
  798. BUG();
  799. }
  800. err = copied;
  801. done:
  802. skb_free_datagram(sk, skb);
  803. out:
  804. pr_debug("ping_recvmsg -> %d\n", err);
  805. return err;
  806. }
  807. EXPORT_SYMBOL_GPL(ping_recvmsg);
  808. int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  809. {
  810. pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
  811. inet_sk(sk), inet_sk(sk)->inet_num, skb);
  812. if (sock_queue_rcv_skb(sk, skb) < 0) {
  813. kfree_skb(skb);
  814. pr_debug("ping_queue_rcv_skb -> failed\n");
  815. return -1;
  816. }
  817. return 0;
  818. }
  819. EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
  820. /*
  821. * All we need to do is get the socket.
  822. */
  823. bool ping_rcv(struct sk_buff *skb)
  824. {
  825. struct sock *sk;
  826. struct net *net = dev_net(skb->dev);
  827. struct icmphdr *icmph = icmp_hdr(skb);
  828. /* We assume the packet has already been checked by icmp_rcv */
  829. pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
  830. skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
  831. /* Push ICMP header back */
  832. skb_push(skb, skb->data - (u8 *)icmph);
  833. sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
  834. if (sk) {
  835. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  836. pr_debug("rcv on socket %p\n", sk);
  837. if (skb2)
  838. ping_queue_rcv_skb(sk, skb2);
  839. sock_put(sk);
  840. return true;
  841. }
  842. pr_debug("no socket, dropping\n");
  843. return false;
  844. }
  845. EXPORT_SYMBOL_GPL(ping_rcv);
  846. struct proto ping_prot = {
  847. .name = "PING",
  848. .owner = THIS_MODULE,
  849. .init = ping_init_sock,
  850. .close = ping_close,
  851. .connect = ip4_datagram_connect,
  852. .disconnect = __udp_disconnect,
  853. .setsockopt = ip_setsockopt,
  854. .getsockopt = ip_getsockopt,
  855. .sendmsg = ping_v4_sendmsg,
  856. .recvmsg = ping_recvmsg,
  857. .bind = ping_bind,
  858. .backlog_rcv = ping_queue_rcv_skb,
  859. .release_cb = ip4_datagram_release_cb,
  860. .hash = ping_hash,
  861. .unhash = ping_unhash,
  862. .get_port = ping_get_port,
  863. .obj_size = sizeof(struct inet_sock),
  864. };
  865. EXPORT_SYMBOL(ping_prot);
  866. #ifdef CONFIG_PROC_FS
  867. static struct sock *ping_get_first(struct seq_file *seq, int start)
  868. {
  869. struct sock *sk;
  870. struct ping_iter_state *state = seq->private;
  871. struct net *net = seq_file_net(seq);
  872. for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
  873. ++state->bucket) {
  874. struct hlist_nulls_node *node;
  875. struct hlist_nulls_head *hslot;
  876. hslot = &ping_table.hash[state->bucket];
  877. if (hlist_nulls_empty(hslot))
  878. continue;
  879. sk_nulls_for_each(sk, node, hslot) {
  880. if (net_eq(sock_net(sk), net) &&
  881. sk->sk_family == state->family)
  882. goto found;
  883. }
  884. }
  885. sk = NULL;
  886. found:
  887. return sk;
  888. }
  889. static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
  890. {
  891. struct ping_iter_state *state = seq->private;
  892. struct net *net = seq_file_net(seq);
  893. do {
  894. sk = sk_nulls_next(sk);
  895. } while (sk && (!net_eq(sock_net(sk), net)));
  896. if (!sk)
  897. return ping_get_first(seq, state->bucket + 1);
  898. return sk;
  899. }
  900. static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
  901. {
  902. struct sock *sk = ping_get_first(seq, 0);
  903. if (sk)
  904. while (pos && (sk = ping_get_next(seq, sk)) != NULL)
  905. --pos;
  906. return pos ? NULL : sk;
  907. }
  908. void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
  909. __acquires(ping_table.lock)
  910. {
  911. struct ping_iter_state *state = seq->private;
  912. state->bucket = 0;
  913. state->family = family;
  914. read_lock_bh(&ping_table.lock);
  915. return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
  916. }
  917. EXPORT_SYMBOL_GPL(ping_seq_start);
  918. static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
  919. {
  920. return ping_seq_start(seq, pos, AF_INET);
  921. }
  922. void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  923. {
  924. struct sock *sk;
  925. if (v == SEQ_START_TOKEN)
  926. sk = ping_get_idx(seq, 0);
  927. else
  928. sk = ping_get_next(seq, v);
  929. ++*pos;
  930. return sk;
  931. }
  932. EXPORT_SYMBOL_GPL(ping_seq_next);
  933. void ping_seq_stop(struct seq_file *seq, void *v)
  934. __releases(ping_table.lock)
  935. {
  936. read_unlock_bh(&ping_table.lock);
  937. }
  938. EXPORT_SYMBOL_GPL(ping_seq_stop);
  939. static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
  940. int bucket)
  941. {
  942. struct inet_sock *inet = inet_sk(sp);
  943. __be32 dest = inet->inet_daddr;
  944. __be32 src = inet->inet_rcv_saddr;
  945. __u16 destp = ntohs(inet->inet_dport);
  946. __u16 srcp = ntohs(inet->inet_sport);
  947. seq_printf(f, "%5d: %08X:%04X %08X:%04X"
  948. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
  949. bucket, src, srcp, dest, destp, sp->sk_state,
  950. sk_wmem_alloc_get(sp),
  951. sk_rmem_alloc_get(sp),
  952. 0, 0L, 0,
  953. from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
  954. 0, sock_i_ino(sp),
  955. refcount_read(&sp->sk_refcnt), sp,
  956. atomic_read(&sp->sk_drops));
  957. }
  958. static int ping_v4_seq_show(struct seq_file *seq, void *v)
  959. {
  960. seq_setwidth(seq, 127);
  961. if (v == SEQ_START_TOKEN)
  962. seq_puts(seq, " sl local_address rem_address st tx_queue "
  963. "rx_queue tr tm->when retrnsmt uid timeout "
  964. "inode ref pointer drops");
  965. else {
  966. struct ping_iter_state *state = seq->private;
  967. ping_v4_format_sock(v, seq, state->bucket);
  968. }
  969. seq_pad(seq, '\n');
  970. return 0;
  971. }
  972. static const struct seq_operations ping_v4_seq_ops = {
  973. .start = ping_v4_seq_start,
  974. .show = ping_v4_seq_show,
  975. .next = ping_seq_next,
  976. .stop = ping_seq_stop,
  977. };
  978. static int __net_init ping_v4_proc_init_net(struct net *net)
  979. {
  980. if (!proc_create_net("icmp", 0444, net->proc_net, &ping_v4_seq_ops,
  981. sizeof(struct ping_iter_state)))
  982. return -ENOMEM;
  983. return 0;
  984. }
  985. static void __net_exit ping_v4_proc_exit_net(struct net *net)
  986. {
  987. remove_proc_entry("icmp", net->proc_net);
  988. }
  989. static struct pernet_operations ping_v4_net_ops = {
  990. .init = ping_v4_proc_init_net,
  991. .exit = ping_v4_proc_exit_net,
  992. };
  993. int __init ping_proc_init(void)
  994. {
  995. return register_pernet_subsys(&ping_v4_net_ops);
  996. }
  997. void ping_proc_exit(void)
  998. {
  999. unregister_pernet_subsys(&ping_v4_net_ops);
  1000. }
  1001. #endif
  1002. void __init ping_init(void)
  1003. {
  1004. int i;
  1005. for (i = 0; i < PING_HTABLE_SIZE; i++)
  1006. INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
  1007. rwlock_init(&ping_table.lock);
  1008. }