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 * Returns the size of the socket address if successful; otherwise
312 size_t rpc_uaddr2sockaddr(const char *uaddr
, const size_t uaddr_len
,
313 struct sockaddr
*sap
, const size_t salen
)
315 char *c
, buf
[RPCBIND_MAXUADDRLEN
];
316 unsigned long portlo
, porthi
;
319 if (uaddr_len
> sizeof(buf
))
322 memcpy(buf
, uaddr
, uaddr_len
);
324 buf
[uaddr_len
] = '\n';
325 buf
[uaddr_len
+ 1] = '\0';
327 c
= strrchr(buf
, '.');
328 if (unlikely(c
== NULL
))
330 if (unlikely(strict_strtoul(c
+ 1, 10, &portlo
) != 0))
332 if (unlikely(portlo
> 255))
338 c
= strrchr(buf
, '.');
339 if (unlikely(c
== NULL
))
341 if (unlikely(strict_strtoul(c
+ 1, 10, &porthi
) != 0))
343 if (unlikely(porthi
> 255))
346 port
= (unsigned short)((porthi
<< 8) | portlo
);
350 if (rpc_pton(buf
, strlen(buf
), sap
, salen
) == 0)
353 switch (sap
->sa_family
) {
355 ((struct sockaddr_in
*)sap
)->sin_port
= htons(port
);
356 return sizeof(struct sockaddr_in
);
358 ((struct sockaddr_in6
*)sap
)->sin6_port
= htons(port
);
359 return sizeof(struct sockaddr_in6
);
364 EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr
);