xref: /linux/net/ipv6/inet6_hashtables.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Generic INET6 transport hashtables
8  *
9  * Authors:	Lotsa people, from code originally in tcp, generalised here
10  *		by Arnaldo Carvalho de Melo <acme@mandriva.com>
11  */
12 
13 #include <linux/module.h>
14 #include <linux/random.h>
15 
16 #include <net/addrconf.h>
17 #include <net/hotdata.h>
18 #include <net/inet_connection_sock.h>
19 #include <net/inet_hashtables.h>
20 #include <net/inet6_hashtables.h>
21 #include <net/secure_seq.h>
22 #include <net/ip.h>
23 #include <net/sock_reuseport.h>
24 #include <net/tcp.h>
25 
26 void inet6_init_ehash_secret(void)
27 {
28 	net_get_random_sleepable_once(&inet6_ehash_secret,
29 				      sizeof(inet6_ehash_secret));
30 	net_get_random_sleepable_once(&tcp_ipv6_hash_secret,
31 				      sizeof(tcp_ipv6_hash_secret));
32 }
33 
34 u32 inet6_ehashfn(const struct net *net,
35 		  const struct in6_addr *laddr, const u16 lport,
36 		  const struct in6_addr *faddr, const __be16 fport)
37 {
38 	u32 a, b, c;
39 
40 	/*
41 	 * Please look at jhash() implementation for reference.
42 	 * Hash laddr + faddr + lport/fport + net_hash_mix.
43 	 * Notes:
44 	 * We combine laddr[0] (high order 32 bits of local address)
45 	 * with net_hash_mix() to hash a multiple of 3 words.
46 	 *
47 	 * We do not include JHASH_INITVAL + 36 contribution
48 	 * to initial values of a, b, c.
49 	 */
50 
51 	a = b = c = tcp_ipv6_hash_secret;
52 
53 	a += (__force u32)laddr->s6_addr32[0] ^ net_hash_mix(net);
54 	b += (__force u32)laddr->s6_addr32[1];
55 	c += (__force u32)laddr->s6_addr32[2];
56 	__jhash_mix(a, b, c);
57 
58 	a += (__force u32)laddr->s6_addr32[3];
59 	b += (__force u32)faddr->s6_addr32[0];
60 	c += (__force u32)faddr->s6_addr32[1];
61 	__jhash_mix(a, b, c);
62 
63 	a += (__force u32)faddr->s6_addr32[2];
64 	b += (__force u32)faddr->s6_addr32[3];
65 	c += (__force u32)fport;
66 	__jhash_final(a, b, c);
67 
68 	/* Note: We need to add @lport instead of fully hashing it.
69 	 * See commits 9544d60a2605 ("inet: change lport contribution
70 	 * to inet_ehashfn() and inet6_ehashfn()") and d4438ce68bf1
71 	 * ("inet: call inet6_ehashfn() once from inet6_hash_connect()")
72 	 * for references.
73 	 */
74 	return lport + c;
75 }
76 EXPORT_SYMBOL_GPL(inet6_ehashfn);
77 
78 /*
79  * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
80  * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
81  *
82  * The sockhash lock must be held as a reader here.
83  */
84 struct sock *__inet6_lookup_established(const struct net *net,
85 					const struct in6_addr *saddr,
86 					const __be16 sport,
87 					const struct in6_addr *daddr,
88 					const u16 hnum,
89 					const int dif, const int sdif)
90 {
91 	const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
92 	const struct hlist_nulls_node *node;
93 	struct inet_ehash_bucket *head;
94 	struct inet_hashinfo *hashinfo;
95 	unsigned int hash, slot;
96 	struct sock *sk;
97 
98 	hashinfo = net->ipv4.tcp_death_row.hashinfo;
99 	hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
100 	slot = hash & hashinfo->ehash_mask;
101 	head = &hashinfo->ehash[slot];
102 begin:
103 	sk_nulls_for_each_rcu(sk, node, &head->chain) {
104 		if (sk->sk_hash != hash)
105 			continue;
106 		if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
107 			continue;
108 		if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
109 			goto out;
110 
111 		if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) ||
112 			     !inet6_match(net, sk, saddr, daddr, ports, dif,
113 					  sdif))) {
114 			sock_gen_put(sk);
115 			goto begin;
116 		}
117 		goto found;
118 	}
119 	if (get_nulls_value(node) != slot)
120 		goto begin;
121 out:
122 	sk = NULL;
123 found:
124 	return sk;
125 }
126 EXPORT_SYMBOL(__inet6_lookup_established);
127 
128 static inline int compute_score(struct sock *sk, const struct net *net,
129 				const unsigned short hnum,
130 				const struct in6_addr *daddr,
131 				const int dif, const int sdif)
132 {
133 	int score = -1;
134 
135 	if (net_eq(sock_net(sk), net) &&
136 	    READ_ONCE(inet_sk(sk)->inet_num) == hnum &&
137 	    sk->sk_family == PF_INET6) {
138 		if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
139 			return -1;
140 
141 		if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
142 			return -1;
143 
144 		score =  sk->sk_bound_dev_if ? 2 : 1;
145 		if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
146 			score++;
147 	}
148 	return score;
149 }
150 
151 /**
152  * inet6_lookup_reuseport() - execute reuseport logic on AF_INET6 socket if necessary.
153  * @net: network namespace.
154  * @sk: AF_INET6 socket, must be in TCP_LISTEN state for TCP or TCP_CLOSE for UDP.
155  * @skb: context for a potential SK_REUSEPORT program.
156  * @doff: header offset.
157  * @saddr: source address.
158  * @sport: source port.
159  * @daddr: destination address.
160  * @hnum: destination port in host byte order.
161  * @ehashfn: hash function used to generate the fallback hash.
162  *
163  * Return: NULL if sk doesn't have SO_REUSEPORT set, otherwise a pointer to
164  *         the selected sock or an error.
165  */
166 struct sock *inet6_lookup_reuseport(const struct net *net, struct sock *sk,
167 				    struct sk_buff *skb, int doff,
168 				    const struct in6_addr *saddr,
169 				    __be16 sport,
170 				    const struct in6_addr *daddr,
171 				    unsigned short hnum,
172 				    inet6_ehashfn_t *ehashfn)
173 {
174 	struct sock *reuse_sk = NULL;
175 	u32 phash;
176 
177 	if (sk->sk_reuseport) {
178 		phash = INDIRECT_CALL_INET(ehashfn, udp6_ehashfn, inet6_ehashfn,
179 					   net, daddr, hnum, saddr, sport);
180 		reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
181 	}
182 	return reuse_sk;
183 }
184 EXPORT_SYMBOL_GPL(inet6_lookup_reuseport);
185 
186 /* called with rcu_read_lock() */
187 static struct sock *inet6_lhash2_lookup(const struct net *net,
188 		struct inet_listen_hashbucket *ilb2,
189 		struct sk_buff *skb, int doff,
190 		const struct in6_addr *saddr,
191 		const __be16 sport, const struct in6_addr *daddr,
192 		const unsigned short hnum, const int dif, const int sdif)
193 {
194 	struct sock *sk, *result = NULL;
195 	struct hlist_nulls_node *node;
196 	int score, hiscore = 0;
197 
198 	sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
199 		score = compute_score(sk, net, hnum, daddr, dif, sdif);
200 		if (score > hiscore) {
201 			result = inet6_lookup_reuseport(net, sk, skb, doff,
202 							saddr, sport, daddr, hnum, inet6_ehashfn);
203 			if (result)
204 				return result;
205 
206 			result = sk;
207 			hiscore = score;
208 		}
209 	}
210 
211 	return result;
212 }
213 
214 struct sock *inet6_lookup_run_sk_lookup(const struct net *net,
215 					int protocol,
216 					struct sk_buff *skb, int doff,
217 					const struct in6_addr *saddr,
218 					const __be16 sport,
219 					const struct in6_addr *daddr,
220 					const u16 hnum, const int dif,
221 					inet6_ehashfn_t *ehashfn)
222 {
223 	struct sock *sk, *reuse_sk;
224 	bool no_reuseport;
225 
226 	no_reuseport = bpf_sk_lookup_run_v6(net, protocol, saddr, sport,
227 					    daddr, hnum, dif, &sk);
228 	if (no_reuseport || IS_ERR_OR_NULL(sk))
229 		return sk;
230 
231 	reuse_sk = inet6_lookup_reuseport(net, sk, skb, doff,
232 					  saddr, sport, daddr, hnum, ehashfn);
233 	if (reuse_sk)
234 		sk = reuse_sk;
235 	return sk;
236 }
237 EXPORT_SYMBOL_GPL(inet6_lookup_run_sk_lookup);
238 
239 struct sock *inet6_lookup_listener(const struct net *net,
240 				   struct sk_buff *skb, int doff,
241 				   const struct in6_addr *saddr,
242 				   const __be16 sport,
243 				   const struct in6_addr *daddr,
244 				   const unsigned short hnum,
245 				   const int dif, const int sdif)
246 {
247 	struct inet_listen_hashbucket *ilb2;
248 	struct inet_hashinfo *hashinfo;
249 	struct sock *result = NULL;
250 	unsigned int hash2;
251 
252 	/* Lookup redirect from BPF */
253 	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
254 		result = inet6_lookup_run_sk_lookup(net, IPPROTO_TCP, skb, doff,
255 						    saddr, sport, daddr, hnum, dif,
256 						    inet6_ehashfn);
257 		if (result)
258 			goto done;
259 	}
260 
261 	hashinfo = net->ipv4.tcp_death_row.hashinfo;
262 	hash2 = ipv6_portaddr_hash(net, daddr, hnum);
263 	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
264 
265 	result = inet6_lhash2_lookup(net, ilb2, skb, doff,
266 				     saddr, sport, daddr, hnum,
267 				     dif, sdif);
268 	if (result)
269 		goto done;
270 
271 	/* Lookup lhash2 with in6addr_any */
272 	hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
273 	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
274 
275 	result = inet6_lhash2_lookup(net, ilb2, skb, doff,
276 				     saddr, sport, &in6addr_any, hnum,
277 				     dif, sdif);
278 done:
279 	if (IS_ERR(result))
280 		return NULL;
281 	return result;
282 }
283 EXPORT_SYMBOL_GPL(inet6_lookup_listener);
284 
285 struct sock *inet6_lookup(const struct net *net,
286 			  struct sk_buff *skb, int doff,
287 			  const struct in6_addr *saddr, const __be16 sport,
288 			  const struct in6_addr *daddr, const __be16 dport,
289 			  const int dif)
290 {
291 	struct sock *sk;
292 	bool refcounted;
293 
294 	sk = __inet6_lookup(net, skb, doff, saddr, sport, daddr,
295 			    ntohs(dport), dif, 0, &refcounted);
296 	if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
297 		sk = NULL;
298 	return sk;
299 }
300 EXPORT_SYMBOL_GPL(inet6_lookup);
301 
302 static int __inet6_check_established(struct inet_timewait_death_row *death_row,
303 				     struct sock *sk, const __u16 lport,
304 				     struct inet_timewait_sock **twp,
305 				     bool rcu_lookup,
306 				     u32 hash)
307 {
308 	struct inet_hashinfo *hinfo = death_row->hashinfo;
309 	struct inet_sock *inet = inet_sk(sk);
310 	const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
311 	const struct in6_addr *saddr = &sk->sk_v6_daddr;
312 	const int dif = sk->sk_bound_dev_if;
313 	struct net *net = sock_net(sk);
314 	const int sdif = l3mdev_master_ifindex_by_index(net, dif);
315 	const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
316 	struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
317 	struct inet_timewait_sock *tw = NULL;
318 	const struct hlist_nulls_node *node;
319 	struct sock *sk2;
320 	spinlock_t *lock;
321 
322 	if (rcu_lookup) {
323 		sk_nulls_for_each(sk2, node, &head->chain) {
324 			if (sk2->sk_hash != hash ||
325 			    !inet6_match(net, sk2, saddr, daddr,
326 					 ports, dif, sdif))
327 				continue;
328 			if (sk2->sk_state == TCP_TIME_WAIT)
329 				break;
330 			return -EADDRNOTAVAIL;
331 		}
332 		return 0;
333 	}
334 
335 	lock = inet_ehash_lockp(hinfo, hash);
336 	spin_lock(lock);
337 
338 	sk_nulls_for_each(sk2, node, &head->chain) {
339 		if (sk2->sk_hash != hash)
340 			continue;
341 
342 		if (likely(inet6_match(net, sk2, saddr, daddr, ports,
343 				       dif, sdif))) {
344 			if (sk2->sk_state == TCP_TIME_WAIT) {
345 				tw = inet_twsk(sk2);
346 				if (tcp_twsk_unique(sk, sk2, twp))
347 					break;
348 			}
349 			goto not_unique;
350 		}
351 	}
352 
353 	/* Must record num and sport now. Otherwise we will see
354 	 * in hash table socket with a funny identity.
355 	 */
356 	inet->inet_num = lport;
357 	inet->inet_sport = htons(lport);
358 	sk->sk_hash = hash;
359 	WARN_ON(!sk_unhashed(sk));
360 	__sk_nulls_add_node_rcu(sk, &head->chain);
361 	if (tw) {
362 		sk_nulls_del_node_init_rcu((struct sock *)tw);
363 		__NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
364 	}
365 	spin_unlock(lock);
366 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
367 
368 	if (twp) {
369 		*twp = tw;
370 	} else if (tw) {
371 		/* Silly. Should hash-dance instead... */
372 		inet_twsk_deschedule_put(tw);
373 	}
374 	return 0;
375 
376 not_unique:
377 	spin_unlock(lock);
378 	return -EADDRNOTAVAIL;
379 }
380 
381 static u64 inet6_sk_port_offset(const struct sock *sk)
382 {
383 	const struct inet_sock *inet = inet_sk(sk);
384 
385 	return secure_ipv6_port_ephemeral(sk->sk_v6_rcv_saddr.s6_addr32,
386 					  sk->sk_v6_daddr.s6_addr32,
387 					  inet->inet_dport);
388 }
389 
390 int inet6_hash_connect(struct inet_timewait_death_row *death_row,
391 		       struct sock *sk)
392 {
393 	const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
394 	const struct in6_addr *saddr = &sk->sk_v6_daddr;
395 	const struct inet_sock *inet = inet_sk(sk);
396 	const struct net *net = sock_net(sk);
397 	u64 port_offset = 0;
398 	u32 hash_port0;
399 
400 	if (!inet_sk(sk)->inet_num)
401 		port_offset = inet6_sk_port_offset(sk);
402 
403 	inet6_init_ehash_secret();
404 
405 	hash_port0 = inet6_ehashfn(net, daddr, 0, saddr, inet->inet_dport);
406 
407 	return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
408 				   __inet6_check_established);
409 }
410 EXPORT_SYMBOL_GPL(inet6_hash_connect);
411