]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifbridge.c
network_cmds-596.100.2.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / ifbridge.c
1 /*
2 * Copyright (c) 2009-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*-
30 * Copyright 2001 Wasabi Systems, Inc.
31 * All rights reserved.
32 *
33 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed for the NetBSD Project by
46 * Wasabi Systems, Inc.
47 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
48 * or promote products derived from this software without specific prior
49 * written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 #include <sys/param.h>
65 #include <sys/ioctl.h>
66 #include <sys/socket.h>
67 #include <sys/sockio.h>
68
69 #include <stdlib.h>
70 #include <unistd.h>
71
72 #include <net/ethernet.h>
73 #include <net/if.h>
74 #include <net/if_bridgevar.h>
75 #include <net/route.h>
76
77 #include <ctype.h>
78 #include <stdio.h>
79 #include <string.h>
80 #include <stdlib.h>
81 #include <unistd.h>
82 #include <err.h>
83 #include <errno.h>
84
85 #include <arpa/inet.h>
86
87 #include "ifconfig.h"
88
89 #define PV2ID(pv, epri, eaddr) do { \
90 epri = pv >> 48; \
91 eaddr[0] = pv >> 40; \
92 eaddr[1] = pv >> 32; \
93 eaddr[2] = pv >> 24; \
94 eaddr[3] = pv >> 16; \
95 eaddr[4] = pv >> 8; \
96 eaddr[5] = pv >> 0; \
97 } while (0)
98
99 static const char *stpstates[] = {
100 "disabled",
101 "listening",
102 "learning",
103 "forwarding",
104 "blocking",
105 "discarding"
106 };
107 static const char *stpproto[] = {
108 "stp",
109 "-",
110 "rstp"
111 };
112 static const char *stproles[] = {
113 "disabled",
114 "root",
115 "designated",
116 "alternate",
117 "backup"
118 };
119
120 static int
121 get_val(const char *cp, u_long *valp)
122 {
123 char *endptr;
124 u_long val;
125
126 errno = 0;
127 val = strtoul(cp, &endptr, 0);
128 if (cp[0] == '\0' || endptr[0] != '\0' || errno == ERANGE)
129 return (-1);
130
131 *valp = val;
132 return (0);
133 }
134
135 static int
136 do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
137 {
138 struct ifdrv ifd;
139
140 memset(&ifd, 0, sizeof(ifd));
141
142 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
143 ifd.ifd_cmd = op;
144 ifd.ifd_len = argsize;
145 ifd.ifd_data = arg;
146
147 return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
148 }
149
150 static void
151 do_bridgeflag(int sock, const char *ifs, int flag, int set)
152 {
153 struct ifbreq req;
154
155 strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
156
157 if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
158 err(1, "unable to get bridge flags");
159
160 if (set)
161 req.ifbr_ifsflags |= flag;
162 else
163 req.ifbr_ifsflags &= ~flag;
164
165 if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
166 err(1, "unable to set bridge flags");
167 }
168
169 static void
170 bridge_interfaces(int s, const char *prefix)
171 {
172 struct ifbifconf bifc;
173 struct ifbreq *req;
174 char *inbuf = NULL, *ninbuf;
175 char *p, *pad;
176 int i, len = 8192;
177
178 pad = strdup(prefix);
179 if (pad == NULL)
180 err(1, "strdup");
181 /* replace the prefix with whitespace */
182 for (p = pad; *p != '\0'; p++) {
183 if(isprint(*p))
184 *p = ' ';
185 }
186
187 for (;;) {
188 ninbuf = realloc(inbuf, len);
189 if (ninbuf == NULL)
190 err(1, "unable to allocate interface buffer");
191 bifc.ifbic_len = len;
192 bifc.ifbic_buf = inbuf = ninbuf;
193 if (do_cmd(s, BRDGGIFS, &bifc, sizeof(bifc), 0) < 0)
194 err(1, "unable to get interface list");
195 if ((bifc.ifbic_len + sizeof(*req)) < len)
196 break;
197 len *= 2;
198 }
199
200 for (i = 0; i < bifc.ifbic_len / sizeof(*req); i++) {
201 req = bifc.ifbic_req + i;
202 printf("%s%s ", prefix, req->ifbr_ifsname);
203 printb("flags", req->ifbr_ifsflags, IFBIFBITS);
204 printf("\n");
205
206 printf("%s", pad);
207 printf("ifmaxaddr %u", req->ifbr_addrmax);
208 printf(" port %u priority %u", req->ifbr_portno,
209 req->ifbr_priority);
210 printf(" path cost %u", req->ifbr_path_cost);
211
212 if (req->ifbr_ifsflags & IFBIF_STP) {
213 if (req->ifbr_proto <
214 sizeof(stpproto) / sizeof(stpproto[0]))
215 printf(" proto %s", stpproto[req->ifbr_proto]);
216 else
217 printf(" <unknown proto %d>",
218 req->ifbr_proto);
219
220 printf("\n%s", pad);
221 if (req->ifbr_role <
222 sizeof(stproles) / sizeof(stproles[0]))
223 printf("role %s", stproles[req->ifbr_role]);
224 else
225 printf("<unknown role %d>",
226 req->ifbr_role);
227 if (req->ifbr_state <
228 sizeof(stpstates) / sizeof(stpstates[0]))
229 printf(" state %s", stpstates[req->ifbr_state]);
230 else
231 printf(" <unknown state %d>",
232 req->ifbr_state);
233 }
234 printf("\n");
235
236 if (verbose) {
237 struct ifbrhostfilter ifbrfh;
238 struct in_addr in;
239 struct ether_addr ea;
240
241 bzero(&ifbrfh, sizeof(struct ifbrhostfilter));
242 strlcpy(ifbrfh.ifbrhf_ifsname, req->ifbr_ifsname, sizeof(ifbrfh.ifbrhf_ifsname));
243 if (do_cmd(s, BRDGGHOSTFILTER, &ifbrfh, sizeof(ifbrfh), 0) < 0)
244 err(1, "unable to get host filter settings for %s",
245 ifbrfh.ifbrhf_ifsname);
246
247 if (ifbrfh.ifbrhf_flags & IFBRHF_ENABLED) {
248 in.s_addr = ifbrfh.ifbrhf_ipsrc;
249 bcopy(ifbrfh.ifbrhf_hwsrca, ea.octet, ETHER_ADDR_LEN);
250 } else {
251 in.s_addr = INADDR_ANY;
252 bzero(ea.octet, ETHER_ADDR_LEN);
253 }
254 printf("%s", pad);
255 printf("hostfilter %d hw: %s ip: %s",
256 ifbrfh.ifbrhf_flags & IFBRHF_ENABLED ? 1 : 0,
257 ether_ntoa(&ea), inet_ntoa(in));
258
259 printf("\n");
260 }
261 }
262
263 free(inbuf);
264 free(pad);
265 }
266
267 static void
268 bridge_addresses(int s, const char *prefix)
269 {
270 struct ifbaconf ifbac;
271 struct ifbareq *ifba;
272 char *inbuf = NULL, *ninbuf;
273 int i, len = 8192;
274 struct ether_addr ea;
275
276 for (;;) {
277 ninbuf = realloc(inbuf, len);
278 if (ninbuf == NULL)
279 err(1, "unable to allocate address buffer");
280 ifbac.ifbac_len = len;
281 ifbac.ifbac_buf = inbuf = ninbuf;
282 if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
283 err(1, "unable to get address cache");
284 if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
285 break;
286 len *= 2;
287 }
288
289 for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
290 ifba = ifbac.ifbac_req + i;
291 memcpy(ea.octet, ifba->ifba_dst,
292 sizeof(ea.octet));
293 printf("%s%s Vlan%d %s %lu ", prefix, ether_ntoa(&ea),
294 ifba->ifba_vlan, ifba->ifba_ifsname, ifba->ifba_expire);
295 printb("flags", ifba->ifba_flags, IFBAFBITS);
296 printf("\n");
297 }
298
299 free(inbuf);
300 }
301
302 #define MAX_IPv6_STR_LEN INET6_ADDRSTRLEN
303 static void
304 bridge_mac_nat(int s, const char *prefix)
305 {
306 char *buf;
307 unsigned int count;
308 struct ether_addr ea;
309 unsigned int i;
310 struct ifbrmnelist mnl;
311 char *scan;
312
313 bzero(&mnl, sizeof(mnl));
314 if (do_cmd(s, BRDGGMACNATLIST, &mnl, sizeof(mnl), 0) < 0) {
315 /* err(1, "unable to get mac nat list"); */
316 return;
317 }
318 if (mnl.ifbml_len == 0) {
319 return;
320 }
321 printf("\tMAC NAT list:\n");
322 if (mnl.ifbml_elsize == 0) {
323 err(1, "kernel reported zero length element size");
324 }
325 if (mnl.ifbml_elsize < sizeof(struct ifbrmne)) {
326 err(1, "struct element size too small, kernel mismatch");
327 }
328 buf = malloc(mnl.ifbml_len);
329 if (buf == NULL) {
330 err(1, "unable to allocate mac nat list buffer");
331 }
332 mnl.ifbml_buf = buf;
333 if (do_cmd(s, BRDGGMACNATLIST, &mnl, sizeof(mnl), 0) < 0) {
334 err(1, "unable to get mac nat list");
335 }
336 count = mnl.ifbml_len / mnl.ifbml_elsize;
337 for (i = 0, scan = buf; i < count; i++, scan += mnl.ifbml_elsize) {
338 struct ifbrmne *ifbmne = (struct ifbrmne *)scan;
339 char ntopbuf[INET6_ADDRSTRLEN];
340
341 memcpy(ea.octet, ifbmne->ifbmne_mac,
342 sizeof(ea.octet));
343 inet_ntop(ifbmne->ifbmne_af, &ifbmne->ifbmne_ip,
344 ntopbuf, sizeof(ntopbuf));
345 printf("%s%s %s %s %lu\n",
346 prefix, ifbmne->ifbmne_ifname, ntopbuf, ether_ntoa(&ea),
347 (unsigned long)ifbmne->ifbmne_expire);
348 }
349 free(buf);
350 }
351
352 static void
353 bridge_status(int s)
354 {
355 struct ifbropreq ifbp;
356 struct ifbrparam param;
357 u_int16_t pri;
358 u_int8_t ht, fd, ma, hc, pro;
359 u_int8_t lladdr[ETHER_ADDR_LEN];
360 u_int16_t bprio;
361 u_int32_t csize, ctime;
362 u_int32_t ipfflags;
363
364 if (do_cmd(s, BRDGGCACHE, &param, sizeof(param), 0) < 0)
365 return;
366 csize = param.ifbrp_csize;
367 if (do_cmd(s, BRDGGTO, &param, sizeof(param), 0) < 0)
368 return;
369 ctime = param.ifbrp_ctime;
370 if (do_cmd(s, BRDGGFILT, &param, sizeof(param), 0) < 0)
371 return;
372 ipfflags = param.ifbrp_filter;
373 if (do_cmd(s, BRDGPARAM, &ifbp, sizeof(ifbp), 0) < 0)
374 return;
375 pri = ifbp.ifbop_priority;
376 pro = ifbp.ifbop_protocol;
377 ht = ifbp.ifbop_hellotime;
378 fd = ifbp.ifbop_fwddelay;
379 hc = ifbp.ifbop_holdcount;
380 ma = ifbp.ifbop_maxage;
381
382 printf("\tConfiguration:\n");
383 PV2ID(ifbp.ifbop_bridgeid, bprio, lladdr);
384 printf("\t\tid %s priority %u hellotime %u fwddelay %u\n",
385 ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
386 printf("\t\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
387 ma, hc, stpproto[pro], csize, ctime);
388
389 PV2ID(ifbp.ifbop_designated_root, bprio, lladdr);
390 printf("\t\troot id %s priority %d ifcost %u port %u\n",
391 ether_ntoa((struct ether_addr *)lladdr), bprio,
392 ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
393
394 printf("\t\tipfilter %s flags 0x%x\n",
395 (ipfflags & IFBF_FILT_USEIPF) ? "enabled" : "disabled", ipfflags);
396
397 bridge_interfaces(s, "\tmember: ");
398
399 if (!all || verbose > 1) {
400 printf("\tAddress cache:\n");
401 bridge_addresses(s, "\t\t");
402 bridge_mac_nat(s, "\t\t");
403 }
404 return;
405
406 }
407
408 static void
409 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
410 {
411 struct ifbreq req;
412
413 memset(&req, 0, sizeof(req));
414 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
415 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
416 err(1, "BRDGADD %s", val);
417 }
418
419 static void
420 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
421 {
422 struct ifbreq req;
423
424 memset(&req, 0, sizeof(req));
425 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
426 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
427 err(1, "BRDGDEL %s", val);
428 }
429
430 static void
431 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
432 {
433
434 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
435 }
436
437 static void
438 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
439 {
440
441 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
442 }
443
444 static void
445 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
446 {
447
448 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
449 }
450
451 static void
452 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
453 {
454
455 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
456 }
457
458 #ifdef notdef
459 static void
460 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
461 {
462
463 do_bridgeflag(s, val, IFBIF_STICKY, 1);
464 }
465
466 static void
467 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
468 {
469
470 do_bridgeflag(s, val, IFBIF_STICKY, 0);
471 }
472
473 static void
474 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
475 {
476 struct ifbreq req;
477
478 memset(&req, 0, sizeof(req));
479 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
480 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
481 err(1, "BRDGADDS %s", val);
482 }
483
484 static void
485 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
486 {
487 struct ifbreq req;
488
489 memset(&req, 0, sizeof(req));
490 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
491 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
492 err(1, "BRDGDELS %s", val);
493 }
494 #endif
495
496 static void
497 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
498 {
499
500 do_bridgeflag(s, val, IFBIF_STP, 1);
501 }
502
503 static void
504 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
505 {
506
507 do_bridgeflag(s, val, IFBIF_STP, 0);
508 }
509
510 #ifdef notdef
511 static void
512 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
513 {
514 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
515 }
516
517 static void
518 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
519 {
520 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
521 }
522
523 static void
524 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
525 {
526 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
527 }
528
529 static void
530 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
531 {
532 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
533 }
534
535 static void
536 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
537 {
538 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
539 }
540
541 static void
542 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
543 {
544 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
545 }
546
547 static void
548 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
549 {
550 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
551 }
552
553 static void
554 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
555 {
556 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
557 }
558 #endif
559
560 static void
561 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
562 {
563 struct ifbreq req;
564
565 memset(&req, 0, sizeof(req));
566 req.ifbr_ifsflags = IFBF_FLUSHDYN;
567 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
568 err(1, "BRDGFLUSH");
569 }
570
571 static void
572 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
573 {
574 struct ifbreq req;
575
576 memset(&req, 0, sizeof(req));
577 req.ifbr_ifsflags = IFBF_FLUSHALL;
578 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
579 err(1, "BRDGFLUSH");
580 }
581
582 static void
583 setbridge_static(const char *val, const char *mac, int s,
584 const struct afswtch *afp)
585 {
586 struct ifbareq req;
587 struct ether_addr *ea;
588
589 memset(&req, 0, sizeof(req));
590 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
591
592 ea = ether_aton(mac);
593 if (ea == NULL)
594 errx(1, "%s: invalid address: %s", val, mac);
595
596 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
597 req.ifba_flags = IFBAF_STATIC;
598 req.ifba_vlan = 1; /* XXX allow user to specify */
599
600 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
601 err(1, "BRDGSADDR %s", val);
602 }
603
604 static void
605 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
606 {
607 struct ifbareq req;
608 struct ether_addr *ea;
609
610 memset(&req, 0, sizeof(req));
611
612 ea = ether_aton(val);
613 if (ea == NULL)
614 errx(1, "invalid address: %s", val);
615
616 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
617
618 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
619 err(1, "BRDGDADDR %s", val);
620 }
621
622 static void
623 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
624 {
625
626 bridge_addresses(s, "");
627 }
628
629 static void
630 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
631 {
632 struct ifbrparam param;
633 u_long val;
634
635 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
636 errx(1, "invalid value: %s", arg);
637
638 param.ifbrp_csize = val & 0xffffffff;
639
640 if (do_cmd(s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
641 err(1, "BRDGSCACHE %s", arg);
642 }
643
644 static void
645 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
646 {
647 struct ifbrparam param;
648 u_long val;
649
650 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
651 errx(1, "invalid value: %s", arg);
652
653 param.ifbrp_hellotime = val & 0xff;
654
655 if (do_cmd(s, BRDGSHT, &param, sizeof(param), 1) < 0)
656 err(1, "BRDGSHT %s", arg);
657 }
658
659 static void
660 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
661 {
662 struct ifbrparam param;
663 u_long val;
664
665 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
666 errx(1, "invalid value: %s", arg);
667
668 param.ifbrp_fwddelay = val & 0xff;
669
670 if (do_cmd(s, BRDGSFD, &param, sizeof(param), 1) < 0)
671 err(1, "BRDGSFD %s", arg);
672 }
673
674 static void
675 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
676 {
677 struct ifbrparam param;
678 u_long val;
679
680 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
681 errx(1, "invalid value: %s", arg);
682
683 param.ifbrp_maxage = val & 0xff;
684
685 if (do_cmd(s, BRDGSMA, &param, sizeof(param), 1) < 0)
686 err(1, "BRDGSMA %s", arg);
687 }
688
689 static void
690 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
691 {
692 struct ifbrparam param;
693 u_long val;
694
695 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
696 errx(1, "invalid value: %s", arg);
697
698 param.ifbrp_prio = val & 0xffff;
699
700 if (do_cmd(s, BRDGSPRI, &param, sizeof(param), 1) < 0)
701 err(1, "BRDGSPRI %s", arg);
702 }
703
704 #ifdef notdef
705 static void
706 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
707 {
708 struct ifbrparam param;
709
710 if (strcasecmp(arg, "stp") == 0) {
711 param.ifbrp_proto = 0;
712 } else if (strcasecmp(arg, "rstp") == 0) {
713 param.ifbrp_proto = 2;
714 } else {
715 errx(1, "unknown stp protocol");
716 }
717
718 if (do_cmd(s, BRDGSPROTO, &param, sizeof(param), 1) < 0)
719 err(1, "BRDGSPROTO %s", arg);
720 }
721
722 static void
723 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
724 {
725 struct ifbrparam param;
726 u_long val;
727
728 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
729 errx(1, "invalid value: %s", arg);
730
731 param.ifbrp_txhc = val & 0xff;
732
733 if (do_cmd(s, BRDGSTXHC, &param, sizeof(param), 1) < 0)
734 err(1, "BRDGSTXHC %s", arg);
735 }
736 #endif
737
738 static void
739 setbridge_ifpriority(const char *ifn, const char *pri, int s,
740 const struct afswtch *afp)
741 {
742 struct ifbreq req;
743 u_long val;
744
745 memset(&req, 0, sizeof(req));
746
747 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
748 errx(1, "invalid value: %s", pri);
749
750 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
751 req.ifbr_priority = val & 0xff;
752
753 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
754 err(1, "BRDGSIFPRIO %s", pri);
755 }
756
757 static void
758 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
759 const struct afswtch *afp)
760 {
761 struct ifbreq req;
762 u_long val;
763
764 memset(&req, 0, sizeof(req));
765
766 if (get_val(cost, &val) < 0)
767 errx(1, "invalid value: %s", cost);
768
769 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
770 req.ifbr_path_cost = val;
771
772 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
773 err(1, "BRDGSIFCOST %s", cost);
774 }
775
776 #ifdef notdef
777 static void
778 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
779 const struct afswtch *afp)
780 {
781 struct ifbreq req;
782 u_long val;
783
784 memset(&req, 0, sizeof(req));
785
786 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
787 errx(1, "invalid value: %s", arg);
788
789 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
790 req.ifbr_addrmax = val & 0xffffffff;
791
792 if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
793 err(1, "BRDGSIFAMAX %s", arg);
794 }
795 #endif
796
797 static void
798 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
799 {
800 struct ifbrparam param;
801 u_long val;
802
803 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
804 errx(1, "invalid value: %s", arg);
805
806 param.ifbrp_ctime = val & 0xffffffff;
807
808 if (do_cmd(s, BRDGSTO, &param, sizeof(param), 1) < 0)
809 err(1, "BRDGSTO %s", arg);
810 }
811
812 #ifdef notdef
813 static void
814 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
815 {
816
817 do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
818 }
819
820 static void
821 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
822 {
823
824 do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
825 }
826 #endif
827
828
829 static void
830 setbridge_hostfilter(const char *ifn, const char *addr, int s,
831 const struct afswtch *afp)
832 {
833 struct ifbrhostfilter req;
834 struct ether_addr *ea;
835 struct in_addr in;
836
837 memset(&req, 0, sizeof(req));
838 req.ifbrhf_flags = IFBRHF_ENABLED;
839
840 strlcpy(req.ifbrhf_ifsname, ifn, sizeof(req.ifbrhf_ifsname));
841
842 ea = ether_aton(addr);
843 if (ea != NULL) {
844 req.ifbrhf_flags |= IFBRHF_HWSRC;
845 bcopy(ea, req.ifbrhf_hwsrca, sizeof(req.ifbrhf_hwsrca));
846 } else if (inet_aton(addr, &in) != 0) {
847 req.ifbrhf_flags |= IFBRHF_IPSRC;
848 req.ifbrhf_ipsrc = in.s_addr;
849 } else
850 errx(1, "invalid address: %s", addr);
851
852 if (do_cmd(s, BRDGSHOSTFILTER, &req, sizeof(req), 1) < 0)
853 err(1, "BRDGSHOSTFILTER %s %s", ifn, addr);
854 }
855
856 static void
857 unsetbridge_hostfilter(const char *ifn, int d, int s, const struct afswtch *afp)
858 {
859 struct ifbrhostfilter req;
860
861 memset(&req, 0, sizeof(req));
862 strlcpy(req.ifbrhf_ifsname, ifn, sizeof(req.ifbrhf_ifsname));
863
864 if (do_cmd(s, BRDGSHOSTFILTER, &req, sizeof(req), 1) < 0)
865 err(1, "BRDGSHOSTFILTER");
866 }
867
868 static void
869 setbridge_macnat(const char *val, int d, int s, const struct afswtch *afp)
870 {
871
872 do_bridgeflag(s, val, IFBIF_MAC_NAT, 1);
873 }
874
875 static void
876 unsetbridge_macnat(const char *val, int d, int s, const struct afswtch *afp)
877 {
878
879 do_bridgeflag(s, val, IFBIF_MAC_NAT, 0);
880 }
881
882 static struct cmd bridge_cmds[] = {
883 DEF_CMD_ARG("addm", setbridge_add),
884 DEF_CMD_ARG("deletem", setbridge_delete),
885 DEF_CMD_ARG("discover", setbridge_discover),
886 DEF_CMD_ARG("-discover", unsetbridge_discover),
887 DEF_CMD_ARG("learn", setbridge_learn),
888 DEF_CMD_ARG("-learn", unsetbridge_learn),
889 #ifdef notdef
890 DEF_CMD_ARG("sticky", setbridge_sticky),
891 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
892 DEF_CMD_ARG("span", setbridge_span),
893 DEF_CMD_ARG("-span", unsetbridge_span),
894 #endif
895 DEF_CMD_ARG("stp", setbridge_stp),
896 DEF_CMD_ARG("-stp", unsetbridge_stp),
897 #ifdef notdef
898 DEF_CMD_ARG("edge", setbridge_edge),
899 DEF_CMD_ARG("-edge", unsetbridge_edge),
900 DEF_CMD_ARG("autoedge", setbridge_autoedge),
901 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
902 DEF_CMD_ARG("ptp", setbridge_ptp),
903 DEF_CMD_ARG("-ptp", unsetbridge_ptp),
904 DEF_CMD_ARG("autoptp", setbridge_autoptp),
905 DEF_CMD_ARG("-autoptp", unsetbridge_autoptp),
906 #endif
907 DEF_CMD("flush", 0, setbridge_flush),
908 DEF_CMD("flushall", 0, setbridge_flushall),
909 DEF_CMD_ARG2("static", setbridge_static),
910 DEF_CMD_ARG("deladdr", setbridge_deladdr),
911 DEF_CMD("addr", 1, setbridge_addr),
912 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
913 DEF_CMD_ARG("hellotime", setbridge_hellotime),
914 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
915 DEF_CMD_ARG("maxage", setbridge_maxage),
916 DEF_CMD_ARG("priority", setbridge_priority),
917 #ifdef notdef
918 DEF_CMD_ARG("proto", setbridge_protocol),
919 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
920 #endif
921 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
922 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
923 #ifdef notdef
924 DEF_CMD_ARG2("ifmaxaddr", setbridge_ifmaxaddr),
925 #endif
926 DEF_CMD_ARG("timeout", setbridge_timeout),
927 #ifdef notdef
928 DEF_CMD_ARG("private", setbridge_private),
929 DEF_CMD_ARG("-private", unsetbridge_private),
930 #endif
931 DEF_CMD_ARG2("hostfilter", setbridge_hostfilter),
932 DEF_CMD_ARG("-hostfilter", unsetbridge_hostfilter),
933 DEF_CMD_ARG("macnat", setbridge_macnat),
934 DEF_CMD_ARG("-macnat", unsetbridge_macnat),
935 };
936 static struct afswtch af_bridge = {
937 .af_name = "af_bridge",
938 .af_af = AF_UNSPEC,
939 .af_other_status = bridge_status,
940 };
941
942 static __constructor void
943 bridge_ctor(void)
944 {
945 #define N(a) (sizeof(a) / sizeof(a[0]))
946 int i;
947
948 for (i = 0; i < N(bridge_cmds); i++)
949 cmd_register(&bridge_cmds[i]);
950 af_register(&af_bridge);
951 #undef N
952 }