aboutsummaryrefslogtreecommitdiff
path: root/sbin/ifconfig/af_inet6.c
blob: fcd04139a8c151adf49fc0d128e2a5f5849292f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1983, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <ifaddrs.h>

#include <arpa/inet.h>

#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <netinet6/nd6.h>	/* Define ND6_INFINITE_LIFETIME */

#include "ifconfig.h"
#include "ifconfig_netlink.h"

#ifndef WITHOUT_NETLINK
struct in6_px {
	struct in6_addr		addr;
	int			plen;
	bool			set;
};
struct in6_pdata {
	struct in6_px		addr;
	struct in6_px		dst_addr;
	struct in6_addrlifetime	lifetime;
	uint32_t		flags;
	uint32_t		vhid;
};

static struct in6_pdata in6_del;
static struct in6_pdata in6_add = {
	.lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME },
};
#else
static	struct in6_ifreq in6_ridreq;
static	struct in6_aliasreq in6_addreq =
  { .ifra_flags = 0,
    .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
#endif
static	int ip6lifetime;

#ifdef WITHOUT_NETLINK
static	int prefix(void *, int);
#endif
static	char *sec2str(time_t);
static	int explicit_prefix = 0;
extern	char *f_inet6, *f_addr;

extern void setnd6flags(if_ctx *, const char *, int);
extern void setnd6defif(if_ctx *,const char *, int);
extern void nd6_status(if_ctx *);

static	char addr_buf[NI_MAXHOST];	/*for getnameinfo()*/

static void
setifprefixlen(if_ctx *ctx __netlink_unused, const char *addr, int dummy __unused)
{
#ifdef WITHOUT_NETLINK
	const struct afswtch *afp = ctx->afp;

        if (afp->af_getprefix != NULL)
                afp->af_getprefix(addr, MASK);
#else
	int plen = strtol(addr, NULL, 10);

	if ((plen < 0) || (plen > 128))
		errx(1, "%s: bad value", addr);
	in6_add.addr.plen = plen;
#endif
	explicit_prefix = 1;
}

static void
setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
{
	const struct afswtch *afp = ctx->afp;

	if (afp->af_af != AF_INET6)
		err(1, "address flags can be set only for inet6 addresses");

#ifdef WITHOUT_NETLINK
	if (flag < 0)
		in6_addreq.ifra_flags &= ~(-flag);
	else
		in6_addreq.ifra_flags |= flag;
#else
	if (flag < 0)
		in6_add.flags &= ~(-flag);
	else
		in6_add.flags |= flag;
#endif
}

static void
setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
{
	const struct afswtch *afp = ctx->afp;
	struct timespec now;
	time_t newval;
	char *ep;
#ifdef WITHOUT_NETLINK
	struct in6_addrlifetime *lifetime = &in6_addreq.ifra_lifetime;
#else
	struct in6_addrlifetime *lifetime = &in6_add.lifetime;
#endif

	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
	newval = (time_t)strtoul(val, &ep, 0);
	if (val == ep)
		errx(1, "invalid %s", cmd);
	if (afp->af_af != AF_INET6)
		errx(1, "%s not allowed for the AF", cmd);
	if (strcmp(cmd, "vltime") == 0) {
		lifetime->ia6t_expire = now.tv_sec + newval;
		lifetime->ia6t_vltime = newval;
	} else if (strcmp(cmd, "pltime") == 0) {
		lifetime->ia6t_preferred = now.tv_sec + newval;
		lifetime->ia6t_pltime = newval;
	}
}

static void
setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
{
	setip6lifetime(ctx, "pltime", seconds);
}

static void
setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
{
	setip6lifetime(ctx, "vltime", seconds);
}

static void
setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
{
	const struct afswtch *afp = ctx->afp;
	struct ifaddrs *ifap, *ifa;
	const struct sockaddr_in6 *sin6 = NULL;
	const struct in6_addr *lladdr = NULL;
	struct in6_addr *in6;

	if (afp->af_af != AF_INET6)
		errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
#ifdef WITHOUT_NETLINK
 	in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
#else
	in6 = &in6_add.addr.addr;
#endif
	if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
		errx(EXIT_FAILURE, "interface index is already filled");
	if (getifaddrs(&ifap) != 0)
		err(EXIT_FAILURE, "getifaddrs");
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
		if (ifa->ifa_addr->sa_family == AF_INET6 &&
		    strcmp(ifa->ifa_name, ctx->ifname) == 0) {
			sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
				lladdr = &sin6->sin6_addr;
				break;
			}
		}
	}
	if (!lladdr)
		errx(EXIT_FAILURE, "could not determine link local address"); 

 	memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);

	freeifaddrs(ifap);
}

