2 * Copyright 2009, Oracle. All rights reserved.
4 * Convert socket addresses to presentation addresses and universal
5 * addresses, and vice versa.
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.
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.
20 #include <linux/sunrpc/clnt.h>
22 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
24 static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
25 char *buf, const int buflen)
27 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
28 const struct in6_addr *addr = &sin6->sin6_addr;
31 * RFC 4291, Section 2.2.2
33 * Shorthanded ANY address
35 if (ipv6_addr_any(addr))
36 return snprintf(buf, buflen, "::");
39 * RFC 4291, Section 2.2.2
41 * Shorthanded loopback address
43 if (ipv6_addr_loopback(addr))
44 return snprintf(buf, buflen, "::1");
47 * RFC 4291, Section 2.2.3
49 * Special presentation address format for mapped v4
52 if (ipv6_addr_v4mapped(addr))
53 return snprintf(buf, buflen, "::ffff:%pI4",
57 * RFC 4291, Section 2.2.1
59 * To keep the result as short as possible, especially
60 * since we don't shorthand, we don't want leading zeros
61 * in each halfword, so avoid %pI6.
63 return snprintf(buf, buflen, "%x:%x:%x:%x:%x:%x:%x:%x",
64 ntohs(addr->s6_addr16[0]), ntohs(addr->s6_addr16[1]),
65 ntohs(addr->s6_addr16[2]), ntohs(addr->s6_addr16[3]),
66 ntohs(addr->s6_addr16[4]), ntohs(addr->s6_addr16[5]),
67 ntohs(addr->s6_addr16[6]), ntohs(addr->s6_addr16[7]));
70 static size_t rpc_ntop6(const struct sockaddr *sap,
71 char *buf, const size_t buflen)
73 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
74 char scopebuf[IPV6_SCOPE_ID_LEN];
78 len = rpc_ntop6_noscopeid(sap, buf, buflen);
79 if (unlikely(len == 0))
82 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
83 !(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_SITELOCAL))
86 rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
87 IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
88 if (unlikely((size_t)rc > sizeof(scopebuf)))
92 if (unlikely(len > buflen))
95 strcat(buf, scopebuf);
99 #else /* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
101 static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
102 char *buf, const int buflen)
107 static size_t rpc_ntop6(const struct sockaddr *sap,
108 char *buf, const size_t buflen)
113 #endif /* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
115 static int rpc_ntop4(const struct sockaddr *sap,
116 char *buf, const size_t buflen)
118 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
120 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
124 * rpc_ntop - construct a presentation address in @buf
125 * @sap: socket address
126 * @buf: construction area
127 * @buflen: size of @buf, in bytes
129 * Plants a %NUL-terminated string in @buf and returns the length
130 * of the string, excluding the %NUL. Otherwise zero is returned.
132 size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
134 switch (sap->sa_family) {
136 return rpc_ntop4(sap, buf, buflen);
138 return rpc_ntop6(sap, buf, buflen);
143 EXPORT_SYMBOL_GPL(rpc_ntop);
145 static size_t rpc_pton4(const char *buf, const size_t buflen,
146 struct sockaddr *sap, const size_t salen)
148 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
149 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
151 if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in))
154 memset(sap, 0, sizeof(struct sockaddr_in));
156 if (in4_pton(buf, buflen, addr, '\0', NULL) == 0)
159 sin->sin_family = AF_INET;
160 return sizeof(struct sockaddr_in);;
163 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
164 static int rpc_parse_scope_id(const char *buf, const size_t buflen,
165 const char *delim, struct sockaddr_in6 *sin6)
170 if ((buf + buflen) == delim)
173 if (*delim != IPV6_SCOPE_DELIMITER)
176 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
177 !(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_SITELOCAL))
180 len = (buf + buflen) - delim - 1;
181 p = kstrndup(delim + 1, len, GFP_KERNEL);
183 unsigned long scope_id = 0;
184 struct net_device *dev;
186 dev = dev_get_by_name(&init_net, p);
188 scope_id = dev->ifindex;
191 if (strict_strtoul(p, 10, &scope_id) == 0) {
199 sin6->sin6_scope_id = scope_id;
206 static size_t rpc_pton6(const char *buf, const size_t buflen,
207 struct sockaddr *sap, const size_t salen)
209 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
210 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
213 if (buflen > (INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN) ||
214 salen < sizeof(struct sockaddr_in6))
217 memset(sap, 0, sizeof(struct sockaddr_in6));
219 if (in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0)
222 if (!rpc_parse_scope_id(buf, buflen, delim, sin6))
225 sin6->sin6_family = AF_INET6;
226 return sizeof(struct sockaddr_in6);
229 static size_t rpc_pton6(const char *buf, const size_t buflen,
230 struct sockaddr *sap, const size_t salen)
237 * rpc_pton - Construct a sockaddr in @sap
238 * @buf: C string containing presentation format IP address
239 * @buflen: length of presentation address in bytes
240 * @sap: buffer into which to plant socket address
241 * @salen: size of buffer in bytes
243 * Returns the size of the socket address if successful; otherwise
246 * Plants a socket address in @sap and returns the size of the
247 * socket address, if successful. Returns zero if an error
250 size_t rpc_pton(const char *buf, const size_t buflen,
251 struct sockaddr *sap, const size_t salen)
255 for (i = 0; i < buflen; i++)
257 return rpc_pton6(buf, buflen, sap, salen);
258 return rpc_pton4(buf, buflen, sap, salen);
260 EXPORT_SYMBOL_GPL(rpc_pton);
263 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
264 * @sap: socket address
266 * Returns a %NUL-terminated string in dynamically allocated memory;
267 * otherwise NULL is returned if an error occurred. Caller must
268 * free the returned string.
270 char *rpc_sockaddr2uaddr(const struct sockaddr *sap)
272 char portbuf[RPCBIND_MAXUADDRPLEN];
273 char addrbuf[RPCBIND_MAXUADDRLEN];
276 switch (sap->sa_family) {
278 if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
280 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
283 if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
285 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
291 if (snprintf(portbuf, sizeof(portbuf),
292 ".%u.%u", port >> 8, port & 0xff) > (int)sizeof(portbuf))
295 if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) > sizeof(addrbuf))
298 return kstrdup(addrbuf, GFP_KERNEL);
300 EXPORT_SYMBOL_GPL(rpc_sockaddr2uaddr);
303 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
304 * @uaddr: C string containing universal address to convert
305 * @uaddr_len: length of universal address string
306 * @sap: buffer into which to plant socket address
307 * @salen: size of buffer
309 * @uaddr does not have to be '\0'-terminated, but strict_strtoul() and
310 * rpc_pton() require proper string termination to be successful.
312 * Returns the size of the socket address if successful; otherwise
315 size_t rpc_uaddr2sockaddr(const char *uaddr, const size_t uaddr_len,
316 struct sockaddr *sap, const size_t salen)
318 char *c, buf[RPCBIND_MAXUADDRLEN + sizeof('\0')];
319 unsigned long portlo, porthi;
322 if (uaddr_len > RPCBIND_MAXUADDRLEN)
325 memcpy(buf, uaddr, uaddr_len);
327 buf[uaddr_len] = '\0';
328 c = strrchr(buf, '.');
329 if (unlikely(c == NULL))
331 if (unlikely(strict_strtoul(c + 1, 10, &portlo) != 0))
333 if (unlikely(portlo > 255))
337 c = strrchr(buf, '.');
338 if (unlikely(c == NULL))
340 if (unlikely(strict_strtoul(c + 1, 10, &porthi) != 0))
342 if (unlikely(porthi > 255))
345 port = (unsigned short)((porthi << 8) | portlo);
348 if (rpc_pton(buf, strlen(buf), sap, salen) == 0)
351 switch (sap->sa_family) {
353 ((struct sockaddr_in *)sap)->sin_port = htons(port);
354 return sizeof(struct sockaddr_in);
356 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
357 return sizeof(struct sockaddr_in6);
362 EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr);