xref: /linux/net/ipv4/ping.c (revision 43a6684519ab0a6c52024b5e25322476cabad893)
1c319b4d7SVasiliy Kulikov /*
2c319b4d7SVasiliy Kulikov  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3c319b4d7SVasiliy Kulikov  *		operating system.  INET is implemented using the  BSD Socket
4c319b4d7SVasiliy Kulikov  *		interface as the means of communication with the user level.
5c319b4d7SVasiliy Kulikov  *
6c319b4d7SVasiliy Kulikov  *		"Ping" sockets
7c319b4d7SVasiliy Kulikov  *
8c319b4d7SVasiliy Kulikov  *		This program is free software; you can redistribute it and/or
9c319b4d7SVasiliy Kulikov  *		modify it under the terms of the GNU General Public License
10c319b4d7SVasiliy Kulikov  *		as published by the Free Software Foundation; either version
11c319b4d7SVasiliy Kulikov  *		2 of the License, or (at your option) any later version.
12c319b4d7SVasiliy Kulikov  *
13c319b4d7SVasiliy Kulikov  * Based on ipv4/udp.c code.
14c319b4d7SVasiliy Kulikov  *
15c319b4d7SVasiliy Kulikov  * Authors:	Vasiliy Kulikov / Openwall (for Linux 2.6),
16c319b4d7SVasiliy Kulikov  *		Pavel Kankovsky (for Linux 2.4.32)
17c319b4d7SVasiliy Kulikov  *
18c319b4d7SVasiliy Kulikov  * Pavel gave all rights to bugs to Vasiliy,
19c319b4d7SVasiliy Kulikov  * none of the bugs are Pavel's now.
20c319b4d7SVasiliy Kulikov  *
21c319b4d7SVasiliy Kulikov  */
22c319b4d7SVasiliy Kulikov 
23c319b4d7SVasiliy Kulikov #include <linux/uaccess.h>
24c319b4d7SVasiliy Kulikov #include <linux/types.h>
25c319b4d7SVasiliy Kulikov #include <linux/fcntl.h>
26c319b4d7SVasiliy Kulikov #include <linux/socket.h>
27c319b4d7SVasiliy Kulikov #include <linux/sockios.h>
28c319b4d7SVasiliy Kulikov #include <linux/in.h>
29c319b4d7SVasiliy Kulikov #include <linux/errno.h>
30c319b4d7SVasiliy Kulikov #include <linux/timer.h>
31c319b4d7SVasiliy Kulikov #include <linux/mm.h>
32c319b4d7SVasiliy Kulikov #include <linux/inet.h>
33c319b4d7SVasiliy Kulikov #include <linux/netdevice.h>
34c319b4d7SVasiliy Kulikov #include <net/snmp.h>
35c319b4d7SVasiliy Kulikov #include <net/ip.h>
36c319b4d7SVasiliy Kulikov #include <net/icmp.h>
37c319b4d7SVasiliy Kulikov #include <net/protocol.h>
38c319b4d7SVasiliy Kulikov #include <linux/skbuff.h>
39c319b4d7SVasiliy Kulikov #include <linux/proc_fs.h>
40bc3b2d7fSPaul Gortmaker #include <linux/export.h>
41c319b4d7SVasiliy Kulikov #include <net/sock.h>
42c319b4d7SVasiliy Kulikov #include <net/ping.h>
43c319b4d7SVasiliy Kulikov #include <net/udp.h>
44c319b4d7SVasiliy Kulikov #include <net/route.h>
45c319b4d7SVasiliy Kulikov #include <net/inet_common.h>
46c319b4d7SVasiliy Kulikov #include <net/checksum.h>
47c319b4d7SVasiliy Kulikov 
486d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
496d0bfe22SLorenzo Colitti #include <linux/in6.h>
506d0bfe22SLorenzo Colitti #include <linux/icmpv6.h>
516d0bfe22SLorenzo Colitti #include <net/addrconf.h>
526d0bfe22SLorenzo Colitti #include <net/ipv6.h>
536d0bfe22SLorenzo Colitti #include <net/transp_v6.h>
546d0bfe22SLorenzo Colitti #endif
55c319b4d7SVasiliy Kulikov 
56ea074b34SStephen Hemminger struct ping_table {
57ea074b34SStephen Hemminger 	struct hlist_nulls_head	hash[PING_HTABLE_SIZE];
58ea074b34SStephen Hemminger 	rwlock_t		lock;
59ea074b34SStephen Hemminger };
606d0bfe22SLorenzo Colitti 
61ea074b34SStephen Hemminger static struct ping_table ping_table;
626d0bfe22SLorenzo Colitti struct pingv6_ops pingv6_ops;
636d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(pingv6_ops);
64c319b4d7SVasiliy Kulikov 
651b1cb1f7SEric Dumazet static u16 ping_port_rover;
66c319b4d7SVasiliy Kulikov 
676eada011SEric Dumazet static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)
68c319b4d7SVasiliy Kulikov {
696eada011SEric Dumazet 	u32 res = (num + net_hash_mix(net)) & mask;
7095c96174SEric Dumazet 
716eada011SEric Dumazet 	pr_debug("hash(%u) = %u\n", num, res);
72c319b4d7SVasiliy Kulikov 	return res;
73c319b4d7SVasiliy Kulikov }
746d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_hash);
75c319b4d7SVasiliy Kulikov 
76c319b4d7SVasiliy Kulikov static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
7795c96174SEric Dumazet 					     struct net *net, unsigned int num)
78c319b4d7SVasiliy Kulikov {
79c319b4d7SVasiliy Kulikov 	return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
80c319b4d7SVasiliy Kulikov }
81c319b4d7SVasiliy Kulikov 
826d0bfe22SLorenzo Colitti int ping_get_port(struct sock *sk, unsigned short ident)
83c319b4d7SVasiliy Kulikov {
84c319b4d7SVasiliy Kulikov 	struct hlist_nulls_node *node;
85c319b4d7SVasiliy Kulikov 	struct hlist_nulls_head *hlist;
86c319b4d7SVasiliy Kulikov 	struct inet_sock *isk, *isk2;
87c319b4d7SVasiliy Kulikov 	struct sock *sk2 = NULL;
88c319b4d7SVasiliy Kulikov 
89c319b4d7SVasiliy Kulikov 	isk = inet_sk(sk);
90c319b4d7SVasiliy Kulikov 	write_lock_bh(&ping_table.lock);
91c319b4d7SVasiliy Kulikov 	if (ident == 0) {
92c319b4d7SVasiliy Kulikov 		u32 i;
93c319b4d7SVasiliy Kulikov 		u16 result = ping_port_rover + 1;
94c319b4d7SVasiliy Kulikov 
95c319b4d7SVasiliy Kulikov 		for (i = 0; i < (1L << 16); i++, result++) {
96c319b4d7SVasiliy Kulikov 			if (!result)
97c319b4d7SVasiliy Kulikov 				result++; /* avoid zero */
98c319b4d7SVasiliy Kulikov 			hlist = ping_hashslot(&ping_table, sock_net(sk),
99c319b4d7SVasiliy Kulikov 					    result);
100c319b4d7SVasiliy Kulikov 			ping_portaddr_for_each_entry(sk2, node, hlist) {
101c319b4d7SVasiliy Kulikov 				isk2 = inet_sk(sk2);
102c319b4d7SVasiliy Kulikov 
103c319b4d7SVasiliy Kulikov 				if (isk2->inet_num == result)
104c319b4d7SVasiliy Kulikov 					goto next_port;
105c319b4d7SVasiliy Kulikov 			}
106c319b4d7SVasiliy Kulikov 
107c319b4d7SVasiliy Kulikov 			/* found */
108c319b4d7SVasiliy Kulikov 			ping_port_rover = ident = result;
109c319b4d7SVasiliy Kulikov 			break;
110c319b4d7SVasiliy Kulikov next_port:
111c319b4d7SVasiliy Kulikov 			;
112c319b4d7SVasiliy Kulikov 		}
113c319b4d7SVasiliy Kulikov 		if (i >= (1L << 16))
114c319b4d7SVasiliy Kulikov 			goto fail;
115c319b4d7SVasiliy Kulikov 	} else {
116c319b4d7SVasiliy Kulikov 		hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
117c319b4d7SVasiliy Kulikov 		ping_portaddr_for_each_entry(sk2, node, hlist) {
118c319b4d7SVasiliy Kulikov 			isk2 = inet_sk(sk2);
119c319b4d7SVasiliy Kulikov 
1206d0bfe22SLorenzo Colitti 			/* BUG? Why is this reuse and not reuseaddr? ping.c
1216d0bfe22SLorenzo Colitti 			 * doesn't turn off SO_REUSEADDR, and it doesn't expect
1226d0bfe22SLorenzo Colitti 			 * that other ping processes can steal its packets.
1236d0bfe22SLorenzo Colitti 			 */
124c319b4d7SVasiliy Kulikov 			if ((isk2->inet_num == ident) &&
125c319b4d7SVasiliy Kulikov 			    (sk2 != sk) &&
126c319b4d7SVasiliy Kulikov 			    (!sk2->sk_reuse || !sk->sk_reuse))
127c319b4d7SVasiliy Kulikov 				goto fail;
128c319b4d7SVasiliy Kulikov 		}
129c319b4d7SVasiliy Kulikov 	}
130c319b4d7SVasiliy Kulikov 
131c319b4d7SVasiliy Kulikov 	pr_debug("found port/ident = %d\n", ident);
132c319b4d7SVasiliy Kulikov 	isk->inet_num = ident;
133c319b4d7SVasiliy Kulikov 	if (sk_unhashed(sk)) {
134c319b4d7SVasiliy Kulikov 		pr_debug("was not hashed\n");
135c319b4d7SVasiliy Kulikov 		sock_hold(sk);
136c319b4d7SVasiliy Kulikov 		hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
137c319b4d7SVasiliy Kulikov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
138c319b4d7SVasiliy Kulikov 	}
139c319b4d7SVasiliy Kulikov 	write_unlock_bh(&ping_table.lock);
140c319b4d7SVasiliy Kulikov 	return 0;
141c319b4d7SVasiliy Kulikov 
142c319b4d7SVasiliy Kulikov fail:
143c319b4d7SVasiliy Kulikov 	write_unlock_bh(&ping_table.lock);
144c319b4d7SVasiliy Kulikov 	return 1;
145c319b4d7SVasiliy Kulikov }
1466d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_get_port);
147c319b4d7SVasiliy Kulikov 
148086c653fSCraig Gallek int ping_hash(struct sock *sk)
149c319b4d7SVasiliy Kulikov {
1506d0bfe22SLorenzo Colitti 	pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
151c319b4d7SVasiliy Kulikov 	BUG(); /* "Please do not press this button again." */
152086c653fSCraig Gallek 
153086c653fSCraig Gallek 	return 0;
154c319b4d7SVasiliy Kulikov }
155c319b4d7SVasiliy Kulikov 
1566d0bfe22SLorenzo Colitti void ping_unhash(struct sock *sk)
157c319b4d7SVasiliy Kulikov {
158c319b4d7SVasiliy Kulikov 	struct inet_sock *isk = inet_sk(sk);
159*43a66845SEric Dumazet 
1606d0bfe22SLorenzo Colitti 	pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
161c319b4d7SVasiliy Kulikov 	write_lock_bh(&ping_table.lock);
162*43a66845SEric Dumazet 	if (sk_hashed(sk)) {
163c319b4d7SVasiliy Kulikov 		hlist_nulls_del(&sk->sk_nulls_node);
164a134f083SDavid S. Miller 		sk_nulls_node_init(&sk->sk_nulls_node);
165c319b4d7SVasiliy Kulikov 		sock_put(sk);
166747465efSEric Dumazet 		isk->inet_num = 0;
167747465efSEric Dumazet 		isk->inet_sport = 0;
168c319b4d7SVasiliy Kulikov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
169c319b4d7SVasiliy Kulikov 	}
170*43a66845SEric Dumazet 	write_unlock_bh(&ping_table.lock);
171c319b4d7SVasiliy Kulikov }
1726d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_unhash);
173c319b4d7SVasiliy Kulikov 
1746d0bfe22SLorenzo Colitti static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
175c319b4d7SVasiliy Kulikov {
176c319b4d7SVasiliy Kulikov 	struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
177c319b4d7SVasiliy Kulikov 	struct sock *sk = NULL;
178c319b4d7SVasiliy Kulikov 	struct inet_sock *isk;
179c319b4d7SVasiliy Kulikov 	struct hlist_nulls_node *hnode;
1806d0bfe22SLorenzo Colitti 	int dif = skb->dev->ifindex;
181c319b4d7SVasiliy Kulikov 
1826d0bfe22SLorenzo Colitti 	if (skb->protocol == htons(ETH_P_IP)) {
183747465efSEric Dumazet 		pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
1846d0bfe22SLorenzo Colitti 			 (int)ident, &ip_hdr(skb)->daddr, dif);
1856d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
1866d0bfe22SLorenzo Colitti 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1876d0bfe22SLorenzo Colitti 		pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
1886d0bfe22SLorenzo Colitti 			 (int)ident, &ipv6_hdr(skb)->daddr, dif);
1896d0bfe22SLorenzo Colitti #endif
1906d0bfe22SLorenzo Colitti 	}
1916d0bfe22SLorenzo Colitti 
192c319b4d7SVasiliy Kulikov 	read_lock_bh(&ping_table.lock);
193c319b4d7SVasiliy Kulikov 
194c319b4d7SVasiliy Kulikov 	ping_portaddr_for_each_entry(sk, hnode, hslot) {
195c319b4d7SVasiliy Kulikov 		isk = inet_sk(sk);
196c319b4d7SVasiliy Kulikov 
1976d0bfe22SLorenzo Colitti 		pr_debug("iterate\n");
1986d0bfe22SLorenzo Colitti 		if (isk->inet_num != ident)
1996d0bfe22SLorenzo Colitti 			continue;
2006d0bfe22SLorenzo Colitti 
2016d0bfe22SLorenzo Colitti 		if (skb->protocol == htons(ETH_P_IP) &&
2026d0bfe22SLorenzo Colitti 		    sk->sk_family == AF_INET) {
203747465efSEric Dumazet 			pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
204747465efSEric Dumazet 				 (int) isk->inet_num, &isk->inet_rcv_saddr,
205c319b4d7SVasiliy Kulikov 				 sk->sk_bound_dev_if);
206c319b4d7SVasiliy Kulikov 
2076d0bfe22SLorenzo Colitti 			if (isk->inet_rcv_saddr &&
2086d0bfe22SLorenzo Colitti 			    isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
209c319b4d7SVasiliy Kulikov 				continue;
2106d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
2116d0bfe22SLorenzo Colitti 		} else if (skb->protocol == htons(ETH_P_IPV6) &&
2126d0bfe22SLorenzo Colitti 			   sk->sk_family == AF_INET6) {
2136d0bfe22SLorenzo Colitti 
2146d0bfe22SLorenzo Colitti 			pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
2156d0bfe22SLorenzo Colitti 				 (int) isk->inet_num,
216efe4208fSEric Dumazet 				 &sk->sk_v6_rcv_saddr,
2176d0bfe22SLorenzo Colitti 				 sk->sk_bound_dev_if);
2186d0bfe22SLorenzo Colitti 
219efe4208fSEric Dumazet 			if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
220efe4208fSEric Dumazet 			    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr,
2216d0bfe22SLorenzo Colitti 					     &ipv6_hdr(skb)->daddr))
222c319b4d7SVasiliy Kulikov 				continue;
2236d0bfe22SLorenzo Colitti #endif
22491a0b603SJane Zhou 		} else {
22591a0b603SJane Zhou 			continue;
2266d0bfe22SLorenzo Colitti 		}
2276d0bfe22SLorenzo Colitti 
228c319b4d7SVasiliy Kulikov 		if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
229c319b4d7SVasiliy Kulikov 			continue;
230c319b4d7SVasiliy Kulikov 
231c319b4d7SVasiliy Kulikov 		sock_hold(sk);
232c319b4d7SVasiliy Kulikov 		goto exit;
233c319b4d7SVasiliy Kulikov 	}
234c319b4d7SVasiliy Kulikov 
235c319b4d7SVasiliy Kulikov 	sk = NULL;
236c319b4d7SVasiliy Kulikov exit:
237c319b4d7SVasiliy Kulikov 	read_unlock_bh(&ping_table.lock);
238c319b4d7SVasiliy Kulikov 
239c319b4d7SVasiliy Kulikov 	return sk;
240c319b4d7SVasiliy Kulikov }
241c319b4d7SVasiliy Kulikov 
2427064d16eSEric W. Biederman static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
2437064d16eSEric W. Biederman 					  kgid_t *high)
244f56e03e8SVasiliy Kulikov {
245ba6b918aSCong Wang 	kgid_t *data = net->ipv4.ping_group_range.range;
24695c96174SEric Dumazet 	unsigned int seq;
24795c96174SEric Dumazet 
248f56e03e8SVasiliy Kulikov 	do {
249ba6b918aSCong Wang 		seq = read_seqbegin(&net->ipv4.ping_group_range.lock);
250f56e03e8SVasiliy Kulikov 
251f56e03e8SVasiliy Kulikov 		*low = data[0];
252f56e03e8SVasiliy Kulikov 		*high = data[1];
253ba6b918aSCong Wang 	} while (read_seqretry(&net->ipv4.ping_group_range.lock, seq));
254f56e03e8SVasiliy Kulikov }
255f56e03e8SVasiliy Kulikov 
256f56e03e8SVasiliy Kulikov 
2576d0bfe22SLorenzo Colitti int ping_init_sock(struct sock *sk)
258c319b4d7SVasiliy Kulikov {
259c319b4d7SVasiliy Kulikov 	struct net *net = sock_net(sk);
2607064d16eSEric W. Biederman 	kgid_t group = current_egid();
261b04c4619SWang, Xiaoming 	struct group_info *group_info;
26281243eacSAlexey Dobriyan 	int i;
263ae2975bcSEric W. Biederman 	kgid_t low, high;
264b04c4619SWang, Xiaoming 	int ret = 0;
265c319b4d7SVasiliy Kulikov 
2669145736dSLorenzo Colitti 	if (sk->sk_family == AF_INET6)
2679145736dSLorenzo Colitti 		sk->sk_ipv6only = 1;
2689145736dSLorenzo Colitti 
2697064d16eSEric W. Biederman 	inet_get_ping_group_range_net(net, &low, &high);
2707064d16eSEric W. Biederman 	if (gid_lte(low, group) && gid_lte(group, high))
271c319b4d7SVasiliy Kulikov 		return 0;
272c319b4d7SVasiliy Kulikov 
273b04c4619SWang, Xiaoming 	group_info = get_current_groups();
27481243eacSAlexey Dobriyan 	for (i = 0; i < group_info->ngroups; i++) {
27581243eacSAlexey Dobriyan 		kgid_t gid = group_info->gid[i];
27681243eacSAlexey Dobriyan 
277ae2975bcSEric W. Biederman 		if (gid_lte(low, gid) && gid_lte(gid, high))
278b04c4619SWang, Xiaoming 			goto out_release_group;
279c319b4d7SVasiliy Kulikov 	}
280c319b4d7SVasiliy Kulikov 
281b04c4619SWang, Xiaoming 	ret = -EACCES;
282b04c4619SWang, Xiaoming 
283b04c4619SWang, Xiaoming out_release_group:
284b04c4619SWang, Xiaoming 	put_group_info(group_info);
285b04c4619SWang, Xiaoming 	return ret;
286c319b4d7SVasiliy Kulikov }
2876d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_init_sock);
288c319b4d7SVasiliy Kulikov 
2896d0bfe22SLorenzo Colitti void ping_close(struct sock *sk, long timeout)
290c319b4d7SVasiliy Kulikov {
291c319b4d7SVasiliy Kulikov 	pr_debug("ping_close(sk=%p,sk->num=%u)\n",
292c319b4d7SVasiliy Kulikov 		 inet_sk(sk), inet_sk(sk)->inet_num);
293c319b4d7SVasiliy Kulikov 	pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
294c319b4d7SVasiliy Kulikov 
295c319b4d7SVasiliy Kulikov 	sk_common_release(sk);
296c319b4d7SVasiliy Kulikov }
2976d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_close);
298c319b4d7SVasiliy Kulikov 
2996d0bfe22SLorenzo Colitti /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
300a06a2d37SWu Fengguang static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
3016d0bfe22SLorenzo Colitti 				struct sockaddr *uaddr, int addr_len) {
3026d0bfe22SLorenzo Colitti 	struct net *net = sock_net(sk);
3036d0bfe22SLorenzo Colitti 	if (sk->sk_family == AF_INET) {
304c319b4d7SVasiliy Kulikov 		struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
305c319b4d7SVasiliy Kulikov 		int chk_addr_ret;
306c319b4d7SVasiliy Kulikov 
3076d0bfe22SLorenzo Colitti 		if (addr_len < sizeof(*addr))
308c319b4d7SVasiliy Kulikov 			return -EINVAL;
309c319b4d7SVasiliy Kulikov 
3109145736dSLorenzo Colitti 		if (addr->sin_family != AF_INET &&
3119145736dSLorenzo Colitti 		    !(addr->sin_family == AF_UNSPEC &&
3129145736dSLorenzo Colitti 		      addr->sin_addr.s_addr == htonl(INADDR_ANY)))
3139145736dSLorenzo Colitti 			return -EAFNOSUPPORT;
3149145736dSLorenzo Colitti 
3156d0bfe22SLorenzo Colitti 		pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
3166d0bfe22SLorenzo Colitti 			 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
317c319b4d7SVasiliy Kulikov 
3186d0bfe22SLorenzo Colitti 		chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
3196d0bfe22SLorenzo Colitti 
320747465efSEric Dumazet 		if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
321c319b4d7SVasiliy Kulikov 			chk_addr_ret = RTN_LOCAL;
322c319b4d7SVasiliy Kulikov 
32349a60158SVincent Bernat 		if ((net->ipv4.sysctl_ip_nonlocal_bind == 0 &&
324c319b4d7SVasiliy Kulikov 		    isk->freebind == 0 && isk->transparent == 0 &&
325c319b4d7SVasiliy Kulikov 		     chk_addr_ret != RTN_LOCAL) ||
326c319b4d7SVasiliy Kulikov 		    chk_addr_ret == RTN_MULTICAST ||
327c319b4d7SVasiliy Kulikov 		    chk_addr_ret == RTN_BROADCAST)
328c319b4d7SVasiliy Kulikov 			return -EADDRNOTAVAIL;
329c319b4d7SVasiliy Kulikov 
3306d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
3316d0bfe22SLorenzo Colitti 	} else if (sk->sk_family == AF_INET6) {
3326d0bfe22SLorenzo Colitti 		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
3336d0bfe22SLorenzo Colitti 		int addr_type, scoped, has_addr;
3346d0bfe22SLorenzo Colitti 		struct net_device *dev = NULL;
3356d0bfe22SLorenzo Colitti 
3366d0bfe22SLorenzo Colitti 		if (addr_len < sizeof(*addr))
3376d0bfe22SLorenzo Colitti 			return -EINVAL;
3386d0bfe22SLorenzo Colitti 
33982b276cdSHannes Frederic Sowa 		if (addr->sin6_family != AF_INET6)
3409145736dSLorenzo Colitti 			return -EAFNOSUPPORT;
34182b276cdSHannes Frederic Sowa 
3426d0bfe22SLorenzo Colitti 		pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
3436d0bfe22SLorenzo Colitti 			 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
3446d0bfe22SLorenzo Colitti 
3456d0bfe22SLorenzo Colitti 		addr_type = ipv6_addr_type(&addr->sin6_addr);
3466d0bfe22SLorenzo Colitti 		scoped = __ipv6_addr_needs_scope_id(addr_type);
3476d0bfe22SLorenzo Colitti 		if ((addr_type != IPV6_ADDR_ANY &&
3486d0bfe22SLorenzo Colitti 		     !(addr_type & IPV6_ADDR_UNICAST)) ||
3496d0bfe22SLorenzo Colitti 		    (scoped && !addr->sin6_scope_id))
3506d0bfe22SLorenzo Colitti 			return -EINVAL;
3516d0bfe22SLorenzo Colitti 
3526d0bfe22SLorenzo Colitti 		rcu_read_lock();
3536d0bfe22SLorenzo Colitti 		if (addr->sin6_scope_id) {
3546d0bfe22SLorenzo Colitti 			dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
3556d0bfe22SLorenzo Colitti 			if (!dev) {
3566d0bfe22SLorenzo Colitti 				rcu_read_unlock();
3576d0bfe22SLorenzo Colitti 				return -ENODEV;
3586d0bfe22SLorenzo Colitti 			}
3596d0bfe22SLorenzo Colitti 		}
3606d0bfe22SLorenzo Colitti 		has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
3616d0bfe22SLorenzo Colitti 						    scoped);
3626d0bfe22SLorenzo Colitti 		rcu_read_unlock();
3636d0bfe22SLorenzo Colitti 
36435a256feSTom Herbert 		if (!(net->ipv6.sysctl.ip_nonlocal_bind ||
36535a256feSTom Herbert 		      isk->freebind || isk->transparent || has_addr ||
3666d0bfe22SLorenzo Colitti 		      addr_type == IPV6_ADDR_ANY))
3676d0bfe22SLorenzo Colitti 			return -EADDRNOTAVAIL;
3686d0bfe22SLorenzo Colitti 
3696d0bfe22SLorenzo Colitti 		if (scoped)
3706d0bfe22SLorenzo Colitti 			sk->sk_bound_dev_if = addr->sin6_scope_id;
3716d0bfe22SLorenzo Colitti #endif
3726d0bfe22SLorenzo Colitti 	} else {
3736d0bfe22SLorenzo Colitti 		return -EAFNOSUPPORT;
3746d0bfe22SLorenzo Colitti 	}
3756d0bfe22SLorenzo Colitti 	return 0;
3766d0bfe22SLorenzo Colitti }
3776d0bfe22SLorenzo Colitti 
378a06a2d37SWu Fengguang static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
3796d0bfe22SLorenzo Colitti {
3806d0bfe22SLorenzo Colitti 	if (saddr->sa_family == AF_INET) {
3816d0bfe22SLorenzo Colitti 		struct inet_sock *isk = inet_sk(sk);
3826d0bfe22SLorenzo Colitti 		struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
3836d0bfe22SLorenzo Colitti 		isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
3846d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
3856d0bfe22SLorenzo Colitti 	} else if (saddr->sa_family == AF_INET6) {
3866d0bfe22SLorenzo Colitti 		struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
3876d0bfe22SLorenzo Colitti 		struct ipv6_pinfo *np = inet6_sk(sk);
388efe4208fSEric Dumazet 		sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
3896d0bfe22SLorenzo Colitti #endif
3906d0bfe22SLorenzo Colitti 	}
3916d0bfe22SLorenzo Colitti }
3926d0bfe22SLorenzo Colitti 
393a06a2d37SWu Fengguang static void ping_clear_saddr(struct sock *sk, int dif)
3946d0bfe22SLorenzo Colitti {
3956d0bfe22SLorenzo Colitti 	sk->sk_bound_dev_if = dif;
3966d0bfe22SLorenzo Colitti 	if (sk->sk_family == AF_INET) {
3976d0bfe22SLorenzo Colitti 		struct inet_sock *isk = inet_sk(sk);
3986d0bfe22SLorenzo Colitti 		isk->inet_rcv_saddr = isk->inet_saddr = 0;
3996d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
4006d0bfe22SLorenzo Colitti 	} else if (sk->sk_family == AF_INET6) {
4016d0bfe22SLorenzo Colitti 		struct ipv6_pinfo *np = inet6_sk(sk);
402efe4208fSEric Dumazet 		memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
4036d0bfe22SLorenzo Colitti 		memset(&np->saddr, 0, sizeof(np->saddr));
4046d0bfe22SLorenzo Colitti #endif
4056d0bfe22SLorenzo Colitti 	}
4066d0bfe22SLorenzo Colitti }
4076d0bfe22SLorenzo Colitti /*
4086d0bfe22SLorenzo Colitti  * We need our own bind because there are no privileged id's == local ports.
4096d0bfe22SLorenzo Colitti  * Moreover, we don't allow binding to multi- and broadcast addresses.
4106d0bfe22SLorenzo Colitti  */
4116d0bfe22SLorenzo Colitti 
4126d0bfe22SLorenzo Colitti int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
4136d0bfe22SLorenzo Colitti {
4146d0bfe22SLorenzo Colitti 	struct inet_sock *isk = inet_sk(sk);
4156d0bfe22SLorenzo Colitti 	unsigned short snum;
4166d0bfe22SLorenzo Colitti 	int err;
4176d0bfe22SLorenzo Colitti 	int dif = sk->sk_bound_dev_if;
4186d0bfe22SLorenzo Colitti 
4196d0bfe22SLorenzo Colitti 	err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
4206d0bfe22SLorenzo Colitti 	if (err)
4216d0bfe22SLorenzo Colitti 		return err;
4226d0bfe22SLorenzo Colitti 
423c319b4d7SVasiliy Kulikov 	lock_sock(sk);
424c319b4d7SVasiliy Kulikov 
425c319b4d7SVasiliy Kulikov 	err = -EINVAL;
426c319b4d7SVasiliy Kulikov 	if (isk->inet_num != 0)
427c319b4d7SVasiliy Kulikov 		goto out;
428c319b4d7SVasiliy Kulikov 
429c319b4d7SVasiliy Kulikov 	err = -EADDRINUSE;
4306d0bfe22SLorenzo Colitti 	ping_set_saddr(sk, uaddr);
4316d0bfe22SLorenzo Colitti 	snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
4326d0bfe22SLorenzo Colitti 	if (ping_get_port(sk, snum) != 0) {
4336d0bfe22SLorenzo Colitti 		ping_clear_saddr(sk, dif);
434c319b4d7SVasiliy Kulikov 		goto out;
435c319b4d7SVasiliy Kulikov 	}
436c319b4d7SVasiliy Kulikov 
437a7ef6715SGao Feng 	pr_debug("after bind(): num = %hu, dif = %d\n",
438a7ef6715SGao Feng 		 isk->inet_num,
439a7ef6715SGao Feng 		 sk->sk_bound_dev_if);
440c319b4d7SVasiliy Kulikov 
441c319b4d7SVasiliy Kulikov 	err = 0;
442c2bb06dbSEric Dumazet 	if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
443c319b4d7SVasiliy Kulikov 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
444c2bb06dbSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
445c2bb06dbSEric Dumazet 	if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
446c2bb06dbSEric Dumazet 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
447c2bb06dbSEric Dumazet #endif
4486d0bfe22SLorenzo Colitti 
449c319b4d7SVasiliy Kulikov 	if (snum)
450c319b4d7SVasiliy Kulikov 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
451c319b4d7SVasiliy Kulikov 	isk->inet_sport = htons(isk->inet_num);
452c319b4d7SVasiliy Kulikov 	isk->inet_daddr = 0;
453c319b4d7SVasiliy Kulikov 	isk->inet_dport = 0;
4546d0bfe22SLorenzo Colitti 
4556d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
4566d0bfe22SLorenzo Colitti 	if (sk->sk_family == AF_INET6)
457efe4208fSEric Dumazet 		memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
4586d0bfe22SLorenzo Colitti #endif
4596d0bfe22SLorenzo Colitti 
460c319b4d7SVasiliy Kulikov 	sk_dst_reset(sk);
461c319b4d7SVasiliy Kulikov out:
462c319b4d7SVasiliy Kulikov 	release_sock(sk);
463c319b4d7SVasiliy Kulikov 	pr_debug("ping_v4_bind -> %d\n", err);
464c319b4d7SVasiliy Kulikov 	return err;
465c319b4d7SVasiliy Kulikov }
4666d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_bind);
467c319b4d7SVasiliy Kulikov 
468c319b4d7SVasiliy Kulikov /*
469c319b4d7SVasiliy Kulikov  * Is this a supported type of ICMP message?
470c319b4d7SVasiliy Kulikov  */
471c319b4d7SVasiliy Kulikov 
4726d0bfe22SLorenzo Colitti static inline int ping_supported(int family, int type, int code)
473c319b4d7SVasiliy Kulikov {
4746d0bfe22SLorenzo Colitti 	return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
4756d0bfe22SLorenzo Colitti 	       (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
476c319b4d7SVasiliy Kulikov }
477c319b4d7SVasiliy Kulikov 
478c319b4d7SVasiliy Kulikov /*
479c319b4d7SVasiliy Kulikov  * This routine is called by the ICMP module when it gets some
480c319b4d7SVasiliy Kulikov  * sort of error condition.
481c319b4d7SVasiliy Kulikov  */
482c319b4d7SVasiliy Kulikov 
4836d0bfe22SLorenzo Colitti void ping_err(struct sk_buff *skb, int offset, u32 info)
484c319b4d7SVasiliy Kulikov {
4856d0bfe22SLorenzo Colitti 	int family;
4866d0bfe22SLorenzo Colitti 	struct icmphdr *icmph;
487c319b4d7SVasiliy Kulikov 	struct inet_sock *inet_sock;
4886d0bfe22SLorenzo Colitti 	int type;
4896d0bfe22SLorenzo Colitti 	int code;
490c319b4d7SVasiliy Kulikov 	struct net *net = dev_net(skb->dev);
491c319b4d7SVasiliy Kulikov 	struct sock *sk;
492c319b4d7SVasiliy Kulikov 	int harderr;
493c319b4d7SVasiliy Kulikov 	int err;
494c319b4d7SVasiliy Kulikov 
4956d0bfe22SLorenzo Colitti 	if (skb->protocol == htons(ETH_P_IP)) {
4966d0bfe22SLorenzo Colitti 		family = AF_INET;
4976d0bfe22SLorenzo Colitti 		type = icmp_hdr(skb)->type;
4986d0bfe22SLorenzo Colitti 		code = icmp_hdr(skb)->code;
4996d0bfe22SLorenzo Colitti 		icmph = (struct icmphdr *)(skb->data + offset);
5006d0bfe22SLorenzo Colitti 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
5016d0bfe22SLorenzo Colitti 		family = AF_INET6;
5026d0bfe22SLorenzo Colitti 		type = icmp6_hdr(skb)->icmp6_type;
5036d0bfe22SLorenzo Colitti 		code = icmp6_hdr(skb)->icmp6_code;
5046d0bfe22SLorenzo Colitti 		icmph = (struct icmphdr *) (skb->data + offset);
5056d0bfe22SLorenzo Colitti 	} else {
5066d0bfe22SLorenzo Colitti 		BUG();
5076d0bfe22SLorenzo Colitti 	}
5086d0bfe22SLorenzo Colitti 
509c319b4d7SVasiliy Kulikov 	/* We assume the packet has already been checked by icmp_unreach */
510c319b4d7SVasiliy Kulikov 
5116d0bfe22SLorenzo Colitti 	if (!ping_supported(family, icmph->type, icmph->code))
512c319b4d7SVasiliy Kulikov 		return;
513c319b4d7SVasiliy Kulikov 
5146d0bfe22SLorenzo Colitti 	pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
5156d0bfe22SLorenzo Colitti 		 skb->protocol, type, code, ntohs(icmph->un.echo.id),
5166d0bfe22SLorenzo Colitti 		 ntohs(icmph->un.echo.sequence));
517c319b4d7SVasiliy Kulikov 
5186d0bfe22SLorenzo Colitti 	sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
51951456b29SIan Morris 	if (!sk) {
520c319b4d7SVasiliy Kulikov 		pr_debug("no socket, dropping\n");
521c319b4d7SVasiliy Kulikov 		return;	/* No socket for error */
522c319b4d7SVasiliy Kulikov 	}
523c319b4d7SVasiliy Kulikov 	pr_debug("err on socket %p\n", sk);
524c319b4d7SVasiliy Kulikov 
525c319b4d7SVasiliy Kulikov 	err = 0;
526c319b4d7SVasiliy Kulikov 	harderr = 0;
527c319b4d7SVasiliy Kulikov 	inet_sock = inet_sk(sk);
528c319b4d7SVasiliy Kulikov 
5296d0bfe22SLorenzo Colitti 	if (skb->protocol == htons(ETH_P_IP)) {
530c319b4d7SVasiliy Kulikov 		switch (type) {
531c319b4d7SVasiliy Kulikov 		default:
532c319b4d7SVasiliy Kulikov 		case ICMP_TIME_EXCEEDED:
533c319b4d7SVasiliy Kulikov 			err = EHOSTUNREACH;
534c319b4d7SVasiliy Kulikov 			break;
535c319b4d7SVasiliy Kulikov 		case ICMP_SOURCE_QUENCH:
536c319b4d7SVasiliy Kulikov 			/* This is not a real error but ping wants to see it.
5376d0bfe22SLorenzo Colitti 			 * Report it with some fake errno.
5386d0bfe22SLorenzo Colitti 			 */
539c319b4d7SVasiliy Kulikov 			err = EREMOTEIO;
540c319b4d7SVasiliy Kulikov 			break;
541c319b4d7SVasiliy Kulikov 		case ICMP_PARAMETERPROB:
542c319b4d7SVasiliy Kulikov 			err = EPROTO;
543c319b4d7SVasiliy Kulikov 			harderr = 1;
544c319b4d7SVasiliy Kulikov 			break;
545c319b4d7SVasiliy Kulikov 		case ICMP_DEST_UNREACH:
546c319b4d7SVasiliy Kulikov 			if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
54736393395SDavid S. Miller 				ipv4_sk_update_pmtu(skb, sk, info);
548c319b4d7SVasiliy Kulikov 				if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
549c319b4d7SVasiliy Kulikov 					err = EMSGSIZE;
550c319b4d7SVasiliy Kulikov 					harderr = 1;
551c319b4d7SVasiliy Kulikov 					break;
552c319b4d7SVasiliy Kulikov 				}
553c319b4d7SVasiliy Kulikov 				goto out;
554c319b4d7SVasiliy Kulikov 			}
555c319b4d7SVasiliy Kulikov 			err = EHOSTUNREACH;
556c319b4d7SVasiliy Kulikov 			if (code <= NR_ICMP_UNREACH) {
557c319b4d7SVasiliy Kulikov 				harderr = icmp_err_convert[code].fatal;
558c319b4d7SVasiliy Kulikov 				err = icmp_err_convert[code].errno;
559c319b4d7SVasiliy Kulikov 			}
560c319b4d7SVasiliy Kulikov 			break;
561c319b4d7SVasiliy Kulikov 		case ICMP_REDIRECT:
562c319b4d7SVasiliy Kulikov 			/* See ICMP_SOURCE_QUENCH */
56355be7a9cSDavid S. Miller 			ipv4_sk_redirect(skb, sk);
564c319b4d7SVasiliy Kulikov 			err = EREMOTEIO;
565c319b4d7SVasiliy Kulikov 			break;
566c319b4d7SVasiliy Kulikov 		}
5676d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
5686d0bfe22SLorenzo Colitti 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
5696d0bfe22SLorenzo Colitti 		harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
5706d0bfe22SLorenzo Colitti #endif
5716d0bfe22SLorenzo Colitti 	}
572c319b4d7SVasiliy Kulikov 
573c319b4d7SVasiliy Kulikov 	/*
574c319b4d7SVasiliy Kulikov 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
575c319b4d7SVasiliy Kulikov 	 *	4.1.3.3.
576c319b4d7SVasiliy Kulikov 	 */
5776d0bfe22SLorenzo Colitti 	if ((family == AF_INET && !inet_sock->recverr) ||
5786d0bfe22SLorenzo Colitti 	    (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
579c319b4d7SVasiliy Kulikov 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
580c319b4d7SVasiliy Kulikov 			goto out;
581c319b4d7SVasiliy Kulikov 	} else {
5826d0bfe22SLorenzo Colitti 		if (family == AF_INET) {
583c319b4d7SVasiliy Kulikov 			ip_icmp_error(sk, skb, err, 0 /* no remote port */,
584c319b4d7SVasiliy Kulikov 				      info, (u8 *)icmph);
5856d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
5866d0bfe22SLorenzo Colitti 		} else if (family == AF_INET6) {
5876d0bfe22SLorenzo Colitti 			pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
5886d0bfe22SLorenzo Colitti 						   info, (u8 *)icmph);
5896d0bfe22SLorenzo Colitti #endif
5906d0bfe22SLorenzo Colitti 		}
591c319b4d7SVasiliy Kulikov 	}
592c319b4d7SVasiliy Kulikov 	sk->sk_err = err;
593c319b4d7SVasiliy Kulikov 	sk->sk_error_report(sk);
594c319b4d7SVasiliy Kulikov out:
595c319b4d7SVasiliy Kulikov 	sock_put(sk);
596c319b4d7SVasiliy Kulikov }
5976d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_err);
598c319b4d7SVasiliy Kulikov 
599c319b4d7SVasiliy Kulikov /*
6006d0bfe22SLorenzo Colitti  *	Copy and checksum an ICMP Echo packet from user space into a buffer
6016d0bfe22SLorenzo Colitti  *	starting from the payload.
602c319b4d7SVasiliy Kulikov  */
603c319b4d7SVasiliy Kulikov 
6046d0bfe22SLorenzo Colitti int ping_getfrag(void *from, char *to,
605c319b4d7SVasiliy Kulikov 		 int offset, int fraglen, int odd, struct sk_buff *skb)
606c319b4d7SVasiliy Kulikov {
607c319b4d7SVasiliy Kulikov 	struct pingfakehdr *pfh = (struct pingfakehdr *)from;
608c319b4d7SVasiliy Kulikov 
609c319b4d7SVasiliy Kulikov 	if (offset == 0) {
61021226abbSAl Viro 		fraglen -= sizeof(struct icmphdr);
61121226abbSAl Viro 		if (fraglen < 0)
612c319b4d7SVasiliy Kulikov 			BUG();
6130b62fca2SAl Viro 		if (!csum_and_copy_from_iter_full(to + sizeof(struct icmphdr),
61421226abbSAl Viro 			    fraglen, &pfh->wcheck,
6150b62fca2SAl Viro 			    &pfh->msg->msg_iter))
616c319b4d7SVasiliy Kulikov 			return -EFAULT;
6176d0bfe22SLorenzo Colitti 	} else if (offset < sizeof(struct icmphdr)) {
618c319b4d7SVasiliy Kulikov 			BUG();
6196d0bfe22SLorenzo Colitti 	} else {
6200b62fca2SAl Viro 		if (!csum_and_copy_from_iter_full(to, fraglen, &pfh->wcheck,
6210b62fca2SAl Viro 					    &pfh->msg->msg_iter))
622c319b4d7SVasiliy Kulikov 			return -EFAULT;
623c319b4d7SVasiliy Kulikov 	}
624c319b4d7SVasiliy Kulikov 
6256d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
6266d0bfe22SLorenzo Colitti 	/* For IPv6, checksum each skb as we go along, as expected by
6276d0bfe22SLorenzo Colitti 	 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
6286d0bfe22SLorenzo Colitti 	 * wcheck, it will be finalized in ping_v4_push_pending_frames.
6296d0bfe22SLorenzo Colitti 	 */
6306d0bfe22SLorenzo Colitti 	if (pfh->family == AF_INET6) {
6316d0bfe22SLorenzo Colitti 		skb->csum = pfh->wcheck;
6326d0bfe22SLorenzo Colitti 		skb->ip_summed = CHECKSUM_NONE;
6336d0bfe22SLorenzo Colitti 		pfh->wcheck = 0;
6346d0bfe22SLorenzo Colitti 	}
6356d0bfe22SLorenzo Colitti #endif
6366d0bfe22SLorenzo Colitti 
6376d0bfe22SLorenzo Colitti 	return 0;
6386d0bfe22SLorenzo Colitti }
6396d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_getfrag);
6406d0bfe22SLorenzo Colitti 
6416d0bfe22SLorenzo Colitti static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
64275e308c8SChangli Gao 				       struct flowi4 *fl4)
643c319b4d7SVasiliy Kulikov {
644c319b4d7SVasiliy Kulikov 	struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
645c319b4d7SVasiliy Kulikov 
64673d2c667SWANG Cong 	if (!skb)
64773d2c667SWANG Cong 		return 0;
648c319b4d7SVasiliy Kulikov 	pfh->wcheck = csum_partial((char *)&pfh->icmph,
649c319b4d7SVasiliy Kulikov 		sizeof(struct icmphdr), pfh->wcheck);
650c319b4d7SVasiliy Kulikov 	pfh->icmph.checksum = csum_fold(pfh->wcheck);
651c319b4d7SVasiliy Kulikov 	memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
652c319b4d7SVasiliy Kulikov 	skb->ip_summed = CHECKSUM_NONE;
653c319b4d7SVasiliy Kulikov 	return ip_push_pending_frames(sk, fl4);
654c319b4d7SVasiliy Kulikov }
655c319b4d7SVasiliy Kulikov 
6566d0bfe22SLorenzo Colitti int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
6576d0bfe22SLorenzo Colitti 			void *user_icmph, size_t icmph_len) {
6586d0bfe22SLorenzo Colitti 	u8 type, code;
6596d0bfe22SLorenzo Colitti 
6606d0bfe22SLorenzo Colitti 	if (len > 0xFFFF)
6616d0bfe22SLorenzo Colitti 		return -EMSGSIZE;
6626d0bfe22SLorenzo Colitti 
6630eab121eSKees Cook 	/* Must have at least a full ICMP header. */
6640eab121eSKees Cook 	if (len < icmph_len)
6650eab121eSKees Cook 		return -EINVAL;
6660eab121eSKees Cook 
6676d0bfe22SLorenzo Colitti 	/*
6686d0bfe22SLorenzo Colitti 	 *	Check the flags.
6696d0bfe22SLorenzo Colitti 	 */
6706d0bfe22SLorenzo Colitti 
6716d0bfe22SLorenzo Colitti 	/* Mirror BSD error message compatibility */
6726d0bfe22SLorenzo Colitti 	if (msg->msg_flags & MSG_OOB)
6736d0bfe22SLorenzo Colitti 		return -EOPNOTSUPP;
6746d0bfe22SLorenzo Colitti 
6756d0bfe22SLorenzo Colitti 	/*
6766d0bfe22SLorenzo Colitti 	 *	Fetch the ICMP header provided by the userland.
6776d0bfe22SLorenzo Colitti 	 *	iovec is modified! The ICMP header is consumed.
6786d0bfe22SLorenzo Colitti 	 */
6796ce8e9ceSAl Viro 	if (memcpy_from_msg(user_icmph, msg, icmph_len))
6806d0bfe22SLorenzo Colitti 		return -EFAULT;
6816d0bfe22SLorenzo Colitti 
6826d0bfe22SLorenzo Colitti 	if (family == AF_INET) {
6836d0bfe22SLorenzo Colitti 		type = ((struct icmphdr *) user_icmph)->type;
6846d0bfe22SLorenzo Colitti 		code = ((struct icmphdr *) user_icmph)->code;
6856d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
6866d0bfe22SLorenzo Colitti 	} else if (family == AF_INET6) {
6876d0bfe22SLorenzo Colitti 		type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
6886d0bfe22SLorenzo Colitti 		code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
6896d0bfe22SLorenzo Colitti #endif
6906d0bfe22SLorenzo Colitti 	} else {
6916d0bfe22SLorenzo Colitti 		BUG();
6926d0bfe22SLorenzo Colitti 	}
6936d0bfe22SLorenzo Colitti 
6946d0bfe22SLorenzo Colitti 	if (!ping_supported(family, type, code))
6956d0bfe22SLorenzo Colitti 		return -EINVAL;
6966d0bfe22SLorenzo Colitti 
6976d0bfe22SLorenzo Colitti 	return 0;
6986d0bfe22SLorenzo Colitti }
6996d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_common_sendmsg);
7006d0bfe22SLorenzo Colitti 
7011b784140SYing Xue static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
702c319b4d7SVasiliy Kulikov {
703c319b4d7SVasiliy Kulikov 	struct net *net = sock_net(sk);
704c319b4d7SVasiliy Kulikov 	struct flowi4 fl4;
705c319b4d7SVasiliy Kulikov 	struct inet_sock *inet = inet_sk(sk);
706c319b4d7SVasiliy Kulikov 	struct ipcm_cookie ipc;
707c319b4d7SVasiliy Kulikov 	struct icmphdr user_icmph;
708c319b4d7SVasiliy Kulikov 	struct pingfakehdr pfh;
709c319b4d7SVasiliy Kulikov 	struct rtable *rt = NULL;
710c319b4d7SVasiliy Kulikov 	struct ip_options_data opt_copy;
711c319b4d7SVasiliy Kulikov 	int free = 0;
712747465efSEric Dumazet 	__be32 saddr, daddr, faddr;
713c319b4d7SVasiliy Kulikov 	u8  tos;
714c319b4d7SVasiliy Kulikov 	int err;
715c319b4d7SVasiliy Kulikov 
7166d0bfe22SLorenzo Colitti 	pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
717c319b4d7SVasiliy Kulikov 
7186d0bfe22SLorenzo Colitti 	err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
7196d0bfe22SLorenzo Colitti 				  sizeof(user_icmph));
7206d0bfe22SLorenzo Colitti 	if (err)
7216d0bfe22SLorenzo Colitti 		return err;
722c319b4d7SVasiliy Kulikov 
723c319b4d7SVasiliy Kulikov 	/*
724c319b4d7SVasiliy Kulikov 	 *	Get and verify the address.
725c319b4d7SVasiliy Kulikov 	 */
726c319b4d7SVasiliy Kulikov 
727c319b4d7SVasiliy Kulikov 	if (msg->msg_name) {
728342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
729c319b4d7SVasiliy Kulikov 		if (msg->msg_namelen < sizeof(*usin))
730c319b4d7SVasiliy Kulikov 			return -EINVAL;
731c319b4d7SVasiliy Kulikov 		if (usin->sin_family != AF_INET)
7329145736dSLorenzo Colitti 			return -EAFNOSUPPORT;
733c319b4d7SVasiliy Kulikov 		daddr = usin->sin_addr.s_addr;
734c319b4d7SVasiliy Kulikov 		/* no remote port */
735c319b4d7SVasiliy Kulikov 	} else {
736c319b4d7SVasiliy Kulikov 		if (sk->sk_state != TCP_ESTABLISHED)
737c319b4d7SVasiliy Kulikov 			return -EDESTADDRREQ;
738c319b4d7SVasiliy Kulikov 		daddr = inet->inet_daddr;
739c319b4d7SVasiliy Kulikov 		/* no remote port */
740c319b4d7SVasiliy Kulikov 	}
741c319b4d7SVasiliy Kulikov 
742c14ac945SSoheil Hassas Yeganeh 	ipc.sockc.tsflags = sk->sk_tsflags;
743c319b4d7SVasiliy Kulikov 	ipc.addr = inet->inet_saddr;
744c319b4d7SVasiliy Kulikov 	ipc.opt = NULL;
745c319b4d7SVasiliy Kulikov 	ipc.oif = sk->sk_bound_dev_if;
746c319b4d7SVasiliy Kulikov 	ipc.tx_flags = 0;
747aa661581SFrancesco Fusco 	ipc.ttl = 0;
748aa661581SFrancesco Fusco 	ipc.tos = -1;
749bf84a010SDaniel Borkmann 
750c319b4d7SVasiliy Kulikov 	if (msg->msg_controllen) {
75124025c46SSoheil Hassas Yeganeh 		err = ip_cmsg_send(sk, msg, &ipc, false);
75291948309SEric Dumazet 		if (unlikely(err)) {
75391948309SEric Dumazet 			kfree(ipc.opt);
754c319b4d7SVasiliy Kulikov 			return err;
75591948309SEric Dumazet 		}
756c319b4d7SVasiliy Kulikov 		if (ipc.opt)
757c319b4d7SVasiliy Kulikov 			free = 1;
758c319b4d7SVasiliy Kulikov 	}
759c319b4d7SVasiliy Kulikov 	if (!ipc.opt) {
760c319b4d7SVasiliy Kulikov 		struct ip_options_rcu *inet_opt;
761c319b4d7SVasiliy Kulikov 
762c319b4d7SVasiliy Kulikov 		rcu_read_lock();
763c319b4d7SVasiliy Kulikov 		inet_opt = rcu_dereference(inet->inet_opt);
764c319b4d7SVasiliy Kulikov 		if (inet_opt) {
765c319b4d7SVasiliy Kulikov 			memcpy(&opt_copy, inet_opt,
766c319b4d7SVasiliy Kulikov 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
767c319b4d7SVasiliy Kulikov 			ipc.opt = &opt_copy.opt;
768c319b4d7SVasiliy Kulikov 		}
769c319b4d7SVasiliy Kulikov 		rcu_read_unlock();
770c319b4d7SVasiliy Kulikov 	}
771c319b4d7SVasiliy Kulikov 
772c14ac945SSoheil Hassas Yeganeh 	sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
773c14ac945SSoheil Hassas Yeganeh 
774c319b4d7SVasiliy Kulikov 	saddr = ipc.addr;
775c319b4d7SVasiliy Kulikov 	ipc.addr = faddr = daddr;
776c319b4d7SVasiliy Kulikov 
777c319b4d7SVasiliy Kulikov 	if (ipc.opt && ipc.opt->opt.srr) {
778c319b4d7SVasiliy Kulikov 		if (!daddr)
779c319b4d7SVasiliy Kulikov 			return -EINVAL;
780c319b4d7SVasiliy Kulikov 		faddr = ipc.opt->opt.faddr;
781c319b4d7SVasiliy Kulikov 	}
782aa661581SFrancesco Fusco 	tos = get_rttos(&ipc, inet);
783c319b4d7SVasiliy Kulikov 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
784c319b4d7SVasiliy Kulikov 	    (msg->msg_flags & MSG_DONTROUTE) ||
785c319b4d7SVasiliy Kulikov 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
786c319b4d7SVasiliy Kulikov 		tos |= RTO_ONLINK;
787c319b4d7SVasiliy Kulikov 	}
788c319b4d7SVasiliy Kulikov 
789c319b4d7SVasiliy Kulikov 	if (ipv4_is_multicast(daddr)) {
790c319b4d7SVasiliy Kulikov 		if (!ipc.oif)
791c319b4d7SVasiliy Kulikov 			ipc.oif = inet->mc_index;
792c319b4d7SVasiliy Kulikov 		if (!saddr)
793c319b4d7SVasiliy Kulikov 			saddr = inet->mc_addr;
79476e21053SErich E. Hoover 	} else if (!ipc.oif)
79576e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
796c319b4d7SVasiliy Kulikov 
797c319b4d7SVasiliy Kulikov 	flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
798c319b4d7SVasiliy Kulikov 			   RT_SCOPE_UNIVERSE, sk->sk_protocol,
799e2d118a1SLorenzo Colitti 			   inet_sk_flowi_flags(sk), faddr, saddr, 0, 0,
800e2d118a1SLorenzo Colitti 			   sk->sk_uid);
801c319b4d7SVasiliy Kulikov 
802c319b4d7SVasiliy Kulikov 	security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
803c319b4d7SVasiliy Kulikov 	rt = ip_route_output_flow(net, &fl4, sk);
804c319b4d7SVasiliy Kulikov 	if (IS_ERR(rt)) {
805c319b4d7SVasiliy Kulikov 		err = PTR_ERR(rt);
806c319b4d7SVasiliy Kulikov 		rt = NULL;
807c319b4d7SVasiliy Kulikov 		if (err == -ENETUNREACH)
808f1d8cba6SEric Dumazet 			IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
809c319b4d7SVasiliy Kulikov 		goto out;
810c319b4d7SVasiliy Kulikov 	}
811c319b4d7SVasiliy Kulikov 
812c319b4d7SVasiliy Kulikov 	err = -EACCES;
813c319b4d7SVasiliy Kulikov 	if ((rt->rt_flags & RTCF_BROADCAST) &&
814c319b4d7SVasiliy Kulikov 	    !sock_flag(sk, SOCK_BROADCAST))
815c319b4d7SVasiliy Kulikov 		goto out;
816c319b4d7SVasiliy Kulikov 
817c319b4d7SVasiliy Kulikov 	if (msg->msg_flags & MSG_CONFIRM)
818c319b4d7SVasiliy Kulikov 		goto do_confirm;
819c319b4d7SVasiliy Kulikov back_from_confirm:
820c319b4d7SVasiliy Kulikov 
821c319b4d7SVasiliy Kulikov 	if (!ipc.addr)
822c319b4d7SVasiliy Kulikov 		ipc.addr = fl4.daddr;
823c319b4d7SVasiliy Kulikov 
824c319b4d7SVasiliy Kulikov 	lock_sock(sk);
825c319b4d7SVasiliy Kulikov 
826c319b4d7SVasiliy Kulikov 	pfh.icmph.type = user_icmph.type; /* already checked */
827c319b4d7SVasiliy Kulikov 	pfh.icmph.code = user_icmph.code; /* ditto */
828c319b4d7SVasiliy Kulikov 	pfh.icmph.checksum = 0;
829c319b4d7SVasiliy Kulikov 	pfh.icmph.un.echo.id = inet->inet_sport;
830c319b4d7SVasiliy Kulikov 	pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
831cacdc7d2SAl Viro 	pfh.msg = msg;
832c319b4d7SVasiliy Kulikov 	pfh.wcheck = 0;
8336d0bfe22SLorenzo Colitti 	pfh.family = AF_INET;
834c319b4d7SVasiliy Kulikov 
835c319b4d7SVasiliy Kulikov 	err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
836c319b4d7SVasiliy Kulikov 			0, &ipc, &rt, msg->msg_flags);
837c319b4d7SVasiliy Kulikov 	if (err)
838c319b4d7SVasiliy Kulikov 		ip_flush_pending_frames(sk);
839c319b4d7SVasiliy Kulikov 	else
8406d0bfe22SLorenzo Colitti 		err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
841c319b4d7SVasiliy Kulikov 	release_sock(sk);
842c319b4d7SVasiliy Kulikov 
843c319b4d7SVasiliy Kulikov out:
844c319b4d7SVasiliy Kulikov 	ip_rt_put(rt);
845c319b4d7SVasiliy Kulikov 	if (free)
846c319b4d7SVasiliy Kulikov 		kfree(ipc.opt);
847c319b4d7SVasiliy Kulikov 	if (!err) {
848c319b4d7SVasiliy Kulikov 		icmp_out_count(sock_net(sk), user_icmph.type);
849c319b4d7SVasiliy Kulikov 		return len;
850c319b4d7SVasiliy Kulikov 	}
851c319b4d7SVasiliy Kulikov 	return err;
852c319b4d7SVasiliy Kulikov 
853c319b4d7SVasiliy Kulikov do_confirm:
8540dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
8550dec879fSJulian Anastasov 		dst_confirm_neigh(&rt->dst, &fl4.daddr);
856c319b4d7SVasiliy Kulikov 	if (!(msg->msg_flags & MSG_PROBE) || len)
857c319b4d7SVasiliy Kulikov 		goto back_from_confirm;
858c319b4d7SVasiliy Kulikov 	err = 0;
859c319b4d7SVasiliy Kulikov 	goto out;
860c319b4d7SVasiliy Kulikov }
861c319b4d7SVasiliy Kulikov 
8621b784140SYing Xue int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
8631b784140SYing Xue 		 int flags, int *addr_len)
864c319b4d7SVasiliy Kulikov {
865c319b4d7SVasiliy Kulikov 	struct inet_sock *isk = inet_sk(sk);
8666d0bfe22SLorenzo Colitti 	int family = sk->sk_family;
867c319b4d7SVasiliy Kulikov 	struct sk_buff *skb;
868c319b4d7SVasiliy Kulikov 	int copied, err;
869c319b4d7SVasiliy Kulikov 
870c319b4d7SVasiliy Kulikov 	pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
871c319b4d7SVasiliy Kulikov 
872a5e7424dSDavid S. Miller 	err = -EOPNOTSUPP;
873c319b4d7SVasiliy Kulikov 	if (flags & MSG_OOB)
874c319b4d7SVasiliy Kulikov 		goto out;
875c319b4d7SVasiliy Kulikov 
876f4713a3dSWillem de Bruijn 	if (flags & MSG_ERRQUEUE)
877f4713a3dSWillem de Bruijn 		return inet_recv_error(sk, msg, len, addr_len);
878c319b4d7SVasiliy Kulikov 
879c319b4d7SVasiliy Kulikov 	skb = skb_recv_datagram(sk, flags, noblock, &err);
880c319b4d7SVasiliy Kulikov 	if (!skb)
881c319b4d7SVasiliy Kulikov 		goto out;
882c319b4d7SVasiliy Kulikov 
883c319b4d7SVasiliy Kulikov 	copied = skb->len;
884c319b4d7SVasiliy Kulikov 	if (copied > len) {
885c319b4d7SVasiliy Kulikov 		msg->msg_flags |= MSG_TRUNC;
886c319b4d7SVasiliy Kulikov 		copied = len;
887c319b4d7SVasiliy Kulikov 	}
888c319b4d7SVasiliy Kulikov 
889c319b4d7SVasiliy Kulikov 	/* Don't bother checking the checksum */
89051f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
891c319b4d7SVasiliy Kulikov 	if (err)
892c319b4d7SVasiliy Kulikov 		goto done;
893c319b4d7SVasiliy Kulikov 
894c319b4d7SVasiliy Kulikov 	sock_recv_timestamp(msg, sk, skb);
895c319b4d7SVasiliy Kulikov 
8966d0bfe22SLorenzo Colitti 	/* Copy the address and add cmsg data. */
8976d0bfe22SLorenzo Colitti 	if (family == AF_INET) {
898342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
899bceaa902SHannes Frederic Sowa 
900cf970c00SHannes Frederic Sowa 		if (sin) {
901c319b4d7SVasiliy Kulikov 			sin->sin_family = AF_INET;
902c319b4d7SVasiliy Kulikov 			sin->sin_port = 0 /* skb->h.uh->source */;
903c319b4d7SVasiliy Kulikov 			sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
904c319b4d7SVasiliy Kulikov 			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
905bceaa902SHannes Frederic Sowa 			*addr_len = sizeof(*sin);
906cf970c00SHannes Frederic Sowa 		}
9076d0bfe22SLorenzo Colitti 
908c319b4d7SVasiliy Kulikov 		if (isk->cmsg_flags)
909c319b4d7SVasiliy Kulikov 			ip_cmsg_recv(msg, skb);
9106d0bfe22SLorenzo Colitti 
9116d0bfe22SLorenzo Colitti #if IS_ENABLED(CONFIG_IPV6)
9126d0bfe22SLorenzo Colitti 	} else if (family == AF_INET6) {
9136d0bfe22SLorenzo Colitti 		struct ipv6_pinfo *np = inet6_sk(sk);
9146d0bfe22SLorenzo Colitti 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
915342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
916bceaa902SHannes Frederic Sowa 
917cf970c00SHannes Frederic Sowa 		if (sin6) {
9186d0bfe22SLorenzo Colitti 			sin6->sin6_family = AF_INET6;
9196d0bfe22SLorenzo Colitti 			sin6->sin6_port = 0;
9206d0bfe22SLorenzo Colitti 			sin6->sin6_addr = ip6->saddr;
921c26d6b46SCong Wang 			sin6->sin6_flowinfo = 0;
9226d0bfe22SLorenzo Colitti 			if (np->sndflow)
9236d0bfe22SLorenzo Colitti 				sin6->sin6_flowinfo = ip6_flowinfo(ip6);
924cf970c00SHannes Frederic Sowa 			sin6->sin6_scope_id =
925cf970c00SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
9264330487aSDuan Jiong 						    inet6_iif(skb));
927bceaa902SHannes Frederic Sowa 			*addr_len = sizeof(*sin6);
928cf970c00SHannes Frederic Sowa 		}
9296d0bfe22SLorenzo Colitti 
9306d0bfe22SLorenzo Colitti 		if (inet6_sk(sk)->rxopt.all)
9314b261c75SHannes Frederic Sowa 			pingv6_ops.ip6_datagram_recv_common_ctl(sk, msg, skb);
9324b261c75SHannes Frederic Sowa 		if (skb->protocol == htons(ETH_P_IPV6) &&
9334b261c75SHannes Frederic Sowa 		    inet6_sk(sk)->rxopt.all)
9344b261c75SHannes Frederic Sowa 			pingv6_ops.ip6_datagram_recv_specific_ctl(sk, msg, skb);
9354b261c75SHannes Frederic Sowa 		else if (skb->protocol == htons(ETH_P_IP) && isk->cmsg_flags)
9364b261c75SHannes Frederic Sowa 			ip_cmsg_recv(msg, skb);
9376d0bfe22SLorenzo Colitti #endif
9386d0bfe22SLorenzo Colitti 	} else {
9396d0bfe22SLorenzo Colitti 		BUG();
9406d0bfe22SLorenzo Colitti 	}
9416d0bfe22SLorenzo Colitti 
942c319b4d7SVasiliy Kulikov 	err = copied;
943c319b4d7SVasiliy Kulikov 
944c319b4d7SVasiliy Kulikov done:
945c319b4d7SVasiliy Kulikov 	skb_free_datagram(sk, skb);
946c319b4d7SVasiliy Kulikov out:
947c319b4d7SVasiliy Kulikov 	pr_debug("ping_recvmsg -> %d\n", err);
948c319b4d7SVasiliy Kulikov 	return err;
949c319b4d7SVasiliy Kulikov }
9506d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_recvmsg);
951c319b4d7SVasiliy Kulikov 
9526d0bfe22SLorenzo Colitti int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
953c319b4d7SVasiliy Kulikov {
954c319b4d7SVasiliy Kulikov 	pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
955c319b4d7SVasiliy Kulikov 		 inet_sk(sk), inet_sk(sk)->inet_num, skb);
956c319b4d7SVasiliy Kulikov 	if (sock_queue_rcv_skb(sk, skb) < 0) {
957c319b4d7SVasiliy Kulikov 		kfree_skb(skb);
958c319b4d7SVasiliy Kulikov 		pr_debug("ping_queue_rcv_skb -> failed\n");
959c319b4d7SVasiliy Kulikov 		return -1;
960c319b4d7SVasiliy Kulikov 	}
961c319b4d7SVasiliy Kulikov 	return 0;
962c319b4d7SVasiliy Kulikov }
9636d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
964c319b4d7SVasiliy Kulikov 
965c319b4d7SVasiliy Kulikov 
966c319b4d7SVasiliy Kulikov /*
967c319b4d7SVasiliy Kulikov  *	All we need to do is get the socket.
968c319b4d7SVasiliy Kulikov  */
969c319b4d7SVasiliy Kulikov 
970e3e32170SRick Jones bool ping_rcv(struct sk_buff *skb)
971c319b4d7SVasiliy Kulikov {
972c319b4d7SVasiliy Kulikov 	struct sock *sk;
973c319b4d7SVasiliy Kulikov 	struct net *net = dev_net(skb->dev);
974c319b4d7SVasiliy Kulikov 	struct icmphdr *icmph = icmp_hdr(skb);
975c319b4d7SVasiliy Kulikov 
976c319b4d7SVasiliy Kulikov 	/* We assume the packet has already been checked by icmp_rcv */
977c319b4d7SVasiliy Kulikov 
978c319b4d7SVasiliy Kulikov 	pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
979c319b4d7SVasiliy Kulikov 		 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
980c319b4d7SVasiliy Kulikov 
981c319b4d7SVasiliy Kulikov 	/* Push ICMP header back */
982c319b4d7SVasiliy Kulikov 	skb_push(skb, skb->data - (u8 *)icmph);
983c319b4d7SVasiliy Kulikov 
9846d0bfe22SLorenzo Colitti 	sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
98500db4124SIan Morris 	if (sk) {
986fc752f1fSsubashab@codeaurora.org 		struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
987fc752f1fSsubashab@codeaurora.org 
988c319b4d7SVasiliy Kulikov 		pr_debug("rcv on socket %p\n", sk);
989fc752f1fSsubashab@codeaurora.org 		if (skb2)
990fc752f1fSsubashab@codeaurora.org 			ping_queue_rcv_skb(sk, skb2);
991c319b4d7SVasiliy Kulikov 		sock_put(sk);
992e3e32170SRick Jones 		return true;
993c319b4d7SVasiliy Kulikov 	}
994c319b4d7SVasiliy Kulikov 	pr_debug("no socket, dropping\n");
995c319b4d7SVasiliy Kulikov 
996e3e32170SRick Jones 	return false;
997c319b4d7SVasiliy Kulikov }
9986d0bfe22SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_rcv);
999c319b4d7SVasiliy Kulikov 
1000c319b4d7SVasiliy Kulikov struct proto ping_prot = {
1001c319b4d7SVasiliy Kulikov 	.name =		"PING",
1002c319b4d7SVasiliy Kulikov 	.owner =	THIS_MODULE,
1003c319b4d7SVasiliy Kulikov 	.init =		ping_init_sock,
1004c319b4d7SVasiliy Kulikov 	.close =	ping_close,
1005c319b4d7SVasiliy Kulikov 	.connect =	ip4_datagram_connect,
1006286c72deSEric Dumazet 	.disconnect =	__udp_disconnect,
1007c319b4d7SVasiliy Kulikov 	.setsockopt =	ip_setsockopt,
1008c319b4d7SVasiliy Kulikov 	.getsockopt =	ip_getsockopt,
10096d0bfe22SLorenzo Colitti 	.sendmsg =	ping_v4_sendmsg,
1010c319b4d7SVasiliy Kulikov 	.recvmsg =	ping_recvmsg,
1011c319b4d7SVasiliy Kulikov 	.bind =		ping_bind,
1012c319b4d7SVasiliy Kulikov 	.backlog_rcv =	ping_queue_rcv_skb,
10138141ed9fSSteffen Klassert 	.release_cb =	ip4_datagram_release_cb,
10146d0bfe22SLorenzo Colitti 	.hash =		ping_hash,
10156d0bfe22SLorenzo Colitti 	.unhash =	ping_unhash,
10166d0bfe22SLorenzo Colitti 	.get_port =	ping_get_port,
1017c319b4d7SVasiliy Kulikov 	.obj_size =	sizeof(struct inet_sock),
1018c319b4d7SVasiliy Kulikov };
1019c319b4d7SVasiliy Kulikov EXPORT_SYMBOL(ping_prot);
1020c319b4d7SVasiliy Kulikov 
1021c319b4d7SVasiliy Kulikov #ifdef CONFIG_PROC_FS
1022c319b4d7SVasiliy Kulikov 
1023c319b4d7SVasiliy Kulikov static struct sock *ping_get_first(struct seq_file *seq, int start)
1024c319b4d7SVasiliy Kulikov {
1025c319b4d7SVasiliy Kulikov 	struct sock *sk;
1026c319b4d7SVasiliy Kulikov 	struct ping_iter_state *state = seq->private;
1027c319b4d7SVasiliy Kulikov 	struct net *net = seq_file_net(seq);
1028c319b4d7SVasiliy Kulikov 
1029c319b4d7SVasiliy Kulikov 	for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
1030c319b4d7SVasiliy Kulikov 	     ++state->bucket) {
1031c319b4d7SVasiliy Kulikov 		struct hlist_nulls_node *node;
103275e308c8SChangli Gao 		struct hlist_nulls_head *hslot;
103375e308c8SChangli Gao 
103475e308c8SChangli Gao 		hslot = &ping_table.hash[state->bucket];
1035c319b4d7SVasiliy Kulikov 
1036c319b4d7SVasiliy Kulikov 		if (hlist_nulls_empty(hslot))
1037c319b4d7SVasiliy Kulikov 			continue;
1038c319b4d7SVasiliy Kulikov 
1039c319b4d7SVasiliy Kulikov 		sk_nulls_for_each(sk, node, hslot) {
10408cc785f6SLorenzo Colitti 			if (net_eq(sock_net(sk), net) &&
10418cc785f6SLorenzo Colitti 			    sk->sk_family == state->family)
1042c319b4d7SVasiliy Kulikov 				goto found;
1043c319b4d7SVasiliy Kulikov 		}
1044c319b4d7SVasiliy Kulikov 	}
1045c319b4d7SVasiliy Kulikov 	sk = NULL;
1046c319b4d7SVasiliy Kulikov found:
1047c319b4d7SVasiliy Kulikov 	return sk;
1048c319b4d7SVasiliy Kulikov }
1049c319b4d7SVasiliy Kulikov 
1050c319b4d7SVasiliy Kulikov static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1051c319b4d7SVasiliy Kulikov {
1052c319b4d7SVasiliy Kulikov 	struct ping_iter_state *state = seq->private;
1053c319b4d7SVasiliy Kulikov 	struct net *net = seq_file_net(seq);
1054c319b4d7SVasiliy Kulikov 
1055c319b4d7SVasiliy Kulikov 	do {
1056c319b4d7SVasiliy Kulikov 		sk = sk_nulls_next(sk);
1057c319b4d7SVasiliy Kulikov 	} while (sk && (!net_eq(sock_net(sk), net)));
1058c319b4d7SVasiliy Kulikov 
1059c319b4d7SVasiliy Kulikov 	if (!sk)
1060c319b4d7SVasiliy Kulikov 		return ping_get_first(seq, state->bucket + 1);
1061c319b4d7SVasiliy Kulikov 	return sk;
1062c319b4d7SVasiliy Kulikov }
1063c319b4d7SVasiliy Kulikov 
1064c319b4d7SVasiliy Kulikov static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1065c319b4d7SVasiliy Kulikov {
1066c319b4d7SVasiliy Kulikov 	struct sock *sk = ping_get_first(seq, 0);
1067c319b4d7SVasiliy Kulikov 
1068c319b4d7SVasiliy Kulikov 	if (sk)
1069c319b4d7SVasiliy Kulikov 		while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1070c319b4d7SVasiliy Kulikov 			--pos;
1071c319b4d7SVasiliy Kulikov 	return pos ? NULL : sk;
1072c319b4d7SVasiliy Kulikov }
1073c319b4d7SVasiliy Kulikov 
1074d862e546SLorenzo Colitti void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
1075ad64b8beSLance Richardson 	__acquires(ping_table.lock)
1076c319b4d7SVasiliy Kulikov {
1077c319b4d7SVasiliy Kulikov 	struct ping_iter_state *state = seq->private;
1078c319b4d7SVasiliy Kulikov 	state->bucket = 0;
10798cc785f6SLorenzo Colitti 	state->family = family;
1080c319b4d7SVasiliy Kulikov 
1081c319b4d7SVasiliy Kulikov 	read_lock_bh(&ping_table.lock);
1082c319b4d7SVasiliy Kulikov 
1083c319b4d7SVasiliy Kulikov 	return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1084c319b4d7SVasiliy Kulikov }
1085d862e546SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_seq_start);
1086c319b4d7SVasiliy Kulikov 
10878cc785f6SLorenzo Colitti static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
10888cc785f6SLorenzo Colitti {
10898cc785f6SLorenzo Colitti 	return ping_seq_start(seq, pos, AF_INET);
10908cc785f6SLorenzo Colitti }
10918cc785f6SLorenzo Colitti 
1092d862e546SLorenzo Colitti void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1093c319b4d7SVasiliy Kulikov {
1094c319b4d7SVasiliy Kulikov 	struct sock *sk;
1095c319b4d7SVasiliy Kulikov 
1096c319b4d7SVasiliy Kulikov 	if (v == SEQ_START_TOKEN)
1097c319b4d7SVasiliy Kulikov 		sk = ping_get_idx(seq, 0);
1098c319b4d7SVasiliy Kulikov 	else
1099c319b4d7SVasiliy Kulikov 		sk = ping_get_next(seq, v);
1100c319b4d7SVasiliy Kulikov 
1101c319b4d7SVasiliy Kulikov 	++*pos;
1102c319b4d7SVasiliy Kulikov 	return sk;
1103c319b4d7SVasiliy Kulikov }
1104d862e546SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_seq_next);
1105c319b4d7SVasiliy Kulikov 
1106d862e546SLorenzo Colitti void ping_seq_stop(struct seq_file *seq, void *v)
1107ad64b8beSLance Richardson 	__releases(ping_table.lock)
1108c319b4d7SVasiliy Kulikov {
1109c319b4d7SVasiliy Kulikov 	read_unlock_bh(&ping_table.lock);
1110c319b4d7SVasiliy Kulikov }
1111d862e546SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_seq_stop);
1112c319b4d7SVasiliy Kulikov 
11138cc785f6SLorenzo Colitti static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1114652586dfSTetsuo Handa 		int bucket)
1115c319b4d7SVasiliy Kulikov {
1116c319b4d7SVasiliy Kulikov 	struct inet_sock *inet = inet_sk(sp);
1117c319b4d7SVasiliy Kulikov 	__be32 dest = inet->inet_daddr;
1118c319b4d7SVasiliy Kulikov 	__be32 src = inet->inet_rcv_saddr;
1119c319b4d7SVasiliy Kulikov 	__u16 destp = ntohs(inet->inet_dport);
1120c319b4d7SVasiliy Kulikov 	__u16 srcp = ntohs(inet->inet_sport);
1121c319b4d7SVasiliy Kulikov 
1122c319b4d7SVasiliy Kulikov 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1123652586dfSTetsuo Handa 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
1124c319b4d7SVasiliy Kulikov 		bucket, src, srcp, dest, destp, sp->sk_state,
1125c319b4d7SVasiliy Kulikov 		sk_wmem_alloc_get(sp),
1126c319b4d7SVasiliy Kulikov 		sk_rmem_alloc_get(sp),
1127a7cb5a49SEric W. Biederman 		0, 0L, 0,
1128a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
1129a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
1130c319b4d7SVasiliy Kulikov 		atomic_read(&sp->sk_refcnt), sp,
1131652586dfSTetsuo Handa 		atomic_read(&sp->sk_drops));
1132c319b4d7SVasiliy Kulikov }
1133c319b4d7SVasiliy Kulikov 
11348cc785f6SLorenzo Colitti static int ping_v4_seq_show(struct seq_file *seq, void *v)
1135c319b4d7SVasiliy Kulikov {
1136652586dfSTetsuo Handa 	seq_setwidth(seq, 127);
1137c319b4d7SVasiliy Kulikov 	if (v == SEQ_START_TOKEN)
1138652586dfSTetsuo Handa 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
1139c319b4d7SVasiliy Kulikov 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
1140c319b4d7SVasiliy Kulikov 			   "inode ref pointer drops");
1141c319b4d7SVasiliy Kulikov 	else {
1142c319b4d7SVasiliy Kulikov 		struct ping_iter_state *state = seq->private;
1143c319b4d7SVasiliy Kulikov 
1144652586dfSTetsuo Handa 		ping_v4_format_sock(v, seq, state->bucket);
1145c319b4d7SVasiliy Kulikov 	}
1146652586dfSTetsuo Handa 	seq_pad(seq, '\n');
1147c319b4d7SVasiliy Kulikov 	return 0;
1148c319b4d7SVasiliy Kulikov }
1149c319b4d7SVasiliy Kulikov 
1150c319b4d7SVasiliy Kulikov static int ping_seq_open(struct inode *inode, struct file *file)
1151c319b4d7SVasiliy Kulikov {
11528cc785f6SLorenzo Colitti 	struct ping_seq_afinfo *afinfo = PDE_DATA(inode);
11538cc785f6SLorenzo Colitti 	return seq_open_net(inode, file, &afinfo->seq_ops,
1154c319b4d7SVasiliy Kulikov 			   sizeof(struct ping_iter_state));
1155c319b4d7SVasiliy Kulikov }
1156c319b4d7SVasiliy Kulikov 
1157d862e546SLorenzo Colitti const struct file_operations ping_seq_fops = {
1158c319b4d7SVasiliy Kulikov 	.open		= ping_seq_open,
1159c319b4d7SVasiliy Kulikov 	.read		= seq_read,
1160c319b4d7SVasiliy Kulikov 	.llseek		= seq_lseek,
1161c319b4d7SVasiliy Kulikov 	.release	= seq_release_net,
1162c319b4d7SVasiliy Kulikov };
1163d862e546SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_seq_fops);
1164c319b4d7SVasiliy Kulikov 
11658cc785f6SLorenzo Colitti static struct ping_seq_afinfo ping_v4_seq_afinfo = {
11668cc785f6SLorenzo Colitti 	.name		= "icmp",
11678cc785f6SLorenzo Colitti 	.family		= AF_INET,
11688cc785f6SLorenzo Colitti 	.seq_fops	= &ping_seq_fops,
11698cc785f6SLorenzo Colitti 	.seq_ops	= {
11708cc785f6SLorenzo Colitti 		.start		= ping_v4_seq_start,
11718cc785f6SLorenzo Colitti 		.show		= ping_v4_seq_show,
11728cc785f6SLorenzo Colitti 		.next		= ping_seq_next,
11738cc785f6SLorenzo Colitti 		.stop		= ping_seq_stop,
11748cc785f6SLorenzo Colitti 	},
11758cc785f6SLorenzo Colitti };
11768cc785f6SLorenzo Colitti 
1177d862e546SLorenzo Colitti int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo)
1178c319b4d7SVasiliy Kulikov {
1179c319b4d7SVasiliy Kulikov 	struct proc_dir_entry *p;
11808cc785f6SLorenzo Colitti 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
11818cc785f6SLorenzo Colitti 			     afinfo->seq_fops, afinfo);
1182c319b4d7SVasiliy Kulikov 	if (!p)
11838cc785f6SLorenzo Colitti 		return -ENOMEM;
11848cc785f6SLorenzo Colitti 	return 0;
1185c319b4d7SVasiliy Kulikov }
1186d862e546SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_proc_register);
1187c319b4d7SVasiliy Kulikov 
1188d862e546SLorenzo Colitti void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo)
1189c319b4d7SVasiliy Kulikov {
11908cc785f6SLorenzo Colitti 	remove_proc_entry(afinfo->name, net->proc_net);
1191c319b4d7SVasiliy Kulikov }
1192d862e546SLorenzo Colitti EXPORT_SYMBOL_GPL(ping_proc_unregister);
1193c319b4d7SVasiliy Kulikov 
11948cc785f6SLorenzo Colitti static int __net_init ping_v4_proc_init_net(struct net *net)
1195c319b4d7SVasiliy Kulikov {
11968cc785f6SLorenzo Colitti 	return ping_proc_register(net, &ping_v4_seq_afinfo);
1197c319b4d7SVasiliy Kulikov }
1198c319b4d7SVasiliy Kulikov 
11998cc785f6SLorenzo Colitti static void __net_exit ping_v4_proc_exit_net(struct net *net)
1200c319b4d7SVasiliy Kulikov {
12018cc785f6SLorenzo Colitti 	ping_proc_unregister(net, &ping_v4_seq_afinfo);
1202c319b4d7SVasiliy Kulikov }
1203c319b4d7SVasiliy Kulikov 
12048cc785f6SLorenzo Colitti static struct pernet_operations ping_v4_net_ops = {
12058cc785f6SLorenzo Colitti 	.init = ping_v4_proc_init_net,
12068cc785f6SLorenzo Colitti 	.exit = ping_v4_proc_exit_net,
1207c319b4d7SVasiliy Kulikov };
1208c319b4d7SVasiliy Kulikov 
1209c319b4d7SVasiliy Kulikov int __init ping_proc_init(void)
1210c319b4d7SVasiliy Kulikov {
12118cc785f6SLorenzo Colitti 	return register_pernet_subsys(&ping_v4_net_ops);
1212c319b4d7SVasiliy Kulikov }
1213c319b4d7SVasiliy Kulikov 
1214c319b4d7SVasiliy Kulikov void ping_proc_exit(void)
1215c319b4d7SVasiliy Kulikov {
12168cc785f6SLorenzo Colitti 	unregister_pernet_subsys(&ping_v4_net_ops);
1217c319b4d7SVasiliy Kulikov }
1218c319b4d7SVasiliy Kulikov 
1219c319b4d7SVasiliy Kulikov #endif
1220c319b4d7SVasiliy Kulikov 
1221c319b4d7SVasiliy Kulikov void __init ping_init(void)
1222c319b4d7SVasiliy Kulikov {
1223c319b4d7SVasiliy Kulikov 	int i;
1224c319b4d7SVasiliy Kulikov 
1225c319b4d7SVasiliy Kulikov 	for (i = 0; i < PING_HTABLE_SIZE; i++)
1226c319b4d7SVasiliy Kulikov 		INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1227c319b4d7SVasiliy Kulikov 	rwlock_init(&ping_table.lock);
1228c319b4d7SVasiliy Kulikov }
1229