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