blob: 0d2f13a9f81135c0d5a76bd66cfb82001ae9a8f0 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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/ipv6.h>
37#include <net/icmp.h>
38#include <net/protocol.h>
39#include <linux/skbuff.h>
40#include <linux/proc_fs.h>
41#include <linux/export.h>
42#include <net/sock.h>
43#include <net/ping.h>
44#include <net/udp.h>
45#include <net/route.h>
46#include <net/inet_common.h>
47#include <net/checksum.h>
48
49
50static struct ping_table ping_table;
51
52static u16 ping_port_rover;
53
54static inline int ping_hashfn(struct net *net, unsigned num, unsigned mask)
55{
56 int res = (num + net_hash_mix(net)) & mask;
57 pr_debug("hash(%d) = %d\n", num, res);
58 return res;
59}
60
61static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
62 struct net *net, unsigned num)
63{
64 return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
65}
66
67static int ping_v4_get_port(struct sock *sk, unsigned short ident)
68{
69 struct hlist_nulls_node *node;
70 struct hlist_nulls_head *hlist;
71 struct inet_sock *isk, *isk2;
72 struct sock *sk2 = NULL;
73
74 isk = inet_sk(sk);
75 write_lock_bh(&ping_table.lock);
76 if (ident == 0) {
77 u32 i;
78 u16 result = ping_port_rover + 1;
79
80 for (i = 0; i < (1L << 16); i++, result++) {
81 if (!result)
82 result++; /* avoid zero */
83 hlist = ping_hashslot(&ping_table, sock_net(sk),
84 result);
85 ping_portaddr_for_each_entry(sk2, node, hlist) {
86 isk2 = inet_sk(sk2);
87
88 if (isk2->inet_num == result)
89 goto next_port;
90 }
91
92 /* found */
93 ping_port_rover = ident = result;
94 break;
95next_port:
96 ;
97 }
98 if (i >= (1L << 16))
99 goto fail;
100 } else {
101 hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
102 ping_portaddr_for_each_entry(sk2, node, hlist) {
103 isk2 = inet_sk(sk2);
104
105 if ((isk2->inet_num == ident) &&
106 (sk2 != sk) &&
107 (!sk2->sk_reuse || !sk->sk_reuse))
108 goto fail;
109 }
110 }
111
112 pr_debug("found port/ident = %d\n", ident);
113 isk->inet_num = ident;
114 if (sk_unhashed(sk)) {
115 pr_debug("was not hashed\n");
116 sock_hold(sk);
117 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
118 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
119 }
120 write_unlock_bh(&ping_table.lock);
121 return 0;
122
123fail:
124 write_unlock_bh(&ping_table.lock);
125 return 1;
126}
127
128static void ping_v4_hash(struct sock *sk)
129{
130 pr_debug("ping_v4_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
131 BUG(); /* "Please do not press this button again." */
132}
133
134static void ping_v4_unhash(struct sock *sk)
135{
136 struct inet_sock *isk = inet_sk(sk);
137 pr_debug("ping_v4_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
xf.li6c8fc1e2023-08-12 00:11:09 -0700138 //BDSA-2017-0084(CVE-2017-2671)
139 write_lock_bh(&ping_table.lock);
lh9ed821d2023-04-07 01:36:19 -0700140 if (sk_hashed(sk)) {
lh9ed821d2023-04-07 01:36:19 -0700141 hlist_nulls_del(&sk->sk_nulls_node);
142 sk_nulls_node_init(&sk->sk_nulls_node);
143 sock_put(sk);
144 isk->inet_num = 0;
145 isk->inet_sport = 0;
146 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
lh9ed821d2023-04-07 01:36:19 -0700147 }
xf.li6c8fc1e2023-08-12 00:11:09 -0700148 write_unlock_bh(&ping_table.lock);
lh9ed821d2023-04-07 01:36:19 -0700149}
150
151static struct sock *ping_v4_lookup(struct net *net, __be32 saddr, __be32 daddr,
152 u16 ident, int dif)
153{
154 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
155 struct sock *sk = NULL;
156 struct inet_sock *isk;
157 struct hlist_nulls_node *hnode;
158
159 pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
160 (int)ident, &daddr, dif);
161 read_lock_bh(&ping_table.lock);
162
163 ping_portaddr_for_each_entry(sk, hnode, hslot) {
164 isk = inet_sk(sk);
165
166 pr_debug("found: %p: num = %d, daddr = %pI4, dif = %d\n", sk,
167 (int)isk->inet_num, &isk->inet_rcv_saddr,
168 sk->sk_bound_dev_if);
169
170 pr_debug("iterate\n");
171 if (isk->inet_num != ident)
172 continue;
173 if (isk->inet_rcv_saddr && isk->inet_rcv_saddr != daddr)
174 continue;
175 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
176 continue;
177
178 sock_hold(sk);
179 goto exit;
180 }
181
182 sk = NULL;
183exit:
184 read_unlock_bh(&ping_table.lock);
185
186 return sk;
187}
188
189static void inet_get_ping_group_range_net(struct net *net, gid_t *low,
190 gid_t *high)
191{
192 gid_t *data = net->ipv4.sysctl_ping_group_range;
193 unsigned seq;
194 do {
195 seq = read_seqbegin(&sysctl_local_ports.lock);
196
197 *low = data[0];
198 *high = data[1];
199 } while (read_seqretry(&sysctl_local_ports.lock, seq));
200}
201
202
203static int ping_init_sock(struct sock *sk)
204{
205 struct net *net = sock_net(sk);
206 gid_t group = current_egid();
207 gid_t range[2];
208 struct group_info *group_info;
209 int i, j, count;
210 int ret = 0;
211
212 inet_get_ping_group_range_net(net, range, range+1);
213 if (range[0] <= group && group <= range[1])
214 return 0;
215
216 group_info = get_current_groups();
217 count = group_info->ngroups;
218 for (i = 0; i < group_info->nblocks; i++) {
219 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
220
221 for (j = 0; j < cp_count; j++) {
222 group = group_info->blocks[i][j];
223 if (range[0] <= group && group <= range[1])
224 goto out_release_group;
225 }
226
227 count -= cp_count;
228 }
229
230 ret = -EACCES;
231
232out_release_group:
233 put_group_info(group_info);
234 return ret;
235}
236
237static void ping_close(struct sock *sk, long timeout)
238{
239 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
240 inet_sk(sk), inet_sk(sk)->inet_num);
241 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
242
243 sk_common_release(sk);
244}
245
246/*
247 * We need our own bind because there are no privileged id's == local ports.
248 * Moreover, we don't allow binding to multi- and broadcast addresses.
249 */
250
251static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
252{
253 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
254 struct inet_sock *isk = inet_sk(sk);
255 unsigned short snum;
256 int chk_addr_ret;
257 int err;
258
259 if (addr_len < sizeof(struct sockaddr_in))
260 return -EINVAL;
261
262 pr_debug("ping_v4_bind(sk=%p,sa_addr=%08x,sa_port=%d)\n",
263 sk, addr->sin_addr.s_addr, ntohs(addr->sin_port));
264
265 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
266 if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
267 chk_addr_ret = RTN_LOCAL;
268
269 if ((sysctl_ip_nonlocal_bind == 0 &&
270 isk->freebind == 0 && isk->transparent == 0 &&
271 chk_addr_ret != RTN_LOCAL) ||
272 chk_addr_ret == RTN_MULTICAST ||
273 chk_addr_ret == RTN_BROADCAST)
274 return -EADDRNOTAVAIL;
275
276 lock_sock(sk);
277
278 err = -EINVAL;
279 if (isk->inet_num != 0)
280 goto out;
281
282 err = -EADDRINUSE;
283 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
284 snum = ntohs(addr->sin_port);
285 if (ping_v4_get_port(sk, snum) != 0) {
286 isk->inet_saddr = isk->inet_rcv_saddr = 0;
287 goto out;
288 }
289
290 pr_debug("after bind(): num = %d, daddr = %pI4, dif = %d\n",
291 (int)isk->inet_num,
292 &isk->inet_rcv_saddr,
293 (int)sk->sk_bound_dev_if);
294
295 err = 0;
296 if (isk->inet_rcv_saddr)
297 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
298 if (snum)
299 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
300 isk->inet_sport = htons(isk->inet_num);
301 isk->inet_daddr = 0;
302 isk->inet_dport = 0;
303 sk_dst_reset(sk);
304out:
305 release_sock(sk);
306 pr_debug("ping_v4_bind -> %d\n", err);
307 return err;
308}
309
310/*
311 * Is this a supported type of ICMP message?
312 */
313
314static inline int ping_supported(int type, int code)
315{
316 if (type == ICMP_ECHO && code == 0)
317 return 1;
318 return 0;
319}
320
321/*
322 * This routine is called by the ICMP module when it gets some
323 * sort of error condition.
324 */
325
326static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
327
328void ping_err(struct sk_buff *skb, u32 info)
329{
330 struct iphdr *iph = (struct iphdr *)skb->data;
331 struct icmphdr *icmph = (struct icmphdr *)(skb->data+(iph->ihl<<2));
332 struct inet_sock *inet_sock;
333 int type = icmp_hdr(skb)->type;
334 int code = icmp_hdr(skb)->code;
335 struct net *net = dev_net(skb->dev);
336 struct sock *sk;
337 int harderr;
338 int err;
339
340 /* We assume the packet has already been checked by icmp_unreach */
341
342 if (!ping_supported(icmph->type, icmph->code))
343 return;
344
345 pr_debug("ping_err(type=%04x,code=%04x,id=%04x,seq=%04x)\n", type,
346 code, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
347
348 sk = ping_v4_lookup(net, iph->daddr, iph->saddr,
349 ntohs(icmph->un.echo.id), skb->dev->ifindex);
350 if (sk == NULL) {
351 pr_debug("no socket, dropping\n");
352 return; /* No socket for error */
353 }
354 pr_debug("err on socket %p\n", sk);
355
356 err = 0;
357 harderr = 0;
358 inet_sock = inet_sk(sk);
359
360 switch (type) {
361 default:
362 case ICMP_TIME_EXCEEDED:
363 err = EHOSTUNREACH;
364 break;
365 case ICMP_SOURCE_QUENCH:
366 /* This is not a real error but ping wants to see it.
367 * Report it with some fake errno. */
368 err = EREMOTEIO;
369 break;
370 case ICMP_PARAMETERPROB:
371 err = EPROTO;
372 harderr = 1;
373 break;
374 case ICMP_DEST_UNREACH:
375 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
376 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
377 err = EMSGSIZE;
378 harderr = 1;
379 break;
380 }
381 goto out;
382 }
383 err = EHOSTUNREACH;
384 if (code <= NR_ICMP_UNREACH) {
385 harderr = icmp_err_convert[code].fatal;
386 err = icmp_err_convert[code].errno;
387 }
388 break;
389 case ICMP_REDIRECT:
390 /* See ICMP_SOURCE_QUENCH */
391 err = EREMOTEIO;
392 break;
393 }
394
395 /*
396 * RFC1122: OK. Passes ICMP errors back to application, as per
397 * 4.1.3.3.
398 */
399 if (!inet_sock->recverr) {
400 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
401 goto out;
402 } else {
403 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
404 info, (u8 *)icmph);
405 }
406 sk->sk_err = err;
407 sk->sk_error_report(sk);
408out:
409 sock_put(sk);
410}
411
412/*
413 * Copy and checksum an ICMP Echo packet from user space into a buffer.
414 */
415
416struct pingfakehdr {
417 struct icmphdr icmph;
418 struct iovec *iov;
419 __wsum wcheck;
420};
421
422static int ping_getfrag(void *from, char * to,
423 int offset, int fraglen, int odd, struct sk_buff *skb)
424{
425 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
426
427 if (offset == 0) {
428 if (fraglen < sizeof(struct icmphdr))
429 BUG();
430 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
431 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
432 &pfh->wcheck))
433 return -EFAULT;
434
435 return 0;
436 }
437 if (offset < sizeof(struct icmphdr))
438 BUG();
439 if (csum_partial_copy_fromiovecend
440 (to, pfh->iov, offset - sizeof(struct icmphdr),
441 fraglen, &pfh->wcheck))
442 return -EFAULT;
443 return 0;
444}
445
446static int ping_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
447 struct flowi4 *fl4)
448{
449 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
450
451 pfh->wcheck = csum_partial((char *)&pfh->icmph,
452 sizeof(struct icmphdr), pfh->wcheck);
453 pfh->icmph.checksum = csum_fold(pfh->wcheck);
454 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
455 skb->ip_summed = CHECKSUM_NONE;
456 return ip_push_pending_frames(sk, fl4);
457}
458
459static int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
460 size_t len)
461{
462 struct net *net = sock_net(sk);
463 struct flowi4 fl4;
464 struct inet_sock *inet = inet_sk(sk);
465 struct ipcm_cookie ipc;
466 struct icmphdr user_icmph;
467 struct pingfakehdr pfh;
468 struct rtable *rt = NULL;
469 struct ip_options_data opt_copy;
470 int free = 0;
471 __be32 saddr, daddr, faddr;
472 u8 tos;
473 int err;
474
475 pr_debug("ping_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
476
477
478 if (len > 0xFFFF)
479 return -EMSGSIZE;
480
481 /*
482 * Check the flags.
483 */
484
485 /* Mirror BSD error message compatibility */
486 if (msg->msg_flags & MSG_OOB)
487 return -EOPNOTSUPP;
488
489 /*
490 * Fetch the ICMP header provided by the userland.
491 * iovec is modified!
492 */
493
494 if (memcpy_fromiovec((u8 *)&user_icmph, msg->msg_iov,
495 sizeof(struct icmphdr)))
496 return -EFAULT;
497 if (!ping_supported(user_icmph.type, user_icmph.code))
498 return -EINVAL;
499
500 /*
501 * Get and verify the address.
502 */
503
504 if (msg->msg_name) {
505 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
506 if (msg->msg_namelen < sizeof(*usin))
507 return -EINVAL;
508 if (usin->sin_family != AF_INET)
509 return -EINVAL;
510 daddr = usin->sin_addr.s_addr;
511 /* no remote port */
512 } else {
513 if (sk->sk_state != TCP_ESTABLISHED)
514 return -EDESTADDRREQ;
515 daddr = inet->inet_daddr;
516 /* no remote port */
517 }
518
519 ipc.addr = inet->inet_saddr;
520 ipc.opt = NULL;
521 ipc.oif = sk->sk_bound_dev_if;
522 ipc.tx_flags = 0;
523 err = sock_tx_timestamp(sk, &ipc.tx_flags);
524 if (err)
525 return err;
526
527 if (msg->msg_controllen) {
528 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
529 if (err)
530 return err;
531 if (ipc.opt)
532 free = 1;
533 }
534 if (!ipc.opt) {
535 struct ip_options_rcu *inet_opt;
536
537 rcu_read_lock();
538 inet_opt = rcu_dereference(inet->inet_opt);
539 if (inet_opt) {
540 memcpy(&opt_copy, inet_opt,
541 sizeof(*inet_opt) + inet_opt->opt.optlen);
542 ipc.opt = &opt_copy.opt;
543 }
544 rcu_read_unlock();
545 }
546
547 saddr = ipc.addr;
548 ipc.addr = faddr = daddr;
549
550 if (ipc.opt && ipc.opt->opt.srr) {
551 if (!daddr)
552 return -EINVAL;
553 faddr = ipc.opt->opt.faddr;
554 }
555 tos = RT_TOS(inet->tos);
556 if (sock_flag(sk, SOCK_LOCALROUTE) ||
557 (msg->msg_flags & MSG_DONTROUTE) ||
558 (ipc.opt && ipc.opt->opt.is_strictroute)) {
559 tos |= RTO_ONLINK;
560 }
561
562 if (ipv4_is_multicast(daddr)) {
563 if (!ipc.oif)
564 ipc.oif = inet->mc_index;
565 if (!saddr)
566 saddr = inet->mc_addr;
567 } else if (!ipc.oif)
568 ipc.oif = inet->uc_index;
569
570 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
571 RT_SCOPE_UNIVERSE, sk->sk_protocol,
572 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
573
574 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
575 rt = ip_route_output_flow(net, &fl4, sk);
576 if (IS_ERR(rt)) {
577 err = PTR_ERR(rt);
578 rt = NULL;
579 if (err == -ENETUNREACH)
580 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
581 goto out;
582 }
583
584 err = -EACCES;
585 if ((rt->rt_flags & RTCF_BROADCAST) &&
586 !sock_flag(sk, SOCK_BROADCAST))
587 goto out;
588
589 if (msg->msg_flags & MSG_CONFIRM)
590 goto do_confirm;
591back_from_confirm:
592
593 if (!ipc.addr)
594 ipc.addr = fl4.daddr;
595
596 lock_sock(sk);
597
598 pfh.icmph.type = user_icmph.type; /* already checked */
599 pfh.icmph.code = user_icmph.code; /* ditto */
600 pfh.icmph.checksum = 0;
601 pfh.icmph.un.echo.id = inet->inet_sport;
602 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
603 pfh.iov = msg->msg_iov;
604 pfh.wcheck = 0;
605
606 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
607 0, &ipc, &rt, msg->msg_flags);
608 if (err)
609 ip_flush_pending_frames(sk);
610 else
611 err = ping_push_pending_frames(sk, &pfh, &fl4);
612 release_sock(sk);
613
614out:
615 ip_rt_put(rt);
616 if (free)
617 kfree(ipc.opt);
618 if (!err) {
619 icmp_out_count(sock_net(sk), user_icmph.type);
620 return len;
621 }
622 return err;
623
624do_confirm:
625 dst_confirm(&rt->dst);
626 if (!(msg->msg_flags & MSG_PROBE) || len)
627 goto back_from_confirm;
628 err = 0;
629 goto out;
630}
631
632static int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
633 size_t len, int noblock, int flags, int *addr_len)
634{
635 struct inet_sock *isk = inet_sk(sk);
636 struct sk_buff *skb;
637 int copied, err;
638
639 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
640
641 err = -EOPNOTSUPP;
642 if (flags & MSG_OOB)
643 goto out;
644
645 if (flags & MSG_ERRQUEUE)
646 return ip_recv_error(sk, msg, len, addr_len);
647
648 skb = skb_recv_datagram(sk, flags, noblock, &err);
649 if (!skb)
650 goto out;
651
652 copied = skb->len;
653 if (copied > len) {
654 msg->msg_flags |= MSG_TRUNC;
655 copied = len;
656 }
657
658 /* Don't bother checking the checksum */
659 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
660 if (err)
661 goto done;
662
663 sock_recv_timestamp(msg, sk, skb);
664
665 /* Copy the address. */
666 if (msg->msg_name) {
667 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
668
lh758261d2023-07-13 05:52:04 -0700669 if (sin) { //CVE-2013-6432
670 sin->sin_family = AF_INET;
671 sin->sin_port = 0 /* skb->h.uh->source */;
672 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
673 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
674 *addr_len = sizeof(*sin);
675 }
lh9ed821d2023-04-07 01:36:19 -0700676 }
677 if (isk->cmsg_flags)
678 ip_cmsg_recv(msg, skb);
679 err = copied;
680
681done:
682 skb_free_datagram(sk, skb);
683out:
684 pr_debug("ping_recvmsg -> %d\n", err);
685 return err;
686}
687
688static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
689{
690 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
691 inet_sk(sk), inet_sk(sk)->inet_num, skb);
692 if (sock_queue_rcv_skb(sk, skb) < 0) {
693 kfree_skb(skb);
694 pr_debug("ping_queue_rcv_skb -> failed\n");
695 return -1;
696 }
697 return 0;
698}
699
700
701/*
702 * All we need to do is get the socket.
703 */
704
705void ping_rcv(struct sk_buff *skb)
706{
707 struct sock *sk;
708 struct net *net = dev_net(skb->dev);
709 struct iphdr *iph = ip_hdr(skb);
710 struct icmphdr *icmph = icmp_hdr(skb);
711 __be32 saddr = iph->saddr;
712 __be32 daddr = iph->daddr;
713
714 /* We assume the packet has already been checked by icmp_rcv */
715
716 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
717 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
718
719 /* Push ICMP header back */
720 skb_push(skb, skb->data - (u8 *)icmph);
721
722 sk = ping_v4_lookup(net, saddr, daddr, ntohs(icmph->un.echo.id),
723 skb->dev->ifindex);
724 if (sk != NULL) {
725 pr_debug("rcv on socket %p\n", sk);
726 ping_queue_rcv_skb(sk, skb_get(skb));
727 sock_put(sk);
728 return;
729 }
730 pr_debug("no socket, dropping\n");
731
732 /* We're called from icmp_rcv(). kfree_skb() is done there. */
733}
734
735struct proto ping_prot = {
736 .name = "PING",
737 .owner = THIS_MODULE,
738 .init = ping_init_sock,
739 .close = ping_close,
740 .connect = ip4_datagram_connect,
741 .disconnect = udp_disconnect,
742 .setsockopt = ip_setsockopt,
743 .getsockopt = ip_getsockopt,
744 .sendmsg = ping_sendmsg,
745 .recvmsg = ping_recvmsg,
746 .bind = ping_bind,
747 .backlog_rcv = ping_queue_rcv_skb,
748 .hash = ping_v4_hash,
749 .unhash = ping_v4_unhash,
750 .get_port = ping_v4_get_port,
751 .obj_size = sizeof(struct inet_sock),
752};
753EXPORT_SYMBOL(ping_prot);
754
755#ifdef CONFIG_PROC_FS
756
757static struct sock *ping_get_first(struct seq_file *seq, int start)
758{
759 struct sock *sk;
760 struct ping_iter_state *state = seq->private;
761 struct net *net = seq_file_net(seq);
762
763 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
764 ++state->bucket) {
765 struct hlist_nulls_node *node;
766 struct hlist_nulls_head *hslot;
767
768 hslot = &ping_table.hash[state->bucket];
769
770 if (hlist_nulls_empty(hslot))
771 continue;
772
773 sk_nulls_for_each(sk, node, hslot) {
774 if (net_eq(sock_net(sk), net))
775 goto found;
776 }
777 }
778 sk = NULL;
779found:
780 return sk;
781}
782
783static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
784{
785 struct ping_iter_state *state = seq->private;
786 struct net *net = seq_file_net(seq);
787
788 do {
789 sk = sk_nulls_next(sk);
790 } while (sk && (!net_eq(sock_net(sk), net)));
791
792 if (!sk)
793 return ping_get_first(seq, state->bucket + 1);
794 return sk;
795}
796
797static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
798{
799 struct sock *sk = ping_get_first(seq, 0);
800
801 if (sk)
802 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
803 --pos;
804 return pos ? NULL : sk;
805}
806
807static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
808{
809 struct ping_iter_state *state = seq->private;
810 state->bucket = 0;
811
812 read_lock_bh(&ping_table.lock);
813
814 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
815}
816
817static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
818{
819 struct sock *sk;
820
821 if (v == SEQ_START_TOKEN)
822 sk = ping_get_idx(seq, 0);
823 else
824 sk = ping_get_next(seq, v);
825
826 ++*pos;
827 return sk;
828}
829
830static void ping_seq_stop(struct seq_file *seq, void *v)
831{
832 read_unlock_bh(&ping_table.lock);
833}
834
835static void ping_format_sock(struct sock *sp, struct seq_file *f,
836 int bucket, int *len)
837{
838 struct inet_sock *inet = inet_sk(sp);
839 __be32 dest = inet->inet_daddr;
840 __be32 src = inet->inet_rcv_saddr;
841 __u16 destp = ntohs(inet->inet_dport);
842 __u16 srcp = ntohs(inet->inet_sport);
843
844 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
845 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
846 bucket, src, srcp, dest, destp, sp->sk_state,
847 sk_wmem_alloc_get(sp),
848 sk_rmem_alloc_get(sp),
849 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
850 atomic_read(&sp->sk_refcnt), sp,
851 atomic_read(&sp->sk_drops), len);
852}
853
854static int ping_seq_show(struct seq_file *seq, void *v)
855{
856 if (v == SEQ_START_TOKEN)
857 seq_printf(seq, "%-127s\n",
858 " sl local_address rem_address st tx_queue "
859 "rx_queue tr tm->when retrnsmt uid timeout "
860 "inode ref pointer drops");
861 else {
862 struct ping_iter_state *state = seq->private;
863 int len;
864
865 ping_format_sock(v, seq, state->bucket, &len);
866 seq_printf(seq, "%*s\n", 127 - len, "");
867 }
868 return 0;
869}
870
871static const struct seq_operations ping_seq_ops = {
872 .show = ping_seq_show,
873 .start = ping_seq_start,
874 .next = ping_seq_next,
875 .stop = ping_seq_stop,
876};
877
878static int ping_seq_open(struct inode *inode, struct file *file)
879{
880 return seq_open_net(inode, file, &ping_seq_ops,
881 sizeof(struct ping_iter_state));
882}
883
884static const struct file_operations ping_seq_fops = {
885 .open = ping_seq_open,
886 .read = seq_read,
887 .llseek = seq_lseek,
888 .release = seq_release_net,
889};
890
891static int ping_proc_register(struct net *net)
892{
893 struct proc_dir_entry *p;
894 int rc = 0;
895
896 p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
897 if (!p)
898 rc = -ENOMEM;
899 return rc;
900}
901
902static void ping_proc_unregister(struct net *net)
903{
904 proc_net_remove(net, "icmp");
905}
906
907
908static int __net_init ping_proc_init_net(struct net *net)
909{
910 return ping_proc_register(net);
911}
912
913static void __net_exit ping_proc_exit_net(struct net *net)
914{
915 ping_proc_unregister(net);
916}
917
918static struct pernet_operations ping_net_ops = {
919 .init = ping_proc_init_net,
920 .exit = ping_proc_exit_net,
921};
922
923int __init ping_proc_init(void)
924{
925 return register_pernet_subsys(&ping_net_ops);
926}
927
928void ping_proc_exit(void)
929{
930 unregister_pernet_subsys(&ping_net_ops);
931}
932
933#endif
934
935void __init ping_init(void)
936{
937 int i;
938
939 for (i = 0; i < PING_HTABLE_SIZE; i++)
940 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
941 rwlock_init(&ping_table.lock);
942}