blob: a8d7ed0623416ffa5d3400cb70a54be5ae340da4 [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);
Lorenzo Colitti8b17a9a2013-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 Colitti8b17a9a2013-01-16 22:09:49 +0000163EXPORT_SYMBOL_GPL(ping_unhash);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000164
Lorenzo Colitti8b17a9a2013-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 Colitti8b17a9a2013-01-16 22:09:49 +0000171 int dif = skb->dev->ifindex;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000172
Lorenzo Colitti8b17a9a2013-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 Colitti8b17a9a2013-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 Colitti8b17a9a2013-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];
251 struct group_info *group_info = get_current_groups();
252 int i, j, count = group_info->ngroups;
253
254 inet_get_ping_group_range_net(net, range, range+1);
255 if (range[0] <= group && group <= range[1])
256 return 0;
257
258 for (i = 0; i < group_info->nblocks; i++) {
259 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
260
261 for (j = 0; j < cp_count; j++) {
262 group = group_info->blocks[i][j];
263 if (range[0] <= group && group <= range[1])
264 return 0;
265 }
266
267 count -= cp_count;
268 }
269
270 return -EACCES;
271}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000272EXPORT_SYMBOL_GPL(ping_init_sock);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000273
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000274void ping_close(struct sock *sk, long timeout)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000275{
276 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000277 inet_sk(sk), inet_sk(sk)->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000278 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
279
280 sk_common_release(sk);
281}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000282EXPORT_SYMBOL_GPL(ping_close);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000283
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000284/* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
285int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
286 struct sockaddr *uaddr, int addr_len) {
287 struct net *net = sock_net(sk);
288 if (sk->sk_family == AF_INET) {
289 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
290 int chk_addr_ret;
291
292 if (addr_len < sizeof(*addr))
293 return -EINVAL;
294
295 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n",
296 sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port));
297
298 chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
299
300 if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
301 chk_addr_ret = RTN_LOCAL;
302
303 if ((sysctl_ip_nonlocal_bind == 0 &&
304 isk->freebind == 0 && isk->transparent == 0 &&
305 chk_addr_ret != RTN_LOCAL) ||
306 chk_addr_ret == RTN_MULTICAST ||
307 chk_addr_ret == RTN_BROADCAST)
308 return -EADDRNOTAVAIL;
309
310#if IS_ENABLED(CONFIG_IPV6)
311 } else if (sk->sk_family == AF_INET6) {
312 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
313 int addr_type, scoped, has_addr;
314 struct net_device *dev = NULL;
315
316 if (addr_len < sizeof(*addr))
317 return -EINVAL;
318
319 pr_debug("ping_check_bind_addr(sk=%p,addr=%pI6c,port=%d)\n",
320 sk, addr->sin6_addr.s6_addr, ntohs(addr->sin6_port));
321
322 addr_type = ipv6_addr_type(&addr->sin6_addr);
323 scoped = __ipv6_addr_needs_scope_id(addr_type);
324 if ((addr_type != IPV6_ADDR_ANY &&
325 !(addr_type & IPV6_ADDR_UNICAST)) ||
326 (scoped && !addr->sin6_scope_id))
327 return -EINVAL;
328
329 rcu_read_lock();
330 if (addr->sin6_scope_id) {
331 dev = dev_get_by_index_rcu(net, addr->sin6_scope_id);
332 if (!dev) {
333 rcu_read_unlock();
334 return -ENODEV;
335 }
336 }
337 has_addr = pingv6_ops.ipv6_chk_addr(net, &addr->sin6_addr, dev,
338 scoped);
339 rcu_read_unlock();
340
341 if (!(isk->freebind || isk->transparent || has_addr ||
342 addr_type == IPV6_ADDR_ANY))
343 return -EADDRNOTAVAIL;
344
345 if (scoped)
346 sk->sk_bound_dev_if = addr->sin6_scope_id;
347#endif
348 } else {
349 return -EAFNOSUPPORT;
350 }
351 return 0;
352}
353
354void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
355{
356 if (saddr->sa_family == AF_INET) {
357 struct inet_sock *isk = inet_sk(sk);
358 struct sockaddr_in *addr = (struct sockaddr_in *) saddr;
359 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
360#if IS_ENABLED(CONFIG_IPV6)
361 } else if (saddr->sa_family == AF_INET6) {
362 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
363 struct ipv6_pinfo *np = inet6_sk(sk);
364 np->rcv_saddr = np->saddr = addr->sin6_addr;
365#endif
366 }
367}
368
369void ping_clear_saddr(struct sock *sk, int dif)
370{
371 sk->sk_bound_dev_if = dif;
372 if (sk->sk_family == AF_INET) {
373 struct inet_sock *isk = inet_sk(sk);
374 isk->inet_rcv_saddr = isk->inet_saddr = 0;
375#if IS_ENABLED(CONFIG_IPV6)
376 } else if (sk->sk_family == AF_INET6) {
377 struct ipv6_pinfo *np = inet6_sk(sk);
378 memset(&np->rcv_saddr, 0, sizeof(np->rcv_saddr));
379 memset(&np->saddr, 0, sizeof(np->saddr));
380#endif
381 }
382}
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000383/*
384 * We need our own bind because there are no privileged id's == local ports.
385 * Moreover, we don't allow binding to multi- and broadcast addresses.
386 */
387
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000388int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000389{
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000390 struct inet_sock *isk = inet_sk(sk);
391 unsigned short snum;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000392 int err;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000393 int dif = sk->sk_bound_dev_if;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000394
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000395 err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
396 if (err)
397 return err;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000398
399 lock_sock(sk);
400
401 err = -EINVAL;
402 if (isk->inet_num != 0)
403 goto out;
404
405 err = -EADDRINUSE;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000406 ping_set_saddr(sk, uaddr);
407 snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
408 if (ping_get_port(sk, snum) != 0) {
409 ping_clear_saddr(sk, dif);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000410 goto out;
411 }
412
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000413 pr_debug("after bind(): num = %d, dif = %d\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000414 (int)isk->inet_num,
Joe Perches058bd4d2012-03-11 18:36:11 +0000415 (int)sk->sk_bound_dev_if);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000416
417 err = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000418 if ((sk->sk_family == AF_INET && isk->inet_rcv_saddr) ||
419 (sk->sk_family == AF_INET6 &&
420 !ipv6_addr_any(&inet6_sk(sk)->rcv_saddr)))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000421 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000422
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000423 if (snum)
424 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
425 isk->inet_sport = htons(isk->inet_num);
426 isk->inet_daddr = 0;
427 isk->inet_dport = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000428
429#if IS_ENABLED(CONFIG_IPV6)
430 if (sk->sk_family == AF_INET6)
431 memset(&inet6_sk(sk)->daddr, 0, sizeof(inet6_sk(sk)->daddr));
432#endif
433
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000434 sk_dst_reset(sk);
435out:
436 release_sock(sk);
437 pr_debug("ping_v4_bind -> %d\n", err);
438 return err;
439}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000440EXPORT_SYMBOL_GPL(ping_bind);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000441
442/*
443 * Is this a supported type of ICMP message?
444 */
445
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000446static inline int ping_supported(int family, int type, int code)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000447{
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000448 return (family == AF_INET && type == ICMP_ECHO && code == 0) ||
449 (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000450}
451
452/*
453 * This routine is called by the ICMP module when it gets some
454 * sort of error condition.
455 */
456
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000457void ping_err(struct sk_buff *skb, int offset, u32 info)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000458{
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000459 int family;
460 struct icmphdr *icmph;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000461 struct inet_sock *inet_sock;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000462 int type;
463 int code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000464 struct net *net = dev_net(skb->dev);
465 struct sock *sk;
466 int harderr;
467 int err;
468
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000469 if (skb->protocol == htons(ETH_P_IP)) {
470 struct iphdr *iph = (struct iphdr *)skb->data;
471 offset = iph->ihl << 2;
472 family = AF_INET;
473 type = icmp_hdr(skb)->type;
474 code = icmp_hdr(skb)->code;
475 icmph = (struct icmphdr *)(skb->data + offset);
476 } else if (skb->protocol == htons(ETH_P_IPV6)) {
477 family = AF_INET6;
478 type = icmp6_hdr(skb)->icmp6_type;
479 code = icmp6_hdr(skb)->icmp6_code;
480 icmph = (struct icmphdr *) (skb->data + offset);
481 } else {
482 BUG();
483 }
484
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000485 /* We assume the packet has already been checked by icmp_unreach */
486
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000487 if (!ping_supported(family, icmph->type, icmph->code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000488 return;
489
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000490 pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
491 skb->protocol, type, code, ntohs(icmph->un.echo.id),
492 ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000493
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000494 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000495 if (sk == NULL) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000496 pr_debug("no socket, dropping\n");
497 return; /* No socket for error */
498 }
499 pr_debug("err on socket %p\n", sk);
500
501 err = 0;
502 harderr = 0;
503 inet_sock = inet_sk(sk);
504
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000505 if (skb->protocol == htons(ETH_P_IP)) {
506 switch (type) {
507 default:
508 case ICMP_TIME_EXCEEDED:
509 err = EHOSTUNREACH;
510 break;
511 case ICMP_SOURCE_QUENCH:
512 /* This is not a real error but ping wants to see it.
513 * Report it with some fake errno. */
514 err = EREMOTEIO;
515 break;
516 case ICMP_PARAMETERPROB:
517 err = EPROTO;
518 harderr = 1;
519 break;
520 case ICMP_DEST_UNREACH:
521 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
522 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
523 err = EMSGSIZE;
524 harderr = 1;
525 break;
526 }
527 goto out;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000528 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000529 err = EHOSTUNREACH;
530 if (code <= NR_ICMP_UNREACH) {
531 harderr = icmp_err_convert[code].fatal;
532 err = icmp_err_convert[code].errno;
533 }
534 break;
535 case ICMP_REDIRECT:
536 /* See ICMP_SOURCE_QUENCH */
537 err = EREMOTEIO;
538 break;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000539 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000540#if IS_ENABLED(CONFIG_IPV6)
541 } else if (skb->protocol == htons(ETH_P_IPV6)) {
542 harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
543#endif
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000544 }
545
546 /*
547 * RFC1122: OK. Passes ICMP errors back to application, as per
548 * 4.1.3.3.
549 */
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000550 if ((family == AF_INET && !inet_sock->recverr) ||
551 (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000552 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
553 goto out;
554 } else {
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000555 if (family == AF_INET) {
556 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
557 info, (u8 *)icmph);
558#if IS_ENABLED(CONFIG_IPV6)
559 } else if (family == AF_INET6) {
560 pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
561 info, (u8 *)icmph);
562#endif
563 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000564 }
565 sk->sk_err = err;
566 sk->sk_error_report(sk);
567out:
568 sock_put(sk);
569}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000570EXPORT_SYMBOL_GPL(ping_err);
571
572void ping_v4_err(struct sk_buff *skb, u32 info)
573{
574 ping_err(skb, 0, info);
575}
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000576
577/*
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000578 * Copy and checksum an ICMP Echo packet from user space into a buffer
579 * starting from the payload.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000580 */
581
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000582int ping_getfrag(void *from, char *to,
583 int offset, int fraglen, int odd, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000584{
585 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
586
587 if (offset == 0) {
588 if (fraglen < sizeof(struct icmphdr))
589 BUG();
590 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
591 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
592 &pfh->wcheck))
593 return -EFAULT;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000594 } else if (offset < sizeof(struct icmphdr)) {
595 BUG();
596 } else {
597 if (csum_partial_copy_fromiovecend
598 (to, pfh->iov, offset - sizeof(struct icmphdr),
599 fraglen, &pfh->wcheck))
600 return -EFAULT;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000601 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000602
603#if IS_ENABLED(CONFIG_IPV6)
604 /* For IPv6, checksum each skb as we go along, as expected by
605 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
606 * wcheck, it will be finalized in ping_v4_push_pending_frames.
607 */
608 if (pfh->family == AF_INET6) {
609 skb->csum = pfh->wcheck;
610 skb->ip_summed = CHECKSUM_NONE;
611 pfh->wcheck = 0;
612 }
613#endif
614
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000615 return 0;
616}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000617EXPORT_SYMBOL_GPL(ping_getfrag);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000618
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000619static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
620 struct flowi4 *fl4)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000621{
622 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
623
624 pfh->wcheck = csum_partial((char *)&pfh->icmph,
625 sizeof(struct icmphdr), pfh->wcheck);
626 pfh->icmph.checksum = csum_fold(pfh->wcheck);
627 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
628 skb->ip_summed = CHECKSUM_NONE;
629 return ip_push_pending_frames(sk, fl4);
630}
631
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000632int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
633 void *user_icmph, size_t icmph_len) {
634 u8 type, code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000635
636 if (len > 0xFFFF)
637 return -EMSGSIZE;
638
639 /*
640 * Check the flags.
641 */
642
643 /* Mirror BSD error message compatibility */
644 if (msg->msg_flags & MSG_OOB)
645 return -EOPNOTSUPP;
646
647 /*
648 * Fetch the ICMP header provided by the userland.
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000649 * iovec is modified! The ICMP header is consumed.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000650 */
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000651 if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000652 return -EFAULT;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000653
654 if (family == AF_INET) {
655 type = ((struct icmphdr *) user_icmph)->type;
656 code = ((struct icmphdr *) user_icmph)->code;
657#if IS_ENABLED(CONFIG_IPV6)
658 } else if (family == AF_INET6) {
659 type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
660 code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
661#endif
662 } else {
663 BUG();
664 }
665
666 if (!ping_supported(family, type, code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000667 return -EINVAL;
668
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000669 return 0;
670}
671EXPORT_SYMBOL_GPL(ping_common_sendmsg);
672
673int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
674 size_t len)
675{
676 struct net *net = sock_net(sk);
677 struct flowi4 fl4;
678 struct inet_sock *inet = inet_sk(sk);
679 struct ipcm_cookie ipc;
680 struct icmphdr user_icmph;
681 struct pingfakehdr pfh;
682 struct rtable *rt = NULL;
683 struct ip_options_data opt_copy;
684 int free = 0;
685 __be32 saddr, daddr, faddr;
686 u8 tos;
687 int err;
688
689 pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
690
691 err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
692 sizeof(user_icmph));
693 if (err)
694 return err;
695
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000696 /*
697 * Get and verify the address.
698 */
699
700 if (msg->msg_name) {
701 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
702 if (msg->msg_namelen < sizeof(*usin))
703 return -EINVAL;
704 if (usin->sin_family != AF_INET)
705 return -EINVAL;
706 daddr = usin->sin_addr.s_addr;
707 /* no remote port */
708 } else {
709 if (sk->sk_state != TCP_ESTABLISHED)
710 return -EDESTADDRREQ;
711 daddr = inet->inet_daddr;
712 /* no remote port */
713 }
714
715 ipc.addr = inet->inet_saddr;
716 ipc.opt = NULL;
717 ipc.oif = sk->sk_bound_dev_if;
718 ipc.tx_flags = 0;
719 err = sock_tx_timestamp(sk, &ipc.tx_flags);
720 if (err)
721 return err;
722
723 if (msg->msg_controllen) {
724 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
725 if (err)
726 return err;
727 if (ipc.opt)
728 free = 1;
729 }
730 if (!ipc.opt) {
731 struct ip_options_rcu *inet_opt;
732
733 rcu_read_lock();
734 inet_opt = rcu_dereference(inet->inet_opt);
735 if (inet_opt) {
736 memcpy(&opt_copy, inet_opt,
737 sizeof(*inet_opt) + inet_opt->opt.optlen);
738 ipc.opt = &opt_copy.opt;
739 }
740 rcu_read_unlock();
741 }
742
743 saddr = ipc.addr;
744 ipc.addr = faddr = daddr;
745
746 if (ipc.opt && ipc.opt->opt.srr) {
747 if (!daddr)
748 return -EINVAL;
749 faddr = ipc.opt->opt.faddr;
750 }
751 tos = RT_TOS(inet->tos);
752 if (sock_flag(sk, SOCK_LOCALROUTE) ||
753 (msg->msg_flags & MSG_DONTROUTE) ||
754 (ipc.opt && ipc.opt->opt.is_strictroute)) {
755 tos |= RTO_ONLINK;
756 }
757
758 if (ipv4_is_multicast(daddr)) {
759 if (!ipc.oif)
760 ipc.oif = inet->mc_index;
761 if (!saddr)
762 saddr = inet->mc_addr;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000763 } else if (!ipc.oif)
764 ipc.oif = inet->uc_index;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000765
766 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
767 RT_SCOPE_UNIVERSE, sk->sk_protocol,
768 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
769
770 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
771 rt = ip_route_output_flow(net, &fl4, sk);
772 if (IS_ERR(rt)) {
773 err = PTR_ERR(rt);
774 rt = NULL;
775 if (err == -ENETUNREACH)
776 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
777 goto out;
778 }
779
780 err = -EACCES;
781 if ((rt->rt_flags & RTCF_BROADCAST) &&
782 !sock_flag(sk, SOCK_BROADCAST))
783 goto out;
784
785 if (msg->msg_flags & MSG_CONFIRM)
786 goto do_confirm;
787back_from_confirm:
788
789 if (!ipc.addr)
790 ipc.addr = fl4.daddr;
791
792 lock_sock(sk);
793
794 pfh.icmph.type = user_icmph.type; /* already checked */
795 pfh.icmph.code = user_icmph.code; /* ditto */
796 pfh.icmph.checksum = 0;
797 pfh.icmph.un.echo.id = inet->inet_sport;
798 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
799 pfh.iov = msg->msg_iov;
800 pfh.wcheck = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000801 pfh.family = AF_INET;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000802
803 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
804 0, &ipc, &rt, msg->msg_flags);
805 if (err)
806 ip_flush_pending_frames(sk);
807 else
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000808 err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000809 release_sock(sk);
810
811out:
812 ip_rt_put(rt);
813 if (free)
814 kfree(ipc.opt);
815 if (!err) {
816 icmp_out_count(sock_net(sk), user_icmph.type);
817 return len;
818 }
819 return err;
820
821do_confirm:
822 dst_confirm(&rt->dst);
823 if (!(msg->msg_flags & MSG_PROBE) || len)
824 goto back_from_confirm;
825 err = 0;
826 goto out;
827}
828
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000829int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
830 size_t len, int noblock, int flags, int *addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000831{
832 struct inet_sock *isk = inet_sk(sk);
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000833 int family = sk->sk_family;
834 struct sockaddr_in *sin;
835 struct sockaddr_in6 *sin6;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000836 struct sk_buff *skb;
837 int copied, err;
838
839 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
840
David S. Millera5e74242012-02-21 17:59:19 -0500841 err = -EOPNOTSUPP;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000842 if (flags & MSG_OOB)
843 goto out;
844
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000845 if (addr_len) {
846 if (family == AF_INET)
847 *addr_len = sizeof(*sin);
848 else if (family == AF_INET6 && addr_len)
849 *addr_len = sizeof(*sin6);
850 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000851
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000852 if (flags & MSG_ERRQUEUE) {
853 if (family == AF_INET) {
854 return ip_recv_error(sk, msg, len);
855#if IS_ENABLED(CONFIG_IPV6)
856 } else if (family == AF_INET6) {
857 return pingv6_ops.ipv6_recv_error(sk, msg, len);
858#endif
859 }
860 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000861
862 skb = skb_recv_datagram(sk, flags, noblock, &err);
863 if (!skb)
864 goto out;
865
866 copied = skb->len;
867 if (copied > len) {
868 msg->msg_flags |= MSG_TRUNC;
869 copied = len;
870 }
871
872 /* Don't bother checking the checksum */
873 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
874 if (err)
875 goto done;
876
877 sock_recv_timestamp(msg, sk, skb);
878
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000879 /* Copy the address and add cmsg data. */
880 if (family == AF_INET) {
881 sin = (struct sockaddr_in *) msg->msg_name;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000882 sin->sin_family = AF_INET;
883 sin->sin_port = 0 /* skb->h.uh->source */;
884 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
885 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000886
887 if (isk->cmsg_flags)
888 ip_cmsg_recv(msg, skb);
889
890#if IS_ENABLED(CONFIG_IPV6)
891 } else if (family == AF_INET6) {
892 struct ipv6_pinfo *np = inet6_sk(sk);
893 struct ipv6hdr *ip6 = ipv6_hdr(skb);
894 sin6 = (struct sockaddr_in6 *) msg->msg_name;
895 sin6->sin6_family = AF_INET6;
896 sin6->sin6_port = 0;
897 sin6->sin6_addr = ip6->saddr;
898
Cong Wanga1a35442014-03-10 18:59:25 -0600899 sin6->sin6_flowinfo = 0;
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000900 if (np->sndflow)
901 sin6->sin6_flowinfo =
902 *(__be32 *)ip6 & IPV6_FLOWINFO_MASK;
903
Cong Wanga1a35442014-03-10 18:59:25 -0600904 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
905 IP6CB(skb)->iif);
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000906
907 if (inet6_sk(sk)->rxopt.all)
908 pingv6_ops.datagram_recv_ctl(sk, msg, skb);
909#endif
910 } else {
911 BUG();
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000912 }
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000913
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000914 err = copied;
915
916done:
917 skb_free_datagram(sk, skb);
918out:
919 pr_debug("ping_recvmsg -> %d\n", err);
920 return err;
921}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000922EXPORT_SYMBOL_GPL(ping_recvmsg);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000923
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000924int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000925{
926 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000927 inet_sk(sk), inet_sk(sk)->inet_num, skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000928 if (sock_queue_rcv_skb(sk, skb) < 0) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000929 kfree_skb(skb);
930 pr_debug("ping_queue_rcv_skb -> failed\n");
931 return -1;
932 }
933 return 0;
934}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000935EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000936
937
938/*
939 * All we need to do is get the socket.
940 */
941
942void ping_rcv(struct sk_buff *skb)
943{
944 struct sock *sk;
945 struct net *net = dev_net(skb->dev);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000946 struct icmphdr *icmph = icmp_hdr(skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000947
948 /* We assume the packet has already been checked by icmp_rcv */
949
950 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000951 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000952
953 /* Push ICMP header back */
954 skb_push(skb, skb->data - (u8 *)icmph);
955
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000956 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000957 if (sk != NULL) {
958 pr_debug("rcv on socket %p\n", sk);
959 ping_queue_rcv_skb(sk, skb_get(skb));
960 sock_put(sk);
961 return;
962 }
963 pr_debug("no socket, dropping\n");
964
965 /* We're called from icmp_rcv(). kfree_skb() is done there. */
966}
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000967EXPORT_SYMBOL_GPL(ping_rcv);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000968
969struct proto ping_prot = {
970 .name = "PING",
971 .owner = THIS_MODULE,
972 .init = ping_init_sock,
973 .close = ping_close,
974 .connect = ip4_datagram_connect,
975 .disconnect = udp_disconnect,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000976 .setsockopt = ip_setsockopt,
977 .getsockopt = ip_getsockopt,
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000978 .sendmsg = ping_v4_sendmsg,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000979 .recvmsg = ping_recvmsg,
980 .bind = ping_bind,
981 .backlog_rcv = ping_queue_rcv_skb,
Lorenzo Colitti8b17a9a2013-01-16 22:09:49 +0000982 .hash = ping_hash,
983 .unhash = ping_unhash,
984 .get_port = ping_get_port,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000985 .obj_size = sizeof(struct inet_sock),
986};
987EXPORT_SYMBOL(ping_prot);
988
989#ifdef CONFIG_PROC_FS
990
991static struct sock *ping_get_first(struct seq_file *seq, int start)
992{
993 struct sock *sk;
994 struct ping_iter_state *state = seq->private;
995 struct net *net = seq_file_net(seq);
996
997 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
998 ++state->bucket) {
999 struct hlist_nulls_node *node;
Changli Gao75e308c2011-05-18 21:16:01 +00001000 struct hlist_nulls_head *hslot;
1001
1002 hslot = &ping_table.hash[state->bucket];
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001003
1004 if (hlist_nulls_empty(hslot))
1005 continue;
1006
1007 sk_nulls_for_each(sk, node, hslot) {
1008 if (net_eq(sock_net(sk), net))
1009 goto found;
1010 }
1011 }
1012 sk = NULL;
1013found:
1014 return sk;
1015}
1016
1017static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1018{
1019 struct ping_iter_state *state = seq->private;
1020 struct net *net = seq_file_net(seq);
1021
1022 do {
1023 sk = sk_nulls_next(sk);
1024 } while (sk && (!net_eq(sock_net(sk), net)));
1025
1026 if (!sk)
1027 return ping_get_first(seq, state->bucket + 1);
1028 return sk;
1029}
1030
1031static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1032{
1033 struct sock *sk = ping_get_first(seq, 0);
1034
1035 if (sk)
1036 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1037 --pos;
1038 return pos ? NULL : sk;
1039}
1040
1041static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
1042{
1043 struct ping_iter_state *state = seq->private;
1044 state->bucket = 0;
1045
1046 read_lock_bh(&ping_table.lock);
1047
1048 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1049}
1050
1051static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1052{
1053 struct sock *sk;
1054
1055 if (v == SEQ_START_TOKEN)
1056 sk = ping_get_idx(seq, 0);
1057 else
1058 sk = ping_get_next(seq, v);
1059
1060 ++*pos;
1061 return sk;
1062}
1063
1064static void ping_seq_stop(struct seq_file *seq, void *v)
1065{
1066 read_unlock_bh(&ping_table.lock);
1067}
1068
1069static void ping_format_sock(struct sock *sp, struct seq_file *f,
1070 int bucket, int *len)
1071{
1072 struct inet_sock *inet = inet_sk(sp);
1073 __be32 dest = inet->inet_daddr;
1074 __be32 src = inet->inet_rcv_saddr;
1075 __u16 destp = ntohs(inet->inet_dport);
1076 __u16 srcp = ntohs(inet->inet_sport);
1077
1078 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1079 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
1080 bucket, src, srcp, dest, destp, sp->sk_state,
1081 sk_wmem_alloc_get(sp),
1082 sk_rmem_alloc_get(sp),
1083 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1084 atomic_read(&sp->sk_refcnt), sp,
1085 atomic_read(&sp->sk_drops), len);
1086}
1087
1088static int ping_seq_show(struct seq_file *seq, void *v)
1089{
1090 if (v == SEQ_START_TOKEN)
1091 seq_printf(seq, "%-127s\n",
1092 " sl local_address rem_address st tx_queue "
1093 "rx_queue tr tm->when retrnsmt uid timeout "
1094 "inode ref pointer drops");
1095 else {
1096 struct ping_iter_state *state = seq->private;
1097 int len;
1098
1099 ping_format_sock(v, seq, state->bucket, &len);
1100 seq_printf(seq, "%*s\n", 127 - len, "");
1101 }
1102 return 0;
1103}
1104
1105static const struct seq_operations ping_seq_ops = {
1106 .show = ping_seq_show,
1107 .start = ping_seq_start,
1108 .next = ping_seq_next,
1109 .stop = ping_seq_stop,
1110};
1111
1112static int ping_seq_open(struct inode *inode, struct file *file)
1113{
1114 return seq_open_net(inode, file, &ping_seq_ops,
1115 sizeof(struct ping_iter_state));
1116}
1117
1118static const struct file_operations ping_seq_fops = {
1119 .open = ping_seq_open,
1120 .read = seq_read,
1121 .llseek = seq_lseek,
1122 .release = seq_release_net,
1123};
1124
1125static int ping_proc_register(struct net *net)
1126{
1127 struct proc_dir_entry *p;
1128 int rc = 0;
1129
1130 p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
1131 if (!p)
1132 rc = -ENOMEM;
1133 return rc;
1134}
1135
1136static void ping_proc_unregister(struct net *net)
1137{
1138 proc_net_remove(net, "icmp");
1139}
1140
1141
1142static int __net_init ping_proc_init_net(struct net *net)
1143{
1144 return ping_proc_register(net);
1145}
1146
1147static void __net_exit ping_proc_exit_net(struct net *net)
1148{
1149 ping_proc_unregister(net);
1150}
1151
1152static struct pernet_operations ping_net_ops = {
1153 .init = ping_proc_init_net,
1154 .exit = ping_proc_exit_net,
1155};
1156
1157int __init ping_proc_init(void)
1158{
1159 return register_pernet_subsys(&ping_net_ops);
1160}
1161
1162void ping_proc_exit(void)
1163{
1164 unregister_pernet_subsys(&ping_net_ops);
1165}
1166
1167#endif
1168
1169void __init ping_init(void)
1170{
1171 int i;
1172
1173 for (i = 0; i < PING_HTABLE_SIZE; i++)
1174 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1175 rwlock_init(&ping_table.lock);
1176}