1e5539fb6SRyan Moeller /* 2e5539fb6SRyan Moeller * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org> 3e5539fb6SRyan Moeller * 4e5539fb6SRyan Moeller * Redistribution and use in source and binary forms, with or without 5e5539fb6SRyan Moeller * modification, are permitted provided that the following conditions 6e5539fb6SRyan Moeller * are met: 7e5539fb6SRyan Moeller * 1. Redistributions of source code must retain the above copyright 8e5539fb6SRyan Moeller * notice, this list of conditions and the following disclaimer. 9e5539fb6SRyan Moeller * 2. Redistributions in binary form must reproduce the above copyright 10e5539fb6SRyan Moeller * notice, this list of conditions and the following disclaimer in the 11e5539fb6SRyan Moeller * documentation and/or other materials provided with the distribution. 12e5539fb6SRyan Moeller * 13e5539fb6SRyan Moeller * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14e5539fb6SRyan Moeller * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15e5539fb6SRyan Moeller * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16e5539fb6SRyan Moeller * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17e5539fb6SRyan Moeller * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18e5539fb6SRyan Moeller * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19e5539fb6SRyan Moeller * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20e5539fb6SRyan Moeller * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21e5539fb6SRyan Moeller * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22e5539fb6SRyan Moeller * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23e5539fb6SRyan Moeller * SUCH DAMAGE. 24e5539fb6SRyan Moeller */ 25e5539fb6SRyan Moeller #include <sys/param.h> 26e5539fb6SRyan Moeller #include <sys/ioctl.h> 27e5539fb6SRyan Moeller 28e5539fb6SRyan Moeller #include <net/ethernet.h> 29e5539fb6SRyan Moeller #include <net/if.h> 30e5539fb6SRyan Moeller #include <net/if_bridgevar.h> 31e5539fb6SRyan Moeller #include <net/route.h> 32e5539fb6SRyan Moeller 33e5539fb6SRyan Moeller #include <assert.h> 34e5539fb6SRyan Moeller #include <errno.h> 35e5539fb6SRyan Moeller #include <stdbool.h> 36e5539fb6SRyan Moeller #include <stdlib.h> 37e5539fb6SRyan Moeller #include <string.h> 38e5539fb6SRyan Moeller 39e5539fb6SRyan Moeller #include "libifconfig.h" 40e5539fb6SRyan Moeller #include "libifconfig_internal.h" 41e5539fb6SRyan Moeller 42e5539fb6SRyan Moeller /* Internal structure used for allocations and frees */ 43e5539fb6SRyan Moeller struct _ifconfig_bridge_status { 44e5539fb6SRyan Moeller struct ifconfig_bridge_status inner; /* wrapped bridge status */ 45e5539fb6SRyan Moeller struct ifbropreq params; /* operational parameters */ 46e5539fb6SRyan Moeller }; 47e5539fb6SRyan Moeller 48e5539fb6SRyan Moeller static int 49e5539fb6SRyan Moeller ifconfig_bridge_ioctlwrap(ifconfig_handle_t *h, const char *name, 50e5539fb6SRyan Moeller unsigned long cmd, void *arg, size_t arglen, bool set) 51e5539fb6SRyan Moeller { 52e5539fb6SRyan Moeller struct ifdrv ifd = { 0 }; 53e5539fb6SRyan Moeller unsigned long req = set ? SIOCSDRVSPEC : SIOCGDRVSPEC; 54e5539fb6SRyan Moeller 55e5539fb6SRyan Moeller strlcpy(ifd.ifd_name, name, sizeof(ifd.ifd_name)); 56e5539fb6SRyan Moeller ifd.ifd_cmd = cmd; 57e5539fb6SRyan Moeller ifd.ifd_data = arg; 58e5539fb6SRyan Moeller ifd.ifd_len = arglen; 59e5539fb6SRyan Moeller 60e5539fb6SRyan Moeller return (ifconfig_ioctlwrap(h, AF_LOCAL, req, &ifd)); 61e5539fb6SRyan Moeller } 62e5539fb6SRyan Moeller 63e5539fb6SRyan Moeller int 64e5539fb6SRyan Moeller ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h, 65e5539fb6SRyan Moeller const char *name, struct ifconfig_bridge_status **bridgep) 66e5539fb6SRyan Moeller { 67e5539fb6SRyan Moeller struct ifbifconf members; 68e5539fb6SRyan Moeller struct ifbrparam cache_param; 69e5539fb6SRyan Moeller struct _ifconfig_bridge_status *bridge; 70e5539fb6SRyan Moeller char *buf; 71e5539fb6SRyan Moeller 72e5539fb6SRyan Moeller *bridgep = NULL; 73e5539fb6SRyan Moeller 74e5539fb6SRyan Moeller bridge = calloc(1, sizeof(struct _ifconfig_bridge_status)); 75e5539fb6SRyan Moeller if (bridge == NULL) { 76e5539fb6SRyan Moeller h->error.errtype = OTHER; 77e5539fb6SRyan Moeller h->error.errcode = ENOMEM; 78e5539fb6SRyan Moeller return (-1); 79e5539fb6SRyan Moeller } 80e5539fb6SRyan Moeller bridge->inner.params = &bridge->params; 81e5539fb6SRyan Moeller 82e5539fb6SRyan Moeller if (ifconfig_bridge_ioctlwrap(h, name, BRDGGCACHE, 83e5539fb6SRyan Moeller &cache_param, sizeof(cache_param), false) != 0) { 84e5539fb6SRyan Moeller free(bridge); 85e5539fb6SRyan Moeller return (-1); 86e5539fb6SRyan Moeller } 87e5539fb6SRyan Moeller bridge->inner.cache_size = cache_param.ifbrp_csize; 88e5539fb6SRyan Moeller 89e5539fb6SRyan Moeller if (ifconfig_bridge_ioctlwrap(h, name, BRDGGTO, 90e5539fb6SRyan Moeller &cache_param, sizeof(cache_param), false) != 0) { 91e5539fb6SRyan Moeller free(bridge); 92e5539fb6SRyan Moeller return (-1); 93e5539fb6SRyan Moeller } 94e5539fb6SRyan Moeller bridge->inner.cache_lifetime = cache_param.ifbrp_ctime; 95e5539fb6SRyan Moeller 96e5539fb6SRyan Moeller if (ifconfig_bridge_ioctlwrap(h, name, BRDGPARAM, 97e5539fb6SRyan Moeller &bridge->params, sizeof(bridge->params), false) != 0) { 98e5539fb6SRyan Moeller free(bridge); 99e5539fb6SRyan Moeller return (-1); 100e5539fb6SRyan Moeller } 101e5539fb6SRyan Moeller 102e5539fb6SRyan Moeller members.ifbic_buf = NULL; 103e5539fb6SRyan Moeller for (size_t len = 8192; 104e5539fb6SRyan Moeller (buf = realloc(members.ifbic_buf, len)) != NULL; 105e5539fb6SRyan Moeller len *= 2) { 106e5539fb6SRyan Moeller members.ifbic_buf = buf; 107e5539fb6SRyan Moeller members.ifbic_len = len; 108e5539fb6SRyan Moeller if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS, 109e5539fb6SRyan Moeller &members, sizeof(members), false) != 0) { 110e5539fb6SRyan Moeller free(buf); 111e5539fb6SRyan Moeller free(bridge); 112e5539fb6SRyan Moeller return (-1); 113e5539fb6SRyan Moeller } 114*3d36053cSMichael Gmelin if ((members.ifbic_len + sizeof(*members.ifbic_req)) < len) 115e5539fb6SRyan Moeller break; 116e5539fb6SRyan Moeller } 117e5539fb6SRyan Moeller if (buf == NULL) { 118e5539fb6SRyan Moeller free(members.ifbic_buf); 119e5539fb6SRyan Moeller free(bridge); 120e5539fb6SRyan Moeller h->error.errtype = OTHER; 121e5539fb6SRyan Moeller h->error.errcode = ENOMEM; 122e5539fb6SRyan Moeller return (-1); 123e5539fb6SRyan Moeller } 124e5539fb6SRyan Moeller bridge->inner.members = members.ifbic_req; 125e5539fb6SRyan Moeller bridge->inner.members_count = 126e5539fb6SRyan Moeller members.ifbic_len / sizeof(*members.ifbic_req); 127e5539fb6SRyan Moeller 128e5539fb6SRyan Moeller *bridgep = &bridge->inner; 129e5539fb6SRyan Moeller 130e5539fb6SRyan Moeller return (0); 131e5539fb6SRyan Moeller } 132e5539fb6SRyan Moeller 133e5539fb6SRyan Moeller void 134e5539fb6SRyan Moeller ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge) 135e5539fb6SRyan Moeller { 136e5539fb6SRyan Moeller if (bridge != NULL) { 137e5539fb6SRyan Moeller free(bridge->members); 138e5539fb6SRyan Moeller free(bridge); 139e5539fb6SRyan Moeller } 140e5539fb6SRyan Moeller } 141