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.19 2005/03/04 02:37:44 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 *if_nm);
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)
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 __unused, int s __unused,
765 const struct afswtch *afp)
767 if (*afp->af_getaddr == NULL)
770 * Delay the ioctl to set the interface addr until flags are all set.
771 * The address interpretation may depend on the flags,
772 * and the flags may change when the address is set.
775 if (doalias == 0 && afp->af_af != AF_LINK)
777 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
781 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
783 struct addrinfo hints, *srcres, *dstres;
784 struct ifaliasreq addr_req;
787 struct in6_aliasreq in6_addr_req;
790 memset(&hints, 0, sizeof(hints));
791 hints.ai_family = afp->af_af;
793 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
794 errx(1, "error in parsing address string: %s",
795 gai_strerror(ecode));
797 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
798 errx(1, "error in parsing address string: %s",
799 gai_strerror(ecode));
801 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
803 "source and destination address families do not match");
805 switch (srcres->ai_addr->sa_family) {
807 memset(&addr_req, 0, sizeof(addr_req));
808 strncpy(addr_req.ifra_name, name, IFNAMSIZ);
809 memcpy(&addr_req.ifra_addr, srcres->ai_addr,
810 srcres->ai_addr->sa_len);
811 memcpy(&addr_req.ifra_dstaddr, dstres->ai_addr,
812 dstres->ai_addr->sa_len);
814 if (ioctl(s, SIOCSIFPHYADDR, &addr_req) < 0)
815 warn("SIOCSIFPHYADDR");
820 memset(&in6_addr_req, 0, sizeof(in6_addr_req));
821 strncpy(in6_addr_req.ifra_name, name, IFNAMSIZ);
822 memcpy(&in6_addr_req.ifra_addr, srcres->ai_addr,
823 srcres->ai_addr->sa_len);
824 memcpy(&in6_addr_req.ifra_dstaddr, dstres->ai_addr,
825 dstres->ai_addr->sa_len);
827 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addr_req) < 0)
828 warn("SIOCSIFPHYADDR_IN6");
833 warn("address family not supported");
836 freeaddrinfo(srcres);
837 freeaddrinfo(dstres);
842 deletetunnel(const char *vname __unused, int param __unused, int s,
843 const struct afswtch *afp __unused)
846 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
847 err(1, "SIOCDIFPHYADDR");
851 setifnetmask(const char *addr, int dummy __unused, int s __unused,
852 const struct afswtch *afp)
854 if (*afp->af_getaddr == NULL)
857 (*afp->af_getaddr)(addr, MASK);
862 setifprefixlen(const char *addr, int dummy __unused, int s __unused,
863 const struct afswtch *afp)
865 if (*afp->af_getprefix)
866 (*afp->af_getprefix)(addr, MASK);
871 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
872 const struct afswtch *afp)
874 if (afp->af_af != AF_INET6)
875 err(1, "address flags can be set only for inet6 addresses");
878 in6_addreq.ifra_flags &= ~(-flag);
880 in6_addreq.ifra_flags |= flag;
884 setip6pltime(const char *seconds, int dummy __unused, int s,
885 const struct afswtch *afp)
887 setip6lifetime("pltime", seconds, s, afp);
891 setip6vltime(const char *seconds, int dummy __unused, int s,
892 const struct afswtch *afp)
894 setip6lifetime("vltime", seconds, s, afp);
898 setip6lifetime(const char *cmd, const char *val, int s __unused,
899 const struct afswtch *afp)
905 newval = (time_t)strtoul(val, &ep, 0);
907 errx(1, "invalid %s", cmd);
908 if (afp->af_af != AF_INET6)
909 errx(1, "%s not allowed for the AF", cmd);
910 if (strcmp(cmd, "vltime") == 0) {
911 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
912 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
913 } else if (strcmp(cmd, "pltime") == 0) {
914 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
915 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
920 setip6eui64(const char *cmd, int dummy __unused, int s __unused,
921 const struct afswtch *afp)
923 struct ifaddrs *ifap, *ifa;
924 const struct sockaddr_in6 *sin6 = NULL;
925 const struct in6_addr *lladdr = NULL;
926 struct in6_addr *in6;
928 if (afp->af_af != AF_INET6)
929 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
930 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
931 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
932 errx(EXIT_FAILURE, "interface index is already filled");
933 if (getifaddrs(&ifap) != 0)
934 err(EXIT_FAILURE, "getifaddrs");
935 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
936 if (ifa->ifa_addr->sa_family == AF_INET6 &&
937 strcmp(ifa->ifa_name, name) == 0) {
938 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
939 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
940 lladdr = &sin6->sin6_addr;
946 errx(EXIT_FAILURE, "could not determine link local address");
948 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
955 setifbroadaddr(const char *addr, int dummy __unused, int s __unused,
956 const struct afswtch *afp)
958 if (*afp->af_getaddr == NULL)
960 (*afp->af_getaddr)(addr, DSTADDR);
964 setifipdst(const char *addr, int dummy __unused, int s __unused,
965 const struct afswtch *afp __unused)
967 in_getaddr(addr, DSTADDR);
972 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
975 notealias(const char *addr __unused, int param, int s __unused,
976 const struct afswtch *afp)
978 if (setaddr && doalias == 0 && param < 0)
979 bcopy((caddr_t)rqtosa(af_addreq),
980 (caddr_t)rqtosa(af_ridreq),
981 rqtosa(af_addreq)->sa_len);
992 setifdstaddr(const char *addr, int param __unused, int s __unused,
993 const struct afswtch *afp)
995 if (*afp->af_getaddr == NULL)
997 (*afp->af_getaddr)(addr, DSTADDR);
1001 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1002 * of the ifreq structure, which may confuse other parts of ifconfig.
1003 * Make a private copy so we can avoid that.
1006 setifflags(const char *vname, int value, int s,
1007 const struct afswtch *afp __unused)
1009 struct ifreq my_ifr;
1011 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1013 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1014 Perror("ioctl (SIOCGIFFLAGS)");
1017 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1018 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1025 my_ifr.ifr_flags = flags & 0xffff;
1026 my_ifr.ifr_flagshigh = flags >> 16;
1027 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1032 setifcap(const char *vname, int value, int s,
1033 const struct afswtch *afp __unused)
1036 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1037 Perror("ioctl (SIOCGIFCAP)");
1040 flags = ifr.ifr_curcap;
1046 ifr.ifr_reqcap = flags;
1047 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1052 setifmetric(const char *val, int dummy __unused, int s,
1053 const struct afswtch *afp __unused)
1055 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1056 ifr.ifr_metric = atoi(val);
1057 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1058 warn("ioctl (set metric)");
1062 setifmtu(const char *val, int dummy __unused, int s,
1063 const struct afswtch *afp __unused)
1065 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1066 ifr.ifr_mtu = atoi(val);
1067 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1068 warn("ioctl (set mtu)");
1072 setifname(const char *val, int dummy __unused, int s,
1073 const struct afswtch *afp __unused)
1077 newname = strdup(val);
1079 ifr.ifr_data = newname;
1080 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1081 warn("ioctl (set name)");
1085 strlcpy(name, newname, sizeof(name));
1090 * Even if we just created the interface, we don't need to print
1091 * its name because we just nailed it down separately.
1097 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1098 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1102 "\003\1rxcsum\2txcsum\3netcons\7polling"
1105 * Print the status of the interface. If an address family was
1106 * specified, show it and it only; otherwise, show them all.
1109 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
1110 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
1112 const struct afswtch *p = NULL;
1113 struct rt_addrinfo info;
1123 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1124 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1126 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1129 printf("%s: ", name);
1130 printb("flags", flags, IFFBITS);
1131 if (ifm->ifm_data.ifi_metric)
1132 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1133 if (ifm->ifm_data.ifi_mtu)
1134 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1137 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1138 if (ifr.ifr_curcap != 0) {
1139 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1142 if (supmedia && ifr.ifr_reqcap != 0) {
1143 printf("\tcapability list:\n");
1144 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1151 while (addrcount > 0) {
1153 info.rti_addrs = ifam->ifam_addrs;
1155 /* Expand the compacted addresses */
1156 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1160 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1162 (*p->af_status)(s, &info);
1164 } else for (p = afs; p->af_name; p++) {
1165 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1166 (*p->af_status)(s, &info);
1169 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1171 if (allfamilies || afp->af_status == link_status)
1172 link_status(s, (struct rt_addrinfo *)sdl);
1174 if (allfamilies || afp->af_status == media_status)
1175 media_status(s, NULL);
1178 if (allfamilies || afp->af_status == vlan_status)
1179 vlan_status(s, NULL);
1181 #ifdef USE_IEEE80211
1182 if (allfamilies || afp->af_status == ieee80211_status)
1183 ieee80211_status(s, NULL);
1185 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1186 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1187 printf("%s", ifs.ascii);
1189 if (!allfamilies && !p && afp->af_status != media_status &&
1190 afp->af_status != link_status
1192 && afp->af_status != vlan_status
1195 warnx("%s has no %s interface address!", name, afp->af_name);
1202 tunnel_status(int s)
1204 char psrcaddr[NI_MAXHOST];
1205 char pdstaddr[NI_MAXHOST];
1206 u_long srccmd, dstcmd;
1208 const char *ver = "";
1209 #ifdef NI_WITHSCOPEID
1210 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1212 const int niflag = NI_NUMERICHOST;
1215 struct in6_ifreq in6_ifr;
1219 psrcaddr[0] = pdstaddr[0] = '\0';
1222 memset(&in6_ifr, 0, sizeof(in6_ifr));
1223 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1224 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1226 srccmd = SIOCGIFPSRCADDR;
1227 dstcmd = SIOCGIFPDSTADDR;
1231 srccmd = SIOCGIFPSRCADDR_IN6;
1232 dstcmd = SIOCGIFPDSTADDR_IN6;
1233 ifrp = (struct ifreq *)&in6_ifr;
1236 srccmd = SIOCGIFPSRCADDR;
1237 dstcmd = SIOCGIFPDSTADDR;
1241 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1243 #if defined(INET6) && defined(__KAME__) && defined(KAME_SCOPEID)
1244 if (ifrp->ifr_addr.sa_family == AF_INET6)
1245 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1247 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1248 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1250 if (ifrp->ifr_addr.sa_family == AF_INET6)
1254 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1256 #if defined(INET6) && defined(__KAME__) && defined(KAME_SCOPEID)
1257 if (ifrp->ifr_addr.sa_family == AF_INET6)
1258 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1260 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1261 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1263 printf("\ttunnel inet%s %s --> %s\n", ver,
1264 psrcaddr, pdstaddr);
1268 in_status(int s __unused, struct rt_addrinfo *info)
1270 struct sockaddr_in *addr_in, null_in;
1272 memset(&null_in, 0, sizeof(null_in));
1274 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1275 printf("\tinet %s ", inet_ntoa(addr_in->sin_addr));
1277 if (flags & IFF_POINTOPOINT) {
1278 /* note RTAX_BRD overlap with IFF_BROADCAST */
1279 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1282 printf("--> %s ", inet_ntoa(addr_in->sin_addr));
1285 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1288 printf("netmask 0x%lx ",
1289 (unsigned long)ntohl(addr_in->sin_addr.s_addr));
1291 if (flags & IFF_BROADCAST) {
1292 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1293 addr_in = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1294 if (addr_in && addr_in->sin_addr.s_addr != 0)
1295 printf("broadcast %s", inet_ntoa(addr_in->sin_addr));
1301 #if defined(__KAME__) && defined(KAME_SCOPEID)
1303 in6_fillscopeid(struct sockaddr_in6 *sin6)
1305 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1306 sin6->sin6_scope_id =
1307 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1308 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1311 #endif /* defined(__KAME__) && defined(KAME_SCOPEID) */
1314 in6_status(int s __unused, struct rt_addrinfo *info)
1316 struct sockaddr_in6 *addr_in, null_in;
1317 struct in6_ifreq ifr6;
1320 struct in6_addrlifetime lifetime;
1321 time_t t = time(NULL);
1325 memset(&null_in, 0, sizeof(null_in));
1327 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1328 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1329 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1330 perror("ifconfig: socket");
1333 ifr6.ifr_addr = *addr_in;
1334 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1335 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1339 flags6 = ifr6.ifr_ifru.ifru_flags6;
1340 memset(&lifetime, 0, sizeof(lifetime));
1341 ifr6.ifr_addr = *addr_in;
1342 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1343 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1347 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1350 /* XXX: embedded link local addr check */
1351 if (IN6_IS_ADDR_LINKLOCAL(&addr_in->sin6_addr) &&
1352 *(u_short *)&addr_in->sin6_addr.s6_addr[2] != 0) {
1355 idx = *(u_short *)&addr_in->sin6_addr.s6_addr[2];
1356 *(u_short *)&addr_in->sin6_addr.s6_addr[2] = 0;
1357 if (addr_in->sin6_scope_id == 0)
1358 addr_in->sin6_scope_id = ntohs(idx);
1360 scopeid = addr_in->sin6_scope_id;
1362 error = getnameinfo((struct sockaddr *)addr_in, addr_in->sin6_len, addr_buf,
1363 sizeof(addr_buf), NULL, 0,
1364 NI_NUMERICHOST|NI_WITHSCOPEID);
1366 inet_ntop(AF_INET6, &addr_in->sin6_addr, addr_buf,
1368 printf("\tinet6 %s ", addr_buf);
1370 if (flags & IFF_POINTOPOINT) {
1371 /* note RTAX_BRD overlap with IFF_BROADCAST */
1372 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1374 * some of the interfaces do not have valid destination
1377 if (addr_in && addr_in->sin6_family == AF_INET6) {
1378 /* XXX: embedded link local addr check */
1379 if (IN6_IS_ADDR_LINKLOCAL(&addr_in->sin6_addr) &&
1380 *(u_short *)&addr_in->sin6_addr.s6_addr[2] != 0) {
1383 idx = *(u_short *)&addr_in->sin6_addr.s6_addr[2];
1384 *(u_short *)&addr_in->sin6_addr.s6_addr[2] = 0;
1385 if (addr_in->sin6_scope_id == 0)
1386 addr_in->sin6_scope_id = ntohs(idx);
1389 error = getnameinfo((struct sockaddr *)addr_in,
1390 addr_in->sin6_len, addr_buf,
1391 sizeof(addr_buf), NULL, 0,
1392 NI_NUMERICHOST|NI_WITHSCOPEID);
1394 inet_ntop(AF_INET6, &addr_in->sin6_addr, addr_buf,
1396 printf("--> %s ", addr_buf);
1400 addr_in = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1403 printf("prefixlen %d ", prefix(&addr_in->sin6_addr,
1404 sizeof(struct in6_addr)));
1406 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1408 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1409 printf("tentative ");
1410 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1411 printf("duplicated ");
1412 if ((flags6 & IN6_IFF_DETACHED) != 0)
1413 printf("detached ");
1414 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1415 printf("deprecated ");
1416 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1417 printf("autoconf ");
1418 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1419 printf("temporary ");
1422 printf("scopeid 0x%x ", scopeid);
1424 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1426 if (lifetime.ia6t_preferred) {
1427 printf("%s ", lifetime.ia6t_preferred < t
1428 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1433 if (lifetime.ia6t_expire) {
1434 printf("%s ", lifetime.ia6t_expire < t
1435 ? "0" : sec2str(lifetime.ia6t_expire - t));
1446 ipx_status(int s __unused, struct rt_addrinfo *info)
1448 struct sockaddr_ipx *sipx, null_sipx;
1450 memset(&null_sipx, 0, sizeof(null_sipx));
1452 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1453 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1455 if (flags & IFF_POINTOPOINT) {
1456 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1459 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1466 at_status(int s __unused, struct rt_addrinfo *info)
1468 struct sockaddr_at *sat, null_sat;
1469 struct netrange *nr;
1471 memset(&null_sat, 0, sizeof(null_sat));
1473 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1474 nr = &sat->sat_range.r_netrange;
1475 printf("\tatalk %d.%d range %d-%d phase %d",
1476 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1477 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1478 if (flags & IFF_POINTOPOINT) {
1479 /* note RTAX_BRD overlap with IFF_BROADCAST */
1480 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1484 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1486 if (flags & IFF_BROADCAST) {
1487 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1488 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1490 printf(" broadcast %d.%d",
1491 ntohs(sat->sat_addr.s_net),
1492 sat->sat_addr.s_node);
1500 xns_status(int s __unused, struct rt_addrinfo *info)
1502 struct sockaddr_ns *sns, null_sns;
1504 memset(&null_sns, 0, sizeof(null_sns));
1506 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1507 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1509 if (flags & IFF_POINTOPOINT) {
1510 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1513 printf("--> %s ", ns_ntoa(sns->sns_addr));
1523 link_status(int s __unused, struct rt_addrinfo *info)
1525 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1527 if (sdl->sdl_alen > 0) {
1528 if (sdl->sdl_type == IFT_ETHER &&
1529 sdl->sdl_alen == ETHER_ADDR_LEN)
1530 printf("\tether %s\n",
1531 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1533 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1535 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1541 Perror(const char *cmd)
1546 errx(1, "%s: no such interface", cmd);
1550 errx(1, "%s: permission denied", cmd);
1558 #define SIN(x) ((struct sockaddr_in *) &(x))
1559 struct sockaddr_in *sintab[] = {
1560 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1561 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1564 in_getaddr(const char *s, int which)
1566 struct sockaddr_in *addr_in = sintab[which];
1570 addr_in->sin_len = sizeof(*addr_in);
1572 addr_in->sin_family = AF_INET;
1574 if (which == ADDR) {
1577 if((p = strrchr(s, '/')) != NULL) {
1578 /* address is `name/masklen' */
1581 struct sockaddr_in *min = sintab[MASK];
1583 ret = sscanf(p+1, "%u", &masklen);
1584 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1586 errx(1, "%s: bad value", s);
1588 min->sin_len = sizeof(*min);
1589 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1594 if (inet_aton(s, &addr_in->sin_addr))
1596 if ((hp = gethostbyname(s)) != 0)
1597 bcopy(hp->h_addr, (char *)&addr_in->sin_addr,
1598 MIN((size_t)hp->h_length, sizeof(addr_in->sin_addr)));
1599 else if ((np = getnetbyname(s)) != 0)
1600 addr_in->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1602 errx(1, "%s: bad value", s);
1606 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
1607 struct sockaddr_in6 *sin6tab[] = {
1608 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1609 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1612 in6_getaddr(const char *s, int which)
1614 struct sockaddr_in6 *addr_in = sin6tab[which];
1615 struct addrinfo hints, *res;
1620 addr_in->sin6_len = sizeof(*addr_in);
1622 addr_in->sin6_family = AF_INET6;
1624 if (which == ADDR) {
1626 if((p = strrchr(s, '/')) != NULL) {
1628 in6_getprefix(p + 1, MASK);
1629 explicit_prefix = 1;
1633 if (addr_in->sin6_family == AF_INET6) {
1634 bzero(&hints, sizeof(struct addrinfo));
1635 hints.ai_family = AF_INET6;
1636 error = getaddrinfo(s, NULL, &hints, &res);
1639 if (inet_pton(AF_INET6, s, &addr_in->sin6_addr) != 1)
1640 errx(1, "%s: bad value", s);
1642 bcopy(res->ai_addr, addr_in, res->ai_addrlen);
1646 in6_getprefix(const char *plen, int which)
1648 struct sockaddr_in6 *addr_in = sin6tab[which];
1650 int len = atoi(plen);
1652 if ((len < 0) || (len > 128))
1653 errx(1, "%s: bad value", plen);
1654 addr_in->sin6_len = sizeof(*addr_in);
1656 addr_in->sin6_family = AF_INET6;
1657 if ((len == 0) || (len == 128)) {
1658 memset(&addr_in->sin6_addr, 0xff, sizeof(struct in6_addr));
1661 memset((void *)&addr_in->sin6_addr, 0x00, sizeof(addr_in->sin6_addr));
1662 for (cp = (u_char *)&addr_in->sin6_addr; len > 7; len -= 8)
1664 *cp = 0xff << (8 - len);
1669 * Print a value a la the %b format of the kernel's printf
1672 printb(const char *s, unsigned v, const char *bits)
1677 if (bits && *bits == 8)
1678 printf("%s=%o", s, v);
1680 printf("%s=%x", s, v);
1684 while ((i = *bits++) != '\0') {
1685 if (v & (1 << (i-1))) {
1689 for (; (c = *bits) > 32; bits++)
1692 for (; *bits > 32; bits++)
1700 #define SIPX(x) ((struct sockaddr_ipx *) &(x))
1701 struct sockaddr_ipx *sipxtab[] = {
1702 SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1703 SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1706 ipx_getaddr(const char *addr, int which)
1708 struct sockaddr_ipx *sipx = sipxtab[which];
1710 sipx->sipx_family = AF_IPX;
1711 sipx->sipx_len = sizeof(*sipx);
1712 sipx->sipx_addr = ipx_addr(addr);
1714 printf("Attempt to set IPX netmask will be ineffectual\n");
1719 at_getaddr(const char *addr, int which)
1721 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1724 sat->sat_family = AF_APPLETALK;
1725 sat->sat_len = sizeof(*sat);
1727 errx(1, "AppleTalk does not use netmasks");
1728 if (sscanf(addr, "%u.%u", &net, &node) != 2
1729 || net > 0xffff || node > 0xfe)
1730 errx(1, "%s: illegal address", addr);
1731 sat->sat_addr.s_net = htons(net);
1732 sat->sat_addr.s_node = node;
1736 link_getaddr(const char *addr, int which)
1739 struct sockaddr_dl sdl;
1740 struct sockaddr *sa = &ridreq.ifr_addr;
1743 errx(1, "can't set link-level netmask or broadcast");
1744 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1745 errx(1, "malloc failed");
1747 strcpy(temp + 1, addr);
1748 sdl.sdl_len = sizeof(sdl);
1749 link_addr(temp, &sdl);
1751 if (sdl.sdl_alen > sizeof(sa->sa_data))
1752 errx(1, "malformed link-level address");
1753 sa->sa_family = AF_LINK;
1754 sa->sa_len = sdl.sdl_alen;
1755 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1758 /* XXX FIXME -- should use strtoul for better parsing. */
1760 setatrange(const char *range, int dummy __unused, int s __unused,
1761 const struct afswtch *afp __unused)
1763 u_short first = 123, last = 123;
1765 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1766 || first == 0 || first > 0xffff
1767 || last == 0 || last > 0xffff || first > last)
1768 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1769 at_nr.nr_firstnet = htons(first);
1770 at_nr.nr_lastnet = htons(last);
1774 setatphase(const char *phase, int dummy __unused, int s __unused,
1775 const struct afswtch *afp __unused)
1777 if (!strcmp(phase, "1"))
1779 else if (!strcmp(phase, "2"))
1782 errx(1, "%s: illegal phase", phase);
1786 checkatrange(struct sockaddr_at *sat)
1788 if (at_nr.nr_phase == 0)
1789 at_nr.nr_phase = 2; /* Default phase 2 */
1790 if (at_nr.nr_firstnet == 0)
1791 at_nr.nr_firstnet = /* Default range of one */
1792 at_nr.nr_lastnet = sat->sat_addr.s_net;
1793 printf("\tatalk %d.%d range %d-%d phase %d\n",
1794 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1795 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1796 if ((u_short) ntohs(at_nr.nr_firstnet) >
1797 (u_short) ntohs(sat->sat_addr.s_net)
1798 || (u_short) ntohs(at_nr.nr_lastnet) <
1799 (u_short) ntohs(sat->sat_addr.s_net))
1800 errx(1, "AppleTalk address is not in range");
1801 sat->sat_range.r_netrange = at_nr;
1805 #define SNS(x) ((struct sockaddr_ns *) &(x))
1806 struct sockaddr_ns *snstab[] = {
1807 SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1808 SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1811 xns_getaddr(const char *addr, int which)
1813 struct sockaddr_ns *sns = snstab[which];
1815 sns->sns_family = AF_NS;
1816 sns->sns_len = sizeof(*sns);
1817 sns->sns_addr = ns_addr(addr);
1819 printf("Attempt to set XNS netmask will be ineffectual\n");
1825 prefix(void *val, int size)
1827 u_char *addr = (u_char *)val;
1828 int byte, bit, plen = 0;
1830 for (byte = 0; byte < size; byte++, plen += 8)
1831 if (addr[byte] != 0xff)
1835 for (bit = 7; bit != 0; bit--, plen++)
1836 if (!(addr[byte] & (1 << bit)))
1838 for (; bit != 0; bit--)
1839 if (addr[byte] & (1 << bit))
1842 for (; byte < size; byte++)
1849 sec2str(time_t total)
1851 static char result[256];
1852 int days, hours, mins, secs;
1857 days = total / 3600 / 24;
1858 hours = (total / 3600) % 24;
1859 mins = (total / 60) % 60;
1864 p += sprintf(p, "%dd", days);
1866 if (!first || hours) {
1868 p += sprintf(p, "%dh", hours);
1870 if (!first || mins) {
1872 p += sprintf(p, "%dm", mins);
1874 sprintf(p, "%ds", secs);
1876 sprintf(result, "%lu", (unsigned long)total);
1883 ifmaybeload(char *if_nm)
1885 struct module_stat mstat;
1887 char ifkind[35], *cp, *dp;
1890 /* turn interface and unit into module name */
1891 strcpy(ifkind, "if_");
1892 for (cp = if_nm, dp = ifkind + 3;
1893 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1897 /* scan files in kernel */
1898 mstat.version = sizeof(struct module_stat);
1899 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1900 /* scan modules in file */
1901 for (modid = kldfirstmod(fileid); modid > 0;
1902 modid = modfnext(modid)) {
1903 if (modstat(modid, &mstat) < 0)
1905 /* strip bus name if present */
1906 if ((cp = strchr(mstat.name, '/')) != NULL) {
1911 /* already loaded? */
1912 if (strncmp(if_nm, cp, strlen(cp)) == 0 ||
1913 strncmp(ifkind, cp, strlen(cp)) == 0)
1918 /* not present, we should try to load it */
1925 struct if_clonereq ifcr;
1930 s = socket(AF_INET, SOCK_DGRAM, 0);
1934 memset(&ifcr, 0, sizeof(ifcr));
1936 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1937 err(1, "SIOCIFGCLONERS for count");
1939 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
1941 err(1, "unable to allocate cloner name buffer");
1943 ifcr.ifcr_count = ifcr.ifcr_total;
1944 ifcr.ifcr_buffer = buf;
1946 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
1947 err(1, "SIOCIFGCLONERS for names");
1950 * In case some disappeared in the mean time, clamp it down.
1952 if (ifcr.ifcr_count > ifcr.ifcr_total)
1953 ifcr.ifcr_count = ifcr.ifcr_total;
1955 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
1970 s = socket(AF_INET, SOCK_DGRAM, 0);
1974 memset(&ifr, 0, sizeof(ifr));
1975 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1976 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
1977 err(1, "SIOCIFCREATE");
1980 * If we get a different name back then we put in, we probably
1981 * want to print it out, but we might change our mind later so
1982 * we just signal our intrest and leave the printout for later.
1984 if (strcmp(name, ifr.ifr_name) != 0) {
1986 strlcpy(name, ifr.ifr_name, sizeof(name));
1993 clone_destroy(const char *val __unused, int d __unused, int s,
1994 const struct afswtch *rafp __unused)
1997 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1998 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
1999 err(1, "SIOCIFDESTROY");
2001 * If we create and destroy an interface in the same command,
2002 * there isn't any reason to print it's name.