blob: 1de2285e8ec7a56241a7d4966b6fade0e3b05728 [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 Colittif7048202013-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 Colittif7048202013-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 Colittif7048202013-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 Colittif7048202013-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 Colittif7048202013-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 Colittif7048202013-01-16 22:09:49 +0000141EXPORT_SYMBOL_GPL(ping_get_port);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000142
Lorenzo Colittif7048202013-01-16 22:09:49 +0000143void ping_hash(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000144{
Lorenzo Colittif7048202013-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 Colittif7048202013-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);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000152 pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000153 if (sk_hashed(sk)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000154 write_lock_bh(&ping_table.lock);
155 hlist_nulls_del(&sk->sk_nulls_node);
156 sock_put(sk);
Eric Dumazet747465e2012-01-16 19:27:39 +0000157 isk->inet_num = 0;
158 isk->inet_sport = 0;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000159 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
160 write_unlock_bh(&ping_table.lock);
161 }
162}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000163EXPORT_SYMBOL_GPL(ping_unhash);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000164
Lorenzo Colittif7048202013-01-16 22:09:49 +0000165static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000166{
167 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
168 struct sock *sk = NULL;
169 struct inet_sock *isk;
170 struct hlist_nulls_node *hnode;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000171 int dif = skb->dev->ifindex;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000172
Lorenzo Colittif7048202013-01-16 22:09:49 +0000173 if (skb->protocol == htons(ETH_P_IP)) {
174 pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
175 (int)ident, &ip_hdr(skb)->daddr, dif);
176#if IS_ENABLED(CONFIG_IPV6)
177 } else if (skb->protocol == htons(ETH_P_IPV6)) {
178 pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
179 (int)ident, &ipv6_hdr(skb)->daddr, dif);
180#endif
181 }
182
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000183 read_lock_bh(&ping_table.lock);
184
185 ping_portaddr_for_each_entry(sk, hnode, hslot) {
186 isk = inet_sk(sk);
187
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000188 pr_debug("iterate\n");
189 if (isk->inet_num != ident)
190 continue;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000191
192 if (skb->protocol == htons(ETH_P_IP) &&
193 sk->sk_family == AF_INET) {
194 pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
195 (int) isk->inet_num, &isk->inet_rcv_saddr,
196 sk->sk_bound_dev_if);
197
198 if (isk->inet_rcv_saddr &&
199 isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
200 continue;
201#if IS_ENABLED(CONFIG_IPV6)
202 } else if (skb->protocol == htons(ETH_P_IPV6) &&
203 sk->sk_family == AF_INET6) {
204 struct ipv6_pinfo *np = inet6_sk(sk);
205
206 pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
207 (int) isk->inet_num,
208 &inet6_sk(sk)->rcv_saddr,
209 sk->sk_bound_dev_if);
210
211 if (!ipv6_addr_any(&np->rcv_saddr) &&
212 !ipv6_addr_equal(&np->rcv_saddr,
213 &ipv6_hdr(skb)->daddr))
214 continue;
215#endif
216 }
217
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000218 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
219 continue;
220
221 sock_hold(sk);
222 goto exit;
223 }
224
225 sk = NULL;
226exit:
227 read_unlock_bh(&ping_table.lock);
228
229 return sk;
230}
231
Changli Gao75e308c2011-05-18 21:16:01 +0000232static void inet_get_ping_group_range_net(struct net *net, gid_t *low,
233 gid_t *high)
Vasiliy Kulikovf56e03e2011-05-17 00:16:56 +0000234{
235 gid_t *data = net->ipv4.sysctl_ping_group_range;
236 unsigned seq;
237 do {
238 seq = read_seqbegin(&sysctl_local_ports.lock);
239
240 *low = data[0];
241 *high = data[1];
242 } while (read_seqretry(&sysctl_local_ports.lock, seq));
243}
244
245
Lorenzo Colittif7048202013-01-16 22:09:49 +0000246int ping_init_sock(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000247{
248 struct net *net = sock_net(sk);
249 gid_t group = current_egid();
250 gid_t range[2];
Wang, Xiaomingd21fa7b2014-04-14 12:30:45 -0400251 struct group_info *group_info;
Wang, Xiaoming67c6fc92014-04-14 12:30:45 -0400252 int i, j, count;
253 int ret = 0;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000254
255 inet_get_ping_group_range_net(net, range, range+1);
256 if (range[0] <= group && group <= range[1])
257 return 0;
258
Wang, Xiaomingd21fa7b2014-04-14 12:30:45 -0400259 group_info = get_current_groups();
260 count = group_info->ngroups;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000261 for (i = 0; i < group_info->nblocks; i++) {
262 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
263
264 for (j = 0; j < cp_count; j++) {
265 group = group_info->blocks[i][j];
266 if (range[0] <= group && group <= range[1])
Wang, Xiaomingd21fa7b2014-04-14 12:30:45 -0400267 goto out_release_group;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000268 }
269
270 count -= cp_count;
271 }
272
Wang, Xiaomingd21fa7b2014-04-14 12:30:45 -0400273 ret = -EACCES;
274
275out_release_group:
276 put_group_info(group_info);
277 return ret;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000278}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000279EXPORT_SYMBOL_GPL(ping_init_sock);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000280
Lorenzo Colittif7048202013-01-16 22:09:49 +0000281void ping_close(struct sock *sk, long timeout)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000282{
283 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000284 inet_sk(sk), inet_sk(sk)->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000285 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
286
287 sk_common_release(sk);
288}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000289EXPORT_SYMBOL_GPL(ping_close);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000290
Lorenzo Colittif7048202013-01-16 22:09:49 +0000291/* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
292int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
293 struct sockaddr *uaddr, int addr_len) {
294 struct net *net = sock_net(sk);
295 if (sk->sk_family == AF_INET) {
296 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
297 int chk_addr_ret;
298
299 if (addr_len < sizeof(*addr))
300 return -EINVAL;
301
302 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
303 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
304
305 chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
306
307 if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
308 chk_addr_ret = RTN_LOCAL;
309
310 if ((sysctl_ip_nonlocal_bind == 0 &&
311 isk->freebind == 0 && isk->transparent == 0 &&
312 chk_addr_ret != RTN_LOCAL) ||
313 chk_addr_ret == RTN_MULTICAST ||
314 chk_addr_ret == RTN_BROADCAST)
315 return -EADDRNOTAVAIL;
316
317#if IS_ENABLED(CONFIG_IPV6)
318 } else if (sk->sk_family == AF_INET6) {
319 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
320 int addr_type, scoped, has_addr;
321 struct net_device *dev = NULL;
322
323 if (addr_len < sizeof(*addr))
324 return -EINVAL;
325
326 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
327 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
328
329 addr_type = ipv6_addr_type(&addr->sin6_addr);
330 scoped = __ipv6_addr_needs_scope_id(addr_type);
331 if ((addr_type != IPV6_ADDR_ANY &&
332 !(addr_type & IPV6_ADDR_UNICAST)) ||
333 (scoped && !addr->sin6_scope_id))
334 return -EINVAL;
335
336 rcu_read_lock();
337 if (addr->sin6_scope_id) {
338 dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
339 if (!dev) {
340 rcu_read_unlock();
341 return -ENODEV;
342 }
343 }
344 has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
345 scoped);
346 rcu_read_unlock();
347
348 if (!(isk->freebind || isk->transparent || has_addr ||
349 addr_type == IPV6_ADDR_ANY))
350 return -EADDRNOTAVAIL;
351
352 if (scoped)
353 sk->sk_bound_dev_if = addr->sin6_scope_id;
354#endif
355 } else {
356 return -EAFNOSUPPORT;
357 }
358 return 0;
359}
360
361void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
362{
363 if (saddr->sa_family == AF_INET) {
364 struct inet_sock *isk = inet_sk(sk);
365 struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
366 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
367#if IS_ENABLED(CONFIG_IPV6)
368 } else if (saddr->sa_family == AF_INET6) {
369 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
370 struct ipv6_pinfo *np = inet6_sk(sk);
371 np->rcv_saddr = np->saddr = addr->sin6_addr;
372#endif
373 }
374}
375
376void ping_clear_saddr(struct sock *sk, int dif)
377{
378 sk->sk_bound_dev_if = dif;
379 if (sk->sk_family == AF_INET) {
380 struct inet_sock *isk = inet_sk(sk);
381 isk->inet_rcv_saddr = isk->inet_saddr = 0;
382#if IS_ENABLED(CONFIG_IPV6)
383 } else if (sk->sk_family == AF_INET6) {
384 struct ipv6_pinfo *np = inet6_sk(sk);
385 memset(&np->rcv_saddr, 0, sizeof(np->rcv_saddr));
386 memset(&np->saddr, 0, sizeof(np->saddr));
387#endif
388 }
389}
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000390/*
391 * We need our own bind because there are no privileged id's == local ports.
392 * Moreover, we don't allow binding to multi- and broadcast addresses.
393 */
394
Lorenzo Colittif7048202013-01-16 22:09:49 +0000395int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000396{
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000397 struct inet_sock *isk = inet_sk(sk);
398 unsigned short snum;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000399 int err;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000400 int dif = sk->sk_bound_dev_if;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000401
Lorenzo Colittif7048202013-01-16 22:09:49 +0000402 err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
403 if (err)
404 return err;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000405
406 lock_sock(sk);
407
408 err = -EINVAL;
409 if (isk->inet_num != 0)
410 goto out;
411
412 err = -EADDRINUSE;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000413 ping_set_saddr(sk, uaddr);
414 snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
415 if (ping_get_port(sk, snum) != 0) {
416 ping_clear_saddr(sk, dif);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000417 goto out;
418 }
419
Lorenzo Colittif7048202013-01-16 22:09:49 +0000420 pr_debug("after bind(): num = %d, dif = %d\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000421 (int)isk->inet_num,
Joe Perches058bd4d2012-03-11 18:36:11 +0000422 (int)sk->sk_bound_dev_if);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000423
424 err = 0;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000425 if ((sk->sk_family == AF_INET && isk->inet_rcv_saddr) ||
426 (sk->sk_family == AF_INET6 &&
427 !ipv6_addr_any(&inet6_sk(sk)->rcv_saddr)))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000428 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000429
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000430 if (snum)
431 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
432 isk->inet_sport = htons(isk->inet_num);
433 isk->inet_daddr = 0;
434 isk->inet_dport = 0;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000435
436#if IS_ENABLED(CONFIG_IPV6)
437 if (sk->sk_family == AF_INET6)
438 memset(&inet6_sk(sk)->daddr, 0, sizeof(inet6_sk(sk)->daddr));
439#endif
440
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000441 sk_dst_reset(sk);
442out:
443 release_sock(sk);
444 pr_debug("ping_v4_bind -> %d\n", err);
445 return err;
446}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000447EXPORT_SYMBOL_GPL(ping_bind);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000448
449/*
450 * Is this a supported type of ICMP message?
451 */
452
Lorenzo Colittif7048202013-01-16 22:09:49 +0000453static inline int ping_supported(int family, int type, int code)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000454{
Lorenzo Colittif7048202013-01-16 22:09:49 +0000455 return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
456 (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000457}
458
459/*
460 * This routine is called by the ICMP module when it gets some
461 * sort of error condition.
462 */
463
Lorenzo Colittif7048202013-01-16 22:09:49 +0000464void ping_err(struct sk_buff *skb, int offset, u32 info)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000465{
Lorenzo Colittif7048202013-01-16 22:09:49 +0000466 int family;
467 struct icmphdr *icmph;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000468 struct inet_sock *inet_sock;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000469 int type;
470 int code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000471 struct net *net = dev_net(skb->dev);
472 struct sock *sk;
473 int harderr;
474 int err;
475
Lorenzo Colittif7048202013-01-16 22:09:49 +0000476 if (skb->protocol == htons(ETH_P_IP)) {
477 struct iphdr *iph = (struct iphdr *)skb->data;
478 offset = iph->ihl << 2;
479 family = AF_INET;
480 type = icmp_hdr(skb)->type;
481 code = icmp_hdr(skb)->code;
482 icmph = (struct icmphdr *)(skb->data + offset);
483 } else if (skb->protocol == htons(ETH_P_IPV6)) {
484 family = AF_INET6;
485 type = icmp6_hdr(skb)->icmp6_type;
486 code = icmp6_hdr(skb)->icmp6_code;
487 icmph = (struct icmphdr *) (skb->data + offset);
488 } else {
489 BUG();
490 }
491
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000492 /* We assume the packet has already been checked by icmp_unreach */
493
Lorenzo Colittif7048202013-01-16 22:09:49 +0000494 if (!ping_supported(family, icmph->type, icmph->code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000495 return;
496
Lorenzo Colittif7048202013-01-16 22:09:49 +0000497 pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
498 skb->protocol, type, code, ntohs(icmph->un.echo.id),
499 ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000500
Lorenzo Colittif7048202013-01-16 22:09:49 +0000501 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000502 if (sk == NULL) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000503 pr_debug("no socket, dropping\n");
504 return; /* No socket for error */
505 }
506 pr_debug("err on socket %p\n", sk);
507
508 err = 0;
509 harderr = 0;
510 inet_sock = inet_sk(sk);
511
Lorenzo Colittif7048202013-01-16 22:09:49 +0000512 if (skb->protocol == htons(ETH_P_IP)) {
513 switch (type) {
514 default:
515 case ICMP_TIME_EXCEEDED:
516 err = EHOSTUNREACH;
517 break;
518 case ICMP_SOURCE_QUENCH:
519 /* This is not a real error but ping wants to see it.
520 * Report it with some fake errno. */
521 err = EREMOTEIO;
522 break;
523 case ICMP_PARAMETERPROB:
524 err = EPROTO;
525 harderr = 1;
526 break;
527 case ICMP_DEST_UNREACH:
528 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
529 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
530 err = EMSGSIZE;
531 harderr = 1;
532 break;
533 }
534 goto out;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000535 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000536 err = EHOSTUNREACH;
537 if (code <= NR_ICMP_UNREACH) {
538 harderr = icmp_err_convert[code].fatal;
539 err = icmp_err_convert[code].errno;
540 }
541 break;
542 case ICMP_REDIRECT:
543 /* See ICMP_SOURCE_QUENCH */
544 err = EREMOTEIO;
545 break;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000546 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000547#if IS_ENABLED(CONFIG_IPV6)
548 } else if (skb->protocol == htons(ETH_P_IPV6)) {
549 harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
550#endif
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000551 }
552
553 /*
554 * RFC1122: OK. Passes ICMP errors back to application, as per
555 * 4.1.3.3.
556 */
Lorenzo Colittif7048202013-01-16 22:09:49 +0000557 if ((family == AF_INET && !inet_sock->recverr) ||
558 (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000559 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
560 goto out;
561 } else {
Lorenzo Colittif7048202013-01-16 22:09:49 +0000562 if (family == AF_INET) {
563 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
564 info, (u8 *)icmph);
565#if IS_ENABLED(CONFIG_IPV6)
566 } else if (family == AF_INET6) {
567 pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
568 info, (u8 *)icmph);
569#endif
570 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000571 }
572 sk->sk_err = err;
573 sk->sk_error_report(sk);
574out:
575 sock_put(sk);
576}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000577EXPORT_SYMBOL_GPL(ping_err);
578
579void ping_v4_err(struct sk_buff *skb, u32 info)
580{
581 ping_err(skb, 0, info);
582}
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000583
584/*
Lorenzo Colittif7048202013-01-16 22:09:49 +0000585 * Copy and checksum an ICMP Echo packet from user space into a buffer
586 * starting from the payload.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000587 */
588
Lorenzo Colittif7048202013-01-16 22:09:49 +0000589int ping_getfrag(void *from, char *to,
590 int offset, int fraglen, int odd, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000591{
592 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
593
594 if (offset == 0) {
595 if (fraglen < sizeof(struct icmphdr))
596 BUG();
597 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
598 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
599 &pfh->wcheck))
600 return -EFAULT;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000601 } else if (offset < sizeof(struct icmphdr)) {
602 BUG();
603 } else {
604 if (csum_partial_copy_fromiovecend
605 (to, pfh->iov, offset - sizeof(struct icmphdr),
606 fraglen, &pfh->wcheck))
607 return -EFAULT;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000608 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000609
610#if IS_ENABLED(CONFIG_IPV6)
611 /* For IPv6, checksum each skb as we go along, as expected by
612 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
613 * wcheck, it will be finalized in ping_v4_push_pending_frames.
614 */
615 if (pfh->family == AF_INET6) {
616 skb->csum = pfh->wcheck;
617 skb->ip_summed = CHECKSUM_NONE;
618 pfh->wcheck = 0;
619 }
620#endif
621
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000622 return 0;
623}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000624EXPORT_SYMBOL_GPL(ping_getfrag);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000625
Lorenzo Colittif7048202013-01-16 22:09:49 +0000626static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
627 struct flowi4 *fl4)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000628{
629 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
630
631 pfh->wcheck = csum_partial((char *)&pfh->icmph,
632 sizeof(struct icmphdr), pfh->wcheck);
633 pfh->icmph.checksum = csum_fold(pfh->wcheck);
634 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
635 skb->ip_summed = CHECKSUM_NONE;
636 return ip_push_pending_frames(sk, fl4);
637}
638
Lorenzo Colittif7048202013-01-16 22:09:49 +0000639int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
640 void *user_icmph, size_t icmph_len) {
641 u8 type, code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000642
643 if (len > 0xFFFF)
644 return -EMSGSIZE;
645
646 /*
647 * Check the flags.
648 */
649
650 /* Mirror BSD error message compatibility */
651 if (msg->msg_flags & MSG_OOB)
652 return -EOPNOTSUPP;
653
654 /*
655 * Fetch the ICMP header provided by the userland.
Lorenzo Colittif7048202013-01-16 22:09:49 +0000656 * iovec is modified! The ICMP header is consumed.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000657 */
Lorenzo Colittif7048202013-01-16 22:09:49 +0000658 if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000659 return -EFAULT;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000660
661 if (family == AF_INET) {
662 type = ((struct icmphdr *) user_icmph)->type;
663 code = ((struct icmphdr *) user_icmph)->code;
664#if IS_ENABLED(CONFIG_IPV6)
665 } else if (family == AF_INET6) {
666 type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
667 code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
668#endif
669 } else {
670 BUG();
671 }
672
673 if (!ping_supported(family, type, code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000674 return -EINVAL;
675
Lorenzo Colittif7048202013-01-16 22:09:49 +0000676 return 0;
677}
678EXPORT_SYMBOL_GPL(ping_common_sendmsg);
679
680int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
681 size_t len)
682{
683 struct net *net = sock_net(sk);
684 struct flowi4 fl4;
685 struct inet_sock *inet = inet_sk(sk);
686 struct ipcm_cookie ipc;
687 struct icmphdr user_icmph;
688 struct pingfakehdr pfh;
689 struct rtable *rt = NULL;
690 struct ip_options_data opt_copy;
691 int free = 0;
692 __be32 saddr, daddr, faddr;
693 u8 tos;
694 int err;
695
696 pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
697
698 err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
699 sizeof(user_icmph));
700 if (err)
701 return err;
702
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000703 /*
704 * Get and verify the address.
705 */
706
707 if (msg->msg_name) {
708 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
709 if (msg->msg_namelen < sizeof(*usin))
710 return -EINVAL;
711 if (usin->sin_family != AF_INET)
712 return -EINVAL;
713 daddr = usin->sin_addr.s_addr;
714 /* no remote port */
715 } else {
716 if (sk->sk_state != TCP_ESTABLISHED)
717 return -EDESTADDRREQ;
718 daddr = inet->inet_daddr;
719 /* no remote port */
720 }
721
722 ipc.addr = inet->inet_saddr;
723 ipc.opt = NULL;
724 ipc.oif = sk->sk_bound_dev_if;
725 ipc.tx_flags = 0;
726 err = sock_tx_timestamp(sk, &ipc.tx_flags);
727 if (err)
728 return err;
729
730 if (msg->msg_controllen) {
731 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
732 if (err)
733 return err;
734 if (ipc.opt)
735 free = 1;
736 }
737 if (!ipc.opt) {
738 struct ip_options_rcu *inet_opt;
739
740 rcu_read_lock();
741 inet_opt = rcu_dereference(inet->inet_opt);
742 if (inet_opt) {
743 memcpy(&opt_copy, inet_opt,
744 sizeof(*inet_opt) + inet_opt->opt.optlen);
745 ipc.opt = &opt_copy.opt;
746 }
747 rcu_read_unlock();
748 }
749
750 saddr = ipc.addr;
751 ipc.addr = faddr = daddr;
752
753 if (ipc.opt && ipc.opt->opt.srr) {
754 if (!daddr)
755 return -EINVAL;
756 faddr = ipc.opt->opt.faddr;
757 }
758 tos = RT_TOS(inet->tos);
759 if (sock_flag(sk, SOCK_LOCALROUTE) ||
760 (msg->msg_flags & MSG_DONTROUTE) ||
761 (ipc.opt && ipc.opt->opt.is_strictroute)) {
762 tos |= RTO_ONLINK;
763 }
764
765 if (ipv4_is_multicast(daddr)) {
766 if (!ipc.oif)
767 ipc.oif = inet->mc_index;
768 if (!saddr)
769 saddr = inet->mc_addr;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000770 } else if (!ipc.oif)
771 ipc.oif = inet->uc_index;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000772
773 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
774 RT_SCOPE_UNIVERSE, sk->sk_protocol,
Lorenzo Colitti462ce7c2014-03-31 16:23:51 +0900775 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0,
776 sock_i_uid(sk));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000777
778 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
779 rt = ip_route_output_flow(net, &fl4, sk);
780 if (IS_ERR(rt)) {
781 err = PTR_ERR(rt);
782 rt = NULL;
783 if (err == -ENETUNREACH)
Eric Dumazet5fdc3812013-11-28 09:51:22 -0800784 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000785 goto out;
786 }
787
788 err = -EACCES;
789 if ((rt->rt_flags & RTCF_BROADCAST) &&
790 !sock_flag(sk, SOCK_BROADCAST))
791 goto out;
792
793 if (msg->msg_flags & MSG_CONFIRM)
794 goto do_confirm;
795back_from_confirm:
796
797 if (!ipc.addr)
798 ipc.addr = fl4.daddr;
799
800 lock_sock(sk);
801
802 pfh.icmph.type = user_icmph.type; /* already checked */
803 pfh.icmph.code = user_icmph.code; /* ditto */
804 pfh.icmph.checksum = 0;
805 pfh.icmph.un.echo.id = inet->inet_sport;
806 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
807 pfh.iov = msg->msg_iov;
808 pfh.wcheck = 0;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000809 pfh.family = AF_INET;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000810
811 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
812 0, &ipc, &rt, msg->msg_flags);
813 if (err)
814 ip_flush_pending_frames(sk);
815 else
Lorenzo Colittif7048202013-01-16 22:09:49 +0000816 err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000817 release_sock(sk);
818
819out:
820 ip_rt_put(rt);
821 if (free)
822 kfree(ipc.opt);
823 if (!err) {
824 icmp_out_count(sock_net(sk), user_icmph.type);
825 return len;
826 }
827 return err;
828
829do_confirm:
830 dst_confirm(&rt->dst);
831 if (!(msg->msg_flags & MSG_PROBE) || len)
832 goto back_from_confirm;
833 err = 0;
834 goto out;
835}
836
Lorenzo Colittif7048202013-01-16 22:09:49 +0000837int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
838 size_t len, int noblock, int flags, int *addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000839{
840 struct inet_sock *isk = inet_sk(sk);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000841 int family = sk->sk_family;
842 struct sockaddr_in *sin;
843 struct sockaddr_in6 *sin6;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000844 struct sk_buff *skb;
845 int copied, err;
846
847 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
848
David S. Millera5e74242012-02-21 17:59:19 -0500849 err = -EOPNOTSUPP;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000850 if (flags & MSG_OOB)
851 goto out;
852
Lorenzo Colittif7048202013-01-16 22:09:49 +0000853 if (addr_len) {
854 if (family == AF_INET)
855 *addr_len = sizeof(*sin);
856 else if (family == AF_INET6 && addr_len)
857 *addr_len = sizeof(*sin6);
858 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000859
Lorenzo Colittif7048202013-01-16 22:09:49 +0000860 if (flags & MSG_ERRQUEUE) {
861 if (family == AF_INET) {
Ethan Chenc523abc2014-03-07 11:49:08 -0800862 return ip_recv_error(sk, msg, len, addr_len);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000863#if IS_ENABLED(CONFIG_IPV6)
864 } else if (family == AF_INET6) {
Ethan Chenc523abc2014-03-07 11:49:08 -0800865 return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000866#endif
867 }
868 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000869
870 skb = skb_recv_datagram(sk, flags, noblock, &err);
871 if (!skb)
872 goto out;
873
874 copied = skb->len;
875 if (copied > len) {
876 msg->msg_flags |= MSG_TRUNC;
877 copied = len;
878 }
879
880 /* Don't bother checking the checksum */
881 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
882 if (err)
883 goto done;
884
885 sock_recv_timestamp(msg, sk, skb);
886
Lorenzo Colittif7048202013-01-16 22:09:49 +0000887 /* Copy the address and add cmsg data. */
888 if (family == AF_INET) {
889 sin = (struct sockaddr_in *) msg->msg_name;
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700890 if (sin) {
891 sin->sin_family = AF_INET;
892 sin->sin_port = 0 /* skb->h.uh->source */;
893 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
894 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
895 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000896
897 if (isk->cmsg_flags)
898 ip_cmsg_recv(msg, skb);
899
900#if IS_ENABLED(CONFIG_IPV6)
901 } else if (family == AF_INET6) {
902 struct ipv6_pinfo *np = inet6_sk(sk);
903 struct ipv6hdr *ip6 = ipv6_hdr(skb);
904 sin6 = (struct sockaddr_in6 *) msg->msg_name;
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700905 if (sin6) {
906 sin6->sin6_family = AF_INET6;
907 sin6->sin6_port = 0;
908 sin6->sin6_addr = ip6->saddr;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000909
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700910 if (np->sndflow)
911 sin6->sin6_flowinfo =
912 *(__be32 *)ip6 & IPV6_FLOWINFO_MASK;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000913
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700914 if (__ipv6_addr_needs_scope_id(
915 ipv6_addr_type(&sin6->sin6_addr)))
916 sin6->sin6_scope_id = IP6CB(skb)->iif;
917 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000918
919 if (inet6_sk(sk)->rxopt.all)
920 pingv6_ops.datagram_recv_ctl(sk, msg, skb);
921#endif
922 } else {
923 BUG();
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000924 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000925
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000926 err = copied;
927
928done:
929 skb_free_datagram(sk, skb);
930out:
931 pr_debug("ping_recvmsg -> %d\n", err);
932 return err;
933}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000934EXPORT_SYMBOL_GPL(ping_recvmsg);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000935
Lorenzo Colittif7048202013-01-16 22:09:49 +0000936int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000937{
938 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000939 inet_sk(sk), inet_sk(sk)->inet_num, skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000940 if (sock_queue_rcv_skb(sk, skb) < 0) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000941 kfree_skb(skb);
942 pr_debug("ping_queue_rcv_skb -> failed\n");
943 return -1;
944 }
945 return 0;
946}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000947EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000948
949
950/*
951 * All we need to do is get the socket.
952 */
953
954void ping_rcv(struct sk_buff *skb)
955{
956 struct sock *sk;
957 struct net *net = dev_net(skb->dev);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000958 struct icmphdr *icmph = icmp_hdr(skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000959
960 /* We assume the packet has already been checked by icmp_rcv */
961
962 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000963 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000964
965 /* Push ICMP header back */
966 skb_push(skb, skb->data - (u8 *)icmph);
967
Lorenzo Colittif7048202013-01-16 22:09:49 +0000968 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000969 if (sk != NULL) {
970 pr_debug("rcv on socket %p\n", sk);
971 ping_queue_rcv_skb(sk, skb_get(skb));
972 sock_put(sk);
973 return;
974 }
975 pr_debug("no socket, dropping\n");
976
977 /* We're called from icmp_rcv(). kfree_skb() is done there. */
978}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000979EXPORT_SYMBOL_GPL(ping_rcv);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000980
981struct proto ping_prot = {
982 .name = "PING",
983 .owner = THIS_MODULE,
984 .init = ping_init_sock,
985 .close = ping_close,
986 .connect = ip4_datagram_connect,
987 .disconnect = udp_disconnect,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000988 .setsockopt = ip_setsockopt,
989 .getsockopt = ip_getsockopt,
Lorenzo Colittif7048202013-01-16 22:09:49 +0000990 .sendmsg = ping_v4_sendmsg,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000991 .recvmsg = ping_recvmsg,
992 .bind = ping_bind,
993 .backlog_rcv = ping_queue_rcv_skb,
Lorenzo Colittif7048202013-01-16 22:09:49 +0000994 .hash = ping_hash,
995 .unhash = ping_unhash,
996 .get_port = ping_get_port,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000997 .obj_size = sizeof(struct inet_sock),
998};
999EXPORT_SYMBOL(ping_prot);
1000
1001#ifdef CONFIG_PROC_FS
1002
1003static struct sock *ping_get_first(struct seq_file *seq, int start)
1004{
1005 struct sock *sk;
1006 struct ping_iter_state *state = seq->private;
1007 struct net *net = seq_file_net(seq);
1008
1009 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
1010 ++state->bucket) {
1011 struct hlist_nulls_node *node;
Changli Gao75e308c2011-05-18 21:16:01 +00001012 struct hlist_nulls_head *hslot;
1013
1014 hslot = &ping_table.hash[state->bucket];
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001015
1016 if (hlist_nulls_empty(hslot))
1017 continue;
1018
1019 sk_nulls_for_each(sk, node, hslot) {
1020 if (net_eq(sock_net(sk), net))
1021 goto found;
1022 }
1023 }
1024 sk = NULL;
1025found:
1026 return sk;
1027}
1028
1029static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1030{
1031 struct ping_iter_state *state = seq->private;
1032 struct net *net = seq_file_net(seq);
1033
1034 do {
1035 sk = sk_nulls_next(sk);
1036 } while (sk && (!net_eq(sock_net(sk), net)));
1037
1038 if (!sk)
1039 return ping_get_first(seq, state->bucket + 1);
1040 return sk;
1041}
1042
1043static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1044{
1045 struct sock *sk = ping_get_first(seq, 0);
1046
1047 if (sk)
1048 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1049 --pos;
1050 return pos ? NULL : sk;
1051}
1052
1053static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
1054{
1055 struct ping_iter_state *state = seq->private;
1056 state->bucket = 0;
1057
1058 read_lock_bh(&ping_table.lock);
1059
1060 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1061}
1062
1063static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1064{
1065 struct sock *sk;
1066
1067 if (v == SEQ_START_TOKEN)
1068 sk = ping_get_idx(seq, 0);
1069 else
1070 sk = ping_get_next(seq, v);
1071
1072 ++*pos;
1073 return sk;
1074}
1075
1076static void ping_seq_stop(struct seq_file *seq, void *v)
1077{
1078 read_unlock_bh(&ping_table.lock);
1079}
1080
1081static void ping_format_sock(struct sock *sp, struct seq_file *f,
Tetsuo Handa588af242013-11-14 14:31:57 -08001082 int bucket)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001083{
1084 struct inet_sock *inet = inet_sk(sp);
1085 __be32 dest = inet->inet_daddr;
1086 __be32 src = inet->inet_rcv_saddr;
1087 __u16 destp = ntohs(inet->inet_dport);
1088 __u16 srcp = ntohs(inet->inet_sport);
1089
1090 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
Tetsuo Handa588af242013-11-14 14:31:57 -08001091 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d",
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001092 bucket, src, srcp, dest, destp, sp->sk_state,
1093 sk_wmem_alloc_get(sp),
1094 sk_rmem_alloc_get(sp),
1095 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1096 atomic_read(&sp->sk_refcnt), sp,
Tetsuo Handa588af242013-11-14 14:31:57 -08001097 atomic_read(&sp->sk_drops));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001098}
1099
1100static int ping_seq_show(struct seq_file *seq, void *v)
1101{
Tetsuo Handa588af242013-11-14 14:31:57 -08001102 seq_setwidth(seq, 127);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001103 if (v == SEQ_START_TOKEN)
Tetsuo Handa588af242013-11-14 14:31:57 -08001104 seq_puts(seq, " sl local_address rem_address st tx_queue "
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001105 "rx_queue tr tm->when retrnsmt uid timeout "
1106 "inode ref pointer drops");
1107 else {
1108 struct ping_iter_state *state = seq->private;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001109
Tetsuo Handa588af242013-11-14 14:31:57 -08001110 ping_format_sock(v, seq, state->bucket);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001111 }
Tetsuo Handa588af242013-11-14 14:31:57 -08001112 seq_pad(seq, '\n');
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001113 return 0;
1114}
1115
1116static const struct seq_operations ping_seq_ops = {
1117 .show = ping_seq_show,
1118 .start = ping_seq_start,
1119 .next = ping_seq_next,
1120 .stop = ping_seq_stop,
1121};
1122
1123static int ping_seq_open(struct inode *inode, struct file *file)
1124{
1125 return seq_open_net(inode, file, &ping_seq_ops,
1126 sizeof(struct ping_iter_state));
1127}
1128
1129static const struct file_operations ping_seq_fops = {
1130 .open = ping_seq_open,
1131 .read = seq_read,
1132 .llseek = seq_lseek,
1133 .release = seq_release_net,
1134};
1135
1136static int ping_proc_register(struct net *net)
1137{
1138 struct proc_dir_entry *p;
1139 int rc = 0;
1140
1141 p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
1142 if (!p)
1143 rc = -ENOMEM;
1144 return rc;
1145}
1146
1147static void ping_proc_unregister(struct net *net)
1148{
1149 proc_net_remove(net, "icmp");
1150}
1151
1152
1153static int __net_init ping_proc_init_net(struct net *net)
1154{
1155 return ping_proc_register(net);
1156}
1157
1158static void __net_exit ping_proc_exit_net(struct net *net)
1159{
1160 ping_proc_unregister(net);
1161}
1162
1163static struct pernet_operations ping_net_ops = {
1164 .init = ping_proc_init_net,
1165 .exit = ping_proc_exit_net,
1166};
1167
1168int __init ping_proc_init(void)
1169{
1170 return register_pernet_subsys(&ping_net_ops);
1171}
1172
1173void ping_proc_exit(void)
1174{
1175 unregister_pernet_subsys(&ping_net_ops);
1176}
1177
1178#endif
1179
1180void __init ping_init(void)
1181{
1182 int i;
1183
1184 for (i = 0; i < PING_HTABLE_SIZE; i++)
1185 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1186 rwlock_init(&ping_table.lock);
1187}