blob: 8bb623d357adf9c1ec2b88010338f5be1b3182ae [file] [log] [blame]
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07001/*
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
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070016#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>
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -070025#include <net/xfrm.h>
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070026
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/*
Eric Dumazet3c689b72008-10-08 14:18:04 -070033 * This struct holds the first and last local port number.
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070034 */
Eric Dumazet3c689b72008-10-08 14:18:04 -070035struct local_ports sysctl_local_ports __read_mostly = {
Eric Dumazetc4dbe542011-05-24 14:08:08 +020036 .lock = __SEQLOCK_UNLOCKED(sysctl_local_ports.lock),
Eric Dumazet3c689b72008-10-08 14:18:04 -070037 .range = { 32768, 61000 },
38};
Stephen Hemminger227b60f2007-10-10 17:30:46 -070039
Amerigo Wange3826f12010-05-05 00:27:06 +000040unsigned long *sysctl_local_reserved_ports;
41EXPORT_SYMBOL(sysctl_local_reserved_ports);
42
Stephen Hemminger227b60f2007-10-10 17:30:46 -070043void inet_get_local_port_range(int *low, int *high)
44{
Eric Dumazet95c96172012-04-15 05:58:06 +000045 unsigned int seq;
46
Stephen Hemminger227b60f2007-10-10 17:30:46 -070047 do {
Eric Dumazet3c689b72008-10-08 14:18:04 -070048 seq = read_seqbegin(&sysctl_local_ports.lock);
Stephen Hemminger227b60f2007-10-10 17:30:46 -070049
Eric Dumazet3c689b72008-10-08 14:18:04 -070050 *low = sysctl_local_ports.range[0];
51 *high = sysctl_local_ports.range[1];
52 } while (read_seqretry(&sysctl_local_ports.lock, seq));
Stephen Hemminger227b60f2007-10-10 17:30:46 -070053}
54EXPORT_SYMBOL(inet_get_local_port_range);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070055
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -080056int inet_csk_bind_conflict(const struct sock *sk,
Alex Copotaacd9282012-04-12 22:21:45 +000057 const struct inet_bind_bucket *tb, bool relax)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070058{
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070059 struct sock *sk2;
60 struct hlist_node *node;
61 int reuse = sk->sk_reuse;
Tom Herbertda5e3632013-01-22 09:50:24 +000062 int reuseport = sk->sk_reuseport;
63 kuid_t uid = sock_i_uid((struct sock *)sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070064
Pavel Emelyanov7477fd2e2008-04-14 02:42:27 -070065 /*
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
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070072 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)) {
Tom Herbertda5e3632013-01-22 09:50:24 +000078 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))))) {
Eric Dumazet68835ab2010-11-30 19:04:07 +000083 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))
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070086 break;
David S. Miller8d238b22010-04-28 11:25:59 -070087 }
Alex Copotaacd9282012-04-12 22:21:45 +000088 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 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070096 }
97 }
98 return node != NULL;
99}
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -0800100EXPORT_SYMBOL_GPL(inet_csk_bind_conflict);
101
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700102/* Obtain a reference to a local port for the given sock,
103 * if snum is zero it means select any available local port.
104 */
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800105int inet_csk_get_port(struct sock *sk, unsigned short snum)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700106{
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -0700107 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700108 struct inet_bind_hashbucket *head;
109 struct hlist_node *node;
110 struct inet_bind_bucket *tb;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800111 int ret, attempts = 5;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900112 struct net *net = sock_net(sk);
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800113 int smallest_size = -1, smallest_rover;
Tom Herbertda5e3632013-01-22 09:50:24 +0000114 kuid_t uid = sock_i_uid(sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700115
116 local_bh_disable();
117 if (!snum) {
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700118 int remaining, rover, low, high;
119
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800120again:
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700121 inet_get_local_port_range(&low, &high);
Anton Arapova25de532007-10-18 22:00:17 -0700122 remaining = (high - low) + 1;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800123 smallest_rover = rover = net_random() % remaining + low;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700124
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800125 smallest_size = -1;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700126 do {
Amerigo Wange3826f12010-05-05 00:27:06 +0000127 if (inet_is_reserved_local_port(rover))
128 goto next_nolock;
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -0700129 head = &hashinfo->bhash[inet_bhashfn(net, rover,
130 hashinfo->bhash_size)];
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700131 spin_lock(&head->lock);
132 inet_bind_bucket_for_each(tb, node, &head->chain)
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800133 if (net_eq(ib_net(tb), net) && tb->port == rover) {
Tom Herbertda5e3632013-01-22 09:50:24 +0000134 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))) &&
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800140 (tb->num_owners < smallest_size || smallest_size == -1)) {
141 smallest_size = tb->num_owners;
142 smallest_rover = rover;
Alex Copotaacd9282012-04-12 22:21:45 +0000143 if (atomic_read(&hashinfo->bsockets) > (high - low) + 1 &&
144 !inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) {
David S. Miller8d238b22010-04-28 11:25:59 -0700145 snum = smallest_rover;
Flavio Leitnerfddb7b52012-01-25 08:34:52 +0000146 goto tb_found;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800147 }
148 }
Alex Copotaacd9282012-04-12 22:21:45 +0000149 if (!inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) {
Flavio Leitner2b05ad32012-01-25 08:34:51 +0000150 snum = rover;
Flavio Leitnerfddb7b52012-01-25 08:34:52 +0000151 goto tb_found;
Flavio Leitner2b05ad32012-01-25 08:34:51 +0000152 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700153 goto next;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800154 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700155 break;
156 next:
157 spin_unlock(&head->lock);
Amerigo Wange3826f12010-05-05 00:27:06 +0000158 next_nolock:
Stephen Hemminger6df71632005-11-03 16:33:23 -0800159 if (++rover > high)
160 rover = low;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700161 } while (--remaining > 0);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700162
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;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800170 if (remaining <= 0) {
171 if (smallest_size != -1) {
172 snum = smallest_rover;
173 goto have_snum;
174 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700175 goto fail;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800176 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700177 /* 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 {
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800182have_snum:
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -0700183 head = &hashinfo->bhash[inet_bhashfn(net, snum,
184 hashinfo->bhash_size)];
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700185 spin_lock(&head->lock);
186 inet_bind_bucket_for_each(tb, node, &head->chain)
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800187 if (net_eq(ib_net(tb), net) && tb->port == snum)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700188 goto tb_found;
189 }
190 tb = NULL;
191 goto tb_not_found;
192tb_found:
193 if (!hlist_empty(&tb->owners)) {
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +0000194 if (sk->sk_reuse == SK_FORCE_REUSE)
195 goto success;
196
Tom Herbertda5e3632013-01-22 09:50:24 +0000197 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))) &&
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800201 smallest_size == -1) {
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700202 goto success;
203 } else {
204 ret = 1;
Alex Copotaacd9282012-04-12 22:21:45 +0000205 if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, true)) {
Tom Herbertda5e3632013-01-22 09:50:24 +0000206 if (((sk->sk_reuse && sk->sk_state != TCP_LISTEN) ||
207 (sk->sk_reuseport && uid_eq(tb->fastuid, uid))) &&
Stephen Hemminger5add3002009-02-01 01:40:17 -0800208 smallest_size != -1 && --attempts >= 0) {
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800209 spin_unlock(&head->lock);
210 goto again;
211 }
Alex Copotaacd9282012-04-12 22:21:45 +0000212
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700213 goto fail_unlock;
Evgeniy Polyakova9d8f912009-01-19 16:46:02 -0800214 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700215 }
216 }
217tb_not_found:
218 ret = 1;
Pavel Emelyanov941b1d22008-01-31 05:05:50 -0800219 if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
220 net, head, snum)) == NULL)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700221 goto fail_unlock;
222 if (hlist_empty(&tb->owners)) {
223 if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
224 tb->fastreuse = 1;
225 else
226 tb->fastreuse = 0;
Tom Herbertda5e3632013-01-22 09:50:24 +0000227 if (sk->sk_reuseport) {
228 tb->fastreuseport = 1;
229 tb->fastuid = uid;
230 } else {
231 tb->fastreuseport = 0;
232 tb->fastuid = 0;
233 }
234 } else {
235 if (tb->fastreuse &&
236 (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
237 tb->fastreuse = 0;
238 if (tb->fastreuseport &&
239 (!sk->sk_reuseport || !uid_eq(tb->fastuid, uid))) {
240 tb->fastreuseport = 0;
241 tb->fastuid = 0;
242 }
243 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700244success:
245 if (!inet_csk(sk)->icsk_bind_hash)
246 inet_bind_hash(sk, tb, snum);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700247 WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900248 ret = 0;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700249
250fail_unlock:
251 spin_unlock(&head->lock);
252fail:
253 local_bh_enable();
254 return ret;
255}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700256EXPORT_SYMBOL_GPL(inet_csk_get_port);
257
258/*
259 * Wait for an incoming connection, avoid race conditions. This must be called
260 * with the socket locked.
261 */
262static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
263{
264 struct inet_connection_sock *icsk = inet_csk(sk);
265 DEFINE_WAIT(wait);
266 int err;
267
268 /*
269 * True wake-one mechanism for incoming connections: only
270 * one process gets woken up, not the 'whole herd'.
271 * Since we do not 'race & poll' for established sockets
272 * anymore, the common case will execute the loop only once.
273 *
274 * Subtle issue: "add_wait_queue_exclusive()" will be added
275 * after any current non-exclusive waiters, and we know that
276 * it will always _stay_ after any new non-exclusive waiters
277 * because all non-exclusive waiters are added at the
278 * beginning of the wait-queue. As such, it's ok to "drop"
279 * our exclusiveness temporarily when we get woken up without
280 * having to remove and re-insert us on the wait queue.
281 */
282 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000283 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700284 TASK_INTERRUPTIBLE);
285 release_sock(sk);
286 if (reqsk_queue_empty(&icsk->icsk_accept_queue))
287 timeo = schedule_timeout(timeo);
288 lock_sock(sk);
289 err = 0;
290 if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
291 break;
292 err = -EINVAL;
293 if (sk->sk_state != TCP_LISTEN)
294 break;
295 err = sock_intr_errno(timeo);
296 if (signal_pending(current))
297 break;
298 err = -EAGAIN;
299 if (!timeo)
300 break;
301 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000302 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700303 return err;
304}
305
306/*
307 * This will accept the next outstanding connection.
308 */
309struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
310{
311 struct inet_connection_sock *icsk = inet_csk(sk);
Jerry Chu83368862012-08-31 12:29:12 +0000312 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700313 struct sock *newsk;
Jerry Chu83368862012-08-31 12:29:12 +0000314 struct request_sock *req;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700315 int error;
316
317 lock_sock(sk);
318
319 /* We need to make sure that this socket is listening,
320 * and that it has something pending.
321 */
322 error = -EINVAL;
323 if (sk->sk_state != TCP_LISTEN)
324 goto out_err;
325
326 /* Find already established connection */
Jerry Chu83368862012-08-31 12:29:12 +0000327 if (reqsk_queue_empty(queue)) {
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700328 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
329
330 /* If this is a non blocking socket don't sleep */
331 error = -EAGAIN;
332 if (!timeo)
333 goto out_err;
334
335 error = inet_csk_wait_for_connect(sk, timeo);
336 if (error)
337 goto out_err;
338 }
Jerry Chu83368862012-08-31 12:29:12 +0000339 req = reqsk_queue_remove(queue);
340 newsk = req->sk;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700341
Jerry Chu83368862012-08-31 12:29:12 +0000342 sk_acceptq_removed(sk);
Eric Dumazet7ab45512012-09-06 08:07:13 +0000343 if (sk->sk_protocol == IPPROTO_TCP && queue->fastopenq != NULL) {
Jerry Chu83368862012-08-31 12:29:12 +0000344 spin_lock_bh(&queue->fastopenq->lock);
345 if (tcp_rsk(req)->listener) {
346 /* We are still waiting for the final ACK from 3WHS
347 * so can't free req now. Instead, we set req->sk to
348 * NULL to signify that the child socket is taken
349 * so reqsk_fastopen_remove() will free the req
350 * when 3WHS finishes (or is aborted).
351 */
352 req->sk = NULL;
353 req = NULL;
354 }
355 spin_unlock_bh(&queue->fastopenq->lock);
356 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700357out:
358 release_sock(sk);
Jerry Chu83368862012-08-31 12:29:12 +0000359 if (req)
360 __reqsk_free(req);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700361 return newsk;
362out_err:
363 newsk = NULL;
Jerry Chu83368862012-08-31 12:29:12 +0000364 req = NULL;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700365 *err = error;
366 goto out;
367}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700368EXPORT_SYMBOL(inet_csk_accept);
369
370/*
371 * Using different timers for retransmit, delayed acks and probes
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900372 * We may wish use just one timer maintaining a list of expire jiffies
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700373 * to optimize.
374 */
375void inet_csk_init_xmit_timers(struct sock *sk,
376 void (*retransmit_handler)(unsigned long),
377 void (*delack_handler)(unsigned long),
378 void (*keepalive_handler)(unsigned long))
379{
380 struct inet_connection_sock *icsk = inet_csk(sk);
381
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800382 setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler,
383 (unsigned long)sk);
384 setup_timer(&icsk->icsk_delack_timer, delack_handler,
385 (unsigned long)sk);
386 setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700387 icsk->icsk_pending = icsk->icsk_ack.pending = 0;
388}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700389EXPORT_SYMBOL(inet_csk_init_xmit_timers);
390
391void inet_csk_clear_xmit_timers(struct sock *sk)
392{
393 struct inet_connection_sock *icsk = inet_csk(sk);
394
395 icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
396
397 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
398 sk_stop_timer(sk, &icsk->icsk_delack_timer);
399 sk_stop_timer(sk, &sk->sk_timer);
400}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700401EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
402
403void inet_csk_delete_keepalive_timer(struct sock *sk)
404{
405 sk_stop_timer(sk, &sk->sk_timer);
406}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700407EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
408
409void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
410{
411 sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
412}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700413EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
414
Jianjun Kongd93191002008-11-03 00:23:42 -0800415struct dst_entry *inet_csk_route_req(struct sock *sk,
David S. Miller6bd023f2011-05-18 18:32:03 -0400416 struct flowi4 *fl4,
David S. Millerba3f7f02012-07-17 14:02:46 -0700417 const struct request_sock *req)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700418{
419 struct rtable *rt;
420 const struct inet_request_sock *ireq = inet_rsk(req);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000421 struct ip_options_rcu *opt = inet_rsk(req)->opt;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700422 struct net *net = sock_net(sk);
David S. Miller3e129392012-07-10 04:01:57 -0700423 int flags = inet_sk_flowi_flags(sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700424
David S. Miller6bd023f2011-05-18 18:32:03 -0400425 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
David S. Millere79d9bc2011-03-31 04:53:20 -0700426 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
Eric Dumazet74338192012-05-31 21:00:26 +0000427 sk->sk_protocol,
Eric Dumazet7586ece2012-06-20 05:02:19 +0000428 flags,
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000429 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr,
David S. Millere79d9bc2011-03-31 04:53:20 -0700430 ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport);
David S. Miller6bd023f2011-05-18 18:32:03 -0400431 security_req_classify_flow(req, flowi4_to_flowi(fl4));
432 rt = ip_route_output_flow(net, fl4, sk);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800433 if (IS_ERR(rt))
Ilpo Järvinen857a6e02008-12-14 23:13:08 -0800434 goto no_route;
Julian Anastasov155e8332012-10-08 11:41:18 +0000435 if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
Ilpo Järvinen857a6e02008-12-14 23:13:08 -0800436 goto route_err;
Changli Gaod8d1f302010-06-10 23:31:35 -0700437 return &rt->dst;
Ilpo Järvinen857a6e02008-12-14 23:13:08 -0800438
439route_err:
440 ip_rt_put(rt);
441no_route:
442 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
443 return NULL;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700444}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700445EXPORT_SYMBOL_GPL(inet_csk_route_req);
446
David S. Miller77357a92011-05-08 14:34:22 -0700447struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
448 struct sock *newsk,
449 const struct request_sock *req)
450{
451 const struct inet_request_sock *ireq = inet_rsk(req);
452 struct inet_sock *newinet = inet_sk(newsk);
Christoph Paasch1a7b27c2012-08-20 02:52:09 +0000453 struct ip_options_rcu *opt;
David S. Miller77357a92011-05-08 14:34:22 -0700454 struct net *net = sock_net(sk);
455 struct flowi4 *fl4;
456 struct rtable *rt;
457
458 fl4 = &newinet->cork.fl.u.ip4;
Christoph Paasch1a7b27c2012-08-20 02:52:09 +0000459
460 rcu_read_lock();
461 opt = rcu_dereference(newinet->inet_opt);
David S. Miller77357a92011-05-08 14:34:22 -0700462 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
463 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
464 sk->sk_protocol, inet_sk_flowi_flags(sk),
465 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr,
466 ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport);
467 security_req_classify_flow(req, flowi4_to_flowi(fl4));
468 rt = ip_route_output_flow(net, fl4, sk);
469 if (IS_ERR(rt))
470 goto no_route;
Julian Anastasov155e8332012-10-08 11:41:18 +0000471 if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
David S. Miller77357a92011-05-08 14:34:22 -0700472 goto route_err;
Christoph Paasch1a7b27c2012-08-20 02:52:09 +0000473 rcu_read_unlock();
David S. Miller77357a92011-05-08 14:34:22 -0700474 return &rt->dst;
475
476route_err:
477 ip_rt_put(rt);
478no_route:
Christoph Paasch1a7b27c2012-08-20 02:52:09 +0000479 rcu_read_unlock();
David S. Miller77357a92011-05-08 14:34:22 -0700480 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
481 return NULL;
482}
483EXPORT_SYMBOL_GPL(inet_csk_route_child_sock);
484
Al Viro6b729772006-09-27 18:36:59 -0700485static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800486 const u32 rnd, const u32 synq_hsize)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700487{
Al Viro6b729772006-09-27 18:36:59 -0700488 return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700489}
490
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000491#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700492#define AF_INET_FAMILY(fam) ((fam) == AF_INET)
493#else
494#define AF_INET_FAMILY(fam) 1
495#endif
496
497struct request_sock *inet_csk_search_req(const struct sock *sk,
498 struct request_sock ***prevp,
Al Viro6b729772006-09-27 18:36:59 -0700499 const __be16 rport, const __be32 raddr,
Al Viro7f25afb2006-09-27 18:27:47 -0700500 const __be32 laddr)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700501{
502 const struct inet_connection_sock *icsk = inet_csk(sk);
503 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
504 struct request_sock *req, **prev;
505
506 for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
507 lopt->nr_table_entries)];
508 (req = *prev) != NULL;
509 prev = &req->dl_next) {
510 const struct inet_request_sock *ireq = inet_rsk(req);
511
512 if (ireq->rmt_port == rport &&
513 ireq->rmt_addr == raddr &&
514 ireq->loc_addr == laddr &&
515 AF_INET_FAMILY(req->rsk_ops->family)) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700516 WARN_ON(req->sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700517 *prevp = prev;
518 break;
519 }
520 }
521
522 return req;
523}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700524EXPORT_SYMBOL_GPL(inet_csk_search_req);
525
526void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
Arnaldo Carvalho de Meloc2977c22005-12-13 23:15:12 -0800527 unsigned long timeout)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700528{
529 struct inet_connection_sock *icsk = inet_csk(sk);
530 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
531 const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
532 lopt->hash_rnd, lopt->nr_table_entries);
533
534 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
535 inet_csk_reqsk_queue_added(sk, timeout);
536}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000537EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700538
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700539/* Only thing we need from tcp.h */
540extern int sysctl_tcp_synack_retries;
541
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700542
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000543/* Decide when to expire the request and when to resend SYN-ACK */
544static inline void syn_ack_recalc(struct request_sock *req, const int thresh,
545 const int max_retries,
546 const u8 rskq_defer_accept,
547 int *expire, int *resend)
548{
549 if (!rskq_defer_accept) {
Eric Dumazete6c022a2012-10-27 23:16:46 +0000550 *expire = req->num_timeout >= thresh;
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000551 *resend = 1;
552 return;
553 }
Eric Dumazete6c022a2012-10-27 23:16:46 +0000554 *expire = req->num_timeout >= thresh &&
555 (!inet_rsk(req)->acked || req->num_timeout >= max_retries);
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000556 /*
557 * Do not resend while waiting for data after ACK,
558 * start to resend on end of deferring period to give
559 * last chance for data or ACK to create established socket.
560 */
561 *resend = !inet_rsk(req)->acked ||
Eric Dumazete6c022a2012-10-27 23:16:46 +0000562 req->num_timeout >= rskq_defer_accept - 1;
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000563}
564
Eric Dumazete6c022a2012-10-27 23:16:46 +0000565int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req)
566{
567 int err = req->rsk_ops->rtx_syn_ack(parent, req, NULL);
568
569 if (!err)
570 req->num_retrans++;
571 return err;
572}
573EXPORT_SYMBOL(inet_rtx_syn_ack);
574
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700575void inet_csk_reqsk_queue_prune(struct sock *parent,
576 const unsigned long interval,
577 const unsigned long timeout,
578 const unsigned long max_rto)
579{
580 struct inet_connection_sock *icsk = inet_csk(parent);
581 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
582 struct listen_sock *lopt = queue->listen_opt;
David S. Millerec0a1962008-06-12 16:31:35 -0700583 int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
584 int thresh = max_retries;
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700585 unsigned long now = jiffies;
586 struct request_sock **reqp, *req;
587 int i, budget;
588
589 if (lopt == NULL || lopt->qlen == 0)
590 return;
591
592 /* Normally all the openreqs are young and become mature
593 * (i.e. converted to established socket) for first timeout.
Eric Dumazetfd4f2ce2012-04-12 19:48:40 +0000594 * If synack was not acknowledged for 1 second, it means
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700595 * one of the following things: synack was lost, ack was lost,
596 * rtt is high or nobody planned to ack (i.e. synflood).
597 * When server is a bit loaded, queue is populated with old
598 * open requests, reducing effective size of queue.
599 * When server is well loaded, queue size reduces to zero
600 * after several minutes of work. It is not synflood,
601 * it is normal operation. The solution is pruning
602 * too old entries overriding normal timeout, when
603 * situation becomes dangerous.
604 *
605 * Essentially, we reserve half of room for young
606 * embrions; and abort old ones without pity, if old
607 * ones are about to clog our table.
608 */
609 if (lopt->qlen>>(lopt->max_qlen_log-1)) {
610 int young = (lopt->qlen_young<<1);
611
612 while (thresh > 2) {
613 if (lopt->qlen < young)
614 break;
615 thresh--;
616 young <<= 1;
617 }
618 }
619
David S. Millerec0a1962008-06-12 16:31:35 -0700620 if (queue->rskq_defer_accept)
621 max_retries = queue->rskq_defer_accept;
622
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700623 budget = 2 * (lopt->nr_table_entries / (timeout / interval));
624 i = lopt->clock_hand;
625
626 do {
627 reqp=&lopt->syn_table[i];
628 while ((req = *reqp) != NULL) {
629 if (time_after_eq(now, req->expires)) {
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000630 int expire = 0, resend = 0;
631
632 syn_ack_recalc(req, thresh, max_retries,
633 queue->rskq_defer_accept,
634 &expire, &resend);
Eric Dumazetc72e1182012-04-12 22:16:05 +0000635 req->rsk_ops->syn_ack_timeout(parent, req);
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000636 if (!expire &&
637 (!resend ||
Eric Dumazete6c022a2012-10-27 23:16:46 +0000638 !inet_rtx_syn_ack(parent, req) ||
Julian Anastasov0c3d79b2009-10-19 10:03:58 +0000639 inet_rsk(req)->acked)) {
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700640 unsigned long timeo;
641
Eric Dumazete6c022a2012-10-27 23:16:46 +0000642 if (req->num_timeout++ == 0)
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700643 lopt->qlen_young--;
Eric Dumazete6c022a2012-10-27 23:16:46 +0000644 timeo = min(timeout << req->num_timeout,
645 max_rto);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700646 req->expires = now + timeo;
647 reqp = &req->dl_next;
648 continue;
649 }
650
651 /* Drop this request */
652 inet_csk_reqsk_queue_unlink(parent, req, reqp);
653 reqsk_queue_removed(queue, req);
654 reqsk_free(req);
655 continue;
656 }
657 reqp = &req->dl_next;
658 }
659
660 i = (i + 1) & (lopt->nr_table_entries - 1);
661
662 } while (--budget > 0);
663
664 lopt->clock_hand = i;
665
666 if (lopt->qlen)
667 inet_csk_reset_keepalive_timer(parent, interval);
668}
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700669EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
670
Eric Dumazete56c57d2011-11-08 17:07:07 -0500671/**
672 * inet_csk_clone_lock - clone an inet socket, and lock its clone
673 * @sk: the socket to clone
674 * @req: request_sock
675 * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
676 *
677 * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
678 */
679struct sock *inet_csk_clone_lock(const struct sock *sk,
680 const struct request_sock *req,
681 const gfp_t priority)
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700682{
Eric Dumazete56c57d2011-11-08 17:07:07 -0500683 struct sock *newsk = sk_clone_lock(sk, priority);
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700684
685 if (newsk != NULL) {
686 struct inet_connection_sock *newicsk = inet_csk(newsk);
687
688 newsk->sk_state = TCP_SYN_RECV;
689 newicsk->icsk_bind_hash = NULL;
690
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000691 inet_sk(newsk)->inet_dport = inet_rsk(req)->rmt_port;
692 inet_sk(newsk)->inet_num = ntohs(inet_rsk(req)->loc_port);
693 inet_sk(newsk)->inet_sport = inet_rsk(req)->loc_port;
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700694 newsk->sk_write_space = sk_stream_write_space;
695
696 newicsk->icsk_retransmits = 0;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300697 newicsk->icsk_backoff = 0;
698 newicsk->icsk_probes_out = 0;
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700699
700 /* Deinitialize accept_queue to trap illegal accesses. */
701 memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
Venkat Yekkirala4237c752006-07-24 23:32:50 -0700702
703 security_inet_csk_clone(newsk, req);
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700704 }
705 return newsk;
706}
Eric Dumazete56c57d2011-11-08 17:07:07 -0500707EXPORT_SYMBOL_GPL(inet_csk_clone_lock);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700708
709/*
710 * At this point, there should be no process reference to this
711 * socket, and thus no user references at all. Therefore we
712 * can assume the socket waitqueue is inactive and nobody will
713 * try to jump onto it.
714 */
715void inet_csk_destroy_sock(struct sock *sk)
716{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700717 WARN_ON(sk->sk_state != TCP_CLOSE);
718 WARN_ON(!sock_flag(sk, SOCK_DEAD));
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700719
720 /* It cannot be in hash table! */
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700721 WARN_ON(!sk_unhashed(sk));
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700722
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000723 /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
724 WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700725
726 sk->sk_prot->destroy(sk);
727
728 sk_stream_kill_queues(sk);
729
730 xfrm_sk_free_policy(sk);
731
732 sk_refcnt_debug_release(sk);
733
Eric Dumazetdd24c002008-11-25 21:17:14 -0800734 percpu_counter_dec(sk->sk_prot->orphan_count);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700735 sock_put(sk);
736}
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700737EXPORT_SYMBOL(inet_csk_destroy_sock);
738
Christoph Paasche337e242012-12-14 04:07:58 +0000739/* This function allows to force a closure of a socket after the call to
740 * tcp/dccp_create_openreq_child().
741 */
742void inet_csk_prepare_forced_close(struct sock *sk)
743{
744 /* sk_clone_lock locked the socket and set refcnt to 2 */
745 bh_unlock_sock(sk);
746 sock_put(sk);
747
748 /* The below has to be done to allow calling inet_csk_destroy_sock */
749 sock_set_flag(sk, SOCK_DEAD);
750 percpu_counter_inc(sk->sk_prot->orphan_count);
751 inet_sk(sk)->inet_num = 0;
752}
753EXPORT_SYMBOL(inet_csk_prepare_forced_close);
754
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700755int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
756{
757 struct inet_sock *inet = inet_sk(sk);
758 struct inet_connection_sock *icsk = inet_csk(sk);
759 int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
760
761 if (rc != 0)
762 return rc;
763
764 sk->sk_max_ack_backlog = 0;
765 sk->sk_ack_backlog = 0;
766 inet_csk_delack_init(sk);
767
768 /* There is race window here: we announce ourselves listening,
769 * but this transition is still not validated by get_port().
770 * It is OK, because this socket enters to hash table only
771 * after validation is complete.
772 */
773 sk->sk_state = TCP_LISTEN;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000774 if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
775 inet->inet_sport = htons(inet->inet_num);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700776
777 sk_dst_reset(sk);
778 sk->sk_prot->hash(sk);
779
780 return 0;
781 }
782
783 sk->sk_state = TCP_CLOSE;
784 __reqsk_queue_destroy(&icsk->icsk_accept_queue);
785 return -EADDRINUSE;
786}
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700787EXPORT_SYMBOL_GPL(inet_csk_listen_start);
788
789/*
790 * This routine closes sockets which have been at least partially
791 * opened, but not yet accepted.
792 */
793void inet_csk_listen_stop(struct sock *sk)
794{
795 struct inet_connection_sock *icsk = inet_csk(sk);
Jerry Chu83368862012-08-31 12:29:12 +0000796 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700797 struct request_sock *acc_req;
798 struct request_sock *req;
799
800 inet_csk_delete_keepalive_timer(sk);
801
802 /* make all the listen_opt local to us */
Jerry Chu83368862012-08-31 12:29:12 +0000803 acc_req = reqsk_queue_yank_acceptq(queue);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700804
805 /* Following specs, it would be better either to send FIN
806 * (and enter FIN-WAIT-1, it is normal close)
807 * or to send active reset (abort).
808 * Certainly, it is pretty dangerous while synflood, but it is
809 * bad justification for our negligence 8)
810 * To be honest, we are not able to make either
811 * of the variants now. --ANK
812 */
Jerry Chu83368862012-08-31 12:29:12 +0000813 reqsk_queue_destroy(queue);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700814
815 while ((req = acc_req) != NULL) {
816 struct sock *child = req->sk;
817
818 acc_req = req->dl_next;
819
820 local_bh_disable();
821 bh_lock_sock(child);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700822 WARN_ON(sock_owned_by_user(child));
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700823 sock_hold(child);
824
825 sk->sk_prot->disconnect(child, O_NONBLOCK);
826
827 sock_orphan(child);
828
Herbert Xueb4dea52008-12-29 23:04:08 -0800829 percpu_counter_inc(sk->sk_prot->orphan_count);
830
Eric Dumazet7ab45512012-09-06 08:07:13 +0000831 if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->listener) {
Jerry Chu83368862012-08-31 12:29:12 +0000832 BUG_ON(tcp_sk(child)->fastopen_rsk != req);
833 BUG_ON(sk != tcp_rsk(req)->listener);
834
835 /* Paranoid, to prevent race condition if
836 * an inbound pkt destined for child is
837 * blocked by sock lock in tcp_v4_rcv().
838 * Also to satisfy an assertion in
839 * tcp_v4_destroy_sock().
840 */
841 tcp_sk(child)->fastopen_rsk = NULL;
842 sock_put(sk);
843 }
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700844 inet_csk_destroy_sock(child);
845
846 bh_unlock_sock(child);
847 local_bh_enable();
848 sock_put(child);
849
850 sk_acceptq_removed(sk);
851 __reqsk_free(req);
852 }
Jerry Chu83368862012-08-31 12:29:12 +0000853 if (queue->fastopenq != NULL) {
854 /* Free all the reqs queued in rskq_rst_head. */
855 spin_lock_bh(&queue->fastopenq->lock);
856 acc_req = queue->fastopenq->rskq_rst_head;
857 queue->fastopenq->rskq_rst_head = NULL;
858 spin_unlock_bh(&queue->fastopenq->lock);
859 while ((req = acc_req) != NULL) {
860 acc_req = req->dl_next;
861 __reqsk_free(req);
862 }
863 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700864 WARN_ON(sk->sk_ack_backlog);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700865}
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700866EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
Arnaldo Carvalho de Meloaf05dc92005-12-13 23:16:04 -0800867
868void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
869{
870 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
871 const struct inet_sock *inet = inet_sk(sk);
872
873 sin->sin_family = AF_INET;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000874 sin->sin_addr.s_addr = inet->inet_daddr;
875 sin->sin_port = inet->inet_dport;
Arnaldo Carvalho de Meloaf05dc92005-12-13 23:16:04 -0800876}
Arnaldo Carvalho de Meloaf05dc92005-12-13 23:16:04 -0800877EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
Arnaldo Carvalho de Meloc4d93902006-03-20 22:01:03 -0800878
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800879#ifdef CONFIG_COMPAT
880int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
881 char __user *optval, int __user *optlen)
882{
David S. Millerdbeff122006-03-20 22:52:32 -0800883 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800884
885 if (icsk->icsk_af_ops->compat_getsockopt != NULL)
886 return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
887 optval, optlen);
888 return icsk->icsk_af_ops->getsockopt(sk, level, optname,
889 optval, optlen);
890}
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800891EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
892
893int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700894 char __user *optval, unsigned int optlen)
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800895{
David S. Millerdbeff122006-03-20 22:52:32 -0800896 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800897
898 if (icsk->icsk_af_ops->compat_setsockopt != NULL)
899 return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
900 optval, optlen);
901 return icsk->icsk_af_ops->setsockopt(sk, level, optname,
902 optval, optlen);
903}
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800904EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
905#endif
David S. Miller80d0a692012-07-16 03:28:06 -0700906
907static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl)
908{
Eric Dumazet5abf7f72012-07-17 22:42:13 +0200909 const struct inet_sock *inet = inet_sk(sk);
910 const struct ip_options_rcu *inet_opt;
David S. Miller80d0a692012-07-16 03:28:06 -0700911 __be32 daddr = inet->inet_daddr;
912 struct flowi4 *fl4;
913 struct rtable *rt;
914
915 rcu_read_lock();
916 inet_opt = rcu_dereference(inet->inet_opt);
917 if (inet_opt && inet_opt->opt.srr)
918 daddr = inet_opt->opt.faddr;
919 fl4 = &fl->u.ip4;
920 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr,
921 inet->inet_saddr, inet->inet_dport,
922 inet->inet_sport, sk->sk_protocol,
923 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
924 if (IS_ERR(rt))
925 rt = NULL;
926 if (rt)
927 sk_setup_caps(sk, &rt->dst);
928 rcu_read_unlock();
929
930 return &rt->dst;
931}
932
933struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu)
934{
935 struct dst_entry *dst = __sk_dst_check(sk, 0);
936 struct inet_sock *inet = inet_sk(sk);
937
938 if (!dst) {
939 dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
940 if (!dst)
941 goto out;
942 }
David S. Miller6700c272012-07-17 03:29:28 -0700943 dst->ops->update_pmtu(dst, sk, NULL, mtu);
David S. Miller80d0a692012-07-16 03:28:06 -0700944
945 dst = __sk_dst_check(sk, 0);
946 if (!dst)
947 dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
948out:
949 return dst;
950}
951EXPORT_SYMBOL_GPL(inet_csk_update_pmtu);