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