• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *		Support for INET connection oriented protocols.
8  *
9  * Authors:	See the TCP sources
10  */
11 
12 #include <linux/module.h>
13 #include <linux/jhash.h>
14 
15 #include <net/inet_connection_sock.h>
16 #include <net/inet_hashtables.h>
17 #include <net/inet_timewait_sock.h>
18 #include <net/ip.h>
19 #include <net/route.h>
20 #include <net/tcp_states.h>
21 #include <net/xfrm.h>
22 #include <net/tcp.h>
23 #include <net/sock_reuseport.h>
24 #include <net/addrconf.h>
25 #if IS_ENABLED(CONFIG_NEWIP)
26 #include <net/nip.h>
27 #endif
28 
29 #if IS_ENABLED(CONFIG_IPV6)
30 /* match_sk*_wildcard == true:  IPV6_ADDR_ANY equals to any IPv6 addresses
31  *				if IPv6 only, and any IPv4 addresses
32  *				if not IPv6 only
33  * match_sk*_wildcard == false: addresses must be exactly the same, i.e.
34  *				IPV6_ADDR_ANY only equals to IPV6_ADDR_ANY,
35  *				and 0.0.0.0 equals to 0.0.0.0 only
36  */
ipv6_rcv_saddr_equal(const struct in6_addr * sk1_rcv_saddr6,const struct in6_addr * sk2_rcv_saddr6,__be32 sk1_rcv_saddr,__be32 sk2_rcv_saddr,bool sk1_ipv6only,bool sk2_ipv6only,bool match_sk1_wildcard,bool match_sk2_wildcard)37 static bool ipv6_rcv_saddr_equal(const struct in6_addr *sk1_rcv_saddr6,
38 				 const struct in6_addr *sk2_rcv_saddr6,
39 				 __be32 sk1_rcv_saddr, __be32 sk2_rcv_saddr,
40 				 bool sk1_ipv6only, bool sk2_ipv6only,
41 				 bool match_sk1_wildcard,
42 				 bool match_sk2_wildcard)
43 {
44 	int addr_type = ipv6_addr_type(sk1_rcv_saddr6);
45 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
46 
47 	/* if both are mapped, treat as IPv4 */
48 	if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) {
49 		if (!sk2_ipv6only) {
50 			if (sk1_rcv_saddr == sk2_rcv_saddr)
51 				return true;
52 			return (match_sk1_wildcard && !sk1_rcv_saddr) ||
53 				(match_sk2_wildcard && !sk2_rcv_saddr);
54 		}
55 		return false;
56 	}
57 
58 	if (addr_type == IPV6_ADDR_ANY && addr_type2 == IPV6_ADDR_ANY)
59 		return true;
60 
61 	if (addr_type2 == IPV6_ADDR_ANY && match_sk2_wildcard &&
62 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
63 		return true;
64 
65 	if (addr_type == IPV6_ADDR_ANY && match_sk1_wildcard &&
66 	    !(sk1_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
67 		return true;
68 
69 	if (sk2_rcv_saddr6 &&
70 	    ipv6_addr_equal(sk1_rcv_saddr6, sk2_rcv_saddr6))
71 		return true;
72 
73 	return false;
74 }
75 #endif
76 
77 /* match_sk*_wildcard == true:  0.0.0.0 equals to any IPv4 addresses
78  * match_sk*_wildcard == false: addresses must be exactly the same, i.e.
79  *				0.0.0.0 only equals to 0.0.0.0
80  */
ipv4_rcv_saddr_equal(__be32 sk1_rcv_saddr,__be32 sk2_rcv_saddr,bool sk2_ipv6only,bool match_sk1_wildcard,bool match_sk2_wildcard)81 static bool ipv4_rcv_saddr_equal(__be32 sk1_rcv_saddr, __be32 sk2_rcv_saddr,
82 				 bool sk2_ipv6only, bool match_sk1_wildcard,
83 				 bool match_sk2_wildcard)
84 {
85 	if (!sk2_ipv6only) {
86 		if (sk1_rcv_saddr == sk2_rcv_saddr)
87 			return true;
88 		return (match_sk1_wildcard && !sk1_rcv_saddr) ||
89 			(match_sk2_wildcard && !sk2_rcv_saddr);
90 	}
91 	return false;
92 }
93 
inet_rcv_saddr_equal(const struct sock * sk,const struct sock * sk2,bool match_wildcard)94 bool inet_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2,
95 			  bool match_wildcard)
96 {
97 #if IS_ENABLED(CONFIG_IPV6)
98 	if (sk->sk_family == AF_INET6)
99 		return ipv6_rcv_saddr_equal(&sk->sk_v6_rcv_saddr,
100 					    inet6_rcv_saddr(sk2),
101 					    sk->sk_rcv_saddr,
102 					    sk2->sk_rcv_saddr,
103 					    ipv6_only_sock(sk),
104 					    ipv6_only_sock(sk2),
105 					    match_wildcard,
106 					    match_wildcard);
107 #endif
108 
109 #if IS_ENABLED(CONFIG_NEWIP)
110 	if (sk->sk_family == AF_NINET)
111 		return nip_rcv_saddr_equal(&sk->sk_nip_rcv_saddr,
112 					   &sk2->sk_nip_rcv_saddr,
113 					   sk2->sk_family == AF_NINET,
114 					   match_wildcard,
115 					   match_wildcard);
116 #endif
117 
118 	return ipv4_rcv_saddr_equal(sk->sk_rcv_saddr, sk2->sk_rcv_saddr,
119 				    ipv6_only_sock(sk2), match_wildcard,
120 				    match_wildcard);
121 }
122 EXPORT_SYMBOL(inet_rcv_saddr_equal);
123 
inet_rcv_saddr_any(const struct sock * sk)124 bool inet_rcv_saddr_any(const struct sock *sk)
125 {
126 #if IS_ENABLED(CONFIG_IPV6)
127 	if (sk->sk_family == AF_INET6)
128 		return ipv6_addr_any(&sk->sk_v6_rcv_saddr);
129 #endif
130 	return !sk->sk_rcv_saddr;
131 }
132 
inet_get_local_port_range(struct net * net,int * low,int * high)133 void inet_get_local_port_range(struct net *net, int *low, int *high)
134 {
135 	unsigned int seq;
136 
137 	do {
138 		seq = read_seqbegin(&net->ipv4.ip_local_ports.lock);
139 
140 		*low = net->ipv4.ip_local_ports.range[0];
141 		*high = net->ipv4.ip_local_ports.range[1];
142 	} while (read_seqretry(&net->ipv4.ip_local_ports.lock, seq));
143 }
144 EXPORT_SYMBOL(inet_get_local_port_range);
145 
inet_csk_bind_conflict(const struct sock * sk,const struct inet_bind_bucket * tb,bool relax,bool reuseport_ok)146 static int inet_csk_bind_conflict(const struct sock *sk,
147 				  const struct inet_bind_bucket *tb,
148 				  bool relax, bool reuseport_ok)
149 {
150 	struct sock *sk2;
151 	bool reuse = sk->sk_reuse;
152 	bool reuseport = !!sk->sk_reuseport;
153 	kuid_t uid = sock_i_uid((struct sock *)sk);
154 
155 	/*
156 	 * Unlike other sk lookup places we do not check
157 	 * for sk_net here, since _all_ the socks listed
158 	 * in tb->owners list belong to the same net - the
159 	 * one this bucket belongs to.
160 	 */
161 
162 	sk_for_each_bound(sk2, &tb->owners) {
163 		if (sk != sk2 &&
164 		    (!sk->sk_bound_dev_if ||
165 		     !sk2->sk_bound_dev_if ||
166 		     sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
167 			if (reuse && sk2->sk_reuse &&
168 			    sk2->sk_state != TCP_LISTEN) {
169 				if ((!relax ||
170 				     (!reuseport_ok &&
171 				      reuseport && sk2->sk_reuseport &&
172 				      !rcu_access_pointer(sk->sk_reuseport_cb) &&
173 				      (sk2->sk_state == TCP_TIME_WAIT ||
174 				       uid_eq(uid, sock_i_uid(sk2))))) &&
175 				    inet_rcv_saddr_equal(sk, sk2, true))
176 					break;
177 			} else if (!reuseport_ok ||
178 				   !reuseport || !sk2->sk_reuseport ||
179 				   rcu_access_pointer(sk->sk_reuseport_cb) ||
180 				   (sk2->sk_state != TCP_TIME_WAIT &&
181 				    !uid_eq(uid, sock_i_uid(sk2)))) {
182 				if (inet_rcv_saddr_equal(sk, sk2, true))
183 					break;
184 			}
185 		}
186 	}
187 	return sk2 != NULL;
188 }
189 
190 /*
191  * Find an open port number for the socket.  Returns with the
192  * inet_bind_hashbucket lock held.
193  */
194 static struct inet_bind_hashbucket *
inet_csk_find_open_port(struct sock * sk,struct inet_bind_bucket ** tb_ret,int * port_ret)195 inet_csk_find_open_port(struct sock *sk, struct inet_bind_bucket **tb_ret, int *port_ret)
196 {
197 	struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo;
198 	int port = 0;
199 	struct inet_bind_hashbucket *head;
200 	struct net *net = sock_net(sk);
201 	bool relax = false;
202 	int i, low, high, attempt_half;
203 	struct inet_bind_bucket *tb;
204 	u32 remaining, offset;
205 	int l3mdev;
206 
207 	l3mdev = inet_sk_bound_l3mdev(sk);
208 ports_exhausted:
209 	attempt_half = (sk->sk_reuse == SK_CAN_REUSE) ? 1 : 0;
210 other_half_scan:
211 	inet_get_local_port_range(net, &low, &high);
212 	high++; /* [32768, 60999] -> [32768, 61000[ */
213 	if (high - low < 4)
214 		attempt_half = 0;
215 	if (attempt_half) {
216 		int half = low + (((high - low) >> 2) << 1);
217 
218 		if (attempt_half == 1)
219 			high = half;
220 		else
221 			low = half;
222 	}
223 	remaining = high - low;
224 	if (likely(remaining > 1))
225 		remaining &= ~1U;
226 
227 	offset = prandom_u32() % remaining;
228 	/* __inet_hash_connect() favors ports having @low parity
229 	 * We do the opposite to not pollute connect() users.
230 	 */
231 	offset |= 1U;
232 
233 other_parity_scan:
234 	port = low + offset;
235 	for (i = 0; i < remaining; i += 2, port += 2) {
236 		if (unlikely(port >= high))
237 			port -= remaining;
238 		if (inet_is_local_reserved_port(net, port))
239 			continue;
240 		head = &hinfo->bhash[inet_bhashfn(net, port,
241 						  hinfo->bhash_size)];
242 		spin_lock_bh(&head->lock);
243 		inet_bind_bucket_for_each(tb, &head->chain)
244 			if (net_eq(ib_net(tb), net) && tb->l3mdev == l3mdev &&
245 			    tb->port == port) {
246 				if (!inet_csk_bind_conflict(sk, tb, relax, false))
247 					goto success;
248 				goto next_port;
249 			}
250 		tb = NULL;
251 		goto success;
252 next_port:
253 		spin_unlock_bh(&head->lock);
254 		cond_resched();
255 	}
256 
257 	offset--;
258 	if (!(offset & 1))
259 		goto other_parity_scan;
260 
261 	if (attempt_half == 1) {
262 		/* OK we now try the upper half of the range */
263 		attempt_half = 2;
264 		goto other_half_scan;
265 	}
266 
267 	if (net->ipv4.sysctl_ip_autobind_reuse && !relax) {
268 		/* We still have a chance to connect to different destinations */
269 		relax = true;
270 		goto ports_exhausted;
271 	}
272 	return NULL;
273 success:
274 	*port_ret = port;
275 	*tb_ret = tb;
276 	return head;
277 }
278 
sk_reuseport_match(struct inet_bind_bucket * tb,struct sock * sk)279 static inline int sk_reuseport_match(struct inet_bind_bucket *tb,
280 				     struct sock *sk)
281 {
282 	kuid_t uid = sock_i_uid(sk);
283 
284 	if (tb->fastreuseport <= 0)
285 		return 0;
286 	if (!sk->sk_reuseport)
287 		return 0;
288 	if (rcu_access_pointer(sk->sk_reuseport_cb))
289 		return 0;
290 	if (!uid_eq(tb->fastuid, uid))
291 		return 0;
292 	/* We only need to check the rcv_saddr if this tb was once marked
293 	 * without fastreuseport and then was reset, as we can only know that
294 	 * the fast_*rcv_saddr doesn't have any conflicts with the socks on the
295 	 * owners list.
296 	 */
297 	if (tb->fastreuseport == FASTREUSEPORT_ANY)
298 		return 1;
299 #if IS_ENABLED(CONFIG_IPV6)
300 	if (tb->fast_sk_family == AF_INET6)
301 		return ipv6_rcv_saddr_equal(&tb->fast_v6_rcv_saddr,
302 					    inet6_rcv_saddr(sk),
303 					    tb->fast_rcv_saddr,
304 					    sk->sk_rcv_saddr,
305 					    tb->fast_ipv6_only,
306 					    ipv6_only_sock(sk), true, false);
307 #endif
308 #if IS_ENABLED(CONFIG_NEWIP)
309 	if (tb->fast_sk_family == AF_NINET)
310 		return nip_rcv_saddr_equal(&tb->fast_nip_rcv_saddr,
311 					   &sk->sk_nip_rcv_saddr,
312 					   sk->sk_family == AF_NINET,
313 					   true, false);
314 #endif
315 	return ipv4_rcv_saddr_equal(tb->fast_rcv_saddr, sk->sk_rcv_saddr,
316 				    ipv6_only_sock(sk), true, false);
317 }
318 
inet_csk_update_fastreuse(struct inet_bind_bucket * tb,struct sock * sk)319 void inet_csk_update_fastreuse(struct inet_bind_bucket *tb,
320 			       struct sock *sk)
321 {
322 	kuid_t uid = sock_i_uid(sk);
323 	bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN;
324 
325 	if (hlist_empty(&tb->owners)) {
326 		tb->fastreuse = reuse;
327 		if (sk->sk_reuseport) {
328 			tb->fastreuseport = FASTREUSEPORT_ANY;
329 			tb->fastuid = uid;
330 			tb->fast_rcv_saddr = sk->sk_rcv_saddr;
331 			tb->fast_ipv6_only = ipv6_only_sock(sk);
332 			tb->fast_sk_family = sk->sk_family;
333 #if IS_ENABLED(CONFIG_IPV6)
334 			tb->fast_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
335 #endif
336 #if IS_ENABLED(CONFIG_NEWIP)
337 			tb->fast_nip_rcv_saddr = sk->sk_nip_rcv_saddr;
338 #endif
339 		} else {
340 			tb->fastreuseport = 0;
341 		}
342 	} else {
343 		if (!reuse)
344 			tb->fastreuse = 0;
345 		if (sk->sk_reuseport) {
346 			/* We didn't match or we don't have fastreuseport set on
347 			 * the tb, but we have sk_reuseport set on this socket
348 			 * and we know that there are no bind conflicts with
349 			 * this socket in this tb, so reset our tb's reuseport
350 			 * settings so that any subsequent sockets that match
351 			 * our current socket will be put on the fast path.
352 			 *
353 			 * If we reset we need to set FASTREUSEPORT_STRICT so we
354 			 * do extra checking for all subsequent sk_reuseport
355 			 * socks.
356 			 */
357 			if (!sk_reuseport_match(tb, sk)) {
358 				tb->fastreuseport = FASTREUSEPORT_STRICT;
359 				tb->fastuid = uid;
360 				tb->fast_rcv_saddr = sk->sk_rcv_saddr;
361 				tb->fast_ipv6_only = ipv6_only_sock(sk);
362 				tb->fast_sk_family = sk->sk_family;
363 #if IS_ENABLED(CONFIG_IPV6)
364 				tb->fast_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
365 #endif
366 #if IS_ENABLED(CONFIG_NEWIP)
367 				tb->fast_nip_rcv_saddr = sk->sk_nip_rcv_saddr;
368 #endif
369 			}
370 		} else {
371 			tb->fastreuseport = 0;
372 		}
373 	}
374 }
375 
376 /* Obtain a reference to a local port for the given sock,
377  * if snum is zero it means select any available local port.
378  * We try to allocate an odd port (and leave even ports for connect())
379  */
inet_csk_get_port(struct sock * sk,unsigned short snum)380 int inet_csk_get_port(struct sock *sk, unsigned short snum)
381 {
382 	bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN;
383 	struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo;
384 	int ret = 1, port = snum;
385 	struct inet_bind_hashbucket *head;
386 	struct net *net = sock_net(sk);
387 	struct inet_bind_bucket *tb = NULL;
388 	int l3mdev;
389 
390 	l3mdev = inet_sk_bound_l3mdev(sk);
391 
392 	if (!port) {
393 		head = inet_csk_find_open_port(sk, &tb, &port);
394 		if (!head)
395 			return ret;
396 		if (!tb)
397 			goto tb_not_found;
398 		goto success;
399 	}
400 	head = &hinfo->bhash[inet_bhashfn(net, port,
401 					  hinfo->bhash_size)];
402 	spin_lock_bh(&head->lock);
403 	inet_bind_bucket_for_each(tb, &head->chain)
404 		if (net_eq(ib_net(tb), net) && tb->l3mdev == l3mdev &&
405 		    tb->port == port)
406 			goto tb_found;
407 tb_not_found:
408 	tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
409 				     net, head, port, l3mdev);
410 	if (!tb)
411 		goto fail_unlock;
412 tb_found:
413 	if (!hlist_empty(&tb->owners)) {
414 		if (sk->sk_reuse == SK_FORCE_REUSE)
415 			goto success;
416 
417 		if ((tb->fastreuse > 0 && reuse) ||
418 		    sk_reuseport_match(tb, sk))
419 			goto success;
420 		if (inet_csk_bind_conflict(sk, tb, true, true))
421 			goto fail_unlock;
422 	}
423 success:
424 	inet_csk_update_fastreuse(tb, sk);
425 
426 	if (!inet_csk(sk)->icsk_bind_hash)
427 		inet_bind_hash(sk, tb, port);
428 	WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
429 	ret = 0;
430 
431 fail_unlock:
432 	spin_unlock_bh(&head->lock);
433 	return ret;
434 }
435 EXPORT_SYMBOL_GPL(inet_csk_get_port);
436 
437 /*
438  * Wait for an incoming connection, avoid race conditions. This must be called
439  * with the socket locked.
440  */
inet_csk_wait_for_connect(struct sock * sk,long timeo)441 static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
442 {
443 	struct inet_connection_sock *icsk = inet_csk(sk);
444 	DEFINE_WAIT(wait);
445 	int err;
446 
447 	/*
448 	 * True wake-one mechanism for incoming connections: only
449 	 * one process gets woken up, not the 'whole herd'.
450 	 * Since we do not 'race & poll' for established sockets
451 	 * anymore, the common case will execute the loop only once.
452 	 *
453 	 * Subtle issue: "add_wait_queue_exclusive()" will be added
454 	 * after any current non-exclusive waiters, and we know that
455 	 * it will always _stay_ after any new non-exclusive waiters
456 	 * because all non-exclusive waiters are added at the
457 	 * beginning of the wait-queue. As such, it's ok to "drop"
458 	 * our exclusiveness temporarily when we get woken up without
459 	 * having to remove and re-insert us on the wait queue.
460 	 */
461 	for (;;) {
462 		prepare_to_wait_exclusive(sk_sleep(sk), &wait,
463 					  TASK_INTERRUPTIBLE);
464 		release_sock(sk);
465 		if (reqsk_queue_empty(&icsk->icsk_accept_queue))
466 			timeo = schedule_timeout(timeo);
467 		sched_annotate_sleep();
468 		lock_sock(sk);
469 		err = 0;
470 		if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
471 			break;
472 		err = -EINVAL;
473 		if (sk->sk_state != TCP_LISTEN)
474 			break;
475 		err = sock_intr_errno(timeo);
476 		if (signal_pending(current))
477 			break;
478 		err = -EAGAIN;
479 		if (!timeo)
480 			break;
481 	}
482 	finish_wait(sk_sleep(sk), &wait);
483 	return err;
484 }
485 
486 /*
487  * This will accept the next outstanding connection.
488  */
inet_csk_accept(struct sock * sk,int flags,int * err,bool kern)489 struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
490 {
491 	struct inet_connection_sock *icsk = inet_csk(sk);
492 	struct request_sock_queue *queue = &icsk->icsk_accept_queue;
493 	struct request_sock *req;
494 	struct sock *newsk;
495 	int error;
496 
497 	lock_sock(sk);
498 
499 	/* We need to make sure that this socket is listening,
500 	 * and that it has something pending.
501 	 */
502 	error = -EINVAL;
503 	if (sk->sk_state != TCP_LISTEN)
504 		goto out_err;
505 
506 	/* Find already established connection */
507 	if (reqsk_queue_empty(queue)) {
508 		long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
509 
510 		/* If this is a non blocking socket don't sleep */
511 		error = -EAGAIN;
512 		if (!timeo)
513 			goto out_err;
514 
515 		error = inet_csk_wait_for_connect(sk, timeo);
516 		if (error)
517 			goto out_err;
518 	}
519 	req = reqsk_queue_remove(queue, sk);
520 	newsk = req->sk;
521 
522 	if (sk->sk_protocol == IPPROTO_TCP &&
523 	    tcp_rsk(req)->tfo_listener) {
524 		spin_lock_bh(&queue->fastopenq.lock);
525 		if (tcp_rsk(req)->tfo_listener) {
526 			/* We are still waiting for the final ACK from 3WHS
527 			 * so can't free req now. Instead, we set req->sk to
528 			 * NULL to signify that the child socket is taken
529 			 * so reqsk_fastopen_remove() will free the req
530 			 * when 3WHS finishes (or is aborted).
531 			 */
532 			req->sk = NULL;
533 			req = NULL;
534 		}
535 		spin_unlock_bh(&queue->fastopenq.lock);
536 	}
537 
538 out:
539 	release_sock(sk);
540 	if (newsk && mem_cgroup_sockets_enabled) {
541 		int amt;
542 
543 		/* atomically get the memory usage, set and charge the
544 		 * newsk->sk_memcg.
545 		 */
546 		lock_sock(newsk);
547 
548 		/* The socket has not been accepted yet, no need to look at
549 		 * newsk->sk_wmem_queued.
550 		 */
551 		amt = sk_mem_pages(newsk->sk_forward_alloc +
552 				   atomic_read(&newsk->sk_rmem_alloc));
553 		mem_cgroup_sk_alloc(newsk);
554 		if (newsk->sk_memcg && amt)
555 			mem_cgroup_charge_skmem(newsk->sk_memcg, amt);
556 
557 		release_sock(newsk);
558 	}
559 	if (req)
560 		reqsk_put(req);
561 	return newsk;
562 out_err:
563 	newsk = NULL;
564 	req = NULL;
565 	*err = error;
566 	goto out;
567 }
568 EXPORT_SYMBOL(inet_csk_accept);
569 
570 /*
571  * Using different timers for retransmit, delayed acks and probes
572  * We may wish use just one timer maintaining a list of expire jiffies
573  * to optimize.
574  */
inet_csk_init_xmit_timers(struct sock * sk,void (* retransmit_handler)(struct timer_list * t),void (* delack_handler)(struct timer_list * t),void (* keepalive_handler)(struct timer_list * t))575 void inet_csk_init_xmit_timers(struct sock *sk,
576 			       void (*retransmit_handler)(struct timer_list *t),
577 			       void (*delack_handler)(struct timer_list *t),
578 			       void (*keepalive_handler)(struct timer_list *t))
579 {
580 	struct inet_connection_sock *icsk = inet_csk(sk);
581 
582 	timer_setup(&icsk->icsk_retransmit_timer, retransmit_handler, 0);
583 	timer_setup(&icsk->icsk_delack_timer, delack_handler, 0);
584 	timer_setup(&sk->sk_timer, keepalive_handler, 0);
585 	icsk->icsk_pending = icsk->icsk_ack.pending = 0;
586 }
587 EXPORT_SYMBOL(inet_csk_init_xmit_timers);
588 
inet_csk_clear_xmit_timers(struct sock * sk)589 void inet_csk_clear_xmit_timers(struct sock *sk)
590 {
591 	struct inet_connection_sock *icsk = inet_csk(sk);
592 
593 	icsk->icsk_pending = icsk->icsk_ack.pending = 0;
594 
595 	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
596 	sk_stop_timer(sk, &icsk->icsk_delack_timer);
597 	sk_stop_timer(sk, &sk->sk_timer);
598 }
599 EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
600 
inet_csk_delete_keepalive_timer(struct sock * sk)601 void inet_csk_delete_keepalive_timer(struct sock *sk)
602 {
603 	sk_stop_timer(sk, &sk->sk_timer);
604 }
605 EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
606 
inet_csk_reset_keepalive_timer(struct sock * sk,unsigned long len)607 void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
608 {
609 	sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
610 }
611 EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
612 
inet_csk_route_req(const struct sock * sk,struct flowi4 * fl4,const struct request_sock * req)613 struct dst_entry *inet_csk_route_req(const struct sock *sk,
614 				     struct flowi4 *fl4,
615 				     const struct request_sock *req)
616 {
617 	const struct inet_request_sock *ireq = inet_rsk(req);
618 	struct net *net = read_pnet(&ireq->ireq_net);
619 	struct ip_options_rcu *opt;
620 	struct rtable *rt;
621 
622 	rcu_read_lock();
623 	opt = rcu_dereference(ireq->ireq_opt);
624 
625 	flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
626 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
627 			   sk->sk_protocol, inet_sk_flowi_flags(sk),
628 			   (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
629 			   ireq->ir_loc_addr, ireq->ir_rmt_port,
630 			   htons(ireq->ir_num), sk->sk_uid);
631 	security_req_classify_flow(req, flowi4_to_flowi(fl4));
632 	rt = ip_route_output_flow(net, fl4, sk);
633 	if (IS_ERR(rt))
634 		goto no_route;
635 	if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
636 		goto route_err;
637 	rcu_read_unlock();
638 	return &rt->dst;
639 
640 route_err:
641 	ip_rt_put(rt);
642 no_route:
643 	rcu_read_unlock();
644 	__IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
645 	return NULL;
646 }
647 EXPORT_SYMBOL_GPL(inet_csk_route_req);
648 
inet_csk_route_child_sock(const struct sock * sk,struct sock * newsk,const struct request_sock * req)649 struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
650 					    struct sock *newsk,
651 					    const struct request_sock *req)
652 {
653 	const struct inet_request_sock *ireq = inet_rsk(req);
654 	struct net *net = read_pnet(&ireq->ireq_net);
655 	struct inet_sock *newinet = inet_sk(newsk);
656 	struct ip_options_rcu *opt;
657 	struct flowi4 *fl4;
658 	struct rtable *rt;
659 
660 	opt = rcu_dereference(ireq->ireq_opt);
661 	fl4 = &newinet->cork.fl.u.ip4;
662 
663 	flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
664 			   RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
665 			   sk->sk_protocol, inet_sk_flowi_flags(sk),
666 			   (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
667 			   ireq->ir_loc_addr, ireq->ir_rmt_port,
668 			   htons(ireq->ir_num), sk->sk_uid);
669 	security_req_classify_flow(req, flowi4_to_flowi(fl4));
670 	rt = ip_route_output_flow(net, fl4, sk);
671 	if (IS_ERR(rt))
672 		goto no_route;
673 	if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
674 		goto route_err;
675 	return &rt->dst;
676 
677 route_err:
678 	ip_rt_put(rt);
679 no_route:
680 	__IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
681 	return NULL;
682 }
683 EXPORT_SYMBOL_GPL(inet_csk_route_child_sock);
684 
685 /* Decide when to expire the request and when to resend SYN-ACK */
syn_ack_recalc(struct request_sock * req,const int max_syn_ack_retries,const u8 rskq_defer_accept,int * expire,int * resend)686 static void syn_ack_recalc(struct request_sock *req,
687 			   const int max_syn_ack_retries,
688 			   const u8 rskq_defer_accept,
689 			   int *expire, int *resend)
690 {
691 	if (!rskq_defer_accept) {
692 		*expire = req->num_timeout >= max_syn_ack_retries;
693 		*resend = 1;
694 		return;
695 	}
696 	*expire = req->num_timeout >= max_syn_ack_retries &&
697 		  (!inet_rsk(req)->acked || req->num_timeout >= rskq_defer_accept);
698 	/* Do not resend while waiting for data after ACK,
699 	 * start to resend on end of deferring period to give
700 	 * last chance for data or ACK to create established socket.
701 	 */
702 	*resend = !inet_rsk(req)->acked ||
703 		  req->num_timeout >= rskq_defer_accept - 1;
704 }
705 
inet_rtx_syn_ack(const struct sock * parent,struct request_sock * req)706 int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req)
707 {
708 	int err = req->rsk_ops->rtx_syn_ack(parent, req);
709 
710 	if (!err)
711 		req->num_retrans++;
712 	return err;
713 }
714 EXPORT_SYMBOL(inet_rtx_syn_ack);
715 
716 /* return true if req was found in the ehash table */
reqsk_queue_unlink(struct request_sock * req)717 static bool reqsk_queue_unlink(struct request_sock *req)
718 {
719 	struct inet_hashinfo *hashinfo = req_to_sk(req)->sk_prot->h.hashinfo;
720 	bool found = false;
721 
722 	if (sk_hashed(req_to_sk(req))) {
723 		spinlock_t *lock = inet_ehash_lockp(hashinfo, req->rsk_hash);
724 
725 		spin_lock(lock);
726 		found = __sk_nulls_del_node_init_rcu(req_to_sk(req));
727 		spin_unlock(lock);
728 	}
729 	if (timer_pending(&req->rsk_timer) && del_timer_sync(&req->rsk_timer))
730 		reqsk_put(req);
731 	return found;
732 }
733 
inet_csk_reqsk_queue_drop(struct sock * sk,struct request_sock * req)734 bool inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req)
735 {
736 	bool unlinked = reqsk_queue_unlink(req);
737 
738 	if (unlinked) {
739 		reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
740 		reqsk_put(req);
741 	}
742 	return unlinked;
743 }
744 EXPORT_SYMBOL(inet_csk_reqsk_queue_drop);
745 
inet_csk_reqsk_queue_drop_and_put(struct sock * sk,struct request_sock * req)746 void inet_csk_reqsk_queue_drop_and_put(struct sock *sk, struct request_sock *req)
747 {
748 	inet_csk_reqsk_queue_drop(sk, req);
749 	reqsk_put(req);
750 }
751 EXPORT_SYMBOL(inet_csk_reqsk_queue_drop_and_put);
752 
reqsk_timer_handler(struct timer_list * t)753 static void reqsk_timer_handler(struct timer_list *t)
754 {
755 	struct request_sock *req = from_timer(req, t, rsk_timer);
756 	struct sock *sk_listener = req->rsk_listener;
757 	struct net *net = sock_net(sk_listener);
758 	struct inet_connection_sock *icsk = inet_csk(sk_listener);
759 	struct request_sock_queue *queue = &icsk->icsk_accept_queue;
760 	int max_syn_ack_retries, qlen, expire = 0, resend = 0;
761 
762 	if (inet_sk_state_load(sk_listener) != TCP_LISTEN)
763 		goto drop;
764 
765 	max_syn_ack_retries = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_synack_retries;
766 	/* Normally all the openreqs are young and become mature
767 	 * (i.e. converted to established socket) for first timeout.
768 	 * If synack was not acknowledged for 1 second, it means
769 	 * one of the following things: synack was lost, ack was lost,
770 	 * rtt is high or nobody planned to ack (i.e. synflood).
771 	 * When server is a bit loaded, queue is populated with old
772 	 * open requests, reducing effective size of queue.
773 	 * When server is well loaded, queue size reduces to zero
774 	 * after several minutes of work. It is not synflood,
775 	 * it is normal operation. The solution is pruning
776 	 * too old entries overriding normal timeout, when
777 	 * situation becomes dangerous.
778 	 *
779 	 * Essentially, we reserve half of room for young
780 	 * embrions; and abort old ones without pity, if old
781 	 * ones are about to clog our table.
782 	 */
783 	qlen = reqsk_queue_len(queue);
784 	if ((qlen << 1) > max(8U, READ_ONCE(sk_listener->sk_max_ack_backlog))) {
785 		int young = reqsk_queue_len_young(queue) << 1;
786 
787 		while (max_syn_ack_retries > 2) {
788 			if (qlen < young)
789 				break;
790 			max_syn_ack_retries--;
791 			young <<= 1;
792 		}
793 	}
794 	syn_ack_recalc(req, max_syn_ack_retries, READ_ONCE(queue->rskq_defer_accept),
795 		       &expire, &resend);
796 	req->rsk_ops->syn_ack_timeout(req);
797 	if (!expire &&
798 	    (!resend ||
799 	     !inet_rtx_syn_ack(sk_listener, req) ||
800 	     inet_rsk(req)->acked)) {
801 		unsigned long timeo;
802 
803 		if (req->num_timeout++ == 0)
804 			atomic_dec(&queue->young);
805 		timeo = min(TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
806 		mod_timer(&req->rsk_timer, jiffies + timeo);
807 		return;
808 	}
809 drop:
810 	inet_csk_reqsk_queue_drop_and_put(sk_listener, req);
811 }
812 
reqsk_queue_hash_req(struct request_sock * req,unsigned long timeout)813 static void reqsk_queue_hash_req(struct request_sock *req,
814 				 unsigned long timeout)
815 {
816 	timer_setup(&req->rsk_timer, reqsk_timer_handler, TIMER_PINNED);
817 	mod_timer(&req->rsk_timer, jiffies + timeout);
818 
819 	inet_ehash_insert(req_to_sk(req), NULL, NULL);
820 	/* before letting lookups find us, make sure all req fields
821 	 * are committed to memory and refcnt initialized.
822 	 */
823 	smp_wmb();
824 	refcount_set(&req->rsk_refcnt, 2 + 1);
825 }
826 
inet_csk_reqsk_queue_hash_add(struct sock * sk,struct request_sock * req,unsigned long timeout)827 void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
828 				   unsigned long timeout)
829 {
830 	reqsk_queue_hash_req(req, timeout);
831 	inet_csk_reqsk_queue_added(sk);
832 }
833 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
834 
inet_clone_ulp(const struct request_sock * req,struct sock * newsk,const gfp_t priority)835 static void inet_clone_ulp(const struct request_sock *req, struct sock *newsk,
836 			   const gfp_t priority)
837 {
838 	struct inet_connection_sock *icsk = inet_csk(newsk);
839 
840 	if (!icsk->icsk_ulp_ops)
841 		return;
842 
843 	icsk->icsk_ulp_ops->clone(req, newsk, priority);
844 }
845 
846 /**
847  *	inet_csk_clone_lock - clone an inet socket, and lock its clone
848  *	@sk: the socket to clone
849  *	@req: request_sock
850  *	@priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
851  *
852  *	Caller must unlock socket even in error path (bh_unlock_sock(newsk))
853  */
inet_csk_clone_lock(const struct sock * sk,const struct request_sock * req,const gfp_t priority)854 struct sock *inet_csk_clone_lock(const struct sock *sk,
855 				 const struct request_sock *req,
856 				 const gfp_t priority)
857 {
858 	struct sock *newsk = sk_clone_lock(sk, priority);
859 
860 	if (newsk) {
861 		struct inet_connection_sock *newicsk = inet_csk(newsk);
862 
863 		inet_sk_set_state(newsk, TCP_SYN_RECV);
864 		newicsk->icsk_bind_hash = NULL;
865 
866 		inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port;
867 		inet_sk(newsk)->inet_num = inet_rsk(req)->ir_num;
868 		inet_sk(newsk)->inet_sport = htons(inet_rsk(req)->ir_num);
869 
870 		/* listeners have SOCK_RCU_FREE, not the children */
871 		sock_reset_flag(newsk, SOCK_RCU_FREE);
872 
873 		inet_sk(newsk)->mc_list = NULL;
874 
875 		newsk->sk_mark = inet_rsk(req)->ir_mark;
876 		atomic64_set(&newsk->sk_cookie,
877 			     atomic64_read(&inet_rsk(req)->ir_cookie));
878 
879 		newicsk->icsk_retransmits = 0;
880 		newicsk->icsk_backoff	  = 0;
881 		newicsk->icsk_probes_out  = 0;
882 		newicsk->icsk_probes_tstamp = 0;
883 
884 		/* Deinitialize accept_queue to trap illegal accesses. */
885 		memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
886 
887 		inet_clone_ulp(req, newsk, priority);
888 
889 		security_inet_csk_clone(newsk, req);
890 	}
891 	return newsk;
892 }
893 EXPORT_SYMBOL_GPL(inet_csk_clone_lock);
894 
895 /*
896  * At this point, there should be no process reference to this
897  * socket, and thus no user references at all.  Therefore we
898  * can assume the socket waitqueue is inactive and nobody will
899  * try to jump onto it.
900  */
inet_csk_destroy_sock(struct sock * sk)901 void inet_csk_destroy_sock(struct sock *sk)
902 {
903 	WARN_ON(sk->sk_state != TCP_CLOSE);
904 	WARN_ON(!sock_flag(sk, SOCK_DEAD));
905 
906 	/* It cannot be in hash table! */
907 	WARN_ON(!sk_unhashed(sk));
908 
909 	/* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
910 	WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
911 
912 	sk->sk_prot->destroy(sk);
913 
914 	sk_stream_kill_queues(sk);
915 
916 	xfrm_sk_free_policy(sk);
917 
918 	sk_refcnt_debug_release(sk);
919 
920 	this_cpu_dec(*sk->sk_prot->orphan_count);
921 
922 	sock_put(sk);
923 }
924 EXPORT_SYMBOL(inet_csk_destroy_sock);
925 
926 /* This function allows to force a closure of a socket after the call to
927  * tcp/dccp_create_openreq_child().
928  */
inet_csk_prepare_forced_close(struct sock * sk)929 void inet_csk_prepare_forced_close(struct sock *sk)
930 	__releases(&sk->sk_lock.slock)
931 {
932 	/* sk_clone_lock locked the socket and set refcnt to 2 */
933 	bh_unlock_sock(sk);
934 	sock_put(sk);
935 	inet_csk_prepare_for_destroy_sock(sk);
936 	inet_sk(sk)->inet_num = 0;
937 }
938 EXPORT_SYMBOL(inet_csk_prepare_forced_close);
939 
inet_ulp_can_listen(const struct sock * sk)940 static int inet_ulp_can_listen(const struct sock *sk)
941 {
942 	const struct inet_connection_sock *icsk = inet_csk(sk);
943 
944 	if (icsk->icsk_ulp_ops && !icsk->icsk_ulp_ops->clone)
945 		return -EINVAL;
946 
947 	return 0;
948 }
949 
inet_csk_listen_start(struct sock * sk,int backlog)950 int inet_csk_listen_start(struct sock *sk, int backlog)
951 {
952 	struct inet_connection_sock *icsk = inet_csk(sk);
953 	struct inet_sock *inet = inet_sk(sk);
954 	int err;
955 
956 	err = inet_ulp_can_listen(sk);
957 	if (unlikely(err))
958 		return err;
959 
960 	reqsk_queue_alloc(&icsk->icsk_accept_queue);
961 
962 	sk->sk_ack_backlog = 0;
963 	inet_csk_delack_init(sk);
964 
965 	/* There is race window here: we announce ourselves listening,
966 	 * but this transition is still not validated by get_port().
967 	 * It is OK, because this socket enters to hash table only
968 	 * after validation is complete.
969 	 */
970 	err = -EADDRINUSE;
971 	inet_sk_state_store(sk, TCP_LISTEN);
972 	if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
973 		inet->inet_sport = htons(inet->inet_num);
974 
975 		sk_dst_reset(sk);
976 		err = sk->sk_prot->hash(sk);
977 
978 		if (likely(!err))
979 			return 0;
980 	}
981 
982 	inet_sk_set_state(sk, TCP_CLOSE);
983 	return err;
984 }
985 EXPORT_SYMBOL_GPL(inet_csk_listen_start);
986 
inet_child_forget(struct sock * sk,struct request_sock * req,struct sock * child)987 static void inet_child_forget(struct sock *sk, struct request_sock *req,
988 			      struct sock *child)
989 {
990 	sk->sk_prot->disconnect(child, O_NONBLOCK);
991 
992 	sock_orphan(child);
993 
994 	this_cpu_inc(*sk->sk_prot->orphan_count);
995 
996 	if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->tfo_listener) {
997 		BUG_ON(rcu_access_pointer(tcp_sk(child)->fastopen_rsk) != req);
998 		BUG_ON(sk != req->rsk_listener);
999 
1000 		/* Paranoid, to prevent race condition if
1001 		 * an inbound pkt destined for child is
1002 		 * blocked by sock lock in tcp_v4_rcv().
1003 		 * Also to satisfy an assertion in
1004 		 * tcp_v4_destroy_sock().
1005 		 */
1006 		RCU_INIT_POINTER(tcp_sk(child)->fastopen_rsk, NULL);
1007 	}
1008 	inet_csk_destroy_sock(child);
1009 }
1010 
inet_csk_reqsk_queue_add(struct sock * sk,struct request_sock * req,struct sock * child)1011 struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
1012 				      struct request_sock *req,
1013 				      struct sock *child)
1014 {
1015 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
1016 
1017 	spin_lock(&queue->rskq_lock);
1018 	if (unlikely(sk->sk_state != TCP_LISTEN)) {
1019 		inet_child_forget(sk, req, child);
1020 		child = NULL;
1021 	} else {
1022 		req->sk = child;
1023 		req->dl_next = NULL;
1024 		if (queue->rskq_accept_head == NULL)
1025 			WRITE_ONCE(queue->rskq_accept_head, req);
1026 		else
1027 			queue->rskq_accept_tail->dl_next = req;
1028 		queue->rskq_accept_tail = req;
1029 		sk_acceptq_added(sk);
1030 	}
1031 	spin_unlock(&queue->rskq_lock);
1032 	return child;
1033 }
1034 EXPORT_SYMBOL(inet_csk_reqsk_queue_add);
1035 
inet_csk_complete_hashdance(struct sock * sk,struct sock * child,struct request_sock * req,bool own_req)1036 struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
1037 					 struct request_sock *req, bool own_req)
1038 {
1039 	if (own_req) {
1040 		inet_csk_reqsk_queue_drop(sk, req);
1041 		reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
1042 		if (inet_csk_reqsk_queue_add(sk, req, child))
1043 			return child;
1044 	}
1045 	/* Too bad, another child took ownership of the request, undo. */
1046 	bh_unlock_sock(child);
1047 	sock_put(child);
1048 	return NULL;
1049 }
1050 EXPORT_SYMBOL(inet_csk_complete_hashdance);
1051 
1052 /*
1053  *	This routine closes sockets which have been at least partially
1054  *	opened, but not yet accepted.
1055  */
inet_csk_listen_stop(struct sock * sk)1056 void inet_csk_listen_stop(struct sock *sk)
1057 {
1058 	struct inet_connection_sock *icsk = inet_csk(sk);
1059 	struct request_sock_queue *queue = &icsk->icsk_accept_queue;
1060 	struct request_sock *next, *req;
1061 
1062 	/* Following specs, it would be better either to send FIN
1063 	 * (and enter FIN-WAIT-1, it is normal close)
1064 	 * or to send active reset (abort).
1065 	 * Certainly, it is pretty dangerous while synflood, but it is
1066 	 * bad justification for our negligence 8)
1067 	 * To be honest, we are not able to make either
1068 	 * of the variants now.			--ANK
1069 	 */
1070 	while ((req = reqsk_queue_remove(queue, sk)) != NULL) {
1071 		struct sock *child = req->sk;
1072 
1073 		local_bh_disable();
1074 		bh_lock_sock(child);
1075 		WARN_ON(sock_owned_by_user(child));
1076 		sock_hold(child);
1077 
1078 		inet_child_forget(sk, req, child);
1079 		reqsk_put(req);
1080 		bh_unlock_sock(child);
1081 		local_bh_enable();
1082 		sock_put(child);
1083 
1084 		cond_resched();
1085 	}
1086 	if (queue->fastopenq.rskq_rst_head) {
1087 		/* Free all the reqs queued in rskq_rst_head. */
1088 		spin_lock_bh(&queue->fastopenq.lock);
1089 		req = queue->fastopenq.rskq_rst_head;
1090 		queue->fastopenq.rskq_rst_head = NULL;
1091 		spin_unlock_bh(&queue->fastopenq.lock);
1092 		while (req != NULL) {
1093 			next = req->dl_next;
1094 			reqsk_put(req);
1095 			req = next;
1096 		}
1097 	}
1098 	WARN_ON_ONCE(sk->sk_ack_backlog);
1099 }
1100 EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
1101 
inet_csk_addr2sockaddr(struct sock * sk,struct sockaddr * uaddr)1102 void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
1103 {
1104 	struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
1105 	const struct inet_sock *inet = inet_sk(sk);
1106 
1107 	sin->sin_family		= AF_INET;
1108 	sin->sin_addr.s_addr	= inet->inet_daddr;
1109 	sin->sin_port		= inet->inet_dport;
1110 }
1111 EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
1112 
inet_csk_rebuild_route(struct sock * sk,struct flowi * fl)1113 static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl)
1114 {
1115 	const struct inet_sock *inet = inet_sk(sk);
1116 	const struct ip_options_rcu *inet_opt;
1117 	__be32 daddr = inet->inet_daddr;
1118 	struct flowi4 *fl4;
1119 	struct rtable *rt;
1120 
1121 	rcu_read_lock();
1122 	inet_opt = rcu_dereference(inet->inet_opt);
1123 	if (inet_opt && inet_opt->opt.srr)
1124 		daddr = inet_opt->opt.faddr;
1125 	fl4 = &fl->u.ip4;
1126 	rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr,
1127 				   inet->inet_saddr, inet->inet_dport,
1128 				   inet->inet_sport, sk->sk_protocol,
1129 				   RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
1130 	if (IS_ERR(rt))
1131 		rt = NULL;
1132 	if (rt)
1133 		sk_setup_caps(sk, &rt->dst);
1134 	rcu_read_unlock();
1135 
1136 	return &rt->dst;
1137 }
1138 
inet_csk_update_pmtu(struct sock * sk,u32 mtu)1139 struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu)
1140 {
1141 	struct dst_entry *dst = __sk_dst_check(sk, 0);
1142 	struct inet_sock *inet = inet_sk(sk);
1143 
1144 	if (!dst) {
1145 		dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
1146 		if (!dst)
1147 			goto out;
1148 	}
1149 	dst->ops->update_pmtu(dst, sk, NULL, mtu, true);
1150 
1151 	dst = __sk_dst_check(sk, 0);
1152 	if (!dst)
1153 		dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
1154 out:
1155 	return dst;
1156 }
1157 EXPORT_SYMBOL_GPL(inet_csk_update_pmtu);
1158