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