xref: /freebsd/sbin/ifconfig/ifvlan.c (revision d6b92ffa)
1 /*
2  * Copyright (c) 1999 Bill Paul <wpaul@ctr.columbia.edu>
3  * Copyright (c) 2012 ADARA Networks, Inc.
4  * All rights reserved.
5   *
6  * Portions of this software were developed by Robert N. M. Watson under
7  * contract to ADARA Networks, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by Bill Paul.
20  * 4. Neither the name of the author nor the names of any co-contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 #include <net/ethernet.h>
46 #include <net/if.h>
47 #include <net/if_vlan_var.h>
48 #include <net/route.h>
49 
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <err.h>
56 #include <errno.h>
57 
58 #include "ifconfig.h"
59 
60 #ifndef lint
61 static const char rcsid[] =
62   "$FreeBSD$";
63 #endif
64 
65 #define	NOTAG	((u_short) -1)
66 
67 static 	struct vlanreq params = {
68 	.vlr_tag	= NOTAG,
69 };
70 
71 static int
72 getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq)
73 {
74 	bzero((char *)vreq, sizeof(*vreq));
75 	ifr->ifr_data = (caddr_t)vreq;
76 
77 	return ioctl(s, SIOCGETVLAN, (caddr_t)ifr);
78 }
79 
80 static void
81 vlan_status(int s)
82 {
83 	struct vlanreq		vreq;
84 
85 	if (getvlan(s, &ifr, &vreq) == -1)
86 		return;
87 	printf("\tvlan: %d", vreq.vlr_tag);
88 	if (ioctl(s, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
89 		printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
90 	printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
91 	    "<none>" : vreq.vlr_parent);
92 	printf("\n");
93 }
94 
95 static void
96 vlan_create(int s, struct ifreq *ifr)
97 {
98 	if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
99 		/*
100 		 * One or both parameters were specified, make sure both.
101 		 */
102 		if (params.vlr_tag == NOTAG)
103 			errx(1, "must specify a tag for vlan create");
104 		if (params.vlr_parent[0] == '\0')
105 			errx(1, "must specify a parent device for vlan create");
106 		ifr->ifr_data = (caddr_t) &params;
107 	}
108 	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
109 		err(1, "SIOCIFCREATE2");
110 }
111 
112 static void
113 vlan_cb(int s, void *arg)
114 {
115 	if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
116 		errx(1, "both vlan and vlandev must be specified");
117 }
118 
119 static void
120 vlan_set(int s, struct ifreq *ifr)
121 {
122 	if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
123 		ifr->ifr_data = (caddr_t) &params;
124 		if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
125 			err(1, "SIOCSETVLAN");
126 	}
127 }
128 
129 static
130 DECL_CMD_FUNC(setvlantag, val, d)
131 {
132 	struct vlanreq vreq;
133 	u_long ul;
134 	char *endp;
135 
136 	ul = strtoul(val, &endp, 0);
137 	if (*endp != '\0')
138 		errx(1, "invalid value for vlan");
139 	params.vlr_tag = ul;
140 	/* check if the value can be represented in vlr_tag */
141 	if (params.vlr_tag != ul)
142 		errx(1, "value for vlan out of range");
143 
144 	if (getvlan(s, &ifr, &vreq) != -1)
145 		vlan_set(s, &ifr);
146 }
147 
148 static
149 DECL_CMD_FUNC(setvlandev, val, d)
150 {
151 	struct vlanreq vreq;
152 
153 	strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
154 
155 	if (getvlan(s, &ifr, &vreq) != -1)
156 		vlan_set(s, &ifr);
157 }
158 
159 static
160 DECL_CMD_FUNC(setvlanpcp, val, d)
161 {
162 	u_long ul;
163 	char *endp;
164 
165 	ul = strtoul(val, &endp, 0);
166 	if (*endp != '\0')
167 		errx(1, "invalid value for vlanpcp");
168 	if (ul > 7)
169 		errx(1, "value for vlanpcp out of range");
170 	ifr.ifr_vlan_pcp = ul;
171 	if (ioctl(s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
172 		err(1, "SIOCSVLANPCP");
173 }
174 
175 static
176 DECL_CMD_FUNC(unsetvlandev, val, d)
177 {
178 	struct vlanreq		vreq;
179 
180 	bzero((char *)&vreq, sizeof(struct vlanreq));
181 	ifr.ifr_data = (caddr_t)&vreq;
182 
183 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
184 		err(1, "SIOCGETVLAN");
185 
186 	bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
187 	vreq.vlr_tag = 0;
188 
189 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
190 		err(1, "SIOCSETVLAN");
191 }
192 
193 static struct cmd vlan_cmds[] = {
194 	DEF_CLONE_CMD_ARG("vlan",			setvlantag),
195 	DEF_CLONE_CMD_ARG("vlandev",			setvlandev),
196 	DEF_CMD_ARG("vlanpcp",				setvlanpcp),
197 	/* NB: non-clone cmds */
198 	DEF_CMD_ARG("vlan",				setvlantag),
199 	DEF_CMD_ARG("vlandev",				setvlandev),
200 	/* XXX For compatibility.  Should become DEF_CMD() some day. */
201 	DEF_CMD_OPTARG("-vlandev",			unsetvlandev),
202 	DEF_CMD("vlanmtu",	IFCAP_VLAN_MTU,		setifcap),
203 	DEF_CMD("-vlanmtu",	-IFCAP_VLAN_MTU,	setifcap),
204 	DEF_CMD("vlanhwtag",	IFCAP_VLAN_HWTAGGING,	setifcap),
205 	DEF_CMD("-vlanhwtag",	-IFCAP_VLAN_HWTAGGING,	setifcap),
206 	DEF_CMD("vlanhwfilter",	IFCAP_VLAN_HWFILTER,	setifcap),
207 	DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER,	setifcap),
208 	DEF_CMD("-vlanhwtso",	-IFCAP_VLAN_HWTSO,	setifcap),
209 	DEF_CMD("vlanhwtso",	IFCAP_VLAN_HWTSO,	setifcap),
210 	DEF_CMD("vlanhwcsum",	IFCAP_VLAN_HWCSUM,	setifcap),
211 	DEF_CMD("-vlanhwcsum",	-IFCAP_VLAN_HWCSUM,	setifcap),
212 };
213 static struct afswtch af_vlan = {
214 	.af_name	= "af_vlan",
215 	.af_af		= AF_UNSPEC,
216 	.af_other_status = vlan_status,
217 };
218 
219 static __constructor void
220 vlan_ctor(void)
221 {
222 	size_t i;
223 
224 	for (i = 0; i < nitems(vlan_cmds);  i++)
225 		cmd_register(&vlan_cmds[i]);
226 	af_register(&af_vlan);
227 	callback_register(vlan_cb, NULL);
228 	clone_setdefcallback("vlan", vlan_create);
229 }
230