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>
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.
34 * For high-usage systems, use sysctl to change this to
37 int sysctl_local_port_range[2] = { 1024, 4999 };
39 static inline int inet_csk_bind_conflict(struct sock *sk, struct inet_bind_bucket *tb)
41 const u32 sk_rcv_saddr = inet_rcv_saddr(sk);
43 struct hlist_node *node;
44 int reuse = sk->sk_reuse;
46 sk_for_each_bound(sk2, node, &tb->owners) {
48 !inet_v6_ipv6only(sk2) &&
49 (!sk->sk_bound_dev_if ||
50 !sk2->sk_bound_dev_if ||
51 sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
52 if (!reuse || !sk2->sk_reuse ||
53 sk2->sk_state == TCP_LISTEN) {
54 const u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
55 if (!sk2_rcv_saddr || !sk_rcv_saddr ||
56 sk2_rcv_saddr == sk_rcv_saddr)
64 /* Obtain a reference to a local port for the given sock,
65 * if snum is zero it means select any available local port.
67 int inet_csk_get_port(struct inet_hashinfo *hashinfo,
68 struct sock *sk, unsigned short snum)
70 struct inet_bind_hashbucket *head;
71 struct hlist_node *node;
72 struct inet_bind_bucket *tb;
77 int low = sysctl_local_port_range[0];
78 int high = sysctl_local_port_range[1];
79 int remaining = (high - low) + 1;
82 spin_lock(&hashinfo->portalloc_lock);
83 if (hashinfo->port_rover < low)
86 rover = hashinfo->port_rover;
91 head = &hashinfo->bhash[inet_bhashfn(rover, hashinfo->bhash_size)];
92 spin_lock(&head->lock);
93 inet_bind_bucket_for_each(tb, node, &head->chain)
94 if (tb->port == rover)
98 spin_unlock(&head->lock);
99 } while (--remaining > 0);
100 hashinfo->port_rover = rover;
101 spin_unlock(&hashinfo->portalloc_lock);
103 /* Exhausted local port range during search? It is not
104 * possible for us to be holding one of the bind hash
105 * locks if this test triggers, because if 'remaining'
106 * drops to zero, we broke out of the do/while loop at
107 * the top level, not from the 'break;' statement.
113 /* OK, here is the one we will use. HEAD is
114 * non-NULL and we hold it's mutex.
118 head = &hashinfo->bhash[inet_bhashfn(snum, hashinfo->bhash_size)];
119 spin_lock(&head->lock);
120 inet_bind_bucket_for_each(tb, node, &head->chain)
121 if (tb->port == snum)
127 if (!hlist_empty(&tb->owners)) {
128 if (sk->sk_reuse > 1)
130 if (tb->fastreuse > 0 &&
131 sk->sk_reuse && sk->sk_state != TCP_LISTEN) {
135 if (inet_csk_bind_conflict(sk, tb))
141 if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep, head, snum)) == NULL)
143 if (hlist_empty(&tb->owners)) {
144 if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
148 } else if (tb->fastreuse &&
149 (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
152 if (!inet_csk(sk)->icsk_bind_hash)
153 inet_bind_hash(sk, tb, snum);
154 BUG_TRAP(inet_csk(sk)->icsk_bind_hash == tb);
158 spin_unlock(&head->lock);
164 EXPORT_SYMBOL_GPL(inet_csk_get_port);
167 * Wait for an incoming connection, avoid race conditions. This must be called
168 * with the socket locked.
170 static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
172 struct inet_connection_sock *icsk = inet_csk(sk);
177 * True wake-one mechanism for incoming connections: only
178 * one process gets woken up, not the 'whole herd'.
179 * Since we do not 'race & poll' for established sockets
180 * anymore, the common case will execute the loop only once.
182 * Subtle issue: "add_wait_queue_exclusive()" will be added
183 * after any current non-exclusive waiters, and we know that
184 * it will always _stay_ after any new non-exclusive waiters
185 * because all non-exclusive waiters are added at the
186 * beginning of the wait-queue. As such, it's ok to "drop"
187 * our exclusiveness temporarily when we get woken up without
188 * having to remove and re-insert us on the wait queue.
191 prepare_to_wait_exclusive(sk->sk_sleep, &wait,
194 if (reqsk_queue_empty(&icsk->icsk_accept_queue))
195 timeo = schedule_timeout(timeo);
198 if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
201 if (sk->sk_state != TCP_LISTEN)
203 err = sock_intr_errno(timeo);
204 if (signal_pending(current))
210 finish_wait(sk->sk_sleep, &wait);
215 * This will accept the next outstanding connection.
217 struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
219 struct inet_connection_sock *icsk = inet_csk(sk);
225 /* We need to make sure that this socket is listening,
226 * and that it has something pending.
229 if (sk->sk_state != TCP_LISTEN)
232 /* Find already established connection */
233 if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
234 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
236 /* If this is a non blocking socket don't sleep */
241 error = inet_csk_wait_for_connect(sk, timeo);
246 newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
247 BUG_TRAP(newsk->sk_state != TCP_SYN_RECV);
257 EXPORT_SYMBOL(inet_csk_accept);
260 * Using different timers for retransmit, delayed acks and probes
261 * We may wish use just one timer maintaining a list of expire jiffies
264 void inet_csk_init_xmit_timers(struct sock *sk,
265 void (*retransmit_handler)(unsigned long),
266 void (*delack_handler)(unsigned long),
267 void (*keepalive_handler)(unsigned long))
269 struct inet_connection_sock *icsk = inet_csk(sk);
271 init_timer(&icsk->icsk_retransmit_timer);
272 init_timer(&icsk->icsk_delack_timer);
273 init_timer(&sk->sk_timer);
275 icsk->icsk_retransmit_timer.function = retransmit_handler;
276 icsk->icsk_delack_timer.function = delack_handler;
277 sk->sk_timer.function = keepalive_handler;
279 icsk->icsk_retransmit_timer.data =
280 icsk->icsk_delack_timer.data =
281 sk->sk_timer.data = (unsigned long)sk;
283 icsk->icsk_pending = icsk->icsk_ack.pending = 0;
286 EXPORT_SYMBOL(inet_csk_init_xmit_timers);
288 void inet_csk_clear_xmit_timers(struct sock *sk)
290 struct inet_connection_sock *icsk = inet_csk(sk);
292 icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
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);
299 EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
301 void inet_csk_delete_keepalive_timer(struct sock *sk)
303 sk_stop_timer(sk, &sk->sk_timer);
306 EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
308 void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
310 sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
313 EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
315 struct dst_entry* inet_csk_route_req(struct sock *sk,
316 const struct request_sock *req)
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,
323 { .daddr = ((opt && opt->srr) ?
326 .saddr = ireq->loc_addr,
327 .tos = RT_CONN_FLAGS(sk) } },
328 .proto = sk->sk_protocol,
330 { .sport = inet_sk(sk)->sport,
331 .dport = ireq->rmt_port } } };
333 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
334 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
337 if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
339 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
345 EXPORT_SYMBOL_GPL(inet_csk_route_req);
347 static inline u32 inet_synq_hash(const u32 raddr, const u16 rport,
348 const u32 rnd, const u16 synq_hsize)
350 return jhash_2words(raddr, (u32)rport, rnd) & (synq_hsize - 1);
353 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
354 #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
356 #define AF_INET_FAMILY(fam) 1
359 struct request_sock *inet_csk_search_req(const struct sock *sk,
360 struct request_sock ***prevp,
361 const __u16 rport, const __u32 raddr,
364 const struct inet_connection_sock *icsk = inet_csk(sk);
365 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
366 struct request_sock *req, **prev;
368 for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
369 lopt->nr_table_entries)];
370 (req = *prev) != NULL;
371 prev = &req->dl_next) {
372 const struct inet_request_sock *ireq = inet_rsk(req);
374 if (ireq->rmt_port == rport &&
375 ireq->rmt_addr == raddr &&
376 ireq->loc_addr == laddr &&
377 AF_INET_FAMILY(req->rsk_ops->family)) {
387 EXPORT_SYMBOL_GPL(inet_csk_search_req);
389 void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
390 const unsigned timeout)
392 struct inet_connection_sock *icsk = inet_csk(sk);
393 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
394 const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
395 lopt->hash_rnd, lopt->nr_table_entries);
397 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
398 inet_csk_reqsk_queue_added(sk, timeout);
401 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);