af_inet6.c revision 1.5.18.2 1 /* $NetBSD: af_inet6.c,v 1.5.18.2 2008/09/28 11:17:11 mjf 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.5.18.2 2008/09/28 11:17:11 mjf 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 "extern.h"
57 #include "parse.h"
58 #include "extern.h"
59 #include "af_inetany.h"
60
61 static void in6_constructor(void) __attribute__((constructor));
62 static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
63 struct in6_ifreq *);
64 static void in6_commit_address(prop_dictionary_t, prop_dictionary_t);
65
66 static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *);
67 static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *);
68 static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *);
69 static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *);
70
71 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
72
73 static void in6_delscopeid(struct sockaddr_in6 *sin6);
74 static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
75
76 static struct usage_func usage;
77 static cmdloop_branch_t branch[2];
78
79 static const struct kwinst ia6flagskw[] = {
80 IFKW("anycast", IN6_IFF_ANYCAST)
81 , IFKW("tentative", IN6_IFF_TENTATIVE)
82 , IFKW("deprecated", IN6_IFF_DEPRECATED)
83 };
84
85 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
86 "pltime", 0, NULL, "pltime", &command_root.pb_parser);
87
88 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
89 "vltime", 0, NULL, "vltime", &command_root.pb_parser);
90
91 static const struct kwinst inet6kw[] = {
92 {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
93 , {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
94 , {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
95 .k_bool = true, .k_nextparser = &command_root.pb_parser}
96 };
97
98 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
99 "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
100 struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
101 NULL, inet6kw, __arraycount(inet6kw), NULL);
102
103 static struct afswtch in6af = {
104 .af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
105 .af_addr_commit = in6_commit_address
106 };
107
108 static int
109 prefix(void *val, int size)
110 {
111 u_char *pname = (u_char *)val;
112 int byte, bit, plen = 0;
113
114 for (byte = 0; byte < size; byte++, plen += 8)
115 if (pname[byte] != 0xff)
116 break;
117 if (byte == size)
118 return (plen);
119 for (bit = 7; bit != 0; bit--, plen++)
120 if (!(pname[byte] & (1 << bit)))
121 break;
122 for (; bit != 0; bit--)
123 if (pname[byte] & (1 << bit))
124 return(0);
125 byte++;
126 for (; byte < size; byte++)
127 if (pname[byte])
128 return(0);
129 return (plen);
130 }
131
132 int
133 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
134 {
135 int64_t ia6flag;
136
137 if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
138 errno = ENOENT;
139 return -1;
140 }
141
142 if (ia6flag < 0) {
143 ia6flag = -ia6flag;
144 ifra->ifra_flags &= ~ia6flag;
145 } else
146 ifra->ifra_flags |= ia6flag;
147 return 0;
148 }
149
150 int
151 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
152 {
153 int64_t pltime;
154
155 if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
156 errno = ENOENT;
157 return -1;
158 }
159
160 return setia6lifetime(env, pltime,
161 &ifra->ifra_lifetime.ia6t_preferred,
162 &ifra->ifra_lifetime.ia6t_pltime);
163 }
164
165 int
166 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
167 {
168 int64_t vltime;
169
170 if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
171 errno = ENOENT;
172 return -1;
173 }
174
175 return setia6lifetime(env, vltime,
176 &ifra->ifra_lifetime.ia6t_expire,
177 &ifra->ifra_lifetime.ia6t_vltime);
178 }
179
180 static int
181 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
182 uint32_t *ivalp)
183 {
184 time_t t;
185 int af;
186
187 if ((af = getaf(env)) == -1 || af != AF_INET6) {
188 errx(EXIT_FAILURE,
189 "inet6 address lifetime not allowed for the AF");
190 }
191
192 t = time(NULL);
193 *timep = t + val;
194 *ivalp = val;
195 return 0;
196 }
197
198 int
199 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
200 {
201 char buf[2][80];
202 struct ifaddrs *ifap, *ifa;
203 const struct sockaddr_in6 *sin6 = NULL;
204 const struct in6_addr *lladdr = NULL;
205 struct in6_addr *in6;
206 const char *ifname;
207 bool doit = false;
208 int af;
209
210 if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
211 errno = ENOENT;
212 return -1;
213 }
214
215 if ((ifname = getifname(env)) == NULL)
216 return -1;
217
218 af = getaf(env);
219 if (af != AF_INET6) {
220 errx(EXIT_FAILURE,
221 "eui64 address modifier not allowed for the AF");
222 }
223 in6 = &ifra->ifra_addr.sin6_addr;
224 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
225 union {
226 struct sockaddr_in6 sin6;
227 struct sockaddr sa;
228 } any = {.sin6 = {.sin6_family = AF_INET6}};
229 memcpy(&any.sin6.sin6_addr, &in6addr_any,
230 sizeof(any.sin6.sin6_addr));
231 (void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
232 &any.sa);
233 (void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
234 (const struct sockaddr *)&ifra->ifra_addr);
235 errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
236 buf[0], buf[1]);
237 }
238 if (getifaddrs(&ifap) != 0)
239 err(EXIT_FAILURE, "getifaddrs");
240 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
241 if (ifa->ifa_addr->sa_family == AF_INET6 &&
242 strcmp(ifa->ifa_name, ifname) == 0) {
243 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
244 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
245 lladdr = &sin6->sin6_addr;
246 break;
247 }
248 }
249 }
250 if (!lladdr)
251 errx(EXIT_FAILURE, "could not determine link local address");
252
253 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
254
255 freeifaddrs(ifap);
256 return 0;
257 }
258
259 /* KAME idiosyncrasy */
260 static void
261 in6_delscopeid(struct sockaddr_in6 *sin6)
262 {
263 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
264 sin6->sin6_scope_id == 0)
265 return;
266
267 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = htons(sin6->sin6_scope_id);
268 sin6->sin6_scope_id = 0;
269 }
270
271 /* XXX not really an alias */
272 void
273 in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
274 struct in6_ifreq *creq)
275 {
276 struct in6_ifreq ifr6;
277 struct sockaddr_in6 *sin6;
278 char hbuf[NI_MAXHOST];
279 u_int32_t scopeid;
280 int s;
281 const int niflag = NI_NUMERICHOST;
282 unsigned short flags;
283
284 /* Get the non-alias address for this interface. */
285 if ((s = getsock(AF_INET6)) == -1) {
286 if (errno == EAFNOSUPPORT)
287 return;
288 err(EXIT_FAILURE, "socket");
289 }
290
291 sin6 = &creq->ifr_addr;
292
293 in6_fillscopeid(sin6);
294 scopeid = sin6->sin6_scope_id;
295 if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
296 hbuf, sizeof(hbuf), NULL, 0, niflag))
297 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
298 printf("\tinet6 %s", hbuf);
299
300 if (getifflags(env, oenv, &flags) == -1)
301 err(EXIT_FAILURE, "%s: getifflags", __func__);
302
303 if (flags & IFF_POINTOPOINT) {
304 ifr6 = *creq;
305 if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
306 if (errno != EADDRNOTAVAIL)
307 warn("SIOCGIFDSTADDR_IN6");
308 memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
309 ifr6.ifr_addr.sin6_family = AF_INET6;
310 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
311 }
312 sin6 = &ifr6.ifr_addr;
313 in6_fillscopeid(sin6);
314 hbuf[0] = '\0';
315 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
316 hbuf, sizeof(hbuf), NULL, 0, niflag))
317 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
318 printf(" -> %s", hbuf);
319 }
320
321 ifr6 = *creq;
322 if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
323 if (errno != EADDRNOTAVAIL)
324 warn("SIOCGIFNETMASK_IN6");
325 } else {
326 sin6 = &ifr6.ifr_addr;
327 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
328 sizeof(struct in6_addr)));
329 }
330
331 ifr6 = *creq;
332 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
333 if (errno != EADDRNOTAVAIL)
334 warn("SIOCGIFAFLAG_IN6");
335 } else {
336 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
337 printf(" anycast");
338 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
339 printf(" tentative");
340 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
341 printf(" duplicated");
342 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
343 printf(" detached");
344 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
345 printf(" deprecated");
346 }
347
348 if (scopeid)
349 printf(" scopeid 0x%x", scopeid);
350
351 if (get_flag('L')) {
352 struct in6_addrlifetime *lifetime;
353 ifr6 = *creq;
354 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
355 if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
356 if (errno != EADDRNOTAVAIL)
357 warn("SIOCGIFALIFETIME_IN6");
358 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
359 time_t t = time(NULL);
360 printf(" pltime ");
361 if (lifetime->ia6t_preferred) {
362 printf("%lu",
363 (unsigned long)(lifetime->ia6t_preferred -
364 MIN(t, lifetime->ia6t_preferred)));
365 } else
366 printf("infty");
367
368 printf(" vltime ");
369 if (lifetime->ia6t_expire) {
370 printf("%lu",
371 (unsigned long)(lifetime->ia6t_expire -
372 MIN(t, lifetime->ia6t_expire)));
373 } else
374 printf("infty");
375 }
376 }
377
378 printf("\n");
379 }
380
381 static void
382 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
383 {
384 struct ifaddrs *ifap, *ifa;
385 struct in6_ifreq ifr;
386 const char *ifname;
387
388 if ((ifname = getifname(env)) == NULL)
389 err(EXIT_FAILURE, "%s: getifname", __func__);
390
391 if (getifaddrs(&ifap) != 0)
392 err(EXIT_FAILURE, "getifaddrs");
393 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
394 if (strcmp(ifname, ifa->ifa_name) != 0)
395 continue;
396 if (ifa->ifa_addr->sa_family != AF_INET6)
397 continue;
398 if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
399 continue;
400
401 memset(&ifr, 0, sizeof(ifr));
402 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
403 memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
404 in6_alias(ifname, env, oenv, &ifr);
405 }
406 freeifaddrs(ifap);
407 }
408
409 static int
410 in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
411 {
412 struct in6_aliasreq *ifra = param->req.buf;
413
414 setia6eui64_impl(env, ifra);
415 setia6vltime_impl(env, ifra);
416 setia6pltime_impl(env, ifra);
417 setia6flags_impl(env, ifra);
418 in6_delscopeid(&ifra->ifra_addr);
419 in6_delscopeid(&ifra->ifra_dstaddr);
420
421 return 0;
422 }
423
424 static void
425 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
426 {
427 struct in6_ifreq in6_ifr = {
428 .ifr_addr = {
429 .sin6_family = AF_INET6,
430 .sin6_len = sizeof(in6_ifr.ifr_addr),
431 .sin6_addr = {
432 .s6_addr =
433 {0xff, 0xff, 0xff, 0xff,
434 0xff, 0xff, 0xff, 0xff}
435 }
436 }
437 };
438 static struct sockaddr_in6 in6_defmask = {
439 .sin6_family = AF_INET6,
440 .sin6_len = sizeof(in6_defmask),
441 .sin6_addr = {
442 .s6_addr = {0xff, 0xff, 0xff, 0xff,
443 0xff, 0xff, 0xff, 0xff}
444 }
445 };
446
447 struct in6_aliasreq in6_ifra = {
448 .ifra_prefixmask = {
449 .sin6_family = AF_INET6,
450 .sin6_len = sizeof(in6_ifra.ifra_prefixmask),
451 .sin6_addr = {
452 .s6_addr =
453 {0xff, 0xff, 0xff, 0xff,
454 0xff, 0xff, 0xff, 0xff}}},
455 .ifra_lifetime = {
456 .ia6t_pltime = ND6_INFINITE_LIFETIME
457 , .ia6t_vltime = ND6_INFINITE_LIFETIME
458 }
459 };
460 struct afparam in6param = {
461 .req = BUFPARAM(in6_ifra)
462 , .dgreq = BUFPARAM(in6_ifr)
463 , .name = {
464 {.buf = in6_ifr.ifr_name,
465 .buflen = sizeof(in6_ifr.ifr_name)},
466 {.buf = in6_ifra.ifra_name,
467 .buflen = sizeof(in6_ifra.ifra_name)}
468 }
469 , .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
470 , .addr = BUFPARAM(in6_ifra.ifra_addr)
471 , .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
472 , .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
473 , .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
474 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
475 , .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
476 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
477 , .defmask = BUFPARAM(in6_defmask)
478 , .pre_aifaddr = in6_pre_aifaddr
479 };
480 commit_address(env, oenv, &in6param);
481 }
482
483 static void
484 in6_usage(prop_dictionary_t env)
485 {
486 fprintf(stderr,
487 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
488 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] "
489 "[ eui64 ]\n");
490 }
491
492 static void
493 in6_constructor(void)
494 {
495 if (register_flag('L') != 0)
496 err(EXIT_FAILURE, __func__);
497 register_family(&in6af);
498 usage_func_init(&usage, in6_usage);
499 register_usage(&usage);
500 cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
501 cmdloop_branch_init(&branch[1], &inet6.pk_parser);
502 register_cmdloop_branch(&branch[0]);
503 register_cmdloop_branch(&branch[1]);
504 }
505