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 #ifndef lint
63 static const char rcsid[] =
64 "$FreeBSD: stable/12/sbin/ifconfig/ifvlan.c 368297 2020-12-03 10:32:54Z hselasky $";
65 #endif
66
67 #define NOTAG ((u_short) -1)
68
69 static struct vlanreq params = {
70 .vlr_tag = NOTAG,
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 if (ioctl(s, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
91 printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
92 printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
93 "<none>" : vreq.vlr_parent);
94 printf("\n");
95 }
96
97 static void
vlan_create(int s,struct ifreq * ifr)98 vlan_create(int s, struct ifreq *ifr)
99 {
100 if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
101 /*
102 * One or both parameters were specified, make sure both.
103 */
104 if (params.vlr_tag == NOTAG)
105 errx(1, "must specify a tag for vlan create");
106 if (params.vlr_parent[0] == '\0')
107 errx(1, "must specify a parent device for vlan create");
108 ifr->ifr_data = (caddr_t) ¶ms;
109 }
110 ioctl_ifcreate(s, ifr);
111 }
112
113 static void
vlan_cb(int s,void * arg)114 vlan_cb(int s, void *arg)
115 {
116 if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
117 errx(1, "both vlan and vlandev must be specified");
118 }
119
120 static void
vlan_set(int s,struct ifreq * ifr)121 vlan_set(int s, struct ifreq *ifr)
122 {
123 if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
124 ifr->ifr_data = (caddr_t) ¶ms;
125 if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
126 err(1, "SIOCSETVLAN");
127 }
128 }
129
130 static
DECL_CMD_FUNC(setvlantag,val,d)131 DECL_CMD_FUNC(setvlantag, val, d)
132 {
133 struct vlanreq vreq;
134 u_long ul;
135 char *endp;
136
137 ul = strtoul(val, &endp, 0);
138 if (*endp != '\0')
139 errx(1, "invalid value for vlan");
140 params.vlr_tag = ul;
141 /* check if the value can be represented in vlr_tag */
142 if (params.vlr_tag != ul)
143 errx(1, "value for vlan out of range");
144
145 if (getvlan(s, &ifr, &vreq) != -1)
146 vlan_set(s, &ifr);
147 }
148
149 static
DECL_CMD_FUNC(setvlandev,val,d)150 DECL_CMD_FUNC(setvlandev, val, d)
151 {
152 struct vlanreq vreq;
153
154 strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
155
156 if (getvlan(s, &ifr, &vreq) != -1)
157 vlan_set(s, &ifr);
158 }
159
160 static
DECL_CMD_FUNC(setvlanpcp,val,d)161 DECL_CMD_FUNC(setvlanpcp, val, d)
162 {
163 u_long ul;
164 char *endp;
165
166 ul = strtoul(val, &endp, 0);
167 if (*endp != '\0')
168 errx(1, "invalid value for vlanpcp");
169 if (ul > 7)
170 errx(1, "value for vlanpcp out of range");
171 ifr.ifr_vlan_pcp = ul;
172 if (ioctl(s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
173 err(1, "SIOCSVLANPCP");
174 }
175
176 static
DECL_CMD_FUNC(unsetvlandev,val,d)177 DECL_CMD_FUNC(unsetvlandev, val, d)
178 {
179 struct vlanreq vreq;
180
181 bzero((char *)&vreq, sizeof(struct vlanreq));
182 ifr.ifr_data = (caddr_t)&vreq;
183
184 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
185 err(1, "SIOCGETVLAN");
186
187 bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
188 vreq.vlr_tag = 0;
189
190 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
191 err(1, "SIOCSETVLAN");
192 }
193
194 static struct cmd vlan_cmds[] = {
195 DEF_CLONE_CMD_ARG("vlan", setvlantag),
196 DEF_CLONE_CMD_ARG("vlandev", setvlandev),
197 DEF_CMD_ARG("vlanpcp", setvlanpcp),
198 /* NB: non-clone cmds */
199 DEF_CMD_ARG("vlan", setvlantag),
200 DEF_CMD_ARG("vlandev", setvlandev),
201 /* XXX For compatibility. Should become DEF_CMD() some day. */
202 DEF_CMD_OPTARG("-vlandev", unsetvlandev),
203 DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap),
204 DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap),
205 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap),
206 DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap),
207 DEF_CMD("vlanhwfilter", IFCAP_VLAN_HWFILTER, setifcap),
208 DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER, setifcap),
209 DEF_CMD("-vlanhwtso", -IFCAP_VLAN_HWTSO, setifcap),
210 DEF_CMD("vlanhwtso", IFCAP_VLAN_HWTSO, setifcap),
211 DEF_CMD("vlanhwcsum", IFCAP_VLAN_HWCSUM, setifcap),
212 DEF_CMD("-vlanhwcsum", -IFCAP_VLAN_HWCSUM, setifcap),
213 };
214 static struct afswtch af_vlan = {
215 .af_name = "af_vlan",
216 .af_af = AF_UNSPEC,
217 .af_other_status = vlan_status,
218 };
219
220 static __constructor void
vlan_ctor(void)221 vlan_ctor(void)
222 {
223 size_t i;
224
225 for (i = 0; i < nitems(vlan_cmds); i++)
226 cmd_register(&vlan_cmds[i]);
227 af_register(&af_vlan);
228 callback_register(vlan_cb, NULL);
229 clone_setdefcallback("vlan", vlan_create);
230 }
231