blob: 83ebbe2a003cc0ed31f3fea5e3ff3c7f3b04a4d5 [file] [log] [blame]
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001/*
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
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000023#include <linux/uaccess.h>
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000024#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>
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000036#include <net/icmp.h>
37#include <net/protocol.h>
38#include <linux/skbuff.h>
39#include <linux/proc_fs.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040040#include <linux/export.h>
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000041#include <net/sock.h>
42#include <net/ping.h>
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000043#include <net/udp.h>
44#include <net/route.h>
45#include <net/inet_common.h>
46#include <net/checksum.h>
47
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +000048#if IS_ENABLED(CONFIG_IPV6)
49#include <linux/in6.h>
50#include <linux/icmpv6.h>
51#include <net/addrconf.h>
52#include <net/ipv6.h>
53#include <net/transp_v6.h>
54#endif
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000055
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +000056
57struct ping_table ping_table;
58struct pingv6_ops pingv6_ops;
59EXPORT_SYMBOL_GPL(pingv6_ops);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000060
Eric Dumazet1b1cb1f2011-05-13 22:59:19 +000061static u16 ping_port_rover;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000062
63static inline int ping_hashfn(struct net *net, unsigned num, unsigned mask)
64{
65 int res = (num + net_hash_mix(net)) & mask;
66 pr_debug("hash(%d) = %d\n", num, res);
67 return res;
68}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +000069EXPORT_SYMBOL_GPL(ping_hash);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000070
71static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
72 struct net *net, unsigned num)
73{
74 return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
75}
76
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +000077int ping_get_port(struct sock *sk, unsigned short ident)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000078{
79 struct hlist_nulls_node *node;
80 struct hlist_nulls_head *hlist;
81 struct inet_sock *isk, *isk2;
82 struct sock *sk2 = NULL;
83
84 isk = inet_sk(sk);
85 write_lock_bh(&ping_table.lock);
86 if (ident == 0) {
87 u32 i;
88 u16 result = ping_port_rover + 1;
89
90 for (i = 0; i < (1L << 16); i++, result++) {
91 if (!result)
92 result++; /* avoid zero */
93 hlist = ping_hashslot(&ping_table, sock_net(sk),
94 result);
95 ping_portaddr_for_each_entry(sk2, node, hlist) {
96 isk2 = inet_sk(sk2);
97
98 if (isk2->inet_num == result)
99 goto next_port;
100 }
101
102 /* found */
103 ping_port_rover = ident = result;
104 break;
105next_port:
106 ;
107 }
108 if (i >= (1L << 16))
109 goto fail;
110 } else {
111 hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
112 ping_portaddr_for_each_entry(sk2, node, hlist) {
113 isk2 = inet_sk(sk2);
114
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000115 /* BUG? Why is this reuse and not reuseaddr? ping.c
116 * doesn't turn off SO_REUSEADDR, and it doesn't expect
117 * that other ping processes can steal its packets.
118 */
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000119 if ((isk2->inet_num == ident) &&
120 (sk2 != sk) &&
121 (!sk2->sk_reuse || !sk->sk_reuse))
122 goto fail;
123 }
124 }
125
126 pr_debug("found port/ident = %d\n", ident);
127 isk->inet_num = ident;
128 if (sk_unhashed(sk)) {
129 pr_debug("was not hashed\n");
130 sock_hold(sk);
131 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
132 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
133 }
134 write_unlock_bh(&ping_table.lock);
135 return 0;
136
137fail:
138 write_unlock_bh(&ping_table.lock);
139 return 1;
140}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000141EXPORT_SYMBOL_GPL(ping_get_port);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000142
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000143void ping_hash(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000144{
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000145 pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000146 BUG(); /* "Please do not press this button again." */
147}
148
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000149void ping_unhash(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000150{
151 struct inet_sock *isk = inet_sk(sk);
Eric Dumazetb706b2e2017-03-24 19:36:13 -0700152
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000153 pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
Eric Dumazetb706b2e2017-03-24 19:36:13 -0700154 write_lock_bh(&ping_table.lock);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000155 if (sk_hashed(sk)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000156 hlist_nulls_del(&sk->sk_nulls_node);
David S. Millerfdf459e2015-05-01 22:02:47 -0400157 sk_nulls_node_init(&sk->sk_nulls_node);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000158 sock_put(sk);
Eric Dumazet747465e2012-01-16 19:27:39 +0000159 isk->inet_num = 0;
160 isk->inet_sport = 0;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000161 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000162 }
Eric Dumazetb706b2e2017-03-24 19:36:13 -0700163 write_unlock_bh(&ping_table.lock);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000164}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000165EXPORT_SYMBOL_GPL(ping_unhash);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000166
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000167static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000168{
169 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
170 struct sock *sk = NULL;
171 struct inet_sock *isk;
172 struct hlist_nulls_node *hnode;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000173 int dif = skb->dev->ifindex;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000174
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000175 if (skb->protocol == htons(ETH_P_IP)) {
176 pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
177 (int)ident, &ip_hdr(skb)->daddr, dif);
178#if IS_ENABLED(CONFIG_IPV6)
179 } else if (skb->protocol == htons(ETH_P_IPV6)) {
180 pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
181 (int)ident, &ipv6_hdr(skb)->daddr, dif);
182#endif
183 }
184
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000185 read_lock_bh(&ping_table.lock);
186
187 ping_portaddr_for_each_entry(sk, hnode, hslot) {
188 isk = inet_sk(sk);
189
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000190 pr_debug("iterate\n");
191 if (isk->inet_num != ident)
192 continue;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000193
194 if (skb->protocol == htons(ETH_P_IP) &&
195 sk->sk_family == AF_INET) {
196 pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
197 (int) isk->inet_num, &isk->inet_rcv_saddr,
198 sk->sk_bound_dev_if);
199
200 if (isk->inet_rcv_saddr &&
201 isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
202 continue;
203#if IS_ENABLED(CONFIG_IPV6)
204 } else if (skb->protocol == htons(ETH_P_IPV6) &&
205 sk->sk_family == AF_INET6) {
206 struct ipv6_pinfo *np = inet6_sk(sk);
207
208 pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
209 (int) isk->inet_num,
210 &inet6_sk(sk)->rcv_saddr,
211 sk->sk_bound_dev_if);
212
213 if (!ipv6_addr_any(&np->rcv_saddr) &&
214 !ipv6_addr_equal(&np->rcv_saddr,
215 &ipv6_hdr(skb)->daddr))
216 continue;
217#endif
Jane Zhou432bc4d2014-11-24 11:44:08 -0800218 } else {
219 continue;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000220 }
221
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000222 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
223 continue;
224
225 sock_hold(sk);
226 goto exit;
227 }
228
229 sk = NULL;
230exit:
231 read_unlock_bh(&ping_table.lock);
232
233 return sk;
234}
235
Changli Gao75e308c2011-05-18 21:16:01 +0000236static void inet_get_ping_group_range_net(struct net *net, gid_t *low,
237 gid_t *high)
Vasiliy Kulikovf56e03e2011-05-17 00:16:56 +0000238{
239 gid_t *data = net->ipv4.sysctl_ping_group_range;
240 unsigned seq;
241 do {
242 seq = read_seqbegin(&sysctl_local_ports.lock);
243
244 *low = data[0];
245 *high = data[1];
246 } while (read_seqretry(&sysctl_local_ports.lock, seq));
247}
248
249
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000250int ping_init_sock(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000251{
252 struct net *net = sock_net(sk);
253 gid_t group = current_egid();
254 gid_t range[2];
Wang, Xiaoming62e06b82014-04-14 12:30:45 -0400255 struct group_info *group_info;
256 int i, j, count ;
257 int ret = 0;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000258
Lorenzo Colittifd42b742015-03-03 23:16:16 +0900259 if (sk->sk_family == AF_INET6)
260 inet6_sk(sk)->ipv6only = 1;
261
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000262 inet_get_ping_group_range_net(net, range, range+1);
263 if (range[0] <= group && group <= range[1])
264 return 0;
265
Wang, Xiaoming62e06b82014-04-14 12:30:45 -0400266 group_info = get_current_groups();
267 count = group_info->ngroups;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000268 for (i = 0; i < group_info->nblocks; i++) {
269 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
270
271 for (j = 0; j < cp_count; j++) {
272 group = group_info->blocks[i][j];
273 if (range[0] <= group && group <= range[1])
Wang, Xiaoming62e06b82014-04-14 12:30:45 -0400274 goto out_release_group;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000275 }
276
277 count -= cp_count;
278 }
279
Wang, Xiaoming62e06b82014-04-14 12:30:45 -0400280 ret = -EACCES;
281
282out_release_group:
283 put_group_info(group_info);
284 return ret;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000285}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000286EXPORT_SYMBOL_GPL(ping_init_sock);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000287
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000288void ping_close(struct sock *sk, long timeout)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000289{
290 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000291 inet_sk(sk), inet_sk(sk)->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000292 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
293
294 sk_common_release(sk);
295}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000296EXPORT_SYMBOL_GPL(ping_close);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000297
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000298/* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
299int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
300 struct sockaddr *uaddr, int addr_len) {
301 struct net *net = sock_net(sk);
302 if (sk->sk_family == AF_INET) {
303 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
304 int chk_addr_ret;
305
306 if (addr_len < sizeof(*addr))
307 return -EINVAL;
308
Lorenzo Colittifd42b742015-03-03 23:16:16 +0900309 if (addr->sin_family != AF_INET &&
310 !(addr->sin_family == AF_UNSPEC &&
311 addr->sin_addr.s_addr == htonl(INADDR_ANY)))
312 return -EAFNOSUPPORT;
313
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000314 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
315 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
316
317 chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
318
319 if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
320 chk_addr_ret = RTN_LOCAL;
321
322 if ((sysctl_ip_nonlocal_bind == 0 &&
323 isk->freebind == 0 && isk->transparent == 0 &&
324 chk_addr_ret != RTN_LOCAL) ||
325 chk_addr_ret == RTN_MULTICAST ||
326 chk_addr_ret == RTN_BROADCAST)
327 return -EADDRNOTAVAIL;
328
329#if IS_ENABLED(CONFIG_IPV6)
330 } else if (sk->sk_family == AF_INET6) {
331 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
332 int addr_type, scoped, has_addr;
333 struct net_device *dev = NULL;
334
335 if (addr_len < sizeof(*addr))
336 return -EINVAL;
337
Lorenzo Colittifd42b742015-03-03 23:16:16 +0900338 if (addr->sin6_family != AF_INET6)
339 return -EAFNOSUPPORT;
340
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000341 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
342 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
343
344 addr_type = ipv6_addr_type(&addr->sin6_addr);
345 scoped = __ipv6_addr_needs_scope_id(addr_type);
346 if ((addr_type != IPV6_ADDR_ANY &&
347 !(addr_type & IPV6_ADDR_UNICAST)) ||
348 (scoped && !addr->sin6_scope_id))
349 return -EINVAL;
350
351 rcu_read_lock();
352 if (addr->sin6_scope_id) {
353 dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
354 if (!dev) {
355 rcu_read_unlock();
356 return -ENODEV;
357 }
358 }
359 has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
360 scoped);
361 rcu_read_unlock();
362
363 if (!(isk->freebind || isk->transparent || has_addr ||
364 addr_type == IPV6_ADDR_ANY))
365 return -EADDRNOTAVAIL;
366
367 if (scoped)
368 sk->sk_bound_dev_if = addr->sin6_scope_id;
369#endif
370 } else {
371 return -EAFNOSUPPORT;
372 }
373 return 0;
374}
375
376void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
377{
378 if (saddr->sa_family == AF_INET) {
379 struct inet_sock *isk = inet_sk(sk);
380 struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
381 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
382#if IS_ENABLED(CONFIG_IPV6)
383 } else if (saddr->sa_family == AF_INET6) {
384 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
385 struct ipv6_pinfo *np = inet6_sk(sk);
386 np->rcv_saddr = np->saddr = addr->sin6_addr;
387#endif
388 }
389}
390
391void ping_clear_saddr(struct sock *sk, int dif)
392{
393 sk->sk_bound_dev_if = dif;
394 if (sk->sk_family == AF_INET) {
395 struct inet_sock *isk = inet_sk(sk);
396 isk->inet_rcv_saddr = isk->inet_saddr = 0;
397#if IS_ENABLED(CONFIG_IPV6)
398 } else if (sk->sk_family == AF_INET6) {
399 struct ipv6_pinfo *np = inet6_sk(sk);
400 memset(&np->rcv_saddr, 0, sizeof(np->rcv_saddr));
401 memset(&np->saddr, 0, sizeof(np->saddr));
402#endif
403 }
404}
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000405/*
406 * We need our own bind because there are no privileged id's == local ports.
407 * Moreover, we don't allow binding to multi- and broadcast addresses.
408 */
409
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000410int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000411{
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000412 struct inet_sock *isk = inet_sk(sk);
413 unsigned short snum;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000414 int err;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000415 int dif = sk->sk_bound_dev_if;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000416
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000417 err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
418 if (err)
419 return err;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000420
421 lock_sock(sk);
422
423 err = -EINVAL;
424 if (isk->inet_num != 0)
425 goto out;
426
427 err = -EADDRINUSE;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000428 ping_set_saddr(sk, uaddr);
429 snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
430 if (ping_get_port(sk, snum) != 0) {
431 ping_clear_saddr(sk, dif);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000432 goto out;
433 }
434
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000435 pr_debug("after bind(): num = %d, dif = %d\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000436 (int)isk->inet_num,
Joe Perches058bd4d2012-03-11 18:36:11 +0000437 (int)sk->sk_bound_dev_if);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000438
439 err = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000440 if ((sk->sk_family == AF_INET && isk->inet_rcv_saddr) ||
441 (sk->sk_family == AF_INET6 &&
442 !ipv6_addr_any(&inet6_sk(sk)->rcv_saddr)))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000443 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000444
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000445 if (snum)
446 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
447 isk->inet_sport = htons(isk->inet_num);
448 isk->inet_daddr = 0;
449 isk->inet_dport = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000450
451#if IS_ENABLED(CONFIG_IPV6)
452 if (sk->sk_family == AF_INET6)
453 memset(&inet6_sk(sk)->daddr, 0, sizeof(inet6_sk(sk)->daddr));
454#endif
455
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000456 sk_dst_reset(sk);
457out:
458 release_sock(sk);
459 pr_debug("ping_v4_bind -> %d\n", err);
460 return err;
461}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000462EXPORT_SYMBOL_GPL(ping_bind);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000463
464/*
465 * Is this a supported type of ICMP message?
466 */
467
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000468static inline int ping_supported(int family, int type, int code)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000469{
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000470 return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
471 (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000472}
473
474/*
475 * This routine is called by the ICMP module when it gets some
476 * sort of error condition.
477 */
478
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000479void ping_err(struct sk_buff *skb, int offset, u32 info)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000480{
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000481 int family;
482 struct icmphdr *icmph;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000483 struct inet_sock *inet_sock;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000484 int type;
485 int code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000486 struct net *net = dev_net(skb->dev);
487 struct sock *sk;
488 int harderr;
489 int err;
490
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000491 if (skb->protocol == htons(ETH_P_IP)) {
492 struct iphdr *iph = (struct iphdr *)skb->data;
493 offset = iph->ihl << 2;
494 family = AF_INET;
495 type = icmp_hdr(skb)->type;
496 code = icmp_hdr(skb)->code;
497 icmph = (struct icmphdr *)(skb->data + offset);
498 } else if (skb->protocol == htons(ETH_P_IPV6)) {
499 family = AF_INET6;
500 type = icmp6_hdr(skb)->icmp6_type;
501 code = icmp6_hdr(skb)->icmp6_code;
502 icmph = (struct icmphdr *) (skb->data + offset);
503 } else {
504 BUG();
505 }
506
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000507 /* We assume the packet has already been checked by icmp_unreach */
508
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000509 if (!ping_supported(family, icmph->type, icmph->code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000510 return;
511
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000512 pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
513 skb->protocol, type, code, ntohs(icmph->un.echo.id),
514 ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000515
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000516 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000517 if (sk == NULL) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000518 pr_debug("no socket, dropping\n");
519 return; /* No socket for error */
520 }
521 pr_debug("err on socket %p\n", sk);
522
523 err = 0;
524 harderr = 0;
525 inet_sock = inet_sk(sk);
526
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000527 if (skb->protocol == htons(ETH_P_IP)) {
528 switch (type) {
529 default:
530 case ICMP_TIME_EXCEEDED:
531 err = EHOSTUNREACH;
532 break;
533 case ICMP_SOURCE_QUENCH:
534 /* This is not a real error but ping wants to see it.
535 * Report it with some fake errno. */
536 err = EREMOTEIO;
537 break;
538 case ICMP_PARAMETERPROB:
539 err = EPROTO;
540 harderr = 1;
541 break;
542 case ICMP_DEST_UNREACH:
543 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
544 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
545 err = EMSGSIZE;
546 harderr = 1;
547 break;
548 }
549 goto out;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000550 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000551 err = EHOSTUNREACH;
552 if (code <= NR_ICMP_UNREACH) {
553 harderr = icmp_err_convert[code].fatal;
554 err = icmp_err_convert[code].errno;
555 }
556 break;
557 case ICMP_REDIRECT:
558 /* See ICMP_SOURCE_QUENCH */
559 err = EREMOTEIO;
560 break;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000561 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000562#if IS_ENABLED(CONFIG_IPV6)
563 } else if (skb->protocol == htons(ETH_P_IPV6)) {
564 harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
565#endif
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000566 }
567
568 /*
569 * RFC1122: OK. Passes ICMP errors back to application, as per
570 * 4.1.3.3.
571 */
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000572 if ((family == AF_INET && !inet_sock->recverr) ||
573 (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000574 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
575 goto out;
576 } else {
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000577 if (family == AF_INET) {
578 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
579 info, (u8 *)icmph);
580#if IS_ENABLED(CONFIG_IPV6)
581 } else if (family == AF_INET6) {
582 pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
583 info, (u8 *)icmph);
584#endif
585 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000586 }
587 sk->sk_err = err;
588 sk->sk_error_report(sk);
589out:
590 sock_put(sk);
591}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000592EXPORT_SYMBOL_GPL(ping_err);
593
594void ping_v4_err(struct sk_buff *skb, u32 info)
595{
596 ping_err(skb, 0, info);
597}
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000598
599/*
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000600 * Copy and checksum an ICMP Echo packet from user space into a buffer
601 * starting from the payload.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000602 */
603
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000604int ping_getfrag(void *from, char *to,
605 int offset, int fraglen, int odd, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000606{
607 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
608
609 if (offset == 0) {
610 if (fraglen < sizeof(struct icmphdr))
611 BUG();
612 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
613 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
614 &pfh->wcheck))
615 return -EFAULT;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000616 } else if (offset < sizeof(struct icmphdr)) {
617 BUG();
618 } else {
619 if (csum_partial_copy_fromiovecend
620 (to, pfh->iov, offset - sizeof(struct icmphdr),
621 fraglen, &pfh->wcheck))
622 return -EFAULT;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000623 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000624
625#if IS_ENABLED(CONFIG_IPV6)
626 /* For IPv6, checksum each skb as we go along, as expected by
627 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
628 * wcheck, it will be finalized in ping_v4_push_pending_frames.
629 */
630 if (pfh->family == AF_INET6) {
631 skb->csum = pfh->wcheck;
632 skb->ip_summed = CHECKSUM_NONE;
633 pfh->wcheck = 0;
634 }
635#endif
636
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000637 return 0;
638}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000639EXPORT_SYMBOL_GPL(ping_getfrag);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000640
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000641static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
642 struct flowi4 *fl4)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000643{
644 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
645
646 pfh->wcheck = csum_partial((char *)&pfh->icmph,
647 sizeof(struct icmphdr), pfh->wcheck);
648 pfh->icmph.checksum = csum_fold(pfh->wcheck);
649 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
650 skb->ip_summed = CHECKSUM_NONE;
651 return ip_push_pending_frames(sk, fl4);
652}
653
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000654int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
655 void *user_icmph, size_t icmph_len) {
656 u8 type, code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000657
Kees Cook5d7ca792016-12-05 10:34:38 -0800658 if (len > 0xFFFF || len < icmph_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000659 return -EMSGSIZE;
660
Kees Cook5d7ca792016-12-05 10:34:38 -0800661 /* Must have at least a full ICMP header. */
662 if (len < icmph_len)
663 return -EINVAL;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000664 /*
665 * Check the flags.
666 */
667
668 /* Mirror BSD error message compatibility */
669 if (msg->msg_flags & MSG_OOB)
670 return -EOPNOTSUPP;
671
672 /*
673 * Fetch the ICMP header provided by the userland.
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000674 * iovec is modified! The ICMP header is consumed.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000675 */
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000676 if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000677 return -EFAULT;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000678
679 if (family == AF_INET) {
680 type = ((struct icmphdr *) user_icmph)->type;
681 code = ((struct icmphdr *) user_icmph)->code;
682#if IS_ENABLED(CONFIG_IPV6)
683 } else if (family == AF_INET6) {
684 type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
685 code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
686#endif
687 } else {
688 BUG();
689 }
690
691 if (!ping_supported(family, type, code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000692 return -EINVAL;
693
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000694 return 0;
695}
696EXPORT_SYMBOL_GPL(ping_common_sendmsg);
697
698int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
699 size_t len)
700{
701 struct net *net = sock_net(sk);
702 struct flowi4 fl4;
703 struct inet_sock *inet = inet_sk(sk);
704 struct ipcm_cookie ipc;
705 struct icmphdr user_icmph;
706 struct pingfakehdr pfh;
707 struct rtable *rt = NULL;
708 struct ip_options_data opt_copy;
709 int free = 0;
710 __be32 saddr, daddr, faddr;
711 u8 tos;
712 int err;
713
714 pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
715
716 err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
717 sizeof(user_icmph));
718 if (err)
719 return err;
720
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000721 /*
722 * Get and verify the address.
723 */
724
725 if (msg->msg_name) {
726 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
727 if (msg->msg_namelen < sizeof(*usin))
728 return -EINVAL;
729 if (usin->sin_family != AF_INET)
Lorenzo Colittifd42b742015-03-03 23:16:16 +0900730 return -EAFNOSUPPORT;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000731 daddr = usin->sin_addr.s_addr;
732 /* no remote port */
733 } else {
734 if (sk->sk_state != TCP_ESTABLISHED)
735 return -EDESTADDRREQ;
736 daddr = inet->inet_daddr;
737 /* no remote port */
738 }
739
740 ipc.addr = inet->inet_saddr;
741 ipc.opt = NULL;
742 ipc.oif = sk->sk_bound_dev_if;
743 ipc.tx_flags = 0;
Daniel Borkmann087b1f82013-04-14 08:08:13 +0000744
745 sock_tx_timestamp(sk, &ipc.tx_flags);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000746
747 if (msg->msg_controllen) {
748 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
749 if (err)
750 return err;
751 if (ipc.opt)
752 free = 1;
753 }
754 if (!ipc.opt) {
755 struct ip_options_rcu *inet_opt;
756
757 rcu_read_lock();
758 inet_opt = rcu_dereference(inet->inet_opt);
759 if (inet_opt) {
760 memcpy(&opt_copy, inet_opt,
761 sizeof(*inet_opt) + inet_opt->opt.optlen);
762 ipc.opt = &opt_copy.opt;
763 }
764 rcu_read_unlock();
765 }
766
767 saddr = ipc.addr;
768 ipc.addr = faddr = daddr;
769
770 if (ipc.opt && ipc.opt->opt.srr) {
771 if (!daddr)
772 return -EINVAL;
773 faddr = ipc.opt->opt.faddr;
774 }
775 tos = RT_TOS(inet->tos);
776 if (sock_flag(sk, SOCK_LOCALROUTE) ||
777 (msg->msg_flags & MSG_DONTROUTE) ||
778 (ipc.opt && ipc.opt->opt.is_strictroute)) {
779 tos |= RTO_ONLINK;
780 }
781
782 if (ipv4_is_multicast(daddr)) {
783 if (!ipc.oif)
784 ipc.oif = inet->mc_index;
785 if (!saddr)
786 saddr = inet->mc_addr;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000787 } else if (!ipc.oif)
788 ipc.oif = inet->uc_index;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000789
790 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
791 RT_SCOPE_UNIVERSE, sk->sk_protocol,
chrmhoffmann014132b2018-06-17 15:00:06 +0200792 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000793
794 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
795 rt = ip_route_output_flow(net, &fl4, sk);
796 if (IS_ERR(rt)) {
797 err = PTR_ERR(rt);
798 rt = NULL;
799 if (err == -ENETUNREACH)
Eric Dumazet36157402013-11-28 09:51:22 -0800800 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000801 goto out;
802 }
803
804 err = -EACCES;
805 if ((rt->rt_flags & RTCF_BROADCAST) &&
806 !sock_flag(sk, SOCK_BROADCAST))
807 goto out;
808
809 if (msg->msg_flags & MSG_CONFIRM)
810 goto do_confirm;
811back_from_confirm:
812
813 if (!ipc.addr)
814 ipc.addr = fl4.daddr;
815
816 lock_sock(sk);
817
818 pfh.icmph.type = user_icmph.type; /* already checked */
819 pfh.icmph.code = user_icmph.code; /* ditto */
820 pfh.icmph.checksum = 0;
821 pfh.icmph.un.echo.id = inet->inet_sport;
822 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
823 pfh.iov = msg->msg_iov;
824 pfh.wcheck = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000825 pfh.family = AF_INET;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000826
827 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
828 0, &ipc, &rt, msg->msg_flags);
829 if (err)
830 ip_flush_pending_frames(sk);
831 else
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000832 err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000833 release_sock(sk);
834
835out:
836 ip_rt_put(rt);
837 if (free)
838 kfree(ipc.opt);
839 if (!err) {
840 icmp_out_count(sock_net(sk), user_icmph.type);
841 return len;
842 }
843 return err;
844
845do_confirm:
846 dst_confirm(&rt->dst);
847 if (!(msg->msg_flags & MSG_PROBE) || len)
848 goto back_from_confirm;
849 err = 0;
850 goto out;
851}
852
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000853int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
854 size_t len, int noblock, int flags, int *addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000855{
856 struct inet_sock *isk = inet_sk(sk);
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000857 int family = sk->sk_family;
858 struct sockaddr_in *sin;
859 struct sockaddr_in6 *sin6;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000860 struct sk_buff *skb;
861 int copied, err;
862
863 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
864
David S. Millera5e74242012-02-21 17:59:19 -0500865 err = -EOPNOTSUPP;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000866 if (flags & MSG_OOB)
867 goto out;
868
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000869 if (addr_len) {
870 if (family == AF_INET)
871 *addr_len = sizeof(*sin);
872 else if (family == AF_INET6 && addr_len)
873 *addr_len = sizeof(*sin6);
874 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000875
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000876 if (flags & MSG_ERRQUEUE) {
877 if (family == AF_INET) {
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100878 return ip_recv_error(sk, msg, len, addr_len);
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000879#if IS_ENABLED(CONFIG_IPV6)
880 } else if (family == AF_INET6) {
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100881 return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000882#endif
883 }
884 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000885
886 skb = skb_recv_datagram(sk, flags, noblock, &err);
887 if (!skb)
888 goto out;
889
890 copied = skb->len;
891 if (copied > len) {
892 msg->msg_flags |= MSG_TRUNC;
893 copied = len;
894 }
895
896 /* Don't bother checking the checksum */
897 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
898 if (err)
899 goto done;
900
901 sock_recv_timestamp(msg, sk, skb);
902
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000903 /* Copy the address and add cmsg data. */
904 if (family == AF_INET) {
905 sin = (struct sockaddr_in *) msg->msg_name;
Hannes Frederic Sowa80c4cec2014-05-19 14:16:47 -0700906 if (sin) {
907 sin->sin_family = AF_INET;
908 sin->sin_port = 0 /* skb->h.uh->source */;
909 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
910 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
911 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000912
913 if (isk->cmsg_flags)
914 ip_cmsg_recv(msg, skb);
915
916#if IS_ENABLED(CONFIG_IPV6)
917 } else if (family == AF_INET6) {
918 struct ipv6_pinfo *np = inet6_sk(sk);
919 struct ipv6hdr *ip6 = ipv6_hdr(skb);
920 sin6 = (struct sockaddr_in6 *) msg->msg_name;
Hannes Frederic Sowa80c4cec2014-05-19 14:16:47 -0700921 if (sin6) {
922 sin6->sin6_family = AF_INET6;
923 sin6->sin6_port = 0;
924 sin6->sin6_addr = ip6->saddr;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000925
Hannes Frederic Sowa80c4cec2014-05-19 14:16:47 -0700926 sin6->sin6_flowinfo = 0;
927 if (np->sndflow)
928 sin6->sin6_flowinfo =
929 *(__be32 *)ip6 & IPV6_FLOWINFO_MASK;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000930
Hannes Frederic Sowa80c4cec2014-05-19 14:16:47 -0700931 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
932 IP6CB(skb)->iif);
933 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000934
935 if (inet6_sk(sk)->rxopt.all)
936 pingv6_ops.datagram_recv_ctl(sk, msg, skb);
937#endif
938 } else {
939 BUG();
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000940 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000941
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000942 err = copied;
943
944done:
945 skb_free_datagram(sk, skb);
946out:
947 pr_debug("ping_recvmsg -> %d\n", err);
948 return err;
949}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000950EXPORT_SYMBOL_GPL(ping_recvmsg);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000951
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000952int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000953{
954 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000955 inet_sk(sk), inet_sk(sk)->inet_num, skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000956 if (sock_queue_rcv_skb(sk, skb) < 0) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000957 kfree_skb(skb);
958 pr_debug("ping_queue_rcv_skb -> failed\n");
959 return -1;
960 }
961 return 0;
962}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000963EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000964
965
966/*
967 * All we need to do is get the socket.
968 */
969
970void ping_rcv(struct sk_buff *skb)
971{
972 struct sock *sk;
973 struct net *net = dev_net(skb->dev);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000974 struct icmphdr *icmph = icmp_hdr(skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000975
976 /* We assume the packet has already been checked by icmp_rcv */
977
978 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000979 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000980
981 /* Push ICMP header back */
982 skb_push(skb, skb->data - (u8 *)icmph);
983
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000984 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000985 if (sk != NULL) {
986 pr_debug("rcv on socket %p\n", sk);
987 ping_queue_rcv_skb(sk, skb_get(skb));
988 sock_put(sk);
989 return;
990 }
991 pr_debug("no socket, dropping\n");
992
993 /* We're called from icmp_rcv(). kfree_skb() is done there. */
994}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000995EXPORT_SYMBOL_GPL(ping_rcv);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000996
997struct proto ping_prot = {
998 .name = "PING",
999 .owner = THIS_MODULE,
1000 .init = ping_init_sock,
1001 .close = ping_close,
1002 .connect = ip4_datagram_connect,
1003 .disconnect = udp_disconnect,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001004 .setsockopt = ip_setsockopt,
1005 .getsockopt = ip_getsockopt,
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +00001006 .sendmsg = ping_v4_sendmsg,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001007 .recvmsg = ping_recvmsg,
1008 .bind = ping_bind,
1009 .backlog_rcv = ping_queue_rcv_skb,
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +00001010 .hash = ping_hash,
1011 .unhash = ping_unhash,
1012 .get_port = ping_get_port,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001013 .obj_size = sizeof(struct inet_sock),
1014};
1015EXPORT_SYMBOL(ping_prot);
1016
1017#ifdef CONFIG_PROC_FS
1018
1019static struct sock *ping_get_first(struct seq_file *seq, int start)
1020{
1021 struct sock *sk;
1022 struct ping_iter_state *state = seq->private;
1023 struct net *net = seq_file_net(seq);
1024
1025 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
1026 ++state->bucket) {
1027 struct hlist_nulls_node *node;
Changli Gao75e308c2011-05-18 21:16:01 +00001028 struct hlist_nulls_head *hslot;
1029
1030 hslot = &ping_table.hash[state->bucket];
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001031
1032 if (hlist_nulls_empty(hslot))
1033 continue;
1034
1035 sk_nulls_for_each(sk, node, hslot) {
1036 if (net_eq(sock_net(sk), net))
1037 goto found;
1038 }
1039 }
1040 sk = NULL;
1041found:
1042 return sk;
1043}
1044
1045static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1046{
1047 struct ping_iter_state *state = seq->private;
1048 struct net *net = seq_file_net(seq);
1049
1050 do {
1051 sk = sk_nulls_next(sk);
1052 } while (sk && (!net_eq(sock_net(sk), net)));
1053
1054 if (!sk)
1055 return ping_get_first(seq, state->bucket + 1);
1056 return sk;
1057}
1058
1059static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1060{
1061 struct sock *sk = ping_get_first(seq, 0);
1062
1063 if (sk)
1064 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1065 --pos;
1066 return pos ? NULL : sk;
1067}
1068
1069static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
1070{
1071 struct ping_iter_state *state = seq->private;
1072 state->bucket = 0;
1073
1074 read_lock_bh(&ping_table.lock);
1075
1076 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1077}
1078
1079static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1080{
1081 struct sock *sk;
1082
1083 if (v == SEQ_START_TOKEN)
1084 sk = ping_get_idx(seq, 0);
1085 else
1086 sk = ping_get_next(seq, v);
1087
1088 ++*pos;
1089 return sk;
1090}
1091
1092static void ping_seq_stop(struct seq_file *seq, void *v)
1093{
1094 read_unlock_bh(&ping_table.lock);
1095}
1096
1097static void ping_format_sock(struct sock *sp, struct seq_file *f,
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001098 int bucket)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001099{
1100 struct inet_sock *inet = inet_sk(sp);
1101 __be32 dest = inet->inet_daddr;
1102 __be32 src = inet->inet_rcv_saddr;
1103 __u16 destp = ntohs(inet->inet_dport);
1104 __u16 srcp = ntohs(inet->inet_sport);
1105
1106 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001107 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d",
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001108 bucket, src, srcp, dest, destp, sp->sk_state,
1109 sk_wmem_alloc_get(sp),
1110 sk_rmem_alloc_get(sp),
1111 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1112 atomic_read(&sp->sk_refcnt), sp,
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001113 atomic_read(&sp->sk_drops));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001114}
1115
1116static int ping_seq_show(struct seq_file *seq, void *v)
1117{
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001118 seq_setwidth(seq, 127);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001119 if (v == SEQ_START_TOKEN)
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001120 seq_puts(seq, " sl local_address rem_address st tx_queue "
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001121 "rx_queue tr tm->when retrnsmt uid timeout "
1122 "inode ref pointer drops");
1123 else {
1124 struct ping_iter_state *state = seq->private;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001125
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001126 ping_format_sock(v, seq, state->bucket);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001127 }
Tetsuo Handabe8bd602013-11-14 14:31:57 -08001128 seq_pad(seq, '\n');
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001129 return 0;
1130}
1131
1132static const struct seq_operations ping_seq_ops = {
1133 .show = ping_seq_show,
1134 .start = ping_seq_start,
1135 .next = ping_seq_next,
1136 .stop = ping_seq_stop,
1137};
1138
1139static int ping_seq_open(struct inode *inode, struct file *file)
1140{
1141 return seq_open_net(inode, file, &ping_seq_ops,
1142 sizeof(struct ping_iter_state));
1143}
1144
1145static const struct file_operations ping_seq_fops = {
1146 .open = ping_seq_open,
1147 .read = seq_read,
1148 .llseek = seq_lseek,
1149 .release = seq_release_net,
1150};
1151
1152static int ping_proc_register(struct net *net)
1153{
1154 struct proc_dir_entry *p;
1155 int rc = 0;
1156
1157 p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
1158 if (!p)
1159 rc = -ENOMEM;
1160 return rc;
1161}
1162
1163static void ping_proc_unregister(struct net *net)
1164{
1165 proc_net_remove(net, "icmp");
1166}
1167
1168
1169static int __net_init ping_proc_init_net(struct net *net)
1170{
1171 return ping_proc_register(net);
1172}
1173
1174static void __net_exit ping_proc_exit_net(struct net *net)
1175{
1176 ping_proc_unregister(net);
1177}
1178
1179static struct pernet_operations ping_net_ops = {
1180 .init = ping_proc_init_net,
1181 .exit = ping_proc_exit_net,
1182};
1183
1184int __init ping_proc_init(void)
1185{
1186 return register_pernet_subsys(&ping_net_ops);
1187}
1188
1189void ping_proc_exit(void)
1190{
1191 unregister_pernet_subsys(&ping_net_ops);
1192}
1193
1194#endif
1195
1196void __init ping_init(void)
1197{
1198 int i;
1199
1200 for (i = 0; i < PING_HTABLE_SIZE; i++)
1201 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1202 rwlock_init(&ping_table.lock);
1203}