xref: /dragonfly/sbin/ifconfig/ifbridge.c (revision 03517d4e)
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  * $FreeBSD: src/sbin/ifconfig/ifbridge.c,v 1.1.2.2 2005/12/28 04:12:58 thompsa Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <net/if.h>
43 #include <net/route.h>
44 #include <net/ethernet.h>
45 #include <net/bridge/if_bridgevar.h>
46 
47 #include <ctype.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <err.h>
53 #include <errno.h>
54 
55 #include "ifconfig.h"
56 
57 static int
58 get_val(const char *cp, u_long *valp)
59 {
60 	char *endptr;
61 	u_long val;
62 
63 	errno = 0;
64 	val = strtoul(cp, &endptr, 0);
65 	if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
66 		return (-1);
67 
68 	*valp = val;
69 	return (0);
70 }
71 
72 static int
73 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
74 {
75 	struct ifdrv ifd;
76 
77 	memset(&ifd, 0, sizeof(ifd));
78 
79 	strlcpy(ifd.ifd_name, IfName, sizeof(ifd.ifd_name));
80 	ifd.ifd_cmd = op;
81 	ifd.ifd_len = argsize;
82 	ifd.ifd_data = arg;
83 
84 	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
85 }
86 
87 static void
88 do_bridgeflag(int sock, const char *ifs, int flag, int set)
89 {
90 	struct ifbreq req;
91 
92 	strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
93 
94 	if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
95 		err(1, "unable to get bridge flags");
96 
97 	if (set)
98 		req.ifbr_ifsflags |= flag;
99 	else
100 		req.ifbr_ifsflags &= ~flag;
101 
102 	if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
103 		err(1, "unable to set bridge flags");
104 }
105 
106 static void
107 bridge_interfaces(int s, const char *prefix)
108 {
109 	static const char *stpstates[] = {
110 		"disabled",
111 		"listening",
112 		"learning",
113 		"forwarding",
114 		"blocking",
115 		"bonded",
116 		"blocking (link1)"
117 	};
118 	struct ifbifconf bifc;
119 	struct ifbreq *req;
120 	char *inbuf = NULL, *ninbuf;
121 	char *p, *pad;
122 	size_t i, len = 8192;
123 
124 	pad = strdup(prefix);
125 	if (pad == NULL)
126 		err(1, "strdup");
127 	/* replace the prefix with whitespace */
128 	for (p = pad; *p != '\0'; p++) {
129 		if(isprint(*p))
130 			*p = ' ';
131 	}
132 
133 	for (;;) {
134 		ninbuf = realloc(inbuf, len);
135 		if (ninbuf == NULL)
136 			err(1, "unable to allocate interface buffer");
137 		bifc.ifbic_len = len;
138 		bifc.ifbic_buf = inbuf = ninbuf;
139 		if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
140 			err(1, "unable to get interface list");
141 		if ((bifc.ifbic_len + sizeof(*req)) < len)
142 			break;
143 		len *= 2;
144 	}
145 
146 	for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
147 		req = bifc.ifbic_req + i;
148 		printf("%s%s ", prefix, req->ifbr_ifsname);
149 		printb("flags", req->ifbr_ifsflags, IFBIFBITS);
150 		printf("\n");
151 
152 		if (req->ifbr_ifsflags & IFBIF_STP) {
153 			printf("%s", pad);
154 			printf("port %u priority %u",
155 			    req->ifbr_portno, req->ifbr_priority);
156 			printf(" pathcost %u", req->ifbr_path_cost);
157 			if (req->ifbr_state < nitems(stpstates))
158 				printf(" %s", stpstates[req->ifbr_state]);
159 			else
160 				printf(" <unknown state %d>",
161 				    req->ifbr_state);
162 			printf("\n");
163 			printf("%sbondweight %u\n",
164 				pad, req->ifbr_bond_weight);
165 			printf("%sdesignated root:   %016jx\n",
166 				pad, (uintmax_t)req->ifbr_designated_root);
167 			printf("%sdesignated bridge: %016jx\n",
168 				pad, (uintmax_t)req->ifbr_designated_bridge);
169 			printf("%sdesignated cost:   %u\n",
170 				pad, req->ifbr_designated_cost);
171 			printf("%sdesignated port:   %u\n",
172 				pad, req->ifbr_designated_port);
173 			if (verbose) {
174 				printf("%speer root:   %016jx\n",
175 					pad, (uintmax_t)req->ifbr_peer_root);
176 				printf("%speer bridge: %016jx\n",
177 					pad, (uintmax_t)req->ifbr_peer_bridge);
178 				printf("%speer cost:   %u\n",
179 					pad, req->ifbr_peer_cost);
180 				printf("%speer port:   %u\n",
181 					pad, req->ifbr_peer_port);
182 			}
183 		}
184 	}
185 
186 	free(inbuf);
187 }
188 
189 static void
190 bridge_addresses(int s, const char *prefix)
191 {
192 	struct ifbaconf ifbac;
193 	struct ifbareq *ifba;
194 	struct ether_addr ea;
195 	char *inbuf = NULL, *ninbuf;
196 	size_t i, len = 8192;
197 
198 	for (;;) {
199 		ninbuf = realloc(inbuf, len);
200 		if (ninbuf == NULL)
201 			err(1, "unable to allocate address buffer");
202 		ifbac.ifbac_len = len;
203 		ifbac.ifbac_buf = inbuf = ninbuf;
204 		if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
205 			err(1, "unable to get address cache");
206 		if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
207 			break;
208 		len *= 2;
209 	}
210 
211 	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
212 		ifba = ifbac.ifbac_req + i;
213 		memcpy(ea.octet, ifba->ifba_dst,
214 		    sizeof(ea.octet));
215 		printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
216 		    ifba->ifba_ifsname, ifba->ifba_expire);
217 		printb("flags", ifba->ifba_flags, IFBAFBITS);
218 		printf("\n");
219 	}
220 
221 	free(inbuf);
222 }
223 
224 static void
225 bridge_status(int s)
226 {
227 	struct ifbrparam param;
228 	u_int16_t pri;
229 	u_int8_t ht, fd, ma;
230 
231 	if (do_cmd(s, BRDGGPRI, &param, sizeof(param), 0) < 0)
232 		return;
233 	pri = param.ifbrp_prio;
234 
235 	if (do_cmd(s, BRDGGHT, &param, sizeof(param), 0) < 0)
236 		return;
237 	ht = param.ifbrp_hellotime;
238 
239 	if (do_cmd(s, BRDGGFD, &param, sizeof(param), 0) < 0)
240 		return;
241 	fd = param.ifbrp_fwddelay;
242 
243 	if (do_cmd(s, BRDGGMA, &param, sizeof(param), 0) < 0)
244 		return;
245 	ma = param.ifbrp_maxage;
246 
247 	printf("\tpriority %u hellotime %u fwddelay %u maxage %u\n",
248 	    pri, ht, fd, ma);
249 
250 	bridge_interfaces(s, "\tmember: ");
251 
252 	return;
253 
254 }
255 
256 static void
257 setbridge_add(const char *val, int d __unused, int s,
258 	      const struct afswtch *afp __unused)
259 {
260 	struct ifbreq req;
261 
262 	memset(&req, 0, sizeof(req));
263 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
264 	if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
265 		err(1, "BRDGADD %s",  val);
266 }
267 
268 static void
269 setbridge_delete(const char *val, int d __unused, int s,
270 		 const struct afswtch *afp __unused)
271 {
272 	struct ifbreq req;
273 
274 	memset(&req, 0, sizeof(req));
275 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
276 	if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
277 		err(1, "BRDGDEL %s",  val);
278 }
279 
280 static void
281 setbridge_discover(const char *val, int d __unused, int s,
282 		   const struct afswtch *afp __unused)
283 {
284 	do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
285 }
286 
287 static void
288 unsetbridge_discover(const char *val, int d __unused, int s,
289 		     const struct afswtch *afp __unused)
290 {
291 	do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
292 }
293 
294 static void
295 setbridge_learn(const char *val, int d __unused, int s,
296 		const struct afswtch *afp __unused)
297 {
298 	do_bridgeflag(s, val, IFBIF_LEARNING,  1);
299 }
300 
301 static void
302 unsetbridge_learn(const char *val, int d __unused, int s,
303 		  const struct afswtch *afp __unused)
304 {
305 	do_bridgeflag(s, val, IFBIF_LEARNING,  0);
306 }
307 
308 static void
309 setbridge_span(const char *val, int d __unused, int s,
310 	       const struct afswtch *afp __unused)
311 {
312 	struct ifbreq req;
313 
314 	memset(&req, 0, sizeof(req));
315 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
316 	if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
317 		err(1, "BRDGADDS %s",  val);
318 }
319 
320 static void
321 unsetbridge_span(const char *val, int d __unused, int s,
322 		 const struct afswtch *afp __unused)
323 {
324 	struct ifbreq req;
325 
326 	memset(&req, 0, sizeof(req));
327 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
328 	if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
329 		err(1, "BRDGDELS %s",  val);
330 }
331 
332 static void
333 setbridge_stp(const char *val, int d __unused, int s,
334 	      const struct afswtch *afp __unused)
335 {
336 	do_bridgeflag(s, val, IFBIF_STP, 1);
337 }
338 
339 static void
340 unsetbridge_stp(const char *val, int d __unused, int s,
341 		const struct afswtch *afp __unused)
342 {
343 	do_bridgeflag(s, val, IFBIF_STP, 0);
344 }
345 
346 static void
347 setbridge_flush(const char *val __unused, int d __unused, int s,
348 		const struct afswtch *afp __unused)
349 {
350 	struct ifbreq req;
351 
352 	memset(&req, 0, sizeof(req));
353 	req.ifbr_ifsflags = IFBF_FLUSHDYN;
354 	if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
355 		err(1, "BRDGFLUSH");
356 }
357 
358 static void
359 setbridge_flushall(const char *val __unused, int d __unused, int s,
360 		   const struct afswtch *afp __unused)
361 {
362 	struct ifbreq req;
363 
364 	memset(&req, 0, sizeof(req));
365 	req.ifbr_ifsflags = IFBF_FLUSHALL;
366 	if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
367 		err(1, "BRDGFLUSH");
368 }
369 
370 static void
371 setbridge_static(const char *val, const char *mac, int s,
372 		 const struct afswtch *afp __unused)
373 {
374 	struct ifbareq req;
375 	struct ether_addr *ea;
376 
377 	memset(&req, 0, sizeof(req));
378 	strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
379 
380 	ea = ether_aton(mac);
381 	if (ea == NULL)
382 		errx(1, "%s: invalid address: %s", val, mac);
383 
384 	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
385 	req.ifba_flags = IFBAF_STATIC;
386 
387 	if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
388 		err(1, "BRDGSADDR %s",  val);
389 }
390 
391 static void
392 setbridge_deladdr(const char *val, int d __unused, int s,
393 		  const struct afswtch *afp __unused)
394 {
395 	struct ifbareq req;
396 	struct ether_addr *ea;
397 
398 	memset(&req, 0, sizeof(req));
399 
400 	ea = ether_aton(val);
401 	if (ea == NULL)
402 		errx(1, "invalid address: %s",  val);
403 
404 	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
405 
406 	if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
407 		err(1, "BRDGDADDR %s",  val);
408 }
409 
410 static void
411 getbridge_addr(const char *val __unused, int d __unused, int s,
412 	       const struct afswtch *afp __unused)
413 {
414 	bridge_addresses(s, "");
415 }
416 
417 static void
418 setbridge_maxaddr(const char *arg, int d __unused, int s,
419 		  const struct afswtch *afp __unused)
420 {
421 	struct ifbrparam param;
422 	u_long val;
423 
424 	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
425 		errx(1, "invalid value: %s",  arg);
426 
427 	param.ifbrp_csize = val & 0xffffffff;
428 
429 	if (do_cmd(s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
430 		err(1, "BRDGSCACHE %s",  arg);
431 }
432 
433 static void
434 setbridge_hellotime(const char *arg, int d __unused, int s,
435 		    const struct afswtch *afp __unused)
436 {
437 	struct ifbrparam param;
438 	u_long val;
439 
440 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
441 		errx(1, "invalid value: %s",  arg);
442 
443 	param.ifbrp_hellotime = val & 0xff;
444 
445 	if (do_cmd(s, BRDGSHT, &param, sizeof(param), 1) < 0)
446 		err(1, "BRDGSHT %s",  arg);
447 }
448 
449 static void
450 setbridge_fwddelay(const char *arg, int d __unused, int s,
451 		   const struct afswtch *afp __unused)
452 {
453 	struct ifbrparam param;
454 	u_long val;
455 
456 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
457 		errx(1, "invalid value: %s",  arg);
458 
459 	param.ifbrp_fwddelay = val & 0xff;
460 
461 	if (do_cmd(s, BRDGSFD, &param, sizeof(param), 1) < 0)
462 		err(1, "BRDGSFD %s",  arg);
463 }
464 
465 static void
466 setbridge_maxage(const char *arg, int d __unused, int s,
467 		 const struct afswtch *afp __unused)
468 {
469 	struct ifbrparam param;
470 	u_long val;
471 
472 	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
473 		errx(1, "invalid value: %s",  arg);
474 
475 	param.ifbrp_maxage = val & 0xff;
476 
477 	if (do_cmd(s, BRDGSMA, &param, sizeof(param), 1) < 0)
478 		err(1, "BRDGSMA %s",  arg);
479 }
480 
481 static void
482 setbridge_priority(const char *arg, int d __unused, int s,
483 		   const struct afswtch *afp __unused)
484 {
485 	struct ifbrparam param;
486 	u_long val;
487 
488 	if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
489 		errx(1, "invalid value: %s",  arg);
490 
491 	param.ifbrp_prio = val & 0xffff;
492 
493 	if (do_cmd(s, BRDGSPRI, &param, sizeof(param), 1) < 0)
494 		err(1, "BRDGSPRI %s",  arg);
495 }
496 
497 static void
498 setbridge_ifpriority(const char *ifn, const char *pri, int s,
499 		     const struct afswtch *afp __unused)
500 {
501 	struct ifbreq req;
502 	u_long val;
503 
504 	memset(&req, 0, sizeof(req));
505 
506 	if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
507 		errx(1, "invalid value: %s",  pri);
508 
509 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
510 	req.ifbr_priority = val & 0xff;
511 
512 	if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
513 		err(1, "BRDGSIFPRIO %s",  pri);
514 }
515 
516 static void
517 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
518 		     const struct afswtch *afp __unused)
519 {
520 	struct ifbreq req;
521 	u_long val;
522 
523 	memset(&req, 0, sizeof(req));
524 
525 	if (get_val(cost, &val) < 0 || (val & ~0xff) != 0)
526 		errx(1, "invalid value: %s",  cost);
527 
528 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
529 	req.ifbr_path_cost = val & 0xffff;
530 
531 	if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
532 		err(1, "BRDGSIFCOST %s",  cost);
533 }
534 
535 static void
536 setbridge_ifbondweight(const char *ifn, const char *cost, int s,
537 		       const struct afswtch *afp __unused)
538 {
539 	struct ifbreq req;
540 	u_long val;
541 
542 	memset(&req, 0, sizeof(req));
543 
544 	if (get_val(cost, &val) < 0 || (val & ~0xff) != 0)
545 		errx(1, "invalid value: %s",  cost);
546 
547 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
548 	if (val > 65535)
549 		val = 65535;
550 	req.ifbr_bond_weight = (uint16_t)val;
551 
552 	if (do_cmd(s, BRDGSBONDWGHT, &req, sizeof(req), 1) < 0)
553 		err(1, "BRDGSBONDWGHT %s",  cost);
554 }
555 
556 static void
557 setbridge_timeout(const char *arg, int d __unused, int s,
558 		  const struct afswtch *afp __unused)
559 {
560 	struct ifbrparam param;
561 	u_long val;
562 
563 	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
564 		errx(1, "invalid value: %s",  arg);
565 
566 	param.ifbrp_ctime = val & 0xffffffff;
567 
568 	if (do_cmd(s, BRDGSTO, &param, sizeof(param), 1) < 0)
569 		err(1, "BRDGSTO %s",  arg);
570 }
571 
572 static struct cmd bridge_cmds[] = {
573 	DEF_CMD_ARG("addm",		setbridge_add),
574 	DEF_CMD_ARG("deletem",		setbridge_delete),
575 	DEF_CMD_ARG("discover",		setbridge_discover),
576 	DEF_CMD_ARG("-discover",	unsetbridge_discover),
577 	DEF_CMD_ARG("learn",		setbridge_learn),
578 	DEF_CMD_ARG("-learn",		unsetbridge_learn),
579 	DEF_CMD_ARG("span",		setbridge_span),
580 	DEF_CMD_ARG("-span",		unsetbridge_span),
581 	DEF_CMD_ARG("stp",		setbridge_stp),
582 	DEF_CMD_ARG("-stp",		unsetbridge_stp),
583 	DEF_CMD("flush", 0,		setbridge_flush),
584 	DEF_CMD("flushall", 0,		setbridge_flushall),
585 	DEF_CMD_ARG2("static",		setbridge_static),
586 	DEF_CMD_ARG("deladdr",		setbridge_deladdr),
587 	DEF_CMD("addr",	 1,		getbridge_addr),
588 	DEF_CMD_ARG("maxaddr",		setbridge_maxaddr),
589 	DEF_CMD_ARG("hellotime",	setbridge_hellotime),
590 	DEF_CMD_ARG("fwddelay",		setbridge_fwddelay),
591 	DEF_CMD_ARG("maxage",		setbridge_maxage),
592 	DEF_CMD_ARG("priority",		setbridge_priority),
593 	DEF_CMD_ARG2("ifpriority",	setbridge_ifpriority),
594 	DEF_CMD_ARG2("ifpathcost",	setbridge_ifpathcost),
595 	DEF_CMD_ARG2("ifbondweight",	setbridge_ifbondweight),
596 	DEF_CMD_ARG("timeout",		setbridge_timeout),
597 };
598 static struct afswtch af_bridge = {
599 	.af_name	= "af_bridge",
600 	.af_af		= AF_UNSPEC,
601 	.af_other_status = bridge_status,
602 };
603 
604 __constructor(141)
605 static void
606 bridge_ctor(void)
607 {
608 	size_t i;
609 
610 	for (i = 0; i < nitems(bridge_cmds);  i++)
611 		cmd_register(&bridge_cmds[i]);
612 
613 	af_register(&af_bridge);
614 }
615