namecache work stage 2: move struct namecache to its own header file and
[dragonfly.git] / sbin / ifconfig / ifconfig.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
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.51.2.19 2003/01/28 11:02:56 fjoe Exp $
36 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.2 2003/06/17 04:27:33 dillon Exp $
37 */
38
39#include <sys/param.h>
40#include <sys/ioctl.h>
41#include <sys/socket.h>
42#include <sys/sysctl.h>
43#include <sys/time.h>
44#include <sys/module.h>
45#include <sys/linker.h>
46
47#include <net/ethernet.h>
48#include <net/if.h>
49#include <net/if_var.h>
50#include <net/if_dl.h>
51#include <net/if_types.h>
52#include <net/route.h>
53
54/* IP */
55#include <netinet/in.h>
56#include <netinet/in_var.h>
57#include <arpa/inet.h>
58#include <netdb.h>
59
60#ifdef INET6
61#include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
62#endif
63
64#ifndef NO_IPX
65/* IPX */
66#define IPXIP
67#define IPTUNNEL
68#include <netipx/ipx.h>
69#include <netipx/ipx_if.h>
70#endif
71
72/* Appletalk */
73#include <netatalk/at.h>
74
75/* XNS */
76#ifdef NS
77#define NSIP
78#include <netns/ns.h>
79#include <netns/ns_if.h>
80#endif
81
82/* OSI */
83
84#include <ctype.h>
85#include <err.h>
86#include <errno.h>
87#include <fcntl.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
91#include <unistd.h>
92#include <ifaddrs.h>
93
94#include "ifconfig.h"
95
96/* wrapper for KAME-special getnameinfo() */
97#ifndef NI_WITHSCOPEID
98#define NI_WITHSCOPEID 0
99#endif
100
101struct ifreq ifr, ridreq;
102struct ifaliasreq addreq;
103#ifdef INET6
104struct in6_ifreq in6_ridreq;
105struct in6_aliasreq in6_addreq =
106 { { 0 },
107 { 0 },
108 { 0 },
109 { 0 },
110 0,
111 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
112#endif
113struct sockaddr_in netmask;
114struct netrange at_nr; /* AppleTalk net range */
115
116char name[32];
117int flags;
118int setaddr;
119int setipdst;
120int setmask;
121int doalias;
122int clearaddr;
123int newaddr = 1;
124#ifdef INET6
125static int ip6lifetime;
126#endif
127
128struct afswtch;
129
130int supmedia = 0;
131int listcloners = 0;
132
133#ifdef INET6
134char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
135#endif
136
137void Perror __P((const char *cmd));
138void checkatrange __P((struct sockaddr_at *));
139int ifconfig __P((int argc, char *const *argv, const struct afswtch *afp));
140void notealias __P((const char *, int, int, const struct afswtch *afp));
141void list_cloners __P((void));
142void printb __P((const char *s, unsigned value, const char *bits));
143void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
144void status __P((const struct afswtch *afp, int addrcount,
145 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
146 struct ifa_msghdr *ifam));
147void tunnel_status __P((int s));
148void usage __P((void));
149void ifmaybeload __P((char *name));
150
151#ifdef INET6
152void in6_fillscopeid __P((struct sockaddr_in6 *sin6));
153int prefix __P((void *, int));
154static char *sec2str __P((time_t));
155int explicit_prefix = 0;
156#endif
157
158typedef void c_func __P((const char *cmd, int arg, int s, const struct afswtch *afp));
159typedef void c_func2 __P((const char *arg, const char *arg2, int s, const struct afswtch *afp));
160c_func setatphase, setatrange;
161c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
162c_func2 settunnel;
163c_func deletetunnel;
164#ifdef INET6
165c_func setifprefixlen;
166c_func setip6flags;
167c_func setip6pltime;
168c_func setip6vltime;
169c_func2 setip6lifetime;
170c_func setip6eui64;
171#endif
172c_func setifipdst;
173c_func setifflags, setifmetric, setifmtu, setifcap;
174c_func clone_destroy;
175
176
177void clone_create __P((void));
178
179
180#define NEXTARG 0xffffff
181#define NEXTARG2 0xfffffe
182
183const
184struct cmd {
185 const char *c_name;
186 int c_parameter; /* NEXTARG means next argv */
187 void (*c_func) __P((const char *, int, int, const struct afswtch *afp));
188 void (*c_func2) __P((const char *, const char *, int, const struct afswtch *afp));
189} cmds[] = {
190 { "up", IFF_UP, setifflags } ,
191 { "down", -IFF_UP, setifflags },
192 { "arp", -IFF_NOARP, setifflags },
193 { "-arp", IFF_NOARP, setifflags },
194 { "debug", IFF_DEBUG, setifflags },
195 { "-debug", -IFF_DEBUG, setifflags },
196 { "promisc", IFF_PPROMISC, setifflags },
197 { "-promisc", -IFF_PPROMISC, setifflags },
198 { "add", IFF_UP, notealias },
199 { "alias", IFF_UP, notealias },
200 { "-alias", -IFF_UP, notealias },
201 { "delete", -IFF_UP, notealias },
202 { "remove", -IFF_UP, notealias },
203#ifdef notdef
204#define EN_SWABIPS 0x1000
205 { "swabips", EN_SWABIPS, setifflags },
206 { "-swabips", -EN_SWABIPS, setifflags },
207#endif
208 { "netmask", NEXTARG, setifnetmask },
209#ifdef INET6
210 { "prefixlen", NEXTARG, setifprefixlen },
211 { "anycast", IN6_IFF_ANYCAST, setip6flags },
212 { "tentative", IN6_IFF_TENTATIVE, setip6flags },
213 { "-tentative", -IN6_IFF_TENTATIVE, setip6flags },
214 { "deprecated", IN6_IFF_DEPRECATED, setip6flags },
215 { "-deprecated", -IN6_IFF_DEPRECATED, setip6flags },
216 { "autoconf", IN6_IFF_AUTOCONF, setip6flags },
217 { "-autoconf", -IN6_IFF_AUTOCONF, setip6flags },
218 { "pltime", NEXTARG, setip6pltime },
219 { "vltime", NEXTARG, setip6vltime },
220 { "eui64", 0, setip6eui64 },
221#endif
222 { "range", NEXTARG, setatrange },
223 { "phase", NEXTARG, setatphase },
224 { "metric", NEXTARG, setifmetric },
225 { "broadcast", NEXTARG, setifbroadaddr },
226 { "ipdst", NEXTARG, setifipdst },
227 { "tunnel", NEXTARG2, NULL, settunnel },
228 { "deletetunnel", 0, deletetunnel },
229 { "link0", IFF_LINK0, setifflags },
230 { "-link0", -IFF_LINK0, setifflags },
231 { "link1", IFF_LINK1, setifflags },
232 { "-link1", -IFF_LINK1, setifflags },
233 { "link2", IFF_LINK2, setifflags },
234 { "-link2", -IFF_LINK2, setifflags },
235#ifdef USE_IF_MEDIA
236 { "media", NEXTARG, setmedia },
237 { "mediaopt", NEXTARG, setmediaopt },
238 { "-mediaopt", NEXTARG, unsetmediaopt },
239#endif
240#ifdef USE_VLANS
241 { "vlan", NEXTARG, setvlantag },
242 { "vlandev", NEXTARG, setvlandev },
243 { "-vlandev", NEXTARG, unsetvlandev },
244#endif
245#if 0
246 /* XXX `create' special-cased below */
247 {"create", 0, clone_create },
248 {"plumb", 0, clone_create },
249#endif
250 {"destroy", 0, clone_destroy },
251 {"unplumb", 0, clone_destroy },
252#ifdef USE_IEEE80211
253 { "ssid", NEXTARG, set80211ssid },
254 { "nwid", NEXTARG, set80211ssid },
255 { "stationname", NEXTARG, set80211stationname },
256 { "station", NEXTARG, set80211stationname }, /* BSD/OS */
257 { "channel", NEXTARG, set80211channel },
258 { "authmode", NEXTARG, set80211authmode },
259 { "powersavemode", NEXTARG, set80211powersavemode },
260 { "powersave", 1, set80211powersave },
261 { "-powersave", 0, set80211powersave },
262 { "powersavesleep", NEXTARG, set80211powersavesleep },
263 { "wepmode", NEXTARG, set80211wepmode },
264 { "wep", 1, set80211wep },
265 { "-wep", 0, set80211wep },
266 { "weptxkey", NEXTARG, set80211weptxkey },
267 { "wepkey", NEXTARG, set80211wepkey },
268 { "nwkey", NEXTARG, set80211nwkey }, /* NetBSD */
269 { "-nwkey", 0, set80211wep }, /* NetBSD */
270#endif
271 { "rxcsum", IFCAP_RXCSUM, setifcap },
272 { "-rxcsum", -IFCAP_RXCSUM, setifcap },
273 { "txcsum", IFCAP_TXCSUM, setifcap },
274 { "-txcsum", -IFCAP_TXCSUM, setifcap },
275 { "netcons", IFCAP_NETCONS, setifcap },
276 { "-netcons", -IFCAP_NETCONS, setifcap },
277 { "normal", -IFF_LINK0, setifflags },
278 { "compress", IFF_LINK0, setifflags },
279 { "noicmp", IFF_LINK1, setifflags },
280 { "mtu", NEXTARG, setifmtu },
281 { 0, 0, setifaddr },
282 { 0, 0, setifdstaddr },
283};
284
285/*
286 * XNS support liberally adapted from code written at the University of
287 * Maryland principally by James O'Toole and Chris Torek.
288 */
289typedef void af_status __P((int, struct rt_addrinfo *));
290typedef void af_getaddr __P((const char *, int));
291typedef void af_getprefix __P((const char *, int));
292
293af_status in_status, at_status, link_status;
294af_getaddr in_getaddr, at_getaddr, link_getaddr;
295
296#ifndef NO_IPX
297af_status ipx_status;
298af_getaddr ipx_getaddr;
299#endif
300
301#ifdef INET6
302af_status in6_status;
303af_getaddr in6_getaddr;
304af_getprefix in6_getprefix;
305#endif /*INET6*/
306#ifdef NS
307af_status xns_status;
308af_getaddr xns_getaddr;
309#endif
310
311/* Known address families */
312const
313struct afswtch {
314 const char *af_name;
315 short af_af;
316 af_status *af_status;
317 af_getaddr *af_getaddr;
318 af_getprefix *af_getprefix;
319 u_long af_difaddr;
320 u_long af_aifaddr;
321 caddr_t af_ridreq;
322 caddr_t af_addreq;
323} afs[] = {
324#define C(x) ((caddr_t) &x)
325 { "inet", AF_INET, in_status, in_getaddr, NULL,
326 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
327#ifdef INET6
328 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
329 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
330 C(in6_ridreq), C(in6_addreq) },
331#endif /*INET6*/
332#ifndef NO_IPX
333 { "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
334 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
335#endif
336 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
337 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
338#ifdef NS
339 { "ns", AF_NS, xns_status, xns_getaddr, NULL,
340 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
341#endif
342 { "link", AF_LINK, link_status, link_getaddr, NULL,
343 0, SIOCSIFLLADDR, NULL, C(ridreq) },
344 { "ether", AF_LINK, link_status, link_getaddr, NULL,
345 0, SIOCSIFLLADDR, NULL, C(ridreq) },
346 { "lladdr", AF_LINK, link_status, link_getaddr, NULL,
347 0, SIOCSIFLLADDR, NULL, C(ridreq) },
348#if 0 /* XXX conflicts with the media command */
349#ifdef USE_IF_MEDIA
350 { "media", AF_UNSPEC, media_status, NULL, NULL, }, /* XXX not real!! */
351#endif
352#ifdef USE_VLANS
353 { "vlan", AF_UNSPEC, vlan_status, NULL, NULL, }, /* XXX not real!! */
354#endif
355#ifdef USE_IEEE80211
356 { "ieee80211", AF_UNSPEC, ieee80211_status, NULL, NULL, }, /* XXX not real!! */
357#endif
358#endif
359 { 0, 0, 0, 0 }
360};
361
362/*
363 * Expand the compacted form of addresses as returned via the
364 * configuration read via sysctl().
365 */
366
367#define ROUNDUP(a) \
368 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
369#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
370
371void
372rt_xaddrs(cp, cplim, rtinfo)
373 caddr_t cp, cplim;
374 struct rt_addrinfo *rtinfo;
375{
376 struct sockaddr *sa;
377 int i;
378
379 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
380 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
381 if ((rtinfo->rti_addrs & (1 << i)) == 0)
382 continue;
383 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
384 ADVANCE(cp, sa);
385 }
386}
387
388
389void
390usage()
391{
392#ifndef INET6
393 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
394 "usage: ifconfig interface address_family [address [dest_address]]",
395 " [parameters]",
396 " ifconfig -C",
397 " ifconfig interface create",
398 " ifconfig -a [-d] [-m] [-u] [address_family]",
399 " ifconfig -l [-d] [-u] [address_family]",
400 " ifconfig [-d] [-m] [-u]");
401#else
402 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
403 "usage: ifconfig [-L] interface address_family [address [dest_address]]",
404 " [parameters]",
405 " ifconfig -C",
406 " ifconfig interface create",
407 " ifconfig -a [-L] [-d] [-m] [-u] [address_family]",
408 " ifconfig -l [-d] [-u] [address_family]",
409 " ifconfig [-L] [-d] [-m] [-u]");
410#endif
411 exit(1);
412}
413
414int
415main(argc, argv)
416 int argc;
417 char *const *argv;
418{
419 int c;
420 int all, namesonly, downonly, uponly;
421 int foundit = 0, need_nl = 0;
422 const struct afswtch *afp = 0;
423 int addrcount;
424 struct if_msghdr *ifm, *nextifm;
425 struct ifa_msghdr *ifam;
426 struct sockaddr_dl *sdl;
427 char *buf, *lim, *next;
428
429
430 size_t needed;
431 int mib[6];
432
433 /* Parse leading line options */
434 all = downonly = uponly = namesonly = 0;
435 while ((c = getopt(argc, argv, "adlmuC"
436#ifdef INET6
437 "L"
438#endif
439 )) != -1) {
440 switch (c) {
441 case 'a': /* scan all interfaces */
442 all++;
443 break;
444 case 'd': /* restrict scan to "down" interfaces */
445 downonly++;
446 break;
447 case 'l': /* scan interface names only */
448 namesonly++;
449 break;
450 case 'm': /* show media choices in status */
451 supmedia = 1;
452 break;
453 case 'u': /* restrict scan to "up" interfaces */
454 uponly++;
455 break;
456 case 'C':
457 listcloners = 1;
458 break;
459#ifdef INET6
460 case 'L':
461 ip6lifetime++; /* print IPv6 address lifetime */
462 break;
463#endif
464 default:
465 usage();
466 break;
467 }
468 }
469 argc -= optind;
470 argv += optind;
471
472 if (listcloners) {
473 /* -C must be solitary */
474 if (all || supmedia || uponly || downonly || namesonly ||
475 argc > 0)
476 usage();
477
478 list_cloners();
479 exit(0);
480 }
481
482 /* -l cannot be used with -a or -m */
483 if (namesonly && (all || supmedia))
484 usage();
485
486 /* nonsense.. */
487 if (uponly && downonly)
488 usage();
489
490 /* no arguments is equivalent to '-a' */
491 if (!namesonly && argc < 1)
492 all = 1;
493
494 /* -a and -l allow an address family arg to limit the output */
495 if (all || namesonly) {
496 if (argc > 1)
497 usage();
498
499 if (argc == 1) {
500 for (afp = afs; afp->af_name; afp++)
501 if (strcmp(afp->af_name, *argv) == 0) {
502 argc--, argv++;
503 break;
504 }
505 if (afp->af_name == NULL)
506 usage();
507 /* leave with afp non-zero */
508 }
509 } else {
510 /* not listing, need an argument */
511 if (argc < 1)
512 usage();
513
514 strncpy(name, *argv, sizeof(name));
515 argc--, argv++;
516
517 /* check and maybe load support for this interface */
518 ifmaybeload(name);
519
520 /*
521 * NOTE: We must special-case the `create' command right
522 * here as we would otherwise fail when trying to find
523 * the interface.
524 */
525 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
526 strcmp(argv[0], "plumb") == 0)) {
527 clone_create();
528 argc--, argv++;
529 if (argc == 0)
530 exit(0);
531 }
532 }
533
534 /* Check for address family */
535 if (argc > 0) {
536 for (afp = afs; afp->af_name; afp++)
537 if (strcmp(afp->af_name, *argv) == 0) {
538 argc--, argv++;
539 break;
540 }
541 if (afp->af_name == NULL)
542 afp = NULL; /* not a family, NULL */
543 }
544
545 mib[0] = CTL_NET;
546 mib[1] = PF_ROUTE;
547 mib[2] = 0;
548 mib[3] = 0; /* address family */
549 mib[4] = NET_RT_IFLIST;
550 mib[5] = 0;
551
552 /* if particular family specified, only ask about it */
553 if (afp)
554 mib[3] = afp->af_af;
555
556 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
557 errx(1, "iflist-sysctl-estimate");
558 if ((buf = malloc(needed)) == NULL)
559 errx(1, "malloc");
560 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
561 errx(1, "actual retrieval of interface table");
562 lim = buf + needed;
563
564 next = buf;
565 while (next < lim) {
566
567 ifm = (struct if_msghdr *)next;
568
569 if (ifm->ifm_type == RTM_IFINFO) {
570 sdl = (struct sockaddr_dl *)(ifm + 1);
571 flags = ifm->ifm_flags;
572 } else {
573 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
574 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
575 ifm->ifm_type);
576 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
577 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
578 lim);
579 exit (1);
580 }
581
582 next += ifm->ifm_msglen;
583 ifam = NULL;
584 addrcount = 0;
585 while (next < lim) {
586
587 nextifm = (struct if_msghdr *)next;
588
589 if (nextifm->ifm_type != RTM_NEWADDR)
590 break;
591
592 if (ifam == NULL)
593 ifam = (struct ifa_msghdr *)nextifm;
594
595 addrcount++;
596 next += nextifm->ifm_msglen;
597 }
598
599 if (all || namesonly) {
600 if (uponly)
601 if ((flags & IFF_UP) == 0)
602 continue; /* not up */
603 if (downonly)
604 if (flags & IFF_UP)
605 continue; /* not down */
606 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
607 name[sdl->sdl_nlen] = '\0';
608 if (namesonly) {
609 if (afp == NULL ||
610 afp->af_status != link_status ||
611 sdl->sdl_type == IFT_ETHER) {
612 if (need_nl)
613 putchar(' ');
614 fputs(name, stdout);
615 need_nl++;
616 }
617 continue;
618 }
619 } else {
620 if (strlen(name) != sdl->sdl_nlen)
621 continue; /* not same len */
622 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
623 continue; /* not same name */
624 }
625
626 if (argc > 0)
627 ifconfig(argc, argv, afp);
628 else
629 status(afp, addrcount, sdl, ifm, ifam);
630
631 if (all == 0 && namesonly == 0) {
632 foundit++; /* flag it as 'done' */
633 break;
634 }
635 }
636 free(buf);
637
638 if (namesonly && need_nl > 0)
639 putchar('\n');
640
641 if (all == 0 && namesonly == 0 && foundit == 0)
642 errx(1, "interface %s does not exist", name);
643
644
645 exit (0);
646}
647
648
649int
650ifconfig(argc, argv, afp)
651 int argc;
652 char *const *argv;
653 const struct afswtch *afp;
654{
655 int s;
656
657 if (afp == NULL)
658 afp = &afs[0];
659 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
660 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
661
662 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
663 err(1, "socket");
664
665 while (argc > 0) {
666 register const struct cmd *p;
667
668 for (p = cmds; p->c_name; p++)
669 if (strcmp(*argv, p->c_name) == 0)
670 break;
671 if (p->c_name == 0 && setaddr)
672 p++; /* got src, do dst */
673 if (p->c_func || p->c_func2) {
674 if (p->c_parameter == NEXTARG) {
675 if (argv[1] == NULL)
676 errx(1, "'%s' requires argument",
677 p->c_name);
678 (*p->c_func)(argv[1], 0, s, afp);
679 argc--, argv++;
680 } else if (p->c_parameter == NEXTARG2) {
681 if (argc < 3)
682 errx(1, "'%s' requires 2 arguments",
683 p->c_name);
684 (*p->c_func2)(argv[1], argv[2], s, afp);
685 argc -= 2, argv += 2;
686 } else
687 (*p->c_func)(*argv, p->c_parameter, s, afp);
688 }
689 argc--, argv++;
690 }
691#ifdef INET6
692 if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
693 /* Aggregatable address architecture defines all prefixes
694 are 64. So, it is convenient to set prefixlen to 64 if
695 it is not specified. */
696 setifprefixlen("64", 0, s, afp);
697 /* in6_getprefix("64", MASK) if MASK is available here... */
698 }
699#endif
700#ifndef NO_IPX
701 if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
702 struct ipxip_req rq;
703 int size = sizeof(rq);
704
705 rq.rq_ipx = addreq.ifra_addr;
706 rq.rq_ip = addreq.ifra_dstaddr;
707
708 if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
709 Perror("Encapsulation Routing");
710 }
711#endif
712 if (ifr.ifr_addr.sa_family == AF_APPLETALK)
713 checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
714#ifdef NS
715 if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
716 struct nsip_req rq;
717 int size = sizeof(rq);
718
719 rq.rq_ns = addreq.ifra_addr;
720 rq.rq_ip = addreq.ifra_dstaddr;
721
722 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
723 Perror("Encapsulation Routing");
724 }
725#endif
726 if (clearaddr) {
727 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
728 warnx("interface %s cannot change %s addresses!",
729 name, afp->af_name);
730 clearaddr = NULL;
731 }
732 }
733 if (clearaddr) {
734 int ret;
735 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
736 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
737 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
738 /* means no previous address for interface */
739 } else
740 Perror("ioctl (SIOCDIFADDR)");
741 }
742 }
743 if (newaddr) {
744 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
745 warnx("interface %s cannot change %s addresses!",
746 name, afp->af_name);
747 newaddr = 0;
748 }
749 }
750 if (newaddr && (setaddr || setmask)) {
751 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
752 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
753 Perror("ioctl (SIOCAIFADDR)");
754 }
755 close(s);
756 return(0);
757}
758#define RIDADDR 0
759#define ADDR 1
760#define MASK 2
761#define DSTADDR 3
762
763/*ARGSUSED*/
764void
765setifaddr(addr, param, s, afp)
766 const char *addr;
767 int param;
768 int s;
769 const struct afswtch *afp;
770{
771 if (*afp->af_getaddr == NULL)
772 return;
773 /*
774 * Delay the ioctl to set the interface addr until flags are all set.
775 * The address interpretation may depend on the flags,
776 * and the flags may change when the address is set.
777 */
778 setaddr++;
779 if (doalias == 0 && afp->af_af != AF_LINK)
780 clearaddr = 1;
781 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
782}
783
784void
785settunnel(src, dst, s, afp)
786 const char *src, *dst;
787 int s;
788 const struct afswtch *afp;
789{
790 struct addrinfo hints, *srcres, *dstres;
791 struct ifaliasreq addreq;
792 int ecode;
793#ifdef INET6
794 struct in6_aliasreq in6_addreq;
795#endif
796
797 memset(&hints, 0, sizeof(hints));
798 hints.ai_family = afp->af_af;
799
800 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
801 errx(1, "error in parsing address string: %s",
802 gai_strerror(ecode));
803
804 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
805 errx(1, "error in parsing address string: %s",
806 gai_strerror(ecode));
807
808 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
809 errx(1,
810 "source and destination address families do not match");
811
812 switch (srcres->ai_addr->sa_family) {
813 case AF_INET:
814 memset(&addreq, 0, sizeof(addreq));
815 strncpy(addreq.ifra_name, name, IFNAMSIZ);
816 memcpy(&addreq.ifra_addr, srcres->ai_addr,
817 srcres->ai_addr->sa_len);
818 memcpy(&addreq.ifra_dstaddr, dstres->ai_addr,
819 dstres->ai_addr->sa_len);
820
821 if (ioctl(s, SIOCSIFPHYADDR, &addreq) < 0)
822 warn("SIOCSIFPHYADDR");
823 break;
824
825#ifdef INET6
826 case AF_INET6:
827 memset(&in6_addreq, 0, sizeof(in6_addreq));
828 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
829 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr,
830 srcres->ai_addr->sa_len);
831 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
832 dstres->ai_addr->sa_len);
833
834 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
835 warn("SIOCSIFPHYADDR_IN6");
836 break;
837#endif /* INET6 */
838
839 default:
840 warn("address family not supported");
841 }
842
843 freeaddrinfo(srcres);
844 freeaddrinfo(dstres);
845}
846
847/* ARGSUSED */
848void
849deletetunnel(vname, param, s, afp)
850 const char *vname;
851 int param;
852 int s;
853 const struct afswtch *afp;
854{
855
856 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
857 err(1, "SIOCDIFPHYADDR");
858}
859
860void
861setifnetmask(addr, dummy, s, afp)
862 const char *addr;
863 int dummy __unused;
864 int s;
865 const struct afswtch *afp;
866{
867 if (*afp->af_getaddr == NULL)
868 return;
869 setmask++;
870 (*afp->af_getaddr)(addr, MASK);
871}
872
873#ifdef INET6
874void
875setifprefixlen(addr, dummy, s, afp)
876 const char *addr;
877 int dummy __unused;
878 int s;
879 const struct afswtch *afp;
880{
881 if (*afp->af_getprefix)
882 (*afp->af_getprefix)(addr, MASK);
883 explicit_prefix = 1;
884}
885
886void
887setip6flags(dummyaddr, flag, dummysoc, afp)
888 const char *dummyaddr __unused;
889 int flag;
890 int dummysoc __unused;
891 const struct afswtch *afp;
892{
893 if (afp->af_af != AF_INET6)
894 err(1, "address flags can be set only for inet6 addresses");
895
896 if (flag < 0)
897 in6_addreq.ifra_flags &= ~(-flag);
898 else
899 in6_addreq.ifra_flags |= flag;
900}
901
902void
903setip6pltime(seconds, dummy, s, afp)
904 const char *seconds;
905 int dummy __unused;
906 int s;
907 const struct afswtch *afp;
908{
909 setip6lifetime("pltime", seconds, s, afp);
910}
911
912void
913setip6vltime(seconds, dummy, s, afp)
914 const char *seconds;
915 int dummy __unused;
916 int s;
917 const struct afswtch *afp;
918{
919 setip6lifetime("vltime", seconds, s, afp);
920}
921
922void
923setip6lifetime(cmd, val, s, afp)
924 const char *cmd;
925 const char *val;
926 int s;
927 const struct afswtch *afp;
928{
929 time_t newval, t;
930 char *ep;
931
932 t = time(NULL);
933 newval = (time_t)strtoul(val, &ep, 0);
934 if (val == ep)
935 errx(1, "invalid %s", cmd);
936 if (afp->af_af != AF_INET6)
937 errx(1, "%s not allowed for the AF", cmd);
938 if (strcmp(cmd, "vltime") == 0) {
939 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
940 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
941 } else if (strcmp(cmd, "pltime") == 0) {
942 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
943 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
944 }
945}
946
947void
948setip6eui64(cmd, dummy, s, afp)
949 const char *cmd;
950 int dummy __unused;
951 int s;
952 const struct afswtch *afp;
953{
954 struct ifaddrs *ifap, *ifa;
955 const struct sockaddr_in6 *sin6 = NULL;
956 const struct in6_addr *lladdr = NULL;
957 struct in6_addr *in6;
958
959 if (afp->af_af != AF_INET6)
960 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
961 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
962 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
963 errx(EXIT_FAILURE, "interface index is already filled");
964 if (getifaddrs(&ifap) != 0)
965 err(EXIT_FAILURE, "getifaddrs");
966 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
967 if (ifa->ifa_addr->sa_family == AF_INET6 &&
968 strcmp(ifa->ifa_name, name) == 0) {
969 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
970 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
971 lladdr = &sin6->sin6_addr;
972 break;
973 }
974 }
975 }
976 if (!lladdr)
977 errx(EXIT_FAILURE, "could not determine link local address");
978
979 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
980
981 freeifaddrs(ifap);
982}
983#endif
984
985void
986setifbroadaddr(addr, dummy, s, afp)
987 const char *addr;
988 int dummy __unused;
989 int s;
990 const struct afswtch *afp;
991{
992 if (*afp->af_getaddr == NULL)
993 return;
994 (*afp->af_getaddr)(addr, DSTADDR);
995}
996
997void
998setifipdst(addr, dummy, s, afp)
999 const char *addr;
1000 int dummy __unused;
1001 int s;
1002 const struct afswtch *afp;
1003{
1004 in_getaddr(addr, DSTADDR);
1005 setipdst++;
1006 clearaddr = 0;
1007 newaddr = 0;
1008}
1009#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1010
1011void
1012notealias(addr, param, s, afp)
1013 const char *addr;
1014 int param;
1015 int s;
1016 const struct afswtch *afp;
1017{
1018 if (setaddr && doalias == 0 && param < 0)
1019 bcopy((caddr_t)rqtosa(af_addreq),
1020 (caddr_t)rqtosa(af_ridreq),
1021 rqtosa(af_addreq)->sa_len);
1022 doalias = param;
1023 if (param < 0) {
1024 clearaddr = 1;
1025 newaddr = 0;
1026 } else
1027 clearaddr = 0;
1028}
1029
1030/*ARGSUSED*/
1031void
1032setifdstaddr(addr, param, s, afp)
1033 const char *addr;
1034 int param __unused;
1035 int s;
1036 const struct afswtch *afp;
1037{
1038 if (*afp->af_getaddr == NULL)
1039 return;
1040 (*afp->af_getaddr)(addr, DSTADDR);
1041}
1042
1043/*
1044 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1045 * of the ifreq structure, which may confuse other parts of ifconfig.
1046 * Make a private copy so we can avoid that.
1047 */
1048void
1049setifflags(vname, value, s, afp)
1050 const char *vname;
1051 int value;
1052 int s;
1053 const struct afswtch *afp;
1054{
1055 struct ifreq my_ifr;
1056
1057 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
1058
1059 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1060 Perror("ioctl (SIOCGIFFLAGS)");
1061 exit(1);
1062 }
1063 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
1064 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1065
1066 if (value < 0) {
1067 value = -value;
1068 flags &= ~value;
1069 } else
1070 flags |= value;
1071 my_ifr.ifr_flags = flags & 0xffff;
1072 my_ifr.ifr_flagshigh = flags >> 16;
1073 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1074 Perror(vname);
1075}
1076
1077void
1078setifcap(vname, value, s, afp)
1079 const char *vname;
1080 int value;
1081 int s;
1082 const struct afswtch *afp;
1083{
1084
1085 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1086 Perror("ioctl (SIOCGIFCAP)");
1087 exit(1);
1088 }
1089 flags = ifr.ifr_curcap;
1090 if (value < 0) {
1091 value = -value;
1092 flags &= ~value;
1093 } else
1094 flags |= value;
1095 ifr.ifr_reqcap = flags;
1096 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1097 Perror(vname);
1098}
1099
1100void
1101setifmetric(val, dummy, s, afp)
1102 const char *val;
1103 int dummy __unused;
1104 int s;
1105 const struct afswtch *afp;
1106{
1107 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1108 ifr.ifr_metric = atoi(val);
1109 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1110 warn("ioctl (set metric)");
1111}
1112
1113void
1114setifmtu(val, dummy, s, afp)
1115 const char *val;
1116 int dummy __unused;
1117 int s;
1118 const struct afswtch *afp;
1119{
1120 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1121 ifr.ifr_mtu = atoi(val);
1122 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1123 warn("ioctl (set mtu)");
1124}
1125
1126#define IFFBITS \
1127"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1128"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1129"\20MULTICAST"
1130
1131#define IFCAPBITS \
1132"\003\1rxcsum\2txcsum\3netcons"
1133
1134/*
1135 * Print the status of the interface. If an address family was
1136 * specified, show it and it only; otherwise, show them all.
1137 */
1138void
1139status(afp, addrcount, sdl, ifm, ifam)
1140 const struct afswtch *afp;
1141 int addrcount;
1142 struct sockaddr_dl *sdl;
1143 struct if_msghdr *ifm;
1144 struct ifa_msghdr *ifam;
1145{
1146 const struct afswtch *p = NULL;
1147 struct rt_addrinfo info;
1148 int allfamilies, s;
1149 struct ifstat ifs;
1150
1151 if (afp == NULL) {
1152 allfamilies = 1;
1153 afp = &afs[0];
1154 } else
1155 allfamilies = 0;
1156
1157 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1158 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1159
1160 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
1161 err(1, "socket");
1162
1163 printf("%s: ", name);
1164 printb("flags", flags, IFFBITS);
1165 if (ifm->ifm_data.ifi_metric)
1166 printf(" metric %ld", ifm->ifm_data.ifi_metric);
1167 if (ifm->ifm_data.ifi_mtu)
1168 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
1169 putchar('\n');
1170
1171 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1172 if (ifr.ifr_curcap != 0) {
1173 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1174 putchar('\n');
1175 }
1176 if (supmedia && ifr.ifr_reqcap != 0) {
1177 printf("\tcapability list:\n");
1178 printb("\t\t", ifr.ifr_reqcap, IFCAPBITS);
1179 putchar('\n');
1180 }
1181 }
1182
1183 tunnel_status(s);
1184
1185 while (addrcount > 0) {
1186
1187 info.rti_addrs = ifam->ifam_addrs;
1188
1189 /* Expand the compacted addresses */
1190 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
1191 &info);
1192
1193 if (!allfamilies) {
1194 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family) {
1195 p = afp;
1196 (*p->af_status)(s, &info);
1197 }
1198 } else for (p = afs; p->af_name; p++) {
1199 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family)
1200 (*p->af_status)(s, &info);
1201 }
1202 addrcount--;
1203 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
1204 }
1205 if (allfamilies || afp->af_status == link_status)
1206 link_status(s, (struct rt_addrinfo *)sdl);
1207#ifdef USE_IF_MEDIA
1208 if (allfamilies || afp->af_status == media_status)
1209 media_status(s, NULL);
1210#endif
1211#ifdef USE_VLANS
1212 if (allfamilies || afp->af_status == vlan_status)
1213 vlan_status(s, NULL);
1214#endif
1215#ifdef USE_IEEE80211
1216 if (allfamilies || afp->af_status == ieee80211_status)
1217 ieee80211_status(s, NULL);
1218#endif
1219 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1220 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1221 printf("%s", ifs.ascii);
1222
1223 if (!allfamilies && !p && afp->af_status != media_status &&
1224 afp->af_status != link_status
1225#ifdef USE_VLANS
1226 && afp->af_status != vlan_status
1227#endif
1228 )
1229 warnx("%s has no %s interface address!", name, afp->af_name);
1230
1231 close(s);
1232 return;
1233}
1234
1235void
1236tunnel_status(s)
1237 int s;
1238{
1239 char psrcaddr[NI_MAXHOST];
1240 char pdstaddr[NI_MAXHOST];
1241 u_long srccmd, dstcmd;
1242 struct ifreq *ifrp;
1243 const char *ver = "";
1244#ifdef NI_WITHSCOPEID
1245 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1246#else
1247 const int niflag = NI_NUMERICHOST;
1248#endif
1249#ifdef INET6
1250 struct in6_ifreq in6_ifr;
1251 int s6;
1252#endif /* INET6 */
1253
1254 psrcaddr[0] = pdstaddr[0] = '\0';
1255
1256#ifdef INET6
1257 memset(&in6_ifr, 0, sizeof(in6_ifr));
1258 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
1259 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
1260 if (s6 < 0) {
1261 srccmd = SIOCGIFPSRCADDR;
1262 dstcmd = SIOCGIFPDSTADDR;
1263 ifrp = &ifr;
1264 } else {
1265 close(s6);
1266 srccmd = SIOCGIFPSRCADDR_IN6;
1267 dstcmd = SIOCGIFPDSTADDR_IN6;
1268 ifrp = (struct ifreq *)&in6_ifr;
1269 }
1270#else /* INET6 */
1271 srccmd = SIOCGIFPSRCADDR;
1272 dstcmd = SIOCGIFPDSTADDR;
1273 ifrp = &ifr;
1274#endif /* INET6 */
1275
1276 if (ioctl(s, srccmd, (caddr_t)ifrp) < 0)
1277 return;
1278#ifdef INET6
1279 if (ifrp->ifr_addr.sa_family == AF_INET6)
1280 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1281#endif
1282 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1283 psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
1284#ifdef INET6
1285 if (ifrp->ifr_addr.sa_family == AF_INET6)
1286 ver = "6";
1287#endif
1288
1289 if (ioctl(s, dstcmd, (caddr_t)ifrp) < 0)
1290 return;
1291#ifdef INET6
1292 if (ifrp->ifr_addr.sa_family == AF_INET6)
1293 in6_fillscopeid((struct sockaddr_in6 *)&ifrp->ifr_addr);
1294#endif
1295 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
1296 pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
1297
1298 printf("\ttunnel inet%s %s --> %s\n", ver,
1299 psrcaddr, pdstaddr);
1300}
1301
1302void
1303in_status(s, info)
1304 int s __unused;
1305 struct rt_addrinfo * info;
1306{
1307 struct sockaddr_in *sin, null_sin;
1308
1309 memset(&null_sin, 0, sizeof(null_sin));
1310
1311 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
1312 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1313
1314 if (flags & IFF_POINTOPOINT) {
1315 /* note RTAX_BRD overlap with IFF_BROADCAST */
1316 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1317 if (!sin)
1318 sin = &null_sin;
1319 printf("--> %s ", inet_ntoa(sin->sin_addr));
1320 }
1321
1322 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1323 if (!sin)
1324 sin = &null_sin;
1325 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
1326
1327 if (flags & IFF_BROADCAST) {
1328 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1329 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
1330 if (sin && sin->sin_addr.s_addr != 0)
1331 printf("broadcast %s", inet_ntoa(sin->sin_addr));
1332 }
1333 putchar('\n');
1334}
1335
1336#ifdef INET6
1337void
1338in6_fillscopeid(sin6)
1339 struct sockaddr_in6 *sin6;
1340{
1341#if defined(__KAME__) && defined(KAME_SCOPEID)
1342 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1343 sin6->sin6_scope_id =
1344 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1345 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1346 }
1347#endif
1348}
1349
1350void
1351in6_status(s, info)
1352 int s __unused;
1353 struct rt_addrinfo * info;
1354{
1355 struct sockaddr_in6 *sin, null_sin;
1356 struct in6_ifreq ifr6;
1357 int s6;
1358 u_int32_t flags6;
1359 struct in6_addrlifetime lifetime;
1360 time_t t = time(NULL);
1361 int error;
1362 u_int32_t scopeid;
1363
1364 memset(&null_sin, 0, sizeof(null_sin));
1365
1366 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
1367 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
1368 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1369 perror("ifconfig: socket");
1370 return;
1371 }
1372 ifr6.ifr_addr = *sin;
1373 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1374 perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
1375 close(s6);
1376 return;
1377 }
1378 flags6 = ifr6.ifr_ifru.ifru_flags6;
1379 memset(&lifetime, 0, sizeof(lifetime));
1380 ifr6.ifr_addr = *sin;
1381 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
1382 perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
1383 close(s6);
1384 return;
1385 }
1386 lifetime = ifr6.ifr_ifru.ifru_lifetime;
1387 close(s6);
1388
1389 /* XXX: embedded link local addr check */
1390 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1391 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1392 u_short index;
1393
1394 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1395 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1396 if (sin->sin6_scope_id == 0)
1397 sin->sin6_scope_id = ntohs(index);
1398 }
1399 scopeid = sin->sin6_scope_id;
1400
1401 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
1402 sizeof(addr_buf), NULL, 0,
1403 NI_NUMERICHOST|NI_WITHSCOPEID);
1404 if (error != 0)
1405 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1406 sizeof(addr_buf));
1407 printf("\tinet6 %s ", addr_buf);
1408
1409 if (flags & IFF_POINTOPOINT) {
1410 /* note RTAX_BRD overlap with IFF_BROADCAST */
1411 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1412 /*
1413 * some of the interfaces do not have valid destination
1414 * address.
1415 */
1416 if (sin && sin->sin6_family == AF_INET6) {
1417 int error;
1418
1419 /* XXX: embedded link local addr check */
1420 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
1421 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
1422 u_short index;
1423
1424 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
1425 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
1426 if (sin->sin6_scope_id == 0)
1427 sin->sin6_scope_id = ntohs(index);
1428 }
1429
1430 error = getnameinfo((struct sockaddr *)sin,
1431 sin->sin6_len, addr_buf,
1432 sizeof(addr_buf), NULL, 0,
1433 NI_NUMERICHOST|NI_WITHSCOPEID);
1434 if (error != 0)
1435 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
1436 sizeof(addr_buf));
1437 printf("--> %s ", addr_buf);
1438 }
1439 }
1440
1441 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1442 if (!sin)
1443 sin = &null_sin;
1444 printf("prefixlen %d ", prefix(&sin->sin6_addr,
1445 sizeof(struct in6_addr)));
1446
1447 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1448 printf("anycast ");
1449 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
1450 printf("tentative ");
1451 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
1452 printf("duplicated ");
1453 if ((flags6 & IN6_IFF_DETACHED) != 0)
1454 printf("detached ");
1455 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
1456 printf("deprecated ");
1457 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
1458 printf("autoconf ");
1459 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
1460 printf("temporary ");
1461
1462 if (scopeid)
1463 printf("scopeid 0x%x ", scopeid);
1464
1465 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1466 printf("pltime ");
1467 if (lifetime.ia6t_preferred) {
1468 printf("%s ", lifetime.ia6t_preferred < t
1469 ? "0" : sec2str(lifetime.ia6t_preferred - t));
1470 } else
1471 printf("infty ");
1472
1473 printf("vltime ");
1474 if (lifetime.ia6t_expire) {
1475 printf("%s ", lifetime.ia6t_expire < t
1476 ? "0" : sec2str(lifetime.ia6t_expire - t));
1477 } else
1478 printf("infty ");
1479 }
1480
1481 putchar('\n');
1482}
1483#endif /*INET6*/
1484
1485#ifndef NO_IPX
1486void
1487ipx_status(s, info)
1488 int s __unused;
1489 struct rt_addrinfo * info;
1490{
1491 struct sockaddr_ipx *sipx, null_sipx;
1492
1493 memset(&null_sipx, 0, sizeof(null_sipx));
1494
1495 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1496 printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1497
1498 if (flags & IFF_POINTOPOINT) {
1499 sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1500 if (!sipx)
1501 sipx = &null_sipx;
1502 printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1503 }
1504 putchar('\n');
1505}
1506#endif
1507
1508void
1509at_status(s, info)
1510 int s __unused;
1511 struct rt_addrinfo * info;
1512{
1513 struct sockaddr_at *sat, null_sat;
1514 struct netrange *nr;
1515
1516 memset(&null_sat, 0, sizeof(null_sat));
1517
1518 sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1519 nr = &sat->sat_range.r_netrange;
1520 printf("\tatalk %d.%d range %d-%d phase %d",
1521 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1522 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1523 if (flags & IFF_POINTOPOINT) {
1524 /* note RTAX_BRD overlap with IFF_BROADCAST */
1525 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1526 if (!sat)
1527 sat = &null_sat;
1528 printf("--> %d.%d",
1529 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1530 }
1531 if (flags & IFF_BROADCAST) {
1532 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
1533 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1534 if (sat)
1535 printf(" broadcast %d.%d",
1536 ntohs(sat->sat_addr.s_net),
1537 sat->sat_addr.s_node);
1538 }
1539
1540 putchar('\n');
1541}
1542
1543#ifdef NS
1544void
1545xns_status(s, info)
1546 int s __unused;
1547 struct rt_addrinfo * info;
1548{
1549 struct sockaddr_ns *sns, null_sns;
1550
1551 memset(&null_sns, 0, sizeof(null_sns));
1552
1553 sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1554 printf("\tns %s ", ns_ntoa(sns->sns_addr));
1555
1556 if (flags & IFF_POINTOPOINT) {
1557 sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1558 if (!sns)
1559 sns = &null_sns;
1560 printf("--> %s ", ns_ntoa(sns->sns_addr));
1561 }
1562
1563 putchar('\n');
1564 close(s);
1565}
1566#endif
1567
1568
1569void
1570link_status(s, info)
1571 int s __unused;
1572 struct rt_addrinfo *info;
1573{
1574 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1575
1576 if (sdl->sdl_alen > 0) {
1577 if (sdl->sdl_type == IFT_ETHER &&
1578 sdl->sdl_alen == ETHER_ADDR_LEN)
1579 printf("\tether %s\n",
1580 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1581 else {
1582 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
1583
1584 printf("\tlladdr %s\n", link_ntoa(sdl) + n);
1585 }
1586 }
1587}
1588
1589void
1590Perror(cmd)
1591 const char *cmd;
1592{
1593 switch (errno) {
1594
1595 case ENXIO:
1596 errx(1, "%s: no such interface", cmd);
1597 break;
1598
1599 case EPERM:
1600 errx(1, "%s: permission denied", cmd);
1601 break;
1602
1603 default:
1604 err(1, "%s", cmd);
1605 }
1606}
1607
1608#define SIN(x) ((struct sockaddr_in *) &(x))
1609struct sockaddr_in *sintab[] = {
1610SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1611SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1612
1613void
1614in_getaddr(s, which)
1615 const char *s;
1616 int which;
1617{
1618 register struct sockaddr_in *sin = sintab[which];
1619 struct hostent *hp;
1620 struct netent *np;
1621
1622 sin->sin_len = sizeof(*sin);
1623 if (which != MASK)
1624 sin->sin_family = AF_INET;
1625
1626 if (which == ADDR) {
1627 char *p = NULL;
1628
1629 if((p = strrchr(s, '/')) != NULL) {
1630 /* address is `name/masklen' */
1631 int masklen;
1632 int ret;
1633 struct sockaddr_in *min = sintab[MASK];
1634 *p = '\0';
1635 ret = sscanf(p+1, "%u", &masklen);
1636 if(ret != 1 || (masklen < 0 || masklen > 32)) {
1637 *p = '/';
1638 errx(1, "%s: bad value", s);
1639 }
1640 min->sin_len = sizeof(*min);
1641 min->sin_addr.s_addr = htonl(~((1LL << (32 - masklen)) - 1) &
1642 0xffffffff);
1643 }
1644 }
1645
1646 if (inet_aton(s, &sin->sin_addr))
1647 return;
1648 if ((hp = gethostbyname(s)) != 0)
1649 bcopy(hp->h_addr, (char *)&sin->sin_addr,
1650 MIN(hp->h_length, sizeof(sin->sin_addr)));
1651 else if ((np = getnetbyname(s)) != 0)
1652 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1653 else
1654 errx(1, "%s: bad value", s);
1655}
1656
1657#ifdef INET6
1658#define SIN6(x) ((struct sockaddr_in6 *) &(x))
1659struct sockaddr_in6 *sin6tab[] = {
1660SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1661SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1662
1663void
1664in6_getaddr(s, which)
1665 const char *s;
1666 int which;
1667{
1668 register struct sockaddr_in6 *sin = sin6tab[which];
1669 struct addrinfo hints, *res;
1670 int error = -1;
1671
1672 newaddr &= 1;
1673
1674 sin->sin6_len = sizeof(*sin);
1675 if (which != MASK)
1676 sin->sin6_family = AF_INET6;
1677
1678 if (which == ADDR) {
1679 char *p = NULL;
1680 if((p = strrchr(s, '/')) != NULL) {
1681 *p = '\0';
1682 in6_getprefix(p + 1, MASK);
1683 explicit_prefix = 1;
1684 }
1685 }
1686
1687 if (sin->sin6_family == AF_INET6) {
1688 bzero(&hints, sizeof(struct addrinfo));
1689 hints.ai_family = AF_INET6;
1690 error = getaddrinfo(s, NULL, &hints, &res);
1691 }
1692 if (error != 0) {
1693 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1694 errx(1, "%s: bad value", s);
1695 } else
1696 bcopy(res->ai_addr, sin, res->ai_addrlen);
1697}
1698
1699void
1700in6_getprefix(plen, which)
1701 const char *plen;
1702 int which;
1703{
1704 register struct sockaddr_in6 *sin = sin6tab[which];
1705 register u_char *cp;
1706 int len = atoi(plen);
1707
1708 if ((len < 0) || (len > 128))
1709 errx(1, "%s: bad value", plen);
1710 sin->sin6_len = sizeof(*sin);
1711 if (which != MASK)
1712 sin->sin6_family = AF_INET6;
1713 if ((len == 0) || (len == 128)) {
1714 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1715 return;
1716 }
1717 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1718 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1719 *cp++ = 0xff;
1720 *cp = 0xff << (8 - len);
1721}
1722#endif
1723
1724/*
1725 * Print a value a la the %b format of the kernel's printf
1726 */
1727void
1728printb(s, v, bits)
1729 const char *s;
1730 register unsigned v;
1731 register const char *bits;
1732{
1733 register int i, any = 0;
1734 register char c;
1735
1736 if (bits && *bits == 8)
1737 printf("%s=%o", s, v);
1738 else
1739 printf("%s=%x", s, v);
1740 bits++;
1741 if (bits) {
1742 putchar('<');
1743 while ((i = *bits++) != '\0') {
1744 if (v & (1 << (i-1))) {
1745 if (any)
1746 putchar(',');
1747 any = 1;
1748 for (; (c = *bits) > 32; bits++)
1749 putchar(c);
1750 } else
1751 for (; *bits > 32; bits++)
1752 ;
1753 }
1754 putchar('>');
1755 }
1756}
1757
1758#ifndef NO_IPX
1759#define SIPX(x) ((struct sockaddr_ipx *) &(x))
1760struct sockaddr_ipx *sipxtab[] = {
1761SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1762SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1763
1764void
1765ipx_getaddr(addr, which)
1766 const char *addr;
1767 int which;
1768{
1769 struct sockaddr_ipx *sipx = sipxtab[which];
1770
1771 sipx->sipx_family = AF_IPX;
1772 sipx->sipx_len = sizeof(*sipx);
1773 sipx->sipx_addr = ipx_addr(addr);
1774 if (which == MASK)
1775 printf("Attempt to set IPX netmask will be ineffectual\n");
1776}
1777#endif
1778
1779void
1780at_getaddr(addr, which)
1781 const char *addr;
1782 int which;
1783{
1784 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1785 u_int net, node;
1786
1787 sat->sat_family = AF_APPLETALK;
1788 sat->sat_len = sizeof(*sat);
1789 if (which == MASK)
1790 errx(1, "AppleTalk does not use netmasks");
1791 if (sscanf(addr, "%u.%u", &net, &node) != 2
1792 || net > 0xffff || node > 0xfe)
1793 errx(1, "%s: illegal address", addr);
1794 sat->sat_addr.s_net = htons(net);
1795 sat->sat_addr.s_node = node;
1796}
1797
1798void
1799link_getaddr(addr, which)
1800 const char *addr;
1801 int which;
1802{
1803 char *temp;
1804 struct sockaddr_dl sdl;
1805 struct sockaddr *sa = &ridreq.ifr_addr;
1806
1807 if (which != ADDR)
1808 errx(1, "can't set link-level netmask or broadcast");
1809 if ((temp = malloc(strlen(addr) + 1)) == NULL)
1810 errx(1, "malloc failed");
1811 temp[0] = ':';
1812 strcpy(temp + 1, addr);
1813 sdl.sdl_len = sizeof(sdl);
1814 link_addr(temp, &sdl);
1815 free(temp);
1816 if (sdl.sdl_alen > sizeof(sa->sa_data))
1817 errx(1, "malformed link-level address");
1818 sa->sa_family = AF_LINK;
1819 sa->sa_len = sdl.sdl_alen;
1820 bcopy(LLADDR(&sdl), sa->sa_data, sdl.sdl_alen);
1821}
1822
1823/* XXX FIXME -- should use strtoul for better parsing. */
1824void
1825setatrange(range, dummy, s, afp)
1826 const char *range;
1827 int dummy __unused;
1828 int s;
1829 const struct afswtch *afp;
1830{
1831 u_short first = 123, last = 123;
1832
1833 if (sscanf(range, "%hu-%hu", &first, &last) != 2
1834 || first == 0 || first > 0xffff
1835 || last == 0 || last > 0xffff || first > last)
1836 errx(1, "%s: illegal net range: %u-%u", range, first, last);
1837 at_nr.nr_firstnet = htons(first);
1838 at_nr.nr_lastnet = htons(last);
1839}
1840
1841void
1842setatphase(phase, dummy, s, afp)
1843 const char *phase;
1844 int dummy __unused;
1845 int s;
1846 const struct afswtch *afp;
1847{
1848 if (!strcmp(phase, "1"))
1849 at_nr.nr_phase = 1;
1850 else if (!strcmp(phase, "2"))
1851 at_nr.nr_phase = 2;
1852 else
1853 errx(1, "%s: illegal phase", phase);
1854}
1855
1856void
1857checkatrange(struct sockaddr_at *sat)
1858{
1859 if (at_nr.nr_phase == 0)
1860 at_nr.nr_phase = 2; /* Default phase 2 */
1861 if (at_nr.nr_firstnet == 0)
1862 at_nr.nr_firstnet = /* Default range of one */
1863 at_nr.nr_lastnet = sat->sat_addr.s_net;
1864printf("\tatalk %d.%d range %d-%d phase %d\n",
1865 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1866 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1867 if ((u_short) ntohs(at_nr.nr_firstnet) >
1868 (u_short) ntohs(sat->sat_addr.s_net)
1869 || (u_short) ntohs(at_nr.nr_lastnet) <
1870 (u_short) ntohs(sat->sat_addr.s_net))
1871 errx(1, "AppleTalk address is not in range");
1872 sat->sat_range.r_netrange = at_nr;
1873}
1874
1875#ifdef NS
1876#define SNS(x) ((struct sockaddr_ns *) &(x))
1877struct sockaddr_ns *snstab[] = {
1878SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1879SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1880
1881void
1882xns_getaddr(addr, which)
1883 const char *addr;
1884 int which;
1885{
1886 struct sockaddr_ns *sns = snstab[which];
1887
1888 sns->sns_family = AF_NS;
1889 sns->sns_len = sizeof(*sns);
1890 sns->sns_addr = ns_addr(addr);
1891 if (which == MASK)
1892 printf("Attempt to set XNS netmask will be ineffectual\n");
1893}
1894#endif
1895
1896#ifdef INET6
1897int
1898prefix(val, size)
1899 void *val;
1900 int size;
1901{
1902 register u_char *name = (u_char *)val;
1903 register int byte, bit, plen = 0;
1904
1905 for (byte = 0; byte < size; byte++, plen += 8)
1906 if (name[byte] != 0xff)
1907 break;
1908 if (byte == size)
1909 return (plen);
1910 for (bit = 7; bit != 0; bit--, plen++)
1911 if (!(name[byte] & (1 << bit)))
1912 break;
1913 for (; bit != 0; bit--)
1914 if (name[byte] & (1 << bit))
1915 return(0);
1916 byte++;
1917 for (; byte < size; byte++)
1918 if (name[byte])
1919 return(0);
1920 return (plen);
1921}
1922
1923static char *
1924sec2str(total)
1925 time_t total;
1926{
1927 static char result[256];
1928 int days, hours, mins, secs;
1929 int first = 1;
1930 char *p = result;
1931
1932 if (0) {
1933 days = total / 3600 / 24;
1934 hours = (total / 3600) % 24;
1935 mins = (total / 60) % 60;
1936 secs = total % 60;
1937
1938 if (days) {
1939 first = 0;
1940 p += sprintf(p, "%dd", days);
1941 }
1942 if (!first || hours) {
1943 first = 0;
1944 p += sprintf(p, "%dh", hours);
1945 }
1946 if (!first || mins) {
1947 first = 0;
1948 p += sprintf(p, "%dm", mins);
1949 }
1950 sprintf(p, "%ds", secs);
1951 } else
1952 sprintf(result, "%lu", (unsigned long)total);
1953
1954 return(result);
1955}
1956#endif /*INET6*/
1957
1958void
1959ifmaybeload(name)
1960 char *name;
1961{
1962 struct module_stat mstat;
1963 int fileid, modid;
1964 char ifkind[35], *cp, *dp;
1965
1966
1967 /* turn interface and unit into module name */
1968 strcpy(ifkind, "if_");
1969 for (cp = name, dp = ifkind + 3;
1970 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1971 *dp = *cp;
1972 *dp = 0;
1973
1974 /* scan files in kernel */
1975 mstat.version = sizeof(struct module_stat);
1976 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1977 /* scan modules in file */
1978 for (modid = kldfirstmod(fileid); modid > 0;
1979 modid = modfnext(modid)) {
1980 if (modstat(modid, &mstat) < 0)
1981 continue;
1982 /* strip bus name if present */
1983 if ((cp = strchr(mstat.name, '/')) != NULL) {
1984 cp++;
1985 } else {
1986 cp = mstat.name;
1987 }
1988 /* already loaded? */
1989 if (!strcmp(ifkind, cp))
1990 return;
1991 }
1992 }
1993
1994 /* not present, we should try to load it */
1995 kldload(ifkind);
1996}
1997
1998void
1999list_cloners(void)
2000{
2001 struct if_clonereq ifcr;
2002 char *cp, *buf;
2003 int idx;
2004 int s;
2005
2006 s = socket(AF_INET, SOCK_DGRAM, 0);
2007 if (s == -1)
2008 err(1, "socket");
2009
2010 memset(&ifcr, 0, sizeof(ifcr));
2011
2012 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
2013 err(1, "SIOCIFGCLONERS for count");
2014
2015 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
2016 if (buf == NULL)
2017 err(1, "unable to allocate cloner name buffer");
2018
2019 ifcr.ifcr_count = ifcr.ifcr_total;
2020 ifcr.ifcr_buffer = buf;
2021
2022 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
2023 err(1, "SIOCIFGCLONERS for names");
2024
2025 /*
2026 * In case some disappeared in the mean time, clamp it down.
2027 */
2028 if (ifcr.ifcr_count > ifcr.ifcr_total)
2029 ifcr.ifcr_count = ifcr.ifcr_total;
2030
2031 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
2032 if (idx > 0)
2033 putchar(' ');
2034 printf("%s", cp);
2035 }
2036
2037 putchar('\n');
2038 free(buf);
2039}
2040
2041void
2042clone_create()
2043{
2044 int s;
2045
2046 s = socket(AF_INET, SOCK_DGRAM, 0);
2047 if (s == -1)
2048 err(1, "socket");
2049
2050 memset(&ifr, 0, sizeof(ifr));
2051 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2052 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
2053 err(1, "SIOCIFCREATE");
2054
2055 if (strcmp(name, ifr.ifr_name) != 0) {
2056 printf("%s\n", ifr.ifr_name);
2057 strlcpy(name, ifr.ifr_name, sizeof(name));
2058 }
2059
2060 close(s);
2061}
2062
2063void
2064clone_destroy(val, d, s, rafp)
2065 const char *val;
2066 int d;
2067 int s;
2068 const struct afswtch *rafp;
2069{
2070
2071 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
2072 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
2073 err(1, "SIOCIFDESTROY");
2074}