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.14 2005/03/03 23:52:00 cpressey Exp $
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
45 #include <sys/module.h>
46 #include <sys/linker.h>
48 #include <net/ethernet.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <arpa/inet.h>
62 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
69 #include <netipx/ipx.h>
70 #include <netipx/ipx_if.h>
74 #include <netatalk/at.h>
80 #include <netns/ns_if.h>
97 /* wrapper for KAME-special getnameinfo() */
98 #ifndef NI_WITHSCOPEID
99 #define NI_WITHSCOPEID 0
102 struct ifreq ifr, ridreq;
103 struct ifaliasreq addreq;
105 struct in6_ifreq in6_ridreq;
106 struct in6_aliasreq in6_addreq =
108 { 0, 0, 0, 0, { { { 0 } } }, 0 },
109 { 0, 0, 0, 0, { { { 0 } } }, 0 },
110 { 0, 0, 0, 0, { { { 0 } } }, 0 },
112 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
114 struct sockaddr_in netmask;
115 struct netrange at_nr; /* AppleTalk net range */
126 static int ip6lifetime;
133 int printname = 0; /* Print the name of the created interface. */
136 char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
139 void Perror(const char *cmd);
140 void checkatrange(struct sockaddr_at *);
141 int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
142 void notealias(const char *, int, int, const struct afswtch *afp);
143 void list_cloners(void);
144 void printb(const char *s, unsigned value, const char *bits);
145 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
146 void status(const struct afswtch *afp, int addrcount,
147 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
148 struct ifa_msghdr *ifam);
149 void tunnel_status(int s);
151 void ifmaybeload(char *name);
154 void in6_fillscopeid(struct sockaddr_in6 *sin6);
155 int prefix(void *, int);
156 static char *sec2str(time_t);
157 int explicit_prefix = 0;
160 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
161 typedef void c_func2(const char *arg, const char *arg2, int s, const struct afswtch *afp);
162 c_func setatphase, setatrange;
163 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
167 c_func setifprefixlen;
171 c_func2 setip6lifetime;
175 c_func setifflags, setifmetric, setifmtu, setifcap;
176 c_func clone_destroy;
180 void clone_create(void);
183 #define NEXTARG 0xffffff
184 #define NEXTARG2 0xfffffe
189 int c_parameter; /* NEXTARG means next argv */
190 void (*c_func)(const char *, int, int, const struct afswtch *afp);
191 void (*c_func2)(const char *, const char *, int, const struct afswtch *afp);
193 { "up", IFF_UP, setifflags, NULL },
194 { "down", -IFF_UP, setifflags, NULL },
195 { "arp", -IFF_NOARP, setifflags, NULL },
196 { "-arp", IFF_NOARP, setifflags, NULL },
197 { "debug", IFF_DEBUG, setifflags, NULL },
198 { "-debug", -IFF_DEBUG, setifflags, NULL },
199 { "promisc", IFF_PPROMISC, setifflags, NULL },
200 { "-promisc", -IFF_PPROMISC, setifflags, NULL },
201 { "add", IFF_UP, notealias, NULL },
202 { "alias", IFF_UP, notealias, NULL },
203 { "-alias", -IFF_UP, notealias, NULL },
204 { "delete", -IFF_UP, notealias, NULL },
205 { "remove", -IFF_UP, notealias, NULL },
207 #define EN_SWABIPS 0x1000
208 { "swabips", EN_SWABIPS, setifflags, NULL },
209 { "-swabips", -EN_SWABIPS, setifflags, NULL },
211 { "netmask", NEXTARG, setifnetmask, NULL },
213 { "prefixlen", NEXTARG, setifprefixlen, NULL },
214 { "anycast", IN6_IFF_ANYCAST, setip6flags, NULL },
215 { "tentative", IN6_IFF_TENTATIVE, setip6flags, NULL },
216 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags, NULL },
217 { "deprecated", IN6_IFF_DEPRECATED, setip6flags, NULL },
218 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags, NULL },
219 { "autoconf", IN6_IFF_AUTOCONF, setip6flags, NULL },
220 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags, NULL },
221 { "pltime", NEXTARG, setip6pltime, NULL },
222 { "vltime", NEXTARG, setip6vltime, NULL },
223 { "eui64", 0, setip6eui64, NULL },
225 { "range", NEXTARG, setatrange, NULL },
226 { "phase", NEXTARG, setatphase, NULL },
227 { "metric", NEXTARG, setifmetric, NULL },
228 { "broadcast", NEXTARG, setifbroadaddr, NULL },
229 { "ipdst", NEXTARG, setifipdst, NULL },
230 { "tunnel", NEXTARG2, NULL, settunnel },
231 { "deletetunnel", 0, deletetunnel, NULL },
232 { "link0", IFF_LINK0, setifflags, NULL },
233 { "-link0", -IFF_LINK0, setifflags, NULL },
234 { "link1", IFF_LINK1, setifflags, NULL },
235 { "-link1", -IFF_LINK1, setifflags, NULL },
236 { "link2", IFF_LINK2, setifflags, NULL },
237 { "-link2", -IFF_LINK2, setifflags, NULL },
239 { "media", NEXTARG, setmedia, NULL },
240 { "mediaopt", NEXTARG, setmediaopt, NULL },
241 { "-mediaopt", NEXTARG, unsetmediaopt, NULL },
244 { "vlan", NEXTARG, setvlantag, NULL },
245 { "vlandev", NEXTARG, setvlandev, NULL },
246 { "-vlandev", NEXTARG, unsetvlandev, NULL },
249 /* XXX `create' special-cased below */
250 {"create", 0, clone_create, NULL },
251 {"plumb", 0, clone_create, NULL },
253 {"destroy", 0, clone_destroy, NULL },
254 {"unplumb", 0, clone_destroy, NULL },
256 { "ssid", NEXTARG, set80211ssid, NULL },
257 { "nwid", NEXTARG, set80211ssid, NULL },
258 { "stationname", NEXTARG, set80211stationname, NULL },
259 { "station", NEXTARG, set80211stationname, NULL },/* BSD/OS */
260 { "channel", NEXTARG, set80211channel, NULL },
261 { "authmode", NEXTARG, set80211authmode, NULL },
262 { "powersavemode", NEXTARG, set80211powersavemode, NULL },
263 { "powersave", 1, set80211powersave, NULL },
264 { "-powersave", 0, set80211powersave, NULL },
265 { "powersavesleep", NEXTARG, set80211powersavesleep, NULL },
266 { "wepmode", NEXTARG, set80211wepmode, NULL },
267 { "wep", 1, set80211wep, NULL },
268 { "-wep", 0, set80211wep, NULL },
269 { "weptxkey", NEXTARG, set80211weptxkey, NULL },
270 { "wepkey", NEXTARG, set80211wepkey, NULL },
271 { "nwkey", NEXTARG, set80211nwkey, NULL }, /* NetBSD */
272 { "-nwkey", 0, set80211wep, NULL }, /* NetBSD */
274 { "rxcsum", IFCAP_RXCSUM, setifcap, NULL },
275 { "-rxcsum", -IFCAP_RXCSUM, setifcap, NULL },
276 { "txcsum", IFCAP_TXCSUM, setifcap, NULL },
277 { "-txcsum", -IFCAP_TXCSUM, setifcap, NULL },
278 { "netcons", IFCAP_NETCONS, setifcap, NULL },
279 { "-netcons", -IFCAP_NETCONS, setifcap, NULL },
280 { "polling", IFCAP_POLLING, setifcap, NULL },
281 { "-polling", -IFCAP_POLLING, setifcap, NULL },
282 { "normal", -IFF_LINK0, setifflags, NULL },
283 { "compress", IFF_LINK0, setifflags, NULL },
284 { "noicmp", IFF_LINK1, setifflags, NULL },
285 { "mtu", NEXTARG, setifmtu, NULL },
286 { "name", NEXTARG, setifname, NULL },
287 { NULL, 0, setifaddr, NULL },
288 { NULL, 0, setifdstaddr, NULL },
292 * XNS support liberally adapted from code written at the University of
293 * Maryland principally by James O'Toole and Chris Torek.
295 typedef void af_status(int, struct rt_addrinfo *);
296 typedef void af_getaddr(const char *, int);
297 typedef void af_getprefix(const char *, int);
299 af_status in_status, at_status, link_status;
300 af_getaddr in_getaddr, at_getaddr, link_getaddr;
303 af_status ipx_status;
304 af_getaddr ipx_getaddr;
308 af_status in6_status;
309 af_getaddr in6_getaddr;
310 af_getprefix in6_getprefix;
313 af_status xns_status;
314 af_getaddr xns_getaddr;
317 /* Known address families */
322 af_status *af_status;
323 af_getaddr *af_getaddr;
324 af_getprefix *af_getprefix;
330 #define C(x) ((caddr_t) &x)
331 { "inet", AF_INET, in_status, in_getaddr, NULL,
332 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
334 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
335 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
336 C(in6_ridreq), C(in6_addreq) },
339 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
340 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
342 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
343 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
345 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
346 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
348 { "link", AF_LINK, link_status, link_getaddr, NULL,
349 0, SIOCSIFLLADDR, NULL, C(ridreq) },
350 { "ether", AF_LINK, link_status, link_getaddr, NULL,
351 0, SIOCSIFLLADDR, NULL, C(ridreq) },
352 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
353 0, SIOCSIFLLADDR, NULL, C(ridreq) },
354 #if 0 /* XXX conflicts with the media command */
356 { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */
359 { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, }, /* XXX not real!! */
362 { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, }, /* XXX not real!! */
365 { NULL, 0, NULL, NULL, NULL, 0, 0, NULL, NULL }
369 * Expand the compacted form of addresses as returned via the
370 * configuration read via sysctl().
374 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
375 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
378 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
383 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
384 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
385 if ((rtinfo->rti_addrs & (1 << i)) == 0)
387 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
397 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
398 "usage: ifconfig interface address_family [address [dest_address]]",
401 " ifconfig interface create",
402 " ifconfig -a [-d] [-m] [-u] [address_family]",
403 " ifconfig -l [-d] [-u] [address_family]",
404 " ifconfig [-d] [-m] [-u]");
406 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
407 "usage: ifconfig [-L] interface address_family [address [dest_address]]",
410 " ifconfig interface create",
411 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
412 " ifconfig -l [-d] [-u] [address_family]",
413 " ifconfig [-L] [-d] [-m] [-u]");
419 main(int argc, char * const *argv)
422 int all, namesonly, downonly, uponly;
424 const struct afswtch *afp = 0;
425 int addrcount, ifindex;
426 struct if_msghdr *ifm, *nextifm;
427 struct ifa_msghdr *ifam;
428 struct sockaddr_dl *sdl;
429 char *buf, *lim, *next;
434 /* Parse leading line options */
435 all = downonly = uponly = namesonly = 0;
436 while ((c = getopt(argc, argv, "adlmuC"
442 case 'a': /* scan all interfaces */
445 case 'd': /* restrict scan to "down" interfaces */
448 case 'l': /* scan interface names only */
451 case 'm': /* show media choices in status */
454 case 'u': /* restrict scan to "up" interfaces */
462 ip6lifetime++; /* print IPv6 address lifetime */
474 /* -C must be solitary */
475 if (all || supmedia || uponly || downonly || namesonly ||
483 /* -l cannot be used with -a or -m */
484 if (namesonly && (all || supmedia))
488 if (uponly && downonly)
491 /* no arguments is equivalent to '-a' */
492 if (!namesonly && argc < 1)
495 /* -a and -l allow an address family arg to limit the output */
496 if (all || namesonly) {
502 for (afp = afs; afp->af_name; afp++)
503 if (strcmp(afp->af_name, *argv) == 0) {
507 if (afp->af_name == NULL)
509 /* leave with afp non-zero */
512 /* not listing, need an argument */
516 strncpy(name, *argv, sizeof(name));
519 /* check and maybe load support for this interface */
523 * NOTE: We must special-case the `create' command right
524 * here as we would otherwise fail when trying to find
527 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
528 strcmp(argv[0], "plumb") == 0)) {
534 ifindex = if_nametoindex(name);
536 errx(1, "interface %s does not exist", name);
539 /* Check for address family */
541 for (afp = afs; afp->af_name; afp++)
542 if (strcmp(afp->af_name, *argv) == 0) {
546 if (afp->af_name == NULL)
547 afp = NULL; /* not a family, NULL */
553 mib[3] = 0; /* address family */
554 mib[4] = NET_RT_IFLIST;
555 mib[5] = ifindex; /* interface index */
557 /* if particular family specified, only ask about it */
561 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
562 errx(1, "iflist-sysctl-estimate");
563 if ((buf = malloc(needed)) == NULL)
565 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
566 errx(1, "actual retrieval of interface table");
572 ifm = (struct if_msghdr *)next;
574 if (ifm->ifm_type == RTM_IFINFO) {
575 sdl = (struct sockaddr_dl *)(ifm + 1);
576 flags = ifm->ifm_flags;
578 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
579 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
581 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
582 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
587 next += ifm->ifm_msglen;
592 nextifm = (struct if_msghdr *)next;
594 if (nextifm->ifm_type != RTM_NEWADDR)
598 ifam = (struct ifa_msghdr *)nextifm;
601 next += nextifm->ifm_msglen;
603 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
604 name[sdl->sdl_nlen] = '\0';
606 if (all || namesonly) {
609 /* sdl_data may not be terminated, don't use strlcpy */
610 if ((len = sdl->sdl_nlen) > sizeof(name) - 1)
611 len = sizeof(name) - 1;
612 bcopy(sdl->sdl_data, name, len);
616 if ((flags & IFF_UP) == 0)
617 continue; /* not up */
620 continue; /* not down */
623 afp->af_status != link_status ||
624 sdl->sdl_type == IFT_ETHER) {
635 ifconfig(argc, argv, afp);
637 status(afp, addrcount, sdl, ifm, ifam);
641 if (namesonly && need_nl > 0)
645 printf("%s\n", name);
652 ifconfig(int argc, char *const *argv, const struct afswtch *afp)
658 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
659 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
661 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
665 register const struct cmd *p;
667 for (p = cmds; p->c_name; p++)
668 if (strcmp(*argv, p->c_name) == 0)
670 if (p->c_name == 0 && setaddr)
671 p++; /* got src, do dst */
672 if (p->c_func || p->c_func2) {
673 if (p->c_parameter == NEXTARG) {
675 errx(1, "'%s' requires argument",
677 (*p->c_func)(argv[1], 0, s, afp);
679 } else if (p->c_parameter == NEXTARG2) {
681 errx(1, "'%s' requires 2 arguments",
683 (*p->c_func2)(argv[1], argv[2], s, afp);
684 argc -= 2, argv += 2;
686 (*p->c_func)(*argv, p->c_parameter, s, afp);
691 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
692 /* Aggregatable address architecture defines all prefixes
693 are 64. So, it is convenient to set prefixlen to 64 if
694 it is not specified. */
695 setifprefixlen("64", 0, s, afp);
696 /* in6_getprefix("64", MASK) if MASK is available here... */
700 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
702 int size = sizeof(rq);
704 rq.rq_ipx = addreq.ifra_addr;
705 rq.rq_ip = addreq.ifra_dstaddr;
707 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
708 Perror("Encapsulation Routing");
711 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
712 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
714 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
716 int size = sizeof(rq);
718 rq.rq_ns = addreq.ifra_addr;
719 rq.rq_ip = addreq.ifra_dstaddr;
721 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
722 Perror("Encapsulation Routing");
726 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
727 warnx("interface %s cannot change %s addresses!",
734 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
735 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
736 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
737 /* means no previous address for interface */
739 Perror("ioctl (SIOCDIFADDR)");
743 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
744 warnx("interface %s cannot change %s addresses!",
749 if (newaddr && (setaddr || setmask)) {
750 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
751 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
752 Perror("ioctl (SIOCAIFADDR)");
764 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
766 if (*afp->af_getaddr == NULL)
769 * Delay the ioctl to set the interface addr until flags are all set.
770 * The address interpretation may depend on the flags,
771 * and the flags may change when the address is set.
774 if (doalias == 0 && afp->af_af != AF_LINK)
776 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
780 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
782 struct addrinfo hints, *srcres, *dstres;
783 struct ifaliasreq addreq;
786 struct in6_aliasreq in6_addreq;
789 memset(&hints, 0, sizeof(hints));
790 hints.ai_family = afp->af_af;
792 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
793 errx(1, "error in parsing address string: %s",
794 gai_strerror(ecode));
796 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
797 errx(1, "error in parsing address string: %s",
798 gai_strerror(ecode));
800 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
802 "source and destination address families do not match");
804 switch (srcres->ai_addr->sa_family) {
806 memset(&addreq, 0, sizeof(addreq));
807 strncpy(addreq.ifra_name, name, IFNAMSIZ);
808 memcpy(&addreq.ifra_addr, srcres->ai_addr,
809 srcres->ai_addr->sa_len);
810 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
811 dstres->ai_addr->sa_len);
813 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
814 warn("SIOCSIFPHYADDR");
819 memset(&in6_addreq, 0, sizeof(in6_addreq));
820 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
821 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
822 srcres->ai_addr->sa_len);
823 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
824 dstres->ai_addr->sa_len);
826 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
827 warn("SIOCSIFPHYADDR_IN6");
832 warn("address family not supported");
835 freeaddrinfo(srcres);
836 freeaddrinfo(dstres);
841 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
844 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
845 err(1, "SIOCDIFPHYADDR");
849 setifnetmask(const char *addr, int dummy __unused, int s,
850 const struct afswtch *afp)
852 if (*afp->af_getaddr == NULL)
855 (*afp->af_getaddr)(addr, MASK);
860 setifprefixlen(const char *addr, int dummy __unused, int s,
861 const struct afswtch *afp)
863 if (*afp->af_getprefix)
864 (*afp->af_getprefix)(addr, MASK);
869 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
870 const struct afswtch *afp)
872 if (afp->af_af != AF_INET6)
873 err(1, "address flags can be set only for inet6 addresses");
876 in6_addreq.ifra_flags &= ~(-flag);
878 in6_addreq.ifra_flags |= flag;
882 setip6pltime(const char *seconds, int dummy __unused, int s,
883 const struct afswtch *afp)
885 setip6lifetime("pltime", seconds, s, afp);
889 setip6vltime(const char *seconds, int dummy __unused, int s,
890 const struct afswtch *afp)
892 setip6lifetime("vltime", seconds, s, afp);
896 setip6lifetime(const char *cmd, const char *val, int s,
897 const struct afswtch *afp)
903 newval = (time_t)strtoul(val, &ep, 0);
905 errx(1, "invalid %s", cmd);
906 if (afp->af_af != AF_INET6)
907 errx(1, "%s not allowed for the AF", cmd);
908 if (strcmp(cmd, "vltime") == 0) {
909 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
910 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
911 } else if (strcmp(cmd, "pltime") == 0) {
912 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
913 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
918 setip6eui64(const char *cmd, int dummy __unused, int s,
919 const struct afswtch *afp)
921 struct ifaddrs *ifap, *ifa;
922 const struct sockaddr_in6 *sin6 = NULL;
923 const struct in6_addr *lladdr = NULL;
924 struct in6_addr *in6;
926 if (afp->af_af != AF_INET6)
927 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
928 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
929 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
930 errx(EXIT_FAILURE, "interface index is already filled");
931 if (getifaddrs(&ifap) != 0)
932 err(EXIT_FAILURE, "getifaddrs");
933 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
934 if (ifa->ifa_addr->sa_family == AF_INET6 &&
935 strcmp(ifa->ifa_name, name) == 0) {
936 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
937 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
938 lladdr = &sin6->sin6_addr;
944 errx(EXIT_FAILURE, "could not determine link local address");
946 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
953 setifbroadaddr(const char *addr, int dummy __unused, int s,
954 const struct afswtch *afp)
956 if (*afp->af_getaddr == NULL)
958 (*afp->af_getaddr)(addr, DSTADDR);
962 setifipdst(const char *addr, int dummy __unused, int s,
963 const struct afswtch *afp)
965 in_getaddr(addr, DSTADDR);
970 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
973 notealias(const char *addr, int param, int s, const struct afswtch *afp)
975 if (setaddr && doalias == 0 && param < 0)
976 bcopy((caddr_t)rqtosa(af_addreq),
977 (caddr_t)rqtosa(af_ridreq),
978 rqtosa(af_addreq)->sa_len);
989 setifdstaddr(const char *addr, int param __unused, int s,
990 const struct afswtch *afp)
992 if (*afp->af_getaddr == NULL)
994 (*afp->af_getaddr)(addr, DSTADDR);
998 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
999 * of the ifreq structure, which may confuse other parts of ifconfig.
1000 * Make a private copy so we can avoid that.
1003 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1005 struct ifreq my_ifr;
1007 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1009 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1010 Perror("ioctl (SIOCGIFFLAGS)");
1013 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1014 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1021 my_ifr.ifr_flags = flags & 0xffff;
1022 my_ifr.ifr_flagshigh = flags >> 16;
1023 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1028 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1031 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1032 Perror("ioctl (SIOCGIFCAP)");
1035 flags = ifr.ifr_curcap;
1041 ifr.ifr_reqcap = flags;
1042 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1047 setifmetric(const char *val, int dummy __unused, int s,
1048 const struct afswtch *afp)
1050 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1051 ifr.ifr_metric = atoi(val);
1052 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1053 warn("ioctl (set metric)");
1057 setifmtu(const char *val, int dummy __unused, int s, const struct afswtch *afp)
1059 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1060 ifr.ifr_mtu = atoi(val);
1061 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1062 warn("ioctl (set mtu)");
1066 setifname(const char *val, int dummy __unused, int s,
1067 const struct afswtch *afp)
1071 newname = strdup(val);
1073 ifr.ifr_data = newname;
1074 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1075 warn("ioctl (set name)");
1079 strlcpy(name, newname, sizeof(name));
1084 * Even if we just created the interface, we don't need to print
1085 * its name because we just nailed it down separately.
1091 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1092 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1096 "\003\1rxcsum\2txcsum\3netcons\7polling"
1099 * Print the status of the interface. If an address family was
1100 * specified, show it and it only; otherwise, show them all.
1103 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1104 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1106 const struct afswtch *p = NULL;
1107 struct rt_addrinfo info;
1117 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1118 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1120 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1123 printf("%s: ", name);
1124 printb("flags", flags, IFFBITS);
1125 if (ifm->ifm_data.ifi_metric)
1126 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1127 if (ifm->ifm_data.ifi_mtu)
1128 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1131 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1132 if (ifr.ifr_curcap != 0) {
1133 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1136 if (supmedia && ifr.ifr_reqcap != 0) {
1137 printf("\tcapability list:\n");
1138 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1145 while (addrcount > 0) {
1147 info.rti_addrs = ifam->ifam_addrs;
1149 /* Expand the compacted addresses */
1150 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1154 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1156 (*p->af_status)(s, &info);
1158 } else for (p = afs; p->af_name; p++) {
1159 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1160 (*p->af_status)(s, &info);
1163 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1165 if (allfamilies || afp->af_status == link_status)
1166 link_status(s, (struct rt_addrinfo *)sdl);
1168 if (allfamilies || afp->af_status == media_status)
1169 media_status(s, NULL);
1172 if (allfamilies || afp->af_status == vlan_status)
1173 vlan_status(s, NULL);
1175 #ifdef USE_IEEE80211
1176 if (allfamilies || afp->af_status == ieee80211_status)
1177 ieee80211_status(s, NULL);
1179 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1180 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1181 printf("%s", ifs.ascii);
1183 if (!allfamilies && !p && afp->af_status != media_status &&
1184 afp->af_status != link_status
1186 && afp->af_status != vlan_status
1189 warnx("%s has no %s interface address!", name, afp->af_name);
1196 tunnel_status(int s)
1198 char psrcaddr[NI_MAXHOST];
1199 char pdstaddr[NI_MAXHOST];
1200 u_long srccmd, dstcmd;
1202 const char *ver = "";
1203 #ifdef NI_WITHSCOPEID
1204 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1206 const int niflag = NI_NUMERICHOST;
1209 struct in6_ifreq in6_ifr;
1213 psrcaddr[0] = pdstaddr[0] = '\0';
1216 memset(&in6_ifr, 0, sizeof(in6_ifr));
1217 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1218 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1220 srccmd = SIOCGIFPSRCADDR;
1221 dstcmd = SIOCGIFPDSTADDR;
1225 srccmd = SIOCGIFPSRCADDR_IN6;
1226 dstcmd = SIOCGIFPDSTADDR_IN6;
1227 ifrp = (struct ifreq *)&in6_ifr;
1230 srccmd = SIOCGIFPSRCADDR;
1231 dstcmd = SIOCGIFPDSTADDR;
1235 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1238 if (ifrp->ifr_addr.sa_family == AF_INET6)
1239 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1241 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1242 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1244 if (ifrp->ifr_addr.sa_family == AF_INET6)
1248 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1251 if (ifrp->ifr_addr.sa_family == AF_INET6)
1252 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1254 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1255 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1257 printf("\ttunnel inet%s %s --> %s\n", ver,
1258 psrcaddr, pdstaddr);
1262 in_status(int s __unused, struct rt_addrinfo *info)
1264 struct sockaddr_in *sin, null_sin;
1266 memset(&null_sin, 0, sizeof(null_sin));
1268 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1269 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1271 if (flags & IFF_POINTOPOINT) {
1272 /* note RTAX_BRD overlap with IFF_BROADCAST */
1273 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1276 printf("--> %s ", inet_ntoa(sin->sin_addr));
1279 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1282 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1284 if (flags & IFF_BROADCAST) {
1285 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1286 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1287 if (sin && sin->sin_addr.s_addr != 0)
1288 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1295 in6_fillscopeid(struct sockaddr_in6 *sin6)
1297 #if defined(__KAME__) && defined(KAME_SCOPEID)
1298 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1299 sin6->sin6_scope_id =
1300 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1301 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1307 in6_status(int s __unused, struct rt_addrinfo *info)
1309 struct sockaddr_in6 *sin, null_sin;
1310 struct in6_ifreq ifr6;
1313 struct in6_addrlifetime lifetime;
1314 time_t t = time(NULL);
1318 memset(&null_sin, 0, sizeof(null_sin));
1320 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1321 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1322 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1323 perror("ifconfig: socket");
1326 ifr6.ifr_addr = *sin;
1327 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1328 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1332 flags6 = ifr6.ifr_ifru.ifru_flags6;
1333 memset(&lifetime, 0, sizeof(lifetime));
1334 ifr6.ifr_addr = *sin;
1335 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1336 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1340 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1343 /* XXX: embedded link local addr check */
1344 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1345 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1348 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1349 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1350 if (sin->sin6_scope_id == 0)
1351 sin->sin6_scope_id = ntohs(index);
1353 scopeid = sin->sin6_scope_id;
1355 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1356 sizeof(addr_buf), NULL, 0,
1357 NI_NUMERICHOST|NI_WITHSCOPEID);
1359 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1361 printf("\tinet6 %s ", addr_buf);
1363 if (flags & IFF_POINTOPOINT) {
1364 /* note RTAX_BRD overlap with IFF_BROADCAST */
1365 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1367 * some of the interfaces do not have valid destination
1370 if (sin && sin->sin6_family == AF_INET6) {
1373 /* XXX: embedded link local addr check */
1374 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1375 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1378 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1379 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1380 if (sin->sin6_scope_id == 0)
1381 sin->sin6_scope_id = ntohs(index);
1384 error = getnameinfo((struct sockaddr *)sin,
1385 sin->sin6_len, addr_buf,
1386 sizeof(addr_buf), NULL, 0,
1387 NI_NUMERICHOST|NI_WITHSCOPEID);
1389 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1391 printf("--> %s ", addr_buf);
1395 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1398 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1399 sizeof(struct in6_addr)));
1401 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1403 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1404 printf("tentative ");
1405 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1406 printf("duplicated ");
1407 if ((flags6 & IN6_IFF_DETACHED) != 0)
1408 printf("detached ");
1409 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1410 printf("deprecated ");
1411 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1412 printf("autoconf ");
1413 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1414 printf("temporary ");
1417 printf("scopeid 0x%x ", scopeid);
1419 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1421 if (lifetime.ia6t_preferred) {
1422 printf("%s ", lifetime.ia6t_preferred < t
1423 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1428 if (lifetime.ia6t_expire) {
1429 printf("%s ", lifetime.ia6t_expire < t
1430 ? "0" : sec2str(lifetime.ia6t_expire - t));
1441 ipx_status(int s __unused, struct rt_addrinfo *info)
1443 struct sockaddr_ipx *sipx, null_sipx;
1445 memset(&null_sipx, 0, sizeof(null_sipx));
1447 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1448 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1450 if (flags & IFF_POINTOPOINT) {
1451 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1454 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1461 at_status(int s __unused, struct rt_addrinfo *info)
1463 struct sockaddr_at *sat, null_sat;
1464 struct netrange *nr;
1466 memset(&null_sat, 0, sizeof(null_sat));
1468 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1469 nr = &sat->sat_range.r_netrange;
1470 printf("\tatalk %d.%d range %d-%d phase %d",
1471 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1472 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1473 if (flags & IFF_POINTOPOINT) {
1474 /* note RTAX_BRD overlap with IFF_BROADCAST */
1475 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1479 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1481 if (flags & IFF_BROADCAST) {
1482 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1483 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1485 printf(" broadcast %d.%d",
1486 ntohs(sat->sat_addr.s_net),
1487 sat->sat_addr.s_node);
1495 xns_status(int s __unused, struct rt_addrinfo *info)
1497 struct sockaddr_ns *sns, null_sns;
1499 memset(&null_sns, 0, sizeof(null_sns));
1501 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1502 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1504 if (flags & IFF_POINTOPOINT) {
1505 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1508 printf("--> %s ", ns_ntoa(sns->sns_addr));
1518 link_status(int s __unused, struct rt_addrinfo *info)
1520 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1522 if (sdl->sdl_alen > 0) {
1523 if (sdl->sdl_type == IFT_ETHER &&
1524 sdl->sdl_alen == ETHER_ADDR_LEN)
1525 printf("\tether %s\n",
1526 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1528 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1530 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1536 Perror(const char *cmd)
1541 errx(1, "%s: no such interface", cmd);
1545 errx(1, "%s: permission denied", cmd);
1553 #define SIN(x) ((struct sockaddr_in *) &(x))
1554 struct sockaddr_in *sintab[] = {
1555 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1556 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1559 in_getaddr(const char *s, int which)
1561 register struct sockaddr_in *sin = sintab[which];
1565 sin->sin_len = sizeof(*sin);
1567 sin->sin_family = AF_INET;
1569 if (which == ADDR) {
1572 if((p = strrchr(s, '/')) != NULL) {
1573 /* address is `name/masklen' */
1576 struct sockaddr_in *min = sintab[MASK];
1578 ret = sscanf(p+1, "%u", &masklen);
1579 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1581 errx(1, "%s: bad value", s);
1583 min->sin_len = sizeof(*min);
1584 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1589 if (inet_aton(s, &sin->sin_addr))
1591 if ((hp = gethostbyname(s)) != 0)
1592 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1593 MIN(hp->h_length, sizeof(sin->sin_addr)));
1594 else if ((np = getnetbyname(s)) != 0)
1595 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1597 errx(1, "%s: bad value", s);
1601 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1602 struct sockaddr_in6 *sin6tab[] = {
1603 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1604 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1607 in6_getaddr(const char *s, int which)
1609 register struct sockaddr_in6 *sin = sin6tab[which];
1610 struct addrinfo hints, *res;
1615 sin->sin6_len = sizeof(*sin);
1617 sin->sin6_family = AF_INET6;
1619 if (which == ADDR) {
1621 if((p = strrchr(s, '/')) != NULL) {
1623 in6_getprefix(p + 1, MASK);
1624 explicit_prefix = 1;
1628 if (sin->sin6_family == AF_INET6) {
1629 bzero(&hints, sizeof(struct addrinfo));
1630 hints.ai_family = AF_INET6;
1631 error = getaddrinfo(s, NULL, &hints, &res);
1634 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1635 errx(1, "%s: bad value", s);
1637 bcopy(res->ai_addr, sin, res->ai_addrlen);
1641 in6_getprefix(const char *plen, int which)
1643 register struct sockaddr_in6 *sin = sin6tab[which];
1644 register u_char *cp;
1645 int len = atoi(plen);
1647 if ((len < 0) || (len > 128))
1648 errx(1, "%s: bad value", plen);
1649 sin->sin6_len = sizeof(*sin);
1651 sin->sin6_family = AF_INET6;
1652 if ((len == 0) || (len == 128)) {
1653 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1656 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1657 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1659 *cp = 0xff << (8 - len);
1664 * Print a value a la the %b format of the kernel's printf
1667 printb(const char *s, register unsigned v, register const char *bits)
1669 register int i, any = 0;
1672 if (bits && *bits == 8)
1673 printf("%s=%o", s, v);
1675 printf("%s=%x", s, v);
1679 while ((i = *bits++) != '\0') {
1680 if (v & (1 << (i-1))) {
1684 for (; (c = *bits) > 32; bits++)
1687 for (; *bits > 32; bits++)
1695 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1696 struct sockaddr_ipx *sipxtab[] = {
1697 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1698 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1701 ipx_getaddr(const char *addr, int which)
1703 struct sockaddr_ipx *sipx = sipxtab[which];
1705 sipx->sipx_family = AF_IPX;
1706 sipx->sipx_len = sizeof(*sipx);
1707 sipx->sipx_addr = ipx_addr(addr);
1709 printf("Attempt to set IPX netmask will be ineffectual\n");
1714 at_getaddr(const char *addr, int which)
1716 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1719 sat->sat_family = AF_APPLETALK;
1720 sat->sat_len = sizeof(*sat);
1722 errx(1, "AppleTalk does not use netmasks");
1723 if (sscanf(addr, "%u.%u", &net, &node) != 2
1724 || net > 0xffff || node > 0xfe)
1725 errx(1, "%s: illegal address", addr);
1726 sat->sat_addr.s_net = htons(net);
1727 sat->sat_addr.s_node = node;
1731 link_getaddr(const char *addr, int which)
1734 struct sockaddr_dl sdl;
1735 struct sockaddr *sa = &ridreq.ifr_addr;
1738 errx(1, "can't set link-level netmask or broadcast");
1739 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1740 errx(1, "malloc failed");
1742 strcpy(temp + 1, addr);
1743 sdl.sdl_len = sizeof(sdl);
1744 link_addr(temp, &sdl);
1746 if (sdl.sdl_alen > sizeof(sa->sa_data))
1747 errx(1, "malformed link-level address");
1748 sa->sa_family = AF_LINK;
1749 sa->sa_len = sdl.sdl_alen;
1750 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1753 /* XXX FIXME -- should use strtoul for better parsing. */
1755 setatrange(const char *range, int dummy __unused, int s,
1756 const struct afswtch *afp)
1758 u_short first = 123, last = 123;
1760 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1761 || first == 0 || first > 0xffff
1762 || last == 0 || last > 0xffff || first > last)
1763 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1764 at_nr.nr_firstnet = htons(first);
1765 at_nr.nr_lastnet = htons(last);
1769 setatphase(const char *phase, int dummy __unused, int s,
1770 const struct afswtch *afp)
1772 if (!strcmp(phase, "1"))
1774 else if (!strcmp(phase, "2"))
1777 errx(1, "%s: illegal phase", phase);
1781 checkatrange(struct sockaddr_at *sat)
1783 if (at_nr.nr_phase == 0)
1784 at_nr.nr_phase = 2; /* Default phase 2 */
1785 if (at_nr.nr_firstnet == 0)
1786 at_nr.nr_firstnet = /* Default range of one */
1787 at_nr.nr_lastnet = sat->sat_addr.s_net;
1788 printf("\tatalk %d.%d range %d-%d phase %d\n",
1789 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1790 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1791 if ((u_short) ntohs(at_nr.nr_firstnet) >
1792 (u_short) ntohs(sat->sat_addr.s_net)
1793 || (u_short) ntohs(at_nr.nr_lastnet) <
1794 (u_short) ntohs(sat->sat_addr.s_net))
1795 errx(1, "AppleTalk address is not in range");
1796 sat->sat_range.r_netrange = at_nr;
1800 #define SNS(x) ((struct sockaddr_ns *) &(x))
1801 struct sockaddr_ns *snstab[] = {
1802 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1803 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1806 xns_getaddr(const char *addr, int which)
1808 struct sockaddr_ns *sns = snstab[which];
1810 sns->sns_family = AF_NS;
1811 sns->sns_len = sizeof(*sns);
1812 sns->sns_addr = ns_addr(addr);
1814 printf("Attempt to set XNS netmask will be ineffectual\n");
1820 prefix(void *val, int size)
1822 register u_char *name = (u_char *)val;
1823 register int byte, bit, plen = 0;
1825 for (byte = 0; byte < size; byte++, plen += 8)
1826 if (name[byte] != 0xff)
1830 for (bit = 7; bit != 0; bit--, plen++)
1831 if (!(name[byte] & (1 << bit)))
1833 for (; bit != 0; bit--)
1834 if (name[byte] & (1 << bit))
1837 for (; byte < size; byte++)
1844 sec2str(time_t total)
1846 static char result[256];
1847 int days, hours, mins, secs;
1852 days = total / 3600 / 24;
1853 hours = (total / 3600) % 24;
1854 mins = (total / 60) % 60;
1859 p += sprintf(p, "%dd", days);
1861 if (!first || hours) {
1863 p += sprintf(p, "%dh", hours);
1865 if (!first || mins) {
1867 p += sprintf(p, "%dm", mins);
1869 sprintf(p, "%ds", secs);
1871 sprintf(result, "%lu", (unsigned long)total);
1878 ifmaybeload(char *name)
1880 struct module_stat mstat;
1882 char ifkind[35], *cp, *dp;
1885 /* turn interface and unit into module name */
1886 strcpy(ifkind, "if_");
1887 for (cp = name, dp = ifkind + 3;
1888 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1892 /* scan files in kernel */
1893 mstat.version = sizeof(struct module_stat);
1894 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1895 /* scan modules in file */
1896 for (modid = kldfirstmod(fileid); modid > 0;
1897 modid = modfnext(modid)) {
1898 if (modstat(modid, &mstat) < 0)
1900 /* strip bus name if present */
1901 if ((cp = strchr(mstat.name, '/')) != NULL) {
1906 /* already loaded? */
1907 if (strncmp(name, cp, strlen(cp)) == 0 ||
1908 strncmp(ifkind, cp, strlen(cp)) == 0)
1913 /* not present, we should try to load it */
1920 struct if_clonereq ifcr;
1925 s = socket(AF_INET, SOCK_DGRAM, 0);
1929 memset(&ifcr, 0, sizeof(ifcr));
1931 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1932 err(1, "SIOCIFGCLONERS for count");
1934 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1936 err(1, "unable to allocate cloner name buffer");
1938 ifcr.ifcr_count = ifcr.ifcr_total;
1939 ifcr.ifcr_buffer = buf;
1941 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1942 err(1, "SIOCIFGCLONERS for names");
1945 * In case some disappeared in the mean time, clamp it down.
1947 if (ifcr.ifcr_count > ifcr.ifcr_total)
1948 ifcr.ifcr_count = ifcr.ifcr_total;
1950 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1965 s = socket(AF_INET, SOCK_DGRAM, 0);
1969 memset(&ifr, 0, sizeof(ifr));
1970 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1971 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
1972 err(1, "SIOCIFCREATE");
1975 * If we get a different name back then we put in, we probably
1976 * want to print it out, but we might change our mind later so
1977 * we just signal our intrest and leave the printout for later.
1979 if (strcmp(name, ifr.ifr_name) != 0) {
1981 strlcpy(name, ifr.ifr_name, sizeof(name));
1988 clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
1991 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1992 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
1993 err(1, "SIOCIFDESTROY");
1995 * If we create and destroy an interface in the same command,
1996 * there isn't any reason to print it's name.