ifconfig.c revision 1.73
1/* $NetBSD: ifconfig.c,v 1.73 2000/03/06 09:00:13 enami 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.73 2000/03/06 09:00:13 enami 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 /* The following operations assume inet family as the 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], *cp; 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 for (i = 0; i < ifc.ifc_len; ) { 631 /* Copy the mininum ifreq into the buffer. */ 632 cp = ((caddr_t)ifc.ifc_req + i); 633 memcpy(ifrbuf, cp, sizeof(*ifr)); 634 635 /* Now compute the actual size of the ifreq. */ 636 ifr = (struct ifreq *)ifrbuf; 637 siz = ifr->ifr_addr.sa_len; 638 if (siz < sizeof(ifr->ifr_addr)) 639 siz = sizeof(ifr->ifr_addr); 640 siz += sizeof(ifr->ifr_name); 641 i += siz; 642 643 /* Now copy the whole thing. */ 644 if (sizeof(ifrbuf) < siz) 645 errx(1, "ifr too big"); 646 memcpy(ifrbuf, cp, siz); 647 648 if (!strncmp(iname, ifr->ifr_name, sizeof(ifr->ifr_name))) { 649 if (ifr->ifr_addr.sa_family == af) 650 switch (af) { 651 case AF_INET: 652 in_alias(ifr); 653 break; 654 default: 655 /*none*/ 656 } 657 } 658 } 659} 660 661void 662printall() 663{ 664 char inbuf[8192]; 665 const struct sockaddr_dl *sdl = NULL; 666 struct ifconf ifc; 667 struct ifreq ifreq, *ifr; 668 int i, siz, idx; 669 char ifrbuf[8192], *cp; 670 671 ifc.ifc_len = sizeof(inbuf); 672 ifc.ifc_buf = inbuf; 673 getsock(af); 674 if (s < 0) 675 err(1, "socket"); 676 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) 677 err(1, "SIOCGIFCONF"); 678 ifreq.ifr_name[0] = '\0'; 679 for (i = 0, idx = 0; i < ifc.ifc_len; ) { 680 /* Copy the mininum ifreq into the buffer. */ 681 cp = ((caddr_t)ifc.ifc_req + i); 682 memcpy(ifrbuf, cp, sizeof(*ifr)); 683 684 /* Now compute the actual size of the ifreq. */ 685 ifr = (struct ifreq *)ifrbuf; 686 siz = ifr->ifr_addr.sa_len; 687 if (siz < sizeof(ifr->ifr_addr)) 688 siz = sizeof(ifr->ifr_addr); 689 siz += sizeof(ifr->ifr_name); 690 i += siz; 691 692 /* Now copy the whole thing. */ 693 if (sizeof(ifrbuf) < siz) 694 errx(1, "ifr too big"); 695 memcpy(ifrbuf, cp, siz); 696 697 if (ifr->ifr_addr.sa_family == AF_LINK) 698 sdl = (const struct sockaddr_dl *) &ifr->ifr_addr; 699 if (!strncmp(ifreq.ifr_name, ifr->ifr_name, 700 sizeof(ifr->ifr_name))) { 701 if (Aflag && ifr->ifr_addr.sa_family == AF_INET) 702 in_alias(ifr); 703 continue; 704 } 705 (void) strncpy(name, ifr->ifr_name, sizeof(ifr->ifr_name)); 706 ifreq = *ifr; 707 708 if (getinfo(&ifreq) < 0) 709 continue; 710 if (bflag && (flags & (IFF_POINTOPOINT|IFF_LOOPBACK))) 711 continue; 712 if (dflag && (flags & IFF_UP) != 0) 713 continue; 714 if (uflag && (flags & IFF_UP) == 0) 715 continue; 716 717 if (sflag && carrier()) 718 continue; 719 idx++; 720 /* 721 * Are we just listing the interfaces? 722 */ 723 if (lflag) { 724 if (idx > 1) 725 putchar(' '); 726 fputs(name, stdout); 727 continue; 728 } 729 730 if (sdl == NULL) { 731 status(NULL, 0); 732 } else { 733 status(LLADDR(sdl), sdl->sdl_alen); 734 sdl = NULL; 735 } 736 } 737 if (lflag) 738 putchar('\n'); 739} 740 741#define RIDADDR 0 742#define ADDR 1 743#define MASK 2 744#define DSTADDR 3 745 746/*ARGSUSED*/ 747void 748setifaddr(addr, param) 749 char *addr; 750 int param; 751{ 752 /* 753 * Delay the ioctl to set the interface addr until flags are all set. 754 * The address interpretation may depend on the flags, 755 * and the flags may change when the address is set. 756 */ 757 setaddr++; 758 if (newaddr == -1) 759 newaddr = 1; 760 if (doalias == 0) 761 clearaddr = 1; 762 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR)); 763} 764 765void 766setifnetmask(addr, d) 767 char *addr; 768 int d; 769{ 770 (*afp->af_getaddr)(addr, MASK); 771} 772 773void 774setifbroadaddr(addr, d) 775 char *addr; 776 int d; 777{ 778 (*afp->af_getaddr)(addr, DSTADDR); 779} 780 781void 782setifipdst(addr, d) 783 char *addr; 784 int d; 785{ 786 in_getaddr(addr, DSTADDR); 787 setipdst++; 788 clearaddr = 0; 789 newaddr = 0; 790} 791 792#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) 793/*ARGSUSED*/ 794void 795notealias(addr, param) 796 char *addr; 797 int param; 798{ 799 if (setaddr && doalias == 0 && param < 0) 800 (void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq), 801 rqtosa(af_addreq)->sa_len); 802 doalias = param; 803 if (param < 0) { 804 clearaddr = 1; 805 newaddr = 0; 806 } else 807 clearaddr = 0; 808} 809 810/*ARGSUSED*/ 811void 812notrailers(vname, value) 813 char *vname; 814 int value; 815{ 816 puts("Note: trailers are no longer sent, but always received"); 817} 818 819/*ARGSUSED*/ 820void 821setifdstaddr(addr, param) 822 char *addr; 823 int param; 824{ 825 (*afp->af_getaddr)(addr, DSTADDR); 826} 827 828void 829setifflags(vname, value) 830 char *vname; 831 int value; 832{ 833 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) 834 err(1, "SIOCGIFFLAGS"); 835 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 836 flags = ifr.ifr_flags; 837 838 if (value < 0) { 839 value = -value; 840 flags &= ~value; 841 } else 842 flags |= value; 843 ifr.ifr_flags = flags; 844 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) 845 err(1, "SIOCSIFFLAGS"); 846 847 reset_if_flags = 1; 848} 849 850#ifdef INET6 851void 852setia6flags(vname, value) 853 char *vname; 854 int value; 855{ 856 if (value < 0) { 857 value = -value; 858 in6_addreq.ifra_flags &= ~value; 859 } else 860 in6_addreq.ifra_flags |= value; 861} 862 863void 864setia6pltime(val, d) 865 char *val; 866 int d; 867{ 868 setia6lifetime("pltime", val); 869} 870 871void 872setia6vltime(val, d) 873 char *val; 874 int d; 875{ 876 setia6lifetime("vltime", val); 877} 878 879void 880setia6lifetime(cmd, val) 881 char *cmd; 882 char *val; 883{ 884 time_t newval, t; 885 char *ep; 886 887 t = time(NULL); 888 newval = (time_t)strtoul(val, &ep, 0); 889 if (val == ep) 890 errx(1, "invalid %s", cmd); 891 if (afp->af_af != AF_INET6) 892 errx(1, "%s not allowed for the AF", cmd); 893 if (strcmp(cmd, "vltime") == 0) { 894 in6_addreq.ifra_lifetime.ia6t_expire = t + newval; 895 in6_addreq.ifra_lifetime.ia6t_vltime = newval; 896 } else if (strcmp(cmd, "pltime") == 0) { 897 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval; 898 in6_addreq.ifra_lifetime.ia6t_pltime = newval; 899 } 900} 901#endif 902 903void 904setifmetric(val, d) 905 char *val; 906 int d; 907{ 908 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 909 ifr.ifr_metric = atoi(val); 910 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) 911 warn("SIOCSIFMETRIC"); 912} 913 914void 915setifmtu(val, d) 916 char *val; 917 int d; 918{ 919 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 920 ifr.ifr_mtu = atoi(val); 921 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) 922 warn("SIOCSIFMTU"); 923} 924 925void 926setifnwid(val, d) 927 char *val; 928 int d; 929{ 930 u_int8_t nwid[IEEE80211_NWID_LEN]; 931 932 memset(&nwid, 0, sizeof(nwid)); 933 (void)strncpy(nwid, val, sizeof(nwid)); 934 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 935 ifr.ifr_data = (caddr_t)nwid; 936 if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0) 937 warn("SIOCS80211NWID"); 938} 939 940void 941ieee80211_status() 942{ 943 u_int8_t nwid[IEEE80211_NWID_LEN + 1]; 944 945 memset(&ifr, 0, sizeof(ifr)); 946 ifr.ifr_data = (caddr_t)nwid; 947 (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 948 nwid[IEEE80211_NWID_LEN] = 0; 949 if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) == 0) 950 printf("\tnwid %s\n", nwid); 951} 952 953void 954init_current_media() 955{ 956 struct ifmediareq ifmr; 957 958 /* 959 * If we have not yet done so, grab the currently-selected 960 * media. 961 */ 962 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) { 963 (void) memset(&ifmr, 0, sizeof(ifmr)); 964 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 965 966 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 967 /* 968 * If we get E2BIG, the kernel is telling us 969 * that there are more, so we can ignore it. 970 */ 971 if (errno != E2BIG) 972 err(1, "SGIOCGIFMEDIA"); 973 } 974 975 media_current = ifmr.ifm_current; 976 } 977 978 /* Sanity. */ 979 if (IFM_TYPE(media_current) == 0) 980 errx(1, "%s: no link type?", name); 981} 982 983void 984process_media_commands() 985{ 986 987 if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) { 988 /* Nothing to do. */ 989 return; 990 } 991 992 /* 993 * Media already set up, and commands sanity-checked. Set/clear 994 * any options, and we're ready to go. 995 */ 996 media_current |= mediaopt_set; 997 media_current &= ~mediaopt_clear; 998 999 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1000 ifr.ifr_media = media_current; 1001 1002 if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0) 1003 err(1, "SIOCSIFMEDIA"); 1004} 1005 1006void 1007setmedia(val, d) 1008 char *val; 1009 int d; 1010{ 1011 int type, subtype, inst; 1012 1013 init_current_media(); 1014 1015 /* Only one media command may be given. */ 1016 if (actions & A_MEDIA) 1017 errx(1, "only one `media' command may be issued"); 1018 1019 /* Must not come after mediaopt commands */ 1020 if (actions & A_MEDIAOPT) 1021 errx(1, "may not issue `media' after `mediaopt' commands"); 1022 1023 /* 1024 * No need to check if `instance' has been issued; setmediainst() 1025 * craps out if `media' has not been specified. 1026 */ 1027 1028 type = IFM_TYPE(media_current); 1029 inst = IFM_INST(media_current); 1030 1031 /* Look up the subtype. */ 1032 subtype = get_media_subtype(type, val); 1033 1034 /* Build the new current media word. */ 1035 media_current = IFM_MAKEWORD(type, subtype, 0, inst); 1036 1037 /* Media will be set after other processing is complete. */ 1038} 1039 1040void 1041setmediaopt(val, d) 1042 char *val; 1043 int d; 1044{ 1045 1046 init_current_media(); 1047 1048 /* Can only issue `mediaopt' once. */ 1049 if (actions & A_MEDIAOPTSET) 1050 errx(1, "only one `mediaopt' command may be issued"); 1051 1052 /* Can't issue `mediaopt' if `instance' has already been issued. */ 1053 if (actions & A_MEDIAINST) 1054 errx(1, "may not issue `mediaopt' after `instance'"); 1055 1056 mediaopt_set = get_media_options(IFM_TYPE(media_current), val); 1057 1058 /* Media will be set after other processing is complete. */ 1059} 1060 1061void 1062unsetmediaopt(val, d) 1063 char *val; 1064 int d; 1065{ 1066 1067 init_current_media(); 1068 1069 /* Can only issue `-mediaopt' once. */ 1070 if (actions & A_MEDIAOPTCLR) 1071 errx(1, "only one `-mediaopt' command may be issued"); 1072 1073 /* May not issue `media' and `-mediaopt'. */ 1074 if (actions & A_MEDIA) 1075 errx(1, "may not issue both `media' and `-mediaopt'"); 1076 1077 /* 1078 * No need to check for A_MEDIAINST, since the test for A_MEDIA 1079 * implicitly checks for A_MEDIAINST. 1080 */ 1081 1082 mediaopt_clear = get_media_options(IFM_TYPE(media_current), val); 1083 1084 /* Media will be set after other processing is complete. */ 1085} 1086 1087void 1088setmediainst(val, d) 1089 char *val; 1090 int d; 1091{ 1092 int type, subtype, options, inst; 1093 1094 init_current_media(); 1095 1096 /* Can only issue `instance' once. */ 1097 if (actions & A_MEDIAINST) 1098 errx(1, "only one `instance' command may be issued"); 1099 1100 /* Must have already specified `media' */ 1101 if ((actions & A_MEDIA) == 0) 1102 errx(1, "must specify `media' before `instance'"); 1103 1104 type = IFM_TYPE(media_current); 1105 subtype = IFM_SUBTYPE(media_current); 1106 options = IFM_OPTIONS(media_current); 1107 1108 inst = atoi(val); 1109 if (inst < 0 || inst > IFM_INST_MAX) 1110 errx(1, "invalid media instance: %s", val); 1111 1112 media_current = IFM_MAKEWORD(type, subtype, options, inst); 1113 1114 /* Media will be set after other processing is complete. */ 1115} 1116 1117struct ifmedia_description ifm_type_descriptions[] = 1118 IFM_TYPE_DESCRIPTIONS; 1119 1120struct ifmedia_description ifm_subtype_descriptions[] = 1121 IFM_SUBTYPE_DESCRIPTIONS; 1122 1123struct ifmedia_description ifm_option_descriptions[] = 1124 IFM_OPTION_DESCRIPTIONS; 1125 1126const char * 1127get_media_type_string(mword) 1128 int mword; 1129{ 1130 struct ifmedia_description *desc; 1131 1132 for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; 1133 desc++) { 1134 if (IFM_TYPE(mword) == desc->ifmt_word) 1135 return (desc->ifmt_string); 1136 } 1137 return ("<unknown type>"); 1138} 1139 1140const char * 1141get_media_subtype_string(mword) 1142 int mword; 1143{ 1144 struct ifmedia_description *desc; 1145 1146 for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL; 1147 desc++) { 1148 if (IFM_TYPE_MATCH(desc->ifmt_word, mword) && 1149 IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword)) 1150 return (desc->ifmt_string); 1151 } 1152 return ("<unknown subtype>"); 1153} 1154 1155int 1156get_media_subtype(type, val) 1157 int type; 1158 const char *val; 1159{ 1160 int rval; 1161 1162 rval = lookup_media_word(ifm_subtype_descriptions, type, val); 1163 if (rval == -1) 1164 errx(1, "unknown %s media subtype: %s", 1165 get_media_type_string(type), val); 1166 1167 return (rval); 1168} 1169 1170int 1171get_media_options(type, val) 1172 int type; 1173 const char *val; 1174{ 1175 char *optlist, *str; 1176 int option, rval = 0; 1177 1178 /* We muck with the string, so copy it. */ 1179 optlist = strdup(val); 1180 if (optlist == NULL) 1181 err(1, "strdup"); 1182 str = optlist; 1183 1184 /* 1185 * Look up the options in the user-provided comma-separated list. 1186 */ 1187 for (; (str = strtok(str, ",")) != NULL; str = NULL) { 1188 option = lookup_media_word(ifm_option_descriptions, type, str); 1189 if (option == -1) 1190 errx(1, "unknown %s media option: %s", 1191 get_media_type_string(type), str); 1192 rval |= IFM_OPTIONS(option); 1193 } 1194 1195 free(optlist); 1196 return (rval); 1197} 1198 1199int 1200lookup_media_word(desc, type, val) 1201 struct ifmedia_description *desc; 1202 int type; 1203 const char *val; 1204{ 1205 1206 for (; desc->ifmt_string != NULL; desc++) { 1207 if (IFM_TYPE_MATCH(desc->ifmt_word, type) && 1208 strcasecmp(desc->ifmt_string, val) == 0) 1209 return (desc->ifmt_word); 1210 } 1211 return (-1); 1212} 1213 1214void 1215print_media_word(ifmw, print_type, as_syntax) 1216 int ifmw, print_type, as_syntax; 1217{ 1218 struct ifmedia_description *desc; 1219 int seen_option = 0; 1220 1221 if (print_type) 1222 printf("%s ", get_media_type_string(ifmw)); 1223 printf("%s%s", as_syntax ? "media " : "", 1224 get_media_subtype_string(ifmw)); 1225 1226 /* Find options. */ 1227 for (desc = ifm_option_descriptions; desc->ifmt_string != NULL; 1228 desc++) { 1229 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) && 1230 (ifmw & IFM_OPTIONS(desc->ifmt_word)) != 0 && 1231 (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) { 1232 if (seen_option == 0) 1233 printf(" %s", as_syntax ? "mediaopt " : ""); 1234 printf("%s%s", seen_option ? "," : "", 1235 desc->ifmt_string); 1236 seen_option |= IFM_OPTIONS(desc->ifmt_word); 1237 } 1238 } 1239 if (IFM_INST(ifmw) != 0) 1240 printf(" instance %d", IFM_INST(ifmw)); 1241} 1242 1243int carrier() 1244{ 1245 struct ifmediareq ifmr; 1246 1247 (void) memset(&ifmr, 0, sizeof(ifmr)); 1248 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 1249 1250 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 1251 /* 1252 * Interface doesn't support SIOC{G,S}IFMEDIA; 1253 * assume ok. 1254 */ 1255 return 0; 1256 } 1257 if ((ifmr.ifm_status & IFM_AVALID) == 0) { 1258 /* 1259 * Interface doesn't report media-valid status. 1260 * assume ok. 1261 */ 1262 return 0; 1263 } 1264 /* otherwise, return ok for active, not-ok if not active. */ 1265 return !(ifmr.ifm_status & IFM_ACTIVE); 1266} 1267 1268 1269#define IFFBITS \ 1270"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\ 1271\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST" 1272 1273const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST; 1274 1275const struct ifmedia_status_description ifm_status_descriptions[] = 1276 IFM_STATUS_DESCRIPTIONS; 1277 1278/* 1279 * Print the status of the interface. If an address family was 1280 * specified, show it and it only; otherwise, show them all. 1281 */ 1282void 1283status(ap, alen) 1284 const u_int8_t *ap; 1285 int alen; 1286{ 1287 struct afswtch *p = afp; 1288 struct ifmediareq ifmr; 1289 int *media_list, i; 1290 1291 printf("%s: ", name); 1292 printb("flags", flags, IFFBITS); 1293 if (metric) 1294 printf(" metric %d", metric); 1295 if (mtu) 1296 printf(" mtu %d", mtu); 1297 putchar('\n'); 1298 1299 ieee80211_status(); 1300 1301 if (ap && alen > 0) { 1302 printf("\taddress:"); 1303 for (i = 0; i < alen; i++, ap++) 1304 printf("%c%02x", i > 0 ? ':' : ' ', *ap); 1305 putchar('\n'); 1306 } 1307 1308 (void) memset(&ifmr, 0, sizeof(ifmr)); 1309 (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name)); 1310 1311 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 1312 /* 1313 * Interface doesn't support SIOC{G,S}IFMEDIA. 1314 */ 1315 goto proto_status; 1316 } 1317 1318 if (ifmr.ifm_count == 0) { 1319 warnx("%s: no media types?", name); 1320 goto proto_status; 1321 } 1322 1323 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int)); 1324 if (media_list == NULL) 1325 err(1, "malloc"); 1326 ifmr.ifm_ulist = media_list; 1327 1328 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) 1329 err(1, "SIOCGIFMEDIA"); 1330 1331 printf("\tmedia: "); 1332 print_media_word(ifmr.ifm_current, 1, 0); 1333 if (ifmr.ifm_active != ifmr.ifm_current) { 1334 putchar(' '); 1335 putchar('('); 1336 print_media_word(ifmr.ifm_active, 0, 0); 1337 putchar(')'); 1338 } 1339 putchar('\n'); 1340 1341 if (ifmr.ifm_status & IFM_STATUS_VALID) { 1342 const struct ifmedia_status_description *ifms; 1343 int bitno, found = 0; 1344 1345 printf("\tstatus: "); 1346 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) { 1347 for (ifms = ifm_status_descriptions; 1348 ifms->ifms_valid != 0; ifms++) { 1349 if (ifms->ifms_type != 1350 IFM_TYPE(ifmr.ifm_current) || 1351 ifms->ifms_valid != 1352 ifm_status_valid_list[bitno]) 1353 continue; 1354 printf("%s%s", found ? ", " : "", 1355 IFM_STATUS_DESC(ifms, ifmr.ifm_status)); 1356 found = 1; 1357 1358 /* 1359 * For each valid indicator bit, there's 1360 * only one entry for each media type, so 1361 * terminate the inner loop now. 1362 */ 1363 break; 1364 } 1365 } 1366 1367 if (found == 0) 1368 printf("unknown"); 1369 putchar('\n'); 1370 } 1371 1372 if (mflag) { 1373 int type, printed_type; 1374 1375 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) { 1376 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) { 1377 if (IFM_TYPE(media_list[i]) == type) { 1378 if (printed_type == 0) { 1379 printf("\tsupported %s media:\n", 1380 get_media_type_string(type)); 1381 printed_type = 1; 1382 } 1383 printf("\t\t"); 1384 print_media_word(media_list[i], 0, 1); 1385 printf("\n"); 1386 } 1387 } 1388 } 1389 } 1390 1391 free(media_list); 1392 1393 proto_status: 1394 if ((p = afp) != NULL) { 1395 (*p->af_status)(1); 1396 if (Aflag & !aflag) 1397 printalias(name, p->af_af); 1398 } else for (p = afs; p->af_name; p++) { 1399 ifr.ifr_addr.sa_family = p->af_af; 1400 (*p->af_status)(0); 1401 if (Aflag & !aflag && p->af_af == AF_INET) 1402 printalias(name, p->af_af); 1403 } 1404} 1405 1406void 1407in_alias(creq) 1408 struct ifreq *creq; 1409{ 1410 struct sockaddr_in *sin; 1411 1412 if (lflag) 1413 return; 1414 1415 /* Get the non-alias address for this interface. */ 1416 getsock(AF_INET); 1417 if (s < 0) { 1418 if (errno == EPROTONOSUPPORT) 1419 return; 1420 err(1, "socket"); 1421 } 1422 (void) memset(&ifr, 0, sizeof(ifr)); 1423 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1424 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1425 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1426 return; 1427 } else 1428 warn("SIOCGIFADDR"); 1429 } 1430 /* If creq and ifr are the same address, this is not an alias. */ 1431 if (memcmp(&ifr.ifr_addr, &creq->ifr_addr, 1432 sizeof(creq->ifr_addr)) == 0) 1433 return; 1434 (void) memset(&addreq, 0, sizeof(addreq)); 1435 (void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name)); 1436 addreq.ifra_addr = creq->ifr_addr; 1437 if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) { 1438 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1439 return; 1440 } else 1441 warn("SIOCGIFALIAS"); 1442 } 1443 1444 sin = (struct sockaddr_in *)&addreq.ifra_addr; 1445 printf("\tinet alias %s", inet_ntoa(sin->sin_addr)); 1446 1447 if (flags & IFF_POINTOPOINT) { 1448 sin = (struct sockaddr_in *)&addreq.ifra_dstaddr; 1449 printf(" -> %s", inet_ntoa(sin->sin_addr)); 1450 } 1451 1452 sin = (struct sockaddr_in *)&addreq.ifra_mask; 1453 printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr)); 1454 1455 if (flags & IFF_BROADCAST) { 1456 sin = (struct sockaddr_in *)&addreq.ifra_broadaddr; 1457 printf(" broadcast %s", inet_ntoa(sin->sin_addr)); 1458 } 1459 printf("\n"); 1460} 1461 1462void 1463in_status(force) 1464 int force; 1465{ 1466 struct sockaddr_in *sin; 1467 1468 getsock(AF_INET); 1469 if (s < 0) { 1470 if (errno == EPROTONOSUPPORT) 1471 return; 1472 err(1, "socket"); 1473 } 1474 (void) memset(&ifr, 0, sizeof(ifr)); 1475 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1476 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1477 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1478 if (!force) 1479 return; 1480 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1481 } else 1482 warn("SIOCGIFADDR"); 1483 } 1484 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1485 sin = (struct sockaddr_in *)&ifr.ifr_addr; 1486 printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 1487 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1488 if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) { 1489 if (errno != EADDRNOTAVAIL) 1490 warn("SIOCGIFNETMASK"); 1491 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1492 } else 1493 netmask.sin_addr = 1494 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; 1495 if (flags & IFF_POINTOPOINT) { 1496 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) { 1497 if (errno == EADDRNOTAVAIL) 1498 (void) memset(&ifr.ifr_addr, 0, 1499 sizeof(ifr.ifr_addr)); 1500 else 1501 warn("SIOCGIFDSTADDR"); 1502 } 1503 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1504 sin = (struct sockaddr_in *)&ifr.ifr_dstaddr; 1505 printf("--> %s ", inet_ntoa(sin->sin_addr)); 1506 } 1507 printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr)); 1508 if (flags & IFF_BROADCAST) { 1509 if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) { 1510 if (errno == EADDRNOTAVAIL) 1511 (void) memset(&ifr.ifr_addr, 0, 1512 sizeof(ifr.ifr_addr)); 1513 else 1514 warn("SIOCGIFBRDADDR"); 1515 } 1516 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1517 sin = (struct sockaddr_in *)&ifr.ifr_addr; 1518 if (sin->sin_addr.s_addr != 0) 1519 printf("broadcast %s", inet_ntoa(sin->sin_addr)); 1520 } 1521 putchar('\n'); 1522} 1523 1524void 1525setifprefixlen(addr, d) 1526 char *addr; 1527 int d; 1528{ 1529 if (*afp->af_getprefix) 1530 (*afp->af_getprefix)(addr, MASK); 1531 explicit_prefix = 1; 1532} 1533 1534#ifdef INET6 1535void 1536in6_fillscopeid(sin6) 1537 struct sockaddr_in6 *sin6; 1538{ 1539#if defined(__KAME__) && defined(KAME_SCOPEID) 1540 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 1541 sin6->sin6_scope_id = 1542 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]); 1543 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0; 1544 } 1545#endif 1546} 1547 1548/* XXX not really an alias */ 1549void 1550in6_alias(creq) 1551 struct in6_ifreq *creq; 1552{ 1553 struct sockaddr_in6 *sin6; 1554 char hbuf[NI_MAXHOST]; 1555 u_int32_t scopeid; 1556#ifdef NI_WITHSCOPEID 1557 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 1558#else 1559 const int niflag = NI_NUMERICHOST; 1560#endif 1561 1562 /* Get the non-alias address for this interface. */ 1563 getsock(AF_INET6); 1564 if (s < 0) { 1565 if (errno == EPROTONOSUPPORT) 1566 return; 1567 err(1, "socket"); 1568 } 1569 1570 sin6 = (struct sockaddr_in6 *)&creq->ifr_addr; 1571 1572 in6_fillscopeid(sin6); 1573 scopeid = sin6->sin6_scope_id; 1574 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, 1575 hbuf, sizeof(hbuf), NULL, 0, niflag)) 1576 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */ 1577 printf("\tinet6 %s", hbuf); 1578 1579 if (flags & IFF_POINTOPOINT) { 1580 (void) memset(&ifr6, 0, sizeof(ifr6)); 1581 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1582 ifr6.ifr_addr = creq->ifr_addr; 1583 if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) { 1584 if (errno != EADDRNOTAVAIL) 1585 warn("SIOCGIFDSTADDR_IN6"); 1586 (void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr)); 1587 ifr6.ifr_addr.sin6_family = AF_INET6; 1588 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6); 1589 } 1590 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr; 1591 in6_fillscopeid(sin6); 1592 hbuf[0] = '\0'; 1593 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, 1594 hbuf, sizeof(hbuf), NULL, 0, niflag)) 1595 strncpy(hbuf, "", sizeof(hbuf)); /* some message? */ 1596 printf(" -> %s", hbuf); 1597 } 1598 1599 (void) memset(&ifr6, 0, sizeof(ifr6)); 1600 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1601 ifr6.ifr_addr = creq->ifr_addr; 1602 if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) { 1603 if (errno != EADDRNOTAVAIL) 1604 warn("SIOCGIFNETMASK_IN6"); 1605 } else { 1606 sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr; 1607 printf(" prefixlen %d", prefix(&sin6->sin6_addr, 1608 sizeof(struct in6_addr))); 1609 } 1610 1611 (void) memset(&ifr6, 0, sizeof(ifr6)); 1612 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1613 ifr6.ifr_addr = creq->ifr_addr; 1614 if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) { 1615 if (errno != EADDRNOTAVAIL) 1616 warn("SIOCGIFAFLAG_IN6"); 1617 } else { 1618 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST) 1619 printf(" anycast"); 1620 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE) 1621 printf(" tentative"); 1622 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED) 1623 printf(" duplicated"); 1624 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED) 1625 printf(" detached"); 1626 } 1627 1628 if (scopeid) 1629 printf(" scopeid 0x%x", scopeid); 1630 1631 if (Lflag) { 1632 struct in6_addrlifetime *lifetime; 1633 (void) memset(&ifr6, 0, sizeof(ifr6)); 1634 (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); 1635 ifr6.ifr_addr = creq->ifr_addr; 1636 lifetime = &ifr6.ifr_ifru.ifru_lifetime; 1637 if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) { 1638 if (errno != EADDRNOTAVAIL) 1639 warn("SIOCGIFALIFETIME_IN6"); 1640 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) { 1641 time_t t = time(NULL); 1642 printf(" pltime "); 1643 if (lifetime->ia6t_preferred) { 1644 printf("%s", lifetime->ia6t_preferred < t 1645 ? "0" 1646 : sec2str(lifetime->ia6t_preferred - t)); 1647 } else 1648 printf("infty"); 1649 1650 printf(" vltime "); 1651 if (lifetime->ia6t_expire) { 1652 printf("%s", lifetime->ia6t_expire < t 1653 ? "0" 1654 : sec2str(lifetime->ia6t_expire - t)); 1655 } else 1656 printf("infty"); 1657 } 1658 } 1659 1660 printf("\n"); 1661} 1662 1663void 1664in6_status(force) 1665 int force; 1666{ 1667 char inbuf[8192]; 1668 struct ifconf ifc; 1669 struct ifreq *ifr; 1670 int i, siz; 1671 char ifrbuf[8192], *cp; 1672 1673 ifc.ifc_len = sizeof(inbuf); 1674 ifc.ifc_buf = inbuf; 1675 getsock(af); 1676 if (s < 0) 1677 err(1, "socket"); 1678 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) 1679 err(1, "SIOCGIFCONF"); 1680 for (i = 0; i < ifc.ifc_len; ) { 1681 /* Copy the mininum ifreq into the buffer. */ 1682 cp = ((caddr_t)ifc.ifc_req + i); 1683 memcpy(ifrbuf, cp, sizeof(*ifr)); 1684 1685 /* Now compute the actual size of the ifreq. */ 1686 ifr = (struct ifreq *)ifrbuf; 1687 siz = ifr->ifr_addr.sa_len; 1688 if (siz < sizeof(ifr->ifr_addr)) 1689 siz = sizeof(ifr->ifr_addr); 1690 siz += sizeof(ifr->ifr_name); 1691 i += siz; 1692 1693 /* Now copy the whole thing. */ 1694 if (sizeof(ifrbuf) < siz) 1695 errx(1, "ifr too big"); 1696 memcpy(ifrbuf, cp, siz); 1697 1698 if (!strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))) { 1699 if (ifr->ifr_addr.sa_family == AF_INET6) 1700 in6_alias((struct in6_ifreq *)ifr); 1701 } 1702 } 1703} 1704#endif /*INET6*/ 1705 1706#ifndef INET_ONLY 1707 1708void 1709at_status(force) 1710 int force; 1711{ 1712 struct sockaddr_at *sat, null_sat; 1713 struct netrange *nr; 1714 1715 getsock(AF_APPLETALK); 1716 if (s < 0) { 1717 if (errno == EPROTONOSUPPORT) 1718 return; 1719 err(1, "socket"); 1720 } 1721 (void) memset(&ifr, 0, sizeof(ifr)); 1722 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1723 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1724 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1725 if (!force) 1726 return; 1727 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1728 } else 1729 warn("SIOCGIFADDR"); 1730 } 1731 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1732 sat = (struct sockaddr_at *)&ifr.ifr_addr; 1733 1734 (void) memset(&null_sat, 0, sizeof(null_sat)); 1735 1736 nr = (struct netrange *) &sat->sat_zero; 1737 printf("\tatalk %d.%d range %d-%d phase %d", 1738 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node, 1739 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase); 1740 if (flags & IFF_POINTOPOINT) { 1741 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) { 1742 if (errno == EADDRNOTAVAIL) 1743 (void) memset(&ifr.ifr_addr, 0, 1744 sizeof(ifr.ifr_addr)); 1745 else 1746 warn("SIOCGIFDSTADDR"); 1747 } 1748 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1749 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr; 1750 if (!sat) 1751 sat = &null_sat; 1752 printf("--> %d.%d", 1753 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node); 1754 } 1755 if (flags & IFF_BROADCAST) { 1756 /* note RTAX_BRD overlap with IFF_POINTOPOINT */ 1757 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr; 1758 if (sat) 1759 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net), 1760 sat->sat_addr.s_node); 1761 } 1762 putchar('\n'); 1763} 1764 1765void 1766xns_status(force) 1767 int force; 1768{ 1769 struct sockaddr_ns *sns; 1770 1771 getsock(AF_NS); 1772 if (s < 0) { 1773 if (errno == EPROTONOSUPPORT) 1774 return; 1775 err(1, "socket"); 1776 } 1777 (void) memset(&ifr, 0, sizeof(ifr)); 1778 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1779 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) { 1780 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1781 if (!force) 1782 return; 1783 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1784 } else 1785 warn("SIOCGIFADDR"); 1786 } 1787 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1788 sns = (struct sockaddr_ns *)&ifr.ifr_addr; 1789 printf("\tns %s ", ns_ntoa(sns->sns_addr)); 1790 if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */ 1791 if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) { 1792 if (errno == EADDRNOTAVAIL) 1793 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1794 else 1795 warn("SIOCGIFDSTADDR"); 1796 } 1797 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1798 sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr; 1799 printf("--> %s ", ns_ntoa(sns->sns_addr)); 1800 } 1801 putchar('\n'); 1802} 1803 1804void 1805iso_status(force) 1806 int force; 1807{ 1808 struct sockaddr_iso *siso; 1809 struct iso_ifreq ifr; 1810 1811 getsock(AF_ISO); 1812 if (s < 0) { 1813 if (errno == EPROTONOSUPPORT) 1814 return; 1815 err(1, "socket"); 1816 } 1817 (void) memset(&ifr, 0, sizeof(ifr)); 1818 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 1819 if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) { 1820 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) { 1821 if (!force) 1822 return; 1823 (void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr)); 1824 } else 1825 warn("SIOCGIFADDR_ISO"); 1826 } 1827 (void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); 1828 siso = &ifr.ifr_Addr; 1829 printf("\tiso %s ", iso_ntoa(&siso->siso_addr)); 1830 if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) { 1831 if (errno == EADDRNOTAVAIL) 1832 memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr)); 1833 else 1834 warn("SIOCGIFNETMASK_ISO"); 1835 } else { 1836 if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr)) 1837 siso->siso_addr.isoa_len = siso->siso_len 1838 - offsetof(struct sockaddr_iso, siso_addr); 1839 printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr)); 1840 } 1841 if (flags & IFF_POINTOPOINT) { 1842 if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) { 1843 if (errno == EADDRNOTAVAIL) 1844 memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr)); 1845 else 1846 warn("SIOCGIFDSTADDR_ISO"); 1847 } 1848 (void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); 1849 siso = &ifr.ifr_Addr; 1850 printf("--> %s ", iso_ntoa(&siso->siso_addr)); 1851 } 1852 putchar('\n'); 1853} 1854 1855#endif /* INET_ONLY */ 1856 1857#define SIN(x) ((struct sockaddr_in *) &(x)) 1858struct sockaddr_in *sintab[] = { 1859SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr), 1860SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)}; 1861 1862void 1863in_getaddr(s, which) 1864 char *s; 1865 int which; 1866{ 1867 struct sockaddr_in *sin = sintab[which]; 1868 struct hostent *hp; 1869 struct netent *np; 1870 1871 sin->sin_len = sizeof(*sin); 1872 if (which != MASK) 1873 sin->sin_family = AF_INET; 1874 1875 if (which == ADDR) { 1876 char *p = NULL; 1877 1878 if((p = strrchr(s, '/')) != NULL) { 1879 /* address is `name/masklen' */ 1880 int masklen; 1881 int ret; 1882 struct sockaddr_in *min = sintab[MASK]; 1883 *p = '\0'; 1884 ret = sscanf(p+1, "%u", &masklen); 1885 if(ret != 1 || (masklen < 0 || masklen > 32)) { 1886 *p = '/'; 1887 errx(1, "%s: bad value", s); 1888 } 1889 min->sin_len = sizeof(*min); 1890 min->sin_addr.s_addr = 1891 htonl(~((1LL << (32 - masklen)) - 1) & 1892 0xffffffff); 1893 } 1894 } 1895 1896 if (inet_aton(s, &sin->sin_addr) == 0) { 1897 if ((hp = gethostbyname(s)) != NULL) 1898 (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length); 1899 else if ((np = getnetbyname(s)) != NULL) 1900 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 1901 else 1902 errx(1, "%s: bad value", s); 1903 } 1904} 1905 1906/* 1907 * Print a value a la the %b format of the kernel's printf 1908 */ 1909void 1910printb(s, v, bits) 1911 char *s; 1912 char *bits; 1913 unsigned short v; 1914{ 1915 int i, any = 0; 1916 char c; 1917 1918 if (bits && *bits == 8) 1919 printf("%s=%o", s, v); 1920 else 1921 printf("%s=%x", s, v); 1922 bits++; 1923 if (bits) { 1924 putchar('<'); 1925 while ((i = *bits++) != 0) { 1926 if (v & (1 << (i-1))) { 1927 if (any) 1928 putchar(','); 1929 any = 1; 1930 for (; (c = *bits) > 32; bits++) 1931 putchar(c); 1932 } else 1933 for (; *bits > 32; bits++) 1934 ; 1935 } 1936 putchar('>'); 1937 } 1938} 1939 1940#ifdef INET6 1941#define SIN6(x) ((struct sockaddr_in6 *) &(x)) 1942struct sockaddr_in6 *sin6tab[] = { 1943SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), 1944SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)}; 1945 1946void 1947in6_getaddr(s, which) 1948 char *s; 1949 int which; 1950{ 1951#if defined(__KAME__) && defined(KAME_SCOPEID) 1952 struct sockaddr_in6 *sin6 = sin6tab[which]; 1953 struct addrinfo hints, *res; 1954 int error; 1955 1956 memset(&hints, 0, sizeof(hints)); 1957 hints.ai_family = AF_INET6; 1958 hints.ai_socktype = SOCK_DGRAM; 1959#if 0 /* in_getaddr() allows FQDN */ 1960 hints.ai_flags = AI_NUMERICHOST; 1961#endif 1962 error = getaddrinfo(s, "0", &hints, &res); 1963 if (error) 1964 errx(1, "%s: %s", s, gai_strerror(error)); 1965 if (res->ai_next) 1966 errx(1, "%s: resolved to multiple hosts", s); 1967 if (res->ai_addrlen != sizeof(struct sockaddr_in6)) 1968 errx(1, "%s: bad value", s); 1969 memcpy(sin6, res->ai_addr, res->ai_addrlen); 1970 freeaddrinfo(res); 1971 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) { 1972 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 1973 htons(sin6->sin6_scope_id); 1974 sin6->sin6_scope_id = 0; 1975 } 1976#else 1977 struct sockaddr_in6 *sin = sin6tab[which]; 1978 1979 sin->sin6_len = sizeof(*sin); 1980 if (which != MASK) 1981 sin->sin6_family = AF_INET6; 1982 1983 if (which == ADDR) { 1984 char *p = NULL; 1985 if((p = strrchr(s, '/')) != NULL) { 1986 *p = '\0'; 1987 in6_getprefix(p + 1, MASK); 1988 explicit_prefix = 1; 1989 } 1990 } 1991 1992 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 1993 errx(1, "%s: bad value", s); 1994#endif 1995} 1996 1997void 1998in6_getprefix(plen, which) 1999 char *plen; 2000 int which; 2001{ 2002 register struct sockaddr_in6 *sin = sin6tab[which]; 2003 register u_char *cp; 2004 int len = strtol(plen, (char **)NULL, 10); 2005 2006 if ((len < 0) || (len > 128)) 2007 errx(1, "%s: bad value", plen); 2008 sin->sin6_len = sizeof(*sin); 2009 if (which != MASK) 2010 sin->sin6_family = AF_INET6; 2011 if ((len == 0) || (len == 128)) { 2012 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); 2013 return; 2014 } 2015 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); 2016 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 2017 *cp++ = 0xff; 2018 *cp = 0xff << (8 - len); 2019} 2020 2021int 2022prefix(val, size) 2023 void *val; 2024 int size; 2025{ 2026 register u_char *name = (u_char *)val; 2027 register int byte, bit, plen = 0; 2028 2029 for (byte = 0; byte < size; byte++, plen += 8) 2030 if (name[byte] != 0xff) 2031 break; 2032 if (byte == size) 2033 return (plen); 2034 for (bit = 7; bit != 0; bit--, plen++) 2035 if (!(name[byte] & (1 << bit))) 2036 break; 2037 for (; bit != 0; bit--) 2038 if (name[byte] & (1 << bit)) 2039 return(0); 2040 byte++; 2041 for (; byte < size; byte++) 2042 if (name[byte]) 2043 return(0); 2044 return (plen); 2045} 2046#endif /*INET6*/ 2047 2048#ifndef INET_ONLY 2049void 2050at_getaddr(addr, which) 2051 char *addr; 2052 int which; 2053{ 2054 struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr; 2055 u_int net, node; 2056 2057 sat->sat_family = AF_APPLETALK; 2058 sat->sat_len = sizeof(*sat); 2059 if (which == MASK) 2060 errx(1, "AppleTalk does not use netmasks\n"); 2061 if (sscanf(addr, "%u.%u", &net, &node) != 2 2062 || net == 0 || net > 0xffff || node == 0 || node > 0xfe) 2063 errx(1, "%s: illegal address", addr); 2064 sat->sat_addr.s_net = htons(net); 2065 sat->sat_addr.s_node = node; 2066} 2067 2068void 2069setatrange(range, d) 2070 char *range; 2071 int d; 2072{ 2073 u_short first = 123, last = 123; 2074 2075 if (sscanf(range, "%hu-%hu", &first, &last) != 2 2076 || first == 0 || first > 0xffff 2077 || last == 0 || last > 0xffff || first > last) 2078 errx(1, "%s: illegal net range: %u-%u", range, first, last); 2079 at_nr.nr_firstnet = htons(first); 2080 at_nr.nr_lastnet = htons(last); 2081} 2082 2083void 2084setatphase(phase, d) 2085 char *phase; 2086 int d; 2087{ 2088 if (!strcmp(phase, "1")) 2089 at_nr.nr_phase = 1; 2090 else if (!strcmp(phase, "2")) 2091 at_nr.nr_phase = 2; 2092 else 2093 errx(1, "%s: illegal phase", phase); 2094} 2095 2096void 2097checkatrange(sat) 2098 struct sockaddr_at *sat; 2099{ 2100 if (at_nr.nr_phase == 0) 2101 at_nr.nr_phase = 2; /* Default phase 2 */ 2102 if (at_nr.nr_firstnet == 0) 2103 at_nr.nr_firstnet = /* Default range of one */ 2104 at_nr.nr_lastnet = sat->sat_addr.s_net; 2105 printf("\tatalk %d.%d range %d-%d phase %d\n", 2106 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node, 2107 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase); 2108 if ((u_short) ntohs(at_nr.nr_firstnet) > 2109 (u_short) ntohs(sat->sat_addr.s_net) 2110 || (u_short) ntohs(at_nr.nr_lastnet) < 2111 (u_short) ntohs(sat->sat_addr.s_net)) 2112 errx(1, "AppleTalk address is not in range"); 2113 *((struct netrange *) &sat->sat_zero) = at_nr; 2114} 2115 2116#define SNS(x) ((struct sockaddr_ns *) &(x)) 2117struct sockaddr_ns *snstab[] = { 2118SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr), 2119SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)}; 2120 2121void 2122xns_getaddr(addr, which) 2123 char *addr; 2124 int which; 2125{ 2126 struct sockaddr_ns *sns = snstab[which]; 2127 2128 sns->sns_family = AF_NS; 2129 sns->sns_len = sizeof(*sns); 2130 sns->sns_addr = ns_addr(addr); 2131 if (which == MASK) 2132 puts("Attempt to set XNS netmask will be ineffectual"); 2133} 2134 2135#define SISO(x) ((struct sockaddr_iso *) &(x)) 2136struct sockaddr_iso *sisotab[] = { 2137SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr), 2138SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)}; 2139 2140void 2141iso_getaddr(addr, which) 2142 char *addr; 2143 int which; 2144{ 2145 struct sockaddr_iso *siso = sisotab[which]; 2146 siso->siso_addr = *iso_addr(addr); 2147 2148 if (which == MASK) { 2149 siso->siso_len = TSEL(siso) - (caddr_t)(siso); 2150 siso->siso_nlen = 0; 2151 } else { 2152 siso->siso_len = sizeof(*siso); 2153 siso->siso_family = AF_ISO; 2154 } 2155} 2156 2157void 2158setsnpaoffset(val, d) 2159 char *val; 2160 int d; 2161{ 2162 iso_addreq.ifra_snpaoffset = atoi(val); 2163} 2164 2165void 2166setnsellength(val, d) 2167 char *val; 2168 int d; 2169{ 2170 nsellength = atoi(val); 2171 if (nsellength < 0) 2172 errx(1, "Negative NSEL length is absurd"); 2173 if (afp == 0 || afp->af_af != AF_ISO) 2174 errx(1, "Setting NSEL length valid only for iso"); 2175} 2176 2177void 2178fixnsel(s) 2179 struct sockaddr_iso *s; 2180{ 2181 if (s->siso_family == 0) 2182 return; 2183 s->siso_tlen = nsellength; 2184} 2185 2186void 2187adjust_nsellength() 2188{ 2189 fixnsel(sisotab[RIDADDR]); 2190 fixnsel(sisotab[ADDR]); 2191 fixnsel(sisotab[DSTADDR]); 2192} 2193 2194#endif /* INET_ONLY */ 2195 2196void 2197usage() 2198{ 2199 fprintf(stderr, 2200 "usage: ifconfig [ -m ] [ -A ] %s interface\n%s%s%s%s%s%s%s%s%s%s%s%s", 2201#ifdef INET6 2202 "[ -L ]", 2203#else 2204 "", 2205#endif 2206 "\t[ af [ address [ dest_addr ] ] [ up ] [ down ] ", 2207 "[ netmask mask ] ]\n", 2208 "\t[ metric n ]\n", 2209 "\t[ mtu n ]\n", 2210 "\t[ arp | -arp ]\n", 2211 "\t[ media mtype ]\n", 2212 "\t[ mediaopt mopts ]\n", 2213 "\t[ -mediaopt mopts ]\n", 2214 "\t[ instance minst ]\n", 2215 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n", 2216 " ifconfig -a [ -A ] [ -m ] [ -d ] [ -u ] [ af ]\n", 2217 " ifconfig -l [ -d ] [ -u ]\n"); 2218 exit(1); 2219} 2220 2221#ifdef INET6 2222char * 2223sec2str(total) 2224 time_t total; 2225{ 2226 static char result[256]; 2227 int days, hours, mins, secs; 2228 int first = 1; 2229 char *p = result; 2230 2231 if (0) { /*XXX*/ 2232 days = total / 3600 / 24; 2233 hours = (total / 3600) % 24; 2234 mins = (total / 60) % 60; 2235 secs = total % 60; 2236 2237 if (days) { 2238 first = 0; 2239 p += sprintf(p, "%dd", days); 2240 } 2241 if (!first || hours) { 2242 first = 0; 2243 p += sprintf(p, "%dh", hours); 2244 } 2245 if (!first || mins) { 2246 first = 0; 2247 p += sprintf(p, "%dm", mins); 2248 } 2249 sprintf(p, "%ds", secs); 2250 } else 2251 sprintf(p, "%lu", (u_long)total); 2252 2253 return(result); 2254} 2255#endif 2256