blob: 46b8ad858955e169f791e6c7e2c958adf599717d [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;
252 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,
775 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
776
777 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
778 rt = ip_route_output_flow(net, &fl4, sk);
779 if (IS_ERR(rt)) {
780 err = PTR_ERR(rt);
781 rt = NULL;
782 if (err == -ENETUNREACH)
783 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
784 goto out;
785 }
786
787 err = -EACCES;
788 if ((rt->rt_flags & RTCF_BROADCAST) &&
789 !sock_flag(sk, SOCK_BROADCAST))
790 goto out;
791
792 if (msg->msg_flags & MSG_CONFIRM)
793 goto do_confirm;
794back_from_confirm:
795
796 if (!ipc.addr)
797 ipc.addr = fl4.daddr;
798
799 lock_sock(sk);
800
801 pfh.icmph.type = user_icmph.type; /* already checked */
802 pfh.icmph.code = user_icmph.code; /* ditto */
803 pfh.icmph.checksum = 0;
804 pfh.icmph.un.echo.id = inet->inet_sport;
805 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
806 pfh.iov = msg->msg_iov;
807 pfh.wcheck = 0;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000808 pfh.family = AF_INET;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000809
810 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
811 0, &ipc, &rt, msg->msg_flags);
812 if (err)
813 ip_flush_pending_frames(sk);
814 else
Lorenzo Colittif7048202013-01-16 22:09:49 +0000815 err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000816 release_sock(sk);
817
818out:
819 ip_rt_put(rt);
820 if (free)
821 kfree(ipc.opt);
822 if (!err) {
823 icmp_out_count(sock_net(sk), user_icmph.type);
824 return len;
825 }
826 return err;
827
828do_confirm:
829 dst_confirm(&rt->dst);
830 if (!(msg->msg_flags & MSG_PROBE) || len)
831 goto back_from_confirm;
832 err = 0;
833 goto out;
834}
835
Lorenzo Colittif7048202013-01-16 22:09:49 +0000836int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
837 size_t len, int noblock, int flags, int *addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000838{
839 struct inet_sock *isk = inet_sk(sk);
Lorenzo Colittif7048202013-01-16 22:09:49 +0000840 int family = sk->sk_family;
841 struct sockaddr_in *sin;
842 struct sockaddr_in6 *sin6;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000843 struct sk_buff *skb;
844 int copied, err;
845
846 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
847
David S. Millera5e74242012-02-21 17:59:19 -0500848 err = -EOPNOTSUPP;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000849 if (flags & MSG_OOB)
850 goto out;
851
Lorenzo Colittif7048202013-01-16 22:09:49 +0000852 if (addr_len) {
853 if (family == AF_INET)
854 *addr_len = sizeof(*sin);
855 else if (family == AF_INET6 && addr_len)
856 *addr_len = sizeof(*sin6);
857 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000858
Lorenzo Colittif7048202013-01-16 22:09:49 +0000859 if (flags & MSG_ERRQUEUE) {
860 if (family == AF_INET) {
861 return ip_recv_error(sk, msg, len);
862#if IS_ENABLED(CONFIG_IPV6)
863 } else if (family == AF_INET6) {
864 return pingv6_ops.ipv6_recv_error(sk, msg, len);
865#endif
866 }
867 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000868
869 skb = skb_recv_datagram(sk, flags, noblock, &err);
870 if (!skb)
871 goto out;
872
873 copied = skb->len;
874 if (copied > len) {
875 msg->msg_flags |= MSG_TRUNC;
876 copied = len;
877 }
878
879 /* Don't bother checking the checksum */
880 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
881 if (err)
882 goto done;
883
884 sock_recv_timestamp(msg, sk, skb);
885
Lorenzo Colittif7048202013-01-16 22:09:49 +0000886 /* Copy the address and add cmsg data. */
887 if (family == AF_INET) {
888 sin = (struct sockaddr_in *) msg->msg_name;
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700889 if (sin) {
890 sin->sin_family = AF_INET;
891 sin->sin_port = 0 /* skb->h.uh->source */;
892 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
893 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
894 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000895
896 if (isk->cmsg_flags)
897 ip_cmsg_recv(msg, skb);
898
899#if IS_ENABLED(CONFIG_IPV6)
900 } else if (family == AF_INET6) {
901 struct ipv6_pinfo *np = inet6_sk(sk);
902 struct ipv6hdr *ip6 = ipv6_hdr(skb);
903 sin6 = (struct sockaddr_in6 *) msg->msg_name;
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700904 if (sin6) {
905 sin6->sin6_family = AF_INET6;
906 sin6->sin6_port = 0;
907 sin6->sin6_addr = ip6->saddr;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000908
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700909 if (np->sndflow)
910 sin6->sin6_flowinfo =
911 *(__be32 *)ip6 & IPV6_FLOWINFO_MASK;
Lorenzo Colittif7048202013-01-16 22:09:49 +0000912
Hannes Frederic Sowabfe5eb62014-05-19 14:16:47 -0700913 if (__ipv6_addr_needs_scope_id(
914 ipv6_addr_type(&sin6->sin6_addr)))
915 sin6->sin6_scope_id = IP6CB(skb)->iif;
916 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000917
918 if (inet6_sk(sk)->rxopt.all)
919 pingv6_ops.datagram_recv_ctl(sk, msg, skb);
920#endif
921 } else {
922 BUG();
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000923 }
Lorenzo Colittif7048202013-01-16 22:09:49 +0000924
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000925 err = copied;
926
927done:
928 skb_free_datagram(sk, skb);
929out:
930 pr_debug("ping_recvmsg -> %d\n", err);
931 return err;
932}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000933EXPORT_SYMBOL_GPL(ping_recvmsg);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000934
Lorenzo Colittif7048202013-01-16 22:09:49 +0000935int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000936{
937 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000938 inet_sk(sk), inet_sk(sk)->inet_num, skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000939 if (sock_queue_rcv_skb(sk, skb) < 0) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000940 kfree_skb(skb);
941 pr_debug("ping_queue_rcv_skb -> failed\n");
942 return -1;
943 }
944 return 0;
945}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000946EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000947
948
949/*
950 * All we need to do is get the socket.
951 */
952
953void ping_rcv(struct sk_buff *skb)
954{
955 struct sock *sk;
956 struct net *net = dev_net(skb->dev);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000957 struct icmphdr *icmph = icmp_hdr(skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000958
959 /* We assume the packet has already been checked by icmp_rcv */
960
961 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000962 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000963
964 /* Push ICMP header back */
965 skb_push(skb, skb->data - (u8 *)icmph);
966
Lorenzo Colittif7048202013-01-16 22:09:49 +0000967 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000968 if (sk != NULL) {
969 pr_debug("rcv on socket %p\n", sk);
970 ping_queue_rcv_skb(sk, skb_get(skb));
971 sock_put(sk);
972 return;
973 }
974 pr_debug("no socket, dropping\n");
975
976 /* We're called from icmp_rcv(). kfree_skb() is done there. */
977}
Lorenzo Colittif7048202013-01-16 22:09:49 +0000978EXPORT_SYMBOL_GPL(ping_rcv);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000979
980struct proto ping_prot = {
981 .name = "PING",
982 .owner = THIS_MODULE,
983 .init = ping_init_sock,
984 .close = ping_close,
985 .connect = ip4_datagram_connect,
986 .disconnect = udp_disconnect,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000987 .setsockopt = ip_setsockopt,
988 .getsockopt = ip_getsockopt,
Lorenzo Colittif7048202013-01-16 22:09:49 +0000989 .sendmsg = ping_v4_sendmsg,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000990 .recvmsg = ping_recvmsg,
991 .bind = ping_bind,
992 .backlog_rcv = ping_queue_rcv_skb,
Lorenzo Colittif7048202013-01-16 22:09:49 +0000993 .hash = ping_hash,
994 .unhash = ping_unhash,
995 .get_port = ping_get_port,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000996 .obj_size = sizeof(struct inet_sock),
997};
998EXPORT_SYMBOL(ping_prot);
999
1000#ifdef CONFIG_PROC_FS
1001
1002static struct sock *ping_get_first(struct seq_file *seq, int start)
1003{
1004 struct sock *sk;
1005 struct ping_iter_state *state = seq->private;
1006 struct net *net = seq_file_net(seq);
1007
1008 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
1009 ++state->bucket) {
1010 struct hlist_nulls_node *node;
Changli Gao75e308c2011-05-18 21:16:01 +00001011 struct hlist_nulls_head *hslot;
1012
1013 hslot = &ping_table.hash[state->bucket];
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001014
1015 if (hlist_nulls_empty(hslot))
1016 continue;
1017
1018 sk_nulls_for_each(sk, node, hslot) {
1019 if (net_eq(sock_net(sk), net))
1020 goto found;
1021 }
1022 }
1023 sk = NULL;
1024found:
1025 return sk;
1026}
1027
1028static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1029{
1030 struct ping_iter_state *state = seq->private;
1031 struct net *net = seq_file_net(seq);
1032
1033 do {
1034 sk = sk_nulls_next(sk);
1035 } while (sk && (!net_eq(sock_net(sk), net)));
1036
1037 if (!sk)
1038 return ping_get_first(seq, state->bucket + 1);
1039 return sk;
1040}
1041
1042static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1043{
1044 struct sock *sk = ping_get_first(seq, 0);
1045
1046 if (sk)
1047 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1048 --pos;
1049 return pos ? NULL : sk;
1050}
1051
1052static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
1053{
1054 struct ping_iter_state *state = seq->private;
1055 state->bucket = 0;
1056
1057 read_lock_bh(&ping_table.lock);
1058
1059 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1060}
1061
1062static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1063{
1064 struct sock *sk;
1065
1066 if (v == SEQ_START_TOKEN)
1067 sk = ping_get_idx(seq, 0);
1068 else
1069 sk = ping_get_next(seq, v);
1070
1071 ++*pos;
1072 return sk;
1073}
1074
1075static void ping_seq_stop(struct seq_file *seq, void *v)
1076{
1077 read_unlock_bh(&ping_table.lock);
1078}
1079
1080static void ping_format_sock(struct sock *sp, struct seq_file *f,
1081 int bucket, int *len)
1082{
1083 struct inet_sock *inet = inet_sk(sp);
1084 __be32 dest = inet->inet_daddr;
1085 __be32 src = inet->inet_rcv_saddr;
1086 __u16 destp = ntohs(inet->inet_dport);
1087 __u16 srcp = ntohs(inet->inet_sport);
1088
1089 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1090 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
1091 bucket, src, srcp, dest, destp, sp->sk_state,
1092 sk_wmem_alloc_get(sp),
1093 sk_rmem_alloc_get(sp),
1094 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1095 atomic_read(&sp->sk_refcnt), sp,
1096 atomic_read(&sp->sk_drops), len);
1097}
1098
1099static int ping_seq_show(struct seq_file *seq, void *v)
1100{
1101 if (v == SEQ_START_TOKEN)
1102 seq_printf(seq, "%-127s\n",
1103 " sl local_address rem_address st tx_queue "
1104 "rx_queue tr tm->when retrnsmt uid timeout "
1105 "inode ref pointer drops");
1106 else {
1107 struct ping_iter_state *state = seq->private;
1108 int len;
1109
1110 ping_format_sock(v, seq, state->bucket, &len);
1111 seq_printf(seq, "%*s\n", 127 - len, "");
1112 }
1113 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}