1 /* 2 * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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 23 * SUCH DAMAGE. 24 */ 25 #include <sys/param.h> 26 #include <sys/ioctl.h> 27 28 #include <net/ethernet.h> 29 #include <net/if.h> 30 #include <net/if_bridgevar.h> 31 #include <net/route.h> 32 33 #include <assert.h> 34 #include <errno.h> 35 #include <stdbool.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #include "libifconfig.h" 40 #include "libifconfig_internal.h" 41 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 */ 46 }; 47 48 static int 49 ifconfig_bridge_ioctlwrap(ifconfig_handle_t *h, const char *name, 50 unsigned long cmd, void *arg, size_t arglen, bool set) 51 { 52 struct ifdrv ifd = { 0 }; 53 unsigned long req = set ? SIOCSDRVSPEC : SIOCGDRVSPEC; 54 55 strlcpy(ifd.ifd_name, name, sizeof(ifd.ifd_name)); 56 ifd.ifd_cmd = cmd; 57 ifd.ifd_data = arg; 58 ifd.ifd_len = arglen; 59 60 return (ifconfig_ioctlwrap(h, AF_LOCAL, req, &ifd)); 61 } 62 63 int 64 ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h, 65 const char *name, struct ifconfig_bridge_status **bridgep) 66 { 67 struct ifbifconf members; 68 struct ifbrparam cache_param; 69 struct _ifconfig_bridge_status *bridge = NULL; 70 char *buf = NULL; 71 72 members.ifbic_buf = NULL; 73 *bridgep = NULL; 74 75 bridge = calloc(1, sizeof(struct _ifconfig_bridge_status)); 76 if (bridge == NULL) { 77 h->error.errtype = OTHER; 78 h->error.errcode = ENOMEM; 79 goto err; 80 } 81 bridge->inner.params = &bridge->params; 82 83 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGCACHE, 84 &cache_param, sizeof(cache_param), false) != 0) { 85 goto err; 86 } 87 bridge->inner.cache_size = cache_param.ifbrp_csize; 88 89 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGTO, 90 &cache_param, sizeof(cache_param), false) != 0) { 91 goto err; 92 } 93 bridge->inner.cache_lifetime = cache_param.ifbrp_ctime; 94 95 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGFLAGS, 96 &cache_param, sizeof(cache_param), false) != 0) { 97 goto err; 98 } 99 bridge->inner.flags = cache_param.ifbrp_flags; 100 101 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGDEFPVID, 102 &cache_param, sizeof(cache_param), false) != 0) { 103 goto err; 104 } 105 bridge->inner.defpvid = cache_param.ifbrp_defpvid; 106 107 if (ifconfig_bridge_ioctlwrap(h, name, BRDGPARAM, 108 &bridge->params, sizeof(bridge->params), false) != 0) { 109 goto err; 110 } 111 112 for (size_t len = 8192; 113 (buf = realloc(members.ifbic_buf, len)) != NULL; 114 len *= 2) { 115 members.ifbic_buf = buf; 116 members.ifbic_len = len; 117 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS, 118 &members, sizeof(members), false) != 0) { 119 goto err; 120 } 121 if ((members.ifbic_len + sizeof(*members.ifbic_req)) < len) 122 break; 123 } 124 if (buf == NULL) { 125 h->error.errtype = OTHER; 126 h->error.errcode = ENOMEM; 127 goto err; 128 } 129 bridge->inner.members = members.ifbic_req; 130 bridge->inner.members_count = 131 members.ifbic_len / sizeof(*members.ifbic_req); 132 133 bridge->inner.member_vlans = calloc(bridge->inner.members_count, 134 sizeof(ifbvlan_set_t)); 135 if (bridge->inner.member_vlans == NULL) { 136 h->error.errtype = OTHER; 137 h->error.errcode = ENOMEM; 138 goto err; 139 } 140 for (size_t i = 0; i < bridge->inner.members_count; ++i) { 141 struct ifbif_vlan_req vreq; 142 memset(&vreq, 0, sizeof(vreq)); 143 strlcpy(vreq.bv_ifname, bridge->inner.members[i].ifbr_ifsname, 144 sizeof(vreq.bv_ifname)); 145 146 if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFVLANSET, &vreq, 147 sizeof(vreq), false) != 0) { 148 goto err; 149 } 150 151 __BIT_COPY(BRVLAN_SETSIZE, &vreq.bv_set, 152 &bridge->inner.member_vlans[i]); 153 } 154 155 *bridgep = &bridge->inner; 156 157 return (0); 158 159 err: 160 free(members.ifbic_buf); 161 if (bridge) 162 free(bridge->inner.member_vlans); 163 free(bridge); 164 return (-1); 165 } 166 167 void 168 ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge) 169 { 170 if (bridge != NULL) { 171 free(bridge->members); 172 free(bridge); 173 } 174 } 175