inet6_hashtables.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 int inet6_ehashfn(const struct in6_addr *laddr, const u16 lport,
  24. const struct in6_addr *faddr, const u16 fport,
  25. const int ehash_size)
  26. {
  27. int hashent = (lport ^ fport);
  28. hashent ^= (laddr->s6_addr32[3] ^ faddr->s6_addr32[3]);
  29. hashent ^= hashent >> 16;
  30. hashent ^= hashent >> 8;
  31. return (hashent & (ehash_size - 1));
  32. }
  33. static inline int inet6_sk_ehashfn(const struct sock *sk, const int ehash_size)
  34. {
  35. const struct inet_sock *inet = inet_sk(sk);
  36. const struct ipv6_pinfo *np = inet6_sk(sk);
  37. const struct in6_addr *laddr = &np->rcv_saddr;
  38. const struct in6_addr *faddr = &np->daddr;
  39. const __u16 lport = inet->num;
  40. const __u16 fport = inet->dport;
  41. return inet6_ehashfn(laddr, lport, faddr, fport, ehash_size);
  42. }
  43. /*
  44. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  45. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  46. *
  47. * The sockhash lock must be held as a reader here.
  48. */
  49. static inline struct sock *
  50. __inet6_lookup_established(struct inet_hashinfo *hashinfo,
  51. const struct in6_addr *saddr,
  52. const u16 sport,
  53. const struct in6_addr *daddr,
  54. const u16 hnum,
  55. const int dif)
  56. {
  57. struct sock *sk;
  58. const struct hlist_node *node;
  59. const __u32 ports = INET_COMBINED_PORTS(sport, hnum);
  60. /* Optimize here for direct hit, only listening connections can
  61. * have wildcards anyways.
  62. */
  63. const int hash = inet6_ehashfn(daddr, hnum, saddr, sport,
  64. hashinfo->ehash_size);
  65. struct inet_ehash_bucket *head = &hashinfo->ehash[hash];
  66. read_lock(&head->lock);
  67. sk_for_each(sk, node, &head->chain) {
  68. /* For IPV6 do the cheaper port and family tests first. */
  69. if (INET6_MATCH(sk, saddr, daddr, ports, dif))
  70. goto hit; /* You sunk my battleship! */
  71. }
  72. /* Must check for a TIME_WAIT'er before going to listener hash. */
  73. sk_for_each(sk, node, &(head + hashinfo->ehash_size)->chain) {
  74. const struct inet_timewait_sock *tw = inet_twsk(sk);
  75. if(*((__u32 *)&(tw->tw_dport)) == ports &&
  76. sk->sk_family == PF_INET6) {
  77. const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
  78. if (ipv6_addr_equal(&tcp6tw->tw_v6_daddr, saddr) &&
  79. ipv6_addr_equal(&tcp6tw->tw_v6_rcv_saddr, daddr) &&
  80. (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif))
  81. goto hit;
  82. }
  83. }
  84. read_unlock(&head->lock);
  85. return NULL;
  86. hit:
  87. sock_hold(sk);
  88. read_unlock(&head->lock);
  89. return sk;
  90. }
  91. extern struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
  92. const struct in6_addr *daddr,
  93. const unsigned short hnum,
  94. const int dif);
  95. static inline struct sock *__inet6_lookup(struct inet_hashinfo *hashinfo,
  96. const struct in6_addr *saddr,
  97. const u16 sport,
  98. const struct in6_addr *daddr,
  99. const u16 hnum,
  100. const int dif)
  101. {
  102. struct sock *sk = __inet6_lookup_established(hashinfo, saddr, sport,
  103. daddr, hnum, dif);
  104. if (sk)
  105. return sk;
  106. return inet6_lookup_listener(hashinfo, daddr, hnum, dif);
  107. }
  108. extern struct sock *inet6_lookup(struct inet_hashinfo *hashinfo,
  109. const struct in6_addr *saddr, const u16 sport,
  110. const struct in6_addr *daddr, const u16 dport,
  111. const int dif);
  112. #endif /* defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) */
  113. #endif /* _INET6_HASHTABLES_H */