xref: /openbmc/linux/net/ipv6/inet6_connection_sock.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28129765aSArnaldo Carvalho de Melo /*
38129765aSArnaldo Carvalho de Melo  * INET        An implementation of the TCP/IP protocol suite for the LINUX
48129765aSArnaldo Carvalho de Melo  *             operating system.  INET is implemented using the  BSD Socket
58129765aSArnaldo Carvalho de Melo  *             interface as the means of communication with the user level.
68129765aSArnaldo Carvalho de Melo  *
78129765aSArnaldo Carvalho de Melo  *             Support for INET6 connection oriented protocols.
88129765aSArnaldo Carvalho de Melo  *
98129765aSArnaldo Carvalho de Melo  * Authors:    See the TCPv6 sources
108129765aSArnaldo Carvalho de Melo  */
118129765aSArnaldo Carvalho de Melo 
128129765aSArnaldo Carvalho de Melo #include <linux/module.h>
138129765aSArnaldo Carvalho de Melo #include <linux/in6.h>
148129765aSArnaldo Carvalho de Melo #include <linux/ipv6.h>
158129765aSArnaldo Carvalho de Melo #include <linux/jhash.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
178129765aSArnaldo Carvalho de Melo 
188129765aSArnaldo Carvalho de Melo #include <net/addrconf.h>
198129765aSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
20b9750ce1SArnaldo Carvalho de Melo #include <net/inet_ecn.h>
21b9750ce1SArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
22b9750ce1SArnaldo Carvalho de Melo #include <net/ip6_route.h>
238129765aSArnaldo Carvalho de Melo #include <net/sock.h>
249f5336e2SAdrian Bunk #include <net/inet6_connection_sock.h>
25c125e80bSCraig Gallek #include <net/sock_reuseport.h>
268129765aSArnaldo Carvalho de Melo 
inet6_csk_route_req(const struct sock * sk,struct flowi6 * fl6,const struct request_sock * req,u8 proto)2730d50c61SEric Dumazet struct dst_entry *inet6_csk_route_req(const struct sock *sk,
283840a06eSNeal Cardwell 				      struct flowi6 *fl6,
29f76b33c3SEric Dumazet 				      const struct request_sock *req,
30f76b33c3SEric Dumazet 				      u8 proto)
31ae4694b2SDavid S. Miller {
32634fb979SEric Dumazet 	struct inet_request_sock *ireq = inet_rsk(req);
33f76b33c3SEric Dumazet 	const struct ipv6_pinfo *np = inet6_sk(sk);
34ae4694b2SDavid S. Miller 	struct in6_addr *final_p, final;
35ae4694b2SDavid S. Miller 	struct dst_entry *dst;
36ae4694b2SDavid S. Miller 
373840a06eSNeal Cardwell 	memset(fl6, 0, sizeof(*fl6));
38f76b33c3SEric Dumazet 	fl6->flowi6_proto = proto;
39634fb979SEric Dumazet 	fl6->daddr = ireq->ir_v6_rmt_addr;
4045f6fad8SEric Dumazet 	rcu_read_lock();
4145f6fad8SEric Dumazet 	final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
4245f6fad8SEric Dumazet 	rcu_read_unlock();
43634fb979SEric Dumazet 	fl6->saddr = ireq->ir_v6_loc_addr;
44634fb979SEric Dumazet 	fl6->flowi6_oif = ireq->ir_iif;
4584f39b08SLorenzo Colitti 	fl6->flowi6_mark = ireq->ir_mark;
46634fb979SEric Dumazet 	fl6->fl6_dport = ireq->ir_rmt_port;
47b44084c2SEric Dumazet 	fl6->fl6_sport = htons(ireq->ir_num);
48e2d118a1SLorenzo Colitti 	fl6->flowi6_uid = sk->sk_uid;
493df98d79SPaul Moore 	security_req_classify_flow(req, flowi6_to_flowi_common(fl6));
50ae4694b2SDavid S. Miller 
51c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
5268d0c6d3SDavid S. Miller 	if (IS_ERR(dst))
53ae4694b2SDavid S. Miller 		return NULL;
54ae4694b2SDavid S. Miller 
55ae4694b2SDavid S. Miller 	return dst;
56ae4694b2SDavid S. Miller }
57f76b33c3SEric Dumazet EXPORT_SYMBOL(inet6_csk_route_req);
58ae4694b2SDavid S. Miller 
inet6_csk_addr2sockaddr(struct sock * sk,struct sockaddr * uaddr)59b9750ce1SArnaldo Carvalho de Melo void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
60b9750ce1SArnaldo Carvalho de Melo {
61b9750ce1SArnaldo Carvalho de Melo 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
62b9750ce1SArnaldo Carvalho de Melo 
63b9750ce1SArnaldo Carvalho de Melo 	sin6->sin6_family = AF_INET6;
64efe4208fSEric Dumazet 	sin6->sin6_addr = sk->sk_v6_daddr;
65c720c7e8SEric Dumazet 	sin6->sin6_port	= inet_sk(sk)->inet_dport;
66b9750ce1SArnaldo Carvalho de Melo 	/* We do not store received flowlabel for TCP */
67b9750ce1SArnaldo Carvalho de Melo 	sin6->sin6_flowinfo = 0;
68842df073SHannes Frederic Sowa 	sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
69842df073SHannes Frederic Sowa 						  sk->sk_bound_dev_if);
70b9750ce1SArnaldo Carvalho de Melo }
71b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
72b9750ce1SArnaldo Carvalho de Melo 
73a47ed4cdSNoriaki TAKAMIYA static inline
__inet6_csk_dst_check(struct sock * sk,u32 cookie)74a47ed4cdSNoriaki TAKAMIYA struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)
75a47ed4cdSNoriaki TAKAMIYA {
766f3118b5SNicolas Dichtel 	return __sk_dst_check(sk, cookie);
77a47ed4cdSNoriaki TAKAMIYA }
78a47ed4cdSNoriaki TAKAMIYA 
inet6_csk_route_socket(struct sock * sk,struct flowi6 * fl6)79d3818c92SEric Dumazet static struct dst_entry *inet6_csk_route_socket(struct sock *sk,
80d3818c92SEric Dumazet 						struct flowi6 *fl6)
81b9750ce1SArnaldo Carvalho de Melo {
82b9750ce1SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
83b9750ce1SArnaldo Carvalho de Melo 	struct ipv6_pinfo *np = inet6_sk(sk);
8420c59de2SArnaud Ebalard 	struct in6_addr *final_p, final;
8535ad9b9cSDavid S. Miller 	struct dst_entry *dst;
86b9750ce1SArnaldo Carvalho de Melo 
87d3818c92SEric Dumazet 	memset(fl6, 0, sizeof(*fl6));
88d3818c92SEric Dumazet 	fl6->flowi6_proto = sk->sk_protocol;
89efe4208fSEric Dumazet 	fl6->daddr = sk->sk_v6_daddr;
90d3818c92SEric Dumazet 	fl6->saddr = np->saddr;
91d3818c92SEric Dumazet 	fl6->flowlabel = np->flow_label;
92d3818c92SEric Dumazet 	IP6_ECN_flow_xmit(sk, fl6->flowlabel);
93d3818c92SEric Dumazet 	fl6->flowi6_oif = sk->sk_bound_dev_if;
94d3818c92SEric Dumazet 	fl6->flowi6_mark = sk->sk_mark;
95d3818c92SEric Dumazet 	fl6->fl6_sport = inet->inet_sport;
96d3818c92SEric Dumazet 	fl6->fl6_dport = inet->inet_dport;
97e2d118a1SLorenzo Colitti 	fl6->flowi6_uid = sk->sk_uid;
983df98d79SPaul Moore 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
99b9750ce1SArnaldo Carvalho de Melo 
10045f6fad8SEric Dumazet 	rcu_read_lock();
10145f6fad8SEric Dumazet 	final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
10245f6fad8SEric Dumazet 	rcu_read_unlock();
103b9750ce1SArnaldo Carvalho de Melo 
104a47ed4cdSNoriaki TAKAMIYA 	dst = __inet6_csk_dst_check(sk, np->dst_cookie);
10535ad9b9cSDavid S. Miller 	if (!dst) {
106c4e85f73SSabrina Dubroca 		dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
107b9750ce1SArnaldo Carvalho de Melo 
10835ad9b9cSDavid S. Miller 		if (!IS_ERR(dst))
1096bd4f355SEric Dumazet 			ip6_dst_store(sk, dst, NULL, NULL);
11035ad9b9cSDavid S. Miller 	}
11135ad9b9cSDavid S. Miller 	return dst;
11235ad9b9cSDavid S. Miller }
11335ad9b9cSDavid S. Miller 
inet6_csk_xmit(struct sock * sk,struct sk_buff * skb,struct flowi * fl_unused)114b0270e91SEric Dumazet int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl_unused)
11535ad9b9cSDavid S. Miller {
11635ad9b9cSDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
11735ad9b9cSDavid S. Miller 	struct flowi6 fl6;
11835ad9b9cSDavid S. Miller 	struct dst_entry *dst;
11935ad9b9cSDavid S. Miller 	int res;
12035ad9b9cSDavid S. Miller 
121d3818c92SEric Dumazet 	dst = inet6_csk_route_socket(sk, &fl6);
12268d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
123*2f2d9972SEric Dumazet 		WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst));
124b9750ce1SArnaldo Carvalho de Melo 		sk->sk_route_caps = 0;
125b0013fd4SAlexey Kuznetsov 		kfree_skb(skb);
12668d0c6d3SDavid S. Miller 		return PTR_ERR(dst);
127b9750ce1SArnaldo Carvalho de Melo 	}
128b9750ce1SArnaldo Carvalho de Melo 
129d14730b8SEric Dumazet 	rcu_read_lock();
130d14730b8SEric Dumazet 	skb_dst_set_noref(skb, dst);
131b9750ce1SArnaldo Carvalho de Melo 
132b9750ce1SArnaldo Carvalho de Melo 	/* Restore final destination back after routing done */
133efe4208fSEric Dumazet 	fl6.daddr = sk->sk_v6_daddr;
134b9750ce1SArnaldo Carvalho de Melo 
13592e55f41SPablo Neira 	res = ip6_xmit(sk, skb, &fl6, sk->sk_mark, rcu_dereference(np->opt),
1364f6570d7SEric Dumazet 		       np->tclass,  sk->sk_priority);
137d14730b8SEric Dumazet 	rcu_read_unlock();
138d14730b8SEric Dumazet 	return res;
139b9750ce1SArnaldo Carvalho de Melo }
140b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_csk_xmit);
14135ad9b9cSDavid S. Miller 
inet6_csk_update_pmtu(struct sock * sk,u32 mtu)14235ad9b9cSDavid S. Miller struct dst_entry *inet6_csk_update_pmtu(struct sock *sk, u32 mtu)
14335ad9b9cSDavid S. Miller {
144d3818c92SEric Dumazet 	struct flowi6 fl6;
145d3818c92SEric Dumazet 	struct dst_entry *dst = inet6_csk_route_socket(sk, &fl6);
14635ad9b9cSDavid S. Miller 
14735ad9b9cSDavid S. Miller 	if (IS_ERR(dst))
14835ad9b9cSDavid S. Miller 		return NULL;
149bd085ef6SHangbin Liu 	dst->ops->update_pmtu(dst, sk, NULL, mtu, true);
15035ad9b9cSDavid S. Miller 
151b4dd0067SEric Dumazet 	dst = inet6_csk_route_socket(sk, &fl6);
152b4dd0067SEric Dumazet 	return IS_ERR(dst) ? NULL : dst;
15335ad9b9cSDavid S. Miller }
15435ad9b9cSDavid S. Miller EXPORT_SYMBOL_GPL(inet6_csk_update_pmtu);
155