2 * Copyright 2001 Wasabi Systems, Inc.
5 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
37 static const char rcsid[] =
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
49 #include <net/ethernet.h>
51 #include <net/if_bridgevar.h>
52 #include <net/route.h>
64 #define PV2ID(pv, epri, eaddr) do { \
66 eaddr[0] = pv >> 40; \
67 eaddr[1] = pv >> 32; \
68 eaddr[2] = pv >> 24; \
69 eaddr[3] = pv >> 16; \
74 static const char *stpstates[] = {
82 static const char *stpproto[] = {
87 static const char *stproles[] = {
96 get_val(const char *cp, u_long *valp)
102 val = strtoul(cp, &endptr, 0);
103 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
111 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
115 memset(&ifd, 0, sizeof(ifd));
117 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
119 ifd.ifd_len = argsize;
122 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
126 do_bridgeflag(int sock, const char *ifs, int flag, int set)
130 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
132 if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
133 err(1, "unable to get bridge flags");
136 req.ifbr_ifsflags |= flag;
138 req.ifbr_ifsflags &= ~flag;
140 if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
141 err(1, "unable to set bridge flags");
145 bridge_interfaces(int s, const char *prefix)
147 struct ifbifconf bifc;
149 char *inbuf = NULL, *ninbuf;
153 pad = strdup(prefix);
156 /* replace the prefix with whitespace */
157 for (p = pad; *p != '\0'; p++) {
163 ninbuf = realloc(inbuf, len);
165 err(1, "unable to allocate interface buffer");
166 bifc.ifbic_len = len;
167 bifc.ifbic_buf = inbuf = ninbuf;
168 if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
169 err(1, "unable to get interface list");
170 if ((bifc.ifbic_len + sizeof(*req)) < len)
175 for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
176 req = bifc.ifbic_req + i;
177 printf("%s%s ", prefix, req->ifbr_ifsname);
178 printb("flags", req->ifbr_ifsflags, IFBIFBITS);
182 printf("ifmaxaddr %u", req->ifbr_addrmax);
183 printf(" port %u priority %u", req->ifbr_portno,
185 printf(" path cost %u", req->ifbr_path_cost);
187 if (req->ifbr_ifsflags & IFBIF_STP) {
188 if (req->ifbr_proto < nitems(stpproto))
189 printf(" proto %s", stpproto[req->ifbr_proto]);
191 printf(" <unknown proto %d>",
195 if (req->ifbr_role < nitems(stproles))
196 printf("role %s", stproles[req->ifbr_role]);
198 printf("<unknown role %d>",
200 if (req->ifbr_state < nitems(stpstates))
201 printf(" state %s", stpstates[req->ifbr_state]);
203 printf(" <unknown state %d>",
213 bridge_addresses(int s, const char *prefix)
215 struct ifbaconf ifbac;
216 struct ifbareq *ifba;
217 char *inbuf = NULL, *ninbuf;
219 struct ether_addr ea;
222 ninbuf = realloc(inbuf, len);
224 err(1, "unable to allocate address buffer");
225 ifbac.ifbac_len = len;
226 ifbac.ifbac_buf = inbuf = ninbuf;
227 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
228 err(1, "unable to get address cache");
229 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
234 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
235 ifba = ifbac.ifbac_req + i;
236 memcpy(ea.octet, ifba->ifba_dst,
238 printf("%s%s Vlan%d %s %lu ", prefix, ether_ntoa(&ea),
239 ifba->ifba_vlan, ifba->ifba_ifsname, ifba->ifba_expire);
240 printb("flags", ifba->ifba_flags, IFBAFBITS);
250 struct ifbropreq ifbp;
251 struct ifbrparam param;
253 u_int8_t ht, fd, ma, hc, pro;
254 u_int8_t lladdr[ETHER_ADDR_LEN];
256 u_int32_t csize, ctime;
258 if (do_cmd(s, BRDGGCACHE, ¶m, sizeof(param), 0) < 0)
260 csize = param.ifbrp_csize;
261 if (do_cmd(s, BRDGGTO, ¶m, sizeof(param), 0) < 0)
263 ctime = param.ifbrp_ctime;
264 if (do_cmd(s, BRDGPARAM, &ifbp, sizeof(ifbp), 0) < 0)
266 pri = ifbp.ifbop_priority;
267 pro = ifbp.ifbop_protocol;
268 ht = ifbp.ifbop_hellotime;
269 fd = ifbp.ifbop_fwddelay;
270 hc = ifbp.ifbop_holdcount;
271 ma = ifbp.ifbop_maxage;
273 PV2ID(ifbp.ifbop_bridgeid, bprio, lladdr);
274 printf("\tid %s priority %u hellotime %u fwddelay %u\n",
275 ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
276 printf("\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
277 ma, hc, stpproto[pro], csize, ctime);
279 PV2ID(ifbp.ifbop_designated_root, bprio, lladdr);
280 printf("\troot id %s priority %d ifcost %u port %u\n",
281 ether_ntoa((struct ether_addr *)lladdr), bprio,
282 ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
284 bridge_interfaces(s, "\tmember: ");
291 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
295 memset(&req, 0, sizeof(req));
296 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
297 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
298 err(1, "BRDGADD %s", val);
302 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
306 memset(&req, 0, sizeof(req));
307 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
308 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
309 err(1, "BRDGDEL %s", val);
313 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
316 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
320 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
323 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
327 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
330 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
334 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
337 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
341 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
344 do_bridgeflag(s, val, IFBIF_STICKY, 1);
348 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
351 do_bridgeflag(s, val, IFBIF_STICKY, 0);
355 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
359 memset(&req, 0, sizeof(req));
360 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
361 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
362 err(1, "BRDGADDS %s", val);
366 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
370 memset(&req, 0, sizeof(req));
371 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
372 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
373 err(1, "BRDGDELS %s", val);
377 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
380 do_bridgeflag(s, val, IFBIF_STP, 1);
384 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
387 do_bridgeflag(s, val, IFBIF_STP, 0);
391 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
393 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
397 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
399 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
403 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
405 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
409 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
411 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
415 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
417 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
421 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
423 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
427 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
429 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
433 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
435 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
439 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
443 memset(&req, 0, sizeof(req));
444 req.ifbr_ifsflags = IFBF_FLUSHDYN;
445 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
450 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
454 memset(&req, 0, sizeof(req));
455 req.ifbr_ifsflags = IFBF_FLUSHALL;
456 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
461 setbridge_static(const char *val, const char *mac, int s,
462 const struct afswtch *afp)
465 struct ether_addr *ea;
467 memset(&req, 0, sizeof(req));
468 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
470 ea = ether_aton(mac);
472 errx(1, "%s: invalid address: %s", val, mac);
474 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
475 req.ifba_flags = IFBAF_STATIC;
476 req.ifba_vlan = 1; /* XXX allow user to specify */
478 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
479 err(1, "BRDGSADDR %s", val);
483 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
486 struct ether_addr *ea;
488 memset(&req, 0, sizeof(req));
490 ea = ether_aton(val);
492 errx(1, "invalid address: %s", val);
494 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
496 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
497 err(1, "BRDGDADDR %s", val);
501 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
504 bridge_addresses(s, "");
508 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
510 struct ifbrparam param;
513 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
514 errx(1, "invalid value: %s", arg);
516 param.ifbrp_csize = val & 0xffffffff;
518 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
519 err(1, "BRDGSCACHE %s", arg);
523 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
525 struct ifbrparam param;
528 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
529 errx(1, "invalid value: %s", arg);
531 param.ifbrp_hellotime = val & 0xff;
533 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0)
534 err(1, "BRDGSHT %s", arg);
538 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
540 struct ifbrparam param;
543 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
544 errx(1, "invalid value: %s", arg);
546 param.ifbrp_fwddelay = val & 0xff;
548 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0)
549 err(1, "BRDGSFD %s", arg);
553 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
555 struct ifbrparam param;
558 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
559 errx(1, "invalid value: %s", arg);
561 param.ifbrp_maxage = val & 0xff;
563 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0)
564 err(1, "BRDGSMA %s", arg);
568 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
570 struct ifbrparam param;
573 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
574 errx(1, "invalid value: %s", arg);
576 param.ifbrp_prio = val & 0xffff;
578 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
579 err(1, "BRDGSPRI %s", arg);
583 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
585 struct ifbrparam param;
587 if (strcasecmp(arg, "stp") == 0) {
588 param.ifbrp_proto = 0;
589 } else if (strcasecmp(arg, "rstp") == 0) {
590 param.ifbrp_proto = 2;
592 errx(1, "unknown stp protocol");
595 if (do_cmd(s, BRDGSPROTO, ¶m, sizeof(param), 1) < 0)
596 err(1, "BRDGSPROTO %s", arg);
600 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
602 struct ifbrparam param;
605 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
606 errx(1, "invalid value: %s", arg);
608 param.ifbrp_txhc = val & 0xff;
610 if (do_cmd(s, BRDGSTXHC, ¶m, sizeof(param), 1) < 0)
611 err(1, "BRDGSTXHC %s", arg);
615 setbridge_ifpriority(const char *ifn, const char *pri, int s,
616 const struct afswtch *afp)
621 memset(&req, 0, sizeof(req));
623 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
624 errx(1, "invalid value: %s", pri);
626 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
627 req.ifbr_priority = val & 0xff;
629 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
630 err(1, "BRDGSIFPRIO %s", pri);
634 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
635 const struct afswtch *afp)
640 memset(&req, 0, sizeof(req));
642 if (get_val(cost, &val) < 0)
643 errx(1, "invalid value: %s", cost);
645 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
646 req.ifbr_path_cost = val;
648 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
649 err(1, "BRDGSIFCOST %s", cost);
653 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
654 const struct afswtch *afp)
659 memset(&req, 0, sizeof(req));
661 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
662 errx(1, "invalid value: %s", arg);
664 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
665 req.ifbr_addrmax = val & 0xffffffff;
667 if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
668 err(1, "BRDGSIFAMAX %s", arg);
672 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
674 struct ifbrparam param;
677 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
678 errx(1, "invalid value: %s", arg);
680 param.ifbrp_ctime = val & 0xffffffff;
682 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0)
683 err(1, "BRDGSTO %s", arg);
687 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
690 do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
694 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
697 do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
700 static struct cmd bridge_cmds[] = {
701 DEF_CMD_ARG("addm", setbridge_add),
702 DEF_CMD_ARG("deletem", setbridge_delete),
703 DEF_CMD_ARG("discover", setbridge_discover),
704 DEF_CMD_ARG("-discover", unsetbridge_discover),
705 DEF_CMD_ARG("learn", setbridge_learn),
706 DEF_CMD_ARG("-learn", unsetbridge_learn),
707 DEF_CMD_ARG("sticky", setbridge_sticky),
708 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
709 DEF_CMD_ARG("span", setbridge_span),
710 DEF_CMD_ARG("-span", unsetbridge_span),
711 DEF_CMD_ARG("stp", setbridge_stp),
712 DEF_CMD_ARG("-stp", unsetbridge_stp),
713 DEF_CMD_ARG("edge", setbridge_edge),
714 DEF_CMD_ARG("-edge", unsetbridge_edge),
715 DEF_CMD_ARG("autoedge", setbridge_autoedge),
716 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
717 DEF_CMD_ARG("ptp", setbridge_ptp),
718 DEF_CMD_ARG("-ptp", unsetbridge_ptp),
719 DEF_CMD_ARG("autoptp", setbridge_autoptp),
720 DEF_CMD_ARG("-autoptp", unsetbridge_autoptp),
721 DEF_CMD("flush", 0, setbridge_flush),
722 DEF_CMD("flushall", 0, setbridge_flushall),
723 DEF_CMD_ARG2("static", setbridge_static),
724 DEF_CMD_ARG("deladdr", setbridge_deladdr),
725 DEF_CMD("addr", 1, setbridge_addr),
726 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
727 DEF_CMD_ARG("hellotime", setbridge_hellotime),
728 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
729 DEF_CMD_ARG("maxage", setbridge_maxage),
730 DEF_CMD_ARG("priority", setbridge_priority),
731 DEF_CMD_ARG("proto", setbridge_protocol),
732 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
733 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
734 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
735 DEF_CMD_ARG2("ifmaxaddr", setbridge_ifmaxaddr),
736 DEF_CMD_ARG("timeout", setbridge_timeout),
737 DEF_CMD_ARG("private", setbridge_private),
738 DEF_CMD_ARG("-private", unsetbridge_private),
740 static struct afswtch af_bridge = {
741 .af_name = "af_bridge",
743 .af_other_status = bridge_status,
746 static __constructor void
751 for (i = 0; i < nitems(bridge_cmds); i++)
752 cmd_register(&bridge_cmds[i]);
753 af_register(&af_bridge);