1 /*- 2 * Copyright 2001 Wasabi Systems, Inc. 3 * All rights reserved. 4 * 5 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the NetBSD Project by 18 * Wasabi Systems, Inc. 19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 20 * or promote products derived from this software without specific prior 21 * written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 * $FreeBSD: src/sbin/ifconfig/ifbridge.c,v 1.1.2.2 2005/12/28 04:12:58 thompsa Exp $ 36 */ 37 38 #include <sys/param.h> 39 #include <sys/ioctl.h> 40 #include <sys/socket.h> 41 #include <sys/sockio.h> 42 43 #include <stdlib.h> 44 #include <unistd.h> 45 46 #include <net/ethernet.h> 47 #include <net/if.h> 48 #include <net/bridge/if_bridgevar.h> 49 #include <net/route.h> 50 51 #include <ctype.h> 52 #include <stdio.h> 53 #include <string.h> 54 #include <stdlib.h> 55 #include <unistd.h> 56 #include <err.h> 57 #include <errno.h> 58 59 #include "ifconfig.h" 60 61 static int 62 get_val(const char *cp, u_long *valp) 63 { 64 char *endptr; 65 u_long val; 66 67 errno = 0; 68 val = strtoul(cp, &endptr, 0); 69 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE) 70 return (-1); 71 72 *valp = val; 73 return (0); 74 } 75 76 static int 77 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set) 78 { 79 struct ifdrv ifd; 80 81 memset(&ifd, 0, sizeof(ifd)); 82 83 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name)); 84 ifd.ifd_cmd = op; 85 ifd.ifd_len = argsize; 86 ifd.ifd_data = arg; 87 88 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd)); 89 } 90 91 static void 92 do_bridgeflag(int sock, const char *ifs, int flag, int set) 93 { 94 struct ifbreq req; 95 96 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname)); 97 98 if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0) 99 err(1, "unable to get bridge flags"); 100 101 if (set) 102 req.ifbr_ifsflags |= flag; 103 else 104 req.ifbr_ifsflags &= ~flag; 105 106 if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0) 107 err(1, "unable to set bridge flags"); 108 } 109 110 static void 111 bridge_interfaces(int s, const char *prefix) 112 { 113 static const char *stpstates[] = { 114 "disabled", 115 "listening", 116 "learning", 117 "forwarding", 118 "blocking", 119 "bonded", 120 "blocking (link1)" 121 }; 122 struct ifbifconf bifc; 123 struct ifbreq *req; 124 char *inbuf = NULL, *ninbuf; 125 char *p, *pad; 126 int i, len = 8192; 127 128 pad = strdup(prefix); 129 if (pad == NULL) 130 err(1, "strdup"); 131 /* replace the prefix with whitespace */ 132 for (p = pad; *p != '\0'; p++) { 133 if(isprint(*p)) 134 *p = ' '; 135 } 136 137 for (;;) { 138 ninbuf = realloc(inbuf, len); 139 if (ninbuf == NULL) 140 err(1, "unable to allocate interface buffer"); 141 bifc.ifbic_len = len; 142 bifc.ifbic_buf = inbuf = ninbuf; 143 if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0) 144 err(1, "unable to get interface list"); 145 if ((bifc.ifbic_len + sizeof(*req)) < len) 146 break; 147 len *= 2; 148 } 149 150 for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) { 151 req = bifc.ifbic_req + i; 152 printf("%s%s ", prefix, req->ifbr_ifsname); 153 printb("flags", req->ifbr_ifsflags, IFBIFBITS); 154 printf("\n"); 155 156 if (req->ifbr_ifsflags & IFBIF_STP) { 157 printf("%s", pad); 158 printf("port %u priority %u", 159 req->ifbr_portno, req->ifbr_priority); 160 printf(" pathcost %u", req->ifbr_path_cost); 161 if (req->ifbr_state < 162 sizeof(stpstates) / sizeof(stpstates[0])) 163 printf(" %s", stpstates[req->ifbr_state]); 164 else 165 printf(" <unknown state %d>", 166 req->ifbr_state); 167 printf("\n"); 168 printf("%sbondweight %u\n", 169 pad, req->ifbr_bond_weight); 170 printf("%sdesignated root: %016jx\n", 171 pad, (intmax_t)req->ifbr_designated_root); 172 printf("%sdesignated bridge: %016jx\n", 173 pad, (intmax_t)req->ifbr_designated_bridge); 174 printf("%sdesignated cost: %u\n", 175 pad, req->ifbr_designated_cost); 176 printf("%sdesignated port: %u\n", 177 pad, req->ifbr_designated_port); 178 if (verbose) { 179 printf("%speer root: %016jx\n", 180 pad, (intmax_t)req->ifbr_peer_root); 181 printf("%speer bridge: %016jx\n", 182 pad, (intmax_t)req->ifbr_peer_bridge); 183 printf("%speer cost: %u\n", 184 pad, req->ifbr_peer_cost); 185 printf("%speer port: %u\n", 186 pad, req->ifbr_peer_port); 187 } 188 } 189 } 190 191 free(inbuf); 192 } 193 194 static void 195 bridge_addresses(int s, const char *prefix) 196 { 197 struct ifbaconf ifbac; 198 struct ifbareq *ifba; 199 char *inbuf = NULL, *ninbuf; 200 int i, len = 8192; 201 struct ether_addr ea; 202 203 for (;;) { 204 ninbuf = realloc(inbuf, len); 205 if (ninbuf == NULL) 206 err(1, "unable to allocate address buffer"); 207 ifbac.ifbac_len = len; 208 ifbac.ifbac_buf = inbuf = ninbuf; 209 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0) 210 err(1, "unable to get address cache"); 211 if ((ifbac.ifbac_len + sizeof(*ifba)) < len) 212 break; 213 len *= 2; 214 } 215 216 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) { 217 ifba = ifbac.ifbac_req + i; 218 memcpy(ea.octet, ifba->ifba_dst, 219 sizeof(ea.octet)); 220 printf("%s%s %s %lu ", prefix, ether_ntoa(&ea), 221 ifba->ifba_ifsname, ifba->ifba_expire); 222 printb("flags", ifba->ifba_flags, IFBAFBITS); 223 printf("\n"); 224 } 225 226 free(inbuf); 227 } 228 229 static void 230 bridge_status(int s) 231 { 232 struct ifbrparam param; 233 u_int16_t pri; 234 u_int8_t ht, fd, ma; 235 236 if (do_cmd(s, BRDGGPRI, ¶m, sizeof(param), 0) < 0) 237 return; 238 pri = param.ifbrp_prio; 239 240 if (do_cmd(s, BRDGGHT, ¶m, sizeof(param), 0) < 0) 241 return; 242 ht = param.ifbrp_hellotime; 243 244 if (do_cmd(s, BRDGGFD, ¶m, sizeof(param), 0) < 0) 245 return; 246 fd = param.ifbrp_fwddelay; 247 248 if (do_cmd(s, BRDGGMA, ¶m, sizeof(param), 0) < 0) 249 return; 250 ma = param.ifbrp_maxage; 251 252 printf("\tpriority %u hellotime %u fwddelay %u maxage %u\n", 253 pri, ht, fd, ma); 254 255 bridge_interfaces(s, "\tmember: "); 256 257 return; 258 259 } 260 261 static void 262 setbridge_add(const char *val, int d, int s, const struct afswtch *afp) 263 { 264 struct ifbreq req; 265 266 memset(&req, 0, sizeof(req)); 267 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname)); 268 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0) 269 err(1, "BRDGADD %s", val); 270 } 271 272 static void 273 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp) 274 { 275 struct ifbreq req; 276 277 memset(&req, 0, sizeof(req)); 278 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname)); 279 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0) 280 err(1, "BRDGDEL %s", val); 281 } 282 283 static void 284 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp) 285 { 286 287 do_bridgeflag(s, val, IFBIF_DISCOVER, 1); 288 } 289 290 static void 291 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp) 292 { 293 294 do_bridgeflag(s, val, IFBIF_DISCOVER, 0); 295 } 296 297 static void 298 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp) 299 { 300 301 do_bridgeflag(s, val, IFBIF_LEARNING, 1); 302 } 303 304 static void 305 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp) 306 { 307 308 do_bridgeflag(s, val, IFBIF_LEARNING, 0); 309 } 310 311 static void 312 setbridge_span(const char *val, int d, int s, const struct afswtch *afp) 313 { 314 struct ifbreq req; 315 316 memset(&req, 0, sizeof(req)); 317 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname)); 318 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0) 319 err(1, "BRDGADDS %s", val); 320 } 321 322 static void 323 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp) 324 { 325 struct ifbreq req; 326 327 memset(&req, 0, sizeof(req)); 328 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname)); 329 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0) 330 err(1, "BRDGDELS %s", val); 331 } 332 333 static void 334 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp) 335 { 336 337 do_bridgeflag(s, val, IFBIF_STP, 1); 338 } 339 340 static void 341 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp) 342 { 343 344 do_bridgeflag(s, val, IFBIF_STP, 0); 345 } 346 347 static void 348 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp) 349 { 350 struct ifbreq req; 351 352 memset(&req, 0, sizeof(req)); 353 req.ifbr_ifsflags = IFBF_FLUSHDYN; 354 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0) 355 err(1, "BRDGFLUSH"); 356 } 357 358 static void 359 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp) 360 { 361 struct ifbreq req; 362 363 memset(&req, 0, sizeof(req)); 364 req.ifbr_ifsflags = IFBF_FLUSHALL; 365 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0) 366 err(1, "BRDGFLUSH"); 367 } 368 369 static void 370 setbridge_static(const char *val, const char *mac, int s, 371 const struct afswtch *afp) 372 { 373 struct ifbareq req; 374 struct ether_addr *ea; 375 376 memset(&req, 0, sizeof(req)); 377 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname)); 378 379 ea = ether_aton(mac); 380 if (ea == NULL) 381 errx(1, "%s: invalid address: %s", val, mac); 382 383 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst)); 384 req.ifba_flags = IFBAF_STATIC; 385 386 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0) 387 err(1, "BRDGSADDR %s", val); 388 } 389 390 static void 391 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp) 392 { 393 struct ifbareq req; 394 struct ether_addr *ea; 395 396 memset(&req, 0, sizeof(req)); 397 398 ea = ether_aton(val); 399 if (ea == NULL) 400 errx(1, "invalid address: %s", val); 401 402 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst)); 403 404 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0) 405 err(1, "BRDGDADDR %s", val); 406 } 407 408 static void 409 getbridge_addr(const char *val, int d, int s, const struct afswtch *afp) 410 { 411 bridge_addresses(s, ""); 412 } 413 414 static void 415 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp) 416 { 417 struct ifbrparam param; 418 u_long val; 419 420 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0) 421 errx(1, "invalid value: %s", arg); 422 423 param.ifbrp_csize = val & 0xffffffff; 424 425 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0) 426 err(1, "BRDGSCACHE %s", arg); 427 } 428 429 static void 430 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp) 431 { 432 struct ifbrparam param; 433 u_long val; 434 435 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0) 436 errx(1, "invalid value: %s", arg); 437 438 param.ifbrp_hellotime = val & 0xff; 439 440 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0) 441 err(1, "BRDGSHT %s", arg); 442 } 443 444 static void 445 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp) 446 { 447 struct ifbrparam param; 448 u_long val; 449 450 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0) 451 errx(1, "invalid value: %s", arg); 452 453 param.ifbrp_fwddelay = val & 0xff; 454 455 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0) 456 err(1, "BRDGSFD %s", arg); 457 } 458 459 static void 460 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp) 461 { 462 struct ifbrparam param; 463 u_long val; 464 465 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0) 466 errx(1, "invalid value: %s", arg); 467 468 param.ifbrp_maxage = val & 0xff; 469 470 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0) 471 err(1, "BRDGSMA %s", arg); 472 } 473 474 static void 475 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp) 476 { 477 struct ifbrparam param; 478 u_long val; 479 480 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0) 481 errx(1, "invalid value: %s", arg); 482 483 param.ifbrp_prio = val & 0xffff; 484 485 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0) 486 err(1, "BRDGSPRI %s", arg); 487 } 488 489 static void 490 setbridge_ifpriority(const char *ifn, const char *pri, int s, 491 const struct afswtch *afp) 492 { 493 struct ifbreq req; 494 u_long val; 495 496 memset(&req, 0, sizeof(req)); 497 498 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0) 499 errx(1, "invalid value: %s", pri); 500 501 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname)); 502 req.ifbr_priority = val & 0xff; 503 504 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0) 505 err(1, "BRDGSIFPRIO %s", pri); 506 } 507 508 static void 509 setbridge_ifpathcost(const char *ifn, const char *cost, int s, 510 const struct afswtch *afp) 511 { 512 struct ifbreq req; 513 u_long val; 514 515 memset(&req, 0, sizeof(req)); 516 517 if (get_val(cost, &val) < 0 || (val & ~0xff) != 0) 518 errx(1, "invalid value: %s", cost); 519 520 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname)); 521 req.ifbr_path_cost = val & 0xffff; 522 523 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0) 524 err(1, "BRDGSIFCOST %s", cost); 525 } 526 527 static void 528 setbridge_ifbondweight(const char *ifn, const char *cost, int s, 529 const struct afswtch *afp) 530 { 531 struct ifbreq req; 532 u_long val; 533 534 memset(&req, 0, sizeof(req)); 535 536 if (get_val(cost, &val) < 0 || (val & ~0xff) != 0) 537 errx(1, "invalid value: %s", cost); 538 539 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname)); 540 if (val > 65535) 541 val = 65535; 542 req.ifbr_bond_weight = (uint16_t)val; 543 544 if (do_cmd(s, BRDGSBONDWGHT, &req, sizeof(req), 1) < 0) 545 err(1, "BRDGSBONDWGHT %s", cost); 546 } 547 548 static void 549 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp) 550 { 551 struct ifbrparam param; 552 u_long val; 553 554 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0) 555 errx(1, "invalid value: %s", arg); 556 557 param.ifbrp_ctime = val & 0xffffffff; 558 559 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0) 560 err(1, "BRDGSTO %s", arg); 561 } 562 563 static struct cmd bridge_cmds[] = { 564 DEF_CMD_ARG("addm", setbridge_add), 565 DEF_CMD_ARG("deletem", setbridge_delete), 566 DEF_CMD_ARG("discover", setbridge_discover), 567 DEF_CMD_ARG("-discover", unsetbridge_discover), 568 DEF_CMD_ARG("learn", setbridge_learn), 569 DEF_CMD_ARG("-learn", unsetbridge_learn), 570 DEF_CMD_ARG("span", setbridge_span), 571 DEF_CMD_ARG("-span", unsetbridge_span), 572 DEF_CMD_ARG("stp", setbridge_stp), 573 DEF_CMD_ARG("-stp", unsetbridge_stp), 574 DEF_CMD("flush", 0, setbridge_flush), 575 DEF_CMD("flushall", 0, setbridge_flushall), 576 DEF_CMD_ARG2("static", setbridge_static), 577 DEF_CMD_ARG("deladdr", setbridge_deladdr), 578 DEF_CMD("addr", 1, getbridge_addr), 579 DEF_CMD_ARG("maxaddr", setbridge_maxaddr), 580 DEF_CMD_ARG("hellotime", setbridge_hellotime), 581 DEF_CMD_ARG("fwddelay", setbridge_fwddelay), 582 DEF_CMD_ARG("maxage", setbridge_maxage), 583 DEF_CMD_ARG("priority", setbridge_priority), 584 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority), 585 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost), 586 DEF_CMD_ARG2("ifbondweight", setbridge_ifbondweight), 587 DEF_CMD_ARG("timeout", setbridge_timeout), 588 }; 589 static struct afswtch af_bridge = { 590 .af_name = "af_bridge", 591 .af_af = AF_UNSPEC, 592 .af_other_status = bridge_status, 593 }; 594 595 static __constructor(101) void 596 bridge_ctor(void) 597 { 598 #define N(a) (sizeof(a) / sizeof(a[0])) 599 int i; 600 601 for (i = 0; i < N(bridge_cmds); i++) 602 cmd_register(&bridge_cmds[i]); 603 af_register(&af_bridge); 604 #undef N 605 } 606