addr.c 8.7 KB

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