1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <time.h>
43 #include <ifaddrs.h>
44
45 #include <arpa/inet.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51
52 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
53
54 #include "ifconfig.h"
55
56 static struct in6_ifreq in6_ridreq;
57 static struct in6_aliasreq in6_addreq =
58 { .ifra_flags = 0,
59 .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
60 static int ip6lifetime;
61
62 static int prefix(void *, int);
63 static char *sec2str(time_t);
64 static int explicit_prefix = 0;
65 extern char *f_inet6, *f_addr;
66
67 extern void setnd6flags(const char *, int, int, const struct afswtch *);
68 extern void setnd6defif(const char *, int, int, const struct afswtch *);
69 extern void nd6_status(int);
70
71 static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/
72
73 static void
setifprefixlen(const char * addr,int dummy __unused,int s,const struct afswtch * afp)74 setifprefixlen(const char *addr, int dummy __unused, int s,
75 const struct afswtch *afp)
76 {
77 if (afp->af_getprefix != NULL)
78 afp->af_getprefix(addr, MASK);
79 explicit_prefix = 1;
80 }
81
82 static void
setip6flags(const char * dummyaddr __unused,int flag,int dummysoc __unused,const struct afswtch * afp)83 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
84 const struct afswtch *afp)
85 {
86 if (afp->af_af != AF_INET6)
87 err(1, "address flags can be set only for inet6 addresses");
88
89 if (flag < 0)
90 in6_addreq.ifra_flags &= ~(-flag);
91 else
92 in6_addreq.ifra_flags |= flag;
93 }
94
95 static void
setip6lifetime(const char * cmd,const char * val,int s,const struct afswtch * afp)96 setip6lifetime(const char *cmd, const char *val, int s,
97 const struct afswtch *afp)
98 {
99 struct timespec now;
100 time_t newval;
101 char *ep;
102
103 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
104 newval = (time_t)strtoul(val, &ep, 0);
105 if (val == ep)
106 errx(1, "invalid %s", cmd);
107 if (afp->af_af != AF_INET6)
108 errx(1, "%s not allowed for the AF", cmd);
109 if (strcmp(cmd, "vltime") == 0) {
110 in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval;
111 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
112 } else if (strcmp(cmd, "pltime") == 0) {
113 in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval;
114 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
115 }
116 }
117
118 static void
setip6pltime(const char * seconds,int dummy __unused,int s,const struct afswtch * afp)119 setip6pltime(const char *seconds, int dummy __unused, int s,
120 const struct afswtch *afp)
121 {
122 setip6lifetime("pltime", seconds, s, afp);
123 }
124
125 static void
setip6vltime(const char * seconds,int dummy __unused,int s,const struct afswtch * afp)126 setip6vltime(const char *seconds, int dummy __unused, int s,
127 const struct afswtch *afp)
128 {
129 setip6lifetime("vltime", seconds, s, afp);
130 }
131
132 static void
setip6eui64(const char * cmd,int dummy __unused,int s,const struct afswtch * afp)133 setip6eui64(const char *cmd, int dummy __unused, int s,
134 const struct afswtch *afp)
135 {
136 struct ifaddrs *ifap, *ifa;
137 const struct sockaddr_in6 *sin6 = NULL;
138 const struct in6_addr *lladdr = NULL;
139 struct in6_addr *in6;
140
141 if (afp->af_af != AF_INET6)
142 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
143 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
144 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
145 errx(EXIT_FAILURE, "interface index is already filled");
146 if (getifaddrs(&ifap) != 0)
147 err(EXIT_FAILURE, "getifaddrs");
148 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
149 if (ifa->ifa_addr->sa_family == AF_INET6 &&
150 strcmp(ifa->ifa_name, name) == 0) {
151 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
152 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
153 lladdr = &sin6->sin6_addr;
154 break;
155 }
156 }
157 }
158 if (!lladdr)
159 errx(EXIT_FAILURE, "could not determine link local address");
160
161 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
162
163 freeifaddrs(ifap);
164 }
165
166 static void
in6_status(int s __unused,const struct ifaddrs * ifa)167 in6_status(int s __unused, const struct ifaddrs *ifa)
168 {
169 struct sockaddr_in6 *sin, null_sin;
170 struct in6_ifreq ifr6;
171 int s6;
172 u_int32_t flags6;
173 struct in6_addrlifetime lifetime;
174 struct timespec now;
175 int error, n_flags;
176
177 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
178
179 memset(&null_sin, 0, sizeof(null_sin));
180
181 sin = (struct sockaddr_in6 *)ifa->ifa_addr;
182 if (sin == NULL)
183 return;
184
185 strlcpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
186 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
187 warn("socket(AF_INET6,SOCK_DGRAM)");
188 return;
189 }
190 ifr6.ifr_addr = *sin;
191 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
192 warn("ioctl(SIOCGIFAFLAG_IN6)");
193 close(s6);
194 return;
195 }
196 flags6 = ifr6.ifr_ifru.ifru_flags6;
197 memset(&lifetime, 0, sizeof(lifetime));
198 ifr6.ifr_addr = *sin;
199 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
200 warn("ioctl(SIOCGIFALIFETIME_IN6)");
201 close(s6);
202 return;
203 }
204 lifetime = ifr6.ifr_ifru.ifru_lifetime;
205 close(s6);
206
207 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
208 n_flags = 0;
209 else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
210 n_flags = NI_NOFQDN;
211 else
212 n_flags = NI_NUMERICHOST;
213 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
214 addr_buf, sizeof(addr_buf), NULL, 0,
215 n_flags);
216 if (error != 0)
217 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
218 sizeof(addr_buf));
219 printf("\tinet6 %s", addr_buf);
220
221 if (ifa->ifa_flags & IFF_POINTOPOINT) {
222 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
223 /*
224 * some of the interfaces do not have valid destination
225 * address.
226 */
227 if (sin != NULL && sin->sin6_family == AF_INET6) {
228 int error;
229
230 error = getnameinfo((struct sockaddr *)sin,
231 sin->sin6_len, addr_buf,
232 sizeof(addr_buf), NULL, 0,
233 NI_NUMERICHOST);
234 if (error != 0)
235 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
236 sizeof(addr_buf));
237 printf(" --> %s", addr_buf);
238 }
239 }
240
241 sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
242 if (sin == NULL)
243 sin = &null_sin;
244 if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
245 printf("/%d", prefix(&sin->sin6_addr,
246 sizeof(struct in6_addr)));
247 else
248 printf(" prefixlen %d", prefix(&sin->sin6_addr,
249 sizeof(struct in6_addr)));
250
251 if ((flags6 & IN6_IFF_ANYCAST) != 0)
252 printf(" anycast");
253 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
254 printf(" tentative");
255 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
256 printf(" duplicated");
257 if ((flags6 & IN6_IFF_DETACHED) != 0)
258 printf(" detached");
259 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
260 printf(" deprecated");
261 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
262 printf(" autoconf");
263 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
264 printf(" temporary");
265 if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
266 printf(" prefer_source");
267
268 if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
269 printf(" scopeid 0x%x",
270 ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
271
272 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
273 printf(" pltime");
274 if (lifetime.ia6t_preferred) {
275 printf(" %s", lifetime.ia6t_preferred < now.tv_sec
276 ? "0" :
277 sec2str(lifetime.ia6t_preferred - now.tv_sec));
278 } else
279 printf(" infty");
280
281 printf(" vltime");
282 if (lifetime.ia6t_expire) {
283 printf(" %s", lifetime.ia6t_expire < now.tv_sec
284 ? "0" :
285 sec2str(lifetime.ia6t_expire - now.tv_sec));
286 } else
287 printf(" infty");
288 }
289
290 print_vhid(ifa, " ");
291
292 putchar('\n');
293 }
294
295 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
296 static struct sockaddr_in6 *sin6tab[] = {
297 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
298 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
299 };
300
301 static void
in6_getprefix(const char * plen,int which)302 in6_getprefix(const char *plen, int which)
303 {
304 struct sockaddr_in6 *sin = sin6tab[which];
305 u_char *cp;
306 int len = atoi(plen);
307
308 if ((len < 0) || (len > 128))
309 errx(1, "%s: bad value", plen);
310 sin->sin6_len = sizeof(*sin);
311 if (which != MASK)
312 sin->sin6_family = AF_INET6;
313 if ((len == 0) || (len == 128)) {
314 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
315 return;
316 }
317 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
318 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
319 *cp++ = 0xff;
320 *cp = 0xff << (8 - len);
321 }
322
323 static void
in6_getaddr(const char * s,int which)324 in6_getaddr(const char *s, int which)
325 {
326 struct sockaddr_in6 *sin = sin6tab[which];
327 struct addrinfo hints, *res;
328 int error = -1;
329
330 newaddr &= 1;
331
332 sin->sin6_len = sizeof(*sin);
333 if (which != MASK)
334 sin->sin6_family = AF_INET6;
335
336 if (which == ADDR) {
337 char *p = NULL;
338 if((p = strrchr(s, '/')) != NULL) {
339 *p = '\0';
340 in6_getprefix(p + 1, MASK);
341 explicit_prefix = 1;
342 }
343 }
344
345 if (sin->sin6_family == AF_INET6) {
346 bzero(&hints, sizeof(struct addrinfo));
347 hints.ai_family = AF_INET6;
348 error = getaddrinfo(s, NULL, &hints, &res);
349 if (error != 0) {
350 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
351 errx(1, "%s: bad value", s);
352 } else {
353 bcopy(res->ai_addr, sin, res->ai_addrlen);
354 freeaddrinfo(res);
355 }
356 }
357 }
358
359 static int
prefix(void * val,int size)360 prefix(void *val, int size)
361 {
362 u_char *name = (u_char *)val;
363 int byte, bit, plen = 0;
364
365 for (byte = 0; byte < size; byte++, plen += 8)
366 if (name[byte] != 0xff)
367 break;
368 if (byte == size)
369 return (plen);
370 for (bit = 7; bit != 0; bit--, plen++)
371 if (!(name[byte] & (1 << bit)))
372 break;
373 for (; bit != 0; bit--)
374 if (name[byte] & (1 << bit))
375 return(0);
376 byte++;
377 for (; byte < size; byte++)
378 if (name[byte])
379 return(0);
380 return (plen);
381 }
382
383 static char *
sec2str(time_t total)384 sec2str(time_t total)
385 {
386 static char result[256];
387 int days, hours, mins, secs;
388 int first = 1;
389 char *p = result;
390
391 if (0) {
392 days = total / 3600 / 24;
393 hours = (total / 3600) % 24;
394 mins = (total / 60) % 60;
395 secs = total % 60;
396
397 if (days) {
398 first = 0;
399 p += sprintf(p, "%dd", days);
400 }
401 if (!first || hours) {
402 first = 0;
403 p += sprintf(p, "%dh", hours);
404 }
405 if (!first || mins) {
406 first = 0;
407 p += sprintf(p, "%dm", mins);
408 }
409 sprintf(p, "%ds", secs);
410 } else
411 sprintf(result, "%lu", (unsigned long)total);
412
413 return(result);
414 }
415
416 static void
in6_postproc(int s,const struct afswtch * afp,int newaddr __unused,int ifflags __unused)417 in6_postproc(int s, const struct afswtch *afp, int newaddr __unused,
418 int ifflags __unused)
419 {
420 if (explicit_prefix == 0) {
421 /* Aggregatable address architecture defines all prefixes
422 are 64. So, it is convenient to set prefixlen to 64 if
423 it is not specified. */
424 setifprefixlen("64", 0, s, afp);
425 /* in6_getprefix("64", MASK) if MASK is available here... */
426 }
427 }
428
429 static void
in6_status_tunnel(int s)430 in6_status_tunnel(int s)
431 {
432 char src[NI_MAXHOST];
433 char dst[NI_MAXHOST];
434 struct in6_ifreq in6_ifr;
435 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
436
437 memset(&in6_ifr, 0, sizeof(in6_ifr));
438 strlcpy(in6_ifr.ifr_name, name, sizeof(in6_ifr.ifr_name));
439
440 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
441 return;
442 if (sa->sa_family != AF_INET6)
443 return;
444 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
445 NI_NUMERICHOST) != 0)
446 src[0] = '\0';
447
448 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
449 return;
450 if (sa->sa_family != AF_INET6)
451 return;
452 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
453 NI_NUMERICHOST) != 0)
454 dst[0] = '\0';
455
456 printf("\ttunnel inet6 %s --> %s\n", src, dst);
457 }
458
459 static void
in6_set_tunnel(int s,struct addrinfo * srcres,struct addrinfo * dstres)460 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
461 {
462 struct in6_aliasreq in6_addreq;
463
464 memset(&in6_addreq, 0, sizeof(in6_addreq));
465 strlcpy(in6_addreq.ifra_name, name, sizeof(in6_addreq.ifra_name));
466 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
467 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
468 dstres->ai_addr->sa_len);
469
470 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
471 warn("SIOCSIFPHYADDR_IN6");
472 }
473
474 static struct cmd inet6_cmds[] = {
475 DEF_CMD_ARG("prefixlen", setifprefixlen),
476 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
477 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
478 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
479 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
480 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
481 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
482 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
483 DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE, setip6flags),
484 DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
485 DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV, setnd6flags),
486 DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV, setnd6flags),
487 DEF_CMD("no_radr", ND6_IFF_NO_RADR, setnd6flags),
488 DEF_CMD("-no_radr", -ND6_IFF_NO_RADR, setnd6flags),
489 DEF_CMD("defaultif", 1, setnd6defif),
490 DEF_CMD("-defaultif", -1, setnd6defif),
491 DEF_CMD("ifdisabled", ND6_IFF_IFDISABLED, setnd6flags),
492 DEF_CMD("-ifdisabled", -ND6_IFF_IFDISABLED, setnd6flags),
493 DEF_CMD("nud", ND6_IFF_PERFORMNUD, setnd6flags),
494 DEF_CMD("-nud", -ND6_IFF_PERFORMNUD, setnd6flags),
495 DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
496 DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
497 DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
498 DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
499 DEF_CMD("no_dad", ND6_IFF_NO_DAD, setnd6flags),
500 DEF_CMD("-no_dad", -ND6_IFF_NO_DAD, setnd6flags),
501 DEF_CMD_ARG("pltime", setip6pltime),
502 DEF_CMD_ARG("vltime", setip6vltime),
503 DEF_CMD("eui64", 0, setip6eui64),
504 #ifdef EXPERIMENTAL
505 DEF_CMD("ipv6_only", ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
506 DEF_CMD("-ipv6_only", -ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
507 #endif
508 };
509
510 static struct afswtch af_inet6 = {
511 .af_name = "inet6",
512 .af_af = AF_INET6,
513 .af_status = in6_status,
514 .af_getaddr = in6_getaddr,
515 .af_getprefix = in6_getprefix,
516 .af_other_status = nd6_status,
517 .af_postproc = in6_postproc,
518 .af_status_tunnel = in6_status_tunnel,
519 .af_settunnel = in6_set_tunnel,
520 .af_difaddr = SIOCDIFADDR_IN6,
521 .af_aifaddr = SIOCAIFADDR_IN6,
522 .af_ridreq = &in6_addreq,
523 .af_addreq = &in6_addreq,
524 };
525
526 static void
in6_Lopt_cb(const char * optarg __unused)527 in6_Lopt_cb(const char *optarg __unused)
528 {
529 ip6lifetime++; /* print IPv6 address lifetime */
530 }
531 static struct option in6_Lopt = {
532 .opt = "L",
533 .opt_usage = "[-L]",
534 .cb = in6_Lopt_cb
535 };
536
537 static __constructor void
inet6_ctor(void)538 inet6_ctor(void)
539 {
540 size_t i;
541
542 #ifndef RESCUE
543 if (!feature_present("inet6"))
544 return;
545 #endif
546
547 for (i = 0; i < nitems(inet6_cmds); i++)
548 cmd_register(&inet6_cmds[i]);
549 af_register(&af_inet6);
550 opt_register(&in6_Lopt);
551 }
552