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 | /* |
Eric Dumazet | 3c689b7 | 2008-10-08 14:18:04 -0700 | [diff] [blame] | 33 | * This struct holds the first and last local port number. |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 34 | */ |
Eric Dumazet | 3c689b7 | 2008-10-08 14:18:04 -0700 | [diff] [blame] | 35 | struct local_ports sysctl_local_ports __read_mostly = { |
Eric Dumazet | c4dbe54 | 2011-05-24 14:08:08 +0200 | [diff] [blame] | 36 | .lock = __SEQLOCK_UNLOCKED(sysctl_local_ports.lock), |
Eric Dumazet | 3c689b7 | 2008-10-08 14:18:04 -0700 | [diff] [blame] | 37 | .range = { 32768, 61000 }, |
| 38 | }; |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 39 | |
Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 40 | unsigned long *sysctl_local_reserved_ports; |
| 41 | EXPORT_SYMBOL(sysctl_local_reserved_ports); |
| 42 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 43 | void inet_get_local_port_range(int *low, int *high) |
| 44 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 45 | unsigned int seq; |
| 46 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 47 | do { |
Eric Dumazet | 3c689b7 | 2008-10-08 14:18:04 -0700 | [diff] [blame] | 48 | seq = read_seqbegin(&sysctl_local_ports.lock); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 49 | |
Eric Dumazet | 3c689b7 | 2008-10-08 14:18:04 -0700 | [diff] [blame] | 50 | *low = sysctl_local_ports.range[0]; |
| 51 | *high = sysctl_local_ports.range[1]; |
| 52 | } while (read_seqretry(&sysctl_local_ports.lock, seq)); |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 53 | } |
| 54 | EXPORT_SYMBOL(inet_get_local_port_range); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 55 | |
Arnaldo Carvalho de Melo | 971af18 | 2005-12-13 23:14:47 -0800 | [diff] [blame] | 56 | int inet_csk_bind_conflict(const struct sock *sk, |
Alex Copot | aacd928 | 2012-04-12 22:21:45 +0000 | [diff] [blame] | 57 | const struct inet_bind_bucket *tb, bool relax) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 58 | { |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 59 | struct sock *sk2; |
| 60 | struct hlist_node *node; |
| 61 | int reuse = sk->sk_reuse; |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 62 | int reuseport = sk->sk_reuseport; |
| 63 | kuid_t uid = sock_i_uid((struct sock *)sk); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 64 | |
Pavel Emelyanov | 7477fd2e | 2008-04-14 02:42:27 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Unlike other sk lookup places we do not check |
| 67 | * for sk_net here, since _all_ the socks listed |
| 68 | * in tb->owners list belong to the same net - the |
| 69 | * one this bucket belongs to. |
| 70 | */ |
| 71 | |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 72 | sk_for_each_bound(sk2, node, &tb->owners) { |
| 73 | if (sk != sk2 && |
| 74 | !inet_v6_ipv6only(sk2) && |
| 75 | (!sk->sk_bound_dev_if || |
| 76 | !sk2->sk_bound_dev_if || |
| 77 | sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) { |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 78 | if ((!reuse || !sk2->sk_reuse || |
| 79 | sk2->sk_state == TCP_LISTEN) && |
| 80 | (!reuseport || !sk2->sk_reuseport || |
| 81 | (sk2->sk_state != TCP_TIME_WAIT && |
| 82 | !uid_eq(uid, sock_i_uid(sk2))))) { |
Eric Dumazet | 68835ab | 2010-11-30 19:04:07 +0000 | [diff] [blame] | 83 | const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2); |
| 84 | if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) || |
| 85 | sk2_rcv_saddr == sk_rcv_saddr(sk)) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 86 | break; |
David S. Miller | 8d238b2 | 2010-04-28 11:25:59 -0700 | [diff] [blame] | 87 | } |
Alex Copot | aacd928 | 2012-04-12 22:21:45 +0000 | [diff] [blame] | 88 | if (!relax && reuse && sk2->sk_reuse && |
| 89 | sk2->sk_state != TCP_LISTEN) { |
| 90 | const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2); |
| 91 | |
| 92 | if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) || |
| 93 | sk2_rcv_saddr == sk_rcv_saddr(sk)) |
| 94 | break; |
| 95 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | return node != NULL; |
| 99 | } |
Arnaldo Carvalho de Melo | 971af18 | 2005-12-13 23:14:47 -0800 | [diff] [blame] | 100 | EXPORT_SYMBOL_GPL(inet_csk_bind_conflict); |
| 101 | |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 102 | /* Obtain a reference to a local port for the given sock, |
| 103 | * if snum is zero it means select any available local port. |
| 104 | */ |
Arnaldo Carvalho de Melo | ab1e0a1 | 2008-02-03 04:06:04 -0800 | [diff] [blame] | 105 | int inet_csk_get_port(struct sock *sk, unsigned short snum) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 106 | { |
Pavel Emelyanov | 39d8cda | 2008-03-22 16:50:58 -0700 | [diff] [blame] | 107 | struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 108 | struct inet_bind_hashbucket *head; |
| 109 | struct hlist_node *node; |
| 110 | struct inet_bind_bucket *tb; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 111 | int ret, attempts = 5; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 112 | struct net *net = sock_net(sk); |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 113 | int smallest_size = -1, smallest_rover; |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 114 | kuid_t uid = sock_i_uid(sk); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 115 | |
| 116 | local_bh_disable(); |
| 117 | if (!snum) { |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 118 | int remaining, rover, low, high; |
| 119 | |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 120 | again: |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 121 | inet_get_local_port_range(&low, &high); |
Anton Arapov | a25de53 | 2007-10-18 22:00:17 -0700 | [diff] [blame] | 122 | remaining = (high - low) + 1; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 123 | smallest_rover = rover = net_random() % remaining + low; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 124 | |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 125 | smallest_size = -1; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 126 | do { |
Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 127 | if (inet_is_reserved_local_port(rover)) |
| 128 | goto next_nolock; |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 129 | head = &hashinfo->bhash[inet_bhashfn(net, rover, |
| 130 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 131 | spin_lock(&head->lock); |
| 132 | inet_bind_bucket_for_each(tb, node, &head->chain) |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 133 | if (net_eq(ib_net(tb), net) && tb->port == rover) { |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 134 | if (((tb->fastreuse > 0 && |
| 135 | sk->sk_reuse && |
| 136 | sk->sk_state != TCP_LISTEN) || |
| 137 | (tb->fastreuseport > 0 && |
| 138 | sk->sk_reuseport && |
| 139 | uid_eq(tb->fastuid, uid))) && |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 140 | (tb->num_owners < smallest_size || smallest_size == -1)) { |
| 141 | smallest_size = tb->num_owners; |
| 142 | smallest_rover = rover; |
Alex Copot | aacd928 | 2012-04-12 22:21:45 +0000 | [diff] [blame] | 143 | if (atomic_read(&hashinfo->bsockets) > (high - low) + 1 && |
| 144 | !inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) { |
David S. Miller | 8d238b2 | 2010-04-28 11:25:59 -0700 | [diff] [blame] | 145 | snum = smallest_rover; |
Flavio Leitner | fddb7b5 | 2012-01-25 08:34:52 +0000 | [diff] [blame] | 146 | goto tb_found; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
Alex Copot | aacd928 | 2012-04-12 22:21:45 +0000 | [diff] [blame] | 149 | if (!inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, false)) { |
Flavio Leitner | 2b05ad3 | 2012-01-25 08:34:51 +0000 | [diff] [blame] | 150 | snum = rover; |
Flavio Leitner | fddb7b5 | 2012-01-25 08:34:52 +0000 | [diff] [blame] | 151 | goto tb_found; |
Flavio Leitner | 2b05ad3 | 2012-01-25 08:34:51 +0000 | [diff] [blame] | 152 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 153 | goto next; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 154 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 155 | break; |
| 156 | next: |
| 157 | spin_unlock(&head->lock); |
Amerigo Wang | e3826f1 | 2010-05-05 00:27:06 +0000 | [diff] [blame] | 158 | next_nolock: |
Stephen Hemminger | 6df7163 | 2005-11-03 16:33:23 -0800 | [diff] [blame] | 159 | if (++rover > high) |
| 160 | rover = low; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 161 | } while (--remaining > 0); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 162 | |
| 163 | /* Exhausted local port range during search? It is not |
| 164 | * possible for us to be holding one of the bind hash |
| 165 | * locks if this test triggers, because if 'remaining' |
| 166 | * drops to zero, we broke out of the do/while loop at |
| 167 | * the top level, not from the 'break;' statement. |
| 168 | */ |
| 169 | ret = 1; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 170 | if (remaining <= 0) { |
| 171 | if (smallest_size != -1) { |
| 172 | snum = smallest_rover; |
| 173 | goto have_snum; |
| 174 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 175 | goto fail; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 176 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 177 | /* OK, here is the one we will use. HEAD is |
| 178 | * non-NULL and we hold it's mutex. |
| 179 | */ |
| 180 | snum = rover; |
| 181 | } else { |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 182 | have_snum: |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 183 | head = &hashinfo->bhash[inet_bhashfn(net, snum, |
| 184 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 185 | spin_lock(&head->lock); |
| 186 | inet_bind_bucket_for_each(tb, node, &head->chain) |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 187 | if (net_eq(ib_net(tb), net) && tb->port == snum) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 188 | goto tb_found; |
| 189 | } |
| 190 | tb = NULL; |
| 191 | goto tb_not_found; |
| 192 | tb_found: |
| 193 | if (!hlist_empty(&tb->owners)) { |
Pavel Emelyanov | 4a17fd5 | 2012-04-19 03:39:36 +0000 | [diff] [blame] | 194 | if (sk->sk_reuse == SK_FORCE_REUSE) |
| 195 | goto success; |
| 196 | |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 197 | if (((tb->fastreuse > 0 && |
| 198 | sk->sk_reuse && sk->sk_state != TCP_LISTEN) || |
| 199 | (tb->fastreuseport > 0 && |
| 200 | sk->sk_reuseport && uid_eq(tb->fastuid, uid))) && |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 201 | smallest_size == -1) { |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 202 | goto success; |
| 203 | } else { |
| 204 | ret = 1; |
Alex Copot | aacd928 | 2012-04-12 22:21:45 +0000 | [diff] [blame] | 205 | if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, true)) { |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 206 | if (((sk->sk_reuse && sk->sk_state != TCP_LISTEN) || |
| 207 | (sk->sk_reuseport && uid_eq(tb->fastuid, uid))) && |
Stephen Hemminger | 5add300 | 2009-02-01 01:40:17 -0800 | [diff] [blame] | 208 | smallest_size != -1 && --attempts >= 0) { |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 209 | spin_unlock(&head->lock); |
| 210 | goto again; |
| 211 | } |
Alex Copot | aacd928 | 2012-04-12 22:21:45 +0000 | [diff] [blame] | 212 | |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 213 | goto fail_unlock; |
Evgeniy Polyakov | a9d8f91 | 2009-01-19 16:46:02 -0800 | [diff] [blame] | 214 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | tb_not_found: |
| 218 | ret = 1; |
Pavel Emelyanov | 941b1d2 | 2008-01-31 05:05:50 -0800 | [diff] [blame] | 219 | if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep, |
| 220 | net, head, snum)) == NULL) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 221 | goto fail_unlock; |
| 222 | if (hlist_empty(&tb->owners)) { |
| 223 | if (sk->sk_reuse && sk->sk_state != TCP_LISTEN) |
| 224 | tb->fastreuse = 1; |
| 225 | else |
| 226 | tb->fastreuse = 0; |
Tom Herbert | da5e363 | 2013-01-22 09:50:24 +0000 | [diff] [blame^] | 227 | if (sk->sk_reuseport) { |
| 228 | tb->fastreuseport = 1; |
| 229 | tb->fastuid = uid; |
| 230 | } else { |
| 231 | tb->fastreuseport = 0; |
| 232 | tb->fastuid = 0; |
| 233 | } |
| 234 | } else { |
| 235 | if (tb->fastreuse && |
| 236 | (!sk->sk_reuse || sk->sk_state == TCP_LISTEN)) |
| 237 | tb->fastreuse = 0; |
| 238 | if (tb->fastreuseport && |
| 239 | (!sk->sk_reuseport || !uid_eq(tb->fastuid, uid))) { |
| 240 | tb->fastreuseport = 0; |
| 241 | tb->fastuid = 0; |
| 242 | } |
| 243 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 244 | success: |
| 245 | if (!inet_csk(sk)->icsk_bind_hash) |
| 246 | inet_bind_hash(sk, tb, snum); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 247 | WARN_ON(inet_csk(sk)->icsk_bind_hash != tb); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 248 | ret = 0; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 249 | |
| 250 | fail_unlock: |
| 251 | spin_unlock(&head->lock); |
| 252 | fail: |
| 253 | local_bh_enable(); |
| 254 | return ret; |
| 255 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 256 | EXPORT_SYMBOL_GPL(inet_csk_get_port); |
| 257 | |
| 258 | /* |
| 259 | * Wait for an incoming connection, avoid race conditions. This must be called |
| 260 | * with the socket locked. |
| 261 | */ |
| 262 | static int inet_csk_wait_for_connect(struct sock *sk, long timeo) |
| 263 | { |
| 264 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 265 | DEFINE_WAIT(wait); |
| 266 | int err; |
| 267 | |
| 268 | /* |
| 269 | * True wake-one mechanism for incoming connections: only |
| 270 | * one process gets woken up, not the 'whole herd'. |
| 271 | * Since we do not 'race & poll' for established sockets |
| 272 | * anymore, the common case will execute the loop only once. |
| 273 | * |
| 274 | * Subtle issue: "add_wait_queue_exclusive()" will be added |
| 275 | * after any current non-exclusive waiters, and we know that |
| 276 | * it will always _stay_ after any new non-exclusive waiters |
| 277 | * because all non-exclusive waiters are added at the |
| 278 | * beginning of the wait-queue. As such, it's ok to "drop" |
| 279 | * our exclusiveness temporarily when we get woken up without |
| 280 | * having to remove and re-insert us on the wait queue. |
| 281 | */ |
| 282 | for (;;) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 283 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 284 | TASK_INTERRUPTIBLE); |
| 285 | release_sock(sk); |
| 286 | if (reqsk_queue_empty(&icsk->icsk_accept_queue)) |
| 287 | timeo = schedule_timeout(timeo); |
| 288 | lock_sock(sk); |
| 289 | err = 0; |
| 290 | if (!reqsk_queue_empty(&icsk->icsk_accept_queue)) |
| 291 | break; |
| 292 | err = -EINVAL; |
| 293 | if (sk->sk_state != TCP_LISTEN) |
| 294 | break; |
| 295 | err = sock_intr_errno(timeo); |
| 296 | if (signal_pending(current)) |
| 297 | break; |
| 298 | err = -EAGAIN; |
| 299 | if (!timeo) |
| 300 | break; |
| 301 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 302 | finish_wait(sk_sleep(sk), &wait); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 303 | return err; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * This will accept the next outstanding connection. |
| 308 | */ |
| 309 | struct sock *inet_csk_accept(struct sock *sk, int flags, int *err) |
| 310 | { |
| 311 | struct inet_connection_sock *icsk = inet_csk(sk); |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 312 | struct request_sock_queue *queue = &icsk->icsk_accept_queue; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 313 | struct sock *newsk; |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 314 | struct request_sock *req; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 315 | int error; |
| 316 | |
| 317 | lock_sock(sk); |
| 318 | |
| 319 | /* We need to make sure that this socket is listening, |
| 320 | * and that it has something pending. |
| 321 | */ |
| 322 | error = -EINVAL; |
| 323 | if (sk->sk_state != TCP_LISTEN) |
| 324 | goto out_err; |
| 325 | |
| 326 | /* Find already established connection */ |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 327 | if (reqsk_queue_empty(queue)) { |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 328 | long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 329 | |
| 330 | /* If this is a non blocking socket don't sleep */ |
| 331 | error = -EAGAIN; |
| 332 | if (!timeo) |
| 333 | goto out_err; |
| 334 | |
| 335 | error = inet_csk_wait_for_connect(sk, timeo); |
| 336 | if (error) |
| 337 | goto out_err; |
| 338 | } |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 339 | req = reqsk_queue_remove(queue); |
| 340 | newsk = req->sk; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 341 | |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 342 | sk_acceptq_removed(sk); |
Eric Dumazet | 7ab4551 | 2012-09-06 08:07:13 +0000 | [diff] [blame] | 343 | if (sk->sk_protocol == IPPROTO_TCP && queue->fastopenq != NULL) { |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 344 | spin_lock_bh(&queue->fastopenq->lock); |
| 345 | if (tcp_rsk(req)->listener) { |
| 346 | /* We are still waiting for the final ACK from 3WHS |
| 347 | * so can't free req now. Instead, we set req->sk to |
| 348 | * NULL to signify that the child socket is taken |
| 349 | * so reqsk_fastopen_remove() will free the req |
| 350 | * when 3WHS finishes (or is aborted). |
| 351 | */ |
| 352 | req->sk = NULL; |
| 353 | req = NULL; |
| 354 | } |
| 355 | spin_unlock_bh(&queue->fastopenq->lock); |
| 356 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 357 | out: |
| 358 | release_sock(sk); |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 359 | if (req) |
| 360 | __reqsk_free(req); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 361 | return newsk; |
| 362 | out_err: |
| 363 | newsk = NULL; |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 364 | req = NULL; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 365 | *err = error; |
| 366 | goto out; |
| 367 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 368 | EXPORT_SYMBOL(inet_csk_accept); |
| 369 | |
| 370 | /* |
| 371 | * Using different timers for retransmit, delayed acks and probes |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 372 | * 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] | 373 | * to optimize. |
| 374 | */ |
| 375 | void inet_csk_init_xmit_timers(struct sock *sk, |
| 376 | void (*retransmit_handler)(unsigned long), |
| 377 | void (*delack_handler)(unsigned long), |
| 378 | void (*keepalive_handler)(unsigned long)) |
| 379 | { |
| 380 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 381 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 382 | setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler, |
| 383 | (unsigned long)sk); |
| 384 | setup_timer(&icsk->icsk_delack_timer, delack_handler, |
| 385 | (unsigned long)sk); |
| 386 | setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 387 | icsk->icsk_pending = icsk->icsk_ack.pending = 0; |
| 388 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 389 | EXPORT_SYMBOL(inet_csk_init_xmit_timers); |
| 390 | |
| 391 | void inet_csk_clear_xmit_timers(struct sock *sk) |
| 392 | { |
| 393 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 394 | |
| 395 | icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0; |
| 396 | |
| 397 | sk_stop_timer(sk, &icsk->icsk_retransmit_timer); |
| 398 | sk_stop_timer(sk, &icsk->icsk_delack_timer); |
| 399 | sk_stop_timer(sk, &sk->sk_timer); |
| 400 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 401 | EXPORT_SYMBOL(inet_csk_clear_xmit_timers); |
| 402 | |
| 403 | void inet_csk_delete_keepalive_timer(struct sock *sk) |
| 404 | { |
| 405 | sk_stop_timer(sk, &sk->sk_timer); |
| 406 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 407 | EXPORT_SYMBOL(inet_csk_delete_keepalive_timer); |
| 408 | |
| 409 | void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len) |
| 410 | { |
| 411 | sk_reset_timer(sk, &sk->sk_timer, jiffies + len); |
| 412 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 413 | EXPORT_SYMBOL(inet_csk_reset_keepalive_timer); |
| 414 | |
Jianjun Kong | d9319100 | 2008-11-03 00:23:42 -0800 | [diff] [blame] | 415 | struct dst_entry *inet_csk_route_req(struct sock *sk, |
David S. Miller | 6bd023f | 2011-05-18 18:32:03 -0400 | [diff] [blame] | 416 | struct flowi4 *fl4, |
David S. Miller | ba3f7f0 | 2012-07-17 14:02:46 -0700 | [diff] [blame] | 417 | const struct request_sock *req) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 418 | { |
| 419 | struct rtable *rt; |
| 420 | const struct inet_request_sock *ireq = inet_rsk(req); |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 421 | struct ip_options_rcu *opt = inet_rsk(req)->opt; |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 422 | struct net *net = sock_net(sk); |
David S. Miller | 3e12939 | 2012-07-10 04:01:57 -0700 | [diff] [blame] | 423 | int flags = inet_sk_flowi_flags(sk); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 424 | |
David S. Miller | 6bd023f | 2011-05-18 18:32:03 -0400 | [diff] [blame] | 425 | flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, |
David S. Miller | e79d9bc | 2011-03-31 04:53:20 -0700 | [diff] [blame] | 426 | RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, |
Eric Dumazet | 7433819 | 2012-05-31 21:00:26 +0000 | [diff] [blame] | 427 | sk->sk_protocol, |
Eric Dumazet | 7586ece | 2012-06-20 05:02:19 +0000 | [diff] [blame] | 428 | flags, |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 429 | (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr, |
David S. Miller | e79d9bc | 2011-03-31 04:53:20 -0700 | [diff] [blame] | 430 | ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport); |
David S. Miller | 6bd023f | 2011-05-18 18:32:03 -0400 | [diff] [blame] | 431 | security_req_classify_flow(req, flowi4_to_flowi(fl4)); |
| 432 | rt = ip_route_output_flow(net, fl4, sk); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 433 | if (IS_ERR(rt)) |
Ilpo Järvinen | 857a6e0 | 2008-12-14 23:13:08 -0800 | [diff] [blame] | 434 | goto no_route; |
Julian Anastasov | 155e833 | 2012-10-08 11:41:18 +0000 | [diff] [blame] | 435 | if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway) |
Ilpo Järvinen | 857a6e0 | 2008-12-14 23:13:08 -0800 | [diff] [blame] | 436 | goto route_err; |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 437 | return &rt->dst; |
Ilpo Järvinen | 857a6e0 | 2008-12-14 23:13:08 -0800 | [diff] [blame] | 438 | |
| 439 | route_err: |
| 440 | ip_rt_put(rt); |
| 441 | no_route: |
| 442 | IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); |
| 443 | return NULL; |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 444 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 445 | EXPORT_SYMBOL_GPL(inet_csk_route_req); |
| 446 | |
David S. Miller | 77357a9 | 2011-05-08 14:34:22 -0700 | [diff] [blame] | 447 | struct dst_entry *inet_csk_route_child_sock(struct sock *sk, |
| 448 | struct sock *newsk, |
| 449 | const struct request_sock *req) |
| 450 | { |
| 451 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 452 | struct inet_sock *newinet = inet_sk(newsk); |
Christoph Paasch | 1a7b27c | 2012-08-20 02:52:09 +0000 | [diff] [blame] | 453 | struct ip_options_rcu *opt; |
David S. Miller | 77357a9 | 2011-05-08 14:34:22 -0700 | [diff] [blame] | 454 | struct net *net = sock_net(sk); |
| 455 | struct flowi4 *fl4; |
| 456 | struct rtable *rt; |
| 457 | |
| 458 | fl4 = &newinet->cork.fl.u.ip4; |
Christoph Paasch | 1a7b27c | 2012-08-20 02:52:09 +0000 | [diff] [blame] | 459 | |
| 460 | rcu_read_lock(); |
| 461 | opt = rcu_dereference(newinet->inet_opt); |
David S. Miller | 77357a9 | 2011-05-08 14:34:22 -0700 | [diff] [blame] | 462 | flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, |
| 463 | RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, |
| 464 | sk->sk_protocol, inet_sk_flowi_flags(sk), |
| 465 | (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr, |
| 466 | ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport); |
| 467 | security_req_classify_flow(req, flowi4_to_flowi(fl4)); |
| 468 | rt = ip_route_output_flow(net, fl4, sk); |
| 469 | if (IS_ERR(rt)) |
| 470 | goto no_route; |
Julian Anastasov | 155e833 | 2012-10-08 11:41:18 +0000 | [diff] [blame] | 471 | if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway) |
David S. Miller | 77357a9 | 2011-05-08 14:34:22 -0700 | [diff] [blame] | 472 | goto route_err; |
Christoph Paasch | 1a7b27c | 2012-08-20 02:52:09 +0000 | [diff] [blame] | 473 | rcu_read_unlock(); |
David S. Miller | 77357a9 | 2011-05-08 14:34:22 -0700 | [diff] [blame] | 474 | return &rt->dst; |
| 475 | |
| 476 | route_err: |
| 477 | ip_rt_put(rt); |
| 478 | no_route: |
Christoph Paasch | 1a7b27c | 2012-08-20 02:52:09 +0000 | [diff] [blame] | 479 | rcu_read_unlock(); |
David S. Miller | 77357a9 | 2011-05-08 14:34:22 -0700 | [diff] [blame] | 480 | IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); |
| 481 | return NULL; |
| 482 | } |
| 483 | EXPORT_SYMBOL_GPL(inet_csk_route_child_sock); |
| 484 | |
Al Viro | 6b72977 | 2006-09-27 18:36:59 -0700 | [diff] [blame] | 485 | static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport, |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 486 | const u32 rnd, const u32 synq_hsize) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 487 | { |
Al Viro | 6b72977 | 2006-09-27 18:36:59 -0700 | [diff] [blame] | 488 | 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] | 489 | } |
| 490 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 491 | #if IS_ENABLED(CONFIG_IPV6) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 492 | #define AF_INET_FAMILY(fam) ((fam) == AF_INET) |
| 493 | #else |
| 494 | #define AF_INET_FAMILY(fam) 1 |
| 495 | #endif |
| 496 | |
| 497 | struct request_sock *inet_csk_search_req(const struct sock *sk, |
| 498 | struct request_sock ***prevp, |
Al Viro | 6b72977 | 2006-09-27 18:36:59 -0700 | [diff] [blame] | 499 | const __be16 rport, const __be32 raddr, |
Al Viro | 7f25afb | 2006-09-27 18:27:47 -0700 | [diff] [blame] | 500 | const __be32 laddr) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 501 | { |
| 502 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 503 | struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; |
| 504 | struct request_sock *req, **prev; |
| 505 | |
| 506 | for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd, |
| 507 | lopt->nr_table_entries)]; |
| 508 | (req = *prev) != NULL; |
| 509 | prev = &req->dl_next) { |
| 510 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 511 | |
| 512 | if (ireq->rmt_port == rport && |
| 513 | ireq->rmt_addr == raddr && |
| 514 | ireq->loc_addr == laddr && |
| 515 | AF_INET_FAMILY(req->rsk_ops->family)) { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 516 | WARN_ON(req->sk); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 517 | *prevp = prev; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return req; |
| 523 | } |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 524 | EXPORT_SYMBOL_GPL(inet_csk_search_req); |
| 525 | |
| 526 | 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] | 527 | unsigned long timeout) |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 528 | { |
| 529 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 530 | struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; |
| 531 | const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port, |
| 532 | lopt->hash_rnd, lopt->nr_table_entries); |
| 533 | |
| 534 | reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout); |
| 535 | inet_csk_reqsk_queue_added(sk, timeout); |
| 536 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 537 | EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add); |
Arnaldo Carvalho de Melo | 3f421ba | 2005-08-09 20:11:08 -0700 | [diff] [blame] | 538 | |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 539 | /* Only thing we need from tcp.h */ |
| 540 | extern int sysctl_tcp_synack_retries; |
| 541 | |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 542 | |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 543 | /* Decide when to expire the request and when to resend SYN-ACK */ |
| 544 | static inline void syn_ack_recalc(struct request_sock *req, const int thresh, |
| 545 | const int max_retries, |
| 546 | const u8 rskq_defer_accept, |
| 547 | int *expire, int *resend) |
| 548 | { |
| 549 | if (!rskq_defer_accept) { |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 550 | *expire = req->num_timeout >= thresh; |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 551 | *resend = 1; |
| 552 | return; |
| 553 | } |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 554 | *expire = req->num_timeout >= thresh && |
| 555 | (!inet_rsk(req)->acked || req->num_timeout >= max_retries); |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Do not resend while waiting for data after ACK, |
| 558 | * start to resend on end of deferring period to give |
| 559 | * last chance for data or ACK to create established socket. |
| 560 | */ |
| 561 | *resend = !inet_rsk(req)->acked || |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 562 | req->num_timeout >= rskq_defer_accept - 1; |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 565 | int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req) |
| 566 | { |
| 567 | int err = req->rsk_ops->rtx_syn_ack(parent, req, NULL); |
| 568 | |
| 569 | if (!err) |
| 570 | req->num_retrans++; |
| 571 | return err; |
| 572 | } |
| 573 | EXPORT_SYMBOL(inet_rtx_syn_ack); |
| 574 | |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 575 | void inet_csk_reqsk_queue_prune(struct sock *parent, |
| 576 | const unsigned long interval, |
| 577 | const unsigned long timeout, |
| 578 | const unsigned long max_rto) |
| 579 | { |
| 580 | struct inet_connection_sock *icsk = inet_csk(parent); |
| 581 | struct request_sock_queue *queue = &icsk->icsk_accept_queue; |
| 582 | struct listen_sock *lopt = queue->listen_opt; |
David S. Miller | ec0a196 | 2008-06-12 16:31:35 -0700 | [diff] [blame] | 583 | int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries; |
| 584 | int thresh = max_retries; |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 585 | unsigned long now = jiffies; |
| 586 | struct request_sock **reqp, *req; |
| 587 | int i, budget; |
| 588 | |
| 589 | if (lopt == NULL || lopt->qlen == 0) |
| 590 | return; |
| 591 | |
| 592 | /* Normally all the openreqs are young and become mature |
| 593 | * (i.e. converted to established socket) for first timeout. |
Eric Dumazet | fd4f2ce | 2012-04-12 19:48:40 +0000 | [diff] [blame] | 594 | * If synack was not acknowledged for 1 second, it means |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 595 | * one of the following things: synack was lost, ack was lost, |
| 596 | * rtt is high or nobody planned to ack (i.e. synflood). |
| 597 | * When server is a bit loaded, queue is populated with old |
| 598 | * open requests, reducing effective size of queue. |
| 599 | * When server is well loaded, queue size reduces to zero |
| 600 | * after several minutes of work. It is not synflood, |
| 601 | * it is normal operation. The solution is pruning |
| 602 | * too old entries overriding normal timeout, when |
| 603 | * situation becomes dangerous. |
| 604 | * |
| 605 | * Essentially, we reserve half of room for young |
| 606 | * embrions; and abort old ones without pity, if old |
| 607 | * ones are about to clog our table. |
| 608 | */ |
| 609 | if (lopt->qlen>>(lopt->max_qlen_log-1)) { |
| 610 | int young = (lopt->qlen_young<<1); |
| 611 | |
| 612 | while (thresh > 2) { |
| 613 | if (lopt->qlen < young) |
| 614 | break; |
| 615 | thresh--; |
| 616 | young <<= 1; |
| 617 | } |
| 618 | } |
| 619 | |
David S. Miller | ec0a196 | 2008-06-12 16:31:35 -0700 | [diff] [blame] | 620 | if (queue->rskq_defer_accept) |
| 621 | max_retries = queue->rskq_defer_accept; |
| 622 | |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 623 | budget = 2 * (lopt->nr_table_entries / (timeout / interval)); |
| 624 | i = lopt->clock_hand; |
| 625 | |
| 626 | do { |
| 627 | reqp=&lopt->syn_table[i]; |
| 628 | while ((req = *reqp) != NULL) { |
| 629 | if (time_after_eq(now, req->expires)) { |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 630 | int expire = 0, resend = 0; |
| 631 | |
| 632 | syn_ack_recalc(req, thresh, max_retries, |
| 633 | queue->rskq_defer_accept, |
| 634 | &expire, &resend); |
Eric Dumazet | c72e118 | 2012-04-12 22:16:05 +0000 | [diff] [blame] | 635 | req->rsk_ops->syn_ack_timeout(parent, req); |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 636 | if (!expire && |
| 637 | (!resend || |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 638 | !inet_rtx_syn_ack(parent, req) || |
Julian Anastasov | 0c3d79b | 2009-10-19 10:03:58 +0000 | [diff] [blame] | 639 | inet_rsk(req)->acked)) { |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 640 | unsigned long timeo; |
| 641 | |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 642 | if (req->num_timeout++ == 0) |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 643 | lopt->qlen_young--; |
Eric Dumazet | e6c022a | 2012-10-27 23:16:46 +0000 | [diff] [blame] | 644 | timeo = min(timeout << req->num_timeout, |
| 645 | max_rto); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 646 | req->expires = now + timeo; |
| 647 | reqp = &req->dl_next; |
| 648 | continue; |
| 649 | } |
| 650 | |
| 651 | /* Drop this request */ |
| 652 | inet_csk_reqsk_queue_unlink(parent, req, reqp); |
| 653 | reqsk_queue_removed(queue, req); |
| 654 | reqsk_free(req); |
| 655 | continue; |
| 656 | } |
| 657 | reqp = &req->dl_next; |
| 658 | } |
| 659 | |
| 660 | i = (i + 1) & (lopt->nr_table_entries - 1); |
| 661 | |
| 662 | } while (--budget > 0); |
| 663 | |
| 664 | lopt->clock_hand = i; |
| 665 | |
| 666 | if (lopt->qlen) |
| 667 | inet_csk_reset_keepalive_timer(parent, interval); |
| 668 | } |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 669 | EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune); |
| 670 | |
Eric Dumazet | e56c57d | 2011-11-08 17:07:07 -0500 | [diff] [blame] | 671 | /** |
| 672 | * inet_csk_clone_lock - clone an inet socket, and lock its clone |
| 673 | * @sk: the socket to clone |
| 674 | * @req: request_sock |
| 675 | * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) |
| 676 | * |
| 677 | * Caller must unlock socket even in error path (bh_unlock_sock(newsk)) |
| 678 | */ |
| 679 | struct sock *inet_csk_clone_lock(const struct sock *sk, |
| 680 | const struct request_sock *req, |
| 681 | const gfp_t priority) |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 682 | { |
Eric Dumazet | e56c57d | 2011-11-08 17:07:07 -0500 | [diff] [blame] | 683 | struct sock *newsk = sk_clone_lock(sk, priority); |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 684 | |
| 685 | if (newsk != NULL) { |
| 686 | struct inet_connection_sock *newicsk = inet_csk(newsk); |
| 687 | |
| 688 | newsk->sk_state = TCP_SYN_RECV; |
| 689 | newicsk->icsk_bind_hash = NULL; |
| 690 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 691 | inet_sk(newsk)->inet_dport = inet_rsk(req)->rmt_port; |
| 692 | inet_sk(newsk)->inet_num = ntohs(inet_rsk(req)->loc_port); |
| 693 | inet_sk(newsk)->inet_sport = inet_rsk(req)->loc_port; |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 694 | newsk->sk_write_space = sk_stream_write_space; |
| 695 | |
| 696 | newicsk->icsk_retransmits = 0; |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 697 | newicsk->icsk_backoff = 0; |
| 698 | newicsk->icsk_probes_out = 0; |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 699 | |
| 700 | /* Deinitialize accept_queue to trap illegal accesses. */ |
| 701 | memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue)); |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 702 | |
| 703 | security_inet_csk_clone(newsk, req); |
Arnaldo Carvalho de Melo | 9f1d260 | 2005-08-09 20:11:24 -0700 | [diff] [blame] | 704 | } |
| 705 | return newsk; |
| 706 | } |
Eric Dumazet | e56c57d | 2011-11-08 17:07:07 -0500 | [diff] [blame] | 707 | EXPORT_SYMBOL_GPL(inet_csk_clone_lock); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 708 | |
| 709 | /* |
| 710 | * At this point, there should be no process reference to this |
| 711 | * socket, and thus no user references at all. Therefore we |
| 712 | * can assume the socket waitqueue is inactive and nobody will |
| 713 | * try to jump onto it. |
| 714 | */ |
| 715 | void inet_csk_destroy_sock(struct sock *sk) |
| 716 | { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 717 | WARN_ON(sk->sk_state != TCP_CLOSE); |
| 718 | WARN_ON(!sock_flag(sk, SOCK_DEAD)); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 719 | |
| 720 | /* It cannot be in hash table! */ |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 721 | WARN_ON(!sk_unhashed(sk)); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 722 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 723 | /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */ |
| 724 | WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 725 | |
| 726 | sk->sk_prot->destroy(sk); |
| 727 | |
| 728 | sk_stream_kill_queues(sk); |
| 729 | |
| 730 | xfrm_sk_free_policy(sk); |
| 731 | |
| 732 | sk_refcnt_debug_release(sk); |
| 733 | |
Eric Dumazet | dd24c00 | 2008-11-25 21:17:14 -0800 | [diff] [blame] | 734 | percpu_counter_dec(sk->sk_prot->orphan_count); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 735 | sock_put(sk); |
| 736 | } |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 737 | EXPORT_SYMBOL(inet_csk_destroy_sock); |
| 738 | |
Christoph Paasch | e337e24 | 2012-12-14 04:07:58 +0000 | [diff] [blame] | 739 | /* This function allows to force a closure of a socket after the call to |
| 740 | * tcp/dccp_create_openreq_child(). |
| 741 | */ |
| 742 | void inet_csk_prepare_forced_close(struct sock *sk) |
| 743 | { |
| 744 | /* sk_clone_lock locked the socket and set refcnt to 2 */ |
| 745 | bh_unlock_sock(sk); |
| 746 | sock_put(sk); |
| 747 | |
| 748 | /* The below has to be done to allow calling inet_csk_destroy_sock */ |
| 749 | sock_set_flag(sk, SOCK_DEAD); |
| 750 | percpu_counter_inc(sk->sk_prot->orphan_count); |
| 751 | inet_sk(sk)->inet_num = 0; |
| 752 | } |
| 753 | EXPORT_SYMBOL(inet_csk_prepare_forced_close); |
| 754 | |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 755 | int inet_csk_listen_start(struct sock *sk, const int nr_table_entries) |
| 756 | { |
| 757 | struct inet_sock *inet = inet_sk(sk); |
| 758 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 759 | int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries); |
| 760 | |
| 761 | if (rc != 0) |
| 762 | return rc; |
| 763 | |
| 764 | sk->sk_max_ack_backlog = 0; |
| 765 | sk->sk_ack_backlog = 0; |
| 766 | inet_csk_delack_init(sk); |
| 767 | |
| 768 | /* There is race window here: we announce ourselves listening, |
| 769 | * but this transition is still not validated by get_port(). |
| 770 | * It is OK, because this socket enters to hash table only |
| 771 | * after validation is complete. |
| 772 | */ |
| 773 | sk->sk_state = TCP_LISTEN; |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 774 | if (!sk->sk_prot->get_port(sk, inet->inet_num)) { |
| 775 | inet->inet_sport = htons(inet->inet_num); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 776 | |
| 777 | sk_dst_reset(sk); |
| 778 | sk->sk_prot->hash(sk); |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | sk->sk_state = TCP_CLOSE; |
| 784 | __reqsk_queue_destroy(&icsk->icsk_accept_queue); |
| 785 | return -EADDRINUSE; |
| 786 | } |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(inet_csk_listen_start); |
| 788 | |
| 789 | /* |
| 790 | * This routine closes sockets which have been at least partially |
| 791 | * opened, but not yet accepted. |
| 792 | */ |
| 793 | void inet_csk_listen_stop(struct sock *sk) |
| 794 | { |
| 795 | struct inet_connection_sock *icsk = inet_csk(sk); |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 796 | struct request_sock_queue *queue = &icsk->icsk_accept_queue; |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 797 | struct request_sock *acc_req; |
| 798 | struct request_sock *req; |
| 799 | |
| 800 | inet_csk_delete_keepalive_timer(sk); |
| 801 | |
| 802 | /* make all the listen_opt local to us */ |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 803 | acc_req = reqsk_queue_yank_acceptq(queue); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 804 | |
| 805 | /* Following specs, it would be better either to send FIN |
| 806 | * (and enter FIN-WAIT-1, it is normal close) |
| 807 | * or to send active reset (abort). |
| 808 | * Certainly, it is pretty dangerous while synflood, but it is |
| 809 | * bad justification for our negligence 8) |
| 810 | * To be honest, we are not able to make either |
| 811 | * of the variants now. --ANK |
| 812 | */ |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 813 | reqsk_queue_destroy(queue); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 814 | |
| 815 | while ((req = acc_req) != NULL) { |
| 816 | struct sock *child = req->sk; |
| 817 | |
| 818 | acc_req = req->dl_next; |
| 819 | |
| 820 | local_bh_disable(); |
| 821 | bh_lock_sock(child); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 822 | WARN_ON(sock_owned_by_user(child)); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 823 | sock_hold(child); |
| 824 | |
| 825 | sk->sk_prot->disconnect(child, O_NONBLOCK); |
| 826 | |
| 827 | sock_orphan(child); |
| 828 | |
Herbert Xu | eb4dea5 | 2008-12-29 23:04:08 -0800 | [diff] [blame] | 829 | percpu_counter_inc(sk->sk_prot->orphan_count); |
| 830 | |
Eric Dumazet | 7ab4551 | 2012-09-06 08:07:13 +0000 | [diff] [blame] | 831 | if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->listener) { |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 832 | BUG_ON(tcp_sk(child)->fastopen_rsk != req); |
| 833 | BUG_ON(sk != tcp_rsk(req)->listener); |
| 834 | |
| 835 | /* Paranoid, to prevent race condition if |
| 836 | * an inbound pkt destined for child is |
| 837 | * blocked by sock lock in tcp_v4_rcv(). |
| 838 | * Also to satisfy an assertion in |
| 839 | * tcp_v4_destroy_sock(). |
| 840 | */ |
| 841 | tcp_sk(child)->fastopen_rsk = NULL; |
| 842 | sock_put(sk); |
| 843 | } |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 844 | inet_csk_destroy_sock(child); |
| 845 | |
| 846 | bh_unlock_sock(child); |
| 847 | local_bh_enable(); |
| 848 | sock_put(child); |
| 849 | |
| 850 | sk_acceptq_removed(sk); |
| 851 | __reqsk_free(req); |
| 852 | } |
Jerry Chu | 8336886 | 2012-08-31 12:29:12 +0000 | [diff] [blame] | 853 | if (queue->fastopenq != NULL) { |
| 854 | /* Free all the reqs queued in rskq_rst_head. */ |
| 855 | spin_lock_bh(&queue->fastopenq->lock); |
| 856 | acc_req = queue->fastopenq->rskq_rst_head; |
| 857 | queue->fastopenq->rskq_rst_head = NULL; |
| 858 | spin_unlock_bh(&queue->fastopenq->lock); |
| 859 | while ((req = acc_req) != NULL) { |
| 860 | acc_req = req->dl_next; |
| 861 | __reqsk_free(req); |
| 862 | } |
| 863 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 864 | WARN_ON(sk->sk_ack_backlog); |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 865 | } |
Arnaldo Carvalho de Melo | a019d6f | 2005-08-09 20:15:09 -0700 | [diff] [blame] | 866 | EXPORT_SYMBOL_GPL(inet_csk_listen_stop); |
Arnaldo Carvalho de Melo | af05dc9 | 2005-12-13 23:16:04 -0800 | [diff] [blame] | 867 | |
| 868 | void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr) |
| 869 | { |
| 870 | struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; |
| 871 | const struct inet_sock *inet = inet_sk(sk); |
| 872 | |
| 873 | sin->sin_family = AF_INET; |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 874 | sin->sin_addr.s_addr = inet->inet_daddr; |
| 875 | sin->sin_port = inet->inet_dport; |
Arnaldo Carvalho de Melo | af05dc9 | 2005-12-13 23:16:04 -0800 | [diff] [blame] | 876 | } |
Arnaldo Carvalho de Melo | af05dc9 | 2005-12-13 23:16:04 -0800 | [diff] [blame] | 877 | EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr); |
Arnaldo Carvalho de Melo | c4d9390 | 2006-03-20 22:01:03 -0800 | [diff] [blame] | 878 | |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 879 | #ifdef CONFIG_COMPAT |
| 880 | int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname, |
| 881 | char __user *optval, int __user *optlen) |
| 882 | { |
David S. Miller | dbeff12 | 2006-03-20 22:52:32 -0800 | [diff] [blame] | 883 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 884 | |
| 885 | if (icsk->icsk_af_ops->compat_getsockopt != NULL) |
| 886 | return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname, |
| 887 | optval, optlen); |
| 888 | return icsk->icsk_af_ops->getsockopt(sk, level, optname, |
| 889 | optval, optlen); |
| 890 | } |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 891 | EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt); |
| 892 | |
| 893 | int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 894 | char __user *optval, unsigned int optlen) |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 895 | { |
David S. Miller | dbeff12 | 2006-03-20 22:52:32 -0800 | [diff] [blame] | 896 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 897 | |
| 898 | if (icsk->icsk_af_ops->compat_setsockopt != NULL) |
| 899 | return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname, |
| 900 | optval, optlen); |
| 901 | return icsk->icsk_af_ops->setsockopt(sk, level, optname, |
| 902 | optval, optlen); |
| 903 | } |
Arnaldo Carvalho de Melo | dec73ff | 2006-03-20 22:46:16 -0800 | [diff] [blame] | 904 | EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt); |
| 905 | #endif |
David S. Miller | 80d0a69 | 2012-07-16 03:28:06 -0700 | [diff] [blame] | 906 | |
| 907 | static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl) |
| 908 | { |
Eric Dumazet | 5abf7f7 | 2012-07-17 22:42:13 +0200 | [diff] [blame] | 909 | const struct inet_sock *inet = inet_sk(sk); |
| 910 | const struct ip_options_rcu *inet_opt; |
David S. Miller | 80d0a69 | 2012-07-16 03:28:06 -0700 | [diff] [blame] | 911 | __be32 daddr = inet->inet_daddr; |
| 912 | struct flowi4 *fl4; |
| 913 | struct rtable *rt; |
| 914 | |
| 915 | rcu_read_lock(); |
| 916 | inet_opt = rcu_dereference(inet->inet_opt); |
| 917 | if (inet_opt && inet_opt->opt.srr) |
| 918 | daddr = inet_opt->opt.faddr; |
| 919 | fl4 = &fl->u.ip4; |
| 920 | rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, |
| 921 | inet->inet_saddr, inet->inet_dport, |
| 922 | inet->inet_sport, sk->sk_protocol, |
| 923 | RT_CONN_FLAGS(sk), sk->sk_bound_dev_if); |
| 924 | if (IS_ERR(rt)) |
| 925 | rt = NULL; |
| 926 | if (rt) |
| 927 | sk_setup_caps(sk, &rt->dst); |
| 928 | rcu_read_unlock(); |
| 929 | |
| 930 | return &rt->dst; |
| 931 | } |
| 932 | |
| 933 | struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu) |
| 934 | { |
| 935 | struct dst_entry *dst = __sk_dst_check(sk, 0); |
| 936 | struct inet_sock *inet = inet_sk(sk); |
| 937 | |
| 938 | if (!dst) { |
| 939 | dst = inet_csk_rebuild_route(sk, &inet->cork.fl); |
| 940 | if (!dst) |
| 941 | goto out; |
| 942 | } |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 943 | dst->ops->update_pmtu(dst, sk, NULL, mtu); |
David S. Miller | 80d0a69 | 2012-07-16 03:28:06 -0700 | [diff] [blame] | 944 | |
| 945 | dst = __sk_dst_check(sk, 0); |
| 946 | if (!dst) |
| 947 | dst = inet_csk_rebuild_route(sk, &inet->cork.fl); |
| 948 | out: |
| 949 | return dst; |
| 950 | } |
| 951 | EXPORT_SYMBOL_GPL(inet_csk_update_pmtu); |