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