inet_hashtables.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. * Generic INET transport hashtables
  7. *
  8. * Authors: Lotsa people, from code originally in tcp
  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. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/wait.h>
  20. #include <net/inet_connection_sock.h>
  21. #include <net/inet_hashtables.h>
  22. #include <net/ip.h>
  23. /*
  24. * Allocate and initialize a new local port bind bucket.
  25. * The bindhash mutex for snum's hash chain must be held here.
  26. */
  27. struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
  28. struct net *net,
  29. struct inet_bind_hashbucket *head,
  30. const unsigned short snum)
  31. {
  32. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
  33. if (tb != NULL) {
  34. tb->ib_net = net;
  35. tb->port = snum;
  36. tb->fastreuse = 0;
  37. INIT_HLIST_HEAD(&tb->owners);
  38. hlist_add_head(&tb->node, &head->chain);
  39. }
  40. return tb;
  41. }
  42. /*
  43. * Caller must hold hashbucket lock for this tb with local BH disabled
  44. */
  45. void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
  46. {
  47. if (hlist_empty(&tb->owners)) {
  48. __hlist_del(&tb->node);
  49. kmem_cache_free(cachep, tb);
  50. }
  51. }
  52. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  53. const unsigned short snum)
  54. {
  55. inet_sk(sk)->num = snum;
  56. sk_add_bind_node(sk, &tb->owners);
  57. inet_csk(sk)->icsk_bind_hash = tb;
  58. }
  59. /*
  60. * Get rid of any references to a local port held by the given sock.
  61. */
  62. static void __inet_put_port(struct sock *sk)
  63. {
  64. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  65. const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size);
  66. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  67. struct inet_bind_bucket *tb;
  68. spin_lock(&head->lock);
  69. tb = inet_csk(sk)->icsk_bind_hash;
  70. __sk_del_bind_node(sk);
  71. inet_csk(sk)->icsk_bind_hash = NULL;
  72. inet_sk(sk)->num = 0;
  73. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  74. spin_unlock(&head->lock);
  75. }
  76. void inet_put_port(struct sock *sk)
  77. {
  78. local_bh_disable();
  79. __inet_put_port(sk);
  80. local_bh_enable();
  81. }
  82. EXPORT_SYMBOL(inet_put_port);
  83. /*
  84. * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
  85. * Look, when several writers sleep and reader wakes them up, all but one
  86. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  87. * this, _but_ remember, it adds useless work on UP machines (wake up each
  88. * exclusive lock release). It should be ifdefed really.
  89. */
  90. void inet_listen_wlock(struct inet_hashinfo *hashinfo)
  91. __acquires(hashinfo->lhash_lock)
  92. {
  93. write_lock(&hashinfo->lhash_lock);
  94. if (atomic_read(&hashinfo->lhash_users)) {
  95. DEFINE_WAIT(wait);
  96. for (;;) {
  97. prepare_to_wait_exclusive(&hashinfo->lhash_wait,
  98. &wait, TASK_UNINTERRUPTIBLE);
  99. if (!atomic_read(&hashinfo->lhash_users))
  100. break;
  101. write_unlock_bh(&hashinfo->lhash_lock);
  102. schedule();
  103. write_lock_bh(&hashinfo->lhash_lock);
  104. }
  105. finish_wait(&hashinfo->lhash_wait, &wait);
  106. }
  107. }
  108. /*
  109. * Don't inline this cruft. Here are some nice properties to exploit here. The
  110. * BSD API does not allow a listening sock to specify the remote port nor the
  111. * remote address for the connection. So always assume those are both
  112. * wildcarded during the search since they can never be otherwise.
  113. */
  114. static struct sock *inet_lookup_listener_slow(struct net *net,
  115. const struct hlist_head *head,
  116. const __be32 daddr,
  117. const unsigned short hnum,
  118. const int dif)
  119. {
  120. struct sock *result = NULL, *sk;
  121. const struct hlist_node *node;
  122. int hiscore = -1;
  123. sk_for_each(sk, node, head) {
  124. const struct inet_sock *inet = inet_sk(sk);
  125. if (sk->sk_net == net && inet->num == hnum &&
  126. !ipv6_only_sock(sk)) {
  127. const __be32 rcv_saddr = inet->rcv_saddr;
  128. int score = sk->sk_family == PF_INET ? 1 : 0;
  129. if (rcv_saddr) {
  130. if (rcv_saddr != daddr)
  131. continue;
  132. score += 2;
  133. }
  134. if (sk->sk_bound_dev_if) {
  135. if (sk->sk_bound_dev_if != dif)
  136. continue;
  137. score += 2;
  138. }
  139. if (score == 5)
  140. return sk;
  141. if (score > hiscore) {
  142. hiscore = score;
  143. result = sk;
  144. }
  145. }
  146. }
  147. return result;
  148. }
  149. /* Optimize the common listener case. */
  150. struct sock *__inet_lookup_listener(struct net *net,
  151. struct inet_hashinfo *hashinfo,
  152. const __be32 daddr, const unsigned short hnum,
  153. const int dif)
  154. {
  155. struct sock *sk = NULL;
  156. const struct hlist_head *head;
  157. read_lock(&hashinfo->lhash_lock);
  158. head = &hashinfo->listening_hash[inet_lhashfn(hnum)];
  159. if (!hlist_empty(head)) {
  160. const struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
  161. if (inet->num == hnum && !sk->sk_node.next &&
  162. (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
  163. (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
  164. !sk->sk_bound_dev_if && sk->sk_net == net)
  165. goto sherry_cache;
  166. sk = inet_lookup_listener_slow(net, head, daddr, hnum, dif);
  167. }
  168. if (sk) {
  169. sherry_cache:
  170. sock_hold(sk);
  171. }
  172. read_unlock(&hashinfo->lhash_lock);
  173. return sk;
  174. }
  175. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  176. struct sock * __inet_lookup_established(struct net *net,
  177. struct inet_hashinfo *hashinfo,
  178. const __be32 saddr, const __be16 sport,
  179. const __be32 daddr, const u16 hnum,
  180. const int dif)
  181. {
  182. INET_ADDR_COOKIE(acookie, saddr, daddr)
  183. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  184. struct sock *sk;
  185. const struct hlist_node *node;
  186. /* Optimize here for direct hit, only listening connections can
  187. * have wildcards anyways.
  188. */
  189. unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
  190. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  191. rwlock_t *lock = inet_ehash_lockp(hashinfo, hash);
  192. prefetch(head->chain.first);
  193. read_lock(lock);
  194. sk_for_each(sk, node, &head->chain) {
  195. if (INET_MATCH(sk, net, hash, acookie,
  196. saddr, daddr, ports, dif))
  197. goto hit; /* You sunk my battleship! */
  198. }
  199. /* Must check for a TIME_WAIT'er before going to listener hash. */
  200. sk_for_each(sk, node, &head->twchain) {
  201. if (INET_TW_MATCH(sk, net, hash, acookie,
  202. saddr, daddr, ports, dif))
  203. goto hit;
  204. }
  205. sk = NULL;
  206. out:
  207. read_unlock(lock);
  208. return sk;
  209. hit:
  210. sock_hold(sk);
  211. goto out;
  212. }
  213. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  214. /* called with local bh disabled */
  215. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  216. struct sock *sk, __u16 lport,
  217. struct inet_timewait_sock **twp)
  218. {
  219. struct inet_hashinfo *hinfo = death_row->hashinfo;
  220. struct inet_sock *inet = inet_sk(sk);
  221. __be32 daddr = inet->rcv_saddr;
  222. __be32 saddr = inet->daddr;
  223. int dif = sk->sk_bound_dev_if;
  224. INET_ADDR_COOKIE(acookie, saddr, daddr)
  225. const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
  226. unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
  227. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  228. rwlock_t *lock = inet_ehash_lockp(hinfo, hash);
  229. struct sock *sk2;
  230. const struct hlist_node *node;
  231. struct inet_timewait_sock *tw;
  232. struct net *net = sk->sk_net;
  233. prefetch(head->chain.first);
  234. write_lock(lock);
  235. /* Check TIME-WAIT sockets first. */
  236. sk_for_each(sk2, node, &head->twchain) {
  237. tw = inet_twsk(sk2);
  238. if (INET_TW_MATCH(sk2, net, hash, acookie,
  239. saddr, daddr, ports, dif)) {
  240. if (twsk_unique(sk, sk2, twp))
  241. goto unique;
  242. else
  243. goto not_unique;
  244. }
  245. }
  246. tw = NULL;
  247. /* And established part... */
  248. sk_for_each(sk2, node, &head->chain) {
  249. if (INET_MATCH(sk2, net, hash, acookie,
  250. saddr, daddr, ports, dif))
  251. goto not_unique;
  252. }
  253. unique:
  254. /* Must record num and sport now. Otherwise we will see
  255. * in hash table socket with a funny identity. */
  256. inet->num = lport;
  257. inet->sport = htons(lport);
  258. sk->sk_hash = hash;
  259. BUG_TRAP(sk_unhashed(sk));
  260. __sk_add_node(sk, &head->chain);
  261. sock_prot_inuse_add(sk->sk_prot, 1);
  262. write_unlock(lock);
  263. if (twp) {
  264. *twp = tw;
  265. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  266. } else if (tw) {
  267. /* Silly. Should hash-dance instead... */
  268. inet_twsk_deschedule(tw, death_row);
  269. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  270. inet_twsk_put(tw);
  271. }
  272. return 0;
  273. not_unique:
  274. write_unlock(lock);
  275. return -EADDRNOTAVAIL;
  276. }
  277. static inline u32 inet_sk_port_offset(const struct sock *sk)
  278. {
  279. const struct inet_sock *inet = inet_sk(sk);
  280. return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
  281. inet->dport);
  282. }
  283. void __inet_hash_nolisten(struct sock *sk)
  284. {
  285. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  286. struct hlist_head *list;
  287. rwlock_t *lock;
  288. struct inet_ehash_bucket *head;
  289. BUG_TRAP(sk_unhashed(sk));
  290. sk->sk_hash = inet_sk_ehashfn(sk);
  291. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  292. list = &head->chain;
  293. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  294. write_lock(lock);
  295. __sk_add_node(sk, list);
  296. sock_prot_inuse_add(sk->sk_prot, 1);
  297. write_unlock(lock);
  298. }
  299. EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
  300. static void __inet_hash(struct sock *sk)
  301. {
  302. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  303. struct hlist_head *list;
  304. rwlock_t *lock;
  305. if (sk->sk_state != TCP_LISTEN) {
  306. __inet_hash_nolisten(sk);
  307. return;
  308. }
  309. BUG_TRAP(sk_unhashed(sk));
  310. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  311. lock = &hashinfo->lhash_lock;
  312. inet_listen_wlock(hashinfo);
  313. __sk_add_node(sk, list);
  314. sock_prot_inuse_add(sk->sk_prot, 1);
  315. write_unlock(lock);
  316. wake_up(&hashinfo->lhash_wait);
  317. }
  318. void inet_hash(struct sock *sk)
  319. {
  320. if (sk->sk_state != TCP_CLOSE) {
  321. local_bh_disable();
  322. __inet_hash(sk);
  323. local_bh_enable();
  324. }
  325. }
  326. EXPORT_SYMBOL_GPL(inet_hash);
  327. void inet_unhash(struct sock *sk)
  328. {
  329. rwlock_t *lock;
  330. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  331. if (sk_unhashed(sk))
  332. goto out;
  333. if (sk->sk_state == TCP_LISTEN) {
  334. local_bh_disable();
  335. inet_listen_wlock(hashinfo);
  336. lock = &hashinfo->lhash_lock;
  337. } else {
  338. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  339. write_lock_bh(lock);
  340. }
  341. if (__sk_del_node_init(sk))
  342. sock_prot_inuse_add(sk->sk_prot, -1);
  343. write_unlock_bh(lock);
  344. out:
  345. if (sk->sk_state == TCP_LISTEN)
  346. wake_up(&hashinfo->lhash_wait);
  347. }
  348. EXPORT_SYMBOL_GPL(inet_unhash);
  349. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  350. struct sock *sk, u32 port_offset,
  351. int (*check_established)(struct inet_timewait_death_row *,
  352. struct sock *, __u16, struct inet_timewait_sock **),
  353. void (*hash)(struct sock *sk))
  354. {
  355. struct inet_hashinfo *hinfo = death_row->hashinfo;
  356. const unsigned short snum = inet_sk(sk)->num;
  357. struct inet_bind_hashbucket *head;
  358. struct inet_bind_bucket *tb;
  359. int ret;
  360. struct net *net = sk->sk_net;
  361. if (!snum) {
  362. int i, remaining, low, high, port;
  363. static u32 hint;
  364. u32 offset = hint + port_offset;
  365. struct hlist_node *node;
  366. struct inet_timewait_sock *tw = NULL;
  367. inet_get_local_port_range(&low, &high);
  368. remaining = (high - low) + 1;
  369. local_bh_disable();
  370. for (i = 1; i <= remaining; i++) {
  371. port = low + (i + offset) % remaining;
  372. head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
  373. spin_lock(&head->lock);
  374. /* Does not bother with rcv_saddr checks,
  375. * because the established check is already
  376. * unique enough.
  377. */
  378. inet_bind_bucket_for_each(tb, node, &head->chain) {
  379. if (tb->ib_net == net && tb->port == port) {
  380. BUG_TRAP(!hlist_empty(&tb->owners));
  381. if (tb->fastreuse >= 0)
  382. goto next_port;
  383. if (!check_established(death_row, sk,
  384. port, &tw))
  385. goto ok;
  386. goto next_port;
  387. }
  388. }
  389. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  390. net, head, port);
  391. if (!tb) {
  392. spin_unlock(&head->lock);
  393. break;
  394. }
  395. tb->fastreuse = -1;
  396. goto ok;
  397. next_port:
  398. spin_unlock(&head->lock);
  399. }
  400. local_bh_enable();
  401. return -EADDRNOTAVAIL;
  402. ok:
  403. hint += i;
  404. /* Head lock still held and bh's disabled */
  405. inet_bind_hash(sk, tb, port);
  406. if (sk_unhashed(sk)) {
  407. inet_sk(sk)->sport = htons(port);
  408. hash(sk);
  409. }
  410. spin_unlock(&head->lock);
  411. if (tw) {
  412. inet_twsk_deschedule(tw, death_row);
  413. inet_twsk_put(tw);
  414. }
  415. ret = 0;
  416. goto out;
  417. }
  418. head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
  419. tb = inet_csk(sk)->icsk_bind_hash;
  420. spin_lock_bh(&head->lock);
  421. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  422. hash(sk);
  423. spin_unlock_bh(&head->lock);
  424. return 0;
  425. } else {
  426. spin_unlock(&head->lock);
  427. /* No definite answer... Walk to established hash table */
  428. ret = check_established(death_row, sk, snum, NULL);
  429. out:
  430. local_bh_enable();
  431. return ret;
  432. }
  433. }
  434. /*
  435. * Bind a port for a connect operation and hash it.
  436. */
  437. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  438. struct sock *sk)
  439. {
  440. return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
  441. __inet_check_established, __inet_hash_nolisten);
  442. }
  443. EXPORT_SYMBOL_GPL(inet_hash_connect);