addr.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2009, Oracle. All rights reserved.
  4. *
  5. * Convert socket addresses to presentation addresses and universal
  6. * addresses, and vice versa.
  7. *
  8. * Universal addresses are introduced by RFC 1833 and further refined by
  9. * recent RFCs describing NFSv4. The universal address format is part
  10. * of the external (network) interface provided by rpcbind version 3
  11. * and 4, and by NFSv4. Such an address is a string containing a
  12. * presentation format IP address followed by a port number in
  13. * "hibyte.lobyte" format.
  14. *
  15. * IPv6 addresses can also include a scope ID, typically denoted by
  16. * a '%' followed by a device name or a non-negative integer. Refer to
  17. * RFC 4291, Section 2.2 for details on IPv6 presentation formats.
  18. */
  19. #include <net/ipv6.h>
  20. #include <linux/sunrpc/addr.h>
  21. #include <linux/sunrpc/msg_prot.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #if IS_ENABLED(CONFIG_IPV6)
  25. static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
  26. char *buf, const int buflen)
  27. {
  28. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  29. const struct in6_addr *addr = &sin6->sin6_addr;
  30. /*
  31. * RFC 4291, Section 2.2.2
  32. *
  33. * Shorthanded ANY address
  34. */
  35. if (ipv6_addr_any(addr))
  36. return snprintf(buf, buflen, "::");
  37. /*
  38. * RFC 4291, Section 2.2.2
  39. *
  40. * Shorthanded loopback address
  41. */
  42. if (ipv6_addr_loopback(addr))
  43. return snprintf(buf, buflen, "::1");
  44. /*
  45. * RFC 4291, Section 2.2.3
  46. *
  47. * Special presentation address format for mapped v4
  48. * addresses.
  49. */
  50. if (ipv6_addr_v4mapped(addr))
  51. return snprintf(buf, buflen, "::ffff:%pI4",
  52. &addr->s6_addr32[3]);
  53. /*
  54. * RFC 4291, Section 2.2.1
  55. */
  56. return snprintf(buf, buflen, "%pI6c", addr);
  57. }
  58. static size_t rpc_ntop6(const struct sockaddr *sap,
  59. char *buf, const size_t buflen)
  60. {
  61. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  62. char scopebuf[IPV6_SCOPE_ID_LEN];
  63. size_t len;
  64. int rc;
  65. len = rpc_ntop6_noscopeid(sap, buf, buflen);
  66. if (unlikely(len == 0))
  67. return len;
  68. if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
  69. return len;
  70. if (sin6->sin6_scope_id == 0)
  71. return len;
  72. rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
  73. IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
  74. if (unlikely((size_t)rc >= sizeof(scopebuf)))
  75. return 0;
  76. len += rc;
  77. if (unlikely(len >= buflen))
  78. return 0;
  79. strcat(buf, scopebuf);
  80. return len;
  81. }
  82. #else /* !IS_ENABLED(CONFIG_IPV6) */
  83. static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
  84. char *buf, const int buflen)
  85. {
  86. return 0;
  87. }
  88. static size_t rpc_ntop6(const struct sockaddr *sap,
  89. char *buf, const size_t buflen)
  90. {
  91. return 0;
  92. }
  93. #endif /* !IS_ENABLED(CONFIG_IPV6) */
  94. static int rpc_ntop4(const struct sockaddr *sap,
  95. char *buf, const size_t buflen)
  96. {
  97. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  98. return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
  99. }
  100. /**
  101. * rpc_ntop - construct a presentation address in @buf
  102. * @sap: socket address
  103. * @buf: construction area
  104. * @buflen: size of @buf, in bytes
  105. *
  106. * Plants a %NUL-terminated string in @buf and returns the length
  107. * of the string, excluding the %NUL. Otherwise zero is returned.
  108. */
  109. size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
  110. {
  111. switch (sap->sa_family) {
  112. case AF_INET:
  113. return rpc_ntop4(sap, buf, buflen);
  114. case AF_INET6:
  115. return rpc_ntop6(sap, buf, buflen);
  116. }
  117. return 0;
  118. }
  119. EXPORT_SYMBOL_GPL(rpc_ntop);
  120. static size_t rpc_pton4(const char *buf, const size_t buflen,
  121. struct sockaddr *sap, const size_t salen)
  122. {
  123. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  124. u8 *addr = (u8 *)&sin->sin_addr.s_addr;
  125. if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in))
  126. return 0;
  127. memset(sap, 0, sizeof(struct sockaddr_in));
  128. if (in4_pton(buf, buflen, addr, '\0', NULL) == 0)
  129. return 0;
  130. sin->sin_family = AF_INET;
  131. return sizeof(struct sockaddr_in);
  132. }
  133. #if IS_ENABLED(CONFIG_IPV6)
  134. static int rpc_parse_scope_id(struct net *net, const char *buf,
  135. const size_t buflen, const char *delim,
  136. struct sockaddr_in6 *sin6)
  137. {
  138. char p[IPV6_SCOPE_ID_LEN + 1];
  139. size_t len;
  140. u32 scope_id = 0;
  141. struct net_device *dev;
  142. if ((buf + buflen) == delim)
  143. return 1;
  144. if (*delim != IPV6_SCOPE_DELIMITER)
  145. return 0;
  146. if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
  147. return 0;
  148. len = (buf + buflen) - delim - 1;
  149. if (len > IPV6_SCOPE_ID_LEN)
  150. return 0;
  151. memcpy(p, delim + 1, len);
  152. p[len] = 0;
  153. dev = dev_get_by_name(net, p);
  154. if (dev != NULL) {
  155. scope_id = dev->ifindex;
  156. dev_put(dev);
  157. } else {
  158. if (kstrtou32(p, 10, &scope_id) != 0)
  159. return 0;
  160. }
  161. sin6->sin6_scope_id = scope_id;
  162. return 1;
  163. }
  164. static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
  165. struct sockaddr *sap, const size_t salen)
  166. {
  167. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  168. u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
  169. const char *delim;
  170. if (buflen > (INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN) ||
  171. salen < sizeof(struct sockaddr_in6))
  172. return 0;
  173. memset(sap, 0, sizeof(struct sockaddr_in6));
  174. if (in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0)
  175. return 0;
  176. if (!rpc_parse_scope_id(net, buf, buflen, delim, sin6))
  177. return 0;
  178. sin6->sin6_family = AF_INET6;
  179. return sizeof(struct sockaddr_in6);
  180. }
  181. #else
  182. static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
  183. struct sockaddr *sap, const size_t salen)
  184. {
  185. return 0;
  186. }
  187. #endif
  188. /**
  189. * rpc_pton - Construct a sockaddr in @sap
  190. * @net: applicable network namespace
  191. * @buf: C string containing presentation format IP address
  192. * @buflen: length of presentation address in bytes
  193. * @sap: buffer into which to plant socket address
  194. * @salen: size of buffer in bytes
  195. *
  196. * Returns the size of the socket address if successful; otherwise
  197. * zero is returned.
  198. *
  199. * Plants a socket address in @sap and returns the size of the
  200. * socket address, if successful. Returns zero if an error
  201. * occurred.
  202. */
  203. size_t rpc_pton(struct net *net, const char *buf, const size_t buflen,
  204. struct sockaddr *sap, const size_t salen)
  205. {
  206. unsigned int i;
  207. for (i = 0; i < buflen; i++)
  208. if (buf[i] == ':')
  209. return rpc_pton6(net, buf, buflen, sap, salen);
  210. return rpc_pton4(buf, buflen, sap, salen);
  211. }
  212. EXPORT_SYMBOL_GPL(rpc_pton);
  213. /**
  214. * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
  215. * @sap: socket address
  216. * @gfp_flags: allocation mode
  217. *
  218. * Returns a %NUL-terminated string in dynamically allocated memory;
  219. * otherwise NULL is returned if an error occurred. Caller must
  220. * free the returned string.
  221. */
  222. char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags)
  223. {
  224. char portbuf[RPCBIND_MAXUADDRPLEN];
  225. char addrbuf[RPCBIND_MAXUADDRLEN];
  226. unsigned short port;
  227. switch (sap->sa_family) {
  228. case AF_INET:
  229. if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
  230. return NULL;
  231. port = ntohs(((struct sockaddr_in *)sap)->sin_port);
  232. break;
  233. case AF_INET6:
  234. if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
  235. return NULL;
  236. port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
  237. break;
  238. default:
  239. return NULL;
  240. }
  241. if (snprintf(portbuf, sizeof(portbuf),
  242. ".%u.%u", port >> 8, port & 0xff) > (int)sizeof(portbuf))
  243. return NULL;
  244. if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) > sizeof(addrbuf))
  245. return NULL;
  246. return kstrdup(addrbuf, gfp_flags);
  247. }
  248. /**
  249. * rpc_uaddr2sockaddr - convert a universal address to a socket address.
  250. * @net: applicable network namespace
  251. * @uaddr: C string containing universal address to convert
  252. * @uaddr_len: length of universal address string
  253. * @sap: buffer into which to plant socket address
  254. * @salen: size of buffer
  255. *
  256. * @uaddr does not have to be '\0'-terminated, but kstrtou8() and
  257. * rpc_pton() require proper string termination to be successful.
  258. *
  259. * Returns the size of the socket address if successful; otherwise
  260. * zero is returned.
  261. */
  262. size_t rpc_uaddr2sockaddr(struct net *net, const char *uaddr,
  263. const size_t uaddr_len, struct sockaddr *sap,
  264. const size_t salen)
  265. {
  266. char *c, buf[RPCBIND_MAXUADDRLEN + sizeof('\0')];
  267. u8 portlo, porthi;
  268. unsigned short port;
  269. if (uaddr_len > RPCBIND_MAXUADDRLEN)
  270. return 0;
  271. memcpy(buf, uaddr, uaddr_len);
  272. buf[uaddr_len] = '\0';
  273. c = strrchr(buf, '.');
  274. if (unlikely(c == NULL))
  275. return 0;
  276. if (unlikely(kstrtou8(c + 1, 10, &portlo) != 0))
  277. return 0;
  278. *c = '\0';
  279. c = strrchr(buf, '.');
  280. if (unlikely(c == NULL))
  281. return 0;
  282. if (unlikely(kstrtou8(c + 1, 10, &porthi) != 0))
  283. return 0;
  284. port = (unsigned short)((porthi << 8) | portlo);
  285. *c = '\0';
  286. if (rpc_pton(net, buf, strlen(buf), sap, salen) == 0)
  287. return 0;
  288. switch (sap->sa_family) {
  289. case AF_INET:
  290. ((struct sockaddr_in *)sap)->sin_port = htons(port);
  291. return sizeof(struct sockaddr_in);
  292. case AF_INET6:
  293. ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
  294. return sizeof(struct sockaddr_in6);
  295. }
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr);