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.9 2004/03/17 09:32:18 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;
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 if (all || namesonly) {
600 /* sdl_data may not be terminated, don't use strlcpy */
601 if ((len = sdl->sdl_nlen) > sizeof(name) - 1)
602 len = sizeof(name) - 1;
603 bcopy(sdl->sdl_data, name, len);
607 if ((flags & IFF_UP) == 0)
608 continue; /* not up */
611 continue; /* not down */
612 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
613 name[sdl->sdl_nlen] = '\0';
616 afp->af_status != link_status ||
617 sdl->sdl_type == IFT_ETHER) {
626 if (strlen(name) != sdl->sdl_nlen)
627 continue; /* not same len */
628 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
629 continue; /* not same name */
633 ifconfig(argc, argv, afp);
635 status(afp, addrcount, sdl, ifm, ifam);
637 if (all == 0 && namesonly == 0) {
638 foundit++; /* flag it as 'done' */
644 if (namesonly && need_nl > 0)
648 printf("%s\n", name);
650 if (all == 0 && namesonly == 0 && foundit == 0)
651 errx(1, "interface %s does not exist", name);
659 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
665 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
666 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
668 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
672 register const struct cmd *p;
674 for (p = cmds; p->c_name; p++)
675 if (strcmp(*argv, p->c_name) == 0)
677 if (p->c_name == 0 && setaddr)
678 p++; /* got src, do dst */
679 if (p->c_func || p->c_func2) {
680 if (p->c_parameter == NEXTARG) {
682 errx(1, "'%s' requires argument",
684 (*p->c_func)(argv[1], 0, s, afp);
686 } else if (p->c_parameter == NEXTARG2) {
688 errx(1, "'%s' requires 2 arguments",
690 (*p->c_func2)(argv[1], argv[2], s, afp);
691 argc -= 2, argv += 2;
693 (*p->c_func)(*argv, p->c_parameter, s, afp);
698 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
699 /* Aggregatable address architecture defines all prefixes
700 are 64. So, it is convenient to set prefixlen to 64 if
701 it is not specified. */
702 setifprefixlen("64", 0, s, afp);
703 /* in6_getprefix("64", MASK) if MASK is available here... */
707 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
709 int size = sizeof(rq);
711 rq.rq_ipx = addreq.ifra_addr;
712 rq.rq_ip = addreq.ifra_dstaddr;
714 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
715 Perror("Encapsulation Routing");
718 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
719 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
721 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
723 int size = sizeof(rq);
725 rq.rq_ns = addreq.ifra_addr;
726 rq.rq_ip = addreq.ifra_dstaddr;
728 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
729 Perror("Encapsulation Routing");
733 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
734 warnx("interface %s cannot change %s addresses!",
741 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
742 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
743 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
744 /* means no previous address for interface */
746 Perror("ioctl (SIOCDIFADDR)");
750 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
751 warnx("interface %s cannot change %s addresses!",
756 if (newaddr && (setaddr || setmask)) {
757 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
758 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
759 Perror("ioctl (SIOCAIFADDR)");
771 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
773 if (*afp->af_getaddr == NULL)
776 * Delay the ioctl to set the interface addr until flags are all set.
777 * The address interpretation may depend on the flags,
778 * and the flags may change when the address is set.
781 if (doalias == 0 && afp->af_af != AF_LINK)
783 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
787 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
789 struct addrinfo hints, *srcres, *dstres;
790 struct ifaliasreq addreq;
793 struct in6_aliasreq in6_addreq;
796 memset(&hints, 0, sizeof(hints));
797 hints.ai_family = afp->af_af;
799 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
800 errx(1, "error in parsing address string: %s",
801 gai_strerror(ecode));
803 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
804 errx(1, "error in parsing address string: %s",
805 gai_strerror(ecode));
807 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
809 "source and destination address families do not match");
811 switch (srcres->ai_addr->sa_family) {
813 memset(&addreq, 0, sizeof(addreq));
814 strncpy(addreq.ifra_name, name, IFNAMSIZ);
815 memcpy(&addreq.ifra_addr, srcres->ai_addr,
816 srcres->ai_addr->sa_len);
817 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
818 dstres->ai_addr->sa_len);
820 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
821 warn("SIOCSIFPHYADDR");
826 memset(&in6_addreq, 0, sizeof(in6_addreq));
827 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
828 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
829 srcres->ai_addr->sa_len);
830 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
831 dstres->ai_addr->sa_len);
833 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
834 warn("SIOCSIFPHYADDR_IN6");
839 warn("address family not supported");
842 freeaddrinfo(srcres);
843 freeaddrinfo(dstres);
848 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
851 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
852 err(1, "SIOCDIFPHYADDR");
856 setifnetmask(const char *addr, int dummy __unused, int s,
857 const struct afswtch *afp)
859 if (*afp->af_getaddr == NULL)
862 (*afp->af_getaddr)(addr, MASK);
867 setifprefixlen(const char *addr, int dummy __unused, int s,
868 const struct afswtch *afp)
870 if (*afp->af_getprefix)
871 (*afp->af_getprefix)(addr, MASK);
876 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
877 const struct afswtch *afp)
879 if (afp->af_af != AF_INET6)
880 err(1, "address flags can be set only for inet6 addresses");
883 in6_addreq.ifra_flags &= ~(-flag);
885 in6_addreq.ifra_flags |= flag;
889 setip6pltime(const char *seconds, int dummy __unused, int s,
890 const struct afswtch *afp)
892 setip6lifetime("pltime", seconds, s, afp);
896 setip6vltime(const char *seconds, int dummy __unused, int s,
897 const struct afswtch *afp)
899 setip6lifetime("vltime", seconds, s, afp);
903 setip6lifetime(const char *cmd, const char *val, int s,
904 const struct afswtch *afp)
910 newval = (time_t)strtoul(val, &ep, 0);
912 errx(1, "invalid %s", cmd);
913 if (afp->af_af != AF_INET6)
914 errx(1, "%s not allowed for the AF", cmd);
915 if (strcmp(cmd, "vltime") == 0) {
916 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
917 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
918 } else if (strcmp(cmd, "pltime") == 0) {
919 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
920 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
925 setip6eui64(const char *cmd, int dummy __unused, int s,
926 const struct afswtch *afp)
928 struct ifaddrs *ifap, *ifa;
929 const struct sockaddr_in6 *sin6 = NULL;
930 const struct in6_addr *lladdr = NULL;
931 struct in6_addr *in6;
933 if (afp->af_af != AF_INET6)
934 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
935 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
936 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
937 errx(EXIT_FAILURE, "interface index is already filled");
938 if (getifaddrs(&ifap) != 0)
939 err(EXIT_FAILURE, "getifaddrs");
940 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
941 if (ifa->ifa_addr->sa_family == AF_INET6 &&
942 strcmp(ifa->ifa_name, name) == 0) {
943 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
944 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
945 lladdr = &sin6->sin6_addr;
951 errx(EXIT_FAILURE, "could not determine link local address");
953 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
960 setifbroadaddr(const char *addr, int dummy __unused, int s,
961 const struct afswtch *afp)
963 if (*afp->af_getaddr == NULL)
965 (*afp->af_getaddr)(addr, DSTADDR);
969 setifipdst(const char *addr, int dummy __unused, int s,
970 const struct afswtch *afp)
972 in_getaddr(addr, DSTADDR);
977 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
980 notealias(const char *addr, int param, int s, const struct afswtch *afp)
982 if (setaddr && doalias == 0 && param < 0)
983 bcopy((caddr_t)rqtosa(af_addreq),
984 (caddr_t)rqtosa(af_ridreq),
985 rqtosa(af_addreq)->sa_len);
996 setifdstaddr(const char *addr, int param __unused, int s,
997 const struct afswtch *afp)
999 if (*afp->af_getaddr == NULL)
1001 (*afp->af_getaddr)(addr, DSTADDR);
1005 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1006 * of the ifreq structure, which may confuse other parts of ifconfig.
1007 * Make a private copy so we can avoid that.
1010 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1012 struct ifreq my_ifr;
1014 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1016 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1017 Perror("ioctl (SIOCGIFFLAGS)");
1020 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1021 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1028 my_ifr.ifr_flags = flags & 0xffff;
1029 my_ifr.ifr_flagshigh = flags >> 16;
1030 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1035 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1038 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1039 Perror("ioctl (SIOCGIFCAP)");
1042 flags = ifr.ifr_curcap;
1048 ifr.ifr_reqcap = flags;
1049 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1054 setifmetric(const char *val, int dummy __unused, int s,
1055 const struct afswtch *afp)
1057 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1058 ifr.ifr_metric = atoi(val);
1059 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1060 warn("ioctl (set metric)");
1064 setifmtu(const char *val, int dummy __unused, int s, const struct afswtch *afp)
1066 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1067 ifr.ifr_mtu = atoi(val);
1068 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1069 warn("ioctl (set mtu)");
1073 setifname(const char *val, int dummy __unused, int s,
1074 const struct afswtch *afp)
1078 newname = strdup(val);
1080 ifr.ifr_data = newname;
1081 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1082 warn("ioctl (set name)");
1086 strlcpy(name, newname, sizeof(name));
1091 * Even if we just created the interface, we don't need to print
1092 * its name because we just nailed it down separately.
1098 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1099 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1103 "\003\1rxcsum\2txcsum\3netcons"
1106 * Print the status of the interface. If an address family was
1107 * specified, show it and it only; otherwise, show them all.
1110 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1111 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1113 const struct afswtch *p = NULL;
1114 struct rt_addrinfo info;
1124 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1125 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1127 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1130 printf("%s: ", name);
1131 printb("flags", flags, IFFBITS);
1132 if (ifm->ifm_data.ifi_metric)
1133 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1134 if (ifm->ifm_data.ifi_mtu)
1135 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1138 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1139 if (ifr.ifr_curcap != 0) {
1140 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1143 if (supmedia && ifr.ifr_reqcap != 0) {
1144 printf("\tcapability list:\n");
1145 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1152 while (addrcount > 0) {
1154 info.rti_addrs = ifam->ifam_addrs;
1156 /* Expand the compacted addresses */
1157 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1161 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1163 (*p->af_status)(s, &info);
1165 } else for (p = afs; p->af_name; p++) {
1166 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1167 (*p->af_status)(s, &info);
1170 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1172 if (allfamilies || afp->af_status == link_status)
1173 link_status(s, (struct rt_addrinfo *)sdl);
1175 if (allfamilies || afp->af_status == media_status)
1176 media_status(s, NULL);
1179 if (allfamilies || afp->af_status == vlan_status)
1180 vlan_status(s, NULL);
1182 #ifdef USE_IEEE80211
1183 if (allfamilies || afp->af_status == ieee80211_status)
1184 ieee80211_status(s, NULL);
1186 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1187 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1188 printf("%s", ifs.ascii);
1190 if (!allfamilies && !p && afp->af_status != media_status &&
1191 afp->af_status != link_status
1193 && afp->af_status != vlan_status
1196 warnx("%s has no %s interface address!", name, afp->af_name);
1203 tunnel_status(int s)
1205 char psrcaddr[NI_MAXHOST];
1206 char pdstaddr[NI_MAXHOST];
1207 u_long srccmd, dstcmd;
1209 const char *ver = "";
1210 #ifdef NI_WITHSCOPEID
1211 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1213 const int niflag = NI_NUMERICHOST;
1216 struct in6_ifreq in6_ifr;
1220 psrcaddr[0] = pdstaddr[0] = '\0';
1223 memset(&in6_ifr, 0, sizeof(in6_ifr));
1224 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1225 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1227 srccmd = SIOCGIFPSRCADDR;
1228 dstcmd = SIOCGIFPDSTADDR;
1232 srccmd = SIOCGIFPSRCADDR_IN6;
1233 dstcmd = SIOCGIFPDSTADDR_IN6;
1234 ifrp = (struct ifreq *)&in6_ifr;
1237 srccmd = SIOCGIFPSRCADDR;
1238 dstcmd = SIOCGIFPDSTADDR;
1242 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1245 if (ifrp->ifr_addr.sa_family == AF_INET6)
1246 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1248 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1249 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1251 if (ifrp->ifr_addr.sa_family == AF_INET6)
1255 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1258 if (ifrp->ifr_addr.sa_family == AF_INET6)
1259 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1261 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1262 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1264 printf("\ttunnel inet%s %s --> %s\n", ver,
1265 psrcaddr, pdstaddr);
1269 in_status(int s __unused, struct rt_addrinfo *info)
1271 struct sockaddr_in *sin, null_sin;
1273 memset(&null_sin, 0, sizeof(null_sin));
1275 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1276 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1278 if (flags & IFF_POINTOPOINT) {
1279 /* note RTAX_BRD overlap with IFF_BROADCAST */
1280 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1283 printf("--> %s ", inet_ntoa(sin->sin_addr));
1286 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1289 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1291 if (flags & IFF_BROADCAST) {
1292 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1293 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1294 if (sin && sin->sin_addr.s_addr != 0)
1295 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1302 in6_fillscopeid(struct sockaddr_in6 *sin6)
1304 #if defined(__KAME__) && defined(KAME_SCOPEID)
1305 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1306 sin6->sin6_scope_id =
1307 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1308 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1314 in6_status(int s __unused, struct rt_addrinfo *info)
1316 struct sockaddr_in6 *sin, null_sin;
1317 struct in6_ifreq ifr6;
1320 struct in6_addrlifetime lifetime;
1321 time_t t = time(NULL);
1325 memset(&null_sin, 0, sizeof(null_sin));
1327 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1328 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1329 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1330 perror("ifconfig: socket");
1333 ifr6.ifr_addr = *sin;
1334 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1335 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1339 flags6 = ifr6.ifr_ifru.ifru_flags6;
1340 memset(&lifetime, 0, sizeof(lifetime));
1341 ifr6.ifr_addr = *sin;
1342 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1343 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1347 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1350 /* XXX: embedded link local addr check */
1351 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1352 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1355 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1356 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1357 if (sin->sin6_scope_id == 0)
1358 sin->sin6_scope_id = ntohs(index);
1360 scopeid = sin->sin6_scope_id;
1362 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1363 sizeof(addr_buf), NULL, 0,
1364 NI_NUMERICHOST|NI_WITHSCOPEID);
1366 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1368 printf("\tinet6 %s ", addr_buf);
1370 if (flags & IFF_POINTOPOINT) {
1371 /* note RTAX_BRD overlap with IFF_BROADCAST */
1372 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1374 * some of the interfaces do not have valid destination
1377 if (sin && sin->sin6_family == AF_INET6) {
1380 /* XXX: embedded link local addr check */
1381 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1382 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1385 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1386 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1387 if (sin->sin6_scope_id == 0)
1388 sin->sin6_scope_id = ntohs(index);
1391 error = getnameinfo((struct sockaddr *)sin,
1392 sin->sin6_len, addr_buf,
1393 sizeof(addr_buf), NULL, 0,
1394 NI_NUMERICHOST|NI_WITHSCOPEID);
1396 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1398 printf("--> %s ", addr_buf);
1402 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1405 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1406 sizeof(struct in6_addr)));
1408 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1410 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1411 printf("tentative ");
1412 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1413 printf("duplicated ");
1414 if ((flags6 & IN6_IFF_DETACHED) != 0)
1415 printf("detached ");
1416 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1417 printf("deprecated ");
1418 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1419 printf("autoconf ");
1420 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1421 printf("temporary ");
1424 printf("scopeid 0x%x ", scopeid);
1426 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1428 if (lifetime.ia6t_preferred) {
1429 printf("%s ", lifetime.ia6t_preferred < t
1430 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1435 if (lifetime.ia6t_expire) {
1436 printf("%s ", lifetime.ia6t_expire < t
1437 ? "0" : sec2str(lifetime.ia6t_expire - t));
1448 ipx_status(int s __unused, struct rt_addrinfo *info)
1450 struct sockaddr_ipx *sipx, null_sipx;
1452 memset(&null_sipx, 0, sizeof(null_sipx));
1454 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1455 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1457 if (flags & IFF_POINTOPOINT) {
1458 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1461 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1468 at_status(int s __unused, struct rt_addrinfo *info)
1470 struct sockaddr_at *sat, null_sat;
1471 struct netrange *nr;
1473 memset(&null_sat, 0, sizeof(null_sat));
1475 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1476 nr = &sat->sat_range.r_netrange;
1477 printf("\tatalk %d.%d range %d-%d phase %d",
1478 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1479 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1480 if (flags & IFF_POINTOPOINT) {
1481 /* note RTAX_BRD overlap with IFF_BROADCAST */
1482 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1486 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1488 if (flags & IFF_BROADCAST) {
1489 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1490 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1492 printf(" broadcast %d.%d",
1493 ntohs(sat->sat_addr.s_net),
1494 sat->sat_addr.s_node);
1502 xns_status(int s __unused, struct rt_addrinfo *info)
1504 struct sockaddr_ns *sns, null_sns;
1506 memset(&null_sns, 0, sizeof(null_sns));
1508 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1509 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1511 if (flags & IFF_POINTOPOINT) {
1512 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1515 printf("--> %s ", ns_ntoa(sns->sns_addr));
1525 link_status(int s __unused, struct rt_addrinfo *info)
1527 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1529 if (sdl->sdl_alen > 0) {
1530 if (sdl->sdl_type == IFT_ETHER &&
1531 sdl->sdl_alen == ETHER_ADDR_LEN)
1532 printf("\tether %s\n",
1533 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1535 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1537 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1543 Perror(const char *cmd)
1548 errx(1, "%s: no such interface", cmd);
1552 errx(1, "%s: permission denied", cmd);
1560 #define SIN(x) ((struct sockaddr_in *) &(x))
1561 struct sockaddr_in *sintab[] = {
1562 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1563 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1566 in_getaddr(const char *s, int which)
1568 register struct sockaddr_in *sin = sintab[which];
1572 sin->sin_len = sizeof(*sin);
1574 sin->sin_family = AF_INET;
1576 if (which == ADDR) {
1579 if((p = strrchr(s, '/')) != NULL) {
1580 /* address is `name/masklen' */
1583 struct sockaddr_in *min = sintab[MASK];
1585 ret = sscanf(p+1, "%u", &masklen);
1586 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1588 errx(1, "%s: bad value", s);
1590 min->sin_len = sizeof(*min);
1591 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1596 if (inet_aton(s, &sin->sin_addr))
1598 if ((hp = gethostbyname(s)) != 0)
1599 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1600 MIN(hp->h_length, sizeof(sin->sin_addr)));
1601 else if ((np = getnetbyname(s)) != 0)
1602 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1604 errx(1, "%s: bad value", s);
1608 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1609 struct sockaddr_in6 *sin6tab[] = {
1610 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1611 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1614 in6_getaddr(const char *s, int which)
1616 register struct sockaddr_in6 *sin = sin6tab[which];
1617 struct addrinfo hints, *res;
1622 sin->sin6_len = sizeof(*sin);
1624 sin->sin6_family = AF_INET6;
1626 if (which == ADDR) {
1628 if((p = strrchr(s, '/')) != NULL) {
1630 in6_getprefix(p + 1, MASK);
1631 explicit_prefix = 1;
1635 if (sin->sin6_family == AF_INET6) {
1636 bzero(&hints, sizeof(struct addrinfo));
1637 hints.ai_family = AF_INET6;
1638 error = getaddrinfo(s, NULL, &hints, &res);
1641 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1642 errx(1, "%s: bad value", s);
1644 bcopy(res->ai_addr, sin, res->ai_addrlen);
1648 in6_getprefix(const char *plen, int which)
1650 register struct sockaddr_in6 *sin = sin6tab[which];
1651 register u_char *cp;
1652 int len = atoi(plen);
1654 if ((len < 0) || (len > 128))
1655 errx(1, "%s: bad value", plen);
1656 sin->sin6_len = sizeof(*sin);
1658 sin->sin6_family = AF_INET6;
1659 if ((len == 0) || (len == 128)) {
1660 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1663 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1664 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1666 *cp = 0xff << (8 - len);
1671 * Print a value a la the %b format of the kernel's printf
1674 printb(const char *s, register unsigned v, register const char *bits)
1676 register int i, any = 0;
1679 if (bits && *bits == 8)
1680 printf("%s=%o", s, v);
1682 printf("%s=%x", s, v);
1686 while ((i = *bits++) != '\0') {
1687 if (v & (1 << (i-1))) {
1691 for (; (c = *bits) > 32; bits++)
1694 for (; *bits > 32; bits++)
1702 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1703 struct sockaddr_ipx *sipxtab[] = {
1704 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1705 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1708 ipx_getaddr(const char *addr, int which)
1710 struct sockaddr_ipx *sipx = sipxtab[which];
1712 sipx->sipx_family = AF_IPX;
1713 sipx->sipx_len = sizeof(*sipx);
1714 sipx->sipx_addr = ipx_addr(addr);
1716 printf("Attempt to set IPX netmask will be ineffectual\n");
1721 at_getaddr(const char *addr, int which)
1723 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1726 sat->sat_family = AF_APPLETALK;
1727 sat->sat_len = sizeof(*sat);
1729 errx(1, "AppleTalk does not use netmasks");
1730 if (sscanf(addr, "%u.%u", &net, &node) != 2
1731 || net > 0xffff || node > 0xfe)
1732 errx(1, "%s: illegal address", addr);
1733 sat->sat_addr.s_net = htons(net);
1734 sat->sat_addr.s_node = node;
1738 link_getaddr(const char *addr, int which)
1741 struct sockaddr_dl sdl;
1742 struct sockaddr *sa = &ridreq.ifr_addr;
1745 errx(1, "can't set link-level netmask or broadcast");
1746 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1747 errx(1, "malloc failed");
1749 strcpy(temp + 1, addr);
1750 sdl.sdl_len = sizeof(sdl);
1751 link_addr(temp, &sdl);
1753 if (sdl.sdl_alen > sizeof(sa->sa_data))
1754 errx(1, "malformed link-level address");
1755 sa->sa_family = AF_LINK;
1756 sa->sa_len = sdl.sdl_alen;
1757 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1760 /* XXX FIXME -- should use strtoul for better parsing. */
1762 setatrange(const char *range, int dummy __unused, int s,
1763 const struct afswtch *afp)
1765 u_short first = 123, last = 123;
1767 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1768 || first == 0 || first > 0xffff
1769 || last == 0 || last > 0xffff || first > last)
1770 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1771 at_nr.nr_firstnet = htons(first);
1772 at_nr.nr_lastnet = htons(last);
1776 setatphase(const char *phase, int dummy __unused, int s,
1777 const struct afswtch *afp)
1779 if (!strcmp(phase, "1"))
1781 else if (!strcmp(phase, "2"))
1784 errx(1, "%s: illegal phase", phase);
1788 checkatrange(struct sockaddr_at *sat)
1790 if (at_nr.nr_phase == 0)
1791 at_nr.nr_phase = 2; /* Default phase 2 */
1792 if (at_nr.nr_firstnet == 0)
1793 at_nr.nr_firstnet = /* Default range of one */
1794 at_nr.nr_lastnet = sat->sat_addr.s_net;
1795 printf("\tatalk %d.%d range %d-%d phase %d\n",
1796 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1797 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1798 if ((u_short) ntohs(at_nr.nr_firstnet) >
1799 (u_short) ntohs(sat->sat_addr.s_net)
1800 || (u_short) ntohs(at_nr.nr_lastnet) <
1801 (u_short) ntohs(sat->sat_addr.s_net))
1802 errx(1, "AppleTalk address is not in range");
1803 sat->sat_range.r_netrange = at_nr;
1807 #define SNS(x) ((struct sockaddr_ns *) &(x))
1808 struct sockaddr_ns *snstab[] = {
1809 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1810 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1813 xns_getaddr(const char *addr, int which)
1815 struct sockaddr_ns *sns = snstab[which];
1817 sns->sns_family = AF_NS;
1818 sns->sns_len = sizeof(*sns);
1819 sns->sns_addr = ns_addr(addr);
1821 printf("Attempt to set XNS netmask will be ineffectual\n");
1827 prefix(void *val, int size)
1829 register u_char *name = (u_char *)val;
1830 register int byte, bit, plen = 0;
1832 for (byte = 0; byte < size; byte++, plen += 8)
1833 if (name[byte] != 0xff)
1837 for (bit = 7; bit != 0; bit--, plen++)
1838 if (!(name[byte] & (1 << bit)))
1840 for (; bit != 0; bit--)
1841 if (name[byte] & (1 << bit))
1844 for (; byte < size; byte++)
1851 sec2str(time_t total)
1853 static char result[256];
1854 int days, hours, mins, secs;
1859 days = total / 3600 / 24;
1860 hours = (total / 3600) % 24;
1861 mins = (total / 60) % 60;
1866 p += sprintf(p, "%dd", days);
1868 if (!first || hours) {
1870 p += sprintf(p, "%dh", hours);
1872 if (!first || mins) {
1874 p += sprintf(p, "%dm", mins);
1876 sprintf(p, "%ds", secs);
1878 sprintf(result, "%lu", (unsigned long)total);
1885 ifmaybeload(char *name)
1887 struct module_stat mstat;
1889 char ifkind[35], *cp, *dp;
1892 /* turn interface and unit into module name */
1893 strcpy(ifkind, "if_");
1894 for (cp = name, dp = ifkind + 3;
1895 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1899 /* scan files in kernel */
1900 mstat.version = sizeof(struct module_stat);
1901 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1902 /* scan modules in file */
1903 for (modid = kldfirstmod(fileid); modid > 0;
1904 modid = modfnext(modid)) {
1905 if (modstat(modid, &mstat) < 0)
1907 /* strip bus name if present */
1908 if ((cp = strchr(mstat.name, '/')) != NULL) {
1913 /* already loaded? */
1914 if (strncmp(name, cp, strlen(cp)) == 0 ||
1915 strncmp(ifkind, cp, strlen(cp)) == 0)
1920 /* not present, we should try to load it */
1927 struct if_clonereq ifcr;
1932 s = socket(AF_INET, SOCK_DGRAM, 0);
1936 memset(&ifcr, 0, sizeof(ifcr));
1938 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1939 err(1, "SIOCIFGCLONERS for count");
1941 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1943 err(1, "unable to allocate cloner name buffer");
1945 ifcr.ifcr_count = ifcr.ifcr_total;
1946 ifcr.ifcr_buffer = buf;
1948 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1949 err(1, "SIOCIFGCLONERS for names");
1952 * In case some disappeared in the mean time, clamp it down.
1954 if (ifcr.ifcr_count > ifcr.ifcr_total)
1955 ifcr.ifcr_count = ifcr.ifcr_total;
1957 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1972 s = socket(AF_INET, SOCK_DGRAM, 0);
1976 memset(&ifr, 0, sizeof(ifr));
1977 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1978 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
1979 err(1, "SIOCIFCREATE");
1982 * If we get a different name back then we put in, we probably
1983 * want to print it out, but we might change our mind later so
1984 * we just signal our intrest and leave the printout for later.
1986 if (strcmp(name, ifr.ifr_name) != 0) {
1988 strlcpy(name, ifr.ifr_name, sizeof(name));
1995 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
1998 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1999 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
2000 err(1, "SIOCIFDESTROY");
2002 * If we create and destroy an interface in the same command,
2003 * there isn't any reason to print it's name.