ping.c 29 KB

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