~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/ping.c

Version: ~ [ linux-5.3-rc8 ] ~ [ linux-5.2.13 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.71 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.142 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.191 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.191 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.73 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp