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