blob: b189278c7bc180a882b1ebb30ecd19ca19010199 [file] [log] [blame]
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Support for INET connection oriented protocols.
7 *
8 * Authors: See the TCP sources
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or(at your option) any later version.
14 */
15
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070016#include <linux/module.h>
17#include <linux/jhash.h>
18
19#include <net/inet_connection_sock.h>
20#include <net/inet_hashtables.h>
21#include <net/inet_timewait_sock.h>
22#include <net/ip.h>
23#include <net/route.h>
24#include <net/tcp_states.h>
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -070025#include <net/xfrm.h>
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070026
27#ifdef INET_CSK_DEBUG
28const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
29EXPORT_SYMBOL(inet_csk_timer_bug_msg);
30#endif
31
32/*
33 * This array holds the first and last local port number.
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070034 */
Mark Glines3f196eb2007-05-31 15:44:48 -070035int sysctl_local_port_range[2] = { 32768, 61000 };
Stephen Hemminger227b60f2007-10-10 17:30:46 -070036DEFINE_SEQLOCK(sysctl_port_range_lock);
37
38void 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}
48EXPORT_SYMBOL(inet_get_local_port_range);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070049
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -080050int inet_csk_bind_conflict(const struct sock *sk,
51 const struct inet_bind_bucket *tb)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070052{
Al Viro82103232006-09-27 18:44:10 -070053 const __be32 sk_rcv_saddr = inet_rcv_saddr(sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070054 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) {
Al Viro82103232006-09-27 18:44:10 -070066 const __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070067 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
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -080076EXPORT_SYMBOL_GPL(inet_csk_bind_conflict);
77
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070078/* Obtain a reference to a local port for the given sock,
79 * if snum is zero it means select any available local port.
80 */
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -080081int inet_csk_get_port(struct sock *sk, unsigned short snum)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070082{
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -080083 struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070084 struct inet_bind_hashbucket *head;
85 struct hlist_node *node;
86 struct inet_bind_bucket *tb;
87 int ret;
Pavel Emelyanov941b1d22008-01-31 05:05:50 -080088 struct net *net = sk->sk_net;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070089
90 local_bh_disable();
91 if (!snum) {
Stephen Hemminger227b60f2007-10-10 17:30:46 -070092 int remaining, rover, low, high;
93
94 inet_get_local_port_range(&low, &high);
Anton Arapova25de532007-10-18 22:00:17 -070095 remaining = (high - low) + 1;
Stephen Hemminger227b60f2007-10-10 17:30:46 -070096 rover = net_random() % remaining + low;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070097
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070098 do {
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070099 head = &hashinfo->bhash[inet_bhashfn(rover, hashinfo->bhash_size)];
100 spin_lock(&head->lock);
101 inet_bind_bucket_for_each(tb, node, &head->chain)
Pavel Emelyanov941b1d22008-01-31 05:05:50 -0800102 if (tb->ib_net == net && tb->port == rover)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700103 goto next;
104 break;
105 next:
106 spin_unlock(&head->lock);
Stephen Hemminger6df71632005-11-03 16:33:23 -0800107 if (++rover > high)
108 rover = low;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700109 } while (--remaining > 0);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700110
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)
Pavel Emelyanov941b1d22008-01-31 05:05:50 -0800129 if (tb->ib_net == net && tb->port == snum)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700130 goto tb_found;
131 }
132 tb = NULL;
133 goto tb_not_found;
134tb_found:
135 if (!hlist_empty(&tb->owners)) {
136 if (sk->sk_reuse > 1)
137 goto success;
138 if (tb->fastreuse > 0 &&
139 sk->sk_reuse && sk->sk_state != TCP_LISTEN) {
140 goto success;
141 } else {
142 ret = 1;
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800143 if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb))
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700144 goto fail_unlock;
145 }
146 }
147tb_not_found:
148 ret = 1;
Pavel Emelyanov941b1d22008-01-31 05:05:50 -0800149 if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
150 net, head, snum)) == NULL)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700151 goto fail_unlock;
152 if (hlist_empty(&tb->owners)) {
153 if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
154 tb->fastreuse = 1;
155 else
156 tb->fastreuse = 0;
157 } else if (tb->fastreuse &&
158 (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
159 tb->fastreuse = 0;
160success:
161 if (!inet_csk(sk)->icsk_bind_hash)
162 inet_bind_hash(sk, tb, snum);
163 BUG_TRAP(inet_csk(sk)->icsk_bind_hash == tb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900164 ret = 0;
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700165
166fail_unlock:
167 spin_unlock(&head->lock);
168fail:
169 local_bh_enable();
170 return ret;
171}
172
173EXPORT_SYMBOL_GPL(inet_csk_get_port);
174
175/*
176 * Wait for an incoming connection, avoid race conditions. This must be called
177 * with the socket locked.
178 */
179static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
180{
181 struct inet_connection_sock *icsk = inet_csk(sk);
182 DEFINE_WAIT(wait);
183 int err;
184
185 /*
186 * True wake-one mechanism for incoming connections: only
187 * one process gets woken up, not the 'whole herd'.
188 * Since we do not 'race & poll' for established sockets
189 * anymore, the common case will execute the loop only once.
190 *
191 * Subtle issue: "add_wait_queue_exclusive()" will be added
192 * after any current non-exclusive waiters, and we know that
193 * it will always _stay_ after any new non-exclusive waiters
194 * because all non-exclusive waiters are added at the
195 * beginning of the wait-queue. As such, it's ok to "drop"
196 * our exclusiveness temporarily when we get woken up without
197 * having to remove and re-insert us on the wait queue.
198 */
199 for (;;) {
200 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
201 TASK_INTERRUPTIBLE);
202 release_sock(sk);
203 if (reqsk_queue_empty(&icsk->icsk_accept_queue))
204 timeo = schedule_timeout(timeo);
205 lock_sock(sk);
206 err = 0;
207 if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
208 break;
209 err = -EINVAL;
210 if (sk->sk_state != TCP_LISTEN)
211 break;
212 err = sock_intr_errno(timeo);
213 if (signal_pending(current))
214 break;
215 err = -EAGAIN;
216 if (!timeo)
217 break;
218 }
219 finish_wait(sk->sk_sleep, &wait);
220 return err;
221}
222
223/*
224 * This will accept the next outstanding connection.
225 */
226struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
227{
228 struct inet_connection_sock *icsk = inet_csk(sk);
229 struct sock *newsk;
230 int error;
231
232 lock_sock(sk);
233
234 /* We need to make sure that this socket is listening,
235 * and that it has something pending.
236 */
237 error = -EINVAL;
238 if (sk->sk_state != TCP_LISTEN)
239 goto out_err;
240
241 /* Find already established connection */
242 if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
243 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
244
245 /* If this is a non blocking socket don't sleep */
246 error = -EAGAIN;
247 if (!timeo)
248 goto out_err;
249
250 error = inet_csk_wait_for_connect(sk, timeo);
251 if (error)
252 goto out_err;
253 }
254
255 newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
256 BUG_TRAP(newsk->sk_state != TCP_SYN_RECV);
257out:
258 release_sock(sk);
259 return newsk;
260out_err:
261 newsk = NULL;
262 *err = error;
263 goto out;
264}
265
266EXPORT_SYMBOL(inet_csk_accept);
267
268/*
269 * Using different timers for retransmit, delayed acks and probes
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900270 * We may wish use just one timer maintaining a list of expire jiffies
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700271 * to optimize.
272 */
273void inet_csk_init_xmit_timers(struct sock *sk,
274 void (*retransmit_handler)(unsigned long),
275 void (*delack_handler)(unsigned long),
276 void (*keepalive_handler)(unsigned long))
277{
278 struct inet_connection_sock *icsk = inet_csk(sk);
279
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800280 setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler,
281 (unsigned long)sk);
282 setup_timer(&icsk->icsk_delack_timer, delack_handler,
283 (unsigned long)sk);
284 setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700285 icsk->icsk_pending = icsk->icsk_ack.pending = 0;
286}
287
288EXPORT_SYMBOL(inet_csk_init_xmit_timers);
289
290void inet_csk_clear_xmit_timers(struct sock *sk)
291{
292 struct inet_connection_sock *icsk = inet_csk(sk);
293
294 icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
295
296 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
297 sk_stop_timer(sk, &icsk->icsk_delack_timer);
298 sk_stop_timer(sk, &sk->sk_timer);
299}
300
301EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
302
303void inet_csk_delete_keepalive_timer(struct sock *sk)
304{
305 sk_stop_timer(sk, &sk->sk_timer);
306}
307
308EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
309
310void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
311{
312 sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
313}
314
315EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
316
317struct dst_entry* inet_csk_route_req(struct sock *sk,
318 const struct request_sock *req)
319{
320 struct rtable *rt;
321 const struct inet_request_sock *ireq = inet_rsk(req);
322 struct ip_options *opt = inet_rsk(req)->opt;
323 struct flowi fl = { .oif = sk->sk_bound_dev_if,
324 .nl_u = { .ip4_u =
325 { .daddr = ((opt && opt->srr) ?
326 opt->faddr :
327 ireq->rmt_addr),
328 .saddr = ireq->loc_addr,
329 .tos = RT_CONN_FLAGS(sk) } },
330 .proto = sk->sk_protocol,
331 .uli_u = { .ports =
332 { .sport = inet_sk(sk)->sport,
333 .dport = ireq->rmt_port } } };
334
Venkat Yekkirala4237c752006-07-24 23:32:50 -0700335 security_req_classify_flow(req, &fl);
Denis V. Lunevf1b050b2008-01-22 22:07:10 -0800336 if (ip_route_output_flow(&init_net, &rt, &fl, sk, 0)) {
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700337 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
338 return NULL;
339 }
340 if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
341 ip_rt_put(rt);
342 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
343 return NULL;
344 }
345 return &rt->u.dst;
346}
347
348EXPORT_SYMBOL_GPL(inet_csk_route_req);
349
Al Viro6b729772006-09-27 18:36:59 -0700350static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800351 const u32 rnd, const u32 synq_hsize)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700352{
Al Viro6b729772006-09-27 18:36:59 -0700353 return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700354}
355
356#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
357#define AF_INET_FAMILY(fam) ((fam) == AF_INET)
358#else
359#define AF_INET_FAMILY(fam) 1
360#endif
361
362struct request_sock *inet_csk_search_req(const struct sock *sk,
363 struct request_sock ***prevp,
Al Viro6b729772006-09-27 18:36:59 -0700364 const __be16 rport, const __be32 raddr,
Al Viro7f25afb2006-09-27 18:27:47 -0700365 const __be32 laddr)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700366{
367 const struct inet_connection_sock *icsk = inet_csk(sk);
368 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
369 struct request_sock *req, **prev;
370
371 for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
372 lopt->nr_table_entries)];
373 (req = *prev) != NULL;
374 prev = &req->dl_next) {
375 const struct inet_request_sock *ireq = inet_rsk(req);
376
377 if (ireq->rmt_port == rport &&
378 ireq->rmt_addr == raddr &&
379 ireq->loc_addr == laddr &&
380 AF_INET_FAMILY(req->rsk_ops->family)) {
381 BUG_TRAP(!req->sk);
382 *prevp = prev;
383 break;
384 }
385 }
386
387 return req;
388}
389
390EXPORT_SYMBOL_GPL(inet_csk_search_req);
391
392void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
Arnaldo Carvalho de Meloc2977c22005-12-13 23:15:12 -0800393 unsigned long timeout)
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700394{
395 struct inet_connection_sock *icsk = inet_csk(sk);
396 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
397 const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
398 lopt->hash_rnd, lopt->nr_table_entries);
399
400 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
401 inet_csk_reqsk_queue_added(sk, timeout);
402}
403
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700404/* Only thing we need from tcp.h */
405extern int sysctl_tcp_synack_retries;
406
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700407EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700408
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700409void inet_csk_reqsk_queue_prune(struct sock *parent,
410 const unsigned long interval,
411 const unsigned long timeout,
412 const unsigned long max_rto)
413{
414 struct inet_connection_sock *icsk = inet_csk(parent);
415 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
416 struct listen_sock *lopt = queue->listen_opt;
417 int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
418 int thresh = max_retries;
419 unsigned long now = jiffies;
420 struct request_sock **reqp, *req;
421 int i, budget;
422
423 if (lopt == NULL || lopt->qlen == 0)
424 return;
425
426 /* Normally all the openreqs are young and become mature
427 * (i.e. converted to established socket) for first timeout.
428 * If synack was not acknowledged for 3 seconds, it means
429 * one of the following things: synack was lost, ack was lost,
430 * rtt is high or nobody planned to ack (i.e. synflood).
431 * When server is a bit loaded, queue is populated with old
432 * open requests, reducing effective size of queue.
433 * When server is well loaded, queue size reduces to zero
434 * after several minutes of work. It is not synflood,
435 * it is normal operation. The solution is pruning
436 * too old entries overriding normal timeout, when
437 * situation becomes dangerous.
438 *
439 * Essentially, we reserve half of room for young
440 * embrions; and abort old ones without pity, if old
441 * ones are about to clog our table.
442 */
443 if (lopt->qlen>>(lopt->max_qlen_log-1)) {
444 int young = (lopt->qlen_young<<1);
445
446 while (thresh > 2) {
447 if (lopt->qlen < young)
448 break;
449 thresh--;
450 young <<= 1;
451 }
452 }
453
454 if (queue->rskq_defer_accept)
455 max_retries = queue->rskq_defer_accept;
456
457 budget = 2 * (lopt->nr_table_entries / (timeout / interval));
458 i = lopt->clock_hand;
459
460 do {
461 reqp=&lopt->syn_table[i];
462 while ((req = *reqp) != NULL) {
463 if (time_after_eq(now, req->expires)) {
464 if ((req->retrans < thresh ||
465 (inet_rsk(req)->acked && req->retrans < max_retries))
466 && !req->rsk_ops->rtx_syn_ack(parent, req, NULL)) {
467 unsigned long timeo;
468
469 if (req->retrans++ == 0)
470 lopt->qlen_young--;
471 timeo = min((timeout << req->retrans), max_rto);
472 req->expires = now + timeo;
473 reqp = &req->dl_next;
474 continue;
475 }
476
477 /* Drop this request */
478 inet_csk_reqsk_queue_unlink(parent, req, reqp);
479 reqsk_queue_removed(queue, req);
480 reqsk_free(req);
481 continue;
482 }
483 reqp = &req->dl_next;
484 }
485
486 i = (i + 1) & (lopt->nr_table_entries - 1);
487
488 } while (--budget > 0);
489
490 lopt->clock_hand = i;
491
492 if (lopt->qlen)
493 inet_csk_reset_keepalive_timer(parent, interval);
494}
495
496EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
497
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700498struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
Al Virodd0fc662005-10-07 07:46:04 +0100499 const gfp_t priority)
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700500{
501 struct sock *newsk = sk_clone(sk, priority);
502
503 if (newsk != NULL) {
504 struct inet_connection_sock *newicsk = inet_csk(newsk);
505
506 newsk->sk_state = TCP_SYN_RECV;
507 newicsk->icsk_bind_hash = NULL;
508
509 inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
510 newsk->sk_write_space = sk_stream_write_space;
511
512 newicsk->icsk_retransmits = 0;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300513 newicsk->icsk_backoff = 0;
514 newicsk->icsk_probes_out = 0;
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700515
516 /* Deinitialize accept_queue to trap illegal accesses. */
517 memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
Venkat Yekkirala4237c752006-07-24 23:32:50 -0700518
519 security_inet_csk_clone(newsk, req);
Arnaldo Carvalho de Melo9f1d2602005-08-09 20:11:24 -0700520 }
521 return newsk;
522}
523
524EXPORT_SYMBOL_GPL(inet_csk_clone);
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700525
526/*
527 * At this point, there should be no process reference to this
528 * socket, and thus no user references at all. Therefore we
529 * can assume the socket waitqueue is inactive and nobody will
530 * try to jump onto it.
531 */
532void inet_csk_destroy_sock(struct sock *sk)
533{
534 BUG_TRAP(sk->sk_state == TCP_CLOSE);
535 BUG_TRAP(sock_flag(sk, SOCK_DEAD));
536
537 /* It cannot be in hash table! */
538 BUG_TRAP(sk_unhashed(sk));
539
540 /* If it has not 0 inet_sk(sk)->num, it must be bound */
541 BUG_TRAP(!inet_sk(sk)->num || inet_csk(sk)->icsk_bind_hash);
542
543 sk->sk_prot->destroy(sk);
544
545 sk_stream_kill_queues(sk);
546
547 xfrm_sk_free_policy(sk);
548
549 sk_refcnt_debug_release(sk);
550
551 atomic_dec(sk->sk_prot->orphan_count);
552 sock_put(sk);
553}
554
555EXPORT_SYMBOL(inet_csk_destroy_sock);
556
557int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
558{
559 struct inet_sock *inet = inet_sk(sk);
560 struct inet_connection_sock *icsk = inet_csk(sk);
561 int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
562
563 if (rc != 0)
564 return rc;
565
566 sk->sk_max_ack_backlog = 0;
567 sk->sk_ack_backlog = 0;
568 inet_csk_delack_init(sk);
569
570 /* There is race window here: we announce ourselves listening,
571 * but this transition is still not validated by get_port().
572 * It is OK, because this socket enters to hash table only
573 * after validation is complete.
574 */
575 sk->sk_state = TCP_LISTEN;
576 if (!sk->sk_prot->get_port(sk, inet->num)) {
577 inet->sport = htons(inet->num);
578
579 sk_dst_reset(sk);
580 sk->sk_prot->hash(sk);
581
582 return 0;
583 }
584
585 sk->sk_state = TCP_CLOSE;
586 __reqsk_queue_destroy(&icsk->icsk_accept_queue);
587 return -EADDRINUSE;
588}
589
590EXPORT_SYMBOL_GPL(inet_csk_listen_start);
591
592/*
593 * This routine closes sockets which have been at least partially
594 * opened, but not yet accepted.
595 */
596void inet_csk_listen_stop(struct sock *sk)
597{
598 struct inet_connection_sock *icsk = inet_csk(sk);
599 struct request_sock *acc_req;
600 struct request_sock *req;
601
602 inet_csk_delete_keepalive_timer(sk);
603
604 /* make all the listen_opt local to us */
605 acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
606
607 /* Following specs, it would be better either to send FIN
608 * (and enter FIN-WAIT-1, it is normal close)
609 * or to send active reset (abort).
610 * Certainly, it is pretty dangerous while synflood, but it is
611 * bad justification for our negligence 8)
612 * To be honest, we are not able to make either
613 * of the variants now. --ANK
614 */
615 reqsk_queue_destroy(&icsk->icsk_accept_queue);
616
617 while ((req = acc_req) != NULL) {
618 struct sock *child = req->sk;
619
620 acc_req = req->dl_next;
621
622 local_bh_disable();
623 bh_lock_sock(child);
624 BUG_TRAP(!sock_owned_by_user(child));
625 sock_hold(child);
626
627 sk->sk_prot->disconnect(child, O_NONBLOCK);
628
629 sock_orphan(child);
630
631 atomic_inc(sk->sk_prot->orphan_count);
632
633 inet_csk_destroy_sock(child);
634
635 bh_unlock_sock(child);
636 local_bh_enable();
637 sock_put(child);
638
639 sk_acceptq_removed(sk);
640 __reqsk_free(req);
641 }
642 BUG_TRAP(!sk->sk_ack_backlog);
643}
644
645EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
Arnaldo Carvalho de Meloaf05dc92005-12-13 23:16:04 -0800646
647void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
648{
649 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
650 const struct inet_sock *inet = inet_sk(sk);
651
652 sin->sin_family = AF_INET;
653 sin->sin_addr.s_addr = inet->daddr;
654 sin->sin_port = inet->dport;
655}
656
657EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
Arnaldo Carvalho de Meloc4d93902006-03-20 22:01:03 -0800658
659int inet_csk_ctl_sock_create(struct socket **sock, unsigned short family,
660 unsigned short type, unsigned char protocol)
661{
662 int rc = sock_create_kern(family, type, protocol, sock);
663
664 if (rc == 0) {
665 (*sock)->sk->sk_allocation = GFP_ATOMIC;
666 inet_sk((*sock)->sk)->uc_ttl = -1;
667 /*
668 * Unhash it so that IP input processing does not even see it,
669 * we do not wish this socket to see incoming packets.
670 */
671 (*sock)->sk->sk_prot->unhash((*sock)->sk);
672 }
673 return rc;
674}
675
676EXPORT_SYMBOL_GPL(inet_csk_ctl_sock_create);
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800677
678#ifdef CONFIG_COMPAT
679int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
680 char __user *optval, int __user *optlen)
681{
David S. Millerdbeff122006-03-20 22:52:32 -0800682 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800683
684 if (icsk->icsk_af_ops->compat_getsockopt != NULL)
685 return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
686 optval, optlen);
687 return icsk->icsk_af_ops->getsockopt(sk, level, optname,
688 optval, optlen);
689}
690
691EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
692
693int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
694 char __user *optval, int optlen)
695{
David S. Millerdbeff122006-03-20 22:52:32 -0800696 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800697
698 if (icsk->icsk_af_ops->compat_setsockopt != NULL)
699 return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
700 optval, optlen);
701 return icsk->icsk_af_ops->setsockopt(sk, level, optname,
702 optval, optlen);
703}
704
705EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
706#endif