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);
181 if (req->ifbr_ifsflags & IFBIF_STP) {
183 printf("port %u priority %u",
184 req->ifbr_portno, req->ifbr_priority);
185 printf(" path cost %u", req->ifbr_path_cost);
186 if (req->ifbr_proto <
187 sizeof(stpproto) / sizeof(stpproto[0]))
188 printf(" proto %s", stpproto[req->ifbr_proto]);
190 printf(" <unknown proto %d>",
195 sizeof(stproles) / sizeof(stproles[0]))
196 printf("role %s", stproles[req->ifbr_role]);
198 printf("<unknown role %d>",
200 if (req->ifbr_state <
201 sizeof(stpstates) / sizeof(stpstates[0]))
202 printf(" state %s", stpstates[req->ifbr_state]);
204 printf(" <unknown state %d>",
214 bridge_addresses(int s, const char *prefix)
216 struct ifbaconf ifbac;
217 struct ifbareq *ifba;
218 char *inbuf = NULL, *ninbuf;
220 struct ether_addr ea;
223 ninbuf = realloc(inbuf, len);
225 err(1, "unable to allocate address buffer");
226 ifbac.ifbac_len = len;
227 ifbac.ifbac_buf = inbuf = ninbuf;
228 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
229 err(1, "unable to get address cache");
230 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
235 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
236 ifba = ifbac.ifbac_req + i;
237 memcpy(ea.octet, ifba->ifba_dst,
239 printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
240 ifba->ifba_ifsname, ifba->ifba_expire);
241 printb("flags", ifba->ifba_flags, IFBAFBITS);
251 struct ifbropreq param;
253 u_int8_t ht, fd, ma, hc, pro;
254 u_int8_t lladdr[ETHER_ADDR_LEN];
257 if (do_cmd(s, BRDGPARAM, ¶m, sizeof(param), 0) < 0)
259 pri = param.ifbop_priority;
260 pro = param.ifbop_protocol;
261 ht = param.ifbop_hellotime;
262 fd = param.ifbop_fwddelay;
263 hc = param.ifbop_holdcount;
264 ma = param.ifbop_maxage;
266 PV2ID(param.ifbop_bridgeid, bprio, lladdr);
267 printf("\tid %s priority %u hellotime %u fwddelay %u\n",
268 ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
269 printf("\tmaxage %u holdcnt %u proto %s\n",
270 ma, hc, stpproto[pro]);
272 PV2ID(param.ifbop_designated_root, bprio, lladdr);
273 printf("\troot id %s priority %d ifcost %u port %u\n",
274 ether_ntoa((struct ether_addr *)lladdr), bprio,
275 param.ifbop_root_path_cost, param.ifbop_root_port & 0xfff);
277 bridge_interfaces(s, "\tmember: ");
284 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
288 memset(&req, 0, sizeof(req));
289 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
290 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
291 err(1, "BRDGADD %s", val);
295 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
299 memset(&req, 0, sizeof(req));
300 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
301 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
302 err(1, "BRDGDEL %s", val);
306 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
309 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
313 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
316 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
320 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
323 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
327 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
330 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
334 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
337 do_bridgeflag(s, val, IFBIF_STICKY, 1);
341 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
344 do_bridgeflag(s, val, IFBIF_STICKY, 0);
348 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
352 memset(&req, 0, sizeof(req));
353 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
354 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
355 err(1, "BRDGADDS %s", val);
359 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
363 memset(&req, 0, sizeof(req));
364 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
365 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
366 err(1, "BRDGDELS %s", val);
370 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
373 do_bridgeflag(s, val, IFBIF_STP, 1);
377 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
380 do_bridgeflag(s, val, IFBIF_STP, 0);
384 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
386 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
390 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
392 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
396 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
398 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
402 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
404 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
408 setbridge_p2p(const char *val, int d, int s, const struct afswtch *afp)
410 do_bridgeflag(s, val, IFBIF_BSTP_P2P, 1);
414 unsetbridge_p2p(const char *val, int d, int s, const struct afswtch *afp)
416 do_bridgeflag(s, val, IFBIF_BSTP_P2P, 0);
420 setbridge_autop2p(const char *val, int d, int s, const struct afswtch *afp)
422 do_bridgeflag(s, val, IFBIF_BSTP_AUTOP2P, 1);
426 unsetbridge_autop2p(const char *val, int d, int s, const struct afswtch *afp)
428 do_bridgeflag(s, val, IFBIF_BSTP_AUTOP2P, 0);
432 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
436 memset(&req, 0, sizeof(req));
437 req.ifbr_ifsflags = IFBF_FLUSHDYN;
438 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
443 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
447 memset(&req, 0, sizeof(req));
448 req.ifbr_ifsflags = IFBF_FLUSHALL;
449 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
454 setbridge_static(const char *val, const char *mac, int s,
455 const struct afswtch *afp)
458 struct ether_addr *ea;
460 memset(&req, 0, sizeof(req));
461 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
463 ea = ether_aton(mac);
465 errx(1, "%s: invalid address: %s", val, mac);
467 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
468 req.ifba_flags = IFBAF_STATIC;
470 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
471 err(1, "BRDGSADDR %s", val);
475 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
478 struct ether_addr *ea;
480 memset(&req, 0, sizeof(req));
482 ea = ether_aton(val);
484 errx(1, "invalid address: %s", val);
486 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
488 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
489 err(1, "BRDGDADDR %s", val);
493 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
496 bridge_addresses(s, "");
500 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
502 struct ifbrparam param;
505 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
506 errx(1, "invalid value: %s", arg);
508 param.ifbrp_csize = val & 0xffffffff;
510 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
511 err(1, "BRDGSCACHE %s", arg);
515 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
517 struct ifbrparam param;
520 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
521 errx(1, "invalid value: %s", arg);
523 param.ifbrp_hellotime = val & 0xff;
525 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0)
526 err(1, "BRDGSHT %s", arg);
530 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
532 struct ifbrparam param;
535 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
536 errx(1, "invalid value: %s", arg);
538 param.ifbrp_fwddelay = val & 0xff;
540 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0)
541 err(1, "BRDGSFD %s", arg);
545 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
547 struct ifbrparam param;
550 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
551 errx(1, "invalid value: %s", arg);
553 param.ifbrp_maxage = val & 0xff;
555 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0)
556 err(1, "BRDGSMA %s", arg);
560 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
562 struct ifbrparam param;
565 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
566 errx(1, "invalid value: %s", arg);
568 param.ifbrp_prio = val & 0xffff;
570 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
571 err(1, "BRDGSPRI %s", arg);
575 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
577 struct ifbrparam param;
579 if (strcasecmp(arg, "stp") == 0) {
580 param.ifbrp_proto = 0;
581 } else if (strcasecmp(arg, "rstp") == 0) {
582 param.ifbrp_proto = 2;
584 errx(1, "unknown stp protocol");
587 if (do_cmd(s, BRDGSPROTO, ¶m, sizeof(param), 1) < 0)
588 err(1, "BRDGSPROTO %s", arg);
592 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
594 struct ifbrparam param;
597 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
598 errx(1, "invalid value: %s", arg);
600 param.ifbrp_txhc = val & 0xff;
602 if (do_cmd(s, BRDGSTXHC, ¶m, sizeof(param), 1) < 0)
603 err(1, "BRDGSTXHC %s", arg);
607 setbridge_ifpriority(const char *ifn, const char *pri, int s,
608 const struct afswtch *afp)
613 memset(&req, 0, sizeof(req));
615 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
616 errx(1, "invalid value: %s", pri);
618 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
619 req.ifbr_priority = val & 0xff;
621 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
622 err(1, "BRDGSIFPRIO %s", pri);
626 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
627 const struct afswtch *afp)
632 memset(&req, 0, sizeof(req));
634 if (get_val(cost, &val) < 0)
635 errx(1, "invalid value: %s", cost);
637 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
638 req.ifbr_path_cost = val;
640 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
641 err(1, "BRDGSIFCOST %s", cost);
645 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
647 struct ifbrparam param;
650 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
651 errx(1, "invalid value: %s", arg);
653 param.ifbrp_ctime = val & 0xffffffff;
655 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0)
656 err(1, "BRDGSTO %s", arg);
659 static struct cmd bridge_cmds[] = {
660 DEF_CMD_ARG("addm", setbridge_add),
661 DEF_CMD_ARG("deletem", setbridge_delete),
662 DEF_CMD_ARG("discover", setbridge_discover),
663 DEF_CMD_ARG("-discover", unsetbridge_discover),
664 DEF_CMD_ARG("learn", setbridge_learn),
665 DEF_CMD_ARG("-learn", unsetbridge_learn),
666 DEF_CMD_ARG("sticky", setbridge_sticky),
667 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
668 DEF_CMD_ARG("span", setbridge_span),
669 DEF_CMD_ARG("-span", unsetbridge_span),
670 DEF_CMD_ARG("stp", setbridge_stp),
671 DEF_CMD_ARG("-stp", unsetbridge_stp),
672 DEF_CMD_ARG("edge", setbridge_edge),
673 DEF_CMD_ARG("-edge", unsetbridge_edge),
674 DEF_CMD_ARG("autoedge", setbridge_autoedge),
675 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
676 DEF_CMD_ARG("p2p", setbridge_p2p),
677 DEF_CMD_ARG("-p2p", unsetbridge_p2p),
678 DEF_CMD_ARG("autop2p", setbridge_autop2p),
679 DEF_CMD_ARG("-autop2p", unsetbridge_autop2p),
680 DEF_CMD("flush", 0, setbridge_flush),
681 DEF_CMD("flushall", 0, setbridge_flushall),
682 DEF_CMD_ARG2("static", setbridge_static),
683 DEF_CMD_ARG("deladdr", setbridge_deladdr),
684 DEF_CMD("addr", 1, setbridge_addr),
685 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
686 DEF_CMD_ARG("hellotime", setbridge_hellotime),
687 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
688 DEF_CMD_ARG("maxage", setbridge_maxage),
689 DEF_CMD_ARG("priority", setbridge_priority),
690 DEF_CMD_ARG("proto", setbridge_protocol),
691 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
692 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
693 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
694 DEF_CMD_ARG("timeout", setbridge_timeout),
696 static struct afswtch af_bridge = {
697 .af_name = "af_bridge",
699 .af_other_status = bridge_status,
702 static __constructor void
705 #define N(a) (sizeof(a) / sizeof(a[0]))
708 for (i = 0; i < N(bridge_cmds); i++)
709 cmd_register(&bridge_cmds[i]);
710 af_register(&af_bridge);