Integrate the remaining parts of the network interface aliasing
[dragonfly.git] / sbin / ifconfig / ifconfig.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)ifconfig.c       8.2 (Berkeley) 2/16/94
35  * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.96 2004/02/27 06:43:14 kan Exp $
36  * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.7 2004/03/15 22:39:37 hmp Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 #include <sys/time.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53
54 /* IP */
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <arpa/inet.h>
58 #include <netdb.h>
59
60 #ifdef INET6
61 #include <netinet6/nd6.h>       /* Define ND6_INFINITE_LIFETIME */
62 #endif
63
64 #ifndef NO_IPX
65 /* IPX */
66 #define IPXIP
67 #define IPTUNNEL
68 #include <netipx/ipx.h>
69 #include <netipx/ipx_if.h>
70 #endif
71
72 /* Appletalk */
73 #include <netatalk/at.h>
74
75 /* XNS */
76 #ifdef NS
77 #define NSIP
78 #include <netns/ns.h>
79 #include <netns/ns_if.h>
80 #endif
81
82 /* OSI */
83
84 #include <ctype.h>
85 #include <err.h>
86 #include <errno.h>
87 #include <fcntl.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <unistd.h>
92 #include <ifaddrs.h>
93
94 #include "ifconfig.h"
95
96 /* wrapper for KAME-special getnameinfo() */
97 #ifndef NI_WITHSCOPEID
98 #define NI_WITHSCOPEID  0
99 #endif
100
101 struct  ifreq           ifr, ridreq;
102 struct  ifaliasreq      addreq;
103 #ifdef INET6
104 struct  in6_ifreq       in6_ridreq;
105 struct  in6_aliasreq    in6_addreq = 
106   { { 0 }, 
107     { 0 }, 
108     { 0 }, 
109     { 0 }, 
110     0, 
111     { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
112 #endif
113 struct  sockaddr_in     netmask;
114 struct  netrange        at_nr;          /* AppleTalk net range */
115
116 char    name[IFNAMSIZ];
117 int     flags;
118 int     setaddr;
119 int     setipdst;
120 int     setmask;
121 int     doalias;
122 int     clearaddr;
123 int     newaddr = 1;
124 #ifdef INET6
125 static  int ip6lifetime;
126 #endif
127
128 struct  afswtch;
129
130 int supmedia = 0;
131 int listcloners = 0;
132 int printname = 0;              /* Print the name of the created interface. */
133
134 #ifdef INET6
135 char    addr_buf[MAXHOSTNAMELEN *2 + 1];        /*for getnameinfo()*/
136 #endif
137
138 void    Perror(const char *cmd);
139 void    checkatrange(struct sockaddr_at *);
140 int     ifconfig(int argc, char *const *argv, const struct afswtch *afp);
141 void    notealias(const char *, int, int, const struct afswtch *afp);
142 void    list_cloners(void);
143 void    printb(const char *s, unsigned value, const char *bits);
144 void    rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
145 void    status(const struct afswtch *afp, int addrcount,
146                     struct sockaddr_dl *sdl, struct if_msghdr *ifm,
147                     struct ifa_msghdr *ifam);
148 void    tunnel_status(int s);
149 void    usage(void);
150 void    ifmaybeload(char *name);
151
152 #ifdef INET6
153 void    in6_fillscopeid(struct sockaddr_in6 *sin6);
154 int     prefix(void *, int);
155 static  char *sec2str(time_t);
156 int     explicit_prefix = 0;
157 #endif
158
159 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
160 typedef void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp);
161 c_func  setatphase, setatrange;
162 c_func  setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
163 c_func2 settunnel;
164 c_func  deletetunnel;
165 #ifdef INET6
166 c_func  setifprefixlen;
167 c_func  setip6flags;
168 c_func  setip6pltime;
169 c_func  setip6vltime;
170 c_func2 setip6lifetime;
171 c_func  setip6eui64;
172 #endif
173 c_func  setifipdst;
174 c_func  setifflags, setifmetric, setifmtu, setifcap;
175 c_func  clone_destroy;
176 c_func  setifname;
177
178
179 void clone_create(void);
180
181
182 #define NEXTARG         0xffffff
183 #define NEXTARG2        0xfffffe
184
185 const
186 struct  cmd {
187         const   char *c_name;
188         int     c_parameter;            /* NEXTARG means next argv */
189         void    (*c_func)(const char *, int, int, const struct afswtch *afp);
190         void    (*c_func2)(const char *, const char *, int, const struct afswtch *afp);
191 } cmds[] = {
192         { "up",         IFF_UP,         setifflags } ,
193         { "down",       -IFF_UP,        setifflags },
194         { "arp",        -IFF_NOARP,     setifflags },
195         { "-arp",       IFF_NOARP,      setifflags },
196         { "debug",      IFF_DEBUG,      setifflags },
197         { "-debug",     -IFF_DEBUG,     setifflags },
198         { "promisc",    IFF_PPROMISC,   setifflags },
199         { "-promisc",   -IFF_PPROMISC,  setifflags },
200         { "add",        IFF_UP,         notealias },
201         { "alias",      IFF_UP,         notealias },
202         { "-alias",     -IFF_UP,        notealias },
203         { "delete",     -IFF_UP,        notealias },
204         { "remove",     -IFF_UP,        notealias },
205 #ifdef notdef
206 #define EN_SWABIPS      0x1000
207         { "swabips",    EN_SWABIPS,     setifflags },
208         { "-swabips",   -EN_SWABIPS,    setifflags },
209 #endif
210         { "netmask",    NEXTARG,        setifnetmask },
211 #ifdef INET6
212         { "prefixlen",  NEXTARG,        setifprefixlen },
213         { "anycast",    IN6_IFF_ANYCAST, setip6flags },
214         { "tentative",  IN6_IFF_TENTATIVE, setip6flags },
215         { "-tentative", -IN6_IFF_TENTATIVE, setip6flags },
216         { "deprecated", IN6_IFF_DEPRECATED, setip6flags },
217         { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags },
218         { "autoconf",   IN6_IFF_AUTOCONF, setip6flags },
219         { "-autoconf",  -IN6_IFF_AUTOCONF, setip6flags },
220         { "pltime",     NEXTARG,        setip6pltime },
221         { "vltime",     NEXTARG,        setip6vltime },
222         { "eui64",      0,              setip6eui64 },
223 #endif
224         { "range",      NEXTARG,        setatrange },
225         { "phase",      NEXTARG,        setatphase },
226         { "metric",     NEXTARG,        setifmetric },
227         { "broadcast",  NEXTARG,        setifbroadaddr },
228         { "ipdst",      NEXTARG,        setifipdst },
229         { "tunnel",     NEXTARG2,       NULL,   settunnel },
230         { "deletetunnel", 0,            deletetunnel },
231         { "link0",      IFF_LINK0,      setifflags },
232         { "-link0",     -IFF_LINK0,     setifflags },
233         { "link1",      IFF_LINK1,      setifflags },
234         { "-link1",     -IFF_LINK1,     setifflags },
235         { "link2",      IFF_LINK2,      setifflags },
236         { "-link2",     -IFF_LINK2,     setifflags },
237 #ifdef USE_IF_MEDIA
238         { "media",      NEXTARG,        setmedia },
239         { "mediaopt",   NEXTARG,        setmediaopt },
240         { "-mediaopt",  NEXTARG,        unsetmediaopt },
241 #endif
242 #ifdef USE_VLANS
243         { "vlan",       NEXTARG,        setvlantag },
244         { "vlandev",    NEXTARG,        setvlandev },
245         { "-vlandev",   NEXTARG,        unsetvlandev },
246 #endif
247 #if 0
248         /* XXX `create' special-cased below */
249         {"create",      0,              clone_create },
250         {"plumb",       0,              clone_create },
251 #endif
252         {"destroy",     0,              clone_destroy },
253         {"unplumb",     0,              clone_destroy },
254 #ifdef USE_IEEE80211
255         { "ssid",       NEXTARG,        set80211ssid },
256         { "nwid",       NEXTARG,        set80211ssid },
257         { "stationname", NEXTARG,       set80211stationname },
258         { "station",    NEXTARG,        set80211stationname },  /* BSD/OS */
259         { "channel",    NEXTARG,        set80211channel },
260         { "authmode",   NEXTARG,        set80211authmode },
261         { "powersavemode", NEXTARG,     set80211powersavemode },
262         { "powersave",  1,              set80211powersave },
263         { "-powersave", 0,              set80211powersave },
264         { "powersavesleep", NEXTARG,    set80211powersavesleep },
265         { "wepmode",    NEXTARG,        set80211wepmode },
266         { "wep",        1,              set80211wep },
267         { "-wep",       0,              set80211wep },
268         { "weptxkey",   NEXTARG,        set80211weptxkey },
269         { "wepkey",     NEXTARG,        set80211wepkey },
270         { "nwkey",      NEXTARG,        set80211nwkey },        /* NetBSD */
271         { "-nwkey",     0,              set80211wep },          /* NetBSD */
272 #endif
273         { "rxcsum",     IFCAP_RXCSUM,   setifcap },
274         { "-rxcsum",    -IFCAP_RXCSUM,  setifcap },
275         { "txcsum",     IFCAP_TXCSUM,   setifcap },
276         { "-txcsum",    -IFCAP_TXCSUM,  setifcap },
277         { "netcons",    IFCAP_NETCONS,  setifcap },
278         { "-netcons",   -IFCAP_NETCONS, setifcap },
279         { "normal",     -IFF_LINK0,     setifflags },
280         { "compress",   IFF_LINK0,      setifflags },
281         { "noicmp",     IFF_LINK1,      setifflags },
282         { "mtu",        NEXTARG,        setifmtu },
283         { "name",       NEXTARG,        setifname },
284         { 0,            0,              setifaddr },
285         { 0,            0,              setifdstaddr },
286 };
287
288 /*
289  * XNS support liberally adapted from code written at the University of
290  * Maryland principally by James O'Toole and Chris Torek.
291  */
292 typedef void af_status(int, struct rt_addrinfo *);
293 typedef void af_getaddr(const char *, int);
294 typedef void af_getprefix(const char *, int);
295
296 af_status       in_status, at_status, link_status;
297 af_getaddr      in_getaddr, at_getaddr, link_getaddr;
298
299 #ifndef NO_IPX
300 af_status       ipx_status;
301 af_getaddr      ipx_getaddr;
302 #endif
303
304 #ifdef INET6
305 af_status       in6_status;
306 af_getaddr      in6_getaddr;
307 af_getprefix    in6_getprefix;
308 #endif /*INET6*/
309 #ifdef NS
310 af_status       xns_status;
311 af_getaddr      xns_getaddr;
312 #endif
313
314 /* Known address families */
315 const
316 struct  afswtch {
317         const char *af_name;
318         short af_af;
319         af_status *af_status;
320         af_getaddr *af_getaddr;
321         af_getprefix *af_getprefix;
322         u_long af_difaddr;
323         u_long af_aifaddr;
324         caddr_t af_ridreq;
325         caddr_t af_addreq;
326 } afs[] = {
327 #define C(x) ((caddr_t) &x)
328         { "inet", AF_INET, in_status, in_getaddr, NULL,
329              SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
330 #ifdef INET6
331         { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
332              SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
333              C(in6_ridreq), C(in6_addreq) },
334 #endif /*INET6*/
335 #ifndef NO_IPX
336         { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
337              SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
338 #endif
339         { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
340              SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
341 #ifdef NS
342         { "ns", AF_NS, xns_status, xns_getaddr, NULL,
343              SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
344 #endif
345         { "link", AF_LINK, link_status, link_getaddr, NULL,
346              0, SIOCSIFLLADDR, NULL, C(ridreq) },
347         { "ether", AF_LINK, link_status, link_getaddr, NULL,
348              0, SIOCSIFLLADDR, NULL, C(ridreq) },
349         { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
350              0, SIOCSIFLLADDR, NULL, C(ridreq) },
351 #if 0   /* XXX conflicts with the media command */
352 #ifdef USE_IF_MEDIA
353         { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */
354 #endif
355 #ifdef USE_VLANS
356         { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, },  /* XXX not real!! */
357 #endif
358 #ifdef USE_IEEE80211
359         { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, },  /* XXX not real!! */
360 #endif
361 #endif
362         { 0,    0,          0,          0 }
363 };
364
365 /*
366  * Expand the compacted form of addresses as returned via the
367  * configuration read via sysctl().
368  */
369
370 #define ROUNDUP(a) \
371         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
372 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
373
374 void
375 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
376 {
377         struct sockaddr *sa;
378         int i;
379
380         memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
381         for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
382                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
383                         continue;
384                 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
385                 ADVANCE(cp, sa);
386         }
387 }
388
389
390 void
391 usage(void)
392 {
393 #ifndef INET6
394         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
395         "usage: ifconfig interface address_family [address [dest_address]]",
396         "                [parameters]",
397         "       ifconfig -C",
398         "       ifconfig interface create",
399         "       ifconfig -a [-d] [-m] [-u] [address_family]",
400         "       ifconfig -l [-d] [-u] [address_family]",
401         "       ifconfig [-d] [-m] [-u]");
402 #else
403         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
404         "usage: ifconfig [-L] interface address_family [address [dest_address]]",
405         "                [parameters]",
406         "       ifconfig -C",
407         "       ifconfig interface create",
408         "       ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
409         "       ifconfig -l [-d] [-u] [address_family]",
410         "       ifconfig [-L] [-d] [-m] [-u]");
411 #endif
412         exit(1);
413 }
414
415 int
416 main(int argc, char * const *argv)
417 {
418         int c;
419         int all, namesonly, downonly, uponly;
420         int foundit = 0, need_nl = 0;
421         const struct afswtch *afp = 0;
422         int addrcount;
423         struct  if_msghdr *ifm, *nextifm;
424         struct  ifa_msghdr *ifam;
425         struct  sockaddr_dl *sdl;
426         char    *buf, *lim, *next;
427
428
429         size_t needed;
430         int mib[6];
431
432         /* Parse leading line options */
433         all = downonly = uponly = namesonly = 0;
434         while ((c = getopt(argc, argv, "adlmuC"
435 #ifdef INET6
436                                         "L"
437 #endif
438                         )) != -1) {
439                 switch (c) {
440                 case 'a':       /* scan all interfaces */
441                         all++;
442                         break;
443                 case 'd':       /* restrict scan to "down" interfaces */
444                         downonly++;
445                         break;
446                 case 'l':       /* scan interface names only */
447                         namesonly++;
448                         break;
449                 case 'm':       /* show media choices in status */
450                         supmedia = 1;
451                         break;
452                 case 'u':       /* restrict scan to "up" interfaces */
453                         uponly++;
454                         break;
455                 case 'C':
456                         listcloners = 1;
457                         break;
458 #ifdef INET6
459                 case 'L':
460                         ip6lifetime++;  /* print IPv6 address lifetime */
461                         break;
462 #endif
463                 default:
464                         usage();
465                         break;
466                 }
467         }
468         argc -= optind;
469         argv += optind;
470
471         if (listcloners) {
472                 /* -C must be solitary */
473                 if (all || supmedia || uponly || downonly || namesonly ||
474                     argc > 0)
475                         usage();
476                 
477                 list_cloners();
478                 exit(0);
479         }
480
481         /* -l cannot be used with -a or -m */
482         if (namesonly && (all || supmedia))
483                 usage();
484
485         /* nonsense.. */
486         if (uponly && downonly)
487                 usage();
488
489         /* no arguments is equivalent to '-a' */
490         if (!namesonly && argc < 1)
491                 all = 1;
492
493         /* -a and -l allow an address family arg to limit the output */
494         if (all || namesonly) {
495                 if (argc > 1)
496                         usage();
497
498                 if (argc == 1) {
499                         for (afp = afs; afp->af_name; afp++)
500                                 if (strcmp(afp->af_name, *argv) == 0) {
501                                         argc--, argv++;
502                                         break;
503                                 }
504                         if (afp->af_name == NULL)
505                                 usage();
506                         /* leave with afp non-zero */
507                 }
508         } else {
509                 /* not listing, need an argument */
510                 if (argc < 1)
511                         usage();
512
513                 strncpy(name, *argv, sizeof(name));
514                 argc--, argv++;
515
516                 /* check and maybe load support for this interface */
517                 ifmaybeload(name);
518
519                 /*
520                  * NOTE:  We must special-case the `create' command right
521                  * here as we would otherwise fail when trying to find
522                  * the interface.
523                  */
524                 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
525                     strcmp(argv[0], "plumb") == 0)) {
526                         clone_create();
527                         argc--, argv++;
528                         if (argc == 0)
529                                 goto end;
530                 }
531         }
532
533         /* Check for address family */
534         if (argc > 0) {
535                 for (afp = afs; afp->af_name; afp++)
536                         if (strcmp(afp->af_name, *argv) == 0) {
537                                 argc--, argv++;
538                                 break;
539                         }
540                 if (afp->af_name == NULL)
541                         afp = NULL;     /* not a family, NULL */
542         }
543
544         mib[0] = CTL_NET;
545         mib[1] = PF_ROUTE;
546         mib[2] = 0;
547         mib[3] = 0;     /* address family */
548         mib[4] = NET_RT_IFLIST;
549         mib[5] = 0;
550
551         /* if particular family specified, only ask about it */
552         if (afp)
553                 mib[3] = afp->af_af;
554
555         if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
556                 errx(1, "iflist-sysctl-estimate");
557         if ((buf = malloc(needed)) == NULL)
558                 errx(1, "malloc");
559         if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
560                 errx(1, "actual retrieval of interface table");
561         lim = buf + needed;
562
563         next = buf;
564         while (next < lim) {
565
566                 ifm = (struct if_msghdr *)next;
567                 
568                 if (ifm->ifm_type == RTM_IFINFO) {
569                         sdl = (struct sockaddr_dl *)(ifm + 1);
570                         flags = ifm->ifm_flags;
571                 } else {
572                         fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
573                         fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
574                                 ifm->ifm_type);
575                         fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
576                         fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
577                                 lim);
578                         exit (1);
579                 }
580
581                 next += ifm->ifm_msglen;
582                 ifam = NULL;
583                 addrcount = 0;
584                 while (next < lim) {
585
586                         nextifm = (struct if_msghdr *)next;
587
588                         if (nextifm->ifm_type != RTM_NEWADDR)
589                                 break;
590
591                         if (ifam == NULL)
592                                 ifam = (struct ifa_msghdr *)nextifm;
593
594                         addrcount++;
595                         next += nextifm->ifm_msglen;
596                 }
597                 strlcpy(name, sdl->sdl_data,
598                                 sizeof(name) <= sdl->sdl_nlen ?
599                                 sizeof(name) : sdl->sdl_nlen + 1);
600
601                 if (all || namesonly) {
602                         if (uponly)
603                                 if ((flags & IFF_UP) == 0)
604                                         continue; /* not up */
605                         if (downonly)
606                                 if (flags & IFF_UP)
607                                         continue; /* not down */
608                         strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
609                         name[sdl->sdl_nlen] = '\0';
610                         if (namesonly) {
611                                 if (afp == NULL ||
612                                         afp->af_status != link_status ||
613                                         sdl->sdl_type == IFT_ETHER) {
614                                         if (need_nl)
615                                                 putchar(' ');
616                                         fputs(name, stdout);
617                                         need_nl++;
618                                 }
619                                 continue;
620                         }
621                 } else {
622                         if (strlen(name) != sdl->sdl_nlen)
623                                 continue; /* not same len */
624                         if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
625                                 continue; /* not same name */
626                 }
627
628                 if (argc > 0)
629                         ifconfig(argc, argv, afp);
630                 else
631                         status(afp, addrcount, sdl, ifm, ifam);
632
633                 if (all == 0 && namesonly == 0) {
634                         foundit++; /* flag it as 'done' */
635                         break;
636                 }
637         }
638         free(buf);
639
640         if (namesonly && need_nl > 0)
641                 putchar('\n');
642 end:
643         if (printname)
644                 printf("%s\n", name);
645
646         if (all == 0 && namesonly == 0 && foundit == 0)
647                 errx(1, "interface %s does not exist", name);
648
649
650         exit (0);
651 }
652
653
654 int
655 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
656 {
657         int s;
658
659         if (afp == NULL)
660                 afp = &afs[0];
661         ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
662         strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
663
664         if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
665                 err(1, "socket");
666
667         while (argc > 0) {
668                 register const struct cmd *p;
669
670                 for (p = cmds; p->c_name; p++)
671                         if (strcmp(*argv, p->c_name) == 0)
672                                 break;
673                 if (p->c_name == 0 && setaddr)
674                         p++;    /* got src, do dst */
675                 if (p->c_func || p->c_func2) {
676                         if (p->c_parameter == NEXTARG) {
677                                 if (argv[1] == NULL)
678                                         errx(1, "'%s' requires argument",
679                                             p->c_name);
680                                 (*p->c_func)(argv[1], 0, s, afp);
681                                 argc--, argv++;
682                         } else if (p->c_parameter == NEXTARG2) {
683                                 if (argc < 3)
684                                         errx(1, "'%s' requires 2 arguments",
685                                             p->c_name);
686                                 (*p->c_func2)(argv[1], argv[2], s, afp);
687                                 argc -= 2, argv += 2;
688                         } else
689                                 (*p->c_func)(*argv, p->c_parameter, s, afp);
690                 }
691                 argc--, argv++;
692         }
693 #ifdef INET6
694         if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
695                 /* Aggregatable address architecture defines all prefixes
696                    are 64. So, it is convenient to set prefixlen to 64 if
697                    it is not specified. */
698                 setifprefixlen("64", 0, s, afp);
699                 /* in6_getprefix("64", MASK) if MASK is available here... */
700         }
701 #endif
702 #ifndef NO_IPX
703         if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
704                 struct ipxip_req rq;
705                 int size = sizeof(rq);
706
707                 rq.rq_ipx = addreq.ifra_addr;
708                 rq.rq_ip = addreq.ifra_dstaddr;
709
710                 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
711                         Perror("Encapsulation Routing");
712         }
713 #endif
714         if (ifr.ifr_addr.sa_family == AF_APPLETALK)
715                 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
716 #ifdef NS
717         if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
718                 struct nsip_req rq;
719                 int size = sizeof(rq);
720
721                 rq.rq_ns = addreq.ifra_addr;
722                 rq.rq_ip = addreq.ifra_dstaddr;
723
724                 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
725                         Perror("Encapsulation Routing");
726         }
727 #endif
728         if (clearaddr) {
729                 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
730                         warnx("interface %s cannot change %s addresses!",
731                               name, afp->af_name);
732                         clearaddr = NULL;
733                 }
734         }
735         if (clearaddr) {
736                 int ret;
737                 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
738                 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
739                         if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
740                                 /* means no previous address for interface */
741                         } else
742                                 Perror("ioctl (SIOCDIFADDR)");
743                 }
744         }
745         if (newaddr) {
746                 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
747                         warnx("interface %s cannot change %s addresses!",
748                               name, afp->af_name);
749                         newaddr = 0;
750                 }
751         }
752         if (newaddr && (setaddr || setmask)) {
753                 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
754                 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
755                         Perror("ioctl (SIOCAIFADDR)");
756         }
757         close(s);
758         return(0);
759 }
760 #define RIDADDR 0
761 #define ADDR    1
762 #define MASK    2
763 #define DSTADDR 3
764
765 /*ARGSUSED*/
766 void
767 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
768 {
769         if (*afp->af_getaddr == NULL)
770                 return;
771         /*
772          * Delay the ioctl to set the interface addr until flags are all set.
773          * The address interpretation may depend on the flags,
774          * and the flags may change when the address is set.
775          */
776         setaddr++;
777         if (doalias == 0 && afp->af_af != AF_LINK)
778                 clearaddr = 1;
779         (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
780 }
781
782 void
783 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
784 {
785         struct addrinfo hints, *srcres, *dstres;
786         struct ifaliasreq addreq;
787         int ecode;
788 #ifdef INET6
789         struct in6_aliasreq in6_addreq; 
790 #endif
791
792         memset(&hints, 0, sizeof(hints));
793         hints.ai_family = afp->af_af;
794
795         if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
796                 errx(1, "error in parsing address string: %s",
797                     gai_strerror(ecode));
798
799         if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)  
800                 errx(1, "error in parsing address string: %s",
801                     gai_strerror(ecode));
802
803         if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
804                 errx(1,
805                     "source and destination address families do not match");
806
807         switch (srcres->ai_addr->sa_family) {
808         case AF_INET:
809                 memset(&addreq, 0, sizeof(addreq));
810                 strncpy(addreq.ifra_name, name, IFNAMSIZ);
811                 memcpy(&addreq.ifra_addr, srcres->ai_addr,
812                     srcres->ai_addr->sa_len);
813                 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
814                     dstres->ai_addr->sa_len);
815
816                 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
817                         warn("SIOCSIFPHYADDR");
818                 break;
819
820 #ifdef INET6
821         case AF_INET6:
822                 memset(&in6_addreq, 0, sizeof(in6_addreq));
823                 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
824                 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
825                     srcres->ai_addr->sa_len);
826                 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
827                     dstres->ai_addr->sa_len);
828
829                 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
830                         warn("SIOCSIFPHYADDR_IN6");
831                 break;
832 #endif /* INET6 */
833
834         default:
835                 warn("address family not supported");
836         }
837
838         freeaddrinfo(srcres);
839         freeaddrinfo(dstres);
840 }
841
842 /* ARGSUSED */
843 void
844 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
845 {
846
847         if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
848                 err(1, "SIOCDIFPHYADDR");
849 }
850
851 void
852 setifnetmask(const char *addr, int dummy __unused, int s,
853              const struct afswtch *afp)
854 {
855         if (*afp->af_getaddr == NULL)
856                 return;
857         setmask++;
858         (*afp->af_getaddr)(addr, MASK);
859 }
860
861 #ifdef INET6
862 void
863 setifprefixlen(const char *addr, int dummy __unused, int s,
864                const struct afswtch *afp)
865 {
866         if (*afp->af_getprefix)
867                 (*afp->af_getprefix)(addr, MASK);
868         explicit_prefix = 1;
869 }
870
871 void
872 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
873             const struct afswtch *afp)
874 {
875         if (afp->af_af != AF_INET6)
876                 err(1, "address flags can be set only for inet6 addresses");
877
878         if (flag < 0)
879                 in6_addreq.ifra_flags &= ~(-flag);
880         else
881                 in6_addreq.ifra_flags |= flag;
882 }
883
884 void
885 setip6pltime(const char *seconds, int dummy __unused, int s,
886              const struct afswtch *afp)
887 {
888         setip6lifetime("pltime", seconds, s, afp);
889 }
890
891 void
892 setip6vltime(const char *seconds, int dummy __unused, int s,
893              const struct afswtch *afp)
894 {
895         setip6lifetime("vltime", seconds, s, afp);
896 }
897
898 void
899 setip6lifetime(const char *cmd, const char *val, int s,
900                const struct afswtch *afp)
901 {
902         time_t newval, t;
903         char *ep;
904
905         t = time(NULL);
906         newval = (time_t)strtoul(val, &ep, 0);
907         if (val == ep)
908                 errx(1, "invalid %s", cmd);
909         if (afp->af_af != AF_INET6)
910                 errx(1, "%s not allowed for the AF", cmd);
911         if (strcmp(cmd, "vltime") == 0) {
912                 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
913                 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
914         } else if (strcmp(cmd, "pltime") == 0) {
915                 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
916                 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
917         }
918 }
919
920 void
921 setip6eui64(const char *cmd, int dummy __unused, int s,
922             const struct afswtch *afp)
923 {
924         struct ifaddrs *ifap, *ifa;
925         const struct sockaddr_in6 *sin6 = NULL;
926         const struct in6_addr *lladdr = NULL;
927         struct in6_addr *in6;
928
929         if (afp->af_af != AF_INET6)
930                 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
931         in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
932         if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
933                 errx(EXIT_FAILURE, "interface index is already filled");
934         if (getifaddrs(&ifap) != 0)
935                 err(EXIT_FAILURE, "getifaddrs");
936         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
937                 if (ifa->ifa_addr->sa_family == AF_INET6 &&
938                     strcmp(ifa->ifa_name, name) == 0) {
939                         sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
940                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
941                                 lladdr = &sin6->sin6_addr;
942                                 break;
943                         }
944                 }
945         }
946         if (!lladdr)
947                 errx(EXIT_FAILURE, "could not determine link local address"); 
948
949         memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
950
951         freeifaddrs(ifap);
952 }
953 #endif
954
955 void
956 setifbroadaddr(const char *addr, int dummy __unused, int s,
957                const struct afswtch *afp)
958 {
959         if (*afp->af_getaddr == NULL)
960                 return;
961         (*afp->af_getaddr)(addr, DSTADDR);
962 }
963
964 void
965 setifipdst(const char *addr, int dummy __unused, int s,
966            const struct afswtch *afp)
967 {
968         in_getaddr(addr, DSTADDR);
969         setipdst++;
970         clearaddr = 0;
971         newaddr = 0;
972 }
973 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
974
975 void
976 notealias(const char *addr, int param, int s, const struct afswtch *afp)
977 {
978         if (setaddr && doalias == 0 && param < 0)
979                 bcopy((caddr_t)rqtosa(af_addreq),
980                       (caddr_t)rqtosa(af_ridreq),
981                       rqtosa(af_addreq)->sa_len);
982         doalias = param;
983         if (param < 0) {
984                 clearaddr = 1;
985                 newaddr = 0;
986         } else
987                 clearaddr = 0;
988 }
989
990 /*ARGSUSED*/
991 void
992 setifdstaddr(const char *addr, int param __unused, int s,
993              const struct afswtch *afp)
994 {
995         if (*afp->af_getaddr == NULL)
996                 return;
997         (*afp->af_getaddr)(addr, DSTADDR);
998 }
999
1000 /*
1001  * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1002  * of the ifreq structure, which may confuse other parts of ifconfig.
1003  * Make a private copy so we can avoid that.
1004  */
1005 void
1006 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1007 {
1008         struct ifreq            my_ifr;
1009
1010         bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1011
1012         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1013                 Perror("ioctl (SIOCGIFFLAGS)");
1014                 exit(1);
1015         }
1016         strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1017         flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1018
1019         if (value < 0) {
1020                 value = -value;
1021                 flags &= ~value;
1022         } else
1023                 flags |= value;
1024         my_ifr.ifr_flags = flags & 0xffff;
1025         my_ifr.ifr_flagshigh = flags >> 16;
1026         if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1027                 Perror(vname);
1028 }
1029
1030 void
1031 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1032 {
1033
1034         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1035                 Perror("ioctl (SIOCGIFCAP)");
1036                 exit(1);
1037         }
1038         flags = ifr.ifr_curcap;
1039         if (value < 0) {
1040                 value = -value;
1041                 flags &= ~value;
1042         } else
1043                 flags |= value;
1044         ifr.ifr_reqcap = flags;
1045         if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1046                 Perror(vname);
1047 }
1048
1049 void
1050 setifmetric(const char *val, int dummy __unused, int s,
1051             const struct afswtch *afp)
1052 {
1053         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1054         ifr.ifr_metric = atoi(val);
1055         if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1056                 warn("ioctl (set metric)");
1057 }
1058
1059 void
1060 setifmtu(const char *val, int dummy __unused, int s, const struct afswtch *afp)
1061 {
1062         strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1063         ifr.ifr_mtu = atoi(val);
1064         if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1065                 warn("ioctl (set mtu)");
1066 }
1067
1068 void
1069 setifname(const char *val, int dummy __unused, int s, 
1070     const struct afswtch *afp)
1071 {
1072         char    *newname;
1073
1074         newname = strdup(val);
1075         
1076         ifr.ifr_data = newname;
1077         if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1078                 warn("ioctl (set name)");
1079                 free(newname);
1080                 return;
1081         }
1082         strlcpy(name, newname, sizeof(name));
1083
1084         free(newname);
1085
1086         /*
1087          * Even if we just created the interface, we don't need to print
1088          * its name because we just nailed it down separately.
1089          */
1090         printname = 0;
1091 }
1092
1093 #define IFFBITS \
1094 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1095 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1096 "\20MULTICAST"
1097
1098 #define IFCAPBITS \
1099 "\003\1rxcsum\2txcsum\3netcons"
1100
1101 /*
1102  * Print the status of the interface.  If an address family was
1103  * specified, show it and it only; otherwise, show them all.
1104  */
1105 void
1106 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1107        struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1108 {
1109         const struct afswtch *p = NULL;
1110         struct  rt_addrinfo info;
1111         int allfamilies, s;
1112         struct ifstat ifs;
1113
1114         if (afp == NULL) {
1115                 allfamilies = 1;
1116                 afp = &afs[0];
1117         } else
1118                 allfamilies = 0;
1119
1120         ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1121         strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1122
1123         if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1124                 err(1, "socket");
1125
1126         printf("%s: ", name);
1127         printb("flags", flags, IFFBITS);
1128         if (ifm->ifm_data.ifi_metric)
1129                 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1130         if (ifm->ifm_data.ifi_mtu)
1131                 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1132         putchar('\n');
1133
1134         if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1135                 if (ifr.ifr_curcap != 0) {
1136                         printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1137                         putchar('\n');
1138                 }
1139                 if (supmedia && ifr.ifr_reqcap != 0) {
1140                         printf("\tcapability list:\n");
1141                         printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1142                         putchar('\n');
1143                 }
1144         }
1145
1146         tunnel_status(s);
1147
1148         while (addrcount > 0) {
1149                 
1150                 info.rti_addrs = ifam->ifam_addrs;
1151
1152                 /* Expand the compacted addresses */
1153                 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1154                           &info);
1155
1156                 if (!allfamilies) {
1157                         if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1158                                 p = afp;
1159                                 (*p->af_status)(s, &info);
1160                         }
1161                 } else for (p = afs; p->af_name; p++) {
1162                         if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1163                                 (*p->af_status)(s, &info);
1164                 }
1165                 addrcount--;
1166                 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1167         }
1168         if (allfamilies || afp->af_status == link_status)
1169                 link_status(s, (struct rt_addrinfo *)sdl);
1170 #ifdef USE_IF_MEDIA
1171         if (allfamilies || afp->af_status == media_status)
1172                 media_status(s, NULL);
1173 #endif
1174 #ifdef USE_VLANS
1175         if (allfamilies || afp->af_status == vlan_status)
1176                 vlan_status(s, NULL);
1177 #endif
1178 #ifdef USE_IEEE80211
1179         if (allfamilies || afp->af_status == ieee80211_status)
1180                 ieee80211_status(s, NULL);
1181 #endif
1182         strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1183         if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 
1184                 printf("%s", ifs.ascii);
1185
1186         if (!allfamilies && !p && afp->af_status != media_status &&
1187             afp->af_status != link_status
1188 #ifdef USE_VLANS
1189             && afp->af_status != vlan_status
1190 #endif
1191                 )
1192                 warnx("%s has no %s interface address!", name, afp->af_name);
1193
1194         close(s);
1195         return;
1196 }
1197
1198 void
1199 tunnel_status(int s)
1200 {
1201         char psrcaddr[NI_MAXHOST];
1202         char pdstaddr[NI_MAXHOST];
1203         u_long srccmd, dstcmd;
1204         struct ifreq *ifrp;
1205         const char *ver = "";
1206 #ifdef NI_WITHSCOPEID
1207         const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1208 #else
1209         const int niflag = NI_NUMERICHOST;
1210 #endif
1211 #ifdef INET6
1212         struct in6_ifreq in6_ifr;
1213         int s6;
1214 #endif /* INET6 */
1215
1216         psrcaddr[0] = pdstaddr[0] = '\0';
1217
1218 #ifdef INET6
1219         memset(&in6_ifr, 0, sizeof(in6_ifr));
1220         strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1221         s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1222         if (s6 < 0) {
1223                 srccmd = SIOCGIFPSRCADDR;
1224                 dstcmd = SIOCGIFPDSTADDR;
1225                 ifrp = &ifr;
1226         } else {
1227                 close(s6);
1228                 srccmd = SIOCGIFPSRCADDR_IN6;
1229                 dstcmd = SIOCGIFPDSTADDR_IN6;
1230                 ifrp = (struct ifreq *)&in6_ifr;
1231         }
1232 #else /* INET6 */
1233         srccmd = SIOCGIFPSRCADDR;
1234         dstcmd = SIOCGIFPDSTADDR;
1235         ifrp = &ifr;
1236 #endif /* INET6 */
1237
1238         if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1239                 return;
1240 #ifdef INET6
1241         if (ifrp->ifr_addr.sa_family == AF_INET6)
1242                 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1243 #endif
1244         getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1245             psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1246 #ifdef INET6
1247         if (ifrp->ifr_addr.sa_family == AF_INET6)
1248                 ver = "6";
1249 #endif
1250
1251         if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1252                 return;
1253 #ifdef INET6
1254         if (ifrp->ifr_addr.sa_family == AF_INET6)
1255                 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1256 #endif
1257         getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1258             pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1259
1260         printf("\ttunnel inet%s %s --> %s\n", ver,
1261             psrcaddr, pdstaddr);
1262 }
1263
1264 void
1265 in_status(int s __unused, struct rt_addrinfo *info)
1266 {
1267         struct sockaddr_in *sin, null_sin;
1268         
1269         memset(&null_sin, 0, sizeof(null_sin));
1270
1271         sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1272         printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1273
1274         if (flags & IFF_POINTOPOINT) {
1275                 /* note RTAX_BRD overlap with IFF_BROADCAST */
1276                 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1277                 if (!sin)
1278                         sin = &null_sin;
1279                 printf("--> %s ", inet_ntoa(sin->sin_addr));
1280         }
1281
1282         sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1283         if (!sin)
1284                 sin = &null_sin;
1285         printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1286
1287         if (flags & IFF_BROADCAST) {
1288                 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1289                 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1290                 if (sin && sin->sin_addr.s_addr != 0)
1291                         printf("broadcast %s", inet_ntoa(sin->sin_addr));
1292         }
1293         putchar('\n');
1294 }
1295
1296 #ifdef INET6
1297 void
1298 in6_fillscopeid(struct sockaddr_in6 *sin6)
1299 {
1300 #if defined(__KAME__) && defined(KAME_SCOPEID)
1301         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1302                 sin6->sin6_scope_id =
1303                         ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1304                 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1305         }
1306 #endif
1307 }
1308
1309 void
1310 in6_status(int s __unused, struct rt_addrinfo *info)
1311 {
1312         struct sockaddr_in6 *sin, null_sin;
1313         struct in6_ifreq ifr6;
1314         int s6;
1315         u_int32_t flags6;
1316         struct in6_addrlifetime lifetime;
1317         time_t t = time(NULL);
1318         int error;
1319         u_int32_t scopeid;
1320
1321         memset(&null_sin, 0, sizeof(null_sin));
1322
1323         sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1324         strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1325         if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1326                 perror("ifconfig: socket");
1327                 return;
1328         }
1329         ifr6.ifr_addr = *sin;
1330         if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1331                 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1332                 close(s6);
1333                 return;
1334         }
1335         flags6 = ifr6.ifr_ifru.ifru_flags6;
1336         memset(&lifetime, 0, sizeof(lifetime));
1337         ifr6.ifr_addr = *sin;
1338         if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1339                 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1340                 close(s6);
1341                 return;
1342         }
1343         lifetime = ifr6.ifr_ifru.ifru_lifetime;
1344         close(s6);
1345
1346         /* XXX: embedded link local addr check */
1347         if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1348             *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1349                 u_short index;
1350
1351                 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1352                 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1353                 if (sin->sin6_scope_id == 0)
1354                         sin->sin6_scope_id = ntohs(index);
1355         }
1356         scopeid = sin->sin6_scope_id;
1357
1358         error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1359                             sizeof(addr_buf), NULL, 0,
1360                             NI_NUMERICHOST|NI_WITHSCOPEID);
1361         if (error != 0)
1362                 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1363                           sizeof(addr_buf));
1364         printf("\tinet6 %s ", addr_buf);
1365
1366         if (flags & IFF_POINTOPOINT) {
1367                 /* note RTAX_BRD overlap with IFF_BROADCAST */
1368                 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1369                 /*
1370                  * some of the interfaces do not have valid destination
1371                  * address.
1372                  */
1373                 if (sin && sin->sin6_family == AF_INET6) {
1374                         int error;
1375
1376                         /* XXX: embedded link local addr check */
1377                         if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1378                             *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1379                                 u_short index;
1380
1381                                 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1382                                 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1383                                 if (sin->sin6_scope_id == 0)
1384                                         sin->sin6_scope_id = ntohs(index);
1385                         }
1386
1387                         error = getnameinfo((struct sockaddr *)sin,
1388                                             sin->sin6_len, addr_buf,
1389                                             sizeof(addr_buf), NULL, 0,
1390                                             NI_NUMERICHOST|NI_WITHSCOPEID);
1391                         if (error != 0)
1392                                 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1393                                           sizeof(addr_buf));
1394                         printf("--> %s ", addr_buf);
1395                 }
1396         }
1397
1398         sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1399         if (!sin)
1400                 sin = &null_sin;
1401         printf("prefixlen %d ", prefix(&sin->sin6_addr,
1402                 sizeof(struct in6_addr)));
1403
1404         if ((flags6 & IN6_IFF_ANYCAST) != 0)
1405                 printf("anycast ");
1406         if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1407                 printf("tentative ");
1408         if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1409                 printf("duplicated ");
1410         if ((flags6 & IN6_IFF_DETACHED) != 0)
1411                 printf("detached ");
1412         if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1413                 printf("deprecated ");
1414         if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1415                 printf("autoconf ");
1416         if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1417                 printf("temporary ");
1418
1419         if (scopeid)
1420                 printf("scopeid 0x%x ", scopeid);
1421
1422         if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1423                 printf("pltime ");
1424                 if (lifetime.ia6t_preferred) {
1425                         printf("%s ", lifetime.ia6t_preferred < t
1426                                 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1427                 } else
1428                         printf("infty ");
1429
1430                 printf("vltime ");
1431                 if (lifetime.ia6t_expire) {
1432                         printf("%s ", lifetime.ia6t_expire < t
1433                                 ? "0" : sec2str(lifetime.ia6t_expire - t));
1434                 } else
1435                         printf("infty ");
1436         }
1437
1438         putchar('\n');
1439 }
1440 #endif /*INET6*/
1441
1442 #ifndef NO_IPX
1443 void
1444 ipx_status(int s __unused, struct rt_addrinfo *info)
1445 {
1446         struct sockaddr_ipx *sipx, null_sipx;
1447
1448         memset(&null_sipx, 0, sizeof(null_sipx));
1449
1450         sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1451         printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1452
1453         if (flags & IFF_POINTOPOINT) {
1454                 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1455                 if (!sipx)
1456                         sipx = &null_sipx;
1457                 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1458         }
1459         putchar('\n');
1460 }
1461 #endif
1462
1463 void
1464 at_status(int s __unused, struct rt_addrinfo *info)
1465 {
1466         struct sockaddr_at *sat, null_sat;
1467         struct netrange *nr;
1468
1469         memset(&null_sat, 0, sizeof(null_sat));
1470
1471         sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1472         nr = &sat->sat_range.r_netrange;
1473         printf("\tatalk %d.%d range %d-%d phase %d",
1474                 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1475                 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1476         if (flags & IFF_POINTOPOINT) {
1477                 /* note RTAX_BRD overlap with IFF_BROADCAST */
1478                 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1479                 if (!sat)
1480                         sat = &null_sat;
1481                 printf("--> %d.%d",
1482                         ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1483         }
1484         if (flags & IFF_BROADCAST) {
1485                 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1486                 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1487                 if (sat)
1488                         printf(" broadcast %d.%d",
1489                                 ntohs(sat->sat_addr.s_net),
1490                                 sat->sat_addr.s_node);
1491         }
1492
1493         putchar('\n');
1494 }
1495
1496 #ifdef NS
1497 void
1498 xns_status(int s __unused, struct rt_addrinfo *info)
1499 {
1500         struct sockaddr_ns *sns, null_sns;
1501
1502         memset(&null_sns, 0, sizeof(null_sns));
1503
1504         sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1505         printf("\tns %s ", ns_ntoa(sns->sns_addr));
1506
1507         if (flags & IFF_POINTOPOINT) {
1508                 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1509                 if (!sns)
1510                         sns = &null_sns;
1511                 printf("--> %s ", ns_ntoa(sns->sns_addr));
1512         }
1513
1514         putchar('\n');
1515         close(s);
1516 }
1517 #endif
1518
1519
1520 void
1521 link_status(int s __unused, struct rt_addrinfo *info)
1522 {
1523         struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1524
1525         if (sdl->sdl_alen > 0) {
1526                 if (sdl->sdl_type == IFT_ETHER &&
1527                     sdl->sdl_alen == ETHER_ADDR_LEN)
1528                         printf("\tether %s\n",
1529                             ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1530                 else {
1531                         int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1532
1533                         printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1534                 }
1535         }
1536 }
1537
1538 void
1539 Perror(const char *cmd)
1540 {
1541         switch (errno) {
1542
1543         case ENXIO:
1544                 errx(1, "%s: no such interface", cmd);
1545                 break;
1546
1547         case EPERM:
1548                 errx(1, "%s: permission denied", cmd);
1549                 break;
1550
1551         default:
1552                 err(1, "%s", cmd);
1553         }
1554 }
1555
1556 #define SIN(x) ((struct sockaddr_in *) &(x))
1557 struct sockaddr_in *sintab[] = {
1558 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1559 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1560
1561 void
1562 in_getaddr(const char *s, int which)
1563 {
1564         register struct sockaddr_in *sin = sintab[which];
1565         struct hostent *hp;
1566         struct netent *np;
1567
1568         sin->sin_len = sizeof(*sin);
1569         if (which != MASK)
1570                 sin->sin_family = AF_INET;
1571
1572         if (which == ADDR) {
1573                 char *p = NULL;
1574
1575                 if((p = strrchr(s, '/')) != NULL) {
1576                         /* address is `name/masklen' */
1577                         int masklen;
1578                         int ret;
1579                         struct sockaddr_in *min = sintab[MASK];
1580                         *p = '\0';
1581                         ret = sscanf(p+1, "%u", &masklen);
1582                         if(ret != 1 || (masklen < 0 || masklen > 32)) {
1583                                 *p = '/';
1584                                 errx(1, "%s: bad value", s);
1585                         }
1586                         min->sin_len = sizeof(*min);
1587                         min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) & 
1588                                               0xffffffff);
1589                 }
1590         }
1591
1592         if (inet_aton(s, &sin->sin_addr))
1593                 return;
1594         if ((hp = gethostbyname(s)) != 0)
1595                 bcopy(hp->h_addr, (char *)&sin->sin_addr, 
1596                     MIN(hp->h_length, sizeof(sin->sin_addr)));
1597         else if ((np = getnetbyname(s)) != 0)
1598                 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1599         else
1600                 errx(1, "%s: bad value", s);
1601 }
1602
1603 #ifdef INET6
1604 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1605 struct  sockaddr_in6 *sin6tab[] = {
1606 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1607 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1608
1609 void
1610 in6_getaddr(const char *s, int which)
1611 {
1612         register struct sockaddr_in6 *sin = sin6tab[which];
1613         struct addrinfo hints, *res;
1614         int error = -1;
1615
1616         newaddr &= 1;
1617
1618         sin->sin6_len = sizeof(*sin);
1619         if (which != MASK)
1620                 sin->sin6_family = AF_INET6;
1621
1622         if (which == ADDR) {
1623                 char *p = NULL;
1624                 if((p = strrchr(s, '/')) != NULL) {
1625                         *p = '\0';
1626                         in6_getprefix(p + 1, MASK);
1627                         explicit_prefix = 1;
1628                 }
1629         }
1630
1631         if (sin->sin6_family == AF_INET6) {
1632                 bzero(&hints, sizeof(struct addrinfo));
1633                 hints.ai_family = AF_INET6;
1634                 error = getaddrinfo(s, NULL, &hints, &res);
1635         }
1636         if (error != 0) {
1637                 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1638                         errx(1, "%s: bad value", s);
1639         } else
1640                 bcopy(res->ai_addr, sin, res->ai_addrlen);
1641 }
1642
1643 void
1644 in6_getprefix(const char *plen, int which)
1645 {
1646         register struct sockaddr_in6 *sin = sin6tab[which];
1647         register u_char *cp;
1648         int len = atoi(plen);
1649
1650         if ((len < 0) || (len > 128))
1651                 errx(1, "%s: bad value", plen);
1652         sin->sin6_len = sizeof(*sin);
1653         if (which != MASK)
1654                 sin->sin6_family = AF_INET6;
1655         if ((len == 0) || (len == 128)) {
1656                 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1657                 return;
1658         }
1659         memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1660         for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1661                 *cp++ = 0xff;
1662         *cp = 0xff << (8 - len);
1663 }
1664 #endif
1665
1666 /*
1667  * Print a value a la the %b format of the kernel's printf
1668  */
1669 void
1670 printb(const char *s, register unsigned v, register const char *bits)
1671 {
1672         register int i, any = 0;
1673         register char c;
1674
1675         if (bits && *bits == 8)
1676                 printf("%s=%o", s, v);
1677         else
1678                 printf("%s=%x", s, v);
1679         bits++;
1680         if (bits) {
1681                 putchar('<');
1682                 while ((i = *bits++) != '\0') {
1683                         if (v & (1 << (i-1))) {
1684                                 if (any)
1685                                         putchar(',');
1686                                 any = 1;
1687                                 for (; (c = *bits) > 32; bits++)
1688                                         putchar(c);
1689                         } else
1690                                 for (; *bits > 32; bits++)
1691                                         ;
1692                 }
1693                 putchar('>');
1694         }
1695 }
1696
1697 #ifndef NO_IPX
1698 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1699 struct sockaddr_ipx *sipxtab[] = {
1700 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1701 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1702
1703 void
1704 ipx_getaddr(const char *addr, int which)
1705 {
1706         struct sockaddr_ipx *sipx = sipxtab[which];
1707
1708         sipx->sipx_family = AF_IPX;
1709         sipx->sipx_len = sizeof(*sipx);
1710         sipx->sipx_addr = ipx_addr(addr);
1711         if (which == MASK)
1712                 printf("Attempt to set IPX netmask will be ineffectual\n");
1713 }
1714 #endif
1715
1716 void
1717 at_getaddr(const char *addr, int which)
1718 {
1719         struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1720         u_int net, node;
1721
1722         sat->sat_family = AF_APPLETALK;
1723         sat->sat_len = sizeof(*sat);
1724         if (which == MASK)
1725                 errx(1, "AppleTalk does not use netmasks");
1726         if (sscanf(addr, "%u.%u", &net, &node) != 2
1727             || net > 0xffff || node > 0xfe)
1728                 errx(1, "%s: illegal address", addr);
1729         sat->sat_addr.s_net = htons(net);
1730         sat->sat_addr.s_node = node;
1731 }
1732
1733 void
1734 link_getaddr(const char *addr, int which)
1735 {
1736         char *temp;
1737         struct sockaddr_dl sdl;
1738         struct sockaddr *sa = &ridreq.ifr_addr;
1739
1740         if (which != ADDR)
1741                 errx(1, "can't set link-level netmask or broadcast");
1742         if ((temp = malloc(strlen(addr) + 1)) == NULL)
1743                 errx(1, "malloc failed");
1744         temp[0] = ':';
1745         strcpy(temp + 1, addr);
1746         sdl.sdl_len = sizeof(sdl);
1747         link_addr(temp, &sdl);
1748         free(temp);
1749         if (sdl.sdl_alen > sizeof(sa->sa_data))
1750                 errx(1, "malformed link-level address");
1751         sa->sa_family = AF_LINK;
1752         sa->sa_len = sdl.sdl_alen;
1753         bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1754 }
1755
1756 /* XXX  FIXME -- should use strtoul for better parsing. */
1757 void
1758 setatrange(const char *range, int dummy __unused, int s,
1759            const struct afswtch *afp)
1760 {
1761         u_short first = 123, last = 123;
1762
1763         if (sscanf(range, "%hu-%hu", &first, &last) != 2
1764             || first == 0 || first > 0xffff
1765             || last == 0 || last > 0xffff || first > last)
1766                 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1767         at_nr.nr_firstnet = htons(first);
1768         at_nr.nr_lastnet = htons(last);
1769 }
1770
1771 void
1772 setatphase(const char *phase, int dummy __unused, int s,
1773            const struct afswtch *afp)
1774 {
1775         if (!strcmp(phase, "1"))
1776                 at_nr.nr_phase = 1;
1777         else if (!strcmp(phase, "2"))
1778                 at_nr.nr_phase = 2;
1779         else
1780                 errx(1, "%s: illegal phase", phase);
1781 }
1782
1783 void
1784 checkatrange(struct sockaddr_at *sat)
1785 {
1786         if (at_nr.nr_phase == 0)
1787                 at_nr.nr_phase = 2;     /* Default phase 2 */
1788         if (at_nr.nr_firstnet == 0)
1789                 at_nr.nr_firstnet =     /* Default range of one */
1790                 at_nr.nr_lastnet = sat->sat_addr.s_net;
1791 printf("\tatalk %d.%d range %d-%d phase %d\n",
1792         ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1793         ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1794         if ((u_short) ntohs(at_nr.nr_firstnet) >
1795                         (u_short) ntohs(sat->sat_addr.s_net)
1796                     || (u_short) ntohs(at_nr.nr_lastnet) <
1797                         (u_short) ntohs(sat->sat_addr.s_net))
1798                 errx(1, "AppleTalk address is not in range");
1799         sat->sat_range.r_netrange = at_nr;
1800 }
1801
1802 #ifdef NS
1803 #define SNS(x) ((struct sockaddr_ns *) &(x))
1804 struct sockaddr_ns *snstab[] = {
1805 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1806 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1807
1808 void
1809 xns_getaddr(const char *addr, int which)
1810 {
1811         struct sockaddr_ns *sns = snstab[which];
1812
1813         sns->sns_family = AF_NS;
1814         sns->sns_len = sizeof(*sns);
1815         sns->sns_addr = ns_addr(addr);
1816         if (which == MASK)
1817                 printf("Attempt to set XNS netmask will be ineffectual\n");
1818 }
1819 #endif
1820
1821 #ifdef INET6
1822 int
1823 prefix(void *val, int size)
1824 {
1825         register u_char *name = (u_char *)val;
1826         register int byte, bit, plen = 0;
1827
1828         for (byte = 0; byte < size; byte++, plen += 8)
1829                 if (name[byte] != 0xff)
1830                         break;
1831         if (byte == size)
1832                 return (plen);
1833         for (bit = 7; bit != 0; bit--, plen++)
1834                 if (!(name[byte] & (1 << bit)))
1835                         break;
1836         for (; bit != 0; bit--)
1837                 if (name[byte] & (1 << bit))
1838                         return(0);
1839         byte++;
1840         for (; byte < size; byte++)
1841                 if (name[byte])
1842                         return(0);
1843         return (plen);
1844 }
1845
1846 static char *
1847 sec2str(time_t total)
1848 {
1849         static char result[256];
1850         int days, hours, mins, secs;
1851         int first = 1;
1852         char *p = result;
1853
1854         if (0) {
1855                 days = total / 3600 / 24;
1856                 hours = (total / 3600) % 24;
1857                 mins = (total / 60) % 60;
1858                 secs = total % 60;
1859
1860                 if (days) {
1861                         first = 0;
1862                         p += sprintf(p, "%dd", days);
1863                 }
1864                 if (!first || hours) {
1865                         first = 0;
1866                         p += sprintf(p, "%dh", hours);
1867                 }
1868                 if (!first || mins) {
1869                         first = 0;
1870                         p += sprintf(p, "%dm", mins);
1871                 }
1872                 sprintf(p, "%ds", secs);
1873         } else
1874                 sprintf(result, "%lu", (unsigned long)total);
1875
1876         return(result);
1877 }
1878 #endif /*INET6*/
1879
1880 void
1881 ifmaybeload(char *name)
1882 {
1883         struct module_stat mstat;
1884         int fileid, modid;
1885         char ifkind[35], *cp, *dp;
1886
1887
1888         /* turn interface and unit into module name */
1889         strcpy(ifkind, "if_");
1890         for (cp = name, dp = ifkind + 3;
1891             (*cp != 0) && !isdigit(*cp); cp++, dp++)
1892                 *dp = *cp;
1893         *dp = 0;
1894
1895         /* scan files in kernel */
1896         mstat.version = sizeof(struct module_stat);
1897         for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1898                 /* scan modules in file */
1899                 for (modid = kldfirstmod(fileid); modid > 0;
1900                      modid = modfnext(modid)) {
1901                         if (modstat(modid, &mstat) < 0)
1902                                 continue;
1903                         /* strip bus name if present */
1904                         if ((cp = strchr(mstat.name, '/')) != NULL) {
1905                                 cp++;
1906                         } else {
1907                                 cp = mstat.name;
1908                         }
1909                         /* already loaded? */
1910                         if (strncmp(name, cp, strlen(cp)) == 0 ||
1911                                 strncmp(ifkind, cp, strlen(cp)) == 0)
1912                                 return;
1913                 }
1914         }
1915
1916         /* not present, we should try to load it */
1917         kldload(ifkind);
1918 }
1919
1920 void
1921 list_cloners(void)
1922 {
1923         struct if_clonereq ifcr;
1924         char *cp, *buf;
1925         int idx;
1926         int s;
1927
1928         s = socket(AF_INET, SOCK_DGRAM, 0);
1929         if (s == -1)
1930                 err(1, "socket");
1931
1932         memset(&ifcr, 0, sizeof(ifcr));
1933
1934         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1935                 err(1, "SIOCIFGCLONERS for count");
1936
1937         buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1938         if (buf == NULL)
1939                 err(1, "unable to allocate cloner name buffer");
1940
1941         ifcr.ifcr_count = ifcr.ifcr_total;
1942         ifcr.ifcr_buffer = buf;
1943
1944         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1945                 err(1, "SIOCIFGCLONERS for names");
1946
1947         /*
1948          * In case some disappeared in the mean time, clamp it down.
1949          */
1950         if (ifcr.ifcr_count > ifcr.ifcr_total)
1951                 ifcr.ifcr_count = ifcr.ifcr_total;
1952
1953         for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1954                 if (idx > 0)
1955                         putchar(' ');
1956                 printf("%s", cp);
1957         }
1958
1959         putchar('\n');
1960         free(buf);
1961 }
1962
1963 void
1964 clone_create(void)
1965 {
1966         int s;
1967
1968         s = socket(AF_INET, SOCK_DGRAM, 0);
1969         if (s == -1)
1970                 err(1, "socket");
1971
1972         memset(&ifr, 0, sizeof(ifr));
1973         (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1974         if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
1975                 err(1, "SIOCIFCREATE");
1976
1977         /*
1978          * If we get a different name back then we put in, we probably
1979          * want to print it out, but we might change our mind later so
1980          * we just signal our intrest and leave the printout for later.
1981          */
1982         if (strcmp(name, ifr.ifr_name) != 0) {
1983                 printname = 1;
1984                 strlcpy(name, ifr.ifr_name, sizeof(name));
1985         }
1986
1987         close(s);
1988 }
1989
1990 void
1991 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
1992 {
1993
1994         (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1995         if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
1996                 err(1, "SIOCIFDESTROY");
1997         /*
1998          * If we create and destroy an interface in the same command,
1999          * there isn't any reason to print it's name.
2000          */
2001         printname = 0;
2002 }