SUNRPC: parametrize rpc_pton6() by network context
[deliverable/linux.git] / net / sunrpc / addr.c
CommitLineData
a02d6926
CL
1/*
2 * Copyright 2009, Oracle. All rights reserved.
3 *
4 * Convert socket addresses to presentation addresses and universal
5 * addresses, and vice versa.
6 *
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.
13 *
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.
17 */
18
19#include <net/ipv6.h>
20#include <linux/sunrpc/clnt.h>
5a0e3ad6 21#include <linux/slab.h>
bc3b2d7f 22#include <linux/export.h>
a02d6926 23
dfd56b8b 24#if IS_ENABLED(CONFIG_IPV6)
a02d6926
CL
25
26static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
27 char *buf, const int buflen)
28{
29 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
30 const struct in6_addr *addr = &sin6->sin6_addr;
31
32 /*
33 * RFC 4291, Section 2.2.2
34 *
35 * Shorthanded ANY address
36 */
37 if (ipv6_addr_any(addr))
38 return snprintf(buf, buflen, "::");
39
40 /*
41 * RFC 4291, Section 2.2.2
42 *
43 * Shorthanded loopback address
44 */
45 if (ipv6_addr_loopback(addr))
46 return snprintf(buf, buflen, "::1");
47
48 /*
49 * RFC 4291, Section 2.2.3
50 *
51 * Special presentation address format for mapped v4
52 * addresses.
53 */
54 if (ipv6_addr_v4mapped(addr))
55 return snprintf(buf, buflen, "::ffff:%pI4",
56 &addr->s6_addr32[3]);
57
58 /*
59 * RFC 4291, Section 2.2.1
a02d6926 60 */
dd1fd90f 61 return snprintf(buf, buflen, "%pI6c", addr);
a02d6926
CL
62}
63
64static size_t rpc_ntop6(const struct sockaddr *sap,
65 char *buf, const size_t buflen)
66{
67 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
68 char scopebuf[IPV6_SCOPE_ID_LEN];
69 size_t len;
70 int rc;
71
72 len = rpc_ntop6_noscopeid(sap, buf, buflen);
73 if (unlikely(len == 0))
74 return len;
75
f1a89a11 76 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
a02d6926 77 return len;
7a88efe9
CL
78 if (sin6->sin6_scope_id == 0)
79 return len;
a02d6926
CL
80
81 rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
82 IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
83 if (unlikely((size_t)rc > sizeof(scopebuf)))
84 return 0;
85
86 len += rc;
87 if (unlikely(len > buflen))
88 return 0;
89
90 strcat(buf, scopebuf);
91 return len;
92}
93
dfd56b8b 94#else /* !IS_ENABLED(CONFIG_IPV6) */
a02d6926
CL
95
96static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
97 char *buf, const int buflen)
98{
99 return 0;
100}
101
102static size_t rpc_ntop6(const struct sockaddr *sap,
103 char *buf, const size_t buflen)
104{
105 return 0;
106}
107
dfd56b8b 108#endif /* !IS_ENABLED(CONFIG_IPV6) */
a02d6926
CL
109
110static int rpc_ntop4(const struct sockaddr *sap,
111 char *buf, const size_t buflen)
112{
113 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
114
115 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
116}
117
118/**
119 * rpc_ntop - construct a presentation address in @buf
120 * @sap: socket address
121 * @buf: construction area
122 * @buflen: size of @buf, in bytes
123 *
124 * Plants a %NUL-terminated string in @buf and returns the length
125 * of the string, excluding the %NUL. Otherwise zero is returned.
126 */
127size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
128{
129 switch (sap->sa_family) {
130 case AF_INET:
131 return rpc_ntop4(sap, buf, buflen);
132 case AF_INET6:
133 return rpc_ntop6(sap, buf, buflen);
134 }
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(rpc_ntop);
139
140static size_t rpc_pton4(const char *buf, const size_t buflen,
141 struct sockaddr *sap, const size_t salen)
142{
143 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
144 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
145
146 if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in))
147 return 0;
148
149 memset(sap, 0, sizeof(struct sockaddr_in));
150
151 if (in4_pton(buf, buflen, addr, '\0', NULL) == 0)
152 return 0;
153
154 sin->sin_family = AF_INET;
6eab04a8 155 return sizeof(struct sockaddr_in);
a02d6926
CL
156}
157
dfd56b8b 158#if IS_ENABLED(CONFIG_IPV6)
3065f1e2
SK
159static int rpc_parse_scope_id(struct net *net, const char *buf,
160 const size_t buflen, const char *delim,
161 struct sockaddr_in6 *sin6)
a02d6926
CL
162{
163 char *p;
164 size_t len;
165
166 if ((buf + buflen) == delim)
167 return 1;
168
169 if (*delim != IPV6_SCOPE_DELIMITER)
170 return 0;
171
f1a89a11 172 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
a02d6926
CL
173 return 0;
174
175 len = (buf + buflen) - delim - 1;
176 p = kstrndup(delim + 1, len, GFP_KERNEL);
177 if (p) {
178 unsigned long scope_id = 0;
179 struct net_device *dev;
180
3065f1e2 181 dev = dev_get_by_name(net, p);
a02d6926
CL
182 if (dev != NULL) {
183 scope_id = dev->ifindex;
184 dev_put(dev);
185 } else {
186 if (strict_strtoul(p, 10, &scope_id) == 0) {
187 kfree(p);
188 return 0;
189 }
190 }
191
192 kfree(p);
193
194 sin6->sin6_scope_id = scope_id;
195 return 1;
196 }
197
198 return 0;
199}
200
8b147f74 201static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
a02d6926
CL
202 struct sockaddr *sap, const size_t salen)
203{
204 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
205 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
206 const char *delim;
207
208 if (buflen > (INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN) ||
209 salen < sizeof(struct sockaddr_in6))
210 return 0;
211
212 memset(sap, 0, sizeof(struct sockaddr_in6));
213
214 if (in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0)
215 return 0;
216
8b147f74 217 if (!rpc_parse_scope_id(net, buf, buflen, delim, sin6))
a02d6926
CL
218 return 0;
219
220 sin6->sin6_family = AF_INET6;
221 return sizeof(struct sockaddr_in6);
222}
223#else
8b147f74 224static size_t rpc_pton6(struct net *net, const char *buf, const size_t buflen,
a02d6926
CL
225 struct sockaddr *sap, const size_t salen)
226{
227 return 0;
228}
229#endif
230
231/**
232 * rpc_pton - Construct a sockaddr in @sap
233 * @buf: C string containing presentation format IP address
234 * @buflen: length of presentation address in bytes
235 * @sap: buffer into which to plant socket address
236 * @salen: size of buffer in bytes
237 *
238 * Returns the size of the socket address if successful; otherwise
239 * zero is returned.
240 *
241 * Plants a socket address in @sap and returns the size of the
242 * socket address, if successful. Returns zero if an error
243 * occurred.
244 */
245size_t rpc_pton(const char *buf, const size_t buflen,
246 struct sockaddr *sap, const size_t salen)
247{
248 unsigned int i;
249
250 for (i = 0; i < buflen; i++)
251 if (buf[i] == ':')
8b147f74 252 return rpc_pton6(&init_net, buf, buflen, sap, salen);
a02d6926
CL
253 return rpc_pton4(buf, buflen, sap, salen);
254}
255EXPORT_SYMBOL_GPL(rpc_pton);
256
257/**
258 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
259 * @sap: socket address
d77385f2 260 * @gfp_flags: allocation mode
a02d6926
CL
261 *
262 * Returns a %NUL-terminated string in dynamically allocated memory;
263 * otherwise NULL is returned if an error occurred. Caller must
264 * free the returned string.
265 */
d77385f2 266char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags)
a02d6926
CL
267{
268 char portbuf[RPCBIND_MAXUADDRPLEN];
269 char addrbuf[RPCBIND_MAXUADDRLEN];
270 unsigned short port;
271
272 switch (sap->sa_family) {
273 case AF_INET:
274 if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
275 return NULL;
276 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
277 break;
278 case AF_INET6:
279 if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
280 return NULL;
281 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
282 break;
283 default:
284 return NULL;
285 }
286
287 if (snprintf(portbuf, sizeof(portbuf),
288 ".%u.%u", port >> 8, port & 0xff) > (int)sizeof(portbuf))
289 return NULL;
290
291 if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) > sizeof(addrbuf))
292 return NULL;
293
d77385f2 294 return kstrdup(addrbuf, gfp_flags);
a02d6926 295}
a02d6926
CL
296
297/**
298 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
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
303 *
1e360a60
CL
304 * @uaddr does not have to be '\0'-terminated, but strict_strtoul() and
305 * rpc_pton() require proper string termination to be successful.
306 *
a02d6926
CL
307 * Returns the size of the socket address if successful; otherwise
308 * zero is returned.
309 */
310size_t rpc_uaddr2sockaddr(const char *uaddr, const size_t uaddr_len,
311 struct sockaddr *sap, const size_t salen)
312{
1e360a60 313 char *c, buf[RPCBIND_MAXUADDRLEN + sizeof('\0')];
a02d6926
CL
314 unsigned long portlo, porthi;
315 unsigned short port;
316
1e360a60 317 if (uaddr_len > RPCBIND_MAXUADDRLEN)
a02d6926
CL
318 return 0;
319
320 memcpy(buf, uaddr, uaddr_len);
321
1e360a60 322 buf[uaddr_len] = '\0';
a02d6926
CL
323 c = strrchr(buf, '.');
324 if (unlikely(c == NULL))
325 return 0;
326 if (unlikely(strict_strtoul(c + 1, 10, &portlo) != 0))
327 return 0;
328 if (unlikely(portlo > 255))
329 return 0;
330
1e360a60 331 *c = '\0';
a02d6926
CL
332 c = strrchr(buf, '.');
333 if (unlikely(c == NULL))
334 return 0;
335 if (unlikely(strict_strtoul(c + 1, 10, &porthi) != 0))
336 return 0;
337 if (unlikely(porthi > 255))
338 return 0;
339
340 port = (unsigned short)((porthi << 8) | portlo);
341
1e360a60 342 *c = '\0';
a02d6926
CL
343 if (rpc_pton(buf, strlen(buf), sap, salen) == 0)
344 return 0;
345
346 switch (sap->sa_family) {
347 case AF_INET:
348 ((struct sockaddr_in *)sap)->sin_port = htons(port);
349 return sizeof(struct sockaddr_in);
350 case AF_INET6:
351 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
352 return sizeof(struct sockaddr_in6);
353 }
354
355 return 0;
356}
357EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr);
This page took 0.206673 seconds and 5 git commands to generate.