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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
44 static const char rcsid[] =
45 "$FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.51.2.19 2003/01/28 11:02:56 fjoe Exp $";
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
53 #include <sys/module.h>
54 #include <sys/linker.h>
56 #include <net/ethernet.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <arpa/inet.h>
70 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
77 #include <netipx/ipx.h>
78 #include <netipx/ipx_if.h>
82 #include <netatalk/at.h>
88 #include <netns/ns_if.h>
103 #include "ifconfig.h"
105 /* wrapper for KAME-special getnameinfo() */
106 #ifndef NI_WITHSCOPEID
107 #define NI_WITHSCOPEID 0
110 struct ifreq ifr, ridreq;
111 struct ifaliasreq addreq;
113 struct in6_ifreq in6_ridreq;
114 struct in6_aliasreq in6_addreq =
120 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
122 struct sockaddr_in netmask;
123 struct netrange at_nr; /* AppleTalk net range */
134 static int ip6lifetime;
143 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
146 void Perror __P((const char *cmd));
147 void checkatrange __P((struct sockaddr_at *));
148 int ifconfig __P((int argc, char *const *argv, const struct afswtch *afp));
149 void notealias __P((const char *, int, int, const struct afswtch *afp));
150 void list_cloners __P((void));
151 void printb __P((const char *s, unsigned value, const char *bits));
152 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
153 void status __P((const struct afswtch *afp, int addrcount,
154 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
155 struct ifa_msghdr *ifam));
156 void tunnel_status __P((int s));
157 void usage __P((void));
158 void ifmaybeload __P((char *name));
161 void in6_fillscopeid __P((struct sockaddr_in6 *sin6));
162 int prefix __P((void *, int));
163 static char *sec2str __P((time_t));
164 int explicit_prefix = 0;
167 typedef void c_func __P((const char *cmd, int arg, int s, const struct afswtch *afp));
168 typedef void c_func2 __P((const char *arg, const char *arg2, int s, const struct afswtch *afp));
169 c_func setatphase, setatrange;
170 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
174 c_func setifprefixlen;
178 c_func2 setip6lifetime;
182 c_func setifflags, setifmetric, setifmtu, setifcap;
183 c_func clone_destroy;
186 void clone_create __P((void));
189 #define NEXTARG 0xffffff
190 #define NEXTARG2 0xfffffe
195 int c_parameter; /* NEXTARG means next argv */
196 void (*c_func) __P((const char *, int, int, const struct afswtch *afp));
197 void (*c_func2) __P((const char *, const char *, int, const struct afswtch *afp));
199 { "up", IFF_UP, setifflags } ,
200 { "down", -IFF_UP, setifflags },
201 { "arp", -IFF_NOARP, setifflags },
202 { "-arp", IFF_NOARP, setifflags },
203 { "debug", IFF_DEBUG, setifflags },
204 { "-debug", -IFF_DEBUG, setifflags },
205 { "promisc", IFF_PPROMISC, setifflags },
206 { "-promisc", -IFF_PPROMISC, setifflags },
207 { "add", IFF_UP, notealias },
208 { "alias", IFF_UP, notealias },
209 { "-alias", -IFF_UP, notealias },
210 { "delete", -IFF_UP, notealias },
211 { "remove", -IFF_UP, notealias },
213 #define EN_SWABIPS 0x1000
214 { "swabips", EN_SWABIPS, setifflags },
215 { "-swabips", -EN_SWABIPS, setifflags },
217 { "netmask", NEXTARG, setifnetmask },
219 { "prefixlen", NEXTARG, setifprefixlen },
220 { "anycast", IN6_IFF_ANYCAST, setip6flags },
221 { "tentative", IN6_IFF_TENTATIVE, setip6flags },
222 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags },
223 { "deprecated", IN6_IFF_DEPRECATED, setip6flags },
224 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags },
225 { "autoconf", IN6_IFF_AUTOCONF, setip6flags },
226 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags },
227 { "pltime", NEXTARG, setip6pltime },
228 { "vltime", NEXTARG, setip6vltime },
229 { "eui64", 0, setip6eui64 },
231 { "range", NEXTARG, setatrange },
232 { "phase", NEXTARG, setatphase },
233 { "metric", NEXTARG, setifmetric },
234 { "broadcast", NEXTARG, setifbroadaddr },
235 { "ipdst", NEXTARG, setifipdst },
236 { "tunnel", NEXTARG2, NULL, settunnel },
237 { "deletetunnel", 0, deletetunnel },
238 { "link0", IFF_LINK0, setifflags },
239 { "-link0", -IFF_LINK0, setifflags },
240 { "link1", IFF_LINK1, setifflags },
241 { "-link1", -IFF_LINK1, setifflags },
242 { "link2", IFF_LINK2, setifflags },
243 { "-link2", -IFF_LINK2, setifflags },
245 { "media", NEXTARG, setmedia },
246 { "mediaopt", NEXTARG, setmediaopt },
247 { "-mediaopt", NEXTARG, unsetmediaopt },
250 { "vlan", NEXTARG, setvlantag },
251 { "vlandev", NEXTARG, setvlandev },
252 { "-vlandev", NEXTARG, unsetvlandev },
255 /* XXX `create' special-cased below */
256 {"create", 0, clone_create },
257 {"plumb", 0, clone_create },
259 {"destroy", 0, clone_destroy },
260 {"unplumb", 0, clone_destroy },
262 { "ssid", NEXTARG, set80211ssid },
263 { "nwid", NEXTARG, set80211ssid },
264 { "stationname", NEXTARG, set80211stationname },
265 { "station", NEXTARG, set80211stationname }, /* BSD/OS */
266 { "channel", NEXTARG, set80211channel },
267 { "authmode", NEXTARG, set80211authmode },
268 { "powersavemode", NEXTARG, set80211powersavemode },
269 { "powersave", 1, set80211powersave },
270 { "-powersave", 0, set80211powersave },
271 { "powersavesleep", NEXTARG, set80211powersavesleep },
272 { "wepmode", NEXTARG, set80211wepmode },
273 { "wep", 1, set80211wep },
274 { "-wep", 0, set80211wep },
275 { "weptxkey", NEXTARG, set80211weptxkey },
276 { "wepkey", NEXTARG, set80211wepkey },
277 { "nwkey", NEXTARG, set80211nwkey }, /* NetBSD */
278 { "-nwkey", 0, set80211wep }, /* NetBSD */
280 { "rxcsum", IFCAP_RXCSUM, setifcap },
281 { "-rxcsum", -IFCAP_RXCSUM, setifcap },
282 { "txcsum", IFCAP_TXCSUM, setifcap },
283 { "-txcsum", -IFCAP_TXCSUM, setifcap },
284 { "netcons", IFCAP_NETCONS, setifcap },
285 { "-netcons", -IFCAP_NETCONS, setifcap },
286 { "normal", -IFF_LINK0, setifflags },
287 { "compress", IFF_LINK0, setifflags },
288 { "noicmp", IFF_LINK1, setifflags },
289 { "mtu", NEXTARG, setifmtu },
291 { 0, 0, setifdstaddr },
295 * XNS support liberally adapted from code written at the University of
296 * Maryland principally by James O'Toole and Chris Torek.
298 typedef void af_status __P((int, struct rt_addrinfo *));
299 typedef void af_getaddr __P((const char *, int));
300 typedef void af_getprefix __P((const char *, int));
302 af_status in_status, at_status, link_status;
303 af_getaddr in_getaddr, at_getaddr, link_getaddr;
306 af_status ipx_status;
307 af_getaddr ipx_getaddr;
311 af_status in6_status;
312 af_getaddr in6_getaddr;
313 af_getprefix in6_getprefix;
316 af_status xns_status;
317 af_getaddr xns_getaddr;
320 /* Known address families */
325 af_status *af_status;
326 af_getaddr *af_getaddr;
327 af_getprefix *af_getprefix;
333 #define C(x) ((caddr_t) &x)
334 { "inet", AF_INET, in_status, in_getaddr, NULL,
335 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
337 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
338 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
339 C(in6_ridreq), C(in6_addreq) },
342 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
343 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
345 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
346 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
348 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
349 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
351 { "link", AF_LINK, link_status, link_getaddr, NULL,
352 0, SIOCSIFLLADDR, NULL, C(ridreq) },
353 { "ether", AF_LINK, link_status, link_getaddr, NULL,
354 0, SIOCSIFLLADDR, NULL, C(ridreq) },
355 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
356 0, SIOCSIFLLADDR, NULL, C(ridreq) },
357 #if 0 /* XXX conflicts with the media command */
359 { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */
362 { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, }, /* XXX not real!! */
365 { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, }, /* XXX not real!! */
372 * Expand the compacted form of addresses as returned via the
373 * configuration read via sysctl().
377 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
378 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
381 rt_xaddrs(cp, cplim, rtinfo)
383 struct rt_addrinfo *rtinfo;
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)
392 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
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]]",
406 " ifconfig interface create",
407 " ifconfig -a [-d] [-m] [-u] [address_family]",
408 " ifconfig -l [-d] [-u] [address_family]",
409 " ifconfig [-d] [-m] [-u]");
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]]",
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]");
429 int all, namesonly, downonly, uponly;
430 int foundit = 0, need_nl = 0;
431 const struct afswtch *afp = 0;
433 struct if_msghdr *ifm, *nextifm;
434 struct ifa_msghdr *ifam;
435 struct sockaddr_dl *sdl;
436 char *buf, *lim, *next;
442 /* Parse leading line options */
443 all = downonly = uponly = namesonly = 0;
444 while ((c = getopt(argc, argv, "adlmuC"
450 case 'a': /* scan all interfaces */
453 case 'd': /* restrict scan to "down" interfaces */
456 case 'l': /* scan interface names only */
459 case 'm': /* show media choices in status */
462 case 'u': /* restrict scan to "up" interfaces */
470 ip6lifetime++; /* print IPv6 address lifetime */
482 /* -C must be solitary */
483 if (all || supmedia || uponly || downonly || namesonly ||
491 /* -l cannot be used with -a or -m */
492 if (namesonly && (all || supmedia))
496 if (uponly && downonly)
499 /* no arguments is equivalent to '-a' */
500 if (!namesonly && argc < 1)
503 /* -a and -l allow an address family arg to limit the output */
504 if (all || namesonly) {
509 for (afp = afs; afp->af_name; afp++)
510 if (strcmp(afp->af_name, *argv) == 0) {
514 if (afp->af_name == NULL)
516 /* leave with afp non-zero */
519 /* not listing, need an argument */
523 strncpy(name, *argv, sizeof(name));
526 /* check and maybe load support for this interface */
530 * NOTE: We must special-case the `create' command right
531 * here as we would otherwise fail when trying to find
534 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
535 strcmp(argv[0], "plumb") == 0)) {
543 /* Check for address family */
545 for (afp = afs; afp->af_name; afp++)
546 if (strcmp(afp->af_name, *argv) == 0) {
550 if (afp->af_name == NULL)
551 afp = NULL; /* not a family, NULL */
557 mib[3] = 0; /* address family */
558 mib[4] = NET_RT_IFLIST;
561 /* if particular family specified, only ask about it */
565 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
566 errx(1, "iflist-sysctl-estimate");
567 if ((buf = malloc(needed)) == NULL)
569 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
570 errx(1, "actual retrieval of interface table");
576 ifm = (struct if_msghdr *)next;
578 if (ifm->ifm_type == RTM_IFINFO) {
579 sdl = (struct sockaddr_dl *)(ifm + 1);
580 flags = ifm->ifm_flags;
582 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
583 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
585 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
586 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
591 next += ifm->ifm_msglen;
596 nextifm = (struct if_msghdr *)next;
598 if (nextifm->ifm_type != RTM_NEWADDR)
602 ifam = (struct ifa_msghdr *)nextifm;
605 next += nextifm->ifm_msglen;
608 if (all || namesonly) {
610 if ((flags & IFF_UP) == 0)
611 continue; /* not up */
614 continue; /* not down */
615 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
616 name[sdl->sdl_nlen] = '\0';
619 afp->af_status != link_status ||
620 sdl->sdl_type == IFT_ETHER) {
629 if (strlen(name) != sdl->sdl_nlen)
630 continue; /* not same len */
631 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
632 continue; /* not same name */
636 ifconfig(argc, argv, afp);
638 status(afp, addrcount, sdl, ifm, ifam);
640 if (all == 0 && namesonly == 0) {
641 foundit++; /* flag it as 'done' */
647 if (namesonly && need_nl > 0)
650 if (all == 0 && namesonly == 0 && foundit == 0)
651 errx(1, "interface %s does not exist", name);
659 ifconfig(argc, argv, afp)
662 const struct afswtch *afp;
668 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
669 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
671 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
675 register const struct cmd *p;
677 for (p = cmds; p->c_name; p++)
678 if (strcmp(*argv, p->c_name) == 0)
680 if (p->c_name == 0 && setaddr)
681 p++; /* got src, do dst */
682 if (p->c_func || p->c_func2) {
683 if (p->c_parameter == NEXTARG) {
685 errx(1, "'%s' requires argument",
687 (*p->c_func)(argv[1], 0, s, afp);
689 } else if (p->c_parameter == NEXTARG2) {
691 errx(1, "'%s' requires 2 arguments",
693 (*p->c_func2)(argv[1], argv[2], s, afp);
694 argc -= 2, argv += 2;
696 (*p->c_func)(*argv, p->c_parameter, s, afp);
701 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
702 /* Aggregatable address architecture defines all prefixes
703 are 64. So, it is convenient to set prefixlen to 64 if
704 it is not specified. */
705 setifprefixlen("64", 0, s, afp);
706 /* in6_getprefix("64", MASK) if MASK is available here... */
710 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
712 int size = sizeof(rq);
714 rq.rq_ipx = addreq.ifra_addr;
715 rq.rq_ip = addreq.ifra_dstaddr;
717 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
718 Perror("Encapsulation Routing");
721 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
722 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
724 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
726 int size = sizeof(rq);
728 rq.rq_ns = addreq.ifra_addr;
729 rq.rq_ip = addreq.ifra_dstaddr;
731 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
732 Perror("Encapsulation Routing");
736 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
737 warnx("interface %s cannot change %s addresses!",
744 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
745 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
746 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
747 /* means no previous address for interface */
749 Perror("ioctl (SIOCDIFADDR)");
753 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
754 warnx("interface %s cannot change %s addresses!",
759 if (newaddr && (setaddr || setmask)) {
760 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
761 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
762 Perror("ioctl (SIOCAIFADDR)");
774 setifaddr(addr, param, s, afp)
778 const struct afswtch *afp;
780 if (*afp->af_getaddr == NULL)
783 * Delay the ioctl to set the interface addr until flags are all set.
784 * The address interpretation may depend on the flags,
785 * and the flags may change when the address is set.
788 if (doalias == 0 && afp->af_af != AF_LINK)
790 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
794 settunnel(src, dst, s, afp)
795 const char *src, *dst;
797 const struct afswtch *afp;
799 struct addrinfo hints, *srcres, *dstres;
800 struct ifaliasreq addreq;
803 struct in6_aliasreq in6_addreq;
806 memset(&hints, 0, sizeof(hints));
807 hints.ai_family = afp->af_af;
809 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
810 errx(1, "error in parsing address string: %s",
811 gai_strerror(ecode));
813 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
814 errx(1, "error in parsing address string: %s",
815 gai_strerror(ecode));
817 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
819 "source and destination address families do not match");
821 switch (srcres->ai_addr->sa_family) {
823 memset(&addreq, 0, sizeof(addreq));
824 strncpy(addreq.ifra_name, name, IFNAMSIZ);
825 memcpy(&addreq.ifra_addr, srcres->ai_addr,
826 srcres->ai_addr->sa_len);
827 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
828 dstres->ai_addr->sa_len);
830 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
831 warn("SIOCSIFPHYADDR");
836 memset(&in6_addreq, 0, sizeof(in6_addreq));
837 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
838 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
839 srcres->ai_addr->sa_len);
840 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
841 dstres->ai_addr->sa_len);
843 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
844 warn("SIOCSIFPHYADDR_IN6");
849 warn("address family not supported");
852 freeaddrinfo(srcres);
853 freeaddrinfo(dstres);
858 deletetunnel(vname, param, s, afp)
862 const struct afswtch *afp;
865 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
866 err(1, "SIOCDIFPHYADDR");
870 setifnetmask(addr, dummy, s, afp)
874 const struct afswtch *afp;
876 if (*afp->af_getaddr == NULL)
879 (*afp->af_getaddr)(addr, MASK);
884 setifprefixlen(addr, dummy, s, afp)
888 const struct afswtch *afp;
890 if (*afp->af_getprefix)
891 (*afp->af_getprefix)(addr, MASK);
896 setip6flags(dummyaddr, flag, dummysoc, afp)
897 const char *dummyaddr __unused;
899 int dummysoc __unused;
900 const struct afswtch *afp;
902 if (afp->af_af != AF_INET6)
903 err(1, "address flags can be set only for inet6 addresses");
906 in6_addreq.ifra_flags &= ~(-flag);
908 in6_addreq.ifra_flags |= flag;
912 setip6pltime(seconds, dummy, s, afp)
916 const struct afswtch *afp;
918 setip6lifetime("pltime", seconds, s, afp);
922 setip6vltime(seconds, dummy, s, afp)
926 const struct afswtch *afp;
928 setip6lifetime("vltime", seconds, s, afp);
932 setip6lifetime(cmd, val, s, afp)
936 const struct afswtch *afp;
942 newval = (time_t)strtoul(val, &ep, 0);
944 errx(1, "invalid %s", cmd);
945 if (afp->af_af != AF_INET6)
946 errx(1, "%s not allowed for the AF", cmd);
947 if (strcmp(cmd, "vltime") == 0) {
948 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
949 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
950 } else if (strcmp(cmd, "pltime") == 0) {
951 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
952 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
957 setip6eui64(cmd, dummy, s, afp)
961 const struct afswtch *afp;
963 struct ifaddrs *ifap, *ifa;
964 const struct sockaddr_in6 *sin6 = NULL;
965 const struct in6_addr *lladdr = NULL;
966 struct in6_addr *in6;
968 if (afp->af_af != AF_INET6)
969 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
970 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
971 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
972 errx(EXIT_FAILURE, "interface index is already filled");
973 if (getifaddrs(&ifap) != 0)
974 err(EXIT_FAILURE, "getifaddrs");
975 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
976 if (ifa->ifa_addr->sa_family == AF_INET6 &&
977 strcmp(ifa->ifa_name, name) == 0) {
978 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
979 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
980 lladdr = &sin6->sin6_addr;
986 errx(EXIT_FAILURE, "could not determine link local address");
988 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
995 setifbroadaddr(addr, dummy, s, afp)
999 const struct afswtch *afp;
1001 if (*afp->af_getaddr == NULL)
1003 (*afp->af_getaddr)(addr, DSTADDR);
1007 setifipdst(addr, dummy, s, afp)
1011 const struct afswtch *afp;
1013 in_getaddr(addr, DSTADDR);
1018 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1021 notealias(addr, param, s, afp)
1025 const struct afswtch *afp;
1027 if (setaddr && doalias == 0 && param < 0)
1028 bcopy((caddr_t)rqtosa(af_addreq),
1029 (caddr_t)rqtosa(af_ridreq),
1030 rqtosa(af_addreq)->sa_len);
1041 setifdstaddr(addr, param, s, afp)
1045 const struct afswtch *afp;
1047 if (*afp->af_getaddr == NULL)
1049 (*afp->af_getaddr)(addr, DSTADDR);
1053 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1054 * of the ifreq structure, which may confuse other parts of ifconfig.
1055 * Make a private copy so we can avoid that.
1058 setifflags(vname, value, s, afp)
1062 const struct afswtch *afp;
1064 struct ifreq my_ifr;
1066 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1068 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1069 Perror("ioctl (SIOCGIFFLAGS)");
1072 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1073 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1080 my_ifr.ifr_flags = flags & 0xffff;
1081 my_ifr.ifr_flagshigh = flags >> 16;
1082 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1087 setifcap(vname, value, s, afp)
1091 const struct afswtch *afp;
1094 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1095 Perror("ioctl (SIOCGIFCAP)");
1098 flags = ifr.ifr_curcap;
1104 ifr.ifr_reqcap = flags;
1105 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1110 setifmetric(val, dummy, s, afp)
1114 const struct afswtch *afp;
1116 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1117 ifr.ifr_metric = atoi(val);
1118 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1119 warn("ioctl (set metric)");
1123 setifmtu(val, dummy, s, afp)
1127 const struct afswtch *afp;
1129 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1130 ifr.ifr_mtu = atoi(val);
1131 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1132 warn("ioctl (set mtu)");
1136 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1137 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1141 "\003\1rxcsum\2txcsum\3netcons"
1144 * Print the status of the interface. If an address family was
1145 * specified, show it and it only; otherwise, show them all.
1148 status(afp, addrcount, sdl, ifm, ifam)
1149 const struct afswtch *afp;
1151 struct sockaddr_dl *sdl;
1152 struct if_msghdr *ifm;
1153 struct ifa_msghdr *ifam;
1155 const struct afswtch *p = NULL;
1156 struct rt_addrinfo info;
1166 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1167 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1169 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1172 printf("%s: ", name);
1173 printb("flags", flags, IFFBITS);
1174 if (ifm->ifm_data.ifi_metric)
1175 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1176 if (ifm->ifm_data.ifi_mtu)
1177 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1180 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1181 if (ifr.ifr_curcap != 0) {
1182 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1185 if (supmedia && ifr.ifr_reqcap != 0) {
1186 printf("\tcapability list:\n");
1187 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1194 while (addrcount > 0) {
1196 info.rti_addrs = ifam->ifam_addrs;
1198 /* Expand the compacted addresses */
1199 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1203 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1205 (*p->af_status)(s, &info);
1207 } else for (p = afs; p->af_name; p++) {
1208 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1209 (*p->af_status)(s, &info);
1212 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1214 if (allfamilies || afp->af_status == link_status)
1215 link_status(s, (struct rt_addrinfo *)sdl);
1217 if (allfamilies || afp->af_status == media_status)
1218 media_status(s, NULL);
1221 if (allfamilies || afp->af_status == vlan_status)
1222 vlan_status(s, NULL);
1224 #ifdef USE_IEEE80211
1225 if (allfamilies || afp->af_status == ieee80211_status)
1226 ieee80211_status(s, NULL);
1228 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1229 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1230 printf("%s", ifs.ascii);
1232 if (!allfamilies && !p && afp->af_status != media_status &&
1233 afp->af_status != link_status
1235 && afp->af_status != vlan_status
1238 warnx("%s has no %s interface address!", name, afp->af_name);
1248 char psrcaddr[NI_MAXHOST];
1249 char pdstaddr[NI_MAXHOST];
1250 u_long srccmd, dstcmd;
1252 const char *ver = "";
1253 #ifdef NI_WITHSCOPEID
1254 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1256 const int niflag = NI_NUMERICHOST;
1259 struct in6_ifreq in6_ifr;
1263 psrcaddr[0] = pdstaddr[0] = '\0';
1266 memset(&in6_ifr, 0, sizeof(in6_ifr));
1267 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1268 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1270 srccmd = SIOCGIFPSRCADDR;
1271 dstcmd = SIOCGIFPDSTADDR;
1275 srccmd = SIOCGIFPSRCADDR_IN6;
1276 dstcmd = SIOCGIFPDSTADDR_IN6;
1277 ifrp = (struct ifreq *)&in6_ifr;
1280 srccmd = SIOCGIFPSRCADDR;
1281 dstcmd = SIOCGIFPDSTADDR;
1285 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1288 if (ifrp->ifr_addr.sa_family == AF_INET6)
1289 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1291 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1292 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1294 if (ifrp->ifr_addr.sa_family == AF_INET6)
1298 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1301 if (ifrp->ifr_addr.sa_family == AF_INET6)
1302 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1304 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1305 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1307 printf("\ttunnel inet%s %s --> %s\n", ver,
1308 psrcaddr, pdstaddr);
1314 struct rt_addrinfo * info;
1316 struct sockaddr_in *sin, null_sin;
1318 memset(&null_sin, 0, sizeof(null_sin));
1320 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1321 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1323 if (flags & IFF_POINTOPOINT) {
1324 /* note RTAX_BRD overlap with IFF_BROADCAST */
1325 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1328 printf("--> %s ", inet_ntoa(sin->sin_addr));
1331 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1334 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1336 if (flags & IFF_BROADCAST) {
1337 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1338 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1339 if (sin && sin->sin_addr.s_addr != 0)
1340 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1347 in6_fillscopeid(sin6)
1348 struct sockaddr_in6 *sin6;
1350 #if defined(__KAME__) && defined(KAME_SCOPEID)
1351 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1352 sin6->sin6_scope_id =
1353 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1354 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1362 struct rt_addrinfo * info;
1364 struct sockaddr_in6 *sin, null_sin;
1365 struct in6_ifreq ifr6;
1368 struct in6_addrlifetime lifetime;
1369 time_t t = time(NULL);
1373 memset(&null_sin, 0, sizeof(null_sin));
1375 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1376 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1377 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1378 perror("ifconfig: socket");
1381 ifr6.ifr_addr = *sin;
1382 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1383 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1387 flags6 = ifr6.ifr_ifru.ifru_flags6;
1388 memset(&lifetime, 0, sizeof(lifetime));
1389 ifr6.ifr_addr = *sin;
1390 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1391 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1395 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1398 /* XXX: embedded link local addr check */
1399 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1400 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1403 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1404 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1405 if (sin->sin6_scope_id == 0)
1406 sin->sin6_scope_id = ntohs(index);
1408 scopeid = sin->sin6_scope_id;
1410 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1411 sizeof(addr_buf), NULL, 0,
1412 NI_NUMERICHOST|NI_WITHSCOPEID);
1414 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1416 printf("\tinet6 %s ", addr_buf);
1418 if (flags & IFF_POINTOPOINT) {
1419 /* note RTAX_BRD overlap with IFF_BROADCAST */
1420 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1422 * some of the interfaces do not have valid destination
1425 if (sin && sin->sin6_family == AF_INET6) {
1428 /* XXX: embedded link local addr check */
1429 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1430 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1433 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1434 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1435 if (sin->sin6_scope_id == 0)
1436 sin->sin6_scope_id = ntohs(index);
1439 error = getnameinfo((struct sockaddr *)sin,
1440 sin->sin6_len, addr_buf,
1441 sizeof(addr_buf), NULL, 0,
1442 NI_NUMERICHOST|NI_WITHSCOPEID);
1444 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1446 printf("--> %s ", addr_buf);
1450 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1453 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1454 sizeof(struct in6_addr)));
1456 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1458 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1459 printf("tentative ");
1460 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1461 printf("duplicated ");
1462 if ((flags6 & IN6_IFF_DETACHED) != 0)
1463 printf("detached ");
1464 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1465 printf("deprecated ");
1466 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1467 printf("autoconf ");
1468 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1469 printf("temporary ");
1472 printf("scopeid 0x%x ", scopeid);
1474 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1476 if (lifetime.ia6t_preferred) {
1477 printf("%s ", lifetime.ia6t_preferred < t
1478 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1483 if (lifetime.ia6t_expire) {
1484 printf("%s ", lifetime.ia6t_expire < t
1485 ? "0" : sec2str(lifetime.ia6t_expire - t));
1498 struct rt_addrinfo * info;
1500 struct sockaddr_ipx *sipx, null_sipx;
1502 memset(&null_sipx, 0, sizeof(null_sipx));
1504 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1505 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1507 if (flags & IFF_POINTOPOINT) {
1508 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1511 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1520 struct rt_addrinfo * info;
1522 struct sockaddr_at *sat, null_sat;
1523 struct netrange *nr;
1525 memset(&null_sat, 0, sizeof(null_sat));
1527 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1528 nr = &sat->sat_range.r_netrange;
1529 printf("\tatalk %d.%d range %d-%d phase %d",
1530 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1531 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1532 if (flags & IFF_POINTOPOINT) {
1533 /* note RTAX_BRD overlap with IFF_BROADCAST */
1534 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1538 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1540 if (flags & IFF_BROADCAST) {
1541 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1542 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1544 printf(" broadcast %d.%d",
1545 ntohs(sat->sat_addr.s_net),
1546 sat->sat_addr.s_node);
1556 struct rt_addrinfo * info;
1558 struct sockaddr_ns *sns, null_sns;
1560 memset(&null_sns, 0, sizeof(null_sns));
1562 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1563 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1565 if (flags & IFF_POINTOPOINT) {
1566 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1569 printf("--> %s ", ns_ntoa(sns->sns_addr));
1579 link_status(s, info)
1581 struct rt_addrinfo *info;
1583 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1585 if (sdl->sdl_alen > 0) {
1586 if (sdl->sdl_type == IFT_ETHER &&
1587 sdl->sdl_alen == ETHER_ADDR_LEN)
1588 printf("\tether %s\n",
1589 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1591 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1593 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1605 errx(1, "%s: no such interface", cmd);
1609 errx(1, "%s: permission denied", cmd);
1617 #define SIN(x) ((struct sockaddr_in *) &(x))
1618 struct sockaddr_in *sintab[] = {
1619 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1620 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1623 in_getaddr(s, which)
1627 register struct sockaddr_in *sin = sintab[which];
1631 sin->sin_len = sizeof(*sin);
1633 sin->sin_family = AF_INET;
1635 if (which == ADDR) {
1638 if((p = strrchr(s, '/')) != NULL) {
1639 /* address is `name/masklen' */
1642 struct sockaddr_in *min = sintab[MASK];
1644 ret = sscanf(p+1, "%u", &masklen);
1645 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1647 errx(1, "%s: bad value", s);
1649 min->sin_len = sizeof(*min);
1650 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1655 if (inet_aton(s, &sin->sin_addr))
1657 if ((hp = gethostbyname(s)) != 0)
1658 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1659 MIN(hp->h_length, sizeof(sin->sin_addr)));
1660 else if ((np = getnetbyname(s)) != 0)
1661 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1663 errx(1, "%s: bad value", s);
1667 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1668 struct sockaddr_in6 *sin6tab[] = {
1669 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1670 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1673 in6_getaddr(s, which)
1677 register struct sockaddr_in6 *sin = sin6tab[which];
1678 struct addrinfo hints, *res;
1683 sin->sin6_len = sizeof(*sin);
1685 sin->sin6_family = AF_INET6;
1687 if (which == ADDR) {
1689 if((p = strrchr(s, '/')) != NULL) {
1691 in6_getprefix(p + 1, MASK);
1692 explicit_prefix = 1;
1696 if (sin->sin6_family == AF_INET6) {
1697 bzero(&hints, sizeof(struct addrinfo));
1698 hints.ai_family = AF_INET6;
1699 error = getaddrinfo(s, NULL, &hints, &res);
1702 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1703 errx(1, "%s: bad value", s);
1705 bcopy(res->ai_addr, sin, res->ai_addrlen);
1709 in6_getprefix(plen, which)
1713 register struct sockaddr_in6 *sin = sin6tab[which];
1714 register u_char *cp;
1715 int len = atoi(plen);
1717 if ((len < 0) || (len > 128))
1718 errx(1, "%s: bad value", plen);
1719 sin->sin6_len = sizeof(*sin);
1721 sin->sin6_family = AF_INET6;
1722 if ((len == 0) || (len == 128)) {
1723 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1726 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1727 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1729 *cp = 0xff << (8 - len);
1734 * Print a value a la the %b format of the kernel's printf
1739 register unsigned v;
1740 register const char *bits;
1742 register int i, any = 0;
1745 if (bits && *bits == 8)
1746 printf("%s=%o", s, v);
1748 printf("%s=%x", s, v);
1752 while ((i = *bits++) != '\0') {
1753 if (v & (1 << (i-1))) {
1757 for (; (c = *bits) > 32; bits++)
1760 for (; *bits > 32; bits++)
1768 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1769 struct sockaddr_ipx *sipxtab[] = {
1770 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1771 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1774 ipx_getaddr(addr, which)
1778 struct sockaddr_ipx *sipx = sipxtab[which];
1780 sipx->sipx_family = AF_IPX;
1781 sipx->sipx_len = sizeof(*sipx);
1782 sipx->sipx_addr = ipx_addr(addr);
1784 printf("Attempt to set IPX netmask will be ineffectual\n");
1789 at_getaddr(addr, which)
1793 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1796 sat->sat_family = AF_APPLETALK;
1797 sat->sat_len = sizeof(*sat);
1799 errx(1, "AppleTalk does not use netmasks");
1800 if (sscanf(addr, "%u.%u", &net, &node) != 2
1801 || net > 0xffff || node > 0xfe)
1802 errx(1, "%s: illegal address", addr);
1803 sat->sat_addr.s_net = htons(net);
1804 sat->sat_addr.s_node = node;
1808 link_getaddr(addr, which)
1813 struct sockaddr_dl sdl;
1814 struct sockaddr *sa = &ridreq.ifr_addr;
1817 errx(1, "can't set link-level netmask or broadcast");
1818 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1819 errx(1, "malloc failed");
1821 strcpy(temp + 1, addr);
1822 sdl.sdl_len = sizeof(sdl);
1823 link_addr(temp, &sdl);
1825 if (sdl.sdl_alen > sizeof(sa->sa_data))
1826 errx(1, "malformed link-level address");
1827 sa->sa_family = AF_LINK;
1828 sa->sa_len = sdl.sdl_alen;
1829 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1832 /* XXX FIXME -- should use strtoul for better parsing. */
1834 setatrange(range, dummy, s, afp)
1838 const struct afswtch *afp;
1840 u_short first = 123, last = 123;
1842 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1843 || first == 0 || first > 0xffff
1844 || last == 0 || last > 0xffff || first > last)
1845 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1846 at_nr.nr_firstnet = htons(first);
1847 at_nr.nr_lastnet = htons(last);
1851 setatphase(phase, dummy, s, afp)
1855 const struct afswtch *afp;
1857 if (!strcmp(phase, "1"))
1859 else if (!strcmp(phase, "2"))
1862 errx(1, "%s: illegal phase", phase);
1866 checkatrange(struct sockaddr_at *sat)
1868 if (at_nr.nr_phase == 0)
1869 at_nr.nr_phase = 2; /* Default phase 2 */
1870 if (at_nr.nr_firstnet == 0)
1871 at_nr.nr_firstnet = /* Default range of one */
1872 at_nr.nr_lastnet = sat->sat_addr.s_net;
1873 printf("\tatalk %d.%d range %d-%d phase %d\n",
1874 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1875 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1876 if ((u_short) ntohs(at_nr.nr_firstnet) >
1877 (u_short) ntohs(sat->sat_addr.s_net)
1878 || (u_short) ntohs(at_nr.nr_lastnet) <
1879 (u_short) ntohs(sat->sat_addr.s_net))
1880 errx(1, "AppleTalk address is not in range");
1881 sat->sat_range.r_netrange = at_nr;
1885 #define SNS(x) ((struct sockaddr_ns *) &(x))
1886 struct sockaddr_ns *snstab[] = {
1887 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1888 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1891 xns_getaddr(addr, which)
1895 struct sockaddr_ns *sns = snstab[which];
1897 sns->sns_family = AF_NS;
1898 sns->sns_len = sizeof(*sns);
1899 sns->sns_addr = ns_addr(addr);
1901 printf("Attempt to set XNS netmask will be ineffectual\n");
1911 register u_char *name = (u_char *)val;
1912 register int byte, bit, plen = 0;
1914 for (byte = 0; byte < size; byte++, plen += 8)
1915 if (name[byte] != 0xff)
1919 for (bit = 7; bit != 0; bit--, plen++)
1920 if (!(name[byte] & (1 << bit)))
1922 for (; bit != 0; bit--)
1923 if (name[byte] & (1 << bit))
1926 for (; byte < size; byte++)
1936 static char result[256];
1937 int days, hours, mins, secs;
1942 days = total / 3600 / 24;
1943 hours = (total / 3600) % 24;
1944 mins = (total / 60) % 60;
1949 p += sprintf(p, "%dd", days);
1951 if (!first || hours) {
1953 p += sprintf(p, "%dh", hours);
1955 if (!first || mins) {
1957 p += sprintf(p, "%dm", mins);
1959 sprintf(p, "%ds", secs);
1961 sprintf(result, "%lu", (unsigned long)total);
1971 struct module_stat mstat;
1973 char ifkind[35], *cp, *dp;
1976 /* turn interface and unit into module name */
1977 strcpy(ifkind, "if_");
1978 for (cp = name, dp = ifkind + 3;
1979 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1983 /* scan files in kernel */
1984 mstat.version = sizeof(struct module_stat);
1985 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1986 /* scan modules in file */
1987 for (modid = kldfirstmod(fileid); modid > 0;
1988 modid = modfnext(modid)) {
1989 if (modstat(modid, &mstat) < 0)
1991 /* strip bus name if present */
1992 if ((cp = strchr(mstat.name, '/')) != NULL) {
1997 /* already loaded? */
1998 if (!strcmp(ifkind, cp))
2003 /* not present, we should try to load it */
2010 struct if_clonereq ifcr;
2015 s = socket(AF_INET, SOCK_DGRAM, 0);
2019 memset(&ifcr, 0, sizeof(ifcr));
2021 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
2022 err(1, "SIOCIFGCLONERS for count");
2024 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
2026 err(1, "unable to allocate cloner name buffer");
2028 ifcr.ifcr_count = ifcr.ifcr_total;
2029 ifcr.ifcr_buffer = buf;
2031 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
2032 err(1, "SIOCIFGCLONERS for names");
2035 * In case some disappeared in the mean time, clamp it down.
2037 if (ifcr.ifcr_count > ifcr.ifcr_total)
2038 ifcr.ifcr_count = ifcr.ifcr_total;
2040 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
2055 s = socket(AF_INET, SOCK_DGRAM, 0);
2059 memset(&ifr, 0, sizeof(ifr));
2060 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2061 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
2062 err(1, "SIOCIFCREATE");
2064 if (strcmp(name, ifr.ifr_name) != 0) {
2065 printf("%s\n", ifr.ifr_name);
2066 strlcpy(name, ifr.ifr_name, sizeof(name));
2073 clone_destroy(val, d, s, rafp)
2077 const struct afswtch *rafp;
2080 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2081 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
2082 err(1, "SIOCIFDESTROY");