inet_hashtables.c 14 KB

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