Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 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. |
| 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 Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 16 | #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 Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 25 | #include <net/xfrm.h> |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 26 | |
| 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 |
| 31 | |
| 32 | /* |
| 33 | * This array holds the first and last local port number. |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 34 | */ |
Mark Glines | 3f196eb | 2007-05-31 15:44:48 -0700 | [diff] [blame] | 35 | int sysctl_local_port_range[2] = { 32768, 61000 }; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 36 | |
Arnaldo Carvalho de Melo | 971af18 | 2005-12-13 23:14:47 -0800 | [diff] [blame] | 37 | int inet_csk_bind_conflict(const struct sock *sk, |
| 38 | const struct inet_bind_bucket *tb) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 39 | { |
Al Viro | 8210323 | 2006-09-27 18:44:10 -0700 | [diff] [blame] | 40 | const __be32 sk_rcv_saddr = inet_rcv_saddr(sk); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 41 | struct sock *sk2; |
| 42 | struct hlist_node *node; |
| 43 | int reuse = sk->sk_reuse; |
| 44 | |
| 45 | sk_for_each_bound(sk2, node, &tb->owners) { |
| 46 | if (sk != sk2 && |
| 47 | !inet_v6_ipv6only(sk2) && |
| 48 | (!sk->sk_bound_dev_if || |
| 49 | !sk2->sk_bound_dev_if || |
| 50 | sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) { |
| 51 | if (!reuse || !sk2->sk_reuse || |
| 52 | sk2->sk_state == TCP_LISTEN) { |
Al Viro | 8210323 | 2006-09-27 18:44:10 -0700 | [diff] [blame] | 53 | const __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 54 | if (!sk2_rcv_saddr || !sk_rcv_saddr || |
| 55 | sk2_rcv_saddr == sk_rcv_saddr) |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return node != NULL; |
| 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | 971af18 | 2005-12-13 23:14:47 -0800 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(inet_csk_bind_conflict); |
| 64 | |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 65 | /* Obtain a reference to a local port for the given sock, |
| 66 | * if snum is zero it means select any available local port. |
| 67 | */ |
| 68 | int inet_csk_get_port(struct inet_hashinfo *hashinfo, |
Arnaldo Carvalho de Melo | 971af18 | 2005-12-13 23:14:47 -0800 | [diff] [blame] | 69 | struct sock *sk, unsigned short snum, |
| 70 | int (*bind_conflict)(const struct sock *sk, |
| 71 | const struct inet_bind_bucket *tb)) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 72 | { |
| 73 | struct inet_bind_hashbucket *head; |
| 74 | struct hlist_node *node; |
| 75 | struct inet_bind_bucket *tb; |
| 76 | int ret; |
| 77 | |
| 78 | local_bh_disable(); |
| 79 | if (!snum) { |
| 80 | int low = sysctl_local_port_range[0]; |
| 81 | int high = sysctl_local_port_range[1]; |
| 82 | int remaining = (high - low) + 1; |
Stephen Hemminger | 6df7163 | 2005-11-03 16:33:23 -0800 | [diff] [blame] | 83 | int rover = net_random() % (high - low) + low; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 84 | |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 85 | do { |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 86 | head = &hashinfo->bhash[inet_bhashfn(rover, hashinfo->bhash_size)]; |
| 87 | spin_lock(&head->lock); |
| 88 | inet_bind_bucket_for_each(tb, node, &head->chain) |
| 89 | if (tb->port == rover) |
| 90 | goto next; |
| 91 | break; |
| 92 | next: |
| 93 | spin_unlock(&head->lock); |
Stephen Hemminger | 6df7163 | 2005-11-03 16:33:23 -0800 | [diff] [blame] | 94 | if (++rover > high) |
| 95 | rover = low; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 96 | } while (--remaining > 0); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 97 | |
| 98 | /* Exhausted local port range during search? It is not |
| 99 | * possible for us to be holding one of the bind hash |
| 100 | * locks if this test triggers, because if 'remaining' |
| 101 | * drops to zero, we broke out of the do/while loop at |
| 102 | * the top level, not from the 'break;' statement. |
| 103 | */ |
| 104 | ret = 1; |
| 105 | if (remaining <= 0) |
| 106 | goto fail; |
| 107 | |
| 108 | /* OK, here is the one we will use. HEAD is |
| 109 | * non-NULL and we hold it's mutex. |
| 110 | */ |
| 111 | snum = rover; |
| 112 | } else { |
| 113 | head = &hashinfo->bhash[inet_bhashfn(snum, hashinfo->bhash_size)]; |
| 114 | spin_lock(&head->lock); |
| 115 | inet_bind_bucket_for_each(tb, node, &head->chain) |
| 116 | if (tb->port == snum) |
| 117 | goto tb_found; |
| 118 | } |
| 119 | tb = NULL; |
| 120 | goto tb_not_found; |
| 121 | tb_found: |
| 122 | if (!hlist_empty(&tb->owners)) { |
| 123 | if (sk->sk_reuse > 1) |
| 124 | goto success; |
| 125 | if (tb->fastreuse > 0 && |
| 126 | sk->sk_reuse && sk->sk_state != TCP_LISTEN) { |
| 127 | goto success; |
| 128 | } else { |
| 129 | ret = 1; |
Arnaldo Carvalho de Melo | 971af18 | 2005-12-13 23:14:47 -0800 | [diff] [blame] | 130 | if (bind_conflict(sk, tb)) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 131 | goto fail_unlock; |
| 132 | } |
| 133 | } |
| 134 | tb_not_found: |
| 135 | ret = 1; |
| 136 | if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep, head, snum)) == NULL) |
| 137 | goto fail_unlock; |
| 138 | if (hlist_empty(&tb->owners)) { |
| 139 | if (sk->sk_reuse && sk->sk_state != TCP_LISTEN) |
| 140 | tb->fastreuse = 1; |
| 141 | else |
| 142 | tb->fastreuse = 0; |
| 143 | } else if (tb->fastreuse && |
| 144 | (!sk->sk_reuse || sk->sk_state == TCP_LISTEN)) |
| 145 | tb->fastreuse = 0; |
| 146 | success: |
| 147 | if (!inet_csk(sk)->icsk_bind_hash) |
| 148 | inet_bind_hash(sk, tb, snum); |
| 149 | BUG_TRAP(inet_csk(sk)->icsk_bind_hash == tb); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 150 | ret = 0; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 151 | |
| 152 | fail_unlock: |
| 153 | spin_unlock(&head->lock); |
| 154 | fail: |
| 155 | local_bh_enable(); |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | EXPORT_SYMBOL_GPL(inet_csk_get_port); |
| 160 | |
| 161 | /* |
| 162 | * Wait for an incoming connection, avoid race conditions. This must be called |
| 163 | * with the socket locked. |
| 164 | */ |
| 165 | static int inet_csk_wait_for_connect(struct sock *sk, long timeo) |
| 166 | { |
| 167 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 168 | DEFINE_WAIT(wait); |
| 169 | int err; |
| 170 | |
| 171 | /* |
| 172 | * True wake-one mechanism for incoming connections: only |
| 173 | * one process gets woken up, not the 'whole herd'. |
| 174 | * Since we do not 'race & poll' for established sockets |
| 175 | * anymore, the common case will execute the loop only once. |
| 176 | * |
| 177 | * Subtle issue: "add_wait_queue_exclusive()" will be added |
| 178 | * after any current non-exclusive waiters, and we know that |
| 179 | * it will always _stay_ after any new non-exclusive waiters |
| 180 | * because all non-exclusive waiters are added at the |
| 181 | * beginning of the wait-queue. As such, it's ok to "drop" |
| 182 | * our exclusiveness temporarily when we get woken up without |
| 183 | * having to remove and re-insert us on the wait queue. |
| 184 | */ |
| 185 | for (;;) { |
| 186 | prepare_to_wait_exclusive(sk->sk_sleep, &wait, |
| 187 | TASK_INTERRUPTIBLE); |
| 188 | release_sock(sk); |
| 189 | if (reqsk_queue_empty(&icsk->icsk_accept_queue)) |
| 190 | timeo = schedule_timeout(timeo); |
| 191 | lock_sock(sk); |
| 192 | err = 0; |
| 193 | if (!reqsk_queue_empty(&icsk->icsk_accept_queue)) |
| 194 | break; |
| 195 | err = -EINVAL; |
| 196 | if (sk->sk_state != TCP_LISTEN) |
| 197 | break; |
| 198 | err = sock_intr_errno(timeo); |
| 199 | if (signal_pending(current)) |
| 200 | break; |
| 201 | err = -EAGAIN; |
| 202 | if (!timeo) |
| 203 | break; |
| 204 | } |
| 205 | finish_wait(sk->sk_sleep, &wait); |
| 206 | return err; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * This will accept the next outstanding connection. |
| 211 | */ |
| 212 | struct sock *inet_csk_accept(struct sock *sk, int flags, int *err) |
| 213 | { |
| 214 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 215 | struct sock *newsk; |
| 216 | int error; |
| 217 | |
| 218 | lock_sock(sk); |
| 219 | |
| 220 | /* We need to make sure that this socket is listening, |
| 221 | * and that it has something pending. |
| 222 | */ |
| 223 | error = -EINVAL; |
| 224 | if (sk->sk_state != TCP_LISTEN) |
| 225 | goto out_err; |
| 226 | |
| 227 | /* Find already established connection */ |
| 228 | if (reqsk_queue_empty(&icsk->icsk_accept_queue)) { |
| 229 | long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 230 | |
| 231 | /* If this is a non blocking socket don't sleep */ |
| 232 | error = -EAGAIN; |
| 233 | if (!timeo) |
| 234 | goto out_err; |
| 235 | |
| 236 | error = inet_csk_wait_for_connect(sk, timeo); |
| 237 | if (error) |
| 238 | goto out_err; |
| 239 | } |
| 240 | |
| 241 | newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk); |
| 242 | BUG_TRAP(newsk->sk_state != TCP_SYN_RECV); |
| 243 | out: |
| 244 | release_sock(sk); |
| 245 | return newsk; |
| 246 | out_err: |
| 247 | newsk = NULL; |
| 248 | *err = error; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | EXPORT_SYMBOL(inet_csk_accept); |
| 253 | |
| 254 | /* |
| 255 | * Using different timers for retransmit, delayed acks and probes |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 256 | * We may wish use just one timer maintaining a list of expire jiffies |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 257 | * to optimize. |
| 258 | */ |
| 259 | void inet_csk_init_xmit_timers(struct sock *sk, |
| 260 | void (*retransmit_handler)(unsigned long), |
| 261 | void (*delack_handler)(unsigned long), |
| 262 | void (*keepalive_handler)(unsigned long)) |
| 263 | { |
| 264 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 265 | |
| 266 | init_timer(&icsk->icsk_retransmit_timer); |
| 267 | init_timer(&icsk->icsk_delack_timer); |
| 268 | init_timer(&sk->sk_timer); |
| 269 | |
| 270 | icsk->icsk_retransmit_timer.function = retransmit_handler; |
| 271 | icsk->icsk_delack_timer.function = delack_handler; |
| 272 | sk->sk_timer.function = keepalive_handler; |
| 273 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 274 | icsk->icsk_retransmit_timer.data = |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 275 | icsk->icsk_delack_timer.data = |
| 276 | sk->sk_timer.data = (unsigned long)sk; |
| 277 | |
| 278 | icsk->icsk_pending = icsk->icsk_ack.pending = 0; |
| 279 | } |
| 280 | |
| 281 | EXPORT_SYMBOL(inet_csk_init_xmit_timers); |
| 282 | |
| 283 | void inet_csk_clear_xmit_timers(struct sock *sk) |
| 284 | { |
| 285 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 286 | |
| 287 | icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0; |
| 288 | |
| 289 | sk_stop_timer(sk, &icsk->icsk_retransmit_timer); |
| 290 | sk_stop_timer(sk, &icsk->icsk_delack_timer); |
| 291 | sk_stop_timer(sk, &sk->sk_timer); |
| 292 | } |
| 293 | |
| 294 | EXPORT_SYMBOL(inet_csk_clear_xmit_timers); |
| 295 | |
| 296 | void inet_csk_delete_keepalive_timer(struct sock *sk) |
| 297 | { |
| 298 | sk_stop_timer(sk, &sk->sk_timer); |
| 299 | } |
| 300 | |
| 301 | EXPORT_SYMBOL(inet_csk_delete_keepalive_timer); |
| 302 | |
| 303 | void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len) |
| 304 | { |
| 305 | sk_reset_timer(sk, &sk->sk_timer, jiffies + len); |
| 306 | } |
| 307 | |
| 308 | EXPORT_SYMBOL(inet_csk_reset_keepalive_timer); |
| 309 | |
| 310 | struct dst_entry* inet_csk_route_req(struct sock *sk, |
| 311 | const struct request_sock *req) |
| 312 | { |
| 313 | struct rtable *rt; |
| 314 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 315 | struct ip_options *opt = inet_rsk(req)->opt; |
| 316 | struct flowi fl = { .oif = sk->sk_bound_dev_if, |
| 317 | .nl_u = { .ip4_u = |
| 318 | { .daddr = ((opt && opt->srr) ? |
| 319 | opt->faddr : |
| 320 | ireq->rmt_addr), |
| 321 | .saddr = ireq->loc_addr, |
| 322 | .tos = RT_CONN_FLAGS(sk) } }, |
| 323 | .proto = sk->sk_protocol, |
| 324 | .uli_u = { .ports = |
| 325 | { .sport = inet_sk(sk)->sport, |
| 326 | .dport = ireq->rmt_port } } }; |
| 327 | |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 328 | security_req_classify_flow(req, &fl); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 329 | if (ip_route_output_flow(&rt, &fl, sk, 0)) { |
| 330 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); |
| 331 | return NULL; |
| 332 | } |
| 333 | if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) { |
| 334 | ip_rt_put(rt); |
| 335 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); |
| 336 | return NULL; |
| 337 | } |
| 338 | return &rt->u.dst; |
| 339 | } |
| 340 | |
| 341 | EXPORT_SYMBOL_GPL(inet_csk_route_req); |
| 342 | |
Al Viro | 6b72977 | 2006-09-27 18:36:59 -0700 | [diff] [blame] | 343 | static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport, |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 344 | const u32 rnd, const u32 synq_hsize) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 345 | { |
Al Viro | 6b72977 | 2006-09-27 18:36:59 -0700 | [diff] [blame] | 346 | return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 350 | #define AF_INET_FAMILY(fam) ((fam) == AF_INET) |
| 351 | #else |
| 352 | #define AF_INET_FAMILY(fam) 1 |
| 353 | #endif |
| 354 | |
| 355 | struct request_sock *inet_csk_search_req(const struct sock *sk, |
| 356 | struct request_sock ***prevp, |
Al Viro | 6b72977 | 2006-09-27 18:36:59 -0700 | [diff] [blame] | 357 | const __be16 rport, const __be32 raddr, |
Al Viro | 7f25afb | 2006-09-27 18:27:47 -0700 | [diff] [blame] | 358 | const __be32 laddr) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 359 | { |
| 360 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 361 | struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; |
| 362 | struct request_sock *req, **prev; |
| 363 | |
| 364 | for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd, |
| 365 | lopt->nr_table_entries)]; |
| 366 | (req = *prev) != NULL; |
| 367 | prev = &req->dl_next) { |
| 368 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 369 | |
| 370 | if (ireq->rmt_port == rport && |
| 371 | ireq->rmt_addr == raddr && |
| 372 | ireq->loc_addr == laddr && |
| 373 | AF_INET_FAMILY(req->rsk_ops->family)) { |
| 374 | BUG_TRAP(!req->sk); |
| 375 | *prevp = prev; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return req; |
| 381 | } |
| 382 | |
| 383 | EXPORT_SYMBOL_GPL(inet_csk_search_req); |
| 384 | |
| 385 | void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req, |
Arnaldo Carvalho de Melo | c2977c2 | 2005-12-13 23:15:12 -0800 | [diff] [blame] | 386 | unsigned long timeout) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 387 | { |
| 388 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 389 | struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; |
| 390 | const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port, |
| 391 | lopt->hash_rnd, lopt->nr_table_entries); |
| 392 | |
| 393 | reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout); |
| 394 | inet_csk_reqsk_queue_added(sk, timeout); |
| 395 | } |
| 396 | |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 397 | /* Only thing we need from tcp.h */ |
| 398 | extern int sysctl_tcp_synack_retries; |
| 399 | |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 400 | EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add); |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 401 | |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 402 | void inet_csk_reqsk_queue_prune(struct sock *parent, |
| 403 | const unsigned long interval, |
| 404 | const unsigned long timeout, |
| 405 | const unsigned long max_rto) |
| 406 | { |
| 407 | struct inet_connection_sock *icsk = inet_csk(parent); |
| 408 | struct request_sock_queue *queue = &icsk->icsk_accept_queue; |
| 409 | struct listen_sock *lopt = queue->listen_opt; |
| 410 | int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries; |
| 411 | int thresh = max_retries; |
| 412 | unsigned long now = jiffies; |
| 413 | struct request_sock **reqp, *req; |
| 414 | int i, budget; |
| 415 | |
| 416 | if (lopt == NULL || lopt->qlen == 0) |
| 417 | return; |
| 418 | |
| 419 | /* Normally all the openreqs are young and become mature |
| 420 | * (i.e. converted to established socket) for first timeout. |
| 421 | * If synack was not acknowledged for 3 seconds, it means |
| 422 | * one of the following things: synack was lost, ack was lost, |
| 423 | * rtt is high or nobody planned to ack (i.e. synflood). |
| 424 | * When server is a bit loaded, queue is populated with old |
| 425 | * open requests, reducing effective size of queue. |
| 426 | * When server is well loaded, queue size reduces to zero |
| 427 | * after several minutes of work. It is not synflood, |
| 428 | * it is normal operation. The solution is pruning |
| 429 | * too old entries overriding normal timeout, when |
| 430 | * situation becomes dangerous. |
| 431 | * |
| 432 | * Essentially, we reserve half of room for young |
| 433 | * embrions; and abort old ones without pity, if old |
| 434 | * ones are about to clog our table. |
| 435 | */ |
| 436 | if (lopt->qlen>>(lopt->max_qlen_log-1)) { |
| 437 | int young = (lopt->qlen_young<<1); |
| 438 | |
| 439 | while (thresh > 2) { |
| 440 | if (lopt->qlen < young) |
| 441 | break; |
| 442 | thresh--; |
| 443 | young <<= 1; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (queue->rskq_defer_accept) |
| 448 | max_retries = queue->rskq_defer_accept; |
| 449 | |
| 450 | budget = 2 * (lopt->nr_table_entries / (timeout / interval)); |
| 451 | i = lopt->clock_hand; |
| 452 | |
| 453 | do { |
| 454 | reqp=&lopt->syn_table[i]; |
| 455 | while ((req = *reqp) != NULL) { |
| 456 | if (time_after_eq(now, req->expires)) { |
| 457 | if ((req->retrans < thresh || |
| 458 | (inet_rsk(req)->acked && req->retrans < max_retries)) |
| 459 | && !req->rsk_ops->rtx_syn_ack(parent, req, NULL)) { |
| 460 | unsigned long timeo; |
| 461 | |
| 462 | if (req->retrans++ == 0) |
| 463 | lopt->qlen_young--; |
| 464 | timeo = min((timeout << req->retrans), max_rto); |
| 465 | req->expires = now + timeo; |
| 466 | reqp = &req->dl_next; |
| 467 | continue; |
| 468 | } |
| 469 | |
| 470 | /* Drop this request */ |
| 471 | inet_csk_reqsk_queue_unlink(parent, req, reqp); |
| 472 | reqsk_queue_removed(queue, req); |
| 473 | reqsk_free(req); |
| 474 | continue; |
| 475 | } |
| 476 | reqp = &req->dl_next; |
| 477 | } |
| 478 | |
| 479 | i = (i + 1) & (lopt->nr_table_entries - 1); |
| 480 | |
| 481 | } while (--budget > 0); |
| 482 | |
| 483 | lopt->clock_hand = i; |
| 484 | |
| 485 | if (lopt->qlen) |
| 486 | inet_csk_reset_keepalive_timer(parent, interval); |
| 487 | } |
| 488 | |
| 489 | EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune); |
| 490 | |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 491 | struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 492 | const gfp_t priority) |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 493 | { |
| 494 | struct sock *newsk = sk_clone(sk, priority); |
| 495 | |
| 496 | if (newsk != NULL) { |
| 497 | struct inet_connection_sock *newicsk = inet_csk(newsk); |
| 498 | |
| 499 | newsk->sk_state = TCP_SYN_RECV; |
| 500 | newicsk->icsk_bind_hash = NULL; |
| 501 | |
| 502 | inet_sk(newsk)->dport = inet_rsk(req)->rmt_port; |
| 503 | newsk->sk_write_space = sk_stream_write_space; |
| 504 | |
| 505 | newicsk->icsk_retransmits = 0; |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 506 | newicsk->icsk_backoff = 0; |
| 507 | newicsk->icsk_probes_out = 0; |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 508 | |
| 509 | /* Deinitialize accept_queue to trap illegal accesses. */ |
| 510 | memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue)); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 511 | |
| 512 | security_inet_csk_clone(newsk, req); |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 513 | } |
| 514 | return newsk; |
| 515 | } |
| 516 | |
| 517 | EXPORT_SYMBOL_GPL(inet_csk_clone); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * At this point, there should be no process reference to this |
| 521 | * socket, and thus no user references at all. Therefore we |
| 522 | * can assume the socket waitqueue is inactive and nobody will |
| 523 | * try to jump onto it. |
| 524 | */ |
| 525 | void inet_csk_destroy_sock(struct sock *sk) |
| 526 | { |
| 527 | BUG_TRAP(sk->sk_state == TCP_CLOSE); |
| 528 | BUG_TRAP(sock_flag(sk, SOCK_DEAD)); |
| 529 | |
| 530 | /* It cannot be in hash table! */ |
| 531 | BUG_TRAP(sk_unhashed(sk)); |
| 532 | |
| 533 | /* If it has not 0 inet_sk(sk)->num, it must be bound */ |
| 534 | BUG_TRAP(!inet_sk(sk)->num || inet_csk(sk)->icsk_bind_hash); |
| 535 | |
| 536 | sk->sk_prot->destroy(sk); |
| 537 | |
| 538 | sk_stream_kill_queues(sk); |
| 539 | |
| 540 | xfrm_sk_free_policy(sk); |
| 541 | |
| 542 | sk_refcnt_debug_release(sk); |
| 543 | |
| 544 | atomic_dec(sk->sk_prot->orphan_count); |
| 545 | sock_put(sk); |
| 546 | } |
| 547 | |
| 548 | EXPORT_SYMBOL(inet_csk_destroy_sock); |
| 549 | |
| 550 | int inet_csk_listen_start(struct sock *sk, const int nr_table_entries) |
| 551 | { |
| 552 | struct inet_sock *inet = inet_sk(sk); |
| 553 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 554 | int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries); |
| 555 | |
| 556 | if (rc != 0) |
| 557 | return rc; |
| 558 | |
| 559 | sk->sk_max_ack_backlog = 0; |
| 560 | sk->sk_ack_backlog = 0; |
| 561 | inet_csk_delack_init(sk); |
| 562 | |
| 563 | /* There is race window here: we announce ourselves listening, |
| 564 | * but this transition is still not validated by get_port(). |
| 565 | * It is OK, because this socket enters to hash table only |
| 566 | * after validation is complete. |
| 567 | */ |
| 568 | sk->sk_state = TCP_LISTEN; |
| 569 | if (!sk->sk_prot->get_port(sk, inet->num)) { |
| 570 | inet->sport = htons(inet->num); |
| 571 | |
| 572 | sk_dst_reset(sk); |
| 573 | sk->sk_prot->hash(sk); |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | sk->sk_state = TCP_CLOSE; |
| 579 | __reqsk_queue_destroy(&icsk->icsk_accept_queue); |
| 580 | return -EADDRINUSE; |
| 581 | } |
| 582 | |
| 583 | EXPORT_SYMBOL_GPL(inet_csk_listen_start); |
| 584 | |
| 585 | /* |
| 586 | * This routine closes sockets which have been at least partially |
| 587 | * opened, but not yet accepted. |
| 588 | */ |
| 589 | void inet_csk_listen_stop(struct sock *sk) |
| 590 | { |
| 591 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 592 | struct request_sock *acc_req; |
| 593 | struct request_sock *req; |
| 594 | |
| 595 | inet_csk_delete_keepalive_timer(sk); |
| 596 | |
| 597 | /* make all the listen_opt local to us */ |
| 598 | acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue); |
| 599 | |
| 600 | /* Following specs, it would be better either to send FIN |
| 601 | * (and enter FIN-WAIT-1, it is normal close) |
| 602 | * or to send active reset (abort). |
| 603 | * Certainly, it is pretty dangerous while synflood, but it is |
| 604 | * bad justification for our negligence 8) |
| 605 | * To be honest, we are not able to make either |
| 606 | * of the variants now. --ANK |
| 607 | */ |
| 608 | reqsk_queue_destroy(&icsk->icsk_accept_queue); |
| 609 | |
| 610 | while ((req = acc_req) != NULL) { |
| 611 | struct sock *child = req->sk; |
| 612 | |
| 613 | acc_req = req->dl_next; |
| 614 | |
| 615 | local_bh_disable(); |
| 616 | bh_lock_sock(child); |
| 617 | BUG_TRAP(!sock_owned_by_user(child)); |
| 618 | sock_hold(child); |
| 619 | |
| 620 | sk->sk_prot->disconnect(child, O_NONBLOCK); |
| 621 | |
| 622 | sock_orphan(child); |
| 623 | |
| 624 | atomic_inc(sk->sk_prot->orphan_count); |
| 625 | |
| 626 | inet_csk_destroy_sock(child); |
| 627 | |
| 628 | bh_unlock_sock(child); |
| 629 | local_bh_enable(); |
| 630 | sock_put(child); |
| 631 | |
| 632 | sk_acceptq_removed(sk); |
| 633 | __reqsk_free(req); |
| 634 | } |
| 635 | BUG_TRAP(!sk->sk_ack_backlog); |
| 636 | } |
| 637 | |
| 638 | EXPORT_SYMBOL_GPL(inet_csk_listen_stop); |
Arnaldo Carvalho de Melo | af05dc9 | 2005-12-13 23:16:04 -0800 | [diff] [blame] | 639 | |
| 640 | void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr) |
| 641 | { |
| 642 | struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; |
| 643 | const struct inet_sock *inet = inet_sk(sk); |
| 644 | |
| 645 | sin->sin_family = AF_INET; |
| 646 | sin->sin_addr.s_addr = inet->daddr; |
| 647 | sin->sin_port = inet->dport; |
| 648 | } |
| 649 | |
| 650 | EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr); |
Arnaldo Carvalho de Melo | c4d9390 | 2006-03-20 22:01:03 -0800 | [diff] [blame] | 651 | |
| 652 | int inet_csk_ctl_sock_create(struct socket **sock, unsigned short family, |
| 653 | unsigned short type, unsigned char protocol) |
| 654 | { |
| 655 | int rc = sock_create_kern(family, type, protocol, sock); |
| 656 | |
| 657 | if (rc == 0) { |
| 658 | (*sock)->sk->sk_allocation = GFP_ATOMIC; |
| 659 | inet_sk((*sock)->sk)->uc_ttl = -1; |
| 660 | /* |
| 661 | * Unhash it so that IP input processing does not even see it, |
| 662 | * we do not wish this socket to see incoming packets. |
| 663 | */ |
| 664 | (*sock)->sk->sk_prot->unhash((*sock)->sk); |
| 665 | } |
| 666 | return rc; |
| 667 | } |
| 668 | |
| 669 | EXPORT_SYMBOL_GPL(inet_csk_ctl_sock_create); |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 670 | |
| 671 | #ifdef CONFIG_COMPAT |
| 672 | int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname, |
| 673 | char __user *optval, int __user *optlen) |
| 674 | { |
David S. Miller | dbeff12 | 2006-03-20 22:52:32 -0800 | [diff] [blame] | 675 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 676 | |
| 677 | if (icsk->icsk_af_ops->compat_getsockopt != NULL) |
| 678 | return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname, |
| 679 | optval, optlen); |
| 680 | return icsk->icsk_af_ops->getsockopt(sk, level, optname, |
| 681 | optval, optlen); |
| 682 | } |
| 683 | |
| 684 | EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt); |
| 685 | |
| 686 | int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname, |
| 687 | char __user *optval, int optlen) |
| 688 | { |
David S. Miller | dbeff12 | 2006-03-20 22:52:32 -0800 | [diff] [blame] | 689 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 690 | |
| 691 | if (icsk->icsk_af_ops->compat_setsockopt != NULL) |
| 692 | return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname, |
| 693 | optval, optlen); |
| 694 | return icsk->icsk_af_ops->setsockopt(sk, level, optname, |
| 695 | optval, optlen); |
| 696 | } |
| 697 | |
| 698 | EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt); |
| 699 | #endif |