| 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/in.h> |
| 13 | #include <net/ip.h> |
| 14 | #include <net/sock.h> |
| 15 | #include <net/route.h> |
| 16 | #include <net/tcp_states.h> |
| 17 | #include <net/sock_reuseport.h> |
| 18 | |
| 19 | int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 20 | { |
| 21 | struct inet_sock *inet = inet_sk(sk); |
| 22 | struct sockaddr_in *usin = (struct sockaddr_in *) uaddr; |
| 23 | struct flowi4 *fl4; |
| 24 | struct rtable *rt; |
| 25 | __be32 saddr; |
| 26 | int oif; |
| 27 | int err; |
| 28 | |
| 29 | |
| 30 | if (addr_len < sizeof(*usin)) |
| 31 | return -EINVAL; |
| 32 | |
| 33 | if (usin->sin_family != AF_INET) |
| 34 | return -EAFNOSUPPORT; |
| 35 | |
| 36 | sk_dst_reset(sk); |
| 37 | |
| 38 | oif = sk->sk_bound_dev_if; |
| 39 | saddr = inet->inet_saddr; |
| 40 | if (ipv4_is_multicast(addr: usin->sin_addr.s_addr)) { |
| 41 | if (!oif || netif_index_is_l3_master(net: sock_net(sk), ifindex: oif)) |
| 42 | oif = READ_ONCE(inet->mc_index); |
| 43 | if (!saddr) |
| 44 | saddr = READ_ONCE(inet->mc_addr); |
| 45 | } else if (!oif) { |
| 46 | oif = READ_ONCE(inet->uc_index); |
| 47 | } |
| 48 | fl4 = &inet->cork.fl.u.ip4; |
| 49 | rt = ip_route_connect(fl4, dst: usin->sin_addr.s_addr, src: saddr, oif, |
| 50 | protocol: sk->sk_protocol, sport: inet->inet_sport, |
| 51 | dport: usin->sin_port, sk); |
| 52 | if (IS_ERR(ptr: rt)) { |
| 53 | err = PTR_ERR(ptr: rt); |
| 54 | if (err == -ENETUNREACH) |
| 55 | IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); |
| 56 | goto out; |
| 57 | } |
| 58 | |
| 59 | if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, flag: SOCK_BROADCAST)) { |
| 60 | ip_rt_put(rt); |
| 61 | err = -EACCES; |
| 62 | goto out; |
| 63 | } |
| 64 | |
| 65 | /* Update addresses before rehashing */ |
| 66 | inet->inet_daddr = fl4->daddr; |
| 67 | inet->inet_dport = usin->sin_port; |
| 68 | if (!inet->inet_saddr) |
| 69 | inet->inet_saddr = fl4->saddr; |
| 70 | if (!inet->inet_rcv_saddr) { |
| 71 | inet->inet_rcv_saddr = fl4->saddr; |
| 72 | if (sk->sk_prot->rehash) |
| 73 | sk->sk_prot->rehash(sk); |
| 74 | } |
| 75 | reuseport_has_conns_set(sk); |
| 76 | sk->sk_state = TCP_ESTABLISHED; |
| 77 | sk_set_txhash(sk); |
| 78 | atomic_set(v: &inet->inet_id, i: get_random_u16()); |
| 79 | |
| 80 | sk_dst_set(sk, dst: &rt->dst); |
| 81 | err = 0; |
| 82 | out: |
| 83 | return err; |
| 84 | } |
| 85 | EXPORT_SYMBOL(__ip4_datagram_connect); |
| 86 | |
| 87 | int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 88 | { |
| 89 | int res; |
| 90 | |
| 91 | lock_sock(sk); |
| 92 | res = __ip4_datagram_connect(sk, uaddr, addr_len); |
| 93 | release_sock(sk); |
| 94 | return res; |
| 95 | } |
| 96 | EXPORT_SYMBOL(ip4_datagram_connect); |
| 97 | |
| 98 | /* Because UDP xmit path can manipulate sk_dst_cache without holding |
| 99 | * socket lock, we need to use sk_dst_set() here, |
| 100 | * even if we own the socket lock. |
| 101 | */ |
| 102 | void ip4_datagram_release_cb(struct sock *sk) |
| 103 | { |
| 104 | const struct inet_sock *inet = inet_sk(sk); |
| 105 | struct dst_entry *dst; |
| 106 | struct flowi4 fl4; |
| 107 | struct rtable *rt; |
| 108 | |
| 109 | rcu_read_lock(); |
| 110 | |
| 111 | dst = __sk_dst_get(sk); |
| 112 | if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) { |
| 113 | rcu_read_unlock(); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | inet_sk_init_flowi4(inet, fl4: &fl4); |
| 118 | rt = ip_route_output_flow(sock_net(sk), flp: &fl4, sk); |
| 119 | dst = !IS_ERR(ptr: rt) ? &rt->dst : NULL; |
| 120 | sk_dst_set(sk, dst); |
| 121 | |
| 122 | rcu_read_unlock(); |
| 123 | } |
| 124 | EXPORT_SYMBOL_GPL(ip4_datagram_release_cb); |
| 125 | |