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.
35 * $NetBSD: brconfig.c,v 1.7 2003/09/19 08:39:09 itojun Exp $
36 * $DragonFly: src/sbin/brconfig/Attic/brconfig.c,v 1.1 2005/12/21 16:33:20 corecode Exp $
43 * Configuration utility for the bridge(4) driver.
45 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_var.h>
55 #include <netinet/in.h> /* for struct arpcom */
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
60 #include <netinet/if_ether.h>
61 #include <net/bridge/if_bridgevar.h>
73 const char *cmd_keyword;
76 void (*cmd_func)(const struct command *, int, const char *,
80 #define CMD_INVERT 0x01 /* "invert" the sense of the command */
82 void cmd_add(const struct command *, int, const char *, char **);
83 void cmd_delete(const struct command *, int, const char *, char **);
84 void cmd_up(const struct command *, int, const char *, char **);
85 void cmd_down(const struct command *, int, const char *, char **);
86 void cmd_discover(const struct command *, int, const char *, char **);
87 void cmd_learn(const struct command *, int, const char *, char **);
88 void cmd_flush(const struct command *, int, const char *, char **);
89 void cmd_flushall(const struct command *, int, const char *, char **);
90 void cmd_static(const struct command *, int, const char *, char **);
91 void cmd_deladdr(const struct command *, int, const char *, char **);
92 void cmd_addr(const struct command *, int, const char *, char **);
93 void cmd_maxaddr(const struct command *, int, const char *, char **);
94 void cmd_hellotime(const struct command *, int, const char *, char **);
95 void cmd_fwddelay(const struct command *, int, const char *, char **);
96 void cmd_maxage(const struct command *, int, const char *, char **);
97 void cmd_priority(const struct command *, int, const char *, char **);
98 void cmd_ifpriority(const struct command *, int, const char *, char **);
99 void cmd_ifpathcost(const struct command *, int, const char *, char **);
100 void cmd_timeout(const struct command *, int, const char *, char **);
101 void cmd_stp(const struct command *, int, const char *, char **);
103 const struct command command_table[] = {
104 { "add", 1, 0, cmd_add },
105 { "delete", 1, 0, cmd_delete },
107 { "up", 0, 0, cmd_up },
108 { "down", 0, 0, cmd_down },
110 { "discover", 1, 0, cmd_discover },
111 { "-discover", 1, CMD_INVERT, cmd_discover },
113 { "learn", 1, 0, cmd_learn },
114 { "-learn", 1, CMD_INVERT, cmd_learn },
116 { "flush", 0, 0, cmd_flush },
117 { "flushall", 0, 0, cmd_flushall },
119 { "static", 2, 0, cmd_static },
120 { "deladdr", 1, 0, cmd_deladdr },
122 { "addr", 0, 0, cmd_addr },
123 { "maxaddr", 1, 0, cmd_maxaddr },
125 { "hellotime", 1, 0, cmd_hellotime },
126 { "fwddelay", 1, 0, cmd_fwddelay },
127 { "maxage", 1, 0, cmd_maxage },
128 { "priority", 1, 0, cmd_priority },
129 { "ifpriority", 2, 0, cmd_ifpriority },
130 { "ifpathcost", 2, 0, cmd_ifpathcost },
131 { "timeout", 1, 0, cmd_timeout },
132 { "stp", 1, 0, cmd_stp },
133 { "-stp", 1, CMD_INVERT, cmd_stp },
135 { NULL, 0, 0, NULL },
139 void status(int, const char *);
140 int is_bridge(const char *);
141 void show_config(int, const char *, const char *);
142 void show_interfaces(int, const char *, const char *);
143 void show_addresses(int, const char *, const char *);
144 int get_val(const char *, u_long *);
145 int do_cmd(int, const char *, u_long, void *, size_t, int);
146 void do_ifflag(int, const char *, int, int);
147 void do_bridgeflag(int, const char *, const char *, int, int);
149 void printb(const char *, u_int, const char *);
151 int main(int, char *[]);
160 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
161 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
164 main(int argc, char *argv[])
166 const struct command *cmd;
173 sock = socket(AF_INET, SOCK_DGRAM, 0);
177 while ((ch = getopt(argc, argv, "a")) != -1) {
203 if (is_bridge(bridge) == 0)
204 errx(1, "%s is not a bridge", bridge);
206 /* Get a copy of the interface flags. */
207 strlcpy(g_ifr.ifr_name, bridge, sizeof(g_ifr.ifr_name));
208 if (ioctl(sock, SIOCGIFFLAGS, &g_ifr) < 0)
209 err(1, "unable to get interface flags");
215 status(sock, bridge);
220 for (cmd = command_table; cmd->cmd_keyword != NULL; cmd++) {
221 if (strcmp(cmd->cmd_keyword, argv[0]) == 0)
224 if (cmd->cmd_keyword == NULL)
225 errx(1, "unknown command: %s", argv[0]);
230 if (argc < cmd->cmd_argcnt)
231 errx(1, "command %s requires %d argument%s",
232 cmd->cmd_keyword, cmd->cmd_argcnt,
233 cmd->cmd_argcnt == 1 ? "" : "s");
235 (*cmd->cmd_func)(cmd, sock, bridge, argv);
237 argc -= cmd->cmd_argcnt;
238 argv += cmd->cmd_argcnt;
241 /* If the flags changed, update them. */
242 if (g_ifr_updated && ioctl(sock, SIOCSIFFLAGS, &g_ifr) < 0)
243 err(1, "unable to set interface flags");
251 static const char *usage_strings[] = {
256 "<bridge> add <interface>",
257 "<bridge> delete <interface>",
258 "<bridge> maxaddr <size>",
259 "<bridge> timeout <time>",
260 "<bridge> static <interface> <address>",
261 "<bridge> deladdr <address>",
264 "<bridge> discover|-discover <interface>",
265 "<bridge> learn|-learn <interface>",
266 "<bridge> stp|-stp <interface>",
267 "<bridge> maxage <time>",
268 "<bridge> fwddelay <time>",
269 "<bridge> hellotime <time>",
270 "<bridge> priority <value>",
271 "<bridge> ifpriority <interface> <value>",
272 "<bridge> ifpathcost <interface> <value>",
275 extern const char *__progname;
278 for (i = 0; usage_strings[i] != NULL; i++)
279 fprintf(stderr, "%s %s %s\n",
280 i == 0 ? "usage:" : " ",
281 __progname, usage_strings[i]);
287 is_bridge(const char *bridge)
290 if (strncmp(bridge, "bridge", 6) != 0 ||
291 isdigit(bridge[6]) == 0)
298 printb(const char *s, u_int v, const char *bits)
303 if (bits && *bits == 8)
304 printf("%s=%o", s, v);
306 printf("%s=%x", s, v);
310 while ((i = *bits++) != 0) {
311 if (v & (1 << (i-1))) {
315 for (; (c = *bits) > 32; bits++)
318 for (; *bits > 32; bits++)
328 struct ifaddrs *ifap, *ifa;
331 if (getifaddrs(&ifap) != 0)
332 err(1, "getifaddrs");
334 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
335 if (is_bridge(ifa->ifa_name) == 0)
337 if (p != NULL && strcmp(p, ifa->ifa_name) == 0)
340 status(sock, ifa->ifa_name);
347 status(int sock, const char *bridge)
350 struct ifbrparam bp1, bp2;
352 memset(&ifr, 0, sizeof(ifr));
354 strlcpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
355 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
356 err(1, "unable to get flags");
358 printf("%s: ", bridge);
359 printb("flags", ifr.ifr_flags, IFFBITS);
362 printf("\tConfiguration:\n");
363 show_config(sock, bridge, "\t\t");
365 printf("\tInterfaces:\n");
366 show_interfaces(sock, bridge, "\t\t");
368 if (do_cmd(sock, bridge, BRDGGCACHE, &bp1, sizeof(bp1), 0) < 0)
369 err(1, "unable to get address cache size");
370 if (do_cmd(sock, bridge, BRDGGTO, &bp2, sizeof(bp2), 0) < 0)
371 err(1, "unable to get address timeout");
373 printf("\tAddress cache (max cache: %u, timeout: %u):\n",
374 bp1.ifbrp_csize, bp2.ifbrp_ctime);
375 show_addresses(sock, bridge, "\t\t");
379 show_config(int sock, const char *bridge, const char *prefix)
381 struct ifbrparam param;
385 if (do_cmd(sock, bridge, BRDGGPRI, ¶m, sizeof(param), 0) < 0)
386 err(1, "unable to get bridge priority");
387 pri = param.ifbrp_prio;
389 if (do_cmd(sock, bridge, BRDGGHT, ¶m, sizeof(param), 0) < 0)
390 err(1, "unable to get hellotime");
391 ht = param.ifbrp_hellotime;
393 if (do_cmd(sock, bridge, BRDGGFD, ¶m, sizeof(param), 0) < 0)
394 err(1, "unable to get forward delay");
395 fd = param.ifbrp_fwddelay;
397 if (do_cmd(sock, bridge, BRDGGMA, ¶m, sizeof(param), 0) < 0)
398 err(1, "unable to get max age");
399 ma = param.ifbrp_maxage;
401 printf("%spriority %u hellotime %u fwddelay %u maxage %u\n",
402 prefix, pri, ht, fd, ma);
406 show_interfaces(int sock, const char *bridge, const char *prefix)
408 static const char *stpstates[] = {
415 struct ifbifconf bifc;
417 char *inbuf = NULL, *ninbuf;
421 ninbuf = realloc(inbuf, len);
423 err(1, "unable to allocate interface buffer");
424 bifc.ifbic_len = len;
425 bifc.ifbic_buf = inbuf = ninbuf;
426 if (do_cmd(sock, bridge, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
427 err(1, "unable to get interface list");
428 if ((bifc.ifbic_len + sizeof(*req)) < len)
433 for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
434 req = bifc.ifbic_req + i;
435 printf("%s%s ", prefix, req->ifbr_ifsname);
436 printb("flags", req->ifbr_ifsflags, IFBIFBITS);
438 printf("%s\t", prefix);
439 printf("port %u priority %u",
440 req->ifbr_portno, req->ifbr_priority);
441 if (req->ifbr_ifsflags & IFBIF_STP) {
442 printf(" path cost %u", req->ifbr_path_cost);
443 if (req->ifbr_state <
444 sizeof(stpstates) / sizeof(stpstates[0]))
445 printf(" %s", stpstates[req->ifbr_state]);
447 printf(" <unknown state %d>",
457 show_addresses(int sock, const char *bridge, const char *prefix)
459 struct ifbaconf ifbac;
460 struct ifbareq *ifba;
461 char *inbuf = NULL, *ninbuf;
463 struct ether_addr ea;
466 ninbuf = realloc(inbuf, len);
468 err(1, "unable to allocate address buffer");
469 ifbac.ifbac_len = len;
470 ifbac.ifbac_buf = inbuf = ninbuf;
471 if (do_cmd(sock, bridge, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
472 err(1, "unable to get address cache");
473 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
478 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
479 ifba = ifbac.ifbac_req + i;
480 memcpy(ea.octet, ifba->ifba_dst,
482 printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
483 ifba->ifba_ifsname, ifba->ifba_expire);
484 printb("flags", ifba->ifba_flags, IFBAFBITS);
492 get_val(const char *cp, u_long *valp)
498 val = strtoul(cp, &endptr, 0);
499 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
507 do_cmd(int sock, const char *bridge, u_long op, void *arg, size_t argsize,
512 memset(&ifd, 0, sizeof(ifd));
514 strlcpy(ifd.ifd_name, bridge, sizeof(ifd.ifd_name));
516 ifd.ifd_len = argsize;
519 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
523 do_ifflag(int sock, const char *bridge, int flag, int set)
527 g_ifr.ifr_flags |= flag;
529 g_ifr.ifr_flags &= ~flag;
535 do_bridgeflag(int sock, const char *bridge, const char *ifs, int flag,
540 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
542 if (do_cmd(sock, bridge, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
543 err(1, "unable to get bridge flags");
546 req.ifbr_ifsflags |= flag;
548 req.ifbr_ifsflags &= ~flag;
550 if (do_cmd(sock, bridge, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
551 err(1, "unable to set bridge flags");
555 cmd_add(const struct command *cmd, int sock, const char *bridge,
560 memset(&req, 0, sizeof(req));
562 strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
563 if (do_cmd(sock, bridge, BRDGADD, &req, sizeof(req), 1) < 0)
564 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
568 cmd_delete(const struct command *cmd, int sock, const char *bridge,
573 memset(&req, 0, sizeof(req));
574 strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
575 if (do_cmd(sock, bridge, BRDGDEL, &req, sizeof(req), 1) < 0)
576 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
580 cmd_up(const struct command *cmd, int sock, const char *bridge,
584 do_ifflag(sock, bridge, IFF_UP, 1);
588 cmd_down(const struct command *cmd, int sock, const char *bridge,
592 do_ifflag(sock, bridge, IFF_UP, 0);
596 cmd_discover(const struct command *cmd, int sock, const char *bridge,
600 do_bridgeflag(sock, bridge, argv[0], IFBIF_DISCOVER,
601 (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
605 cmd_learn(const struct command *cmd, int sock, const char *bridge,
609 do_bridgeflag(sock, bridge, argv[0], IFBIF_LEARNING,
610 (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
614 cmd_stp(const struct command *cmd, int sock, const char *bridge,
618 do_bridgeflag(sock, bridge, argv[0], IFBIF_STP,
619 (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
623 cmd_flush(const struct command *cmd, int sock, const char *bridge,
628 memset(&req, 0, sizeof(req));
629 req.ifbr_ifsflags = IFBF_FLUSHDYN;
630 if (do_cmd(sock, bridge, BRDGFLUSH, &req, sizeof(req), 1) < 0)
631 err(1, "%s", cmd->cmd_keyword);
635 cmd_flushall(const struct command *cmd, int sock, const char *bridge,
640 memset(&req, 0, sizeof(req));
641 req.ifbr_ifsflags = IFBF_FLUSHALL;
642 if (do_cmd(sock, bridge, BRDGFLUSH, &req, sizeof(req), 1) < 0)
643 err(1, "%s", cmd->cmd_keyword);
647 cmd_static(const struct command *cmd, int sock, const char *bridge,
651 struct ether_addr *ea;
653 memset(&req, 0, sizeof(req));
654 strlcpy(req.ifba_ifsname, argv[0], sizeof(req.ifba_ifsname));
656 ea = ether_aton(argv[1]);
658 errx(1, "%s: invalid address: %s", cmd->cmd_keyword, argv[1]);
660 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
661 req.ifba_flags = IFBAF_STATIC;
663 if (do_cmd(sock, bridge, BRDGSADDR, &req, sizeof(req), 1) < 0)
664 err(1, "%s %s %s", cmd->cmd_keyword, argv[0], argv[1]);
668 cmd_deladdr(const struct command *cmd, int sock, const char *bridge,
672 struct ether_addr *ea;
674 memset(&req, 0, sizeof(req));
676 ea = ether_aton(argv[0]);
678 errx(1, "%s: invalid address: %s", cmd->cmd_keyword, argv[0]);
680 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
682 if (do_cmd(sock, bridge, BRDGDADDR, &req, sizeof(req), 1) < 0)
683 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
687 cmd_addr(const struct command *cmd, int sock, const char *bridge,
691 show_addresses(sock, bridge, "\t");
695 cmd_maxaddr(const struct command *cmd, int sock, const char *bridge,
698 struct ifbrparam param;
701 if (get_val(argv[0], &val) < 0 || (val & ~0xffffffff) != 0)
702 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
704 param.ifbrp_csize = val & 0xffffffff;
706 if (do_cmd(sock, bridge, BRDGSCACHE, ¶m, sizeof(param), 1) < 0)
707 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
711 cmd_hellotime(const struct command *cmd, int sock, const char *bridge,
714 struct ifbrparam param;
717 if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
718 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
720 param.ifbrp_hellotime = val & 0xff;
722 if (do_cmd(sock, bridge, BRDGSHT, ¶m, sizeof(param), 1) < 0)
723 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
727 cmd_fwddelay(const struct command *cmd, int sock, const char *bridge,
730 struct ifbrparam param;
733 if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
734 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
736 param.ifbrp_fwddelay = val & 0xff;
738 if (do_cmd(sock, bridge, BRDGSFD, ¶m, sizeof(param), 1) < 0)
739 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
743 cmd_maxage(const struct command *cmd, int sock, const char *bridge,
746 struct ifbrparam param;
749 if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
750 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
752 param.ifbrp_maxage = val & 0xff;
754 if (do_cmd(sock, bridge, BRDGSMA, ¶m, sizeof(param), 1) < 0)
755 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
759 cmd_priority(const struct command *cmd, int sock, const char *bridge,
762 struct ifbrparam param;
765 if (get_val(argv[0], &val) < 0 || (val & ~0xffff) != 0)
766 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
768 param.ifbrp_prio = val & 0xffff;
770 if (do_cmd(sock, bridge, BRDGSPRI, ¶m, sizeof(param), 1) < 0)
771 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
775 cmd_ifpriority(const struct command *cmd, int sock, const char *bridge,
781 memset(&req, 0, sizeof(req));
783 if (get_val(argv[1], &val) < 0 || (val & ~0xff) != 0)
784 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[1]);
786 strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
787 req.ifbr_priority = val & 0xff;
789 if (do_cmd(sock, bridge, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
790 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
794 cmd_ifpathcost(const struct command *cmd, int sock, const char *bridge,
800 memset(&req, 0, sizeof(req));
802 if (get_val(argv[1], &val) < 0 || (val & ~0xff) != 0)
803 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[1]);
805 strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
806 req.ifbr_path_cost = val & 0xffff;
808 if (do_cmd(sock, bridge, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
809 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
813 cmd_timeout(const struct command *cmd, int sock, const char *bridge,
816 struct ifbrparam param;
819 if (get_val(argv[0], &val) < 0 || (val & ~0xffffffff) != 0)
820 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
822 param.ifbrp_ctime = val & 0xffffffff;
824 if (do_cmd(sock, bridge, BRDGSTO, ¶m, sizeof(param), 1) < 0)
825 err(1, "%s %s", cmd->cmd_keyword, argv[0]);