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