static void
print_addr(struct sockaddr_in6 *sin)
{
	int error, n_flags;

	if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
		n_flags = 0;
	else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
		n_flags = NI_NOFQDN;
	else
		n_flags = NI_NUMERICHOST;
	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
			    addr_buf, sizeof(addr_buf), NULL, 0,
			    n_flags);
	if (error != 0)
		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
			  sizeof(addr_buf));
	printf("\tinet6 %s", addr_buf);
}

static void
print_p2p(struct sockaddr_in6 *sin)
{
	int error;

	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
	    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);

	if (error != 0)
		inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf));
	printf(" --> %s", addr_buf);
}

static void
print_mask(int plen)
{
	if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
		printf("/%d", plen);
	else
		printf(" prefixlen %d", plen);
}

static void
print_flags(int flags6)
{
	if ((flags6 & IN6_IFF_ANYCAST) != 0)
		printf(" anycast");
	if ((flags6 & IN6_IFF_TENTATIVE) != 0)
		printf(" tentative");
	if ((flags6 & IN6_IFF_DUPLICATED) != 0)
		printf(" duplicated");
	if ((flags6 & IN6_IFF_DETACHED) != 0)
		printf(" detached");
	if ((flags6 & IN6_IFF_DEPRECATED) != 0)
		printf(" deprecated");
	if ((flags6 & IN6_IFF_AUTOCONF) != 0)
		printf(" autoconf");
	if ((flags6 & IN6_IFF_TEMPORARY) != 0)
		printf(" temporary");
	if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
		printf(" prefer_source");

}

static void
print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
{
	printf(" %s", prepend);
	if (px_time == 0)
		printf(" infty");

	printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec));
}

#ifdef WITHOUT_NETLINK
static void
in6_status(if_ctx *ctx, const struct ifaddrs *ifa)
{
	struct sockaddr_in6 *sin, null_sin = {};
	struct in6_ifreq ifr6;
	int s6;
	u_int32_t flags6;
	struct in6_addrlifetime lifetime;

	sin = satosin6(ifa->ifa_addr);
	if (sin == NULL)
		return;

	strlcpy(ifr6.ifr_name, ctx->ifname, sizeof(ifr6.ifr_name));
	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
		warn("socket(AF_INET6,SOCK_DGRAM)");
		return;
	}
	ifr6.ifr_addr = *sin;
	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
		warn("ioctl(SIOCGIFAFLAG_IN6)");
		close(s6);
		return;
	}
	flags6 = ifr6.ifr_ifru.ifru_flags6;
	memset(&lifetime, 0, sizeof(lifetime));
	ifr6.ifr_addr = *sin;
	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
		warn("ioctl(SIOCGIFALIFETIME_IN6)");
		close(s6);
		return;
	}
	lifetime = ifr6.ifr_ifru.ifru_lifetime;
	close(s6);

	print_addr(sin);

	if (ifa->ifa_flags & IFF_POINTOPOINT) {
		sin = satosin6(ifa->ifa_dstaddr);
		/*
		 * some of the interfaces do not have valid destination
		 * address.
		 */
		if (sin != NULL && sin->sin6_family == AF_INET6)
			print_p2p(sin);
	}

	sin = satosin6(ifa->ifa_netmask);
	if (sin == NULL)
		sin = &null_sin;
	print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr)));

	print_flags(flags6);

	if ((satosin6(ifa->ifa_addr))->sin6_scope_id)
		printf(" scopeid 0x%x",
		    (satosin6(ifa->ifa_addr))->sin6_scope_id);

	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
		struct timespec now;

		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
		print_lifetime("pltime", lifetime.ia6t_preferred, &now);
		print_lifetime("vltime", lifetime.ia6t_expire, &now);
	}

	print_vhid(ifa);

	putchar('\n');
}

