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
27 #include <sys/param.h>
28 #include <sys/ioctl.h>
30 #include <net/ethernet.h>
32 #include <net/if_bridgevar.h>
33 #include <net/route.h>
41 #include "libifconfig.h"
42 #include "libifconfig_internal.h"
44 /* Internal structure used for allocations and frees */
45 struct _ifconfig_bridge_status {
46 struct ifconfig_bridge_status inner; /* wrapped bridge status */
47 struct ifbropreq params; /* operational parameters */
51 ifconfig_bridge_ioctlwrap(ifconfig_handle_t *h, const char *name,
52 unsigned long cmd, void *arg, size_t arglen, bool set)
54 struct ifdrv ifd = { 0 };
55 unsigned long req = set ? SIOCSDRVSPEC : SIOCGDRVSPEC;
57 strlcpy(ifd.ifd_name, name, sizeof(ifd.ifd_name));
62 return (ifconfig_ioctlwrap(h, AF_LOCAL, req, &ifd));
66 ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
67 const char *name, struct ifconfig_bridge_status **bridgep)
69 struct ifbifconf members;
70 struct ifbrparam cache_param;
71 struct _ifconfig_bridge_status *bridge;
76 bridge = calloc(1, sizeof(struct _ifconfig_bridge_status));
78 h->error.errtype = OTHER;
79 h->error.errcode = ENOMEM;
82 bridge->inner.params = &bridge->params;
84 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGCACHE,
85 &cache_param, sizeof(cache_param), false) != 0) {
89 bridge->inner.cache_size = cache_param.ifbrp_csize;
91 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGTO,
92 &cache_param, sizeof(cache_param), false) != 0) {
96 bridge->inner.cache_lifetime = cache_param.ifbrp_ctime;
98 if (ifconfig_bridge_ioctlwrap(h, name, BRDGPARAM,
99 &bridge->params, sizeof(bridge->params), false) != 0) {
104 members.ifbic_buf = NULL;
105 for (size_t len = 8192;
106 (buf = realloc(members.ifbic_buf, len)) != NULL;
108 members.ifbic_buf = buf;
109 members.ifbic_len = len;
110 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS,
111 &members, sizeof(members), false) != 0) {
116 if (members.ifbic_len <= len)
120 free(members.ifbic_buf);
122 h->error.errtype = OTHER;
123 h->error.errcode = ENOMEM;
126 bridge->inner.members = members.ifbic_req;
127 bridge->inner.members_count =
128 members.ifbic_len / sizeof(*members.ifbic_req);
130 *bridgep = &bridge->inner;
136 ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge)
138 if (bridge != NULL) {
139 free(bridge->members);