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