Put the 'V' option back in.
[dragonfly.git] / sbin / brconfig / brconfig.c
1 /*
2  * Copyright 2001 Wasabi Systems, Inc.
3  * All rights reserved.
4  *
5  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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
21  *    written permission.
22  *
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.
34  *
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 $
37  *
38  */
39
40 /*
41  * brconfig(8) --
42  *
43  *      Configuration utility for the bridge(4) driver.
44  */
45 #include <sys/cdefs.h>
46
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50
51 #include <net/if.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>
59 #include <net/if.h>
60 #include <netinet/if_ether.h>
61 #include <net/bridge/if_bridgevar.h>
62
63 #include <ctype.h>
64 #include <err.h>
65 #include <errno.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <ifaddrs.h>
71
72 struct command {
73         const char *cmd_keyword;
74         int     cmd_argcnt;
75         int     cmd_flags;
76         void    (*cmd_func)(const struct command *, int, const char *,
77                     char **);
78 };
79
80 #define CMD_INVERT      0x01    /* "invert" the sense of the command */
81
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 **);
102
103 const struct command command_table[] = {
104         { "add",                1,      0,              cmd_add },
105         { "delete",             1,      0,              cmd_delete },
106
107         { "up",                 0,      0,              cmd_up },
108         { "down",               0,      0,              cmd_down },
109
110         { "discover",           1,      0,              cmd_discover },
111         { "-discover",          1,      CMD_INVERT,     cmd_discover },
112
113         { "learn",              1,      0,              cmd_learn },
114         { "-learn",             1,      CMD_INVERT,     cmd_learn },
115
116         { "flush",              0,      0,              cmd_flush },
117         { "flushall",           0,      0,              cmd_flushall },
118
119         { "static",             2,      0,              cmd_static },
120         { "deladdr",            1,      0,              cmd_deladdr },
121
122         { "addr",               0,      0,              cmd_addr },
123         { "maxaddr",            1,      0,              cmd_maxaddr },
124
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 },
134
135         { NULL,                 0,      0,              NULL },
136 };
137
138 void    printall(int);
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);
148
149 void    printb(const char *, u_int, const char *);
150
151 int     main(int, char *[]);
152 void    usage(void);
153
154 int     aflag;
155
156 struct ifreq g_ifr;
157 int     g_ifr_updated;
158
159 #define IFFBITS \
160 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
161 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
162
163 int
164 main(int argc, char *argv[])
165 {
166         const struct command *cmd;
167         char *bridge;
168         int sock, ch;
169
170         if (argc < 2)
171                 usage();
172
173         sock = socket(AF_INET, SOCK_DGRAM, 0);
174         if (sock < 0)
175                 err(1, "socket");
176
177         while ((ch = getopt(argc, argv, "a")) != -1) {
178                 switch (ch) {
179                 case 'a':
180                         aflag = 1;
181                         break;
182
183                 default:
184                         usage();
185                 }
186         }
187
188         argc -= optind;
189         argv += optind;
190
191         if (aflag) {
192                 if (argc != 0)
193                         usage();
194                 printall(sock);
195                 exit(0);
196         }
197
198         if (argc == 0)
199                 usage();
200
201         bridge = argv[0];
202
203         if (is_bridge(bridge) == 0)
204                 errx(1, "%s is not a bridge", bridge);
205
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");
210
211         argc--;
212         argv++;
213
214         if (argc == 0) {
215                 status(sock, bridge);
216                 exit(0);
217         }
218
219         while (argc != 0) {
220                 for (cmd = command_table; cmd->cmd_keyword != NULL; cmd++) {
221                         if (strcmp(cmd->cmd_keyword, argv[0]) == 0)
222                                 break;
223                 }
224                 if (cmd->cmd_keyword == NULL)
225                         errx(1, "unknown command: %s", argv[0]);
226
227                 argc--;
228                 argv++;
229
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");
234
235                 (*cmd->cmd_func)(cmd, sock, bridge, argv);
236
237                 argc -= cmd->cmd_argcnt;
238                 argv += cmd->cmd_argcnt;
239         }
240
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");
244
245         exit (0);
246 }
247
248 void
249 usage(void)
250 {
251         static const char *usage_strings[] = {
252                 "-a",
253                 "<bridge>",
254                 "<bridge> up|down",
255                 "<bridge> addr",
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>",
262                 "<bridge> flush",
263                 "<bridge> flushall",
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>",
273                 NULL,
274         };
275         extern const char *__progname;
276         int i;
277
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]);
282
283         exit(1);
284 }
285
286 int
287 is_bridge(const char *bridge)
288 {
289
290         if (strncmp(bridge, "bridge", 6) != 0 ||
291             isdigit(bridge[6]) == 0)
292                 return (0);
293
294         return (1);
295 }
296
297 void
298 printb(const char *s, u_int v, const char *bits)
299 {
300         int i, any = 0;
301         char c;
302
303         if (bits && *bits == 8)
304                 printf("%s=%o", s, v);
305         else
306                 printf("%s=%x", s, v);
307         bits++;
308         if (bits) { 
309                 putchar('<');
310                 while ((i = *bits++) != 0) {
311                         if (v & (1 << (i-1))) {
312                                 if (any)
313                                         putchar(',');
314                                 any = 1;
315                                 for (; (c = *bits) > 32; bits++)
316                                         putchar(c);
317                         } else
318                                 for (; *bits > 32; bits++)
319                                         ;
320                 }
321                 putchar('>');
322         }
323 }
324
325 void
326 printall(int sock)
327 {
328         struct ifaddrs *ifap, *ifa;
329         char *p;
330
331         if (getifaddrs(&ifap) != 0)
332                 err(1, "getifaddrs");
333         p = NULL;
334         for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
335                 if (is_bridge(ifa->ifa_name) == 0)
336                         continue;
337                 if (p != NULL && strcmp(p, ifa->ifa_name) == 0)
338                         continue;
339                 p = ifa->ifa_name;
340                 status(sock, ifa->ifa_name);
341         }
342
343         freeifaddrs(ifap);
344 }
345
346 void
347 status(int sock, const char *bridge)
348 {
349         struct ifreq ifr;
350         struct ifbrparam bp1, bp2;
351
352         memset(&ifr, 0, sizeof(ifr));
353
354         strlcpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
355         if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
356                 err(1, "unable to get flags");
357
358         printf("%s: ", bridge);
359         printb("flags", ifr.ifr_flags, IFFBITS);
360         printf("\n");
361
362         printf("\tConfiguration:\n");
363         show_config(sock, bridge, "\t\t");
364
365         printf("\tInterfaces:\n");
366         show_interfaces(sock, bridge, "\t\t");
367
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");
372
373         printf("\tAddress cache (max cache: %u, timeout: %u):\n",
374             bp1.ifbrp_csize, bp2.ifbrp_ctime);
375         show_addresses(sock, bridge, "\t\t");
376 }
377
378 void
379 show_config(int sock, const char *bridge, const char *prefix)
380 {
381         struct ifbrparam param;
382         u_int16_t pri;
383         u_int8_t ht, fd, ma;
384
385         if (do_cmd(sock, bridge, BRDGGPRI, &param, sizeof(param), 0) < 0)
386                 err(1, "unable to get bridge priority");
387         pri = param.ifbrp_prio;
388
389         if (do_cmd(sock, bridge, BRDGGHT, &param, sizeof(param), 0) < 0)
390                 err(1, "unable to get hellotime");
391         ht = param.ifbrp_hellotime;
392
393         if (do_cmd(sock, bridge, BRDGGFD, &param, sizeof(param), 0) < 0)
394                 err(1, "unable to get forward delay");
395         fd = param.ifbrp_fwddelay;
396
397         if (do_cmd(sock, bridge, BRDGGMA, &param, sizeof(param), 0) < 0)
398                 err(1, "unable to get max age");
399         ma = param.ifbrp_maxage;
400
401         printf("%spriority %u hellotime %u fwddelay %u maxage %u\n",
402             prefix, pri, ht, fd, ma);
403 }
404
405 void
406 show_interfaces(int sock, const char *bridge, const char *prefix)
407 {
408         static const char *stpstates[] = {
409                 "disabled",
410                 "listening",
411                 "learning",
412                 "forwarding",
413                 "blocking",
414         };
415         struct ifbifconf bifc;
416         struct ifbreq *req;
417         char *inbuf = NULL, *ninbuf;
418         int i, len = 8192;
419
420         for (;;) {
421                 ninbuf = realloc(inbuf, len);
422                 if (ninbuf == NULL)
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)
429                         break;
430                 len *= 2;
431         }
432
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);
437                 printf("\n");
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]);
446                         else
447                                 printf(" <unknown state %d>",
448                                     req->ifbr_state);
449                 }
450                 printf("\n");
451         }
452
453         free(inbuf);
454 }
455
456 void
457 show_addresses(int sock, const char *bridge, const char *prefix)
458 {
459         struct ifbaconf ifbac;
460         struct ifbareq *ifba;
461         char *inbuf = NULL, *ninbuf;
462         int i, len = 8192;
463         struct ether_addr ea;
464
465         for (;;) {
466                 ninbuf = realloc(inbuf, len);
467                 if (ninbuf == NULL)
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)
474                         break;
475                 len *= 2;
476         }
477
478         for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
479                 ifba = ifbac.ifbac_req + i;
480                 memcpy(ea.octet, ifba->ifba_dst,
481                     sizeof(ea.octet));
482                 printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
483                     ifba->ifba_ifsname, ifba->ifba_expire);
484                 printb("flags", ifba->ifba_flags, IFBAFBITS);
485                 printf("\n");
486         }
487
488         free(inbuf);
489 }
490
491 int
492 get_val(const char *cp, u_long *valp)
493 {
494         char *endptr;
495         u_long val;
496
497         errno = 0;
498         val = strtoul(cp, &endptr, 0);
499         if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
500                 return (-1);
501
502         *valp = val;
503         return (0);
504 }
505
506 int
507 do_cmd(int sock, const char *bridge, u_long op, void *arg, size_t argsize,
508     int set)
509 {
510         struct ifdrv ifd;
511
512         memset(&ifd, 0, sizeof(ifd));
513
514         strlcpy(ifd.ifd_name, bridge, sizeof(ifd.ifd_name));
515         ifd.ifd_cmd = op;
516         ifd.ifd_len = argsize;
517         ifd.ifd_data = arg;
518
519         return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
520 }
521
522 void
523 do_ifflag(int sock, const char *bridge, int flag, int set)
524 {
525
526         if (set)
527                 g_ifr.ifr_flags |= flag;
528         else
529                 g_ifr.ifr_flags &= ~flag;
530
531         g_ifr_updated = 1;
532 }
533
534 void
535 do_bridgeflag(int sock, const char *bridge, const char *ifs, int flag,
536     int set)
537 {
538         struct ifbreq req;
539
540         strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
541
542         if (do_cmd(sock, bridge, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
543                 err(1, "unable to get bridge flags");
544
545         if (set)
546                 req.ifbr_ifsflags |= flag;
547         else
548                 req.ifbr_ifsflags &= ~flag;
549
550         if (do_cmd(sock, bridge, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
551                 err(1, "unable to set bridge flags");
552 }
553
554 void
555 cmd_add(const struct command *cmd, int sock, const char *bridge,
556     char **argv)
557 {
558         struct ifbreq req;
559
560         memset(&req, 0, sizeof(req));
561
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]);
565 }
566
567 void
568 cmd_delete(const struct command *cmd, int sock, const char *bridge,
569     char **argv)
570 {
571         struct ifbreq req;
572
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]);
577 }
578
579 void
580 cmd_up(const struct command *cmd, int sock, const char *bridge,
581     char **argv)
582 {
583
584         do_ifflag(sock, bridge, IFF_UP, 1);
585 }
586
587 void
588 cmd_down(const struct command *cmd, int sock, const char *bridge,
589     char **argv)
590 {
591
592         do_ifflag(sock, bridge, IFF_UP, 0);
593 }
594
595 void
596 cmd_discover(const struct command *cmd, int sock, const char *bridge,
597     char **argv)
598 {
599
600         do_bridgeflag(sock, bridge, argv[0], IFBIF_DISCOVER,
601             (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
602 }
603
604 void
605 cmd_learn(const struct command *cmd, int sock, const char *bridge,
606     char **argv)
607 {
608
609         do_bridgeflag(sock, bridge, argv[0], IFBIF_LEARNING,
610             (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
611 }
612
613 void
614 cmd_stp(const struct command *cmd, int sock, const char *bridge,
615     char **argv)
616 {
617
618         do_bridgeflag(sock, bridge, argv[0], IFBIF_STP,
619             (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
620 }
621
622 void
623 cmd_flush(const struct command *cmd, int sock, const char *bridge,
624     char **argv)
625 {
626         struct ifbreq req;
627
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);
632 }
633
634 void
635 cmd_flushall(const struct command *cmd, int sock, const char *bridge,
636     char **argv)
637 {
638         struct ifbreq req;
639
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);
644 }
645
646 void
647 cmd_static(const struct command *cmd, int sock, const char *bridge,
648     char **argv)
649 {
650         struct ifbareq req;
651         struct ether_addr *ea;
652
653         memset(&req, 0, sizeof(req));
654         strlcpy(req.ifba_ifsname, argv[0], sizeof(req.ifba_ifsname));
655
656         ea = ether_aton(argv[1]);
657         if (ea == NULL)
658                 errx(1, "%s: invalid address: %s", cmd->cmd_keyword, argv[1]);
659
660         memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
661         req.ifba_flags = IFBAF_STATIC;
662
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]);
665 }
666
667 void
668 cmd_deladdr(const struct command *cmd, int sock, const char *bridge,
669     char **argv)
670 {
671         struct ifbareq req;
672         struct ether_addr *ea;
673
674         memset(&req, 0, sizeof(req));
675
676         ea = ether_aton(argv[0]);
677         if (ea == NULL)
678                 errx(1, "%s: invalid address: %s", cmd->cmd_keyword, argv[0]);
679
680         memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
681
682         if (do_cmd(sock, bridge, BRDGDADDR, &req, sizeof(req), 1) < 0)
683                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
684 }
685
686 void
687 cmd_addr(const struct command *cmd, int sock, const char *bridge,
688     char **argv)
689 {
690
691         show_addresses(sock, bridge, "\t");
692 }
693
694 void
695 cmd_maxaddr(const struct command *cmd, int sock, const char *bridge,
696     char **argv)
697 {
698         struct ifbrparam param;
699         u_long val;
700
701         if (get_val(argv[0], &val) < 0 || (val & ~0xffffffff) != 0)
702                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
703
704         param.ifbrp_csize = val & 0xffffffff;
705
706         if (do_cmd(sock, bridge, BRDGSCACHE, &param, sizeof(param), 1) < 0)
707                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
708 }
709
710 void
711 cmd_hellotime(const struct command *cmd, int sock, const char *bridge,
712     char **argv)
713 {
714         struct ifbrparam param;
715         u_long val;
716
717         if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
718                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
719
720         param.ifbrp_hellotime = val & 0xff;
721
722         if (do_cmd(sock, bridge, BRDGSHT, &param, sizeof(param), 1) < 0)
723                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
724 }
725
726 void
727 cmd_fwddelay(const struct command *cmd, int sock, const char *bridge,
728     char **argv)
729 {
730         struct ifbrparam param;
731         u_long val;
732
733         if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
734                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
735
736         param.ifbrp_fwddelay = val & 0xff;
737
738         if (do_cmd(sock, bridge, BRDGSFD, &param, sizeof(param), 1) < 0)
739                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
740 }
741
742 void
743 cmd_maxage(const struct command *cmd, int sock, const char *bridge,
744     char **argv)
745 {
746         struct ifbrparam param;
747         u_long val;
748
749         if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
750                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
751
752         param.ifbrp_maxage = val & 0xff;
753
754         if (do_cmd(sock, bridge, BRDGSMA, &param, sizeof(param), 1) < 0)
755                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
756 }
757
758 void
759 cmd_priority(const struct command *cmd, int sock, const char *bridge,
760     char **argv)
761 {
762         struct ifbrparam param;
763         u_long val;
764
765         if (get_val(argv[0], &val) < 0 || (val & ~0xffff) != 0)
766                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
767
768         param.ifbrp_prio = val & 0xffff;
769
770         if (do_cmd(sock, bridge, BRDGSPRI, &param, sizeof(param), 1) < 0)
771                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
772 }
773
774 void
775 cmd_ifpriority(const struct command *cmd, int sock, const char *bridge,
776     char **argv)
777 {
778         struct ifbreq req;
779         u_long val;
780
781         memset(&req, 0, sizeof(req));
782
783         if (get_val(argv[1], &val) < 0 || (val & ~0xff) != 0)
784                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[1]);
785
786         strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
787         req.ifbr_priority = val & 0xff;
788
789         if (do_cmd(sock, bridge, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
790                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
791 }
792
793 void
794 cmd_ifpathcost(const struct command *cmd, int sock, const char *bridge,
795     char **argv)
796 {
797         struct ifbreq req;
798         u_long val;
799
800         memset(&req, 0, sizeof(req));
801
802         if (get_val(argv[1], &val) < 0 || (val & ~0xff) != 0)
803                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[1]);
804
805         strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
806         req.ifbr_path_cost = val & 0xffff;
807
808         if (do_cmd(sock, bridge, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
809                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
810 }
811
812 void
813 cmd_timeout(const struct command *cmd, int sock, const char *bridge,
814     char **argv)
815 {
816         struct ifbrparam param;
817         u_long val;
818
819         if (get_val(argv[0], &val) < 0 || (val & ~0xffffffff) != 0)
820                 errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
821
822         param.ifbrp_ctime = val & 0xffffffff;
823
824         if (do_cmd(sock, bridge, BRDGSTO, &param, sizeof(param), 1) < 0)
825                 err(1, "%s %s", cmd->cmd_keyword, argv[0]);
826 }