blob: 23cd19d45ab3fa1e61df21c046b77bce3ac5a623 [file] [log] [blame]
Chuck Levera02d6922009-08-09 15:09:34 -04001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Chuck Levera02d6922009-08-09 15:09:34 -040022
23#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
24
25static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
26 char *buf, const int buflen)
27{
28 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
29 const struct in6_addr *addr = &sin6->sin6_addr;
30
31 /*
32 * RFC 4291, Section 2.2.2
33 *
34 * Shorthanded ANY address
35 */
36 if (ipv6_addr_any(addr))
37 return snprintf(buf, buflen, "::");
38
39 /*
40 * RFC 4291, Section 2.2.2
41 *
42 * Shorthanded loopback address
43 */
44 if (ipv6_addr_loopback(addr))
45 return snprintf(buf, buflen, "::1");
46
47 /*
48 * RFC 4291, Section 2.2.3
49 *
50 * Special presentation address format for mapped v4
51 * addresses.
52 */
53 if (ipv6_addr_v4mapped(addr))
54 return snprintf(buf, buflen, "::ffff:%pI4",
55 &addr->s6_addr32[3]);
56
57 /*
58 * RFC 4291, Section 2.2.1
Chuck Levera02d6922009-08-09 15:09:34 -040059 */
Chuck Leverdd1fd902009-12-03 15:58:56 -050060 return snprintf(buf, buflen, "%pI6c", addr);
Chuck Levera02d6922009-08-09 15:09:34 -040061}
62
63static size_t rpc_ntop6(const struct sockaddr *sap,
64 char *buf, const size_t buflen)
65{
66 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
67 char scopebuf[IPV6_SCOPE_ID_LEN];
68 size_t len;
69 int rc;
70
71 len = rpc_ntop6_noscopeid(sap, buf, buflen);
72 if (unlikely(len == 0))
73 return len;
74
Chuck Leverf1a89a12010-01-12 17:41:10 -050075 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
Chuck Levera02d6922009-08-09 15:09:34 -040076 return len;
Chuck Lever7a88efe2010-01-12 17:41:20 -050077 if (sin6->sin6_scope_id == 0)
78 return len;
Chuck Levera02d6922009-08-09 15:09:34 -040079
80 rc = snprintf(scopebuf, sizeof(scopebuf), "%c%u",
81 IPV6_SCOPE_DELIMITER, sin6->sin6_scope_id);
82 if (unlikely((size_t)rc > sizeof(scopebuf)))
83 return 0;
84
85 len += rc;
86 if (unlikely(len > buflen))
87 return 0;
88
89 strcat(buf, scopebuf);
90 return len;
91}
92
93#else /* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
94
95static size_t rpc_ntop6_noscopeid(const struct sockaddr *sap,
96 char *buf, const int buflen)
97{
98 return 0;
99}
100
101static size_t rpc_ntop6(const struct sockaddr *sap,
102 char *buf, const size_t buflen)
103{
104 return 0;
105}
106
107#endif /* !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)) */
108
109static int rpc_ntop4(const struct sockaddr *sap,
110 char *buf, const size_t buflen)
111{
112 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
113
114 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
115}
116
117/**
118 * rpc_ntop - construct a presentation address in @buf
119 * @sap: socket address
120 * @buf: construction area
121 * @buflen: size of @buf, in bytes
122 *
123 * Plants a %NUL-terminated string in @buf and returns the length
124 * of the string, excluding the %NUL. Otherwise zero is returned.
125 */
126size_t rpc_ntop(const struct sockaddr *sap, char *buf, const size_t buflen)
127{
128 switch (sap->sa_family) {
129 case AF_INET:
130 return rpc_ntop4(sap, buf, buflen);
131 case AF_INET6:
132 return rpc_ntop6(sap, buf, buflen);
133 }
134
135 return 0;
136}
137EXPORT_SYMBOL_GPL(rpc_ntop);
138
139static size_t rpc_pton4(const char *buf, const size_t buflen,
140 struct sockaddr *sap, const size_t salen)
141{
142 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
143 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
144
145 if (buflen > INET_ADDRSTRLEN || salen < sizeof(struct sockaddr_in))
146 return 0;
147
148 memset(sap, 0, sizeof(struct sockaddr_in));
149
150 if (in4_pton(buf, buflen, addr, '\0', NULL) == 0)
151 return 0;
152
153 sin->sin_family = AF_INET;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700154 return sizeof(struct sockaddr_in);
Chuck Levera02d6922009-08-09 15:09:34 -0400155}
156
157#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
158static int rpc_parse_scope_id(const char *buf, const size_t buflen,
159 const char *delim, struct sockaddr_in6 *sin6)
160{
161 char *p;
162 size_t len;
163
164 if ((buf + buflen) == delim)
165 return 1;
166
167 if (*delim != IPV6_SCOPE_DELIMITER)
168 return 0;
169
Chuck Leverf1a89a12010-01-12 17:41:10 -0500170 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
Chuck Levera02d6922009-08-09 15:09:34 -0400171 return 0;
172
173 len = (buf + buflen) - delim - 1;
174 p = kstrndup(delim + 1, len, GFP_KERNEL);
175 if (p) {
176 unsigned long scope_id = 0;
177 struct net_device *dev;
178
179 dev = dev_get_by_name(&init_net, p);
180 if (dev != NULL) {
181 scope_id = dev->ifindex;
182 dev_put(dev);
183 } else {
184 if (strict_strtoul(p, 10, &scope_id) == 0) {
185 kfree(p);
186 return 0;
187 }
188 }
189
190 kfree(p);
191
192 sin6->sin6_scope_id = scope_id;
193 return 1;
194 }
195
196 return 0;
197}
198
199static size_t rpc_pton6(const char *buf, const size_t buflen,
200 struct sockaddr *sap, const size_t salen)
201{
202 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
203 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
204 const char *delim;
205
206 if (buflen > (INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN) ||
207 salen < sizeof(struct sockaddr_in6))
208 return 0;
209
210 memset(sap, 0, sizeof(struct sockaddr_in6));
211
212 if (in6_pton(buf, buflen, addr, IPV6_SCOPE_DELIMITER, &delim) == 0)
213 return 0;
214
215 if (!rpc_parse_scope_id(buf, buflen, delim, sin6))
216 return 0;
217
218 sin6->sin6_family = AF_INET6;
219 return sizeof(struct sockaddr_in6);
220}
221#else
222static size_t rpc_pton6(const char *buf, const size_t buflen,
223 struct sockaddr *sap, const size_t salen)
224{
225 return 0;
226}
227#endif
228
229/**
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
235 *
236 * Returns the size of the socket address if successful; otherwise
237 * zero is returned.
238 *
239 * Plants a socket address in @sap and returns the size of the
240 * socket address, if successful. Returns zero if an error
241 * occurred.
242 */
243size_t rpc_pton(const char *buf, const size_t buflen,
244 struct sockaddr *sap, const size_t salen)
245{
246 unsigned int i;
247
248 for (i = 0; i < buflen; i++)
249 if (buf[i] == ':')
250 return rpc_pton6(buf, buflen, sap, salen);
251 return rpc_pton4(buf, buflen, sap, salen);
252}
253EXPORT_SYMBOL_GPL(rpc_pton);
254
255/**
256 * rpc_sockaddr2uaddr - Construct a universal address string from @sap.
257 * @sap: socket address
Trond Myklebustd77385f2011-10-17 16:08:10 -0700258 * @gfp_flags: allocation mode
Chuck Levera02d6922009-08-09 15:09:34 -0400259 *
260 * Returns a %NUL-terminated string in dynamically allocated memory;
261 * otherwise NULL is returned if an error occurred. Caller must
262 * free the returned string.
263 */
Trond Myklebustd77385f2011-10-17 16:08:10 -0700264char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags)
Chuck Levera02d6922009-08-09 15:09:34 -0400265{
266 char portbuf[RPCBIND_MAXUADDRPLEN];
267 char addrbuf[RPCBIND_MAXUADDRLEN];
268 unsigned short port;
269
270 switch (sap->sa_family) {
271 case AF_INET:
272 if (rpc_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
273 return NULL;
274 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
275 break;
276 case AF_INET6:
277 if (rpc_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
278 return NULL;
279 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
280 break;
281 default:
282 return NULL;
283 }
284
285 if (snprintf(portbuf, sizeof(portbuf),
286 ".%u.%u", port >> 8, port & 0xff) > (int)sizeof(portbuf))
287 return NULL;
288
289 if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) > sizeof(addrbuf))
290 return NULL;
291
Trond Myklebustd77385f2011-10-17 16:08:10 -0700292 return kstrdup(addrbuf, gfp_flags);
Chuck Levera02d6922009-08-09 15:09:34 -0400293}
294EXPORT_SYMBOL_GPL(rpc_sockaddr2uaddr);
295
296/**
297 * rpc_uaddr2sockaddr - convert a universal address to a socket address.
298 * @uaddr: C string containing universal address to convert
299 * @uaddr_len: length of universal address string
300 * @sap: buffer into which to plant socket address
301 * @salen: size of buffer
302 *
Chuck Lever1e360a62009-11-13 10:52:55 -0500303 * @uaddr does not have to be '\0'-terminated, but strict_strtoul() and
304 * rpc_pton() require proper string termination to be successful.
305 *
Chuck Levera02d6922009-08-09 15:09:34 -0400306 * Returns the size of the socket address if successful; otherwise
307 * zero is returned.
308 */
309size_t rpc_uaddr2sockaddr(const char *uaddr, const size_t uaddr_len,
310 struct sockaddr *sap, const size_t salen)
311{
Chuck Lever1e360a62009-11-13 10:52:55 -0500312 char *c, buf[RPCBIND_MAXUADDRLEN + sizeof('\0')];
Chuck Levera02d6922009-08-09 15:09:34 -0400313 unsigned long portlo, porthi;
314 unsigned short port;
315
Chuck Lever1e360a62009-11-13 10:52:55 -0500316 if (uaddr_len > RPCBIND_MAXUADDRLEN)
Chuck Levera02d6922009-08-09 15:09:34 -0400317 return 0;
318
319 memcpy(buf, uaddr, uaddr_len);
320
Chuck Lever1e360a62009-11-13 10:52:55 -0500321 buf[uaddr_len] = '\0';
Chuck Levera02d6922009-08-09 15:09:34 -0400322 c = strrchr(buf, '.');
323 if (unlikely(c == NULL))
324 return 0;
325 if (unlikely(strict_strtoul(c + 1, 10, &portlo) != 0))
326 return 0;
327 if (unlikely(portlo > 255))
328 return 0;
329
Chuck Lever1e360a62009-11-13 10:52:55 -0500330 *c = '\0';
Chuck Levera02d6922009-08-09 15:09:34 -0400331 c = strrchr(buf, '.');
332 if (unlikely(c == NULL))
333 return 0;
334 if (unlikely(strict_strtoul(c + 1, 10, &porthi) != 0))
335 return 0;
336 if (unlikely(porthi > 255))
337 return 0;
338
339 port = (unsigned short)((porthi << 8) | portlo);
340
Chuck Lever1e360a62009-11-13 10:52:55 -0500341 *c = '\0';
Chuck Levera02d6922009-08-09 15:09:34 -0400342 if (rpc_pton(buf, strlen(buf), sap, salen) == 0)
343 return 0;
344
345 switch (sap->sa_family) {
346 case AF_INET:
347 ((struct sockaddr_in *)sap)->sin_port = htons(port);
348 return sizeof(struct sockaddr_in);
349 case AF_INET6:
350 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
351 return sizeof(struct sockaddr_in6);
352 }
353
354 return 0;
355}
356EXPORT_SYMBOL_GPL(rpc_uaddr2sockaddr);