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