inet_hashtables.c 13 KB

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