xref: /freebsd/lib/libifconfig/libifconfig_bridge.c (revision 032d32c2c276983ce680ffaa6e7d6c12a9584fab)
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
ifconfig_bridge_ioctlwrap(ifconfig_handle_t * h,const char * name,unsigned long cmd,void * arg,size_t arglen,bool set)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
ifconfig_bridge_get_bridge_status(ifconfig_handle_t * h,const char * name,struct ifconfig_bridge_status ** bridgep)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, BRDGPARAM,
96 	    &bridge->params, sizeof(bridge->params), false) != 0) {
97 		goto err;
98 	}
99 
100 	for (size_t len = 8192;
101 	    (buf = realloc(members.ifbic_buf, len)) != NULL;
102 	    len *= 2) {
103 		members.ifbic_buf = buf;
104 		members.ifbic_len = len;
105 		if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFS,
106 		    &members, sizeof(members), false) != 0) {
107 			goto err;
108 		}
109 		if ((members.ifbic_len + sizeof(*members.ifbic_req)) < len)
110 			break;
111 	}
112 	if (buf == NULL) {
113 		h->error.errtype = OTHER;
114 		h->error.errcode = ENOMEM;
115 		goto err;
116 	}
117 	bridge->inner.members = members.ifbic_req;
118 	bridge->inner.members_count =
119 	    members.ifbic_len / sizeof(*members.ifbic_req);
120 
121 	bridge->inner.member_vlans = calloc(bridge->inner.members_count,
122 	    sizeof(ifbvlan_set_t));
123 	if (bridge->inner.member_vlans == NULL) {
124 		h->error.errtype = OTHER;
125 		h->error.errcode = ENOMEM;
126 		goto err;
127 	}
128 	for (size_t i = 0; i < bridge->inner.members_count; ++i) {
129 		struct ifbif_vlan_req vreq;
130 		memset(&vreq, 0, sizeof(vreq));
131 		strlcpy(vreq.bv_ifname, bridge->inner.members[i].ifbr_ifsname,
132 		    sizeof(vreq.bv_ifname));
133 
134 		if (ifconfig_bridge_ioctlwrap(h, name, BRDGGIFVLANSET, &vreq,
135 		    sizeof(vreq), false) != 0) {
136 			goto err;
137 		}
138 
139 		__BIT_COPY(BRVLAN_SETSIZE, &vreq.bv_set,
140 		    &bridge->inner.member_vlans[i]);
141 	}
142 
143 	*bridgep = &bridge->inner;
144 
145 	return (0);
146 
147 err:
148 	free(members.ifbic_buf);
149 	if (bridge)
150 		free(bridge->inner.member_vlans);
151 	free(bridge);
152 	return (-1);
153 }
154 
155 void
ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status * bridge)156 ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge)
157 {
158 	if (bridge != NULL) {
159 		free(bridge->members);
160 		free(bridge);
161 	}
162 }
163