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/module.h>
17 #include <linux/jhash.h>
19 #include <net/inet_connection_sock.h>
20 #include <net/inet_hashtables.h>
21 #include <net/inet_timewait_sock.h>
23 #include <net/route.h>
24 #include <net/tcp_states.h>
28 const char inet_csk_timer_bug_msg
[] = "inet_csk BUG: unknown timer value\n";
29 EXPORT_SYMBOL(inet_csk_timer_bug_msg
);
33 * This array holds the first and last local port number.
35 int sysctl_local_port_range
[2] = { 32768, 61000 };
36 DEFINE_SEQLOCK(sysctl_port_range_lock
);
38 void inet_get_local_port_range(int *low
, int *high
)
42 seq
= read_seqbegin(&sysctl_port_range_lock
);
44 *low
= sysctl_local_port_range
[0];
45 *high
= sysctl_local_port_range
[1];
46 } while (read_seqretry(&sysctl_port_range_lock
, seq
));
48 EXPORT_SYMBOL(inet_get_local_port_range
);
50 int inet_csk_bind_conflict(const struct sock
*sk
,
51 const struct inet_bind_bucket
*tb
)
53 const __be32 sk_rcv_saddr
= inet_rcv_saddr(sk
);
55 struct hlist_node
*node
;
56 int reuse
= sk
->sk_reuse
;
58 sk_for_each_bound(sk2
, node
, &tb
->owners
) {
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
) {
66 const __be32 sk2_rcv_saddr
= inet_rcv_saddr(sk2
);
67 if (!sk2_rcv_saddr
|| !sk_rcv_saddr
||
68 sk2_rcv_saddr
== sk_rcv_saddr
)
76 EXPORT_SYMBOL_GPL(inet_csk_bind_conflict
);
78 /* Obtain a reference to a local port for the given sock,
79 * if snum is zero it means select any available local port.
81 int inet_csk_get_port(struct inet_hashinfo
*hashinfo
,
82 struct sock
*sk
, unsigned short snum
,
83 int (*bind_conflict
)(const struct sock
*sk
,
84 const struct inet_bind_bucket
*tb
))
86 struct inet_bind_hashbucket
*head
;
87 struct hlist_node
*node
;
88 struct inet_bind_bucket
*tb
;
93 int remaining
, rover
, low
, high
;
95 inet_get_local_port_range(&low
, &high
);
96 remaining
= high
- low
;
97 rover
= net_random() % remaining
+ low
;
100 head
= &hashinfo
->bhash
[inet_bhashfn(rover
, hashinfo
->bhash_size
)];
101 spin_lock(&head
->lock
);
102 inet_bind_bucket_for_each(tb
, node
, &head
->chain
)
103 if (tb
->port
== rover
)
107 spin_unlock(&head
->lock
);
110 } while (--remaining
> 0);
112 /* Exhausted local port range during search? It is not
113 * possible for us to be holding one of the bind hash
114 * locks if this test triggers, because if 'remaining'
115 * drops to zero, we broke out of the do/while loop at
116 * the top level, not from the 'break;' statement.
122 /* OK, here is the one we will use. HEAD is
123 * non-NULL and we hold it's mutex.
127 head
= &hashinfo
->bhash
[inet_bhashfn(snum
, hashinfo
->bhash_size
)];
128 spin_lock(&head
->lock
);
129 inet_bind_bucket_for_each(tb
, node
, &head
->chain
)
130 if (tb
->port
== snum
)
136 if (!hlist_empty(&tb
->owners
)) {
137 if (sk
->sk_reuse
> 1)
139 if (tb
->fastreuse
> 0 &&
140 sk
->sk_reuse
&& sk
->sk_state
!= TCP_LISTEN
) {
144 if (bind_conflict(sk
, tb
))
150 if (!tb
&& (tb
= inet_bind_bucket_create(hashinfo
->bind_bucket_cachep
, head
, snum
)) == NULL
)
152 if (hlist_empty(&tb
->owners
)) {
153 if (sk
->sk_reuse
&& sk
->sk_state
!= TCP_LISTEN
)
157 } else if (tb
->fastreuse
&&
158 (!sk
->sk_reuse
|| sk
->sk_state
== TCP_LISTEN
))
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
);
167 spin_unlock(&head
->lock
);
173 EXPORT_SYMBOL_GPL(inet_csk_get_port
);
176 * Wait for an incoming connection, avoid race conditions. This must be called
177 * with the socket locked.
179 static int inet_csk_wait_for_connect(struct sock
*sk
, long timeo
)
181 struct inet_connection_sock
*icsk
= inet_csk(sk
);
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.
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.
200 prepare_to_wait_exclusive(sk
->sk_sleep
, &wait
,
203 if (reqsk_queue_empty(&icsk
->icsk_accept_queue
))
204 timeo
= schedule_timeout(timeo
);
207 if (!reqsk_queue_empty(&icsk
->icsk_accept_queue
))
210 if (sk
->sk_state
!= TCP_LISTEN
)
212 err
= sock_intr_errno(timeo
);
213 if (signal_pending(current
))
219 finish_wait(sk
->sk_sleep
, &wait
);
224 * This will accept the next outstanding connection.
226 struct sock
*inet_csk_accept(struct sock
*sk
, int flags
, int *err
)
228 struct inet_connection_sock
*icsk
= inet_csk(sk
);
234 /* We need to make sure that this socket is listening,
235 * and that it has something pending.
238 if (sk
->sk_state
!= TCP_LISTEN
)
241 /* Find already established connection */
242 if (reqsk_queue_empty(&icsk
->icsk_accept_queue
)) {
243 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
245 /* If this is a non blocking socket don't sleep */
250 error
= inet_csk_wait_for_connect(sk
, timeo
);
255 newsk
= reqsk_queue_get_child(&icsk
->icsk_accept_queue
, sk
);
256 BUG_TRAP(newsk
->sk_state
!= TCP_SYN_RECV
);
266 EXPORT_SYMBOL(inet_csk_accept
);
269 * Using different timers for retransmit, delayed acks and probes
270 * We may wish use just one timer maintaining a list of expire jiffies
273 void 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))
278 struct inet_connection_sock
*icsk
= inet_csk(sk
);
280 init_timer(&icsk
->icsk_retransmit_timer
);
281 init_timer(&icsk
->icsk_delack_timer
);
282 init_timer(&sk
->sk_timer
);
284 icsk
->icsk_retransmit_timer
.function
= retransmit_handler
;
285 icsk
->icsk_delack_timer
.function
= delack_handler
;
286 sk
->sk_timer
.function
= keepalive_handler
;
288 icsk
->icsk_retransmit_timer
.data
=
289 icsk
->icsk_delack_timer
.data
=
290 sk
->sk_timer
.data
= (unsigned long)sk
;
292 icsk
->icsk_pending
= icsk
->icsk_ack
.pending
= 0;
295 EXPORT_SYMBOL(inet_csk_init_xmit_timers
);
297 void inet_csk_clear_xmit_timers(struct sock
*sk
)
299 struct inet_connection_sock
*icsk
= inet_csk(sk
);
301 icsk
->icsk_pending
= icsk
->icsk_ack
.pending
= icsk
->icsk_ack
.blocked
= 0;
303 sk_stop_timer(sk
, &icsk
->icsk_retransmit_timer
);
304 sk_stop_timer(sk
, &icsk
->icsk_delack_timer
);
305 sk_stop_timer(sk
, &sk
->sk_timer
);
308 EXPORT_SYMBOL(inet_csk_clear_xmit_timers
);
310 void inet_csk_delete_keepalive_timer(struct sock
*sk
)
312 sk_stop_timer(sk
, &sk
->sk_timer
);
315 EXPORT_SYMBOL(inet_csk_delete_keepalive_timer
);
317 void inet_csk_reset_keepalive_timer(struct sock
*sk
, unsigned long len
)
319 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ len
);
322 EXPORT_SYMBOL(inet_csk_reset_keepalive_timer
);
324 struct dst_entry
* inet_csk_route_req(struct sock
*sk
,
325 const struct request_sock
*req
)
328 const struct inet_request_sock
*ireq
= inet_rsk(req
);
329 struct ip_options
*opt
= inet_rsk(req
)->opt
;
330 struct flowi fl
= { .oif
= sk
->sk_bound_dev_if
,
332 { .daddr
= ((opt
&& opt
->srr
) ?
335 .saddr
= ireq
->loc_addr
,
336 .tos
= RT_CONN_FLAGS(sk
) } },
337 .proto
= sk
->sk_protocol
,
339 { .sport
= inet_sk(sk
)->sport
,
340 .dport
= ireq
->rmt_port
} } };
342 security_req_classify_flow(req
, &fl
);
343 if (ip_route_output_flow(&rt
, &fl
, sk
, 0)) {
344 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
347 if (opt
&& opt
->is_strictroute
&& rt
->rt_dst
!= rt
->rt_gateway
) {
349 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
355 EXPORT_SYMBOL_GPL(inet_csk_route_req
);
357 static inline u32
inet_synq_hash(const __be32 raddr
, const __be16 rport
,
358 const u32 rnd
, const u32 synq_hsize
)
360 return jhash_2words((__force u32
)raddr
, (__force u32
)rport
, rnd
) & (synq_hsize
- 1);
363 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
364 #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
366 #define AF_INET_FAMILY(fam) 1
369 struct request_sock
*inet_csk_search_req(const struct sock
*sk
,
370 struct request_sock
***prevp
,
371 const __be16 rport
, const __be32 raddr
,
374 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
375 struct listen_sock
*lopt
= icsk
->icsk_accept_queue
.listen_opt
;
376 struct request_sock
*req
, **prev
;
378 for (prev
= &lopt
->syn_table
[inet_synq_hash(raddr
, rport
, lopt
->hash_rnd
,
379 lopt
->nr_table_entries
)];
380 (req
= *prev
) != NULL
;
381 prev
= &req
->dl_next
) {
382 const struct inet_request_sock
*ireq
= inet_rsk(req
);
384 if (ireq
->rmt_port
== rport
&&
385 ireq
->rmt_addr
== raddr
&&
386 ireq
->loc_addr
== laddr
&&
387 AF_INET_FAMILY(req
->rsk_ops
->family
)) {
397 EXPORT_SYMBOL_GPL(inet_csk_search_req
);
399 void inet_csk_reqsk_queue_hash_add(struct sock
*sk
, struct request_sock
*req
,
400 unsigned long timeout
)
402 struct inet_connection_sock
*icsk
= inet_csk(sk
);
403 struct listen_sock
*lopt
= icsk
->icsk_accept_queue
.listen_opt
;
404 const u32 h
= inet_synq_hash(inet_rsk(req
)->rmt_addr
, inet_rsk(req
)->rmt_port
,
405 lopt
->hash_rnd
, lopt
->nr_table_entries
);
407 reqsk_queue_hash_req(&icsk
->icsk_accept_queue
, h
, req
, timeout
);
408 inet_csk_reqsk_queue_added(sk
, timeout
);
411 /* Only thing we need from tcp.h */
412 extern int sysctl_tcp_synack_retries
;
414 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add
);
416 void inet_csk_reqsk_queue_prune(struct sock
*parent
,
417 const unsigned long interval
,
418 const unsigned long timeout
,
419 const unsigned long max_rto
)
421 struct inet_connection_sock
*icsk
= inet_csk(parent
);
422 struct request_sock_queue
*queue
= &icsk
->icsk_accept_queue
;
423 struct listen_sock
*lopt
= queue
->listen_opt
;
424 int max_retries
= icsk
->icsk_syn_retries
? : sysctl_tcp_synack_retries
;
425 int thresh
= max_retries
;
426 unsigned long now
= jiffies
;
427 struct request_sock
**reqp
, *req
;
430 if (lopt
== NULL
|| lopt
->qlen
== 0)
433 /* Normally all the openreqs are young and become mature
434 * (i.e. converted to established socket) for first timeout.
435 * If synack was not acknowledged for 3 seconds, it means
436 * one of the following things: synack was lost, ack was lost,
437 * rtt is high or nobody planned to ack (i.e. synflood).
438 * When server is a bit loaded, queue is populated with old
439 * open requests, reducing effective size of queue.
440 * When server is well loaded, queue size reduces to zero
441 * after several minutes of work. It is not synflood,
442 * it is normal operation. The solution is pruning
443 * too old entries overriding normal timeout, when
444 * situation becomes dangerous.
446 * Essentially, we reserve half of room for young
447 * embrions; and abort old ones without pity, if old
448 * ones are about to clog our table.
450 if (lopt
->qlen
>>(lopt
->max_qlen_log
-1)) {
451 int young
= (lopt
->qlen_young
<<1);
454 if (lopt
->qlen
< young
)
461 if (queue
->rskq_defer_accept
)
462 max_retries
= queue
->rskq_defer_accept
;
464 budget
= 2 * (lopt
->nr_table_entries
/ (timeout
/ interval
));
465 i
= lopt
->clock_hand
;
468 reqp
=&lopt
->syn_table
[i
];
469 while ((req
= *reqp
) != NULL
) {
470 if (time_after_eq(now
, req
->expires
)) {
471 if ((req
->retrans
< thresh
||
472 (inet_rsk(req
)->acked
&& req
->retrans
< max_retries
))
473 && !req
->rsk_ops
->rtx_syn_ack(parent
, req
, NULL
)) {
476 if (req
->retrans
++ == 0)
478 timeo
= min((timeout
<< req
->retrans
), max_rto
);
479 req
->expires
= now
+ timeo
;
480 reqp
= &req
->dl_next
;
484 /* Drop this request */
485 inet_csk_reqsk_queue_unlink(parent
, req
, reqp
);
486 reqsk_queue_removed(queue
, req
);
490 reqp
= &req
->dl_next
;
493 i
= (i
+ 1) & (lopt
->nr_table_entries
- 1);
495 } while (--budget
> 0);
497 lopt
->clock_hand
= i
;
500 inet_csk_reset_keepalive_timer(parent
, interval
);
503 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune
);
505 struct sock
*inet_csk_clone(struct sock
*sk
, const struct request_sock
*req
,
506 const gfp_t priority
)
508 struct sock
*newsk
= sk_clone(sk
, priority
);
511 struct inet_connection_sock
*newicsk
= inet_csk(newsk
);
513 newsk
->sk_state
= TCP_SYN_RECV
;
514 newicsk
->icsk_bind_hash
= NULL
;
516 inet_sk(newsk
)->dport
= inet_rsk(req
)->rmt_port
;
517 newsk
->sk_write_space
= sk_stream_write_space
;
519 newicsk
->icsk_retransmits
= 0;
520 newicsk
->icsk_backoff
= 0;
521 newicsk
->icsk_probes_out
= 0;
523 /* Deinitialize accept_queue to trap illegal accesses. */
524 memset(&newicsk
->icsk_accept_queue
, 0, sizeof(newicsk
->icsk_accept_queue
));
526 security_inet_csk_clone(newsk
, req
);
531 EXPORT_SYMBOL_GPL(inet_csk_clone
);
534 * At this point, there should be no process reference to this
535 * socket, and thus no user references at all. Therefore we
536 * can assume the socket waitqueue is inactive and nobody will
537 * try to jump onto it.
539 void inet_csk_destroy_sock(struct sock
*sk
)
541 BUG_TRAP(sk
->sk_state
== TCP_CLOSE
);
542 BUG_TRAP(sock_flag(sk
, SOCK_DEAD
));
544 /* It cannot be in hash table! */
545 BUG_TRAP(sk_unhashed(sk
));
547 /* If it has not 0 inet_sk(sk)->num, it must be bound */
548 BUG_TRAP(!inet_sk(sk
)->num
|| inet_csk(sk
)->icsk_bind_hash
);
550 sk
->sk_prot
->destroy(sk
);
552 sk_stream_kill_queues(sk
);
554 xfrm_sk_free_policy(sk
);
556 sk_refcnt_debug_release(sk
);
558 atomic_dec(sk
->sk_prot
->orphan_count
);
562 EXPORT_SYMBOL(inet_csk_destroy_sock
);
564 int inet_csk_listen_start(struct sock
*sk
, const int nr_table_entries
)
566 struct inet_sock
*inet
= inet_sk(sk
);
567 struct inet_connection_sock
*icsk
= inet_csk(sk
);
568 int rc
= reqsk_queue_alloc(&icsk
->icsk_accept_queue
, nr_table_entries
);
573 sk
->sk_max_ack_backlog
= 0;
574 sk
->sk_ack_backlog
= 0;
575 inet_csk_delack_init(sk
);
577 /* There is race window here: we announce ourselves listening,
578 * but this transition is still not validated by get_port().
579 * It is OK, because this socket enters to hash table only
580 * after validation is complete.
582 sk
->sk_state
= TCP_LISTEN
;
583 if (!sk
->sk_prot
->get_port(sk
, inet
->num
)) {
584 inet
->sport
= htons(inet
->num
);
587 sk
->sk_prot
->hash(sk
);
592 sk
->sk_state
= TCP_CLOSE
;
593 __reqsk_queue_destroy(&icsk
->icsk_accept_queue
);
597 EXPORT_SYMBOL_GPL(inet_csk_listen_start
);
600 * This routine closes sockets which have been at least partially
601 * opened, but not yet accepted.
603 void inet_csk_listen_stop(struct sock
*sk
)
605 struct inet_connection_sock
*icsk
= inet_csk(sk
);
606 struct request_sock
*acc_req
;
607 struct request_sock
*req
;
609 inet_csk_delete_keepalive_timer(sk
);
611 /* make all the listen_opt local to us */
612 acc_req
= reqsk_queue_yank_acceptq(&icsk
->icsk_accept_queue
);
614 /* Following specs, it would be better either to send FIN
615 * (and enter FIN-WAIT-1, it is normal close)
616 * or to send active reset (abort).
617 * Certainly, it is pretty dangerous while synflood, but it is
618 * bad justification for our negligence 8)
619 * To be honest, we are not able to make either
620 * of the variants now. --ANK
622 reqsk_queue_destroy(&icsk
->icsk_accept_queue
);
624 while ((req
= acc_req
) != NULL
) {
625 struct sock
*child
= req
->sk
;
627 acc_req
= req
->dl_next
;
631 BUG_TRAP(!sock_owned_by_user(child
));
634 sk
->sk_prot
->disconnect(child
, O_NONBLOCK
);
638 atomic_inc(sk
->sk_prot
->orphan_count
);
640 inet_csk_destroy_sock(child
);
642 bh_unlock_sock(child
);
646 sk_acceptq_removed(sk
);
649 BUG_TRAP(!sk
->sk_ack_backlog
);
652 EXPORT_SYMBOL_GPL(inet_csk_listen_stop
);
654 void inet_csk_addr2sockaddr(struct sock
*sk
, struct sockaddr
*uaddr
)
656 struct sockaddr_in
*sin
= (struct sockaddr_in
*)uaddr
;
657 const struct inet_sock
*inet
= inet_sk(sk
);
659 sin
->sin_family
= AF_INET
;
660 sin
->sin_addr
.s_addr
= inet
->daddr
;
661 sin
->sin_port
= inet
->dport
;
664 EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr
);
666 int inet_csk_ctl_sock_create(struct socket
**sock
, unsigned short family
,
667 unsigned short type
, unsigned char protocol
)
669 int rc
= sock_create_kern(family
, type
, protocol
, sock
);
672 (*sock
)->sk
->sk_allocation
= GFP_ATOMIC
;
673 inet_sk((*sock
)->sk
)->uc_ttl
= -1;
675 * Unhash it so that IP input processing does not even see it,
676 * we do not wish this socket to see incoming packets.
678 (*sock
)->sk
->sk_prot
->unhash((*sock
)->sk
);
683 EXPORT_SYMBOL_GPL(inet_csk_ctl_sock_create
);
686 int inet_csk_compat_getsockopt(struct sock
*sk
, int level
, int optname
,
687 char __user
*optval
, int __user
*optlen
)
689 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
691 if (icsk
->icsk_af_ops
->compat_getsockopt
!= NULL
)
692 return icsk
->icsk_af_ops
->compat_getsockopt(sk
, level
, optname
,
694 return icsk
->icsk_af_ops
->getsockopt(sk
, level
, optname
,
698 EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt
);
700 int inet_csk_compat_setsockopt(struct sock
*sk
, int level
, int optname
,
701 char __user
*optval
, int optlen
)
703 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
705 if (icsk
->icsk_af_ops
->compat_setsockopt
!= NULL
)
706 return icsk
->icsk_af_ops
->compat_setsockopt(sk
, level
, optname
,
708 return icsk
->icsk_af_ops
->setsockopt(sk
, level
, optname
,
712 EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt
);