inet_hashtables.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/config.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/list.h>
  19. #include <linux/slab.h>
  20. #include <linux/socket.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <linux/wait.h>
  24. #include <net/inet_connection_sock.h>
  25. #include <net/inet_sock.h>
  26. #include <net/route.h>
  27. #include <net/sock.h>
  28. #include <net/tcp_states.h>
  29. #include <asm/atomic.h>
  30. #include <asm/byteorder.h>
  31. /* This is for all connections with a full identity, no wildcards.
  32. * New scheme, half the table is for TIME_WAIT, the other half is
  33. * for the rest. I'll experiment with dynamic table growth later.
  34. */
  35. struct inet_ehash_bucket {
  36. rwlock_t lock;
  37. struct hlist_head chain;
  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. * First half of the table is for sockets not in TIME_WAIT, second half
  91. * is for TIME_WAIT sockets only.
  92. */
  93. struct inet_ehash_bucket *ehash;
  94. /* Ok, let's try this, I give up, we do need a local binding
  95. * TCP hash as well as the others for fast bind/connect.
  96. */
  97. struct inet_bind_hashbucket *bhash;
  98. int bhash_size;
  99. unsigned int ehash_size;
  100. /* All sockets in TCP_LISTEN state will be in here. This is the only
  101. * table where wildcard'd TCP sockets can exist. Hash function here
  102. * is just local port number.
  103. */
  104. struct hlist_head listening_hash[INET_LHTABLE_SIZE];
  105. /* All the above members are written once at bootup and
  106. * never written again _or_ are predominantly read-access.
  107. *
  108. * Now align to a new cache line as all the following members
  109. * are often dirty.
  110. */
  111. rwlock_t lhash_lock ____cacheline_aligned;
  112. atomic_t lhash_users;
  113. wait_queue_head_t lhash_wait;
  114. kmem_cache_t *bind_bucket_cachep;
  115. };
  116. static inline struct inet_ehash_bucket *inet_ehash_bucket(
  117. struct inet_hashinfo *hashinfo,
  118. unsigned int hash)
  119. {
  120. return &hashinfo->ehash[hash & (hashinfo->ehash_size - 1)];
  121. }
  122. extern struct inet_bind_bucket *
  123. inet_bind_bucket_create(kmem_cache_t *cachep,
  124. struct inet_bind_hashbucket *head,
  125. const unsigned short snum);
  126. extern void inet_bind_bucket_destroy(kmem_cache_t *cachep,
  127. struct inet_bind_bucket *tb);
  128. static inline int inet_bhashfn(const __u16 lport, const int bhash_size)
  129. {
  130. return lport & (bhash_size - 1);
  131. }
  132. extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  133. const unsigned short snum);
  134. /* These can have wildcards, don't try too hard. */
  135. static inline int inet_lhashfn(const unsigned short num)
  136. {
  137. return num & (INET_LHTABLE_SIZE - 1);
  138. }
  139. static inline int inet_sk_listen_hashfn(const struct sock *sk)
  140. {
  141. return inet_lhashfn(inet_sk(sk)->num);
  142. }
  143. /* Caller must disable local BH processing. */
  144. static inline void __inet_inherit_port(struct inet_hashinfo *table,
  145. struct sock *sk, struct sock *child)
  146. {
  147. const int bhash = inet_bhashfn(inet_sk(child)->num, table->bhash_size);
  148. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  149. struct inet_bind_bucket *tb;
  150. spin_lock(&head->lock);
  151. tb = inet_csk(sk)->icsk_bind_hash;
  152. sk_add_bind_node(child, &tb->owners);
  153. inet_csk(child)->icsk_bind_hash = tb;
  154. spin_unlock(&head->lock);
  155. }
  156. static inline void inet_inherit_port(struct inet_hashinfo *table,
  157. struct sock *sk, struct sock *child)
  158. {
  159. local_bh_disable();
  160. __inet_inherit_port(table, sk, child);
  161. local_bh_enable();
  162. }
  163. extern void inet_put_port(struct inet_hashinfo *table, struct sock *sk);
  164. extern void inet_listen_wlock(struct inet_hashinfo *hashinfo);
  165. /*
  166. * - We may sleep inside this lock.
  167. * - If sleeping is not required (or called from BH),
  168. * use plain read_(un)lock(&inet_hashinfo.lhash_lock).
  169. */
  170. static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
  171. {
  172. /* read_lock synchronizes to candidates to writers */
  173. read_lock(&hashinfo->lhash_lock);
  174. atomic_inc(&hashinfo->lhash_users);
  175. read_unlock(&hashinfo->lhash_lock);
  176. }
  177. static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo)
  178. {
  179. if (atomic_dec_and_test(&hashinfo->lhash_users))
  180. wake_up(&hashinfo->lhash_wait);
  181. }
  182. static inline void __inet_hash(struct inet_hashinfo *hashinfo,
  183. struct sock *sk, const int listen_possible)
  184. {
  185. struct hlist_head *list;
  186. rwlock_t *lock;
  187. BUG_TRAP(sk_unhashed(sk));
  188. if (listen_possible && sk->sk_state == TCP_LISTEN) {
  189. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  190. lock = &hashinfo->lhash_lock;
  191. inet_listen_wlock(hashinfo);
  192. } else {
  193. struct inet_ehash_bucket *head;
  194. sk->sk_hash = inet_sk_ehashfn(sk);
  195. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  196. list = &head->chain;
  197. lock = &head->lock;
  198. write_lock(lock);
  199. }
  200. __sk_add_node(sk, list);
  201. sock_prot_inc_use(sk->sk_prot);
  202. write_unlock(lock);
  203. if (listen_possible && sk->sk_state == TCP_LISTEN)
  204. wake_up(&hashinfo->lhash_wait);
  205. }
  206. static inline void inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk)
  207. {
  208. if (sk->sk_state != TCP_CLOSE) {
  209. local_bh_disable();
  210. __inet_hash(hashinfo, sk, 1);
  211. local_bh_enable();
  212. }
  213. }
  214. static inline void inet_unhash(struct inet_hashinfo *hashinfo, struct sock *sk)
  215. {
  216. rwlock_t *lock;
  217. if (sk_unhashed(sk))
  218. goto out;
  219. if (sk->sk_state == TCP_LISTEN) {
  220. local_bh_disable();
  221. inet_listen_wlock(hashinfo);
  222. lock = &hashinfo->lhash_lock;
  223. } else {
  224. lock = &inet_ehash_bucket(hashinfo, sk->sk_hash)->lock;
  225. write_lock_bh(lock);
  226. }
  227. if (__sk_del_node_init(sk))
  228. sock_prot_dec_use(sk->sk_prot);
  229. write_unlock_bh(lock);
  230. out:
  231. if (sk->sk_state == TCP_LISTEN)
  232. wake_up(&hashinfo->lhash_wait);
  233. }
  234. static inline int inet_iif(const struct sk_buff *skb)
  235. {
  236. return ((struct rtable *)skb->dst)->rt_iif;
  237. }
  238. extern struct sock *__inet_lookup_listener(const struct hlist_head *head,
  239. const u32 daddr,
  240. const unsigned short hnum,
  241. const int dif);
  242. /* Optimize the common listener case. */
  243. static inline struct sock *
  244. inet_lookup_listener(struct inet_hashinfo *hashinfo,
  245. const u32 daddr,
  246. const unsigned short hnum, const int dif)
  247. {
  248. struct sock *sk = NULL;
  249. const struct hlist_head *head;
  250. read_lock(&hashinfo->lhash_lock);
  251. head = &hashinfo->listening_hash[inet_lhashfn(hnum)];
  252. if (!hlist_empty(head)) {
  253. const struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
  254. if (inet->num == hnum && !sk->sk_node.next &&
  255. (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
  256. (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
  257. !sk->sk_bound_dev_if)
  258. goto sherry_cache;
  259. sk = __inet_lookup_listener(head, daddr, hnum, dif);
  260. }
  261. if (sk) {
  262. sherry_cache:
  263. sock_hold(sk);
  264. }
  265. read_unlock(&hashinfo->lhash_lock);
  266. return sk;
  267. }
  268. /* Socket demux engine toys. */
  269. #ifdef __BIG_ENDIAN
  270. #define INET_COMBINED_PORTS(__sport, __dport) \
  271. (((__u32)(__sport) << 16) | (__u32)(__dport))
  272. #else /* __LITTLE_ENDIAN */
  273. #define INET_COMBINED_PORTS(__sport, __dport) \
  274. (((__u32)(__dport) << 16) | (__u32)(__sport))
  275. #endif
  276. #if (BITS_PER_LONG == 64)
  277. #ifdef __BIG_ENDIAN
  278. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  279. const __u64 __name = (((__u64)(__saddr)) << 32) | ((__u64)(__daddr));
  280. #else /* __LITTLE_ENDIAN */
  281. #define INET_ADDR_COOKIE(__name, __saddr, __daddr) \
  282. const __u64 __name = (((__u64)(__daddr)) << 32) | ((__u64)(__saddr));
  283. #endif /* __BIG_ENDIAN */
  284. #define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
  285. (((__sk)->sk_hash == (__hash)) && \
  286. ((*((__u64 *)&(inet_sk(__sk)->daddr))) == (__cookie)) && \
  287. ((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
  288. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  289. #define INET_TW_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif)\
  290. (((__sk)->sk_hash == (__hash)) && \
  291. ((*((__u64 *)&(inet_twsk(__sk)->tw_daddr))) == (__cookie)) && \
  292. ((*((__u32 *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
  293. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  294. #else /* 32-bit arch */
  295. #define INET_ADDR_COOKIE(__name, __saddr, __daddr)
  296. #define INET_MATCH(__sk, __hash, __cookie, __saddr, __daddr, __ports, __dif) \
  297. (((__sk)->sk_hash == (__hash)) && \
  298. (inet_sk(__sk)->daddr == (__saddr)) && \
  299. (inet_sk(__sk)->rcv_saddr == (__daddr)) && \
  300. ((*((__u32 *)&(inet_sk(__sk)->dport))) == (__ports)) && \
  301. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  302. #define INET_TW_MATCH(__sk, __hash,__cookie, __saddr, __daddr, __ports, __dif) \
  303. (((__sk)->sk_hash == (__hash)) && \
  304. (inet_twsk(__sk)->tw_daddr == (__saddr)) && \
  305. (inet_twsk(__sk)->tw_rcv_saddr == (__daddr)) && \
  306. ((*((__u32 *)&(inet_twsk(__sk)->tw_dport))) == (__ports)) && \
  307. (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
  308. #endif /* 64-bit arch */
  309. /*
  310. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so we need
  311. * not check it for lookups anymore, thanks Alexey. -DaveM
  312. *
  313. * Local BH must be disabled here.
  314. */
  315. static inline struct sock *
  316. __inet_lookup_established(struct inet_hashinfo *hashinfo,
  317. const u32 saddr, const u16 sport,
  318. const u32 daddr, const u16 hnum,
  319. const int dif)
  320. {
  321. INET_ADDR_COOKIE(acookie, saddr, daddr)
  322. const __u32 ports = INET_COMBINED_PORTS(sport, hnum);
  323. struct sock *sk;
  324. const struct hlist_node *node;
  325. /* Optimize here for direct hit, only listening connections can
  326. * have wildcards anyways.
  327. */
  328. unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
  329. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  330. prefetch(head->chain.first);
  331. read_lock(&head->lock);
  332. sk_for_each(sk, node, &head->chain) {
  333. if (INET_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
  334. goto hit; /* You sunk my battleship! */
  335. }
  336. /* Must check for a TIME_WAIT'er before going to listener hash. */
  337. sk_for_each(sk, node, &(head + hashinfo->ehash_size)->chain) {
  338. if (INET_TW_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
  339. goto hit;
  340. }
  341. sk = NULL;
  342. out:
  343. read_unlock(&head->lock);
  344. return sk;
  345. hit:
  346. sock_hold(sk);
  347. goto out;
  348. }
  349. static inline struct sock *__inet_lookup(struct inet_hashinfo *hashinfo,
  350. const u32 saddr, const u16 sport,
  351. const u32 daddr, const u16 hnum,
  352. const int dif)
  353. {
  354. struct sock *sk = __inet_lookup_established(hashinfo, saddr, sport, daddr,
  355. hnum, dif);
  356. return sk ? : inet_lookup_listener(hashinfo, daddr, hnum, dif);
  357. }
  358. static inline struct sock *inet_lookup(struct inet_hashinfo *hashinfo,
  359. const u32 saddr, const u16 sport,
  360. const u32 daddr, const u16 dport,
  361. const int dif)
  362. {
  363. struct sock *sk;
  364. local_bh_disable();
  365. sk = __inet_lookup(hashinfo, saddr, sport, daddr, ntohs(dport), dif);
  366. local_bh_enable();
  367. return sk;
  368. }
  369. extern int inet_hash_connect(struct inet_timewait_death_row *death_row,
  370. struct sock *sk);
  371. #endif /* _INET_HASHTABLES_H */