2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.51.2.19 2003/01/28 11:02:56 fjoe Exp $
36 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.6 2004/02/04 17:39:59 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
47 #include <net/ethernet.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <arpa/inet.h>
61 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
68 #include <netipx/ipx.h>
69 #include <netipx/ipx_if.h>
73 #include <netatalk/at.h>
79 #include <netns/ns_if.h>
96 /* wrapper for KAME-special getnameinfo() */
97 #ifndef NI_WITHSCOPEID
98 #define NI_WITHSCOPEID 0
101 struct ifreq ifr, ridreq;
102 struct ifaliasreq addreq;
104 struct in6_ifreq in6_ridreq;
105 struct in6_aliasreq in6_addreq =
111 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
113 struct sockaddr_in netmask;
114 struct netrange at_nr; /* AppleTalk net range */
125 static int ip6lifetime;
134 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
137 void Perror(const char *cmd);
138 void checkatrange(struct sockaddr_at *);
139 int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
140 void notealias(const char *, int, int, const struct afswtch *afp);
141 void list_cloners(void);
142 void printb(const char *s, unsigned value, const char *bits);
143 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
144 void status(const struct afswtch *afp, int addrcount,
145 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
146 struct ifa_msghdr *ifam);
147 void tunnel_status(int s);
149 void ifmaybeload(char *name);
152 void in6_fillscopeid(struct sockaddr_in6 *sin6);
153 int prefix(void *, int);
154 static char *sec2str(time_t);
155 int explicit_prefix = 0;
158 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
159 typedef void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp);
160 c_func setatphase, setatrange;
161 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
165 c_func setifprefixlen;
169 c_func2 setip6lifetime;
173 c_func setifflags, setifmetric, setifmtu, setifcap;
174 c_func clone_destroy;
177 void clone_create(void);
180 #define NEXTARG 0xffffff
181 #define NEXTARG2 0xfffffe
186 int c_parameter; /* NEXTARG means next argv */
187 void (*c_func)(const char *, int, int, const struct afswtch *afp);
188 void (*c_func2)(const char *, const char *, int, const struct afswtch *afp);
190 { "up", IFF_UP, setifflags } ,
191 { "down", -IFF_UP, setifflags },
192 { "arp", -IFF_NOARP, setifflags },
193 { "-arp", IFF_NOARP, setifflags },
194 { "debug", IFF_DEBUG, setifflags },
195 { "-debug", -IFF_DEBUG, setifflags },
196 { "promisc", IFF_PPROMISC, setifflags },
197 { "-promisc", -IFF_PPROMISC, setifflags },
198 { "add", IFF_UP, notealias },
199 { "alias", IFF_UP, notealias },
200 { "-alias", -IFF_UP, notealias },
201 { "delete", -IFF_UP, notealias },
202 { "remove", -IFF_UP, notealias },
204 #define EN_SWABIPS 0x1000
205 { "swabips", EN_SWABIPS, setifflags },
206 { "-swabips", -EN_SWABIPS, setifflags },
208 { "netmask", NEXTARG, setifnetmask },
210 { "prefixlen", NEXTARG, setifprefixlen },
211 { "anycast", IN6_IFF_ANYCAST, setip6flags },
212 { "tentative", IN6_IFF_TENTATIVE, setip6flags },
213 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags },
214 { "deprecated", IN6_IFF_DEPRECATED, setip6flags },
215 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags },
216 { "autoconf", IN6_IFF_AUTOCONF, setip6flags },
217 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags },
218 { "pltime", NEXTARG, setip6pltime },
219 { "vltime", NEXTARG, setip6vltime },
220 { "eui64", 0, setip6eui64 },
222 { "range", NEXTARG, setatrange },
223 { "phase", NEXTARG, setatphase },
224 { "metric", NEXTARG, setifmetric },
225 { "broadcast", NEXTARG, setifbroadaddr },
226 { "ipdst", NEXTARG, setifipdst },
227 { "tunnel", NEXTARG2, NULL, settunnel },
228 { "deletetunnel", 0, deletetunnel },
229 { "link0", IFF_LINK0, setifflags },
230 { "-link0", -IFF_LINK0, setifflags },
231 { "link1", IFF_LINK1, setifflags },
232 { "-link1", -IFF_LINK1, setifflags },
233 { "link2", IFF_LINK2, setifflags },
234 { "-link2", -IFF_LINK2, setifflags },
236 { "media", NEXTARG, setmedia },
237 { "mediaopt", NEXTARG, setmediaopt },
238 { "-mediaopt", NEXTARG, unsetmediaopt },
241 { "vlan", NEXTARG, setvlantag },
242 { "vlandev", NEXTARG, setvlandev },
243 { "-vlandev", NEXTARG, unsetvlandev },
246 /* XXX `create' special-cased below */
247 {"create", 0, clone_create },
248 {"plumb", 0, clone_create },
250 {"destroy", 0, clone_destroy },
251 {"unplumb", 0, clone_destroy },
253 { "ssid", NEXTARG, set80211ssid },
254 { "nwid", NEXTARG, set80211ssid },
255 { "stationname", NEXTARG, set80211stationname },
256 { "station", NEXTARG, set80211stationname }, /* BSD/OS */
257 { "channel", NEXTARG, set80211channel },
258 { "authmode", NEXTARG, set80211authmode },
259 { "powersavemode", NEXTARG, set80211powersavemode },
260 { "powersave", 1, set80211powersave },
261 { "-powersave", 0, set80211powersave },
262 { "powersavesleep", NEXTARG, set80211powersavesleep },
263 { "wepmode", NEXTARG, set80211wepmode },
264 { "wep", 1, set80211wep },
265 { "-wep", 0, set80211wep },
266 { "weptxkey", NEXTARG, set80211weptxkey },
267 { "wepkey", NEXTARG, set80211wepkey },
268 { "nwkey", NEXTARG, set80211nwkey }, /* NetBSD */
269 { "-nwkey", 0, set80211wep }, /* NetBSD */
271 { "rxcsum", IFCAP_RXCSUM, setifcap },
272 { "-rxcsum", -IFCAP_RXCSUM, setifcap },
273 { "txcsum", IFCAP_TXCSUM, setifcap },
274 { "-txcsum", -IFCAP_TXCSUM, setifcap },
275 { "netcons", IFCAP_NETCONS, setifcap },
276 { "-netcons", -IFCAP_NETCONS, setifcap },
277 { "normal", -IFF_LINK0, setifflags },
278 { "compress", IFF_LINK0, setifflags },
279 { "noicmp", IFF_LINK1, setifflags },
280 { "mtu", NEXTARG, setifmtu },
282 { 0, 0, setifdstaddr },
286 * XNS support liberally adapted from code written at the University of
287 * Maryland principally by James O'Toole and Chris Torek.
289 typedef void af_status(int, struct rt_addrinfo *);
290 typedef void af_getaddr(const char *, int);
291 typedef void af_getprefix(const char *, int);
293 af_status in_status, at_status, link_status;
294 af_getaddr in_getaddr, at_getaddr, link_getaddr;
297 af_status ipx_status;
298 af_getaddr ipx_getaddr;
302 af_status in6_status;
303 af_getaddr in6_getaddr;
304 af_getprefix in6_getprefix;
307 af_status xns_status;
308 af_getaddr xns_getaddr;
311 /* Known address families */
316 af_status *af_status;
317 af_getaddr *af_getaddr;
318 af_getprefix *af_getprefix;
324 #define C(x) ((caddr_t) &x)
325 { "inet", AF_INET, in_status, in_getaddr, NULL,
326 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
328 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
329 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
330 C(in6_ridreq), C(in6_addreq) },
333 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
334 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
336 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
337 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
339 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
340 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
342 { "link", AF_LINK, link_status, link_getaddr, NULL,
343 0, SIOCSIFLLADDR, NULL, C(ridreq) },
344 { "ether", AF_LINK, link_status, link_getaddr, NULL,
345 0, SIOCSIFLLADDR, NULL, C(ridreq) },
346 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
347 0, SIOCSIFLLADDR, NULL, C(ridreq) },
348 #if 0 /* XXX conflicts with the media command */
350 { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */
353 { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, }, /* XXX not real!! */
356 { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, }, /* XXX not real!! */
363 * Expand the compacted form of addresses as returned via the
364 * configuration read via sysctl().
368 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
369 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
372 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
377 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
378 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
379 if ((rtinfo->rti_addrs & (1 << i)) == 0)
381 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
391 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
392 "usage: ifconfig interface address_family [address [dest_address]]",
395 " ifconfig interface create",
396 " ifconfig -a [-d] [-m] [-u] [address_family]",
397 " ifconfig -l [-d] [-u] [address_family]",
398 " ifconfig [-d] [-m] [-u]");
400 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
401 "usage: ifconfig [-L] interface address_family [address [dest_address]]",
404 " ifconfig interface create",
405 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
406 " ifconfig -l [-d] [-u] [address_family]",
407 " ifconfig [-L] [-d] [-m] [-u]");
413 main(int argc, char * const *argv)
416 int all, namesonly, downonly, uponly;
417 int foundit = 0, need_nl = 0;
418 const struct afswtch *afp = 0;
420 struct if_msghdr *ifm, *nextifm;
421 struct ifa_msghdr *ifam;
422 struct sockaddr_dl *sdl;
423 char *buf, *lim, *next;
429 /* Parse leading line options */
430 all = downonly = uponly = namesonly = 0;
431 while ((c = getopt(argc, argv, "adlmuC"
437 case 'a': /* scan all interfaces */
440 case 'd': /* restrict scan to "down" interfaces */
443 case 'l': /* scan interface names only */
446 case 'm': /* show media choices in status */
449 case 'u': /* restrict scan to "up" interfaces */
457 ip6lifetime++; /* print IPv6 address lifetime */
469 /* -C must be solitary */
470 if (all || supmedia || uponly || downonly || namesonly ||
478 /* -l cannot be used with -a or -m */
479 if (namesonly && (all || supmedia))
483 if (uponly && downonly)
486 /* no arguments is equivalent to '-a' */
487 if (!namesonly && argc < 1)
490 /* -a and -l allow an address family arg to limit the output */
491 if (all || namesonly) {
496 for (afp = afs; afp->af_name; afp++)
497 if (strcmp(afp->af_name, *argv) == 0) {
501 if (afp->af_name == NULL)
503 /* leave with afp non-zero */
506 /* not listing, need an argument */
510 strncpy(name, *argv, sizeof(name));
513 /* check and maybe load support for this interface */
517 * NOTE: We must special-case the `create' command right
518 * here as we would otherwise fail when trying to find
521 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
522 strcmp(argv[0], "plumb") == 0)) {
530 /* Check for address family */
532 for (afp = afs; afp->af_name; afp++)
533 if (strcmp(afp->af_name, *argv) == 0) {
537 if (afp->af_name == NULL)
538 afp = NULL; /* not a family, NULL */
544 mib[3] = 0; /* address family */
545 mib[4] = NET_RT_IFLIST;
548 /* if particular family specified, only ask about it */
552 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
553 errx(1, "iflist-sysctl-estimate");
554 if ((buf = malloc(needed)) == NULL)
556 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
557 errx(1, "actual retrieval of interface table");
563 ifm = (struct if_msghdr *)next;
565 if (ifm->ifm_type == RTM_IFINFO) {
566 sdl = (struct sockaddr_dl *)(ifm + 1);
567 flags = ifm->ifm_flags;
569 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
570 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
572 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
573 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
578 next += ifm->ifm_msglen;
583 nextifm = (struct if_msghdr *)next;
585 if (nextifm->ifm_type != RTM_NEWADDR)
589 ifam = (struct ifa_msghdr *)nextifm;
592 next += nextifm->ifm_msglen;
595 if (all || namesonly) {
597 if ((flags & IFF_UP) == 0)
598 continue; /* not up */
601 continue; /* not down */
602 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
603 name[sdl->sdl_nlen] = '\0';
606 afp->af_status != link_status ||
607 sdl->sdl_type == IFT_ETHER) {
616 if (strlen(name) != sdl->sdl_nlen)
617 continue; /* not same len */
618 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
619 continue; /* not same name */
623 ifconfig(argc, argv, afp);
625 status(afp, addrcount, sdl, ifm, ifam);
627 if (all == 0 && namesonly == 0) {
628 foundit++; /* flag it as 'done' */
634 if (namesonly && need_nl > 0)
637 if (all == 0 && namesonly == 0 && foundit == 0)
638 errx(1, "interface %s does not exist", name);
646 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
652 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
653 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
655 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
659 register const struct cmd *p;
661 for (p = cmds; p->c_name; p++)
662 if (strcmp(*argv, p->c_name) == 0)
664 if (p->c_name == 0 && setaddr)
665 p++; /* got src, do dst */
666 if (p->c_func || p->c_func2) {
667 if (p->c_parameter == NEXTARG) {
669 errx(1, "'%s' requires argument",
671 (*p->c_func)(argv[1], 0, s, afp);
673 } else if (p->c_parameter == NEXTARG2) {
675 errx(1, "'%s' requires 2 arguments",
677 (*p->c_func2)(argv[1], argv[2], s, afp);
678 argc -= 2, argv += 2;
680 (*p->c_func)(*argv, p->c_parameter, s, afp);
685 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
686 /* Aggregatable address architecture defines all prefixes
687 are 64. So, it is convenient to set prefixlen to 64 if
688 it is not specified. */
689 setifprefixlen("64", 0, s, afp);
690 /* in6_getprefix("64", MASK) if MASK is available here... */
694 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
696 int size = sizeof(rq);
698 rq.rq_ipx = addreq.ifra_addr;
699 rq.rq_ip = addreq.ifra_dstaddr;
701 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
702 Perror("Encapsulation Routing");
705 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
706 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
708 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
710 int size = sizeof(rq);
712 rq.rq_ns = addreq.ifra_addr;
713 rq.rq_ip = addreq.ifra_dstaddr;
715 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
716 Perror("Encapsulation Routing");
720 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
721 warnx("interface %s cannot change %s addresses!",
728 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
729 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
730 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
731 /* means no previous address for interface */
733 Perror("ioctl (SIOCDIFADDR)");
737 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
738 warnx("interface %s cannot change %s addresses!",
743 if (newaddr && (setaddr || setmask)) {
744 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
745 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
746 Perror("ioctl (SIOCAIFADDR)");
758 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
760 if (*afp->af_getaddr == NULL)
763 * Delay the ioctl to set the interface addr until flags are all set.
764 * The address interpretation may depend on the flags,
765 * and the flags may change when the address is set.
768 if (doalias == 0 && afp->af_af != AF_LINK)
770 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
774 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
776 struct addrinfo hints, *srcres, *dstres;
777 struct ifaliasreq addreq;
780 struct in6_aliasreq in6_addreq;
783 memset(&hints, 0, sizeof(hints));
784 hints.ai_family = afp->af_af;
786 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
787 errx(1, "error in parsing address string: %s",
788 gai_strerror(ecode));
790 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
791 errx(1, "error in parsing address string: %s",
792 gai_strerror(ecode));
794 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
796 "source and destination address families do not match");
798 switch (srcres->ai_addr->sa_family) {
800 memset(&addreq, 0, sizeof(addreq));
801 strncpy(addreq.ifra_name, name, IFNAMSIZ);
802 memcpy(&addreq.ifra_addr, srcres->ai_addr,
803 srcres->ai_addr->sa_len);
804 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
805 dstres->ai_addr->sa_len);
807 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
808 warn("SIOCSIFPHYADDR");
813 memset(&in6_addreq, 0, sizeof(in6_addreq));
814 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
815 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
816 srcres->ai_addr->sa_len);
817 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
818 dstres->ai_addr->sa_len);
820 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
821 warn("SIOCSIFPHYADDR_IN6");
826 warn("address family not supported");
829 freeaddrinfo(srcres);
830 freeaddrinfo(dstres);
835 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
838 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
839 err(1, "SIOCDIFPHYADDR");
843 setifnetmask(const char *addr, int dummy __unused, int s,
844 const struct afswtch *afp)
846 if (*afp->af_getaddr == NULL)
849 (*afp->af_getaddr)(addr, MASK);
854 setifprefixlen(const char *addr, int dummy __unused, int s,
855 const struct afswtch *afp)
857 if (*afp->af_getprefix)
858 (*afp->af_getprefix)(addr, MASK);
863 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
864 const struct afswtch *afp)
866 if (afp->af_af != AF_INET6)
867 err(1, "address flags can be set only for inet6 addresses");
870 in6_addreq.ifra_flags &= ~(-flag);
872 in6_addreq.ifra_flags |= flag;
876 setip6pltime(const char *seconds, int dummy __unused, int s,
877 const struct afswtch *afp)
879 setip6lifetime("pltime", seconds, s, afp);
883 setip6vltime(const char *seconds, int dummy __unused, int s,
884 const struct afswtch *afp)
886 setip6lifetime("vltime", seconds, s, afp);
890 setip6lifetime(const char *cmd, const char *val, int s,
891 const struct afswtch *afp)
897 newval = (time_t)strtoul(val, &ep, 0);
899 errx(1, "invalid %s", cmd);
900 if (afp->af_af != AF_INET6)
901 errx(1, "%s not allowed for the AF", cmd);
902 if (strcmp(cmd, "vltime") == 0) {
903 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
904 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
905 } else if (strcmp(cmd, "pltime") == 0) {
906 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
907 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
912 setip6eui64(const char *cmd, int dummy __unused, int s,
913 const struct afswtch *afp)
915 struct ifaddrs *ifap, *ifa;
916 const struct sockaddr_in6 *sin6 = NULL;
917 const struct in6_addr *lladdr = NULL;
918 struct in6_addr *in6;
920 if (afp->af_af != AF_INET6)
921 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
922 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
923 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
924 errx(EXIT_FAILURE, "interface index is already filled");
925 if (getifaddrs(&ifap) != 0)
926 err(EXIT_FAILURE, "getifaddrs");
927 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
928 if (ifa->ifa_addr->sa_family == AF_INET6 &&
929 strcmp(ifa->ifa_name, name) == 0) {
930 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
931 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
932 lladdr = &sin6->sin6_addr;
938 errx(EXIT_FAILURE, "could not determine link local address");
940 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
947 setifbroadaddr(const char *addr, int dummy __unused, int s,
948 const struct afswtch *afp)
950 if (*afp->af_getaddr == NULL)
952 (*afp->af_getaddr)(addr, DSTADDR);
956 setifipdst(const char *addr, int dummy __unused, int s,
957 const struct afswtch *afp)
959 in_getaddr(addr, DSTADDR);
964 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
967 notealias(const char *addr, int param, int s, const struct afswtch *afp)
969 if (setaddr && doalias == 0 && param < 0)
970 bcopy((caddr_t)rqtosa(af_addreq),
971 (caddr_t)rqtosa(af_ridreq),
972 rqtosa(af_addreq)->sa_len);
983 setifdstaddr(const char *addr, int param __unused, int s,
984 const struct afswtch *afp)
986 if (*afp->af_getaddr == NULL)
988 (*afp->af_getaddr)(addr, DSTADDR);
992 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
993 * of the ifreq structure, which may confuse other parts of ifconfig.
994 * Make a private copy so we can avoid that.
997 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1001 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1003 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1004 Perror("ioctl (SIOCGIFFLAGS)");
1007 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1008 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1015 my_ifr.ifr_flags = flags & 0xffff;
1016 my_ifr.ifr_flagshigh = flags >> 16;
1017 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1022 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1025 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1026 Perror("ioctl (SIOCGIFCAP)");
1029 flags = ifr.ifr_curcap;
1035 ifr.ifr_reqcap = flags;
1036 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1041 setifmetric(const char *val, int dummy __unused, int s,
1042 const struct afswtch *afp)
1044 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1045 ifr.ifr_metric = atoi(val);
1046 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1047 warn("ioctl (set metric)");
1051 setifmtu(const char *val, int dummy __unused, int s, const struct afswtch *afp)
1053 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1054 ifr.ifr_mtu = atoi(val);
1055 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1056 warn("ioctl (set mtu)");
1060 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1061 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1065 "\003\1rxcsum\2txcsum\3netcons"
1068 * Print the status of the interface. If an address family was
1069 * specified, show it and it only; otherwise, show them all.
1072 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1073 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1075 const struct afswtch *p = NULL;
1076 struct rt_addrinfo info;
1086 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1087 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1089 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1092 printf("%s: ", name);
1093 printb("flags", flags, IFFBITS);
1094 if (ifm->ifm_data.ifi_metric)
1095 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1096 if (ifm->ifm_data.ifi_mtu)
1097 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1100 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1101 if (ifr.ifr_curcap != 0) {
1102 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1105 if (supmedia && ifr.ifr_reqcap != 0) {
1106 printf("\tcapability list:\n");
1107 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1114 while (addrcount > 0) {
1116 info.rti_addrs = ifam->ifam_addrs;
1118 /* Expand the compacted addresses */
1119 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1123 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1125 (*p->af_status)(s, &info);
1127 } else for (p = afs; p->af_name; p++) {
1128 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1129 (*p->af_status)(s, &info);
1132 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1134 if (allfamilies || afp->af_status == link_status)
1135 link_status(s, (struct rt_addrinfo *)sdl);
1137 if (allfamilies || afp->af_status == media_status)
1138 media_status(s, NULL);
1141 if (allfamilies || afp->af_status == vlan_status)
1142 vlan_status(s, NULL);
1144 #ifdef USE_IEEE80211
1145 if (allfamilies || afp->af_status == ieee80211_status)
1146 ieee80211_status(s, NULL);
1148 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1149 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1150 printf("%s", ifs.ascii);
1152 if (!allfamilies && !p && afp->af_status != media_status &&
1153 afp->af_status != link_status
1155 && afp->af_status != vlan_status
1158 warnx("%s has no %s interface address!", name, afp->af_name);
1165 tunnel_status(int s)
1167 char psrcaddr[NI_MAXHOST];
1168 char pdstaddr[NI_MAXHOST];
1169 u_long srccmd, dstcmd;
1171 const char *ver = "";
1172 #ifdef NI_WITHSCOPEID
1173 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1175 const int niflag = NI_NUMERICHOST;
1178 struct in6_ifreq in6_ifr;
1182 psrcaddr[0] = pdstaddr[0] = '\0';
1185 memset(&in6_ifr, 0, sizeof(in6_ifr));
1186 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1187 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1189 srccmd = SIOCGIFPSRCADDR;
1190 dstcmd = SIOCGIFPDSTADDR;
1194 srccmd = SIOCGIFPSRCADDR_IN6;
1195 dstcmd = SIOCGIFPDSTADDR_IN6;
1196 ifrp = (struct ifreq *)&in6_ifr;
1199 srccmd = SIOCGIFPSRCADDR;
1200 dstcmd = SIOCGIFPDSTADDR;
1204 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1207 if (ifrp->ifr_addr.sa_family == AF_INET6)
1208 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1210 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1211 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1213 if (ifrp->ifr_addr.sa_family == AF_INET6)
1217 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1220 if (ifrp->ifr_addr.sa_family == AF_INET6)
1221 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1223 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1224 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1226 printf("\ttunnel inet%s %s --> %s\n", ver,
1227 psrcaddr, pdstaddr);
1231 in_status(int s __unused, struct rt_addrinfo *info)
1233 struct sockaddr_in *sin, null_sin;
1235 memset(&null_sin, 0, sizeof(null_sin));
1237 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1238 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1240 if (flags & IFF_POINTOPOINT) {
1241 /* note RTAX_BRD overlap with IFF_BROADCAST */
1242 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1245 printf("--> %s ", inet_ntoa(sin->sin_addr));
1248 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1251 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1253 if (flags & IFF_BROADCAST) {
1254 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1255 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1256 if (sin && sin->sin_addr.s_addr != 0)
1257 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1264 in6_fillscopeid(struct sockaddr_in6 *sin6)
1266 #if defined(__KAME__) && defined(KAME_SCOPEID)
1267 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1268 sin6->sin6_scope_id =
1269 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1270 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1276 in6_status(int s __unused, struct rt_addrinfo *info)
1278 struct sockaddr_in6 *sin, null_sin;
1279 struct in6_ifreq ifr6;
1282 struct in6_addrlifetime lifetime;
1283 time_t t = time(NULL);
1287 memset(&null_sin, 0, sizeof(null_sin));
1289 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1290 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1291 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1292 perror("ifconfig: socket");
1295 ifr6.ifr_addr = *sin;
1296 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1297 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1301 flags6 = ifr6.ifr_ifru.ifru_flags6;
1302 memset(&lifetime, 0, sizeof(lifetime));
1303 ifr6.ifr_addr = *sin;
1304 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1305 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1309 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1312 /* XXX: embedded link local addr check */
1313 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1314 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1317 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1318 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1319 if (sin->sin6_scope_id == 0)
1320 sin->sin6_scope_id = ntohs(index);
1322 scopeid = sin->sin6_scope_id;
1324 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1325 sizeof(addr_buf), NULL, 0,
1326 NI_NUMERICHOST|NI_WITHSCOPEID);
1328 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1330 printf("\tinet6 %s ", addr_buf);
1332 if (flags & IFF_POINTOPOINT) {
1333 /* note RTAX_BRD overlap with IFF_BROADCAST */
1334 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1336 * some of the interfaces do not have valid destination
1339 if (sin && sin->sin6_family == AF_INET6) {
1342 /* XXX: embedded link local addr check */
1343 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1344 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1347 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1348 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1349 if (sin->sin6_scope_id == 0)
1350 sin->sin6_scope_id = ntohs(index);
1353 error = getnameinfo((struct sockaddr *)sin,
1354 sin->sin6_len, addr_buf,
1355 sizeof(addr_buf), NULL, 0,
1356 NI_NUMERICHOST|NI_WITHSCOPEID);
1358 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1360 printf("--> %s ", addr_buf);
1364 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1367 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1368 sizeof(struct in6_addr)));
1370 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1372 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1373 printf("tentative ");
1374 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1375 printf("duplicated ");
1376 if ((flags6 & IN6_IFF_DETACHED) != 0)
1377 printf("detached ");
1378 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1379 printf("deprecated ");
1380 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1381 printf("autoconf ");
1382 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1383 printf("temporary ");
1386 printf("scopeid 0x%x ", scopeid);
1388 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1390 if (lifetime.ia6t_preferred) {
1391 printf("%s ", lifetime.ia6t_preferred < t
1392 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1397 if (lifetime.ia6t_expire) {
1398 printf("%s ", lifetime.ia6t_expire < t
1399 ? "0" : sec2str(lifetime.ia6t_expire - t));
1410 ipx_status(int s __unused, struct rt_addrinfo *info)
1412 struct sockaddr_ipx *sipx, null_sipx;
1414 memset(&null_sipx, 0, sizeof(null_sipx));
1416 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1417 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1419 if (flags & IFF_POINTOPOINT) {
1420 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1423 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1430 at_status(int s __unused, struct rt_addrinfo *info)
1432 struct sockaddr_at *sat, null_sat;
1433 struct netrange *nr;
1435 memset(&null_sat, 0, sizeof(null_sat));
1437 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1438 nr = &sat->sat_range.r_netrange;
1439 printf("\tatalk %d.%d range %d-%d phase %d",
1440 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1441 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1442 if (flags & IFF_POINTOPOINT) {
1443 /* note RTAX_BRD overlap with IFF_BROADCAST */
1444 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1448 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1450 if (flags & IFF_BROADCAST) {
1451 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1452 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1454 printf(" broadcast %d.%d",
1455 ntohs(sat->sat_addr.s_net),
1456 sat->sat_addr.s_node);
1464 xns_status(int s __unused, struct rt_addrinfo *info)
1466 struct sockaddr_ns *sns, null_sns;
1468 memset(&null_sns, 0, sizeof(null_sns));
1470 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1471 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1473 if (flags & IFF_POINTOPOINT) {
1474 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1477 printf("--> %s ", ns_ntoa(sns->sns_addr));
1487 link_status(int s __unused, struct rt_addrinfo *info)
1489 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1491 if (sdl->sdl_alen > 0) {
1492 if (sdl->sdl_type == IFT_ETHER &&
1493 sdl->sdl_alen == ETHER_ADDR_LEN)
1494 printf("\tether %s\n",
1495 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1497 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1499 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1505 Perror(const char *cmd)
1510 errx(1, "%s: no such interface", cmd);
1514 errx(1, "%s: permission denied", cmd);
1522 #define SIN(x) ((struct sockaddr_in *) &(x))
1523 struct sockaddr_in *sintab[] = {
1524 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1525 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1528 in_getaddr(const char *s, int which)
1530 register struct sockaddr_in *sin = sintab[which];
1534 sin->sin_len = sizeof(*sin);
1536 sin->sin_family = AF_INET;
1538 if (which == ADDR) {
1541 if((p = strrchr(s, '/')) != NULL) {
1542 /* address is `name/masklen' */
1545 struct sockaddr_in *min = sintab[MASK];
1547 ret = sscanf(p+1, "%u", &masklen);
1548 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1550 errx(1, "%s: bad value", s);
1552 min->sin_len = sizeof(*min);
1553 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1558 if (inet_aton(s, &sin->sin_addr))
1560 if ((hp = gethostbyname(s)) != 0)
1561 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1562 MIN(hp->h_length, sizeof(sin->sin_addr)));
1563 else if ((np = getnetbyname(s)) != 0)
1564 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1566 errx(1, "%s: bad value", s);
1570 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1571 struct sockaddr_in6 *sin6tab[] = {
1572 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1573 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1576 in6_getaddr(const char *s, int which)
1578 register struct sockaddr_in6 *sin = sin6tab[which];
1579 struct addrinfo hints, *res;
1584 sin->sin6_len = sizeof(*sin);
1586 sin->sin6_family = AF_INET6;
1588 if (which == ADDR) {
1590 if((p = strrchr(s, '/')) != NULL) {
1592 in6_getprefix(p + 1, MASK);
1593 explicit_prefix = 1;
1597 if (sin->sin6_family == AF_INET6) {
1598 bzero(&hints, sizeof(struct addrinfo));
1599 hints.ai_family = AF_INET6;
1600 error = getaddrinfo(s, NULL, &hints, &res);
1603 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1604 errx(1, "%s: bad value", s);
1606 bcopy(res->ai_addr, sin, res->ai_addrlen);
1610 in6_getprefix(const char *plen, int which)
1612 register struct sockaddr_in6 *sin = sin6tab[which];
1613 register u_char *cp;
1614 int len = atoi(plen);
1616 if ((len < 0) || (len > 128))
1617 errx(1, "%s: bad value", plen);
1618 sin->sin6_len = sizeof(*sin);
1620 sin->sin6_family = AF_INET6;
1621 if ((len == 0) || (len == 128)) {
1622 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1625 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1626 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1628 *cp = 0xff << (8 - len);
1633 * Print a value a la the %b format of the kernel's printf
1636 printb(const char *s, register unsigned v, register const char *bits)
1638 register int i, any = 0;
1641 if (bits && *bits == 8)
1642 printf("%s=%o", s, v);
1644 printf("%s=%x", s, v);
1648 while ((i = *bits++) != '\0') {
1649 if (v & (1 << (i-1))) {
1653 for (; (c = *bits) > 32; bits++)
1656 for (; *bits > 32; bits++)
1664 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1665 struct sockaddr_ipx *sipxtab[] = {
1666 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1667 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1670 ipx_getaddr(const char *addr, int which)
1672 struct sockaddr_ipx *sipx = sipxtab[which];
1674 sipx->sipx_family = AF_IPX;
1675 sipx->sipx_len = sizeof(*sipx);
1676 sipx->sipx_addr = ipx_addr(addr);
1678 printf("Attempt to set IPX netmask will be ineffectual\n");
1683 at_getaddr(const char *addr, int which)
1685 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1688 sat->sat_family = AF_APPLETALK;
1689 sat->sat_len = sizeof(*sat);
1691 errx(1, "AppleTalk does not use netmasks");
1692 if (sscanf(addr, "%u.%u", &net, &node) != 2
1693 || net > 0xffff || node > 0xfe)
1694 errx(1, "%s: illegal address", addr);
1695 sat->sat_addr.s_net = htons(net);
1696 sat->sat_addr.s_node = node;
1700 link_getaddr(const char *addr, int which)
1703 struct sockaddr_dl sdl;
1704 struct sockaddr *sa = &ridreq.ifr_addr;
1707 errx(1, "can't set link-level netmask or broadcast");
1708 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1709 errx(1, "malloc failed");
1711 strcpy(temp + 1, addr);
1712 sdl.sdl_len = sizeof(sdl);
1713 link_addr(temp, &sdl);
1715 if (sdl.sdl_alen > sizeof(sa->sa_data))
1716 errx(1, "malformed link-level address");
1717 sa->sa_family = AF_LINK;
1718 sa->sa_len = sdl.sdl_alen;
1719 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1722 /* XXX FIXME -- should use strtoul for better parsing. */
1724 setatrange(const char *range, int dummy __unused, int s,
1725 const struct afswtch *afp)
1727 u_short first = 123, last = 123;
1729 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1730 || first == 0 || first > 0xffff
1731 || last == 0 || last > 0xffff || first > last)
1732 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1733 at_nr.nr_firstnet = htons(first);
1734 at_nr.nr_lastnet = htons(last);
1738 setatphase(const char *phase, int dummy __unused, int s,
1739 const struct afswtch *afp)
1741 if (!strcmp(phase, "1"))
1743 else if (!strcmp(phase, "2"))
1746 errx(1, "%s: illegal phase", phase);
1750 checkatrange(struct sockaddr_at *sat)
1752 if (at_nr.nr_phase == 0)
1753 at_nr.nr_phase = 2; /* Default phase 2 */
1754 if (at_nr.nr_firstnet == 0)
1755 at_nr.nr_firstnet = /* Default range of one */
1756 at_nr.nr_lastnet = sat->sat_addr.s_net;
1757 printf("\tatalk %d.%d range %d-%d phase %d\n",
1758 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1759 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1760 if ((u_short) ntohs(at_nr.nr_firstnet) >
1761 (u_short) ntohs(sat->sat_addr.s_net)
1762 || (u_short) ntohs(at_nr.nr_lastnet) <
1763 (u_short) ntohs(sat->sat_addr.s_net))
1764 errx(1, "AppleTalk address is not in range");
1765 sat->sat_range.r_netrange = at_nr;
1769 #define SNS(x) ((struct sockaddr_ns *) &(x))
1770 struct sockaddr_ns *snstab[] = {
1771 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1772 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1775 xns_getaddr(const char *addr, int which)
1777 struct sockaddr_ns *sns = snstab[which];
1779 sns->sns_family = AF_NS;
1780 sns->sns_len = sizeof(*sns);
1781 sns->sns_addr = ns_addr(addr);
1783 printf("Attempt to set XNS netmask will be ineffectual\n");
1789 prefix(void *val, int size)
1791 register u_char *name = (u_char *)val;
1792 register int byte, bit, plen = 0;
1794 for (byte = 0; byte < size; byte++, plen += 8)
1795 if (name[byte] != 0xff)
1799 for (bit = 7; bit != 0; bit--, plen++)
1800 if (!(name[byte] & (1 << bit)))
1802 for (; bit != 0; bit--)
1803 if (name[byte] & (1 << bit))
1806 for (; byte < size; byte++)
1813 sec2str(time_t total)
1815 static char result[256];
1816 int days, hours, mins, secs;
1821 days = total / 3600 / 24;
1822 hours = (total / 3600) % 24;
1823 mins = (total / 60) % 60;
1828 p += sprintf(p, "%dd", days);
1830 if (!first || hours) {
1832 p += sprintf(p, "%dh", hours);
1834 if (!first || mins) {
1836 p += sprintf(p, "%dm", mins);
1838 sprintf(p, "%ds", secs);
1840 sprintf(result, "%lu", (unsigned long)total);
1847 ifmaybeload(char *name)
1849 struct module_stat mstat;
1851 char ifkind[35], *cp, *dp;
1854 /* turn interface and unit into module name */
1855 strcpy(ifkind, "if_");
1856 for (cp = name, dp = ifkind + 3;
1857 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1861 /* scan files in kernel */
1862 mstat.version = sizeof(struct module_stat);
1863 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1864 /* scan modules in file */
1865 for (modid = kldfirstmod(fileid); modid > 0;
1866 modid = modfnext(modid)) {
1867 if (modstat(modid, &mstat) < 0)
1869 /* strip bus name if present */
1870 if ((cp = strchr(mstat.name, '/')) != NULL) {
1875 /* already loaded? */
1876 if (!strcmp(ifkind, cp))
1881 /* not present, we should try to load it */
1888 struct if_clonereq ifcr;
1893 s = socket(AF_INET, SOCK_DGRAM, 0);
1897 memset(&ifcr, 0, sizeof(ifcr));
1899 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1900 err(1, "SIOCIFGCLONERS for count");
1902 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1904 err(1, "unable to allocate cloner name buffer");
1906 ifcr.ifcr_count = ifcr.ifcr_total;
1907 ifcr.ifcr_buffer = buf;
1909 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1910 err(1, "SIOCIFGCLONERS for names");
1913 * In case some disappeared in the mean time, clamp it down.
1915 if (ifcr.ifcr_count > ifcr.ifcr_total)
1916 ifcr.ifcr_count = ifcr.ifcr_total;
1918 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1933 s = socket(AF_INET, SOCK_DGRAM, 0);
1937 memset(&ifr, 0, sizeof(ifr));
1938 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1939 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
1940 err(1, "SIOCIFCREATE");
1942 if (strcmp(name, ifr.ifr_name) != 0) {
1943 printf("%s\n", ifr.ifr_name);
1944 strlcpy(name, ifr.ifr_name, sizeof(name));
1951 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
1954 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1955 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
1956 err(1, "SIOCIFDESTROY");