blob: ccefc07beacd37d1c83298638c3de5f31bc07ba9 [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 Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +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
Eric Dumazet95c96172012-04-15 05:58:06 +000063static inline int ping_hashfn(struct net *net, unsigned int num, unsigned int mask)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000064{
65 int res = (num + net_hash_mix(net)) & mask;
Eric Dumazet95c96172012-04-15 05:58:06 +000066
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000067 pr_debug("hash(%d) = %d\n", num, res);
68 return res;
69}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +000070EXPORT_SYMBOL_GPL(ping_hash);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000071
72static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
Eric Dumazet95c96172012-04-15 05:58:06 +000073 struct net *net, unsigned int num)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000074{
75 return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
76}
77
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +000078int ping_get_port(struct sock *sk, unsigned short ident)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +000079{
80 struct hlist_nulls_node *node;
81 struct hlist_nulls_head *hlist;
82 struct inet_sock *isk, *isk2;
83 struct sock *sk2 = NULL;
84
85 isk = inet_sk(sk);
86 write_lock_bh(&ping_table.lock);
87 if (ident == 0) {
88 u32 i;
89 u16 result = ping_port_rover + 1;
90
91 for (i = 0; i < (1L << 16); i++, result++) {
92 if (!result)
93 result++; /* avoid zero */
94 hlist = ping_hashslot(&ping_table, sock_net(sk),
95 result);
96 ping_portaddr_for_each_entry(sk2, node, hlist) {
97 isk2 = inet_sk(sk2);
98
99 if (isk2->inet_num == result)
100 goto next_port;
101 }
102
103 /* found */
104 ping_port_rover = ident = result;
105 break;
106next_port:
107 ;
108 }
109 if (i >= (1L << 16))
110 goto fail;
111 } else {
112 hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
113 ping_portaddr_for_each_entry(sk2, node, hlist) {
114 isk2 = inet_sk(sk2);
115
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000116 /* BUG? Why is this reuse and not reuseaddr? ping.c
117 * doesn't turn off SO_REUSEADDR, and it doesn't expect
118 * that other ping processes can steal its packets.
119 */
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000120 if ((isk2->inet_num == ident) &&
121 (sk2 != sk) &&
122 (!sk2->sk_reuse || !sk->sk_reuse))
123 goto fail;
124 }
125 }
126
127 pr_debug("found port/ident = %d\n", ident);
128 isk->inet_num = ident;
129 if (sk_unhashed(sk)) {
130 pr_debug("was not hashed\n");
131 sock_hold(sk);
132 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
133 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
134 }
135 write_unlock_bh(&ping_table.lock);
136 return 0;
137
138fail:
139 write_unlock_bh(&ping_table.lock);
140 return 1;
141}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000142EXPORT_SYMBOL_GPL(ping_get_port);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000143
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000144void ping_hash(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000145{
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000146 pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000147 BUG(); /* "Please do not press this button again." */
148}
149
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000150void ping_unhash(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000151{
152 struct inet_sock *isk = inet_sk(sk);
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000153 pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000154 if (sk_hashed(sk)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000155 write_lock_bh(&ping_table.lock);
156 hlist_nulls_del(&sk->sk_nulls_node);
157 sock_put(sk);
Eric Dumazet747465e2012-01-16 19:27:39 +0000158 isk->inet_num = 0;
159 isk->inet_sport = 0;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000160 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
161 write_unlock_bh(&ping_table.lock);
162 }
163}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000164EXPORT_SYMBOL_GPL(ping_unhash);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000165
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000166static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000167{
168 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
169 struct sock *sk = NULL;
170 struct inet_sock *isk;
171 struct hlist_nulls_node *hnode;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000172 int dif = skb->dev->ifindex;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000173
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000174 if (skb->protocol == htons(ETH_P_IP)) {
175 pr_debug("try to find: num = %d, daddr = %pI4, dif = %d\n",
176 (int)ident, &ip_hdr(skb)->daddr, dif);
177#if IS_ENABLED(CONFIG_IPV6)
178 } else if (skb->protocol == htons(ETH_P_IPV6)) {
179 pr_debug("try to find: num = %d, daddr = %pI6c, dif = %d\n",
180 (int)ident, &ipv6_hdr(skb)->daddr, dif);
181#endif
182 }
183
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000184 read_lock_bh(&ping_table.lock);
185
186 ping_portaddr_for_each_entry(sk, hnode, hslot) {
187 isk = inet_sk(sk);
188
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000189 pr_debug("iterate\n");
190 if (isk->inet_num != ident)
191 continue;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000192
193 if (skb->protocol == htons(ETH_P_IP) &&
194 sk->sk_family == AF_INET) {
195 pr_debug("found: %p: num=%d, daddr=%pI4, dif=%d\n", sk,
196 (int) isk->inet_num, &isk->inet_rcv_saddr,
197 sk->sk_bound_dev_if);
198
199 if (isk->inet_rcv_saddr &&
200 isk->inet_rcv_saddr != ip_hdr(skb)->daddr)
201 continue;
202#if IS_ENABLED(CONFIG_IPV6)
203 } else if (skb->protocol == htons(ETH_P_IPV6) &&
204 sk->sk_family == AF_INET6) {
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000205
206 pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
207 (int) isk->inet_num,
Eric Dumazetefe42082013-10-03 15:42:29 -0700208 &sk->sk_v6_rcv_saddr,
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000209 sk->sk_bound_dev_if);
210
Eric Dumazetefe42082013-10-03 15:42:29 -0700211 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
212 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr,
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000213 &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
Eric W. Biederman7064d162012-05-24 10:34:21 -0600232static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
233 kgid_t *high)
Vasiliy Kulikovf56e03e2011-05-17 00:16:56 +0000234{
Eric W. Biederman7064d162012-05-24 10:34:21 -0600235 kgid_t *data = net->ipv4.sysctl_ping_group_range;
Eric Dumazet95c96172012-04-15 05:58:06 +0000236 unsigned int seq;
237
Vasiliy Kulikovf56e03e2011-05-17 00:16:56 +0000238 do {
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -0700239 seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock);
Vasiliy Kulikovf56e03e2011-05-17 00:16:56 +0000240
241 *low = data[0];
242 *high = data[1];
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -0700243 } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq));
Vasiliy Kulikovf56e03e2011-05-17 00:16:56 +0000244}
245
246
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000247int ping_init_sock(struct sock *sk)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000248{
249 struct net *net = sock_net(sk);
Eric W. Biederman7064d162012-05-24 10:34:21 -0600250 kgid_t group = current_egid();
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000251 struct group_info *group_info = get_current_groups();
252 int i, j, count = group_info->ngroups;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800253 kgid_t low, high;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000254
Eric W. Biederman7064d162012-05-24 10:34:21 -0600255 inet_get_ping_group_range_net(net, &low, &high);
256 if (gid_lte(low, group) && gid_lte(group, high))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000257 return 0;
258
259 for (i = 0; i < group_info->nblocks; i++) {
260 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000261 for (j = 0; j < cp_count; j++) {
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800262 kgid_t gid = group_info->blocks[i][j];
263 if (gid_lte(low, gid) && gid_lte(gid, high))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000264 return 0;
265 }
266
267 count -= cp_count;
268 }
269
270 return -EACCES;
271}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000272EXPORT_SYMBOL_GPL(ping_init_sock);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000273
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000282EXPORT_SYMBOL_GPL(ping_close);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000283
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000284/* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
Wu Fengguanga06a2d32013-06-12 21:04:16 +0800285static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
286 struct sockaddr *uaddr, int addr_len) {
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000287 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
Wu Fengguanga06a2d32013-06-12 21:04:16 +0800354static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000355{
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);
Eric Dumazetefe42082013-10-03 15:42:29 -0700364 sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000365#endif
366 }
367}
368
Wu Fengguanga06a2d32013-06-12 21:04:16 +0800369static void ping_clear_saddr(struct sock *sk, int dif)
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000370{
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);
Eric Dumazetefe42082013-10-03 15:42:29 -0700378 memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000379 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 Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000393 int dif = sk->sk_bound_dev_if;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000394
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000418 if ((sk->sk_family == AF_INET && isk->inet_rcv_saddr) ||
419 (sk->sk_family == AF_INET6 &&
Eric Dumazetefe42082013-10-03 15:42:29 -0700420 !ipv6_addr_any(&sk->sk_v6_rcv_saddr)))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000421 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000428
429#if IS_ENABLED(CONFIG_IPV6)
430 if (sk->sk_family == AF_INET6)
Eric Dumazetefe42082013-10-03 15:42:29 -0700431 memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000432#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 Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000446static inline int ping_supported(int family, int type, int code)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000447{
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000457void ping_err(struct sk_buff *skb, int offset, u32 info)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000458{
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000459 int family;
460 struct icmphdr *icmph;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000461 struct inet_sock *inet_sock;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +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 Colitti6d0bfe22013-05-22 20:17:31 +0000469 if (skb->protocol == htons(ETH_P_IP)) {
470 family = AF_INET;
471 type = icmp_hdr(skb)->type;
472 code = icmp_hdr(skb)->code;
473 icmph = (struct icmphdr *)(skb->data + offset);
474 } else if (skb->protocol == htons(ETH_P_IPV6)) {
475 family = AF_INET6;
476 type = icmp6_hdr(skb)->icmp6_type;
477 code = icmp6_hdr(skb)->icmp6_code;
478 icmph = (struct icmphdr *) (skb->data + offset);
479 } else {
480 BUG();
481 }
482
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000483 /* We assume the packet has already been checked by icmp_unreach */
484
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000485 if (!ping_supported(family, icmph->type, icmph->code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000486 return;
487
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000488 pr_debug("ping_err(proto=0x%x,type=%d,code=%d,id=%04x,seq=%04x)\n",
489 skb->protocol, type, code, ntohs(icmph->un.echo.id),
490 ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000491
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000492 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000493 if (sk == NULL) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000494 pr_debug("no socket, dropping\n");
495 return; /* No socket for error */
496 }
497 pr_debug("err on socket %p\n", sk);
498
499 err = 0;
500 harderr = 0;
501 inet_sock = inet_sk(sk);
502
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000503 if (skb->protocol == htons(ETH_P_IP)) {
504 switch (type) {
505 default:
506 case ICMP_TIME_EXCEEDED:
507 err = EHOSTUNREACH;
508 break;
509 case ICMP_SOURCE_QUENCH:
510 /* This is not a real error but ping wants to see it.
511 * Report it with some fake errno.
512 */
513 err = EREMOTEIO;
514 break;
515 case ICMP_PARAMETERPROB:
516 err = EPROTO;
517 harderr = 1;
518 break;
519 case ICMP_DEST_UNREACH:
520 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
521 ipv4_sk_update_pmtu(skb, sk, info);
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 Colitti6d0bfe22013-05-22 20:17:31 +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 ipv4_sk_redirect(skb, sk);
538 err = EREMOTEIO;
539 break;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000540 }
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000541#if IS_ENABLED(CONFIG_IPV6)
542 } else if (skb->protocol == htons(ETH_P_IPV6)) {
543 harderr = pingv6_ops.icmpv6_err_convert(type, code, &err);
544#endif
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000545 }
546
547 /*
548 * RFC1122: OK. Passes ICMP errors back to application, as per
549 * 4.1.3.3.
550 */
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000551 if ((family == AF_INET && !inet_sock->recverr) ||
552 (family == AF_INET6 && !inet6_sk(sk)->recverr)) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000553 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
554 goto out;
555 } else {
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000556 if (family == AF_INET) {
557 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
558 info, (u8 *)icmph);
559#if IS_ENABLED(CONFIG_IPV6)
560 } else if (family == AF_INET6) {
561 pingv6_ops.ipv6_icmp_error(sk, skb, err, 0,
562 info, (u8 *)icmph);
563#endif
564 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000565 }
566 sk->sk_err = err;
567 sk->sk_error_report(sk);
568out:
569 sock_put(sk);
570}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000571EXPORT_SYMBOL_GPL(ping_err);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000572
573/*
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000574 * Copy and checksum an ICMP Echo packet from user space into a buffer
575 * starting from the payload.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000576 */
577
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000578int ping_getfrag(void *from, char *to,
579 int offset, int fraglen, int odd, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000580{
581 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
582
583 if (offset == 0) {
584 if (fraglen < sizeof(struct icmphdr))
585 BUG();
586 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
587 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
588 &pfh->wcheck))
589 return -EFAULT;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000590 } else if (offset < sizeof(struct icmphdr)) {
591 BUG();
592 } else {
593 if (csum_partial_copy_fromiovecend
594 (to, pfh->iov, offset - sizeof(struct icmphdr),
595 fraglen, &pfh->wcheck))
596 return -EFAULT;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000597 }
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000598
599#if IS_ENABLED(CONFIG_IPV6)
600 /* For IPv6, checksum each skb as we go along, as expected by
601 * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in
602 * wcheck, it will be finalized in ping_v4_push_pending_frames.
603 */
604 if (pfh->family == AF_INET6) {
605 skb->csum = pfh->wcheck;
606 skb->ip_summed = CHECKSUM_NONE;
607 pfh->wcheck = 0;
608 }
609#endif
610
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000611 return 0;
612}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000613EXPORT_SYMBOL_GPL(ping_getfrag);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000614
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000615static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh,
616 struct flowi4 *fl4)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000617{
618 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
619
620 pfh->wcheck = csum_partial((char *)&pfh->icmph,
621 sizeof(struct icmphdr), pfh->wcheck);
622 pfh->icmph.checksum = csum_fold(pfh->wcheck);
623 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
624 skb->ip_summed = CHECKSUM_NONE;
625 return ip_push_pending_frames(sk, fl4);
626}
627
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000628int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
629 void *user_icmph, size_t icmph_len) {
630 u8 type, code;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000631
632 if (len > 0xFFFF)
633 return -EMSGSIZE;
634
635 /*
636 * Check the flags.
637 */
638
639 /* Mirror BSD error message compatibility */
640 if (msg->msg_flags & MSG_OOB)
641 return -EOPNOTSUPP;
642
643 /*
644 * Fetch the ICMP header provided by the userland.
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000645 * iovec is modified! The ICMP header is consumed.
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000646 */
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000647 if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000648 return -EFAULT;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000649
650 if (family == AF_INET) {
651 type = ((struct icmphdr *) user_icmph)->type;
652 code = ((struct icmphdr *) user_icmph)->code;
653#if IS_ENABLED(CONFIG_IPV6)
654 } else if (family == AF_INET6) {
655 type = ((struct icmp6hdr *) user_icmph)->icmp6_type;
656 code = ((struct icmp6hdr *) user_icmph)->icmp6_code;
657#endif
658 } else {
659 BUG();
660 }
661
662 if (!ping_supported(family, type, code))
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000663 return -EINVAL;
664
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000665 return 0;
666}
667EXPORT_SYMBOL_GPL(ping_common_sendmsg);
668
669int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
670 size_t len)
671{
672 struct net *net = sock_net(sk);
673 struct flowi4 fl4;
674 struct inet_sock *inet = inet_sk(sk);
675 struct ipcm_cookie ipc;
676 struct icmphdr user_icmph;
677 struct pingfakehdr pfh;
678 struct rtable *rt = NULL;
679 struct ip_options_data opt_copy;
680 int free = 0;
681 __be32 saddr, daddr, faddr;
682 u8 tos;
683 int err;
684
685 pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
686
687 err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph,
688 sizeof(user_icmph));
689 if (err)
690 return err;
691
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000692 /*
693 * Get and verify the address.
694 */
695
696 if (msg->msg_name) {
697 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
698 if (msg->msg_namelen < sizeof(*usin))
699 return -EINVAL;
700 if (usin->sin_family != AF_INET)
701 return -EINVAL;
702 daddr = usin->sin_addr.s_addr;
703 /* no remote port */
704 } else {
705 if (sk->sk_state != TCP_ESTABLISHED)
706 return -EDESTADDRREQ;
707 daddr = inet->inet_daddr;
708 /* no remote port */
709 }
710
711 ipc.addr = inet->inet_saddr;
712 ipc.opt = NULL;
713 ipc.oif = sk->sk_bound_dev_if;
714 ipc.tx_flags = 0;
Francesco Fuscoaa661582013-09-24 15:43:09 +0200715 ipc.ttl = 0;
716 ipc.tos = -1;
Daniel Borkmannbf84a012013-04-14 08:08:13 +0000717
718 sock_tx_timestamp(sk, &ipc.tx_flags);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000719
720 if (msg->msg_controllen) {
721 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
722 if (err)
723 return err;
724 if (ipc.opt)
725 free = 1;
726 }
727 if (!ipc.opt) {
728 struct ip_options_rcu *inet_opt;
729
730 rcu_read_lock();
731 inet_opt = rcu_dereference(inet->inet_opt);
732 if (inet_opt) {
733 memcpy(&opt_copy, inet_opt,
734 sizeof(*inet_opt) + inet_opt->opt.optlen);
735 ipc.opt = &opt_copy.opt;
736 }
737 rcu_read_unlock();
738 }
739
740 saddr = ipc.addr;
741 ipc.addr = faddr = daddr;
742
743 if (ipc.opt && ipc.opt->opt.srr) {
744 if (!daddr)
745 return -EINVAL;
746 faddr = ipc.opt->opt.faddr;
747 }
Francesco Fuscoaa661582013-09-24 15:43:09 +0200748 tos = get_rttos(&ipc, inet);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000749 if (sock_flag(sk, SOCK_LOCALROUTE) ||
750 (msg->msg_flags & MSG_DONTROUTE) ||
751 (ipc.opt && ipc.opt->opt.is_strictroute)) {
752 tos |= RTO_ONLINK;
753 }
754
755 if (ipv4_is_multicast(daddr)) {
756 if (!ipc.oif)
757 ipc.oif = inet->mc_index;
758 if (!saddr)
759 saddr = inet->mc_addr;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000760 } else if (!ipc.oif)
761 ipc.oif = inet->uc_index;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000762
763 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
764 RT_SCOPE_UNIVERSE, sk->sk_protocol,
765 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
766
767 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
768 rt = ip_route_output_flow(net, &fl4, sk);
769 if (IS_ERR(rt)) {
770 err = PTR_ERR(rt);
771 rt = NULL;
772 if (err == -ENETUNREACH)
773 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
774 goto out;
775 }
776
777 err = -EACCES;
778 if ((rt->rt_flags & RTCF_BROADCAST) &&
779 !sock_flag(sk, SOCK_BROADCAST))
780 goto out;
781
782 if (msg->msg_flags & MSG_CONFIRM)
783 goto do_confirm;
784back_from_confirm:
785
786 if (!ipc.addr)
787 ipc.addr = fl4.daddr;
788
789 lock_sock(sk);
790
791 pfh.icmph.type = user_icmph.type; /* already checked */
792 pfh.icmph.code = user_icmph.code; /* ditto */
793 pfh.icmph.checksum = 0;
794 pfh.icmph.un.echo.id = inet->inet_sport;
795 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
796 pfh.iov = msg->msg_iov;
797 pfh.wcheck = 0;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000798 pfh.family = AF_INET;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000799
800 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
801 0, &ipc, &rt, msg->msg_flags);
802 if (err)
803 ip_flush_pending_frames(sk);
804 else
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000805 err = ping_v4_push_pending_frames(sk, &pfh, &fl4);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000806 release_sock(sk);
807
808out:
809 ip_rt_put(rt);
810 if (free)
811 kfree(ipc.opt);
812 if (!err) {
813 icmp_out_count(sock_net(sk), user_icmph.type);
814 return len;
815 }
816 return err;
817
818do_confirm:
819 dst_confirm(&rt->dst);
820 if (!(msg->msg_flags & MSG_PROBE) || len)
821 goto back_from_confirm;
822 err = 0;
823 goto out;
824}
825
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000826int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
827 size_t len, int noblock, int flags, int *addr_len)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000828{
829 struct inet_sock *isk = inet_sk(sk);
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000830 int family = sk->sk_family;
831 struct sockaddr_in *sin;
832 struct sockaddr_in6 *sin6;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000833 struct sk_buff *skb;
834 int copied, err;
835
836 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
837
David S. Millera5e74242012-02-21 17:59:19 -0500838 err = -EOPNOTSUPP;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000839 if (flags & MSG_OOB)
840 goto out;
841
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000842 if (addr_len) {
843 if (family == AF_INET)
844 *addr_len = sizeof(*sin);
845 else if (family == AF_INET6 && addr_len)
846 *addr_len = sizeof(*sin6);
847 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000848
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000849 if (flags & MSG_ERRQUEUE) {
850 if (family == AF_INET) {
851 return ip_recv_error(sk, msg, len);
852#if IS_ENABLED(CONFIG_IPV6)
853 } else if (family == AF_INET6) {
854 return pingv6_ops.ipv6_recv_error(sk, msg, len);
855#endif
856 }
857 }
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000858
859 skb = skb_recv_datagram(sk, flags, noblock, &err);
860 if (!skb)
861 goto out;
862
863 copied = skb->len;
864 if (copied > len) {
865 msg->msg_flags |= MSG_TRUNC;
866 copied = len;
867 }
868
869 /* Don't bother checking the checksum */
870 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
871 if (err)
872 goto done;
873
874 sock_recv_timestamp(msg, sk, skb);
875
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000876 /* Copy the address and add cmsg data. */
877 if (family == AF_INET) {
878 sin = (struct sockaddr_in *) msg->msg_name;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000879 sin->sin_family = AF_INET;
880 sin->sin_port = 0 /* skb->h.uh->source */;
881 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
882 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000883
884 if (isk->cmsg_flags)
885 ip_cmsg_recv(msg, skb);
886
887#if IS_ENABLED(CONFIG_IPV6)
888 } else if (family == AF_INET6) {
889 struct ipv6_pinfo *np = inet6_sk(sk);
890 struct ipv6hdr *ip6 = ipv6_hdr(skb);
891 sin6 = (struct sockaddr_in6 *) msg->msg_name;
892 sin6->sin6_family = AF_INET6;
893 sin6->sin6_port = 0;
894 sin6->sin6_addr = ip6->saddr;
895
Cong Wangc26d6b42013-06-02 22:43:52 +0000896 sin6->sin6_flowinfo = 0;
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000897 if (np->sndflow)
898 sin6->sin6_flowinfo = ip6_flowinfo(ip6);
899
Cong Wangc26d6b42013-06-02 22:43:52 +0000900 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
901 IP6CB(skb)->iif);
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000902
903 if (inet6_sk(sk)->rxopt.all)
904 pingv6_ops.ip6_datagram_recv_ctl(sk, msg, skb);
905#endif
906 } else {
907 BUG();
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000908 }
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000909
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000910 err = copied;
911
912done:
913 skb_free_datagram(sk, skb);
914out:
915 pr_debug("ping_recvmsg -> %d\n", err);
916 return err;
917}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000918EXPORT_SYMBOL_GPL(ping_recvmsg);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000919
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000920int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000921{
922 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000923 inet_sk(sk), inet_sk(sk)->inet_num, skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000924 if (sock_queue_rcv_skb(sk, skb) < 0) {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000925 kfree_skb(skb);
926 pr_debug("ping_queue_rcv_skb -> failed\n");
927 return -1;
928 }
929 return 0;
930}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000931EXPORT_SYMBOL_GPL(ping_queue_rcv_skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000932
933
934/*
935 * All we need to do is get the socket.
936 */
937
938void ping_rcv(struct sk_buff *skb)
939{
940 struct sock *sk;
941 struct net *net = dev_net(skb->dev);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000942 struct icmphdr *icmph = icmp_hdr(skb);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000943
944 /* We assume the packet has already been checked by icmp_rcv */
945
946 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
Joe Perches058bd4d2012-03-11 18:36:11 +0000947 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000948
949 /* Push ICMP header back */
950 skb_push(skb, skb->data - (u8 *)icmph);
951
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000952 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000953 if (sk != NULL) {
954 pr_debug("rcv on socket %p\n", sk);
955 ping_queue_rcv_skb(sk, skb_get(skb));
956 sock_put(sk);
957 return;
958 }
959 pr_debug("no socket, dropping\n");
960
961 /* We're called from icmp_rcv(). kfree_skb() is done there. */
962}
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000963EXPORT_SYMBOL_GPL(ping_rcv);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000964
965struct proto ping_prot = {
966 .name = "PING",
967 .owner = THIS_MODULE,
968 .init = ping_init_sock,
969 .close = ping_close,
970 .connect = ip4_datagram_connect,
971 .disconnect = udp_disconnect,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000972 .setsockopt = ip_setsockopt,
973 .getsockopt = ip_getsockopt,
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000974 .sendmsg = ping_v4_sendmsg,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000975 .recvmsg = ping_recvmsg,
976 .bind = ping_bind,
977 .backlog_rcv = ping_queue_rcv_skb,
Steffen Klassert8141ed92013-01-21 02:00:03 +0000978 .release_cb = ip4_datagram_release_cb,
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +0000979 .hash = ping_hash,
980 .unhash = ping_unhash,
981 .get_port = ping_get_port,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +0000982 .obj_size = sizeof(struct inet_sock),
983};
984EXPORT_SYMBOL(ping_prot);
985
986#ifdef CONFIG_PROC_FS
987
988static struct sock *ping_get_first(struct seq_file *seq, int start)
989{
990 struct sock *sk;
991 struct ping_iter_state *state = seq->private;
992 struct net *net = seq_file_net(seq);
993
994 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
995 ++state->bucket) {
996 struct hlist_nulls_node *node;
Changli Gao75e308c2011-05-18 21:16:01 +0000997 struct hlist_nulls_head *hslot;
998
999 hslot = &ping_table.hash[state->bucket];
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001000
1001 if (hlist_nulls_empty(hslot))
1002 continue;
1003
1004 sk_nulls_for_each(sk, node, hslot) {
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001005 if (net_eq(sock_net(sk), net) &&
1006 sk->sk_family == state->family)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001007 goto found;
1008 }
1009 }
1010 sk = NULL;
1011found:
1012 return sk;
1013}
1014
1015static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
1016{
1017 struct ping_iter_state *state = seq->private;
1018 struct net *net = seq_file_net(seq);
1019
1020 do {
1021 sk = sk_nulls_next(sk);
1022 } while (sk && (!net_eq(sock_net(sk), net)));
1023
1024 if (!sk)
1025 return ping_get_first(seq, state->bucket + 1);
1026 return sk;
1027}
1028
1029static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
1030{
1031 struct sock *sk = ping_get_first(seq, 0);
1032
1033 if (sk)
1034 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
1035 --pos;
1036 return pos ? NULL : sk;
1037}
1038
Lorenzo Colittid862e542013-05-31 15:05:50 +00001039void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001040{
1041 struct ping_iter_state *state = seq->private;
1042 state->bucket = 0;
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001043 state->family = family;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001044
1045 read_lock_bh(&ping_table.lock);
1046
1047 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1048}
Lorenzo Colittid862e542013-05-31 15:05:50 +00001049EXPORT_SYMBOL_GPL(ping_seq_start);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001050
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001051static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos)
1052{
1053 return ping_seq_start(seq, pos, AF_INET);
1054}
1055
Lorenzo Colittid862e542013-05-31 15:05:50 +00001056void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001057{
1058 struct sock *sk;
1059
1060 if (v == SEQ_START_TOKEN)
1061 sk = ping_get_idx(seq, 0);
1062 else
1063 sk = ping_get_next(seq, v);
1064
1065 ++*pos;
1066 return sk;
1067}
Lorenzo Colittid862e542013-05-31 15:05:50 +00001068EXPORT_SYMBOL_GPL(ping_seq_next);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001069
Lorenzo Colittid862e542013-05-31 15:05:50 +00001070void ping_seq_stop(struct seq_file *seq, void *v)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001071{
1072 read_unlock_bh(&ping_table.lock);
1073}
Lorenzo Colittid862e542013-05-31 15:05:50 +00001074EXPORT_SYMBOL_GPL(ping_seq_stop);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001075
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001076static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001077 int bucket, int *len)
1078{
1079 struct inet_sock *inet = inet_sk(sp);
1080 __be32 dest = inet->inet_daddr;
1081 __be32 src = inet->inet_rcv_saddr;
1082 __u16 destp = ntohs(inet->inet_dport);
1083 __u16 srcp = ntohs(inet->inet_sport);
1084
1085 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001086 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n",
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001087 bucket, src, srcp, dest, destp, sp->sk_state,
1088 sk_wmem_alloc_get(sp),
1089 sk_rmem_alloc_get(sp),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06001090 0, 0L, 0,
1091 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
1092 0, sock_i_ino(sp),
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001093 atomic_read(&sp->sk_refcnt), sp,
1094 atomic_read(&sp->sk_drops), len);
1095}
1096
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001097static int ping_v4_seq_show(struct seq_file *seq, void *v)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001098{
1099 if (v == SEQ_START_TOKEN)
1100 seq_printf(seq, "%-127s\n",
1101 " sl local_address rem_address st tx_queue "
1102 "rx_queue tr tm->when retrnsmt uid timeout "
1103 "inode ref pointer drops");
1104 else {
1105 struct ping_iter_state *state = seq->private;
1106 int len;
1107
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001108 ping_v4_format_sock(v, seq, state->bucket, &len);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001109 seq_printf(seq, "%*s\n", 127 - len, "");
1110 }
1111 return 0;
1112}
1113
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001114static const struct seq_operations ping_v4_seq_ops = {
1115 .show = ping_v4_seq_show,
1116 .start = ping_v4_seq_start,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001117 .next = ping_seq_next,
1118 .stop = ping_seq_stop,
1119};
1120
1121static int ping_seq_open(struct inode *inode, struct file *file)
1122{
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001123 struct ping_seq_afinfo *afinfo = PDE_DATA(inode);
1124 return seq_open_net(inode, file, &afinfo->seq_ops,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001125 sizeof(struct ping_iter_state));
1126}
1127
Lorenzo Colittid862e542013-05-31 15:05:50 +00001128const struct file_operations ping_seq_fops = {
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001129 .open = ping_seq_open,
1130 .read = seq_read,
1131 .llseek = seq_lseek,
1132 .release = seq_release_net,
1133};
Lorenzo Colittid862e542013-05-31 15:05:50 +00001134EXPORT_SYMBOL_GPL(ping_seq_fops);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001135
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001136static struct ping_seq_afinfo ping_v4_seq_afinfo = {
1137 .name = "icmp",
1138 .family = AF_INET,
1139 .seq_fops = &ping_seq_fops,
1140 .seq_ops = {
1141 .start = ping_v4_seq_start,
1142 .show = ping_v4_seq_show,
1143 .next = ping_seq_next,
1144 .stop = ping_seq_stop,
1145 },
1146};
1147
Lorenzo Colittid862e542013-05-31 15:05:50 +00001148int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001149{
1150 struct proc_dir_entry *p;
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001151 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
1152 afinfo->seq_fops, afinfo);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001153 if (!p)
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001154 return -ENOMEM;
1155 return 0;
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001156}
Lorenzo Colittid862e542013-05-31 15:05:50 +00001157EXPORT_SYMBOL_GPL(ping_proc_register);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001158
Lorenzo Colittid862e542013-05-31 15:05:50 +00001159void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001160{
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001161 remove_proc_entry(afinfo->name, net->proc_net);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001162}
Lorenzo Colittid862e542013-05-31 15:05:50 +00001163EXPORT_SYMBOL_GPL(ping_proc_unregister);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001164
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001165static int __net_init ping_v4_proc_init_net(struct net *net)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001166{
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001167 return ping_proc_register(net, &ping_v4_seq_afinfo);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001168}
1169
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001170static void __net_exit ping_v4_proc_exit_net(struct net *net)
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001171{
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001172 ping_proc_unregister(net, &ping_v4_seq_afinfo);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001173}
1174
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001175static struct pernet_operations ping_v4_net_ops = {
1176 .init = ping_v4_proc_init_net,
1177 .exit = ping_v4_proc_exit_net,
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001178};
1179
1180int __init ping_proc_init(void)
1181{
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001182 return register_pernet_subsys(&ping_v4_net_ops);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001183}
1184
1185void ping_proc_exit(void)
1186{
Lorenzo Colitti8cc785f2013-05-31 15:05:49 +00001187 unregister_pernet_subsys(&ping_v4_net_ops);
Vasiliy Kulikovc319b4d2011-05-13 10:01:00 +00001188}
1189
1190#endif
1191
1192void __init ping_init(void)
1193{
1194 int i;
1195
1196 for (i = 0; i < PING_HTABLE_SIZE; i++)
1197 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
1198 rwlock_init(&ping_table.lock);
1199}