inet6_hashtables.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. struct sk_buff *skb, int doff,
  48. const struct in6_addr *saddr,
  49. const __be16 sport,
  50. const struct in6_addr *daddr,
  51. const unsigned short hnum, const int dif);
  52. static inline struct sock *__inet6_lookup(struct net *net,
  53. struct inet_hashinfo *hashinfo,
  54. struct sk_buff *skb, int doff,
  55. const struct in6_addr *saddr,
  56. const __be16 sport,
  57. const struct in6_addr *daddr,
  58. const u16 hnum,
  59. const int dif,
  60. bool *refcounted)
  61. {
  62. struct sock *sk = __inet6_lookup_established(net, hashinfo, saddr,
  63. sport, daddr, hnum, dif);
  64. *refcounted = true;
  65. if (sk)
  66. return sk;
  67. *refcounted = false;
  68. return inet6_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
  69. daddr, hnum, dif);
  70. }
  71. static inline struct sock *__inet6_lookup_skb(struct inet_hashinfo *hashinfo,
  72. struct sk_buff *skb, int doff,
  73. const __be16 sport,
  74. const __be16 dport,
  75. int iif,
  76. bool *refcounted)
  77. {
  78. struct sock *sk = skb_steal_sock(skb);
  79. *refcounted = true;
  80. if (sk)
  81. return sk;
  82. return __inet6_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
  83. doff, &ipv6_hdr(skb)->saddr, sport,
  84. &ipv6_hdr(skb)->daddr, ntohs(dport),
  85. iif, refcounted);
  86. }
  87. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  88. struct sk_buff *skb, int doff,
  89. const struct in6_addr *saddr, const __be16 sport,
  90. const struct in6_addr *daddr, const __be16 dport,
  91. const int dif);
  92. int inet6_hash(struct sock *sk);
  93. #endif /* IS_ENABLED(CONFIG_IPV6) */
  94. #define INET6_MATCH(__sk, __net, __saddr, __daddr, __ports, __dif) \
  95. (((__sk)->sk_portpair == (__ports)) && \
  96. ((__sk)->sk_family == AF_INET6) && \
  97. ipv6_addr_equal(&(__sk)->sk_v6_daddr, (__saddr)) && \
  98. ipv6_addr_equal(&(__sk)->sk_v6_rcv_saddr, (__daddr)) && \
  99. (!(__sk)->sk_bound_dev_if || \
  100. ((__sk)->sk_bound_dev_if == (__dif))) && \
  101. net_eq(sock_net(__sk), (__net)))
  102. #endif /* _INET6_HASHTABLES_H */