2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Shteryana Shopova <syrinx@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Bridge MIB implementation for SNMPd.
29 * Bridge OS specific ioctls.
34 #include <sys/ioctl.h>
35 #include <sys/param.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
41 #include <net/bridgestp.h>
42 #include <net/ethernet.h>
44 #include <net/if_bridgevar.h>
45 #include <net/if_dl.h>
46 #include <net/if_mib.h>
47 #include <net/if_types.h>
48 #include <netinet/in.h>
58 #include <bsnmp/snmpmod.h>
59 #include <bsnmp/snmp_mibII.h>
61 #define SNMPTREE_TYPES
62 #include "bridge_tree.h"
63 #include "bridge_snmp.h"
68 bridge_ioctl_init(void)
70 if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
71 syslog(LOG_ERR, "cannot open socket : %s", strerror(errno));
79 * Load the if_bridge.ko module in kernel if not already there.
82 bridge_kmod_load(void)
85 const char mod_name[] = "if_bridge";
86 struct module_stat mstat;
88 /* Scan files in kernel. */
89 mstat.version = sizeof(struct module_stat);
90 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
91 /* Scan modules in file. */
92 for (modid = kldfirstmod(fileid); modid > 0;
93 modid = modfnext(modid)) {
95 if (modstat(modid, &mstat) < 0)
98 if (strcmp(mod_name, mstat.name) == 0)
103 /* Not present - load it. */
104 if (kldload(mod_name) < 0) {
105 syslog(LOG_ERR, "failed to load %s kernel module", mod_name);
112 /************************************************************************
117 * Convert the kernel uint64_t value for a bridge id
120 snmp_uint64_to_bridgeid(uint64_t id, bridge_id b_id)
127 for (i = 0; i < SNMP_BRIDGE_ID_LEN; i++, o++)
128 b_id[SNMP_BRIDGE_ID_LEN - i - 1] = *o;
132 * Fetch the bridge configuration parameters from the kernel excluding
133 * it's base MAC address.
136 bridge_get_conf_param(struct bridge_if *bif)
139 struct ifbrparam b_param;
141 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
142 ifd.ifd_len = sizeof(b_param);
143 ifd.ifd_data = &b_param;
145 /* Bridge priority. */
146 ifd.ifd_cmd = BRDGGPRI;
147 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
148 syslog(LOG_ERR, "update bridge: ioctl(BRDGGPRI) failed: %s",
153 bif->priority = b_param.ifbrp_prio;
155 /* Configured max age. */
156 ifd.ifd_cmd = BRDGGMA;
157 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
158 syslog(LOG_ERR, "update bridge: ioctl(BRDGGMA) failed: %s",
164 bif->bridge_max_age = 100 * b_param.ifbrp_maxage;
166 /* Configured hello time. */
167 ifd.ifd_cmd = BRDGGHT;
168 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
169 syslog(LOG_ERR, "update bridge: ioctl(BRDGGHT) failed: %s",
173 bif->bridge_hello_time = 100 * b_param.ifbrp_hellotime;
176 ifd.ifd_cmd = BRDGGFD;
177 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
178 syslog(LOG_ERR, "update bridge: ioctl(BRDGGFD) failed: %s",
182 bif->bridge_fwd_delay = 100 * b_param.ifbrp_fwddelay;
184 /* Number of dropped addresses. */
185 ifd.ifd_cmd = BRDGGRTE;
186 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
187 syslog(LOG_ERR, "update bridge: ioctl(BRDGGRTE) failed: %s",
191 bif->lrnt_drops = b_param.ifbrp_cexceeded;
193 /* Address table timeout. */
194 ifd.ifd_cmd = BRDGGTO;
195 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
196 syslog(LOG_ERR, "update bridge: ioctl(BRDGGTO) failed: %s",
200 bif->age_time = b_param.ifbrp_ctime;
202 /* Address table size. */
203 ifd.ifd_cmd = BRDGGCACHE;
204 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
205 syslog(LOG_ERR, "update bridge: ioctl(BRDGGCACHE) "
206 "failed: %s", strerror(errno));
209 bif->max_addrs = b_param.ifbrp_csize;
215 * Fetch the current bridge STP operational parameters.
216 * Returns: -1 - on error;
217 * 0 - old TC time and Root Port values are same;
218 * 1 - topologyChange notification should be sent;
219 * 2 - newRoot notification should be sent.
222 bridge_get_op_param(struct bridge_if *bif)
226 struct ifbropreq b_req;
228 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
229 ifd.ifd_len = sizeof(b_req);
230 ifd.ifd_data = &b_req;
231 ifd.ifd_cmd = BRDGPARAM;
233 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
234 syslog(LOG_ERR, "update bridge: ioctl(BRDGPARAM) failed: %s",
239 bif->max_age = 100 * b_req.ifbop_maxage;
240 bif->hello_time = 100 * b_req.ifbop_hellotime;
241 bif->fwd_delay = 100 * b_req.ifbop_fwddelay;
242 bif->stp_version = b_req.ifbop_protocol;
243 bif->tx_hold_count = b_req.ifbop_holdcount;
245 if (b_req.ifbop_root_port == 0 &&
246 bif->root_port != b_req.ifbop_root_port)
251 bif->root_port = b_req.ifbop_root_port;
252 bif->root_cost = b_req.ifbop_root_path_cost;
253 snmp_uint64_to_bridgeid(b_req.ifbop_designated_root,
256 if (bif->last_tc_time.tv_sec != b_req.ifbop_last_tc_time.tv_sec) {
258 bif->last_tc_time.tv_sec = b_req.ifbop_last_tc_time.tv_sec;
259 bif->last_tc_time.tv_usec = b_req.ifbop_last_tc_time.tv_usec;
262 * "The trap is not sent if a (begemotBridge)NewRoot
263 * trap is sent for the same transition."
265 if (new_root_send == 0)
269 return (new_root_send);
273 bridge_getinfo_bif(struct bridge_if *bif)
275 if (bridge_get_conf_param(bif) < 0)
278 return (bridge_get_op_param(bif));
282 bridge_set_priority(struct bridge_if *bif, int32_t priority)
285 struct ifbrparam b_param;
287 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
288 ifd.ifd_len = sizeof(b_param);
289 ifd.ifd_data = &b_param;
290 b_param.ifbrp_prio = (uint32_t) priority;
291 ifd.ifd_cmd = BRDGSPRI;
293 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
294 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSPRI) "
295 "failed: %s", strerror(errno));
300 * Re-fetching the data from the driver after that might be a good
301 * idea, since changing our bridge's priority should invoke
302 * recalculation of the active spanning tree topology in the network.
304 bif->priority = priority;
309 * Convert 1/100 of seconds to 1/256 of seconds.
310 * Timeout ::= TEXTUAL-CONVENTION.
311 * To convert a Timeout value into a value in units of
312 * 1/256 seconds, the following algorithm should be used:
313 * b = floor( (n * 256) / 100)
314 * The conversion to 1/256 of a second happens in the kernel -
315 * just make sure we correctly convert the seconds to Timout
319 snmp_timeout2_sec(int32_t secs)
325 bridge_set_maxage(struct bridge_if *bif, int32_t max_age)
328 struct ifbrparam b_param;
330 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
331 ifd.ifd_len = sizeof(b_param);
332 ifd.ifd_data = &b_param;
333 b_param.ifbrp_maxage = snmp_timeout2_sec(max_age);
334 ifd.ifd_cmd = BRDGSMA;
336 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
337 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSMA) "
338 "failed: %s", strerror(errno));
342 bif->bridge_max_age = max_age;
347 bridge_set_hello_time(struct bridge_if *bif, int32_t hello_time)
350 struct ifbrparam b_param;
352 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
353 ifd.ifd_len = sizeof(b_param);
354 ifd.ifd_data = &b_param;
355 b_param.ifbrp_hellotime = snmp_timeout2_sec(hello_time);
356 ifd.ifd_cmd = BRDGSHT;
358 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
359 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSHT) "
360 "failed: %s", strerror(errno));
364 bif->bridge_hello_time = b_param.ifbrp_hellotime;
369 bridge_set_forward_delay(struct bridge_if *bif, int32_t fwd_delay)
372 struct ifbrparam b_param;
374 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
375 ifd.ifd_len = sizeof(b_param);
376 ifd.ifd_data = &b_param;
377 b_param.ifbrp_fwddelay = snmp_timeout2_sec(fwd_delay);
378 ifd.ifd_cmd = BRDGSFD;
380 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
381 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSFD) "
382 "failed: %s", strerror(errno));
386 bif->bridge_fwd_delay = b_param.ifbrp_fwddelay;
391 bridge_set_aging_time(struct bridge_if *bif, int32_t age_time)
394 struct ifbrparam b_param;
396 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
397 ifd.ifd_len = sizeof(b_param);
398 ifd.ifd_data = &b_param;
399 b_param.ifbrp_ctime = (uint32_t) age_time;
400 ifd.ifd_cmd = BRDGSTO;
402 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
403 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSTO) "
404 "failed: %s", strerror(errno));
408 bif->age_time = age_time;
413 bridge_set_max_cache(struct bridge_if *bif, int32_t max_cache)
416 struct ifbrparam b_param;
418 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
419 ifd.ifd_len = sizeof(b_param);
420 ifd.ifd_data = &b_param;
421 b_param.ifbrp_csize = max_cache;
422 ifd.ifd_cmd = BRDGSCACHE;
424 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
425 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSCACHE) "
426 "failed: %s", strerror(errno));
430 bif->max_addrs = b_param.ifbrp_csize;
435 bridge_set_tx_hold_count(struct bridge_if *bif, int32_t tx_hc)
438 struct ifbrparam b_param;
440 if (tx_hc < SNMP_BRIDGE_MIN_TXHC || tx_hc > SNMP_BRIDGE_MAX_TXHC)
443 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
444 ifd.ifd_len = sizeof(b_param);
445 ifd.ifd_data = &b_param;
446 b_param.ifbrp_txhc = tx_hc;
447 ifd.ifd_cmd = BRDGSTXHC;
449 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
450 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSTXHC) "
451 "failed: %s", strerror(errno));
455 bif->tx_hold_count = b_param.ifbrp_txhc;
460 bridge_set_stp_version(struct bridge_if *bif, int32_t stp_proto)
463 struct ifbrparam b_param;
465 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
466 ifd.ifd_len = sizeof(b_param);
467 ifd.ifd_data = &b_param;
468 b_param.ifbrp_proto = stp_proto;
469 ifd.ifd_cmd = BRDGSPROTO;
471 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
472 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSPROTO) "
473 "failed: %s", strerror(errno));
477 bif->stp_version = b_param.ifbrp_proto;
482 * Set the bridge interface status to up/down.
485 bridge_set_if_up(const char* b_name, int8_t up)
490 bzero(&ifr, sizeof(ifr));
491 strlcpy(ifr.ifr_name, b_name, sizeof(ifr.ifr_name));
492 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
493 syslog(LOG_ERR, "set bridge up: ioctl(SIOCGIFFLAGS) "
494 "failed: %s", strerror(errno));
498 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
504 ifr.ifr_flags = flags & 0xffff;
505 ifr.ifr_flagshigh = flags >> 16;
506 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
507 syslog(LOG_ERR, "set bridge up: ioctl(SIOCSIFFLAGS) "
508 "failed: %s", strerror(errno));
516 bridge_create(const char *b_name)
521 bzero(&ifr, sizeof(ifr));
522 strlcpy(ifr.ifr_name, b_name, sizeof(ifr.ifr_name));
524 if (ioctl(sock, SIOCIFCREATE, &ifr) < 0) {
525 syslog(LOG_ERR, "create bridge: ioctl(SIOCIFCREATE) "
526 "failed: %s", strerror(errno));
530 if (strcmp(b_name, ifr.ifr_name) == 0)
533 if ((new_name = strdup(b_name)) == NULL) {
534 syslog(LOG_ERR, "create bridge: strdup() failed");
538 ifr.ifr_data = new_name;
539 if (ioctl(sock, SIOCSIFNAME, (caddr_t) &ifr) < 0) {
540 syslog(LOG_ERR, "create bridge: ioctl(SIOCSIFNAME) "
541 "failed: %s", strerror(errno));
550 bridge_destroy(const char *b_name)
554 bzero(&ifr, sizeof(ifr));
555 strlcpy(ifr.ifr_name, b_name, sizeof(ifr.ifr_name));
557 if (ioctl(sock, SIOCIFDESTROY, &ifr) < 0) {
558 syslog(LOG_ERR, "destroy bridge: ioctl(SIOCIFDESTROY) "
559 "failed: %s", strerror(errno));
567 * Fetch the bridge base MAC address. Return pointer to the
568 * buffer containing the MAC address, NULL on failure.
571 bridge_get_basemac(const char *bif_name, u_char *mac, size_t mlen)
574 char if_name[IFNAMSIZ];
575 struct ifaddrs *ifap, *ifa;
576 struct sockaddr_dl sdl;
578 if (getifaddrs(&ifap) != 0) {
579 syslog(LOG_ERR, "bridge get mac: getifaddrs() failed - %s",
584 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
585 if (ifa->ifa_addr->sa_family != AF_LINK)
589 * Not just casting because of alignment constraints.
591 bcopy(ifa->ifa_addr, &sdl, sizeof(struct sockaddr_dl));
593 if (sdl.sdl_alen > mlen)
596 if ((len = sdl.sdl_nlen) >= IFNAMSIZ)
599 bcopy(sdl.sdl_data, if_name, len);
602 if (strcmp(bif_name, if_name) == 0) {
603 bcopy(sdl.sdl_data + sdl.sdl_nlen, mac, sdl.sdl_alen);
613 /************************************************************************
618 * Convert the kernel STP port state into
619 * the corresopnding enumerated type from SNMP Bridge MIB.
622 state2snmp_st(uint8_t ifbr_state)
624 switch (ifbr_state) {
625 case BSTP_IFSTATE_DISABLED:
626 return (StpPortState_disabled);
627 case BSTP_IFSTATE_LISTENING:
628 return (StpPortState_listening);
629 case BSTP_IFSTATE_LEARNING:
630 return (StpPortState_learning);
631 case BSTP_IFSTATE_FORWARDING:
632 return (StpPortState_forwarding);
633 case BSTP_IFSTATE_BLOCKING:
634 case BSTP_IFSTATE_DISCARDING:
635 return (StpPortState_blocking);
638 return (StpPortState_broken);
642 * Fill in a bridge member information according to data polled from kernel.
645 bridge_port_getinfo_conf(struct ifbreq *k_info, struct bridge_port *bp)
647 bp->state = state2snmp_st(k_info->ifbr_state);
648 bp->priority = k_info->ifbr_priority;
652 * "New implementations should support dot1dStpPortPathCost32.
653 * If the port path costs exceeds the maximum value of this
654 * object then this object should report the maximum value,
655 * namely 65535. Applications should try to read the
656 * dot1dStpPortPathCost32 object if this object reports
657 * the maximum value."
660 if (k_info->ifbr_ifsflags & IFBIF_BSTP_ADMCOST)
661 bp->admin_path_cost = k_info->ifbr_path_cost;
663 bp->admin_path_cost = 0;
665 bp->path_cost = k_info->ifbr_path_cost;
667 if (k_info->ifbr_ifsflags & IFBIF_STP)
668 bp->enable = dot1dStpPortEnable_enabled;
670 bp->enable = dot1dStpPortEnable_disabled;
672 /* Begemot Bridge MIB only. */
673 if (k_info->ifbr_ifsflags & IFBIF_SPAN)
674 bp->span_enable = begemotBridgeBaseSpanEnabled_enabled;
676 bp->span_enable = begemotBridgeBaseSpanEnabled_disabled;
678 if (k_info->ifbr_ifsflags & IFBIF_PRIVATE)
679 bp->priv_set = TruthValue_true;
681 bp->priv_set = TruthValue_false;
683 if (k_info->ifbr_ifsflags & IFBIF_BSTP_ADMEDGE)
684 bp->admin_edge = TruthValue_true;
686 bp->admin_edge = TruthValue_false;
688 if (k_info->ifbr_ifsflags & IFBIF_BSTP_EDGE)
689 bp->oper_edge = TruthValue_true;
691 bp->oper_edge = TruthValue_false;
693 if (k_info->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP) {
694 bp->admin_ptp = StpPortAdminPointToPointType_auto;
695 if (k_info->ifbr_ifsflags & IFBIF_BSTP_PTP)
696 bp->oper_ptp = TruthValue_true;
698 bp->oper_ptp = TruthValue_false;
699 } else if (k_info->ifbr_ifsflags & IFBIF_BSTP_PTP) {
700 bp->admin_ptp = StpPortAdminPointToPointType_forceTrue;
701 bp->oper_ptp = TruthValue_true;
703 bp->admin_ptp = StpPortAdminPointToPointType_forceFalse;
704 bp->oper_ptp = TruthValue_false;
709 * Fill in a bridge interface STP information according to
710 * data polled from kernel.
713 bridge_port_getinfo_opstp(struct ifbpstpreq *bp_stp, struct bridge_port *bp)
715 bp->enable = dot1dStpPortEnable_enabled;
716 bp->fwd_trans = bp_stp->ifbp_fwd_trans;
717 bp->design_cost = bp_stp->ifbp_design_cost;
718 snmp_uint64_to_bridgeid(bp_stp->ifbp_design_root, bp->design_root);
719 snmp_uint64_to_bridgeid(bp_stp->ifbp_design_bridge, bp->design_bridge);
720 bcopy(&(bp_stp->ifbp_design_port), &(bp->design_port),
725 * Clear a bridge interface STP information.
728 bridge_port_clearinfo_opstp(struct bridge_port *bp)
730 if (bp->enable == dot1dStpPortEnable_enabled) {
732 bzero(&(bp->design_root), sizeof(bridge_id));
733 bzero(&(bp->design_bridge), sizeof(bridge_id));
734 bzero(&(bp->design_port), sizeof(port_id));
738 bp->enable = dot1dStpPortEnable_disabled;
742 * Set a bridge member priority.
745 bridge_port_set_priority(const char *bif_name, struct bridge_port *bp,
751 strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
752 ifd.ifd_len = sizeof(b_req);
753 ifd.ifd_data = &b_req;
754 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
756 b_req.ifbr_priority = (uint8_t) priority;
757 ifd.ifd_cmd = BRDGSIFPRIO;
759 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
760 syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFPRIO) "
761 "failed: %s", bp->p_name, strerror(errno));
765 bp->priority = priority;
770 * Set a bridge member STP-enabled flag.
773 bridge_port_set_stp_enable(const char *bif_name, struct bridge_port *bp,
779 if (bp->enable == enable)
782 bzero(&b_req, sizeof(b_req));
783 strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
784 ifd.ifd_len = sizeof(b_req);
785 ifd.ifd_data = &b_req;
786 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
787 ifd.ifd_cmd = BRDGGIFFLGS;
789 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
790 syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
791 "failed: %s", bp->p_name, strerror(errno));
795 if (enable == dot1dStpPortEnable_enabled)
796 b_req.ifbr_ifsflags |= IFBIF_STP;
798 b_req.ifbr_ifsflags &= ~IFBIF_STP;
800 ifd.ifd_cmd = BRDGSIFFLGS;
801 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
802 syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
803 "failed: %s", bp->p_name, strerror(errno));
812 * Set a bridge member STP path cost.
815 bridge_port_set_path_cost(const char *bif_name, struct bridge_port *bp,
821 if (path_cost < SNMP_PORT_MIN_PATHCOST ||
822 path_cost > SNMP_PORT_PATHCOST_OBSOLETE)
825 strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
826 ifd.ifd_len = sizeof(b_req);
827 ifd.ifd_data = &b_req;
828 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
830 b_req.ifbr_path_cost = path_cost;
831 ifd.ifd_cmd = BRDGSIFCOST;
833 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
834 syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFCOST) "
835 "failed: %s", bp->p_name, strerror(errno));
839 bp->admin_path_cost = path_cost;
845 * Set the PonitToPoint status of the link administratively.
848 bridge_port_set_admin_ptp(const char *bif_name, struct bridge_port *bp,
854 if (bp->admin_ptp == admin_ptp)
857 bzero(&b_req, sizeof(b_req));
858 strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
859 ifd.ifd_len = sizeof(b_req);
860 ifd.ifd_data = &b_req;
861 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
862 ifd.ifd_cmd = BRDGGIFFLGS;
864 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
865 syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
866 "failed: %s", bp->p_name, strerror(errno));
871 case StpPortAdminPointToPointType_forceTrue:
872 b_req.ifbr_ifsflags &= ~IFBIF_BSTP_AUTOPTP;
873 b_req.ifbr_ifsflags |= IFBIF_BSTP_PTP;
875 case StpPortAdminPointToPointType_forceFalse:
876 b_req.ifbr_ifsflags &= ~IFBIF_BSTP_AUTOPTP;
877 b_req.ifbr_ifsflags &= ~IFBIF_BSTP_PTP;
879 case StpPortAdminPointToPointType_auto:
880 b_req.ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
884 ifd.ifd_cmd = BRDGSIFFLGS;
885 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
886 syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
887 "failed: %s", bp->p_name, strerror(errno));
891 bp->admin_ptp = admin_ptp;
899 bridge_port_set_admin_edge(const char *bif_name, struct bridge_port *bp,
905 if (bp->admin_edge == enable)
908 bzero(&b_req, sizeof(b_req));
909 strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
910 ifd.ifd_len = sizeof(b_req);
911 ifd.ifd_data = &b_req;
912 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
913 ifd.ifd_cmd = BRDGGIFFLGS;
915 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
916 syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
917 "failed: %s", bp->p_name, strerror(errno));
921 if (enable == TruthValue_true) {
922 b_req.ifbr_ifsflags &= ~IFBIF_BSTP_AUTOEDGE;
923 b_req.ifbr_ifsflags |= IFBIF_BSTP_EDGE;
925 b_req.ifbr_ifsflags &= ~IFBIF_BSTP_EDGE;
927 ifd.ifd_cmd = BRDGSIFFLGS;
928 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
929 syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
930 "failed: %s", bp->p_name, strerror(errno));
934 bp->admin_edge = enable;
940 * Set 'private' flag.
943 bridge_port_set_private(const char *bif_name, struct bridge_port *bp,
949 if (bp->priv_set == priv_set)
952 bzero(&b_req, sizeof(b_req));
953 strlcpy(ifd.ifd_name, bif_name, sizeof(ifd.ifd_name));
954 ifd.ifd_len = sizeof(b_req);
955 ifd.ifd_data = &b_req;
956 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
957 ifd.ifd_cmd = BRDGGIFFLGS;
959 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
960 syslog(LOG_ERR, "get member %s param: ioctl(BRDGGIFFLGS) "
961 "failed: %s", bp->p_name, strerror(errno));
965 if (priv_set == TruthValue_true)
966 b_req.ifbr_ifsflags |= IFBIF_PRIVATE;
967 else if (priv_set == TruthValue_false)
968 b_req.ifbr_ifsflags &= ~IFBIF_PRIVATE;
970 return (SNMP_ERR_WRONG_VALUE);
972 ifd.ifd_cmd = BRDGSIFFLGS;
973 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
974 syslog(LOG_ERR, "set member %s param: ioctl(BRDGSIFFLGS) "
975 "failed: %s", bp->p_name, strerror(errno));
979 bp->priv_set = priv_set;
986 * Add a bridge member port.
989 bridge_port_addm(struct bridge_port *bp, const char *b_name)
994 bzero(&ifd, sizeof(ifd));
995 bzero(&b_req, sizeof(b_req));
997 strlcpy(ifd.ifd_name, b_name, sizeof(ifd.ifd_name));
998 ifd.ifd_len = sizeof(b_req);
999 ifd.ifd_data = &b_req;
1000 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
1002 if (bp->span_enable == begemotBridgeBaseSpanEnabled_enabled)
1003 ifd.ifd_cmd = BRDGADDS;
1005 ifd.ifd_cmd = BRDGADD;
1007 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
1008 syslog(LOG_ERR, "%s - add member : ioctl(%s) failed: %s",
1010 (ifd.ifd_cmd == BRDGADDS ? "BRDGADDS" : "BRDGADD"),
1019 * Delete a bridge member port.
1022 bridge_port_delm(struct bridge_port *bp, const char *b_name)
1025 struct ifbreq b_req;
1027 bzero(&ifd, sizeof(ifd));
1028 bzero(&b_req, sizeof(b_req));
1030 strlcpy(ifd.ifd_name, b_name, sizeof(ifd.ifd_name));
1031 ifd.ifd_len = sizeof(b_req);
1032 ifd.ifd_data = &b_req;
1033 strlcpy(b_req.ifbr_ifsname, bp->p_name, sizeof(b_req.ifbr_ifsname));
1035 if (bp->span_enable == begemotBridgeBaseSpanEnabled_enabled)
1036 ifd.ifd_cmd = BRDGDELS;
1038 ifd.ifd_cmd = BRDGDEL;
1040 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
1041 syslog(LOG_ERR, "%s - add member : ioctl(%s) failed: %s",
1043 (ifd.ifd_cmd == BRDGDELS ? "BRDGDELS" : "BRDGDEL"),
1052 * Fetch the bridge member list from kernel.
1053 * Return -1 on error, or buffer len if successful.
1056 bridge_port_get_iflist(struct bridge_if *bif, struct ifbreq **buf)
1060 struct ifbreq *ninbuf;
1061 struct ifbifconf ifbc;
1065 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
1066 ifd.ifd_cmd = BRDGGIFS;
1067 ifd.ifd_len = sizeof(ifbc);
1068 ifd.ifd_data = &ifbc;
1071 len = n * sizeof(struct ifbreq);
1072 if ((ninbuf = (struct ifbreq *)realloc(*buf, len)) == NULL) {
1073 syslog(LOG_ERR, "get bridge member list: "
1074 "realloc failed: %s", strerror(errno));
1080 ifbc.ifbic_len = len;
1081 ifbc.ifbic_req = *buf = ninbuf;
1083 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
1084 syslog(LOG_ERR, "get bridge member list: ioctl "
1085 "(BRDGGIFS) failed: %s", strerror(errno));
1091 if ((ifbc.ifbic_len + sizeof(struct ifbreq)) < len)
1097 return (ifbc.ifbic_len);
1101 * Fetch the bridge STP member list from kernel.
1102 * Return -1 on error, or buffer len if successful.
1105 bridge_port_get_ifstplist(struct bridge_if *bif, struct ifbpstpreq **buf)
1109 struct ifbpstpreq *ninbuf;
1110 struct ifbpstpconf ifbstp;
1114 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
1115 ifd.ifd_cmd = BRDGGIFSSTP;
1116 ifd.ifd_len = sizeof(ifbstp);
1117 ifd.ifd_data = &ifbstp;
1120 len = n * sizeof(struct ifbpstpreq);
1121 if ((ninbuf = (struct ifbpstpreq *)
1122 realloc(*buf, len)) == NULL) {
1123 syslog(LOG_ERR, "get bridge STP ports list: "
1124 "realloc failed: %s", strerror(errno));
1130 ifbstp.ifbpstp_len = len;
1131 ifbstp.ifbpstp_req = *buf = ninbuf;
1133 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
1134 syslog(LOG_ERR, "get bridge STP ports list: ioctl "
1135 "(BRDGGIFSSTP) failed: %s", strerror(errno));
1141 if ((ifbstp.ifbpstp_len + sizeof(struct ifbpstpreq)) < len)
1147 return (ifbstp.ifbpstp_len);
1151 * Locate a bridge if STP params structure in a buffer.
1153 static struct ifbpstpreq *
1154 bridge_port_find_ifstplist(uint8_t port_no, struct ifbpstpreq *buf,
1158 struct ifbpstpreq *bstp;
1160 for (i = 0; i < buf_len / sizeof(struct ifbpstpreq); i++) {
1162 if (bstp->ifbp_portno == port_no)
1170 * Read the initial info for all members of a bridge interface.
1171 * Returns the number of ports, 0 - if none, otherwise
1172 * -1 if some other error occurred.
1175 bridge_getinfo_bif_ports(struct bridge_if *bif)
1179 struct ifbreq *b_req_buf, *b_req;
1180 struct ifbpstpreq *bs_req_buf, *bs_req;
1181 struct bridge_port *bp;
1184 if ((buf_len = bridge_port_get_iflist(bif, &b_req_buf)) < 0)
1187 for (i = 0; i < buf_len / sizeof(struct ifbreq); i++) {
1188 b_req = b_req_buf + i;
1190 if ((m_if = mib_find_if_sys(b_req->ifbr_portno)) != NULL) {
1191 /* Hopefully we will not fail here. */
1192 if ((bp = bridge_new_port(m_if, bif)) != NULL) {
1193 bp->status = RowStatus_active;
1194 bridge_port_getinfo_conf(b_req, bp);
1195 bridge_port_getinfo_mibif(m_if, bp);
1198 syslog(LOG_ERR, "bridge member %s not present "
1199 "in mibII ifTable", b_req->ifbr_ifsname);
1204 if ((buf_len = bridge_port_get_ifstplist(bif, &bs_req_buf)) < 0)
1207 for (bp = bridge_port_bif_first(bif); bp != NULL;
1208 bp = bridge_port_bif_next(bp)) {
1209 if ((bs_req = bridge_port_find_ifstplist(bp->port_no,
1210 bs_req_buf, buf_len)) == NULL)
1211 bridge_port_clearinfo_opstp(bp);
1213 bridge_port_getinfo_opstp(bs_req, bp);
1221 * Update the information for the bridge interface members.
1224 bridge_update_memif(struct bridge_if *bif)
1229 struct ifbreq *b_req_buf, *b_req;
1230 struct ifbpstpreq *bs_req_buf, *bs_req;
1231 struct bridge_port *bp, *bp_next;
1234 if ((buf_len = bridge_port_get_iflist(bif, &b_req_buf)) < 0)
1237 added = updated = 0;
1239 #define BP_FOUND 0x01
1240 for (i = 0; i < buf_len / sizeof(struct ifbreq); i++) {
1241 b_req = b_req_buf + i;
1243 if ((m_if = mib_find_if_sys(b_req->ifbr_portno)) == NULL) {
1244 syslog(LOG_ERR, "bridge member %s not present "
1245 "in mibII ifTable", b_req->ifbr_ifsname);
1249 if ((bp = bridge_port_find(m_if->index, bif)) == NULL &&
1250 (bp = bridge_new_port(m_if, bif)) != NULL) {
1251 bp->status = RowStatus_active;
1257 bridge_port_getinfo_conf(b_req, bp);
1258 bridge_port_getinfo_mibif(m_if, bp);
1259 bp->flags |= BP_FOUND;
1264 /* Clean up list. */
1265 for (bp = bridge_port_bif_first(bif); bp != NULL; bp = bp_next) {
1266 bp_next = bridge_port_bif_next(bp);
1268 if ((bp->flags & BP_FOUND) == 0 &&
1269 bp->status == RowStatus_active)
1270 bridge_port_remove(bp, bif);
1272 bp->flags |= ~BP_FOUND;
1276 if ((buf_len = bridge_port_get_ifstplist(bif, &bs_req_buf)) < 0)
1279 for (bp = bridge_port_bif_first(bif); bp != NULL;
1280 bp = bridge_port_bif_next(bp)) {
1281 if ((bs_req = bridge_port_find_ifstplist(bp->port_no,
1282 bs_req_buf, buf_len)) == NULL)
1283 bridge_port_clearinfo_opstp(bp);
1285 bridge_port_getinfo_opstp(bs_req, bp);
1288 bif->ports_age = time(NULL);
1293 /************************************************************************
1298 * Update the bridge address info according to the polled data.
1301 bridge_addrs_info_ifaddrlist(struct ifbareq *ifba, struct tp_entry *tpe)
1303 tpe->port_no = if_nametoindex(ifba->ifba_ifsname);
1305 if ((ifba->ifba_flags & IFBAF_TYPEMASK) == IFBAF_STATIC)
1306 tpe->status = TpFdbStatus_mgmt;
1308 tpe->status = TpFdbStatus_learned;
1312 * Read the bridge addresses from kernel.
1313 * Return -1 on error, or buffer len if successful.
1316 bridge_addrs_getinfo_ifalist(struct bridge_if *bif, struct ifbareq **buf)
1320 struct ifbareq *ninbuf;
1321 struct ifbaconf bac;
1325 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
1326 ifd.ifd_cmd = BRDGRTS;
1327 ifd.ifd_len = sizeof(bac);
1328 ifd.ifd_data = &bac;
1331 len = n * sizeof(struct ifbareq);
1332 if ((ninbuf = (struct ifbareq *)realloc(*buf, len)) == NULL) {
1333 syslog(LOG_ERR, "get bridge address list: "
1334 " realloc failed: %s", strerror(errno));
1340 bac.ifbac_len = len;
1341 bac.ifbac_req = *buf = ninbuf;
1343 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
1344 syslog(LOG_ERR, "get bridge address list: "
1345 "ioctl(BRDGRTS) failed: %s", strerror(errno));
1351 if ((bac.ifbac_len + sizeof(struct ifbareq)) < len)
1357 return (bac.ifbac_len);
1361 * Read the initial info for all addresses on a bridge interface.
1362 * Returns the number of addresses, 0 - if none, otherwise
1363 * -1 if some other error occurred.
1366 bridge_getinfo_bif_addrs(struct bridge_if *bif)
1370 struct ifbareq *addr_req_buf, *addr_req;
1371 struct tp_entry *te;
1373 if ((buf_len = bridge_addrs_getinfo_ifalist(bif, &addr_req_buf)) < 0)
1376 for (i = 0; i < buf_len / sizeof(struct ifbareq); i++) {
1377 addr_req = addr_req_buf + i;
1379 if ((te = bridge_new_addrs(addr_req->ifba_dst, bif)) != NULL)
1380 bridge_addrs_info_ifaddrlist(addr_req, te);
1388 * Update the addresses for the bridge interface.
1391 bridge_update_addrs(struct bridge_if *bif)
1396 struct tp_entry *te, *te_next;
1397 struct ifbareq *addr_req_buf, *addr_req;
1399 if ((buf_len = bridge_addrs_getinfo_ifalist(bif, &addr_req_buf)) < 0)
1402 added = updated = 0;
1404 #define BA_FOUND 0x01
1405 for (i = 0; i < buf_len / sizeof(struct ifbareq); i++) {
1406 addr_req = addr_req_buf + i;
1408 if ((te = bridge_addrs_find(addr_req->ifba_dst, bif)) == NULL) {
1411 if ((te = bridge_new_addrs(addr_req->ifba_dst, bif))
1417 bridge_addrs_info_ifaddrlist(addr_req, te);
1418 te-> flags |= BA_FOUND;
1422 for (te = bridge_addrs_bif_first(bif); te != NULL; te = te_next) {
1423 te_next = bridge_addrs_bif_next(te);
1425 if ((te-> flags & BA_FOUND) == 0)
1426 bridge_addrs_remove(te, bif);
1428 te-> flags &= ~BA_FOUND;
1432 bif->addrs_age = time(NULL);
1433 return (updated + added);
1436 /************************************************************************
1437 * Bridge packet filtering.
1439 const char bridge_sysctl[] = "net.link.bridge.";
1444 } bridge_pf_sysctl[] = {
1445 { 1, "pfil_bridge" },
1446 { 1, "pfil_member" },
1447 { 1, "pfil_onlyip" },
1452 bridge_get_pfval(uint8_t which)
1455 if (which > nitems(bridge_pf_sysctl) || which < 1)
1458 return (bridge_pf_sysctl[which - 1].val);
1462 bridge_do_pfctl(int32_t bridge_ctl, enum snmp_op op, int32_t *val)
1468 if (bridge_ctl >= LEAF_begemotBridgeLayer2PfStatus)
1471 if (op == SNMP_OP_SET) {
1473 s_len = sizeof(s_i);
1479 asprintf(&mib_oid, "%s%s", bridge_sysctl,
1480 bridge_pf_sysctl[bridge_ctl].name);
1481 if (mib_oid == NULL)
1484 if (sysctlbyname(mib_oid, &i, &len, (op == SNMP_OP_SET ? &s_i : NULL),
1486 syslog(LOG_ERR, "sysctl(%s) failed - %s", mib_oid,
1492 bridge_pf_sysctl[bridge_ctl].val = i;
1501 bridge_pf_dump(void)
1505 for (i = 0; i < nitems(bridge_pf_sysctl); i++) {
1506 syslog(LOG_ERR, "%s%s = %d", bridge_sysctl,
1507 bridge_pf_sysctl[i].name, bridge_pf_sysctl[i].val);