LCOV - code coverage report
Current view: top level - net/ipv4 - ping.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 428 6.5 %
Date: 2014-04-07 Functions: 8 35 22.9 %
Branches: 8 348 2.3 %

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

Generated by: LCOV version 1.9