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.96 2004/02/27 06:43:14 kan Exp $
36 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.7 2004/03/15 22:39:37 hmp Exp $
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;
132 int printname = 0; /* Print the name of the created interface. */
135 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
138 void Perror(const char *cmd);
139 void checkatrange(struct sockaddr_at *);
140 int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
141 void notealias(const char *, int, int, const struct afswtch *afp);
142 void list_cloners(void);
143 void printb(const char *s, unsigned value, const char *bits);
144 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
145 void status(const struct afswtch *afp, int addrcount,
146 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
147 struct ifa_msghdr *ifam);
148 void tunnel_status(int s);
150 void ifmaybeload(char *name);
153 void in6_fillscopeid(struct sockaddr_in6 *sin6);
154 int prefix(void *, int);
155 static char *sec2str(time_t);
156 int explicit_prefix = 0;
159 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
160 typedef void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp);
161 c_func setatphase, setatrange;
162 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
166 c_func setifprefixlen;
170 c_func2 setip6lifetime;
174 c_func setifflags, setifmetric, setifmtu, setifcap;
175 c_func clone_destroy;
179 void clone_create(void);
182 #define NEXTARG 0xffffff
183 #define NEXTARG2 0xfffffe
188 int c_parameter; /* NEXTARG means next argv */
189 void (*c_func)(const char *, int, int, const struct afswtch *afp);
190 void (*c_func2)(const char *, const char *, int, const struct afswtch *afp);
192 { "up", IFF_UP, setifflags } ,
193 { "down", -IFF_UP, setifflags },
194 { "arp", -IFF_NOARP, setifflags },
195 { "-arp", IFF_NOARP, setifflags },
196 { "debug", IFF_DEBUG, setifflags },
197 { "-debug", -IFF_DEBUG, setifflags },
198 { "promisc", IFF_PPROMISC, setifflags },
199 { "-promisc", -IFF_PPROMISC, setifflags },
200 { "add", IFF_UP, notealias },
201 { "alias", IFF_UP, notealias },
202 { "-alias", -IFF_UP, notealias },
203 { "delete", -IFF_UP, notealias },
204 { "remove", -IFF_UP, notealias },
206 #define EN_SWABIPS 0x1000
207 { "swabips", EN_SWABIPS, setifflags },
208 { "-swabips", -EN_SWABIPS, setifflags },
210 { "netmask", NEXTARG, setifnetmask },
212 { "prefixlen", NEXTARG, setifprefixlen },
213 { "anycast", IN6_IFF_ANYCAST, setip6flags },
214 { "tentative", IN6_IFF_TENTATIVE, setip6flags },
215 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags },
216 { "deprecated", IN6_IFF_DEPRECATED, setip6flags },
217 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags },
218 { "autoconf", IN6_IFF_AUTOCONF, setip6flags },
219 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags },
220 { "pltime", NEXTARG, setip6pltime },
221 { "vltime", NEXTARG, setip6vltime },
222 { "eui64", 0, setip6eui64 },
224 { "range", NEXTARG, setatrange },
225 { "phase", NEXTARG, setatphase },
226 { "metric", NEXTARG, setifmetric },
227 { "broadcast", NEXTARG, setifbroadaddr },
228 { "ipdst", NEXTARG, setifipdst },
229 { "tunnel", NEXTARG2, NULL, settunnel },
230 { "deletetunnel", 0, deletetunnel },
231 { "link0", IFF_LINK0, setifflags },
232 { "-link0", -IFF_LINK0, setifflags },
233 { "link1", IFF_LINK1, setifflags },
234 { "-link1", -IFF_LINK1, setifflags },
235 { "link2", IFF_LINK2, setifflags },
236 { "-link2", -IFF_LINK2, setifflags },
238 { "media", NEXTARG, setmedia },
239 { "mediaopt", NEXTARG, setmediaopt },
240 { "-mediaopt", NEXTARG, unsetmediaopt },
243 { "vlan", NEXTARG, setvlantag },
244 { "vlandev", NEXTARG, setvlandev },
245 { "-vlandev", NEXTARG, unsetvlandev },
248 /* XXX `create' special-cased below */
249 {"create", 0, clone_create },
250 {"plumb", 0, clone_create },
252 {"destroy", 0, clone_destroy },
253 {"unplumb", 0, clone_destroy },
255 { "ssid", NEXTARG, set80211ssid },
256 { "nwid", NEXTARG, set80211ssid },
257 { "stationname", NEXTARG, set80211stationname },
258 { "station", NEXTARG, set80211stationname }, /* BSD/OS */
259 { "channel", NEXTARG, set80211channel },
260 { "authmode", NEXTARG, set80211authmode },
261 { "powersavemode", NEXTARG, set80211powersavemode },
262 { "powersave", 1, set80211powersave },
263 { "-powersave", 0, set80211powersave },
264 { "powersavesleep", NEXTARG, set80211powersavesleep },
265 { "wepmode", NEXTARG, set80211wepmode },
266 { "wep", 1, set80211wep },
267 { "-wep", 0, set80211wep },
268 { "weptxkey", NEXTARG, set80211weptxkey },
269 { "wepkey", NEXTARG, set80211wepkey },
270 { "nwkey", NEXTARG, set80211nwkey }, /* NetBSD */
271 { "-nwkey", 0, set80211wep }, /* NetBSD */
273 { "rxcsum", IFCAP_RXCSUM, setifcap },
274 { "-rxcsum", -IFCAP_RXCSUM, setifcap },
275 { "txcsum", IFCAP_TXCSUM, setifcap },
276 { "-txcsum", -IFCAP_TXCSUM, setifcap },
277 { "netcons", IFCAP_NETCONS, setifcap },
278 { "-netcons", -IFCAP_NETCONS, setifcap },
279 { "normal", -IFF_LINK0, setifflags },
280 { "compress", IFF_LINK0, setifflags },
281 { "noicmp", IFF_LINK1, setifflags },
282 { "mtu", NEXTARG, setifmtu },
283 { "name", NEXTARG, setifname },
285 { 0, 0, setifdstaddr },
289 * XNS support liberally adapted from code written at the University of
290 * Maryland principally by James O'Toole and Chris Torek.
292 typedef void af_status(int, struct rt_addrinfo *);
293 typedef void af_getaddr(const char *, int);
294 typedef void af_getprefix(const char *, int);
296 af_status in_status, at_status, link_status;
297 af_getaddr in_getaddr, at_getaddr, link_getaddr;
300 af_status ipx_status;
301 af_getaddr ipx_getaddr;
305 af_status in6_status;
306 af_getaddr in6_getaddr;
307 af_getprefix in6_getprefix;
310 af_status xns_status;
311 af_getaddr xns_getaddr;
314 /* Known address families */
319 af_status *af_status;
320 af_getaddr *af_getaddr;
321 af_getprefix *af_getprefix;
327 #define C(x) ((caddr_t) &x)
328 { "inet", AF_INET, in_status, in_getaddr, NULL,
329 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
331 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
332 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
333 C(in6_ridreq), C(in6_addreq) },
336 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
337 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
339 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
340 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
342 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
343 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
345 { "link", AF_LINK, link_status, link_getaddr, NULL,
346 0, SIOCSIFLLADDR, NULL, C(ridreq) },
347 { "ether", AF_LINK, link_status, link_getaddr, NULL,
348 0, SIOCSIFLLADDR, NULL, C(ridreq) },
349 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
350 0, SIOCSIFLLADDR, NULL, C(ridreq) },
351 #if 0 /* XXX conflicts with the media command */
353 { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */
356 { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, }, /* XXX not real!! */
359 { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, }, /* XXX not real!! */
366 * Expand the compacted form of addresses as returned via the
367 * configuration read via sysctl().
371 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
372 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
375 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
380 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
381 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
382 if ((rtinfo->rti_addrs & (1 << i)) == 0)
384 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
394 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
395 "usage: ifconfig interface address_family [address [dest_address]]",
398 " ifconfig interface create",
399 " ifconfig -a [-d] [-m] [-u] [address_family]",
400 " ifconfig -l [-d] [-u] [address_family]",
401 " ifconfig [-d] [-m] [-u]");
403 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
404 "usage: ifconfig [-L] interface address_family [address [dest_address]]",
407 " ifconfig interface create",
408 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
409 " ifconfig -l [-d] [-u] [address_family]",
410 " ifconfig [-L] [-d] [-m] [-u]");
416 main(int argc, char * const *argv)
419 int all, namesonly, downonly, uponly;
420 int foundit = 0, need_nl = 0;
421 const struct afswtch *afp = 0;
423 struct if_msghdr *ifm, *nextifm;
424 struct ifa_msghdr *ifam;
425 struct sockaddr_dl *sdl;
426 char *buf, *lim, *next;
432 /* Parse leading line options */
433 all = downonly = uponly = namesonly = 0;
434 while ((c = getopt(argc, argv, "adlmuC"
440 case 'a': /* scan all interfaces */
443 case 'd': /* restrict scan to "down" interfaces */
446 case 'l': /* scan interface names only */
449 case 'm': /* show media choices in status */
452 case 'u': /* restrict scan to "up" interfaces */
460 ip6lifetime++; /* print IPv6 address lifetime */
472 /* -C must be solitary */
473 if (all || supmedia || uponly || downonly || namesonly ||
481 /* -l cannot be used with -a or -m */
482 if (namesonly && (all || supmedia))
486 if (uponly && downonly)
489 /* no arguments is equivalent to '-a' */
490 if (!namesonly && argc < 1)
493 /* -a and -l allow an address family arg to limit the output */
494 if (all || namesonly) {
499 for (afp = afs; afp->af_name; afp++)
500 if (strcmp(afp->af_name, *argv) == 0) {
504 if (afp->af_name == NULL)
506 /* leave with afp non-zero */
509 /* not listing, need an argument */
513 strncpy(name, *argv, sizeof(name));
516 /* check and maybe load support for this interface */
520 * NOTE: We must special-case the `create' command right
521 * here as we would otherwise fail when trying to find
524 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
525 strcmp(argv[0], "plumb") == 0)) {
533 /* Check for address family */
535 for (afp = afs; afp->af_name; afp++)
536 if (strcmp(afp->af_name, *argv) == 0) {
540 if (afp->af_name == NULL)
541 afp = NULL; /* not a family, NULL */
547 mib[3] = 0; /* address family */
548 mib[4] = NET_RT_IFLIST;
551 /* if particular family specified, only ask about it */
555 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
556 errx(1, "iflist-sysctl-estimate");
557 if ((buf = malloc(needed)) == NULL)
559 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
560 errx(1, "actual retrieval of interface table");
566 ifm = (struct if_msghdr *)next;
568 if (ifm->ifm_type == RTM_IFINFO) {
569 sdl = (struct sockaddr_dl *)(ifm + 1);
570 flags = ifm->ifm_flags;
572 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
573 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
575 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
576 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
581 next += ifm->ifm_msglen;
586 nextifm = (struct if_msghdr *)next;
588 if (nextifm->ifm_type != RTM_NEWADDR)
592 ifam = (struct ifa_msghdr *)nextifm;
595 next += nextifm->ifm_msglen;
597 strlcpy(name, sdl->sdl_data,
598 sizeof(name) <= sdl->sdl_nlen ?
599 sizeof(name) : sdl->sdl_nlen + 1);
601 if (all || namesonly) {
603 if ((flags & IFF_UP) == 0)
604 continue; /* not up */
607 continue; /* not down */
608 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
609 name[sdl->sdl_nlen] = '\0';
612 afp->af_status != link_status ||
613 sdl->sdl_type == IFT_ETHER) {
622 if (strlen(name) != sdl->sdl_nlen)
623 continue; /* not same len */
624 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
625 continue; /* not same name */
629 ifconfig(argc, argv, afp);
631 status(afp, addrcount, sdl, ifm, ifam);
633 if (all == 0 && namesonly == 0) {
634 foundit++; /* flag it as 'done' */
640 if (namesonly && need_nl > 0)
644 printf("%s\n", name);
646 if (all == 0 && namesonly == 0 && foundit == 0)
647 errx(1, "interface %s does not exist", name);
655 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
661 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
662 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
664 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
668 register const struct cmd *p;
670 for (p = cmds; p->c_name; p++)
671 if (strcmp(*argv, p->c_name) == 0)
673 if (p->c_name == 0 && setaddr)
674 p++; /* got src, do dst */
675 if (p->c_func || p->c_func2) {
676 if (p->c_parameter == NEXTARG) {
678 errx(1, "'%s' requires argument",
680 (*p->c_func)(argv[1], 0, s, afp);
682 } else if (p->c_parameter == NEXTARG2) {
684 errx(1, "'%s' requires 2 arguments",
686 (*p->c_func2)(argv[1], argv[2], s, afp);
687 argc -= 2, argv += 2;
689 (*p->c_func)(*argv, p->c_parameter, s, afp);
694 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
695 /* Aggregatable address architecture defines all prefixes
696 are 64. So, it is convenient to set prefixlen to 64 if
697 it is not specified. */
698 setifprefixlen("64", 0, s, afp);
699 /* in6_getprefix("64", MASK) if MASK is available here... */
703 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
705 int size = sizeof(rq);
707 rq.rq_ipx = addreq.ifra_addr;
708 rq.rq_ip = addreq.ifra_dstaddr;
710 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
711 Perror("Encapsulation Routing");
714 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
715 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
717 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
719 int size = sizeof(rq);
721 rq.rq_ns = addreq.ifra_addr;
722 rq.rq_ip = addreq.ifra_dstaddr;
724 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
725 Perror("Encapsulation Routing");
729 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
730 warnx("interface %s cannot change %s addresses!",
737 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
738 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
739 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
740 /* means no previous address for interface */
742 Perror("ioctl (SIOCDIFADDR)");
746 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
747 warnx("interface %s cannot change %s addresses!",
752 if (newaddr && (setaddr || setmask)) {
753 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
754 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
755 Perror("ioctl (SIOCAIFADDR)");
767 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
769 if (*afp->af_getaddr == NULL)
772 * Delay the ioctl to set the interface addr until flags are all set.
773 * The address interpretation may depend on the flags,
774 * and the flags may change when the address is set.
777 if (doalias == 0 && afp->af_af != AF_LINK)
779 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
783 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
785 struct addrinfo hints, *srcres, *dstres;
786 struct ifaliasreq addreq;
789 struct in6_aliasreq in6_addreq;
792 memset(&hints, 0, sizeof(hints));
793 hints.ai_family = afp->af_af;
795 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
796 errx(1, "error in parsing address string: %s",
797 gai_strerror(ecode));
799 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
800 errx(1, "error in parsing address string: %s",
801 gai_strerror(ecode));
803 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
805 "source and destination address families do not match");
807 switch (srcres->ai_addr->sa_family) {
809 memset(&addreq, 0, sizeof(addreq));
810 strncpy(addreq.ifra_name, name, IFNAMSIZ);
811 memcpy(&addreq.ifra_addr, srcres->ai_addr,
812 srcres->ai_addr->sa_len);
813 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
814 dstres->ai_addr->sa_len);
816 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
817 warn("SIOCSIFPHYADDR");
822 memset(&in6_addreq, 0, sizeof(in6_addreq));
823 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
824 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
825 srcres->ai_addr->sa_len);
826 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
827 dstres->ai_addr->sa_len);
829 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
830 warn("SIOCSIFPHYADDR_IN6");
835 warn("address family not supported");
838 freeaddrinfo(srcres);
839 freeaddrinfo(dstres);
844 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
847 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
848 err(1, "SIOCDIFPHYADDR");
852 setifnetmask(const char *addr, int dummy __unused, int s,
853 const struct afswtch *afp)
855 if (*afp->af_getaddr == NULL)
858 (*afp->af_getaddr)(addr, MASK);
863 setifprefixlen(const char *addr, int dummy __unused, int s,
864 const struct afswtch *afp)
866 if (*afp->af_getprefix)
867 (*afp->af_getprefix)(addr, MASK);
872 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
873 const struct afswtch *afp)
875 if (afp->af_af != AF_INET6)
876 err(1, "address flags can be set only for inet6 addresses");
879 in6_addreq.ifra_flags &= ~(-flag);
881 in6_addreq.ifra_flags |= flag;
885 setip6pltime(const char *seconds, int dummy __unused, int s,
886 const struct afswtch *afp)
888 setip6lifetime("pltime", seconds, s, afp);
892 setip6vltime(const char *seconds, int dummy __unused, int s,
893 const struct afswtch *afp)
895 setip6lifetime("vltime", seconds, s, afp);
899 setip6lifetime(const char *cmd, const char *val, int s,
900 const struct afswtch *afp)
906 newval = (time_t)strtoul(val, &ep, 0);
908 errx(1, "invalid %s", cmd);
909 if (afp->af_af != AF_INET6)
910 errx(1, "%s not allowed for the AF", cmd);
911 if (strcmp(cmd, "vltime") == 0) {
912 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
913 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
914 } else if (strcmp(cmd, "pltime") == 0) {
915 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
916 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
921 setip6eui64(const char *cmd, int dummy __unused, int s,
922 const struct afswtch *afp)
924 struct ifaddrs *ifap, *ifa;
925 const struct sockaddr_in6 *sin6 = NULL;
926 const struct in6_addr *lladdr = NULL;
927 struct in6_addr *in6;
929 if (afp->af_af != AF_INET6)
930 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
931 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
932 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
933 errx(EXIT_FAILURE, "interface index is already filled");
934 if (getifaddrs(&ifap) != 0)
935 err(EXIT_FAILURE, "getifaddrs");
936 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
937 if (ifa->ifa_addr->sa_family == AF_INET6 &&
938 strcmp(ifa->ifa_name, name) == 0) {
939 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
940 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
941 lladdr = &sin6->sin6_addr;
947 errx(EXIT_FAILURE, "could not determine link local address");
949 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
956 setifbroadaddr(const char *addr, int dummy __unused, int s,
957 const struct afswtch *afp)
959 if (*afp->af_getaddr == NULL)
961 (*afp->af_getaddr)(addr, DSTADDR);
965 setifipdst(const char *addr, int dummy __unused, int s,
966 const struct afswtch *afp)
968 in_getaddr(addr, DSTADDR);
973 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
976 notealias(const char *addr, int param, int s, const struct afswtch *afp)
978 if (setaddr && doalias == 0 && param < 0)
979 bcopy((caddr_t)rqtosa(af_addreq),
980 (caddr_t)rqtosa(af_ridreq),
981 rqtosa(af_addreq)->sa_len);
992 setifdstaddr(const char *addr, int param __unused, int s,
993 const struct afswtch *afp)
995 if (*afp->af_getaddr == NULL)
997 (*afp->af_getaddr)(addr, DSTADDR);
1001 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1002 * of the ifreq structure, which may confuse other parts of ifconfig.
1003 * Make a private copy so we can avoid that.
1006 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1008 struct ifreq my_ifr;
1010 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1012 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1013 Perror("ioctl (SIOCGIFFLAGS)");
1016 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1017 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1024 my_ifr.ifr_flags = flags & 0xffff;
1025 my_ifr.ifr_flagshigh = flags >> 16;
1026 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1031 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1034 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1035 Perror("ioctl (SIOCGIFCAP)");
1038 flags = ifr.ifr_curcap;
1044 ifr.ifr_reqcap = flags;
1045 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1050 setifmetric(const char *val, int dummy __unused, int s,
1051 const struct afswtch *afp)
1053 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1054 ifr.ifr_metric = atoi(val);
1055 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1056 warn("ioctl (set metric)");
1060 setifmtu(const char *val, int dummy __unused, int s, const struct afswtch *afp)
1062 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1063 ifr.ifr_mtu = atoi(val);
1064 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1065 warn("ioctl (set mtu)");
1069 setifname(const char *val, int dummy __unused, int s,
1070 const struct afswtch *afp)
1074 newname = strdup(val);
1076 ifr.ifr_data = newname;
1077 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1078 warn("ioctl (set name)");
1082 strlcpy(name, newname, sizeof(name));
1087 * Even if we just created the interface, we don't need to print
1088 * its name because we just nailed it down separately.
1094 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1095 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1099 "\003\1rxcsum\2txcsum\3netcons"
1102 * Print the status of the interface. If an address family was
1103 * specified, show it and it only; otherwise, show them all.
1106 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1107 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1109 const struct afswtch *p = NULL;
1110 struct rt_addrinfo info;
1120 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1121 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1123 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1126 printf("%s: ", name);
1127 printb("flags", flags, IFFBITS);
1128 if (ifm->ifm_data.ifi_metric)
1129 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1130 if (ifm->ifm_data.ifi_mtu)
1131 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1134 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1135 if (ifr.ifr_curcap != 0) {
1136 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1139 if (supmedia && ifr.ifr_reqcap != 0) {
1140 printf("\tcapability list:\n");
1141 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1148 while (addrcount > 0) {
1150 info.rti_addrs = ifam->ifam_addrs;
1152 /* Expand the compacted addresses */
1153 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1157 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1159 (*p->af_status)(s, &info);
1161 } else for (p = afs; p->af_name; p++) {
1162 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1163 (*p->af_status)(s, &info);
1166 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1168 if (allfamilies || afp->af_status == link_status)
1169 link_status(s, (struct rt_addrinfo *)sdl);
1171 if (allfamilies || afp->af_status == media_status)
1172 media_status(s, NULL);
1175 if (allfamilies || afp->af_status == vlan_status)
1176 vlan_status(s, NULL);
1178 #ifdef USE_IEEE80211
1179 if (allfamilies || afp->af_status == ieee80211_status)
1180 ieee80211_status(s, NULL);
1182 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1183 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1184 printf("%s", ifs.ascii);
1186 if (!allfamilies && !p && afp->af_status != media_status &&
1187 afp->af_status != link_status
1189 && afp->af_status != vlan_status
1192 warnx("%s has no %s interface address!", name, afp->af_name);
1199 tunnel_status(int s)
1201 char psrcaddr[NI_MAXHOST];
1202 char pdstaddr[NI_MAXHOST];
1203 u_long srccmd, dstcmd;
1205 const char *ver = "";
1206 #ifdef NI_WITHSCOPEID
1207 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1209 const int niflag = NI_NUMERICHOST;
1212 struct in6_ifreq in6_ifr;
1216 psrcaddr[0] = pdstaddr[0] = '\0';
1219 memset(&in6_ifr, 0, sizeof(in6_ifr));
1220 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1221 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1223 srccmd = SIOCGIFPSRCADDR;
1224 dstcmd = SIOCGIFPDSTADDR;
1228 srccmd = SIOCGIFPSRCADDR_IN6;
1229 dstcmd = SIOCGIFPDSTADDR_IN6;
1230 ifrp = (struct ifreq *)&in6_ifr;
1233 srccmd = SIOCGIFPSRCADDR;
1234 dstcmd = SIOCGIFPDSTADDR;
1238 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1241 if (ifrp->ifr_addr.sa_family == AF_INET6)
1242 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1244 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1245 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1247 if (ifrp->ifr_addr.sa_family == AF_INET6)
1251 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1254 if (ifrp->ifr_addr.sa_family == AF_INET6)
1255 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1257 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1258 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1260 printf("\ttunnel inet%s %s --> %s\n", ver,
1261 psrcaddr, pdstaddr);
1265 in_status(int s __unused, struct rt_addrinfo *info)
1267 struct sockaddr_in *sin, null_sin;
1269 memset(&null_sin, 0, sizeof(null_sin));
1271 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1272 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1274 if (flags & IFF_POINTOPOINT) {
1275 /* note RTAX_BRD overlap with IFF_BROADCAST */
1276 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1279 printf("--> %s ", inet_ntoa(sin->sin_addr));
1282 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1285 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1287 if (flags & IFF_BROADCAST) {
1288 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1289 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1290 if (sin && sin->sin_addr.s_addr != 0)
1291 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1298 in6_fillscopeid(struct sockaddr_in6 *sin6)
1300 #if defined(__KAME__) && defined(KAME_SCOPEID)
1301 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1302 sin6->sin6_scope_id =
1303 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1304 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1310 in6_status(int s __unused, struct rt_addrinfo *info)
1312 struct sockaddr_in6 *sin, null_sin;
1313 struct in6_ifreq ifr6;
1316 struct in6_addrlifetime lifetime;
1317 time_t t = time(NULL);
1321 memset(&null_sin, 0, sizeof(null_sin));
1323 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1324 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1325 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1326 perror("ifconfig: socket");
1329 ifr6.ifr_addr = *sin;
1330 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1331 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1335 flags6 = ifr6.ifr_ifru.ifru_flags6;
1336 memset(&lifetime, 0, sizeof(lifetime));
1337 ifr6.ifr_addr = *sin;
1338 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1339 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1343 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1346 /* XXX: embedded link local addr check */
1347 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1348 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1351 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1352 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1353 if (sin->sin6_scope_id == 0)
1354 sin->sin6_scope_id = ntohs(index);
1356 scopeid = sin->sin6_scope_id;
1358 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1359 sizeof(addr_buf), NULL, 0,
1360 NI_NUMERICHOST|NI_WITHSCOPEID);
1362 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1364 printf("\tinet6 %s ", addr_buf);
1366 if (flags & IFF_POINTOPOINT) {
1367 /* note RTAX_BRD overlap with IFF_BROADCAST */
1368 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1370 * some of the interfaces do not have valid destination
1373 if (sin && sin->sin6_family == AF_INET6) {
1376 /* XXX: embedded link local addr check */
1377 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1378 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1381 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1382 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1383 if (sin->sin6_scope_id == 0)
1384 sin->sin6_scope_id = ntohs(index);
1387 error = getnameinfo((struct sockaddr *)sin,
1388 sin->sin6_len, addr_buf,
1389 sizeof(addr_buf), NULL, 0,
1390 NI_NUMERICHOST|NI_WITHSCOPEID);
1392 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1394 printf("--> %s ", addr_buf);
1398 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1401 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1402 sizeof(struct in6_addr)));
1404 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1406 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1407 printf("tentative ");
1408 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1409 printf("duplicated ");
1410 if ((flags6 & IN6_IFF_DETACHED) != 0)
1411 printf("detached ");
1412 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1413 printf("deprecated ");
1414 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1415 printf("autoconf ");
1416 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1417 printf("temporary ");
1420 printf("scopeid 0x%x ", scopeid);
1422 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1424 if (lifetime.ia6t_preferred) {
1425 printf("%s ", lifetime.ia6t_preferred < t
1426 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1431 if (lifetime.ia6t_expire) {
1432 printf("%s ", lifetime.ia6t_expire < t
1433 ? "0" : sec2str(lifetime.ia6t_expire - t));
1444 ipx_status(int s __unused, struct rt_addrinfo *info)
1446 struct sockaddr_ipx *sipx, null_sipx;
1448 memset(&null_sipx, 0, sizeof(null_sipx));
1450 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1451 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1453 if (flags & IFF_POINTOPOINT) {
1454 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1457 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1464 at_status(int s __unused, struct rt_addrinfo *info)
1466 struct sockaddr_at *sat, null_sat;
1467 struct netrange *nr;
1469 memset(&null_sat, 0, sizeof(null_sat));
1471 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1472 nr = &sat->sat_range.r_netrange;
1473 printf("\tatalk %d.%d range %d-%d phase %d",
1474 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1475 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1476 if (flags & IFF_POINTOPOINT) {
1477 /* note RTAX_BRD overlap with IFF_BROADCAST */
1478 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1482 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1484 if (flags & IFF_BROADCAST) {
1485 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1486 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1488 printf(" broadcast %d.%d",
1489 ntohs(sat->sat_addr.s_net),
1490 sat->sat_addr.s_node);
1498 xns_status(int s __unused, struct rt_addrinfo *info)
1500 struct sockaddr_ns *sns, null_sns;
1502 memset(&null_sns, 0, sizeof(null_sns));
1504 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1505 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1507 if (flags & IFF_POINTOPOINT) {
1508 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1511 printf("--> %s ", ns_ntoa(sns->sns_addr));
1521 link_status(int s __unused, struct rt_addrinfo *info)
1523 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1525 if (sdl->sdl_alen > 0) {
1526 if (sdl->sdl_type == IFT_ETHER &&
1527 sdl->sdl_alen == ETHER_ADDR_LEN)
1528 printf("\tether %s\n",
1529 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1531 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1533 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1539 Perror(const char *cmd)
1544 errx(1, "%s: no such interface", cmd);
1548 errx(1, "%s: permission denied", cmd);
1556 #define SIN(x) ((struct sockaddr_in *) &(x))
1557 struct sockaddr_in *sintab[] = {
1558 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1559 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1562 in_getaddr(const char *s, int which)
1564 register struct sockaddr_in *sin = sintab[which];
1568 sin->sin_len = sizeof(*sin);
1570 sin->sin_family = AF_INET;
1572 if (which == ADDR) {
1575 if((p = strrchr(s, '/')) != NULL) {
1576 /* address is `name/masklen' */
1579 struct sockaddr_in *min = sintab[MASK];
1581 ret = sscanf(p+1, "%u", &masklen);
1582 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1584 errx(1, "%s: bad value", s);
1586 min->sin_len = sizeof(*min);
1587 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1592 if (inet_aton(s, &sin->sin_addr))
1594 if ((hp = gethostbyname(s)) != 0)
1595 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1596 MIN(hp->h_length, sizeof(sin->sin_addr)));
1597 else if ((np = getnetbyname(s)) != 0)
1598 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1600 errx(1, "%s: bad value", s);
1604 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1605 struct sockaddr_in6 *sin6tab[] = {
1606 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1607 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1610 in6_getaddr(const char *s, int which)
1612 register struct sockaddr_in6 *sin = sin6tab[which];
1613 struct addrinfo hints, *res;
1618 sin->sin6_len = sizeof(*sin);
1620 sin->sin6_family = AF_INET6;
1622 if (which == ADDR) {
1624 if((p = strrchr(s, '/')) != NULL) {
1626 in6_getprefix(p + 1, MASK);
1627 explicit_prefix = 1;
1631 if (sin->sin6_family == AF_INET6) {
1632 bzero(&hints, sizeof(struct addrinfo));
1633 hints.ai_family = AF_INET6;
1634 error = getaddrinfo(s, NULL, &hints, &res);
1637 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1638 errx(1, "%s: bad value", s);
1640 bcopy(res->ai_addr, sin, res->ai_addrlen);
1644 in6_getprefix(const char *plen, int which)
1646 register struct sockaddr_in6 *sin = sin6tab[which];
1647 register u_char *cp;
1648 int len = atoi(plen);
1650 if ((len < 0) || (len > 128))
1651 errx(1, "%s: bad value", plen);
1652 sin->sin6_len = sizeof(*sin);
1654 sin->sin6_family = AF_INET6;
1655 if ((len == 0) || (len == 128)) {
1656 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1659 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1660 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1662 *cp = 0xff << (8 - len);
1667 * Print a value a la the %b format of the kernel's printf
1670 printb(const char *s, register unsigned v, register const char *bits)
1672 register int i, any = 0;
1675 if (bits && *bits == 8)
1676 printf("%s=%o", s, v);
1678 printf("%s=%x", s, v);
1682 while ((i = *bits++) != '\0') {
1683 if (v & (1 << (i-1))) {
1687 for (; (c = *bits) > 32; bits++)
1690 for (; *bits > 32; bits++)
1698 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1699 struct sockaddr_ipx *sipxtab[] = {
1700 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1701 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1704 ipx_getaddr(const char *addr, int which)
1706 struct sockaddr_ipx *sipx = sipxtab[which];
1708 sipx->sipx_family = AF_IPX;
1709 sipx->sipx_len = sizeof(*sipx);
1710 sipx->sipx_addr = ipx_addr(addr);
1712 printf("Attempt to set IPX netmask will be ineffectual\n");
1717 at_getaddr(const char *addr, int which)
1719 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1722 sat->sat_family = AF_APPLETALK;
1723 sat->sat_len = sizeof(*sat);
1725 errx(1, "AppleTalk does not use netmasks");
1726 if (sscanf(addr, "%u.%u", &net, &node) != 2
1727 || net > 0xffff || node > 0xfe)
1728 errx(1, "%s: illegal address", addr);
1729 sat->sat_addr.s_net = htons(net);
1730 sat->sat_addr.s_node = node;
1734 link_getaddr(const char *addr, int which)
1737 struct sockaddr_dl sdl;
1738 struct sockaddr *sa = &ridreq.ifr_addr;
1741 errx(1, "can't set link-level netmask or broadcast");
1742 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1743 errx(1, "malloc failed");
1745 strcpy(temp + 1, addr);
1746 sdl.sdl_len = sizeof(sdl);
1747 link_addr(temp, &sdl);
1749 if (sdl.sdl_alen > sizeof(sa->sa_data))
1750 errx(1, "malformed link-level address");
1751 sa->sa_family = AF_LINK;
1752 sa->sa_len = sdl.sdl_alen;
1753 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1756 /* XXX FIXME -- should use strtoul for better parsing. */
1758 setatrange(const char *range, int dummy __unused, int s,
1759 const struct afswtch *afp)
1761 u_short first = 123, last = 123;
1763 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1764 || first == 0 || first > 0xffff
1765 || last == 0 || last > 0xffff || first > last)
1766 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1767 at_nr.nr_firstnet = htons(first);
1768 at_nr.nr_lastnet = htons(last);
1772 setatphase(const char *phase, int dummy __unused, int s,
1773 const struct afswtch *afp)
1775 if (!strcmp(phase, "1"))
1777 else if (!strcmp(phase, "2"))
1780 errx(1, "%s: illegal phase", phase);
1784 checkatrange(struct sockaddr_at *sat)
1786 if (at_nr.nr_phase == 0)
1787 at_nr.nr_phase = 2; /* Default phase 2 */
1788 if (at_nr.nr_firstnet == 0)
1789 at_nr.nr_firstnet = /* Default range of one */
1790 at_nr.nr_lastnet = sat->sat_addr.s_net;
1791 printf("\tatalk %d.%d range %d-%d phase %d\n",
1792 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1793 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1794 if ((u_short) ntohs(at_nr.nr_firstnet) >
1795 (u_short) ntohs(sat->sat_addr.s_net)
1796 || (u_short) ntohs(at_nr.nr_lastnet) <
1797 (u_short) ntohs(sat->sat_addr.s_net))
1798 errx(1, "AppleTalk address is not in range");
1799 sat->sat_range.r_netrange = at_nr;
1803 #define SNS(x) ((struct sockaddr_ns *) &(x))
1804 struct sockaddr_ns *snstab[] = {
1805 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1806 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1809 xns_getaddr(const char *addr, int which)
1811 struct sockaddr_ns *sns = snstab[which];
1813 sns->sns_family = AF_NS;
1814 sns->sns_len = sizeof(*sns);
1815 sns->sns_addr = ns_addr(addr);
1817 printf("Attempt to set XNS netmask will be ineffectual\n");
1823 prefix(void *val, int size)
1825 register u_char *name = (u_char *)val;
1826 register int byte, bit, plen = 0;
1828 for (byte = 0; byte < size; byte++, plen += 8)
1829 if (name[byte] != 0xff)
1833 for (bit = 7; bit != 0; bit--, plen++)
1834 if (!(name[byte] & (1 << bit)))
1836 for (; bit != 0; bit--)
1837 if (name[byte] & (1 << bit))
1840 for (; byte < size; byte++)
1847 sec2str(time_t total)
1849 static char result[256];
1850 int days, hours, mins, secs;
1855 days = total / 3600 / 24;
1856 hours = (total / 3600) % 24;
1857 mins = (total / 60) % 60;
1862 p += sprintf(p, "%dd", days);
1864 if (!first || hours) {
1866 p += sprintf(p, "%dh", hours);
1868 if (!first || mins) {
1870 p += sprintf(p, "%dm", mins);
1872 sprintf(p, "%ds", secs);
1874 sprintf(result, "%lu", (unsigned long)total);
1881 ifmaybeload(char *name)
1883 struct module_stat mstat;
1885 char ifkind[35], *cp, *dp;
1888 /* turn interface and unit into module name */
1889 strcpy(ifkind, "if_");
1890 for (cp = name, dp = ifkind + 3;
1891 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1895 /* scan files in kernel */
1896 mstat.version = sizeof(struct module_stat);
1897 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1898 /* scan modules in file */
1899 for (modid = kldfirstmod(fileid); modid > 0;
1900 modid = modfnext(modid)) {
1901 if (modstat(modid, &mstat) < 0)
1903 /* strip bus name if present */
1904 if ((cp = strchr(mstat.name, '/')) != NULL) {
1909 /* already loaded? */
1910 if (strncmp(name, cp, strlen(cp)) == 0 ||
1911 strncmp(ifkind, cp, strlen(cp)) == 0)
1916 /* not present, we should try to load it */
1923 struct if_clonereq ifcr;
1928 s = socket(AF_INET, SOCK_DGRAM, 0);
1932 memset(&ifcr, 0, sizeof(ifcr));
1934 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1935 err(1, "SIOCIFGCLONERS for count");
1937 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1939 err(1, "unable to allocate cloner name buffer");
1941 ifcr.ifcr_count = ifcr.ifcr_total;
1942 ifcr.ifcr_buffer = buf;
1944 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1945 err(1, "SIOCIFGCLONERS for names");
1948 * In case some disappeared in the mean time, clamp it down.
1950 if (ifcr.ifcr_count > ifcr.ifcr_total)
1951 ifcr.ifcr_count = ifcr.ifcr_total;
1953 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1968 s = socket(AF_INET, SOCK_DGRAM, 0);
1972 memset(&ifr, 0, sizeof(ifr));
1973 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1974 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
1975 err(1, "SIOCIFCREATE");
1978 * If we get a different name back then we put in, we probably
1979 * want to print it out, but we might change our mind later so
1980 * we just signal our intrest and leave the printout for later.
1982 if (strcmp(name, ifr.ifr_name) != 0) {
1984 strlcpy(name, ifr.ifr_name, sizeof(name));
1991 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
1994 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1995 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
1996 err(1, "SIOCIFDESTROY");
1998 * If we create and destroy an interface in the same command,
1999 * there isn't any reason to print it's name.