Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 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 | |
| 19 | #include <net/ipv6.h> |
| 20 | #include <linux/sunrpc/clnt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 23 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 24 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 25 | |
| 26 | static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap, |
| 27 | char *buf, const int buflen) |
| 28 | { |
| 29 | const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; |
| 30 | const struct in6_addr *addr = &sin6->sin6_addr; |
| 31 | |
| 32 | /* |
| 33 | * RFC 4291, Section 2.2.2 |
| 34 | * |
| 35 | * Shorthanded ANY address |
| 36 | */ |
| 37 | if (ipv6_addr_any(addr)) |
| 38 | return snprintf(buf, buflen, "::"); |
| 39 | |
| 40 | /* |
| 41 | * RFC 4291, Section 2.2.2 |
| 42 | * |
| 43 | * Shorthanded loopback address |
| 44 | */ |
| 45 | if (ipv6_addr_loopback(addr)) |
| 46 | return snprintf(buf, buflen, "::1"); |
| 47 | |
| 48 | /* |
| 49 | * RFC 4291, Section 2.2.3 |
| 50 | * |
| 51 | * Special presentation address format for mapped v4 |
| 52 | * addresses. |
| 53 | */ |
| 54 | if (ipv6_addr_v4mapped(addr)) |
| 55 | return snprintf(buf, buflen, "::ffff:%pI4", |
| 56 | &addr->s6_addr32[3]); |
| 57 | |
| 58 | /* |
| 59 | * RFC 4291, Section 2.2.1 |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 60 | */ |
Chuck Lever | dd1fd90 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 61 | return snprintf(buf, buflen, "%pI6c", addr); |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static size_t rpc_ntop6(const struct sockaddr *sap, |
| 65 | char *buf, const size_t buflen) |
| 66 | { |
| 67 | const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; |
| 68 | char scopebuf[IPV6_SCOPE_ID_LEN]; |
| 69 | size_t len; |
| 70 | int rc; |
| 71 | |
| 72 | len = rpc_ntop6_noscopeid(sap, buf, buflen); |
| 73 | if (unlikely(len == 0)) |
| 74 | return len; |
| 75 | |
Chuck Lever | f1a89a1 | 2010-01-12 17:41:10 -0500 | [diff] [blame] | 76 | if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)) |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 77 | return len; |
Chuck Lever | 7a88efe | 2010-01-12 17:41:20 -0500 | [diff] [blame] | 78 | if (sin6->sin6_scope_id == 0) |
| 79 | return len; |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 80 | |
| 81 | rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u", |
| 82 | IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id); |
| 83 | if (unlikely((size_t)rc > sizeof(scopebuf))) |
| 84 | return 0; |
| 85 | |
| 86 | len += rc; |
| 87 | if (unlikely(len > buflen)) |
| 88 | return 0; |
| 89 | |
| 90 | strcat(buf, scopebuf); |
| 91 | return len; |
| 92 | } |
| 93 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 94 | #else /* !IS_ENABLED(CONFIG_IPV6) */ |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 95 | |
| 96 | static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap, |
| 97 | char *buf, const int buflen) |
| 98 | { |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static size_t rpc_ntop6(const struct sockaddr *sap, |
| 103 | char *buf, const size_t buflen) |
| 104 | { |
| 105 | return 0; |
| 106 | } |
| 107 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 108 | #endif /* !IS_ENABLED(CONFIG_IPV6) */ |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 109 | |
| 110 | static int rpc_ntop4(const struct sockaddr *sap, |
| 111 | char *buf, const size_t buflen) |
| 112 | { |
| 113 | const struct sockaddr_in *sin = (struct sockaddr_in *)sap; |
| 114 | |
| 115 | return snprintf(buf, buflen, "%pI4", &sin->sin_addr); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * rpc_ntop - construct a presentation address in @buf |
| 120 | * @sap: socket address |
| 121 | * @buf: construction area |
| 122 | * @buflen: size of @buf, in bytes |
| 123 | * |
| 124 | * Plants a %NUL-terminated string in @buf and returns the length |
| 125 | * of the string, excluding the %NUL. Otherwise zero is returned. |
| 126 | */ |
| 127 | size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen) |
| 128 | { |
| 129 | switch (sap->sa_family) { |
| 130 | case AF_INET: |
| 131 | return rpc_ntop4(sap, buf, buflen); |
| 132 | case AF_INET6: |
| 133 | return rpc_ntop6(sap, buf, buflen); |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(rpc_ntop); |
| 139 | |
| 140 | static size_t rpc_pton4(const char *buf, const size_t buflen, |
| 141 | struct sockaddr *sap, const size_t salen) |
| 142 | { |
| 143 | struct sockaddr_in *sin = (struct sockaddr_in *)sap; |
| 144 | u8 *addr = (u8 *)&sin->sin_addr.s_addr; |
| 145 | |
| 146 | if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in)) |
| 147 | return 0; |
| 148 | |
| 149 | memset(sap, 0, sizeof(struct sockaddr_in)); |
| 150 | |
| 151 | if (in4_pton(buf, buflen, addr, '\0', NULL) == 0) |
| 152 | return 0; |
| 153 | |
| 154 | sin->sin_family = AF_INET; |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 155 | return sizeof(struct sockaddr_in); |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 158 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 159 | static int rpc_parse_scope_id(const char *buf, const size_t buflen, |
| 160 | const char *delim, struct sockaddr_in6 *sin6) |
| 161 | { |
| 162 | char *p; |
| 163 | size_t len; |
| 164 | |
| 165 | if ((buf + buflen) == delim) |
| 166 | return 1; |
| 167 | |
| 168 | if (*delim != IPV6_SCOPE_DELIMITER) |
| 169 | return 0; |
| 170 | |
Chuck Lever | f1a89a1 | 2010-01-12 17:41:10 -0500 | [diff] [blame] | 171 | if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)) |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 172 | return 0; |
| 173 | |
| 174 | len = (buf + buflen) - delim - 1; |
| 175 | p = kstrndup(delim + 1, len, GFP_KERNEL); |
| 176 | if (p) { |
| 177 | unsigned long scope_id = 0; |
| 178 | struct net_device *dev; |
| 179 | |
| 180 | dev = dev_get_by_name(&init_net, p); |
| 181 | if (dev != NULL) { |
| 182 | scope_id = dev->ifindex; |
| 183 | dev_put(dev); |
| 184 | } else { |
| 185 | if (strict_strtoul(p, 10, &scope_id) == 0) { |
| 186 | kfree(p); |
| 187 | return 0; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | kfree(p); |
| 192 | |
| 193 | sin6->sin6_scope_id = scope_id; |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static size_t rpc_pton6(const char *buf, const size_t buflen, |
| 201 | struct sockaddr *sap, const size_t salen) |
| 202 | { |
| 203 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; |
| 204 | u8 *addr = (u8 *)&sin6->sin6_addr.in6_u; |
| 205 | const char *delim; |
| 206 | |
| 207 | if (buflen > (INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN) || |
| 208 | salen < sizeof(struct sockaddr_in6)) |
| 209 | return 0; |
| 210 | |
| 211 | memset(sap, 0, sizeof(struct sockaddr_in6)); |
| 212 | |
| 213 | if (in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0) |
| 214 | return 0; |
| 215 | |
| 216 | if (!rpc_parse_scope_id(buf, buflen, delim, sin6)) |
| 217 | return 0; |
| 218 | |
| 219 | sin6->sin6_family = AF_INET6; |
| 220 | return sizeof(struct sockaddr_in6); |
| 221 | } |
| 222 | #else |
| 223 | static size_t rpc_pton6(const char *buf, const size_t buflen, |
| 224 | struct sockaddr *sap, const size_t salen) |
| 225 | { |
| 226 | return 0; |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | /** |
| 231 | * rpc_pton - Construct a sockaddr in @sap |
| 232 | * @buf: C string containing presentation format IP address |
| 233 | * @buflen: length of presentation address in bytes |
| 234 | * @sap: buffer into which to plant socket address |
| 235 | * @salen: size of buffer in bytes |
| 236 | * |
| 237 | * Returns the size of the socket address if successful; otherwise |
| 238 | * zero is returned. |
| 239 | * |
| 240 | * Plants a socket address in @sap and returns the size of the |
| 241 | * socket address, if successful. Returns zero if an error |
| 242 | * occurred. |
| 243 | */ |
| 244 | size_t rpc_pton(const char *buf, const size_t buflen, |
| 245 | struct sockaddr *sap, const size_t salen) |
| 246 | { |
| 247 | unsigned int i; |
| 248 | |
| 249 | for (i = 0; i < buflen; i++) |
| 250 | if (buf[i] == ':') |
| 251 | return rpc_pton6(buf, buflen, sap, salen); |
| 252 | return rpc_pton4(buf, buflen, sap, salen); |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(rpc_pton); |
| 255 | |
| 256 | /** |
| 257 | * rpc_sockaddr2uaddr - Construct a universal address string from @sap. |
| 258 | * @sap: socket address |
Trond Myklebust | d77385f | 2011-10-17 16:08:10 -0700 | [diff] [blame] | 259 | * @gfp_flags: allocation mode |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 260 | * |
| 261 | * Returns a %NUL-terminated string in dynamically allocated memory; |
| 262 | * otherwise NULL is returned if an error occurred. Caller must |
| 263 | * free the returned string. |
| 264 | */ |
Trond Myklebust | d77385f | 2011-10-17 16:08:10 -0700 | [diff] [blame] | 265 | char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags) |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 266 | { |
| 267 | char portbuf[RPCBIND_MAXUADDRPLEN]; |
| 268 | char addrbuf[RPCBIND_MAXUADDRLEN]; |
| 269 | unsigned short port; |
| 270 | |
| 271 | switch (sap->sa_family) { |
| 272 | case AF_INET: |
| 273 | if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0) |
| 274 | return NULL; |
| 275 | port = ntohs(((struct sockaddr_in *)sap)->sin_port); |
| 276 | break; |
| 277 | case AF_INET6: |
| 278 | if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0) |
| 279 | return NULL; |
| 280 | port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port); |
| 281 | break; |
| 282 | default: |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | if (snprintf(portbuf, sizeof(portbuf), |
| 287 | ".%u.%u", port >> 8, port & 0xff) > (int)sizeof(portbuf)) |
| 288 | return NULL; |
| 289 | |
| 290 | if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) > sizeof(addrbuf)) |
| 291 | return NULL; |
| 292 | |
Trond Myklebust | d77385f | 2011-10-17 16:08:10 -0700 | [diff] [blame] | 293 | return kstrdup(addrbuf, gfp_flags); |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 294 | } |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * rpc_uaddr2sockaddr - convert a universal address to a socket address. |
| 298 | * @uaddr: C string containing universal address to convert |
| 299 | * @uaddr_len: length of universal address string |
| 300 | * @sap: buffer into which to plant socket address |
| 301 | * @salen: size of buffer |
| 302 | * |
Chuck Lever | 1e360a6 | 2009-11-13 10:52:55 -0500 | [diff] [blame] | 303 | * @uaddr does not have to be '\0'-terminated, but strict_strtoul() and |
| 304 | * rpc_pton() require proper string termination to be successful. |
| 305 | * |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 306 | * Returns the size of the socket address if successful; otherwise |
| 307 | * zero is returned. |
| 308 | */ |
| 309 | size_t rpc_uaddr2sockaddr(const char *uaddr, const size_t uaddr_len, |
| 310 | struct sockaddr *sap, const size_t salen) |
| 311 | { |
Chuck Lever | 1e360a6 | 2009-11-13 10:52:55 -0500 | [diff] [blame] | 312 | char *c, buf[RPCBIND_MAXUADDRLEN + sizeof('\0')]; |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 313 | unsigned long portlo, porthi; |
| 314 | unsigned short port; |
| 315 | |
Chuck Lever | 1e360a6 | 2009-11-13 10:52:55 -0500 | [diff] [blame] | 316 | if (uaddr_len > RPCBIND_MAXUADDRLEN) |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 317 | return 0; |
| 318 | |
| 319 | memcpy(buf, uaddr, uaddr_len); |
| 320 | |
Chuck Lever | 1e360a6 | 2009-11-13 10:52:55 -0500 | [diff] [blame] | 321 | buf[uaddr_len] = '\0'; |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 322 | c = strrchr(buf, '.'); |
| 323 | if (unlikely(c == NULL)) |
| 324 | return 0; |
| 325 | if (unlikely(strict_strtoul(c + 1, 10, &portlo) != 0)) |
| 326 | return 0; |
| 327 | if (unlikely(portlo > 255)) |
| 328 | return 0; |
| 329 | |
Chuck Lever | 1e360a6 | 2009-11-13 10:52:55 -0500 | [diff] [blame] | 330 | *c = '\0'; |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 331 | c = strrchr(buf, '.'); |
| 332 | if (unlikely(c == NULL)) |
| 333 | return 0; |
| 334 | if (unlikely(strict_strtoul(c + 1, 10, &porthi) != 0)) |
| 335 | return 0; |
| 336 | if (unlikely(porthi > 255)) |
| 337 | return 0; |
| 338 | |
| 339 | port = (unsigned short)((porthi << 8) | portlo); |
| 340 | |
Chuck Lever | 1e360a6 | 2009-11-13 10:52:55 -0500 | [diff] [blame] | 341 | *c = '\0'; |
Chuck Lever | a02d692 | 2009-08-09 15:09:34 -0400 | [diff] [blame] | 342 | if (rpc_pton(buf, strlen(buf), sap, salen) == 0) |
| 343 | return 0; |
| 344 | |
| 345 | switch (sap->sa_family) { |
| 346 | case AF_INET: |
| 347 | ((struct sockaddr_in *)sap)->sin_port = htons(port); |
| 348 | return sizeof(struct sockaddr_in); |
| 349 | case AF_INET6: |
| 350 | ((struct sockaddr_in6 *)sap)->sin6_port = htons(port); |
| 351 | return sizeof(struct sockaddr_in6); |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr); |