inet6_hashtables.h 3.7 KB

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