net: ping: make local functions static
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / ping.c
CommitLineData
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
23#include <asm/system.h>
24#include <linux/uaccess.h>
c319b4d7
VK
25#include <linux/types.h>
26#include <linux/fcntl.h>
27#include <linux/socket.h>
28#include <linux/sockios.h>
29#include <linux/in.h>
30#include <linux/errno.h>
31#include <linux/timer.h>
32#include <linux/mm.h>
33#include <linux/inet.h>
34#include <linux/netdevice.h>
35#include <net/snmp.h>
36#include <net/ip.h>
37#include <net/ipv6.h>
38#include <net/icmp.h>
39#include <net/protocol.h>
40#include <linux/skbuff.h>
41#include <linux/proc_fs.h>
42#include <net/sock.h>
43#include <net/ping.h>
44#include <net/icmp.h>
45#include <net/udp.h>
46#include <net/route.h>
47#include <net/inet_common.h>
48#include <net/checksum.h>
49
50
1b1cb1f7 51static struct ping_table ping_table;
c319b4d7 52
1b1cb1f7 53static u16 ping_port_rover;
c319b4d7
VK
54
55static inline int ping_hashfn(struct net *net, unsigned num, unsigned mask)
56{
57 int res = (num + net_hash_mix(net)) & mask;
58 pr_debug("hash(%d) = %d\n", num, res);
59 return res;
60}
61
62static inline struct hlist_nulls_head *ping_hashslot(struct ping_table *table,
63 struct net *net, unsigned num)
64{
65 return &table->hash[ping_hashfn(net, num, PING_HTABLE_MASK)];
66}
67
68static int ping_v4_get_port(struct sock *sk, unsigned short ident)
69{
70 struct hlist_nulls_node *node;
71 struct hlist_nulls_head *hlist;
72 struct inet_sock *isk, *isk2;
73 struct sock *sk2 = NULL;
74
75 isk = inet_sk(sk);
76 write_lock_bh(&ping_table.lock);
77 if (ident == 0) {
78 u32 i;
79 u16 result = ping_port_rover + 1;
80
81 for (i = 0; i < (1L << 16); i++, result++) {
82 if (!result)
83 result++; /* avoid zero */
84 hlist = ping_hashslot(&ping_table, sock_net(sk),
85 result);
86 ping_portaddr_for_each_entry(sk2, node, hlist) {
87 isk2 = inet_sk(sk2);
88
89 if (isk2->inet_num == result)
90 goto next_port;
91 }
92
93 /* found */
94 ping_port_rover = ident = result;
95 break;
96next_port:
97 ;
98 }
99 if (i >= (1L << 16))
100 goto fail;
101 } else {
102 hlist = ping_hashslot(&ping_table, sock_net(sk), ident);
103 ping_portaddr_for_each_entry(sk2, node, hlist) {
104 isk2 = inet_sk(sk2);
105
106 if ((isk2->inet_num == ident) &&
107 (sk2 != sk) &&
108 (!sk2->sk_reuse || !sk->sk_reuse))
109 goto fail;
110 }
111 }
112
113 pr_debug("found port/ident = %d\n", ident);
114 isk->inet_num = ident;
115 if (sk_unhashed(sk)) {
116 pr_debug("was not hashed\n");
117 sock_hold(sk);
118 hlist_nulls_add_head(&sk->sk_nulls_node, hlist);
119 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
120 }
121 write_unlock_bh(&ping_table.lock);
122 return 0;
123
124fail:
125 write_unlock_bh(&ping_table.lock);
126 return 1;
127}
128
129static void ping_v4_hash(struct sock *sk)
130{
131 pr_debug("ping_v4_hash(sk->port=%u)\n", inet_sk(sk)->inet_num);
132 BUG(); /* "Please do not press this button again." */
133}
134
135static void ping_v4_unhash(struct sock *sk)
136{
137 struct inet_sock *isk = inet_sk(sk);
138 pr_debug("ping_v4_unhash(isk=%p,isk->num=%u)\n", isk, isk->inet_num);
139 if (sk_hashed(sk)) {
140 struct hlist_nulls_head *hslot;
141
142 hslot = ping_hashslot(&ping_table, sock_net(sk), isk->inet_num);
143 write_lock_bh(&ping_table.lock);
144 hlist_nulls_del(&sk->sk_nulls_node);
145 sock_put(sk);
146 isk->inet_num = isk->inet_sport = 0;
147 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
148 write_unlock_bh(&ping_table.lock);
149 }
150}
151
1b1cb1f7
ED
152static struct sock *ping_v4_lookup(struct net *net, u32 saddr, u32 daddr,
153 u16 ident, int dif)
c319b4d7
VK
154{
155 struct hlist_nulls_head *hslot = ping_hashslot(&ping_table, net, ident);
156 struct sock *sk = NULL;
157 struct inet_sock *isk;
158 struct hlist_nulls_node *hnode;
159
160 pr_debug("try to find: num = %d, daddr = %ld, dif = %d\n",
161 (int)ident, (unsigned long)daddr, dif);
162 read_lock_bh(&ping_table.lock);
163
164 ping_portaddr_for_each_entry(sk, hnode, hslot) {
165 isk = inet_sk(sk);
166
167 pr_debug("found: %p: num = %d, daddr = %ld, dif = %d\n", sk,
168 (int)isk->inet_num, (unsigned long)isk->inet_rcv_saddr,
169 sk->sk_bound_dev_if);
170
171 pr_debug("iterate\n");
172 if (isk->inet_num != ident)
173 continue;
174 if (isk->inet_rcv_saddr && isk->inet_rcv_saddr != daddr)
175 continue;
176 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
177 continue;
178
179 sock_hold(sk);
180 goto exit;
181 }
182
183 sk = NULL;
184exit:
185 read_unlock_bh(&ping_table.lock);
186
187 return sk;
188}
189
f56e03e8
VK
190static void inet_get_ping_group_range_net(struct net *net, gid_t *low, gid_t *high)
191{
192 gid_t *data = net->ipv4.sysctl_ping_group_range;
193 unsigned seq;
194 do {
195 seq = read_seqbegin(&sysctl_local_ports.lock);
196
197 *low = data[0];
198 *high = data[1];
199 } while (read_seqretry(&sysctl_local_ports.lock, seq));
200}
201
202
c319b4d7
VK
203static int ping_init_sock(struct sock *sk)
204{
205 struct net *net = sock_net(sk);
206 gid_t group = current_egid();
207 gid_t range[2];
208 struct group_info *group_info = get_current_groups();
209 int i, j, count = group_info->ngroups;
210
211 inet_get_ping_group_range_net(net, range, range+1);
212 if (range[0] <= group && group <= range[1])
213 return 0;
214
215 for (i = 0; i < group_info->nblocks; i++) {
216 int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
217
218 for (j = 0; j < cp_count; j++) {
219 group = group_info->blocks[i][j];
220 if (range[0] <= group && group <= range[1])
221 return 0;
222 }
223
224 count -= cp_count;
225 }
226
227 return -EACCES;
228}
229
230static void ping_close(struct sock *sk, long timeout)
231{
232 pr_debug("ping_close(sk=%p,sk->num=%u)\n",
233 inet_sk(sk), inet_sk(sk)->inet_num);
234 pr_debug("isk->refcnt = %d\n", sk->sk_refcnt.counter);
235
236 sk_common_release(sk);
237}
238
239/*
240 * We need our own bind because there are no privileged id's == local ports.
241 * Moreover, we don't allow binding to multi- and broadcast addresses.
242 */
243
244static int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
245{
246 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
247 struct inet_sock *isk = inet_sk(sk);
248 unsigned short snum;
249 int chk_addr_ret;
250 int err;
251
252 if (addr_len < sizeof(struct sockaddr_in))
253 return -EINVAL;
254
255 pr_debug("ping_v4_bind(sk=%p,sa_addr=%08x,sa_port=%d)\n",
256 sk, addr->sin_addr.s_addr, ntohs(addr->sin_port));
257
258 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
259 if (addr->sin_addr.s_addr == INADDR_ANY)
260 chk_addr_ret = RTN_LOCAL;
261
262 if ((sysctl_ip_nonlocal_bind == 0 &&
263 isk->freebind == 0 && isk->transparent == 0 &&
264 chk_addr_ret != RTN_LOCAL) ||
265 chk_addr_ret == RTN_MULTICAST ||
266 chk_addr_ret == RTN_BROADCAST)
267 return -EADDRNOTAVAIL;
268
269 lock_sock(sk);
270
271 err = -EINVAL;
272 if (isk->inet_num != 0)
273 goto out;
274
275 err = -EADDRINUSE;
276 isk->inet_rcv_saddr = isk->inet_saddr = addr->sin_addr.s_addr;
277 snum = ntohs(addr->sin_port);
278 if (ping_v4_get_port(sk, snum) != 0) {
279 isk->inet_saddr = isk->inet_rcv_saddr = 0;
280 goto out;
281 }
282
283 pr_debug("after bind(): num = %d, daddr = %ld, dif = %d\n",
284 (int)isk->inet_num,
285 (unsigned long) isk->inet_rcv_saddr,
286 (int)sk->sk_bound_dev_if);
287
288 err = 0;
289 if (isk->inet_rcv_saddr)
290 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
291 if (snum)
292 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
293 isk->inet_sport = htons(isk->inet_num);
294 isk->inet_daddr = 0;
295 isk->inet_dport = 0;
296 sk_dst_reset(sk);
297out:
298 release_sock(sk);
299 pr_debug("ping_v4_bind -> %d\n", err);
300 return err;
301}
302
303/*
304 * Is this a supported type of ICMP message?
305 */
306
307static inline int ping_supported(int type, int code)
308{
309 if (type == ICMP_ECHO && code == 0)
310 return 1;
311 return 0;
312}
313
314/*
315 * This routine is called by the ICMP module when it gets some
316 * sort of error condition.
317 */
318
319static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
320
321void ping_err(struct sk_buff *skb, u32 info)
322{
323 struct iphdr *iph = (struct iphdr *)skb->data;
324 struct icmphdr *icmph = (struct icmphdr *)(skb->data+(iph->ihl<<2));
325 struct inet_sock *inet_sock;
326 int type = icmph->type;
327 int code = icmph->code;
328 struct net *net = dev_net(skb->dev);
329 struct sock *sk;
330 int harderr;
331 int err;
332
333 /* We assume the packet has already been checked by icmp_unreach */
334
335 if (!ping_supported(icmph->type, icmph->code))
336 return;
337
338 pr_debug("ping_err(type=%04x,code=%04x,id=%04x,seq=%04x)\n", type,
339 code, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
340
341 sk = ping_v4_lookup(net, iph->daddr, iph->saddr,
342 ntohs(icmph->un.echo.id), skb->dev->ifindex);
343 if (sk == NULL) {
344 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
345 pr_debug("no socket, dropping\n");
346 return; /* No socket for error */
347 }
348 pr_debug("err on socket %p\n", sk);
349
350 err = 0;
351 harderr = 0;
352 inet_sock = inet_sk(sk);
353
354 switch (type) {
355 default:
356 case ICMP_TIME_EXCEEDED:
357 err = EHOSTUNREACH;
358 break;
359 case ICMP_SOURCE_QUENCH:
360 /* This is not a real error but ping wants to see it.
361 * Report it with some fake errno. */
362 err = EREMOTEIO;
363 break;
364 case ICMP_PARAMETERPROB:
365 err = EPROTO;
366 harderr = 1;
367 break;
368 case ICMP_DEST_UNREACH:
369 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
370 if (inet_sock->pmtudisc != IP_PMTUDISC_DONT) {
371 err = EMSGSIZE;
372 harderr = 1;
373 break;
374 }
375 goto out;
376 }
377 err = EHOSTUNREACH;
378 if (code <= NR_ICMP_UNREACH) {
379 harderr = icmp_err_convert[code].fatal;
380 err = icmp_err_convert[code].errno;
381 }
382 break;
383 case ICMP_REDIRECT:
384 /* See ICMP_SOURCE_QUENCH */
385 err = EREMOTEIO;
386 break;
387 }
388
389 /*
390 * RFC1122: OK. Passes ICMP errors back to application, as per
391 * 4.1.3.3.
392 */
393 if (!inet_sock->recverr) {
394 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
395 goto out;
396 } else {
397 ip_icmp_error(sk, skb, err, 0 /* no remote port */,
398 info, (u8 *)icmph);
399 }
400 sk->sk_err = err;
401 sk->sk_error_report(sk);
402out:
403 sock_put(sk);
404}
405
406/*
407 * Copy and checksum an ICMP Echo packet from user space into a buffer.
408 */
409
410struct pingfakehdr {
411 struct icmphdr icmph;
412 struct iovec *iov;
413 u32 wcheck;
414};
415
416static int ping_getfrag(void *from, char * to,
417 int offset, int fraglen, int odd, struct sk_buff *skb)
418{
419 struct pingfakehdr *pfh = (struct pingfakehdr *)from;
420
421 if (offset == 0) {
422 if (fraglen < sizeof(struct icmphdr))
423 BUG();
424 if (csum_partial_copy_fromiovecend(to + sizeof(struct icmphdr),
425 pfh->iov, 0, fraglen - sizeof(struct icmphdr),
426 &pfh->wcheck))
427 return -EFAULT;
428
429 return 0;
430 }
431 if (offset < sizeof(struct icmphdr))
432 BUG();
433 if (csum_partial_copy_fromiovecend
434 (to, pfh->iov, offset - sizeof(struct icmphdr),
435 fraglen, &pfh->wcheck))
436 return -EFAULT;
437 return 0;
438}
439
440static int ping_push_pending_frames(struct sock *sk, struct pingfakehdr *pfh, struct flowi4 *fl4)
441{
442 struct sk_buff *skb = skb_peek(&sk->sk_write_queue);
443
444 pfh->wcheck = csum_partial((char *)&pfh->icmph,
445 sizeof(struct icmphdr), pfh->wcheck);
446 pfh->icmph.checksum = csum_fold(pfh->wcheck);
447 memcpy(icmp_hdr(skb), &pfh->icmph, sizeof(struct icmphdr));
448 skb->ip_summed = CHECKSUM_NONE;
449 return ip_push_pending_frames(sk, fl4);
450}
451
bb0cd2fb
CG
452static int ping_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
453 size_t len)
c319b4d7
VK
454{
455 struct net *net = sock_net(sk);
456 struct flowi4 fl4;
457 struct inet_sock *inet = inet_sk(sk);
458 struct ipcm_cookie ipc;
459 struct icmphdr user_icmph;
460 struct pingfakehdr pfh;
461 struct rtable *rt = NULL;
462 struct ip_options_data opt_copy;
463 int free = 0;
464 u32 saddr, daddr, faddr;
465 u8 tos;
466 int err;
467
468 pr_debug("ping_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
469
470
471 if (len > 0xFFFF)
472 return -EMSGSIZE;
473
474 /*
475 * Check the flags.
476 */
477
478 /* Mirror BSD error message compatibility */
479 if (msg->msg_flags & MSG_OOB)
480 return -EOPNOTSUPP;
481
482 /*
483 * Fetch the ICMP header provided by the userland.
484 * iovec is modified!
485 */
486
487 if (memcpy_fromiovec((u8 *)&user_icmph, msg->msg_iov,
488 sizeof(struct icmphdr)))
489 return -EFAULT;
490 if (!ping_supported(user_icmph.type, user_icmph.code))
491 return -EINVAL;
492
493 /*
494 * Get and verify the address.
495 */
496
497 if (msg->msg_name) {
498 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
499 if (msg->msg_namelen < sizeof(*usin))
500 return -EINVAL;
501 if (usin->sin_family != AF_INET)
502 return -EINVAL;
503 daddr = usin->sin_addr.s_addr;
504 /* no remote port */
505 } else {
506 if (sk->sk_state != TCP_ESTABLISHED)
507 return -EDESTADDRREQ;
508 daddr = inet->inet_daddr;
509 /* no remote port */
510 }
511
512 ipc.addr = inet->inet_saddr;
513 ipc.opt = NULL;
514 ipc.oif = sk->sk_bound_dev_if;
515 ipc.tx_flags = 0;
516 err = sock_tx_timestamp(sk, &ipc.tx_flags);
517 if (err)
518 return err;
519
520 if (msg->msg_controllen) {
521 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
522 if (err)
523 return err;
524 if (ipc.opt)
525 free = 1;
526 }
527 if (!ipc.opt) {
528 struct ip_options_rcu *inet_opt;
529
530 rcu_read_lock();
531 inet_opt = rcu_dereference(inet->inet_opt);
532 if (inet_opt) {
533 memcpy(&opt_copy, inet_opt,
534 sizeof(*inet_opt) + inet_opt->opt.optlen);
535 ipc.opt = &opt_copy.opt;
536 }
537 rcu_read_unlock();
538 }
539
540 saddr = ipc.addr;
541 ipc.addr = faddr = daddr;
542
543 if (ipc.opt && ipc.opt->opt.srr) {
544 if (!daddr)
545 return -EINVAL;
546 faddr = ipc.opt->opt.faddr;
547 }
548 tos = RT_TOS(inet->tos);
549 if (sock_flag(sk, SOCK_LOCALROUTE) ||
550 (msg->msg_flags & MSG_DONTROUTE) ||
551 (ipc.opt && ipc.opt->opt.is_strictroute)) {
552 tos |= RTO_ONLINK;
553 }
554
555 if (ipv4_is_multicast(daddr)) {
556 if (!ipc.oif)
557 ipc.oif = inet->mc_index;
558 if (!saddr)
559 saddr = inet->mc_addr;
560 }
561
562 flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
563 RT_SCOPE_UNIVERSE, sk->sk_protocol,
564 inet_sk_flowi_flags(sk), faddr, saddr, 0, 0);
565
566 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
567 rt = ip_route_output_flow(net, &fl4, sk);
568 if (IS_ERR(rt)) {
569 err = PTR_ERR(rt);
570 rt = NULL;
571 if (err == -ENETUNREACH)
572 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
573 goto out;
574 }
575
576 err = -EACCES;
577 if ((rt->rt_flags & RTCF_BROADCAST) &&
578 !sock_flag(sk, SOCK_BROADCAST))
579 goto out;
580
581 if (msg->msg_flags & MSG_CONFIRM)
582 goto do_confirm;
583back_from_confirm:
584
585 if (!ipc.addr)
586 ipc.addr = fl4.daddr;
587
588 lock_sock(sk);
589
590 pfh.icmph.type = user_icmph.type; /* already checked */
591 pfh.icmph.code = user_icmph.code; /* ditto */
592 pfh.icmph.checksum = 0;
593 pfh.icmph.un.echo.id = inet->inet_sport;
594 pfh.icmph.un.echo.sequence = user_icmph.un.echo.sequence;
595 pfh.iov = msg->msg_iov;
596 pfh.wcheck = 0;
597
598 err = ip_append_data(sk, &fl4, ping_getfrag, &pfh, len,
599 0, &ipc, &rt, msg->msg_flags);
600 if (err)
601 ip_flush_pending_frames(sk);
602 else
603 err = ping_push_pending_frames(sk, &pfh, &fl4);
604 release_sock(sk);
605
606out:
607 ip_rt_put(rt);
608 if (free)
609 kfree(ipc.opt);
610 if (!err) {
611 icmp_out_count(sock_net(sk), user_icmph.type);
612 return len;
613 }
614 return err;
615
616do_confirm:
617 dst_confirm(&rt->dst);
618 if (!(msg->msg_flags & MSG_PROBE) || len)
619 goto back_from_confirm;
620 err = 0;
621 goto out;
622}
623
bb0cd2fb
CG
624static int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
625 size_t len, int noblock, int flags, int *addr_len)
c319b4d7
VK
626{
627 struct inet_sock *isk = inet_sk(sk);
628 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
629 struct sk_buff *skb;
630 int copied, err;
631
632 pr_debug("ping_recvmsg(sk=%p,sk->num=%u)\n", isk, isk->inet_num);
633
634 if (flags & MSG_OOB)
635 goto out;
636
637 if (addr_len)
638 *addr_len = sizeof(*sin);
639
640 if (flags & MSG_ERRQUEUE)
641 return ip_recv_error(sk, msg, len);
642
643 skb = skb_recv_datagram(sk, flags, noblock, &err);
644 if (!skb)
645 goto out;
646
647 copied = skb->len;
648 if (copied > len) {
649 msg->msg_flags |= MSG_TRUNC;
650 copied = len;
651 }
652
653 /* Don't bother checking the checksum */
654 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
655 if (err)
656 goto done;
657
658 sock_recv_timestamp(msg, sk, skb);
659
660 /* Copy the address. */
661 if (sin) {
662 sin->sin_family = AF_INET;
663 sin->sin_port = 0 /* skb->h.uh->source */;
664 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
665 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
666 }
667 if (isk->cmsg_flags)
668 ip_cmsg_recv(msg, skb);
669 err = copied;
670
671done:
672 skb_free_datagram(sk, skb);
673out:
674 pr_debug("ping_recvmsg -> %d\n", err);
675 return err;
676}
677
678static int ping_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
679{
680 pr_debug("ping_queue_rcv_skb(sk=%p,sk->num=%d,skb=%p)\n",
681 inet_sk(sk), inet_sk(sk)->inet_num, skb);
682 if (sock_queue_rcv_skb(sk, skb) < 0) {
683 ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_INERRORS);
684 kfree_skb(skb);
685 pr_debug("ping_queue_rcv_skb -> failed\n");
686 return -1;
687 }
688 return 0;
689}
690
691
692/*
693 * All we need to do is get the socket.
694 */
695
696void ping_rcv(struct sk_buff *skb)
697{
698 struct sock *sk;
699 struct net *net = dev_net(skb->dev);
700 struct iphdr *iph = ip_hdr(skb);
701 struct icmphdr *icmph = icmp_hdr(skb);
702 u32 saddr = iph->saddr;
703 u32 daddr = iph->daddr;
704
705 /* We assume the packet has already been checked by icmp_rcv */
706
707 pr_debug("ping_rcv(skb=%p,id=%04x,seq=%04x)\n",
708 skb, ntohs(icmph->un.echo.id), ntohs(icmph->un.echo.sequence));
709
710 /* Push ICMP header back */
711 skb_push(skb, skb->data - (u8 *)icmph);
712
713 sk = ping_v4_lookup(net, saddr, daddr, ntohs(icmph->un.echo.id),
714 skb->dev->ifindex);
715 if (sk != NULL) {
716 pr_debug("rcv on socket %p\n", sk);
717 ping_queue_rcv_skb(sk, skb_get(skb));
718 sock_put(sk);
719 return;
720 }
721 pr_debug("no socket, dropping\n");
722
723 /* We're called from icmp_rcv(). kfree_skb() is done there. */
724}
725
726struct proto ping_prot = {
727 .name = "PING",
728 .owner = THIS_MODULE,
729 .init = ping_init_sock,
730 .close = ping_close,
731 .connect = ip4_datagram_connect,
732 .disconnect = udp_disconnect,
c319b4d7
VK
733 .setsockopt = ip_setsockopt,
734 .getsockopt = ip_getsockopt,
735 .sendmsg = ping_sendmsg,
736 .recvmsg = ping_recvmsg,
737 .bind = ping_bind,
738 .backlog_rcv = ping_queue_rcv_skb,
739 .hash = ping_v4_hash,
740 .unhash = ping_v4_unhash,
741 .get_port = ping_v4_get_port,
742 .obj_size = sizeof(struct inet_sock),
743};
744EXPORT_SYMBOL(ping_prot);
745
746#ifdef CONFIG_PROC_FS
747
748static struct sock *ping_get_first(struct seq_file *seq, int start)
749{
750 struct sock *sk;
751 struct ping_iter_state *state = seq->private;
752 struct net *net = seq_file_net(seq);
753
754 for (state->bucket = start; state->bucket < PING_HTABLE_SIZE;
755 ++state->bucket) {
756 struct hlist_nulls_node *node;
757 struct hlist_nulls_head *hslot = &ping_table.hash[state->bucket];
758
759 if (hlist_nulls_empty(hslot))
760 continue;
761
762 sk_nulls_for_each(sk, node, hslot) {
763 if (net_eq(sock_net(sk), net))
764 goto found;
765 }
766 }
767 sk = NULL;
768found:
769 return sk;
770}
771
772static struct sock *ping_get_next(struct seq_file *seq, struct sock *sk)
773{
774 struct ping_iter_state *state = seq->private;
775 struct net *net = seq_file_net(seq);
776
777 do {
778 sk = sk_nulls_next(sk);
779 } while (sk && (!net_eq(sock_net(sk), net)));
780
781 if (!sk)
782 return ping_get_first(seq, state->bucket + 1);
783 return sk;
784}
785
786static struct sock *ping_get_idx(struct seq_file *seq, loff_t pos)
787{
788 struct sock *sk = ping_get_first(seq, 0);
789
790 if (sk)
791 while (pos && (sk = ping_get_next(seq, sk)) != NULL)
792 --pos;
793 return pos ? NULL : sk;
794}
795
796static void *ping_seq_start(struct seq_file *seq, loff_t *pos)
797{
798 struct ping_iter_state *state = seq->private;
799 state->bucket = 0;
800
801 read_lock_bh(&ping_table.lock);
802
803 return *pos ? ping_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
804}
805
806static void *ping_seq_next(struct seq_file *seq, void *v, loff_t *pos)
807{
808 struct sock *sk;
809
810 if (v == SEQ_START_TOKEN)
811 sk = ping_get_idx(seq, 0);
812 else
813 sk = ping_get_next(seq, v);
814
815 ++*pos;
816 return sk;
817}
818
819static void ping_seq_stop(struct seq_file *seq, void *v)
820{
821 read_unlock_bh(&ping_table.lock);
822}
823
824static void ping_format_sock(struct sock *sp, struct seq_file *f,
825 int bucket, int *len)
826{
827 struct inet_sock *inet = inet_sk(sp);
828 __be32 dest = inet->inet_daddr;
829 __be32 src = inet->inet_rcv_saddr;
830 __u16 destp = ntohs(inet->inet_dport);
831 __u16 srcp = ntohs(inet->inet_sport);
832
833 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
834 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
835 bucket, src, srcp, dest, destp, sp->sk_state,
836 sk_wmem_alloc_get(sp),
837 sk_rmem_alloc_get(sp),
838 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
839 atomic_read(&sp->sk_refcnt), sp,
840 atomic_read(&sp->sk_drops), len);
841}
842
843static int ping_seq_show(struct seq_file *seq, void *v)
844{
845 if (v == SEQ_START_TOKEN)
846 seq_printf(seq, "%-127s\n",
847 " sl local_address rem_address st tx_queue "
848 "rx_queue tr tm->when retrnsmt uid timeout "
849 "inode ref pointer drops");
850 else {
851 struct ping_iter_state *state = seq->private;
852 int len;
853
854 ping_format_sock(v, seq, state->bucket, &len);
855 seq_printf(seq, "%*s\n", 127 - len, "");
856 }
857 return 0;
858}
859
860static const struct seq_operations ping_seq_ops = {
861 .show = ping_seq_show,
862 .start = ping_seq_start,
863 .next = ping_seq_next,
864 .stop = ping_seq_stop,
865};
866
867static int ping_seq_open(struct inode *inode, struct file *file)
868{
869 return seq_open_net(inode, file, &ping_seq_ops,
870 sizeof(struct ping_iter_state));
871}
872
873static const struct file_operations ping_seq_fops = {
874 .open = ping_seq_open,
875 .read = seq_read,
876 .llseek = seq_lseek,
877 .release = seq_release_net,
878};
879
880static int ping_proc_register(struct net *net)
881{
882 struct proc_dir_entry *p;
883 int rc = 0;
884
885 p = proc_net_fops_create(net, "icmp", S_IRUGO, &ping_seq_fops);
886 if (!p)
887 rc = -ENOMEM;
888 return rc;
889}
890
891static void ping_proc_unregister(struct net *net)
892{
893 proc_net_remove(net, "icmp");
894}
895
896
897static int __net_init ping_proc_init_net(struct net *net)
898{
899 return ping_proc_register(net);
900}
901
902static void __net_exit ping_proc_exit_net(struct net *net)
903{
904 ping_proc_unregister(net);
905}
906
907static struct pernet_operations ping_net_ops = {
908 .init = ping_proc_init_net,
909 .exit = ping_proc_exit_net,
910};
911
912int __init ping_proc_init(void)
913{
914 return register_pernet_subsys(&ping_net_ops);
915}
916
917void ping_proc_exit(void)
918{
919 unregister_pernet_subsys(&ping_net_ops);
920}
921
922#endif
923
924void __init ping_init(void)
925{
926 int i;
927
928 for (i = 0; i < PING_HTABLE_SIZE; i++)
929 INIT_HLIST_NULLS_HEAD(&ping_table.hash[i], i);
930 rwlock_init(&ping_table.lock);
931}