]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/af_inet6.c
ifconfig: fix warnings #2
[FreeBSD/FreeBSD.git] / sbin / ifconfig / af_inet6.c
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 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD$";
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <time.h>
48 #include <ifaddrs.h>
49
50 #include <arpa/inet.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56
57 #include <netinet6/nd6.h>       /* Define ND6_INFINITE_LIFETIME */
58
59 #include "ifconfig.h"
60 #include "ifconfig_netlink.h"
61
62 #ifndef WITHOUT_NETLINK
63 struct in6_px {
64         struct in6_addr         addr;
65         int                     plen;
66         bool                    set;
67 };
68 struct in6_pdata {
69         struct in6_px           addr;
70         struct in6_px           dst_addr;
71         struct in6_addrlifetime lifetime;
72         uint32_t                flags;
73         uint32_t                vhid;
74 };
75
76 static struct in6_pdata in6_del;
77 static struct in6_pdata in6_add = {
78         .lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME },
79 };
80 #else
81 static  struct in6_ifreq in6_ridreq;
82 static  struct in6_aliasreq in6_addreq =
83   { .ifra_flags = 0,
84     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
85 #endif
86 static  int ip6lifetime;
87
88 #ifdef WITHOUT_NETLINK
89 static  int prefix(void *, int);
90 #endif
91 static  char *sec2str(time_t);
92 static  int explicit_prefix = 0;
93 extern  char *f_inet6, *f_addr;
94
95 extern void setnd6flags(if_ctx *, const char *, int);
96 extern void setnd6defif(if_ctx *,const char *, int);
97 extern void nd6_status(if_ctx *);
98
99 static  char addr_buf[NI_MAXHOST];      /*for getnameinfo()*/
100
101 static void
102 setifprefixlen(if_ctx *ctx __netlink_unused, const char *addr, int dummy __unused)
103 {
104 #ifdef WITHOUT_NETLINK
105         const struct afswtch *afp = ctx->afp;
106
107         if (afp->af_getprefix != NULL)
108                 afp->af_getprefix(addr, MASK);
109 #else
110         int plen = strtol(addr, NULL, 10);
111
112         if ((plen < 0) || (plen > 128))
113                 errx(1, "%s: bad value", addr);
114         in6_add.addr.plen = plen;
115 #endif
116         explicit_prefix = 1;
117 }
118
119 static void
120 setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
121 {
122         const struct afswtch *afp = ctx->afp;
123
124         if (afp->af_af != AF_INET6)
125                 err(1, "address flags can be set only for inet6 addresses");
126
127 #ifdef WITHOUT_NETLINK
128         if (flag < 0)
129                 in6_addreq.ifra_flags &= ~(-flag);
130         else
131                 in6_addreq.ifra_flags |= flag;
132 #else
133         if (flag < 0)
134                 in6_add.flags &= ~(-flag);
135         else
136                 in6_add.flags |= flag;
137 #endif
138 }
139
140 static void
141 setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
142 {
143         const struct afswtch *afp = ctx->afp;
144         struct timespec now;
145         time_t newval;
146         char *ep;
147 #ifdef WITHOUT_NETLINK
148         struct in6_addrlifetime *lifetime = &in6_addreq.ifra_lifetime;
149 #else
150         struct in6_addrlifetime *lifetime = &in6_add.lifetime;
151 #endif
152
153         clock_gettime(CLOCK_MONOTONIC_FAST, &now);
154         newval = (time_t)strtoul(val, &ep, 0);
155         if (val == ep)
156                 errx(1, "invalid %s", cmd);
157         if (afp->af_af != AF_INET6)
158                 errx(1, "%s not allowed for the AF", cmd);
159         if (strcmp(cmd, "vltime") == 0) {
160                 lifetime->ia6t_expire = now.tv_sec + newval;
161                 lifetime->ia6t_vltime = newval;
162         } else if (strcmp(cmd, "pltime") == 0) {
163                 lifetime->ia6t_preferred = now.tv_sec + newval;
164                 lifetime->ia6t_pltime = newval;
165         }
166 }
167
168 static void
169 setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
170 {
171         setip6lifetime(ctx, "pltime", seconds);
172 }
173
174 static void
175 setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
176 {
177         setip6lifetime(ctx, "vltime", seconds);
178 }
179
180 static void
181 setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
182 {
183         const struct afswtch *afp = ctx->afp;
184         struct ifaddrs *ifap, *ifa;
185         const struct sockaddr_in6 *sin6 = NULL;
186         const struct in6_addr *lladdr = NULL;
187         struct in6_addr *in6;
188
189         if (afp->af_af != AF_INET6)
190                 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
191 #ifdef WITHOUT_NETLINK
192         in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
193 #else
194         in6 = &in6_add.addr.addr;
195 #endif
196         if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
197                 errx(EXIT_FAILURE, "interface index is already filled");
198         if (getifaddrs(&ifap) != 0)
199                 err(EXIT_FAILURE, "getifaddrs");
200         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
201                 if (ifa->ifa_addr->sa_family == AF_INET6 &&
202                     strcmp(ifa->ifa_name, name) == 0) {
203                         sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
204                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
205                                 lladdr = &sin6->sin6_addr;
206                                 break;
207                         }
208                 }
209         }
210         if (!lladdr)
211                 errx(EXIT_FAILURE, "could not determine link local address"); 
212
213         memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
214
215         freeifaddrs(ifap);
216 }
217
218 static void
219 print_addr(struct sockaddr_in6 *sin)
220 {
221         int error, n_flags;
222
223         if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
224                 n_flags = 0;
225         else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
226                 n_flags = NI_NOFQDN;
227         else
228                 n_flags = NI_NUMERICHOST;
229         error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
230                             addr_buf, sizeof(addr_buf), NULL, 0,
231                             n_flags);
232         if (error != 0)
233                 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
234                           sizeof(addr_buf));
235         printf("\tinet6 %s", addr_buf);
236 }
237
238 static void
239 print_p2p(struct sockaddr_in6 *sin)
240 {
241         int error;
242
243         error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
244             sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
245
246         if (error != 0)
247                 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf));
248         printf(" --> %s", addr_buf);
249 }
250
251 static void
252 print_mask(int plen)
253 {
254         if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
255                 printf("/%d", plen);
256         else
257                 printf(" prefixlen %d", plen);
258 }
259
260 static void
261 print_flags(int flags6)
262 {
263         if ((flags6 & IN6_IFF_ANYCAST) != 0)
264                 printf(" anycast");
265         if ((flags6 & IN6_IFF_TENTATIVE) != 0)
266                 printf(" tentative");
267         if ((flags6 & IN6_IFF_DUPLICATED) != 0)
268                 printf(" duplicated");
269         if ((flags6 & IN6_IFF_DETACHED) != 0)
270                 printf(" detached");
271         if ((flags6 & IN6_IFF_DEPRECATED) != 0)
272                 printf(" deprecated");
273         if ((flags6 & IN6_IFF_AUTOCONF) != 0)
274                 printf(" autoconf");
275         if ((flags6 & IN6_IFF_TEMPORARY) != 0)
276                 printf(" temporary");
277         if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
278                 printf(" prefer_source");
279
280 }
281
282 static void
283 print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
284 {
285         printf(" %s", prepend);
286         if (px_time == 0)
287                 printf(" infty");
288
289         printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec));
290 }
291
292 #ifdef WITHOUT_NETLINK
293 static void
294 in6_status(if_ctx *ctx __unused, const struct ifaddrs *ifa)
295 {
296         struct sockaddr_in6 *sin, null_sin = {};
297         struct in6_ifreq ifr6;
298         int s6;
299         u_int32_t flags6;
300         struct in6_addrlifetime lifetime;
301
302         sin = (struct sockaddr_in6 *)ifa->ifa_addr;
303         if (sin == NULL)
304                 return;
305
306         strlcpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
307         if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
308                 warn("socket(AF_INET6,SOCK_DGRAM)");
309                 return;
310         }
311         ifr6.ifr_addr = *sin;
312         if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
313                 warn("ioctl(SIOCGIFAFLAG_IN6)");
314                 close(s6);
315                 return;
316         }
317         flags6 = ifr6.ifr_ifru.ifru_flags6;
318         memset(&lifetime, 0, sizeof(lifetime));
319         ifr6.ifr_addr = *sin;
320         if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
321                 warn("ioctl(SIOCGIFALIFETIME_IN6)");
322                 close(s6);
323                 return;
324         }
325         lifetime = ifr6.ifr_ifru.ifru_lifetime;
326         close(s6);
327
328         print_addr(sin);
329
330         if (ifa->ifa_flags & IFF_POINTOPOINT) {
331                 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
332                 /*
333                  * some of the interfaces do not have valid destination
334                  * address.
335                  */
336                 if (sin != NULL && sin->sin6_family == AF_INET6)
337                         print_p2p(sin);
338         }
339
340         sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
341         if (sin == NULL)
342                 sin = &null_sin;
343         print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr)));
344
345         print_flags(flags6);
346
347         if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
348                 printf(" scopeid 0x%x",
349                     ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
350
351         if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
352                 struct timespec now;
353
354                 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
355                 print_lifetime("pltime", lifetime.ia6t_preferred, &now);
356                 print_lifetime("vltime", lifetime.ia6t_expire, &now);
357         }
358
359         print_vhid(ifa, " ");
360
361         putchar('\n');
362 }
363
364 #else
365 static void
366 show_lifetime(struct ifa_cacheinfo *ci)
367 {
368         struct timespec now;
369         uint32_t pl, vl;
370
371         if (ci == NULL)
372                 return;
373
374         int count = ci->ifa_prefered != ND6_INFINITE_LIFETIME;
375         count += ci->ifa_valid != ND6_INFINITE_LIFETIME;
376         if (count == 0)
377                 return;
378
379         pl = (ci->ifa_prefered == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_prefered;
380         vl = (ci->ifa_valid == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_valid;
381
382         clock_gettime(CLOCK_MONOTONIC_FAST, &now);
383         print_lifetime("pltime", pl + now.tv_sec, &now);
384         print_lifetime("vltime", vl + now.tv_sec, &now);
385 }
386
387 static void
388 in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
389 {
390         int plen = ifa->ifa_prefixlen;
391         uint32_t scopeid;
392
393         if (ifa->ifa_local == NULL) {
394                 /* Non-P2P address */
395                 scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
396                 print_addr(satosin6(ifa->ifa_address));
397         } else {
398                 scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
399                 print_addr(satosin6(ifa->ifa_local));
400                 print_p2p(satosin6(ifa->ifa_address));
401         }
402
403         print_mask(plen);
404         print_flags(ifa->ifaf_flags);
405
406         if (scopeid != 0)
407                 printf(" scopeid 0x%x", scopeid);
408
409         show_lifetime(ifa->ifa_cacheinfo);
410
411         if (ifa->ifaf_vhid != 0)
412                 printf(" vhid %d", ifa->ifaf_vhid);
413
414         putchar('\n');
415 }
416
417 static struct in6_px *sin6tab_nl[] = {
418         &in6_del.addr,          /* RIDADDR */
419         &in6_add.addr,          /* ADDR */
420         NULL,                   /* MASK */
421         &in6_add.dst_addr,      /* DSTADDR*/
422 };
423
424 static void
425 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
426 {
427         sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
428         sin6tab_nl[to]->set = sin6tab_nl[from]->set;
429 }
430
431 static void
432 in6_getaddr(const char *addr_str, int which)
433 {
434         struct in6_px *px = sin6tab_nl[which];
435
436         newaddr &= 1;
437
438         px->set = true;
439         px->plen = 128;
440         if (which == ADDR) {
441                 char *p = NULL;
442                 if((p = strrchr(addr_str, '/')) != NULL) {
443                         *p = '\0';
444                         int plen = strtol(p + 1, NULL, 10);
445                         if (plen < 0 || plen > 128)
446                                 errx(1, "%s: bad value", p + 1);
447                         px->plen = plen;
448                         explicit_prefix = 1;
449                 }
450         }
451
452         struct addrinfo hints = { .ai_family = AF_INET6 };
453         struct addrinfo *res;
454
455         int error = getaddrinfo(addr_str, NULL, &hints, &res);
456         if (error != 0) {
457                 if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
458                         errx(1, "%s: bad value", addr_str);
459         } else {
460                 struct sockaddr_in6 *sin6;
461
462                 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
463                 px->addr = sin6->sin6_addr;
464                 freeaddrinfo(res);
465         }
466 }
467
468 static int
469 in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
470 {
471         struct in6_pdata *pdata = (struct in6_pdata *)data;
472         struct snl_writer nw = {};
473
474         snl_init_writer(ctx->io_ss, &nw);
475         struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
476         struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);
477
478         ifahdr->ifa_family = AF_INET6;
479         ifahdr->ifa_prefixlen = pdata->addr.plen;
480         ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, name);
481
482         snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
483         if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
484                 snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);
485
486         struct ifa_cacheinfo ci = {
487                 .ifa_prefered = pdata->lifetime.ia6t_pltime,
488                 .ifa_valid =  pdata->lifetime.ia6t_vltime,
489         };
490         snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);
491
492         int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
493         snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
494         if (pdata->vhid != 0)
495                 snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
496         snl_end_attr_nested(&nw, off);
497
498         if (!snl_finalize_msg(&nw) || !snl_send_message(ctx->io_ss, hdr))
499                 return (0);
500
501         struct snl_errmsg_data e = {};
502         snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);
503
504         return (e.error);
505 }
506 #endif
507
508 #ifdef WITHOUT_NETLINK
509 static struct   sockaddr_in6 *sin6tab[] = {
510         &in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
511         &in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
512 };
513
514 static void
515 in6_copyaddr(if_ctx *ctx, int to, int from)
516 {
517         memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
518 }
519
520 static void
521 in6_getprefix(const char *plen, int which)
522 {
523         struct sockaddr_in6 *sin = sin6tab[which];
524         u_char *cp;
525         int len = atoi(plen);
526
527         if ((len < 0) || (len > 128))
528                 errx(1, "%s: bad value", plen);
529         sin->sin6_len = sizeof(*sin);
530         if (which != MASK)
531                 sin->sin6_family = AF_INET6;
532         if ((len == 0) || (len == 128)) {
533                 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
534                 return;
535         }
536         memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
537         for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
538                 *cp++ = 0xff;
539         *cp = 0xff << (8 - len);
540 }
541
542 static void
543 in6_getaddr(const char *s, int which)
544 {
545         struct sockaddr_in6 *sin = sin6tab[which];
546         struct addrinfo hints, *res;
547         int error = -1;
548
549         newaddr &= 1;
550
551         sin->sin6_len = sizeof(*sin);
552         if (which != MASK)
553                 sin->sin6_family = AF_INET6;
554
555         if (which == ADDR) {
556                 char *p = NULL;
557                 if((p = strrchr(s, '/')) != NULL) {
558                         *p = '\0';
559                         in6_getprefix(p + 1, MASK);
560                         explicit_prefix = 1;
561                 }
562         }
563
564         if (sin->sin6_family == AF_INET6) {
565                 bzero(&hints, sizeof(struct addrinfo));
566                 hints.ai_family = AF_INET6;
567                 error = getaddrinfo(s, NULL, &hints, &res);
568                 if (error != 0) {
569                         if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
570                                 errx(1, "%s: bad value", s);
571                 } else {
572                         bcopy(res->ai_addr, sin, res->ai_addrlen);
573                         freeaddrinfo(res);
574                 }
575         }
576 }
577
578 static int
579 prefix(void *val, int size)
580 {
581         u_char *name = (u_char *)val;
582         int byte, bit, plen = 0;
583
584         for (byte = 0; byte < size; byte++, plen += 8)
585                 if (name[byte] != 0xff)
586                         break;
587         if (byte == size)
588                 return (plen);
589         for (bit = 7; bit != 0; bit--, plen++)
590                 if (!(name[byte] & (1 << bit)))
591                         break;
592         for (; bit != 0; bit--)
593                 if (name[byte] & (1 << bit))
594                         return(0);
595         byte++;
596         for (; byte < size; byte++)
597                 if (name[byte])
598                         return(0);
599         return (plen);
600 }
601 #endif
602
603 static char *
604 sec2str(time_t total)
605 {
606         static char result[256];
607         int days, hours, mins, secs;
608         int first = 1;
609         char *p = result;
610
611         if (0) {
612                 days = total / 3600 / 24;
613                 hours = (total / 3600) % 24;
614                 mins = (total / 60) % 60;
615                 secs = total % 60;
616
617                 if (days) {
618                         first = 0;
619                         p += sprintf(p, "%dd", days);
620                 }
621                 if (!first || hours) {
622                         first = 0;
623                         p += sprintf(p, "%dh", hours);
624                 }
625                 if (!first || mins) {
626                         first = 0;
627                         p += sprintf(p, "%dm", mins);
628                 }
629                 sprintf(p, "%ds", secs);
630         } else
631                 sprintf(result, "%lu", (unsigned long)total);
632
633         return(result);
634 }
635
636 static void
637 in6_postproc(if_ctx *ctx, int newaddr __unused,
638     int ifflags __unused)
639 {
640         if (explicit_prefix == 0) {
641                 /* Aggregatable address architecture defines all prefixes
642                    are 64. So, it is convenient to set prefixlen to 64 if
643                    it is not specified. */
644                 setifprefixlen(ctx, "64", 0);
645                 /* in6_getprefix("64", MASK) if MASK is available here... */
646         }
647 }
648
649 static void
650 in6_status_tunnel(int s)
651 {
652         char src[NI_MAXHOST];
653         char dst[NI_MAXHOST];
654         struct in6_ifreq in6_ifr;
655         const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
656
657         memset(&in6_ifr, 0, sizeof(in6_ifr));
658         strlcpy(in6_ifr.ifr_name, name, sizeof(in6_ifr.ifr_name));
659
660         if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
661                 return;
662         if (sa->sa_family != AF_INET6)
663                 return;
664         if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
665             NI_NUMERICHOST) != 0)
666                 src[0] = '\0';
667
668         if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
669                 return;
670         if (sa->sa_family != AF_INET6)
671                 return;
672         if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
673             NI_NUMERICHOST) != 0)
674                 dst[0] = '\0';
675
676         printf("\ttunnel inet6 %s --> %s\n", src, dst);
677 }
678
679 static void
680 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
681 {
682         struct in6_aliasreq in6_addreq; 
683
684         memset(&in6_addreq, 0, sizeof(in6_addreq));
685         strlcpy(in6_addreq.ifra_name, name, sizeof(in6_addreq.ifra_name));
686         memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
687         memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
688             dstres->ai_addr->sa_len);
689
690         if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
691                 warn("SIOCSIFPHYADDR_IN6");
692 }
693
694 static void
695 in6_set_vhid(int vhid)
696 {
697 #ifdef WITHOUT_NETLINK
698         in6_addreq.ifra_vhid = vhid;
699 #else
700         in6_add.vhid = (uint32_t)vhid;
701 #endif
702 }
703
704 static struct cmd inet6_cmds[] = {
705         DEF_CMD_ARG("prefixlen",                        setifprefixlen),
706         DEF_CMD("anycast",      IN6_IFF_ANYCAST,        setip6flags),
707         DEF_CMD("tentative",    IN6_IFF_TENTATIVE,      setip6flags),
708         DEF_CMD("-tentative",   -IN6_IFF_TENTATIVE,     setip6flags),
709         DEF_CMD("deprecated",   IN6_IFF_DEPRECATED,     setip6flags),
710         DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,     setip6flags),
711         DEF_CMD("autoconf",     IN6_IFF_AUTOCONF,       setip6flags),
712         DEF_CMD("-autoconf",    -IN6_IFF_AUTOCONF,      setip6flags),
713         DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,  setip6flags),
714         DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
715         DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV,   setnd6flags),
716         DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,  setnd6flags),
717         DEF_CMD("no_radr",      ND6_IFF_NO_RADR,        setnd6flags),
718         DEF_CMD("-no_radr",     -ND6_IFF_NO_RADR,       setnd6flags),
719         DEF_CMD("defaultif",    1,                      setnd6defif),
720         DEF_CMD("-defaultif",   -1,                     setnd6defif),
721         DEF_CMD("ifdisabled",   ND6_IFF_IFDISABLED,     setnd6flags),
722         DEF_CMD("-ifdisabled",  -ND6_IFF_IFDISABLED,    setnd6flags),
723         DEF_CMD("nud",          ND6_IFF_PERFORMNUD,     setnd6flags),
724         DEF_CMD("-nud",         -ND6_IFF_PERFORMNUD,    setnd6flags),
725         DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
726         DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
727         DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
728         DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
729         DEF_CMD("no_dad",       ND6_IFF_NO_DAD,         setnd6flags),
730         DEF_CMD("-no_dad",      -ND6_IFF_NO_DAD,        setnd6flags),
731         DEF_CMD_ARG("pltime",                           setip6pltime),
732         DEF_CMD_ARG("vltime",                           setip6vltime),
733         DEF_CMD("eui64",        0,                      setip6eui64),
734 #ifdef EXPERIMENTAL
735         DEF_CMD("ipv6_only",    ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
736         DEF_CMD("-ipv6_only",   -ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
737 #endif
738 };
739
740 static struct afswtch af_inet6 = {
741         .af_name        = "inet6",
742         .af_af          = AF_INET6,
743 #ifdef WITHOUT_NETLINK
744         .af_status      = in6_status,
745 #else
746         .af_status      = in6_status_nl,
747 #endif
748         .af_getaddr     = in6_getaddr,
749         .af_copyaddr    = in6_copyaddr,
750 #ifdef WITHOUT_NETLINK
751         .af_getprefix   = in6_getprefix,
752 #endif
753         .af_other_status = nd6_status,
754         .af_postproc    = in6_postproc,
755         .af_status_tunnel = in6_status_tunnel,
756         .af_settunnel   = in6_set_tunnel,
757         .af_setvhid     = in6_set_vhid,
758 #ifdef WITHOUT_NETLINK
759         .af_difaddr     = SIOCDIFADDR_IN6,
760         .af_aifaddr     = SIOCAIFADDR_IN6,
761         .af_ridreq      = &in6_addreq,
762         .af_addreq      = &in6_addreq,
763         .af_exec        = af_exec_ioctl,
764 #else
765         .af_difaddr     = NL_RTM_DELADDR,
766         .af_aifaddr     = NL_RTM_NEWADDR,
767         .af_ridreq      = &in6_add,
768         .af_addreq      = &in6_add,
769         .af_exec        = in6_exec_nl,
770 #endif
771 };
772
773 static void
774 in6_Lopt_cb(const char *arg __unused)
775 {
776         ip6lifetime++;  /* print IPv6 address lifetime */
777 }
778 static struct option in6_Lopt = {
779         .opt = "L",
780         .opt_usage = "[-L]",
781         .cb = in6_Lopt_cb
782 };
783
784 static __constructor void
785 inet6_ctor(void)
786 {
787         size_t i;
788
789 #ifndef RESCUE
790         if (!feature_present("inet6"))
791                 return;
792 #endif
793
794         for (i = 0; i < nitems(inet6_cmds);  i++)
795                 cmd_register(&inet6_cmds[i]);
796         af_register(&af_inet6);
797         opt_register(&in6_Lopt);
798 }