]> git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/ifconfig.tproj/ifbridge.c
md5: Don't symlink non working bins, setuid appropriate bins
[apple_cmds.git] / network_cmds / 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 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 135000
304 static void
305 bridge_mac_nat(int s, const char *prefix)
306 {
307 char *buf;
308 unsigned int count;
309 struct ether_addr ea;
310 unsigned int i;
311 struct ifbrmnelist mnl;
312 char *scan;
313
314 bzero(&mnl, sizeof(mnl));
315 if (do_cmd(s, BRDGGMACNATLIST, &mnl, sizeof(mnl), 0) < 0) {
316 /* err(1, "unable to get mac nat list"); */
317 return;
318 }
319 if (mnl.ifbml_len == 0) {
320 return;
321 }
322 printf("\tMAC NAT list:\n");
323 if (mnl.ifbml_elsize == 0) {
324 err(1, "kernel reported zero length element size");
325 }
326 if (mnl.ifbml_elsize < sizeof(struct ifbrmne)) {
327 err(1, "struct element size too small, kernel mismatch");
328 }
329 buf = malloc(mnl.ifbml_len);
330 if (buf == NULL) {
331 err(1, "unable to allocate mac nat list buffer");
332 }
333 mnl.ifbml_buf = buf;
334 if (do_cmd(s, BRDGGMACNATLIST, &mnl, sizeof(mnl), 0) < 0) {
335 err(1, "unable to get mac nat list");
336 }
337 count = mnl.ifbml_len / mnl.ifbml_elsize;
338 for (i = 0, scan = buf; i < count; i++, scan += mnl.ifbml_elsize) {
339 struct ifbrmne *ifbmne = (struct ifbrmne *)scan;
340 char ntopbuf[INET6_ADDRSTRLEN];
341
342 memcpy(ea.octet, ifbmne->ifbmne_mac,
343 sizeof(ea.octet));
344 inet_ntop(ifbmne->ifbmne_af, &ifbmne->ifbmne_ip,
345 ntopbuf, sizeof(ntopbuf));
346 printf("%s%s %s %s %lu\n",
347 prefix, ifbmne->ifbmne_ifname, ntopbuf, ether_ntoa(&ea),
348 (unsigned long)ifbmne->ifbmne_expire);
349 }
350 free(buf);
351 }
352 #endif
353
354 static void
355 bridge_status(int s)
356 {
357 struct ifbropreq ifbp;
358 struct ifbrparam param;
359 u_int16_t pri;
360 u_int8_t ht, fd, ma, hc, pro;
361 u_int8_t lladdr[ETHER_ADDR_LEN];
362 u_int16_t bprio;
363 u_int32_t csize, ctime;
364 u_int32_t ipfflags;
365
366 if (do_cmd(s, BRDGGCACHE, &param, sizeof(param), 0) < 0)
367 return;
368 csize = param.ifbrp_csize;
369 if (do_cmd(s, BRDGGTO, &param, sizeof(param), 0) < 0)
370 return;
371 ctime = param.ifbrp_ctime;
372 if (do_cmd(s, BRDGGFILT, &param, sizeof(param), 0) < 0)
373 return;
374 ipfflags = param.ifbrp_filter;
375 if (do_cmd(s, BRDGPARAM, &ifbp, sizeof(ifbp), 0) < 0)
376 return;
377 pri = ifbp.ifbop_priority;
378 pro = ifbp.ifbop_protocol;
379 ht = ifbp.ifbop_hellotime;
380 fd = ifbp.ifbop_fwddelay;
381 hc = ifbp.ifbop_holdcount;
382 ma = ifbp.ifbop_maxage;
383
384 printf("\tConfiguration:\n");
385 PV2ID(ifbp.ifbop_bridgeid, bprio, lladdr);
386 printf("\t\tid %s priority %u hellotime %u fwddelay %u\n",
387 ether_ntoa((struct ether_addr *)lladdr), pri, ht, fd);
388 printf("\t\tmaxage %u holdcnt %u proto %s maxaddr %u timeout %u\n",
389 ma, hc, stpproto[pro], csize, ctime);
390
391 PV2ID(ifbp.ifbop_designated_root, bprio, lladdr);
392 printf("\t\troot id %s priority %d ifcost %u port %u\n",
393 ether_ntoa((struct ether_addr *)lladdr), bprio,
394 ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
395
396 printf("\t\tipfilter %s flags 0x%x\n",
397 (ipfflags & IFBF_FILT_USEIPF) ? "enabled" : "disabled", ipfflags);
398
399 bridge_interfaces(s, "\tmember: ");
400
401 if (!all || verbose > 1) {
402 printf("\tAddress cache:\n");
403 bridge_addresses(s, "\t\t");
404 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 135000
405 bridge_mac_nat(s, "\t\t");
406 #endif
407 }
408 return;
409
410 }
411
412 static void
413 setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
414 {
415 struct ifbreq req;
416
417 memset(&req, 0, sizeof(req));
418 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
419 if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
420 err(1, "BRDGADD %s", val);
421 }
422
423 static void
424 setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
425 {
426 struct ifbreq req;
427
428 memset(&req, 0, sizeof(req));
429 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
430 if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
431 err(1, "BRDGDEL %s", val);
432 }
433
434 static void
435 setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
436 {
437
438 do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
439 }
440
441 static void
442 unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
443 {
444
445 do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
446 }
447
448 static void
449 setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
450 {
451
452 do_bridgeflag(s, val, IFBIF_LEARNING, 1);
453 }
454
455 static void
456 unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
457 {
458
459 do_bridgeflag(s, val, IFBIF_LEARNING, 0);
460 }
461
462 #ifdef notdef
463 static void
464 setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
465 {
466
467 do_bridgeflag(s, val, IFBIF_STICKY, 1);
468 }
469
470 static void
471 unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
472 {
473
474 do_bridgeflag(s, val, IFBIF_STICKY, 0);
475 }
476
477 static void
478 setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
479 {
480 struct ifbreq req;
481
482 memset(&req, 0, sizeof(req));
483 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
484 if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
485 err(1, "BRDGADDS %s", val);
486 }
487
488 static void
489 unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
490 {
491 struct ifbreq req;
492
493 memset(&req, 0, sizeof(req));
494 strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
495 if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
496 err(1, "BRDGDELS %s", val);
497 }
498 #endif
499
500 static void
501 setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
502 {
503
504 do_bridgeflag(s, val, IFBIF_STP, 1);
505 }
506
507 static void
508 unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
509 {
510
511 do_bridgeflag(s, val, IFBIF_STP, 0);
512 }
513
514 #ifdef notdef
515 static void
516 setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
517 {
518 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
519 }
520
521 static void
522 unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
523 {
524 do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
525 }
526
527 static void
528 setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
529 {
530 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
531 }
532
533 static void
534 unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
535 {
536 do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
537 }
538
539 static void
540 setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
541 {
542 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
543 }
544
545 static void
546 unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
547 {
548 do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
549 }
550
551 static void
552 setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
553 {
554 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
555 }
556
557 static void
558 unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
559 {
560 do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
561 }
562 #endif
563
564 static void
565 setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
566 {
567 struct ifbreq req;
568
569 memset(&req, 0, sizeof(req));
570 req.ifbr_ifsflags = IFBF_FLUSHDYN;
571 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
572 err(1, "BRDGFLUSH");
573 }
574
575 static void
576 setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
577 {
578 struct ifbreq req;
579
580 memset(&req, 0, sizeof(req));
581 req.ifbr_ifsflags = IFBF_FLUSHALL;
582 if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
583 err(1, "BRDGFLUSH");
584 }
585
586 static void
587 setbridge_static(const char *val, const char *mac, int s,
588 const struct afswtch *afp)
589 {
590 struct ifbareq req;
591 struct ether_addr *ea;
592
593 memset(&req, 0, sizeof(req));
594 strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));
595
596 ea = ether_aton(mac);
597 if (ea == NULL)
598 errx(1, "%s: invalid address: %s", val, mac);
599
600 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
601 req.ifba_flags = IFBAF_STATIC;
602 req.ifba_vlan = 1; /* XXX allow user to specify */
603
604 if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
605 err(1, "BRDGSADDR %s", val);
606 }
607
608 static void
609 setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
610 {
611 struct ifbareq req;
612 struct ether_addr *ea;
613
614 memset(&req, 0, sizeof(req));
615
616 ea = ether_aton(val);
617 if (ea == NULL)
618 errx(1, "invalid address: %s", val);
619
620 memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
621
622 if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
623 err(1, "BRDGDADDR %s", val);
624 }
625
626 static void
627 setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
628 {
629
630 bridge_addresses(s, "");
631 }
632
633 static void
634 setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
635 {
636 struct ifbrparam param;
637 u_long val;
638
639 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
640 errx(1, "invalid value: %s", arg);
641
642 param.ifbrp_csize = val & 0xffffffff;
643
644 if (do_cmd(s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
645 err(1, "BRDGSCACHE %s", arg);
646 }
647
648 static void
649 setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
650 {
651 struct ifbrparam param;
652 u_long val;
653
654 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
655 errx(1, "invalid value: %s", arg);
656
657 param.ifbrp_hellotime = val & 0xff;
658
659 if (do_cmd(s, BRDGSHT, &param, sizeof(param), 1) < 0)
660 err(1, "BRDGSHT %s", arg);
661 }
662
663 static void
664 setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
665 {
666 struct ifbrparam param;
667 u_long val;
668
669 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
670 errx(1, "invalid value: %s", arg);
671
672 param.ifbrp_fwddelay = val & 0xff;
673
674 if (do_cmd(s, BRDGSFD, &param, sizeof(param), 1) < 0)
675 err(1, "BRDGSFD %s", arg);
676 }
677
678 static void
679 setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
680 {
681 struct ifbrparam param;
682 u_long val;
683
684 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
685 errx(1, "invalid value: %s", arg);
686
687 param.ifbrp_maxage = val & 0xff;
688
689 if (do_cmd(s, BRDGSMA, &param, sizeof(param), 1) < 0)
690 err(1, "BRDGSMA %s", arg);
691 }
692
693 static void
694 setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
695 {
696 struct ifbrparam param;
697 u_long val;
698
699 if (get_val(arg, &val) < 0 || (val & ~0xffff) != 0)
700 errx(1, "invalid value: %s", arg);
701
702 param.ifbrp_prio = val & 0xffff;
703
704 if (do_cmd(s, BRDGSPRI, &param, sizeof(param), 1) < 0)
705 err(1, "BRDGSPRI %s", arg);
706 }
707
708 #ifdef notdef
709 static void
710 setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
711 {
712 struct ifbrparam param;
713
714 if (strcasecmp(arg, "stp") == 0) {
715 param.ifbrp_proto = 0;
716 } else if (strcasecmp(arg, "rstp") == 0) {
717 param.ifbrp_proto = 2;
718 } else {
719 errx(1, "unknown stp protocol");
720 }
721
722 if (do_cmd(s, BRDGSPROTO, &param, sizeof(param), 1) < 0)
723 err(1, "BRDGSPROTO %s", arg);
724 }
725
726 static void
727 setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
728 {
729 struct ifbrparam param;
730 u_long val;
731
732 if (get_val(arg, &val) < 0 || (val & ~0xff) != 0)
733 errx(1, "invalid value: %s", arg);
734
735 param.ifbrp_txhc = val & 0xff;
736
737 if (do_cmd(s, BRDGSTXHC, &param, sizeof(param), 1) < 0)
738 err(1, "BRDGSTXHC %s", arg);
739 }
740 #endif
741
742 static void
743 setbridge_ifpriority(const char *ifn, const char *pri, int s,
744 const struct afswtch *afp)
745 {
746 struct ifbreq req;
747 u_long val;
748
749 memset(&req, 0, sizeof(req));
750
751 if (get_val(pri, &val) < 0 || (val & ~0xff) != 0)
752 errx(1, "invalid value: %s", pri);
753
754 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
755 req.ifbr_priority = val & 0xff;
756
757 if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
758 err(1, "BRDGSIFPRIO %s", pri);
759 }
760
761 static void
762 setbridge_ifpathcost(const char *ifn, const char *cost, int s,
763 const struct afswtch *afp)
764 {
765 struct ifbreq req;
766 u_long val;
767
768 memset(&req, 0, sizeof(req));
769
770 if (get_val(cost, &val) < 0)
771 errx(1, "invalid value: %s", cost);
772
773 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
774 req.ifbr_path_cost = val;
775
776 if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
777 err(1, "BRDGSIFCOST %s", cost);
778 }
779
780 #ifdef notdef
781 static void
782 setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
783 const struct afswtch *afp)
784 {
785 struct ifbreq req;
786 u_long val;
787
788 memset(&req, 0, sizeof(req));
789
790 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
791 errx(1, "invalid value: %s", arg);
792
793 strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
794 req.ifbr_addrmax = val & 0xffffffff;
795
796 if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
797 err(1, "BRDGSIFAMAX %s", arg);
798 }
799 #endif
800
801 static void
802 setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
803 {
804 struct ifbrparam param;
805 u_long val;
806
807 if (get_val(arg, &val) < 0 || (val & ~0xffffffff) != 0)
808 errx(1, "invalid value: %s", arg);
809
810 param.ifbrp_ctime = val & 0xffffffff;
811
812 if (do_cmd(s, BRDGSTO, &param, sizeof(param), 1) < 0)
813 err(1, "BRDGSTO %s", arg);
814 }
815
816 #ifdef notdef
817 static void
818 setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
819 {
820
821 do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
822 }
823
824 static void
825 unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
826 {
827
828 do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
829 }
830 #endif
831
832
833 static void
834 setbridge_hostfilter(const char *ifn, const char *addr, int s,
835 const struct afswtch *afp)
836 {
837 struct ifbrhostfilter req;
838 struct ether_addr *ea;
839 struct in_addr in;
840
841 memset(&req, 0, sizeof(req));
842 req.ifbrhf_flags = IFBRHF_ENABLED;
843
844 strlcpy(req.ifbrhf_ifsname, ifn, sizeof(req.ifbrhf_ifsname));
845
846 ea = ether_aton(addr);
847 if (ea != NULL) {
848 req.ifbrhf_flags |= IFBRHF_HWSRC;
849 bcopy(ea, req.ifbrhf_hwsrca, sizeof(req.ifbrhf_hwsrca));
850 } else if (inet_aton(addr, &in) != 0) {
851 req.ifbrhf_flags |= IFBRHF_IPSRC;
852 req.ifbrhf_ipsrc = in.s_addr;
853 } else
854 errx(1, "invalid address: %s", addr);
855
856 if (do_cmd(s, BRDGSHOSTFILTER, &req, sizeof(req), 1) < 0)
857 err(1, "BRDGSHOSTFILTER %s %s", ifn, addr);
858 }
859
860 static void
861 unsetbridge_hostfilter(const char *ifn, int d, int s, const struct afswtch *afp)
862 {
863 struct ifbrhostfilter req;
864
865 memset(&req, 0, sizeof(req));
866 strlcpy(req.ifbrhf_ifsname, ifn, sizeof(req.ifbrhf_ifsname));
867
868 if (do_cmd(s, BRDGSHOSTFILTER, &req, sizeof(req), 1) < 0)
869 err(1, "BRDGSHOSTFILTER");
870 }
871
872 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 135000
873 static void
874 setbridge_macnat(const char *val, int d, int s, const struct afswtch *afp)
875 {
876
877 do_bridgeflag(s, val, IFBIF_MAC_NAT, 1);
878 }
879
880 static void
881 unsetbridge_macnat(const char *val, int d, int s, const struct afswtch *afp)
882 {
883
884 do_bridgeflag(s, val, IFBIF_MAC_NAT, 0);
885 }
886 #endif
887
888 static struct cmd bridge_cmds[] = {
889 DEF_CMD_ARG("addm", setbridge_add),
890 DEF_CMD_ARG("deletem", setbridge_delete),
891 DEF_CMD_ARG("discover", setbridge_discover),
892 DEF_CMD_ARG("-discover", unsetbridge_discover),
893 DEF_CMD_ARG("learn", setbridge_learn),
894 DEF_CMD_ARG("-learn", unsetbridge_learn),
895 #ifdef notdef
896 DEF_CMD_ARG("sticky", setbridge_sticky),
897 DEF_CMD_ARG("-sticky", unsetbridge_sticky),
898 DEF_CMD_ARG("span", setbridge_span),
899 DEF_CMD_ARG("-span", unsetbridge_span),
900 #endif
901 DEF_CMD_ARG("stp", setbridge_stp),
902 DEF_CMD_ARG("-stp", unsetbridge_stp),
903 #ifdef notdef
904 DEF_CMD_ARG("edge", setbridge_edge),
905 DEF_CMD_ARG("-edge", unsetbridge_edge),
906 DEF_CMD_ARG("autoedge", setbridge_autoedge),
907 DEF_CMD_ARG("-autoedge", unsetbridge_autoedge),
908 DEF_CMD_ARG("ptp", setbridge_ptp),
909 DEF_CMD_ARG("-ptp", unsetbridge_ptp),
910 DEF_CMD_ARG("autoptp", setbridge_autoptp),
911 DEF_CMD_ARG("-autoptp", unsetbridge_autoptp),
912 #endif
913 DEF_CMD("flush", 0, setbridge_flush),
914 DEF_CMD("flushall", 0, setbridge_flushall),
915 DEF_CMD_ARG2("static", setbridge_static),
916 DEF_CMD_ARG("deladdr", setbridge_deladdr),
917 DEF_CMD("addr", 1, setbridge_addr),
918 DEF_CMD_ARG("maxaddr", setbridge_maxaddr),
919 DEF_CMD_ARG("hellotime", setbridge_hellotime),
920 DEF_CMD_ARG("fwddelay", setbridge_fwddelay),
921 DEF_CMD_ARG("maxage", setbridge_maxage),
922 DEF_CMD_ARG("priority", setbridge_priority),
923 #ifdef notdef
924 DEF_CMD_ARG("proto", setbridge_protocol),
925 DEF_CMD_ARG("holdcnt", setbridge_holdcount),
926 #endif
927 DEF_CMD_ARG2("ifpriority", setbridge_ifpriority),
928 DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost),
929 #ifdef notdef
930 DEF_CMD_ARG2("ifmaxaddr", setbridge_ifmaxaddr),
931 #endif
932 DEF_CMD_ARG("timeout", setbridge_timeout),
933 #ifdef notdef
934 DEF_CMD_ARG("private", setbridge_private),
935 DEF_CMD_ARG("-private", unsetbridge_private),
936 #endif
937 DEF_CMD_ARG2("hostfilter", setbridge_hostfilter),
938 DEF_CMD_ARG("-hostfilter", unsetbridge_hostfilter),
939 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 135000
940 DEF_CMD_ARG("macnat", setbridge_macnat),
941 DEF_CMD_ARG("-macnat", unsetbridge_macnat),
942 #endif
943 };
944 static struct afswtch af_bridge = {
945 .af_name = "af_bridge",
946 .af_af = AF_UNSPEC,
947 .af_other_status = bridge_status,
948 };
949
950 static __constructor void
951 bridge_ctor(void)
952 {
953 #define N(a) (sizeof(a) / sizeof(a[0]))
954 int i;
955
956 for (i = 0; i < N(bridge_cmds); i++)
957 cmd_register(&bridge_cmds[i]);
958 af_register(&af_bridge);
959 #undef N
960 }