inet6_hashtables.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 _INET6_HASHTABLES_H
  14. #define _INET6_HASHTABLES_H
  15. #include <linux/config.h>
  16. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  17. #include <linux/in6.h>
  18. #include <linux/ipv6.h>
  19. #include <linux/types.h>
  20. #include <net/ipv6.h>
  21. struct inet_hashinfo;
  22. /* I have no idea if this is a good hash for v6 or not. -DaveM */
  23. static inline unsigned int inet6_ehashfn(const struct in6_addr *laddr, const u16 lport,
  24. const struct in6_addr *faddr, const u16 fport)
  25. {
  26. unsigned int hashent = (lport ^ fport);
  27. hashent ^= (laddr->s6_addr32[3] ^ faddr->s6_addr32[3]);
  28. hashent ^= hashent >> 16;
  29. hashent ^= hashent >> 8;
  30. return hashent;
  31. }
  32. static inline int inet6_sk_ehashfn(const struct sock *sk)
  33. {
  34. const struct inet_sock *inet = inet_sk(sk);
  35. const struct ipv6_pinfo *np = inet6_sk(sk);
  36. const struct in6_addr *laddr = &np->rcv_saddr;
  37. const struct in6_addr *faddr = &np->daddr;
  38. const __u16 lport = inet->num;
  39. const __u16 fport = inet->dport;
  40. return inet6_ehashfn(laddr, lport, faddr, fport);
  41. }
  42. static inline void __inet6_hash(struct inet_hashinfo *hashinfo,
  43. struct sock *sk)
  44. {
  45. struct hlist_head *list;
  46. rwlock_t *lock;
  47. BUG_TRAP(sk_unhashed(sk));
  48. if (sk->sk_state == TCP_LISTEN) {
  49. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  50. lock = &hashinfo->lhash_lock;
  51. inet_listen_wlock(hashinfo);
  52. } else {
  53. unsigned int hash;
  54. sk->sk_hash = hash = inet6_sk_ehashfn(sk);
  55. hash &= (hashinfo->ehash_size - 1);
  56. list = &hashinfo->ehash[hash].chain;
  57. lock = &hashinfo->ehash[hash].lock;
  58. write_lock(lock);
  59. }
  60. __sk_add_node(sk, list);
  61. sock_prot_inc_use(sk->sk_prot);
  62. write_unlock(lock);
  63. }
  64. /*
  65. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  66. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  67. *
  68. * The sockhash lock must be held as a reader here.
  69. */
  70. static inline struct sock *
  71. __inet6_lookup_established(struct inet_hashinfo *hashinfo,
  72. const struct in6_addr *saddr,
  73. const u16 sport,
  74. const struct in6_addr *daddr,
  75. const u16 hnum,
  76. const int dif)
  77. {
  78. struct sock *sk;
  79. const struct hlist_node *node;
  80. const __u32 ports = INET_COMBINED_PORTS(sport, hnum);
  81. /* Optimize here for direct hit, only listening connections can
  82. * have wildcards anyways.
  83. */
  84. unsigned int hash = inet6_ehashfn(daddr, hnum, saddr, sport);
  85. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  86. prefetch(head->chain.first);
  87. read_lock(&head->lock);
  88. sk_for_each(sk, node, &head->chain) {
  89. /* For IPV6 do the cheaper port and family tests first. */
  90. if (INET6_MATCH(sk, hash, saddr, daddr, ports, dif))
  91. goto hit; /* You sunk my battleship! */
  92. }
  93. /* Must check for a TIME_WAIT'er before going to listener hash. */
  94. sk_for_each(sk, node, &(head + hashinfo->ehash_size)->chain) {
  95. const struct inet_timewait_sock *tw = inet_twsk(sk);
  96. if(*((__u32 *)&(tw->tw_dport)) == ports &&
  97. sk->sk_family == PF_INET6) {
  98. const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
  99. if (ipv6_addr_equal(&tw6->tw_v6_daddr, saddr) &&
  100. ipv6_addr_equal(&tw6->tw_v6_rcv_saddr, daddr) &&
  101. (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif))
  102. goto hit;
  103. }
  104. }
  105. read_unlock(&head->lock);
  106. return NULL;
  107. hit:
  108. sock_hold(sk);
  109. read_unlock(&head->lock);
  110. return sk;
  111. }
  112. extern struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
  113. const struct in6_addr *daddr,
  114. const unsigned short hnum,
  115. const int dif);
  116. static inline struct sock *__inet6_lookup(struct inet_hashinfo *hashinfo,
  117. const struct in6_addr *saddr,
  118. const u16 sport,
  119. const struct in6_addr *daddr,
  120. const u16 hnum,
  121. const int dif)
  122. {
  123. struct sock *sk = __inet6_lookup_established(hashinfo, saddr, sport,
  124. daddr, hnum, dif);
  125. if (sk)
  126. return sk;
  127. return inet6_lookup_listener(hashinfo, daddr, hnum, dif);
  128. }
  129. extern struct sock *inet6_lookup(struct inet_hashinfo *hashinfo,
  130. const struct in6_addr *saddr, const u16 sport,
  131. const struct in6_addr *daddr, const u16 dport,
  132. const int dif);
  133. #endif /* defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) */
  134. #endif /* _INET6_HASHTABLES_H */