|
23 | 23 |
|
24 | 24 | #include <net/inet_connection_sock.h> |
25 | 25 | #include <net/inet_sock.h> |
| 26 | +#include <net/ip.h> |
26 | 27 | #include <net/sock.h> |
27 | 28 | #include <net/route.h> |
28 | 29 | #include <net/tcp_states.h> |
@@ -90,7 +91,28 @@ struct inet_bind_bucket { |
90 | 91 | struct hlist_head owners; |
91 | 92 | }; |
92 | 93 |
|
93 | | -static inline struct net *ib_net(struct inet_bind_bucket *ib) |
| 94 | +struct inet_bind2_bucket { |
| 95 | + possible_net_t ib_net; |
| 96 | + int l3mdev; |
| 97 | + unsigned short port; |
| 98 | + union { |
| 99 | +#if IS_ENABLED(CONFIG_IPV6) |
| 100 | + struct in6_addr v6_rcv_saddr; |
| 101 | +#endif |
| 102 | + __be32 rcv_saddr; |
| 103 | + }; |
| 104 | + /* Node in the bhash2 inet_bind_hashbucket chain */ |
| 105 | + struct hlist_node node; |
| 106 | + /* List of sockets hashed to this bucket */ |
| 107 | + struct hlist_head owners; |
| 108 | +}; |
| 109 | + |
| 110 | +static inline struct net *ib_net(const struct inet_bind_bucket *ib) |
| 111 | +{ |
| 112 | + return read_pnet(&ib->ib_net); |
| 113 | +} |
| 114 | + |
| 115 | +static inline struct net *ib2_net(const struct inet_bind2_bucket *ib) |
94 | 116 | { |
95 | 117 | return read_pnet(&ib->ib_net); |
96 | 118 | } |
@@ -133,7 +155,14 @@ struct inet_hashinfo { |
133 | 155 | * TCP hash as well as the others for fast bind/connect. |
134 | 156 | */ |
135 | 157 | struct kmem_cache *bind_bucket_cachep; |
| 158 | + /* This bind table is hashed by local port */ |
136 | 159 | struct inet_bind_hashbucket *bhash; |
| 160 | + struct kmem_cache *bind2_bucket_cachep; |
| 161 | + /* This bind table is hashed by local port and sk->sk_rcv_saddr (ipv4) |
| 162 | + * or sk->sk_v6_rcv_saddr (ipv6). This 2nd bind table is used |
| 163 | + * primarily for expediting bind conflict resolution. |
| 164 | + */ |
| 165 | + struct inet_bind_hashbucket *bhash2; |
137 | 166 | unsigned int bhash_size; |
138 | 167 |
|
139 | 168 | /* The 2nd listener table hashed by local port and address */ |
@@ -182,14 +211,61 @@ inet_bind_bucket_create(struct kmem_cache *cachep, struct net *net, |
182 | 211 | void inet_bind_bucket_destroy(struct kmem_cache *cachep, |
183 | 212 | struct inet_bind_bucket *tb); |
184 | 213 |
|
| 214 | +bool inet_bind_bucket_match(const struct inet_bind_bucket *tb, |
| 215 | + const struct net *net, unsigned short port, |
| 216 | + int l3mdev); |
| 217 | + |
| 218 | +struct inet_bind2_bucket * |
| 219 | +inet_bind2_bucket_create(struct kmem_cache *cachep, struct net *net, |
| 220 | + struct inet_bind_hashbucket *head, |
| 221 | + unsigned short port, int l3mdev, |
| 222 | + const struct sock *sk); |
| 223 | + |
| 224 | +void inet_bind2_bucket_destroy(struct kmem_cache *cachep, |
| 225 | + struct inet_bind2_bucket *tb); |
| 226 | + |
| 227 | +struct inet_bind2_bucket * |
| 228 | +inet_bind2_bucket_find(const struct inet_bind_hashbucket *head, |
| 229 | + const struct net *net, |
| 230 | + unsigned short port, int l3mdev, |
| 231 | + const struct sock *sk); |
| 232 | + |
| 233 | +bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, |
| 234 | + const struct net *net, unsigned short port, |
| 235 | + int l3mdev, const struct sock *sk); |
| 236 | + |
185 | 237 | static inline u32 inet_bhashfn(const struct net *net, const __u16 lport, |
186 | 238 | const u32 bhash_size) |
187 | 239 | { |
188 | 240 | return (lport + net_hash_mix(net)) & (bhash_size - 1); |
189 | 241 | } |
190 | 242 |
|
| 243 | +static inline struct inet_bind_hashbucket * |
| 244 | +inet_bhashfn_portaddr(const struct inet_hashinfo *hinfo, const struct sock *sk, |
| 245 | + const struct net *net, unsigned short port) |
| 246 | +{ |
| 247 | + u32 hash; |
| 248 | + |
| 249 | +#if IS_ENABLED(CONFIG_IPV6) |
| 250 | + if (sk->sk_family == AF_INET6) |
| 251 | + hash = ipv6_portaddr_hash(net, &sk->sk_v6_rcv_saddr, port); |
| 252 | + else |
| 253 | +#endif |
| 254 | + hash = ipv4_portaddr_hash(net, sk->sk_rcv_saddr, port); |
| 255 | + return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)]; |
| 256 | +} |
| 257 | + |
| 258 | +struct inet_bind_hashbucket * |
| 259 | +inet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port); |
| 260 | + |
| 261 | +/* This should be called whenever a socket's sk_rcv_saddr (ipv4) or |
| 262 | + * sk_v6_rcv_saddr (ipv6) changes after it has been binded. The socket's |
| 263 | + * rcv_saddr field should already have been updated when this is called. |
| 264 | + */ |
| 265 | +int inet_bhash2_update_saddr(struct inet_bind_hashbucket *prev_saddr, struct sock *sk); |
| 266 | + |
191 | 267 | void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb, |
192 | | - const unsigned short snum); |
| 268 | + struct inet_bind2_bucket *tb2, unsigned short port); |
193 | 269 |
|
194 | 270 | /* Caller must disable local BH processing. */ |
195 | 271 | int __inet_inherit_port(const struct sock *sk, struct sock *child); |
|
0 commit comments