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