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.2 2003/06/17 04:27:33 dillon 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 __P((const char *cmd));
138 void checkatrange __P((struct sockaddr_at *));
139 int ifconfig __P((int argc, char *const *argv, const struct afswtch *afp));
140 void notealias __P((const char *, int, int, const struct afswtch *afp));
141 void list_cloners __P((void));
142 void printb __P((const char *s, unsigned value, const char *bits));
143 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
144 void status __P((const struct afswtch *afp, int addrcount,
145 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
146 struct ifa_msghdr *ifam));
147 void tunnel_status __P((int s));
148 void usage __P((void));
149 void ifmaybeload __P((char *name));
152 void in6_fillscopeid __P((struct sockaddr_in6 *sin6));
153 int prefix __P((void *, int));
154 static char *sec2str __P((time_t));
155 int explicit_prefix = 0;
158 typedef void c_func __P((const char *cmd, int arg, int s, const struct afswtch *afp));
159 typedef void c_func2 __P((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 __P((void));
180 #define NEXTARG 0xffffff
181 #define NEXTARG2 0xfffffe
186 int c_parameter; /* NEXTARG means next argv */
187 void (*c_func) __P((const char *, int, int, const struct afswtch *afp));
188 void (*c_func2) __P((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 __P((int, struct rt_addrinfo *));
290 typedef void af_getaddr __P((const char *, int));
291 typedef void af_getprefix __P((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(cp, cplim, rtinfo)
374 struct rt_addrinfo *rtinfo;
379 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
380 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
381 if ((rtinfo->rti_addrs & (1 << i)) == 0)
383 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
393 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
394 "usage: ifconfig interface address_family [address [dest_address]]",
397 " ifconfig interface create",
398 " ifconfig -a [-d] [-m] [-u] [address_family]",
399 " ifconfig -l [-d] [-u] [address_family]",
400 " ifconfig [-d] [-m] [-u]");
402 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
403 "usage: ifconfig [-L] interface address_family [address [dest_address]]",
406 " ifconfig interface create",
407 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
408 " ifconfig -l [-d] [-u] [address_family]",
409 " ifconfig [-L] [-d] [-m] [-u]");
420 int all, namesonly, downonly, uponly;
421 int foundit = 0, need_nl = 0;
422 const struct afswtch *afp = 0;
424 struct if_msghdr *ifm, *nextifm;
425 struct ifa_msghdr *ifam;
426 struct sockaddr_dl *sdl;
427 char *buf, *lim, *next;
433 /* Parse leading line options */
434 all = downonly = uponly = namesonly = 0;
435 while ((c = getopt(argc, argv, "adlmuC"
441 case 'a': /* scan all interfaces */
444 case 'd': /* restrict scan to "down" interfaces */
447 case 'l': /* scan interface names only */
450 case 'm': /* show media choices in status */
453 case 'u': /* restrict scan to "up" interfaces */
461 ip6lifetime++; /* print IPv6 address lifetime */
473 /* -C must be solitary */
474 if (all || supmedia || uponly || downonly || namesonly ||
482 /* -l cannot be used with -a or -m */
483 if (namesonly && (all || supmedia))
487 if (uponly && downonly)
490 /* no arguments is equivalent to '-a' */
491 if (!namesonly && argc < 1)
494 /* -a and -l allow an address family arg to limit the output */
495 if (all || namesonly) {
500 for (afp = afs; afp->af_name; afp++)
501 if (strcmp(afp->af_name, *argv) == 0) {
505 if (afp->af_name == NULL)
507 /* leave with afp non-zero */
510 /* not listing, need an argument */
514 strncpy(name, *argv, sizeof(name));
517 /* check and maybe load support for this interface */
521 * NOTE: We must special-case the `create' command right
522 * here as we would otherwise fail when trying to find
525 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
526 strcmp(argv[0], "plumb") == 0)) {
534 /* Check for address family */
536 for (afp = afs; afp->af_name; afp++)
537 if (strcmp(afp->af_name, *argv) == 0) {
541 if (afp->af_name == NULL)
542 afp = NULL; /* not a family, NULL */
548 mib[3] = 0; /* address family */
549 mib[4] = NET_RT_IFLIST;
552 /* if particular family specified, only ask about it */
556 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
557 errx(1, "iflist-sysctl-estimate");
558 if ((buf = malloc(needed)) == NULL)
560 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
561 errx(1, "actual retrieval of interface table");
567 ifm = (struct if_msghdr *)next;
569 if (ifm->ifm_type == RTM_IFINFO) {
570 sdl = (struct sockaddr_dl *)(ifm + 1);
571 flags = ifm->ifm_flags;
573 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
574 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
576 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
577 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
582 next += ifm->ifm_msglen;
587 nextifm = (struct if_msghdr *)next;
589 if (nextifm->ifm_type != RTM_NEWADDR)
593 ifam = (struct ifa_msghdr *)nextifm;
596 next += nextifm->ifm_msglen;
599 if (all || namesonly) {
601 if ((flags & IFF_UP) == 0)
602 continue; /* not up */
605 continue; /* not down */
606 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
607 name[sdl->sdl_nlen] = '\0';
610 afp->af_status != link_status ||
611 sdl->sdl_type == IFT_ETHER) {
620 if (strlen(name) != sdl->sdl_nlen)
621 continue; /* not same len */
622 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
623 continue; /* not same name */
627 ifconfig(argc, argv, afp);
629 status(afp, addrcount, sdl, ifm, ifam);
631 if (all == 0 && namesonly == 0) {
632 foundit++; /* flag it as 'done' */
638 if (namesonly && need_nl > 0)
641 if (all == 0 && namesonly == 0 && foundit == 0)
642 errx(1, "interface %s does not exist", name);
650 ifconfig(argc, argv, afp)
653 const struct afswtch *afp;
659 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
660 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
662 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
666 register const struct cmd *p;
668 for (p = cmds; p->c_name; p++)
669 if (strcmp(*argv, p->c_name) == 0)
671 if (p->c_name == 0 && setaddr)
672 p++; /* got src, do dst */
673 if (p->c_func || p->c_func2) {
674 if (p->c_parameter == NEXTARG) {
676 errx(1, "'%s' requires argument",
678 (*p->c_func)(argv[1], 0, s, afp);
680 } else if (p->c_parameter == NEXTARG2) {
682 errx(1, "'%s' requires 2 arguments",
684 (*p->c_func2)(argv[1], argv[2], s, afp);
685 argc -= 2, argv += 2;
687 (*p->c_func)(*argv, p->c_parameter, s, afp);
692 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
693 /* Aggregatable address architecture defines all prefixes
694 are 64. So, it is convenient to set prefixlen to 64 if
695 it is not specified. */
696 setifprefixlen("64", 0, s, afp);
697 /* in6_getprefix("64", MASK) if MASK is available here... */
701 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
703 int size = sizeof(rq);
705 rq.rq_ipx = addreq.ifra_addr;
706 rq.rq_ip = addreq.ifra_dstaddr;
708 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
709 Perror("Encapsulation Routing");
712 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
713 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
715 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
717 int size = sizeof(rq);
719 rq.rq_ns = addreq.ifra_addr;
720 rq.rq_ip = addreq.ifra_dstaddr;
722 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
723 Perror("Encapsulation Routing");
727 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
728 warnx("interface %s cannot change %s addresses!",
735 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
736 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
737 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
738 /* means no previous address for interface */
740 Perror("ioctl (SIOCDIFADDR)");
744 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
745 warnx("interface %s cannot change %s addresses!",
750 if (newaddr && (setaddr || setmask)) {
751 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
752 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
753 Perror("ioctl (SIOCAIFADDR)");
765 setifaddr(addr, param, s, afp)
769 const struct afswtch *afp;
771 if (*afp->af_getaddr == NULL)
774 * Delay the ioctl to set the interface addr until flags are all set.
775 * The address interpretation may depend on the flags,
776 * and the flags may change when the address is set.
779 if (doalias == 0 && afp->af_af != AF_LINK)
781 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
785 settunnel(src, dst, s, afp)
786 const char *src, *dst;
788 const struct afswtch *afp;
790 struct addrinfo hints, *srcres, *dstres;
791 struct ifaliasreq addreq;
794 struct in6_aliasreq in6_addreq;
797 memset(&hints, 0, sizeof(hints));
798 hints.ai_family = afp->af_af;
800 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
801 errx(1, "error in parsing address string: %s",
802 gai_strerror(ecode));
804 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
805 errx(1, "error in parsing address string: %s",
806 gai_strerror(ecode));
808 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
810 "source and destination address families do not match");
812 switch (srcres->ai_addr->sa_family) {
814 memset(&addreq, 0, sizeof(addreq));
815 strncpy(addreq.ifra_name, name, IFNAMSIZ);
816 memcpy(&addreq.ifra_addr, srcres->ai_addr,
817 srcres->ai_addr->sa_len);
818 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
819 dstres->ai_addr->sa_len);
821 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
822 warn("SIOCSIFPHYADDR");
827 memset(&in6_addreq, 0, sizeof(in6_addreq));
828 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
829 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
830 srcres->ai_addr->sa_len);
831 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
832 dstres->ai_addr->sa_len);
834 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
835 warn("SIOCSIFPHYADDR_IN6");
840 warn("address family not supported");
843 freeaddrinfo(srcres);
844 freeaddrinfo(dstres);
849 deletetunnel(vname, param, s, afp)
853 const struct afswtch *afp;
856 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
857 err(1, "SIOCDIFPHYADDR");
861 setifnetmask(addr, dummy, s, afp)
865 const struct afswtch *afp;
867 if (*afp->af_getaddr == NULL)
870 (*afp->af_getaddr)(addr, MASK);
875 setifprefixlen(addr, dummy, s, afp)
879 const struct afswtch *afp;
881 if (*afp->af_getprefix)
882 (*afp->af_getprefix)(addr, MASK);
887 setip6flags(dummyaddr, flag, dummysoc, afp)
888 const char *dummyaddr __unused;
890 int dummysoc __unused;
891 const struct afswtch *afp;
893 if (afp->af_af != AF_INET6)
894 err(1, "address flags can be set only for inet6 addresses");
897 in6_addreq.ifra_flags &= ~(-flag);
899 in6_addreq.ifra_flags |= flag;
903 setip6pltime(seconds, dummy, s, afp)
907 const struct afswtch *afp;
909 setip6lifetime("pltime", seconds, s, afp);
913 setip6vltime(seconds, dummy, s, afp)
917 const struct afswtch *afp;
919 setip6lifetime("vltime", seconds, s, afp);
923 setip6lifetime(cmd, val, s, afp)
927 const struct afswtch *afp;
933 newval = (time_t)strtoul(val, &ep, 0);
935 errx(1, "invalid %s", cmd);
936 if (afp->af_af != AF_INET6)
937 errx(1, "%s not allowed for the AF", cmd);
938 if (strcmp(cmd, "vltime") == 0) {
939 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
940 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
941 } else if (strcmp(cmd, "pltime") == 0) {
942 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
943 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
948 setip6eui64(cmd, dummy, s, afp)
952 const struct afswtch *afp;
954 struct ifaddrs *ifap, *ifa;
955 const struct sockaddr_in6 *sin6 = NULL;
956 const struct in6_addr *lladdr = NULL;
957 struct in6_addr *in6;
959 if (afp->af_af != AF_INET6)
960 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
961 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
962 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
963 errx(EXIT_FAILURE, "interface index is already filled");
964 if (getifaddrs(&ifap) != 0)
965 err(EXIT_FAILURE, "getifaddrs");
966 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
967 if (ifa->ifa_addr->sa_family == AF_INET6 &&
968 strcmp(ifa->ifa_name, name) == 0) {
969 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
970 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
971 lladdr = &sin6->sin6_addr;
977 errx(EXIT_FAILURE, "could not determine link local address");
979 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
986 setifbroadaddr(addr, dummy, s, afp)
990 const struct afswtch *afp;
992 if (*afp->af_getaddr == NULL)
994 (*afp->af_getaddr)(addr, DSTADDR);
998 setifipdst(addr, dummy, s, afp)
1002 const struct afswtch *afp;
1004 in_getaddr(addr, DSTADDR);
1009 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1012 notealias(addr, param, s, afp)
1016 const struct afswtch *afp;
1018 if (setaddr && doalias == 0 && param < 0)
1019 bcopy((caddr_t)rqtosa(af_addreq),
1020 (caddr_t)rqtosa(af_ridreq),
1021 rqtosa(af_addreq)->sa_len);
1032 setifdstaddr(addr, param, s, afp)
1036 const struct afswtch *afp;
1038 if (*afp->af_getaddr == NULL)
1040 (*afp->af_getaddr)(addr, DSTADDR);
1044 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1045 * of the ifreq structure, which may confuse other parts of ifconfig.
1046 * Make a private copy so we can avoid that.
1049 setifflags(vname, value, s, afp)
1053 const struct afswtch *afp;
1055 struct ifreq my_ifr;
1057 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1059 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1060 Perror("ioctl (SIOCGIFFLAGS)");
1063 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1064 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1071 my_ifr.ifr_flags = flags & 0xffff;
1072 my_ifr.ifr_flagshigh = flags >> 16;
1073 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1078 setifcap(vname, value, s, afp)
1082 const struct afswtch *afp;
1085 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1086 Perror("ioctl (SIOCGIFCAP)");
1089 flags = ifr.ifr_curcap;
1095 ifr.ifr_reqcap = flags;
1096 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1101 setifmetric(val, dummy, s, afp)
1105 const struct afswtch *afp;
1107 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1108 ifr.ifr_metric = atoi(val);
1109 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1110 warn("ioctl (set metric)");
1114 setifmtu(val, dummy, s, afp)
1118 const struct afswtch *afp;
1120 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1121 ifr.ifr_mtu = atoi(val);
1122 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1123 warn("ioctl (set mtu)");
1127 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1128 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1132 "\003\1rxcsum\2txcsum\3netcons"
1135 * Print the status of the interface. If an address family was
1136 * specified, show it and it only; otherwise, show them all.
1139 status(afp, addrcount, sdl, ifm, ifam)
1140 const struct afswtch *afp;
1142 struct sockaddr_dl *sdl;
1143 struct if_msghdr *ifm;
1144 struct ifa_msghdr *ifam;
1146 const struct afswtch *p = NULL;
1147 struct rt_addrinfo info;
1157 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1158 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1160 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1163 printf("%s: ", name);
1164 printb("flags", flags, IFFBITS);
1165 if (ifm->ifm_data.ifi_metric)
1166 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1167 if (ifm->ifm_data.ifi_mtu)
1168 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1171 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1172 if (ifr.ifr_curcap != 0) {
1173 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1176 if (supmedia && ifr.ifr_reqcap != 0) {
1177 printf("\tcapability list:\n");
1178 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1185 while (addrcount > 0) {
1187 info.rti_addrs = ifam->ifam_addrs;
1189 /* Expand the compacted addresses */
1190 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1194 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1196 (*p->af_status)(s, &info);
1198 } else for (p = afs; p->af_name; p++) {
1199 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1200 (*p->af_status)(s, &info);
1203 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1205 if (allfamilies || afp->af_status == link_status)
1206 link_status(s, (struct rt_addrinfo *)sdl);
1208 if (allfamilies || afp->af_status == media_status)
1209 media_status(s, NULL);
1212 if (allfamilies || afp->af_status == vlan_status)
1213 vlan_status(s, NULL);
1215 #ifdef USE_IEEE80211
1216 if (allfamilies || afp->af_status == ieee80211_status)
1217 ieee80211_status(s, NULL);
1219 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1220 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1221 printf("%s", ifs.ascii);
1223 if (!allfamilies && !p && afp->af_status != media_status &&
1224 afp->af_status != link_status
1226 && afp->af_status != vlan_status
1229 warnx("%s has no %s interface address!", name, afp->af_name);
1239 char psrcaddr[NI_MAXHOST];
1240 char pdstaddr[NI_MAXHOST];
1241 u_long srccmd, dstcmd;
1243 const char *ver = "";
1244 #ifdef NI_WITHSCOPEID
1245 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1247 const int niflag = NI_NUMERICHOST;
1250 struct in6_ifreq in6_ifr;
1254 psrcaddr[0] = pdstaddr[0] = '\0';
1257 memset(&in6_ifr, 0, sizeof(in6_ifr));
1258 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1259 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1261 srccmd = SIOCGIFPSRCADDR;
1262 dstcmd = SIOCGIFPDSTADDR;
1266 srccmd = SIOCGIFPSRCADDR_IN6;
1267 dstcmd = SIOCGIFPDSTADDR_IN6;
1268 ifrp = (struct ifreq *)&in6_ifr;
1271 srccmd = SIOCGIFPSRCADDR;
1272 dstcmd = SIOCGIFPDSTADDR;
1276 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1279 if (ifrp->ifr_addr.sa_family == AF_INET6)
1280 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1282 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1283 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1285 if (ifrp->ifr_addr.sa_family == AF_INET6)
1289 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1292 if (ifrp->ifr_addr.sa_family == AF_INET6)
1293 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1295 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1296 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1298 printf("\ttunnel inet%s %s --> %s\n", ver,
1299 psrcaddr, pdstaddr);
1305 struct rt_addrinfo * info;
1307 struct sockaddr_in *sin, null_sin;
1309 memset(&null_sin, 0, sizeof(null_sin));
1311 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1312 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1314 if (flags & IFF_POINTOPOINT) {
1315 /* note RTAX_BRD overlap with IFF_BROADCAST */
1316 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1319 printf("--> %s ", inet_ntoa(sin->sin_addr));
1322 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1325 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1327 if (flags & IFF_BROADCAST) {
1328 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1329 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1330 if (sin && sin->sin_addr.s_addr != 0)
1331 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1338 in6_fillscopeid(sin6)
1339 struct sockaddr_in6 *sin6;
1341 #if defined(__KAME__) && defined(KAME_SCOPEID)
1342 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1343 sin6->sin6_scope_id =
1344 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1345 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1353 struct rt_addrinfo * info;
1355 struct sockaddr_in6 *sin, null_sin;
1356 struct in6_ifreq ifr6;
1359 struct in6_addrlifetime lifetime;
1360 time_t t = time(NULL);
1364 memset(&null_sin, 0, sizeof(null_sin));
1366 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1367 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1368 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1369 perror("ifconfig: socket");
1372 ifr6.ifr_addr = *sin;
1373 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1374 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1378 flags6 = ifr6.ifr_ifru.ifru_flags6;
1379 memset(&lifetime, 0, sizeof(lifetime));
1380 ifr6.ifr_addr = *sin;
1381 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1382 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1386 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1389 /* XXX: embedded link local addr check */
1390 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1391 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1394 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1395 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1396 if (sin->sin6_scope_id == 0)
1397 sin->sin6_scope_id = ntohs(index);
1399 scopeid = sin->sin6_scope_id;
1401 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1402 sizeof(addr_buf), NULL, 0,
1403 NI_NUMERICHOST|NI_WITHSCOPEID);
1405 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1407 printf("\tinet6 %s ", addr_buf);
1409 if (flags & IFF_POINTOPOINT) {
1410 /* note RTAX_BRD overlap with IFF_BROADCAST */
1411 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1413 * some of the interfaces do not have valid destination
1416 if (sin && sin->sin6_family == AF_INET6) {
1419 /* XXX: embedded link local addr check */
1420 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1421 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1424 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1425 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1426 if (sin->sin6_scope_id == 0)
1427 sin->sin6_scope_id = ntohs(index);
1430 error = getnameinfo((struct sockaddr *)sin,
1431 sin->sin6_len, addr_buf,
1432 sizeof(addr_buf), NULL, 0,
1433 NI_NUMERICHOST|NI_WITHSCOPEID);
1435 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1437 printf("--> %s ", addr_buf);
1441 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1444 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1445 sizeof(struct in6_addr)));
1447 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1449 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1450 printf("tentative ");
1451 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1452 printf("duplicated ");
1453 if ((flags6 & IN6_IFF_DETACHED) != 0)
1454 printf("detached ");
1455 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1456 printf("deprecated ");
1457 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1458 printf("autoconf ");
1459 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1460 printf("temporary ");
1463 printf("scopeid 0x%x ", scopeid);
1465 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1467 if (lifetime.ia6t_preferred) {
1468 printf("%s ", lifetime.ia6t_preferred < t
1469 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1474 if (lifetime.ia6t_expire) {
1475 printf("%s ", lifetime.ia6t_expire < t
1476 ? "0" : sec2str(lifetime.ia6t_expire - t));
1489 struct rt_addrinfo * info;
1491 struct sockaddr_ipx *sipx, null_sipx;
1493 memset(&null_sipx, 0, sizeof(null_sipx));
1495 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1496 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1498 if (flags & IFF_POINTOPOINT) {
1499 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1502 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1511 struct rt_addrinfo * info;
1513 struct sockaddr_at *sat, null_sat;
1514 struct netrange *nr;
1516 memset(&null_sat, 0, sizeof(null_sat));
1518 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1519 nr = &sat->sat_range.r_netrange;
1520 printf("\tatalk %d.%d range %d-%d phase %d",
1521 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1522 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1523 if (flags & IFF_POINTOPOINT) {
1524 /* note RTAX_BRD overlap with IFF_BROADCAST */
1525 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1529 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1531 if (flags & IFF_BROADCAST) {
1532 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1533 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1535 printf(" broadcast %d.%d",
1536 ntohs(sat->sat_addr.s_net),
1537 sat->sat_addr.s_node);
1547 struct rt_addrinfo * info;
1549 struct sockaddr_ns *sns, null_sns;
1551 memset(&null_sns, 0, sizeof(null_sns));
1553 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1554 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1556 if (flags & IFF_POINTOPOINT) {
1557 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1560 printf("--> %s ", ns_ntoa(sns->sns_addr));
1570 link_status(s, info)
1572 struct rt_addrinfo *info;
1574 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1576 if (sdl->sdl_alen > 0) {
1577 if (sdl->sdl_type == IFT_ETHER &&
1578 sdl->sdl_alen == ETHER_ADDR_LEN)
1579 printf("\tether %s\n",
1580 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1582 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1584 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1596 errx(1, "%s: no such interface", cmd);
1600 errx(1, "%s: permission denied", cmd);
1608 #define SIN(x) ((struct sockaddr_in *) &(x))
1609 struct sockaddr_in *sintab[] = {
1610 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1611 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1614 in_getaddr(s, which)
1618 register struct sockaddr_in *sin = sintab[which];
1622 sin->sin_len = sizeof(*sin);
1624 sin->sin_family = AF_INET;
1626 if (which == ADDR) {
1629 if((p = strrchr(s, '/')) != NULL) {
1630 /* address is `name/masklen' */
1633 struct sockaddr_in *min = sintab[MASK];
1635 ret = sscanf(p+1, "%u", &masklen);
1636 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1638 errx(1, "%s: bad value", s);
1640 min->sin_len = sizeof(*min);
1641 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1646 if (inet_aton(s, &sin->sin_addr))
1648 if ((hp = gethostbyname(s)) != 0)
1649 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1650 MIN(hp->h_length, sizeof(sin->sin_addr)));
1651 else if ((np = getnetbyname(s)) != 0)
1652 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1654 errx(1, "%s: bad value", s);
1658 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1659 struct sockaddr_in6 *sin6tab[] = {
1660 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1661 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1664 in6_getaddr(s, which)
1668 register struct sockaddr_in6 *sin = sin6tab[which];
1669 struct addrinfo hints, *res;
1674 sin->sin6_len = sizeof(*sin);
1676 sin->sin6_family = AF_INET6;
1678 if (which == ADDR) {
1680 if((p = strrchr(s, '/')) != NULL) {
1682 in6_getprefix(p + 1, MASK);
1683 explicit_prefix = 1;
1687 if (sin->sin6_family == AF_INET6) {
1688 bzero(&hints, sizeof(struct addrinfo));
1689 hints.ai_family = AF_INET6;
1690 error = getaddrinfo(s, NULL, &hints, &res);
1693 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1694 errx(1, "%s: bad value", s);
1696 bcopy(res->ai_addr, sin, res->ai_addrlen);
1700 in6_getprefix(plen, which)
1704 register struct sockaddr_in6 *sin = sin6tab[which];
1705 register u_char *cp;
1706 int len = atoi(plen);
1708 if ((len < 0) || (len > 128))
1709 errx(1, "%s: bad value", plen);
1710 sin->sin6_len = sizeof(*sin);
1712 sin->sin6_family = AF_INET6;
1713 if ((len == 0) || (len == 128)) {
1714 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1717 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1718 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1720 *cp = 0xff << (8 - len);
1725 * Print a value a la the %b format of the kernel's printf
1730 register unsigned v;
1731 register const char *bits;
1733 register int i, any = 0;
1736 if (bits && *bits == 8)
1737 printf("%s=%o", s, v);
1739 printf("%s=%x", s, v);
1743 while ((i = *bits++) != '\0') {
1744 if (v & (1 << (i-1))) {
1748 for (; (c = *bits) > 32; bits++)
1751 for (; *bits > 32; bits++)
1759 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1760 struct sockaddr_ipx *sipxtab[] = {
1761 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1762 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1765 ipx_getaddr(addr, which)
1769 struct sockaddr_ipx *sipx = sipxtab[which];
1771 sipx->sipx_family = AF_IPX;
1772 sipx->sipx_len = sizeof(*sipx);
1773 sipx->sipx_addr = ipx_addr(addr);
1775 printf("Attempt to set IPX netmask will be ineffectual\n");
1780 at_getaddr(addr, which)
1784 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1787 sat->sat_family = AF_APPLETALK;
1788 sat->sat_len = sizeof(*sat);
1790 errx(1, "AppleTalk does not use netmasks");
1791 if (sscanf(addr, "%u.%u", &net, &node) != 2
1792 || net > 0xffff || node > 0xfe)
1793 errx(1, "%s: illegal address", addr);
1794 sat->sat_addr.s_net = htons(net);
1795 sat->sat_addr.s_node = node;
1799 link_getaddr(addr, which)
1804 struct sockaddr_dl sdl;
1805 struct sockaddr *sa = &ridreq.ifr_addr;
1808 errx(1, "can't set link-level netmask or broadcast");
1809 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1810 errx(1, "malloc failed");
1812 strcpy(temp + 1, addr);
1813 sdl.sdl_len = sizeof(sdl);
1814 link_addr(temp, &sdl);
1816 if (sdl.sdl_alen > sizeof(sa->sa_data))
1817 errx(1, "malformed link-level address");
1818 sa->sa_family = AF_LINK;
1819 sa->sa_len = sdl.sdl_alen;
1820 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1823 /* XXX FIXME -- should use strtoul for better parsing. */
1825 setatrange(range, dummy, s, afp)
1829 const struct afswtch *afp;
1831 u_short first = 123, last = 123;
1833 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1834 || first == 0 || first > 0xffff
1835 || last == 0 || last > 0xffff || first > last)
1836 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1837 at_nr.nr_firstnet = htons(first);
1838 at_nr.nr_lastnet = htons(last);
1842 setatphase(phase, dummy, s, afp)
1846 const struct afswtch *afp;
1848 if (!strcmp(phase, "1"))
1850 else if (!strcmp(phase, "2"))
1853 errx(1, "%s: illegal phase", phase);
1857 checkatrange(struct sockaddr_at *sat)
1859 if (at_nr.nr_phase == 0)
1860 at_nr.nr_phase = 2; /* Default phase 2 */
1861 if (at_nr.nr_firstnet == 0)
1862 at_nr.nr_firstnet = /* Default range of one */
1863 at_nr.nr_lastnet = sat->sat_addr.s_net;
1864 printf("\tatalk %d.%d range %d-%d phase %d\n",
1865 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1866 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1867 if ((u_short) ntohs(at_nr.nr_firstnet) >
1868 (u_short) ntohs(sat->sat_addr.s_net)
1869 || (u_short) ntohs(at_nr.nr_lastnet) <
1870 (u_short) ntohs(sat->sat_addr.s_net))
1871 errx(1, "AppleTalk address is not in range");
1872 sat->sat_range.r_netrange = at_nr;
1876 #define SNS(x) ((struct sockaddr_ns *) &(x))
1877 struct sockaddr_ns *snstab[] = {
1878 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1879 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1882 xns_getaddr(addr, which)
1886 struct sockaddr_ns *sns = snstab[which];
1888 sns->sns_family = AF_NS;
1889 sns->sns_len = sizeof(*sns);
1890 sns->sns_addr = ns_addr(addr);
1892 printf("Attempt to set XNS netmask will be ineffectual\n");
1902 register u_char *name = (u_char *)val;
1903 register int byte, bit, plen = 0;
1905 for (byte = 0; byte < size; byte++, plen += 8)
1906 if (name[byte] != 0xff)
1910 for (bit = 7; bit != 0; bit--, plen++)
1911 if (!(name[byte] & (1 << bit)))
1913 for (; bit != 0; bit--)
1914 if (name[byte] & (1 << bit))
1917 for (; byte < size; byte++)
1927 static char result[256];
1928 int days, hours, mins, secs;
1933 days = total / 3600 / 24;
1934 hours = (total / 3600) % 24;
1935 mins = (total / 60) % 60;
1940 p += sprintf(p, "%dd", days);
1942 if (!first || hours) {
1944 p += sprintf(p, "%dh", hours);
1946 if (!first || mins) {
1948 p += sprintf(p, "%dm", mins);
1950 sprintf(p, "%ds", secs);
1952 sprintf(result, "%lu", (unsigned long)total);
1962 struct module_stat mstat;
1964 char ifkind[35], *cp, *dp;
1967 /* turn interface and unit into module name */
1968 strcpy(ifkind, "if_");
1969 for (cp = name, dp = ifkind + 3;
1970 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1974 /* scan files in kernel */
1975 mstat.version = sizeof(struct module_stat);
1976 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1977 /* scan modules in file */
1978 for (modid = kldfirstmod(fileid); modid > 0;
1979 modid = modfnext(modid)) {
1980 if (modstat(modid, &mstat) < 0)
1982 /* strip bus name if present */
1983 if ((cp = strchr(mstat.name, '/')) != NULL) {
1988 /* already loaded? */
1989 if (!strcmp(ifkind, cp))
1994 /* not present, we should try to load it */
2001 struct if_clonereq ifcr;
2006 s = socket(AF_INET, SOCK_DGRAM, 0);
2010 memset(&ifcr, 0, sizeof(ifcr));
2012 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
2013 err(1, "SIOCIFGCLONERS for count");
2015 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
2017 err(1, "unable to allocate cloner name buffer");
2019 ifcr.ifcr_count = ifcr.ifcr_total;
2020 ifcr.ifcr_buffer = buf;
2022 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
2023 err(1, "SIOCIFGCLONERS for names");
2026 * In case some disappeared in the mean time, clamp it down.
2028 if (ifcr.ifcr_count > ifcr.ifcr_total)
2029 ifcr.ifcr_count = ifcr.ifcr_total;
2031 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
2046 s = socket(AF_INET, SOCK_DGRAM, 0);
2050 memset(&ifr, 0, sizeof(ifr));
2051 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2052 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
2053 err(1, "SIOCIFCREATE");
2055 if (strcmp(name, ifr.ifr_name) != 0) {
2056 printf("%s\n", ifr.ifr_name);
2057 strlcpy(name, ifr.ifr_name, sizeof(name));
2064 clone_destroy(val, d, s, rafp)
2068 const struct afswtch *rafp;
2071 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2072 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
2073 err(1, "SIOCIFDESTROY");