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