addr.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * linux/include/linux/sunrpc/addr.h
  3. *
  4. * Various routines for copying and comparing sockaddrs and for
  5. * converting them to and from presentation format.
  6. */
  7. #ifndef _LINUX_SUNRPC_ADDR_H
  8. #define _LINUX_SUNRPC_ADDR_H
  9. #include <linux/socket.h>
  10. #include <linux/in.h>
  11. #include <linux/in6.h>
  12. #include <net/ipv6.h>
  13. size_t rpc_ntop(const struct sockaddr *, char *, const size_t);
  14. size_t rpc_pton(struct net *, const char *, const size_t,
  15. struct sockaddr *, const size_t);
  16. char * rpc_sockaddr2uaddr(const struct sockaddr *, gfp_t);
  17. size_t rpc_uaddr2sockaddr(struct net *, const char *, const size_t,
  18. struct sockaddr *, const size_t);
  19. static inline unsigned short rpc_get_port(const struct sockaddr *sap)
  20. {
  21. switch (sap->sa_family) {
  22. case AF_INET:
  23. return ntohs(((struct sockaddr_in *)sap)->sin_port);
  24. case AF_INET6:
  25. return ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
  26. }
  27. return 0;
  28. }
  29. static inline void rpc_set_port(struct sockaddr *sap,
  30. const unsigned short port)
  31. {
  32. switch (sap->sa_family) {
  33. case AF_INET:
  34. ((struct sockaddr_in *)sap)->sin_port = htons(port);
  35. break;
  36. case AF_INET6:
  37. ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
  38. break;
  39. }
  40. }
  41. #define IPV6_SCOPE_DELIMITER '%'
  42. #define IPV6_SCOPE_ID_LEN sizeof("%nnnnnnnnnn")
  43. static inline bool rpc_cmp_addr4(const struct sockaddr *sap1,
  44. const struct sockaddr *sap2)
  45. {
  46. const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sap1;
  47. const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sap2;
  48. return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
  49. }
  50. static inline bool __rpc_copy_addr4(struct sockaddr *dst,
  51. const struct sockaddr *src)
  52. {
  53. const struct sockaddr_in *ssin = (struct sockaddr_in *) src;
  54. struct sockaddr_in *dsin = (struct sockaddr_in *) dst;
  55. dsin->sin_family = ssin->sin_family;
  56. dsin->sin_addr.s_addr = ssin->sin_addr.s_addr;
  57. return true;
  58. }
  59. #if IS_ENABLED(CONFIG_IPV6)
  60. static inline bool rpc_cmp_addr6(const struct sockaddr *sap1,
  61. const struct sockaddr *sap2)
  62. {
  63. const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sap1;
  64. const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sap2;
  65. if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
  66. return false;
  67. else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  68. return sin1->sin6_scope_id == sin2->sin6_scope_id;
  69. return true;
  70. }
  71. static inline bool __rpc_copy_addr6(struct sockaddr *dst,
  72. const struct sockaddr *src)
  73. {
  74. const struct sockaddr_in6 *ssin6 = (const struct sockaddr_in6 *) src;
  75. struct sockaddr_in6 *dsin6 = (struct sockaddr_in6 *) dst;
  76. dsin6->sin6_family = ssin6->sin6_family;
  77. dsin6->sin6_addr = ssin6->sin6_addr;
  78. dsin6->sin6_scope_id = ssin6->sin6_scope_id;
  79. return true;
  80. }
  81. #else /* !(IS_ENABLED(CONFIG_IPV6) */
  82. static inline bool rpc_cmp_addr6(const struct sockaddr *sap1,
  83. const struct sockaddr *sap2)
  84. {
  85. return false;
  86. }
  87. static inline bool __rpc_copy_addr6(struct sockaddr *dst,
  88. const struct sockaddr *src)
  89. {
  90. return false;
  91. }
  92. #endif /* !(IS_ENABLED(CONFIG_IPV6) */
  93. /**
  94. * rpc_cmp_addr - compare the address portion of two sockaddrs.
  95. * @sap1: first sockaddr
  96. * @sap2: second sockaddr
  97. *
  98. * Just compares the family and address portion. Ignores port, but
  99. * compares the scope if it's a link-local address.
  100. *
  101. * Returns true if the addrs are equal, false if they aren't.
  102. */
  103. static inline bool rpc_cmp_addr(const struct sockaddr *sap1,
  104. const struct sockaddr *sap2)
  105. {
  106. if (sap1->sa_family == sap2->sa_family) {
  107. switch (sap1->sa_family) {
  108. case AF_INET:
  109. return rpc_cmp_addr4(sap1, sap2);
  110. case AF_INET6:
  111. return rpc_cmp_addr6(sap1, sap2);
  112. }
  113. }
  114. return false;
  115. }
  116. /**
  117. * rpc_cmp_addr_port - compare the address and port number of two sockaddrs.
  118. * @sap1: first sockaddr
  119. * @sap2: second sockaddr
  120. */
  121. static inline bool rpc_cmp_addr_port(const struct sockaddr *sap1,
  122. const struct sockaddr *sap2)
  123. {
  124. if (!rpc_cmp_addr(sap1, sap2))
  125. return false;
  126. return rpc_get_port(sap1) == rpc_get_port(sap2);
  127. }
  128. /**
  129. * rpc_copy_addr - copy the address portion of one sockaddr to another
  130. * @dst: destination sockaddr
  131. * @src: source sockaddr
  132. *
  133. * Just copies the address portion and family. Ignores port, scope, etc.
  134. * Caller is responsible for making certain that dst is large enough to hold
  135. * the address in src. Returns true if address family is supported. Returns
  136. * false otherwise.
  137. */
  138. static inline bool rpc_copy_addr(struct sockaddr *dst,
  139. const struct sockaddr *src)
  140. {
  141. switch (src->sa_family) {
  142. case AF_INET:
  143. return __rpc_copy_addr4(dst, src);
  144. case AF_INET6:
  145. return __rpc_copy_addr6(dst, src);
  146. }
  147. return false;
  148. }
  149. /**
  150. * rpc_get_scope_id - return scopeid for a given sockaddr
  151. * @sa: sockaddr to get scopeid from
  152. *
  153. * Returns the value of the sin6_scope_id for AF_INET6 addrs, or 0 if
  154. * not an AF_INET6 address.
  155. */
  156. static inline u32 rpc_get_scope_id(const struct sockaddr *sa)
  157. {
  158. if (sa->sa_family != AF_INET6)
  159. return 0;
  160. return ((struct sockaddr_in6 *) sa)->sin6_scope_id;
  161. }
  162. #endif /* _LINUX_SUNRPC_ADDR_H */