inet6_hashtables.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #if IS_ENABLED(CONFIG_IPV6)
  16. #include <linux/in6.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/types.h>
  19. #include <linux/jhash.h>
  20. #include <net/inet_sock.h>
  21. #include <net/ipv6.h>
  22. #include <net/netns/hash.h>
  23. struct inet_hashinfo;
  24. static inline unsigned int __inet6_ehashfn(const u32 lhash,
  25. const u16 lport,
  26. const u32 fhash,
  27. const __be16 fport,
  28. const u32 initval)
  29. {
  30. const u32 ports = (((u32)lport) << 16) | (__force u32)fport;
  31. return jhash_3words(lhash, fhash, ports, initval);
  32. }
  33. /*
  34. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  35. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  36. *
  37. * The sockhash lock must be held as a reader here.
  38. */
  39. struct sock *__inet6_lookup_established(struct net *net,
  40. struct inet_hashinfo *hashinfo,
  41. const struct in6_addr *saddr,
  42. const __be16 sport,
  43. const struct in6_addr *daddr,
  44. const u16 hnum, const int dif);
  45. struct sock *inet6_lookup_listener(struct net *net,
  46. struct inet_hashinfo *hashinfo,
  47. const struct in6_addr *saddr,
  48. const __be16 sport,
  49. const struct in6_addr *daddr,
  50. const unsigned short hnum, const int dif);
  51. static inline struct sock *__inet6_lookup(struct net *net,
  52. struct inet_hashinfo *hashinfo,
  53. const struct in6_addr *saddr,
  54. const __be16 sport,
  55. const struct in6_addr *daddr,
  56. const u16 hnum,
  57. const int dif)
  58. {
  59. struct sock *sk = __inet6_lookup_established(net, hashinfo, saddr,
  60. sport, daddr, hnum, dif);
  61. if (sk)
  62. return sk;
  63. return inet6_lookup_listener(net, hashinfo, saddr, sport,
  64. daddr, hnum, dif);
  65. }
  66. static inline struct sock *__inet6_lookup_skb(struct inet_hashinfo *hashinfo,
  67. struct sk_buff *skb,
  68. const __be16 sport,
  69. const __be16 dport,
  70. int iif)
  71. {
  72. struct sock *sk = skb_steal_sock(skb);
  73. if (sk)
  74. return sk;
  75. return __inet6_lookup(dev_net(skb_dst(skb)->dev), hashinfo,
  76. &ipv6_hdr(skb)->saddr, sport,
  77. &ipv6_hdr(skb)->daddr, ntohs(dport),
  78. iif);
  79. }
  80. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  81. const struct in6_addr *saddr, const __be16 sport,
  82. const struct in6_addr *daddr, const __be16 dport,
  83. const int dif);
  84. #endif /* IS_ENABLED(CONFIG_IPV6) */
  85. #define INET6_MATCH(__sk, __net, __saddr, __daddr, __ports, __dif) \
  86. (((__sk)->sk_portpair == (__ports)) && \
  87. ((__sk)->sk_family == AF_INET6) && \
  88. ipv6_addr_equal(&(__sk)->sk_v6_daddr, (__saddr)) && \
  89. ipv6_addr_equal(&(__sk)->sk_v6_rcv_saddr, (__daddr)) && \
  90. (!(__sk)->sk_bound_dev_if || \
  91. ((__sk)->sk_bound_dev_if == (__dif))) && \
  92. net_eq(sock_net(__sk), (__net)))
  93. #endif /* _INET6_HASHTABLES_H */