LCOV - code coverage report
Current view: top level - net/ipv4 - datagram.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 57 74 77.0 %
Date: 2021-04-22 12:43:58 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  *      common UDP/RAW code
       4             :  *      Linux INET implementation
       5             :  *
       6             :  * Authors:
       7             :  *      Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
       8             :  */
       9             : 
      10             : #include <linux/types.h>
      11             : #include <linux/module.h>
      12             : #include <linux/ip.h>
      13             : #include <linux/in.h>
      14             : #include <net/ip.h>
      15             : #include <net/sock.h>
      16             : #include <net/route.h>
      17             : #include <net/tcp_states.h>
      18             : #include <net/sock_reuseport.h>
      19             : 
      20          33 : int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
      21             : {
      22          33 :         struct inet_sock *inet = inet_sk(sk);
      23          33 :         struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
      24          33 :         struct flowi4 *fl4;
      25          33 :         struct rtable *rt;
      26          33 :         __be32 saddr;
      27          33 :         int oif;
      28          33 :         int err;
      29             : 
      30             : 
      31          33 :         if (addr_len < sizeof(*usin))
      32             :                 return -EINVAL;
      33             : 
      34          33 :         if (usin->sin_family != AF_INET)
      35             :                 return -EAFNOSUPPORT;
      36             : 
      37          33 :         sk_dst_reset(sk);
      38             : 
      39          33 :         oif = sk->sk_bound_dev_if;
      40          33 :         saddr = inet->inet_saddr;
      41          33 :         if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
      42           0 :                 if (!oif || netif_index_is_l3_master(sock_net(sk), oif))
      43           0 :                         oif = inet->mc_index;
      44           0 :                 if (!saddr)
      45           0 :                         saddr = inet->mc_addr;
      46             :         }
      47          33 :         fl4 = &inet->cork.fl.u.ip4;
      48          99 :         rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
      49          33 :                               RT_CONN_FLAGS(sk), oif,
      50          33 :                               sk->sk_protocol,
      51          33 :                               inet->inet_sport, usin->sin_port, sk);
      52          33 :         if (IS_ERR(rt)) {
      53          20 :                 err = PTR_ERR(rt);
      54          20 :                 if (err == -ENETUNREACH)
      55          20 :                         IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
      56          20 :                 goto out;
      57             :         }
      58             : 
      59          13 :         if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
      60           0 :                 ip_rt_put(rt);
      61           0 :                 err = -EACCES;
      62           0 :                 goto out;
      63             :         }
      64          13 :         if (!inet->inet_saddr)
      65          13 :                 inet->inet_saddr = fl4->saddr;    /* Update source address */
      66          13 :         if (!inet->inet_rcv_saddr) {
      67          13 :                 inet->inet_rcv_saddr = fl4->saddr;
      68          13 :                 if (sk->sk_prot->rehash)
      69          13 :                         sk->sk_prot->rehash(sk);
      70             :         }
      71          13 :         inet->inet_daddr = fl4->daddr;
      72          13 :         inet->inet_dport = usin->sin_port;
      73          13 :         reuseport_has_conns(sk, true);
      74          13 :         sk->sk_state = TCP_ESTABLISHED;
      75          13 :         sk_set_txhash(sk);
      76          13 :         inet->inet_id = prandom_u32();
      77             : 
      78          13 :         sk_dst_set(sk, &rt->dst);
      79          13 :         err = 0;
      80             : out:
      81             :         return err;
      82             : }
      83             : EXPORT_SYMBOL(__ip4_datagram_connect);
      84             : 
      85          33 : int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
      86             : {
      87          33 :         int res;
      88             : 
      89          33 :         lock_sock(sk);
      90          33 :         res = __ip4_datagram_connect(sk, uaddr, addr_len);
      91          33 :         release_sock(sk);
      92          33 :         return res;
      93             : }
      94             : EXPORT_SYMBOL(ip4_datagram_connect);
      95             : 
      96             : /* Because UDP xmit path can manipulate sk_dst_cache without holding
      97             :  * socket lock, we need to use sk_dst_set() here,
      98             :  * even if we own the socket lock.
      99             :  */
     100          71 : void ip4_datagram_release_cb(struct sock *sk)
     101             : {
     102          71 :         const struct inet_sock *inet = inet_sk(sk);
     103          71 :         const struct ip_options_rcu *inet_opt;
     104          71 :         __be32 daddr = inet->inet_daddr;
     105          71 :         struct dst_entry *dst;
     106          71 :         struct flowi4 fl4;
     107          71 :         struct rtable *rt;
     108             : 
     109          71 :         rcu_read_lock();
     110             : 
     111          71 :         dst = __sk_dst_get(sk);
     112          71 :         if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
     113          71 :                 rcu_read_unlock();
     114          71 :                 return;
     115             :         }
     116           0 :         inet_opt = rcu_dereference(inet->inet_opt);
     117           0 :         if (inet_opt && inet_opt->opt.srr)
     118           0 :                 daddr = inet_opt->opt.faddr;
     119           0 :         rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
     120           0 :                                    inet->inet_saddr, inet->inet_dport,
     121           0 :                                    inet->inet_sport, sk->sk_protocol,
     122           0 :                                    RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
     123             : 
     124           0 :         dst = !IS_ERR(rt) ? &rt->dst : NULL;
     125           0 :         sk_dst_set(sk, dst);
     126             : 
     127           0 :         rcu_read_unlock();
     128             : }
     129             : EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);

Generated by: LCOV version 1.14