inet_hashtables.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. EXPORT_SYMBOL(inet_listen_wlock);
  109. /*
  110. * Don't inline this cruft. Here are some nice properties to exploit here. The
  111. * BSD API does not allow a listening sock to specify the remote port nor the
  112. * remote address for the connection. So always assume those are both
  113. * wildcarded during the search since they can never be otherwise.
  114. */
  115. static struct sock *inet_lookup_listener_slow(struct net *net,
  116. const struct hlist_head *head,
  117. const __be32 daddr,
  118. const unsigned short hnum,
  119. const int dif)
  120. {
  121. struct sock *result = NULL, *sk;
  122. const struct hlist_node *node;
  123. int hiscore = -1;
  124. sk_for_each(sk, node, head) {
  125. const struct inet_sock *inet = inet_sk(sk);
  126. if (sk->sk_net == net && inet->num == hnum &&
  127. !ipv6_only_sock(sk)) {
  128. const __be32 rcv_saddr = inet->rcv_saddr;
  129. int score = sk->sk_family == PF_INET ? 1 : 0;
  130. if (rcv_saddr) {
  131. if (rcv_saddr != daddr)
  132. continue;
  133. score += 2;
  134. }
  135. if (sk->sk_bound_dev_if) {
  136. if (sk->sk_bound_dev_if != dif)
  137. continue;
  138. score += 2;
  139. }
  140. if (score == 5)
  141. return sk;
  142. if (score > hiscore) {
  143. hiscore = score;
  144. result = sk;
  145. }
  146. }
  147. }
  148. return result;
  149. }
  150. /* Optimize the common listener case. */
  151. struct sock *__inet_lookup_listener(struct net *net,
  152. struct inet_hashinfo *hashinfo,
  153. const __be32 daddr, const unsigned short hnum,
  154. const int dif)
  155. {
  156. struct sock *sk = NULL;
  157. const struct hlist_head *head;
  158. read_lock(&hashinfo->lhash_lock);
  159. head = &hashinfo->listening_hash[inet_lhashfn(hnum)];
  160. if (!hlist_empty(head)) {
  161. const struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
  162. if (inet->num == hnum && !sk->sk_node.next &&
  163. (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
  164. (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
  165. !sk->sk_bound_dev_if && sk->sk_net == net)
  166. goto sherry_cache;
  167. sk = inet_lookup_listener_slow(net, head, daddr, hnum, dif);
  168. }
  169. if (sk) {
  170. sherry_cache:
  171. sock_hold(sk);
  172. }
  173. read_unlock(&hashinfo->lhash_lock);
  174. return sk;
  175. }
  176. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  177. struct sock * __inet_lookup_established(struct net *net,
  178. struct inet_hashinfo *hashinfo,
  179. const __be32 saddr, const __be16 sport,
  180. const __be32 daddr, const u16 hnum,
  181. const int dif)
  182. {
  183. INET_ADDR_COOKIE(acookie, saddr, daddr)
  184. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  185. struct sock *sk;
  186. const struct hlist_node *node;
  187. /* Optimize here for direct hit, only listening connections can
  188. * have wildcards anyways.
  189. */
  190. unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
  191. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  192. rwlock_t *lock = inet_ehash_lockp(hashinfo, hash);
  193. prefetch(head->chain.first);
  194. read_lock(lock);
  195. sk_for_each(sk, node, &head->chain) {
  196. if (INET_MATCH(sk, net, hash, acookie,
  197. saddr, daddr, ports, dif))
  198. goto hit; /* You sunk my battleship! */
  199. }
  200. /* Must check for a TIME_WAIT'er before going to listener hash. */
  201. sk_for_each(sk, node, &head->twchain) {
  202. if (INET_TW_MATCH(sk, net, hash, acookie,
  203. saddr, daddr, ports, dif))
  204. goto hit;
  205. }
  206. sk = NULL;
  207. out:
  208. read_unlock(lock);
  209. return sk;
  210. hit:
  211. sock_hold(sk);
  212. goto out;
  213. }
  214. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  215. /* called with local bh disabled */
  216. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  217. struct sock *sk, __u16 lport,
  218. struct inet_timewait_sock **twp)
  219. {
  220. struct inet_hashinfo *hinfo = death_row->hashinfo;
  221. struct inet_sock *inet = inet_sk(sk);
  222. __be32 daddr = inet->rcv_saddr;
  223. __be32 saddr = inet->daddr;
  224. int dif = sk->sk_bound_dev_if;
  225. INET_ADDR_COOKIE(acookie, saddr, daddr)
  226. const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
  227. unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
  228. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  229. rwlock_t *lock = inet_ehash_lockp(hinfo, hash);
  230. struct sock *sk2;
  231. const struct hlist_node *node;
  232. struct inet_timewait_sock *tw;
  233. struct net *net = sk->sk_net;
  234. prefetch(head->chain.first);
  235. write_lock(lock);
  236. /* Check TIME-WAIT sockets first. */
  237. sk_for_each(sk2, node, &head->twchain) {
  238. tw = inet_twsk(sk2);
  239. if (INET_TW_MATCH(sk2, net, hash, acookie,
  240. saddr, daddr, ports, dif)) {
  241. if (twsk_unique(sk, sk2, twp))
  242. goto unique;
  243. else
  244. goto not_unique;
  245. }
  246. }
  247. tw = NULL;
  248. /* And established part... */
  249. sk_for_each(sk2, node, &head->chain) {
  250. if (INET_MATCH(sk2, net, hash, acookie,
  251. saddr, daddr, ports, dif))
  252. goto not_unique;
  253. }
  254. unique:
  255. /* Must record num and sport now. Otherwise we will see
  256. * in hash table socket with a funny identity. */
  257. inet->num = lport;
  258. inet->sport = htons(lport);
  259. sk->sk_hash = hash;
  260. BUG_TRAP(sk_unhashed(sk));
  261. __sk_add_node(sk, &head->chain);
  262. sock_prot_inuse_add(sk->sk_prot, 1);
  263. write_unlock(lock);
  264. if (twp) {
  265. *twp = tw;
  266. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  267. } else if (tw) {
  268. /* Silly. Should hash-dance instead... */
  269. inet_twsk_deschedule(tw, death_row);
  270. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  271. inet_twsk_put(tw);
  272. }
  273. return 0;
  274. not_unique:
  275. write_unlock(lock);
  276. return -EADDRNOTAVAIL;
  277. }
  278. static inline u32 inet_sk_port_offset(const struct sock *sk)
  279. {
  280. const struct inet_sock *inet = inet_sk(sk);
  281. return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
  282. inet->dport);
  283. }
  284. void __inet_hash_nolisten(struct sock *sk)
  285. {
  286. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  287. struct hlist_head *list;
  288. rwlock_t *lock;
  289. struct inet_ehash_bucket *head;
  290. BUG_TRAP(sk_unhashed(sk));
  291. sk->sk_hash = inet_sk_ehashfn(sk);
  292. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  293. list = &head->chain;
  294. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  295. write_lock(lock);
  296. __sk_add_node(sk, list);
  297. sock_prot_inuse_add(sk->sk_prot, 1);
  298. write_unlock(lock);
  299. }
  300. EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
  301. static void __inet_hash(struct sock *sk)
  302. {
  303. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  304. struct hlist_head *list;
  305. rwlock_t *lock;
  306. if (sk->sk_state != TCP_LISTEN) {
  307. __inet_hash_nolisten(sk);
  308. return;
  309. }
  310. BUG_TRAP(sk_unhashed(sk));
  311. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  312. lock = &hashinfo->lhash_lock;
  313. inet_listen_wlock(hashinfo);
  314. __sk_add_node(sk, list);
  315. sock_prot_inuse_add(sk->sk_prot, 1);
  316. write_unlock(lock);
  317. wake_up(&hashinfo->lhash_wait);
  318. }
  319. void inet_hash(struct sock *sk)
  320. {
  321. if (sk->sk_state != TCP_CLOSE) {
  322. local_bh_disable();
  323. __inet_hash(sk);
  324. local_bh_enable();
  325. }
  326. }
  327. EXPORT_SYMBOL_GPL(inet_hash);
  328. void inet_unhash(struct sock *sk)
  329. {
  330. rwlock_t *lock;
  331. struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
  332. if (sk_unhashed(sk))
  333. goto out;
  334. if (sk->sk_state == TCP_LISTEN) {
  335. local_bh_disable();
  336. inet_listen_wlock(hashinfo);
  337. lock = &hashinfo->lhash_lock;
  338. } else {
  339. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  340. write_lock_bh(lock);
  341. }
  342. if (__sk_del_node_init(sk))
  343. sock_prot_inuse_add(sk->sk_prot, -1);
  344. write_unlock_bh(lock);
  345. out:
  346. if (sk->sk_state == TCP_LISTEN)
  347. wake_up(&hashinfo->lhash_wait);
  348. }
  349. EXPORT_SYMBOL_GPL(inet_unhash);
  350. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  351. struct sock *sk, u32 port_offset,
  352. int (*check_established)(struct inet_timewait_death_row *,
  353. struct sock *, __u16, struct inet_timewait_sock **),
  354. void (*hash)(struct sock *sk))
  355. {
  356. struct inet_hashinfo *hinfo = death_row->hashinfo;
  357. const unsigned short snum = inet_sk(sk)->num;
  358. struct inet_bind_hashbucket *head;
  359. struct inet_bind_bucket *tb;
  360. int ret;
  361. struct net *net = sk->sk_net;
  362. if (!snum) {
  363. int i, remaining, low, high, port;
  364. static u32 hint;
  365. u32 offset = hint + port_offset;
  366. struct hlist_node *node;
  367. struct inet_timewait_sock *tw = NULL;
  368. inet_get_local_port_range(&low, &high);
  369. remaining = (high - low) + 1;
  370. local_bh_disable();
  371. for (i = 1; i <= remaining; i++) {
  372. port = low + (i + offset) % remaining;
  373. head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
  374. spin_lock(&head->lock);
  375. /* Does not bother with rcv_saddr checks,
  376. * because the established check is already
  377. * unique enough.
  378. */
  379. inet_bind_bucket_for_each(tb, node, &head->chain) {
  380. if (tb->ib_net == net && tb->port == port) {
  381. BUG_TRAP(!hlist_empty(&tb->owners));
  382. if (tb->fastreuse >= 0)
  383. goto next_port;
  384. if (!check_established(death_row, sk,
  385. port, &tw))
  386. goto ok;
  387. goto next_port;
  388. }
  389. }
  390. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  391. net, head, port);
  392. if (!tb) {
  393. spin_unlock(&head->lock);
  394. break;
  395. }
  396. tb->fastreuse = -1;
  397. goto ok;
  398. next_port:
  399. spin_unlock(&head->lock);
  400. }
  401. local_bh_enable();
  402. return -EADDRNOTAVAIL;
  403. ok:
  404. hint += i;
  405. /* Head lock still held and bh's disabled */
  406. inet_bind_hash(sk, tb, port);
  407. if (sk_unhashed(sk)) {
  408. inet_sk(sk)->sport = htons(port);
  409. hash(sk);
  410. }
  411. spin_unlock(&head->lock);
  412. if (tw) {
  413. inet_twsk_deschedule(tw, death_row);
  414. inet_twsk_put(tw);
  415. }
  416. ret = 0;
  417. goto out;
  418. }
  419. head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
  420. tb = inet_csk(sk)->icsk_bind_hash;
  421. spin_lock_bh(&head->lock);
  422. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  423. hash(sk);
  424. spin_unlock_bh(&head->lock);
  425. return 0;
  426. } else {
  427. spin_unlock(&head->lock);
  428. /* No definite answer... Walk to established hash table */
  429. ret = check_established(death_row, sk, snum, NULL);
  430. out:
  431. local_bh_enable();
  432. return ret;
  433. }
  434. }
  435. EXPORT_SYMBOL_GPL(__inet_hash_connect);
  436. /*
  437. * Bind a port for a connect operation and hash it.
  438. */
  439. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  440. struct sock *sk)
  441. {
  442. return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
  443. __inet_check_established, __inet_hash_nolisten);
  444. }
  445. EXPORT_SYMBOL_GPL(inet_hash_connect);