#else
static void
show_lifetime(struct ifa_cacheinfo *ci)
{
	struct timespec now;
	uint32_t pl, vl;

	if (ci == NULL)
		return;

	int count = ci->ifa_prefered != ND6_INFINITE_LIFETIME;
	count += ci->ifa_valid != ND6_INFINITE_LIFETIME;
	if (count == 0)
		return;

	pl = (ci->ifa_prefered == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_prefered;
	vl = (ci->ifa_valid == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_valid;

	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
	print_lifetime("pltime", pl + now.tv_sec, &now);
	print_lifetime("vltime", vl + now.tv_sec, &now);
}

static void
in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
{
	int plen = ifa->ifa_prefixlen;
	uint32_t scopeid;

	if (ifa->ifa_local == NULL) {
		/* Non-P2P address */
		scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
		print_addr(satosin6(ifa->ifa_address));
	} else {
		scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
		print_addr(satosin6(ifa->ifa_local));
		print_p2p(satosin6(ifa->ifa_address));
	}

	print_mask(plen);
	print_flags(ifa->ifaf_flags);

	if (scopeid != 0)
		printf(" scopeid 0x%x", scopeid);

	show_lifetime(ifa->ifa_cacheinfo);

	if (ifa->ifaf_vhid != 0)
		printf(" vhid %d", ifa->ifaf_vhid);

	putchar('\n');
}

static struct in6_px *sin6tab_nl[] = {
        &in6_del.addr,          /* RIDADDR */
        &in6_add.addr,          /* ADDR */
        NULL,                   /* MASK */
        &in6_add.dst_addr,      /* DSTADDR*/
};

static void
in6_copyaddr(if_ctx *ctx __unused, int to, int from)
{
	sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
	sin6tab_nl[to]->set = sin6tab_nl[from]->set;
}

static void
in6_getaddr(const char *addr_str, int which)
{
        struct in6_px *px = sin6tab_nl[which];

        px->set = true;
        px->plen = 128;
        if (which == ADDR) {
                char *p = NULL;
                if((p = strrchr(addr_str, '/')) != NULL) {
                        *p = '\0';
                        int plen = strtol(p + 1, NULL, 10);
			if (plen < 0 || plen > 128)
                                errx(1, "%s: bad value", p + 1);
                        px->plen = plen;
                        explicit_prefix = 1;
                }
        }

        struct addrinfo hints = { .ai_family = AF_INET6 };
        struct addrinfo *res;

        int error = getaddrinfo(addr_str, NULL, &hints, &res);
        if (error != 0) {
                if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
                        errx(1, "%s: bad value", addr_str);
        } else {
                struct sockaddr_in6 *sin6;

                sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
                px->addr = sin6->sin6_addr;
                freeaddrinfo(res);
        }
}

static int
in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
{
	struct in6_pdata *pdata = (struct in6_pdata *)data;
	struct snl_writer nw = {};

	snl_init_writer(ctx->io_ss, &nw);
	struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
	struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);

	ifahdr->ifa_family = AF_INET6;
	ifahdr->ifa_prefixlen = pdata->addr.plen;
	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);

	snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
	if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
		snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);

	struct ifa_cacheinfo ci = {
		.ifa_prefered = pdata->lifetime.ia6t_pltime,
		.ifa_valid =  pdata->lifetime.ia6t_vltime,
	};
	snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);

	int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
	snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
	if (pdata->vhid != 0)
		snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
	snl_end_attr_nested(&nw, off);

	if (! (hdr = snl_finalize_msg(&nw)) || !snl_send_message(ctx->io_ss, hdr))
		return (0);

	struct snl_errmsg_data e = {};
	snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);

	return (e.error);
}
#endif

#ifdef WITHOUT_NETLINK
static struct	sockaddr_in6 *sin6tab[] = {
	&in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
	&in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
};

static void
in6_copyaddr(if_ctx *ctx __unused, int to, int from)
{
	memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
}

static void
in6_getprefix(const char *plen, int which)
{
	struct sockaddr_in6 *sin = sin6tab[which];
	u_char *cp;
	int len = atoi(plen);

	if ((len < 0) || (len > 128))
		errx(1, "%s: bad value", plen);
	sin->sin6_len = sizeof(*sin);
	if (which != MASK)
		sin->sin6_family = AF_INET6;
	if ((len == 0) || (len == 128)) {
		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
		return;
	}
	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
		*cp++ = 0xff;
	*cp = 0xff << (8 - len);
}

