inet_hashtables.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #ifndef _INET_HASHTABLES_H
  14. #define _INET_HASHTABLES_H
  15. #include <linux/interrupt.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/socket.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/types.h>
  22. #include <linux/wait.h>
  23. #include <net/inet_connection_sock.h>
  24. #include <net/inet_sock.h>
  25. #include <net/route.h>
  26. #include <net/sock.h>
  27. #include <net/tcp_states.h>
  28. #include <asm/atomic.h>
  29. #include <asm/byteorder.h>
  30. /* This is for all connections with a full identity, no wildcards.
  31. * One chain is dedicated to TIME_WAIT sockets.
  32. * I'll experiment with dynamic table growth later.
  33. */
  34. struct inet_ehash_bucket {
  35. rwlock_t lock;
  36. struct hlist_head chain;
  37. struct hlist_head twchain;
  38. };
  39. /* There are a few simple rules, which allow for local port reuse by
  40. * an application. In essence:
  41. *
  42. * 1) Sockets bound to different interfaces may share a local port.
  43. * Failing that, goto test 2.
  44. * 2) If all sockets have sk->sk_reuse set, and none of them are in
  45. * TCP_LISTEN state, the port may be shared.
  46. * Failing that, goto test 3.
  47. * 3) If all sockets are bound to a specific inet_sk(sk)->rcv_saddr local
  48. * address, and none of them are the same, the port may be
  49. * shared.
  50. * Failing this, the port cannot be shared.
  51. *
  52. * The interesting point, is test #2. This is what an FTP server does
  53. * all day. To optimize this case we use a specific flag bit defined
  54. * below. As we add sockets to a bind bucket list, we perform a
  55. * check of: (newsk->sk_reuse && (newsk->sk_state != TCP_LISTEN))
  56. * As long as all sockets added to a bind bucket pass this test,
  57. * the flag bit will be set.
  58. * The resulting situation is that tcp_v[46]_verify_bind() can just check
  59. * for this flag bit, if it is set and the socket trying to bind has
  60. * sk->sk_reuse set, we don't even have to walk the owners list at all,
  61. * we return that it is ok to bind this socket to the requested local port.
  62. *
  63. * Sounds like a lot of work, but it is worth it. In a more naive
  64. * implementation (ie. current FreeBSD etc.) the entire list of ports
  65. * must be walked for each data port opened by an ftp server. Needless
  66. * to say, this does not scale at all. With a couple thousand FTP
  67. * users logged onto your box, isn't it nice to know that new data
  68. * ports are created in O(1) time? I thought so. ;-) -DaveM
  69. */
  70. struct inet_bind_bucket {
  71. unsigned short port;
  72. signed short fastreuse;
  73. struct hlist_node node;
  74. struct hlist_head owners;
  75. };
  76. #define inet_bind_bucket_for_each(tb, node, head) \
  77. hlist_for_each_entry(tb, node, head, node)
  78. struct inet_bind_hashbucket {
  79. spinlock_t lock;
  80. struct hlist_head chain;
  81. };
  82. /* This is for listening sockets, thus all sockets which possess wildcards. */
  83. #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
  84. struct inet_hashinfo {
  85. /* This is for sockets with full identity only. Sockets here will
  86. * always be without wildcards and will have the following invariant:
  87. *
  88. * TCP_ESTABLISHED <= sk->sk_state < TCP_CLOSE
  89. *
  90. * TIME_WAIT sockets use a separate chain (twchain).
  91. */
  92. struct inet_ehash_bucket *ehash;
  93. /* Ok, let's try this, I give up, we do need a local binding
  94. * TCP hash as well as the others for fast bind/connect.
  95. */
  96. struct inet_bind_hashbucket *bhash;
  97. unsigned int bhash_size;
  98. unsigned int ehash_size;
  99. /* All sockets in TCP_LISTEN state will be in here. This is the only
  100. * table where wildcard'd TCP sockets can exist. Hash function here
  101. * is just local port number.
  102. */
  103. struct hlist_head listening_hash[INET_LHTABLE_SIZE];
  104. /* All the above members are written once at bootup and
  105. * never written again _or_ are predominantly read-access.
  106. *
  107. * Now align to a new cache line as all the following members
  108. * are often dirty.
  109. */
  110. rwlock_t lhash_lock ____cacheline_aligned;
  111. atomic_t lhash_users;
  112. wait_queue_head_t lhash_wait;
  113. struct kmem_cache *bind_bucket_cachep;
  114. };
  115. static inline struct inet_ehash_bucket *inet_ehash_bucket(
  116. struct inet_hashinfo *hashinfo,
  117. unsigned int hash)
  118. {
  119. return &hashinfo->ehash[hash & (hashinfo->ehash_size - 1)];
  120. }
  121. extern struct inet_bind_bucket *
  122. inet_bind_bucket_create(struct kmem_cache *cachep,
  123. struct inet_bind_hashbucket *head,
  124. const unsigned short snum);
  125. extern void inet_bind_bucket_destroy(struct kmem_cache *cachep,
  126. struct inet_bind_bucket *tb);
  127. static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
  128. {
  129. return lport & (bhash_size - 1);
  130. }
  131. extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  132. const unsigned short snum);
  133. /* These can have wildcards, don't try too hard. */
  134. static inline int inet_lhashfn(const unsigned short num)
  135. {
  136. return num & (INET_LHTABLE_SIZE - 1);
  137. }
  138. static inline int inet_sk_listen_hashfn(const struct sock *sk)
  139. {
  140. return inet_lhashfn(inet_sk(sk)->num);
  141. }
  142. /* Caller must disable local BH processing. */
  143. static inline void __inet_inherit_port(struct inet_hashinfo *table,
  144. struct sock *sk, struct sock *child)
  145. {
  146. const int bhash = inet_bhashfn(inet_sk(child)->num, table->bhash_size);
  147. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  148. struct inet_bind_bucket *tb;
  149. spin_lock(&head->lock);
  150. tb = inet_csk(sk)->icsk_bind_hash;
  151. sk_add_bind_node(child, &tb->owners);
  152. inet_csk(child)->icsk_bind_hash = tb;
  153. spin_unlock(&head->lock);
  154. }
  155. static inline void inet_inherit_port(struct inet_hashinfo *table,
  156. struct sock *sk, struct sock *child)
  157. {
  158. local_bh_disable();
  159. __inet_inherit_port(table, sk, child);
  160. local_bh_enable();
  161. }
  162. extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
  163. extern void inet_listen_wlock(struct inet_hashinfo *hashinfo);
  164. /*
  165. * - We may sleep inside this lock.
  166. * - If sleeping is not required (or called from BH),
  167. * use plain read_(un)lock(&inet_hashinfo.lhash_lock).
  168. */
  169. static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
  170. {
  171. /* read_lock synchronizes to candidates to writers */
  172. read_lock(&hashinfo->lhash_lock);
  173. atomic_inc(&hashinfo->lhash_users);
  174. read_unlock(&hashinfo->lhash_lock);
  175. }
  176. static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo)
  177. {
  178. if (atomic_dec_and_test(&hashinfo->lhash_users))
  179. wake_up(&hashinfo->lhash_wait);
  180. }
  181. static inline void __inet_hash(struct inet_hashinfo *hashinfo,
  182. struct sock *sk, const int listen_possible)
  183. {
  184. struct hlist_head *list;
  185. rwlock_t *lock;
  186. BUG_TRAP(sk_unhashed(sk));
  187. if (listen_possible && sk->sk_state == TCP_LISTEN) {
  188. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  189. lock = &hashinfo->lhash_lock;
  190. inet_listen_wlock(hashinfo);
  191. } else {
  192. struct inet_ehash_bucket *head;
  193. sk->sk_hash = inet_sk_ehashfn(sk);
  194. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  195. list = &head->chain;
  196. lock = &head->lock;
  197. write_lock(lock);
  198. }
  199. __sk_add_node(sk, list);
  200. sock_prot_inc_use(sk->sk_prot);
  201. write_unlock(lock);
  202. if (listen_possible && sk->sk_state == TCP_LISTEN)
  203. wake_up(&hashinfo->lhash_wait);
  204. }
  205. static inline void inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk)
  206. {
  207. if (sk->sk_state != TCP_CLOSE) {
  208. local_bh_disable();
  209. __inet_hash(hashinfo, sk, 1);
  210. local_bh_enable();
  211. }
  212. }
  213. static inline void inet_unhash(struct inet_hashinfo *hashinfo, struct sock *sk)
  214. {
  215. rwlock_t *lock;
  216. if (sk_unhashed(sk))
  217. goto out;
  218. if (sk->sk_state == TCP_LISTEN) {
  219. local_bh_disable();
  220. inet_listen_wlock(hashinfo);
  221. lock = &hashinfo->lhash_lock;
  222. } else {
  223. lock = &inet_ehash_bucket(hashinfo, sk->sk_hash)->lock;
  224. write_lock_bh(lock);
  225. }
  226. if (__sk_del_node_init(sk))
  227. sock_prot_dec_use(sk->sk_prot);
  228. write_unlock_bh(lock);
  229. out:
  230. if (sk->sk_state == TCP_LISTEN)
  231. wake_up(&hashinfo->lhash_wait);
  232. }
  233. static inline int inet_iif(const struct sk_buff *skb)
  234. {
  235. return ((struct rtable *)skb->dst)->rt_iif;
  236. }
  237. extern struct sock *__inet_lookup_listener(struct inet_hashinfo *hashinfo,
  238. const __be32 daddr,
  239. const unsigned short hnum,
  240. const int dif);
  241. static inline struct sock *inet_lookup_listener(struct inet_hashinfo *hashinfo,
  242. __be32 daddr, __be16 dport, int dif)
  243. {
  244. return __inet_lookup_listener(hashinfo, daddr, ntohs(dport), dif);
  245. }
  246. /* Socket demux engine toys. */
  247. /* What happens here is ugly; there's a pair of adjacent fields in
  248. struct inet_sock; __be16 dport followed by __u16 num. We want to
  249. search by pair, so we combine the keys into a single 32bit value
  250. and compare with 32bit value read from &...->dport. Let's at least
  251. make sure that it's not mixed with anything else...
  252. On 64bit targets we combine comparisons with pair of adjacent __be32
  253. fields in the same way.
  254. */
  255. typedef __u32 __bitwise __portpair;
  256. #ifdef __BIG_ENDIAN
  257. #define INET_COMBINED_PORTS(__sport, __dport) \
  258. ((__force __portpair)(((__force __u32)(__be16)(__sport) << 16) | (__u32)(__dport)))
  259. #else /* __LITTLE_ENDIAN */
  260. #define INET_COMBINED_PORTS(__sport, __dport) \
  261. ((__force __portpair)(((__u32)(__dport) << 16) | (__force __u32)(__be16)(__sport)))
  262. #endif
  263. #if (BITS_PER_LONG == 64)
  264. typedef __u64 __bitwise __addrpair;
  265. #ifdef __BIG_ENDIAN
  266. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  267. const __addrpair __name = (__force __addrpair) ( \
  268. (((__force __u64)(__be32)(__saddr)) << 32) | \
  269. ((__force __u64)(__be32)(__daddr)));
  270. #else /* __LITTLE_ENDIAN */
  271. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  272. const __addrpair __name = (__force __addrpair) ( \
  273. (((__force __u64)(__be32)(__daddr)) << 32) | \
  274. ((__force __u64)(__be32)(__saddr)));
  275. #endif /* __BIG_ENDIAN */
  276. #define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
  277. (((__sk)->sk_hash == (__hash)) && \
  278. ((*((__addrpair *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
  279. ((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
  280. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  281. #define INET_TW_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
  282. (((__sk)->sk_hash == (__hash)) && \
  283. ((*((__addrpair *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
  284. ((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
  285. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  286. #else /* 32-bit arch */
  287. #define INET_ADDR_COOKIE(__name, __saddr, __daddr)
  288. #define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif) \
  289. (((__sk)->sk_hash == (__hash)) && \
  290. (inet_sk(__sk)->daddr == (__saddr)) && \
  291. (inet_sk(__sk)->rcv_saddr == (__daddr)) && \
  292. ((*((__portpair *)&(inet_sk(__sk)->dport))) == (__ports)) && \
  293. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  294. #define INET_TW_MATCH(__sk, __hash,__cookie, __saddr, __daddr, __ports, __dif) \
  295. (((__sk)->sk_hash == (__hash)) && \
  296. (inet_twsk(__sk)->tw_daddr == (__saddr)) && \
  297. (inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \
  298. ((*((__portpair *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
  299. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  300. #endif /* 64-bit arch */
  301. /*
  302. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
  303. * not check it for lookups anymore, thanks Alexey. -DaveM
  304. *
  305. * Local BH must be disabled here.
  306. */
  307. static inline struct sock *
  308. __inet_lookup_established(struct inet_hashinfo *hashinfo,
  309. const __be32 saddr, const __be16 sport,
  310. const __be32 daddr, const u16 hnum,
  311. const int dif)
  312. {
  313. INET_ADDR_COOKIE(acookie, saddr, daddr)
  314. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  315. struct sock *sk;
  316. const struct hlist_node *node;
  317. /* Optimize here for direct hit, only listening connections can
  318. * have wildcards anyways.
  319. */
  320. unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
  321. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  322. prefetch(head->chain.first);
  323. read_lock(&head->lock);
  324. sk_for_each(sk, node, &head->chain) {
  325. if (INET_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
  326. goto hit; /* You sunk my battleship! */
  327. }
  328. /* Must check for a TIME_WAIT'er before going to listener hash. */
  329. sk_for_each(sk, node, &head->twchain) {
  330. if (INET_TW_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
  331. goto hit;
  332. }
  333. sk = NULL;
  334. out:
  335. read_unlock(&head->lock);
  336. return sk;
  337. hit:
  338. sock_hold(sk);
  339. goto out;
  340. }
  341. static inline struct sock *
  342. inet_lookup_established(struct inet_hashinfo *hashinfo,
  343. const __be32 saddr, const __be16 sport,
  344. const __be32 daddr, const __be16 dport,
  345. const int dif)
  346. {
  347. return __inet_lookup_established(hashinfo, saddr, sport, daddr,
  348. ntohs(dport), dif);
  349. }
  350. static inline struct sock *__inet_lookup(struct inet_hashinfo *hashinfo,
  351. const __be32 saddr, const __be16 sport,
  352. const __be32 daddr, const __be16 dport,
  353. const int dif)
  354. {
  355. u16 hnum = ntohs(dport);
  356. struct sock *sk = __inet_lookup_established(hashinfo, saddr, sport, daddr,
  357. hnum, dif);
  358. return sk ? : __inet_lookup_listener(hashinfo, daddr, hnum, dif);
  359. }
  360. static inline struct sock *inet_lookup(struct inet_hashinfo *hashinfo,
  361. const __be32 saddr, const __be16 sport,
  362. const __be32 daddr, const __be16 dport,
  363. const int dif)
  364. {
  365. struct sock *sk;
  366. local_bh_disable();
  367. sk = __inet_lookup(hashinfo, saddr, sport, daddr, dport, dif);
  368. local_bh_enable();
  369. return sk;
  370. }
  371. extern int inet_hash_connect(struct inet_timewait_death_row *death_row,
  372. struct sock *sk);
  373. #endif /* _INET_HASHTABLES_H */