2 * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 #include <sys/param.h>
26 #include <sys/ioctl.h>
28 #include <net/ethernet.h>
30 #include <net/if_bridgevar.h>
31 #include <net/route.h>
39 #include "libifconfig.h"
40 #include "libifconfig_internal.h"
42 /* Internal structure used for allocations and frees */
43 struct _ifconfig_bridge_status {
44 struct ifconfig_bridge_status inner; /* wrapped bridge status */
45 struct ifbropreq params; /* operational parameters */
49 ifconfig_bridge_ioctlwrap(ifconfig_handle_t *h, const char *name,
50 unsigned long cmd, void *arg, size_t arglen, bool set)
52 struct ifdrv ifd = { 0 };
53 unsigned long req = set ? SIOCSDRVSPEC : SIOCGDRVSPEC;
55 strlcpy(ifd.ifd_name, name, sizeof(ifd.ifd_name));
60 return (ifconfig_ioctlwrap(h, AF_LOCAL, req, &ifd));
64 ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
65 const char *name, struct ifconfig_bridge_status **bridgep)
67 struct ifbifconf members;
68 struct ifbrparam cache_param;
69 struct _ifconfig_bridge_status *bridge;
74 bridge = calloc(1, sizeof(struct _ifconfig_bridge_status));
76 h->error.errtype = OTHER;
77 h->error.errcode = ENOMEM;
80 bridge->inner.params = &bridge->params;
82 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGCACHE,
83 &cache_param, sizeof(cache_param), false) != 0) {
87 bridge->inner.cache_size = cache_param.ifbrp_csize;
89 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGTO,
90 &cache_param, sizeof(cache_param), false) != 0) {
94 bridge->inner.cache_lifetime = cache_param.ifbrp_ctime;
96 if (ifconfig_bridge_ioctlwrap(h, name, BRDGPARAM,
97 &bridge->params, sizeof(bridge->params), false) != 0) {
102 members.ifbic_buf = NULL;
103 for (size_t len = 8192;
104 (buf = realloc(members.ifbic_buf, len)) != NULL;
106 members.ifbic_buf = buf;
107 members.ifbic_len = len;
108 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS,
109 &members, sizeof(members), false) != 0) {
114 if (members.ifbic_len <= len)
118 free(members.ifbic_buf);
120 h->error.errtype = OTHER;
121 h->error.errcode = ENOMEM;
124 bridge->inner.members = members.ifbic_req;
125 bridge->inner.members_count =
126 members.ifbic_len / sizeof(*members.ifbic_req);
128 *bridgep = &bridge->inner;
134 ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge)
136 if (bridge != NULL) {
137 free(bridge->members);