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>
65 get_val(const char *cp, u_long *valp)
71 val = strtoul(cp, &endptr, 0);
72 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
80 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
84 memset(&ifd, 0, sizeof(ifd));
86 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
88 ifd.ifd_len = argsize;
91 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
95 do_bridgeflag(int sock, const char *ifs, int flag, int set)
99 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
101 if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
102 err(1, "unable to get bridge flags");
105 req.ifbr_ifsflags |= flag;
107 req.ifbr_ifsflags &= ~flag;
109 if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
110 err(1, "unable to set bridge flags");
114 bridge_interfaces(int s, const char *prefix)
116 static const char *stpstates[] = {
123 struct ifbifconf bifc;
125 char *inbuf = NULL, *ninbuf;
129 pad = strdup(prefix);
132 /* replace the prefix with whitespace */
133 for (p = pad; *p != '\0'; p++) {
139 ninbuf = realloc(inbuf, len);
141 err(1, "unable to allocate interface buffer");
142 bifc.ifbic_len = len;
143 bifc.ifbic_buf = inbuf = ninbuf;
144 if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
145 err(1, "unable to get interface list");
146 if ((bifc.ifbic_len + sizeof(*req)) < len)
151 for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
152 req = bifc.ifbic_req + i;
153 printf("%s%s ", prefix, req->ifbr_ifsname);
154 printb("flags", req->ifbr_ifsflags, IFBIFBITS);
157 if (req->ifbr_ifsflags & IFBIF_STP) {
159 printf("port %u priority %u",
160 req->ifbr_portno, req->ifbr_priority);
161 printf(" path cost %u", req->ifbr_path_cost);
162 if (req->ifbr_state <
163 sizeof(stpstates) / sizeof(stpstates[0]))
164 printf(" %s", stpstates[req->ifbr_state]);
166 printf(" <unknown state %d>",
176 bridge_addresses(int s, const char *prefix)
178 struct ifbaconf ifbac;
179 struct ifbareq *ifba;
180 char *inbuf = NULL, *ninbuf;
182 struct ether_addr ea;
185 ninbuf = realloc(inbuf, len);
187 err(1, "unable to allocate address buffer");
188 ifbac.ifbac_len = len;
189 ifbac.ifbac_buf = inbuf = ninbuf;
190 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
191 err(1, "unable to get address cache");
192 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
197 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
198 ifba = ifbac.ifbac_req + i;
199 memcpy(ea.octet, ifba->ifba_dst,
201 printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
202 ifba->ifba_ifsname, ifba->ifba_expire);
203 printb("flags", ifba->ifba_flags, IFBAFBITS);
213 struct ifbrparam param;
217 if (do_cmd(s, BRDGGPRI, ¶m, sizeof(param), 0) < 0)
219 pri = param.ifbrp_prio;
221 if (do_cmd(s, BRDGGHT, ¶m, sizeof(param), 0) < 0)
223 ht = param.ifbrp_hellotime;
225 if (do_cmd(s, BRDGGFD, ¶m, sizeof(param), 0) < 0)
227 fd = param.ifbrp_fwddelay;
229 if (do_cmd(s, BRDGGMA, ¶m, sizeof(param), 0) < 0)
231 ma = param.ifbrp_maxage;
233 printf("\tpriority %u hellotime %u fwddelay %u maxage %u\n",
236 bridge_interfaces(s, "\tmember: ");
243 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
247 memset(&req, 0, sizeof(req));
248 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
249 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
250 err(1, "BRDGADD %s", val);
254 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
258 memset(&req, 0, sizeof(req));
259 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
260 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
261 err(1, "BRDGDEL %s", val);
265 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
268 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
272 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
275 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
279 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
282 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
286 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
289 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
293 setbridge_span(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, BRDGADDS, &req, sizeof(req), 1) < 0)
300 err(1, "BRDGADDS %s", val);
304 unsetbridge_span(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, BRDGDELS, &req, sizeof(req), 1) < 0)
311 err(1, "BRDGDELS %s", val);
315 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
318 do_bridgeflag(s, val, IFBIF_STP, 1);
322 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
325 do_bridgeflag(s, val, IFBIF_STP, 0);
329 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
333 memset(&req, 0, sizeof(req));
334 req.ifbr_ifsflags = IFBF_FLUSHDYN;
335 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
340 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
344 memset(&req, 0, sizeof(req));
345 req.ifbr_ifsflags = IFBF_FLUSHALL;
346 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
351 setbridge_static(const char *val, const char *mac, int s,
352 const struct afswtch *afp)
355 struct ether_addr *ea;
357 memset(&req, 0, sizeof(req));
358 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
360 ea = ether_aton(mac);
362 errx(1, "%s: invalid address: %s", val, mac);
364 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
365 req.ifba_flags = IFBAF_STATIC;
367 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
368 err(1, "BRDGSADDR %s", val);
372 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
375 struct ether_addr *ea;
377 memset(&req, 0, sizeof(req));
379 ea = ether_aton(val);
381 errx(1, "invalid address: %s", val);
383 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
385 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
386 err(1, "BRDGDADDR %s", val);
390 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
393 bridge_addresses(s, "");
397 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
399 struct ifbrparam param;
402 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
403 errx(1, "invalid value: %s", arg);
405 param.ifbrp_csize = val & 0xffffffff;
407 if (do_cmd(s, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
408 err(1, "BRDGSCACHE %s", arg);
412 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
414 struct ifbrparam param;
417 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
418 errx(1, "invalid value: %s", arg);
420 param.ifbrp_hellotime = val & 0xff;
422 if (do_cmd(s, BRDGSHT, ¶m, sizeof(param), 1) < 0)
423 err(1, "BRDGSHT %s", arg);
427 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
429 struct ifbrparam param;
432 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
433 errx(1, "invalid value: %s", arg);
435 param.ifbrp_fwddelay = val & 0xff;
437 if (do_cmd(s, BRDGSFD, ¶m, sizeof(param), 1) < 0)
438 err(1, "BRDGSFD %s", arg);
442 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
444 struct ifbrparam param;
447 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
448 errx(1, "invalid value: %s", arg);
450 param.ifbrp_maxage = val & 0xff;
452 if (do_cmd(s, BRDGSMA, ¶m, sizeof(param), 1) < 0)
453 err(1, "BRDGSMA %s", arg);
457 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
459 struct ifbrparam param;
462 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
463 errx(1, "invalid value: %s", arg);
465 param.ifbrp_prio = val & 0xffff;
467 if (do_cmd(s, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
468 err(1, "BRDGSPRI %s", arg);
472 setbridge_ifpriority(const char *ifn, const char *pri, int s,
473 const struct afswtch *afp)
478 memset(&req, 0, sizeof(req));
480 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
481 errx(1, "invalid value: %s", pri);
483 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
484 req.ifbr_priority = val & 0xff;
486 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
487 err(1, "BRDGSIFPRIO %s", pri);
491 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
492 const struct afswtch *afp)
497 memset(&req, 0, sizeof(req));
499 if (get_val(cost, &val) < 0 || (val & ~0xff) != 0)
500 errx(1, "invalid value: %s", cost);
502 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
503 req.ifbr_path_cost = val & 0xffff;
505 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
506 err(1, "BRDGSIFCOST %s", cost);
510 setbridge_timeout(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_ctime = val & 0xffffffff;
520 if (do_cmd(s, BRDGSTO, ¶m, sizeof(param), 1) < 0)
521 err(1, "BRDGSTO %s", arg);
524 static struct cmd bridge_cmds[] = {
525 DEF_CMD_ARG("addm", setbridge_add),
526 DEF_CMD_ARG("deletem", setbridge_delete),
527 DEF_CMD_ARG("discover", setbridge_discover),
528 DEF_CMD_ARG("-discover", unsetbridge_discover),
529 DEF_CMD_ARG("learn", setbridge_learn),
530 DEF_CMD_ARG("-learn", unsetbridge_learn),
531 DEF_CMD_ARG("span", setbridge_span),
532 DEF_CMD_ARG("-span", unsetbridge_span),
533 DEF_CMD_ARG("stp", setbridge_stp),
534 DEF_CMD_ARG("-stp", unsetbridge_stp),
535 DEF_CMD("flush", 0, setbridge_flush),
536 DEF_CMD("flushall", 0, setbridge_flushall),
537 DEF_CMD_ARG2("static", setbridge_static),
538 DEF_CMD_ARG("deladdr", setbridge_deladdr),
539 DEF_CMD("addr", 1, setbridge_addr),
540 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
541 DEF_CMD_ARG("hellotime", setbridge_hellotime),
542 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
543 DEF_CMD_ARG("maxage", setbridge_maxage),
544 DEF_CMD_ARG("priority", setbridge_priority),
545 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
546 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
547 DEF_CMD_ARG("timeout", setbridge_timeout),
549 static struct afswtch af_bridge = {
550 .af_name = "af_bridge",
552 .af_other_status = bridge_status,
555 static __constructor void
558 #define N(a) (sizeof(a) / sizeof(a[0]))
561 for (i = 0; i < N(bridge_cmds); i++)
562 cmd_register(&bridge_cmds[i]);
563 af_register(&af_bridge);