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.
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.
13 * Based on ipv4/udp.c code.
15 * Authors: Vasiliy Kulikov / Openwall (for Linux 2.6),
16 * Pavel Kankovsky (for Linux 2.4.32)
18 * Pavel gave all rights to bugs to Vasiliy,
19 * none of the bugs are Pavel's now.
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>
29 #include <linux/errno.h>
30 #include <linux/timer.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
37 #include <net/protocol.h>
38 #include <linux/skbuff.h>
39 #include <linux/proc_fs.h>
40 #include <linux/export.h>
44 #include <net/route.h>
45 #include <net/inet_common.h>
46 #include <net/checksum.h>
48 #if IS_ENABLED(CONFIG_IPV6)
49 #include <linux/in6.h>
50 #include <linux/icmpv6.h>
51 #include <net/addrconf.h>
53 #include <net/transp_v6.h>
57 struct hlist_nulls_head hash[PING_HTABLE_SIZE];
61 static struct ping_table ping_table;
62 struct pingv6_ops pingv6_ops;
63 EXPORT_SYMBOL_GPL(pingv6_ops);
65 static u16 ping_port_rover;
67 static inline int ping_hashfn(struct net *net, unsigned int num, unsigned int mask)
69 int res = (num + net_hash_mix(net)) & mask;
71 pr_debug("hash(%d) = %d\n", num, res);
74 EXPORT_SYMBOL_GPL(ping_hash);
76 static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
77 struct net *net, unsigned int num)
79 return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
82 int ping_get_port(struct sock *sk, unsigned short ident)
84 struct hlist_nulls_node *node;
85 struct hlist_nulls_head *hlist;
86 struct inet_sock *isk, *isk2;
87 struct sock *sk2 = NULL;
90 write_lock_bh(&ping_table.lock);
93 u16 result = ping_port_rover + 1;
95 for (i = 0; i < (1L << 16); i++, result++) {
97 result++; /* avoid zero */
98 hlist = ping_hashslot(&ping_table, sock_net(sk),
100 ping_portaddr_for_each_entry(sk2, node, hlist) {
103 if (isk2->inet_num == result)
108 ping_port_rover = ident = result;
116 hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
117 ping_portaddr_for_each_entry(sk2, node, hlist) {
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.
124 if ((isk2->inet_num == ident) &&
126 (!sk2->sk_reuse || !sk->sk_reuse))
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");
136 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
137 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
139 write_unlock_bh(&ping_table.lock);
143 write_unlock_bh(&ping_table.lock);
146 EXPORT_SYMBOL_GPL(ping_get_port);
148 void ping_hash(struct sock *sk)
150 pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
151 BUG(); /* "Please do not press this button again." */
154 void ping_unhash(struct sock *sk)
156 struct inet_sock *isk = inet_sk(sk);
157 pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
159 write_lock_bh(&ping_table.lock);
160 hlist_nulls_del(&sk->sk_nulls_node);
164 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
165 write_unlock_bh(&ping_table.lock);
168 EXPORT_SYMBOL_GPL(ping_unhash);
170 static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
172 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
173 struct sock *sk = NULL;
174 struct inet_sock *isk;
175 struct hlist_nulls_node *hnode;
176 int dif = skb->dev->ifindex;
178 if (skb->protocol == htons(ETH_P_IP)) {
179 pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
180 (int)ident, &ip_hdr(skb)->daddr, dif);
181 #if IS_ENABLED(CONFIG_IPV6)
182 } else if (skb->protocol == htons(ETH_P_IPV6)) {
183 pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
184 (int)ident, &ipv6_hdr(skb)->daddr, dif);
188 read_lock_bh(&ping_table.lock);
190 ping_portaddr_for_each_entry(sk, hnode, hslot) {
193 pr_debug("iterate\n");
194 if (isk->inet_num != ident)
197 if (skb->protocol == htons(ETH_P_IP) &&
198 sk->sk_family == AF_INET) {
199 pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
200 (int) isk->inet_num, &isk->inet_rcv_saddr,
201 sk->sk_bound_dev_if);
203 if (isk->inet_rcv_saddr &&
204 isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
206 #if IS_ENABLED(CONFIG_IPV6)
207 } else if (skb->protocol == htons(ETH_P_IPV6) &&
208 sk->sk_family == AF_INET6) {
210 pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
212 &sk->sk_v6_rcv_saddr,
213 sk->sk_bound_dev_if);
215 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
216 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr,
217 &ipv6_hdr(skb)->daddr))
222 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
231 read_unlock_bh(&ping_table.lock);
236 static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
239 kgid_t *data = net->ipv4.sysctl_ping_group_range;
243 seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock);
247 } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq));
251 int ping_init_sock(struct sock *sk)
253 struct net *net = sock_net(sk);
254 kgid_t group = current_egid();
255 struct group_info *group_info = get_current_groups();
256 int i, j, count = group_info->ngroups;
259 inet_get_ping_group_range_net(net, &low, &high);
260 if (gid_lte(low, group) && gid_lte(group, high))
263 for (i = 0; i < group_info->nblocks; i++) {
264 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
265 for (j = 0; j < cp_count; j++) {
266 kgid_t gid = group_info->blocks[i][j];
267 if (gid_lte(low, gid) && gid_lte(gid, high))
276 EXPORT_SYMBOL_GPL(ping_init_sock);
278 void ping_close(struct sock *sk, long timeout)
280 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
281 inet_sk(sk), inet_sk(sk)->inet_num);
282 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
284 sk_common_release(sk);
286 EXPORT_SYMBOL_GPL(ping_close);
288 /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
289 static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
290 struct sockaddr *uaddr, int addr_len) {
291 struct net *net = sock_net(sk);
292 if (sk->sk_family == AF_INET) {
293 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
296 if (addr_len < sizeof(*addr))
299 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
300 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
302 chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
304 if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
305 chk_addr_ret = RTN_LOCAL;
307 if ((sysctl_ip_nonlocal_bind == 0 &&
308 isk->freebind == 0 && isk->transparent == 0 &&
309 chk_addr_ret != RTN_LOCAL) ||
310 chk_addr_ret == RTN_MULTICAST ||
311 chk_addr_ret == RTN_BROADCAST)
312 return -EADDRNOTAVAIL;
314 #if IS_ENABLED(CONFIG_IPV6)
315 } else if (sk->sk_family == AF_INET6) {
316 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
317 int addr_type, scoped, has_addr;
318 struct net_device *dev = NULL;
320 if (addr_len < sizeof(*addr))
323 if (addr->sin6_family != AF_INET6)
326 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
327 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
329 addr_type = ipv6_addr_type(&addr->sin6_addr);
330 scoped = __ipv6_addr_needs_scope_id(addr_type);
331 if ((addr_type != IPV6_ADDR_ANY &&
332 !(addr_type & IPV6_ADDR_UNICAST)) ||
333 (scoped && !addr->sin6_scope_id))
337 if (addr->sin6_scope_id) {
338 dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
344 has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
348 if (!(isk->freebind || isk->transparent || has_addr ||
349 addr_type == IPV6_ADDR_ANY))
350 return -EADDRNOTAVAIL;
353 sk->sk_bound_dev_if = addr->sin6_scope_id;
356 return -EAFNOSUPPORT;
361 static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
363 if (saddr->sa_family == AF_INET) {
364 struct inet_sock *isk = inet_sk(sk);
365 struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
366 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
367 #if IS_ENABLED(CONFIG_IPV6)
368 } else if (saddr->sa_family == AF_INET6) {
369 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
370 struct ipv6_pinfo *np = inet6_sk(sk);
371 sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
376 static void ping_clear_saddr(struct sock *sk, int dif)
378 sk->sk_bound_dev_if = dif;
379 if (sk->sk_family == AF_INET) {
380 struct inet_sock *isk = inet_sk(sk);
381 isk->inet_rcv_saddr = isk->inet_saddr = 0;
382 #if IS_ENABLED(CONFIG_IPV6)
383 } else if (sk->sk_family == AF_INET6) {
384 struct ipv6_pinfo *np = inet6_sk(sk);
385 memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
386 memset(&np->saddr, 0, sizeof(np->saddr));
391 * We need our own bind because there are no privileged id's == local ports.
392 * Moreover, we don't allow binding to multi- and broadcast addresses.
395 int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
397 struct inet_sock *isk = inet_sk(sk);
400 int dif = sk->sk_bound_dev_if;
402 err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
409 if (isk->inet_num != 0)
413 ping_set_saddr(sk, uaddr);
414 snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
415 if (ping_get_port(sk, snum) != 0) {
416 ping_clear_saddr(sk, dif);
420 pr_debug("after bind(): num = %d, dif = %d\n",
422 (int)sk->sk_bound_dev_if);
425 if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
426 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
427 #if IS_ENABLED(CONFIG_IPV6)
428 if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
429 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
433 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
434 isk->inet_sport = htons(isk->inet_num);
438 #if IS_ENABLED(CONFIG_IPV6)
439 if (sk->sk_family == AF_INET6)
440 memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
446 pr_debug("ping_v4_bind -> %d\n", err);
449 EXPORT_SYMBOL_GPL(ping_bind);
452 * Is this a supported type of ICMP message?
455 static inline int ping_supported(int family, int type, int code)
457 return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
458 (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
462 * This routine is called by the ICMP module when it gets some
463 * sort of error condition.
466 void ping_err(struct sk_buff *skb, int offset, u32 info)
469 struct icmphdr *icmph;
470 struct inet_sock *inet_sock;
473 struct net *net = dev_net(skb->dev);
478 if (skb->protocol == htons(ETH_P_IP)) {
480 type = icmp_hdr(skb)->type;
481 code = icmp_hdr(skb)->code;
482 icmph = (struct icmphdr *)(skb->data + offset);
483 } else if (skb->protocol == htons(ETH_P_IPV6)) {
485 type = icmp6_hdr(skb)->icmp6_type;
486 code = icmp6_hdr(skb)->icmp6_code;
487 icmph = (struct icmphdr *) (skb->data + offset);
492 /* We assume the packet has already been checked by icmp_unreach */
494 if (!ping_supported(family, icmph->type, icmph->code))
497 pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
498 skb->protocol, type, code, ntohs(icmph->un.echo.id),
499 ntohs(icmph->un.echo.sequence));
501 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
503 pr_debug("no socket, dropping\n");
504 return; /* No socket for error */
506 pr_debug("err on socket %p\n", sk);
510 inet_sock = inet_sk(sk);
512 if (skb->protocol == htons(ETH_P_IP)) {
515 case ICMP_TIME_EXCEEDED:
518 case ICMP_SOURCE_QUENCH:
519 /* This is not a real error but ping wants to see it.
520 * Report it with some fake errno.
524 case ICMP_PARAMETERPROB:
528 case ICMP_DEST_UNREACH:
529 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
530 ipv4_sk_update_pmtu(skb, sk, info);
531 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
539 if (code <= NR_ICMP_UNREACH) {
540 harderr = icmp_err_convert[code].fatal;
541 err = icmp_err_convert[code].errno;
545 /* See ICMP_SOURCE_QUENCH */
546 ipv4_sk_redirect(skb, sk);
550 #if IS_ENABLED(CONFIG_IPV6)
551 } else if (skb->protocol == htons(ETH_P_IPV6)) {
552 harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
557 * RFC1122: OK. Passes ICMP errors back to application, as per
560 if ((family == AF_INET && !inet_sock->recverr) ||
561 (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
562 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
565 if (family == AF_INET) {
566 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
568 #if IS_ENABLED(CONFIG_IPV6)
569 } else if (family == AF_INET6) {
570 pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
576 sk->sk_error_report(sk);
580 EXPORT_SYMBOL_GPL(ping_err);
583 * Copy and checksum an ICMP Echo packet from user space into a buffer
584 * starting from the payload.
587 int ping_getfrag(void *from, char *to,
588 int offset, int fraglen, int odd, struct sk_buff *skb)
590 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
593 if (fraglen < sizeof(struct icmphdr))
595 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
596 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
599 } else if (offset < sizeof(struct icmphdr)) {
602 if (csum_partial_copy_fromiovecend
603 (to, pfh->iov, offset - sizeof(struct icmphdr),
604 fraglen, &pfh->wcheck))
608 #if IS_ENABLED(CONFIG_IPV6)
609 /* For IPv6, checksum each skb as we go along, as expected by
610 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
611 * wcheck, it will be finalized in ping_v4_push_pending_frames.
613 if (pfh->family == AF_INET6) {
614 skb->csum = pfh->wcheck;
615 skb->ip_summed = CHECKSUM_NONE;
622 EXPORT_SYMBOL_GPL(ping_getfrag);
624 static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
627 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
629 pfh->wcheck = csum_partial((char *)&pfh->icmph,
630 sizeof(struct icmphdr), pfh->wcheck);
631 pfh->icmph.checksum = csum_fold(pfh->wcheck);
632 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
633 skb->ip_summed = CHECKSUM_NONE;
634 return ip_push_pending_frames(sk, fl4);
637 int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
638 void *user_icmph, size_t icmph_len) {
648 /* Mirror BSD error message compatibility */
649 if (msg->msg_flags & MSG_OOB)
653 * Fetch the ICMP header provided by the userland.
654 * iovec is modified! The ICMP header is consumed.
656 if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len))
659 if (family == AF_INET) {
660 type = ((struct icmphdr *) user_icmph)->type;
661 code = ((struct icmphdr *) user_icmph)->code;
662 #if IS_ENABLED(CONFIG_IPV6)
663 } else if (family == AF_INET6) {
664 type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
665 code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
671 if (!ping_supported(family, type, code))
676 EXPORT_SYMBOL_GPL(ping_common_sendmsg);
678 static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
681 struct net *net = sock_net(sk);
683 struct inet_sock *inet = inet_sk(sk);
684 struct ipcm_cookie ipc;
685 struct icmphdr user_icmph;
686 struct pingfakehdr pfh;
687 struct rtable *rt = NULL;
688 struct ip_options_data opt_copy;
690 __be32 saddr, daddr, faddr;
694 pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
696 err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
702 * Get and verify the address.
706 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
707 if (msg->msg_namelen < sizeof(*usin))
709 if (usin->sin_family != AF_INET)
711 daddr = usin->sin_addr.s_addr;
714 if (sk->sk_state != TCP_ESTABLISHED)
715 return -EDESTADDRREQ;
716 daddr = inet->inet_daddr;
720 ipc.addr = inet->inet_saddr;
722 ipc.oif = sk->sk_bound_dev_if;
727 sock_tx_timestamp(sk, &ipc.tx_flags);
729 if (msg->msg_controllen) {
730 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
737 struct ip_options_rcu *inet_opt;
740 inet_opt = rcu_dereference(inet->inet_opt);
742 memcpy(&opt_copy, inet_opt,
743 sizeof(*inet_opt) + inet_opt->opt.optlen);
744 ipc.opt = &opt_copy.opt;
750 ipc.addr = faddr = daddr;
752 if (ipc.opt && ipc.opt->opt.srr) {
755 faddr = ipc.opt->opt.faddr;
757 tos = get_rttos(&ipc, inet);
758 if (sock_flag(sk, SOCK_LOCALROUTE) ||
759 (msg->msg_flags & MSG_DONTROUTE) ||
760 (ipc.opt && ipc.opt->opt.is_strictroute)) {
764 if (ipv4_is_multicast(daddr)) {
766 ipc.oif = inet->mc_index;
768 saddr = inet->mc_addr;
770 ipc.oif = inet->uc_index;
772 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
773 RT_SCOPE_UNIVERSE, sk->sk_protocol,
774 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
776 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
777 rt = ip_route_output_flow(net, &fl4, sk);
781 if (err == -ENETUNREACH)
782 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
787 if ((rt->rt_flags & RTCF_BROADCAST) &&
788 !sock_flag(sk, SOCK_BROADCAST))
791 if (msg->msg_flags & MSG_CONFIRM)
796 ipc.addr = fl4.daddr;
800 pfh.icmph.type = user_icmph.type; /* already checked */
801 pfh.icmph.code = user_icmph.code; /* ditto */
802 pfh.icmph.checksum = 0;
803 pfh.icmph.un.echo.id = inet->inet_sport;
804 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
805 pfh.iov = msg->msg_iov;
807 pfh.family = AF_INET;
809 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
810 0, &ipc, &rt, msg->msg_flags);
812 ip_flush_pending_frames(sk);
814 err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
822 icmp_out_count(sock_net(sk), user_icmph.type);
828 dst_confirm(&rt->dst);
829 if (!(msg->msg_flags & MSG_PROBE) || len)
830 goto back_from_confirm;
835 int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
836 size_t len, int noblock, int flags, int *addr_len)
838 struct inet_sock *isk = inet_sk(sk);
839 int family = sk->sk_family;
843 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
849 if (flags & MSG_ERRQUEUE) {
850 if (family == AF_INET) {
851 return ip_recv_error(sk, msg, len, addr_len);
852 #if IS_ENABLED(CONFIG_IPV6)
853 } else if (family == AF_INET6) {
854 return pingv6_ops.ipv6_recv_error(sk, msg, len,
860 skb = skb_recv_datagram(sk, flags, noblock, &err);
866 msg->msg_flags |= MSG_TRUNC;
870 /* Don't bother checking the checksum */
871 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
875 sock_recv_timestamp(msg, sk, skb);
877 /* Copy the address and add cmsg data. */
878 if (family == AF_INET) {
879 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
882 sin->sin_family = AF_INET;
883 sin->sin_port = 0 /* skb->h.uh->source */;
884 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
885 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
886 *addr_len = sizeof(*sin);
890 ip_cmsg_recv(msg, skb);
892 #if IS_ENABLED(CONFIG_IPV6)
893 } else if (family == AF_INET6) {
894 struct ipv6_pinfo *np = inet6_sk(sk);
895 struct ipv6hdr *ip6 = ipv6_hdr(skb);
896 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
899 sin6->sin6_family = AF_INET6;
901 sin6->sin6_addr = ip6->saddr;
902 sin6->sin6_flowinfo = 0;
904 sin6->sin6_flowinfo = ip6_flowinfo(ip6);
905 sin6->sin6_scope_id =
906 ipv6_iface_scope_id(&sin6->sin6_addr,
908 *addr_len = sizeof(*sin6);
911 if (inet6_sk(sk)->rxopt.all)
912 pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
913 if (skb->protocol == htons(ETH_P_IPV6) &&
914 inet6_sk(sk)->rxopt.all)
915 pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
916 else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
917 ip_cmsg_recv(msg, skb);
926 skb_free_datagram(sk, skb);
928 pr_debug("ping_recvmsg -> %d\n", err);
931 EXPORT_SYMBOL_GPL(ping_recvmsg);
933 int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
935 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
936 inet_sk(sk), inet_sk(sk)->inet_num, skb);
937 if (sock_queue_rcv_skb(sk, skb) < 0) {
939 pr_debug("ping_queue_rcv_skb -> failed\n");
944 EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
948 * All we need to do is get the socket.
951 void ping_rcv(struct sk_buff *skb)
954 struct net *net = dev_net(skb->dev);
955 struct icmphdr *icmph = icmp_hdr(skb);
957 /* We assume the packet has already been checked by icmp_rcv */
959 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
960 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
962 /* Push ICMP header back */
963 skb_push(skb, skb->data - (u8 *)icmph);
965 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
967 pr_debug("rcv on socket %p\n", sk);
968 ping_queue_rcv_skb(sk, skb_get(skb));
972 pr_debug("no socket, dropping\n");
974 /* We're called from icmp_rcv(). kfree_skb() is done there. */
976 EXPORT_SYMBOL_GPL(ping_rcv);
978 struct proto ping_prot = {
980 .owner = THIS_MODULE,
981 .init = ping_init_sock,
983 .connect = ip4_datagram_connect,
984 .disconnect = udp_disconnect,
985 .setsockopt = ip_setsockopt,
986 .getsockopt = ip_getsockopt,
987 .sendmsg = ping_v4_sendmsg,
988 .recvmsg = ping_recvmsg,
990 .backlog_rcv = ping_queue_rcv_skb,
991 .release_cb = ip4_datagram_release_cb,
993 .unhash = ping_unhash,
994 .get_port = ping_get_port,
995 .obj_size = sizeof(struct inet_sock),
997 EXPORT_SYMBOL(ping_prot);
999 #ifdef CONFIG_PROC_FS
1001 static struct sock *ping_get_first(struct seq_file *seq, int start)
1004 struct ping_iter_state *state = seq->private;
1005 struct net *net = seq_file_net(seq);
1007 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
1009 struct hlist_nulls_node *node;
1010 struct hlist_nulls_head *hslot;
1012 hslot = &ping_table.hash[state->bucket];
1014 if (hlist_nulls_empty(hslot))
1017 sk_nulls_for_each(sk, node, hslot) {
1018 if (net_eq(sock_net(sk), net) &&
1019 sk->sk_family == state->family)
1028 static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1030 struct ping_iter_state *state = seq->private;
1031 struct net *net = seq_file_net(seq);
1034 sk = sk_nulls_next(sk);
1035 } while (sk && (!net_eq(sock_net(sk), net)));
1038 return ping_get_first(seq, state->bucket + 1);
1042 static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1044 struct sock *sk = ping_get_first(seq, 0);
1047 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1049 return pos ? NULL : sk;
1052 void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
1054 struct ping_iter_state *state = seq->private;
1056 state->family = family;
1058 read_lock_bh(&ping_table.lock);
1060 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1062 EXPORT_SYMBOL_GPL(ping_seq_start);
1064 static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
1066 return ping_seq_start(seq, pos, AF_INET);
1069 void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1073 if (v == SEQ_START_TOKEN)
1074 sk = ping_get_idx(seq, 0);
1076 sk = ping_get_next(seq, v);
1081 EXPORT_SYMBOL_GPL(ping_seq_next);
1083 void ping_seq_stop(struct seq_file *seq, void *v)
1085 read_unlock_bh(&ping_table.lock);
1087 EXPORT_SYMBOL_GPL(ping_seq_stop);
1089 static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1092 struct inet_sock *inet = inet_sk(sp);
1093 __be32 dest = inet->inet_daddr;
1094 __be32 src = inet->inet_rcv_saddr;
1095 __u16 destp = ntohs(inet->inet_dport);
1096 __u16 srcp = ntohs(inet->inet_sport);
1098 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1099 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
1100 bucket, src, srcp, dest, destp, sp->sk_state,
1101 sk_wmem_alloc_get(sp),
1102 sk_rmem_alloc_get(sp),
1104 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
1106 atomic_read(&sp->sk_refcnt), sp,
1107 atomic_read(&sp->sk_drops));
1110 static int ping_v4_seq_show(struct seq_file *seq, void *v)
1112 seq_setwidth(seq, 127);
1113 if (v == SEQ_START_TOKEN)
1114 seq_puts(seq, " sl local_address rem_address st tx_queue "
1115 "rx_queue tr tm->when retrnsmt uid timeout "
1116 "inode ref pointer drops");
1118 struct ping_iter_state *state = seq->private;
1120 ping_v4_format_sock(v, seq, state->bucket);
1126 static const struct seq_operations ping_v4_seq_ops = {
1127 .show = ping_v4_seq_show,
1128 .start = ping_v4_seq_start,
1129 .next = ping_seq_next,
1130 .stop = ping_seq_stop,
1133 static int ping_seq_open(struct inode *inode, struct file *file)
1135 struct ping_seq_afinfo *afinfo = PDE_DATA(inode);
1136 return seq_open_net(inode, file, &afinfo->seq_ops,
1137 sizeof(struct ping_iter_state));
1140 const struct file_operations ping_seq_fops = {
1141 .open = ping_seq_open,
1143 .llseek = seq_lseek,
1144 .release = seq_release_net,
1146 EXPORT_SYMBOL_GPL(ping_seq_fops);
1148 static struct ping_seq_afinfo ping_v4_seq_afinfo = {
1151 .seq_fops = &ping_seq_fops,
1153 .start = ping_v4_seq_start,
1154 .show = ping_v4_seq_show,
1155 .next = ping_seq_next,
1156 .stop = ping_seq_stop,
1160 int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo)
1162 struct proc_dir_entry *p;
1163 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
1164 afinfo->seq_fops, afinfo);
1169 EXPORT_SYMBOL_GPL(ping_proc_register);
1171 void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo)
1173 remove_proc_entry(afinfo->name, net->proc_net);
1175 EXPORT_SYMBOL_GPL(ping_proc_unregister);
1177 static int __net_init ping_v4_proc_init_net(struct net *net)
1179 return ping_proc_register(net, &ping_v4_seq_afinfo);
1182 static void __net_exit ping_v4_proc_exit_net(struct net *net)
1184 ping_proc_unregister(net, &ping_v4_seq_afinfo);
1187 static struct pernet_operations ping_v4_net_ops = {
1188 .init = ping_v4_proc_init_net,
1189 .exit = ping_v4_proc_exit_net,
1192 int __init ping_proc_init(void)
1194 return register_pernet_subsys(&ping_v4_net_ops);
1197 void ping_proc_exit(void)
1199 unregister_pernet_subsys(&ping_v4_net_ops);
1204 void __init ping_init(void)
1208 for (i = 0; i < PING_HTABLE_SIZE; i++)
1209 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1210 rwlock_init(&ping_table.lock);