xref: /f-stack/tools/ifconfig/ifvlan.c (revision d4a07e70)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1999 Bill Paul <[email protected]>
5  * Copyright (c) 2012 ADARA Networks, Inc.
6  * All rights reserved.
7   *
8  * Portions of this software were developed by Robert N. M. Watson under
9  * contract to ADARA Networks, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Bill Paul.
22  * 4. Neither the name of the author nor the names of any co-contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36  * THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 
44 #include <stdlib.h>
45 #include <unistd.h>
46 
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_vlan_var.h>
50 #include <net/route.h>
51 
52 #include <ctype.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <err.h>
58 #include <errno.h>
59 
60 #include "ifconfig.h"
61 
62 #ifndef lint
63 static const char rcsid[] =
64   "$FreeBSD$";
65 #endif
66 
67 #define	NOTAG	((u_short) -1)
68 
69 static const char proto_8021Q[]  = "802.1q";
70 static const char proto_8021ad[] = "802.1ad";
71 static const char proto_qinq[] = "qinq";
72 
73 static 	struct vlanreq params = {
74 	.vlr_tag	= NOTAG,
75 	.vlr_proto	= ETHERTYPE_VLAN,
76 };
77 
78 static int
getvlan(int s,struct ifreq * ifr,struct vlanreq * vreq)79 getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq)
80 {
81 	bzero((char *)vreq, sizeof(*vreq));
82 	ifr->ifr_data = (caddr_t)vreq;
83 
84 #ifndef FSTACK
85 	return ioctl(s, SIOCGETVLAN, (caddr_t)ifr);
86 #else
87 	size_t offset = (char *)&(ifr->ifr_data) - (char *)ifr;
88 	size_t clen = sizeof(*vreq);
89 	return ioctl_va(s, SIOCGETVLAN, (caddr_t)ifr, 3, offset, ifr->ifr_data, clen);
90 #endif
91 }
92 
93 static void
vlan_status(int s)94 vlan_status(int s)
95 {
96 	struct vlanreq		vreq;
97 
98 	if (getvlan(s, &ifr, &vreq) == -1)
99 		return;
100 	printf("\tvlan: %d", vreq.vlr_tag);
101 	printf(" vlanproto: ");
102 	switch (vreq.vlr_proto) {
103 		case ETHERTYPE_VLAN:
104 			printf(proto_8021Q);
105 			break;
106 		case ETHERTYPE_QINQ:
107 			printf(proto_8021ad);
108 			break;
109 		default:
110 			printf("0x%04x", vreq.vlr_proto);
111 	}
112 	if (ioctl(s, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
113 		printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
114 	printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
115 	    "<none>" : vreq.vlr_parent);
116 	printf("\n");
117 }
118 
119 static int
vlan_match_ethervid(const char * name)120 vlan_match_ethervid(const char *name)
121 {
122 	return (strchr(name, '.') != NULL);
123 }
124 
125 static void
vlan_parse_ethervid(const char * name)126 vlan_parse_ethervid(const char *name)
127 {
128 	char ifname[IFNAMSIZ];
129 	char *cp;
130 	int vid;
131 
132 	strlcpy(ifname, name, IFNAMSIZ);
133 	if ((cp = strrchr(ifname, '.')) == NULL)
134 		return;
135 	/*
136 	 * Don't mix vlan/vlandev parameters with dot notation.
137 	 */
138 	if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0')
139 		errx(1, "ambiguous vlan specification");
140 	/*
141 	 * Derive params from interface name: "parent.vid".
142 	 */
143 	*cp++ = '\0';
144 	if ((*cp < '1') || (*cp > '9'))
145 		errx(1, "invalid vlan tag");
146 
147 	vid = *cp++ - '0';
148 	while ((*cp >= '0') && (*cp <= '9'))
149 		vid = (vid * 10) + (*cp++ - '0');
150 	if ((*cp != '\0') || (vid & ~0xFFF))
151 		errx(1, "invalid vlan tag");
152 
153 	strlcpy(params.vlr_parent, ifname, IFNAMSIZ);
154 	params.vlr_tag = (vid & 0xFFF);
155 }
156 
157 static void
vlan_create(int s,struct ifreq * ifr)158 vlan_create(int s, struct ifreq *ifr)
159 {
160 	vlan_parse_ethervid(ifr->ifr_name);
161 
162 	if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
163 		/*
164 		 * One or both parameters were specified, make sure both.
165 		 */
166 		if (params.vlr_tag == NOTAG)
167 			errx(1, "must specify a tag for vlan create");
168 		if (params.vlr_parent[0] == '\0')
169 			errx(1, "must specify a parent device for vlan create");
170 		ifr->ifr_data = (caddr_t) &params;
171 #ifdef FSTACK
172 		size_t offset = (char *)&(ifr->ifr_data) - (char *)ifr;
173 		size_t clen = sizeof(params);
174 		if (ioctl_va(s, SIOCIFCREATE2, ifr, 3, offset, ifr->ifr_data, clen) < 0)
175 			err(1, "SIOCIFCREATE2");
176 		return;
177 #endif
178 	}
179 	ioctl_ifcreate(s, ifr);
180 }
181 
182 static void
vlan_cb(int s,void * arg)183 vlan_cb(int s, void *arg)
184 {
185 	if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
186 		errx(1, "both vlan and vlandev must be specified");
187 }
188 
189 static void
vlan_set(int s,struct ifreq * ifr)190 vlan_set(int s, struct ifreq *ifr)
191 {
192 	if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
193 		ifr->ifr_data = (caddr_t) &params;
194 #ifndef FSTACK
195 		if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
196 #else
197 		size_t offset = (char *)&(ifr->ifr_data) - (char *)ifr;
198 		size_t clen = sizeof(params);
199 		if (ioctl_va(s, SIOCSETVLAN, ifr, 3, offset, ifr->ifr_data, clen) == -1)
200 #endif
201 			err(1, "SIOCSETVLAN");
202 	}
203 }
204 
205 static
DECL_CMD_FUNC(setvlantag,val,d)206 DECL_CMD_FUNC(setvlantag, val, d)
207 {
208 	struct vlanreq vreq;
209 	u_long ul;
210 	char *endp;
211 
212 	ul = strtoul(val, &endp, 0);
213 	if (*endp != '\0')
214 		errx(1, "invalid value for vlan");
215 	params.vlr_tag = ul;
216 	/* check if the value can be represented in vlr_tag */
217 	if (params.vlr_tag != ul)
218 		errx(1, "value for vlan out of range");
219 
220 	if (getvlan(s, &ifr, &vreq) != -1)
221 		vlan_set(s, &ifr);
222 }
223 
224 static
DECL_CMD_FUNC(setvlandev,val,d)225 DECL_CMD_FUNC(setvlandev, val, d)
226 {
227 	struct vlanreq vreq;
228 
229 	strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
230 
231 	if (getvlan(s, &ifr, &vreq) != -1)
232 		vlan_set(s, &ifr);
233 }
234 
235 static
DECL_CMD_FUNC(setvlanproto,val,d)236 DECL_CMD_FUNC(setvlanproto, val, d)
237 {
238 	struct vlanreq vreq;
239 
240 	if (strncasecmp(proto_8021Q, val,
241 	    strlen(proto_8021Q)) == 0) {
242 		params.vlr_proto = ETHERTYPE_VLAN;
243 	} else if ((strncasecmp(proto_8021ad, val, strlen(proto_8021ad)) == 0)
244 	    || (strncasecmp(proto_qinq, val, strlen(proto_qinq)) == 0)) {
245 		params.vlr_proto = ETHERTYPE_QINQ;
246 	} else
247 		errx(1, "invalid value for vlanproto");
248 
249 	if (getvlan(s, &ifr, &vreq) != -1)
250 		vlan_set(s, &ifr);
251 }
252 
253 static
DECL_CMD_FUNC(setvlanpcp,val,d)254 DECL_CMD_FUNC(setvlanpcp, val, d)
255 {
256 	u_long ul;
257 	char *endp;
258 
259 	ul = strtoul(val, &endp, 0);
260 	if (*endp != '\0')
261 		errx(1, "invalid value for vlanpcp");
262 	if (ul > 7)
263 		errx(1, "value for vlanpcp out of range");
264 	ifr.ifr_vlan_pcp = ul;
265 	if (ioctl(s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
266 		err(1, "SIOCSVLANPCP");
267 }
268 
269 static
DECL_CMD_FUNC(unsetvlandev,val,d)270 DECL_CMD_FUNC(unsetvlandev, val, d)
271 {
272 	struct vlanreq		vreq;
273 
274 	bzero((char *)&vreq, sizeof(struct vlanreq));
275 	ifr.ifr_data = (caddr_t)&vreq;
276 
277 #ifndef FSTACK
278 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
279 #else
280 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
281 	size_t clen = sizeof(vreq);
282 	if (ioctl_va(s, SIOCGETVLAN, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1)
283 #endif
284 		err(1, "SIOCGETVLAN");
285 
286 	bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
287 	vreq.vlr_tag = 0;
288 
289 #ifndef FSTACK
290 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
291 #else
292 	if (ioctl_va(s, SIOCSETVLAN, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1)
293 #endif
294 		err(1, "SIOCSETVLAN");
295 }
296 
297 static struct cmd vlan_cmds[] = {
298 	DEF_CLONE_CMD_ARG("vlan",			setvlantag),
299 	DEF_CLONE_CMD_ARG("vlandev",			setvlandev),
300 	DEF_CLONE_CMD_ARG("vlanproto",			setvlanproto),
301 	DEF_CMD_ARG("vlanpcp",				setvlanpcp),
302 	/* NB: non-clone cmds */
303 	DEF_CMD_ARG("vlan",				setvlantag),
304 	DEF_CMD_ARG("vlandev",				setvlandev),
305 	DEF_CMD_ARG("vlanproto",			setvlanproto),
306 	/* XXX For compatibility.  Should become DEF_CMD() some day. */
307 	DEF_CMD_OPTARG("-vlandev",			unsetvlandev),
308 	DEF_CMD("vlanmtu",	IFCAP_VLAN_MTU,		setifcap),
309 	DEF_CMD("-vlanmtu",	-IFCAP_VLAN_MTU,	setifcap),
310 	DEF_CMD("vlanhwtag",	IFCAP_VLAN_HWTAGGING,	setifcap),
311 	DEF_CMD("-vlanhwtag",	-IFCAP_VLAN_HWTAGGING,	setifcap),
312 	DEF_CMD("vlanhwfilter",	IFCAP_VLAN_HWFILTER,	setifcap),
313 	DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER,	setifcap),
314 	DEF_CMD("-vlanhwtso",	-IFCAP_VLAN_HWTSO,	setifcap),
315 	DEF_CMD("vlanhwtso",	IFCAP_VLAN_HWTSO,	setifcap),
316 	DEF_CMD("vlanhwcsum",	IFCAP_VLAN_HWCSUM,	setifcap),
317 	DEF_CMD("-vlanhwcsum",	-IFCAP_VLAN_HWCSUM,	setifcap),
318 };
319 static struct afswtch af_vlan = {
320 	.af_name	= "af_vlan",
321 	.af_af		= AF_UNSPEC,
322 	.af_other_status = vlan_status,
323 };
324 
325 static __constructor void
vlan_ctor(void)326 vlan_ctor(void)
327 {
328 	size_t i;
329 
330 	for (i = 0; i < nitems(vlan_cmds);  i++)
331 		cmd_register(&vlan_cmds[i]);
332 	af_register(&af_vlan);
333 	callback_register(vlan_cb, NULL);
334 	clone_setdefcallback_prefix("vlan", vlan_create);
335 	clone_setdefcallback_filter(vlan_match_ethervid, vlan_create);
336 }
337