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>
64 #include <libifconfig.h>
68 static const char *stpstates[] = { STP_STATES };
69 static const char *stpproto[] = { STP_PROTOS };
70 static const char *stproles[] = { STP_ROLES };
73 get_val(const char *cp, u_long *valp)
79 val = strtoul(cp, &endptr, 0);
80 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
88 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
92 memset(&ifd, 0, sizeof(ifd));
94 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
96 ifd.ifd_len = argsize;
99 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
103 do_bridgeflag(int sock, const char *ifs, int flag, int set)
107 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
109 if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
110 err(1, "unable to get bridge flags");
113 req.ifbr_ifsflags |= flag;
115 req.ifbr_ifsflags &= ~flag;
117 if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
118 err(1, "unable to set bridge flags");
122 bridge_addresses(int s, const char *prefix)
124 struct ifbaconf ifbac;
125 struct ifbareq *ifba;
126 char *inbuf = NULL, *ninbuf;
128 struct ether_addr ea;
131 ninbuf = realloc(inbuf, len);
133 err(1, "unable to allocate address buffer");
134 ifbac.ifbac_len = len;
135 ifbac.ifbac_buf = inbuf = ninbuf;
136 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
137 err(1, "unable to get address cache");
138 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
143 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
144 ifba = ifbac.ifbac_req + i;
145 memcpy(ea.octet, ifba->ifba_dst,
147 printf("%s%s Vlan%d %s %lu ", prefix, ether_ntoa(&ea),
148 ifba->ifba_vlan, ifba->ifba_ifsname, ifba->ifba_expire);
149 printb("flags", ifba->ifba_flags, IFBAFBITS);
159 struct ifconfig_bridge_status *bridge;
160 struct ifbropreq *params;
161 const char *pad, *prefix;
162 uint8_t lladdr[ETHER_ADDR_LEN];
165 if (ifconfig_bridge_get_bridge_status(lifh, name, &bridge) == -1)
168 params = bridge->params;
170 PV2ID(params->ifbop_bridgeid, bprio, lladdr);
171 printf("\tid %s priority %u hellotime %u fwddelay %u\n",
172 ether_ntoa((struct ether_addr *)lladdr),
173 params->ifbop_priority,
174 params->ifbop_hellotime,
175 params->ifbop_fwddelay);
176 printf("\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
177 params->ifbop_maxage,
178 params->ifbop_holdcount,
179 stpproto[params->ifbop_protocol],
181 bridge->cache_lifetime);
182 PV2ID(params->ifbop_designated_root, bprio, lladdr);
183 printf("\troot id %s priority %d ifcost %u port %u\n",
184 ether_ntoa((struct ether_addr *)lladdr),
186 params->ifbop_root_path_cost,
187 params->ifbop_root_port & 0xfff);
189 prefix = "\tmember: ";
191 for (size_t i = 0; i < bridge->members_count; ++i) {
192 struct ifbreq *member = &bridge->members[i];
194 printf("%s%s ", prefix, member->ifbr_ifsname);
195 printb("flags", member->ifbr_ifsflags, IFBIFBITS);
197 printf("ifmaxaddr %u port %u priority %u path cost %u",
198 member->ifbr_addrmax,
200 member->ifbr_priority,
201 member->ifbr_path_cost);
202 if (member->ifbr_ifsflags & IFBIF_STP) {
203 uint8_t proto = member->ifbr_proto;
204 uint8_t role = member->ifbr_role;
205 uint8_t state = member->ifbr_state;
207 if (proto < nitems(stpproto))
208 printf(" proto %s", stpproto[proto]);
210 printf(" <unknown proto %d>", proto);
212 if (role < nitems(stproles))
213 printf("role %s", stproles[role]);
215 printf("<unknown role %d>", role);
216 if (state < nitems(stpstates))
217 printf(" state %s", stpstates[state]);
219 printf(" <unknown state %d>", state);
224 ifconfig_bridge_free_bridge_status(bridge);
228 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
232 memset(&req, 0, sizeof(req));
233 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
234 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
235 err(1, "BRDGADD %s", val);
239 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
243 memset(&req, 0, sizeof(req));
244 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
245 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
246 err(1, "BRDGDEL %s", val);
250 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
253 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
257 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
260 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
264 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
267 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
271 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
274 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
278 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
281 do_bridgeflag(s, val, IFBIF_STICKY, 1);
285 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
288 do_bridgeflag(s, val, IFBIF_STICKY, 0);
292 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
296 memset(&req, 0, sizeof(req));
297 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
298 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
299 err(1, "BRDGADDS %s", val);
303 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
307 memset(&req, 0, sizeof(req));
308 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
309 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
310 err(1, "BRDGDELS %s", val);
314 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
317 do_bridgeflag(s, val, IFBIF_STP, 1);
321 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
324 do_bridgeflag(s, val, IFBIF_STP, 0);
328 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
330 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
334 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
336 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
340 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
342 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
346 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
348 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
352 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
354 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
358 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
360 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
364 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
366 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
370 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
372 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
376 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
380 memset(&req, 0, sizeof(req));
381 req.ifbr_ifsflags = IFBF_FLUSHDYN;
382 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
387 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
391 memset(&req, 0, sizeof(req));
392 req.ifbr_ifsflags = IFBF_FLUSHALL;
393 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
398 setbridge_static(const char *val, const char *mac, int s,
399 const struct afswtch *afp)
402 struct ether_addr *ea;
404 memset(&req, 0, sizeof(req));
405 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
407 ea = ether_aton(mac);
409 errx(1, "%s: invalid address: %s", val, mac);
411 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
412 req.ifba_flags = IFBAF_STATIC;
413 req.ifba_vlan = 1; /* XXX allow user to specify */
415 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
416 err(1, "BRDGSADDR %s", val);
420 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
423 struct ether_addr *ea;
425 memset(&req, 0, sizeof(req));
427 ea = ether_aton(val);
429 errx(1, "invalid address: %s", val);
431 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
433 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
434 err(1, "BRDGDADDR %s", val);
438 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
441 bridge_addresses(s, "");
445 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
447 struct ifbrparam param;
450 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
451 errx(1, "invalid value: %s", arg);
453 param.ifbrp_csize = val & 0xffffffff;
455 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
456 err(1, "BRDGSCACHE %s", arg);
460 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
462 struct ifbrparam param;
465 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
466 errx(1, "invalid value: %s", arg);
468 param.ifbrp_hellotime = val & 0xff;
470 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0)
471 err(1, "BRDGSHT %s", arg);
475 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
477 struct ifbrparam param;
480 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
481 errx(1, "invalid value: %s", arg);
483 param.ifbrp_fwddelay = val & 0xff;
485 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0)
486 err(1, "BRDGSFD %s", arg);
490 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
492 struct ifbrparam param;
495 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
496 errx(1, "invalid value: %s", arg);
498 param.ifbrp_maxage = val & 0xff;
500 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0)
501 err(1, "BRDGSMA %s", arg);
505 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
507 struct ifbrparam param;
510 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
511 errx(1, "invalid value: %s", arg);
513 param.ifbrp_prio = val & 0xffff;
515 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
516 err(1, "BRDGSPRI %s", arg);
520 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
522 struct ifbrparam param;
524 if (strcasecmp(arg, "stp") == 0) {
525 param.ifbrp_proto = 0;
526 } else if (strcasecmp(arg, "rstp") == 0) {
527 param.ifbrp_proto = 2;
529 errx(1, "unknown stp protocol");
532 if (do_cmd(s, BRDGSPROTO, ¶m, sizeof(param), 1) < 0)
533 err(1, "BRDGSPROTO %s", arg);
537 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
539 struct ifbrparam param;
542 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
543 errx(1, "invalid value: %s", arg);
545 param.ifbrp_txhc = val & 0xff;
547 if (do_cmd(s, BRDGSTXHC, ¶m, sizeof(param), 1) < 0)
548 err(1, "BRDGSTXHC %s", arg);
552 setbridge_ifpriority(const char *ifn, const char *pri, int s,
553 const struct afswtch *afp)
558 memset(&req, 0, sizeof(req));
560 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
561 errx(1, "invalid value: %s", pri);
563 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
564 req.ifbr_priority = val & 0xff;
566 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
567 err(1, "BRDGSIFPRIO %s", pri);
571 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
572 const struct afswtch *afp)
577 memset(&req, 0, sizeof(req));
579 if (get_val(cost, &val) < 0)
580 errx(1, "invalid value: %s", cost);
582 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
583 req.ifbr_path_cost = val;
585 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
586 err(1, "BRDGSIFCOST %s", cost);
590 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
591 const struct afswtch *afp)
596 memset(&req, 0, sizeof(req));
598 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
599 errx(1, "invalid value: %s", arg);
601 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
602 req.ifbr_addrmax = val & 0xffffffff;
604 if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
605 err(1, "BRDGSIFAMAX %s", arg);
609 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
611 struct ifbrparam param;
614 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
615 errx(1, "invalid value: %s", arg);
617 param.ifbrp_ctime = val & 0xffffffff;
619 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0)
620 err(1, "BRDGSTO %s", arg);
624 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
627 do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
631 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
634 do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
637 static struct cmd bridge_cmds[] = {
638 DEF_CMD_ARG("addm", setbridge_add),
639 DEF_CMD_ARG("deletem", setbridge_delete),
640 DEF_CMD_ARG("discover", setbridge_discover),
641 DEF_CMD_ARG("-discover", unsetbridge_discover),
642 DEF_CMD_ARG("learn", setbridge_learn),
643 DEF_CMD_ARG("-learn", unsetbridge_learn),
644 DEF_CMD_ARG("sticky", setbridge_sticky),
645 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
646 DEF_CMD_ARG("span", setbridge_span),
647 DEF_CMD_ARG("-span", unsetbridge_span),
648 DEF_CMD_ARG("stp", setbridge_stp),
649 DEF_CMD_ARG("-stp", unsetbridge_stp),
650 DEF_CMD_ARG("edge", setbridge_edge),
651 DEF_CMD_ARG("-edge", unsetbridge_edge),
652 DEF_CMD_ARG("autoedge", setbridge_autoedge),
653 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
654 DEF_CMD_ARG("ptp", setbridge_ptp),
655 DEF_CMD_ARG("-ptp", unsetbridge_ptp),
656 DEF_CMD_ARG("autoptp", setbridge_autoptp),
657 DEF_CMD_ARG("-autoptp", unsetbridge_autoptp),
658 DEF_CMD("flush", 0, setbridge_flush),
659 DEF_CMD("flushall", 0, setbridge_flushall),
660 DEF_CMD_ARG2("static", setbridge_static),
661 DEF_CMD_ARG("deladdr", setbridge_deladdr),
662 DEF_CMD("addr", 1, setbridge_addr),
663 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
664 DEF_CMD_ARG("hellotime", setbridge_hellotime),
665 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
666 DEF_CMD_ARG("maxage", setbridge_maxage),
667 DEF_CMD_ARG("priority", setbridge_priority),
668 DEF_CMD_ARG("proto", setbridge_protocol),
669 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
670 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
671 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
672 DEF_CMD_ARG2("ifmaxaddr", setbridge_ifmaxaddr),
673 DEF_CMD_ARG("timeout", setbridge_timeout),
674 DEF_CMD_ARG("private", setbridge_private),
675 DEF_CMD_ARG("-private", unsetbridge_private),
677 static struct afswtch af_bridge = {
678 .af_name = "af_bridge",
680 .af_other_status = bridge_status,
683 static __constructor void
688 for (i = 0; i < nitems(bridge_cmds); i++)
689 cmd_register(&bridge_cmds[i]);
690 af_register(&af_bridge);