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