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>
21 #include <linux/slab.h>
22 #include <linux/export.h>
24 #if IS_ENABLED(CONFIG_IPV6)
26 static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
27 char *buf, const int buflen)
29 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
30 const struct in6_addr *addr = &sin6->sin6_addr;
33 * RFC 4291, Section 2.2.2
35 * Shorthanded ANY address
37 if (ipv6_addr_any(addr))
38 return snprintf(buf, buflen, "::");
41 * RFC 4291, Section 2.2.2
43 * Shorthanded loopback address
45 if (ipv6_addr_loopback(addr))
46 return snprintf(buf, buflen, "::1");
49 * RFC 4291, Section 2.2.3
51 * Special presentation address format for mapped v4
54 if (ipv6_addr_v4mapped(addr))
55 return snprintf(buf, buflen, "::ffff:%pI4",
59 * RFC 4291, Section 2.2.1
61 return snprintf(buf, buflen, "%pI6c", addr);
64 static size_t rpc_ntop6(const struct sockaddr *sap,
65 char *buf, const size_t buflen)
67 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
68 char scopebuf[IPV6_SCOPE_ID_LEN];
72 len = rpc_ntop6_noscopeid(sap, buf, buflen);
73 if (unlikely(len == 0))
76 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
78 if (sin6->sin6_scope_id == 0)
81 rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
82 IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
83 if (unlikely((size_t)rc > sizeof(scopebuf)))
87 if (unlikely(len > buflen))
90 strcat(buf, scopebuf);
94 #else /* !IS_ENABLED(CONFIG_IPV6) */
96 static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
97 char *buf, const int buflen)
102 static size_t rpc_ntop6(const struct sockaddr *sap,
103 char *buf, const size_t buflen)
108 #endif /* !IS_ENABLED(CONFIG_IPV6) */
110 static int rpc_ntop4(const struct sockaddr *sap,
111 char *buf, const size_t buflen)
113 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
115 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
119 * rpc_ntop - construct a presentation address in @buf
120 * @sap: socket address
121 * @buf: construction area
122 * @buflen: size of @buf, in bytes
124 * Plants a %NUL-terminated string in @buf and returns the length
125 * of the string, excluding the %NUL. Otherwise zero is returned.
127 size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
129 switch (sap->sa_family) {
131 return rpc_ntop4(sap, buf, buflen);
133 return rpc_ntop6(sap, buf, buflen);
138 EXPORT_SYMBOL_GPL(rpc_ntop);
140 static size_t rpc_pton4(const char *buf, const size_t buflen,
141 struct sockaddr *sap, const size_t salen)
143 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
144 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
146 if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in))
149 memset(sap, 0, sizeof(struct sockaddr_in));
151 if (in4_pton(buf, buflen, addr, '\0', NULL) == 0)
154 sin->sin_family = AF_INET;
155 return sizeof(struct sockaddr_in);
158 #if IS_ENABLED(CONFIG_IPV6)
159 static int rpc_parse_scope_id(struct net *net, const char *buf,
160 const size_t buflen, const char *delim,
161 struct sockaddr_in6 *sin6)
166 if ((buf + buflen) == delim)
169 if (*delim != IPV6_SCOPE_DELIMITER)
172 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
175 len = (buf + buflen) - delim - 1;
176 p = kstrndup(delim + 1, len, GFP_KERNEL);
178 unsigned long scope_id = 0;
179 struct net_device *dev;
181 dev = dev_get_by_name(net, p);
183 scope_id = dev->ifindex;
186 if (strict_strtoul(p, 10, &scope_id) == 0) {
194 sin6->sin6_scope_id = scope_id;
201 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
202 struct sockaddr *sap, const size_t salen)
204 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
205 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
208 if (buflen > (INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN) ||
209 salen < sizeof(struct sockaddr_in6))
212 memset(sap, 0, sizeof(struct sockaddr_in6));
214 if (in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0)
217 if (!rpc_parse_scope_id(net, buf, buflen, delim, sin6))
220 sin6->sin6_family = AF_INET6;
221 return sizeof(struct sockaddr_in6);
224 static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
225 struct sockaddr *sap, const size_t salen)
232 * rpc_pton - Construct a sockaddr in @sap
233 * @net: applicable network namespace
234 * @buf: C string containing presentation format IP address
235 * @buflen: length of presentation address in bytes
236 * @sap: buffer into which to plant socket address
237 * @salen: size of buffer in bytes
239 * Returns the size of the socket address if successful; otherwise
242 * Plants a socket address in @sap and returns the size of the
243 * socket address, if successful. Returns zero if an error
246 size_t rpc_pton(struct net *net, const char *buf, const size_t buflen,
247 struct sockaddr *sap, const size_t salen)
251 for (i = 0; i < buflen; i++)
253 return rpc_pton6(net, buf, buflen, sap, salen);
254 return rpc_pton4(buf, buflen, sap, salen);
256 EXPORT_SYMBOL_GPL(rpc_pton);
259 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
260 * @sap: socket address
261 * @gfp_flags: allocation mode
263 * Returns a %NUL-terminated string in dynamically allocated memory;
264 * otherwise NULL is returned if an error occurred. Caller must
265 * free the returned string.
267 char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags)
269 char portbuf[RPCBIND_MAXUADDRPLEN];
270 char addrbuf[RPCBIND_MAXUADDRLEN];
273 switch (sap->sa_family) {
275 if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
277 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
280 if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
282 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
288 if (snprintf(portbuf, sizeof(portbuf),
289 ".%u.%u", port >> 8, port & 0xff) > (int)sizeof(portbuf))
292 if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) > sizeof(addrbuf))
295 return kstrdup(addrbuf, gfp_flags);
299 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
300 * @net: applicable network namespace
301 * @uaddr: C string containing universal address to convert
302 * @uaddr_len: length of universal address string
303 * @sap: buffer into which to plant socket address
304 * @salen: size of buffer
306 * @uaddr does not have to be '\0'-terminated, but strict_strtoul() and
307 * rpc_pton() require proper string termination to be successful.
309 * Returns the size of the socket address if successful; otherwise
312 size_t rpc_uaddr2sockaddr(struct net *net, const char *uaddr,
313 const size_t uaddr_len, struct sockaddr *sap,
316 char *c, buf[RPCBIND_MAXUADDRLEN + sizeof('\0')];
317 unsigned long portlo, porthi;
320 if (uaddr_len > RPCBIND_MAXUADDRLEN)
323 memcpy(buf, uaddr, uaddr_len);
325 buf[uaddr_len] = '\0';
326 c = strrchr(buf, '.');
327 if (unlikely(c == NULL))
329 if (unlikely(strict_strtoul(c + 1, 10, &portlo) != 0))
331 if (unlikely(portlo > 255))
335 c = strrchr(buf, '.');
336 if (unlikely(c == NULL))
338 if (unlikely(strict_strtoul(c + 1, 10, &porthi) != 0))
340 if (unlikely(porthi > 255))
343 port = (unsigned short)((porthi << 8) | portlo);
346 if (rpc_pton(net, buf, strlen(buf), sap, salen) == 0)
349 switch (sap->sa_family) {
351 ((struct sockaddr_in *)sap)->sin_port = htons(port);
352 return sizeof(struct sockaddr_in);
354 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
355 return sizeof(struct sockaddr_in6);
360 EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr);