]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/sunrpc/addr.c
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/addr.h>
21 #include <linux/sunrpc/msg_prot.h>
22 #include <linux/slab.h>
23 #include <linux/export.h>
25 #if IS_ENABLED(CONFIG_IPV6)
27 static size_t rpc_ntop6_noscopeid(const struct sockaddr
*sap
,
28 char *buf
, const int buflen
)
30 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
31 const struct in6_addr
*addr
= &sin6
->sin6_addr
;
34 * RFC 4291, Section 2.2.2
36 * Shorthanded ANY address
38 if (ipv6_addr_any(addr
))
39 return snprintf(buf
, buflen
, "::");
42 * RFC 4291, Section 2.2.2
44 * Shorthanded loopback address
46 if (ipv6_addr_loopback(addr
))
47 return snprintf(buf
, buflen
, "::1");
50 * RFC 4291, Section 2.2.3
52 * Special presentation address format for mapped v4
55 if (ipv6_addr_v4mapped(addr
))
56 return snprintf(buf
, buflen
, "::ffff:%pI4",
60 * RFC 4291, Section 2.2.1
62 return snprintf(buf
, buflen
, "%pI6c", addr
);
65 static size_t rpc_ntop6(const struct sockaddr
*sap
,
66 char *buf
, const size_t buflen
)
68 const struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
69 char scopebuf
[IPV6_SCOPE_ID_LEN
];
73 len
= rpc_ntop6_noscopeid(sap
, buf
, buflen
);
74 if (unlikely(len
== 0))
77 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
79 if (sin6
->sin6_scope_id
== 0)
82 rc
= snprintf(scopebuf
, sizeof(scopebuf
), "%c%u",
83 IPV6_SCOPE_DELIMITER
, sin6
->sin6_scope_id
);
84 if (unlikely((size_t)rc
> sizeof(scopebuf
)))
88 if (unlikely(len
> buflen
))
91 strcat(buf
, scopebuf
);
95 #else /* !IS_ENABLED(CONFIG_IPV6) */
97 static size_t rpc_ntop6_noscopeid(const struct sockaddr
*sap
,
98 char *buf
, const int buflen
)
103 static size_t rpc_ntop6(const struct sockaddr
*sap
,
104 char *buf
, const size_t buflen
)
109 #endif /* !IS_ENABLED(CONFIG_IPV6) */
111 static int rpc_ntop4(const struct sockaddr
*sap
,
112 char *buf
, const size_t buflen
)
114 const struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
116 return snprintf(buf
, buflen
, "%pI4", &sin
->sin_addr
);
120 * rpc_ntop - construct a presentation address in @buf
121 * @sap: socket address
122 * @buf: construction area
123 * @buflen: size of @buf, in bytes
125 * Plants a %NUL-terminated string in @buf and returns the length
126 * of the string, excluding the %NUL. Otherwise zero is returned.
128 size_t rpc_ntop(const struct sockaddr
*sap
, char *buf
, const size_t buflen
)
130 switch (sap
->sa_family
) {
132 return rpc_ntop4(sap
, buf
, buflen
);
134 return rpc_ntop6(sap
, buf
, buflen
);
139 EXPORT_SYMBOL_GPL(rpc_ntop
);
141 static size_t rpc_pton4(const char *buf
, const size_t buflen
,
142 struct sockaddr
*sap
, const size_t salen
)
144 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sap
;
145 u8
*addr
= (u8
*)&sin
->sin_addr
.s_addr
;
147 if (buflen
> INET_ADDRSTRLEN
|| salen
< sizeof(struct sockaddr_in
))
150 memset(sap
, 0, sizeof(struct sockaddr_in
));
152 if (in4_pton(buf
, buflen
, addr
, '\0', NULL
) == 0)
155 sin
->sin_family
= AF_INET
;
156 return sizeof(struct sockaddr_in
);
159 #if IS_ENABLED(CONFIG_IPV6)
160 static int rpc_parse_scope_id(struct net
*net
, const char *buf
,
161 const size_t buflen
, const char *delim
,
162 struct sockaddr_in6
*sin6
)
167 if ((buf
+ buflen
) == delim
)
170 if (*delim
!= IPV6_SCOPE_DELIMITER
)
173 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
176 len
= (buf
+ buflen
) - delim
- 1;
177 p
= kstrndup(delim
+ 1, len
, GFP_KERNEL
);
180 struct net_device
*dev
;
182 dev
= dev_get_by_name(net
, p
);
184 scope_id
= dev
->ifindex
;
187 if (kstrtou32(p
, 10, &scope_id
) == 0) {
195 sin6
->sin6_scope_id
= scope_id
;
202 static size_t rpc_pton6(struct net
*net
, const char *buf
, const size_t buflen
,
203 struct sockaddr
*sap
, const size_t salen
)
205 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sap
;
206 u8
*addr
= (u8
*)&sin6
->sin6_addr
.in6_u
;
209 if (buflen
> (INET6_ADDRSTRLEN
+ IPV6_SCOPE_ID_LEN
) ||
210 salen
< sizeof(struct sockaddr_in6
))
213 memset(sap
, 0, sizeof(struct sockaddr_in6
));
215 if (in6_pton(buf
, buflen
, addr
, IPV6_SCOPE_DELIMITER
, &delim
) == 0)
218 if (!rpc_parse_scope_id(net
, buf
, buflen
, delim
, sin6
))
221 sin6
->sin6_family
= AF_INET6
;
222 return sizeof(struct sockaddr_in6
);
225 static size_t rpc_pton6(struct net
*net
, const char *buf
, const size_t buflen
,
226 struct sockaddr
*sap
, const size_t salen
)
233 * rpc_pton - Construct a sockaddr in @sap
234 * @net: applicable network namespace
235 * @buf: C string containing presentation format IP address
236 * @buflen: length of presentation address in bytes
237 * @sap: buffer into which to plant socket address
238 * @salen: size of buffer in bytes
240 * Returns the size of the socket address if successful; otherwise
243 * Plants a socket address in @sap and returns the size of the
244 * socket address, if successful. Returns zero if an error
247 size_t rpc_pton(struct net
*net
, const char *buf
, const size_t buflen
,
248 struct sockaddr
*sap
, const size_t salen
)
252 for (i
= 0; i
< buflen
; i
++)
254 return rpc_pton6(net
, buf
, buflen
, sap
, salen
);
255 return rpc_pton4(buf
, buflen
, sap
, salen
);
257 EXPORT_SYMBOL_GPL(rpc_pton
);
260 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
261 * @sap: socket address
262 * @gfp_flags: allocation mode
264 * Returns a %NUL-terminated string in dynamically allocated memory;
265 * otherwise NULL is returned if an error occurred. Caller must
266 * free the returned string.
268 char *rpc_sockaddr2uaddr(const struct sockaddr
*sap
, gfp_t gfp_flags
)
270 char portbuf
[RPCBIND_MAXUADDRPLEN
];
271 char addrbuf
[RPCBIND_MAXUADDRLEN
];
274 switch (sap
->sa_family
) {
276 if (rpc_ntop4(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
278 port
= ntohs(((struct sockaddr_in
*)sap
)->sin_port
);
281 if (rpc_ntop6_noscopeid(sap
, addrbuf
, sizeof(addrbuf
)) == 0)
283 port
= ntohs(((struct sockaddr_in6
*)sap
)->sin6_port
);
289 if (snprintf(portbuf
, sizeof(portbuf
),
290 ".%u.%u", port
>> 8, port
& 0xff) > (int)sizeof(portbuf
))
293 if (strlcat(addrbuf
, portbuf
, sizeof(addrbuf
)) > sizeof(addrbuf
))
296 return kstrdup(addrbuf
, gfp_flags
);
300 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
301 * @net: applicable network namespace
302 * @uaddr: C string containing universal address to convert
303 * @uaddr_len: length of universal address string
304 * @sap: buffer into which to plant socket address
305 * @salen: size of buffer
307 * @uaddr does not have to be '\0'-terminated, but kstrtou8() and
308 * rpc_pton() require proper string termination to be successful.
310 * Returns the size of the socket address if successful; otherwise
313 size_t rpc_uaddr2sockaddr(struct net
*net
, const char *uaddr
,
314 const size_t uaddr_len
, struct sockaddr
*sap
,
317 char *c
, buf
[RPCBIND_MAXUADDRLEN
+ sizeof('\0')];
321 if (uaddr_len
> RPCBIND_MAXUADDRLEN
)
324 memcpy(buf
, uaddr
, uaddr_len
);
326 buf
[uaddr_len
] = '\0';
327 c
= strrchr(buf
, '.');
328 if (unlikely(c
== NULL
))
330 if (unlikely(kstrtou8(c
+ 1, 10, &portlo
) != 0))
334 c
= strrchr(buf
, '.');
335 if (unlikely(c
== NULL
))
337 if (unlikely(kstrtou8(c
+ 1, 10, &porthi
) != 0))
340 port
= (unsigned short)((porthi
<< 8) | portlo
);
343 if (rpc_pton(net
, buf
, strlen(buf
), sap
, salen
) == 0)
346 switch (sap
->sa_family
) {
348 ((struct sockaddr_in
*)sap
)->sin_port
= htons(port
);
349 return sizeof(struct sockaddr_in
);
351 ((struct sockaddr_in6
*)sap
)->sin6_port
= htons(port
);
352 return sizeof(struct sockaddr_in6
);
357 EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr
);