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