xref: /freebsd/lib/libifconfig/libifconfig_bridge.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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;
70 	char *buf;
71 
72 	*bridgep = NULL;
73 
74 	bridge = calloc(1, sizeof(struct _ifconfig_bridge_status));
75 	if (bridge == NULL) {
76 		h->error.errtype = OTHER;
77 		h->error.errcode = ENOMEM;
78 		return (-1);
79 	}
80 	bridge->inner.params = &bridge->params;
81 
82 	if (ifconfig_bridge_ioctlwrap(h, name, BRDGGCACHE,
83 	    &cache_param, sizeof(cache_param), false) != 0) {
84 		free(bridge);
85 		return (-1);
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 		free(bridge);
92 		return (-1);
93 	}
94 	bridge->inner.cache_lifetime = cache_param.ifbrp_ctime;
95 
96 	if (ifconfig_bridge_ioctlwrap(h, name, BRDGPARAM,
97 	    &bridge->params, sizeof(bridge->params), false) != 0) {
98 		free(bridge);
99 		return (-1);
100 	}
101 
102 	members.ifbic_buf = NULL;
103 	for (size_t len = 8192;
104 	    (buf = realloc(members.ifbic_buf, len)) != NULL;
105 	    len *= 2) {
106 		members.ifbic_buf = buf;
107 		members.ifbic_len = len;
108 		if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS,
109 		    &members, sizeof(members), false) != 0) {
110 			free(buf);
111 			free(bridge);
112 			return (-1);
113 		}
114 		if (members.ifbic_len <= len)
115 			break;
116 	}
117 	if (buf == NULL) {
118 		free(members.ifbic_buf);
119 		free(bridge);
120 		h->error.errtype = OTHER;
121 		h->error.errcode = ENOMEM;
122 		return (-1);
123 	}
124 	bridge->inner.members = members.ifbic_req;
125 	bridge->inner.members_count =
126 	    members.ifbic_len / sizeof(*members.ifbic_req);
127 
128 	*bridgep = &bridge->inner;
129 
130 	return (0);
131 }
132 
133 void
134 ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge)
135 {
136 	if (bridge != NULL) {
137 		free(bridge->members);
138 		free(bridge);
139 	}
140 }
141