ping.c 28 KB

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