| 1 | /* |
| 2 | * Copyright (c) 1983, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of the University nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.113.2.4 2006/02/09 10:48:43 yar Exp $ |
| 30 | */ |
| 31 | |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/sysctl.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/module.h> |
| 38 | #include <sys/linker.h> |
| 39 | #include <sys/cdefs.h> |
| 40 | #include <sys/queue.h> |
| 41 | |
| 42 | #include <net/ethernet.h> |
| 43 | #include <net/if.h> |
| 44 | #include <net/if_var.h> |
| 45 | #include <net/if_dl.h> |
| 46 | #include <net/if_types.h> |
| 47 | #include <net/route.h> |
| 48 | |
| 49 | /* IP */ |
| 50 | #include <netinet/in.h> |
| 51 | #include <netinet/in_var.h> |
| 52 | #include <arpa/inet.h> |
| 53 | #include <netdb.h> |
| 54 | |
| 55 | #include <ctype.h> |
| 56 | #include <err.h> |
| 57 | #include <errno.h> |
| 58 | #include <fcntl.h> |
| 59 | #include <fnmatch.h> |
| 60 | #include <ifaddrs.h> |
| 61 | #include <stdbool.h> |
| 62 | #include <stdio.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <string.h> |
| 65 | #include <unistd.h> |
| 66 | |
| 67 | #include "ifconfig.h" |
| 68 | |
| 69 | /* |
| 70 | * Since "struct ifreq" is composed of various union members, callers |
| 71 | * should pay special attention to interpret the value. |
| 72 | * (.e.g. little/big endian difference in the structure.) |
| 73 | */ |
| 74 | struct ifreq ifr; |
| 75 | |
| 76 | char name[IFNAMSIZ]; |
| 77 | int setaddr; |
| 78 | int setmask; |
| 79 | int doalias; |
| 80 | int clearaddr; |
| 81 | int newaddr = 1; |
| 82 | int verbose; |
| 83 | int noload; |
| 84 | |
| 85 | int supmedia = 0; |
| 86 | int printkeys = 0; /* Print keying material for interfaces. */ |
| 87 | int printifname = 0; /* Print the name of the created interface. */ |
| 88 | int exit_code = 0; |
| 89 | |
| 90 | /* Formatter strings */ |
| 91 | char *f_inet, *f_inet6, *f_ether, *f_addr; |
| 92 | |
| 93 | static bool group_member(const char *ifname, const char *match, |
| 94 | const char *nomatch); |
| 95 | static int ifconfig(int argc, char *const *argv, int iscreate, |
| 96 | const struct afswtch *afp); |
| 97 | static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl, |
| 98 | struct ifaddrs *ifa); |
| 99 | static void tunnel_status(int s); |
| 100 | static void usage(void) __dead2; |
| 101 | |
| 102 | static int getifflags(const char *ifname, int us); |
| 103 | static struct afswtch *af_getbyname(const char *name); |
| 104 | static struct afswtch *af_getbyfamily(int af); |
| 105 | static void af_other_status(int); |
| 106 | static void printifnamemaybe(void); |
| 107 | static void freeformat(void); |
| 108 | static void setformat(char *input); |
| 109 | |
| 110 | static struct option *opts = NULL; |
| 111 | |
| 112 | struct ifa_order_elt { |
| 113 | int if_order; |
| 114 | int af_orders[255]; |
| 115 | struct ifaddrs *ifa; |
| 116 | TAILQ_ENTRY(ifa_order_elt) link; |
| 117 | }; |
| 118 | TAILQ_HEAD(ifa_queue, ifa_order_elt); |
| 119 | |
| 120 | static int calcorders(struct ifaddrs *ifa, struct ifa_queue *q); |
| 121 | static int cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, |
| 122 | struct ifa_queue *q); |
| 123 | typedef int (*ifaddrs_cmp)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *); |
| 124 | static struct ifaddrs *sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare, |
| 125 | struct ifa_queue *q); |
| 126 | |
| 127 | |
| 128 | void |
| 129 | opt_register(struct option *p) |
| 130 | { |
| 131 | p->next = opts; |
| 132 | opts = p; |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | usage(void) |
| 137 | { |
| 138 | char options[1024]; |
| 139 | struct option *p; |
| 140 | |
| 141 | /* XXX not right but close enough for now */ |
| 142 | options[0] = '\0'; |
| 143 | for (p = opts; p != NULL; p = p->next) { |
| 144 | strlcat(options, p->opt_usage, sizeof(options)); |
| 145 | strlcat(options, " ", sizeof(options)); |
| 146 | } |
| 147 | |
| 148 | fprintf(stderr, |
| 149 | "usage: ifconfig %s[-n] [-f type:format] interface address_family\n" |
| 150 | " [address [dest_address]] [parameters]\n" |
| 151 | " ifconfig [-n] interface create\n" |
| 152 | " ifconfig [-n] interface destroy\n" |
| 153 | " ifconfig -a %s[-G nogroup] [-d | -u] [-m] [-v] [address_family]\n" |
| 154 | " ifconfig -l [-d | -u] [address_family]\n" |
| 155 | " ifconfig %s[-d | -u] [-m] [-v]\n", |
| 156 | options, options, options); |
| 157 | exit(1); |
| 158 | } |
| 159 | |
| 160 | static int |
| 161 | calcorders(struct ifaddrs *ifa, struct ifa_queue *q) |
| 162 | { |
| 163 | struct ifaddrs *prev; |
| 164 | struct ifa_order_elt *cur; |
| 165 | unsigned int ord, af, ifa_ord; |
| 166 | |
| 167 | prev = NULL; |
| 168 | cur = NULL; |
| 169 | ord = 0; |
| 170 | ifa_ord = 0; |
| 171 | |
| 172 | while (ifa != NULL) { |
| 173 | if (prev == NULL || |
| 174 | strcmp(ifa->ifa_name, prev->ifa_name) != 0) { |
| 175 | cur = calloc(1, sizeof(*cur)); |
| 176 | if (cur == NULL) |
| 177 | return (-1); |
| 178 | |
| 179 | TAILQ_INSERT_TAIL(q, cur, link); |
| 180 | cur->if_order = ifa_ord++; |
| 181 | cur->ifa = ifa; |
| 182 | ord = 0; |
| 183 | } |
| 184 | |
| 185 | if (ifa->ifa_addr) { |
| 186 | af = ifa->ifa_addr->sa_family; |
| 187 | |
| 188 | if (af < nitems(cur->af_orders) && |
| 189 | cur->af_orders[af] == 0) |
| 190 | cur->af_orders[af] = ++ord; |
| 191 | } |
| 192 | |
| 193 | prev = ifa; |
| 194 | ifa = ifa->ifa_next; |
| 195 | } |
| 196 | |
| 197 | return (0); |
| 198 | } |
| 199 | |
| 200 | static int |
| 201 | cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q) |
| 202 | { |
| 203 | struct ifa_order_elt *cur, *e1, *e2; |
| 204 | unsigned int af1, af2; |
| 205 | |
| 206 | e1 = e2 = NULL; |
| 207 | |
| 208 | if (strcmp(a->ifa_name, b->ifa_name) != 0) { |
| 209 | TAILQ_FOREACH(cur, q, link) { |
| 210 | if (e1 != NULL && e2 != NULL) |
| 211 | break; |
| 212 | |
| 213 | if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) |
| 214 | e1 = cur; |
| 215 | else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0) |
| 216 | e2 = cur; |
| 217 | } |
| 218 | |
| 219 | if (e1 == NULL || e2 == NULL) |
| 220 | return (0); |
| 221 | else |
| 222 | return (e1->if_order - e2->if_order); |
| 223 | |
| 224 | } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) { |
| 225 | TAILQ_FOREACH(cur, q, link) { |
| 226 | if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) { |
| 227 | e1 = cur; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (e1 == NULL) |
| 233 | return (0); |
| 234 | |
| 235 | af1 = a->ifa_addr->sa_family; |
| 236 | af2 = b->ifa_addr->sa_family; |
| 237 | |
| 238 | if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders)) |
| 239 | return (e1->af_orders[af1] - e1->af_orders[af2]); |
| 240 | } |
| 241 | |
| 242 | return (0); |
| 243 | } |
| 244 | |
| 245 | static struct ifaddrs * |
| 246 | sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare, struct ifa_queue *q) |
| 247 | { |
| 248 | struct ifaddrs *right, *temp, *last, *result, *next, *tail; |
| 249 | |
| 250 | right = temp = last = list; |
| 251 | result = next = tail = NULL; |
| 252 | |
| 253 | if (list == NULL || list->ifa_next == NULL) |
| 254 | return (list); |
| 255 | |
| 256 | while (temp != NULL && temp->ifa_next != NULL) { |
| 257 | last = right; |
| 258 | right = right->ifa_next; |
| 259 | temp = temp->ifa_next->ifa_next; |
| 260 | } |
| 261 | |
| 262 | last->ifa_next = NULL; |
| 263 | |
| 264 | list = sortifaddrs(list, compare, q); |
| 265 | right = sortifaddrs(right, compare, q); |
| 266 | |
| 267 | while (list != NULL || right != NULL) { |
| 268 | if (right == NULL) { |
| 269 | next = list; |
| 270 | list = list->ifa_next; |
| 271 | } else if (list == NULL) { |
| 272 | next = right; |
| 273 | right = right->ifa_next; |
| 274 | } else if (compare(list, right, q) <= 0) { |
| 275 | next = list; |
| 276 | list = list->ifa_next; |
| 277 | } else { |
| 278 | next = right; |
| 279 | right = right->ifa_next; |
| 280 | } |
| 281 | |
| 282 | if (result == NULL) |
| 283 | result = next; |
| 284 | else |
| 285 | tail->ifa_next = next; |
| 286 | |
| 287 | tail = next; |
| 288 | } |
| 289 | |
| 290 | return (result); |
| 291 | } |
| 292 | |
| 293 | static void |
| 294 | printifnamemaybe(void) |
| 295 | { |
| 296 | if (printifname) |
| 297 | printf("%s\n", name); |
| 298 | } |
| 299 | |
| 300 | static void |
| 301 | freeformat(void) |
| 302 | { |
| 303 | if (f_inet != NULL) |
| 304 | free(f_inet); |
| 305 | if (f_inet6 != NULL) |
| 306 | free(f_inet6); |
| 307 | if (f_ether != NULL) |
| 308 | free(f_ether); |
| 309 | if (f_addr != NULL) |
| 310 | free(f_addr); |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | setformat(char *input) |
| 315 | { |
| 316 | char *formatstr, *category, *modifier; |
| 317 | char **fp; |
| 318 | |
| 319 | formatstr = strdup(input); |
| 320 | if (formatstr == NULL) |
| 321 | err(1, "no memory to set format"); |
| 322 | |
| 323 | while ((category = strsep(&formatstr, ",")) != NULL) { |
| 324 | modifier = strchr(category, ':'); |
| 325 | if (modifier == NULL || modifier[1] == '\0') { |
| 326 | warnx("skip invalid format specification: %s\n", |
| 327 | category); |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | modifier[0] = '\0'; |
| 332 | modifier++; |
| 333 | |
| 334 | fp = NULL; |
| 335 | if (strcmp(category, "addr") == 0) |
| 336 | fp = &f_addr; |
| 337 | else if (strcmp(category, "ether") == 0) |
| 338 | fp = &f_ether; |
| 339 | else if (strcmp(category, "inet") == 0) |
| 340 | fp = &f_inet; |
| 341 | else if (strcmp(category, "inet6") == 0) |
| 342 | fp = &f_inet6; |
| 343 | |
| 344 | if (fp != NULL) { |
| 345 | *fp = strdup(modifier); |
| 346 | if (*fp == NULL) |
| 347 | err(1, "strdup"); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | free(formatstr); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | int |
| 356 | main(int argc, char *argv[]) |
| 357 | { |
| 358 | int c, all, namesonly, downonly, uponly; |
| 359 | int ifindex, flags; |
| 360 | const struct afswtch *afp = NULL; |
| 361 | const struct sockaddr_dl *sdl; |
| 362 | const char *ifname, *matchgroup, *nogroup; |
| 363 | struct ifa_order_elt *cur, *tmp; |
| 364 | struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q); |
| 365 | struct ifaddrs *ifap, *sifap, *ifa; |
| 366 | struct ifreq paifr; |
| 367 | struct option *p; |
| 368 | size_t iflen; |
| 369 | char *envformat, *cp; |
| 370 | char options[1024]; |
| 371 | |
| 372 | all = downonly = uponly = namesonly = verbose = noload = 0; |
| 373 | f_inet = f_inet6 = f_ether = f_addr = NULL; |
| 374 | matchgroup = nogroup = NULL; |
| 375 | |
| 376 | /* |
| 377 | * Ensure we print interface name when expected to, |
| 378 | * even if we terminate early due to error. |
| 379 | */ |
| 380 | atexit(printifnamemaybe); |
| 381 | |
| 382 | envformat = getenv("IFCONFIG_FORMAT"); |
| 383 | if (envformat != NULL) |
| 384 | setformat(envformat); |
| 385 | |
| 386 | /* Parse leading line options */ |
| 387 | strlcpy(options, "adf:G:klmnuv", sizeof(options)); |
| 388 | for (p = opts; p != NULL; p = p->next) |
| 389 | strlcat(options, p->opt, sizeof(options)); |
| 390 | while ((c = getopt(argc, argv, options)) != -1) { |
| 391 | switch (c) { |
| 392 | case 'a': /* scan all interfaces */ |
| 393 | all++; |
| 394 | break; |
| 395 | case 'd': /* restrict scan to "down" interfaces */ |
| 396 | downonly++; |
| 397 | break; |
| 398 | case 'f': |
| 399 | setformat(optarg); |
| 400 | break; |
| 401 | case 'G': |
| 402 | if (!all) |
| 403 | usage(); |
| 404 | nogroup = optarg; |
| 405 | break; |
| 406 | case 'k': |
| 407 | printkeys++; |
| 408 | break; |
| 409 | case 'l': /* scan interface names only */ |
| 410 | namesonly++; |
| 411 | break; |
| 412 | case 'm': /* show media choices in status */ |
| 413 | supmedia = 1; |
| 414 | break; |
| 415 | case 'n': /* suppress module loading */ |
| 416 | noload++; |
| 417 | break; |
| 418 | case 'u': /* restrict scan to "up" interfaces */ |
| 419 | uponly++; |
| 420 | break; |
| 421 | case 'v': |
| 422 | verbose++; |
| 423 | break; |
| 424 | case 'g': |
| 425 | if (all) { |
| 426 | matchgroup = optarg; |
| 427 | break; |
| 428 | } |
| 429 | /* FALLTHROUGH (for ifgroup) */ |
| 430 | default: |
| 431 | for (p = opts; p != NULL; p = p->next) |
| 432 | if (p->opt[0] == c) { |
| 433 | p->cb(optarg); |
| 434 | break; |
| 435 | } |
| 436 | if (p == NULL) |
| 437 | usage(); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | argc -= optind; |
| 442 | argv += optind; |
| 443 | |
| 444 | /* -l cannot be used with -a or -m */ |
| 445 | if (namesonly && (all || supmedia)) |
| 446 | usage(); |
| 447 | |
| 448 | /* nonsense.. */ |
| 449 | if (uponly && downonly) |
| 450 | usage(); |
| 451 | |
| 452 | /* no arguments is equivalent to '-a' */ |
| 453 | if (!namesonly && argc < 1) |
| 454 | all = 1; |
| 455 | |
| 456 | /* -a and -l allow an address family arg to limit the output */ |
| 457 | if (all || namesonly) { |
| 458 | if (argc > 1) |
| 459 | usage(); |
| 460 | |
| 461 | ifname = NULL; |
| 462 | ifindex = 0; |
| 463 | if (argc == 1) { |
| 464 | afp = af_getbyname(*argv); |
| 465 | if (afp == NULL) |
| 466 | usage(); |
| 467 | if (afp->af_name != NULL) |
| 468 | argc--, argv++; |
| 469 | /* leave with afp non-zero */ |
| 470 | } |
| 471 | } else { |
| 472 | /* not listing, need an argument */ |
| 473 | if (argc < 1) |
| 474 | usage(); |
| 475 | |
| 476 | ifname = *argv; |
| 477 | argc--, argv++; |
| 478 | |
| 479 | /* check and maybe load support for this interface */ |
| 480 | ifmaybeload(ifname); |
| 481 | |
| 482 | ifindex = if_nametoindex(ifname); |
| 483 | if (ifindex == 0) { |
| 484 | /* |
| 485 | * NOTE: We must special-case the `create' command |
| 486 | * right here as we would otherwise fail when trying |
| 487 | * to find the interface. |
| 488 | */ |
| 489 | if (argc > 0 && (strcmp(argv[0], "create") == 0 || |
| 490 | strcmp(argv[0], "plumb") == 0)) { |
| 491 | iflen = strlcpy(name, ifname, sizeof(name)); |
| 492 | if (iflen >= sizeof(name)) |
| 493 | errx(1, "%s: cloning name too long", |
| 494 | ifname); |
| 495 | ifconfig(argc, argv, 1, NULL); |
| 496 | exit(exit_code); |
| 497 | } |
| 498 | errx(1, "interface %s does not exist", ifname); |
| 499 | } else { |
| 500 | /* |
| 501 | * Do not allow to use `create` command as hostname |
| 502 | * if address family is not specified. |
| 503 | */ |
| 504 | if (argc > 0 && (strcmp(argv[0], "create") == 0 || |
| 505 | strcmp(argv[0], "plumb") == 0)) { |
| 506 | if (argc == 1) |
| 507 | errx(1, "interface %s alreay exists", |
| 508 | ifname); |
| 509 | argc--, argv++; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* Check for address family */ |
| 515 | if (argc > 0) { |
| 516 | afp = af_getbyname(*argv); |
| 517 | if (afp != NULL) |
| 518 | argc--, argv++; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Check for a requested configuration action on a single interface, |
| 523 | * which doesn't require building, sorting, and searching the entire |
| 524 | * system address list. |
| 525 | */ |
| 526 | if (argc > 0 && ifname != NULL) { |
| 527 | iflen = strlcpy(name, ifname, sizeof(name)); |
| 528 | if (iflen >= sizeof(name)) |
| 529 | errx(1, "%s: cloning name too long", ifname); |
| 530 | |
| 531 | flags = getifflags(name, -1); |
| 532 | if (!((downonly && (flags & IFF_UP) != 0) || |
| 533 | (uponly && (flags & IFF_UP) == 0))) { |
| 534 | ifconfig(argc, argv, 0, afp); |
| 535 | } |
| 536 | |
| 537 | exit(exit_code); |
| 538 | } |
| 539 | |
| 540 | if (getifaddrs(&ifap) != 0) |
| 541 | err(1, "getifaddrs"); |
| 542 | if (calcorders(ifap, &q) != 0) |
| 543 | err(1, "calcorders"); |
| 544 | sifap = sortifaddrs(ifap, cmpifaddrs, &q); |
| 545 | |
| 546 | TAILQ_FOREACH_MUTABLE(cur, &q, link, tmp) |
| 547 | free(cur); |
| 548 | |
| 549 | cp = NULL; |
| 550 | ifindex = 0; |
| 551 | for (ifa = sifap; ifa != NULL; ifa = ifa->ifa_next) { |
| 552 | memset(&paifr, 0, sizeof(paifr)); |
| 553 | strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name)); |
| 554 | if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) { |
| 555 | memcpy(&paifr.ifr_addr, ifa->ifa_addr, |
| 556 | ifa->ifa_addr->sa_len); |
| 557 | } |
| 558 | |
| 559 | if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0) |
| 560 | continue; |
| 561 | if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0) |
| 562 | continue; |
| 563 | iflen = strlcpy(name, ifa->ifa_name, sizeof(name)); |
| 564 | if (iflen >= sizeof(name)) { |
| 565 | warnx("%s: interface name too long, skipping", |
| 566 | ifa->ifa_name); |
| 567 | continue; |
| 568 | } |
| 569 | cp = ifa->ifa_name; |
| 570 | |
| 571 | if (downonly && (ifa->ifa_flags & IFF_UP) != 0) |
| 572 | continue; |
| 573 | if (uponly && (ifa->ifa_flags & IFF_UP) == 0) |
| 574 | continue; |
| 575 | if (!group_member(ifa->ifa_name, matchgroup, nogroup)) |
| 576 | continue; |
| 577 | |
| 578 | if (ifa->ifa_addr->sa_family == AF_LINK) |
| 579 | sdl = (const struct sockaddr_dl *)ifa->ifa_addr; |
| 580 | else |
| 581 | sdl = NULL; |
| 582 | |
| 583 | /* Are we just listing the interfaces? */ |
| 584 | if (namesonly) { |
| 585 | if (afp == NULL || |
| 586 | afp->af_af != AF_LINK || |
| 587 | (sdl != NULL && sdl->sdl_type == IFT_ETHER)) { |
| 588 | printf("%s%s", (ifindex > 0 ? " " : ""), name); |
| 589 | ifindex++; |
| 590 | } |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | if (argc > 0) |
| 595 | ifconfig(argc, argv, 0, afp); |
| 596 | else |
| 597 | status(afp, sdl, ifa); |
| 598 | } |
| 599 | |
| 600 | if (namesonly) |
| 601 | putchar('\n'); |
| 602 | |
| 603 | freeifaddrs(ifap); |
| 604 | freeformat(); |
| 605 | |
| 606 | return (exit_code); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /* |
| 611 | * Returns true if an interface should be listed because any its groups |
| 612 | * matches shell pattern "match" and none of groups matches pattern "nomatch". |
| 613 | * If any pattern is NULL, corresponding condition is skipped. |
| 614 | */ |
| 615 | static bool |
| 616 | group_member(const char *ifname, const char *match, const char *nomatch) |
| 617 | { |
| 618 | static int sock = -1; |
| 619 | |
| 620 | struct ifgroupreq ifgr; |
| 621 | struct ifg_req *ifg; |
| 622 | size_t len; |
| 623 | bool matched, nomatched; |
| 624 | |
| 625 | /* Sanity checks. */ |
| 626 | if (match == NULL && nomatch == NULL) |
| 627 | return (true); |
| 628 | if (ifname == NULL) |
| 629 | return (false); |
| 630 | |
| 631 | memset(&ifgr, 0, sizeof(ifgr)); |
| 632 | strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name)); |
| 633 | |
| 634 | /* The socket is opened once. Let _exit() close it. */ |
| 635 | if (sock == -1) { |
| 636 | sock = socket(AF_LOCAL, SOCK_DGRAM, 0); |
| 637 | if (sock == -1) |
| 638 | errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__); |
| 639 | } |
| 640 | |
| 641 | /* Determine amount of memory for the list of groups. */ |
| 642 | if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) { |
| 643 | if (errno == EINVAL || errno == ENOTTY) |
| 644 | return (false); |
| 645 | else |
| 646 | errx(1, "%s: SIOCGIFGROUP", __func__); |
| 647 | } |
| 648 | |
| 649 | /* Obtain the list of groups. */ |
| 650 | len = ifgr.ifgr_len; |
| 651 | ifgr.ifgr_groups = |
| 652 | (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg)); |
| 653 | if (ifgr.ifgr_groups == NULL) |
| 654 | errx(1, "%s: no memory", __func__); |
| 655 | if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) |
| 656 | errx(1, "%s: SIOCGIFGROUP", __func__); |
| 657 | |
| 658 | /* Perform matching. */ |
| 659 | matched = false; |
| 660 | nomatched = true; |
| 661 | for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) { |
| 662 | len -= sizeof(struct ifg_req); |
| 663 | if (match) |
| 664 | matched |= !fnmatch(match, ifg->ifgrq_group, 0); |
| 665 | if (nomatch) |
| 666 | nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0); |
| 667 | } |
| 668 | |
| 669 | if (match && !nomatch) |
| 670 | return (matched); |
| 671 | if (!match && nomatch) |
| 672 | return (nomatched); |
| 673 | return (matched && nomatched); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | static struct afswtch *afs = NULL; |
| 678 | |
| 679 | void |
| 680 | af_register(struct afswtch *p) |
| 681 | { |
| 682 | p->af_next = afs; |
| 683 | afs = p; |
| 684 | } |
| 685 | |
| 686 | static struct afswtch * |
| 687 | af_getbyname(const char *name) |
| 688 | { |
| 689 | struct afswtch *afp; |
| 690 | |
| 691 | for (afp = afs; afp != NULL; afp = afp->af_next) |
| 692 | if (strcmp(afp->af_name, name) == 0) |
| 693 | return afp; |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | static struct afswtch * |
| 698 | af_getbyfamily(int af) |
| 699 | { |
| 700 | struct afswtch *afp; |
| 701 | |
| 702 | for (afp = afs; afp != NULL; afp = afp->af_next) |
| 703 | if (afp->af_af == af) |
| 704 | return afp; |
| 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | static void |
| 709 | af_other_status(int s) |
| 710 | { |
| 711 | struct afswtch *afp; |
| 712 | uint8_t afmask[howmany(AF_MAX, NBBY)]; |
| 713 | |
| 714 | memset(afmask, 0, sizeof(afmask)); |
| 715 | for (afp = afs; afp != NULL; afp = afp->af_next) { |
| 716 | if (afp->af_other_status == NULL) |
| 717 | continue; |
| 718 | if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) |
| 719 | continue; |
| 720 | afp->af_other_status(s); |
| 721 | setbit(afmask, afp->af_af); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | static void |
| 726 | af_all_tunnel_status(int s) |
| 727 | { |
| 728 | struct afswtch *afp; |
| 729 | uint8_t afmask[howmany(AF_MAX, NBBY)]; |
| 730 | |
| 731 | memset(afmask, 0, sizeof(afmask)); |
| 732 | for (afp = afs; afp != NULL; afp = afp->af_next) { |
| 733 | if (afp->af_status_tunnel == NULL) |
| 734 | continue; |
| 735 | if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) |
| 736 | continue; |
| 737 | afp->af_status_tunnel(s); |
| 738 | setbit(afmask, afp->af_af); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | static struct cmd *cmds = NULL; |
| 743 | |
| 744 | void |
| 745 | cmd_register(struct cmd *p) |
| 746 | { |
| 747 | p->c_next = cmds; |
| 748 | cmds = p; |
| 749 | } |
| 750 | |
| 751 | static const struct cmd * |
| 752 | cmd_lookup(const char *name, int iscreate) |
| 753 | { |
| 754 | const struct cmd *p; |
| 755 | |
| 756 | for (p = cmds; p != NULL; p = p->c_next) { |
| 757 | if (strcmp(name, p->c_name) == 0) { |
| 758 | if (iscreate) { |
| 759 | if (p->c_iscloneop) |
| 760 | return p; |
| 761 | } else { |
| 762 | if (!p->c_iscloneop) |
| 763 | return p; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | struct callback { |
| 772 | callback_func *cb_func; |
| 773 | void *cb_arg; |
| 774 | struct callback *cb_next; |
| 775 | }; |
| 776 | static struct callback *callbacks = NULL; |
| 777 | |
| 778 | void |
| 779 | callback_register(callback_func *func, void *arg) |
| 780 | { |
| 781 | struct callback *cb; |
| 782 | |
| 783 | cb = malloc(sizeof(struct callback)); |
| 784 | if (cb == NULL) |
| 785 | errx(1, "unable to allocate memory for callback"); |
| 786 | cb->cb_func = func; |
| 787 | cb->cb_arg = arg; |
| 788 | cb->cb_next = callbacks; |
| 789 | callbacks = cb; |
| 790 | } |
| 791 | |
| 792 | /* specially-handled commands */ |
| 793 | static void setifaddr(const char *, int, int, const struct afswtch *); |
| 794 | static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); |
| 795 | |
| 796 | static void setifdstaddr(const char *, int, int, const struct afswtch *); |
| 797 | static const struct cmd setifdstaddr_cmd = |
| 798 | DEF_CMD("ifdstaddr", 0, setifdstaddr); |
| 799 | |
| 800 | static int |
| 801 | ifconfig(int argc, char *const *argv, int iscreate, |
| 802 | const struct afswtch *uafp) |
| 803 | { |
| 804 | const struct afswtch *afp, *nafp; |
| 805 | struct callback *cb; |
| 806 | int s; |
| 807 | |
| 808 | strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
| 809 | afp = uafp != NULL ? uafp : af_getbyname("inet"); |
| 810 | top: |
| 811 | ifr.ifr_addr.sa_family = |
| 812 | afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? |
| 813 | AF_LOCAL : afp->af_af; |
| 814 | |
| 815 | if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && |
| 816 | (uafp != NULL || errno != EAFNOSUPPORT || |
| 817 | (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) |
| 818 | err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); |
| 819 | |
| 820 | while (argc > 0) { |
| 821 | const struct cmd *p; |
| 822 | |
| 823 | p = cmd_lookup(*argv, iscreate); |
| 824 | |
| 825 | if (iscreate && p == NULL) { |
| 826 | /* |
| 827 | * Push the clone create callback so the new |
| 828 | * device is created and can be used for any |
| 829 | * remaining arguments. |
| 830 | */ |
| 831 | cb = callbacks; |
| 832 | if (cb == NULL) |
| 833 | errx(1, "internal error, no callback"); |
| 834 | callbacks = cb->cb_next; |
| 835 | cb->cb_func(s, cb->cb_arg); |
| 836 | iscreate = 0; |
| 837 | |
| 838 | /* |
| 839 | * After cloning, make sure we have an up-to-date name |
| 840 | * in ifr_name. |
| 841 | */ |
| 842 | strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 843 | |
| 844 | /* |
| 845 | * Handle any address family spec that |
| 846 | * immediately follows and potentially |
| 847 | * recreate the socket. |
| 848 | */ |
| 849 | nafp = af_getbyname(*argv); |
| 850 | if (nafp != NULL) { |
| 851 | argc--, argv++; |
| 852 | if (nafp != afp) { |
| 853 | close(s); |
| 854 | afp = nafp; |
| 855 | goto top; |
| 856 | } |
| 857 | } |
| 858 | /* |
| 859 | * Look for a normal parameter. |
| 860 | */ |
| 861 | continue; |
| 862 | } |
| 863 | if (p == NULL) { |
| 864 | /* |
| 865 | * Not a recognized command, choose between setting |
| 866 | * the interface address and the dst address. |
| 867 | */ |
| 868 | p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); |
| 869 | } |
| 870 | if (p->c_u.c_func || p->c_u.c_func2) { |
| 871 | if (p->c_parameter == NEXTARG) { |
| 872 | if (argv[1] == NULL) |
| 873 | errx(1, "'%s' requires argument", |
| 874 | p->c_name); |
| 875 | p->c_u.c_func(argv[1], 0, s, afp); |
| 876 | argc--, argv++; |
| 877 | } else if (p->c_parameter == OPTARG) { |
| 878 | p->c_u.c_func(argv[1], 0, s, afp); |
| 879 | if (argv[1] != NULL) |
| 880 | argc--, argv++; |
| 881 | } else if (p->c_parameter == NEXTARG2) { |
| 882 | if (argc < 3) |
| 883 | errx(1, "'%s' requires 2 arguments", |
| 884 | p->c_name); |
| 885 | p->c_u.c_func2(argv[1], argv[2], s, afp); |
| 886 | argc -= 2, argv += 2; |
| 887 | } else |
| 888 | p->c_u.c_func(*argv, p->c_parameter, s, afp); |
| 889 | } |
| 890 | argc--, argv++; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Do any post argument processing required by the address family. |
| 895 | */ |
| 896 | if (afp->af_postproc != NULL) |
| 897 | afp->af_postproc(s, afp); |
| 898 | /* |
| 899 | * Do deferred callbacks registered while processing |
| 900 | * command-line arguments. |
| 901 | */ |
| 902 | for (cb = callbacks; cb != NULL; cb = cb->cb_next) |
| 903 | cb->cb_func(s, cb->cb_arg); |
| 904 | /* |
| 905 | * Do deferred operations. |
| 906 | */ |
| 907 | if (clearaddr) { |
| 908 | if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { |
| 909 | warnx("interface %s cannot change %s addresses!", |
| 910 | name, afp->af_name); |
| 911 | clearaddr = 0; |
| 912 | } |
| 913 | } |
| 914 | if (clearaddr) { |
| 915 | int ret; |
| 916 | strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name); |
| 917 | ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); |
| 918 | if (ret < 0) { |
| 919 | if (errno == EADDRNOTAVAIL && (doalias >= 0)) { |
| 920 | /* means no previous address for interface */ |
| 921 | } else |
| 922 | Perror("ioctl (SIOCDIFADDR)"); |
| 923 | } |
| 924 | } |
| 925 | if (newaddr) { |
| 926 | if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { |
| 927 | warnx("interface %s cannot change %s addresses!", |
| 928 | name, afp->af_name); |
| 929 | newaddr = 0; |
| 930 | } |
| 931 | } |
| 932 | if (newaddr && (setaddr || setmask)) { |
| 933 | strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name); |
| 934 | if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) |
| 935 | Perror("ioctl (SIOCAIFADDR)"); |
| 936 | } |
| 937 | |
| 938 | close(s); |
| 939 | return (0); |
| 940 | } |
| 941 | |
| 942 | /*ARGSUSED*/ |
| 943 | static void |
| 944 | setifaddr(const char *addr, int param, int s, const struct afswtch *afp) |
| 945 | { |
| 946 | if (afp->af_getaddr == NULL) |
| 947 | return; |
| 948 | /* |
| 949 | * Delay the ioctl to set the interface addr until flags are all set. |
| 950 | * The address interpretation may depend on the flags, |
| 951 | * and the flags may change when the address is set. |
| 952 | */ |
| 953 | setaddr++; |
| 954 | if (doalias == 0 && afp->af_af != AF_LINK) |
| 955 | clearaddr = 1; |
| 956 | afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR)); |
| 957 | } |
| 958 | |
| 959 | static void |
| 960 | settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) |
| 961 | { |
| 962 | struct addrinfo *srcres, *dstres; |
| 963 | int ecode; |
| 964 | |
| 965 | if (afp->af_settunnel == NULL) { |
| 966 | warn("address family %s does not support tunnel setup", |
| 967 | afp->af_name); |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) |
| 972 | errx(1, "error in parsing address string: %s", |
| 973 | gai_strerror(ecode)); |
| 974 | |
| 975 | if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) |
| 976 | errx(1, "error in parsing address string: %s", |
| 977 | gai_strerror(ecode)); |
| 978 | |
| 979 | if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) |
| 980 | errx(1, |
| 981 | "source and destination address families do not match"); |
| 982 | |
| 983 | afp->af_settunnel(s, srcres, dstres); |
| 984 | |
| 985 | freeaddrinfo(srcres); |
| 986 | freeaddrinfo(dstres); |
| 987 | } |
| 988 | |
| 989 | /* ARGSUSED */ |
| 990 | static void |
| 991 | deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) |
| 992 | { |
| 993 | |
| 994 | if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) |
| 995 | err(1, "SIOCDIFPHYADDR"); |
| 996 | } |
| 997 | |
| 998 | static void |
| 999 | setifnetmask(const char *addr, int dummy __unused, int s, |
| 1000 | const struct afswtch *afp) |
| 1001 | { |
| 1002 | if (afp->af_getaddr != NULL) { |
| 1003 | setmask++; |
| 1004 | afp->af_getaddr(addr, MASK); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | static void |
| 1009 | setifbroadaddr(const char *addr, int dummy __unused, int s, |
| 1010 | const struct afswtch *afp) |
| 1011 | { |
| 1012 | if (afp->af_getaddr != NULL) |
| 1013 | afp->af_getaddr(addr, DSTADDR); |
| 1014 | } |
| 1015 | |
| 1016 | static void |
| 1017 | notealias(const char *addr, int param, int s, const struct afswtch *afp) |
| 1018 | { |
| 1019 | #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) |
| 1020 | if (setaddr && doalias == 0 && param < 0) |
| 1021 | if (afp->af_addreq != NULL && afp->af_ridreq != NULL) |
| 1022 | bcopy((caddr_t)rqtosa(af_addreq), |
| 1023 | (caddr_t)rqtosa(af_ridreq), |
| 1024 | rqtosa(af_addreq)->sa_len); |
| 1025 | doalias = param; |
| 1026 | if (param < 0) { |
| 1027 | clearaddr = 1; |
| 1028 | newaddr = 0; |
| 1029 | } else { |
| 1030 | clearaddr = 0; |
| 1031 | } |
| 1032 | #undef rqtosa |
| 1033 | } |
| 1034 | |
| 1035 | /*ARGSUSED*/ |
| 1036 | static void |
| 1037 | setifdstaddr(const char *addr, int param __unused, int s, |
| 1038 | const struct afswtch *afp) |
| 1039 | { |
| 1040 | if (afp->af_getaddr != NULL) |
| 1041 | afp->af_getaddr(addr, DSTADDR); |
| 1042 | } |
| 1043 | |
| 1044 | static int |
| 1045 | getifflags(const char *ifname, int us) |
| 1046 | { |
| 1047 | struct ifreq my_ifr; |
| 1048 | int s; |
| 1049 | |
| 1050 | memset(&my_ifr, 0, sizeof(struct ifreq)); |
| 1051 | strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name)); |
| 1052 | |
| 1053 | s = us; |
| 1054 | if (us < 0) { |
| 1055 | if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) |
| 1056 | err(1, "socket(family AF_LOCAL,SOCK_DGRAM)"); |
| 1057 | } |
| 1058 | |
| 1059 | if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) |
| 1060 | Perror("ioctl (SIOCGIFFLAGS)"); |
| 1061 | |
| 1062 | if (us < 0) |
| 1063 | close(s); |
| 1064 | |
| 1065 | return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16)); |
| 1066 | } |
| 1067 | |
| 1068 | static void |
| 1069 | setifflags(const char *vname, int value, int s, const struct afswtch *afp) |
| 1070 | { |
| 1071 | struct ifreq my_ifr; |
| 1072 | int flags; |
| 1073 | |
| 1074 | flags = getifflags(name, s); |
| 1075 | if (value < 0) { |
| 1076 | value = -value; |
| 1077 | flags &= ~value; |
| 1078 | } else { |
| 1079 | flags |= value; |
| 1080 | } |
| 1081 | |
| 1082 | memset(&my_ifr, 0, sizeof(struct ifreq)); |
| 1083 | strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name)); |
| 1084 | my_ifr.ifr_flags = flags & 0xffff; |
| 1085 | my_ifr.ifr_flagshigh = flags >> 16; |
| 1086 | if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) |
| 1087 | Perror(vname); |
| 1088 | } |
| 1089 | |
| 1090 | void |
| 1091 | setifcap(const char *vname, int value, int s, const struct afswtch *afp) |
| 1092 | { |
| 1093 | int flags; |
| 1094 | |
| 1095 | if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) |
| 1096 | Perror("ioctl (SIOCGIFCAP)"); |
| 1097 | |
| 1098 | flags = ifr.ifr_curcap; |
| 1099 | if (value < 0) { |
| 1100 | value = -value; |
| 1101 | flags &= ~value; |
| 1102 | } else { |
| 1103 | flags |= value; |
| 1104 | } |
| 1105 | ifr.ifr_reqcap = flags; |
| 1106 | if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) |
| 1107 | Perror(vname); |
| 1108 | } |
| 1109 | |
| 1110 | static void |
| 1111 | setifmetric(const char *val, int dummy __unused, int s, |
| 1112 | const struct afswtch *afp) |
| 1113 | { |
| 1114 | strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); |
| 1115 | ifr.ifr_metric = atoi(val); |
| 1116 | if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) |
| 1117 | err(1, "ioctl SIOCSIFMETRIC (set metric)"); |
| 1118 | } |
| 1119 | |
| 1120 | static void |
| 1121 | setifmtu(const char *val, int dummy __unused, int s, |
| 1122 | const struct afswtch *afp) |
| 1123 | { |
| 1124 | strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); |
| 1125 | ifr.ifr_mtu = atoi(val); |
| 1126 | if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) |
| 1127 | err(1, "ioctl SIOCSIFMTU (set mtu)"); |
| 1128 | } |
| 1129 | |
| 1130 | static void |
| 1131 | setiftsolen(const char *val, int dummy __unused, int s, |
| 1132 | const struct afswtch *afp) |
| 1133 | { |
| 1134 | strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); |
| 1135 | ifr.ifr_tsolen = atoi(val); |
| 1136 | if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0) |
| 1137 | err(1, "ioctl SIOCSIFTSOLEN (set tsolen)"); |
| 1138 | } |
| 1139 | |
| 1140 | static void |
| 1141 | setifname(const char *val, int dummy __unused, int s, |
| 1142 | const struct afswtch *afp) |
| 1143 | { |
| 1144 | char *newname; |
| 1145 | |
| 1146 | strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 1147 | |
| 1148 | newname = strdup(val); |
| 1149 | if (newname == NULL) |
| 1150 | err(1, "no memory to set ifname"); |
| 1151 | ifr.ifr_data = newname; |
| 1152 | if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { |
| 1153 | free(newname); |
| 1154 | err(1, "ioctl SIOCSIFNAME (set name)"); |
| 1155 | } |
| 1156 | printifname = 1; |
| 1157 | strlcpy(name, newname, sizeof(name)); |
| 1158 | free(newname); |
| 1159 | } |
| 1160 | |
| 1161 | static void |
| 1162 | setifpollcpu(const char *val, int dummy __unused, int s, |
| 1163 | const struct afswtch *afp) |
| 1164 | { |
| 1165 | warnx("pollcpu is deprecated, use polling or npolling instead"); |
| 1166 | setifflags("npolling", IFF_NPOLLING, s, afp); |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | #define IFFBITS \ |
| 1171 | "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ |
| 1172 | "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ |
| 1173 | "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT" |
| 1174 | |
| 1175 | #define IFCAPBITS \ |
| 1176 | "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \ |
| 1177 | "\10VLAN_HWCSUM\11TSO" |
| 1178 | |
| 1179 | /* |
| 1180 | * Print the status of the interface. If an address family was |
| 1181 | * specified, show only it; otherwise, show them all. |
| 1182 | */ |
| 1183 | static void |
| 1184 | status(const struct afswtch *afp, const struct sockaddr_dl *sdl, |
| 1185 | struct ifaddrs *ifa) |
| 1186 | { |
| 1187 | struct ifaddrs *ift; |
| 1188 | int allfamilies, s; |
| 1189 | struct ifstat ifs; |
| 1190 | |
| 1191 | if (afp == NULL) { |
| 1192 | allfamilies = 1; |
| 1193 | ifr.ifr_addr.sa_family = AF_LOCAL; |
| 1194 | } else { |
| 1195 | allfamilies = 0; |
| 1196 | ifr.ifr_addr.sa_family = |
| 1197 | afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af; |
| 1198 | } |
| 1199 | strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 1200 | |
| 1201 | s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); |
| 1202 | if (s < 0) |
| 1203 | err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); |
| 1204 | |
| 1205 | printf("%s: ", name); |
| 1206 | printb("flags", ifa->ifa_flags, IFFBITS); |
| 1207 | if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1) |
| 1208 | printf(" metric %d", ifr.ifr_metric); |
| 1209 | if (ioctl(s, SIOCGIFMTU, &ifr) != -1) |
| 1210 | printf(" mtu %d", ifr.ifr_mtu); |
| 1211 | putchar('\n'); |
| 1212 | |
| 1213 | if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { |
| 1214 | if (ifr.ifr_curcap != 0) { |
| 1215 | printb("\toptions", ifr.ifr_curcap, IFCAPBITS); |
| 1216 | putchar('\n'); |
| 1217 | } |
| 1218 | if (supmedia && ifr.ifr_reqcap != 0) { |
| 1219 | printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); |
| 1220 | putchar('\n'); |
| 1221 | if (ifr.ifr_reqcap & IFCAP_TSO) { |
| 1222 | if (ioctl(s, SIOCGIFTSOLEN, |
| 1223 | (caddr_t)&ifr) == 0) { |
| 1224 | printf("\ttsolen %d", ifr.ifr_tsolen); |
| 1225 | putchar('\n'); |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | tunnel_status(s); |
| 1232 | |
| 1233 | for (ift = ifa; ift != NULL; ift = ift->ifa_next) { |
| 1234 | if (ift->ifa_addr == NULL) |
| 1235 | continue; |
| 1236 | if (strcmp(ifa->ifa_name, ift->ifa_name) != 0) |
| 1237 | continue; |
| 1238 | if (allfamilies) { |
| 1239 | const struct afswtch *p; |
| 1240 | p = af_getbyfamily(ift->ifa_addr->sa_family); |
| 1241 | if (p != NULL && p->af_status != NULL) |
| 1242 | p->af_status(s, ift); |
| 1243 | } else if (afp->af_af == ift->ifa_addr->sa_family) |
| 1244 | afp->af_status(s, ift); |
| 1245 | } |
| 1246 | #if 0 |
| 1247 | if (allfamilies || afp->af_af == AF_LINK) { |
| 1248 | const struct afswtch *lafp; |
| 1249 | |
| 1250 | /* |
| 1251 | * Hack; the link level address is received separately |
| 1252 | * from the routing information so any address is not |
| 1253 | * handled above. Cobble together an entry and invoke |
| 1254 | * the status method specially. |
| 1255 | */ |
| 1256 | lafp = af_getbyname("lladdr"); |
| 1257 | if (lafp != NULL) { |
| 1258 | info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl; |
| 1259 | lafp->af_status(s, &info); |
| 1260 | } |
| 1261 | } |
| 1262 | #endif |
| 1263 | if (allfamilies) |
| 1264 | af_other_status(s); |
| 1265 | else if (afp->af_other_status != NULL) |
| 1266 | afp->af_other_status(s); |
| 1267 | |
| 1268 | strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name); |
| 1269 | if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) |
| 1270 | printf("%s", ifs.ascii); |
| 1271 | |
| 1272 | close(s); |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | static void |
| 1277 | tunnel_status(int s) |
| 1278 | { |
| 1279 | af_all_tunnel_status(s); |
| 1280 | } |
| 1281 | |
| 1282 | void |
| 1283 | Perror(const char *cmd) |
| 1284 | { |
| 1285 | switch (errno) { |
| 1286 | |
| 1287 | case ENXIO: |
| 1288 | errx(1, "%s: no such interface", cmd); |
| 1289 | break; |
| 1290 | |
| 1291 | case EPERM: |
| 1292 | errx(1, "%s: permission denied", cmd); |
| 1293 | break; |
| 1294 | |
| 1295 | default: |
| 1296 | err(1, "%s", cmd); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Print a value a la the %pb%i format of the kernel's kprintf() |
| 1302 | */ |
| 1303 | void |
| 1304 | printb(const char *s, unsigned v, const char *bits) |
| 1305 | { |
| 1306 | int i, any = 0; |
| 1307 | char c; |
| 1308 | |
| 1309 | if (bits && *bits == 8) |
| 1310 | printf("%s=%o", s, v); |
| 1311 | else |
| 1312 | printf("%s=%x", s, v); |
| 1313 | bits++; |
| 1314 | if (bits) { |
| 1315 | putchar('<'); |
| 1316 | while ((i = *bits++) != '\0') { |
| 1317 | if (v & (1 << (i-1))) { |
| 1318 | if (any) |
| 1319 | putchar(','); |
| 1320 | any = 1; |
| 1321 | for (; (c = *bits) > 32; bits++) |
| 1322 | putchar(c); |
| 1323 | } else |
| 1324 | for (; *bits > 32; bits++) |
| 1325 | ; |
| 1326 | } |
| 1327 | putchar('>'); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | void |
| 1332 | ifmaybeload(const char *name) |
| 1333 | { |
| 1334 | #define MOD_PREFIX_LEN 3 /* "if_" */ |
| 1335 | struct module_stat mstat; |
| 1336 | int fileid, modid; |
| 1337 | char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; |
| 1338 | const char *cp; |
| 1339 | |
| 1340 | /* loading suppressed by the user */ |
| 1341 | if (noload) |
| 1342 | return; |
| 1343 | |
| 1344 | /* trim the interface number off the end */ |
| 1345 | strlcpy(ifname, name, sizeof(ifname)); |
| 1346 | for (dp = ifname; *dp != 0; dp++) |
| 1347 | if (isdigit(*dp)) { |
| 1348 | *dp = 0; |
| 1349 | break; |
| 1350 | } |
| 1351 | |
| 1352 | /* turn interface and unit into module name */ |
| 1353 | strlcpy(ifkind, "if_", sizeof(ifkind)); |
| 1354 | strlcat(ifkind, ifname, sizeof(ifkind)); |
| 1355 | |
| 1356 | /* scan files in kernel */ |
| 1357 | mstat.version = sizeof(struct module_stat); |
| 1358 | for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { |
| 1359 | /* scan modules in file */ |
| 1360 | for (modid = kldfirstmod(fileid); modid > 0; |
| 1361 | modid = modfnext(modid)) { |
| 1362 | if (modstat(modid, &mstat) < 0) |
| 1363 | continue; |
| 1364 | /* strip bus name if present */ |
| 1365 | if ((cp = strchr(mstat.name, '/')) != NULL) { |
| 1366 | cp++; |
| 1367 | } else { |
| 1368 | cp = mstat.name; |
| 1369 | } |
| 1370 | /* already loaded? */ |
| 1371 | if (strcmp(ifname, cp) == 0 || |
| 1372 | strcmp(ifkind, cp) == 0) |
| 1373 | return; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | /* not present, we should try to load it */ |
| 1378 | kldload(ifkind); |
| 1379 | } |
| 1380 | |
| 1381 | static struct cmd basic_cmds[] = { |
| 1382 | DEF_CMD("up", IFF_UP, setifflags), |
| 1383 | DEF_CMD("down", -IFF_UP, setifflags), |
| 1384 | DEF_CMD("arp", -IFF_NOARP, setifflags), |
| 1385 | DEF_CMD("-arp", IFF_NOARP, setifflags), |
| 1386 | DEF_CMD("debug", IFF_DEBUG, setifflags), |
| 1387 | DEF_CMD("-debug", -IFF_DEBUG, setifflags), |
| 1388 | DEF_CMD("promisc", IFF_PPROMISC, setifflags), |
| 1389 | DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), |
| 1390 | DEF_CMD("add", IFF_UP, notealias), |
| 1391 | DEF_CMD("alias", IFF_UP, notealias), |
| 1392 | DEF_CMD("-alias", -IFF_UP, notealias), |
| 1393 | DEF_CMD("delete", -IFF_UP, notealias), |
| 1394 | DEF_CMD("remove", -IFF_UP, notealias), |
| 1395 | #ifdef notdef |
| 1396 | #define EN_SWABIPS 0x1000 |
| 1397 | DEF_CMD("swabips", EN_SWABIPS, setifflags), |
| 1398 | DEF_CMD("-swabips", -EN_SWABIPS, setifflags), |
| 1399 | #endif |
| 1400 | DEF_CMD_ARG("netmask", setifnetmask), |
| 1401 | DEF_CMD_ARG("metric", setifmetric), |
| 1402 | DEF_CMD_ARG("broadcast", setifbroadaddr), |
| 1403 | DEF_CMD_ARG2("tunnel", settunnel), |
| 1404 | DEF_CMD("-tunnel", 0, deletetunnel), |
| 1405 | DEF_CMD("deletetunnel", 0, deletetunnel), |
| 1406 | DEF_CMD("link0", IFF_LINK0, setifflags), |
| 1407 | DEF_CMD("-link0", -IFF_LINK0, setifflags), |
| 1408 | DEF_CMD("link1", IFF_LINK1, setifflags), |
| 1409 | DEF_CMD("-link1", -IFF_LINK1, setifflags), |
| 1410 | DEF_CMD("link2", IFF_LINK2, setifflags), |
| 1411 | DEF_CMD("-link2", -IFF_LINK2, setifflags), |
| 1412 | DEF_CMD("monitor", IFF_MONITOR, setifflags), |
| 1413 | DEF_CMD("-monitor", -IFF_MONITOR, setifflags), |
| 1414 | DEF_CMD("staticarp", IFF_STATICARP, setifflags), |
| 1415 | DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), |
| 1416 | DEF_CMD("polling", IFF_NPOLLING, setifflags), |
| 1417 | DEF_CMD("-polling", -IFF_NPOLLING, setifflags), |
| 1418 | DEF_CMD("npolling", IFF_NPOLLING, setifflags), |
| 1419 | DEF_CMD("-npolling", -IFF_NPOLLING, setifflags), |
| 1420 | DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), |
| 1421 | DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), |
| 1422 | DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), |
| 1423 | DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), |
| 1424 | DEF_CMD("netcons", IFCAP_NETCONS, setifcap), |
| 1425 | DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), |
| 1426 | DEF_CMD("rss", IFCAP_RSS, setifcap), |
| 1427 | DEF_CMD("-rss", -IFCAP_RSS, setifcap), |
| 1428 | DEF_CMD("tso", IFCAP_TSO, setifcap), |
| 1429 | DEF_CMD("-tso", -IFCAP_TSO, setifcap), |
| 1430 | DEF_CMD("normal", -IFF_LINK0, setifflags), |
| 1431 | DEF_CMD("compress", IFF_LINK0, setifflags), |
| 1432 | DEF_CMD("noicmp", IFF_LINK1, setifflags), |
| 1433 | DEF_CMD_ARG("mtu", setifmtu), |
| 1434 | DEF_CMD_ARG("name", setifname), |
| 1435 | DEF_CMD_ARG("pollcpu", setifpollcpu), |
| 1436 | DEF_CMD_ARG("tsolen", setiftsolen) |
| 1437 | }; |
| 1438 | |
| 1439 | static __constructor(101) void |
| 1440 | ifconfig_ctor(void) |
| 1441 | { |
| 1442 | size_t i; |
| 1443 | |
| 1444 | for (i = 0; i < nitems(basic_cmds); i++) |
| 1445 | cmd_register(&basic_cmds[i]); |
| 1446 | } |