]>
Commit | Line | Data |
---|---|---|
c319b4d7 VK |
1 | /* |
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 | ||
c319b4d7 | 23 | #include <linux/uaccess.h> |
c319b4d7 VK |
24 | #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> | |
c319b4d7 VK |
36 | #include <net/icmp.h> |
37 | #include <net/protocol.h> | |
38 | #include <linux/skbuff.h> | |
39 | #include <linux/proc_fs.h> | |
bc3b2d7f | 40 | #include <linux/export.h> |
c319b4d7 VK |
41 | #include <net/sock.h> |
42 | #include <net/ping.h> | |
c319b4d7 VK |
43 | #include <net/udp.h> |
44 | #include <net/route.h> | |
45 | #include <net/inet_common.h> | |
46 | #include <net/checksum.h> | |
47 | ||
6d0bfe22 LC |
48 | #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 | |
55 | ||
c319b4d7 | 56 | |
6d0bfe22 LC |
57 | struct ping_table ping_table; |
58 | struct pingv6_ops pingv6_ops; | |
59 | EXPORT_SYMBOL_GPL(pingv6_ops); | |
c319b4d7 | 60 | |
1b1cb1f7 | 61 | static u16 ping_port_rover; |
c319b4d7 | 62 | |
95c96174 | 63 | static inline int ping_hashfn(struct net *net, unsigned int num, unsigned int mask) |
c319b4d7 VK |
64 | { |
65 | int res = (num + net_hash_mix(net)) & mask; | |
95c96174 | 66 | |
c319b4d7 VK |
67 | pr_debug("hash(%d) = %d\n", num, res); |
68 | return res; | |
69 | } | |
6d0bfe22 | 70 | EXPORT_SYMBOL_GPL(ping_hash); |
c319b4d7 VK |
71 | |
72 | static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table, | |
95c96174 | 73 | struct net *net, unsigned int num) |
c319b4d7 VK |
74 | { |
75 | return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)]; | |
76 | } | |
77 | ||
6d0bfe22 | 78 | int ping_get_port(struct sock *sk, unsigned short ident) |
c319b4d7 VK |
79 | { |
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; | |
106 | next_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 | ||
6d0bfe22 LC |
116 | /* 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 | */ | |
c319b4d7 VK |
120 | 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 | ||
138 | fail: | |
139 | write_unlock_bh(&ping_table.lock); | |
140 | return 1; | |
141 | } | |
6d0bfe22 | 142 | EXPORT_SYMBOL_GPL(ping_get_port); |
c319b4d7 | 143 | |
6d0bfe22 | 144 | void ping_hash(struct sock *sk) |
c319b4d7 | 145 | { |
6d0bfe22 | 146 | pr_debug("ping_hash(sk->port=%u)\n", inet_sk(sk)->inet_num); |
c319b4d7 VK |
147 | BUG(); /* "Please do not press this button again." */ |
148 | } | |
149 | ||
6d0bfe22 | 150 | void ping_unhash(struct sock *sk) |
c319b4d7 VK |
151 | { |
152 | struct inet_sock *isk = inet_sk(sk); | |
6d0bfe22 | 153 | pr_debug("ping_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num); |
c319b4d7 | 154 | if (sk_hashed(sk)) { |
c319b4d7 VK |
155 | write_lock_bh(&ping_table.lock); |
156 | hlist_nulls_del(&sk->sk_nulls_node); | |
157 | sock_put(sk); | |
747465ef ED |
158 | isk->inet_num = 0; |
159 | isk->inet_sport = 0; | |
c319b4d7 VK |
160 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
161 | write_unlock_bh(&ping_table.lock); | |
162 | } | |
163 | } | |
6d0bfe22 | 164 | EXPORT_SYMBOL_GPL(ping_unhash); |
c319b4d7 | 165 | |
6d0bfe22 | 166 | static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident) |
c319b4d7 VK |
167 | { |
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; | |
6d0bfe22 LC |
172 | int dif = skb->dev->ifindex; |
173 | ||
174 | 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 | } | |
c319b4d7 | 183 | |
c319b4d7 VK |
184 | read_lock_bh(&ping_table.lock); |
185 | ||
186 | ping_portaddr_for_each_entry(sk, hnode, hslot) { | |
187 | isk = inet_sk(sk); | |
188 | ||
c319b4d7 VK |
189 | pr_debug("iterate\n"); |
190 | if (isk->inet_num != ident) | |
191 | continue; | |
6d0bfe22 LC |
192 | |
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) { | |
6d0bfe22 LC |
205 | |
206 | pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk, | |
207 | (int) isk->inet_num, | |
efe4208f | 208 | &sk->sk_v6_rcv_saddr, |
6d0bfe22 LC |
209 | sk->sk_bound_dev_if); |
210 | ||
efe4208f ED |
211 | if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) && |
212 | !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, | |
6d0bfe22 LC |
213 | &ipv6_hdr(skb)->daddr)) |
214 | continue; | |
215 | #endif | |
216 | } | |
217 | ||
c319b4d7 VK |
218 | 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; | |
226 | exit: | |
227 | read_unlock_bh(&ping_table.lock); | |
228 | ||
229 | return sk; | |
230 | } | |
231 | ||
7064d16e EB |
232 | static void inet_get_ping_group_range_net(struct net *net, kgid_t *low, |
233 | kgid_t *high) | |
f56e03e8 | 234 | { |
7064d16e | 235 | kgid_t *data = net->ipv4.sysctl_ping_group_range; |
95c96174 ED |
236 | unsigned int seq; |
237 | ||
f56e03e8 | 238 | do { |
0bbf87d8 | 239 | seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock); |
f56e03e8 VK |
240 | |
241 | *low = data[0]; | |
242 | *high = data[1]; | |
0bbf87d8 | 243 | } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq)); |
f56e03e8 VK |
244 | } |
245 | ||
246 | ||
6d0bfe22 | 247 | int ping_init_sock(struct sock *sk) |
c319b4d7 VK |
248 | { |
249 | struct net *net = sock_net(sk); | |
7064d16e | 250 | kgid_t group = current_egid(); |
c319b4d7 VK |
251 | struct group_info *group_info = get_current_groups(); |
252 | int i, j, count = group_info->ngroups; | |
ae2975bc | 253 | kgid_t low, high; |
c319b4d7 | 254 | |
7064d16e EB |
255 | inet_get_ping_group_range_net(net, &low, &high); |
256 | if (gid_lte(low, group) && gid_lte(group, high)) | |
c319b4d7 VK |
257 | return 0; |
258 | ||
259 | for (i = 0; i < group_info->nblocks; i++) { | |
260 | int cp_count = min_t(int, NGROUPS_PER_BLOCK, count); | |
c319b4d7 | 261 | for (j = 0; j < cp_count; j++) { |
ae2975bc EB |
262 | kgid_t gid = group_info->blocks[i][j]; |
263 | if (gid_lte(low, gid) && gid_lte(gid, high)) | |
c319b4d7 VK |
264 | return 0; |
265 | } | |
266 | ||
267 | count -= cp_count; | |
268 | } | |
269 | ||
270 | return -EACCES; | |
271 | } | |
6d0bfe22 | 272 | EXPORT_SYMBOL_GPL(ping_init_sock); |
c319b4d7 | 273 | |
6d0bfe22 | 274 | void ping_close(struct sock *sk, long timeout) |
c319b4d7 VK |
275 | { |
276 | pr_debug("ping_close(sk=%p,sk->num=%u)\n", | |
058bd4d2 | 277 | inet_sk(sk), inet_sk(sk)->inet_num); |
c319b4d7 VK |
278 | pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter); |
279 | ||
280 | sk_common_release(sk); | |
281 | } | |
6d0bfe22 LC |
282 | EXPORT_SYMBOL_GPL(ping_close); |
283 | ||
284 | /* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */ | |
a06a2d37 WF |
285 | static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk, |
286 | struct sockaddr *uaddr, int addr_len) { | |
6d0bfe22 LC |
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 | ||
a06a2d37 | 354 | static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr) |
6d0bfe22 LC |
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); | |
efe4208f | 364 | sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr; |
6d0bfe22 LC |
365 | #endif |
366 | } | |
367 | } | |
c319b4d7 | 368 | |
a06a2d37 | 369 | static void ping_clear_saddr(struct sock *sk, int dif) |
6d0bfe22 LC |
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); | |
efe4208f | 378 | memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr)); |
6d0bfe22 LC |
379 | memset(&np->saddr, 0, sizeof(np->saddr)); |
380 | #endif | |
381 | } | |
382 | } | |
c319b4d7 VK |
383 | /* |
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 | ||
6d0bfe22 | 388 | int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
c319b4d7 | 389 | { |
c319b4d7 VK |
390 | struct inet_sock *isk = inet_sk(sk); |
391 | unsigned short snum; | |
c319b4d7 | 392 | int err; |
6d0bfe22 | 393 | int dif = sk->sk_bound_dev_if; |
c319b4d7 | 394 | |
6d0bfe22 LC |
395 | err = ping_check_bind_addr(sk, isk, uaddr, addr_len); |
396 | if (err) | |
397 | return err; | |
c319b4d7 VK |
398 | |
399 | lock_sock(sk); | |
400 | ||
401 | err = -EINVAL; | |
402 | if (isk->inet_num != 0) | |
403 | goto out; | |
404 | ||
405 | err = -EADDRINUSE; | |
6d0bfe22 LC |
406 | 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); | |
c319b4d7 VK |
410 | goto out; |
411 | } | |
412 | ||
6d0bfe22 | 413 | pr_debug("after bind(): num = %d, dif = %d\n", |
058bd4d2 | 414 | (int)isk->inet_num, |
058bd4d2 | 415 | (int)sk->sk_bound_dev_if); |
c319b4d7 VK |
416 | |
417 | err = 0; | |
c2bb06db | 418 | if (sk->sk_family == AF_INET && isk->inet_rcv_saddr) |
c319b4d7 | 419 | sk->sk_userlocks |= SOCK_BINDADDR_LOCK; |
c2bb06db ED |
420 | #if IS_ENABLED(CONFIG_IPV6) |
421 | if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr)) | |
422 | sk->sk_userlocks |= SOCK_BINDADDR_LOCK; | |
423 | #endif | |
6d0bfe22 | 424 | |
c319b4d7 VK |
425 | if (snum) |
426 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; | |
427 | isk->inet_sport = htons(isk->inet_num); | |
428 | isk->inet_daddr = 0; | |
429 | isk->inet_dport = 0; | |
6d0bfe22 LC |
430 | |
431 | #if IS_ENABLED(CONFIG_IPV6) | |
432 | if (sk->sk_family == AF_INET6) | |
efe4208f | 433 | memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr)); |
6d0bfe22 LC |
434 | #endif |
435 | ||
c319b4d7 VK |
436 | sk_dst_reset(sk); |
437 | out: | |
438 | release_sock(sk); | |
439 | pr_debug("ping_v4_bind -> %d\n", err); | |
440 | return err; | |
441 | } | |
6d0bfe22 | 442 | EXPORT_SYMBOL_GPL(ping_bind); |
c319b4d7 VK |
443 | |
444 | /* | |
445 | * Is this a supported type of ICMP message? | |
446 | */ | |
447 | ||
6d0bfe22 | 448 | static inline int ping_supported(int family, int type, int code) |
c319b4d7 | 449 | { |
6d0bfe22 LC |
450 | return (family == AF_INET && type == ICMP_ECHO && code == 0) || |
451 | (family == AF_INET6 && type == ICMPV6_ECHO_REQUEST && code == 0); | |
c319b4d7 VK |
452 | } |
453 | ||
454 | /* | |
455 | * This routine is called by the ICMP module when it gets some | |
456 | * sort of error condition. | |
457 | */ | |
458 | ||
6d0bfe22 | 459 | void ping_err(struct sk_buff *skb, int offset, u32 info) |
c319b4d7 | 460 | { |
6d0bfe22 LC |
461 | int family; |
462 | struct icmphdr *icmph; | |
c319b4d7 | 463 | struct inet_sock *inet_sock; |
6d0bfe22 LC |
464 | int type; |
465 | int code; | |
c319b4d7 VK |
466 | struct net *net = dev_net(skb->dev); |
467 | struct sock *sk; | |
468 | int harderr; | |
469 | int err; | |
470 | ||
6d0bfe22 LC |
471 | if (skb->protocol == htons(ETH_P_IP)) { |
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 | ||
c319b4d7 VK |
485 | /* We assume the packet has already been checked by icmp_unreach */ |
486 | ||
6d0bfe22 | 487 | if (!ping_supported(family, icmph->type, icmph->code)) |
c319b4d7 VK |
488 | return; |
489 | ||
6d0bfe22 LC |
490 | 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)); | |
c319b4d7 | 493 | |
6d0bfe22 | 494 | sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id)); |
c319b4d7 | 495 | if (sk == NULL) { |
c319b4d7 VK |
496 | 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 | ||
6d0bfe22 LC |
505 | 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 | */ | |
515 | err = EREMOTEIO; | |
516 | break; | |
517 | case ICMP_PARAMETERPROB: | |
518 | err = EPROTO; | |
519 | harderr = 1; | |
520 | break; | |
521 | case ICMP_DEST_UNREACH: | |
522 | if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ | |
523 | ipv4_sk_update_pmtu(skb, sk, info); | |
524 | if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) { | |
525 | err = EMSGSIZE; | |
526 | harderr = 1; | |
527 | break; | |
528 | } | |
529 | goto out; | |
c319b4d7 | 530 | } |
6d0bfe22 LC |
531 | err = EHOSTUNREACH; |
532 | if (code <= NR_ICMP_UNREACH) { | |
533 | harderr = icmp_err_convert[code].fatal; | |
534 | err = icmp_err_convert[code].errno; | |
535 | } | |
536 | break; | |
537 | case ICMP_REDIRECT: | |
538 | /* See ICMP_SOURCE_QUENCH */ | |
539 | ipv4_sk_redirect(skb, sk); | |
540 | err = EREMOTEIO; | |
541 | break; | |
c319b4d7 | 542 | } |
6d0bfe22 LC |
543 | #if IS_ENABLED(CONFIG_IPV6) |
544 | } else if (skb->protocol == htons(ETH_P_IPV6)) { | |
545 | harderr = pingv6_ops.icmpv6_err_convert(type, code, &err); | |
546 | #endif | |
c319b4d7 VK |
547 | } |
548 | ||
549 | /* | |
550 | * RFC1122: OK. Passes ICMP errors back to application, as per | |
551 | * 4.1.3.3. | |
552 | */ | |
6d0bfe22 LC |
553 | if ((family == AF_INET && !inet_sock->recverr) || |
554 | (family == AF_INET6 && !inet6_sk(sk)->recverr)) { | |
c319b4d7 VK |
555 | if (!harderr || sk->sk_state != TCP_ESTABLISHED) |
556 | goto out; | |
557 | } else { | |
6d0bfe22 LC |
558 | if (family == AF_INET) { |
559 | ip_icmp_error(sk, skb, err, 0 /* no remote port */, | |
560 | info, (u8 *)icmph); | |
561 | #if IS_ENABLED(CONFIG_IPV6) | |
562 | } else if (family == AF_INET6) { | |
563 | pingv6_ops.ipv6_icmp_error(sk, skb, err, 0, | |
564 | info, (u8 *)icmph); | |
565 | #endif | |
566 | } | |
c319b4d7 VK |
567 | } |
568 | sk->sk_err = err; | |
569 | sk->sk_error_report(sk); | |
570 | out: | |
571 | sock_put(sk); | |
572 | } | |
6d0bfe22 | 573 | EXPORT_SYMBOL_GPL(ping_err); |
c319b4d7 VK |
574 | |
575 | /* | |
6d0bfe22 LC |
576 | * Copy and checksum an ICMP Echo packet from user space into a buffer |
577 | * starting from the payload. | |
c319b4d7 VK |
578 | */ |
579 | ||
6d0bfe22 LC |
580 | int ping_getfrag(void *from, char *to, |
581 | int offset, int fraglen, int odd, struct sk_buff *skb) | |
c319b4d7 VK |
582 | { |
583 | struct pingfakehdr *pfh = (struct pingfakehdr *)from; | |
584 | ||
585 | if (offset == 0) { | |
586 | if (fraglen < sizeof(struct icmphdr)) | |
587 | BUG(); | |
588 | if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr), | |
589 | pfh->iov, 0, fraglen - sizeof(struct icmphdr), | |
590 | &pfh->wcheck)) | |
591 | return -EFAULT; | |
6d0bfe22 LC |
592 | } else if (offset < sizeof(struct icmphdr)) { |
593 | BUG(); | |
594 | } else { | |
595 | if (csum_partial_copy_fromiovecend | |
596 | (to, pfh->iov, offset - sizeof(struct icmphdr), | |
597 | fraglen, &pfh->wcheck)) | |
598 | return -EFAULT; | |
599 | } | |
c319b4d7 | 600 | |
6d0bfe22 LC |
601 | #if IS_ENABLED(CONFIG_IPV6) |
602 | /* For IPv6, checksum each skb as we go along, as expected by | |
603 | * icmpv6_push_pending_frames. For IPv4, accumulate the checksum in | |
604 | * wcheck, it will be finalized in ping_v4_push_pending_frames. | |
605 | */ | |
606 | if (pfh->family == AF_INET6) { | |
607 | skb->csum = pfh->wcheck; | |
608 | skb->ip_summed = CHECKSUM_NONE; | |
609 | pfh->wcheck = 0; | |
c319b4d7 | 610 | } |
6d0bfe22 LC |
611 | #endif |
612 | ||
c319b4d7 VK |
613 | return 0; |
614 | } | |
6d0bfe22 | 615 | EXPORT_SYMBOL_GPL(ping_getfrag); |
c319b4d7 | 616 | |
6d0bfe22 LC |
617 | static int ping_v4_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh, |
618 | struct flowi4 *fl4) | |
c319b4d7 VK |
619 | { |
620 | struct sk_buff *skb = skb_peek(&sk->sk_write_queue); | |
621 | ||
622 | pfh->wcheck = csum_partial((char *)&pfh->icmph, | |
623 | sizeof(struct icmphdr), pfh->wcheck); | |
624 | pfh->icmph.checksum = csum_fold(pfh->wcheck); | |
625 | memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr)); | |
626 | skb->ip_summed = CHECKSUM_NONE; | |
627 | return ip_push_pending_frames(sk, fl4); | |
628 | } | |
629 | ||
6d0bfe22 LC |
630 | int ping_common_sendmsg(int family, struct msghdr *msg, size_t len, |
631 | void *user_icmph, size_t icmph_len) { | |
632 | u8 type, code; | |
c319b4d7 VK |
633 | |
634 | if (len > 0xFFFF) | |
635 | return -EMSGSIZE; | |
636 | ||
637 | /* | |
638 | * Check the flags. | |
639 | */ | |
640 | ||
641 | /* Mirror BSD error message compatibility */ | |
642 | if (msg->msg_flags & MSG_OOB) | |
643 | return -EOPNOTSUPP; | |
644 | ||
645 | /* | |
646 | * Fetch the ICMP header provided by the userland. | |
6d0bfe22 | 647 | * iovec is modified! The ICMP header is consumed. |
c319b4d7 | 648 | */ |
6d0bfe22 | 649 | if (memcpy_fromiovec(user_icmph, msg->msg_iov, icmph_len)) |
c319b4d7 | 650 | return -EFAULT; |
6d0bfe22 LC |
651 | |
652 | if (family == AF_INET) { | |
653 | type = ((struct icmphdr *) user_icmph)->type; | |
654 | code = ((struct icmphdr *) user_icmph)->code; | |
655 | #if IS_ENABLED(CONFIG_IPV6) | |
656 | } else if (family == AF_INET6) { | |
657 | type = ((struct icmp6hdr *) user_icmph)->icmp6_type; | |
658 | code = ((struct icmp6hdr *) user_icmph)->icmp6_code; | |
659 | #endif | |
660 | } else { | |
661 | BUG(); | |
662 | } | |
663 | ||
664 | if (!ping_supported(family, type, code)) | |
c319b4d7 VK |
665 | return -EINVAL; |
666 | ||
6d0bfe22 LC |
667 | return 0; |
668 | } | |
669 | EXPORT_SYMBOL_GPL(ping_common_sendmsg); | |
670 | ||
671 | int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |
672 | size_t len) | |
673 | { | |
674 | struct net *net = sock_net(sk); | |
675 | struct flowi4 fl4; | |
676 | struct inet_sock *inet = inet_sk(sk); | |
677 | struct ipcm_cookie ipc; | |
678 | struct icmphdr user_icmph; | |
679 | struct pingfakehdr pfh; | |
680 | struct rtable *rt = NULL; | |
681 | struct ip_options_data opt_copy; | |
682 | int free = 0; | |
683 | __be32 saddr, daddr, faddr; | |
684 | u8 tos; | |
685 | int err; | |
686 | ||
687 | pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num); | |
688 | ||
689 | err = ping_common_sendmsg(AF_INET, msg, len, &user_icmph, | |
690 | sizeof(user_icmph)); | |
691 | if (err) | |
692 | return err; | |
693 | ||
c319b4d7 VK |
694 | /* |
695 | * Get and verify the address. | |
696 | */ | |
697 | ||
698 | if (msg->msg_name) { | |
699 | struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name; | |
700 | if (msg->msg_namelen < sizeof(*usin)) | |
701 | return -EINVAL; | |
702 | if (usin->sin_family != AF_INET) | |
703 | return -EINVAL; | |
704 | daddr = usin->sin_addr.s_addr; | |
705 | /* no remote port */ | |
706 | } else { | |
707 | if (sk->sk_state != TCP_ESTABLISHED) | |
708 | return -EDESTADDRREQ; | |
709 | daddr = inet->inet_daddr; | |
710 | /* no remote port */ | |
711 | } | |
712 | ||
713 | ipc.addr = inet->inet_saddr; | |
714 | ipc.opt = NULL; | |
715 | ipc.oif = sk->sk_bound_dev_if; | |
716 | ipc.tx_flags = 0; | |
aa661581 FF |
717 | ipc.ttl = 0; |
718 | ipc.tos = -1; | |
bf84a010 DB |
719 | |
720 | sock_tx_timestamp(sk, &ipc.tx_flags); | |
c319b4d7 VK |
721 | |
722 | if (msg->msg_controllen) { | |
723 | err = ip_cmsg_send(sock_net(sk), msg, &ipc); | |
724 | if (err) | |
725 | return err; | |
726 | if (ipc.opt) | |
727 | free = 1; | |
728 | } | |
729 | if (!ipc.opt) { | |
730 | struct ip_options_rcu *inet_opt; | |
731 | ||
732 | rcu_read_lock(); | |
733 | inet_opt = rcu_dereference(inet->inet_opt); | |
734 | if (inet_opt) { | |
735 | memcpy(&opt_copy, inet_opt, | |
736 | sizeof(*inet_opt) + inet_opt->opt.optlen); | |
737 | ipc.opt = &opt_copy.opt; | |
738 | } | |
739 | rcu_read_unlock(); | |
740 | } | |
741 | ||
742 | saddr = ipc.addr; | |
743 | ipc.addr = faddr = daddr; | |
744 | ||
745 | if (ipc.opt && ipc.opt->opt.srr) { | |
746 | if (!daddr) | |
747 | return -EINVAL; | |
748 | faddr = ipc.opt->opt.faddr; | |
749 | } | |
aa661581 | 750 | tos = get_rttos(&ipc, inet); |
c319b4d7 VK |
751 | if (sock_flag(sk, SOCK_LOCALROUTE) || |
752 | (msg->msg_flags & MSG_DONTROUTE) || | |
753 | (ipc.opt && ipc.opt->opt.is_strictroute)) { | |
754 | tos |= RTO_ONLINK; | |
755 | } | |
756 | ||
757 | if (ipv4_is_multicast(daddr)) { | |
758 | if (!ipc.oif) | |
759 | ipc.oif = inet->mc_index; | |
760 | if (!saddr) | |
761 | saddr = inet->mc_addr; | |
76e21053 EH |
762 | } else if (!ipc.oif) |
763 | ipc.oif = inet->uc_index; | |
c319b4d7 VK |
764 | |
765 | flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos, | |
766 | RT_SCOPE_UNIVERSE, sk->sk_protocol, | |
767 | inet_sk_flowi_flags(sk), faddr, saddr, 0, 0); | |
768 | ||
769 | security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); | |
770 | rt = ip_route_output_flow(net, &fl4, sk); | |
771 | if (IS_ERR(rt)) { | |
772 | err = PTR_ERR(rt); | |
773 | rt = NULL; | |
774 | if (err == -ENETUNREACH) | |
775 | IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); | |
776 | goto out; | |
777 | } | |
778 | ||
779 | err = -EACCES; | |
780 | if ((rt->rt_flags & RTCF_BROADCAST) && | |
781 | !sock_flag(sk, SOCK_BROADCAST)) | |
782 | goto out; | |
783 | ||
784 | if (msg->msg_flags & MSG_CONFIRM) | |
785 | goto do_confirm; | |
786 | back_from_confirm: | |
787 | ||
788 | if (!ipc.addr) | |
789 | ipc.addr = fl4.daddr; | |
790 | ||
791 | lock_sock(sk); | |
792 | ||
793 | pfh.icmph.type = user_icmph.type; /* already checked */ | |
794 | pfh.icmph.code = user_icmph.code; /* ditto */ | |
795 | pfh.icmph.checksum = 0; | |
796 | pfh.icmph.un.echo.id = inet->inet_sport; | |
797 | pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence; | |
798 | pfh.iov = msg->msg_iov; | |
799 | pfh.wcheck = 0; | |
6d0bfe22 | 800 | pfh.family = AF_INET; |
c319b4d7 VK |
801 | |
802 | err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len, | |
803 | 0, &ipc, &rt, msg->msg_flags); | |
804 | if (err) | |
805 | ip_flush_pending_frames(sk); | |
806 | else | |
6d0bfe22 | 807 | err = ping_v4_push_pending_frames(sk, &pfh, &fl4); |
c319b4d7 VK |
808 | release_sock(sk); |
809 | ||
810 | out: | |
811 | ip_rt_put(rt); | |
812 | if (free) | |
813 | kfree(ipc.opt); | |
814 | if (!err) { | |
815 | icmp_out_count(sock_net(sk), user_icmph.type); | |
816 | return len; | |
817 | } | |
818 | return err; | |
819 | ||
820 | do_confirm: | |
821 | dst_confirm(&rt->dst); | |
822 | if (!(msg->msg_flags & MSG_PROBE) || len) | |
823 | goto back_from_confirm; | |
824 | err = 0; | |
825 | goto out; | |
826 | } | |
827 | ||
6d0bfe22 LC |
828 | int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, |
829 | size_t len, int noblock, int flags, int *addr_len) | |
c319b4d7 VK |
830 | { |
831 | struct inet_sock *isk = inet_sk(sk); | |
6d0bfe22 | 832 | int family = sk->sk_family; |
c319b4d7 VK |
833 | 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 | ||
a5e7424d | 838 | err = -EOPNOTSUPP; |
c319b4d7 VK |
839 | if (flags & MSG_OOB) |
840 | goto out; | |
841 | ||
6d0bfe22 LC |
842 | if (flags & MSG_ERRQUEUE) { |
843 | if (family == AF_INET) { | |
844 | return ip_recv_error(sk, msg, len); | |
845 | #if IS_ENABLED(CONFIG_IPV6) | |
846 | } else if (family == AF_INET6) { | |
847 | return pingv6_ops.ipv6_recv_error(sk, msg, len); | |
848 | #endif | |
849 | } | |
850 | } | |
c319b4d7 VK |
851 | |
852 | skb = skb_recv_datagram(sk, flags, noblock, &err); | |
853 | if (!skb) | |
854 | goto out; | |
855 | ||
856 | copied = skb->len; | |
857 | if (copied > len) { | |
858 | msg->msg_flags |= MSG_TRUNC; | |
859 | copied = len; | |
860 | } | |
861 | ||
862 | /* Don't bother checking the checksum */ | |
863 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | |
864 | if (err) | |
865 | goto done; | |
866 | ||
867 | sock_recv_timestamp(msg, sk, skb); | |
868 | ||
6d0bfe22 LC |
869 | /* Copy the address and add cmsg data. */ |
870 | if (family == AF_INET) { | |
bceaa902 HFS |
871 | struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; |
872 | ||
cf970c00 HFS |
873 | if (sin) { |
874 | sin->sin_family = AF_INET; | |
875 | sin->sin_port = 0 /* skb->h.uh->source */; | |
876 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; | |
877 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
878 | *addr_len = sizeof(*sin); | |
879 | } | |
6d0bfe22 LC |
880 | |
881 | if (isk->cmsg_flags) | |
882 | ip_cmsg_recv(msg, skb); | |
883 | ||
884 | #if IS_ENABLED(CONFIG_IPV6) | |
885 | } else if (family == AF_INET6) { | |
886 | struct ipv6_pinfo *np = inet6_sk(sk); | |
887 | struct ipv6hdr *ip6 = ipv6_hdr(skb); | |
bceaa902 HFS |
888 | struct sockaddr_in6 *sin6 = |
889 | (struct sockaddr_in6 *)msg->msg_name; | |
890 | ||
cf970c00 HFS |
891 | if (sin6) { |
892 | sin6->sin6_family = AF_INET6; | |
893 | sin6->sin6_port = 0; | |
894 | sin6->sin6_addr = ip6->saddr; | |
895 | sin6->sin6_flowinfo = 0; | |
896 | if (np->sndflow) | |
897 | sin6->sin6_flowinfo = ip6_flowinfo(ip6); | |
898 | sin6->sin6_scope_id = | |
899 | ipv6_iface_scope_id(&sin6->sin6_addr, | |
900 | IP6CB(skb)->iif); | |
901 | *addr_len = sizeof(*sin6); | |
902 | } | |
6d0bfe22 LC |
903 | |
904 | if (inet6_sk(sk)->rxopt.all) | |
905 | pingv6_ops.ip6_datagram_recv_ctl(sk, msg, skb); | |
906 | #endif | |
907 | } else { | |
908 | BUG(); | |
c319b4d7 | 909 | } |
6d0bfe22 | 910 | |
c319b4d7 VK |
911 | err = copied; |
912 | ||
913 | done: | |
914 | skb_free_datagram(sk, skb); | |
915 | out: | |
916 | pr_debug("ping_recvmsg -> %d\n", err); | |
917 | return err; | |
918 | } | |
6d0bfe22 | 919 | EXPORT_SYMBOL_GPL(ping_recvmsg); |
c319b4d7 | 920 | |
6d0bfe22 | 921 | int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
c319b4d7 VK |
922 | { |
923 | pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n", | |
058bd4d2 | 924 | inet_sk(sk), inet_sk(sk)->inet_num, skb); |
c319b4d7 | 925 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
c319b4d7 VK |
926 | kfree_skb(skb); |
927 | pr_debug("ping_queue_rcv_skb -> failed\n"); | |
928 | return -1; | |
929 | } | |
930 | return 0; | |
931 | } | |
6d0bfe22 | 932 | EXPORT_SYMBOL_GPL(ping_queue_rcv_skb); |
c319b4d7 VK |
933 | |
934 | ||
935 | /* | |
936 | * All we need to do is get the socket. | |
937 | */ | |
938 | ||
939 | void ping_rcv(struct sk_buff *skb) | |
940 | { | |
941 | struct sock *sk; | |
942 | struct net *net = dev_net(skb->dev); | |
c319b4d7 | 943 | struct icmphdr *icmph = icmp_hdr(skb); |
c319b4d7 VK |
944 | |
945 | /* We assume the packet has already been checked by icmp_rcv */ | |
946 | ||
947 | pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n", | |
058bd4d2 | 948 | skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence)); |
c319b4d7 VK |
949 | |
950 | /* Push ICMP header back */ | |
951 | skb_push(skb, skb->data - (u8 *)icmph); | |
952 | ||
6d0bfe22 | 953 | sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id)); |
c319b4d7 VK |
954 | if (sk != NULL) { |
955 | pr_debug("rcv on socket %p\n", sk); | |
956 | ping_queue_rcv_skb(sk, skb_get(skb)); | |
957 | sock_put(sk); | |
958 | return; | |
959 | } | |
960 | pr_debug("no socket, dropping\n"); | |
961 | ||
962 | /* We're called from icmp_rcv(). kfree_skb() is done there. */ | |
963 | } | |
6d0bfe22 | 964 | EXPORT_SYMBOL_GPL(ping_rcv); |
c319b4d7 VK |
965 | |
966 | struct proto ping_prot = { | |
967 | .name = "PING", | |
968 | .owner = THIS_MODULE, | |
969 | .init = ping_init_sock, | |
970 | .close = ping_close, | |
971 | .connect = ip4_datagram_connect, | |
972 | .disconnect = udp_disconnect, | |
c319b4d7 VK |
973 | .setsockopt = ip_setsockopt, |
974 | .getsockopt = ip_getsockopt, | |
6d0bfe22 | 975 | .sendmsg = ping_v4_sendmsg, |
c319b4d7 VK |
976 | .recvmsg = ping_recvmsg, |
977 | .bind = ping_bind, | |
978 | .backlog_rcv = ping_queue_rcv_skb, | |
8141ed9f | 979 | .release_cb = ip4_datagram_release_cb, |
6d0bfe22 LC |
980 | .hash = ping_hash, |
981 | .unhash = ping_unhash, | |
982 | .get_port = ping_get_port, | |
c319b4d7 VK |
983 | .obj_size = sizeof(struct inet_sock), |
984 | }; | |
985 | EXPORT_SYMBOL(ping_prot); | |
986 | ||
987 | #ifdef CONFIG_PROC_FS | |
988 | ||
989 | static struct sock *ping_get_first(struct seq_file *seq, int start) | |
990 | { | |
991 | struct sock *sk; | |
992 | struct ping_iter_state *state = seq->private; | |
993 | struct net *net = seq_file_net(seq); | |
994 | ||
995 | for (state->bucket = start; state->bucket < PING_HTABLE_SIZE; | |
996 | ++state->bucket) { | |
997 | struct hlist_nulls_node *node; | |
75e308c8 CG |
998 | struct hlist_nulls_head *hslot; |
999 | ||
1000 | hslot = &ping_table.hash[state->bucket]; | |
c319b4d7 VK |
1001 | |
1002 | if (hlist_nulls_empty(hslot)) | |
1003 | continue; | |
1004 | ||
1005 | sk_nulls_for_each(sk, node, hslot) { | |
8cc785f6 LC |
1006 | if (net_eq(sock_net(sk), net) && |
1007 | sk->sk_family == state->family) | |
c319b4d7 VK |
1008 | goto found; |
1009 | } | |
1010 | } | |
1011 | sk = NULL; | |
1012 | found: | |
1013 | return sk; | |
1014 | } | |
1015 | ||
1016 | static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk) | |
1017 | { | |
1018 | struct ping_iter_state *state = seq->private; | |
1019 | struct net *net = seq_file_net(seq); | |
1020 | ||
1021 | do { | |
1022 | sk = sk_nulls_next(sk); | |
1023 | } while (sk && (!net_eq(sock_net(sk), net))); | |
1024 | ||
1025 | if (!sk) | |
1026 | return ping_get_first(seq, state->bucket + 1); | |
1027 | return sk; | |
1028 | } | |
1029 | ||
1030 | static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos) | |
1031 | { | |
1032 | struct sock *sk = ping_get_first(seq, 0); | |
1033 | ||
1034 | if (sk) | |
1035 | while (pos && (sk = ping_get_next(seq, sk)) != NULL) | |
1036 | --pos; | |
1037 | return pos ? NULL : sk; | |
1038 | } | |
1039 | ||
d862e546 | 1040 | void *ping_seq_start(struct seq_file *seq, loff_t *pos, sa_family_t family) |
c319b4d7 VK |
1041 | { |
1042 | struct ping_iter_state *state = seq->private; | |
1043 | state->bucket = 0; | |
8cc785f6 | 1044 | state->family = family; |
c319b4d7 VK |
1045 | |
1046 | read_lock_bh(&ping_table.lock); | |
1047 | ||
1048 | return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN; | |
1049 | } | |
d862e546 | 1050 | EXPORT_SYMBOL_GPL(ping_seq_start); |
c319b4d7 | 1051 | |
8cc785f6 LC |
1052 | static void *ping_v4_seq_start(struct seq_file *seq, loff_t *pos) |
1053 | { | |
1054 | return ping_seq_start(seq, pos, AF_INET); | |
1055 | } | |
1056 | ||
d862e546 | 1057 | void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
c319b4d7 VK |
1058 | { |
1059 | struct sock *sk; | |
1060 | ||
1061 | if (v == SEQ_START_TOKEN) | |
1062 | sk = ping_get_idx(seq, 0); | |
1063 | else | |
1064 | sk = ping_get_next(seq, v); | |
1065 | ||
1066 | ++*pos; | |
1067 | return sk; | |
1068 | } | |
d862e546 | 1069 | EXPORT_SYMBOL_GPL(ping_seq_next); |
c319b4d7 | 1070 | |
d862e546 | 1071 | void ping_seq_stop(struct seq_file *seq, void *v) |
c319b4d7 VK |
1072 | { |
1073 | read_unlock_bh(&ping_table.lock); | |
1074 | } | |
d862e546 | 1075 | EXPORT_SYMBOL_GPL(ping_seq_stop); |
c319b4d7 | 1076 | |
8cc785f6 | 1077 | static void ping_v4_format_sock(struct sock *sp, struct seq_file *f, |
652586df | 1078 | int bucket) |
c319b4d7 VK |
1079 | { |
1080 | struct inet_sock *inet = inet_sk(sp); | |
1081 | __be32 dest = inet->inet_daddr; | |
1082 | __be32 src = inet->inet_rcv_saddr; | |
1083 | __u16 destp = ntohs(inet->inet_dport); | |
1084 | __u16 srcp = ntohs(inet->inet_sport); | |
1085 | ||
1086 | seq_printf(f, "%5d: %08X:%04X %08X:%04X" | |
652586df | 1087 | " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d", |
c319b4d7 VK |
1088 | bucket, src, srcp, dest, destp, sp->sk_state, |
1089 | sk_wmem_alloc_get(sp), | |
1090 | sk_rmem_alloc_get(sp), | |
a7cb5a49 EB |
1091 | 0, 0L, 0, |
1092 | from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), | |
1093 | 0, sock_i_ino(sp), | |
c319b4d7 | 1094 | atomic_read(&sp->sk_refcnt), sp, |
652586df | 1095 | atomic_read(&sp->sk_drops)); |
c319b4d7 VK |
1096 | } |
1097 | ||
8cc785f6 | 1098 | static int ping_v4_seq_show(struct seq_file *seq, void *v) |
c319b4d7 | 1099 | { |
652586df | 1100 | seq_setwidth(seq, 127); |
c319b4d7 | 1101 | if (v == SEQ_START_TOKEN) |
652586df | 1102 | seq_puts(seq, " sl local_address rem_address st tx_queue " |
c319b4d7 VK |
1103 | "rx_queue tr tm->when retrnsmt uid timeout " |
1104 | "inode ref pointer drops"); | |
1105 | else { | |
1106 | struct ping_iter_state *state = seq->private; | |
c319b4d7 | 1107 | |
652586df | 1108 | ping_v4_format_sock(v, seq, state->bucket); |
c319b4d7 | 1109 | } |
652586df | 1110 | seq_pad(seq, '\n'); |
c319b4d7 VK |
1111 | return 0; |
1112 | } | |
1113 | ||
8cc785f6 LC |
1114 | static const struct seq_operations ping_v4_seq_ops = { |
1115 | .show = ping_v4_seq_show, | |
1116 | .start = ping_v4_seq_start, | |
c319b4d7 VK |
1117 | .next = ping_seq_next, |
1118 | .stop = ping_seq_stop, | |
1119 | }; | |
1120 | ||
1121 | static int ping_seq_open(struct inode *inode, struct file *file) | |
1122 | { | |
8cc785f6 LC |
1123 | struct ping_seq_afinfo *afinfo = PDE_DATA(inode); |
1124 | return seq_open_net(inode, file, &afinfo->seq_ops, | |
c319b4d7 VK |
1125 | sizeof(struct ping_iter_state)); |
1126 | } | |
1127 | ||
d862e546 | 1128 | const struct file_operations ping_seq_fops = { |
c319b4d7 VK |
1129 | .open = ping_seq_open, |
1130 | .read = seq_read, | |
1131 | .llseek = seq_lseek, | |
1132 | .release = seq_release_net, | |
1133 | }; | |
d862e546 | 1134 | EXPORT_SYMBOL_GPL(ping_seq_fops); |
c319b4d7 | 1135 | |
8cc785f6 LC |
1136 | static 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 | ||
d862e546 | 1148 | int ping_proc_register(struct net *net, struct ping_seq_afinfo *afinfo) |
c319b4d7 VK |
1149 | { |
1150 | struct proc_dir_entry *p; | |
8cc785f6 LC |
1151 | p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, |
1152 | afinfo->seq_fops, afinfo); | |
c319b4d7 | 1153 | if (!p) |
8cc785f6 LC |
1154 | return -ENOMEM; |
1155 | return 0; | |
c319b4d7 | 1156 | } |
d862e546 | 1157 | EXPORT_SYMBOL_GPL(ping_proc_register); |
c319b4d7 | 1158 | |
d862e546 | 1159 | void ping_proc_unregister(struct net *net, struct ping_seq_afinfo *afinfo) |
c319b4d7 | 1160 | { |
8cc785f6 | 1161 | remove_proc_entry(afinfo->name, net->proc_net); |
c319b4d7 | 1162 | } |
d862e546 | 1163 | EXPORT_SYMBOL_GPL(ping_proc_unregister); |
c319b4d7 | 1164 | |
8cc785f6 | 1165 | static int __net_init ping_v4_proc_init_net(struct net *net) |
c319b4d7 | 1166 | { |
8cc785f6 | 1167 | return ping_proc_register(net, &ping_v4_seq_afinfo); |
c319b4d7 VK |
1168 | } |
1169 | ||
8cc785f6 | 1170 | static void __net_exit ping_v4_proc_exit_net(struct net *net) |
c319b4d7 | 1171 | { |
8cc785f6 | 1172 | ping_proc_unregister(net, &ping_v4_seq_afinfo); |
c319b4d7 VK |
1173 | } |
1174 | ||
8cc785f6 LC |
1175 | static struct pernet_operations ping_v4_net_ops = { |
1176 | .init = ping_v4_proc_init_net, | |
1177 | .exit = ping_v4_proc_exit_net, | |
c319b4d7 VK |
1178 | }; |
1179 | ||
1180 | int __init ping_proc_init(void) | |
1181 | { | |
8cc785f6 | 1182 | return register_pernet_subsys(&ping_v4_net_ops); |
c319b4d7 VK |
1183 | } |
1184 | ||
1185 | void ping_proc_exit(void) | |
1186 | { | |
8cc785f6 | 1187 | unregister_pernet_subsys(&ping_v4_net_ops); |
c319b4d7 VK |
1188 | } |
1189 | ||
1190 | #endif | |
1191 | ||
1192 | void __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 | } |