xref: /freebsd-13-stable/sbin/ifconfig/ifvlan.c (revision de270f81b246e154640c0d250696c458ff1c0e3b)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1999 Bill Paul <wpaul@ctr.columbia.edu>
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 #define	NOTAG	((u_short) -1)
63 
64 static const char proto_8021Q[]  = "802.1q";
65 static const char proto_8021ad[] = "802.1ad";
66 static const char proto_qinq[] = "qinq";
67 
68 static 	struct vlanreq params = {
69 	.vlr_tag	= NOTAG,
70 	.vlr_proto	= ETHERTYPE_VLAN,
71 };
72 
73 static int
getvlan(int s,struct ifreq * ifr,struct vlanreq * vreq)74 getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq)
75 {
76 	bzero((char *)vreq, sizeof(*vreq));
77 	ifr->ifr_data = (caddr_t)vreq;
78 
79 	return ioctl(s, SIOCGETVLAN, (caddr_t)ifr);
80 }
81 
82 static void
vlan_status(int s)83 vlan_status(int s)
84 {
85 	struct vlanreq		vreq;
86 
87 	if (getvlan(s, &ifr, &vreq) == -1)
88 		return;
89 	printf("\tvlan: %d", vreq.vlr_tag);
90 	printf(" vlanproto: ");
91 	switch (vreq.vlr_proto) {
92 		case ETHERTYPE_VLAN:
93 			printf(proto_8021Q);
94 			break;
95 		case ETHERTYPE_QINQ:
96 			printf(proto_8021ad);
97 			break;
98 		default:
99 			printf("0x%04x", vreq.vlr_proto);
100 	}
101 	if (ioctl(s, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
102 		printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
103 	printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
104 	    "<none>" : vreq.vlr_parent);
105 	printf("\n");
106 }
107 
108 static int
vlan_match_ethervid(const char * name)109 vlan_match_ethervid(const char *name)
110 {
111 	return (strchr(name, '.') != NULL);
112 }
113 
114 static void
vlan_parse_ethervid(const char * name)115 vlan_parse_ethervid(const char *name)
116 {
117 	char ifname[IFNAMSIZ];
118 	char *cp;
119 	unsigned int vid;
120 
121 	strlcpy(ifname, name, IFNAMSIZ);
122 	if ((cp = strrchr(ifname, '.')) == NULL)
123 		return;
124 	/*
125 	 * Derive params from interface name: "parent.vid".
126 	 */
127 	*cp++ = '\0';
128 	if ((*cp < '1') || (*cp > '9'))
129 		errx(1, "invalid vlan tag");
130 
131 	vid = *cp++ - '0';
132 	while ((*cp >= '0') && (*cp <= '9')) {
133 		vid = (vid * 10) + (*cp++ - '0');
134 		if (vid >= 0xFFF)
135 			errx(1, "invalid vlan tag");
136 	}
137 	if (*cp != '\0')
138 		errx(1, "invalid vlan tag");
139 
140 	/*
141 	 * allow "devX.Y vlandev devX vlan Y" syntax
142 	 */
143 	if (params.vlr_tag == NOTAG || params.vlr_tag == vid)
144 		params.vlr_tag = vid;
145 	else
146 		errx(1, "ambiguous vlan specification");
147 
148 	/* Restrict overriding interface name */
149 	if (params.vlr_parent[0] == '\0' || !strcmp(params.vlr_parent, ifname))
150 		strlcpy(params.vlr_parent, ifname, IFNAMSIZ);
151 	else
152 		errx(1, "ambiguous vlan specification");
153 }
154 
155 static void
vlan_create(int s,struct ifreq * ifr)156 vlan_create(int s, struct ifreq *ifr)
157 {
158 	vlan_parse_ethervid(ifr->ifr_name);
159 
160 	if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
161 		/*
162 		 * One or both parameters were specified, make sure both.
163 		 */
164 		if (params.vlr_tag == NOTAG)
165 			errx(1, "must specify a tag for vlan create");
166 		if (params.vlr_parent[0] == '\0')
167 			errx(1, "must specify a parent device for vlan create");
168 		ifr->ifr_data = (caddr_t) &params;
169 	}
170 	ioctl_ifcreate(s, ifr);
171 }
172 
173 static void
vlan_cb(int s,void * arg)174 vlan_cb(int s, void *arg)
175 {
176 	if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
177 		errx(1, "both vlan and vlandev must be specified");
178 }
179 
180 static void
vlan_set(int s,struct ifreq * ifr)181 vlan_set(int s, struct ifreq *ifr)
182 {
183 	if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
184 		ifr->ifr_data = (caddr_t) &params;
185 		if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
186 			err(1, "SIOCSETVLAN");
187 	}
188 }
189 
190 static
DECL_CMD_FUNC(setvlantag,val,d)191 DECL_CMD_FUNC(setvlantag, val, d)
192 {
193 	struct vlanreq vreq;
194 	u_long ul;
195 	char *endp;
196 
197 	ul = strtoul(val, &endp, 0);
198 	if (*endp != '\0')
199 		errx(1, "invalid value for vlan");
200 	params.vlr_tag = ul;
201 	/* check if the value can be represented in vlr_tag */
202 	if (params.vlr_tag != ul)
203 		errx(1, "value for vlan out of range");
204 
205 	if (getvlan(s, &ifr, &vreq) != -1)
206 		vlan_set(s, &ifr);
207 }
208 
209 static
DECL_CMD_FUNC(setvlandev,val,d)210 DECL_CMD_FUNC(setvlandev, val, d)
211 {
212 	struct vlanreq vreq;
213 
214 	strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
215 
216 	if (getvlan(s, &ifr, &vreq) != -1)
217 		vlan_set(s, &ifr);
218 }
219 
220 static
DECL_CMD_FUNC(setvlanproto,val,d)221 DECL_CMD_FUNC(setvlanproto, val, d)
222 {
223 	struct vlanreq vreq;
224 
225 	if (strncasecmp(proto_8021Q, val,
226 	    strlen(proto_8021Q)) == 0) {
227 		params.vlr_proto = ETHERTYPE_VLAN;
228 	} else if ((strncasecmp(proto_8021ad, val, strlen(proto_8021ad)) == 0)
229 	    || (strncasecmp(proto_qinq, val, strlen(proto_qinq)) == 0)) {
230 		params.vlr_proto = ETHERTYPE_QINQ;
231 	} else
232 		errx(1, "invalid value for vlanproto");
233 
234 	if (getvlan(s, &ifr, &vreq) != -1)
235 		vlan_set(s, &ifr);
236 }
237 
238 static
DECL_CMD_FUNC(setvlanpcp,val,d)239 DECL_CMD_FUNC(setvlanpcp, val, d)
240 {
241 	u_long ul;
242 	char *endp;
243 
244 	ul = strtoul(val, &endp, 0);
245 	if (*endp != '\0')
246 		errx(1, "invalid value for vlanpcp");
247 	if (ul > 7)
248 		errx(1, "value for vlanpcp out of range");
249 	ifr.ifr_vlan_pcp = ul;
250 	if (ioctl(s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
251 		err(1, "SIOCSVLANPCP");
252 }
253 
254 static
DECL_CMD_FUNC(unsetvlandev,val,d)255 DECL_CMD_FUNC(unsetvlandev, val, d)
256 {
257 	struct vlanreq		vreq;
258 
259 	bzero((char *)&vreq, sizeof(struct vlanreq));
260 	ifr.ifr_data = (caddr_t)&vreq;
261 
262 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
263 		err(1, "SIOCGETVLAN");
264 
265 	bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
266 	vreq.vlr_tag = 0;
267 
268 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
269 		err(1, "SIOCSETVLAN");
270 }
271 
272 static struct cmd vlan_cmds[] = {
273 	DEF_CLONE_CMD_ARG("vlan",			setvlantag),
274 	DEF_CLONE_CMD_ARG("vlandev",			setvlandev),
275 	DEF_CLONE_CMD_ARG("vlanproto",			setvlanproto),
276 	DEF_CMD_ARG("vlanpcp",				setvlanpcp),
277 	/* NB: non-clone cmds */
278 	DEF_CMD_ARG("vlan",				setvlantag),
279 	DEF_CMD_ARG("vlandev",				setvlandev),
280 	DEF_CMD_ARG("vlanproto",			setvlanproto),
281 	/* XXX For compatibility.  Should become DEF_CMD() some day. */
282 	DEF_CMD_OPTARG("-vlandev",			unsetvlandev),
283 	DEF_CMD("vlanmtu",	IFCAP_VLAN_MTU,		setifcap),
284 	DEF_CMD("-vlanmtu",	-IFCAP_VLAN_MTU,	setifcap),
285 	DEF_CMD("vlanhwtag",	IFCAP_VLAN_HWTAGGING,	setifcap),
286 	DEF_CMD("-vlanhwtag",	-IFCAP_VLAN_HWTAGGING,	setifcap),
287 	DEF_CMD("vlanhwfilter",	IFCAP_VLAN_HWFILTER,	setifcap),
288 	DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER,	setifcap),
289 	DEF_CMD("-vlanhwtso",	-IFCAP_VLAN_HWTSO,	setifcap),
290 	DEF_CMD("vlanhwtso",	IFCAP_VLAN_HWTSO,	setifcap),
291 	DEF_CMD("vlanhwcsum",	IFCAP_VLAN_HWCSUM,	setifcap),
292 	DEF_CMD("-vlanhwcsum",	-IFCAP_VLAN_HWCSUM,	setifcap),
293 };
294 static struct afswtch af_vlan = {
295 	.af_name	= "af_vlan",
296 	.af_af		= AF_UNSPEC,
297 	.af_other_status = vlan_status,
298 };
299 
300 static __constructor void
vlan_ctor(void)301 vlan_ctor(void)
302 {
303 	size_t i;
304 
305 	for (i = 0; i < nitems(vlan_cmds);  i++)
306 		cmd_register(&vlan_cmds[i]);
307 	af_register(&af_vlan);
308 	callback_register(vlan_cb, NULL);
309 	clone_setdefcallback_prefix("vlan", vlan_create);
310 	clone_setdefcallback_filter(vlan_match_ethervid, vlan_create);
311 }
312