2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright 2001 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 static const char rcsid[] =
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
51 #include <net/ethernet.h>
53 #include <net/if_bridgevar.h>
54 #include <net/route.h>
66 #define PV2ID(pv, epri, eaddr) do { \
68 eaddr[0] = pv >> 40; \
69 eaddr[1] = pv >> 32; \
70 eaddr[2] = pv >> 24; \
71 eaddr[3] = pv >> 16; \
76 static const char *stpstates[] = {
84 static const char *stpproto[] = {
89 static const char *stproles[] = {
98 get_val(const char *cp, u_long *valp)
104 val = strtoul(cp, &endptr, 0);
105 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
113 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
117 memset(&ifd, 0, sizeof(ifd));
119 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
121 ifd.ifd_len = argsize;
124 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
128 do_bridgeflag(int sock, const char *ifs, int flag, int set)
132 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
134 if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
135 err(1, "unable to get bridge flags");
138 req.ifbr_ifsflags |= flag;
140 req.ifbr_ifsflags &= ~flag;
142 if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
143 err(1, "unable to set bridge flags");
147 bridge_interfaces(int s, const char *prefix)
149 struct ifbifconf bifc;
151 char *inbuf = NULL, *ninbuf;
155 pad = strdup(prefix);
158 /* replace the prefix with whitespace */
159 for (p = pad; *p != '\0'; p++) {
165 ninbuf = realloc(inbuf, len);
167 err(1, "unable to allocate interface buffer");
168 bifc.ifbic_len = len;
169 bifc.ifbic_buf = inbuf = ninbuf;
170 if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
171 err(1, "unable to get interface list");
172 if ((bifc.ifbic_len + sizeof(*req)) < len)
177 for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
178 req = bifc.ifbic_req + i;
179 printf("%s%s ", prefix, req->ifbr_ifsname);
180 printb("flags", req->ifbr_ifsflags, IFBIFBITS);
184 printf("ifmaxaddr %u", req->ifbr_addrmax);
185 printf(" port %u priority %u", req->ifbr_portno,
187 printf(" path cost %u", req->ifbr_path_cost);
189 if (req->ifbr_ifsflags & IFBIF_STP) {
190 if (req->ifbr_proto < nitems(stpproto))
191 printf(" proto %s", stpproto[req->ifbr_proto]);
193 printf(" <unknown proto %d>",
197 if (req->ifbr_role < nitems(stproles))
198 printf("role %s", stproles[req->ifbr_role]);
200 printf("<unknown role %d>",
202 if (req->ifbr_state < nitems(stpstates))
203 printf(" state %s", stpstates[req->ifbr_state]);
205 printf(" <unknown state %d>",
215 bridge_addresses(int s, const char *prefix)
217 struct ifbaconf ifbac;
218 struct ifbareq *ifba;
219 char *inbuf = NULL, *ninbuf;
221 struct ether_addr ea;
224 ninbuf = realloc(inbuf, len);
226 err(1, "unable to allocate address buffer");
227 ifbac.ifbac_len = len;
228 ifbac.ifbac_buf = inbuf = ninbuf;
229 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
230 err(1, "unable to get address cache");
231 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
236 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
237 ifba = ifbac.ifbac_req + i;
238 memcpy(ea.octet, ifba->ifba_dst,
240 printf("%s%s Vlan%d %s %lu ", prefix, ether_ntoa(&ea),
241 ifba->ifba_vlan, ifba->ifba_ifsname, ifba->ifba_expire);
242 printb("flags", ifba->ifba_flags, IFBAFBITS);
252 struct ifbropreq ifbp;
253 struct ifbrparam param;
255 u_int8_t ht, fd, ma, hc, pro;
256 u_int8_t lladdr[ETHER_ADDR_LEN];
258 u_int32_t csize, ctime;
260 if (do_cmd(s, BRDGGCACHE, ¶m, sizeof(param), 0) < 0)
262 csize = param.ifbrp_csize;
263 if (do_cmd(s, BRDGGTO, ¶m, sizeof(param), 0) < 0)
265 ctime = param.ifbrp_ctime;
266 if (do_cmd(s, BRDGPARAM, &ifbp, sizeof(ifbp), 0) < 0)
268 pri = ifbp.ifbop_priority;
269 pro = ifbp.ifbop_protocol;
270 ht = ifbp.ifbop_hellotime;
271 fd = ifbp.ifbop_fwddelay;
272 hc = ifbp.ifbop_holdcount;
273 ma = ifbp.ifbop_maxage;
275 PV2ID(ifbp.ifbop_bridgeid, bprio, lladdr);
276 printf("\tid %s priority %u hellotime %u fwddelay %u\n",
277 ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
278 printf("\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
279 ma, hc, stpproto[pro], csize, ctime);
281 PV2ID(ifbp.ifbop_designated_root, bprio, lladdr);
282 printf("\troot id %s priority %d ifcost %u port %u\n",
283 ether_ntoa((struct ether_addr *)lladdr), bprio,
284 ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
286 bridge_interfaces(s, "\tmember: ");
293 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
297 memset(&req, 0, sizeof(req));
298 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
299 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
300 err(1, "BRDGADD %s", val);
304 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
308 memset(&req, 0, sizeof(req));
309 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
310 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
311 err(1, "BRDGDEL %s", val);
315 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
318 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
322 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
325 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
329 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
332 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
336 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
339 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
343 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
346 do_bridgeflag(s, val, IFBIF_STICKY, 1);
350 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
353 do_bridgeflag(s, val, IFBIF_STICKY, 0);
357 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
361 memset(&req, 0, sizeof(req));
362 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
363 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
364 err(1, "BRDGADDS %s", val);
368 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
372 memset(&req, 0, sizeof(req));
373 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
374 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
375 err(1, "BRDGDELS %s", val);
379 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
382 do_bridgeflag(s, val, IFBIF_STP, 1);
386 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
389 do_bridgeflag(s, val, IFBIF_STP, 0);
393 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
395 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
399 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
401 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
405 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
407 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
411 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
413 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
417 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
419 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
423 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
425 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
429 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
431 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
435 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
437 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
441 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
445 memset(&req, 0, sizeof(req));
446 req.ifbr_ifsflags = IFBF_FLUSHDYN;
447 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
452 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
456 memset(&req, 0, sizeof(req));
457 req.ifbr_ifsflags = IFBF_FLUSHALL;
458 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
463 setbridge_static(const char *val, const char *mac, int s,
464 const struct afswtch *afp)
467 struct ether_addr *ea;
469 memset(&req, 0, sizeof(req));
470 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
472 ea = ether_aton(mac);
474 errx(1, "%s: invalid address: %s", val, mac);
476 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
477 req.ifba_flags = IFBAF_STATIC;
478 req.ifba_vlan = 1; /* XXX allow user to specify */
480 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
481 err(1, "BRDGSADDR %s", val);
485 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
488 struct ether_addr *ea;
490 memset(&req, 0, sizeof(req));
492 ea = ether_aton(val);
494 errx(1, "invalid address: %s", val);
496 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
498 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
499 err(1, "BRDGDADDR %s", val);
503 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
506 bridge_addresses(s, "");
510 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
512 struct ifbrparam param;
515 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
516 errx(1, "invalid value: %s", arg);
518 param.ifbrp_csize = val & 0xffffffff;
520 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
521 err(1, "BRDGSCACHE %s", arg);
525 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
527 struct ifbrparam param;
530 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
531 errx(1, "invalid value: %s", arg);
533 param.ifbrp_hellotime = val & 0xff;
535 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0)
536 err(1, "BRDGSHT %s", arg);
540 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
542 struct ifbrparam param;
545 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
546 errx(1, "invalid value: %s", arg);
548 param.ifbrp_fwddelay = val & 0xff;
550 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0)
551 err(1, "BRDGSFD %s", arg);
555 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
557 struct ifbrparam param;
560 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
561 errx(1, "invalid value: %s", arg);
563 param.ifbrp_maxage = val & 0xff;
565 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0)
566 err(1, "BRDGSMA %s", arg);
570 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
572 struct ifbrparam param;
575 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
576 errx(1, "invalid value: %s", arg);
578 param.ifbrp_prio = val & 0xffff;
580 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
581 err(1, "BRDGSPRI %s", arg);
585 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
587 struct ifbrparam param;
589 if (strcasecmp(arg, "stp") == 0) {
590 param.ifbrp_proto = 0;
591 } else if (strcasecmp(arg, "rstp") == 0) {
592 param.ifbrp_proto = 2;
594 errx(1, "unknown stp protocol");
597 if (do_cmd(s, BRDGSPROTO, ¶m, sizeof(param), 1) < 0)
598 err(1, "BRDGSPROTO %s", arg);
602 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
604 struct ifbrparam param;
607 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
608 errx(1, "invalid value: %s", arg);
610 param.ifbrp_txhc = val & 0xff;
612 if (do_cmd(s, BRDGSTXHC, ¶m, sizeof(param), 1) < 0)
613 err(1, "BRDGSTXHC %s", arg);
617 setbridge_ifpriority(const char *ifn, const char *pri, int s,
618 const struct afswtch *afp)
623 memset(&req, 0, sizeof(req));
625 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
626 errx(1, "invalid value: %s", pri);
628 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
629 req.ifbr_priority = val & 0xff;
631 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
632 err(1, "BRDGSIFPRIO %s", pri);
636 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
637 const struct afswtch *afp)
642 memset(&req, 0, sizeof(req));
644 if (get_val(cost, &val) < 0)
645 errx(1, "invalid value: %s", cost);
647 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
648 req.ifbr_path_cost = val;
650 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
651 err(1, "BRDGSIFCOST %s", cost);
655 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
656 const struct afswtch *afp)
661 memset(&req, 0, sizeof(req));
663 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
664 errx(1, "invalid value: %s", arg);
666 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
667 req.ifbr_addrmax = val & 0xffffffff;
669 if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
670 err(1, "BRDGSIFAMAX %s", arg);
674 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
676 struct ifbrparam param;
679 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
680 errx(1, "invalid value: %s", arg);
682 param.ifbrp_ctime = val & 0xffffffff;
684 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0)
685 err(1, "BRDGSTO %s", arg);
689 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
692 do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
696 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
699 do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
702 static struct cmd bridge_cmds[] = {
703 DEF_CMD_ARG("addm", setbridge_add),
704 DEF_CMD_ARG("deletem", setbridge_delete),
705 DEF_CMD_ARG("discover", setbridge_discover),
706 DEF_CMD_ARG("-discover", unsetbridge_discover),
707 DEF_CMD_ARG("learn", setbridge_learn),
708 DEF_CMD_ARG("-learn", unsetbridge_learn),
709 DEF_CMD_ARG("sticky", setbridge_sticky),
710 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
711 DEF_CMD_ARG("span", setbridge_span),
712 DEF_CMD_ARG("-span", unsetbridge_span),
713 DEF_CMD_ARG("stp", setbridge_stp),
714 DEF_CMD_ARG("-stp", unsetbridge_stp),
715 DEF_CMD_ARG("edge", setbridge_edge),
716 DEF_CMD_ARG("-edge", unsetbridge_edge),
717 DEF_CMD_ARG("autoedge", setbridge_autoedge),
718 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
719 DEF_CMD_ARG("ptp", setbridge_ptp),
720 DEF_CMD_ARG("-ptp", unsetbridge_ptp),
721 DEF_CMD_ARG("autoptp", setbridge_autoptp),
722 DEF_CMD_ARG("-autoptp", unsetbridge_autoptp),
723 DEF_CMD("flush", 0, setbridge_flush),
724 DEF_CMD("flushall", 0, setbridge_flushall),
725 DEF_CMD_ARG2("static", setbridge_static),
726 DEF_CMD_ARG("deladdr", setbridge_deladdr),
727 DEF_CMD("addr", 1, setbridge_addr),
728 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
729 DEF_CMD_ARG("hellotime", setbridge_hellotime),
730 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
731 DEF_CMD_ARG("maxage", setbridge_maxage),
732 DEF_CMD_ARG("priority", setbridge_priority),
733 DEF_CMD_ARG("proto", setbridge_protocol),
734 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
735 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
736 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
737 DEF_CMD_ARG2("ifmaxaddr", setbridge_ifmaxaddr),
738 DEF_CMD_ARG("timeout", setbridge_timeout),
739 DEF_CMD_ARG("private", setbridge_private),
740 DEF_CMD_ARG("-private", unsetbridge_private),
742 static struct afswtch af_bridge = {
743 .af_name = "af_bridge",
745 .af_other_status = bridge_status,
748 static __constructor void
753 for (i = 0; i < nitems(bridge_cmds); i++)
754 cmd_register(&bridge_cmds[i]);
755 af_register(&af_bridge);