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,
37 if (ipv6_addr_any(addr))
45 if (ipv6_addr_loopback(addr))
54 if (ipv6_addr_v4mapped(addr))
55 return snprintf(buf, buflen,
"::ffff:%pI4",
61 return snprintf(buf, buflen,
"%pI6c", addr);
64 static size_t rpc_ntop6(
const struct sockaddr *sap,
65 char *buf,
const size_t buflen)
68 char scopebuf[IPV6_SCOPE_ID_LEN];
72 len = rpc_ntop6_noscopeid(sap, buf, buflen);
81 rc =
snprintf(scopebuf,
sizeof(scopebuf),
"%c%u",
83 if (
unlikely((
size_t)rc >
sizeof(scopebuf)))
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)
110 static int rpc_ntop4(
const struct sockaddr *sap,
111 char *buf,
const size_t buflen)
131 return rpc_ntop4(sap, buf, buflen);
133 return rpc_ntop6(sap, buf, buflen);
140 static size_t rpc_pton4(
const char *buf,
const size_t buflen,
141 struct sockaddr *sap,
const size_t salen)
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,
166 if ((buf + buflen) == delim)
169 if (*delim != IPV6_SCOPE_DELIMITER)
175 len = (buf +
buflen) - delim - 1;
178 unsigned long scope_id = 0;
201 static size_t rpc_pton6(
struct net *net,
const char *buf,
const size_t buflen,
202 struct sockaddr *sap,
const size_t salen)
214 if (
in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0)
217 if (!rpc_parse_scope_id(net, buf, buflen, delim, sin6))
224 static size_t rpc_pton6(
struct net *net,
const char *buf,
const size_t buflen,
225 struct sockaddr *sap,
const size_t salen)
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);
269 char portbuf[RPCBIND_MAXUADDRPLEN];
270 char addrbuf[RPCBIND_MAXUADDRLEN];
275 if (rpc_ntop4(sap, addrbuf,
sizeof(addrbuf)) == 0)
280 if (rpc_ntop6_noscopeid(sap, addrbuf,
sizeof(addrbuf)) == 0)
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);
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';
343 port = (
unsigned short)((porthi << 8) | portlo);