1/*	$NetBSD: brconfig.c,v 1.12 2009/03/16 12:56:19 lukem Exp $	*/
2
3/*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed for the NetBSD Project by
20 *	Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 *    or promote products derived from this software without specific prior
23 *    written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * brconfig(8) --
40 *
41 *	Configuration utility for the bridge(4) driver.
42 */
43#include <sys/cdefs.h>
44
45#ifndef lint
46__RCSID("$NetBSD: brconfig.c,v 1.12 2009/03/16 12:56:19 lukem Exp $");
47#endif
48
49
50#include <sys/param.h>
51#include <sys/socket.h>
52#include <sys/ioctl.h>
53
54#include <net/if.h>
55#include <net/if_ether.h>
56#include <net/if_bridgevar.h>
57
58#include <ctype.h>
59#include <err.h>
60#include <errno.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65#include <ifaddrs.h>
66
67struct command {
68	const char *cmd_keyword;
69	int	cmd_argcnt;
70	int	cmd_flags;
71	void	(*cmd_func)(const struct command *, int, const char *,
72		    char **);
73};
74
75#define	CMD_INVERT	0x01	/* "invert" the sense of the command */
76
77static void	cmd_add(const struct command *, int, const char *, char **);
78static void	cmd_delete(const struct command *, int, const char *, char **);
79static void	cmd_up(const struct command *, int, const char *, char **);
80static void	cmd_down(const struct command *, int, const char *, char **);
81static void	cmd_discover(const struct command *, int, const char *, char **);
82static void	cmd_learn(const struct command *, int, const char *, char **);
83static void	cmd_flush(const struct command *, int, const char *, char **);
84static void	cmd_flushall(const struct command *, int, const char *, char **);
85static void	cmd_static(const struct command *, int, const char *, char **);
86static void	cmd_deladdr(const struct command *, int, const char *, char **);
87static void	cmd_addr(const struct command *, int, const char *, char **);
88static void	cmd_maxaddr(const struct command *, int, const char *, char **);
89static void	cmd_hellotime(const struct command *, int, const char *, char **);
90static void	cmd_fwddelay(const struct command *, int, const char *, char **);
91static void	cmd_maxage(const struct command *, int, const char *, char **);
92static void	cmd_priority(const struct command *, int, const char *, char **);
93static void	cmd_ifpriority(const struct command *, int, const char *, char **);
94static void	cmd_ifpathcost(const struct command *, int, const char *, char **);
95static void	cmd_timeout(const struct command *, int, const char *, char **);
96static void	cmd_stp(const struct command *, int, const char *, char **);
97static void	cmd_ipf(const struct command *, int, const char *, char **);
98
99static const struct command command_table[] = {
100	{ "add",		1,	0,		cmd_add },
101	{ "delete",		1,	0,		cmd_delete },
102
103	{ "up",			0,	0,		cmd_up },
104	{ "down",		0,	0,		cmd_down },
105
106	{ "discover",		1,	0,		cmd_discover },
107	{ "-discover",		1,	CMD_INVERT,	cmd_discover },
108
109	{ "learn",		1,	0,		cmd_learn },
110	{ "-learn",		1,	CMD_INVERT,	cmd_learn },
111
112	{ "flush",		0,	0,		cmd_flush },
113	{ "flushall",		0,	0,		cmd_flushall },
114
115	{ "static",		2,	0,		cmd_static },
116	{ "deladdr",		1,	0,		cmd_deladdr },
117
118	{ "addr",		0,	0,		cmd_addr },
119	{ "maxaddr",		1,	0,		cmd_maxaddr },
120
121	{ "hellotime",		1,	0,		cmd_hellotime },
122	{ "fwddelay",		1,	0,		cmd_fwddelay },
123	{ "maxage",		1,	0,		cmd_maxage },
124	{ "priority",		1,	0,		cmd_priority },
125	{ "ifpriority",		2,	0,		cmd_ifpriority },
126	{ "ifpathcost",		2,	0,		cmd_ifpathcost },
127	{ "timeout",		1,	0,		cmd_timeout },
128	{ "stp",		1,	0,		cmd_stp },
129	{ "-stp",		1,	CMD_INVERT,	cmd_stp },
130
131        { "ipf",                0,      0,              cmd_ipf },
132        { "-ipf",               0,      CMD_INVERT,     cmd_ipf },
133
134	{ NULL,			0,	0,		NULL },
135};
136
137static void	printall(int);
138static void	status(int, const char *);
139static int	is_bridge(const char *);
140static void	show_config(int, const char *, const char *);
141static void	show_interfaces(int, const char *, const char *);
142static void	show_addresses(int, const char *, const char *);
143static int	get_val(const char *, u_long *);
144static int	do_cmd(int, const char *, u_long, void *, size_t, int);
145static void	do_ifflag(int, const char *, int, int);
146static void	do_bridgeflag(int, const char *, const char *, int, int);
147
148static void	printb(const char *, u_int, const char *);
149
150__dead static void	usage(void);
151
152static int	aflag;
153
154static struct ifreq g_ifr;
155static int	g_ifr_updated;
156
157int
158main(int argc, char *argv[])
159{
160	const struct command *cmd;
161	char *bridge;
162	int sock, ch;
163
164	if (argc < 2)
165		usage();
166
167	sock = socket(AF_INET, SOCK_DGRAM, 0);
168	if (sock < 0)
169		err(1, "socket");
170
171	while ((ch = getopt(argc, argv, "a")) != -1) {
172		switch (ch) {
173		case 'a':
174			aflag = 1;
175			break;
176
177		default:
178			usage();
179		}
180	}
181
182	argc -= optind;
183	argv += optind;
184
185	if (aflag) {
186		if (argc != 0)
187			usage();
188		printall(sock);
189		exit(0);
190	}
191
192	if (argc == 0)
193		usage();
194
195	bridge = argv[0];
196
197	if (is_bridge(bridge) == 0)
198		errx(1, "%s is not a bridge", bridge);
199
200	/* Get a copy of the interface flags. */
201	strlcpy(g_ifr.ifr_name, bridge, sizeof(g_ifr.ifr_name));
202	if (ioctl(sock, SIOCGIFFLAGS, &g_ifr) < 0)
203		err(1, "unable to get interface flags");
204
205	argc--;
206	argv++;
207
208	if (argc == 0) {
209		status(sock, bridge);
210		exit(0);
211	}
212
213	while (argc != 0) {
214		for (cmd = command_table; cmd->cmd_keyword != NULL; cmd++) {
215			if (strcmp(cmd->cmd_keyword, argv[0]) == 0)
216				break;
217		}
218		if (cmd->cmd_keyword == NULL)
219			errx(1, "unknown command: %s", argv[0]);
220
221		argc--;
222		argv++;
223
224		if (argc < cmd->cmd_argcnt)
225			errx(1, "command %s requires %d argument%s",
226			    cmd->cmd_keyword, cmd->cmd_argcnt,
227			    cmd->cmd_argcnt == 1 ? "" : "s");
228
229		(*cmd->cmd_func)(cmd, sock, bridge, argv);
230
231		argc -= cmd->cmd_argcnt;
232		argv += cmd->cmd_argcnt;
233	}
234
235	/* If the flags changed, update them. */
236	if (g_ifr_updated && ioctl(sock, SIOCSIFFLAGS, &g_ifr) < 0)
237		err(1, "unable to set interface flags");
238
239	exit (0);
240}
241
242static void
243usage(void)
244{
245	static const char *usage_strings[] = {
246		"-a",
247		"<bridge>",
248		"<bridge> up|down",
249		"<bridge> addr",
250		"<bridge> add <interface>",
251		"<bridge> delete <interface>",
252		"<bridge> maxaddr <size>",
253		"<bridge> timeout <time>",
254		"<bridge> static <interface> <address>",
255		"<bridge> deladdr <address>",
256		"<bridge> flush",
257		"<bridge> flushall",
258		"<bridge> ipf|-ipf",
259		"<bridge> discover|-discover <interface>",
260		"<bridge> learn|-learn <interface>",
261		"<bridge> stp|-stp <interface>",
262		"<bridge> maxage <time>",
263		"<bridge> fwddelay <time>",
264		"<bridge> hellotime <time>",
265		"<bridge> priority <value>",
266		"<bridge> ifpriority <interface> <value>",
267		"<bridge> ifpathcost <interface> <value>",
268		NULL,
269	};
270	extern const char *__progname;
271	int i;
272
273	for (i = 0; usage_strings[i] != NULL; i++)
274		fprintf(stderr, "%s %s %s\n",
275		    i == 0 ? "usage:" : "      ",
276		    __progname, usage_strings[i]);
277
278	exit(1);
279}
280
281static int
282is_bridge(const char *bridge)
283{
284
285	if (strncmp(bridge, "bridge", 6) != 0 ||
286	    isdigit((unsigned char)bridge[6]) == 0)
287		return (0);
288
289	return (1);
290}
291
292static void
293printb(const char *s, u_int v, const char *bits)
294{
295	int i, any = 0;
296	char c;
297
298	if (bits && *bits == 8)
299		printf("%s=%o", s, v);
300	else
301		printf("%s=%x", s, v);
302	if (bits) {
303		bits++;
304		putchar('<');
305		while ((i = *bits++) != 0) {
306			if (v & (1 << (i-1))) {
307				if (any)
308					putchar(',');
309				any = 1;
310				for (; (c = *bits) > 32; bits++)
311					putchar(c);
312			} else
313				for (; *bits > 32; bits++)
314					;
315		}
316		putchar('>');
317	}
318}
319
320static void
321printall(int sock)
322{
323	struct ifaddrs *ifap, *ifa;
324	char *p;
325
326	if (getifaddrs(&ifap) != 0)
327		err(1, "getifaddrs");
328	p = NULL;
329	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
330		if (is_bridge(ifa->ifa_name) == 0)
331			continue;
332		if (p != NULL && strcmp(p, ifa->ifa_name) == 0)
333			continue;
334		p = ifa->ifa_name;
335		status(sock, ifa->ifa_name);
336	}
337
338	freeifaddrs(ifap);
339}
340
341static void
342status(int sock, const char *bridge)
343{
344	struct ifreq ifr;
345	struct ifbrparam bp1, bp2;
346
347	memset(&ifr, 0, sizeof(ifr));
348
349	strlcpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
350	if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
351		err(1, "unable to get flags");
352
353	printf("%s: ", bridge);
354	printb("flags", ifr.ifr_flags, IFFBITS);
355	printf("\n");
356
357	printf("\tConfiguration:\n");
358	show_config(sock, bridge, "\t\t");
359
360	printf("\tInterfaces:\n");
361	show_interfaces(sock, bridge, "\t\t");
362
363	if (do_cmd(sock, bridge, BRDGGCACHE, &bp1, sizeof(bp1), 0) < 0)
364		err(1, "unable to get address cache size");
365	if (do_cmd(sock, bridge, BRDGGTO, &bp2, sizeof(bp2), 0) < 0)
366		err(1, "unable to get address timeout");
367
368	printf("\tAddress cache (max cache: %u, timeout: %u):\n",
369	    bp1.ifbrp_csize, bp2.ifbrp_ctime);
370	show_addresses(sock, bridge, "\t\t");
371}
372
373static void
374show_config(int sock, const char *bridge, const char *prefix)
375{
376	struct ifbrparam param;
377	u_int32_t ipfflags;
378	u_int16_t pri;
379	u_int8_t ht, fd, ma;
380
381	if (do_cmd(sock, bridge, BRDGGPRI, &param, sizeof(param), 0) < 0)
382		err(1, "unable to get bridge priority");
383	pri = param.ifbrp_prio;
384
385	if (do_cmd(sock, bridge, BRDGGHT, &param, sizeof(param), 0) < 0)
386		err(1, "unable to get hellotime");
387	ht = param.ifbrp_hellotime;
388
389	if (do_cmd(sock, bridge, BRDGGFD, &param, sizeof(param), 0) < 0)
390		err(1, "unable to get forward delay");
391	fd = param.ifbrp_fwddelay;
392
393	if (do_cmd(sock, bridge, BRDGGMA, &param, sizeof(param), 0) < 0)
394		err(1, "unable to get max age");
395	ma = param.ifbrp_maxage;
396
397	printf("%spriority %u hellotime %u fwddelay %u maxage %u\n",
398	    prefix, pri, ht, fd, ma);
399
400	if (do_cmd(sock, bridge, BRDGGFILT, &param, sizeof(param), 0) < 0) {
401		/* err(1, "unable to get ipfilter status"); */
402		param.ifbrp_filter = 0;
403	}
404
405	ipfflags = param.ifbrp_filter;
406	printf("%sipfilter %s flags 0x%x\n", prefix,
407		(ipfflags & IFBF_FILT_USEIPF) ? "enabled" : "disabled",
408		ipfflags);
409}
410
411static void
412show_interfaces(int sock, const char *bridge, const char *prefix)
413{
414	static const char *stpstates[] = {
415		"disabled",
416		"listening",
417		"learning",
418		"forwarding",
419		"blocking",
420	};
421	struct ifbifconf bifc;
422	struct ifbreq *req;
423	char *inbuf = NULL, *ninbuf;
424	uint32_t i, len = 8192;
425
426	for (;;) {
427		ninbuf = realloc(inbuf, len);
428		if (ninbuf == NULL)
429			err(1, "unable to allocate interface buffer");
430		bifc.ifbic_len = len;
431		bifc.ifbic_buf = inbuf = ninbuf;
432		if (do_cmd(sock, bridge, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
433			err(1, "unable to get interface list");
434		if ((bifc.ifbic_len + sizeof(*req)) < len)
435			break;
436		len *= 2;
437	}
438
439	for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
440		req = bifc.ifbic_req + i;
441		printf("%s%s ", prefix, req->ifbr_ifsname);
442		printb("flags", req->ifbr_ifsflags, IFBIFBITS);
443		printf("\n");
444		printf("%s\t", prefix);
445		printf("port %u priority %u",
446		    req->ifbr_portno, req->ifbr_priority);
447		if (req->ifbr_ifsflags & IFBIF_STP) {
448			printf(" path cost %u", req->ifbr_path_cost);
449			if (req->ifbr_state <
450			    sizeof(stpstates) / sizeof(stpstates[0]))
451				printf(" %s", stpstates[req->ifbr_state]);
452			else
453				printf(" <unknown state %d>",
454				    req->ifbr_state);
455		}
456		printf("\n");
457	}
458
459	free(inbuf);
460}
461
462static void
463show_addresses(int sock, const char *bridge, const char *prefix)
464{
465	struct ifbaconf ifbac;
466	struct ifbareq *ifba;
467	char *inbuf = NULL, *ninbuf;
468	uint32_t i, len = 8192;
469	struct ether_addr ea;
470
471	for (;;) {
472		ninbuf = realloc(inbuf, len);
473		if (ninbuf == NULL)
474			err(1, "unable to allocate address buffer");
475		ifbac.ifbac_len = len;
476		ifbac.ifbac_buf = inbuf = ninbuf;
477		if (do_cmd(sock, bridge, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
478			err(1, "unable to get address cache");
479		if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
480			break;
481		len *= 2;
482	}
483
484	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
485		ifba = ifbac.ifbac_req + i;
486		memcpy(ea.ether_addr_octet, ifba->ifba_dst,
487		    sizeof(ea.ether_addr_octet));
488		printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
489		    ifba->ifba_ifsname, ifba->ifba_expire);
490		printb("flags", ifba->ifba_flags, IFBAFBITS);
491		printf("\n");
492	}
493
494	free(inbuf);
495}
496
497static int
498get_val(const char *cp, u_long *valp)
499{
500	char *endptr;
501	u_long val;
502
503	errno = 0;
504	val = strtoul(cp, &endptr, 0);
505	if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
506		return (-1);
507
508	*valp = val;
509	return (0);
510}
511
512static int
513do_cmd(int sock, const char *bridge, u_long op, void *arg, size_t argsize,
514    int set)
515{
516	struct ifdrv ifd;
517
518	memset(&ifd, 0, sizeof(ifd));
519
520	strlcpy(ifd.ifd_name, bridge, sizeof(ifd.ifd_name));
521	ifd.ifd_cmd = op;
522	ifd.ifd_len = argsize;
523	ifd.ifd_data = arg;
524
525	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
526}
527
528static void
529do_ifflag(int sock, const char *bridge, int flag, int set)
530{
531
532	if (set)
533		g_ifr.ifr_flags |= flag;
534	else
535		g_ifr.ifr_flags &= ~flag;
536
537	g_ifr_updated = 1;
538}
539
540static void
541do_bridgeflag(int sock, const char *bridge, const char *ifs, int flag,
542    int set)
543{
544	struct ifbreq req;
545
546	strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
547
548	if (do_cmd(sock, bridge, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
549		err(1, "unable to get bridge flags");
550
551	if (set)
552		req.ifbr_ifsflags |= flag;
553	else
554		req.ifbr_ifsflags &= ~flag;
555
556	if (do_cmd(sock, bridge, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
557		err(1, "unable to set bridge flags");
558}
559
560static void
561cmd_add(const struct command *cmd, int sock, const char *bridge,
562    char **argv)
563{
564	struct ifbreq req;
565
566	memset(&req, 0, sizeof(req));
567
568	strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
569	if (do_cmd(sock, bridge, BRDGADD, &req, sizeof(req), 1) < 0)
570		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
571}
572
573static void
574cmd_delete(const struct command *cmd, int sock, const char *bridge,
575    char **argv)
576{
577	struct ifbreq req;
578
579	memset(&req, 0, sizeof(req));
580	strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
581	if (do_cmd(sock, bridge, BRDGDEL, &req, sizeof(req), 1) < 0)
582		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
583}
584
585static void
586cmd_up(const struct command *cmd, int sock, const char *bridge,
587    char **argv)
588{
589
590	do_ifflag(sock, bridge, IFF_UP, 1);
591}
592
593static void
594cmd_down(const struct command *cmd, int sock, const char *bridge,
595    char **argv)
596{
597
598	do_ifflag(sock, bridge, IFF_UP, 0);
599}
600
601static void
602cmd_discover(const struct command *cmd, int sock, const char *bridge,
603    char **argv)
604{
605
606	do_bridgeflag(sock, bridge, argv[0], IFBIF_DISCOVER,
607	    (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
608}
609
610static void
611cmd_learn(const struct command *cmd, int sock, const char *bridge,
612    char **argv)
613{
614
615	do_bridgeflag(sock, bridge, argv[0], IFBIF_LEARNING,
616	    (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
617}
618
619static void
620cmd_stp(const struct command *cmd, int sock, const char *bridge,
621    char **argv)
622{
623
624	do_bridgeflag(sock, bridge, argv[0], IFBIF_STP,
625	    (cmd->cmd_flags & CMD_INVERT) ? 0 : 1);
626}
627
628static void
629cmd_flush(const struct command *cmd, int sock, const char *bridge,
630    char **argv)
631{
632	struct ifbreq req;
633
634	memset(&req, 0, sizeof(req));
635	req.ifbr_ifsflags = IFBF_FLUSHDYN;
636	if (do_cmd(sock, bridge, BRDGFLUSH, &req, sizeof(req), 1) < 0)
637		err(1, "%s", cmd->cmd_keyword);
638}
639
640static void
641cmd_flushall(const struct command *cmd, int sock, const char *bridge,
642    char **argv)
643{
644	struct ifbreq req;
645
646	memset(&req, 0, sizeof(req));
647	req.ifbr_ifsflags = IFBF_FLUSHALL;
648	if (do_cmd(sock, bridge, BRDGFLUSH, &req, sizeof(req), 1) < 0)
649		err(1, "%s", cmd->cmd_keyword);
650}
651
652static void
653cmd_static(const struct command *cmd, int sock, const char *bridge,
654    char **argv)
655{
656	struct ifbareq req;
657	struct ether_addr *ea;
658
659	memset(&req, 0, sizeof(req));
660	strlcpy(req.ifba_ifsname, argv[0], sizeof(req.ifba_ifsname));
661
662	ea = ether_aton(argv[1]);
663	if (ea == NULL)
664		errx(1, "%s: invalid address: %s", cmd->cmd_keyword, argv[1]);
665
666	memcpy(req.ifba_dst, ea->ether_addr_octet, sizeof(req.ifba_dst));
667	req.ifba_flags = IFBAF_STATIC;
668
669	if (do_cmd(sock, bridge, BRDGSADDR, &req, sizeof(req), 1) < 0)
670		err(1, "%s %s %s", cmd->cmd_keyword, argv[0], argv[1]);
671}
672
673static void
674cmd_deladdr(const struct command *cmd, int sock, const char *bridge,
675    char **argv)
676{
677	struct ifbareq req;
678	struct ether_addr *ea;
679
680	memset(&req, 0, sizeof(req));
681
682	ea = ether_aton(argv[0]);
683	if (ea == NULL)
684		errx(1, "%s: invalid address: %s", cmd->cmd_keyword, argv[0]);
685
686	memcpy(req.ifba_dst, ea->ether_addr_octet, sizeof(req.ifba_dst));
687
688	if (do_cmd(sock, bridge, BRDGDADDR, &req, sizeof(req), 1) < 0)
689		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
690}
691
692static void
693cmd_addr(const struct command *cmd, int sock, const char *bridge,
694    char **argv)
695{
696
697	show_addresses(sock, bridge, "\t");
698}
699
700static void
701cmd_maxaddr(const struct command *cmd, int sock, const char *bridge,
702    char **argv)
703{
704	struct ifbrparam param;
705	u_long val;
706
707	if (get_val(argv[0], &val) < 0 || (val & ~0xffffffff) != 0)
708		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
709
710	param.ifbrp_csize = val & 0xffffffff;
711
712	if (do_cmd(sock, bridge, BRDGSCACHE, &param, sizeof(param), 1) < 0)
713		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
714}
715
716static void
717cmd_hellotime(const struct command *cmd, int sock, const char *bridge,
718    char **argv)
719{
720	struct ifbrparam param;
721	u_long val;
722
723	if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
724		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
725
726	param.ifbrp_hellotime = val & 0xff;
727
728	if (do_cmd(sock, bridge, BRDGSHT, &param, sizeof(param), 1) < 0)
729		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
730}
731
732static void
733cmd_fwddelay(const struct command *cmd, int sock, const char *bridge,
734    char **argv)
735{
736	struct ifbrparam param;
737	u_long val;
738
739	if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
740		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
741
742	param.ifbrp_fwddelay = val & 0xff;
743
744	if (do_cmd(sock, bridge, BRDGSFD, &param, sizeof(param), 1) < 0)
745		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
746}
747
748static void
749cmd_maxage(const struct command *cmd, int sock, const char *bridge,
750    char **argv)
751{
752	struct ifbrparam param;
753	u_long val;
754
755	if (get_val(argv[0], &val) < 0 || (val & ~0xff) != 0)
756		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
757
758	param.ifbrp_maxage = val & 0xff;
759
760	if (do_cmd(sock, bridge, BRDGSMA, &param, sizeof(param), 1) < 0)
761		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
762}
763
764static void
765cmd_priority(const struct command *cmd, int sock, const char *bridge,
766    char **argv)
767{
768	struct ifbrparam param;
769	u_long val;
770
771	if (get_val(argv[0], &val) < 0 || (val & ~0xffff) != 0)
772		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
773
774	param.ifbrp_prio = val & 0xffff;
775
776	if (do_cmd(sock, bridge, BRDGSPRI, &param, sizeof(param), 1) < 0)
777		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
778}
779
780static void
781cmd_ifpriority(const struct command *cmd, int sock, const char *bridge,
782    char **argv)
783{
784	struct ifbreq req;
785	u_long val;
786
787	memset(&req, 0, sizeof(req));
788
789	if (get_val(argv[1], &val) < 0 || (val & ~0xff) != 0)
790		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[1]);
791
792	strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
793	req.ifbr_priority = val & 0xff;
794
795	if (do_cmd(sock, bridge, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
796		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
797}
798
799static void
800cmd_ifpathcost(const struct command *cmd, int sock, const char *bridge,
801    char **argv)
802{
803	struct ifbreq req;
804	u_long val;
805
806	memset(&req, 0, sizeof(req));
807
808	if (get_val(argv[1], &val) < 0 || (val & ~0xff) != 0)
809		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[1]);
810
811	strlcpy(req.ifbr_ifsname, argv[0], sizeof(req.ifbr_ifsname));
812	req.ifbr_path_cost = val & 0xffff;
813
814	if (do_cmd(sock, bridge, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
815		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
816}
817
818static void
819cmd_timeout(const struct command *cmd, int sock, const char *bridge,
820    char **argv)
821{
822	struct ifbrparam param;
823	u_long val;
824
825	if (get_val(argv[0], &val) < 0 || (val & ~0xffffffff) != 0)
826		errx(1, "%s: invalid value: %s", cmd->cmd_keyword, argv[0]);
827
828	param.ifbrp_ctime = val & 0xffffffff;
829
830	if (do_cmd(sock, bridge, BRDGSTO, &param, sizeof(param), 1) < 0)
831		err(1, "%s %s", cmd->cmd_keyword, argv[0]);
832}
833
834static void
835cmd_ipf(const struct command *cmd, int sock, const char *bridge,
836    char **argv)
837{
838        struct ifbrparam param;
839
840        if (do_cmd(sock, bridge, BRDGGFILT, &param, sizeof(param), 0) < 0)
841		err(1, "%s", cmd->cmd_keyword);
842
843        param.ifbrp_filter &= ~IFBF_FILT_USEIPF;
844        param.ifbrp_filter |= (cmd->cmd_flags & CMD_INVERT) ? 0 : IFBF_FILT_USEIPF;
845        if (do_cmd(sock, bridge, BRDGSFILT, &param, sizeof(param), 1) < 0)
846		err(1, "%s %x", cmd->cmd_keyword, param.ifbrp_filter);
847}
848