Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [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 | * Authors: Lotsa people, from code originally in tcp |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #ifndef _INET_HASHTABLES_H |
| 15 | #define _INET_HASHTABLES_H |
| 16 | |
| 17 | |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/ip.h> |
| 20 | #include <linux/ipv6.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/socket.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/wait.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | |
| 29 | #include <net/inet_connection_sock.h> |
| 30 | #include <net/inet_sock.h> |
| 31 | #include <net/sock.h> |
| 32 | #include <net/route.h> |
| 33 | #include <net/tcp_states.h> |
| 34 | #include <net/netns/hash.h> |
| 35 | |
| 36 | #include <linux/atomic.h> |
| 37 | #include <asm/byteorder.h> |
| 38 | |
| 39 | struct inet_ehash_bucket { |
| 40 | struct hlist_nulls_head chain; |
| 41 | struct hlist_nulls_head twchain; |
| 42 | }; |
| 43 | |
| 44 | struct inet_bind_bucket { |
| 45 | #ifdef CONFIG_NET_NS |
| 46 | struct net *ib_net; |
| 47 | #endif |
| 48 | unsigned short port; |
| 49 | signed short fastreuse; |
| 50 | int num_owners; |
| 51 | struct hlist_node node; |
| 52 | struct hlist_head owners; |
| 53 | }; |
| 54 | |
| 55 | static inline struct net *ib_net(struct inet_bind_bucket *ib) |
| 56 | { |
| 57 | return read_pnet(&ib->ib_net); |
| 58 | } |
| 59 | |
| 60 | #define inet_bind_bucket_for_each(tb, pos, head) \ |
| 61 | hlist_for_each_entry(tb, pos, head, node) |
| 62 | |
| 63 | struct inet_bind_hashbucket { |
| 64 | spinlock_t lock; |
| 65 | struct hlist_head chain; |
| 66 | }; |
| 67 | |
| 68 | #define LISTENING_NULLS_BASE (1U << 29) |
| 69 | struct inet_listen_hashbucket { |
| 70 | spinlock_t lock; |
| 71 | struct hlist_nulls_head head; |
| 72 | }; |
| 73 | |
| 74 | #define INET_LHTABLE_SIZE 32 |
| 75 | |
| 76 | struct inet_hashinfo { |
| 77 | struct inet_ehash_bucket *ehash; |
| 78 | spinlock_t *ehash_locks; |
| 79 | unsigned int ehash_mask; |
| 80 | unsigned int ehash_locks_mask; |
| 81 | |
| 82 | struct inet_bind_hashbucket *bhash; |
| 83 | |
| 84 | unsigned int bhash_size; |
| 85 | |
| 86 | |
| 87 | struct kmem_cache *bind_bucket_cachep; |
| 88 | |
| 89 | /* All the above members are written once at bootup and |
| 90 | * never written again _or_ are predominantly read-access. |
| 91 | * |
| 92 | * Now align to a new cache line as all the following members |
| 93 | * might be often dirty. |
| 94 | */ |
| 95 | struct inet_listen_hashbucket listening_hash[INET_LHTABLE_SIZE] |
| 96 | ____cacheline_aligned_in_smp; |
| 97 | |
| 98 | atomic_t bsockets; |
| 99 | }; |
| 100 | |
| 101 | static inline struct inet_ehash_bucket *inet_ehash_bucket( |
| 102 | struct inet_hashinfo *hashinfo, |
| 103 | unsigned int hash) |
| 104 | { |
| 105 | return &hashinfo->ehash[hash & hashinfo->ehash_mask]; |
| 106 | } |
| 107 | |
| 108 | static inline spinlock_t *inet_ehash_lockp( |
| 109 | struct inet_hashinfo *hashinfo, |
| 110 | unsigned int hash) |
| 111 | { |
| 112 | return &hashinfo->ehash_locks[hash & hashinfo->ehash_locks_mask]; |
| 113 | } |
| 114 | |
| 115 | static inline int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo) |
| 116 | { |
| 117 | unsigned int i, size = 256; |
| 118 | #if defined(CONFIG_PROVE_LOCKING) |
| 119 | unsigned int nr_pcpus = 2; |
| 120 | #else |
| 121 | unsigned int nr_pcpus = num_possible_cpus(); |
| 122 | #endif |
| 123 | if (nr_pcpus >= 4) |
| 124 | size = 512; |
| 125 | if (nr_pcpus >= 8) |
| 126 | size = 1024; |
| 127 | if (nr_pcpus >= 16) |
| 128 | size = 2048; |
| 129 | if (nr_pcpus >= 32) |
| 130 | size = 4096; |
| 131 | if (sizeof(spinlock_t) != 0) { |
| 132 | #ifdef CONFIG_NUMA |
| 133 | if (size * sizeof(spinlock_t) > PAGE_SIZE) |
| 134 | hashinfo->ehash_locks = vmalloc(size * sizeof(spinlock_t)); |
| 135 | else |
| 136 | #endif |
| 137 | hashinfo->ehash_locks = kmalloc(size * sizeof(spinlock_t), |
| 138 | GFP_KERNEL); |
| 139 | if (!hashinfo->ehash_locks) |
| 140 | return ENOMEM; |
| 141 | for (i = 0; i < size; i++) |
| 142 | spin_lock_init(&hashinfo->ehash_locks[i]); |
| 143 | } |
| 144 | hashinfo->ehash_locks_mask = size - 1; |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static inline void inet_ehash_locks_free(struct inet_hashinfo *hashinfo) |
| 149 | { |
| 150 | if (hashinfo->ehash_locks) { |
| 151 | #ifdef CONFIG_NUMA |
| 152 | unsigned int size = (hashinfo->ehash_locks_mask + 1) * |
| 153 | sizeof(spinlock_t); |
| 154 | if (size > PAGE_SIZE) |
| 155 | vfree(hashinfo->ehash_locks); |
| 156 | else |
| 157 | #endif |
| 158 | kfree(hashinfo->ehash_locks); |
| 159 | hashinfo->ehash_locks = NULL; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | extern struct inet_bind_bucket * |
| 164 | inet_bind_bucket_create(struct kmem_cache *cachep, |
| 165 | struct net *net, |
| 166 | struct inet_bind_hashbucket *head, |
| 167 | const unsigned short snum); |
| 168 | extern void inet_bind_bucket_destroy(struct kmem_cache *cachep, |
| 169 | struct inet_bind_bucket *tb); |
| 170 | |
| 171 | static inline int inet_bhashfn(struct net *net, |
| 172 | const __u16 lport, const int bhash_size) |
| 173 | { |
| 174 | return (lport + net_hash_mix(net)) & (bhash_size - 1); |
| 175 | } |
| 176 | |
| 177 | extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb, |
| 178 | const unsigned short snum); |
| 179 | |
| 180 | static inline int inet_lhashfn(struct net *net, const unsigned short num) |
| 181 | { |
| 182 | return (num + net_hash_mix(net)) & (INET_LHTABLE_SIZE - 1); |
| 183 | } |
| 184 | |
| 185 | static inline int inet_sk_listen_hashfn(const struct sock *sk) |
| 186 | { |
| 187 | return inet_lhashfn(sock_net(sk), inet_sk(sk)->inet_num); |
| 188 | } |
| 189 | |
| 190 | extern int __inet_inherit_port(struct sock *sk, struct sock *child); |
| 191 | |
| 192 | extern void inet_put_port(struct sock *sk); |
| 193 | |
| 194 | void inet_hashinfo_init(struct inet_hashinfo *h); |
| 195 | |
| 196 | extern int __inet_hash_nolisten(struct sock *sk, struct inet_timewait_sock *tw); |
| 197 | extern void inet_hash(struct sock *sk); |
| 198 | extern void inet_unhash(struct sock *sk); |
| 199 | |
| 200 | extern struct sock *__inet_lookup_listener(struct net *net, |
| 201 | struct inet_hashinfo *hashinfo, |
| 202 | const __be32 daddr, |
| 203 | const unsigned short hnum, |
| 204 | const int dif); |
| 205 | |
| 206 | static inline struct sock *inet_lookup_listener(struct net *net, |
| 207 | struct inet_hashinfo *hashinfo, |
| 208 | __be32 daddr, __be16 dport, int dif) |
| 209 | { |
| 210 | return __inet_lookup_listener(net, hashinfo, daddr, ntohs(dport), dif); |
| 211 | } |
| 212 | |
| 213 | typedef __u32 __bitwise __portpair; |
| 214 | #ifdef __BIG_ENDIAN |
| 215 | #define INET_COMBINED_PORTS(__sport, __dport) \ |
| 216 | ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport))) |
| 217 | #else |
| 218 | #define INET_COMBINED_PORTS(__sport, __dport) \ |
| 219 | ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport))) |
| 220 | #endif |
| 221 | |
| 222 | #if (BITS_PER_LONG == 64) |
| 223 | typedef __u64 __bitwise __addrpair; |
| 224 | #ifdef __BIG_ENDIAN |
| 225 | #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ |
| 226 | const __addrpair __name = (__force __addrpair) ( \ |
| 227 | (((__force __u64)(__be32)(__saddr)) << 32) | \ |
| 228 | ((__force __u64)(__be32)(__daddr))); |
| 229 | #else |
| 230 | #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \ |
| 231 | const __addrpair __name = (__force __addrpair) ( \ |
| 232 | (((__force __u64)(__be32)(__daddr)) << 32) | \ |
| 233 | ((__force __u64)(__be32)(__saddr))); |
| 234 | #endif |
| 235 | #define INET_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif)\ |
| 236 | (((__sk)->sk_hash == (__hash)) && net_eq(sock_net(__sk), (__net)) && \ |
| 237 | ((*((__addrpair *)&(inet_sk(__sk)->inet_daddr))) == (__cookie)) && \ |
| 238 | ((*((__portpair *)&(inet_sk(__sk)->inet_dport))) == (__ports)) && \ |
| 239 | (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif)))) |
| 240 | #define INET_TW_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif)\ |
| 241 | (((__sk)->sk_hash == (__hash)) && net_eq(sock_net(__sk), (__net)) && \ |
| 242 | ((*((__addrpair *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \ |
| 243 | ((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \ |
| 244 | (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif)))) |
| 245 | #else |
| 246 | #define INET_ADDR_COOKIE(__name, __saddr, __daddr) |
| 247 | #define INET_MATCH(__sk, __net, __hash, __cookie, __saddr, __daddr, __ports, __dif) \ |
| 248 | (((__sk)->sk_hash == (__hash)) && net_eq(sock_net(__sk), (__net)) && \ |
| 249 | (inet_sk(__sk)->inet_daddr == (__saddr)) && \ |
| 250 | (inet_sk(__sk)->inet_rcv_saddr == (__daddr)) && \ |
| 251 | ((*((__portpair *)&(inet_sk(__sk)->inet_dport))) == (__ports)) && \ |
| 252 | (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif)))) |
| 253 | #define INET_TW_MATCH(__sk, __net, __hash,__cookie, __saddr, __daddr, __ports, __dif) \ |
| 254 | (((__sk)->sk_hash == (__hash)) && net_eq(sock_net(__sk), (__net)) && \ |
| 255 | (inet_twsk(__sk)->tw_daddr == (__saddr)) && \ |
| 256 | (inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \ |
| 257 | ((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \ |
| 258 | (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif)))) |
| 259 | #endif |
| 260 | |
| 261 | extern struct sock * __inet_lookup_established(struct net *net, |
| 262 | struct inet_hashinfo *hashinfo, |
| 263 | const __be32 saddr, const __be16 sport, |
| 264 | const __be32 daddr, const u16 hnum, const int dif); |
| 265 | |
| 266 | static inline struct sock * |
| 267 | inet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo, |
| 268 | const __be32 saddr, const __be16 sport, |
| 269 | const __be32 daddr, const __be16 dport, |
| 270 | const int dif) |
| 271 | { |
| 272 | return __inet_lookup_established(net, hashinfo, saddr, sport, daddr, |
| 273 | ntohs(dport), dif); |
| 274 | } |
| 275 | |
| 276 | static inline struct sock *__inet_lookup(struct net *net, |
| 277 | struct inet_hashinfo *hashinfo, |
| 278 | const __be32 saddr, const __be16 sport, |
| 279 | const __be32 daddr, const __be16 dport, |
| 280 | const int dif) |
| 281 | { |
| 282 | u16 hnum = ntohs(dport); |
| 283 | struct sock *sk = __inet_lookup_established(net, hashinfo, |
| 284 | saddr, sport, daddr, hnum, dif); |
| 285 | |
| 286 | return sk ? : __inet_lookup_listener(net, hashinfo, daddr, hnum, dif); |
| 287 | } |
| 288 | |
| 289 | static inline struct sock *inet_lookup(struct net *net, |
| 290 | struct inet_hashinfo *hashinfo, |
| 291 | const __be32 saddr, const __be16 sport, |
| 292 | const __be32 daddr, const __be16 dport, |
| 293 | const int dif) |
| 294 | { |
| 295 | struct sock *sk; |
| 296 | |
| 297 | local_bh_disable(); |
| 298 | sk = __inet_lookup(net, hashinfo, saddr, sport, daddr, dport, dif); |
| 299 | local_bh_enable(); |
| 300 | |
| 301 | return sk; |
| 302 | } |
| 303 | |
| 304 | static inline struct sock *__inet_lookup_skb(struct inet_hashinfo *hashinfo, |
| 305 | struct sk_buff *skb, |
| 306 | const __be16 sport, |
| 307 | const __be16 dport) |
| 308 | { |
| 309 | struct sock *sk; |
| 310 | const struct iphdr *iph = ip_hdr(skb); |
| 311 | |
| 312 | if (unlikely(sk = skb_steal_sock(skb))) |
| 313 | return sk; |
| 314 | else |
| 315 | return __inet_lookup(dev_net(skb_dst(skb)->dev), hashinfo, |
| 316 | iph->saddr, sport, |
| 317 | iph->daddr, dport, inet_iif(skb)); |
| 318 | } |
| 319 | |
| 320 | extern int __inet_hash_connect(struct inet_timewait_death_row *death_row, |
| 321 | struct sock *sk, |
| 322 | u32 port_offset, |
| 323 | int (*check_established)(struct inet_timewait_death_row *, |
| 324 | struct sock *, __u16, struct inet_timewait_sock **), |
| 325 | int (*hash)(struct sock *sk, struct inet_timewait_sock *twp)); |
| 326 | |
| 327 | extern int inet_hash_connect(struct inet_timewait_death_row *death_row, |
| 328 | struct sock *sk); |
| 329 | #endif |