static void
in6_getaddr(const char *s, int which)
{
	struct sockaddr_in6 *sin = sin6tab[which];
	struct addrinfo hints, *res;
	int error = -1;

	sin->sin6_len = sizeof(*sin);
	if (which != MASK)
		sin->sin6_family = AF_INET6;

	if (which == ADDR) {
		char *p = NULL;
		if((p = strrchr(s, '/')) != NULL) {
			*p = '\0';
			in6_getprefix(p + 1, MASK);
			explicit_prefix = 1;
		}
	}

	if (sin->sin6_family == AF_INET6) {
		bzero(&hints, sizeof(struct addrinfo));
		hints.ai_family = AF_INET6;
		error = getaddrinfo(s, NULL, &hints, &res);
		if (error != 0) {
			if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
				errx(1, "%s: bad value", s);
		} else {
			bcopy(res->ai_addr, sin, res->ai_addrlen);
			freeaddrinfo(res);
		}
	}
}

static int
prefix(void *val, int size)
{
	u_char *name = (u_char *)val;
	int byte, bit, plen = 0;

	for (byte = 0; byte < size; byte++, plen += 8)
		if (name[byte] != 0xff)
			break;
	if (byte == size)
		return (plen);
	for (bit = 7; bit != 0; bit--, plen++)
		if (!(name[byte] & (1 << bit)))
			break;
	for (; bit != 0; bit--)
		if (name[byte] & (1 << bit))
			return(0);
	byte++;
	for (; byte < size; byte++)
		if (name[byte])
			return(0);
	return (plen);
}
#endif

static char *
sec2str(time_t total)
{
	static char result[256];
	int days, hours, mins, secs;
	int first = 1;
	char *p = result;

	if (0) {
		days = total / 3600 / 24;
		hours = (total / 3600) % 24;
		mins = (total / 60) % 60;
		secs = total % 60;

		if (days) {
			first = 0;
			p += sprintf(p, "%dd", days);
		}
		if (!first || hours) {
			first = 0;
			p += sprintf(p, "%dh", hours);
		}
		if (!first || mins) {
			first = 0;
			p += sprintf(p, "%dm", mins);
		}
		sprintf(p, "%ds", secs);
	} else
		sprintf(result, "%lu", (unsigned long)total);

	return(result);
}

static void
in6_postproc(if_ctx *ctx, int newaddr __unused,
    int ifflags __unused)
{
	if (explicit_prefix == 0) {
		/* Aggregatable address architecture defines all prefixes
		   are 64. So, it is convenient to set prefixlen to 64 if
		   it is not specified. */
		setifprefixlen(ctx, "64", 0);
		/* in6_getprefix("64", MASK) if MASK is available here... */
	}
}

static void
in6_status_tunnel(if_ctx *ctx)
{
	char src[NI_MAXHOST];
	char dst[NI_MAXHOST];
	struct in6_ifreq in6_ifr;
	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;

	memset(&in6_ifr, 0, sizeof(in6_ifr));
	strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));

	if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
		return;
	if (sa->sa_family != AF_INET6)
		return;
	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
	    NI_NUMERICHOST) != 0)
		src[0] = '\0';

	if (ioctl_ctx(ctx, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
		return;
	if (sa->sa_family != AF_INET6)
		return;
	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
	    NI_NUMERICHOST) != 0)
		dst[0] = '\0';

	printf("\ttunnel inet6 %s --> %s\n", src, dst);
}

