12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * common UDP/RAW code
41da177e4SLinus Torvalds * Linux INET implementation
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Authors:
71da177e4SLinus Torvalds * Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/in.h>
1320380731SArnaldo Carvalho de Melo #include <net/ip.h>
141da177e4SLinus Torvalds #include <net/sock.h>
151da177e4SLinus Torvalds #include <net/route.h>
16c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
17acdcecc6SWillem de Bruijn #include <net/sock_reuseport.h>
181da177e4SLinus Torvalds
__ip4_datagram_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)1903645a11SEric Dumazet int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
201da177e4SLinus Torvalds {
211da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
221da177e4SLinus Torvalds struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
233038eeacSDavid S. Miller struct flowi4 *fl4;
241da177e4SLinus Torvalds struct rtable *rt;
25bada8adcSAl Viro __be32 saddr;
261da177e4SLinus Torvalds int oif;
271da177e4SLinus Torvalds int err;
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds
301da177e4SLinus Torvalds if (addr_len < sizeof(*usin))
311da177e4SLinus Torvalds return -EINVAL;
321da177e4SLinus Torvalds
331da177e4SLinus Torvalds if (usin->sin_family != AF_INET)
341da177e4SLinus Torvalds return -EAFNOSUPPORT;
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds sk_dst_reset(sk);
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds oif = sk->sk_bound_dev_if;
39c720c7e8SEric Dumazet saddr = inet->inet_saddr;
40f97c1e0cSJoe Perches if (ipv4_is_multicast(usin->sin_addr.s_addr)) {
41854da991SRobert Shearman if (!oif || netif_index_is_l3_master(sock_net(sk), oif))
4202715925SEric Dumazet oif = READ_ONCE(inet->mc_index);
431da177e4SLinus Torvalds if (!saddr)
4402715925SEric Dumazet saddr = READ_ONCE(inet->mc_addr);
450e4d3547SRichard Gobert } else if (!oif) {
46959d5c11SEric Dumazet oif = READ_ONCE(inet->uc_index);
471da177e4SLinus Torvalds }
483038eeacSDavid S. Miller fl4 = &inet->cork.fl.u.ip4;
4967e1e2f4SGuillaume Nault rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr, oif,
5067e1e2f4SGuillaume Nault sk->sk_protocol, inet->inet_sport,
5167e1e2f4SGuillaume Nault usin->sin_port, sk);
52b23dd4feSDavid S. Miller if (IS_ERR(rt)) {
53b23dd4feSDavid S. Miller err = PTR_ERR(rt);
54584bdf8cSWei Dong if (err == -ENETUNREACH)
55c9e90429SEric Dumazet IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
563038eeacSDavid S. Miller goto out;
57584bdf8cSWei Dong }
58584bdf8cSWei Dong
591da177e4SLinus Torvalds if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
601da177e4SLinus Torvalds ip_rt_put(rt);
613038eeacSDavid S. Miller err = -EACCES;
623038eeacSDavid S. Miller goto out;
631da177e4SLinus Torvalds }
64c720c7e8SEric Dumazet if (!inet->inet_saddr)
653038eeacSDavid S. Miller inet->inet_saddr = fl4->saddr; /* Update source address */
66719f8358SEric Dumazet if (!inet->inet_rcv_saddr) {
673038eeacSDavid S. Miller inet->inet_rcv_saddr = fl4->saddr;
68719f8358SEric Dumazet if (sk->sk_prot->rehash)
69719f8358SEric Dumazet sk->sk_prot->rehash(sk);
70719f8358SEric Dumazet }
713038eeacSDavid S. Miller inet->inet_daddr = fl4->daddr;
72c720c7e8SEric Dumazet inet->inet_dport = usin->sin_port;
7369421bf9SKuniyuki Iwashima reuseport_has_conns_set(sk);
741da177e4SLinus Torvalds sk->sk_state = TCP_ESTABLISHED;
75877d1f62STom Herbert sk_set_txhash(sk);
76f866fbc8SEric Dumazet atomic_set(&inet->inet_id, get_random_u16());
771da177e4SLinus Torvalds
78d8d1f30bSChangli Gao sk_dst_set(sk, &rt->dst);
793038eeacSDavid S. Miller err = 0;
803038eeacSDavid S. Miller out:
813038eeacSDavid S. Miller return err;
821da177e4SLinus Torvalds }
8303645a11SEric Dumazet EXPORT_SYMBOL(__ip4_datagram_connect);
8403645a11SEric Dumazet
ip4_datagram_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)8503645a11SEric Dumazet int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
8603645a11SEric Dumazet {
8703645a11SEric Dumazet int res;
8803645a11SEric Dumazet
8903645a11SEric Dumazet lock_sock(sk);
9003645a11SEric Dumazet res = __ip4_datagram_connect(sk, uaddr, addr_len);
9103645a11SEric Dumazet release_sock(sk);
9203645a11SEric Dumazet return res;
9303645a11SEric Dumazet }
941da177e4SLinus Torvalds EXPORT_SYMBOL(ip4_datagram_connect);
958141ed9fSSteffen Klassert
969709674eSEric Dumazet /* Because UDP xmit path can manipulate sk_dst_cache without holding
979709674eSEric Dumazet * socket lock, we need to use sk_dst_set() here,
989709674eSEric Dumazet * even if we own the socket lock.
999709674eSEric Dumazet */
ip4_datagram_release_cb(struct sock * sk)1008141ed9fSSteffen Klassert void ip4_datagram_release_cb(struct sock *sk)
1018141ed9fSSteffen Klassert {
1028141ed9fSSteffen Klassert const struct inet_sock *inet = inet_sk(sk);
1038141ed9fSSteffen Klassert const struct ip_options_rcu *inet_opt;
1048141ed9fSSteffen Klassert __be32 daddr = inet->inet_daddr;
1059709674eSEric Dumazet struct dst_entry *dst;
1068141ed9fSSteffen Klassert struct flowi4 fl4;
1078141ed9fSSteffen Klassert struct rtable *rt;
1088141ed9fSSteffen Klassert
1098141ed9fSSteffen Klassert rcu_read_lock();
1109709674eSEric Dumazet
1119709674eSEric Dumazet dst = __sk_dst_get(sk);
1129709674eSEric Dumazet if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
1139709674eSEric Dumazet rcu_read_unlock();
1149709674eSEric Dumazet return;
1159709674eSEric Dumazet }
1168141ed9fSSteffen Klassert inet_opt = rcu_dereference(inet->inet_opt);
1178141ed9fSSteffen Klassert if (inet_opt && inet_opt->opt.srr)
1188141ed9fSSteffen Klassert daddr = inet_opt->opt.faddr;
1198141ed9fSSteffen Klassert rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
1208141ed9fSSteffen Klassert inet->inet_saddr, inet->inet_dport,
1218141ed9fSSteffen Klassert inet->inet_sport, sk->sk_protocol,
122*a3522a2eSGuillaume Nault ip_sock_rt_tos(sk), sk->sk_bound_dev_if);
1239709674eSEric Dumazet
1249709674eSEric Dumazet dst = !IS_ERR(rt) ? &rt->dst : NULL;
1259709674eSEric Dumazet sk_dst_set(sk, dst);
1269709674eSEric Dumazet
1278141ed9fSSteffen Klassert rcu_read_unlock();
1288141ed9fSSteffen Klassert }
1298141ed9fSSteffen Klassert EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);
130