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