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