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