ping.c 28 KB

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