xref: /linux/net/ipv4/datagram.c (revision a0eb2da92b715d0c97b96b09979689ea09faefe6)
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(usin->sin_addr.s_addr)) {
41 		if (!oif || netif_index_is_l3_master(sock_net(sk), oif))
42 			oif = inet->mc_index;
43 		if (!saddr)
44 			saddr = inet->mc_addr;
45 	}
46 	fl4 = &inet->cork.fl.u.ip4;
47 	rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr,
48 			      RT_CONN_FLAGS(sk), oif,
49 			      sk->sk_protocol,
50 			      inet->inet_sport, usin->sin_port, sk);
51 	if (IS_ERR(rt)) {
52 		err = PTR_ERR(rt);
53 		if (err == -ENETUNREACH)
54 			IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
55 		goto out;
56 	}
57 
58 	if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
59 		ip_rt_put(rt);
60 		err = -EACCES;
61 		goto out;
62 	}
63 	if (!inet->inet_saddr)
64 		inet->inet_saddr = fl4->saddr;	/* Update source address */
65 	if (!inet->inet_rcv_saddr) {
66 		inet->inet_rcv_saddr = fl4->saddr;
67 		if (sk->sk_prot->rehash)
68 			sk->sk_prot->rehash(sk);
69 	}
70 	inet->inet_daddr = fl4->daddr;
71 	inet->inet_dport = usin->sin_port;
72 	reuseport_has_conns(sk, true);
73 	sk->sk_state = TCP_ESTABLISHED;
74 	sk_set_txhash(sk);
75 	inet->inet_id = prandom_u32();
76 
77 	sk_dst_set(sk, &rt->dst);
78 	err = 0;
79 out:
80 	return err;
81 }
82 EXPORT_SYMBOL(__ip4_datagram_connect);
83 
84 int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
85 {
86 	int res;
87 
88 	lock_sock(sk);
89 	res = __ip4_datagram_connect(sk, uaddr, addr_len);
90 	release_sock(sk);
91 	return res;
92 }
93 EXPORT_SYMBOL(ip4_datagram_connect);
94 
95 /* Because UDP xmit path can manipulate sk_dst_cache without holding
96  * socket lock, we need to use sk_dst_set() here,
97  * even if we own the socket lock.
98  */
99 void ip4_datagram_release_cb(struct sock *sk)
100 {
101 	const struct inet_sock *inet = inet_sk(sk);
102 	const struct ip_options_rcu *inet_opt;
103 	__be32 daddr = inet->inet_daddr;
104 	struct dst_entry *dst;
105 	struct flowi4 fl4;
106 	struct rtable *rt;
107 
108 	rcu_read_lock();
109 
110 	dst = __sk_dst_get(sk);
111 	if (!dst || !dst->obsolete || dst->ops->check(dst, 0)) {
112 		rcu_read_unlock();
113 		return;
114 	}
115 	inet_opt = rcu_dereference(inet->inet_opt);
116 	if (inet_opt && inet_opt->opt.srr)
117 		daddr = inet_opt->opt.faddr;
118 	rt = ip_route_output_ports(sock_net(sk), &fl4, sk, daddr,
119 				   inet->inet_saddr, inet->inet_dport,
120 				   inet->inet_sport, sk->sk_protocol,
121 				   RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
122 
123 	dst = !IS_ERR(rt) ? &rt->dst : NULL;
124 	sk_dst_set(sk, dst);
125 
126 	rcu_read_unlock();
127 }
128 EXPORT_SYMBOL_GPL(ip4_datagram_release_cb);
129