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 <
189 sizeof(stpproto) / sizeof(stpproto[0]))
190 printf(" proto %s", stpproto[req->ifbr_proto]);
192 printf(" <unknown proto %d>",
197 sizeof(stproles) / sizeof(stproles[0]))
198 printf("role %s", stproles[req->ifbr_role]);
200 printf("<unknown role %d>",
202 if (req->ifbr_state <
203 sizeof(stpstates) / sizeof(stpstates[0]))
204 printf(" state %s", stpstates[req->ifbr_state]);
206 printf(" <unknown state %d>",
216 bridge_addresses(int s, const char *prefix)
218 struct ifbaconf ifbac;
219 struct ifbareq *ifba;
220 char *inbuf = NULL, *ninbuf;
222 struct ether_addr ea;
225 ninbuf = realloc(inbuf, len);
227 err(1, "unable to allocate address buffer");
228 ifbac.ifbac_len = len;
229 ifbac.ifbac_buf = inbuf = ninbuf;
230 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
231 err(1, "unable to get address cache");
232 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
237 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
238 ifba = ifbac.ifbac_req + i;
239 memcpy(ea.octet, ifba->ifba_dst,
241 printf("%s%s Vlan%d %s %lu ", prefix, ether_ntoa(&ea),
242 ifba->ifba_vlan, ifba->ifba_ifsname, ifba->ifba_expire);
243 printb("flags", ifba->ifba_flags, IFBAFBITS);
253 struct ifbropreq ifbp;
254 struct ifbrparam param;
256 u_int8_t ht, fd, ma, hc, pro;
257 u_int8_t lladdr[ETHER_ADDR_LEN];
259 u_int32_t csize, ctime;
261 if (do_cmd(s, BRDGGCACHE, ¶m, sizeof(param), 0) < 0)
263 csize = param.ifbrp_csize;
264 if (do_cmd(s, BRDGGTO, ¶m, sizeof(param), 0) < 0)
266 ctime = param.ifbrp_ctime;
267 if (do_cmd(s, BRDGPARAM, &ifbp, sizeof(ifbp), 0) < 0)
269 pri = ifbp.ifbop_priority;
270 pro = ifbp.ifbop_protocol;
271 ht = ifbp.ifbop_hellotime;
272 fd = ifbp.ifbop_fwddelay;
273 hc = ifbp.ifbop_holdcount;
274 ma = ifbp.ifbop_maxage;
276 PV2ID(ifbp.ifbop_bridgeid, bprio, lladdr);
277 printf("\tid %s priority %u hellotime %u fwddelay %u\n",
278 ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
279 printf("\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
280 ma, hc, stpproto[pro], csize, ctime);
282 PV2ID(ifbp.ifbop_designated_root, bprio, lladdr);
283 printf("\troot id %s priority %d ifcost %u port %u\n",
284 ether_ntoa((struct ether_addr *)lladdr), bprio,
285 ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
287 bridge_interfaces(s, "\tmember: ");
294 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
298 memset(&req, 0, sizeof(req));
299 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
300 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
301 err(1, "BRDGADD %s", val);
305 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
309 memset(&req, 0, sizeof(req));
310 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
311 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
312 err(1, "BRDGDEL %s", val);
316 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
319 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
323 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
326 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
330 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
333 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
337 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
340 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
344 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
347 do_bridgeflag(s, val, IFBIF_STICKY, 1);
351 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
354 do_bridgeflag(s, val, IFBIF_STICKY, 0);
358 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
362 memset(&req, 0, sizeof(req));
363 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
364 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
365 err(1, "BRDGADDS %s", val);
369 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
373 memset(&req, 0, sizeof(req));
374 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
375 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
376 err(1, "BRDGDELS %s", val);
380 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
383 do_bridgeflag(s, val, IFBIF_STP, 1);
387 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
390 do_bridgeflag(s, val, IFBIF_STP, 0);
394 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
396 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
400 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
402 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
406 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
408 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
412 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
414 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
418 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
420 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
424 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
426 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
430 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
432 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
436 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
438 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
442 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
446 memset(&req, 0, sizeof(req));
447 req.ifbr_ifsflags = IFBF_FLUSHDYN;
448 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
453 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
457 memset(&req, 0, sizeof(req));
458 req.ifbr_ifsflags = IFBF_FLUSHALL;
459 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
464 setbridge_static(const char *val, const char *mac, int s,
465 const struct afswtch *afp)
468 struct ether_addr *ea;
470 memset(&req, 0, sizeof(req));
471 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
473 ea = ether_aton(mac);
475 errx(1, "%s: invalid address: %s", val, mac);
477 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
478 req.ifba_flags = IFBAF_STATIC;
479 req.ifba_vlan = 1; /* XXX allow user to specify */
481 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
482 err(1, "BRDGSADDR %s", val);
486 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
489 struct ether_addr *ea;
491 memset(&req, 0, sizeof(req));
493 ea = ether_aton(val);
495 errx(1, "invalid address: %s", val);
497 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
499 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
500 err(1, "BRDGDADDR %s", val);
504 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
507 bridge_addresses(s, "");
511 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
513 struct ifbrparam param;
516 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
517 errx(1, "invalid value: %s", arg);
519 param.ifbrp_csize = val & 0xffffffff;
521 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
522 err(1, "BRDGSCACHE %s", arg);
526 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
528 struct ifbrparam param;
531 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
532 errx(1, "invalid value: %s", arg);
534 param.ifbrp_hellotime = val & 0xff;
536 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0)
537 err(1, "BRDGSHT %s", arg);
541 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
543 struct ifbrparam param;
546 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
547 errx(1, "invalid value: %s", arg);
549 param.ifbrp_fwddelay = val & 0xff;
551 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0)
552 err(1, "BRDGSFD %s", arg);
556 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
558 struct ifbrparam param;
561 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
562 errx(1, "invalid value: %s", arg);
564 param.ifbrp_maxage = val & 0xff;
566 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0)
567 err(1, "BRDGSMA %s", arg);
571 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
573 struct ifbrparam param;
576 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
577 errx(1, "invalid value: %s", arg);
579 param.ifbrp_prio = val & 0xffff;
581 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
582 err(1, "BRDGSPRI %s", arg);
586 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
588 struct ifbrparam param;
590 if (strcasecmp(arg, "stp") == 0) {
591 param.ifbrp_proto = 0;
592 } else if (strcasecmp(arg, "rstp") == 0) {
593 param.ifbrp_proto = 2;
595 errx(1, "unknown stp protocol");
598 if (do_cmd(s, BRDGSPROTO, ¶m, sizeof(param), 1) < 0)
599 err(1, "BRDGSPROTO %s", arg);
603 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
605 struct ifbrparam param;
608 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
609 errx(1, "invalid value: %s", arg);
611 param.ifbrp_txhc = val & 0xff;
613 if (do_cmd(s, BRDGSTXHC, ¶m, sizeof(param), 1) < 0)
614 err(1, "BRDGSTXHC %s", arg);
618 setbridge_ifpriority(const char *ifn, const char *pri, int s,
619 const struct afswtch *afp)
624 memset(&req, 0, sizeof(req));
626 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
627 errx(1, "invalid value: %s", pri);
629 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
630 req.ifbr_priority = val & 0xff;
632 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
633 err(1, "BRDGSIFPRIO %s", pri);
637 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
638 const struct afswtch *afp)
643 memset(&req, 0, sizeof(req));
645 if (get_val(cost, &val) < 0)
646 errx(1, "invalid value: %s", cost);
648 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
649 req.ifbr_path_cost = val;
651 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
652 err(1, "BRDGSIFCOST %s", cost);
656 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
657 const struct afswtch *afp)
662 memset(&req, 0, sizeof(req));
664 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
665 errx(1, "invalid value: %s", arg);
667 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
668 req.ifbr_addrmax = val & 0xffffffff;
670 if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
671 err(1, "BRDGSIFAMAX %s", arg);
675 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
677 struct ifbrparam param;
680 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
681 errx(1, "invalid value: %s", arg);
683 param.ifbrp_ctime = val & 0xffffffff;
685 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0)
686 err(1, "BRDGSTO %s", arg);
690 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
693 do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
697 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
700 do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
703 static struct cmd bridge_cmds[] = {
704 DEF_CMD_ARG("addm", setbridge_add),
705 DEF_CMD_ARG("deletem", setbridge_delete),
706 DEF_CMD_ARG("discover", setbridge_discover),
707 DEF_CMD_ARG("-discover", unsetbridge_discover),
708 DEF_CMD_ARG("learn", setbridge_learn),
709 DEF_CMD_ARG("-learn", unsetbridge_learn),
710 DEF_CMD_ARG("sticky", setbridge_sticky),
711 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
712 DEF_CMD_ARG("span", setbridge_span),
713 DEF_CMD_ARG("-span", unsetbridge_span),
714 DEF_CMD_ARG("stp", setbridge_stp),
715 DEF_CMD_ARG("-stp", unsetbridge_stp),
716 DEF_CMD_ARG("edge", setbridge_edge),
717 DEF_CMD_ARG("-edge", unsetbridge_edge),
718 DEF_CMD_ARG("autoedge", setbridge_autoedge),
719 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
720 DEF_CMD_ARG("ptp", setbridge_ptp),
721 DEF_CMD_ARG("-ptp", unsetbridge_ptp),
722 DEF_CMD_ARG("autoptp", setbridge_autoptp),
723 DEF_CMD_ARG("-autoptp", unsetbridge_autoptp),
724 DEF_CMD("flush", 0, setbridge_flush),
725 DEF_CMD("flushall", 0, setbridge_flushall),
726 DEF_CMD_ARG2("static", setbridge_static),
727 DEF_CMD_ARG("deladdr", setbridge_deladdr),
728 DEF_CMD("addr", 1, setbridge_addr),
729 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
730 DEF_CMD_ARG("hellotime", setbridge_hellotime),
731 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
732 DEF_CMD_ARG("maxage", setbridge_maxage),
733 DEF_CMD_ARG("priority", setbridge_priority),
734 DEF_CMD_ARG("proto", setbridge_protocol),
735 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
736 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
737 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
738 DEF_CMD_ARG2("ifmaxaddr", setbridge_ifmaxaddr),
739 DEF_CMD_ARG("timeout", setbridge_timeout),
740 DEF_CMD_ARG("private", setbridge_private),
741 DEF_CMD_ARG("-private", unsetbridge_private),
743 static struct afswtch af_bridge = {
744 .af_name = "af_bridge",
746 .af_other_status = bridge_status,
749 static __constructor void
752 #define N(a) (sizeof(a) / sizeof(a[0]))
755 for (i = 0; i < N(bridge_cmds); i++)
756 cmd_register(&bridge_cmds[i]);
757 af_register(&af_bridge);