USB: oti6858: cleanup
[linux/fpc-iii.git] / net / ipv4 / inet_connection_sock.c
blobde5a41de191a675e69701fd5416bda497da62aaf
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.
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>
22 #include <net/ip.h>
23 #include <net/route.h>
24 #include <net/tcp_states.h>
25 #include <net/xfrm.h>
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
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)
40 unsigned seq;
41 do {
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);
54 struct sock *sk2;
55 struct hlist_node *node;
56 int reuse = sk->sk_reuse;
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) {
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)
69 break;
73 return node != NULL;
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;
89 int ret;
90 struct net *net = sk->sk_net;
92 local_bh_disable();
93 if (!snum) {
94 int remaining, rover, low, high;
96 inet_get_local_port_range(&low, &high);
97 remaining = (high - low) + 1;
98 rover = net_random() % remaining + low;
100 do {
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)
104 if (tb->ib_net == net && tb->port == rover)
105 goto next;
106 break;
107 next:
108 spin_unlock(&head->lock);
109 if (++rover > high)
110 rover = low;
111 } while (--remaining > 0);
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.
119 ret = 1;
120 if (remaining <= 0)
121 goto fail;
123 /* OK, here is the one we will use. HEAD is
124 * non-NULL and we hold it's mutex.
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)
131 if (tb->ib_net == net && tb->port == snum)
132 goto tb_found;
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;
145 if (bind_conflict(sk, tb))
146 goto fail_unlock;
149 tb_not_found:
150 ret = 1;
151 if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
152 net, head, snum)) == NULL)
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);
166 ret = 0;
168 fail_unlock:
169 spin_unlock(&head->lock);
170 fail:
171 local_bh_enable();
172 return ret;
175 EXPORT_SYMBOL_GPL(inet_csk_get_port);
178 * Wait for an incoming connection, avoid race conditions. This must be called
179 * with the socket locked.
181 static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
183 struct inet_connection_sock *icsk = inet_csk(sk);
184 DEFINE_WAIT(wait);
185 int err;
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.
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.
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;
221 finish_wait(sk->sk_sleep, &wait);
222 return err;
226 * This will accept the next outstanding connection.
228 struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
230 struct inet_connection_sock *icsk = inet_csk(sk);
231 struct sock *newsk;
232 int error;
234 lock_sock(sk);
236 /* We need to make sure that this socket is listening,
237 * and that it has something pending.
239 error = -EINVAL;
240 if (sk->sk_state != TCP_LISTEN)
241 goto out_err;
243 /* Find already established connection */
244 if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
245 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
247 /* If this is a non blocking socket don't sleep */
248 error = -EAGAIN;
249 if (!timeo)
250 goto out_err;
252 error = inet_csk_wait_for_connect(sk, timeo);
253 if (error)
254 goto out_err;
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;
268 EXPORT_SYMBOL(inet_csk_accept);
271 * Using different timers for retransmit, delayed acks and probes
272 * We may wish use just one timer maintaining a list of expire jiffies
273 * to optimize.
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))
280 struct inet_connection_sock *icsk = inet_csk(sk);
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);
287 icsk->icsk_pending = icsk->icsk_ack.pending = 0;
290 EXPORT_SYMBOL(inet_csk_init_xmit_timers);
292 void inet_csk_clear_xmit_timers(struct sock *sk)
294 struct inet_connection_sock *icsk = inet_csk(sk);
296 icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
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);
303 EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
305 void inet_csk_delete_keepalive_timer(struct sock *sk)
307 sk_stop_timer(sk, &sk->sk_timer);
310 EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
312 void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
314 sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
317 EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
319 struct dst_entry* inet_csk_route_req(struct sock *sk,
320 const struct request_sock *req)
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 } } };
337 security_req_classify_flow(req, &fl);
338 if (ip_route_output_flow(&init_net, &rt, &fl, sk, 0)) {
339 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
340 return NULL;
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;
347 return &rt->u.dst;
350 EXPORT_SYMBOL_GPL(inet_csk_route_req);
352 static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
353 const u32 rnd, const u32 synq_hsize)
355 return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
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
364 struct request_sock *inet_csk_search_req(const struct sock *sk,
365 struct request_sock ***prevp,
366 const __be16 rport, const __be32 raddr,
367 const __be32 laddr)
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;
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);
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;
389 return req;
392 EXPORT_SYMBOL_GPL(inet_csk_search_req);
394 void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
395 unsigned long timeout)
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);
402 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
403 inet_csk_reqsk_queue_added(sk, timeout);
406 /* Only thing we need from tcp.h */
407 extern int sysctl_tcp_synack_retries;
409 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
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)
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;
425 if (lopt == NULL || lopt->qlen == 0)
426 return;
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.
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.
445 if (lopt->qlen>>(lopt->max_qlen_log-1)) {
446 int young = (lopt->qlen_young<<1);
448 while (thresh > 2) {
449 if (lopt->qlen < young)
450 break;
451 thresh--;
452 young <<= 1;
456 if (queue->rskq_defer_accept)
457 max_retries = queue->rskq_defer_accept;
459 budget = 2 * (lopt->nr_table_entries / (timeout / interval));
460 i = lopt->clock_hand;
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;
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;
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;
485 reqp = &req->dl_next;
488 i = (i + 1) & (lopt->nr_table_entries - 1);
490 } while (--budget > 0);
492 lopt->clock_hand = i;
494 if (lopt->qlen)
495 inet_csk_reset_keepalive_timer(parent, interval);
498 EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
500 struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
501 const gfp_t priority)
503 struct sock *newsk = sk_clone(sk, priority);
505 if (newsk != NULL) {
506 struct inet_connection_sock *newicsk = inet_csk(newsk);
508 newsk->sk_state = TCP_SYN_RECV;
509 newicsk->icsk_bind_hash = NULL;
511 inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
512 newsk->sk_write_space = sk_stream_write_space;
514 newicsk->icsk_retransmits = 0;
515 newicsk->icsk_backoff = 0;
516 newicsk->icsk_probes_out = 0;
518 /* Deinitialize accept_queue to trap illegal accesses. */
519 memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
521 security_inet_csk_clone(newsk, req);
523 return newsk;
526 EXPORT_SYMBOL_GPL(inet_csk_clone);
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.
534 void inet_csk_destroy_sock(struct sock *sk)
536 BUG_TRAP(sk->sk_state == TCP_CLOSE);
537 BUG_TRAP(sock_flag(sk, SOCK_DEAD));
539 /* It cannot be in hash table! */
540 BUG_TRAP(sk_unhashed(sk));
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);
545 sk->sk_prot->destroy(sk);
547 sk_stream_kill_queues(sk);
549 xfrm_sk_free_policy(sk);
551 sk_refcnt_debug_release(sk);
553 atomic_dec(sk->sk_prot->orphan_count);
554 sock_put(sk);
557 EXPORT_SYMBOL(inet_csk_destroy_sock);
559 int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
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);
565 if (rc != 0)
566 return rc;
568 sk->sk_max_ack_backlog = 0;
569 sk->sk_ack_backlog = 0;
570 inet_csk_delack_init(sk);
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.
577 sk->sk_state = TCP_LISTEN;
578 if (!sk->sk_prot->get_port(sk, inet->num)) {
579 inet->sport = htons(inet->num);
581 sk_dst_reset(sk);
582 sk->sk_prot->hash(sk);
584 return 0;
587 sk->sk_state = TCP_CLOSE;
588 __reqsk_queue_destroy(&icsk->icsk_accept_queue);
589 return -EADDRINUSE;
592 EXPORT_SYMBOL_GPL(inet_csk_listen_start);
595 * This routine closes sockets which have been at least partially
596 * opened, but not yet accepted.
598 void inet_csk_listen_stop(struct sock *sk)
600 struct inet_connection_sock *icsk = inet_csk(sk);
601 struct request_sock *acc_req;
602 struct request_sock *req;
604 inet_csk_delete_keepalive_timer(sk);
606 /* make all the listen_opt local to us */
607 acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
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
617 reqsk_queue_destroy(&icsk->icsk_accept_queue);
619 while ((req = acc_req) != NULL) {
620 struct sock *child = req->sk;
622 acc_req = req->dl_next;
624 local_bh_disable();
625 bh_lock_sock(child);
626 BUG_TRAP(!sock_owned_by_user(child));
627 sock_hold(child);
629 sk->sk_prot->disconnect(child, O_NONBLOCK);
631 sock_orphan(child);
633 atomic_inc(sk->sk_prot->orphan_count);
635 inet_csk_destroy_sock(child);
637 bh_unlock_sock(child);
638 local_bh_enable();
639 sock_put(child);
641 sk_acceptq_removed(sk);
642 __reqsk_free(req);
644 BUG_TRAP(!sk->sk_ack_backlog);
647 EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
649 void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
651 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
652 const struct inet_sock *inet = inet_sk(sk);
654 sin->sin_family = AF_INET;
655 sin->sin_addr.s_addr = inet->daddr;
656 sin->sin_port = inet->dport;
659 EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
661 int inet_csk_ctl_sock_create(struct socket **sock, unsigned short family,
662 unsigned short type, unsigned char protocol)
664 int rc = sock_create_kern(family, type, protocol, sock);
666 if (rc == 0) {
667 (*sock)->sk->sk_allocation = GFP_ATOMIC;
668 inet_sk((*sock)->sk)->uc_ttl = -1;
670 * Unhash it so that IP input processing does not even see it,
671 * we do not wish this socket to see incoming packets.
673 (*sock)->sk->sk_prot->unhash((*sock)->sk);
675 return rc;
678 EXPORT_SYMBOL_GPL(inet_csk_ctl_sock_create);
680 #ifdef CONFIG_COMPAT
681 int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
682 char __user *optval, int __user *optlen)
684 const struct inet_connection_sock *icsk = inet_csk(sk);
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);
693 EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
695 int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
696 char __user *optval, int optlen)
698 const struct inet_connection_sock *icsk = inet_csk(sk);
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);
707 EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
708 #endif