1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2009, Oracle. All rights reserved.
5 * Convert socket addresses to presentation addresses and universal
6 * addresses, and vice versa.
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.
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.
21 #include <linux/sunrpc/addr.h>
22 #include <linux/sunrpc/msg_prot.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
26 #if IS_ENABLED(CONFIG_IPV6)
28 static size_t rpc_ntop6_noscopeid(const struct sockaddr
*sap
,
29 char *buf
, const int buflen
)
31 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
32 const struct in6_addr
*addr
= &sin6
->sin6_addr
;
35 * RFC 4291, Section 2.2.2
37 * Shorthanded ANY address
39 if (ipv6_addr_any(addr
))
40 return snprintf(buf
, buflen
, "::");
43 * RFC 4291, Section 2.2.2
45 * Shorthanded loopback address
47 if (ipv6_addr_loopback(addr
))
48 return snprintf(buf
, buflen
, "::1");
51 * RFC 4291, Section 2.2.3
53 * Special presentation address format for mapped v4
56 if (ipv6_addr_v4mapped(addr
))
57 return snprintf(buf
, buflen
, "::ffff:%pI4",
61 * RFC 4291, Section 2.2.1
63 return snprintf(buf
, buflen
, "%pI6c", addr
);
66 static size_t rpc_ntop6(const struct sockaddr
*sap
,
67 char *buf
, const size_t buflen
)
69 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
70 char scopebuf
[IPV6_SCOPE_ID_LEN
];
74 len
= rpc_ntop6_noscopeid(sap
, buf
, buflen
);
75 if (unlikely(len
== 0))
78 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
80 if (sin6
->sin6_scope_id
== 0)
83 rc
= snprintf(scopebuf
, sizeof(scopebuf
), "%c%u",
84 IPV6_SCOPE_DELIMITER
, sin6
->sin6_scope_id
);
85 if (unlikely((size_t)rc
>= sizeof(scopebuf
)))
89 if (unlikely(len
>= buflen
))
92 strcat(buf
, scopebuf
);
96 #else /* !IS_ENABLED(CONFIG_IPV6) */
98 static size_t rpc_ntop6_noscopeid(const struct sockaddr
*sap
,
99 char *buf
, const int buflen
)
104 static size_t rpc_ntop6(const struct sockaddr
*sap
,
105 char *buf
, const size_t buflen
)
110 #endif /* !IS_ENABLED(CONFIG_IPV6) */
112 static int rpc_ntop4(const struct sockaddr
*sap
,
113 char *buf
, const size_t buflen
)
115 const struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
117 return snprintf(buf
, buflen
, "%pI4", &sin
->sin_addr
);
121 * rpc_ntop - construct a presentation address in @buf
122 * @sap: socket address
123 * @buf: construction area
124 * @buflen: size of @buf, in bytes
126 * Plants a %NUL-terminated string in @buf and returns the length
127 * of the string, excluding the %NUL. Otherwise zero is returned.
129 size_t rpc_ntop(const struct sockaddr
*sap
, char *buf
, const size_t buflen
)
131 switch (sap
->sa_family
) {
133 return rpc_ntop4(sap
, buf
, buflen
);
135 return rpc_ntop6(sap
, buf
, buflen
);
140 EXPORT_SYMBOL_GPL(rpc_ntop
);
142 static size_t rpc_pton4(const char *buf
, const size_t buflen
,
143 struct sockaddr
*sap
, const size_t salen
)
145 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
146 u8
*addr
= (u8
*)&sin
->sin_addr
.s_addr
;
148 if (buflen
> INET_ADDRSTRLEN
|| salen
< sizeof(struct sockaddr_in
))
151 memset(sap
, 0, sizeof(struct sockaddr_in
));
153 if (in4_pton(buf
, buflen
, addr
, '\0', NULL
) == 0)
156 sin
->sin_family
= AF_INET
;
157 return sizeof(struct sockaddr_in
);
160 #if IS_ENABLED(CONFIG_IPV6)
161 static int rpc_parse_scope_id(struct net
*net
, const char *buf
,
162 const size_t buflen
, const char *delim
,
163 struct sockaddr_in6
*sin6
)
165 char p
[IPV6_SCOPE_ID_LEN
+ 1];
168 struct net_device
*dev
;
170 if ((buf
+ buflen
) == delim
)
173 if (*delim
!= IPV6_SCOPE_DELIMITER
)
176 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
179 len
= (buf
+ buflen
) - delim
- 1;
180 if (len
> IPV6_SCOPE_ID_LEN
)
183 memcpy(p
, delim
+ 1, len
);
186 dev
= dev_get_by_name(net
, p
);
188 scope_id
= dev
->ifindex
;
191 if (kstrtou32(p
, 10, &scope_id
) != 0)
195 sin6
->sin6_scope_id
= scope_id
;
199 static size_t rpc_pton6(struct net
*net
, const char *buf
, const size_t buflen
,
200 struct sockaddr
*sap
, const size_t salen
)
202 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
203 u8
*addr
= (u8
*)&sin6
->sin6_addr
.in6_u
;
206 if (buflen
> (INET6_ADDRSTRLEN
+ IPV6_SCOPE_ID_LEN
) ||
207 salen
< sizeof(struct sockaddr_in6
))
210 memset(sap
, 0, sizeof(struct sockaddr_in6
));
212 if (in6_pton(buf
, buflen
, addr
, IPV6_SCOPE_DELIMITER
, &delim
) == 0)
215 if (!rpc_parse_scope_id(net
, buf
, buflen
, delim
, sin6
))
218 sin6
->sin6_family
= AF_INET6
;
219 return sizeof(struct sockaddr_in6
);
222 static size_t rpc_pton6(struct net
*net
, const char *buf
, const size_t buflen
,
223 struct sockaddr
*sap
, const size_t salen
)
230 * rpc_pton - Construct a sockaddr in @sap
231 * @net: applicable network namespace
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
237 * Returns the size of the socket address if successful; otherwise
240 * Plants a socket address in @sap and returns the size of the
241 * socket address, if successful. Returns zero if an error
244 size_t rpc_pton(struct net
*net
, const char *buf
, const size_t buflen
,
245 struct sockaddr
*sap
, const size_t salen
)
249 for (i
= 0; i
< buflen
; i
++)
251 return rpc_pton6(net
, buf
, buflen
, sap
, salen
);
252 return rpc_pton4(buf
, buflen
, sap
, salen
);
254 EXPORT_SYMBOL_GPL(rpc_pton
);
257 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
258 * @sap: socket address
259 * @gfp_flags: allocation mode
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.
265 char *rpc_sockaddr2uaddr(const struct sockaddr
*sap
, gfp_t gfp_flags
)
267 char portbuf
[RPCBIND_MAXUADDRPLEN
];
268 char addrbuf
[RPCBIND_MAXUADDRLEN
];
271 switch (sap
->sa_family
) {
273 if (rpc_ntop4(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
275 port
= ntohs(((struct sockaddr_in
*)sap
)->sin_port
);
278 if (rpc_ntop6_noscopeid(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
280 port
= ntohs(((struct sockaddr_in6
*)sap
)->sin6_port
);
286 if (snprintf(portbuf
, sizeof(portbuf
),
287 ".%u.%u", port
>> 8, port
& 0xff) >= (int)sizeof(portbuf
))
290 if (strlcat(addrbuf
, portbuf
, sizeof(addrbuf
)) >= sizeof(addrbuf
))
293 return kstrdup(addrbuf
, gfp_flags
);
297 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
298 * @net: applicable network namespace
299 * @uaddr: C string containing universal address to convert
300 * @uaddr_len: length of universal address string
301 * @sap: buffer into which to plant socket address
302 * @salen: size of buffer
304 * @uaddr does not have to be '\0'-terminated, but kstrtou8() and
305 * rpc_pton() require proper string termination to be successful.
307 * Returns the size of the socket address if successful; otherwise
310 size_t rpc_uaddr2sockaddr(struct net
*net
, const char *uaddr
,
311 const size_t uaddr_len
, struct sockaddr
*sap
,
314 char *c
, buf
[RPCBIND_MAXUADDRLEN
+ sizeof('\0')];
318 if (uaddr_len
> RPCBIND_MAXUADDRLEN
)
321 memcpy(buf
, uaddr
, uaddr_len
);
323 buf
[uaddr_len
] = '\0';
324 c
= strrchr(buf
, '.');
325 if (unlikely(c
== NULL
))
327 if (unlikely(kstrtou8(c
+ 1, 10, &portlo
) != 0))
331 c
= strrchr(buf
, '.');
332 if (unlikely(c
== NULL
))
334 if (unlikely(kstrtou8(c
+ 1, 10, &porthi
) != 0))
337 port
= (unsigned short)((porthi
<< 8) | portlo
);
340 if (rpc_pton(net
, buf
, strlen(buf
), sap
, salen
) == 0)
343 switch (sap
->sa_family
) {
345 ((struct sockaddr_in
*)sap
)->sin_port
= htons(port
);
346 return sizeof(struct sockaddr_in
);
348 ((struct sockaddr_in6
*)sap
)->sin6_port
= htons(port
);
349 return sizeof(struct sockaddr_in6
);
354 EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr
);