xref: /freebsd/sbin/ifconfig/ifbridge.c (revision 0f7f3352c8bc463607912e2463d13e52d44a4cae)
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 
36 #ifndef lint
37 static const char rcsid[] =
38   "$FreeBSD$";
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_bridgevar.h>
52 #include <net/route.h>
53 
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <err.h>
60 #include <errno.h>
61 
62 #include "ifconfig.h"
63 
64 #define PV2ID(pv, epri, eaddr)  do {		\
65 		epri     = pv >> 48;		\
66 		eaddr[0] = pv >> 40;		\
67 		eaddr[1] = pv >> 32;		\
68 		eaddr[2] = pv >> 24;		\
69 		eaddr[3] = pv >> 16;		\
70 		eaddr[4] = pv >> 8;		\
71 		eaddr[5] = pv >> 0;		\
72 } while (0)
73 
74 static const char *stpstates[] = {
75 	"disabled",
76 	"listening",
77 	"learning",
78 	"forwarding",
79 	"blocking",
80 	"discarding"
81 };
82 static const char *stpproto[] = {
83 	"stp",
84 	"-",
85 	"rstp"
86 };
87 static const char *stproles[] = {
88 	"disabled",
89 	"root",
90 	"designated",
91 	"alternate",
92 	"backup"
93 };
94 
95 static int
96 get_val(const char *cp, u_long *valp)
97 {
98 	char *endptr;
99 	u_long val;
100 
101 	errno = 0;
102 	val = strtoul(cp, &endptr, 0);
103 	if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
104 		return (-1);
105 
106 	*valp = val;
107 	return (0);
108 }
109 
110 static int
111 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
112 {
113 	struct ifdrv ifd;
114 
115 	memset(&ifd, 0, sizeof(ifd));
116 
117 	strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
118 	ifd.ifd_cmd = op;
119 	ifd.ifd_len = argsize;
120 	ifd.ifd_data = arg;
121 
122 	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
123 }
124 
125 static void
126 do_bridgeflag(int sock, const char *ifs, int flag, int set)
127 {
128 	struct ifbreq req;
129 
130 	strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
131 
132 	if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
133 		err(1, "unable to get bridge flags");
134 
135 	if (set)
136 		req.ifbr_ifsflags |= flag;
137 	else
138 		req.ifbr_ifsflags &= ~flag;
139 
140 	if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
141 		err(1, "unable to set bridge flags");
142 }
143 
144 static void
145 bridge_interfaces(int s, const char *prefix)
146 {
147 	struct ifbifconf bifc;
148 	struct ifbreq *req;
149 	char *inbuf = NULL, *ninbuf;
150 	char *p, *pad;
151 	int i, len = 8192;
152 
153 	pad = strdup(prefix);
154 	if (pad == NULL)
155 		err(1, "strdup");
156 	/* replace the prefix with whitespace */
157 	for (p = pad; *p != '\0'; p++) {
158 		if(isprint(*p))
159 			*p = ' ';
160 	}
161 
162 	for (;;) {
163 		ninbuf = realloc(inbuf, len);
164 		if (ninbuf == NULL)
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)
171 			break;
172 		len *= 2;
173 	}
174 
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);
179 		printf("\n");
180 
181 		printf("%s", pad);
182 		printf("ifmaxaddr %u", req->ifbr_addrmax);
183 		printf(" port %u priority %u", req->ifbr_portno,
184 		    req->ifbr_priority);
185 		printf(" path cost %u", req->ifbr_path_cost);
186 
187 		if (req->ifbr_ifsflags & IFBIF_STP) {
188 			if (req->ifbr_proto < nitems(stpproto))
189 				printf(" proto %s", stpproto[req->ifbr_proto]);
190 			else
191 				printf(" <unknown proto %d>",
192 				    req->ifbr_proto);
193 
194 			printf("\n%s", pad);
195 			if (req->ifbr_role < nitems(stproles))
196 				printf("role %s", stproles[req->ifbr_role]);
197 			else
198 				printf("<unknown role %d>",
199 				    req->ifbr_role);
200 			if (req->ifbr_state < nitems(stpstates))
201 				printf(" state %s", stpstates[req->ifbr_state]);
202 			else
203 				printf(" <unknown state %d>",
204 				    req->ifbr_state);
205 		}
206 		printf("\n");
207 	}
208 
209 	free(inbuf);
210 }
211 
212 static void
213 bridge_addresses(int s, const char *prefix)
214 {
215 	struct ifbaconf ifbac;
216 	struct ifbareq *ifba;
217 	char *inbuf = NULL, *ninbuf;
218 	int i, len = 8192;
219 	struct ether_addr ea;
220 
221 	for (;;) {
222 		ninbuf = realloc(inbuf, len);
223 		if (ninbuf == NULL)
224 			err(1, "unable to allocate address buffer");
225 		ifbac.ifbac_len = len;
226 		ifbac.ifbac_buf = inbuf = ninbuf;
227 		if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
228 			err(1, "unable to get address cache");
229 		if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
230 			break;
231 		len *= 2;
232 	}
233 
234 	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
235 		ifba = ifbac.ifbac_req + i;
236 		memcpy(ea.octet, ifba->ifba_dst,
237 		    sizeof(ea.octet));
238 		printf("%s%s Vlan%d %s %lu ", prefix, ether_ntoa(&ea),
239 		    ifba->ifba_vlan, ifba->ifba_ifsname, ifba->ifba_expire);
240 		printb("flags", ifba->ifba_flags, IFBAFBITS);
241 		printf("\n");
242 	}
243 
244 	free(inbuf);
245 }
246 
247 static void
248 bridge_status(int s)
249 {
250 	struct ifbropreq ifbp;
251 	struct ifbrparam param;
252 	u_int16_t pri;
253 	u_int8_t ht, fd, ma, hc, pro;
254 	u_int8_t lladdr[ETHER_ADDR_LEN];
255 	u_int16_t bprio;
256 	u_int32_t csize, ctime;
257 
258 	if (do_cmd(s, BRDGGCACHE, &param, sizeof(param), 0) < 0)
259 		return;
260 	csize = param.ifbrp_csize;
261 	if (do_cmd(s, BRDGGTO, &param, sizeof(param), 0) < 0)
262 		return;
263 	ctime = param.ifbrp_ctime;
264 	if (do_cmd(s, BRDGPARAM, &ifbp, sizeof(ifbp), 0) < 0)
265 		return;
266 	pri = ifbp.ifbop_priority;
267 	pro = ifbp.ifbop_protocol;
268 	ht = ifbp.ifbop_hellotime;
269 	fd = ifbp.ifbop_fwddelay;
270 	hc = ifbp.ifbop_holdcount;
271 	ma = ifbp.ifbop_maxage;
272 
273 	PV2ID(ifbp.ifbop_bridgeid, bprio, lladdr);
274 	printf("\tid %s priority %u hellotime %u fwddelay %u\n",
275 	    ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
276 	printf("\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
277 	    ma, hc, stpproto[pro], csize, ctime);
278 
279 	PV2ID(ifbp.ifbop_designated_root, bprio, lladdr);
280 	printf("\troot id %s priority %d ifcost %u port %u\n",
281 	    ether_ntoa((struct ether_addr *)lladdr), bprio,
282 	    ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
283 
284 	bridge_interfaces(s, "\tmember: ");
285 
286 	return;
287 
288 }
289 
290 static void
291 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
292 {
293 	struct ifbreq req;
294 
295 	memset(&req, 0, sizeof(req));
296 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
297 	if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
298 		err(1, "BRDGADD %s",  val);
299 }
300 
301 static void
302 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
303 {
304 	struct ifbreq req;
305 
306 	memset(&req, 0, sizeof(req));
307 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
308 	if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
309 		err(1, "BRDGDEL %s",  val);
310 }
311 
312 static void
313 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
314 {
315 
316 	do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
317 }
318 
319 static void
320 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
321 {
322 
323 	do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
324 }
325 
326 static void
327 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
328 {
329 
330 	do_bridgeflag(s, val, IFBIF_LEARNING,  1);
331 }
332 
333 static void
334 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
335 {
336 
337 	do_bridgeflag(s, val, IFBIF_LEARNING,  0);
338 }
339 
340 static void
341 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
342 {
343 
344 	do_bridgeflag(s, val, IFBIF_STICKY,  1);
345 }
346 
347 static void
348 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
349 {
350 
351 	do_bridgeflag(s, val, IFBIF_STICKY,  0);
352 }
353 
354 static void
355 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
356 {
357 	struct ifbreq req;
358 
359 	memset(&req, 0, sizeof(req));
360 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
361 	if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
362 		err(1, "BRDGADDS %s",  val);
363 }
364 
365 static void
366 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
367 {
368 	struct ifbreq req;
369 
370 	memset(&req, 0, sizeof(req));
371 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
372 	if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
373 		err(1, "BRDGDELS %s",  val);
374 }
375 
376 static void
377 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
378 {
379 
380 	do_bridgeflag(s, val, IFBIF_STP, 1);
381 }
382 
383 static void
384 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
385 {
386 
387 	do_bridgeflag(s, val, IFBIF_STP, 0);
388 }
389 
390 static void
391 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
392 {
393 	do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
394 }
395 
396 static void
397 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
398 {
399 	do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
400 }
401 
402 static void
403 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
404 {
405 	do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
406 }
407 
408 static void
409 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
410 {
411 	do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
412 }
413 
414 static void
415 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
416 {
417 	do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
418 }
419 
420 static void
421 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
422 {
423 	do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
424 }
425 
426 static void
427 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
428 {
429 	do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
430 }
431 
432 static void
433 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
434 {
435 	do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
436 }
437 
438 static void
439 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
440 {
441 	struct ifbreq req;
442 
443 	memset(&req, 0, sizeof(req));
444 	req.ifbr_ifsflags = IFBF_FLUSHDYN;
445 	if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
446 		err(1, "BRDGFLUSH");
447 }
448 
449 static void
450 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
451 {
452 	struct ifbreq req;
453 
454 	memset(&req, 0, sizeof(req));
455 	req.ifbr_ifsflags = IFBF_FLUSHALL;
456 	if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
457 		err(1, "BRDGFLUSH");
458 }
459 
460 static void
461 setbridge_static(const char *val, const char *mac, int s,
462     const struct afswtch *afp)
463 {
464 	struct ifbareq req;
465 	struct ether_addr *ea;
466 
467 	memset(&req, 0, sizeof(req));
468 	strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
469 
470 	ea = ether_aton(mac);
471 	if (ea == NULL)
472 		errx(1, "%s: invalid address: %s", val, mac);
473 
474 	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
475 	req.ifba_flags = IFBAF_STATIC;
476 	req.ifba_vlan = 1; /* XXX allow user to specify */
477 
478 	if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
479 		err(1, "BRDGSADDR %s",  val);
480 }
481 
482 static void
483 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
484 {
485 	struct ifbareq req;
486 	struct ether_addr *ea;
487 
488 	memset(&req, 0, sizeof(req));
489 
490 	ea = ether_aton(val);
491 	if (ea == NULL)
492 		errx(1, "invalid address: %s",  val);
493 
494 	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
495 
496 	if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
497 		err(1, "BRDGDADDR %s",  val);
498 }
499 
500 static void
501 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
502 {
503 
504 	bridge_addresses(s, "");
505 }
506 
507 static void
508 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
509 {
510 	struct ifbrparam param;
511 	u_long val;
512 
513 	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
514 		errx(1, "invalid value: %s",  arg);
515 
516 	param.ifbrp_csize = val & 0xffffffff;
517 
518 	if (do_cmd(s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
519 		err(1, "BRDGSCACHE %s",  arg);
520 }
521 
522 static void
523 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
524 {
525 	struct ifbrparam param;
526 	u_long val;
527 
528 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
529 		errx(1, "invalid value: %s",  arg);
530 
531 	param.ifbrp_hellotime = val & 0xff;
532 
533 	if (do_cmd(s, BRDGSHT, &param, sizeof(param), 1) < 0)
534 		err(1, "BRDGSHT %s",  arg);
535 }
536 
537 static void
538 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
539 {
540 	struct ifbrparam param;
541 	u_long val;
542 
543 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
544 		errx(1, "invalid value: %s",  arg);
545 
546 	param.ifbrp_fwddelay = val & 0xff;
547 
548 	if (do_cmd(s, BRDGSFD, &param, sizeof(param), 1) < 0)
549 		err(1, "BRDGSFD %s",  arg);
550 }
551 
552 static void
553 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
554 {
555 	struct ifbrparam param;
556 	u_long val;
557 
558 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
559 		errx(1, "invalid value: %s",  arg);
560 
561 	param.ifbrp_maxage = val & 0xff;
562 
563 	if (do_cmd(s, BRDGSMA, &param, sizeof(param), 1) < 0)
564 		err(1, "BRDGSMA %s",  arg);
565 }
566 
567 static void
568 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
569 {
570 	struct ifbrparam param;
571 	u_long val;
572 
573 	if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
574 		errx(1, "invalid value: %s",  arg);
575 
576 	param.ifbrp_prio = val & 0xffff;
577 
578 	if (do_cmd(s, BRDGSPRI, &param, sizeof(param), 1) < 0)
579 		err(1, "BRDGSPRI %s",  arg);
580 }
581 
582 static void
583 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
584 {
585 	struct ifbrparam param;
586 
587 	if (strcasecmp(arg, "stp") == 0) {
588 		param.ifbrp_proto = 0;
589 	} else if (strcasecmp(arg, "rstp") == 0) {
590 		param.ifbrp_proto = 2;
591 	} else {
592 		errx(1, "unknown stp protocol");
593 	}
594 
595 	if (do_cmd(s, BRDGSPROTO, &param, sizeof(param), 1) < 0)
596 		err(1, "BRDGSPROTO %s",  arg);
597 }
598 
599 static void
600 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
601 {
602 	struct ifbrparam param;
603 	u_long val;
604 
605 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
606 		errx(1, "invalid value: %s",  arg);
607 
608 	param.ifbrp_txhc = val & 0xff;
609 
610 	if (do_cmd(s, BRDGSTXHC, &param, sizeof(param), 1) < 0)
611 		err(1, "BRDGSTXHC %s",  arg);
612 }
613 
614 static void
615 setbridge_ifpriority(const char *ifn, const char *pri, int s,
616     const struct afswtch *afp)
617 {
618 	struct ifbreq req;
619 	u_long val;
620 
621 	memset(&req, 0, sizeof(req));
622 
623 	if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
624 		errx(1, "invalid value: %s",  pri);
625 
626 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
627 	req.ifbr_priority = val & 0xff;
628 
629 	if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
630 		err(1, "BRDGSIFPRIO %s",  pri);
631 }
632 
633 static void
634 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
635     const struct afswtch *afp)
636 {
637 	struct ifbreq req;
638 	u_long val;
639 
640 	memset(&req, 0, sizeof(req));
641 
642 	if (get_val(cost, &val) < 0)
643 		errx(1, "invalid value: %s",  cost);
644 
645 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
646 	req.ifbr_path_cost = val;
647 
648 	if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
649 		err(1, "BRDGSIFCOST %s",  cost);
650 }
651 
652 static void
653 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
654     const struct afswtch *afp)
655 {
656 	struct ifbreq req;
657 	u_long val;
658 
659 	memset(&req, 0, sizeof(req));
660 
661 	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
662 		errx(1, "invalid value: %s",  arg);
663 
664 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
665 	req.ifbr_addrmax = val & 0xffffffff;
666 
667 	if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
668 		err(1, "BRDGSIFAMAX %s",  arg);
669 }
670 
671 static void
672 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
673 {
674 	struct ifbrparam param;
675 	u_long val;
676 
677 	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
678 		errx(1, "invalid value: %s",  arg);
679 
680 	param.ifbrp_ctime = val & 0xffffffff;
681 
682 	if (do_cmd(s, BRDGSTO, &param, sizeof(param), 1) < 0)
683 		err(1, "BRDGSTO %s",  arg);
684 }
685 
686 static void
687 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
688 {
689 
690 	do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
691 }
692 
693 static void
694 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
695 {
696 
697 	do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
698 }
699 
700 static struct cmd bridge_cmds[] = {
701 	DEF_CMD_ARG("addm",		setbridge_add),
702 	DEF_CMD_ARG("deletem",		setbridge_delete),
703 	DEF_CMD_ARG("discover",		setbridge_discover),
704 	DEF_CMD_ARG("-discover",	unsetbridge_discover),
705 	DEF_CMD_ARG("learn",		setbridge_learn),
706 	DEF_CMD_ARG("-learn",		unsetbridge_learn),
707 	DEF_CMD_ARG("sticky",		setbridge_sticky),
708 	DEF_CMD_ARG("-sticky",		unsetbridge_sticky),
709 	DEF_CMD_ARG("span",		setbridge_span),
710 	DEF_CMD_ARG("-span",		unsetbridge_span),
711 	DEF_CMD_ARG("stp",		setbridge_stp),
712 	DEF_CMD_ARG("-stp",		unsetbridge_stp),
713 	DEF_CMD_ARG("edge",		setbridge_edge),
714 	DEF_CMD_ARG("-edge",		unsetbridge_edge),
715 	DEF_CMD_ARG("autoedge",		setbridge_autoedge),
716 	DEF_CMD_ARG("-autoedge",	unsetbridge_autoedge),
717 	DEF_CMD_ARG("ptp",		setbridge_ptp),
718 	DEF_CMD_ARG("-ptp",		unsetbridge_ptp),
719 	DEF_CMD_ARG("autoptp",		setbridge_autoptp),
720 	DEF_CMD_ARG("-autoptp",		unsetbridge_autoptp),
721 	DEF_CMD("flush", 0,		setbridge_flush),
722 	DEF_CMD("flushall", 0,		setbridge_flushall),
723 	DEF_CMD_ARG2("static",		setbridge_static),
724 	DEF_CMD_ARG("deladdr",		setbridge_deladdr),
725 	DEF_CMD("addr",	 1,		setbridge_addr),
726 	DEF_CMD_ARG("maxaddr",		setbridge_maxaddr),
727 	DEF_CMD_ARG("hellotime",	setbridge_hellotime),
728 	DEF_CMD_ARG("fwddelay",		setbridge_fwddelay),
729 	DEF_CMD_ARG("maxage",		setbridge_maxage),
730 	DEF_CMD_ARG("priority",		setbridge_priority),
731 	DEF_CMD_ARG("proto",		setbridge_protocol),
732 	DEF_CMD_ARG("holdcnt",		setbridge_holdcount),
733 	DEF_CMD_ARG2("ifpriority",	setbridge_ifpriority),
734 	DEF_CMD_ARG2("ifpathcost",	setbridge_ifpathcost),
735 	DEF_CMD_ARG2("ifmaxaddr",	setbridge_ifmaxaddr),
736 	DEF_CMD_ARG("timeout",		setbridge_timeout),
737 	DEF_CMD_ARG("private",		setbridge_private),
738 	DEF_CMD_ARG("-private",		unsetbridge_private),
739 };
740 static struct afswtch af_bridge = {
741 	.af_name	= "af_bridge",
742 	.af_af		= AF_UNSPEC,
743 	.af_other_status = bridge_status,
744 };
745 
746 static __constructor void
747 bridge_ctor(void)
748 {
749 	int i;
750 
751 	for (i = 0; i < nitems(bridge_cmds);  i++)
752 		cmd_register(&bridge_cmds[i]);
753 	af_register(&af_bridge);
754 }
755