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>
23 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
25 static size_t rpc_ntop6_noscopeid(const struct sockaddr
*sap
,
26 char *buf
, const int buflen
)
28 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
29 const struct in6_addr
*addr
= &sin6
->sin6_addr
;
32 * RFC 4291, Section 2.2.2
34 * Shorthanded ANY address
36 if (ipv6_addr_any(addr
))
37 return snprintf(buf
, buflen
, "::");
40 * RFC 4291, Section 2.2.2
42 * Shorthanded loopback address
44 if (ipv6_addr_loopback(addr
))
45 return snprintf(buf
, buflen
, "::1");
48 * RFC 4291, Section 2.2.3
50 * Special presentation address format for mapped v4
53 if (ipv6_addr_v4mapped(addr
))
54 return snprintf(buf
, buflen
, "::ffff:%pI4",
58 * RFC 4291, Section 2.2.1
60 return snprintf(buf
, buflen
, "%pI6c", addr
);
63 static size_t rpc_ntop6(const struct sockaddr
*sap
,
64 char *buf
, const size_t buflen
)
66 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
67 char scopebuf
[IPV6_SCOPE_ID_LEN
];
71 len
= rpc_ntop6_noscopeid(sap
, buf
, buflen
);
72 if (unlikely(len
== 0))
75 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
77 if (sin6
->sin6_scope_id
== 0)
80 rc
= snprintf(scopebuf
, sizeof(scopebuf
), "%c%u",
81 IPV6_SCOPE_DELIMITER
, sin6
->sin6_scope_id
);
82 if (unlikely((size_t)rc
> sizeof(scopebuf
)))
86 if (unlikely(len
> buflen
))
89 strcat(buf
, scopebuf
);
93 #else /* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
95 static size_t rpc_ntop6_noscopeid(const struct sockaddr
*sap
,
96 char *buf
, const int buflen
)
101 static size_t rpc_ntop6(const struct sockaddr
*sap
,
102 char *buf
, const size_t buflen
)
107 #endif /* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
109 static int rpc_ntop4(const struct sockaddr
*sap
,
110 char *buf
, const size_t buflen
)
112 const struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
114 return snprintf(buf
, buflen
, "%pI4", &sin
->sin_addr
);
118 * rpc_ntop - construct a presentation address in @buf
119 * @sap: socket address
120 * @buf: construction area
121 * @buflen: size of @buf, in bytes
123 * Plants a %NUL-terminated string in @buf and returns the length
124 * of the string, excluding the %NUL. Otherwise zero is returned.
126 size_t rpc_ntop(const struct sockaddr
*sap
, char *buf
, const size_t buflen
)
128 switch (sap
->sa_family
) {
130 return rpc_ntop4(sap
, buf
, buflen
);
132 return rpc_ntop6(sap
, buf
, buflen
);
137 EXPORT_SYMBOL_GPL(rpc_ntop
);
139 static size_t rpc_pton4(const char *buf
, const size_t buflen
,
140 struct sockaddr
*sap
, const size_t salen
)
142 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
143 u8
*addr
= (u8
*)&sin
->sin_addr
.s_addr
;
145 if (buflen
> INET_ADDRSTRLEN
|| salen
< sizeof(struct sockaddr_in
))
148 memset(sap
, 0, sizeof(struct sockaddr_in
));
150 if (in4_pton(buf
, buflen
, addr
, '\0', NULL
) == 0)
153 sin
->sin_family
= AF_INET
;
154 return sizeof(struct sockaddr_in
);;
157 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
158 static int rpc_parse_scope_id(const char *buf
, const size_t buflen
,
159 const char *delim
, struct sockaddr_in6
*sin6
)
164 if ((buf
+ buflen
) == delim
)
167 if (*delim
!= IPV6_SCOPE_DELIMITER
)
170 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
173 len
= (buf
+ buflen
) - delim
- 1;
174 p
= kstrndup(delim
+ 1, len
, GFP_KERNEL
);
176 unsigned long scope_id
= 0;
177 struct net_device
*dev
;
179 dev
= dev_get_by_name(&init_net
, p
);
181 scope_id
= dev
->ifindex
;
184 if (strict_strtoul(p
, 10, &scope_id
) == 0) {
192 sin6
->sin6_scope_id
= scope_id
;
199 static size_t rpc_pton6(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(buf
, buflen
, delim
, sin6
))
218 sin6
->sin6_family
= AF_INET6
;
219 return sizeof(struct sockaddr_in6
);
222 static size_t rpc_pton6(const char *buf
, const size_t buflen
,
223 struct sockaddr
*sap
, const size_t salen
)
230 * rpc_pton - Construct a sockaddr in @sap
231 * @buf: C string containing presentation format IP address
232 * @buflen: length of presentation address in bytes
233 * @sap: buffer into which to plant socket address
234 * @salen: size of buffer in bytes
236 * Returns the size of the socket address if successful; otherwise
239 * Plants a socket address in @sap and returns the size of the
240 * socket address, if successful. Returns zero if an error
243 size_t rpc_pton(const char *buf
, const size_t buflen
,
244 struct sockaddr
*sap
, const size_t salen
)
248 for (i
= 0; i
< buflen
; i
++)
250 return rpc_pton6(buf
, buflen
, sap
, salen
);
251 return rpc_pton4(buf
, buflen
, sap
, salen
);
253 EXPORT_SYMBOL_GPL(rpc_pton
);
256 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
257 * @sap: socket address
259 * Returns a %NUL-terminated string in dynamically allocated memory;
260 * otherwise NULL is returned if an error occurred. Caller must
261 * free the returned string.
263 char *rpc_sockaddr2uaddr(const struct sockaddr
*sap
)
265 char portbuf
[RPCBIND_MAXUADDRPLEN
];
266 char addrbuf
[RPCBIND_MAXUADDRLEN
];
269 switch (sap
->sa_family
) {
271 if (rpc_ntop4(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
273 port
= ntohs(((struct sockaddr_in
*)sap
)->sin_port
);
276 if (rpc_ntop6_noscopeid(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
278 port
= ntohs(((struct sockaddr_in6
*)sap
)->sin6_port
);
284 if (snprintf(portbuf
, sizeof(portbuf
),
285 ".%u.%u", port
>> 8, port
& 0xff) > (int)sizeof(portbuf
))
288 if (strlcat(addrbuf
, portbuf
, sizeof(addrbuf
)) > sizeof(addrbuf
))
291 return kstrdup(addrbuf
, GFP_KERNEL
);
293 EXPORT_SYMBOL_GPL(rpc_sockaddr2uaddr
);
296 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
297 * @uaddr: C string containing universal address to convert
298 * @uaddr_len: length of universal address string
299 * @sap: buffer into which to plant socket address
300 * @salen: size of buffer
302 * @uaddr does not have to be '\0'-terminated, but strict_strtoul() and
303 * rpc_pton() require proper string termination to be successful.
305 * Returns the size of the socket address if successful; otherwise
308 size_t rpc_uaddr2sockaddr(const char *uaddr
, const size_t uaddr_len
,
309 struct sockaddr
*sap
, const size_t salen
)
311 char *c
, buf
[RPCBIND_MAXUADDRLEN
+ sizeof('\0')];
312 unsigned long portlo
, porthi
;
315 if (uaddr_len
> RPCBIND_MAXUADDRLEN
)
318 memcpy(buf
, uaddr
, uaddr_len
);
320 buf
[uaddr_len
] = '\0';
321 c
= strrchr(buf
, '.');
322 if (unlikely(c
== NULL
))
324 if (unlikely(strict_strtoul(c
+ 1, 10, &portlo
) != 0))
326 if (unlikely(portlo
> 255))
330 c
= strrchr(buf
, '.');
331 if (unlikely(c
== NULL
))
333 if (unlikely(strict_strtoul(c
+ 1, 10, &porthi
) != 0))
335 if (unlikely(porthi
> 255))
338 port
= (unsigned short)((porthi
<< 8) | portlo
);
341 if (rpc_pton(buf
, strlen(buf
), sap
, salen
) == 0)
344 switch (sap
->sa_family
) {
346 ((struct sockaddr_in
*)sap
)->sin_port
= htons(port
);
347 return sizeof(struct sockaddr_in
);
349 ((struct sockaddr_in6
*)sap
)->sin6_port
= htons(port
);
350 return sizeof(struct sockaddr_in6
);
355 EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr
);