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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94
31 * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.96 2004/02/27 06:43:14 kan Exp $
32 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.23 2005/05/25 14:58:32 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/sysctl.h>
41 #include <sys/module.h>
42 #include <sys/linker.h>
44 #include <net/ethernet.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <arpa/inet.h>
58 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
65 #include <netipx/ipx.h>
66 #include <netipx/ipx_if.h>
70 #include <netatalk/at.h>
76 #include <netns/ns_if.h>
93 /* wrapper for KAME-special getnameinfo() */
94 #ifndef NI_WITHSCOPEID
95 #define NI_WITHSCOPEID 0
99 * Since "struct ifreq" is composed of various union members, callers
100 * should pay special attention to interprete the value.
101 * (.e.g. little/big endian difference in the structure.)
103 struct ifreq ifr, ridreq;
104 struct ifaliasreq addreq;
106 struct in6_ifreq in6_ridreq;
107 struct in6_aliasreq in6_addreq =
109 { 0, 0, 0, 0, { { { 0 } } }, 0 },
110 { 0, 0, 0, 0, { { { 0 } } }, 0 },
111 { 0, 0, 0, 0, { { { 0 } } }, 0 },
113 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
115 struct sockaddr_in netmask;
116 struct netrange at_nr; /* AppleTalk net range */
127 static int ip6lifetime;
134 int printname = 0; /* Print the name of the created interface. */
137 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
140 void Perror(const char *cmd);
141 void checkatrange(struct sockaddr_at *);
142 int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
143 void notealias(const char *, int, int, const struct afswtch *afp);
144 void list_cloners(void);
145 void printb(const char *s, unsigned value, const char *bits);
146 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
147 void status(const struct afswtch *afp, int addrcount,
148 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
149 struct ifa_msghdr *ifam);
150 void tunnel_status(int s);
152 void ifmaybeload(char *if_nm);
155 void in6_fillscopeid(struct sockaddr_in6 *sin6);
156 int prefix(void *, int);
157 static char *sec2str(time_t);
158 int explicit_prefix = 0;
161 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
162 typedef void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp);
163 c_func setatphase, setatrange;
164 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
168 c_func setifprefixlen;
172 c_func2 setip6lifetime;
176 c_func setifflags, setifmetric, setifmtu, setifcap;
177 c_func clone_destroy;
181 void clone_create(void);
184 #define NEXTARG 0xffffff
185 #define NEXTARG2 0xfffffe
190 int c_parameter; /* NEXTARG means next argv */
191 void (*c_func)(const char *, int, int, const struct afswtch *afp);
192 void (*c_func2)(const char *, const char *, int, const struct afswtch *afp);
194 { "up", IFF_UP, setifflags, NULL },
195 { "down", -IFF_UP, setifflags, NULL },
196 { "polling", IFF_POLLING, setifflags, NULL },
197 { "-polling", -IFF_POLLING, setifflags, NULL },
198 { "arp", -IFF_NOARP, setifflags, NULL },
199 { "-arp", IFF_NOARP, setifflags, NULL },
200 { "debug", IFF_DEBUG, setifflags, NULL },
201 { "-debug", -IFF_DEBUG, setifflags, NULL },
202 { "promisc", IFF_PPROMISC, setifflags, NULL },
203 { "-promisc", -IFF_PPROMISC, setifflags, NULL },
204 { "add", IFF_UP, notealias, NULL },
205 { "alias", IFF_UP, notealias, NULL },
206 { "-alias", -IFF_UP, notealias, NULL },
207 { "delete", -IFF_UP, notealias, NULL },
208 { "remove", -IFF_UP, notealias, NULL },
210 #define EN_SWABIPS 0x1000
211 { "swabips", EN_SWABIPS, setifflags, NULL },
212 { "-swabips", -EN_SWABIPS, setifflags, NULL },
214 { "netmask", NEXTARG, setifnetmask, NULL },
216 { "prefixlen", NEXTARG, setifprefixlen, NULL },
217 { "anycast", IN6_IFF_ANYCAST, setip6flags, NULL },
218 { "tentative", IN6_IFF_TENTATIVE, setip6flags, NULL },
219 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags, NULL },
220 { "deprecated", IN6_IFF_DEPRECATED, setip6flags, NULL },
221 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags, NULL },
222 { "autoconf", IN6_IFF_AUTOCONF, setip6flags, NULL },
223 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags, NULL },
224 { "pltime", NEXTARG, setip6pltime, NULL },
225 { "vltime", NEXTARG, setip6vltime, NULL },
226 { "eui64", 0, setip6eui64, NULL },
228 { "range", NEXTARG, setatrange, NULL },
229 { "phase", NEXTARG, setatphase, NULL },
230 { "metric", NEXTARG, setifmetric, NULL },
231 { "broadcast", NEXTARG, setifbroadaddr, NULL },
232 { "ipdst", NEXTARG, setifipdst, NULL },
233 { "tunnel", NEXTARG2, NULL, settunnel },
234 { "deletetunnel", 0, deletetunnel, NULL },
235 { "link0", IFF_LINK0, setifflags, NULL },
236 { "-link0", -IFF_LINK0, setifflags, NULL },
237 { "link1", IFF_LINK1, setifflags, NULL },
238 { "-link1", -IFF_LINK1, setifflags, NULL },
239 { "link2", IFF_LINK2, setifflags, NULL },
240 { "-link2", -IFF_LINK2, setifflags, NULL },
242 { "monitor", IFF_MONITOR, setifflags, NULL },
243 { "-monitor", -IFF_MONITOR, setifflags, NULL },
244 { "staticarp", IFF_STATICARP, setifflags, NULL },
245 { "-staticarp", -IFF_STATICARP, setifflags, NULL },
249 { "media", NEXTARG, setmedia, NULL },
250 { "mode", NEXTARG, setmediamode, NULL },
251 { "mediaopt", NEXTARG, setmediaopt, NULL },
252 { "-mediaopt", NEXTARG, unsetmediaopt, NULL },
255 { "vlan", NEXTARG, setvlantag, NULL },
256 { "vlandev", NEXTARG, setvlandev, NULL },
257 { "-vlandev", NEXTARG, unsetvlandev, NULL },
260 /* XXX `create' special-cased below */
261 {"create", 0, clone_create, NULL },
262 {"plumb", 0, clone_create, NULL },
264 {"destroy", 0, clone_destroy, NULL },
265 {"unplumb", 0, clone_destroy, NULL },
267 { "ssid", NEXTARG, set80211ssid, NULL },
268 { "nwid", NEXTARG, set80211ssid, NULL },
269 { "stationname", NEXTARG, set80211stationname, NULL },
270 { "station", NEXTARG, set80211stationname, NULL }, /* BSD/OS */
271 { "channel", NEXTARG, set80211channel, NULL },
272 { "authmode", NEXTARG, set80211authmode, NULL },
273 { "powersavemode", NEXTARG, set80211powersavemode, NULL },
274 { "powersave", 1, set80211powersave, NULL },
275 { "-powersave", 0, set80211powersave, NULL },
276 { "powersavesleep", NEXTARG, set80211powersavesleep, NULL },
277 { "wepmode", NEXTARG, set80211wepmode, NULL },
278 { "wep", 1, set80211wep, NULL },
279 { "-wep", 0, set80211wep, NULL },
280 { "weptxkey", NEXTARG, set80211weptxkey, NULL },
281 { "wepkey", NEXTARG, set80211wepkey, NULL },
282 { "nwkey", NEXTARG, set80211nwkey, NULL }, /* NetBSD */
283 { "-nwkey", 0, set80211wep, NULL }, /* NetBSD */
284 { "rtsthreshold",NEXTARG, set80211rtsthreshold, NULL },
285 { "protmode", NEXTARG, set80211protmode, NULL },
286 { "txpower", NEXTARG, set80211txpower, NULL },
288 { "rxcsum", IFCAP_RXCSUM, setifcap, NULL },
289 { "-rxcsum", -IFCAP_RXCSUM, setifcap, NULL },
290 { "txcsum", IFCAP_TXCSUM, setifcap, NULL },
291 { "-txcsum", -IFCAP_TXCSUM, setifcap, NULL },
292 { "netcons", IFCAP_NETCONS, setifcap, NULL },
293 { "-netcons", -IFCAP_NETCONS, setifcap, NULL },
294 { "vlanmtu", IFCAP_VLAN_MTU, setifcap, NULL },
295 { "-vlanmtu", -IFCAP_VLAN_MTU, setifcap, NULL },
296 { "vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap, NULL },
297 { "-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap, NULL },
298 { "normal", -IFF_LINK0, setifflags, NULL },
299 { "compress", IFF_LINK0, setifflags, NULL },
300 { "noicmp", IFF_LINK1, setifflags, NULL },
301 { "mtu", NEXTARG, setifmtu, NULL },
302 { "name", NEXTARG, setifname, NULL },
303 { NULL, 0, setifaddr, NULL },
304 { NULL, 0, setifdstaddr, NULL },
308 * XNS support liberally adapted from code written at the University of
309 * Maryland principally by James O'Toole and Chris Torek.
311 typedef void af_status(int, struct rt_addrinfo *);
312 typedef void af_getaddr(const char *, int);
313 typedef void af_getprefix(const char *, int);
315 af_status in_status, at_status, link_status;
316 af_getaddr in_getaddr, at_getaddr, link_getaddr;
319 af_status ipx_status;
320 af_getaddr ipx_getaddr;
324 af_status in6_status;
325 af_getaddr in6_getaddr;
326 af_getprefix in6_getprefix;
329 af_status xns_status;
330 af_getaddr xns_getaddr;
333 /* Known address families */
338 af_status *af_status;
339 af_getaddr *af_getaddr;
340 af_getprefix *af_getprefix;
346 #define C(x) ((caddr_t) &x)
347 { "inet", AF_INET, in_status, in_getaddr, NULL,
348 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
350 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
351 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
352 C(in6_ridreq), C(in6_addreq) },
355 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
356 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
358 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
359 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
361 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
362 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
364 { "link", AF_LINK, link_status, link_getaddr, NULL,
365 0, SIOCSIFLLADDR, NULL, C(ridreq) },
366 { "ether", AF_LINK, link_status, link_getaddr, NULL,
367 0, SIOCSIFLLADDR, NULL, C(ridreq) },
368 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
369 0, SIOCSIFLLADDR, NULL, C(ridreq) },
370 { NULL, 0, NULL, NULL, NULL, 0, 0, NULL, NULL }
374 * Expand the compacted form of addresses as returned via the
375 * configuration read via sysctl().
379 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
380 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
383 rt_xaddrs(caddr_t cp, caddr_t cplim, 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]");
424 main(int argc, char * const *argv)
427 int all, namesonly, downonly, uponly;
428 int need_nl = 0, count = 0;
429 const struct afswtch *afp = 0;
430 int addrcount, ifindex;
431 struct if_msghdr *ifm, *nextifm;
432 struct ifa_msghdr *ifam;
433 struct sockaddr_dl *sdl;
434 char *buf, *lim, *next;
439 /* Parse leading line options */
440 all = downonly = uponly = namesonly = 0;
441 while ((c = getopt(argc, argv, "adlmuC"
447 case 'a': /* scan all interfaces */
450 case 'd': /* restrict scan to "down" interfaces */
453 case 'l': /* scan interface names only */
456 case 'm': /* show media choices in status */
459 case 'u': /* restrict scan to "up" interfaces */
467 ip6lifetime++; /* print IPv6 address lifetime */
479 /* -C must be solitary */
480 if (all || supmedia || uponly || downonly || namesonly ||
488 /* -l cannot be used with -a or -m */
489 if (namesonly && (all || supmedia))
493 if (uponly && downonly)
496 /* no arguments is equivalent to '-a' */
497 if (!namesonly && argc < 1)
500 /* -a and -l allow an address family arg to limit the output */
501 if (all || namesonly) {
507 for (afp = afs; afp->af_name; afp++)
508 if (strcmp(afp->af_name, *argv) == 0) {
512 if (afp->af_name == NULL)
514 /* leave with afp non-zero */
517 /* not listing, need an argument */
521 strncpy(name, *argv, sizeof(name));
524 /* check and maybe load support for this interface */
528 * NOTE: We must special-case the `create' command right
529 * here as we would otherwise fail when trying to find
532 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
533 strcmp(argv[0], "plumb") == 0)) {
539 ifindex = if_nametoindex(name);
541 errx(1, "interface %s does not exist", name);
544 /* Check for address family */
546 for (afp = afs; afp->af_name; afp++)
547 if (strcmp(afp->af_name, *argv) == 0) {
551 if (afp->af_name == NULL)
552 afp = NULL; /* not a family, NULL */
559 mib[3] = 0; /* address family */
560 mib[4] = NET_RT_IFLIST;
561 mib[5] = ifindex; /* interface index */
563 /* if particular family specified, only ask about it */
567 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
568 errx(1, "iflist-sysctl-estimate");
569 if ((buf = malloc(needed)) == NULL)
571 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
572 if (errno == ENOMEM && count++ < 10) {
573 warnx("Routing table grew, retrying");
578 errx(1, "actual retrieval of interface table");
585 ifm = (struct if_msghdr *)next;
587 if (ifm->ifm_type == RTM_IFINFO) {
589 if (ifm->ifm_data.ifi_datalen == 0)
590 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
591 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
592 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
594 sdl = (struct sockaddr_dl *)(ifm + 1);
596 flags = ifm->ifm_flags;
598 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
599 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
601 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
602 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
607 next += ifm->ifm_msglen;
612 nextifm = (struct if_msghdr *)next;
614 if (nextifm->ifm_type != RTM_NEWADDR)
618 ifam = (struct ifa_msghdr *)nextifm;
621 next += nextifm->ifm_msglen;
623 memcpy(name, sdl->sdl_data,
624 sizeof(name) < sdl->sdl_nlen ?
625 sizeof(name)-1 : sdl->sdl_nlen);
626 name[sizeof(name) < sdl->sdl_nlen ?
627 sizeof(name)-1 : sdl->sdl_nlen] = '\0';
629 if (all || namesonly) {
632 /* sdl_data may not be terminated, don't use strlcpy */
633 if ((len = sdl->sdl_nlen) > sizeof(name) - 1)
634 len = sizeof(name) - 1;
635 bcopy(sdl->sdl_data, name, len);
639 if ((flags & IFF_UP) == 0)
640 continue; /* not up */
643 continue; /* not down */
646 afp->af_status != link_status ||
647 sdl->sdl_type == IFT_ETHER) {
658 ifconfig(argc, argv, afp);
660 status(afp, addrcount, sdl, ifm, ifam);
664 if (namesonly && need_nl > 0)
668 printf("%s\n", name);
675 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
681 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
682 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
684 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
690 for (p = cmds; p->c_name; p++)
691 if (strcmp(*argv, p->c_name) == 0)
693 if (p->c_name == 0 && setaddr)
694 p++; /* got src, do dst */
695 if (p->c_func || p->c_func2) {
696 if (p->c_parameter == NEXTARG) {
698 errx(1, "'%s' requires argument",
700 (*p->c_func)(argv[1], 0, s, afp);
702 } else if (p->c_parameter == NEXTARG2) {
704 errx(1, "'%s' requires 2 arguments",
706 (*p->c_func2)(argv[1], argv[2], s, afp);
707 argc -= 2, argv += 2;
709 (*p->c_func)(*argv, p->c_parameter, s, afp);
714 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
715 /* Aggregatable address architecture defines all prefixes
716 are 64. So, it is convenient to set prefixlen to 64 if
717 it is not specified. */
718 setifprefixlen("64", 0, s, afp);
719 /* in6_getprefix("64", MASK) if MASK is available here... */
723 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
725 int size = sizeof(rq);
727 rq.rq_ipx = addreq.ifra_addr;
728 rq.rq_ip = addreq.ifra_dstaddr;
730 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
731 Perror("Encapsulation Routing");
734 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
735 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
737 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
739 int size = sizeof(rq);
741 rq.rq_ns = addreq.ifra_addr;
742 rq.rq_ip = addreq.ifra_dstaddr;
744 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
745 Perror("Encapsulation Routing");
749 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
750 warnx("interface %s cannot change %s addresses!",
757 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
758 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
759 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
760 /* means no previous address for interface */
762 Perror("ioctl (SIOCDIFADDR)");
766 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
767 warnx("interface %s cannot change %s addresses!",
772 if (newaddr && (setaddr || setmask)) {
773 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
774 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
775 Perror("ioctl (SIOCAIFADDR)");
787 setifaddr(const char *addr, int param __unused, int s __unused,
788 const struct afswtch *afp)
790 if (*afp->af_getaddr == NULL)
793 * Delay the ioctl to set the interface addr until flags are all set.
794 * The address interpretation may depend on the flags,
795 * and the flags may change when the address is set.
798 if (doalias == 0 && afp->af_af != AF_LINK)
800 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
804 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
806 struct addrinfo hints, *srcres, *dstres;
807 struct ifaliasreq addr_req;
810 struct in6_aliasreq in6_addr_req;
813 memset(&hints, 0, sizeof(hints));
814 hints.ai_family = afp->af_af;
816 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
817 errx(1, "error in parsing address string: %s",
818 gai_strerror(ecode));
820 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
821 errx(1, "error in parsing address string: %s",
822 gai_strerror(ecode));
824 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
826 "source and destination address families do not match");
828 switch (srcres->ai_addr->sa_family) {
830 memset(&addr_req, 0, sizeof(addr_req));
831 strncpy(addr_req.ifra_name, name, IFNAMSIZ);
832 memcpy(&addr_req.ifra_addr, srcres->ai_addr,
833 srcres->ai_addr->sa_len);
834 memcpy(&addr_req.ifra_dstaddr, dstres->ai_addr,
835 dstres->ai_addr->sa_len);
837 if (ioctl(s, SIOCSIFPHYADDR, &addr_req) < 0)
838 warn("SIOCSIFPHYADDR");
843 memset(&in6_addr_req, 0, sizeof(in6_addr_req));
844 strncpy(in6_addr_req.ifra_name, name, IFNAMSIZ);
845 memcpy(&in6_addr_req.ifra_addr, srcres->ai_addr,
846 srcres->ai_addr->sa_len);
847 memcpy(&in6_addr_req.ifra_dstaddr, dstres->ai_addr,
848 dstres->ai_addr->sa_len);
850 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addr_req) < 0)
851 warn("SIOCSIFPHYADDR_IN6");
856 warn("address family not supported");
859 freeaddrinfo(srcres);
860 freeaddrinfo(dstres);
865 deletetunnel(const char *vname __unused, int param __unused, int s,
866 const struct afswtch *afp __unused)
868 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
869 err(1, "SIOCDIFPHYADDR");
873 setifnetmask(const char *addr, int dummy __unused, int s __unused,
874 const struct afswtch *afp)
876 if (*afp->af_getaddr == NULL)
879 (*afp->af_getaddr)(addr, MASK);
884 setifprefixlen(const char *addr, int dummy __unused, int s __unused,
885 const struct afswtch *afp)
887 if (*afp->af_getprefix)
888 (*afp->af_getprefix)(addr, MASK);
893 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
894 const struct afswtch *afp)
896 if (afp->af_af != AF_INET6)
897 err(1, "address flags can be set only for inet6 addresses");
900 in6_addreq.ifra_flags &= ~(-flag);
902 in6_addreq.ifra_flags |= flag;
906 setip6pltime(const char *seconds, int dummy __unused, int s,
907 const struct afswtch *afp)
909 setip6lifetime("pltime", seconds, s, afp);
913 setip6vltime(const char *seconds, int dummy __unused, int s,
914 const struct afswtch *afp)
916 setip6lifetime("vltime", seconds, s, afp);
920 setip6lifetime(const char *cmd, const char *val, int s __unused,
921 const struct afswtch *afp)
927 newval = (time_t)strtoul(val, &ep, 0);
929 errx(1, "invalid %s", cmd);
930 if (afp->af_af != AF_INET6)
931 errx(1, "%s not allowed for the AF", cmd);
932 if (strcmp(cmd, "vltime") == 0) {
933 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
934 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
935 } else if (strcmp(cmd, "pltime") == 0) {
936 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
937 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
942 setip6eui64(const char *cmd, int dummy __unused, int s __unused,
943 const struct afswtch *afp)
945 struct ifaddrs *ifap, *ifa;
946 const struct sockaddr_in6 *sin6 = NULL;
947 const struct in6_addr *lladdr = NULL;
948 struct in6_addr *in6;
950 if (afp->af_af != AF_INET6)
951 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
952 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
953 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
954 errx(EXIT_FAILURE, "interface index is already filled");
955 if (getifaddrs(&ifap) != 0)
956 err(EXIT_FAILURE, "getifaddrs");
957 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
958 if (ifa->ifa_addr->sa_family == AF_INET6 &&
959 strcmp(ifa->ifa_name, name) == 0) {
960 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
961 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
962 lladdr = &sin6->sin6_addr;
968 errx(EXIT_FAILURE, "could not determine link local address");
970 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
977 setifbroadaddr(const char *addr, int dummy __unused, int s __unused,
978 const struct afswtch *afp)
980 if (*afp->af_getaddr == NULL)
982 (*afp->af_getaddr)(addr, DSTADDR);
986 setifipdst(const char *addr, int dummy __unused, int s __unused,
987 const struct afswtch *afp __unused)
989 in_getaddr(addr, DSTADDR);
994 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
997 notealias(const char *addr __unused, int param, int s __unused,
998 const struct afswtch *afp)
1000 if (setaddr && doalias == 0 && param < 0)
1001 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1002 bcopy((caddr_t)rqtosa(af_addreq),
1003 (caddr_t)rqtosa(af_ridreq),
1004 rqtosa(af_addreq)->sa_len);
1015 setifdstaddr(const char *addr, int param __unused, int s __unused,
1016 const struct afswtch *afp)
1018 if (*afp->af_getaddr == NULL)
1020 (*afp->af_getaddr)(addr, DSTADDR);
1024 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1025 * of the ifreq structure, which may confuse other parts of ifconfig.
1026 * Make a private copy so we can avoid that.
1029 setifflags(const char *vname, int value, int s,
1030 const struct afswtch *afp __unused)
1032 struct ifreq my_ifr;
1034 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1036 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1037 Perror("ioctl (SIOCGIFFLAGS)");
1040 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1041 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1048 my_ifr.ifr_flags = flags & 0xffff;
1049 my_ifr.ifr_flagshigh = flags >> 16;
1050 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1055 setifcap(const char *vname, int value, int s,
1056 const struct afswtch *afp __unused)
1059 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1060 Perror("ioctl (SIOCGIFCAP)");
1063 flags = ifr.ifr_curcap;
1069 ifr.ifr_reqcap = flags;
1070 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1075 setifmetric(const char *val, int dummy __unused, int s,
1076 const struct afswtch *afp __unused)
1078 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1079 ifr.ifr_metric = atoi(val);
1080 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1081 warn("ioctl (set metric)");
1085 setifmtu(const char *val, int dummy __unused, int s,
1086 const struct afswtch *afp __unused)
1088 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1089 ifr.ifr_mtu = atoi(val);
1090 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1091 warn("ioctl (set mtu)");
1095 setifname(const char *val, int dummy __unused, int s,
1096 const struct afswtch *afp __unused)
1100 newname = strdup(val);
1102 ifr.ifr_data = newname;
1103 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1104 warn("ioctl (set name)");
1108 strlcpy(name, newname, sizeof(name));
1113 * Even if we just created the interface, we don't need to print
1114 * its name because we just nailed it down separately.
1120 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1121 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1122 "\20MULTICAST\21POLLING\23MONITOR\24STATICARP"
1125 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING"
1128 * Print the status of the interface. If an address family was
1129 * specified, show it and it only; otherwise, show them all.
1132 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1133 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1135 const struct afswtch *p = NULL;
1136 struct rt_addrinfo info;
1146 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1147 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1149 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1152 printf("flags %08x\n", flags);
1153 printf("%s: ", name);
1154 printb("flags", flags, IFFBITS);
1155 if (ifm->ifm_data.ifi_metric)
1156 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1157 if (ifm->ifm_data.ifi_mtu)
1158 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1161 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1162 if (ifr.ifr_curcap != 0) {
1163 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1166 if (supmedia && ifr.ifr_reqcap != 0) {
1167 printf("\tcapability list:\n");
1168 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1175 while (addrcount > 0) {
1177 info.rti_addrs = ifam->ifam_addrs;
1179 /* Expand the compacted addresses */
1180 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1184 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1186 (*p->af_status)(s, &info);
1188 } else for (p = afs; p->af_name; p++) {
1189 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1190 (*p->af_status)(s, &info);
1193 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1195 if (allfamilies || afp->af_status == link_status)
1196 link_status(s, (struct rt_addrinfo *)sdl);
1198 if (allfamilies || afp->af_status == media_status)
1199 media_status(s, NULL);
1202 if (allfamilies || afp->af_status == vlan_status)
1203 vlan_status(s, NULL);
1205 #ifdef USE_IEEE80211
1206 if (allfamilies || afp->af_status == ieee80211_status)
1207 ieee80211_status(s, NULL);
1209 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1210 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1211 printf("%s", ifs.ascii);
1213 if (!allfamilies && !p &&
1215 afp->af_status != media_status &&
1217 afp->af_status != link_status
1219 && afp->af_status != vlan_status
1222 warnx("%s has no %s interface address!", name, afp->af_name);
1229 tunnel_status(int s)
1231 char psrcaddr[NI_MAXHOST];
1232 char pdstaddr[NI_MAXHOST];
1233 u_long srccmd, dstcmd;
1235 const char *ver = "";
1236 #ifdef NI_WITHSCOPEID
1237 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1239 const int niflag = NI_NUMERICHOST;
1242 struct in6_ifreq in6_ifr;
1246 psrcaddr[0] = pdstaddr[0] = '\0';
1249 memset(&in6_ifr, 0, sizeof(in6_ifr));
1250 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1251 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1253 srccmd = SIOCGIFPSRCADDR;
1254 dstcmd = SIOCGIFPDSTADDR;
1258 srccmd = SIOCGIFPSRCADDR_IN6;
1259 dstcmd = SIOCGIFPDSTADDR_IN6;
1260 ifrp = (struct ifreq *)&in6_ifr;
1263 srccmd = SIOCGIFPSRCADDR;
1264 dstcmd = SIOCGIFPDSTADDR;
1268 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1270 #if defined(INET6) && defined(__KAME__) && defined(KAME_SCOPEID)
1271 if (ifrp->ifr_addr.sa_family == AF_INET6)
1272 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1274 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1275 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1277 if (ifrp->ifr_addr.sa_family == AF_INET6)
1281 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1283 #if defined(INET6) && defined(__KAME__) && defined(KAME_SCOPEID)
1284 if (ifrp->ifr_addr.sa_family == AF_INET6)
1285 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1287 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1288 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1290 printf("\ttunnel inet%s %s --> %s\n", ver,
1291 psrcaddr, pdstaddr);
1295 in_status(int s __unused, struct rt_addrinfo *info)
1297 struct sockaddr_in *addr_in, null_in;
1299 memset(&null_in, 0, sizeof(null_in));
1301 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1302 printf("\tinet %s ", inet_ntoa(addr_in->sin_addr));
1304 if (flags & IFF_POINTOPOINT) {
1305 /* note RTAX_BRD overlap with IFF_BROADCAST */
1306 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1309 printf("--> %s ", inet_ntoa(addr_in->sin_addr));
1312 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1315 printf("netmask 0x%lx ",
1316 (unsigned long)ntohl(addr_in->sin_addr.s_addr));
1318 if (flags & IFF_BROADCAST) {
1319 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1320 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1321 if (addr_in && addr_in->sin_addr.s_addr != 0)
1322 printf("broadcast %s", inet_ntoa(addr_in->sin_addr));
1328 #if defined(__KAME__) && defined(KAME_SCOPEID)
1330 in6_fillscopeid(struct sockaddr_in6 *sin6 __unused)
1332 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1333 sin6->sin6_scope_id =
1334 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1335 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1338 #endif /* defined(__KAME__) && defined(KAME_SCOPEID) */
1341 in6_status(int s __unused, struct rt_addrinfo *info)
1343 struct sockaddr_in6 *addr_in, null_in;
1344 struct in6_ifreq ifr6;
1347 struct in6_addrlifetime lifetime;
1348 time_t t = time(NULL);
1352 memset(&null_in, 0, sizeof(null_in));
1354 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1355 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1356 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1357 perror("ifconfig: socket");
1360 ifr6.ifr_addr = *addr_in;
1361 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1362 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1366 flags6 = ifr6.ifr_ifru.ifru_flags6;
1367 memset(&lifetime, 0, sizeof(lifetime));
1368 ifr6.ifr_addr = *addr_in;
1369 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1370 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1374 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1377 /* XXX: embedded link local addr check */
1378 if (IN6_IS_ADDR_LINKLOCAL(&addr_in->sin6_addr) &&
1379 *(u_short *)&addr_in->sin6_addr.s6_addr[2] != 0) {
1382 idx = *(u_short *)&addr_in->sin6_addr.s6_addr[2];
1383 *(u_short *)&addr_in->sin6_addr.s6_addr[2] = 0;
1384 if (addr_in->sin6_scope_id == 0)
1385 addr_in->sin6_scope_id = ntohs(idx);
1387 scopeid = addr_in->sin6_scope_id;
1389 error = getnameinfo((struct sockaddr *)addr_in, addr_in->sin6_len, addr_buf,
1390 sizeof(addr_buf), NULL, 0,
1391 NI_NUMERICHOST|NI_WITHSCOPEID);
1393 inet_ntop(AF_INET6, &addr_in->sin6_addr, addr_buf,
1395 printf("\tinet6 %s ", addr_buf);
1397 if (flags & IFF_POINTOPOINT) {
1398 /* note RTAX_BRD overlap with IFF_BROADCAST */
1399 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1401 * some of the interfaces do not have valid destination
1404 if (addr_in && addr_in->sin6_family == AF_INET6) {
1405 /* XXX: embedded link local addr check */
1406 if (IN6_IS_ADDR_LINKLOCAL(&addr_in->sin6_addr) &&
1407 *(u_short *)&addr_in->sin6_addr.s6_addr[2] != 0) {
1410 idx = *(u_short *)&addr_in->sin6_addr.s6_addr[2];
1411 *(u_short *)&addr_in->sin6_addr.s6_addr[2] = 0;
1412 if (addr_in->sin6_scope_id == 0)
1413 addr_in->sin6_scope_id = ntohs(idx);
1416 error = getnameinfo((struct sockaddr *)addr_in,
1417 addr_in->sin6_len, addr_buf,
1418 sizeof(addr_buf), NULL, 0,
1419 NI_NUMERICHOST|NI_WITHSCOPEID);
1421 inet_ntop(AF_INET6, &addr_in->sin6_addr, addr_buf,
1423 printf("--> %s ", addr_buf);
1427 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1430 printf("prefixlen %d ", prefix(&addr_in->sin6_addr,
1431 sizeof(struct in6_addr)));
1433 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1435 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1436 printf("tentative ");
1437 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1438 printf("duplicated ");
1439 if ((flags6 & IN6_IFF_DETACHED) != 0)
1440 printf("detached ");
1441 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1442 printf("deprecated ");
1443 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1444 printf("autoconf ");
1445 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1446 printf("temporary ");
1449 printf("scopeid 0x%x ", scopeid);
1451 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1453 if (lifetime.ia6t_preferred) {
1454 printf("%s ", lifetime.ia6t_preferred < t
1455 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1460 if (lifetime.ia6t_expire) {
1461 printf("%s ", lifetime.ia6t_expire < t
1462 ? "0" : sec2str(lifetime.ia6t_expire - t));
1473 ipx_status(int s __unused, struct rt_addrinfo *info)
1475 struct sockaddr_ipx *sipx, null_sipx;
1477 memset(&null_sipx, 0, sizeof(null_sipx));
1479 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1480 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1482 if (flags & IFF_POINTOPOINT) {
1483 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1486 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1493 at_status(int s __unused, struct rt_addrinfo *info)
1495 struct sockaddr_at *sat, null_sat;
1496 struct netrange *nr;
1498 memset(&null_sat, 0, sizeof(null_sat));
1500 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1501 nr = &sat->sat_range.r_netrange;
1502 printf("\tatalk %d.%d range %d-%d phase %d",
1503 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1504 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1505 if (flags & IFF_POINTOPOINT) {
1506 /* note RTAX_BRD overlap with IFF_BROADCAST */
1507 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1511 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1513 if (flags & IFF_BROADCAST) {
1514 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1515 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1517 printf(" broadcast %d.%d",
1518 ntohs(sat->sat_addr.s_net),
1519 sat->sat_addr.s_node);
1527 xns_status(int s __unused, struct rt_addrinfo *info)
1529 struct sockaddr_ns *sns, null_sns;
1531 memset(&null_sns, 0, sizeof(null_sns));
1533 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1534 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1536 if (flags & IFF_POINTOPOINT) {
1537 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1540 printf("--> %s ", ns_ntoa(sns->sns_addr));
1550 link_status(int s __unused, struct rt_addrinfo *info)
1552 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1554 if (sdl->sdl_alen > 0) {
1555 if (sdl->sdl_type == IFT_ETHER &&
1556 sdl->sdl_alen == ETHER_ADDR_LEN)
1557 printf("\tether %s\n",
1558 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1560 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1562 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1568 Perror(const char *cmd)
1573 errx(1, "%s: no such interface", cmd);
1577 errx(1, "%s: permission denied", cmd);
1585 #define SIN(x) ((struct sockaddr_in *) &(x))
1586 struct sockaddr_in *sintab[] = {
1587 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1588 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1591 in_getaddr(const char *s, int which)
1593 struct sockaddr_in *addr_in = sintab[which];
1597 addr_in->sin_len = sizeof(*addr_in);
1599 addr_in->sin_family = AF_INET;
1601 if (which == ADDR) {
1604 if((p = strrchr(s, '/')) != NULL) {
1605 /* address is `name/masklen' */
1608 struct sockaddr_in *min = sintab[MASK];
1610 ret = sscanf(p+1, "%u", &masklen);
1611 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1613 errx(1, "%s: bad value", s);
1615 min->sin_len = sizeof(*min);
1616 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1621 if (inet_aton(s, &addr_in->sin_addr))
1623 if ((hp = gethostbyname(s)) != 0)
1624 bcopy(hp->h_addr, (char *)&addr_in->sin_addr,
1625 MIN((size_t)hp->h_length, sizeof(addr_in->sin_addr)));
1626 else if ((np = getnetbyname(s)) != 0)
1627 addr_in->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1629 errx(1, "%s: bad value", s);
1633 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1634 struct sockaddr_in6 *sin6tab[] = {
1635 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1636 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1639 in6_getaddr(const char *s, int which)
1641 struct sockaddr_in6 *addr_in = sin6tab[which];
1642 struct addrinfo hints, *res;
1647 addr_in->sin6_len = sizeof(*addr_in);
1649 addr_in->sin6_family = AF_INET6;
1651 if (which == ADDR) {
1653 if((p = strrchr(s, '/')) != NULL) {
1655 in6_getprefix(p + 1, MASK);
1656 explicit_prefix = 1;
1660 if (addr_in->sin6_family == AF_INET6) {
1661 bzero(&hints, sizeof(struct addrinfo));
1662 hints.ai_family = AF_INET6;
1663 error = getaddrinfo(s, NULL, &hints, &res);
1666 if (inet_pton(AF_INET6, s, &addr_in->sin6_addr) != 1)
1667 errx(1, "%s: bad value", s);
1669 bcopy(res->ai_addr, addr_in, res->ai_addrlen);
1673 in6_getprefix(const char *plen, int which)
1675 struct sockaddr_in6 *addr_in = sin6tab[which];
1677 int len = atoi(plen);
1679 if ((len < 0) || (len > 128))
1680 errx(1, "%s: bad value", plen);
1681 addr_in->sin6_len = sizeof(*addr_in);
1683 addr_in->sin6_family = AF_INET6;
1684 if ((len == 0) || (len == 128)) {
1685 memset(&addr_in->sin6_addr, 0xff, sizeof(struct in6_addr));
1688 memset((void *)&addr_in->sin6_addr, 0x00, sizeof(addr_in->sin6_addr));
1689 for (cp = (u_char *)&addr_in->sin6_addr; len > 7; len -= 8)
1691 *cp = 0xff << (8 - len);
1696 * Print a value a la the %b format of the kernel's printf
1699 printb(const char *s, unsigned v, const char *bits)
1704 if (bits && *bits == 8)
1705 printf("%s=%o", s, v);
1707 printf("%s=%x", s, v);
1711 while ((i = *bits++) != '\0') {
1712 if (v & (1 << (i-1))) {
1716 for (; (c = *bits) > 32; bits++)
1719 for (; *bits > 32; bits++)
1727 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1728 struct sockaddr_ipx *sipxtab[] = {
1729 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1730 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1733 ipx_getaddr(const char *addr, int which)
1735 struct sockaddr_ipx *sipx = sipxtab[which];
1737 sipx->sipx_family = AF_IPX;
1738 sipx->sipx_len = sizeof(*sipx);
1739 sipx->sipx_addr = ipx_addr(addr);
1741 printf("Attempt to set IPX netmask will be ineffectual\n");
1746 at_getaddr(const char *addr, int which)
1748 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1751 sat->sat_family = AF_APPLETALK;
1752 sat->sat_len = sizeof(*sat);
1754 errx(1, "AppleTalk does not use netmasks");
1755 if (sscanf(addr, "%u.%u", &net, &node) != 2
1756 || net > 0xffff || node > 0xfe)
1757 errx(1, "%s: illegal address", addr);
1758 sat->sat_addr.s_net = htons(net);
1759 sat->sat_addr.s_node = node;
1763 link_getaddr(const char *addr, int which)
1766 struct sockaddr_dl sdl;
1767 struct sockaddr *sa = &ridreq.ifr_addr;
1770 errx(1, "can't set link-level netmask or broadcast");
1771 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1772 errx(1, "malloc failed");
1774 strcpy(temp + 1, addr);
1775 sdl.sdl_len = sizeof(sdl);
1776 link_addr(temp, &sdl);
1778 if (sdl.sdl_alen > sizeof(sa->sa_data))
1779 errx(1, "malformed link-level address");
1780 sa->sa_family = AF_LINK;
1781 sa->sa_len = sdl.sdl_alen;
1782 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1785 /* XXX FIXME -- should use strtoul for better parsing. */
1787 setatrange(const char *range, int dummy __unused, int s __unused,
1788 const struct afswtch *afp __unused)
1790 u_int first = 123, last = 123;
1792 if (sscanf(range, "%u-%u", &first, &last) != 2
1793 || first == 0 || first > 0xffff
1794 || last == 0 || last > 0xffff || first > last)
1795 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1796 at_nr.nr_firstnet = htons(first);
1797 at_nr.nr_lastnet = htons(last);
1801 setatphase(const char *phase, int dummy __unused, int s __unused,
1802 const struct afswtch *afp __unused)
1804 if (!strcmp(phase, "1"))
1806 else if (!strcmp(phase, "2"))
1809 errx(1, "%s: illegal phase", phase);
1813 checkatrange(struct sockaddr_at *sat)
1815 if (at_nr.nr_phase == 0)
1816 at_nr.nr_phase = 2; /* Default phase 2 */
1817 if (at_nr.nr_firstnet == 0)
1818 at_nr.nr_firstnet = /* Default range of one */
1819 at_nr.nr_lastnet = sat->sat_addr.s_net;
1820 printf("\tatalk %d.%d range %d-%d phase %d\n",
1821 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1822 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1823 if ((u_short) ntohs(at_nr.nr_firstnet) >
1824 (u_short) ntohs(sat->sat_addr.s_net)
1825 || (u_short) ntohs(at_nr.nr_lastnet) <
1826 (u_short) ntohs(sat->sat_addr.s_net))
1827 errx(1, "AppleTalk address is not in range");
1828 sat->sat_range.r_netrange = at_nr;
1832 #define SNS(x) ((struct sockaddr_ns *) &(x))
1833 struct sockaddr_ns *snstab[] = {
1834 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1835 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1838 xns_getaddr(const char *addr, int which)
1840 struct sockaddr_ns *sns = snstab[which];
1842 sns->sns_family = AF_NS;
1843 sns->sns_len = sizeof(*sns);
1844 sns->sns_addr = ns_addr(addr);
1846 printf("Attempt to set XNS netmask will be ineffectual\n");
1852 prefix(void *val, int size)
1854 u_char *addr = (u_char *)val;
1855 int byte, bit, plen = 0;
1857 for (byte = 0; byte < size; byte++, plen += 8)
1858 if (addr[byte] != 0xff)
1862 for (bit = 7; bit != 0; bit--, plen++)
1863 if (!(addr[byte] & (1 << bit)))
1865 for (; bit != 0; bit--)
1866 if (addr[byte] & (1 << bit))
1869 for (; byte < size; byte++)
1876 sec2str(time_t total)
1878 static char result[256];
1879 int days, hours, mins, secs;
1884 days = total / 3600 / 24;
1885 hours = (total / 3600) % 24;
1886 mins = (total / 60) % 60;
1891 p += sprintf(p, "%dd", days);
1893 if (!first || hours) {
1895 p += sprintf(p, "%dh", hours);
1897 if (!first || mins) {
1899 p += sprintf(p, "%dm", mins);
1901 sprintf(p, "%ds", secs);
1903 sprintf(result, "%lu", (unsigned long)total);
1910 ifmaybeload(char *if_nm)
1912 struct module_stat mstat;
1914 char ifkind[35], *cp, *dp;
1917 /* turn interface and unit into module name */
1918 strcpy(ifkind, "if_");
1919 for (cp = if_nm, dp = ifkind + 3;
1920 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1924 /* scan files in kernel */
1925 mstat.version = sizeof(struct module_stat);
1926 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1927 /* scan modules in file */
1928 for (modid = kldfirstmod(fileid); modid > 0;
1929 modid = modfnext(modid)) {
1930 if (modstat(modid, &mstat) < 0)
1932 /* strip bus name if present */
1933 if ((cp = strchr(mstat.name, '/')) != NULL) {
1938 /* already loaded? */
1939 if (strncmp(if_nm, cp, strlen(cp)) == 0 ||
1940 strncmp(ifkind, cp, strlen(cp)) == 0)
1945 /* not present, we should try to load it */
1952 struct if_clonereq ifcr;
1957 s = socket(AF_INET, SOCK_DGRAM, 0);
1961 memset(&ifcr, 0, sizeof(ifcr));
1963 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1964 err(1, "SIOCIFGCLONERS for count");
1966 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1968 err(1, "unable to allocate cloner name buffer");
1970 ifcr.ifcr_count = ifcr.ifcr_total;
1971 ifcr.ifcr_buffer = buf;
1973 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1974 err(1, "SIOCIFGCLONERS for names");
1977 * In case some disappeared in the mean time, clamp it down.
1979 if (ifcr.ifcr_count > ifcr.ifcr_total)
1980 ifcr.ifcr_count = ifcr.ifcr_total;
1982 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1997 s = socket(AF_INET, SOCK_DGRAM, 0);
2001 memset(&ifr, 0, sizeof(ifr));
2002 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2003 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
2004 err(1, "SIOCIFCREATE");
2007 * If we get a different name back then we put in, we probably
2008 * want to print it out, but we might change our mind later so
2009 * we just signal our intrest and leave the printout for later.
2011 if (strcmp(name, ifr.ifr_name) != 0) {
2013 strlcpy(name, ifr.ifr_name, sizeof(name));
2020 clone_destroy(const char *val __unused, int d __unused, int s,
2021 const struct afswtch *rafp __unused)
2024 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2025 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
2026 err(1, "SIOCIFDESTROY");
2028 * If we create and destroy an interface in the same command,
2029 * there isn't any reason to print it's name.