static void
in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
{
	struct in6_aliasreq in6_req = {}; 

	strlcpy(in6_req.ifra_name, ctx->ifname, sizeof(in6_req.ifra_name));
	memcpy(&in6_req.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
	memcpy(&in6_req.ifra_dstaddr, dstres->ai_addr,
	    dstres->ai_addr->sa_len);

	if (ioctl_ctx(ctx, SIOCSIFPHYADDR_IN6, &in6_req) < 0)
		warn("SIOCSIFPHYADDR_IN6");
}

static void
in6_set_vhid(int vhid)
{
#ifdef WITHOUT_NETLINK
	in6_addreq.ifra_vhid = vhid;
#else
	in6_add.vhid = (uint32_t)vhid;
#endif
}

static struct cmd inet6_cmds[] = {
	DEF_CMD_ARG("prefixlen",			setifprefixlen),
	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
	DEF_CMD("tentative",	IN6_IFF_TENTATIVE,	setip6flags),
	DEF_CMD("-tentative",	-IN6_IFF_TENTATIVE,	setip6flags),
	DEF_CMD("deprecated",	IN6_IFF_DEPRECATED,	setip6flags),
	DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED,	setip6flags),
	DEF_CMD("autoconf",	IN6_IFF_AUTOCONF,	setip6flags),
	DEF_CMD("-autoconf",	-IN6_IFF_AUTOCONF,	setip6flags),
	DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE,	setip6flags),
	DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
	DEF_CMD("accept_rtadv",	ND6_IFF_ACCEPT_RTADV,	setnd6flags),
	DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV,	setnd6flags),
	DEF_CMD("no_radr",	ND6_IFF_NO_RADR,	setnd6flags),
	DEF_CMD("-no_radr",	-ND6_IFF_NO_RADR,	setnd6flags),
	DEF_CMD("defaultif",	1,			setnd6defif),
	DEF_CMD("-defaultif",	-1,			setnd6defif),
	DEF_CMD("ifdisabled",	ND6_IFF_IFDISABLED,	setnd6flags),
	DEF_CMD("-ifdisabled",	-ND6_IFF_IFDISABLED,	setnd6flags),
	DEF_CMD("nud",		ND6_IFF_PERFORMNUD,	setnd6flags),
	DEF_CMD("-nud",		-ND6_IFF_PERFORMNUD,	setnd6flags),
	DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
	DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
	DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
	DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
	DEF_CMD("no_dad",	ND6_IFF_NO_DAD,		setnd6flags),
	DEF_CMD("-no_dad",	-ND6_IFF_NO_DAD,	setnd6flags),
	DEF_CMD_ARG("pltime",        			setip6pltime),
	DEF_CMD_ARG("vltime",        			setip6vltime),
	DEF_CMD("eui64",	0,			setip6eui64),
#ifdef EXPERIMENTAL
	DEF_CMD("ipv6_only",	ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
	DEF_CMD("-ipv6_only",	-ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
#endif
};

static struct afswtch af_inet6 = {
	.af_name	= "inet6",
	.af_af		= AF_INET6,
#ifdef WITHOUT_NETLINK
	.af_status	= in6_status,
#else
	.af_status	= in6_status_nl,
#endif
	.af_getaddr	= in6_getaddr,
	.af_copyaddr	= in6_copyaddr,
#ifdef WITHOUT_NETLINK
	.af_getprefix	= in6_getprefix,
#endif
	.af_other_status = nd6_status,
	.af_postproc	= in6_postproc,
	.af_status_tunnel = in6_status_tunnel,
	.af_settunnel	= in6_set_tunnel,
	.af_setvhid	= in6_set_vhid,
#ifdef WITHOUT_NETLINK
	.af_difaddr	= SIOCDIFADDR_IN6,
	.af_aifaddr	= SIOCAIFADDR_IN6,
	.af_ridreq	= &in6_addreq,
	.af_addreq	= &in6_addreq,
	.af_exec	= af_exec_ioctl,
#else
	.af_difaddr	= NL_RTM_DELADDR,
	.af_aifaddr	= NL_RTM_NEWADDR,
	.af_ridreq	= &in6_add,
	.af_addreq	= &in6_add,
	.af_exec	= in6_exec_nl,
#endif
};

static void
in6_Lopt_cb(const char *arg __unused)
{
	ip6lifetime++;	/* print IPv6 address lifetime */
}
static struct option in6_Lopt = {
	.opt = "L",
	.opt_usage = "[-L]",
	.cb = in6_Lopt_cb
};

static __constructor void
inet6_ctor(void)
{
	size_t i;

#ifndef RESCUE
	if (!feature_present("inet6"))
		return;
#endif

	for (i = 0; i < nitems(inet6_cmds);  i++)
		cmd_register(&inet6_cmds[i]);
	af_register(&af_inet6);
	opt_register(&in6_Lopt);
}