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.22 2005/05/25 01:44:18 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 { "poll", IFF_POLLING, setifflags, NULL },
197 { "-poll", -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 { "polling", IFCAP_POLLING, setifcap, NULL },
295 { "-polling", -IFCAP_POLLING, setifcap, NULL },
296 { "vlanmtu", IFCAP_VLAN_MTU, setifcap, NULL },
297 { "-vlanmtu", -IFCAP_VLAN_MTU, setifcap, NULL },
298 { "vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap, NULL },
299 { "-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap, NULL },
300 { "normal", -IFF_LINK0, setifflags, NULL },
301 { "compress", IFF_LINK0, setifflags, NULL },
302 { "noicmp", IFF_LINK1, setifflags, NULL },
303 { "mtu", NEXTARG, setifmtu, NULL },
304 { "name", NEXTARG, setifname, NULL },
305 { NULL, 0, setifaddr, NULL },
306 { NULL, 0, setifdstaddr, NULL },
310 * XNS support liberally adapted from code written at the University of
311 * Maryland principally by James O'Toole and Chris Torek.
313 typedef void af_status(int, struct rt_addrinfo *);
314 typedef void af_getaddr(const char *, int);
315 typedef void af_getprefix(const char *, int);
317 af_status in_status, at_status, link_status;
318 af_getaddr in_getaddr, at_getaddr, link_getaddr;
321 af_status ipx_status;
322 af_getaddr ipx_getaddr;
326 af_status in6_status;
327 af_getaddr in6_getaddr;
328 af_getprefix in6_getprefix;
331 af_status xns_status;
332 af_getaddr xns_getaddr;
335 /* Known address families */
340 af_status *af_status;
341 af_getaddr *af_getaddr;
342 af_getprefix *af_getprefix;
348 #define C(x) ((caddr_t) &x)
349 { "inet", AF_INET, in_status, in_getaddr, NULL,
350 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
352 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
353 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
354 C(in6_ridreq), C(in6_addreq) },
357 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
358 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
360 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
361 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
363 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
364 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
366 { "link", AF_LINK, link_status, link_getaddr, NULL,
367 0, SIOCSIFLLADDR, NULL, C(ridreq) },
368 { "ether", AF_LINK, link_status, link_getaddr, NULL,
369 0, SIOCSIFLLADDR, NULL, C(ridreq) },
370 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
371 0, SIOCSIFLLADDR, NULL, C(ridreq) },
372 { NULL, 0, NULL, NULL, NULL, 0, 0, NULL, NULL }
376 * Expand the compacted form of addresses as returned via the
377 * configuration read via sysctl().
381 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
382 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
385 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
390 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
391 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
392 if ((rtinfo->rti_addrs & (1 << i)) == 0)
394 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
404 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
405 "usage: ifconfig interface address_family [address [dest_address]]",
408 " ifconfig interface create",
409 " ifconfig -a [-d] [-m] [-u] [address_family]",
410 " ifconfig -l [-d] [-u] [address_family]",
411 " ifconfig [-d] [-m] [-u]");
413 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
414 "usage: ifconfig [-L] interface address_family [address [dest_address]]",
417 " ifconfig interface create",
418 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
419 " ifconfig -l [-d] [-u] [address_family]",
420 " ifconfig [-L] [-d] [-m] [-u]");
426 main(int argc, char * const *argv)
429 int all, namesonly, downonly, uponly;
430 int need_nl = 0, count = 0;
431 const struct afswtch *afp = 0;
432 int addrcount, ifindex;
433 struct if_msghdr *ifm, *nextifm;
434 struct ifa_msghdr *ifam;
435 struct sockaddr_dl *sdl;
436 char *buf, *lim, *next;
441 /* Parse leading line options */
442 all = downonly = uponly = namesonly = 0;
443 while ((c = getopt(argc, argv, "adlmuC"
449 case 'a': /* scan all interfaces */
452 case 'd': /* restrict scan to "down" interfaces */
455 case 'l': /* scan interface names only */
458 case 'm': /* show media choices in status */
461 case 'u': /* restrict scan to "up" interfaces */
469 ip6lifetime++; /* print IPv6 address lifetime */
481 /* -C must be solitary */
482 if (all || supmedia || uponly || downonly || namesonly ||
490 /* -l cannot be used with -a or -m */
491 if (namesonly && (all || supmedia))
495 if (uponly && downonly)
498 /* no arguments is equivalent to '-a' */
499 if (!namesonly && argc < 1)
502 /* -a and -l allow an address family arg to limit the output */
503 if (all || namesonly) {
509 for (afp = afs; afp->af_name; afp++)
510 if (strcmp(afp->af_name, *argv) == 0) {
514 if (afp->af_name == NULL)
516 /* leave with afp non-zero */
519 /* not listing, need an argument */
523 strncpy(name, *argv, sizeof(name));
526 /* check and maybe load support for this interface */
530 * NOTE: We must special-case the `create' command right
531 * here as we would otherwise fail when trying to find
534 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
535 strcmp(argv[0], "plumb") == 0)) {
541 ifindex = if_nametoindex(name);
543 errx(1, "interface %s does not exist", name);
546 /* Check for address family */
548 for (afp = afs; afp->af_name; afp++)
549 if (strcmp(afp->af_name, *argv) == 0) {
553 if (afp->af_name == NULL)
554 afp = NULL; /* not a family, NULL */
561 mib[3] = 0; /* address family */
562 mib[4] = NET_RT_IFLIST;
563 mib[5] = ifindex; /* interface index */
565 /* if particular family specified, only ask about it */
569 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
570 errx(1, "iflist-sysctl-estimate");
571 if ((buf = malloc(needed)) == NULL)
573 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
574 if (errno == ENOMEM && count++ < 10) {
575 warnx("Routing table grew, retrying");
580 errx(1, "actual retrieval of interface table");
587 ifm = (struct if_msghdr *)next;
589 if (ifm->ifm_type == RTM_IFINFO) {
591 if (ifm->ifm_data.ifi_datalen == 0)
592 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
593 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
594 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
596 sdl = (struct sockaddr_dl *)(ifm + 1);
598 flags = ifm->ifm_flags;
600 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
601 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
603 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
604 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
609 next += ifm->ifm_msglen;
614 nextifm = (struct if_msghdr *)next;
616 if (nextifm->ifm_type != RTM_NEWADDR)
620 ifam = (struct ifa_msghdr *)nextifm;
623 next += nextifm->ifm_msglen;
625 memcpy(name, sdl->sdl_data,
626 sizeof(name) < sdl->sdl_nlen ?
627 sizeof(name)-1 : sdl->sdl_nlen);
628 name[sizeof(name) < sdl->sdl_nlen ?
629 sizeof(name)-1 : sdl->sdl_nlen] = '\0';
631 if (all || namesonly) {
634 /* sdl_data may not be terminated, don't use strlcpy */
635 if ((len = sdl->sdl_nlen) > sizeof(name) - 1)
636 len = sizeof(name) - 1;
637 bcopy(sdl->sdl_data, name, len);
641 if ((flags & IFF_UP) == 0)
642 continue; /* not up */
645 continue; /* not down */
648 afp->af_status != link_status ||
649 sdl->sdl_type == IFT_ETHER) {
660 ifconfig(argc, argv, afp);
662 status(afp, addrcount, sdl, ifm, ifam);
666 if (namesonly && need_nl > 0)
670 printf("%s\n", name);
677 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
683 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
684 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
686 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
692 for (p = cmds; p->c_name; p++)
693 if (strcmp(*argv, p->c_name) == 0)
695 if (p->c_name == 0 && setaddr)
696 p++; /* got src, do dst */
697 if (p->c_func || p->c_func2) {
698 if (p->c_parameter == NEXTARG) {
700 errx(1, "'%s' requires argument",
702 (*p->c_func)(argv[1], 0, s, afp);
704 } else if (p->c_parameter == NEXTARG2) {
706 errx(1, "'%s' requires 2 arguments",
708 (*p->c_func2)(argv[1], argv[2], s, afp);
709 argc -= 2, argv += 2;
711 (*p->c_func)(*argv, p->c_parameter, s, afp);
716 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
717 /* Aggregatable address architecture defines all prefixes
718 are 64. So, it is convenient to set prefixlen to 64 if
719 it is not specified. */
720 setifprefixlen("64", 0, s, afp);
721 /* in6_getprefix("64", MASK) if MASK is available here... */
725 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
727 int size = sizeof(rq);
729 rq.rq_ipx = addreq.ifra_addr;
730 rq.rq_ip = addreq.ifra_dstaddr;
732 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
733 Perror("Encapsulation Routing");
736 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
737 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
739 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
741 int size = sizeof(rq);
743 rq.rq_ns = addreq.ifra_addr;
744 rq.rq_ip = addreq.ifra_dstaddr;
746 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
747 Perror("Encapsulation Routing");
751 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
752 warnx("interface %s cannot change %s addresses!",
759 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
760 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
761 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
762 /* means no previous address for interface */
764 Perror("ioctl (SIOCDIFADDR)");
768 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
769 warnx("interface %s cannot change %s addresses!",
774 if (newaddr && (setaddr || setmask)) {
775 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
776 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
777 Perror("ioctl (SIOCAIFADDR)");
789 setifaddr(const char *addr, int param __unused, int s __unused,
790 const struct afswtch *afp)
792 if (*afp->af_getaddr == NULL)
795 * Delay the ioctl to set the interface addr until flags are all set.
796 * The address interpretation may depend on the flags,
797 * and the flags may change when the address is set.
800 if (doalias == 0 && afp->af_af != AF_LINK)
802 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
806 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
808 struct addrinfo hints, *srcres, *dstres;
809 struct ifaliasreq addr_req;
812 struct in6_aliasreq in6_addr_req;
815 memset(&hints, 0, sizeof(hints));
816 hints.ai_family = afp->af_af;
818 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
819 errx(1, "error in parsing address string: %s",
820 gai_strerror(ecode));
822 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
823 errx(1, "error in parsing address string: %s",
824 gai_strerror(ecode));
826 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
828 "source and destination address families do not match");
830 switch (srcres->ai_addr->sa_family) {
832 memset(&addr_req, 0, sizeof(addr_req));
833 strncpy(addr_req.ifra_name, name, IFNAMSIZ);
834 memcpy(&addr_req.ifra_addr, srcres->ai_addr,
835 srcres->ai_addr->sa_len);
836 memcpy(&addr_req.ifra_dstaddr, dstres->ai_addr,
837 dstres->ai_addr->sa_len);
839 if (ioctl(s, SIOCSIFPHYADDR, &addr_req) < 0)
840 warn("SIOCSIFPHYADDR");
845 memset(&in6_addr_req, 0, sizeof(in6_addr_req));
846 strncpy(in6_addr_req.ifra_name, name, IFNAMSIZ);
847 memcpy(&in6_addr_req.ifra_addr, srcres->ai_addr,
848 srcres->ai_addr->sa_len);
849 memcpy(&in6_addr_req.ifra_dstaddr, dstres->ai_addr,
850 dstres->ai_addr->sa_len);
852 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addr_req) < 0)
853 warn("SIOCSIFPHYADDR_IN6");
858 warn("address family not supported");
861 freeaddrinfo(srcres);
862 freeaddrinfo(dstres);
867 deletetunnel(const char *vname __unused, int param __unused, int s,
868 const struct afswtch *afp __unused)
870 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
871 err(1, "SIOCDIFPHYADDR");
875 setifnetmask(const char *addr, int dummy __unused, int s __unused,
876 const struct afswtch *afp)
878 if (*afp->af_getaddr == NULL)
881 (*afp->af_getaddr)(addr, MASK);
886 setifprefixlen(const char *addr, int dummy __unused, int s __unused,
887 const struct afswtch *afp)
889 if (*afp->af_getprefix)
890 (*afp->af_getprefix)(addr, MASK);
895 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
896 const struct afswtch *afp)
898 if (afp->af_af != AF_INET6)
899 err(1, "address flags can be set only for inet6 addresses");
902 in6_addreq.ifra_flags &= ~(-flag);
904 in6_addreq.ifra_flags |= flag;
908 setip6pltime(const char *seconds, int dummy __unused, int s,
909 const struct afswtch *afp)
911 setip6lifetime("pltime", seconds, s, afp);
915 setip6vltime(const char *seconds, int dummy __unused, int s,
916 const struct afswtch *afp)
918 setip6lifetime("vltime", seconds, s, afp);
922 setip6lifetime(const char *cmd, const char *val, int s __unused,
923 const struct afswtch *afp)
929 newval = (time_t)strtoul(val, &ep, 0);
931 errx(1, "invalid %s", cmd);
932 if (afp->af_af != AF_INET6)
933 errx(1, "%s not allowed for the AF", cmd);
934 if (strcmp(cmd, "vltime") == 0) {
935 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
936 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
937 } else if (strcmp(cmd, "pltime") == 0) {
938 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
939 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
944 setip6eui64(const char *cmd, int dummy __unused, int s __unused,
945 const struct afswtch *afp)
947 struct ifaddrs *ifap, *ifa;
948 const struct sockaddr_in6 *sin6 = NULL;
949 const struct in6_addr *lladdr = NULL;
950 struct in6_addr *in6;
952 if (afp->af_af != AF_INET6)
953 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
954 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
955 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
956 errx(EXIT_FAILURE, "interface index is already filled");
957 if (getifaddrs(&ifap) != 0)
958 err(EXIT_FAILURE, "getifaddrs");
959 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
960 if (ifa->ifa_addr->sa_family == AF_INET6 &&
961 strcmp(ifa->ifa_name, name) == 0) {
962 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
963 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
964 lladdr = &sin6->sin6_addr;
970 errx(EXIT_FAILURE, "could not determine link local address");
972 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
979 setifbroadaddr(const char *addr, int dummy __unused, int s __unused,
980 const struct afswtch *afp)
982 if (*afp->af_getaddr == NULL)
984 (*afp->af_getaddr)(addr, DSTADDR);
988 setifipdst(const char *addr, int dummy __unused, int s __unused,
989 const struct afswtch *afp __unused)
991 in_getaddr(addr, DSTADDR);
996 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
999 notealias(const char *addr __unused, int param, int s __unused,
1000 const struct afswtch *afp)
1002 if (setaddr && doalias == 0 && param < 0)
1003 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1004 bcopy((caddr_t)rqtosa(af_addreq),
1005 (caddr_t)rqtosa(af_ridreq),
1006 rqtosa(af_addreq)->sa_len);
1017 setifdstaddr(const char *addr, int param __unused, int s __unused,
1018 const struct afswtch *afp)
1020 if (*afp->af_getaddr == NULL)
1022 (*afp->af_getaddr)(addr, DSTADDR);
1026 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1027 * of the ifreq structure, which may confuse other parts of ifconfig.
1028 * Make a private copy so we can avoid that.
1031 setifflags(const char *vname, int value, int s,
1032 const struct afswtch *afp __unused)
1034 struct ifreq my_ifr;
1036 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1038 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1039 Perror("ioctl (SIOCGIFFLAGS)");
1042 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1043 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1050 my_ifr.ifr_flags = flags & 0xffff;
1051 my_ifr.ifr_flagshigh = flags >> 16;
1052 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1057 setifcap(const char *vname, int value, int s,
1058 const struct afswtch *afp __unused)
1061 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1062 Perror("ioctl (SIOCGIFCAP)");
1065 flags = ifr.ifr_curcap;
1071 ifr.ifr_reqcap = flags;
1072 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1077 setifmetric(const char *val, int dummy __unused, int s,
1078 const struct afswtch *afp __unused)
1080 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1081 ifr.ifr_metric = atoi(val);
1082 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1083 warn("ioctl (set metric)");
1087 setifmtu(const char *val, int dummy __unused, int s,
1088 const struct afswtch *afp __unused)
1090 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1091 ifr.ifr_mtu = atoi(val);
1092 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1093 warn("ioctl (set mtu)");
1097 setifname(const char *val, int dummy __unused, int s,
1098 const struct afswtch *afp __unused)
1102 newname = strdup(val);
1104 ifr.ifr_data = newname;
1105 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1106 warn("ioctl (set name)");
1110 strlcpy(name, newname, sizeof(name));
1115 * Even if we just created the interface, we don't need to print
1116 * its name because we just nailed it down separately.
1122 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1123 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1124 "\20MULTICAST\21POLLING\23MONITOR\24STATICARP"
1127 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING"
1130 * Print the status of the interface. If an address family was
1131 * specified, show it and it only; otherwise, show them all.
1134 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1135 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1137 const struct afswtch *p = NULL;
1138 struct rt_addrinfo info;
1148 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1149 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1151 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1154 printf("flags %08x\n", flags);
1155 printf("%s: ", name);
1156 printb("flags", flags, IFFBITS);
1157 if (ifm->ifm_data.ifi_metric)
1158 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1159 if (ifm->ifm_data.ifi_mtu)
1160 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1163 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1164 if (ifr.ifr_curcap != 0) {
1165 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1168 if (supmedia && ifr.ifr_reqcap != 0) {
1169 printf("\tcapability list:\n");
1170 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1177 while (addrcount > 0) {
1179 info.rti_addrs = ifam->ifam_addrs;
1181 /* Expand the compacted addresses */
1182 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1186 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1188 (*p->af_status)(s, &info);
1190 } else for (p = afs; p->af_name; p++) {
1191 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1192 (*p->af_status)(s, &info);
1195 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1197 if (allfamilies || afp->af_status == link_status)
1198 link_status(s, (struct rt_addrinfo *)sdl);
1200 if (allfamilies || afp->af_status == media_status)
1201 media_status(s, NULL);
1204 if (allfamilies || afp->af_status == vlan_status)
1205 vlan_status(s, NULL);
1207 #ifdef USE_IEEE80211
1208 if (allfamilies || afp->af_status == ieee80211_status)
1209 ieee80211_status(s, NULL);
1211 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1212 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1213 printf("%s", ifs.ascii);
1215 if (!allfamilies && !p &&
1217 afp->af_status != media_status &&
1219 afp->af_status != link_status
1221 && afp->af_status != vlan_status
1224 warnx("%s has no %s interface address!", name, afp->af_name);
1231 tunnel_status(int s)
1233 char psrcaddr[NI_MAXHOST];
1234 char pdstaddr[NI_MAXHOST];
1235 u_long srccmd, dstcmd;
1237 const char *ver = "";
1238 #ifdef NI_WITHSCOPEID
1239 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1241 const int niflag = NI_NUMERICHOST;
1244 struct in6_ifreq in6_ifr;
1248 psrcaddr[0] = pdstaddr[0] = '\0';
1251 memset(&in6_ifr, 0, sizeof(in6_ifr));
1252 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1253 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1255 srccmd = SIOCGIFPSRCADDR;
1256 dstcmd = SIOCGIFPDSTADDR;
1260 srccmd = SIOCGIFPSRCADDR_IN6;
1261 dstcmd = SIOCGIFPDSTADDR_IN6;
1262 ifrp = (struct ifreq *)&in6_ifr;
1265 srccmd = SIOCGIFPSRCADDR;
1266 dstcmd = SIOCGIFPDSTADDR;
1270 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1272 #if defined(INET6) && defined(__KAME__) && defined(KAME_SCOPEID)
1273 if (ifrp->ifr_addr.sa_family == AF_INET6)
1274 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1276 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1277 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1279 if (ifrp->ifr_addr.sa_family == AF_INET6)
1283 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1285 #if defined(INET6) && defined(__KAME__) && defined(KAME_SCOPEID)
1286 if (ifrp->ifr_addr.sa_family == AF_INET6)
1287 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1289 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1290 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1292 printf("\ttunnel inet%s %s --> %s\n", ver,
1293 psrcaddr, pdstaddr);
1297 in_status(int s __unused, struct rt_addrinfo *info)
1299 struct sockaddr_in *addr_in, null_in;
1301 memset(&null_in, 0, sizeof(null_in));
1303 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1304 printf("\tinet %s ", inet_ntoa(addr_in->sin_addr));
1306 if (flags & IFF_POINTOPOINT) {
1307 /* note RTAX_BRD overlap with IFF_BROADCAST */
1308 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1311 printf("--> %s ", inet_ntoa(addr_in->sin_addr));
1314 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1317 printf("netmask 0x%lx ",
1318 (unsigned long)ntohl(addr_in->sin_addr.s_addr));
1320 if (flags & IFF_BROADCAST) {
1321 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1322 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1323 if (addr_in && addr_in->sin_addr.s_addr != 0)
1324 printf("broadcast %s", inet_ntoa(addr_in->sin_addr));
1330 #if defined(__KAME__) && defined(KAME_SCOPEID)
1332 in6_fillscopeid(struct sockaddr_in6 *sin6 __unused)
1334 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1335 sin6->sin6_scope_id =
1336 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1337 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1340 #endif /* defined(__KAME__) && defined(KAME_SCOPEID) */
1343 in6_status(int s __unused, struct rt_addrinfo *info)
1345 struct sockaddr_in6 *addr_in, null_in;
1346 struct in6_ifreq ifr6;
1349 struct in6_addrlifetime lifetime;
1350 time_t t = time(NULL);
1354 memset(&null_in, 0, sizeof(null_in));
1356 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1357 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1358 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1359 perror("ifconfig: socket");
1362 ifr6.ifr_addr = *addr_in;
1363 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1364 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1368 flags6 = ifr6.ifr_ifru.ifru_flags6;
1369 memset(&lifetime, 0, sizeof(lifetime));
1370 ifr6.ifr_addr = *addr_in;
1371 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1372 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1376 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1379 /* XXX: embedded link local addr check */
1380 if (IN6_IS_ADDR_LINKLOCAL(&addr_in->sin6_addr) &&
1381 *(u_short *)&addr_in->sin6_addr.s6_addr[2] != 0) {
1384 idx = *(u_short *)&addr_in->sin6_addr.s6_addr[2];
1385 *(u_short *)&addr_in->sin6_addr.s6_addr[2] = 0;
1386 if (addr_in->sin6_scope_id == 0)
1387 addr_in->sin6_scope_id = ntohs(idx);
1389 scopeid = addr_in->sin6_scope_id;
1391 error = getnameinfo((struct sockaddr *)addr_in, addr_in->sin6_len, addr_buf,
1392 sizeof(addr_buf), NULL, 0,
1393 NI_NUMERICHOST|NI_WITHSCOPEID);
1395 inet_ntop(AF_INET6, &addr_in->sin6_addr, addr_buf,
1397 printf("\tinet6 %s ", addr_buf);
1399 if (flags & IFF_POINTOPOINT) {
1400 /* note RTAX_BRD overlap with IFF_BROADCAST */
1401 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1403 * some of the interfaces do not have valid destination
1406 if (addr_in && addr_in->sin6_family == AF_INET6) {
1407 /* XXX: embedded link local addr check */
1408 if (IN6_IS_ADDR_LINKLOCAL(&addr_in->sin6_addr) &&
1409 *(u_short *)&addr_in->sin6_addr.s6_addr[2] != 0) {
1412 idx = *(u_short *)&addr_in->sin6_addr.s6_addr[2];
1413 *(u_short *)&addr_in->sin6_addr.s6_addr[2] = 0;
1414 if (addr_in->sin6_scope_id == 0)
1415 addr_in->sin6_scope_id = ntohs(idx);
1418 error = getnameinfo((struct sockaddr *)addr_in,
1419 addr_in->sin6_len, addr_buf,
1420 sizeof(addr_buf), NULL, 0,
1421 NI_NUMERICHOST|NI_WITHSCOPEID);
1423 inet_ntop(AF_INET6, &addr_in->sin6_addr, addr_buf,
1425 printf("--> %s ", addr_buf);
1429 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1432 printf("prefixlen %d ", prefix(&addr_in->sin6_addr,
1433 sizeof(struct in6_addr)));
1435 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1437 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1438 printf("tentative ");
1439 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1440 printf("duplicated ");
1441 if ((flags6 & IN6_IFF_DETACHED) != 0)
1442 printf("detached ");
1443 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1444 printf("deprecated ");
1445 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1446 printf("autoconf ");
1447 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1448 printf("temporary ");
1451 printf("scopeid 0x%x ", scopeid);
1453 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1455 if (lifetime.ia6t_preferred) {
1456 printf("%s ", lifetime.ia6t_preferred < t
1457 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1462 if (lifetime.ia6t_expire) {
1463 printf("%s ", lifetime.ia6t_expire < t
1464 ? "0" : sec2str(lifetime.ia6t_expire - t));
1475 ipx_status(int s __unused, struct rt_addrinfo *info)
1477 struct sockaddr_ipx *sipx, null_sipx;
1479 memset(&null_sipx, 0, sizeof(null_sipx));
1481 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1482 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1484 if (flags & IFF_POINTOPOINT) {
1485 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1488 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1495 at_status(int s __unused, struct rt_addrinfo *info)
1497 struct sockaddr_at *sat, null_sat;
1498 struct netrange *nr;
1500 memset(&null_sat, 0, sizeof(null_sat));
1502 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1503 nr = &sat->sat_range.r_netrange;
1504 printf("\tatalk %d.%d range %d-%d phase %d",
1505 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1506 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1507 if (flags & IFF_POINTOPOINT) {
1508 /* note RTAX_BRD overlap with IFF_BROADCAST */
1509 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1513 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1515 if (flags & IFF_BROADCAST) {
1516 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1517 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1519 printf(" broadcast %d.%d",
1520 ntohs(sat->sat_addr.s_net),
1521 sat->sat_addr.s_node);
1529 xns_status(int s __unused, struct rt_addrinfo *info)
1531 struct sockaddr_ns *sns, null_sns;
1533 memset(&null_sns, 0, sizeof(null_sns));
1535 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1536 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1538 if (flags & IFF_POINTOPOINT) {
1539 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1542 printf("--> %s ", ns_ntoa(sns->sns_addr));
1552 link_status(int s __unused, struct rt_addrinfo *info)
1554 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1556 if (sdl->sdl_alen > 0) {
1557 if (sdl->sdl_type == IFT_ETHER &&
1558 sdl->sdl_alen == ETHER_ADDR_LEN)
1559 printf("\tether %s\n",
1560 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1562 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1564 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1570 Perror(const char *cmd)
1575 errx(1, "%s: no such interface", cmd);
1579 errx(1, "%s: permission denied", cmd);
1587 #define SIN(x) ((struct sockaddr_in *) &(x))
1588 struct sockaddr_in *sintab[] = {
1589 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1590 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1593 in_getaddr(const char *s, int which)
1595 struct sockaddr_in *addr_in = sintab[which];
1599 addr_in->sin_len = sizeof(*addr_in);
1601 addr_in->sin_family = AF_INET;
1603 if (which == ADDR) {
1606 if((p = strrchr(s, '/')) != NULL) {
1607 /* address is `name/masklen' */
1610 struct sockaddr_in *min = sintab[MASK];
1612 ret = sscanf(p+1, "%u", &masklen);
1613 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1615 errx(1, "%s: bad value", s);
1617 min->sin_len = sizeof(*min);
1618 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1623 if (inet_aton(s, &addr_in->sin_addr))
1625 if ((hp = gethostbyname(s)) != 0)
1626 bcopy(hp->h_addr, (char *)&addr_in->sin_addr,
1627 MIN((size_t)hp->h_length, sizeof(addr_in->sin_addr)));
1628 else if ((np = getnetbyname(s)) != 0)
1629 addr_in->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1631 errx(1, "%s: bad value", s);
1635 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1636 struct sockaddr_in6 *sin6tab[] = {
1637 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1638 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1641 in6_getaddr(const char *s, int which)
1643 struct sockaddr_in6 *addr_in = sin6tab[which];
1644 struct addrinfo hints, *res;
1649 addr_in->sin6_len = sizeof(*addr_in);
1651 addr_in->sin6_family = AF_INET6;
1653 if (which == ADDR) {
1655 if((p = strrchr(s, '/')) != NULL) {
1657 in6_getprefix(p + 1, MASK);
1658 explicit_prefix = 1;
1662 if (addr_in->sin6_family == AF_INET6) {
1663 bzero(&hints, sizeof(struct addrinfo));
1664 hints.ai_family = AF_INET6;
1665 error = getaddrinfo(s, NULL, &hints, &res);
1668 if (inet_pton(AF_INET6, s, &addr_in->sin6_addr) != 1)
1669 errx(1, "%s: bad value", s);
1671 bcopy(res->ai_addr, addr_in, res->ai_addrlen);
1675 in6_getprefix(const char *plen, int which)
1677 struct sockaddr_in6 *addr_in = sin6tab[which];
1679 int len = atoi(plen);
1681 if ((len < 0) || (len > 128))
1682 errx(1, "%s: bad value", plen);
1683 addr_in->sin6_len = sizeof(*addr_in);
1685 addr_in->sin6_family = AF_INET6;
1686 if ((len == 0) || (len == 128)) {
1687 memset(&addr_in->sin6_addr, 0xff, sizeof(struct in6_addr));
1690 memset((void *)&addr_in->sin6_addr, 0x00, sizeof(addr_in->sin6_addr));
1691 for (cp = (u_char *)&addr_in->sin6_addr; len > 7; len -= 8)
1693 *cp = 0xff << (8 - len);
1698 * Print a value a la the %b format of the kernel's printf
1701 printb(const char *s, unsigned v, const char *bits)
1706 if (bits && *bits == 8)
1707 printf("%s=%o", s, v);
1709 printf("%s=%x", s, v);
1713 while ((i = *bits++) != '\0') {
1714 if (v & (1 << (i-1))) {
1718 for (; (c = *bits) > 32; bits++)
1721 for (; *bits > 32; bits++)
1729 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1730 struct sockaddr_ipx *sipxtab[] = {
1731 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1732 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1735 ipx_getaddr(const char *addr, int which)
1737 struct sockaddr_ipx *sipx = sipxtab[which];
1739 sipx->sipx_family = AF_IPX;
1740 sipx->sipx_len = sizeof(*sipx);
1741 sipx->sipx_addr = ipx_addr(addr);
1743 printf("Attempt to set IPX netmask will be ineffectual\n");
1748 at_getaddr(const char *addr, int which)
1750 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1753 sat->sat_family = AF_APPLETALK;
1754 sat->sat_len = sizeof(*sat);
1756 errx(1, "AppleTalk does not use netmasks");
1757 if (sscanf(addr, "%u.%u", &net, &node) != 2
1758 || net > 0xffff || node > 0xfe)
1759 errx(1, "%s: illegal address", addr);
1760 sat->sat_addr.s_net = htons(net);
1761 sat->sat_addr.s_node = node;
1765 link_getaddr(const char *addr, int which)
1768 struct sockaddr_dl sdl;
1769 struct sockaddr *sa = &ridreq.ifr_addr;
1772 errx(1, "can't set link-level netmask or broadcast");
1773 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1774 errx(1, "malloc failed");
1776 strcpy(temp + 1, addr);
1777 sdl.sdl_len = sizeof(sdl);
1778 link_addr(temp, &sdl);
1780 if (sdl.sdl_alen > sizeof(sa->sa_data))
1781 errx(1, "malformed link-level address");
1782 sa->sa_family = AF_LINK;
1783 sa->sa_len = sdl.sdl_alen;
1784 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1787 /* XXX FIXME -- should use strtoul for better parsing. */
1789 setatrange(const char *range, int dummy __unused, int s __unused,
1790 const struct afswtch *afp __unused)
1792 u_int first = 123, last = 123;
1794 if (sscanf(range, "%u-%u", &first, &last) != 2
1795 || first == 0 || first > 0xffff
1796 || last == 0 || last > 0xffff || first > last)
1797 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1798 at_nr.nr_firstnet = htons(first);
1799 at_nr.nr_lastnet = htons(last);
1803 setatphase(const char *phase, int dummy __unused, int s __unused,
1804 const struct afswtch *afp __unused)
1806 if (!strcmp(phase, "1"))
1808 else if (!strcmp(phase, "2"))
1811 errx(1, "%s: illegal phase", phase);
1815 checkatrange(struct sockaddr_at *sat)
1817 if (at_nr.nr_phase == 0)
1818 at_nr.nr_phase = 2; /* Default phase 2 */
1819 if (at_nr.nr_firstnet == 0)
1820 at_nr.nr_firstnet = /* Default range of one */
1821 at_nr.nr_lastnet = sat->sat_addr.s_net;
1822 printf("\tatalk %d.%d range %d-%d phase %d\n",
1823 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1824 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1825 if ((u_short) ntohs(at_nr.nr_firstnet) >
1826 (u_short) ntohs(sat->sat_addr.s_net)
1827 || (u_short) ntohs(at_nr.nr_lastnet) <
1828 (u_short) ntohs(sat->sat_addr.s_net))
1829 errx(1, "AppleTalk address is not in range");
1830 sat->sat_range.r_netrange = at_nr;
1834 #define SNS(x) ((struct sockaddr_ns *) &(x))
1835 struct sockaddr_ns *snstab[] = {
1836 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1837 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1840 xns_getaddr(const char *addr, int which)
1842 struct sockaddr_ns *sns = snstab[which];
1844 sns->sns_family = AF_NS;
1845 sns->sns_len = sizeof(*sns);
1846 sns->sns_addr = ns_addr(addr);
1848 printf("Attempt to set XNS netmask will be ineffectual\n");
1854 prefix(void *val, int size)
1856 u_char *addr = (u_char *)val;
1857 int byte, bit, plen = 0;
1859 for (byte = 0; byte < size; byte++, plen += 8)
1860 if (addr[byte] != 0xff)
1864 for (bit = 7; bit != 0; bit--, plen++)
1865 if (!(addr[byte] & (1 << bit)))
1867 for (; bit != 0; bit--)
1868 if (addr[byte] & (1 << bit))
1871 for (; byte < size; byte++)
1878 sec2str(time_t total)
1880 static char result[256];
1881 int days, hours, mins, secs;
1886 days = total / 3600 / 24;
1887 hours = (total / 3600) % 24;
1888 mins = (total / 60) % 60;
1893 p += sprintf(p, "%dd", days);
1895 if (!first || hours) {
1897 p += sprintf(p, "%dh", hours);
1899 if (!first || mins) {
1901 p += sprintf(p, "%dm", mins);
1903 sprintf(p, "%ds", secs);
1905 sprintf(result, "%lu", (unsigned long)total);
1912 ifmaybeload(char *if_nm)
1914 struct module_stat mstat;
1916 char ifkind[35], *cp, *dp;
1919 /* turn interface and unit into module name */
1920 strcpy(ifkind, "if_");
1921 for (cp = if_nm, dp = ifkind + 3;
1922 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1926 /* scan files in kernel */
1927 mstat.version = sizeof(struct module_stat);
1928 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1929 /* scan modules in file */
1930 for (modid = kldfirstmod(fileid); modid > 0;
1931 modid = modfnext(modid)) {
1932 if (modstat(modid, &mstat) < 0)
1934 /* strip bus name if present */
1935 if ((cp = strchr(mstat.name, '/')) != NULL) {
1940 /* already loaded? */
1941 if (strncmp(if_nm, cp, strlen(cp)) == 0 ||
1942 strncmp(ifkind, cp, strlen(cp)) == 0)
1947 /* not present, we should try to load it */
1954 struct if_clonereq ifcr;
1959 s = socket(AF_INET, SOCK_DGRAM, 0);
1963 memset(&ifcr, 0, sizeof(ifcr));
1965 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1966 err(1, "SIOCIFGCLONERS for count");
1968 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1970 err(1, "unable to allocate cloner name buffer");
1972 ifcr.ifcr_count = ifcr.ifcr_total;
1973 ifcr.ifcr_buffer = buf;
1975 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1976 err(1, "SIOCIFGCLONERS for names");
1979 * In case some disappeared in the mean time, clamp it down.
1981 if (ifcr.ifcr_count > ifcr.ifcr_total)
1982 ifcr.ifcr_count = ifcr.ifcr_total;
1984 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1999 s = socket(AF_INET, SOCK_DGRAM, 0);
2003 memset(&ifr, 0, sizeof(ifr));
2004 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2005 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
2006 err(1, "SIOCIFCREATE");
2009 * If we get a different name back then we put in, we probably
2010 * want to print it out, but we might change our mind later so
2011 * we just signal our intrest and leave the printout for later.
2013 if (strcmp(name, ifr.ifr_name) != 0) {
2015 strlcpy(name, ifr.ifr_name, sizeof(name));
2022 clone_destroy(const char *val __unused, int d __unused, int s,
2023 const struct afswtch *rafp __unused)
2026 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2027 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
2028 err(1, "SIOCIFDESTROY");
2030 * If we create and destroy an interface in the same command,
2031 * there isn't any reason to print it's name.