inet6_hashtables.h 4.0 KB

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