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