af_inet6.c revision 1.15 1 /* $NetBSD: af_inet6.c,v 1.15 2008/05/11 22:12:04 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: af_inet6.c,v 1.15 2008/05/11 22:12:04 dyoung Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet6/nd6.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <ifaddrs.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <util.h>
54
55 #include "env.h"
56 #include "parse.h"
57 #include "extern.h"
58 #include "af_inet6.h"
59 #include "af_inetany.h"
60
61 struct in6_ifreq in6_ridreq = {
62 .ifr_addr = {
63 .sin6_family = AF_INET6,
64 .sin6_addr = {
65 .s6_addr =
66 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
67 }
68 }
69 };
70
71 struct in6_aliasreq in6_addreq = {
72 .ifra_prefixmask = {
73 .sin6_len = sizeof(in6_addreq.ifra_prefixmask),
74 .sin6_addr = {
75 .s6_addr =
76 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}},
77 .ifra_lifetime = {
78 .ia6t_pltime = ND6_INFINITE_LIFETIME
79 , .ia6t_vltime = ND6_INFINITE_LIFETIME
80 }
81 };
82
83 static const struct kwinst ia6flagskw[] = {
84 IFKW("anycast", IN6_IFF_ANYCAST)
85 , IFKW("tentative", IN6_IFF_TENTATIVE)
86 , IFKW("deprecated", IN6_IFF_DEPRECATED)
87 };
88
89 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
90 "pltime", 0, NULL, "pltime", &command_root.pb_parser);
91
92 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
93 "vltime", 0, NULL, "vltime", &command_root.pb_parser);
94
95 static const struct kwinst inet6kw[] = {
96 {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
97 , {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
98 , {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
99 .k_bool = true, .k_nextparser = &command_root.pb_parser}
100 };
101
102 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
103 "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
104 struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
105 NULL, inet6kw, __arraycount(inet6kw), NULL);
106
107 static void in6_delscopeid(struct sockaddr_in6 *sin6);
108 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
109 static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
110 struct in6_ifreq *);
111
112 static char *
113 sec2str(time_t total)
114 {
115 static char result[256];
116 int days, hours, mins, secs;
117 int first = 1;
118 char *p = result;
119 char *end = &result[sizeof(result)];
120 int n;
121
122 if (0) { /*XXX*/
123 days = total / 3600 / 24;
124 hours = (total / 3600) % 24;
125 mins = (total / 60) % 60;
126 secs = total % 60;
127
128 if (days) {
129 first = 0;
130 n = snprintf(p, end - p, "%dd", days);
131 if (n < 0 || n >= end - p)
132 return(result);
133 p += n;
134 }
135 if (!first || hours) {
136 first = 0;
137 n = snprintf(p, end - p, "%dh", hours);
138 if (n < 0 || n >= end - p)
139 return(result);
140 p += n;
141 }
142 if (!first || mins) {
143 first = 0;
144 n = snprintf(p, end - p, "%dm", mins);
145 if (n < 0 || n >= end - p)
146 return(result);
147 p += n;
148 }
149 snprintf(p, end - p, "%ds", secs);
150 } else
151 snprintf(p, end - p, "%lu", (u_long)total);
152
153 return(result);
154 }
155
156 static int
157 prefix(void *val, int size)
158 {
159 u_char *pname = (u_char *)val;
160 int byte, bit, plen = 0;
161
162 for (byte = 0; byte < size; byte++, plen += 8)
163 if (pname[byte] != 0xff)
164 break;
165 if (byte == size)
166 return (plen);
167 for (bit = 7; bit != 0; bit--, plen++)
168 if (!(pname[byte] & (1 << bit)))
169 break;
170 for (; bit != 0; bit--)
171 if (pname[byte] & (1 << bit))
172 return(0);
173 byte++;
174 for (; byte < size; byte++)
175 if (pname[byte])
176 return(0);
177 return (plen);
178 }
179
180 int
181 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
182 {
183 int64_t ia6flag;
184
185 if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
186 errno = ENOENT;
187 return -1;
188 }
189
190 if (ia6flag < 0) {
191 ia6flag = -ia6flag;
192 ifra->ifra_flags &= ~ia6flag;
193 } else
194 ifra->ifra_flags |= ia6flag;
195 return 0;
196 }
197
198 int
199 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
200 {
201 int64_t pltime;
202
203 if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
204 errno = ENOENT;
205 return -1;
206 }
207
208 return setia6lifetime(env, pltime,
209 &ifra->ifra_lifetime.ia6t_preferred,
210 &ifra->ifra_lifetime.ia6t_pltime);
211 }
212
213 int
214 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
215 {
216 int64_t vltime;
217
218 if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
219 errno = ENOENT;
220 return -1;
221 }
222
223 return setia6lifetime(env, vltime,
224 &ifra->ifra_lifetime.ia6t_expire,
225 &ifra->ifra_lifetime.ia6t_vltime);
226 }
227
228 static int
229 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
230 uint32_t *ivalp)
231 {
232 time_t t;
233 int af;
234
235 if ((af = getaf(env)) == -1 || af != AF_INET6) {
236 errx(EXIT_FAILURE,
237 "inet6 address lifetime not allowed for the AF");
238 }
239
240 t = time(NULL);
241 *timep = t + val;
242 *ivalp = val;
243 return 0;
244 }
245
246 int
247 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
248 {
249 char buf[2][80];
250 struct ifaddrs *ifap, *ifa;
251 const struct sockaddr_in6 *sin6 = NULL;
252 const struct in6_addr *lladdr = NULL;
253 struct in6_addr *in6;
254 const char *ifname;
255 bool doit = false;
256 int af;
257
258 if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
259 errno = ENOENT;
260 return -1;
261 }
262
263 if ((ifname = getifname(env)) == NULL)
264 return -1;
265
266 af = getaf(env);
267 if (af != AF_INET6) {
268 errx(EXIT_FAILURE,
269 "eui64 address modifier not allowed for the AF");
270 }
271 in6 = &ifra->ifra_addr.sin6_addr;
272 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
273 union {
274 struct sockaddr_in6 sin6;
275 struct sockaddr sa;
276 } any = {.sin6 = {.sin6_family = AF_INET6}};
277 memcpy(&any.sin6.sin6_addr, &in6addr_any,
278 sizeof(any.sin6.sin6_addr));
279 (void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
280 &any.sa);
281 (void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
282 (const struct sockaddr *)&ifra->ifra_addr);
283 errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
284 buf[0], buf[1]);
285 }
286 if (getifaddrs(&ifap) != 0)
287 err(EXIT_FAILURE, "getifaddrs");
288 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
289 if (ifa->ifa_addr->sa_family == AF_INET6 &&
290 strcmp(ifa->ifa_name, ifname) == 0) {
291 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
292 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
293 lladdr = &sin6->sin6_addr;
294 break;
295 }
296 }
297 }
298 if (!lladdr)
299 errx(EXIT_FAILURE, "could not determine link local address");
300
301 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
302
303 freeifaddrs(ifap);
304 return 0;
305 }
306
307 /* KAME idiosyncrasy */
308 static void
309 in6_delscopeid(struct sockaddr_in6 *sin6)
310 {
311 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
312 sin6->sin6_scope_id == 0)
313 return;
314
315 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = htons(sin6->sin6_scope_id);
316 sin6->sin6_scope_id = 0;
317 }
318
319 /* KAME idiosyncrasy */
320 void
321 in6_fillscopeid(struct sockaddr_in6 *sin6)
322 {
323 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
324 sin6->sin6_scope_id =
325 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
326 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
327 }
328 }
329
330 /* XXX not really an alias */
331 void
332 in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
333 struct in6_ifreq *creq)
334 {
335 struct in6_ifreq ifr6;
336 struct sockaddr_in6 *sin6;
337 char hbuf[NI_MAXHOST];
338 u_int32_t scopeid;
339 int s;
340 const int niflag = NI_NUMERICHOST;
341 unsigned short flags;
342
343 /* Get the non-alias address for this interface. */
344 if ((s = getsock(AF_INET6)) == -1) {
345 if (errno == EAFNOSUPPORT)
346 return;
347 err(EXIT_FAILURE, "socket");
348 }
349
350 sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
351
352 in6_fillscopeid(sin6);
353 scopeid = sin6->sin6_scope_id;
354 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
355 hbuf, sizeof(hbuf), NULL, 0, niflag))
356 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
357 printf("\tinet6 %s", hbuf);
358
359 if (getifflags(env, oenv, &flags) == -1)
360 err(EXIT_FAILURE, "%s: getifflags", __func__);
361
362 if (flags & IFF_POINTOPOINT) {
363 memset(&ifr6, 0, sizeof(ifr6));
364 estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
365 ifr6.ifr_addr = creq->ifr_addr;
366 if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
367 if (errno != EADDRNOTAVAIL)
368 warn("SIOCGIFDSTADDR_IN6");
369 memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
370 ifr6.ifr_addr.sin6_family = AF_INET6;
371 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
372 }
373 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
374 in6_fillscopeid(sin6);
375 hbuf[0] = '\0';
376 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
377 hbuf, sizeof(hbuf), NULL, 0, niflag))
378 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
379 printf(" -> %s", hbuf);
380 }
381
382 memset(&ifr6, 0, sizeof(ifr6));
383 estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
384 ifr6.ifr_addr = creq->ifr_addr;
385 if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
386 if (errno != EADDRNOTAVAIL)
387 warn("SIOCGIFNETMASK_IN6");
388 } else {
389 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
390 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
391 sizeof(struct in6_addr)));
392 }
393
394 memset(&ifr6, 0, sizeof(ifr6));
395 estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
396 ifr6.ifr_addr = creq->ifr_addr;
397 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
398 if (errno != EADDRNOTAVAIL)
399 warn("SIOCGIFAFLAG_IN6");
400 } else {
401 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
402 printf(" anycast");
403 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
404 printf(" tentative");
405 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
406 printf(" duplicated");
407 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
408 printf(" detached");
409 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
410 printf(" deprecated");
411 }
412
413 if (scopeid)
414 printf(" scopeid 0x%x", scopeid);
415
416 if (Lflag) {
417 struct in6_addrlifetime *lifetime;
418 memset(&ifr6, 0, sizeof(ifr6));
419 estrlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name));
420 ifr6.ifr_addr = creq->ifr_addr;
421 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
422 if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
423 if (errno != EADDRNOTAVAIL)
424 warn("SIOCGIFALIFETIME_IN6");
425 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
426 time_t t = time(NULL);
427 printf(" pltime ");
428 if (lifetime->ia6t_preferred) {
429 printf("%s", lifetime->ia6t_preferred < t
430 ? "0"
431 : sec2str(lifetime->ia6t_preferred - t));
432 } else
433 printf("infty");
434
435 printf(" vltime ");
436 if (lifetime->ia6t_expire) {
437 printf("%s", lifetime->ia6t_expire < t
438 ? "0"
439 : sec2str(lifetime->ia6t_expire - t));
440 } else
441 printf("infty");
442 }
443 }
444
445 printf("\n");
446 }
447
448 void
449 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
450 {
451 struct ifaddrs *ifap, *ifa;
452 struct in6_ifreq ifr;
453 const char *ifname;
454
455 if ((ifname = getifname(env)) == NULL)
456 err(EXIT_FAILURE, "%s: getifname", __func__);
457
458 if (getifaddrs(&ifap) != 0)
459 err(EXIT_FAILURE, "getifaddrs");
460 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
461 if (strcmp(ifname, ifa->ifa_name) != 0)
462 continue;
463 if (ifa->ifa_addr->sa_family != AF_INET6)
464 continue;
465 if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
466 continue;
467
468 memset(&ifr, 0, sizeof(ifr));
469 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
470 memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
471 in6_alias(ifname, env, oenv, &ifr);
472 }
473 freeifaddrs(ifap);
474 }
475
476 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
477 struct sockaddr_in6 *sin6tab[] = {
478 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
479 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
480
481 static int
482 in6_pre_aifaddr(prop_dictionary_t env, struct afparam *param)
483 {
484 struct in6_aliasreq *ifra = param->req.buf;
485
486 setia6eui64_impl(env, ifra);
487 setia6vltime_impl(env, ifra);
488 setia6pltime_impl(env, ifra);
489 setia6flags_impl(env, ifra);
490 in6_delscopeid(&ifra->ifra_addr);
491 in6_delscopeid(&ifra->ifra_dstaddr);
492
493 return 0;
494 }
495
496 void
497 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
498 {
499 struct in6_ifreq in6_ifr = {
500 .ifr_addr = {
501 .sin6_family = AF_INET6,
502 .sin6_addr = {
503 .s6_addr =
504 {0xff, 0xff, 0xff, 0xff,
505 0xff, 0xff, 0xff, 0xff}
506 }
507 }
508 };
509 static struct sockaddr_in6 in6_defmask = {
510 .sin6_addr = {
511 .s6_addr = {0xff, 0xff, 0xff, 0xff,
512 0xff, 0xff, 0xff, 0xff}
513 }
514 };
515
516 struct in6_aliasreq in6_ifra = {
517 .ifra_prefixmask = {
518 .sin6_addr = {
519 .s6_addr =
520 {0xff, 0xff, 0xff, 0xff,
521 0xff, 0xff, 0xff, 0xff}}},
522 .ifra_lifetime = {
523 .ia6t_pltime = ND6_INFINITE_LIFETIME
524 , .ia6t_vltime = ND6_INFINITE_LIFETIME
525 }
526 };
527 struct afparam in6param = {
528 .req = BUFPARAM(in6_ifra)
529 , .dgreq = BUFPARAM(in6_ifr)
530 , .name = {
531 {.buf = in6_ifr.ifr_name,
532 .buflen = sizeof(in6_ifr.ifr_name)},
533 {.buf = in6_ifra.ifra_name,
534 .buflen = sizeof(in6_ifra.ifra_name)}
535 }
536 , .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
537 , .addr = BUFPARAM(in6_ifra.ifra_addr)
538 , .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
539 , .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
540 , .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
541 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
542 , .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
543 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
544 , .defmask = BUFPARAM(in6_defmask)
545 , .pre_aifaddr = in6_pre_aifaddr
546 };
547 commit_address(env, oenv, &in6param);
548 }
549