ifbridge.c revision 146985
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
37static const char rcsid[] =
38  "$FreeBSD: head/sbin/ifconfig/ifbridge.c 146985 2005-06-05 02:59:26Z thompsa $";
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
64static int
65get_val(const char *cp, u_long *valp)
66{
67	char *endptr;
68	u_long val;
69
70	errno = 0;
71	val = strtoul(cp, &endptr, 0);
72	if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
73		return (-1);
74
75	*valp = val;
76	return (0);
77}
78
79static int
80do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
81{
82	struct ifdrv ifd;
83
84	memset(&ifd, 0, sizeof(ifd));
85
86	strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
87	ifd.ifd_cmd = op;
88	ifd.ifd_len = argsize;
89	ifd.ifd_data = arg;
90
91	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
92}
93
94static void
95do_bridgeflag(int sock, const char *ifs, int flag, int set)
96{
97	struct ifbreq req;
98
99	strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
100
101	if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
102		err(1, "unable to get bridge flags");
103
104	if (set)
105		req.ifbr_ifsflags |= flag;
106	else
107		req.ifbr_ifsflags &= ~flag;
108
109	if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
110		err(1, "unable to set bridge flags");
111}
112
113static void
114bridge_interfaces(int s, const char *prefix, int flags)
115{
116	static const char *stpstates[] = {
117		"disabled",
118		"listening",
119		"learning",
120		"forwarding",
121		"blocking",
122	};
123	struct ifbifconf bifc;
124	struct ifbreq *req;
125	char *inbuf = NULL, *ninbuf;
126	int i, len = 8192;
127
128	for (;;) {
129		ninbuf = realloc(inbuf, len);
130		if (ninbuf == NULL)
131			err(1, "unable to allocate interface buffer");
132		bifc.ifbic_len = len;
133		bifc.ifbic_buf = inbuf = ninbuf;
134		if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
135			err(1, "unable to get interface list");
136		if ((bifc.ifbic_len + sizeof(*req)) < len)
137			break;
138		len *= 2;
139	}
140
141	for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
142		req = bifc.ifbic_req + i;
143		printf("%s%s ", prefix, req->ifbr_ifsname);
144		printb("flags", req->ifbr_ifsflags, IFBIFBITS);
145		printf("\n");
146
147		if (!flags) continue;
148
149		printf("%s\t", prefix);
150		printf("port %u priority %u",
151		    req->ifbr_portno, req->ifbr_priority);
152		if (req->ifbr_ifsflags & IFBIF_STP) {
153			printf(" path cost %u", req->ifbr_path_cost);
154			if (req->ifbr_state <
155			    sizeof(stpstates) / sizeof(stpstates[0]))
156				printf(" %s", stpstates[req->ifbr_state]);
157			else
158				printf(" <unknown state %d>",
159				    req->ifbr_state);
160		}
161		printf("\n");
162	}
163
164	free(inbuf);
165}
166
167static void
168bridge_addresses(int s, const char *prefix)
169{
170	struct ifbaconf ifbac;
171	struct ifbareq *ifba;
172	char *inbuf = NULL, *ninbuf;
173	int i, len = 8192;
174	struct ether_addr ea;
175
176	for (;;) {
177		ninbuf = realloc(inbuf, len);
178		if (ninbuf == NULL)
179			err(1, "unable to allocate address buffer");
180		ifbac.ifbac_len = len;
181		ifbac.ifbac_buf = inbuf = ninbuf;
182		if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
183			err(1, "unable to get address cache");
184		if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
185			break;
186		len *= 2;
187	}
188
189	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
190		ifba = ifbac.ifbac_req + i;
191		memcpy(ea.octet, ifba->ifba_dst,
192		    sizeof(ea.octet));
193		printf("%s%s %s %lu ", prefix, ether_ntoa(&ea),
194		    ifba->ifba_ifsname, ifba->ifba_expire);
195		printb("flags", ifba->ifba_flags, IFBAFBITS);
196		printf("\n");
197	}
198
199	free(inbuf);
200}
201
202static void
203bridge_status(int s)
204{
205	struct ifbrparam param;
206	u_int16_t pri;
207	u_int8_t ht, fd, ma;
208
209	if (do_cmd(s, BRDGGPRI, &param, sizeof(param), 0) < 0)
210		return;
211	pri = param.ifbrp_prio;
212
213	if (do_cmd(s, BRDGGHT, &param, sizeof(param), 0) < 0)
214		return;
215	ht = param.ifbrp_hellotime;
216
217	if (do_cmd(s, BRDGGFD, &param, sizeof(param), 0) < 0)
218		return;
219	fd = param.ifbrp_fwddelay;
220
221	if (do_cmd(s, BRDGGMA, &param, sizeof(param), 0) < 0)
222		return;
223	ma = param.ifbrp_maxage;
224
225	printf("\tpriority %u hellotime %u fwddelay %u maxage %u\n",
226	    pri, ht, fd, ma);
227
228	bridge_interfaces(s, "\tmember: ", 0);
229
230	return;
231
232}
233
234static void
235setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
236{
237	struct ifbreq req;
238
239	memset(&req, 0, sizeof(req));
240	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
241	if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
242		err(1, "BRDGADD %s",  val);
243}
244
245static void
246setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
247{
248	struct ifbreq req;
249
250	memset(&req, 0, sizeof(req));
251	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
252	if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
253		err(1, "BRDGDEL %s",  val);
254}
255
256static void
257setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
258{
259
260	do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
261}
262
263static void
264unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
265{
266
267	do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
268}
269
270static void
271setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
272{
273
274	do_bridgeflag(s, val, IFBIF_LEARNING,  1);
275}
276
277static void
278unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
279{
280
281	do_bridgeflag(s, val, IFBIF_LEARNING,  0);
282}
283
284static void
285setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
286{
287
288	do_bridgeflag(s, val, IFBIF_STP, 1);
289}
290
291static void
292unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
293{
294
295	do_bridgeflag(s, val, IFBIF_STP, 0);
296}
297
298static void
299setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
300{
301	struct ifbreq req;
302
303	memset(&req, 0, sizeof(req));
304	req.ifbr_ifsflags = IFBF_FLUSHDYN;
305	if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
306		err(1, "BRDGFLUSH");
307}
308
309static void
310setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
311{
312	struct ifbreq req;
313
314	memset(&req, 0, sizeof(req));
315	req.ifbr_ifsflags = IFBF_FLUSHALL;
316	if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
317		err(1, "BRDGFLUSH");
318}
319
320static void
321setbridge_static(const char *val, const char *mac, int s,
322    const struct afswtch *afp)
323{
324	struct ifbareq req;
325	struct ether_addr *ea;
326
327	memset(&req, 0, sizeof(req));
328	strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
329
330	ea = ether_aton(mac);
331	if (ea == NULL)
332		errx(1, "%s: invalid address: %s", val, mac);
333
334	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
335	req.ifba_flags = IFBAF_STATIC;
336
337	if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
338		err(1, "BRDGSADDR %s",  val);
339}
340
341static void
342setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
343{
344	struct ifbareq req;
345	struct ether_addr *ea;
346
347	memset(&req, 0, sizeof(req));
348
349	ea = ether_aton(val);
350	if (ea == NULL)
351		errx(1, "invalid address: %s",  val);
352
353	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
354
355	if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
356		err(1, "BRDGDADDR %s",  val);
357}
358
359static void
360setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
361{
362
363	bridge_addresses(s, "");
364}
365
366static void
367setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
368{
369	struct ifbrparam param;
370	u_long val;
371
372	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
373		errx(1, "invalid value: %s",  arg);
374
375	param.ifbrp_csize = val & 0xffffffff;
376
377	if (do_cmd(s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
378		err(1, "BRDGSCACHE %s",  arg);
379}
380
381static void
382setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
383{
384	struct ifbrparam param;
385	u_long val;
386
387	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
388		errx(1, "invalid value: %s",  arg);
389
390	param.ifbrp_hellotime = val & 0xff;
391
392	if (do_cmd(s, BRDGSHT, &param, sizeof(param), 1) < 0)
393		err(1, "BRDGSHT %s",  arg);
394}
395
396static void
397setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
398{
399	struct ifbrparam param;
400	u_long val;
401
402	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
403		errx(1, "invalid value: %s",  arg);
404
405	param.ifbrp_fwddelay = val & 0xff;
406
407	if (do_cmd(s, BRDGSFD, &param, sizeof(param), 1) < 0)
408		err(1, "BRDGSFD %s",  arg);
409}
410
411static void
412setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
413{
414	struct ifbrparam param;
415	u_long val;
416
417	if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
418		errx(1, "invalid value: %s",  arg);
419
420	param.ifbrp_maxage = val & 0xff;
421
422	if (do_cmd(s, BRDGSMA, &param, sizeof(param), 1) < 0)
423		err(1, "BRDGSMA %s",  arg);
424}
425
426static void
427setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
428{
429	struct ifbrparam param;
430	u_long val;
431
432	if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
433		errx(1, "invalid value: %s",  arg);
434
435	param.ifbrp_prio = val & 0xffff;
436
437	if (do_cmd(s, BRDGSPRI, &param, sizeof(param), 1) < 0)
438		err(1, "BRDGSPRI %s",  arg);
439}
440
441static void
442setbridge_ifpriority(const char *ifn, const char *pri, int s,
443    const struct afswtch *afp)
444{
445	struct ifbreq req;
446	u_long val;
447
448	memset(&req, 0, sizeof(req));
449
450	if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
451		errx(1, "invalid value: %s",  pri);
452
453	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
454	req.ifbr_priority = val & 0xff;
455
456	if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
457		err(1, "BRDGSIFPRIO %s",  pri);
458}
459
460static void
461setbridge_ifpathcost(const char *ifn, const char *cost, int s,
462    const struct afswtch *afp)
463{
464	struct ifbreq req;
465	u_long val;
466
467	memset(&req, 0, sizeof(req));
468
469	if (get_val(cost, &val) < 0 || (val & ~0xff) != 0)
470		errx(1, "invalid value: %s",  cost);
471
472	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
473	req.ifbr_path_cost = val & 0xffff;
474
475	if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
476		err(1, "BRDGSIFCOST %s",  cost);
477}
478
479static void
480setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
481{
482	struct ifbrparam param;
483	u_long val;
484
485	if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
486		errx(1, "invalid value: %s",  arg);
487
488	param.ifbrp_ctime = val & 0xffffffff;
489
490	if (do_cmd(s, BRDGSTO, &param, sizeof(param), 1) < 0)
491		err(1, "BRDGSTO %s",  arg);
492}
493
494static struct cmd bridge_cmds[] = {
495	DEF_CMD_ARG("addm",		setbridge_add),
496	DEF_CMD_ARG("deletem",		setbridge_delete),
497	DEF_CMD_ARG("discover",		setbridge_discover),
498	DEF_CMD_ARG("-discover",	unsetbridge_discover),
499	DEF_CMD_ARG("learn",		setbridge_learn),
500	DEF_CMD_ARG("-learn",		unsetbridge_learn),
501	DEF_CMD_ARG("stp",		setbridge_stp),
502	DEF_CMD_ARG("-stp",		unsetbridge_stp),
503	DEF_CMD("flush", 0,		setbridge_flush),
504	DEF_CMD("flushall", 0,		setbridge_flushall),
505	DEF_CMD_ARG2("static",		setbridge_static),
506	DEF_CMD_ARG("deladdr",		setbridge_deladdr),
507	DEF_CMD("addr",	 1,		setbridge_addr),
508	DEF_CMD_ARG("maxaddr",		setbridge_maxaddr),
509	DEF_CMD_ARG("hellotime",	setbridge_hellotime),
510	DEF_CMD_ARG("fwddelay",		setbridge_fwddelay),
511	DEF_CMD_ARG("maxage",		setbridge_maxage),
512	DEF_CMD_ARG("priority",		setbridge_priority),
513	DEF_CMD_ARG2("ifpriority",	setbridge_ifpriority),
514	DEF_CMD_ARG2("ifpathcost",	setbridge_ifpathcost),
515	DEF_CMD_ARG("timeout",		setbridge_timeout),
516};
517static struct afswtch af_bridge = {
518	.af_name	= "af_bridge",
519	.af_af		= AF_UNSPEC,
520	.af_other_status = bridge_status,
521};
522
523static __constructor void
524bridge_ctor(void)
525{
526#define	N(a)	(sizeof(a) / sizeof(a[0]))
527	int i;
528
529	for (i = 0; i < N(bridge_cmds);  i++)
530		cmd_register(&bridge_cmds[i]);
531	af_register(&af_bridge);
532#undef N
533}
534