]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/inet_connection_sock.c
kcmp: make it depend on CHECKPOINT_RESTORE
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / inet_connection_sock.c
CommitLineData
3f421baa
ACM
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Support for INET connection oriented protocols.
7 *
8 * Authors: See the TCP sources
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or(at your option) any later version.
14 */
15
3f421baa
ACM
16#include <linux/module.h>
17#include <linux/jhash.h>
18
19#include <net/inet_connection_sock.h>
20#include <net/inet_hashtables.h>
21#include <net/inet_timewait_sock.h>
22#include <net/ip.h>
23#include <net/route.h>
24#include <net/tcp_states.h>
a019d6fe 25#include <net/xfrm.h>
3f421baa
ACM
26
27#ifdef INET_CSK_DEBUG
28const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
29EXPORT_SYMBOL(inet_csk_timer_bug_msg);
30#endif
31
32/*
3c689b73 33 * This struct holds the first and last local port number.
3f421baa 34 */
3c689b73 35struct local_ports sysctl_local_ports __read_mostly = {
c4dbe54e 36 .lock = __SEQLOCK_UNLOCKED(sysctl_local_ports.lock),
3c689b73
ED
37 .range = { 32768, 61000 },
38};
227b60f5 39
e3826f1e
AW
40unsigned long *sysctl_local_reserved_ports;
41EXPORT_SYMBOL(sysctl_local_reserved_ports);
42
227b60f5
SH
43void inet_get_local_port_range(int *low, int *high)
44{
95c96174
ED
45 unsigned int seq;
46
227b60f5 47 do {
3c689b73 48 seq = read_seqbegin(&sysctl_local_ports.lock);
227b60f5 49
3c689b73
ED
50 *low = sysctl_local_ports.range[0];
51 *high = sysctl_local_ports.range[1];
52 } while (read_seqretry(&sysctl_local_ports.lock, seq));
227b60f5
SH
53}
54EXPORT_SYMBOL(inet_get_local_port_range);
3f421baa 55
971af18b 56int inet_csk_bind_conflict(const struct sock *sk,
aacd9289 57 const struct inet_bind_bucket *tb, bool relax)
3f421baa 58{
3f421baa
ACM
59 struct sock *sk2;
60 struct hlist_node *node;
61 int reuse = sk->sk_reuse;
da5e3630
TH
62 int reuseport = sk->sk_reuseport;
63 kuid_t uid = sock_i_uid((struct sock *)sk);
3f421baa 64
7477fd2e
PE
65 /*
66 * Unlike other sk lookup places we do not check
67 * for sk_net here, since _all_ the socks listed
68 * in tb->owners list belong to the same net - the
69 * one this bucket belongs to.
70 */
71
3f421baa
ACM
72 sk_for_each_bound(sk2, node, &tb->owners) {
73 if (sk != sk2 &&
74 !inet_v6_ipv6only(sk2) &&
75 (!sk->sk_bound_dev_if ||
76 !sk2->sk_bound_dev_if ||
77 sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
da5e3630
TH
78 if ((!reuse || !sk2->sk_reuse ||
79 sk2->sk_state == TCP_LISTEN) &&
80 (!reuseport || !sk2->sk_reuseport ||
81 (sk2->sk_state != TCP_TIME_WAIT &&
82 !uid_eq(uid, sock_i_uid(sk2))))) {
68835aba
ED
83 const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
84 if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) ||
85 sk2_rcv_saddr == sk_rcv_saddr(sk))
3f421baa 86 break;
8d238b25 87 }
aacd9289
AC
88 if (!relax && reuse && sk2->sk_reuse &&
89 sk2->sk_state != TCP_LISTEN) {
90 const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
91
92 if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) ||
93 sk2_rcv_saddr == sk_rcv_saddr(sk))
94 break;
95 }
3f421baa
ACM
96 }
97 }
98 return node != NULL;
99}
971af18b
ACM
100EXPORT_SYMBOL_GPL(inet_csk_bind_conflict);
101
3f421baa
ACM
102/* Obtain a reference to a local port for the given sock,
103 * if snum is zero it means select any available local port.
104 */
ab1e0a13 105int inet_csk_get_port(struct sock *sk, unsigned short snum)
3f421baa 106{
39d8cda7 107 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
3f421baa
ACM
108 struct inet_bind_hashbucket *head;
109 struct hlist_node *node;
110 struct inet_bind_bucket *tb;
a9d8f911 111 int ret, attempts = 5;
3b1e0a65 112 struct net *net = sock_net(sk);
a9d8f911 113 int smallest_size = -1, smallest_rover;
da5e3630 114 kuid_t uid = sock_i_uid(sk);
3f421baa
ACM
115
116 local_bh_disable();
117 if (!snum) {
227b60f5
SH
118 int remaining, rover, low, high;
119
a9d8f911 120again:
227b60f5 121 inet_get_local_port_range(&low, &high);
a25de534 122 remaining = (high - low) + 1;
a9d8f911 123 smallest_rover = rover = net_random() % remaining + low;
3f421baa 124
a9d8f911 125 smallest_size = -1;
3f421baa 126 do {
e3826f1e
AW
127 if (inet_is_reserved_local_port(rover))
128 goto next_nolock;
7f635ab7
PE
129 head = &hashinfo->bhash[inet_bhashfn(net, rover,
130 hashinfo->bhash_size)];
3f421baa
ACM
131 spin_lock(&head->lock);
132 inet_bind_bucket_for_each(tb, node, &head->chain)
09ad9bc7 133 if (net_eq(ib_net(tb), net) && tb->port == rover) {
da5e3630
TH
134 if (((tb->fastreuse > 0 &&
135 sk->sk_reuse &&
136 sk->sk_state != TCP_LISTEN) ||
137 (tb->fastreuseport > 0 &&
138 sk->sk_reuseport &&
139 uid_eq(tb->fastuid, uid))) &&
a9d8f911
EP
140 (tb->num_owners < smallest_size || smallest_size == -1)) {
141 smallest_size = tb->num_owners;
142 smallest_rover = rover;
aacd9289
AC
143 if (atomic_read(&hashinfo->bsockets) > (high - low) + 1 &&
144 !inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) {
8d238b25 145 snum = smallest_rover;
fddb7b57 146 goto tb_found;
a9d8f911
EP
147 }
148 }
aacd9289 149 if (!inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) {
2b05ad33 150 snum = rover;
fddb7b57 151 goto tb_found;
2b05ad33 152 }
3f421baa 153 goto next;
a9d8f911 154 }
3f421baa
ACM
155 break;
156 next:
157 spin_unlock(&head->lock);
e3826f1e 158 next_nolock:
6df71634
SH
159 if (++rover > high)
160 rover = low;
3f421baa 161 } while (--remaining > 0);
3f421baa
ACM
162
163 /* Exhausted local port range during search? It is not
164 * possible for us to be holding one of the bind hash
165 * locks if this test triggers, because if 'remaining'
166 * drops to zero, we broke out of the do/while loop at
167 * the top level, not from the 'break;' statement.
168 */
169 ret = 1;
a9d8f911
EP
170 if (remaining <= 0) {
171 if (smallest_size != -1) {
172 snum = smallest_rover;
173 goto have_snum;
174 }
3f421baa 175 goto fail;
a9d8f911 176 }
3f421baa
ACM
177 /* OK, here is the one we will use. HEAD is
178 * non-NULL and we hold it's mutex.
179 */
180 snum = rover;
181 } else {
a9d8f911 182have_snum:
7f635ab7
PE
183 head = &hashinfo->bhash[inet_bhashfn(net, snum,
184 hashinfo->bhash_size)];
3f421baa
ACM
185 spin_lock(&head->lock);
186 inet_bind_bucket_for_each(tb, node, &head->chain)
09ad9bc7 187 if (net_eq(ib_net(tb), net) && tb->port == snum)
3f421baa
ACM
188 goto tb_found;
189 }
190 tb = NULL;
191 goto tb_not_found;
192tb_found:
193 if (!hlist_empty(&tb->owners)) {
4a17fd52
PE
194 if (sk->sk_reuse == SK_FORCE_REUSE)
195 goto success;
196
da5e3630
TH
197 if (((tb->fastreuse > 0 &&
198 sk->sk_reuse && sk->sk_state != TCP_LISTEN) ||
199 (tb->fastreuseport > 0 &&
200 sk->sk_reuseport && uid_eq(tb->fastuid, uid))) &&
a9d8f911 201 smallest_size == -1) {
3f421baa
ACM
202 goto success;
203 } else {
204 ret = 1;
aacd9289 205 if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, true)) {
da5e3630 206 if (((sk->sk_reuse && sk->sk_state != TCP_LISTEN) ||
9c5e0c0b
TH
207 (tb->fastreuseport > 0 &&
208 sk->sk_reuseport && uid_eq(tb->fastuid, uid))) &&
5add3009 209 smallest_size != -1 && --attempts >= 0) {
a9d8f911
EP
210 spin_unlock(&head->lock);
211 goto again;
212 }
aacd9289 213
3f421baa 214 goto fail_unlock;
a9d8f911 215 }
3f421baa
ACM
216 }
217 }
218tb_not_found:
219 ret = 1;
941b1d22
PE
220 if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
221 net, head, snum)) == NULL)
3f421baa
ACM
222 goto fail_unlock;
223 if (hlist_empty(&tb->owners)) {
224 if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
225 tb->fastreuse = 1;
226 else
227 tb->fastreuse = 0;
da5e3630
TH
228 if (sk->sk_reuseport) {
229 tb->fastreuseport = 1;
230 tb->fastuid = uid;
9c5e0c0b 231 } else
da5e3630 232 tb->fastreuseport = 0;
da5e3630
TH
233 } else {
234 if (tb->fastreuse &&
235 (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
236 tb->fastreuse = 0;
237 if (tb->fastreuseport &&
9c5e0c0b 238 (!sk->sk_reuseport || !uid_eq(tb->fastuid, uid)))
da5e3630 239 tb->fastreuseport = 0;
da5e3630 240 }
3f421baa
ACM
241success:
242 if (!inet_csk(sk)->icsk_bind_hash)
243 inet_bind_hash(sk, tb, snum);
547b792c 244 WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
e905a9ed 245 ret = 0;
3f421baa
ACM
246
247fail_unlock:
248 spin_unlock(&head->lock);
249fail:
250 local_bh_enable();
251 return ret;
252}
3f421baa
ACM
253EXPORT_SYMBOL_GPL(inet_csk_get_port);
254
255/*
256 * Wait for an incoming connection, avoid race conditions. This must be called
257 * with the socket locked.
258 */
259static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
260{
261 struct inet_connection_sock *icsk = inet_csk(sk);
262 DEFINE_WAIT(wait);
263 int err;
264
265 /*
266 * True wake-one mechanism for incoming connections: only
267 * one process gets woken up, not the 'whole herd'.
268 * Since we do not 'race & poll' for established sockets
269 * anymore, the common case will execute the loop only once.
270 *
271 * Subtle issue: "add_wait_queue_exclusive()" will be added
272 * after any current non-exclusive waiters, and we know that
273 * it will always _stay_ after any new non-exclusive waiters
274 * because all non-exclusive waiters are added at the
275 * beginning of the wait-queue. As such, it's ok to "drop"
276 * our exclusiveness temporarily when we get woken up without
277 * having to remove and re-insert us on the wait queue.
278 */
279 for (;;) {
aa395145 280 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
3f421baa
ACM
281 TASK_INTERRUPTIBLE);
282 release_sock(sk);
283 if (reqsk_queue_empty(&icsk->icsk_accept_queue))
284 timeo = schedule_timeout(timeo);
285 lock_sock(sk);
286 err = 0;
287 if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
288 break;
289 err = -EINVAL;
290 if (sk->sk_state != TCP_LISTEN)
291 break;
292 err = sock_intr_errno(timeo);
293 if (signal_pending(current))
294 break;
295 err = -EAGAIN;
296 if (!timeo)
297 break;
298 }
aa395145 299 finish_wait(sk_sleep(sk), &wait);
3f421baa
ACM
300 return err;
301}
302
303/*
304 * This will accept the next outstanding connection.
305 */
306struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
307{
308 struct inet_connection_sock *icsk = inet_csk(sk);
8336886f 309 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
3f421baa 310 struct sock *newsk;
8336886f 311 struct request_sock *req;
3f421baa
ACM
312 int error;
313
314 lock_sock(sk);
315
316 /* We need to make sure that this socket is listening,
317 * and that it has something pending.
318 */
319 error = -EINVAL;
320 if (sk->sk_state != TCP_LISTEN)
321 goto out_err;
322
323 /* Find already established connection */
8336886f 324 if (reqsk_queue_empty(queue)) {
3f421baa
ACM
325 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
326
327 /* If this is a non blocking socket don't sleep */
328 error = -EAGAIN;
329 if (!timeo)
330 goto out_err;
331
332 error = inet_csk_wait_for_connect(sk, timeo);
333 if (error)
334 goto out_err;
335 }
8336886f
JC
336 req = reqsk_queue_remove(queue);
337 newsk = req->sk;
338
339 sk_acceptq_removed(sk);
7ab4551f 340 if (sk->sk_protocol == IPPROTO_TCP && queue->fastopenq != NULL) {
8336886f
JC
341 spin_lock_bh(&queue->fastopenq->lock);
342 if (tcp_rsk(req)->listener) {
343 /* We are still waiting for the final ACK from 3WHS
344 * so can't free req now. Instead, we set req->sk to
345 * NULL to signify that the child socket is taken
346 * so reqsk_fastopen_remove() will free the req
347 * when 3WHS finishes (or is aborted).
348 */
349 req->sk = NULL;
350 req = NULL;
351 }
352 spin_unlock_bh(&queue->fastopenq->lock);
353 }
3f421baa
ACM
354out:
355 release_sock(sk);
8336886f
JC
356 if (req)
357 __reqsk_free(req);
3f421baa
ACM
358 return newsk;
359out_err:
360 newsk = NULL;
8336886f 361 req = NULL;
3f421baa
ACM
362 *err = error;
363 goto out;
364}
3f421baa
ACM
365EXPORT_SYMBOL(inet_csk_accept);
366
367/*
368 * Using different timers for retransmit, delayed acks and probes
e905a9ed 369 * We may wish use just one timer maintaining a list of expire jiffies
3f421baa
ACM
370 * to optimize.
371 */
372void inet_csk_init_xmit_timers(struct sock *sk,
373 void (*retransmit_handler)(unsigned long),
374 void (*delack_handler)(unsigned long),
375 void (*keepalive_handler)(unsigned long))
376{
377 struct inet_connection_sock *icsk = inet_csk(sk);
378
b24b8a24
PE
379 setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler,
380 (unsigned long)sk);
381 setup_timer(&icsk->icsk_delack_timer, delack_handler,
382 (unsigned long)sk);
383 setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk);
3f421baa
ACM
384 icsk->icsk_pending = icsk->icsk_ack.pending = 0;
385}
3f421baa
ACM
386EXPORT_SYMBOL(inet_csk_init_xmit_timers);
387
388void inet_csk_clear_xmit_timers(struct sock *sk)
389{
390 struct inet_connection_sock *icsk = inet_csk(sk);
391
392 icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
393
394 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
395 sk_stop_timer(sk, &icsk->icsk_delack_timer);
396 sk_stop_timer(sk, &sk->sk_timer);
397}
3f421baa
ACM
398EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
399
400void inet_csk_delete_keepalive_timer(struct sock *sk)
401{
402 sk_stop_timer(sk, &sk->sk_timer);
403}
3f421baa
ACM
404EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
405
406void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
407{
408 sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
409}
3f421baa
ACM
410EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
411
d9319100 412struct dst_entry *inet_csk_route_req(struct sock *sk,
6bd023f3 413 struct flowi4 *fl4,
ba3f7f04 414 const struct request_sock *req)
3f421baa
ACM
415{
416 struct rtable *rt;
417 const struct inet_request_sock *ireq = inet_rsk(req);
f6d8bd05 418 struct ip_options_rcu *opt = inet_rsk(req)->opt;
84a3aa00 419 struct net *net = sock_net(sk);
3e12939a 420 int flags = inet_sk_flowi_flags(sk);
3f421baa 421
6bd023f3 422 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
e79d9bc7 423 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
7433819a 424 sk->sk_protocol,
7586eceb 425 flags,
f6d8bd05 426 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr,
e79d9bc7 427 ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport);
6bd023f3
DM
428 security_req_classify_flow(req, flowi4_to_flowi(fl4));
429 rt = ip_route_output_flow(net, fl4, sk);
b23dd4fe 430 if (IS_ERR(rt))
857a6e0a 431 goto no_route;
155e8336 432 if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
857a6e0a 433 goto route_err;
d8d1f30b 434 return &rt->dst;
857a6e0a
IJ
435
436route_err:
437 ip_rt_put(rt);
438no_route:
439 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
440 return NULL;
3f421baa 441}
3f421baa
ACM
442EXPORT_SYMBOL_GPL(inet_csk_route_req);
443
77357a95
DM
444struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
445 struct sock *newsk,
446 const struct request_sock *req)
447{
448 const struct inet_request_sock *ireq = inet_rsk(req);
449 struct inet_sock *newinet = inet_sk(newsk);
1a7b27c9 450 struct ip_options_rcu *opt;
77357a95
DM
451 struct net *net = sock_net(sk);
452 struct flowi4 *fl4;
453 struct rtable *rt;
454
455 fl4 = &newinet->cork.fl.u.ip4;
1a7b27c9
CP
456
457 rcu_read_lock();
458 opt = rcu_dereference(newinet->inet_opt);
77357a95
DM
459 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
460 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
461 sk->sk_protocol, inet_sk_flowi_flags(sk),
462 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr,
463 ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport);
464 security_req_classify_flow(req, flowi4_to_flowi(fl4));
465 rt = ip_route_output_flow(net, fl4, sk);
466 if (IS_ERR(rt))
467 goto no_route;
155e8336 468 if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
77357a95 469 goto route_err;
1a7b27c9 470 rcu_read_unlock();
77357a95
DM
471 return &rt->dst;
472
473route_err:
474 ip_rt_put(rt);
475no_route:
1a7b27c9 476 rcu_read_unlock();
77357a95
DM
477 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
478 return NULL;
479}
480EXPORT_SYMBOL_GPL(inet_csk_route_child_sock);
481
6b72977b 482static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
72a3effa 483 const u32 rnd, const u32 synq_hsize)
3f421baa 484{
6b72977b 485 return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
3f421baa
ACM
486}
487
dfd56b8b 488#if IS_ENABLED(CONFIG_IPV6)
3f421baa
ACM
489#define AF_INET_FAMILY(fam) ((fam) == AF_INET)
490#else
491#define AF_INET_FAMILY(fam) 1
492#endif
493
494struct request_sock *inet_csk_search_req(const struct sock *sk,
495 struct request_sock ***prevp,
6b72977b 496 const __be16 rport, const __be32 raddr,
7f25afbb 497 const __be32 laddr)
3f421baa
ACM
498{
499 const struct inet_connection_sock *icsk = inet_csk(sk);
500 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
501 struct request_sock *req, **prev;
502
503 for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
504 lopt->nr_table_entries)];
505 (req = *prev) != NULL;
506 prev = &req->dl_next) {
507 const struct inet_request_sock *ireq = inet_rsk(req);
508
509 if (ireq->rmt_port == rport &&
510 ireq->rmt_addr == raddr &&
511 ireq->loc_addr == laddr &&
512 AF_INET_FAMILY(req->rsk_ops->family)) {
547b792c 513 WARN_ON(req->sk);
3f421baa
ACM
514 *prevp = prev;
515 break;
516 }
517 }
518
519 return req;
520}
3f421baa
ACM
521EXPORT_SYMBOL_GPL(inet_csk_search_req);
522
523void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
c2977c22 524 unsigned long timeout)
3f421baa
ACM
525{
526 struct inet_connection_sock *icsk = inet_csk(sk);
527 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
528 const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
529 lopt->hash_rnd, lopt->nr_table_entries);
530
531 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
532 inet_csk_reqsk_queue_added(sk, timeout);
533}
4bc2f18b 534EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
3f421baa 535
a019d6fe
ACM
536/* Only thing we need from tcp.h */
537extern int sysctl_tcp_synack_retries;
538
9f1d2604 539
0c3d79bc
JA
540/* Decide when to expire the request and when to resend SYN-ACK */
541static inline void syn_ack_recalc(struct request_sock *req, const int thresh,
542 const int max_retries,
543 const u8 rskq_defer_accept,
544 int *expire, int *resend)
545{
546 if (!rskq_defer_accept) {
e6c022a4 547 *expire = req->num_timeout >= thresh;
0c3d79bc
JA
548 *resend = 1;
549 return;
550 }
e6c022a4
ED
551 *expire = req->num_timeout >= thresh &&
552 (!inet_rsk(req)->acked || req->num_timeout >= max_retries);
0c3d79bc
JA
553 /*
554 * Do not resend while waiting for data after ACK,
555 * start to resend on end of deferring period to give
556 * last chance for data or ACK to create established socket.
557 */
558 *resend = !inet_rsk(req)->acked ||
e6c022a4 559 req->num_timeout >= rskq_defer_accept - 1;
0c3d79bc
JA
560}
561
e6c022a4
ED
562int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req)
563{
564 int err = req->rsk_ops->rtx_syn_ack(parent, req, NULL);
565
566 if (!err)
567 req->num_retrans++;
568 return err;
569}
570EXPORT_SYMBOL(inet_rtx_syn_ack);
571
a019d6fe
ACM
572void inet_csk_reqsk_queue_prune(struct sock *parent,
573 const unsigned long interval,
574 const unsigned long timeout,
575 const unsigned long max_rto)
576{
577 struct inet_connection_sock *icsk = inet_csk(parent);
578 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
579 struct listen_sock *lopt = queue->listen_opt;
ec0a1966
DM
580 int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
581 int thresh = max_retries;
a019d6fe
ACM
582 unsigned long now = jiffies;
583 struct request_sock **reqp, *req;
584 int i, budget;
585
586 if (lopt == NULL || lopt->qlen == 0)
587 return;
588
589 /* Normally all the openreqs are young and become mature
590 * (i.e. converted to established socket) for first timeout.
fd4f2cea 591 * If synack was not acknowledged for 1 second, it means
a019d6fe
ACM
592 * one of the following things: synack was lost, ack was lost,
593 * rtt is high or nobody planned to ack (i.e. synflood).
594 * When server is a bit loaded, queue is populated with old
595 * open requests, reducing effective size of queue.
596 * When server is well loaded, queue size reduces to zero
597 * after several minutes of work. It is not synflood,
598 * it is normal operation. The solution is pruning
599 * too old entries overriding normal timeout, when
600 * situation becomes dangerous.
601 *
602 * Essentially, we reserve half of room for young
603 * embrions; and abort old ones without pity, if old
604 * ones are about to clog our table.
605 */
606 if (lopt->qlen>>(lopt->max_qlen_log-1)) {
607 int young = (lopt->qlen_young<<1);
608
609 while (thresh > 2) {
610 if (lopt->qlen < young)
611 break;
612 thresh--;
613 young <<= 1;
614 }
615 }
616
ec0a1966
DM
617 if (queue->rskq_defer_accept)
618 max_retries = queue->rskq_defer_accept;
619
a019d6fe
ACM
620 budget = 2 * (lopt->nr_table_entries / (timeout / interval));
621 i = lopt->clock_hand;
622
623 do {
624 reqp=&lopt->syn_table[i];
625 while ((req = *reqp) != NULL) {
626 if (time_after_eq(now, req->expires)) {
0c3d79bc
JA
627 int expire = 0, resend = 0;
628
629 syn_ack_recalc(req, thresh, max_retries,
630 queue->rskq_defer_accept,
631 &expire, &resend);
c72e1183 632 req->rsk_ops->syn_ack_timeout(parent, req);
0c3d79bc
JA
633 if (!expire &&
634 (!resend ||
e6c022a4 635 !inet_rtx_syn_ack(parent, req) ||
0c3d79bc 636 inet_rsk(req)->acked)) {
a019d6fe
ACM
637 unsigned long timeo;
638
e6c022a4 639 if (req->num_timeout++ == 0)
a019d6fe 640 lopt->qlen_young--;
e6c022a4
ED
641 timeo = min(timeout << req->num_timeout,
642 max_rto);
a019d6fe
ACM
643 req->expires = now + timeo;
644 reqp = &req->dl_next;
645 continue;
646 }
647
648 /* Drop this request */
649 inet_csk_reqsk_queue_unlink(parent, req, reqp);
650 reqsk_queue_removed(queue, req);
651 reqsk_free(req);
652 continue;
653 }
654 reqp = &req->dl_next;
655 }
656
657 i = (i + 1) & (lopt->nr_table_entries - 1);
658
659 } while (--budget > 0);
660
661 lopt->clock_hand = i;
662
663 if (lopt->qlen)
664 inet_csk_reset_keepalive_timer(parent, interval);
665}
a019d6fe
ACM
666EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
667
e56c57d0
ED
668/**
669 * inet_csk_clone_lock - clone an inet socket, and lock its clone
670 * @sk: the socket to clone
671 * @req: request_sock
672 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
673 *
674 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
675 */
676struct sock *inet_csk_clone_lock(const struct sock *sk,
677 const struct request_sock *req,
678 const gfp_t priority)
9f1d2604 679{
e56c57d0 680 struct sock *newsk = sk_clone_lock(sk, priority);
9f1d2604
ACM
681
682 if (newsk != NULL) {
683 struct inet_connection_sock *newicsk = inet_csk(newsk);
684
685 newsk->sk_state = TCP_SYN_RECV;
686 newicsk->icsk_bind_hash = NULL;
687
c720c7e8
ED
688 inet_sk(newsk)->inet_dport = inet_rsk(req)->rmt_port;
689 inet_sk(newsk)->inet_num = ntohs(inet_rsk(req)->loc_port);
690 inet_sk(newsk)->inet_sport = inet_rsk(req)->loc_port;
9f1d2604
ACM
691 newsk->sk_write_space = sk_stream_write_space;
692
693 newicsk->icsk_retransmits = 0;
6687e988
ACM
694 newicsk->icsk_backoff = 0;
695 newicsk->icsk_probes_out = 0;
9f1d2604
ACM
696
697 /* Deinitialize accept_queue to trap illegal accesses. */
698 memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
4237c75c
VY
699
700 security_inet_csk_clone(newsk, req);
9f1d2604
ACM
701 }
702 return newsk;
703}
e56c57d0 704EXPORT_SYMBOL_GPL(inet_csk_clone_lock);
a019d6fe
ACM
705
706/*
707 * At this point, there should be no process reference to this
708 * socket, and thus no user references at all. Therefore we
709 * can assume the socket waitqueue is inactive and nobody will
710 * try to jump onto it.
711 */
712void inet_csk_destroy_sock(struct sock *sk)
713{
547b792c
IJ
714 WARN_ON(sk->sk_state != TCP_CLOSE);
715 WARN_ON(!sock_flag(sk, SOCK_DEAD));
a019d6fe
ACM
716
717 /* It cannot be in hash table! */
547b792c 718 WARN_ON(!sk_unhashed(sk));
a019d6fe 719
c720c7e8
ED
720 /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
721 WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
a019d6fe
ACM
722
723 sk->sk_prot->destroy(sk);
724
725 sk_stream_kill_queues(sk);
726
727 xfrm_sk_free_policy(sk);
728
729 sk_refcnt_debug_release(sk);
730
dd24c001 731 percpu_counter_dec(sk->sk_prot->orphan_count);
a019d6fe
ACM
732 sock_put(sk);
733}
a019d6fe
ACM
734EXPORT_SYMBOL(inet_csk_destroy_sock);
735
e337e24d
CP
736/* This function allows to force a closure of a socket after the call to
737 * tcp/dccp_create_openreq_child().
738 */
739void inet_csk_prepare_forced_close(struct sock *sk)
740{
741 /* sk_clone_lock locked the socket and set refcnt to 2 */
742 bh_unlock_sock(sk);
743 sock_put(sk);
744
745 /* The below has to be done to allow calling inet_csk_destroy_sock */
746 sock_set_flag(sk, SOCK_DEAD);
747 percpu_counter_inc(sk->sk_prot->orphan_count);
748 inet_sk(sk)->inet_num = 0;
749}
750EXPORT_SYMBOL(inet_csk_prepare_forced_close);
751
a019d6fe
ACM
752int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
753{
754 struct inet_sock *inet = inet_sk(sk);
755 struct inet_connection_sock *icsk = inet_csk(sk);
756 int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
757
758 if (rc != 0)
759 return rc;
760
761 sk->sk_max_ack_backlog = 0;
762 sk->sk_ack_backlog = 0;
763 inet_csk_delack_init(sk);
764
765 /* There is race window here: we announce ourselves listening,
766 * but this transition is still not validated by get_port().
767 * It is OK, because this socket enters to hash table only
768 * after validation is complete.
769 */
770 sk->sk_state = TCP_LISTEN;
c720c7e8
ED
771 if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
772 inet->inet_sport = htons(inet->inet_num);
a019d6fe
ACM
773
774 sk_dst_reset(sk);
775 sk->sk_prot->hash(sk);
776
777 return 0;
778 }
779
780 sk->sk_state = TCP_CLOSE;
781 __reqsk_queue_destroy(&icsk->icsk_accept_queue);
782 return -EADDRINUSE;
783}
a019d6fe
ACM
784EXPORT_SYMBOL_GPL(inet_csk_listen_start);
785
786/*
787 * This routine closes sockets which have been at least partially
788 * opened, but not yet accepted.
789 */
790void inet_csk_listen_stop(struct sock *sk)
791{
792 struct inet_connection_sock *icsk = inet_csk(sk);
8336886f 793 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
a019d6fe
ACM
794 struct request_sock *acc_req;
795 struct request_sock *req;
796
797 inet_csk_delete_keepalive_timer(sk);
798
799 /* make all the listen_opt local to us */
8336886f 800 acc_req = reqsk_queue_yank_acceptq(queue);
a019d6fe
ACM
801
802 /* Following specs, it would be better either to send FIN
803 * (and enter FIN-WAIT-1, it is normal close)
804 * or to send active reset (abort).
805 * Certainly, it is pretty dangerous while synflood, but it is
806 * bad justification for our negligence 8)
807 * To be honest, we are not able to make either
808 * of the variants now. --ANK
809 */
8336886f 810 reqsk_queue_destroy(queue);
a019d6fe
ACM
811
812 while ((req = acc_req) != NULL) {
813 struct sock *child = req->sk;
814
815 acc_req = req->dl_next;
816
817 local_bh_disable();
818 bh_lock_sock(child);
547b792c 819 WARN_ON(sock_owned_by_user(child));
a019d6fe
ACM
820 sock_hold(child);
821
822 sk->sk_prot->disconnect(child, O_NONBLOCK);
823
824 sock_orphan(child);
825
eb4dea58
HX
826 percpu_counter_inc(sk->sk_prot->orphan_count);
827
7ab4551f 828 if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->listener) {
8336886f
JC
829 BUG_ON(tcp_sk(child)->fastopen_rsk != req);
830 BUG_ON(sk != tcp_rsk(req)->listener);
831
832 /* Paranoid, to prevent race condition if
833 * an inbound pkt destined for child is
834 * blocked by sock lock in tcp_v4_rcv().
835 * Also to satisfy an assertion in
836 * tcp_v4_destroy_sock().
837 */
838 tcp_sk(child)->fastopen_rsk = NULL;
839 sock_put(sk);
840 }
a019d6fe
ACM
841 inet_csk_destroy_sock(child);
842
843 bh_unlock_sock(child);
844 local_bh_enable();
845 sock_put(child);
846
847 sk_acceptq_removed(sk);
848 __reqsk_free(req);
849 }
8336886f
JC
850 if (queue->fastopenq != NULL) {
851 /* Free all the reqs queued in rskq_rst_head. */
852 spin_lock_bh(&queue->fastopenq->lock);
853 acc_req = queue->fastopenq->rskq_rst_head;
854 queue->fastopenq->rskq_rst_head = NULL;
855 spin_unlock_bh(&queue->fastopenq->lock);
856 while ((req = acc_req) != NULL) {
857 acc_req = req->dl_next;
858 __reqsk_free(req);
859 }
860 }
547b792c 861 WARN_ON(sk->sk_ack_backlog);
a019d6fe 862}
a019d6fe 863EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
af05dc93
ACM
864
865void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
866{
867 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
868 const struct inet_sock *inet = inet_sk(sk);
869
870 sin->sin_family = AF_INET;
c720c7e8
ED
871 sin->sin_addr.s_addr = inet->inet_daddr;
872 sin->sin_port = inet->inet_dport;
af05dc93 873}
af05dc93 874EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
c4d93909 875
dec73ff0
ACM
876#ifdef CONFIG_COMPAT
877int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
878 char __user *optval, int __user *optlen)
879{
dbeff12b 880 const struct inet_connection_sock *icsk = inet_csk(sk);
dec73ff0
ACM
881
882 if (icsk->icsk_af_ops->compat_getsockopt != NULL)
883 return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
884 optval, optlen);
885 return icsk->icsk_af_ops->getsockopt(sk, level, optname,
886 optval, optlen);
887}
dec73ff0
ACM
888EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
889
890int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
b7058842 891 char __user *optval, unsigned int optlen)
dec73ff0 892{
dbeff12b 893 const struct inet_connection_sock *icsk = inet_csk(sk);
dec73ff0
ACM
894
895 if (icsk->icsk_af_ops->compat_setsockopt != NULL)
896 return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
897 optval, optlen);
898 return icsk->icsk_af_ops->setsockopt(sk, level, optname,
899 optval, optlen);
900}
dec73ff0
ACM
901EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
902#endif
80d0a69f
DM
903
904static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl)
905{
5abf7f7e
ED
906 const struct inet_sock *inet = inet_sk(sk);
907 const struct ip_options_rcu *inet_opt;
80d0a69f
DM
908 __be32 daddr = inet->inet_daddr;
909 struct flowi4 *fl4;
910 struct rtable *rt;
911
912 rcu_read_lock();
913 inet_opt = rcu_dereference(inet->inet_opt);
914 if (inet_opt && inet_opt->opt.srr)
915 daddr = inet_opt->opt.faddr;
916 fl4 = &fl->u.ip4;
917 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr,
918 inet->inet_saddr, inet->inet_dport,
919 inet->inet_sport, sk->sk_protocol,
920 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
921 if (IS_ERR(rt))
922 rt = NULL;
923 if (rt)
924 sk_setup_caps(sk, &rt->dst);
925 rcu_read_unlock();
926
927 return &rt->dst;
928}
929
930struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu)
931{
932 struct dst_entry *dst = __sk_dst_check(sk, 0);
933 struct inet_sock *inet = inet_sk(sk);
934
935 if (!dst) {
936 dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
937 if (!dst)
938 goto out;
939 }
6700c270 940 dst->ops->update_pmtu(dst, sk, NULL, mtu);
80d0a69f
DM
941
942 dst = __sk_dst_check(sk, 0);
943 if (!dst)
944 dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
945out:
946 return dst;
947}
948EXPORT_SYMBOL_GPL(inet_csk_update_pmtu);