ifconfig.c revision 1.65
1/* $NetBSD: ifconfig.c,v 1.65 2000/01/25 01:46:57 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * Copyright (c) 1983, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 */ 72 73#include <sys/cdefs.h> 74#ifndef lint 75__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 76 The Regents of the University of California. All rights reserved.\n"); 77#endif /* not lint */ 78 79#ifndef lint 80#if 0 81static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94"; 82#else 83__RCSID("$NetBSD: ifconfig.c,v 1.65 2000/01/25 01:46:57 thorpej Exp $"); 84#endif 85#endif /* not lint */ 86 87#include <sys/param.h> 88#include <sys/socket.h> 89#include <sys/ioctl.h> 90 91#include <net/if.h> 92#include <net/if_dl.h> 93#include <net/if_media.h> 94#include <net/if_ether.h> 95#include <net/if_ieee80211.h> 96#include <netinet/in.h> 97#include <netinet/in_var.h> 98#ifdef INET6 99#include <netinet6/nd6.h> 100#endif 101#include <arpa/inet.h> 102 103#include <netatalk/at.h> 104 105#define NSIP 106#include <netns/ns.h> 107#include <netns/ns_if.h> 108#include <netdb.h> 109 110#define EON 111#include <netiso/iso.h> 112#include <netiso/iso_var.h> 113#include <sys/protosw.h> 114 115#include <ctype.h> 116#include <err.h> 117#include <errno.h> 118#include <stddef.h> 119#include <stdio.h> 120#include <stdlib.h> 121#include <string.h> 122#include <unistd.h> 123 124struct ifreq ifr, ridreq; 125struct ifaliasreq addreq __attribute__((aligned(4))); 126#ifdef INET6 127struct in6_ifreq ifr6; 128struct in6_ifreq in6_ridreq; 129struct in6_aliasreq in6_addreq __attribute__((aligned(4))); 130#endif 131struct iso_ifreq iso_ridreq; 132struct iso_aliasreq iso_addreq; 133struct sockaddr_in netmask; 134struct netrange at_nr; /* AppleTalk net range */ 135 136char name[30]; 137int flags, metric, mtu, setaddr, setipdst, doalias; 138int clearaddr, s; 139int newaddr = -1; 140int nsellength = 1; 141int af; 142int Aflag, aflag, bflag, dflag, lflag, mflag, sflag, uflag; 143#ifdef INET6 144int Lflag; 145#endif 146int reset_if_flags; 147int explicit_prefix = 0; 148 149void notealias __P((char *, int)); 150void notrailers __P((char *, int)); 151void setifaddr __P((char *, int)); 152void setifdstaddr __P((char *, int)); 153void setifflags __P((char *, int)); 154void setifbroadaddr __P((char *, int)); 155void setifipdst __P((char *, int)); 156void setifmetric __P((char *, int)); 157void setifmtu __P((char *, int)); 158void setifnwid __P((char *, int)); 159void setifnetmask __P((char *, int)); 160void setifprefixlen __P((char *, int)); 161void setnsellength __P((char *, int)); 162void setsnpaoffset __P((char *, int)); 163void setatrange __P((char *, int)); 164void setatphase __P((char *, int)); 165#ifdef INET6 166void setia6flags __P((char *, int)); 167void setia6pltime __P((char *, int)); 168void setia6vltime __P((char *, int)); 169void setia6lifetime __P((char *, char *)); 170#endif 171void checkatrange __P ((struct sockaddr_at *)); 172void setmedia __P((char *, int)); 173void setmediaopt __P((char *, int)); 174void unsetmediaopt __P((char *, int)); 175void setmediainst __P((char *, int)); 176void fixnsel __P((struct sockaddr_iso *)); 177int main __P((int, char *[])); 178 179/* 180 * Media stuff. Whenever a media command is first performed, the 181 * currently select media is grabbed for this interface. If `media' 182 * is given, the current media word is modifed. `mediaopt' commands 183 * only modify the set and clear words. They then operate on the 184 * current media word later. 185 */ 186int media_current; 187int mediaopt_set; 188int mediaopt_clear; 189 190int actions; /* Actions performed */ 191 192#define A_MEDIA 0x0001 /* media command */ 193#define A_MEDIAOPTSET 0x0002 /* mediaopt command */ 194#define A_MEDIAOPTCLR 0x0004 /* -mediaopt command */ 195#define A_MEDIAOPT (A_MEDIAOPTSET|A_MEDIAOPTCLR) 196#define A_MEDIAINST 0x0008 /* instance or inst command */ 197 198#define NEXTARG 0xffffff 199 200struct cmd { 201 char *c_name; 202 int c_parameter; /* NEXTARG means next argv */ 203 int c_action; /* defered action */ 204 void (*c_func) __P((char *, int)); 205} cmds[] = { 206 { "up", IFF_UP, 0, setifflags } , 207 { "down", -IFF_UP, 0, setifflags }, 208 { "trailers", -1, 0, notrailers }, 209 { "-trailers", 1, 0, notrailers }, 210 { "arp", -IFF_NOARP, 0, setifflags }, 211 { "-arp", IFF_NOARP, 0, setifflags }, 212 { "debug", IFF_DEBUG, 0, setifflags }, 213 { "-debug", -IFF_DEBUG, 0, setifflags }, 214 { "alias", IFF_UP, 0, notealias }, 215 { "-alias", -IFF_UP, 0, notealias }, 216 { "delete", -IFF_UP, 0, notealias }, 217#ifdef notdef 218#define EN_SWABIPS 0x1000 219 { "swabips", EN_SWABIPS, 0, setifflags }, 220 { "-swabips", -EN_SWABIPS, 0, setifflags }, 221#endif 222 { "netmask", NEXTARG, 0, setifnetmask }, 223 { "metric", NEXTARG, 0, setifmetric }, 224 { "mtu", NEXTARG, 0, setifmtu }, 225 { "nwid", NEXTARG, 0, setifnwid }, 226 { "broadcast", NEXTARG, 0, setifbroadaddr }, 227 { "ipdst", NEXTARG, 0, setifipdst }, 228 { "prefixlen", NEXTARG, 0, setifprefixlen}, 229#ifdef INET6 230 { "anycast", IN6_IFF_ANYCAST, 0, setia6flags }, 231 { "-anycast", -IN6_IFF_ANYCAST, 0, setia6flags }, 232 { "tentative", IN6_IFF_TENTATIVE, 0, setia6flags }, 233 { "-tentative", -IN6_IFF_TENTATIVE, 0, setia6flags }, 234 { "pltime", NEXTARG, 0, setia6pltime }, 235 { "vltime", NEXTARG, 0, setia6vltime }, 236#endif /*INET6*/ 237#ifndef INET_ONLY 238 { "range", NEXTARG, 0, setatrange }, 239 { "phase", NEXTARG, 0, setatphase }, 240 { "snpaoffset", NEXTARG, 0, setsnpaoffset }, 241 { "nsellength", NEXTARG, 0, setnsellength }, 242#endif /* INET_ONLY */ 243 { "link0", IFF_LINK0, 0, setifflags } , 244 { "-link0", -IFF_LINK0, 0, setifflags } , 245 { "link1", IFF_LINK1, 0, setifflags } , 246 { "-link1", -IFF_LINK1, 0, setifflags } , 247 { "link2", IFF_LINK2, 0, setifflags } , 248 { "-link2", -IFF_LINK2, 0, setifflags } , 249 { "media", NEXTARG, A_MEDIA, setmedia }, 250 { "mediaopt", NEXTARG, A_MEDIAOPTSET, setmediaopt }, 251 { "-mediaopt", NEXTARG, A_MEDIAOPTCLR, unsetmediaopt }, 252 { "instance", NEXTARG, A_MEDIAINST, setmediainst }, 253 { "inst", NEXTARG, A_MEDIAINST, setmediainst }, 254 { 0, 0, 0, setifaddr }, 255 { 0, 0, 0, setifdstaddr }, 256}; 257 258void adjust_nsellength __P((void)); 259int getinfo __P((struct ifreq *)); 260int carrier __P((void)); 261void getsock __P((int)); 262void printall __P((void)); 263void printalias __P((const char *, int)); 264void printb __P((char *, unsigned short, char *)); 265int prefix __P((void *, int)); 266void status __P((const u_int8_t *, int)); 267void usage __P((void)); 268char *sec2str __P((time_t)); 269 270const char *get_media_type_string __P((int)); 271const char *get_media_subtype_string __P((int)); 272int get_media_subtype __P((int, const char *)); 273int get_media_options __P((int, const char *)); 274int lookup_media_word __P((struct ifmedia_description *, int, 275 const char *)); 276void print_media_word __P((int, int, int)); 277void process_media_commands __P((void)); 278void init_current_media __P((void)); 279 280/* 281 * XNS support liberally adapted from code written at the University of 282 * Maryland principally by James O'Toole and Chris Torek. 283 */ 284void in_alias __P((struct ifreq *)); 285void in_status __P((int)); 286void in_getaddr __P((char *, int)); 287#ifdef INET6 288void in6_fillscopeid __P((struct sockaddr_in6 *sin6)); 289void in6_alias __P((struct in6_ifreq *)); 290void in6_status __P((int)); 291void in6_getaddr __P((char *, int)); 292void in6_getprefix __P((char *, int)); 293#endif 294void at_status __P((int)); 295void at_getaddr __P((char *, int)); 296void xns_status __P((int)); 297void xns_getaddr __P((char *, int)); 298void iso_status __P((int)); 299void iso_getaddr __P((char *, int)); 300void ieee80211_status __P((void)); 301 302/* Known address families */ 303struct afswtch { 304 char *af_name; 305 short af_af; 306 void (*af_status) __P((int)); 307 void (*af_getaddr) __P((char *, int)); 308 void (*af_getprefix) __P((char *, int)); 309 u_long af_difaddr; 310 u_long af_aifaddr; 311 caddr_t af_ridreq; 312 caddr_t af_addreq; 313} afs[] = { 314#define C(x) ((caddr_t) &x) 315 { "inet", AF_INET, in_status, in_getaddr, NULL, 316 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) }, 317#ifdef INET6 318 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix, 319 SIOCDIFADDR_IN6, SIOCAIFADDR_IN6, C(in6_ridreq), C(in6_addreq) }, 320#endif 321#ifndef INET_ONLY /* small version, for boot media */ 322 { "atalk", AF_APPLETALK, at_status, at_getaddr, NULL, 323 SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) }, 324 { "ns", AF_NS, xns_status, xns_getaddr, NULL, 325 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) }, 326 { "iso", AF_ISO, iso_status, iso_getaddr, NULL, 327 SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) }, 328#endif /* INET_ONLY */ 329 { 0, 0, 0, 0 } 330}; 331 332struct afswtch *afp; /*the address family being set or asked about*/ 333 334struct afswtch *lookup_af __P((const char *)); 335 336int 337main(argc, argv) 338 int argc; 339 char *argv[]; 340{ 341 int ch; 342 343 /* Parse command-line options */ 344 aflag = mflag = 0; 345 while ((ch = getopt(argc, argv, "Aabdlmsu" 346#ifdef INET6 347 "L" 348#endif 349 )) != -1) { 350 switch (ch) { 351 case 'A': 352 Aflag = 1; 353 break; 354 355 case 'a': 356 aflag = 1; 357 break; 358 359 case 'b': 360 bflag = 1; 361 break; 362 363 case 'd': 364 dflag = 1; 365 break; 366 367#ifdef INET6 368 case 'L': 369 Lflag = 1; 370 break; 371#endif 372 373 case 'l': 374 lflag = 1; 375 break; 376 377 case 'm': 378 mflag = 1; 379 break; 380 381 case 's': 382 sflag = 1; 383 break; 384 385 case 'u': 386 uflag = 1; 387 break; 388 389 390 default: 391 usage(); 392 /* NOTREACHED */ 393 } 394 } 395 argc -= optind; 396 argv += optind; 397 398 /* 399 * -l means "list all interfaces", and is mutally exclusive with 400 * all other flags/commands. 401 * 402 * -a means "print status of all interfaces". 403 */ 404 if (lflag && (aflag || mflag || Aflag || argc)) 405 usage(); 406#ifdef INET6 407 if (lflag && Lflag) 408 usage(); 409#endif 410 if (aflag || lflag) { 411 if (argc > 1) 412 usage(); 413 else if (argc == 1) { 414 afp = lookup_af(argv[0]); 415 if (afp == NULL) 416 usage(); 417 } 418 if (afp) 419 af = ifr.ifr_addr.sa_family = afp->af_af; 420 else 421 af = ifr.ifr_addr.sa_family = afs[0].af_af; 422 printall(); 423 exit(0); 424 } 425 426 /* Make sure there's an interface name. */ 427 if (argc < 1) 428 usage(); 429 (void) strncpy(name, argv[0], sizeof(name)); 430 argc--; argv++; 431 432 /* Check for address family. */ 433 afp = NULL; 434 if (argc > 0) { 435 afp = lookup_af(argv[0]); 436 if (afp != NULL) { 437 argv++; 438 argc--; 439 } 440 } 441 442 /* Initialize af, just for use in getinfo(). */ 443 if (afp == NULL) 444 af = afs->af_af; 445 else 446 af = afp->af_af; 447 448 /* Get information about the interface. */ 449 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 450 if (getinfo(&ifr) < 0) 451 exit(1); 452 453 if (sflag) { 454 if (argc != 0) 455 usage(); 456 else 457 exit(carrier()); 458 } 459 460 /* No more arguments means interface status. */ 461 if (argc == 0) { 462 status(NULL, 0); 463 exit(0); 464 } 465 466 /* following operations assume inet familiy as default */ 467 if (afp == NULL) 468 afp = afs; 469 af = ifr.ifr_addr.sa_family = afp->af_af; 470 471#ifdef INET6 472 /* initialization */ 473 in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 474 in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 475#endif 476 477 /* Process commands. */ 478 while (argc > 0) { 479 struct cmd *p; 480 481 for (p = cmds; p->c_name; p++) 482 if (strcmp(argv[0], p->c_name) == 0) 483 break; 484 if (p->c_name == 0 && setaddr) { 485 if ((flags & IFF_POINTOPOINT) == 0) { 486 errx(1, "can't set destination address %s", 487 "on non-point-to-point link"); 488 } 489 p++; /* got src, do dst */ 490 } 491 if (p->c_func) { 492 if (p->c_parameter == NEXTARG) { 493 if (argc < 2) 494 errx(1, "'%s' requires argument", 495 p->c_name); 496 (*p->c_func)(argv[1], 0); 497 argc--, argv++; 498 } else 499 (*p->c_func)(argv[0], p->c_parameter); 500 actions |= p->c_action; 501 } 502 argc--, argv++; 503 } 504 505 /* Process any media commands that may have been issued. */ 506 process_media_commands(); 507 508 if (af == AF_INET6 && explicit_prefix == 0) { 509 /* 510 * Aggregatable address architecture defines all prefixes 511 * are 64. So, it is convenient to set prefixlen to 64 if 512 * it is not specified. 513 */ 514 setifprefixlen("64", 0); 515 /* in6_getprefix("64", MASK) if MASK is available here... */ 516 } 517 518#ifndef INET_ONLY 519 if (af == AF_ISO) 520 adjust_nsellength(); 521 522 if (af == AF_APPLETALK) 523 checkatrange((struct sockaddr_at *) &addreq.ifra_addr); 524 525 if (setipdst && af==AF_NS) { 526 struct nsip_req rq; 527 int size = sizeof(rq); 528 529 rq.rq_ns = addreq.ifra_addr; 530 rq.rq_ip = addreq.ifra_dstaddr; 531 532 if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0) 533 warn("encapsulation routing"); 534 } 535 536#endif /* INET_ONLY */ 537 538 if (clearaddr) { 539 int ret; 540 (void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); 541 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) { 542 if (errno == EADDRNOTAVAIL && (doalias >= 0)) { 543 /* means no previous address for interface */ 544 } else 545 warn("SIOCDIFADDR"); 546 } 547 } 548 if (newaddr > 0) { 549 (void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); 550 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) 551 warn("SIOCAIFADDR"); 552 } 553 if (reset_if_flags && ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) 554 err(1, "SIOCSIFFLAGS"); 555 exit(0); 556} 557 558struct afswtch * 559lookup_af(cp) 560 const char *cp; 561{ 562 struct afswtch *a; 563 564 for (a = afs; a->af_name != NULL; a++) 565 if (strcmp(a->af_name, cp) == 0) 566 return (a); 567 return (NULL); 568} 569 570void 571getsock(naf) 572 int naf; 573{ 574 static int oaf = -1; 575 576 if (oaf == naf) 577 return; 578 if (oaf != -1) 579 close(s); 580 s = socket(naf, SOCK_DGRAM, 0); 581 if (s < 0) 582 oaf = -1; 583 else 584 oaf = naf; 585} 586 587int 588getinfo(ifr) 589 struct ifreq *ifr; 590{ 591 592 getsock(af); 593 if (s < 0) 594 err(1, "socket"); 595 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) { 596 warn("SIOCGIFFLAGS %s", ifr->ifr_name); 597 return (-1); 598 } 599 flags = ifr->ifr_flags; 600 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) { 601 warn("SIOCGIFMETRIC %s", ifr->ifr_name); 602 metric = 0; 603 } else 604 metric = ifr->ifr_metric; 605 if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0) 606 mtu = 0; 607 else 608 mtu = ifr->ifr_mtu; 609 return (0); 610} 611 612void 613printalias(iname, af) 614 const char *iname; 615 int af; 616{ 617 char inbuf[8192]; 618 struct ifconf ifc; 619 struct ifreq *ifr; 620 int i, siz; 621 char ifrbuf[8192]; 622 623 ifc.ifc_len = sizeof(inbuf); 624 ifc.ifc_buf = inbuf; 625 getsock(af); 626 if (s < 0) 627 err(1, "socket"); 628 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) 629 err(1, "SIOCGIFCONF"); 630 ifr = ifc.ifc_req; 631 for (i = 0; i < ifc.ifc_len; ) { 632 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i); 633 siz = sizeof(ifr->ifr_name) + 634 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr) 635 ? ifr->ifr_addr.sa_len 636 : sizeof(struct sockaddr)); 637 i += siz; 638 /* avoid alignment issue */ 639 if (sizeof(ifrbuf) < siz) 640 errx(1, "ifr too big"); 641 memcpy(ifrbuf, ifr, siz); 642 ifr = (struct ifreq *)ifrbuf; 643 if (!strncmp(iname, ifr->ifr_name, sizeof(ifr->ifr_name))) { 644 if (ifr->ifr_addr.sa_family == af) 645 switch (af) { 646 case AF_INET: 647 in_alias(ifr); 648 break; 649 default: 650 /*none*/ 651 } 652 continue; 653 } 654 } 655} 656 657void 658printall() 659{ 660 char inbuf[8192]; 661 const struct sockaddr_dl *sdl = NULL; 662 struct ifconf ifc; 663 struct ifreq ifreq, *ifr; 664 int i, siz, idx; 665 char ifrbuf[8192]; 666 667 ifc.ifc_len = sizeof(inbuf); 668 ifc.ifc_buf = inbuf; 669 getsock(af); 670 if (s < 0) 671 err(1, "socket"); 672 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) 673 err(1, "SIOCGIFCONF"); 674 ifr = ifc.ifc_req; 675 ifreq.ifr_name[0] = '\0'; 676 for (i = 0, idx = 0; i < ifc.ifc_len; ) { 677 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i); 678 siz = sizeof(ifr->ifr_name) + 679 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr) 680 ? ifr->ifr_addr.sa_len 681 : sizeof(struct sockaddr)); 682 i += siz; 683 /* avoid alignment issue */ 684 if (sizeof(ifrbuf) < siz) 685 errx(1, "ifr too big"); 686 memcpy(ifrbuf, ifr, siz); 687 ifr = (struct ifreq *)ifrbuf; 688 if (ifr->ifr_addr.sa_family == AF_LINK) 689 sdl = (const struct sockaddr_dl *) &ifr->ifr_addr; 690 if (!strncmp(ifreq.ifr_name, ifr->ifr_name, 691 sizeof(ifr->ifr_name))) { 692 if (Aflag && ifr->ifr_addr.sa_family == AF_INET) 693 in_alias(ifr); 694 continue; 695 } 696 (void) strncpy(name, ifr->ifr_name, sizeof(ifr->ifr_name)); 697 ifreq = *ifr; 698 699 if (getinfo(&ifreq) < 0) 700 continue; 701 if (bflag && (flags & (IFF_POINTOPOINT|IFF_LOOPBACK))) 702 continue; 703 if (dflag && (flags & IFF_UP) != 0) 704 continue; 705 if (uflag && (flags & IFF_UP) == 0) 706 continue; 707 708 if (sflag && carrier()) 709 continue; 710 idx++; 711 /* 712 * Are we just listing the interfaces? 713 */ 714 if (lflag) { 715 if (idx > 1) 716 putchar(' '); 717 fputs(name, stdout); 718 continue; 719 } 720 721 if (sdl == NULL) { 722 status(NULL, 0); 723 } else { 724 status(LLADDR(sdl), sdl->sdl_alen); 725 sdl = NULL; 726 } 727 } 728 if (lflag) 729 putchar('\n'); 730} 731 732#define RIDADDR 0 733#define ADDR 1 734#define MASK 2 735#define DSTADDR 3 736 737/*ARGSUSED*/ 738void 739setifaddr(addr, param) 740 char *addr; 741 int param; 742{ 743 /* 744 * Delay the ioctl to set the interface addr until flags are all set. 745 * The address interpretation may depend on the flags, 746 * and the flags may change when the address is set. 747 */ 748 setaddr++; 749 if (newaddr == -1) 750 newaddr = 1; 751 if (doalias == 0) 752 clearaddr = 1; 753 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR)); 754} 755 756void 757setifnetmask(addr, d) 758 char *addr; 759 int d; 760{ 761 (*afp->af_getaddr)(addr, MASK); 762} 763 764void 765setifbroadaddr(addr, d) 766 char *addr; 767 int d; 768{ 769 (*afp->af_getaddr)(addr, DSTADDR); 770} 771 772void 773setifipdst(addr, d) 774 char *addr; 775 int d; 776{ 777 in_getaddr(addr, DSTADDR); 778 setipdst++; 779 clearaddr = 0; 780 newaddr = 0; 781} 782 783#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 784/*ARGSUSED*/ 785void 786notealias(addr, param) 787 char *addr; 788 int param; 789{ 790 if (setaddr && doalias == 0 && param < 0) 791 (void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq), 792 rqtosa(af_addreq)->sa_len); 793 doalias = param; 794 if (param < 0) { 795 clearaddr = 1; 796 newaddr = 0; 797 } else 798 clearaddr = 0; 799} 800 801/*ARGSUSED*/ 802void 803notrailers(vname, value) 804 char *vname; 805 int value; 806{ 807 puts("Note: trailers are no longer sent, but always received"); 808} 809 810/*ARGSUSED*/ 811void 812setifdstaddr(addr, param) 813 char *addr; 814 int param; 815{ 816 (*afp->af_getaddr)(addr, DSTADDR); 817} 818 819void 820setifflags(vname, value) 821 char *vname; 822 int value; 823{ 824 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) 825 err(1, "SIOCGIFFLAGS"); 826 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 827 flags = ifr.ifr_flags; 828 829 if (value < 0) { 830 value = -value; 831 flags &= ~value; 832 } else 833 flags |= value; 834 ifr.ifr_flags = flags; 835 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) 836 err(1, "SIOCSIFFLAGS"); 837 838 reset_if_flags = 1; 839} 840 841#ifdef INET6 842void 843setia6flags(vname, value) 844 char *vname; 845 int value; 846{ 847 if (value < 0) { 848 value = -value; 849 in6_addreq.ifra_flags &= ~value; 850 } else 851 in6_addreq.ifra_flags |= value; 852} 853 854void 855setia6pltime(val, d) 856 char *val; 857 int d; 858{ 859 setia6lifetime("pltime", val); 860} 861 862void 863setia6vltime(val, d) 864 char *val; 865 int d; 866{ 867 setia6lifetime("vltime", val); 868} 869 870void 871setia6lifetime(cmd, val) 872 char *cmd; 873 char *val; 874{ 875 time_t newval, t; 876 char *ep; 877 878 t = time(NULL); 879 newval = (time_t)strtoul(val, &ep, 0); 880 if (val == ep) 881 errx(1, "invalid %s", cmd); 882 if (afp->af_af != AF_INET6) 883 errx(1, "%s not allowed for the AF", cmd); 884 if (strcmp(cmd, "vltime") == 0) { 885 in6_addreq.ifra_lifetime.ia6t_expire = t + newval; 886 in6_addreq.ifra_lifetime.ia6t_vltime = newval; 887 } else if (strcmp(cmd, "pltime") == 0) { 888 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval; 889 in6_addreq.ifra_lifetime.ia6t_pltime = newval; 890 } 891} 892#endif 893 894void 895setifmetric(val, d) 896 char *val; 897 int d; 898{ 899 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 900 ifr.ifr_metric = atoi(val); 901 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 902 warn("SIOCSIFMETRIC"); 903} 904 905void 906setifmtu(val, d) 907 char *val; 908 int d; 909{ 910 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 911 ifr.ifr_mtu = atoi(val); 912 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 913 warn("SIOCSIFMTU"); 914} 915 916void 917setifnwid(val, d) 918 char *val; 919 int d; 920{ 921 u_int8_t nwid[IEEE80211_NWID_LEN]; 922 923 memset(&nwid, 0, sizeof(nwid)); 924 (void)strncpy(nwid, val, sizeof(nwid)); 925 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 926 ifr.ifr_data = (caddr_t)nwid; 927 if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0) 928 warn("SIOCS80211NWID"); 929} 930 931void 932ieee80211_status() 933{ 934 u_int8_t nwid[IEEE80211_NWID_LEN + 1]; 935 936 memset(&ifr, 0, sizeof(ifr)); 937 ifr.ifr_data = (caddr_t)nwid; 938 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 939 nwid[IEEE80211_NWID_LEN] = 0; 940 if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) == 0) 941 printf("\tnwid %s\n", nwid); 942} 943 944void 945init_current_media() 946{ 947 struct ifmediareq ifmr; 948 949 /* 950 * If we have not yet done so, grab the currently-selected 951 * media. 952 */ 953 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) { 954 (void) memset(&ifmr, 0, sizeof(ifmr)); 955 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 956 957 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 958 /* 959 * If we get E2BIG, the kernel is telling us 960 * that there are more, so we can ignore it. 961 */ 962 if (errno != E2BIG) 963 err(1, "SGIOCGIFMEDIA"); 964 } 965 966 media_current = ifmr.ifm_current; 967 } 968 969 /* Sanity. */ 970 if (IFM_TYPE(media_current) == 0) 971 errx(1, "%s: no link type?", name); 972} 973 974void 975process_media_commands() 976{ 977 978 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) { 979 /* Nothing to do. */ 980 return; 981 } 982 983 /* 984 * Media already set up, and commands sanity-checked. Set/clear 985 * any options, and we're ready to go. 986 */ 987 media_current |= mediaopt_set; 988 media_current &= ~mediaopt_clear; 989 990 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 991 ifr.ifr_media = media_current; 992 993 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0) 994 err(1, "SIOCSIFMEDIA"); 995} 996 997void 998setmedia(val, d) 999 char *val; 1000 int d; 1001{ 1002 int type, subtype, inst; 1003 1004 init_current_media(); 1005 1006 /* Only one media command may be given. */ 1007 if (actions & A_MEDIA) 1008 errx(1, "only one `media' command may be issued"); 1009 1010 /* Must not come after mediaopt commands */ 1011 if (actions & A_MEDIAOPT) 1012 errx(1, "may not issue `media' after `mediaopt' commands"); 1013 1014 /* 1015 * No need to check if `instance' has been issued; setmediainst() 1016 * craps out if `media' has not been specified. 1017 */ 1018 1019 type = IFM_TYPE(media_current); 1020 inst = IFM_INST(media_current); 1021 1022 /* Look up the subtype. */ 1023 subtype = get_media_subtype(type, val); 1024 1025 /* Build the new current media word. */ 1026 media_current = IFM_MAKEWORD(type, subtype, 0, inst); 1027 1028 /* Media will be set after other processing is complete. */ 1029} 1030 1031void 1032setmediaopt(val, d) 1033 char *val; 1034 int d; 1035{ 1036 1037 init_current_media(); 1038 1039 /* Can only issue `mediaopt' once. */ 1040 if (actions & A_MEDIAOPTSET) 1041 errx(1, "only one `mediaopt' command may be issued"); 1042 1043 /* Can't issue `mediaopt' if `instance' has already been issued. */ 1044 if (actions & A_MEDIAINST) 1045 errx(1, "may not issue `mediaopt' after `instance'"); 1046 1047 mediaopt_set = get_media_options(IFM_TYPE(media_current), val); 1048 1049 /* Media will be set after other processing is complete. */ 1050} 1051 1052void 1053unsetmediaopt(val, d) 1054 char *val; 1055 int d; 1056{ 1057 1058 init_current_media(); 1059 1060 /* Can only issue `-mediaopt' once. */ 1061 if (actions & A_MEDIAOPTCLR) 1062 errx(1, "only one `-mediaopt' command may be issued"); 1063 1064 /* May not issue `media' and `-mediaopt'. */ 1065 if (actions & A_MEDIA) 1066 errx(1, "may not issue both `media' and `-mediaopt'"); 1067 1068 /* 1069 * No need to check for A_MEDIAINST, since the test for A_MEDIA 1070 * implicitly checks for A_MEDIAINST. 1071 */ 1072 1073 mediaopt_clear = get_media_options(IFM_TYPE(media_current), val); 1074 1075 /* Media will be set after other processing is complete. */ 1076} 1077 1078void 1079setmediainst(val, d) 1080 char *val; 1081 int d; 1082{ 1083 int type, subtype, options, inst; 1084 1085 init_current_media(); 1086 1087 /* Can only issue `instance' once. */ 1088 if (actions & A_MEDIAINST) 1089 errx(1, "only one `instance' command may be issued"); 1090 1091 /* Must have already specified `media' */ 1092 if ((actions & A_MEDIA) == 0) 1093 errx(1, "must specify `media' before `instance'"); 1094 1095 type = IFM_TYPE(media_current); 1096 subtype = IFM_SUBTYPE(media_current); 1097 options = IFM_OPTIONS(media_current); 1098 1099 inst = atoi(val); 1100 if (inst < 0 || inst > IFM_INST_MAX) 1101 errx(1, "invalid media instance: %s", val); 1102 1103 media_current = IFM_MAKEWORD(type, subtype, options, inst); 1104 1105 /* Media will be set after other processing is complete. */ 1106} 1107 1108struct ifmedia_description ifm_type_descriptions[] = 1109 IFM_TYPE_DESCRIPTIONS; 1110 1111struct ifmedia_description ifm_subtype_descriptions[] = 1112 IFM_SUBTYPE_DESCRIPTIONS; 1113 1114struct ifmedia_description ifm_option_descriptions[] = 1115 IFM_OPTION_DESCRIPTIONS; 1116 1117const char * 1118get_media_type_string(mword) 1119 int mword; 1120{ 1121 struct ifmedia_description *desc; 1122 1123 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; 1124 desc++) { 1125 if (IFM_TYPE(mword) == desc->ifmt_word) 1126 return (desc->ifmt_string); 1127 } 1128 return ("<unknown type>"); 1129} 1130 1131const char * 1132get_media_subtype_string(mword) 1133 int mword; 1134{ 1135 struct ifmedia_description *desc; 1136 1137 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; 1138 desc++) { 1139 if (IFM_TYPE_MATCH(desc->ifmt_word, mword) && 1140 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword)) 1141 return (desc->ifmt_string); 1142 } 1143 return ("<unknown subtype>"); 1144} 1145 1146int 1147get_media_subtype(type, val) 1148 int type; 1149 const char *val; 1150{ 1151 int rval; 1152 1153 rval = lookup_media_word(ifm_subtype_descriptions, type, val); 1154 if (rval == -1) 1155 errx(1, "unknown %s media subtype: %s", 1156 get_media_type_string(type), val); 1157 1158 return (rval); 1159} 1160 1161int 1162get_media_options(type, val) 1163 int type; 1164 const char *val; 1165{ 1166 char *optlist, *str; 1167 int option, rval = 0; 1168 1169 /* We muck with the string, so copy it. */ 1170 optlist = strdup(val); 1171 if (optlist == NULL) 1172 err(1, "strdup"); 1173 str = optlist; 1174 1175 /* 1176 * Look up the options in the user-provided comma-separated list. 1177 */ 1178 for (; (str = strtok(str, ",")) != NULL; str = NULL) { 1179 option = lookup_media_word(ifm_option_descriptions, type, str); 1180 if (option == -1) 1181 errx(1, "unknown %s media option: %s", 1182 get_media_type_string(type), str); 1183 rval |= IFM_OPTIONS(option); 1184 } 1185 1186 free(optlist); 1187 return (rval); 1188} 1189 1190int 1191lookup_media_word(desc, type, val) 1192 struct ifmedia_description *desc; 1193 int type; 1194 const char *val; 1195{ 1196 1197 for (; desc->ifmt_string != NULL; desc++) { 1198 if (IFM_TYPE_MATCH(desc->ifmt_word, type) && 1199 strcasecmp(desc->ifmt_string, val) == 0) 1200 return (desc->ifmt_word); 1201 } 1202 return (-1); 1203} 1204 1205void 1206print_media_word(ifmw, print_type, as_syntax) 1207 int ifmw, print_type, as_syntax; 1208{ 1209 struct ifmedia_description *desc; 1210 int seen_option = 0; 1211 1212 if (print_type) 1213 printf("%s ", get_media_type_string(ifmw)); 1214 printf("%s%s", as_syntax ? "media " : "", 1215 get_media_subtype_string(ifmw)); 1216 1217 /* Find options. */ 1218 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL; 1219 desc++) { 1220 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 1221 (ifmw & desc->ifmt_word) != 0 && 1222 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) { 1223 if (seen_option == 0) 1224 printf(" %s", as_syntax ? "mediaopt " : ""); 1225 printf("%s%s", seen_option ? "," : "", 1226 desc->ifmt_string); 1227 seen_option |= IFM_OPTIONS(desc->ifmt_word); 1228 } 1229 } 1230 if (IFM_INST(ifmw) != 0) 1231 printf(" instance %d", IFM_INST(ifmw)); 1232} 1233 1234int carrier() 1235{ 1236 struct ifmediareq ifmr; 1237 1238 (void) memset(&ifmr, 0, sizeof(ifmr)); 1239 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 1240 1241 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 1242 /* 1243 * Interface doesn't support SIOC{G,S}IFMEDIA; 1244 * assume ok. 1245 */ 1246 return 0; 1247 } 1248 if ((ifmr.ifm_status & IFM_AVALID) == 0) { 1249 /* 1250 * Interface doesn't report media-valid status. 1251 * assume ok. 1252 */ 1253 return 0; 1254 } 1255 /* otherwise, return ok for active, not-ok if not active. */ 1256 return !(ifmr.ifm_status & IFM_ACTIVE); 1257} 1258 1259 1260#define IFFBITS \ 1261"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\ 1262\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST" 1263 1264const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST; 1265 1266const struct ifmedia_status_description ifm_status_descriptions[] = 1267 IFM_STATUS_DESCRIPTIONS; 1268 1269/* 1270 * Print the status of the interface. If an address family was 1271 * specified, show it and it only; otherwise, show them all. 1272 */ 1273void 1274status(ap, alen) 1275 const u_int8_t *ap; 1276 int alen; 1277{ 1278 struct afswtch *p = afp; 1279 struct ifmediareq ifmr; 1280 int *media_list, i; 1281 1282 printf("%s: ", name); 1283 printb("flags", flags, IFFBITS); 1284 if (metric) 1285 printf(" metric %d", metric); 1286 if (mtu) 1287 printf(" mtu %d", mtu); 1288 putchar('\n'); 1289 1290 ieee80211_status(); 1291 1292 if (ap && alen > 0) { 1293 printf("\taddress:"); 1294 for (i = 0; i < alen; i++, ap++) 1295 printf("%c%02x", i > 0 ? ':' : ' ', *ap); 1296 putchar('\n'); 1297 } 1298 1299 (void) memset(&ifmr, 0, sizeof(ifmr)); 1300 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 1301 1302 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 1303 /* 1304 * Interface doesn't support SIOC{G,S}IFMEDIA. 1305 */ 1306 goto proto_status; 1307 } 1308 1309 if (ifmr.ifm_count == 0) { 1310 warnx("%s: no media types?", name); 1311 goto proto_status; 1312 } 1313 1314 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int)); 1315 if (media_list == NULL) 1316 err(1, "malloc"); 1317 ifmr.ifm_ulist = media_list; 1318 1319 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) 1320 err(1, "SIOCGIFMEDIA"); 1321 1322 printf("\tmedia: "); 1323 print_media_word(ifmr.ifm_current, 1, 0); 1324 if (ifmr.ifm_active != ifmr.ifm_current) { 1325 putchar(' '); 1326 putchar('('); 1327 print_media_word(ifmr.ifm_active, 0, 0); 1328 putchar(')'); 1329 } 1330 putchar('\n'); 1331 1332 if (ifmr.ifm_status & IFM_STATUS_VALID) { 1333 const struct ifmedia_status_description *ifms; 1334 int bitno, found = 0; 1335 1336 printf("\tstatus: "); 1337 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) { 1338 for (ifms = ifm_status_descriptions; 1339 ifms->ifms_valid != 0; ifms++) { 1340 if (ifms->ifms_type != 1341 IFM_TYPE(ifmr.ifm_current) && 1342 ifms->ifms_valid != 1343 ifm_status_valid_list[bitno]) 1344 continue; 1345 printf("%s%s", found ? ", " : "", 1346 IFM_STATUS_DESC(ifms, ifmr.ifm_status)); 1347 found = 1; 1348 1349 /* 1350 * For each valid indicator bit, there's 1351 * only one entry for each media type, so 1352 * terminate the inner loop now. 1353 */ 1354 break; 1355 } 1356 } 1357 1358 if (found == 0) 1359 printf("unknown"); 1360 putchar('\n'); 1361 } 1362 1363 if (mflag) { 1364 int type, printed_type; 1365 1366 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) { 1367 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) { 1368 if (IFM_TYPE(media_list[i]) == type) { 1369 if (printed_type == 0) { 1370 printf("\tsupported %s media:\n", 1371 get_media_type_string(type)); 1372 printed_type = 1; 1373 } 1374 printf("\t\t"); 1375 print_media_word(media_list[i], 0, 1); 1376 printf("\n"); 1377 } 1378 } 1379 } 1380 } 1381 1382 free(media_list); 1383 1384 proto_status: 1385 if ((p = afp) != NULL) { 1386 (*p->af_status)(1); 1387 if (Aflag & !aflag) 1388 printalias(name, p->af_af); 1389 } else for (p = afs; p->af_name; p++) { 1390 ifr.ifr_addr.sa_family = p->af_af; 1391 (*p->af_status)(0); 1392 if (Aflag & !aflag && p->af_af == AF_INET) 1393 printalias(name, p->af_af); 1394 } 1395} 1396 1397void 1398in_alias(creq) 1399 struct ifreq *creq; 1400{ 1401 struct sockaddr_in *sin; 1402 1403 if (lflag) 1404 return; 1405 1406 /* Get the non-alias address for this interface. */ 1407 getsock(AF_INET); 1408 if (s < 0) { 1409 if (errno == EPROTONOSUPPORT) 1410 return; 1411 err(1, "socket"); 1412 } 1413 (void) memset(&ifr, 0, sizeof(ifr)); 1414 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1415 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1416 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1417 return; 1418 } else 1419 warn("SIOCGIFADDR"); 1420 } 1421 /* If creq and ifr are the same address, this is not an alias. */ 1422 if (memcmp(&ifr.ifr_addr, &creq->ifr_addr, 1423 sizeof(creq->ifr_addr)) == 0) 1424 return; 1425 (void) memset(&addreq, 0, sizeof(addreq)); 1426 (void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name)); 1427 addreq.ifra_addr = creq->ifr_addr; 1428 if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) { 1429 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1430 return; 1431 } else 1432 warn("SIOCGIFALIAS"); 1433 } 1434 1435 sin = (struct sockaddr_in *)&addreq.ifra_addr; 1436 printf("\tinet alias %s", inet_ntoa(sin->sin_addr)); 1437 1438 if (flags & IFF_POINTOPOINT) { 1439 sin = (struct sockaddr_in *)&addreq.ifra_dstaddr; 1440 printf(" -> %s", inet_ntoa(sin->sin_addr)); 1441 } 1442 1443 sin = (struct sockaddr_in *)&addreq.ifra_mask; 1444 printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr)); 1445 1446 if (flags & IFF_BROADCAST) { 1447 sin = (struct sockaddr_in *)&addreq.ifra_broadaddr; 1448 printf(" broadcast %s", inet_ntoa(sin->sin_addr)); 1449 } 1450 printf("\n"); 1451} 1452 1453void 1454in_status(force) 1455 int force; 1456{ 1457 struct sockaddr_in *sin; 1458 1459 getsock(AF_INET); 1460 if (s < 0) { 1461 if (errno == EPROTONOSUPPORT) 1462 return; 1463 err(1, "socket"); 1464 } 1465 (void) memset(&ifr, 0, sizeof(ifr)); 1466 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1467 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1468 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1469 if (!force) 1470 return; 1471 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1472 } else 1473 warn("SIOCGIFADDR"); 1474 } 1475 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1476 sin = (struct sockaddr_in *)&ifr.ifr_addr; 1477 printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 1478 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1479 if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) { 1480 if (errno != EADDRNOTAVAIL) 1481 warn("SIOCGIFNETMASK"); 1482 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1483 } else 1484 netmask.sin_addr = 1485 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; 1486 if (flags & IFF_POINTOPOINT) { 1487 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) { 1488 if (errno == EADDRNOTAVAIL) 1489 (void) memset(&ifr.ifr_addr, 0, 1490 sizeof(ifr.ifr_addr)); 1491 else 1492 warn("SIOCGIFDSTADDR"); 1493 } 1494 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1495 sin = (struct sockaddr_in *)&ifr.ifr_dstaddr; 1496 printf("--> %s ", inet_ntoa(sin->sin_addr)); 1497 } 1498 printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr)); 1499 if (flags & IFF_BROADCAST) { 1500 if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) { 1501 if (errno == EADDRNOTAVAIL) 1502 (void) memset(&ifr.ifr_addr, 0, 1503 sizeof(ifr.ifr_addr)); 1504 else 1505 warn("SIOCGIFBRDADDR"); 1506 } 1507 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1508 sin = (struct sockaddr_in *)&ifr.ifr_addr; 1509 if (sin->sin_addr.s_addr != 0) 1510 printf("broadcast %s", inet_ntoa(sin->sin_addr)); 1511 } 1512 putchar('\n'); 1513} 1514 1515void 1516setifprefixlen(addr, d) 1517 char *addr; 1518 int d; 1519{ 1520 if (*afp->af_getprefix) 1521 (*afp->af_getprefix)(addr, MASK); 1522 explicit_prefix = 1; 1523} 1524 1525#ifdef INET6 1526void 1527in6_fillscopeid(sin6) 1528 struct sockaddr_in6 *sin6; 1529{ 1530#if defined(__KAME__) && defined(KAME_SCOPEID) 1531 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 1532 sin6->sin6_scope_id = 1533 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]); 1534 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0; 1535 } 1536#endif 1537} 1538 1539/* XXX not really an alias */ 1540void 1541in6_alias(creq) 1542 struct in6_ifreq *creq; 1543{ 1544 struct sockaddr_in6 *sin6; 1545 char hbuf[NI_MAXHOST]; 1546 u_int32_t scopeid; 1547#ifdef NI_WITHSCOPEID 1548 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 1549#else 1550 const int niflag = NI_NUMERICHOST; 1551#endif 1552 1553 /* Get the non-alias address for this interface. */ 1554 getsock(AF_INET6); 1555 if (s < 0) { 1556 if (errno == EPROTONOSUPPORT) 1557 return; 1558 err(1, "socket"); 1559 } 1560 1561 sin6 = (struct sockaddr_in6 *)&creq->ifr_addr; 1562 1563 in6_fillscopeid(sin6); 1564 scopeid = sin6->sin6_scope_id; 1565 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, 1566 hbuf, sizeof(hbuf), NULL, 0, niflag)) 1567 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */ 1568 printf("\tinet6 %s", hbuf); 1569 1570 if (flags & IFF_POINTOPOINT) { 1571 (void) memset(&ifr6, 0, sizeof(ifr6)); 1572 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1573 ifr6.ifr_addr = creq->ifr_addr; 1574 if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) { 1575 if (errno != EADDRNOTAVAIL) 1576 warn("SIOCGIFDSTADDR_IN6"); 1577 (void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr)); 1578 ifr6.ifr_addr.sin6_family = AF_INET6; 1579 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); 1580 } 1581 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr; 1582 in6_fillscopeid(sin6); 1583 hbuf[0] = '\0'; 1584 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, 1585 hbuf, sizeof(hbuf), NULL, 0, niflag)) 1586 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */ 1587 printf(" -> %s", hbuf); 1588 } 1589 1590 (void) memset(&ifr6, 0, sizeof(ifr6)); 1591 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1592 ifr6.ifr_addr = creq->ifr_addr; 1593 if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) { 1594 if (errno != EADDRNOTAVAIL) 1595 warn("SIOCGIFNETMASK_IN6"); 1596 } else { 1597 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr; 1598 printf(" prefixlen %d", prefix(&sin6->sin6_addr, 1599 sizeof(struct in6_addr))); 1600 } 1601 1602 (void) memset(&ifr6, 0, sizeof(ifr6)); 1603 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1604 ifr6.ifr_addr = creq->ifr_addr; 1605 if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) { 1606 if (errno != EADDRNOTAVAIL) 1607 warn("SIOCGIFAFLAG_IN6"); 1608 } else { 1609 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST) 1610 printf(" anycast"); 1611 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE) 1612 printf(" tentative"); 1613 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED) 1614 printf(" duplicated"); 1615 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED) 1616 printf(" detached"); 1617 } 1618 1619 if (scopeid) 1620 printf(" scopeid 0x%x", scopeid); 1621 1622 if (Lflag) { 1623 struct in6_addrlifetime *lifetime; 1624 (void) memset(&ifr6, 0, sizeof(ifr6)); 1625 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1626 ifr6.ifr_addr = creq->ifr_addr; 1627 lifetime = &ifr6.ifr_ifru.ifru_lifetime; 1628 if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) { 1629 if (errno != EADDRNOTAVAIL) 1630 warn("SIOCGIFALIFETIME_IN6"); 1631 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) { 1632 time_t t = time(NULL); 1633 printf(" pltime "); 1634 if (lifetime->ia6t_preferred) { 1635 printf("%s", lifetime->ia6t_preferred < t 1636 ? "0" 1637 : sec2str(lifetime->ia6t_preferred - t)); 1638 } else 1639 printf("infty"); 1640 1641 printf(" vltime "); 1642 if (lifetime->ia6t_expire) { 1643 printf("%s", lifetime->ia6t_expire < t 1644 ? "0" 1645 : sec2str(lifetime->ia6t_expire - t)); 1646 } else 1647 printf("infty"); 1648 } 1649 } 1650 1651 printf("\n"); 1652} 1653 1654void 1655in6_status(force) 1656 int force; 1657{ 1658 char inbuf[8192]; 1659 struct ifconf ifc; 1660 struct ifreq *ifr; 1661 int i, siz; 1662 char ifrbuf[8192]; 1663 1664 ifc.ifc_len = sizeof(inbuf); 1665 ifc.ifc_buf = inbuf; 1666 getsock(af); 1667 if (s < 0) 1668 err(1, "socket"); 1669 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) 1670 err(1, "SIOCGIFCONF"); 1671 ifr = ifc.ifc_req; 1672 for (i = 0; i < ifc.ifc_len; ) { 1673 ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i); 1674 siz = sizeof(ifr->ifr_name) + 1675 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr) 1676 ? ifr->ifr_addr.sa_len 1677 : sizeof(struct sockaddr)); 1678 i += siz; 1679 /* avoid alignment issue */ 1680 if (sizeof(ifrbuf) < siz) 1681 errx(1, "ifr too big"); 1682 memcpy(ifrbuf, ifr, siz); 1683 ifr = (struct ifreq *)ifrbuf; 1684 if (!strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))) { 1685 if (ifr->ifr_addr.sa_family == AF_INET6) 1686 in6_alias((struct in6_ifreq *)ifr); 1687 } 1688 } 1689} 1690#endif /*INET6*/ 1691 1692#ifndef INET_ONLY 1693 1694void 1695at_status(force) 1696 int force; 1697{ 1698 struct sockaddr_at *sat, null_sat; 1699 struct netrange *nr; 1700 1701 getsock(AF_APPLETALK); 1702 if (s < 0) { 1703 if (errno == EPROTONOSUPPORT) 1704 return; 1705 err(1, "socket"); 1706 } 1707 (void) memset(&ifr, 0, sizeof(ifr)); 1708 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1709 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1710 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1711 if (!force) 1712 return; 1713 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1714 } else 1715 warn("SIOCGIFADDR"); 1716 } 1717 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1718 sat = (struct sockaddr_at *)&ifr.ifr_addr; 1719 1720 (void) memset(&null_sat, 0, sizeof(null_sat)); 1721 1722 nr = (struct netrange *) &sat->sat_zero; 1723 printf("\tatalk %d.%d range %d-%d phase %d", 1724 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node, 1725 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase); 1726 if (flags & IFF_POINTOPOINT) { 1727 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) { 1728 if (errno == EADDRNOTAVAIL) 1729 (void) memset(&ifr.ifr_addr, 0, 1730 sizeof(ifr.ifr_addr)); 1731 else 1732 warn("SIOCGIFDSTADDR"); 1733 } 1734 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1735 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr; 1736 if (!sat) 1737 sat = &null_sat; 1738 printf("--> %d.%d", 1739 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node); 1740 } 1741 if (flags & IFF_BROADCAST) { 1742 /* note RTAX_BRD overlap with IFF_POINTOPOINT */ 1743 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr; 1744 if (sat) 1745 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net), 1746 sat->sat_addr.s_node); 1747 } 1748 putchar('\n'); 1749} 1750 1751void 1752xns_status(force) 1753 int force; 1754{ 1755 struct sockaddr_ns *sns; 1756 1757 getsock(AF_NS); 1758 if (s < 0) { 1759 if (errno == EPROTONOSUPPORT) 1760 return; 1761 err(1, "socket"); 1762 } 1763 (void) memset(&ifr, 0, sizeof(ifr)); 1764 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1765 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1766 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1767 if (!force) 1768 return; 1769 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1770 } else 1771 warn("SIOCGIFADDR"); 1772 } 1773 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1774 sns = (struct sockaddr_ns *)&ifr.ifr_addr; 1775 printf("\tns %s ", ns_ntoa(sns->sns_addr)); 1776 if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */ 1777 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) { 1778 if (errno == EADDRNOTAVAIL) 1779 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1780 else 1781 warn("SIOCGIFDSTADDR"); 1782 } 1783 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1784 sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr; 1785 printf("--> %s ", ns_ntoa(sns->sns_addr)); 1786 } 1787 putchar('\n'); 1788} 1789 1790void 1791iso_status(force) 1792 int force; 1793{ 1794 struct sockaddr_iso *siso; 1795 struct iso_ifreq ifr; 1796 1797 getsock(AF_ISO); 1798 if (s < 0) { 1799 if (errno == EPROTONOSUPPORT) 1800 return; 1801 err(1, "socket"); 1802 } 1803 (void) memset(&ifr, 0, sizeof(ifr)); 1804 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1805 if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) { 1806 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1807 if (!force) 1808 return; 1809 (void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr)); 1810 } else 1811 warn("SIOCGIFADDR_ISO"); 1812 } 1813 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1814 siso = &ifr.ifr_Addr; 1815 printf("\tiso %s ", iso_ntoa(&siso->siso_addr)); 1816 if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) { 1817 if (errno == EADDRNOTAVAIL) 1818 memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr)); 1819 else 1820 warn("SIOCGIFNETMASK_ISO"); 1821 } else { 1822 if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr)) 1823 siso->siso_addr.isoa_len = siso->siso_len 1824 - offsetof(struct sockaddr_iso, siso_addr); 1825 printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr)); 1826 } 1827 if (flags & IFF_POINTOPOINT) { 1828 if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) { 1829 if (errno == EADDRNOTAVAIL) 1830 memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr)); 1831 else 1832 warn("SIOCGIFDSTADDR_ISO"); 1833 } 1834 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1835 siso = &ifr.ifr_Addr; 1836 printf("--> %s ", iso_ntoa(&siso->siso_addr)); 1837 } 1838 putchar('\n'); 1839} 1840 1841#endif /* INET_ONLY */ 1842 1843#define SIN(x) ((struct sockaddr_in *) &(x)) 1844struct sockaddr_in *sintab[] = { 1845SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr), 1846SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)}; 1847 1848void 1849in_getaddr(s, which) 1850 char *s; 1851 int which; 1852{ 1853 struct sockaddr_in *sin = sintab[which]; 1854 struct hostent *hp; 1855 struct netent *np; 1856 1857 sin->sin_len = sizeof(*sin); 1858 if (which != MASK) 1859 sin->sin_family = AF_INET; 1860 1861 if (which == ADDR) { 1862 char *p = NULL; 1863 1864 if((p = strrchr(s, '/')) != NULL) { 1865 /* address is `name/masklen' */ 1866 int masklen; 1867 int ret; 1868 struct sockaddr_in *min = sintab[MASK]; 1869 *p = '\0'; 1870 ret = sscanf(p+1, "%u", &masklen); 1871 if(ret != 1 || (masklen < 0 || masklen > 32)) { 1872 *p = '/'; 1873 errx(1, "%s: bad value", s); 1874 } 1875 min->sin_len = sizeof(*min); 1876 min->sin_addr.s_addr = 1877 htonl(~((1LL << (32 - masklen)) - 1) & 1878 0xffffffff); 1879 } 1880 } 1881 1882 if (inet_aton(s, &sin->sin_addr) == 0) { 1883 if ((hp = gethostbyname(s)) != NULL) 1884 (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length); 1885 else if ((np = getnetbyname(s)) != NULL) 1886 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 1887 else 1888 errx(1, "%s: bad value", s); 1889 } 1890} 1891 1892/* 1893 * Print a value a la the %b format of the kernel's printf 1894 */ 1895void 1896printb(s, v, bits) 1897 char *s; 1898 char *bits; 1899 unsigned short v; 1900{ 1901 int i, any = 0; 1902 char c; 1903 1904 if (bits && *bits == 8) 1905 printf("%s=%o", s, v); 1906 else 1907 printf("%s=%x", s, v); 1908 bits++; 1909 if (bits) { 1910 putchar('<'); 1911 while ((i = *bits++) != 0) { 1912 if (v & (1 << (i-1))) { 1913 if (any) 1914 putchar(','); 1915 any = 1; 1916 for (; (c = *bits) > 32; bits++) 1917 putchar(c); 1918 } else 1919 for (; *bits > 32; bits++) 1920 ; 1921 } 1922 putchar('>'); 1923 } 1924} 1925 1926#ifdef INET6 1927#define SIN6(x) ((struct sockaddr_in6 *) &(x)) 1928struct sockaddr_in6 *sin6tab[] = { 1929SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), 1930SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)}; 1931 1932void 1933in6_getaddr(s, which) 1934 char *s; 1935 int which; 1936{ 1937#if defined(__KAME__) && defined(KAME_SCOPEID) 1938 struct sockaddr_in6 *sin6 = sin6tab[which]; 1939 struct addrinfo hints, *res; 1940 int error; 1941 1942 memset(&hints, 0, sizeof(hints)); 1943 hints.ai_family = AF_INET6; 1944 hints.ai_socktype = SOCK_DGRAM; 1945#if 0 /* in_getaddr() allows FQDN */ 1946 hints.ai_flags = AI_NUMERICHOST; 1947#endif 1948 error = getaddrinfo(s, "0", &hints, &res); 1949 if (error) 1950 errx(1, "%s: %s", s, gai_strerror(error)); 1951 if (res->ai_next) 1952 errx(1, "%s: resolved to multiple hosts", s); 1953 if (res->ai_addrlen != sizeof(struct sockaddr_in6)) 1954 errx(1, "%s: bad value", s); 1955 memcpy(sin6, res->ai_addr, res->ai_addrlen); 1956 freeaddrinfo(res); 1957 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) { 1958 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 1959 htons(sin6->sin6_scope_id); 1960 sin6->sin6_scope_id = 0; 1961 } 1962#else 1963 struct sockaddr_in6 *sin = sin6tab[which]; 1964 1965 sin->sin6_len = sizeof(*sin); 1966 if (which != MASK) 1967 sin->sin6_family = AF_INET6; 1968 1969 if (which == ADDR) { 1970 char *p = NULL; 1971 if((p = strrchr(s, '/')) != NULL) { 1972 *p = '\0'; 1973 in6_getprefix(p + 1, MASK); 1974 explicit_prefix = 1; 1975 } 1976 } 1977 1978 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 1979 errx(1, "%s: bad value", s); 1980#endif 1981} 1982 1983void 1984in6_getprefix(plen, which) 1985 char *plen; 1986 int which; 1987{ 1988 register struct sockaddr_in6 *sin = sin6tab[which]; 1989 register u_char *cp; 1990 int len = strtol(plen, (char **)NULL, 10); 1991 1992 if ((len < 0) || (len > 128)) 1993 errx(1, "%s: bad value", plen); 1994 sin->sin6_len = sizeof(*sin); 1995 if (which != MASK) 1996 sin->sin6_family = AF_INET6; 1997 if ((len == 0) || (len == 128)) { 1998 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); 1999 return; 2000 } 2001 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); 2002 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 2003 *cp++ = 0xff; 2004 *cp = 0xff << (8 - len); 2005} 2006 2007int 2008prefix(val, size) 2009 void *val; 2010 int size; 2011{ 2012 register u_char *name = (u_char *)val; 2013 register int byte, bit, plen = 0; 2014 2015 for (byte = 0; byte < size; byte++, plen += 8) 2016 if (name[byte] != 0xff) 2017 break; 2018 if (byte == size) 2019 return (plen); 2020 for (bit = 7; bit != 0; bit--, plen++) 2021 if (!(name[byte] & (1 << bit))) 2022 break; 2023 for (; bit != 0; bit--) 2024 if (name[byte] & (1 << bit)) 2025 return(0); 2026 byte++; 2027 for (; byte < size; byte++) 2028 if (name[byte]) 2029 return(0); 2030 return (plen); 2031} 2032#endif /*INET6*/ 2033 2034#ifndef INET_ONLY 2035void 2036at_getaddr(addr, which) 2037 char *addr; 2038 int which; 2039{ 2040 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr; 2041 u_int net, node; 2042 2043 sat->sat_family = AF_APPLETALK; 2044 sat->sat_len = sizeof(*sat); 2045 if (which == MASK) 2046 errx(1, "AppleTalk does not use netmasks\n"); 2047 if (sscanf(addr, "%u.%u", &net, &node) != 2 2048 || net == 0 || net > 0xffff || node == 0 || node > 0xfe) 2049 errx(1, "%s: illegal address", addr); 2050 sat->sat_addr.s_net = htons(net); 2051 sat->sat_addr.s_node = node; 2052} 2053 2054void 2055setatrange(range, d) 2056 char *range; 2057 int d; 2058{ 2059 u_short first = 123, last = 123; 2060 2061 if (sscanf(range, "%hu-%hu", &first, &last) != 2 2062 || first == 0 || first > 0xffff 2063 || last == 0 || last > 0xffff || first > last) 2064 errx(1, "%s: illegal net range: %u-%u", range, first, last); 2065 at_nr.nr_firstnet = htons(first); 2066 at_nr.nr_lastnet = htons(last); 2067} 2068 2069void 2070setatphase(phase, d) 2071 char *phase; 2072 int d; 2073{ 2074 if (!strcmp(phase, "1")) 2075 at_nr.nr_phase = 1; 2076 else if (!strcmp(phase, "2")) 2077 at_nr.nr_phase = 2; 2078 else 2079 errx(1, "%s: illegal phase", phase); 2080} 2081 2082void 2083checkatrange(sat) 2084 struct sockaddr_at *sat; 2085{ 2086 if (at_nr.nr_phase == 0) 2087 at_nr.nr_phase = 2; /* Default phase 2 */ 2088 if (at_nr.nr_firstnet == 0) 2089 at_nr.nr_firstnet = /* Default range of one */ 2090 at_nr.nr_lastnet = sat->sat_addr.s_net; 2091 printf("\tatalk %d.%d range %d-%d phase %d\n", 2092 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node, 2093 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase); 2094 if ((u_short) ntohs(at_nr.nr_firstnet) > 2095 (u_short) ntohs(sat->sat_addr.s_net) 2096 || (u_short) ntohs(at_nr.nr_lastnet) < 2097 (u_short) ntohs(sat->sat_addr.s_net)) 2098 errx(1, "AppleTalk address is not in range"); 2099 *((struct netrange *) &sat->sat_zero) = at_nr; 2100} 2101 2102#define SNS(x) ((struct sockaddr_ns *) &(x)) 2103struct sockaddr_ns *snstab[] = { 2104SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr), 2105SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)}; 2106 2107void 2108xns_getaddr(addr, which) 2109 char *addr; 2110 int which; 2111{ 2112 struct sockaddr_ns *sns = snstab[which]; 2113 2114 sns->sns_family = AF_NS; 2115 sns->sns_len = sizeof(*sns); 2116 sns->sns_addr = ns_addr(addr); 2117 if (which == MASK) 2118 puts("Attempt to set XNS netmask will be ineffectual"); 2119} 2120 2121#define SISO(x) ((struct sockaddr_iso *) &(x)) 2122struct sockaddr_iso *sisotab[] = { 2123SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr), 2124SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)}; 2125 2126void 2127iso_getaddr(addr, which) 2128 char *addr; 2129 int which; 2130{ 2131 struct sockaddr_iso *siso = sisotab[which]; 2132 siso->siso_addr = *iso_addr(addr); 2133 2134 if (which == MASK) { 2135 siso->siso_len = TSEL(siso) - (caddr_t)(siso); 2136 siso->siso_nlen = 0; 2137 } else { 2138 siso->siso_len = sizeof(*siso); 2139 siso->siso_family = AF_ISO; 2140 } 2141} 2142 2143void 2144setsnpaoffset(val, d) 2145 char *val; 2146 int d; 2147{ 2148 iso_addreq.ifra_snpaoffset = atoi(val); 2149} 2150 2151void 2152setnsellength(val, d) 2153 char *val; 2154 int d; 2155{ 2156 nsellength = atoi(val); 2157 if (nsellength < 0) 2158 errx(1, "Negative NSEL length is absurd"); 2159 if (afp == 0 || afp->af_af != AF_ISO) 2160 errx(1, "Setting NSEL length valid only for iso"); 2161} 2162 2163void 2164fixnsel(s) 2165 struct sockaddr_iso *s; 2166{ 2167 if (s->siso_family == 0) 2168 return; 2169 s->siso_tlen = nsellength; 2170} 2171 2172void 2173adjust_nsellength() 2174{ 2175 fixnsel(sisotab[RIDADDR]); 2176 fixnsel(sisotab[ADDR]); 2177 fixnsel(sisotab[DSTADDR]); 2178} 2179 2180#endif /* INET_ONLY */ 2181 2182void 2183usage() 2184{ 2185 fprintf(stderr, 2186 "usage: ifconfig [ -m ] [ -A ] %s interface\n%s%s%s%s%s%s%s%s%s%s%s%s", 2187#ifdef INET6 2188 "[ -L ]", 2189#else 2190 "", 2191#endif 2192 "\t[ af [ address [ dest_addr ] ] [ up ] [ down ] ", 2193 "[ netmask mask ] ]\n", 2194 "\t[ metric n ]\n", 2195 "\t[ mtu n ]\n", 2196 "\t[ arp | -arp ]\n", 2197 "\t[ media mtype ]\n", 2198 "\t[ mediaopt mopts ]\n", 2199 "\t[ -mediaopt mopts ]\n", 2200 "\t[ instance minst ]\n", 2201 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n", 2202 " ifconfig -a [ -A ] [ -m ] [ -d ] [ -u ] [ af ]\n", 2203 " ifconfig -l [ -d ] [ -u ]\n"); 2204 exit(1); 2205} 2206 2207#ifdef INET6 2208char * 2209sec2str(total) 2210 time_t total; 2211{ 2212 static char result[256]; 2213 int days, hours, mins, secs; 2214 int first = 1; 2215 char *p = result; 2216 2217 if (0) { /*XXX*/ 2218 days = total / 3600 / 24; 2219 hours = (total / 3600) % 24; 2220 mins = (total / 60) % 60; 2221 secs = total % 60; 2222 2223 if (days) { 2224 first = 0; 2225 p += sprintf(p, "%dd", days); 2226 } 2227 if (!first || hours) { 2228 first = 0; 2229 p += sprintf(p, "%dh", hours); 2230 } 2231 if (!first || mins) { 2232 first = 0; 2233 p += sprintf(p, "%dm", mins); 2234 } 2235 sprintf(p, "%ds", secs); 2236 } else 2237 sprintf(p, "%lu", (u_long)total); 2238 2239 return(result); 2240} 2241#endif 2242