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 * $FreeBSD$
26 */
27#include <sys/param.h>
28#include <sys/ioctl.h>
29
30#include <net/ethernet.h>
31#include <net/if.h>
32#include <net/if_bridgevar.h>
33#include <net/route.h>
34
35#include <assert.h>
36#include <errno.h>
37#include <stdbool.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include "libifconfig.h"
42#include "libifconfig_internal.h"
43
44/* Internal structure used for allocations and frees */
45struct _ifconfig_bridge_status {
46	struct ifconfig_bridge_status inner;	/* wrapped bridge status */
47	struct ifbropreq params;		/* operational parameters */
48};
49
50static int
51ifconfig_bridge_ioctlwrap(ifconfig_handle_t *h, const char *name,
52    unsigned long cmd, void *arg, size_t arglen, bool set)
53{
54	struct ifdrv ifd = { 0 };
55	unsigned long req = set ? SIOCSDRVSPEC : SIOCGDRVSPEC;
56
57	strlcpy(ifd.ifd_name, name, sizeof(ifd.ifd_name));
58	ifd.ifd_cmd = cmd;
59	ifd.ifd_data = arg;
60	ifd.ifd_len = arglen;
61
62	return (ifconfig_ioctlwrap(h, AF_LOCAL, req, &ifd));
63}
64
65int
66ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
67    const char *name, struct ifconfig_bridge_status **bridgep)
68{
69	struct ifbifconf members;
70	struct ifbrparam cache_param;
71	struct _ifconfig_bridge_status *bridge;
72	char *buf;
73
74	*bridgep = NULL;
75
76	bridge = calloc(1, sizeof(struct _ifconfig_bridge_status));
77	if (bridge == NULL) {
78		h->error.errtype = OTHER;
79		h->error.errcode = ENOMEM;
80		return (-1);
81	}
82	bridge->inner.params = &bridge->params;
83
84	if (ifconfig_bridge_ioctlwrap(h, name, BRDGGCACHE,
85	    &cache_param, sizeof(cache_param), false) != 0) {
86		free(bridge);
87		return (-1);
88	}
89	bridge->inner.cache_size = cache_param.ifbrp_csize;
90
91	if (ifconfig_bridge_ioctlwrap(h, name, BRDGGTO,
92	    &cache_param, sizeof(cache_param), false) != 0) {
93		free(bridge);
94		return (-1);
95	}
96	bridge->inner.cache_lifetime = cache_param.ifbrp_ctime;
97
98	if (ifconfig_bridge_ioctlwrap(h, name, BRDGPARAM,
99	    &bridge->params, sizeof(bridge->params), false) != 0) {
100		free(bridge);
101		return (-1);
102	}
103
104	members.ifbic_buf = NULL;
105	for (size_t len = 8192;
106	    (buf = realloc(members.ifbic_buf, len)) != NULL;
107	    len *= 2) {
108		members.ifbic_buf = buf;
109		members.ifbic_len = len;
110		if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS,
111		    &members, sizeof(members), false) != 0) {
112			free(buf);
113			free(bridge);
114			return (-1);
115		}
116		if (members.ifbic_len <= len)
117			break;
118	}
119	if (buf == NULL) {
120		free(members.ifbic_buf);
121		free(bridge);
122		h->error.errtype = OTHER;
123		h->error.errcode = ENOMEM;
124		return (-1);
125	}
126	bridge->inner.members = members.ifbic_req;
127	bridge->inner.members_count =
128	    members.ifbic_len / sizeof(*members.ifbic_req);
129
130	*bridgep = &bridge->inner;
131
132	return (0);
133}
134
135void
136ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge)
137{
138	if (bridge != NULL) {
139		free(bridge->members);
140		free(bridge);
141	}
142}
143