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.
6 * Support for INET connection oriented protocols.
8 * Authors: See the TCP sources
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.
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/jhash.h>
20 #include <net/inet_connection_sock.h>
21 #include <net/inet_hashtables.h>
22 #include <net/inet_timewait_sock.h>
24 #include <net/route.h>
25 #include <net/tcp_states.h>
29 const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
30 EXPORT_SYMBOL(inet_csk_timer_bug_msg);
34 * This array holds the first and last local port number.
35 * For high-usage systems, use sysctl to change this to
38 int sysctl_local_port_range[2] = { 1024, 4999 };
40 static inline int inet_csk_bind_conflict(struct sock *sk, struct inet_bind_bucket *tb)
42 const u32 sk_rcv_saddr = inet_rcv_saddr(sk);
44 struct hlist_node *node;
45 int reuse = sk->sk_reuse;
47 sk_for_each_bound(sk2, node, &tb->owners) {
49 !inet_v6_ipv6only(sk2) &&
50 (!sk->sk_bound_dev_if ||
51 !sk2->sk_bound_dev_if ||
52 sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
53 if (!reuse || !sk2->sk_reuse ||
54 sk2->sk_state == TCP_LISTEN) {
55 const u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
56 if (!sk2_rcv_saddr || !sk_rcv_saddr ||
57 sk2_rcv_saddr == sk_rcv_saddr)
65 /* Obtain a reference to a local port for the given sock,
66 * if snum is zero it means select any available local port.
68 int inet_csk_get_port(struct inet_hashinfo *hashinfo,
69 struct sock *sk, unsigned short snum)
71 struct inet_bind_hashbucket *head;
72 struct hlist_node *node;
73 struct inet_bind_bucket *tb;
78 int low = sysctl_local_port_range[0];
79 int high = sysctl_local_port_range[1];
80 int remaining = (high - low) + 1;
83 spin_lock(&hashinfo->portalloc_lock);
84 if (hashinfo->port_rover < low)
87 rover = hashinfo->port_rover;
92 head = &hashinfo->bhash[inet_bhashfn(rover, hashinfo->bhash_size)];
93 spin_lock(&head->lock);
94 inet_bind_bucket_for_each(tb, node, &head->chain)
95 if (tb->port == rover)
99 spin_unlock(&head->lock);
100 } while (--remaining > 0);
101 hashinfo->port_rover = rover;
102 spin_unlock(&hashinfo->portalloc_lock);
104 /* Exhausted local port range during search? It is not
105 * possible for us to be holding one of the bind hash
106 * locks if this test triggers, because if 'remaining'
107 * drops to zero, we broke out of the do/while loop at
108 * the top level, not from the 'break;' statement.
114 /* OK, here is the one we will use. HEAD is
115 * non-NULL and we hold it's mutex.
119 head = &hashinfo->bhash[inet_bhashfn(snum, hashinfo->bhash_size)];
120 spin_lock(&head->lock);
121 inet_bind_bucket_for_each(tb, node, &head->chain)
122 if (tb->port == snum)
128 if (!hlist_empty(&tb->owners)) {
129 if (sk->sk_reuse > 1)
131 if (tb->fastreuse > 0 &&
132 sk->sk_reuse && sk->sk_state != TCP_LISTEN) {
136 if (inet_csk_bind_conflict(sk, tb))
142 if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep, head, snum)) == NULL)
144 if (hlist_empty(&tb->owners)) {
145 if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
149 } else if (tb->fastreuse &&
150 (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
153 if (!inet_csk(sk)->icsk_bind_hash)
154 inet_bind_hash(sk, tb, snum);
155 BUG_TRAP(inet_csk(sk)->icsk_bind_hash == tb);
159 spin_unlock(&head->lock);
165 EXPORT_SYMBOL_GPL(inet_csk_get_port);
168 * Wait for an incoming connection, avoid race conditions. This must be called
169 * with the socket locked.
171 static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
173 struct inet_connection_sock *icsk = inet_csk(sk);
178 * True wake-one mechanism for incoming connections: only
179 * one process gets woken up, not the 'whole herd'.
180 * Since we do not 'race & poll' for established sockets
181 * anymore, the common case will execute the loop only once.
183 * Subtle issue: "add_wait_queue_exclusive()" will be added
184 * after any current non-exclusive waiters, and we know that
185 * it will always _stay_ after any new non-exclusive waiters
186 * because all non-exclusive waiters are added at the
187 * beginning of the wait-queue. As such, it's ok to "drop"
188 * our exclusiveness temporarily when we get woken up without
189 * having to remove and re-insert us on the wait queue.
192 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
195 if (reqsk_queue_empty(&icsk->icsk_accept_queue))
196 timeo = schedule_timeout(timeo);
199 if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
202 if (sk->sk_state != TCP_LISTEN)
204 err = sock_intr_errno(timeo);
205 if (signal_pending(current))
211 finish_wait(sk->sk_sleep, &wait);
216 * This will accept the next outstanding connection.
218 struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
220 struct inet_connection_sock *icsk = inet_csk(sk);
226 /* We need to make sure that this socket is listening,
227 * and that it has something pending.
230 if (sk->sk_state != TCP_LISTEN)
233 /* Find already established connection */
234 if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
235 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
237 /* If this is a non blocking socket don't sleep */
242 error = inet_csk_wait_for_connect(sk, timeo);
247 newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
248 BUG_TRAP(newsk->sk_state != TCP_SYN_RECV);
258 EXPORT_SYMBOL(inet_csk_accept);
261 * Using different timers for retransmit, delayed acks and probes
262 * We may wish use just one timer maintaining a list of expire jiffies
265 void inet_csk_init_xmit_timers(struct sock *sk,
266 void (*retransmit_handler)(unsigned long),
267 void (*delack_handler)(unsigned long),
268 void (*keepalive_handler)(unsigned long))
270 struct inet_connection_sock *icsk = inet_csk(sk);
272 init_timer(&icsk->icsk_retransmit_timer);
273 init_timer(&icsk->icsk_delack_timer);
274 init_timer(&sk->sk_timer);
276 icsk->icsk_retransmit_timer.function = retransmit_handler;
277 icsk->icsk_delack_timer.function = delack_handler;
278 sk->sk_timer.function = keepalive_handler;
280 icsk->icsk_retransmit_timer.data =
281 icsk->icsk_delack_timer.data =
282 sk->sk_timer.data = (unsigned long)sk;
284 icsk->icsk_pending = icsk->icsk_ack.pending = 0;
287 EXPORT_SYMBOL(inet_csk_init_xmit_timers);
289 void inet_csk_clear_xmit_timers(struct sock *sk)
291 struct inet_connection_sock *icsk = inet_csk(sk);
293 icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
295 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
296 sk_stop_timer(sk, &icsk->icsk_delack_timer);
297 sk_stop_timer(sk, &sk->sk_timer);
300 EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
302 void inet_csk_delete_keepalive_timer(struct sock *sk)
304 sk_stop_timer(sk, &sk->sk_timer);
307 EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
309 void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
311 sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
314 EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
316 struct dst_entry* inet_csk_route_req(struct sock *sk,
317 const struct request_sock *req)
320 const struct inet_request_sock *ireq = inet_rsk(req);
321 struct ip_options *opt = inet_rsk(req)->opt;
322 struct flowi fl = { .oif = sk->sk_bound_dev_if,
324 { .daddr = ((opt && opt->srr) ?
327 .saddr = ireq->loc_addr,
328 .tos = RT_CONN_FLAGS(sk) } },
329 .proto = sk->sk_protocol,
331 { .sport = inet_sk(sk)->sport,
332 .dport = ireq->rmt_port } } };
334 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
335 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
338 if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
340 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
346 EXPORT_SYMBOL_GPL(inet_csk_route_req);
348 static inline u32 inet_synq_hash(const u32 raddr, const u16 rport,
349 const u32 rnd, const u16 synq_hsize)
351 return jhash_2words(raddr, (u32)rport, rnd) & (synq_hsize - 1);
354 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
355 #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
357 #define AF_INET_FAMILY(fam) 1
360 struct request_sock *inet_csk_search_req(const struct sock *sk,
361 struct request_sock ***prevp,
362 const __u16 rport, const __u32 raddr,
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;
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);
375 if (ireq->rmt_port == rport &&
376 ireq->rmt_addr == raddr &&
377 ireq->loc_addr == laddr &&
378 AF_INET_FAMILY(req->rsk_ops->family)) {
388 EXPORT_SYMBOL_GPL(inet_csk_search_req);
390 void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
391 const unsigned timeout)
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);
398 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
399 inet_csk_reqsk_queue_added(sk, timeout);
402 /* Only thing we need from tcp.h */
403 extern int sysctl_tcp_synack_retries;
405 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
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)
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;
415 int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
416 int thresh = max_retries;
417 unsigned long now = jiffies;
418 struct request_sock **reqp, *req;
421 if (lopt == NULL || lopt->qlen == 0)
424 /* Normally all the openreqs are young and become mature
425 * (i.e. converted to established socket) for first timeout.
426 * If synack was not acknowledged for 3 seconds, it means
427 * one of the following things: synack was lost, ack was lost,
428 * rtt is high or nobody planned to ack (i.e. synflood).
429 * When server is a bit loaded, queue is populated with old
430 * open requests, reducing effective size of queue.
431 * When server is well loaded, queue size reduces to zero
432 * after several minutes of work. It is not synflood,
433 * it is normal operation. The solution is pruning
434 * too old entries overriding normal timeout, when
435 * situation becomes dangerous.
437 * Essentially, we reserve half of room for young
438 * embrions; and abort old ones without pity, if old
439 * ones are about to clog our table.
441 if (lopt->qlen>>(lopt->max_qlen_log-1)) {
442 int young = (lopt->qlen_young<<1);
445 if (lopt->qlen < young)
452 if (queue->rskq_defer_accept)
453 max_retries = queue->rskq_defer_accept;
455 budget = 2 * (lopt->nr_table_entries / (timeout / interval));
456 i = lopt->clock_hand;
459 reqp=&lopt->syn_table[i];
460 while ((req = *reqp) != NULL) {
461 if (time_after_eq(now, req->expires)) {
462 if ((req->retrans < thresh ||
463 (inet_rsk(req)->acked && req->retrans < max_retries))
464 && !req->rsk_ops->rtx_syn_ack(parent, req, NULL)) {
467 if (req->retrans++ == 0)
469 timeo = min((timeout << req->retrans), max_rto);
470 req->expires = now + timeo;
471 reqp = &req->dl_next;
475 /* Drop this request */
476 inet_csk_reqsk_queue_unlink(parent, req, reqp);
477 reqsk_queue_removed(queue, req);
481 reqp = &req->dl_next;
484 i = (i + 1) & (lopt->nr_table_entries - 1);
486 } while (--budget > 0);
488 lopt->clock_hand = i;
491 inet_csk_reset_keepalive_timer(parent, interval);
494 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
496 struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
497 const gfp_t priority)
499 struct sock *newsk = sk_clone(sk, priority);
502 struct inet_connection_sock *newicsk = inet_csk(newsk);
504 newsk->sk_state = TCP_SYN_RECV;
505 newicsk->icsk_bind_hash = NULL;
507 inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
508 newsk->sk_write_space = sk_stream_write_space;
510 newicsk->icsk_retransmits = 0;
511 newicsk->icsk_backoff = 0;
512 newicsk->icsk_probes_out = 0;
514 /* Deinitialize accept_queue to trap illegal accesses. */
515 memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
520 EXPORT_SYMBOL_GPL(inet_csk_clone);
523 * At this point, there should be no process reference to this
524 * socket, and thus no user references at all. Therefore we
525 * can assume the socket waitqueue is inactive and nobody will
526 * try to jump onto it.
528 void inet_csk_destroy_sock(struct sock *sk)
530 BUG_TRAP(sk->sk_state == TCP_CLOSE);
531 BUG_TRAP(sock_flag(sk, SOCK_DEAD));
533 /* It cannot be in hash table! */
534 BUG_TRAP(sk_unhashed(sk));
536 /* If it has not 0 inet_sk(sk)->num, it must be bound */
537 BUG_TRAP(!inet_sk(sk)->num || inet_csk(sk)->icsk_bind_hash);
539 sk->sk_prot->destroy(sk);
541 sk_stream_kill_queues(sk);
543 xfrm_sk_free_policy(sk);
545 sk_refcnt_debug_release(sk);
547 atomic_dec(sk->sk_prot->orphan_count);
551 EXPORT_SYMBOL(inet_csk_destroy_sock);
553 int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
555 struct inet_sock *inet = inet_sk(sk);
556 struct inet_connection_sock *icsk = inet_csk(sk);
557 int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
562 sk->sk_max_ack_backlog = 0;
563 sk->sk_ack_backlog = 0;
564 inet_csk_delack_init(sk);
566 /* There is race window here: we announce ourselves listening,
567 * but this transition is still not validated by get_port().
568 * It is OK, because this socket enters to hash table only
569 * after validation is complete.
571 sk->sk_state = TCP_LISTEN;
572 if (!sk->sk_prot->get_port(sk, inet->num)) {
573 inet->sport = htons(inet->num);
576 sk->sk_prot->hash(sk);
581 sk->sk_state = TCP_CLOSE;
582 __reqsk_queue_destroy(&icsk->icsk_accept_queue);
586 EXPORT_SYMBOL_GPL(inet_csk_listen_start);
589 * This routine closes sockets which have been at least partially
590 * opened, but not yet accepted.
592 void inet_csk_listen_stop(struct sock *sk)
594 struct inet_connection_sock *icsk = inet_csk(sk);
595 struct request_sock *acc_req;
596 struct request_sock *req;
598 inet_csk_delete_keepalive_timer(sk);
600 /* make all the listen_opt local to us */
601 acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
603 /* Following specs, it would be better either to send FIN
604 * (and enter FIN-WAIT-1, it is normal close)
605 * or to send active reset (abort).
606 * Certainly, it is pretty dangerous while synflood, but it is
607 * bad justification for our negligence 8)
608 * To be honest, we are not able to make either
609 * of the variants now. --ANK
611 reqsk_queue_destroy(&icsk->icsk_accept_queue);
613 while ((req = acc_req) != NULL) {
614 struct sock *child = req->sk;
616 acc_req = req->dl_next;
620 BUG_TRAP(!sock_owned_by_user(child));
623 sk->sk_prot->disconnect(child, O_NONBLOCK);
627 atomic_inc(sk->sk_prot->orphan_count);
629 inet_csk_destroy_sock(child);
631 bh_unlock_sock(child);
635 sk_acceptq_removed(sk);
638 BUG_TRAP(!sk->sk_ack_backlog);
641 EXPORT_SYMBOL_GPL(inet_csk_listen_stop);