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 #include "bridge_tree.h"
62 #include "bridge_snmp.h"
67 bridge_ioctl_init(void)
69 if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
70 syslog(LOG_ERR, "cannot open socket : %s", strerror(errno));
78 * Load the if_bridge.ko module in kernel if not already there.
81 bridge_kmod_load(void)
84 const char mod_name[] = "if_bridge";
85 struct module_stat mstat;
87 /* Scan files in kernel. */
88 mstat.version = sizeof(struct module_stat);
89 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
90 /* Scan modules in file. */
91 for (modid = kldfirstmod(fileid); modid > 0;
92 modid = modfnext(modid)) {
94 if (modstat(modid, &mstat) < 0)
97 if (strcmp(mod_name, mstat.name) == 0)
102 /* Not present - load it. */
103 if (kldload(mod_name) < 0) {
104 syslog(LOG_ERR, "failed to load %s kernel module", mod_name);
111 /************************************************************************
116 * Convert the kernel uint64_t value for a bridge id
119 snmp_uint64_to_bridgeid(uint64_t id, bridge_id b_id)
126 for (i = 0; i < SNMP_BRIDGE_ID_LEN; i++, o++)
127 b_id[SNMP_BRIDGE_ID_LEN - i - 1] = *o;
131 * Fetch the bridge configuration parameters from the kernel excluding
132 * it's base MAC address.
135 bridge_get_conf_param(struct bridge_if *bif)
138 struct ifbrparam b_param;
140 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
141 ifd.ifd_len = sizeof(b_param);
142 ifd.ifd_data = &b_param;
144 /* Bridge priority. */
145 ifd.ifd_cmd = BRDGGPRI;
146 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
147 syslog(LOG_ERR, "update bridge: ioctl(BRDGGPRI) failed: %s",
152 bif->priority = b_param.ifbrp_prio;
154 /* Configured max age. */
155 ifd.ifd_cmd = BRDGGMA;
156 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
157 syslog(LOG_ERR, "update bridge: ioctl(BRDGGMA) failed: %s",
163 bif->bridge_max_age = 100 * b_param.ifbrp_maxage;
165 /* Configured hello time. */
166 ifd.ifd_cmd = BRDGGHT;
167 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
168 syslog(LOG_ERR, "update bridge: ioctl(BRDGGHT) failed: %s",
172 bif->bridge_hello_time = 100 * b_param.ifbrp_hellotime;
175 ifd.ifd_cmd = BRDGGFD;
176 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
177 syslog(LOG_ERR, "update bridge: ioctl(BRDGGFD) failed: %s",
181 bif->bridge_fwd_delay = 100 * b_param.ifbrp_fwddelay;
183 /* Number of dropped addresses. */
184 ifd.ifd_cmd = BRDGGRTE;
185 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
186 syslog(LOG_ERR, "update bridge: ioctl(BRDGGRTE) failed: %s",
190 bif->lrnt_drops = b_param.ifbrp_cexceeded;
192 /* Address table timeout. */
193 ifd.ifd_cmd = BRDGGTO;
194 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
195 syslog(LOG_ERR, "update bridge: ioctl(BRDGGTO) failed: %s",
199 bif->age_time = b_param.ifbrp_ctime;
201 /* Address table size. */
202 ifd.ifd_cmd = BRDGGCACHE;
203 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
204 syslog(LOG_ERR, "update bridge: ioctl(BRDGGCACHE) "
205 "failed: %s", strerror(errno));
208 bif->max_addrs = b_param.ifbrp_csize;
214 * Fetch the current bridge STP operational parameters.
215 * Returns: -1 - on error;
216 * 0 - old TC time and Root Port values are same;
217 * 1 - topologyChange notification should be sent;
218 * 2 - newRoot notification should be sent.
221 bridge_get_op_param(struct bridge_if *bif)
225 struct ifbropreq b_req;
227 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
228 ifd.ifd_len = sizeof(b_req);
229 ifd.ifd_data = &b_req;
230 ifd.ifd_cmd = BRDGPARAM;
232 if (ioctl(sock, SIOCGDRVSPEC, &ifd) < 0) {
233 syslog(LOG_ERR, "update bridge: ioctl(BRDGPARAM) failed: %s",
238 bif->max_age = 100 * b_req.ifbop_maxage;
239 bif->hello_time = 100 * b_req.ifbop_hellotime;
240 bif->fwd_delay = 100 * b_req.ifbop_fwddelay;
241 bif->stp_version = b_req.ifbop_protocol;
242 bif->tx_hold_count = b_req.ifbop_holdcount;
244 if (b_req.ifbop_root_port == 0 &&
245 bif->root_port != b_req.ifbop_root_port)
250 bif->root_port = b_req.ifbop_root_port;
251 bif->root_cost = b_req.ifbop_root_path_cost;
252 snmp_uint64_to_bridgeid(b_req.ifbop_designated_root,
255 if (bif->last_tc_time.tv_sec != b_req.ifbop_last_tc_time.tv_sec) {
257 bif->last_tc_time.tv_sec = b_req.ifbop_last_tc_time.tv_sec;
258 bif->last_tc_time.tv_usec = b_req.ifbop_last_tc_time.tv_usec;
261 * "The trap is not sent if a (begemotBridge)NewRoot
262 * trap is sent for the same transition."
264 if (new_root_send == 0)
268 return (new_root_send);
272 bridge_getinfo_bif(struct bridge_if *bif)
274 if (bridge_get_conf_param(bif) < 0)
277 return (bridge_get_op_param(bif));
281 bridge_set_priority(struct bridge_if *bif, int32_t priority)
284 struct ifbrparam b_param;
286 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
287 ifd.ifd_len = sizeof(b_param);
288 ifd.ifd_data = &b_param;
289 b_param.ifbrp_prio = (uint32_t) priority;
290 ifd.ifd_cmd = BRDGSPRI;
292 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
293 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSPRI) "
294 "failed: %s", strerror(errno));
299 * Re-fetching the data from the driver after that might be a good
300 * idea, since changing our bridge's priority should invoke
301 * recalculation of the active spanning tree topology in the network.
303 bif->priority = priority;
308 * Convert 1/100 of seconds to 1/256 of seconds.
309 * Timeout ::= TEXTUAL-CONVENTION.
310 * To convert a Timeout value into a value in units of
311 * 1/256 seconds, the following algorithm should be used:
312 * b = floor( (n * 256) / 100)
313 * The conversion to 1/256 of a second happens in the kernel -
314 * just make sure we correctly convert the seconds to Timout
318 snmp_timeout2_sec(int32_t secs)
324 bridge_set_maxage(struct bridge_if *bif, int32_t max_age)
327 struct ifbrparam b_param;
329 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
330 ifd.ifd_len = sizeof(b_param);
331 ifd.ifd_data = &b_param;
332 b_param.ifbrp_maxage = snmp_timeout2_sec(max_age);
333 ifd.ifd_cmd = BRDGSMA;
335 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
336 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSMA) "
337 "failed: %s", strerror(errno));
341 bif->bridge_max_age = max_age;
346 bridge_set_hello_time(struct bridge_if *bif, int32_t hello_time)
349 struct ifbrparam b_param;
351 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
352 ifd.ifd_len = sizeof(b_param);
353 ifd.ifd_data = &b_param;
354 b_param.ifbrp_hellotime = snmp_timeout2_sec(hello_time);
355 ifd.ifd_cmd = BRDGSHT;
357 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
358 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSHT) "
359 "failed: %s", strerror(errno));
363 bif->bridge_hello_time = b_param.ifbrp_hellotime;
368 bridge_set_forward_delay(struct bridge_if *bif, int32_t fwd_delay)
371 struct ifbrparam b_param;
373 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
374 ifd.ifd_len = sizeof(b_param);
375 ifd.ifd_data = &b_param;
376 b_param.ifbrp_fwddelay = snmp_timeout2_sec(fwd_delay);
377 ifd.ifd_cmd = BRDGSFD;
379 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
380 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSFD) "
381 "failed: %s", strerror(errno));
385 bif->bridge_fwd_delay = b_param.ifbrp_fwddelay;
390 bridge_set_aging_time(struct bridge_if *bif, int32_t age_time)
393 struct ifbrparam b_param;
395 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
396 ifd.ifd_len = sizeof(b_param);
397 ifd.ifd_data = &b_param;
398 b_param.ifbrp_ctime = (uint32_t) age_time;
399 ifd.ifd_cmd = BRDGSTO;
401 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
402 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSTO) "
403 "failed: %s", strerror(errno));
407 bif->age_time = age_time;
412 bridge_set_max_cache(struct bridge_if *bif, int32_t max_cache)
415 struct ifbrparam b_param;
417 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
418 ifd.ifd_len = sizeof(b_param);
419 ifd.ifd_data = &b_param;
420 b_param.ifbrp_csize = max_cache;
421 ifd.ifd_cmd = BRDGSCACHE;
423 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
424 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSCACHE) "
425 "failed: %s", strerror(errno));
429 bif->max_addrs = b_param.ifbrp_csize;
434 bridge_set_tx_hold_count(struct bridge_if *bif, int32_t tx_hc)
437 struct ifbrparam b_param;
439 if (tx_hc < SNMP_BRIDGE_MIN_TXHC || tx_hc > SNMP_BRIDGE_MAX_TXHC)
442 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
443 ifd.ifd_len = sizeof(b_param);
444 ifd.ifd_data = &b_param;
445 b_param.ifbrp_txhc = tx_hc;
446 ifd.ifd_cmd = BRDGSTXHC;
448 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
449 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSTXHC) "
450 "failed: %s", strerror(errno));
454 bif->tx_hold_count = b_param.ifbrp_txhc;
459 bridge_set_stp_version(struct bridge_if *bif, int32_t stp_proto)
462 struct ifbrparam b_param;
464 strlcpy(ifd.ifd_name, bif->bif_name, IFNAMSIZ);
465 ifd.ifd_len = sizeof(b_param);
466 ifd.ifd_data = &b_param;
467 b_param.ifbrp_proto = stp_proto;
468 ifd.ifd_cmd = BRDGSPROTO;
470 if (ioctl(sock, SIOCSDRVSPEC, &ifd) < 0) {
471 syslog(LOG_ERR, "set bridge param: ioctl(BRDGSPROTO) "
472 "failed: %s", strerror(errno));
476 bif->stp_version = b_param.ifbrp_proto;
481 * Set the bridge interface status to up/down.
484 bridge_set_if_up(const char* b_name, int8_t up)
489 bzero(&ifr, sizeof(ifr));
490 strlcpy(ifr.ifr_name, b_name, sizeof(ifr.ifr_name));
491 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
492 syslog(LOG_ERR, "set bridge up: ioctl(SIOCGIFFLAGS) "
493 "failed: %s", strerror(errno));
497 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
503 ifr.ifr_flags = flags & 0xffff;
504 ifr.ifr_flagshigh = flags >> 16;
505 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
506 syslog(LOG_ERR, "set bridge up: ioctl(SIOCSIFFLAGS) "
507 "failed: %s", strerror(errno));
515 bridge_create(const char *b_name)
520 bzero(&ifr, sizeof(ifr));
521 strlcpy(ifr.ifr_name, b_name, sizeof(ifr.ifr_name));
523 if (ioctl(sock, SIOCIFCREATE, &ifr) < 0) {
524 syslog(LOG_ERR, "create bridge: ioctl(SIOCIFCREATE) "
525 "failed: %s", strerror(errno));
529 if (strcmp(b_name, ifr.ifr_name) == 0)
532 if ((new_name = strdup(b_name)) == NULL) {
533 syslog(LOG_ERR, "create bridge: strdup() failed");
537 ifr.ifr_data = new_name;
538 if (ioctl(sock, SIOCSIFNAME, (caddr_t) &ifr) < 0) {
539 syslog(LOG_ERR, "create bridge: ioctl(SIOCSIFNAME) "
540 "failed: %s", strerror(errno));
549 bridge_destroy(const char *b_name)
553 bzero(&ifr, sizeof(ifr));
554 strlcpy(ifr.ifr_name, b_name, sizeof(ifr.ifr_name));
556 if (ioctl(sock, SIOCIFDESTROY, &ifr) < 0) {
557 syslog(LOG_ERR, "destroy bridge: ioctl(SIOCIFDESTROY) "
558 "failed: %s", strerror(errno));
566 * Fetch the bridge base MAC address. Return pointer to the
567 * buffer containing the MAC address, NULL on failure.
570 bridge_get_basemac(const char *bif_name, u_char *mac, size_t mlen)
573 char if_name[IFNAMSIZ];
574 struct ifaddrs *ifap, *ifa;
575 struct sockaddr_dl sdl;
577 if (getifaddrs(&ifap) != 0) {
578 syslog(LOG_ERR, "bridge get mac: getifaddrs() failed - %s",
583 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
584 if (ifa->ifa_addr->sa_family != AF_LINK)
588 * 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);