ping.c 29 KB

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