ping.c 29 KB

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