2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1999 Bill Paul <wpaul@ctr.columbia.edu>
5 * Copyright (c) 2012 ADARA Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to ADARA Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
47 #include <net/ethernet.h>
49 #include <net/if_vlan_var.h>
50 #include <net/route.h>
62 #define NOTAG ((u_short) -1)
64 static const char proto_8021Q[] = "802.1q";
65 static const char proto_8021ad[] = "802.1ad";
66 static const char proto_qinq[] = "qinq";
68 static struct vlanreq params = {
70 .vlr_proto = ETHERTYPE_VLAN,
74 vlan_status(if_ctx *ctx)
76 struct vlanreq vreq = {};
77 struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
79 if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) == -1)
81 printf("\tvlan: %d", vreq.vlr_tag);
82 printf(" vlanproto: ");
83 switch (vreq.vlr_proto) {
91 printf("0x%04x", vreq.vlr_proto);
93 if (ioctl_ctx_ifr(ctx, SIOCGVLANPCP, &ifr) != -1)
94 printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
95 printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
96 "<none>" : vreq.vlr_parent);
101 vlan_match_ethervid(const char *name)
103 return (strchr(name, '.') != NULL);
107 vlan_parse_ethervid(const char *name)
109 char ifname[IFNAMSIZ];
113 strlcpy(ifname, name, IFNAMSIZ);
114 if ((cp = strrchr(ifname, '.')) == NULL)
117 * Derive params from interface name: "parent.vid".
120 if ((*cp < '1') || (*cp > '9'))
121 errx(1, "invalid vlan tag");
124 while ((*cp >= '0') && (*cp <= '9')) {
125 vid = (vid * 10) + (*cp++ - '0');
127 errx(1, "invalid vlan tag");
130 errx(1, "invalid vlan tag");
133 * allow "devX.Y vlandev devX vlan Y" syntax
135 if (params.vlr_tag == NOTAG || params.vlr_tag == vid)
136 params.vlr_tag = vid;
138 errx(1, "ambiguous vlan specification");
140 /* Restrict overriding interface name */
141 if (params.vlr_parent[0] == '\0' || !strcmp(params.vlr_parent, ifname))
142 strlcpy(params.vlr_parent, ifname, IFNAMSIZ);
144 errx(1, "ambiguous vlan specification");
148 vlan_create(if_ctx *ctx, struct ifreq *ifr)
150 vlan_parse_ethervid(ifr->ifr_name);
152 if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
154 * One or both parameters were specified, make sure both.
156 if (params.vlr_tag == NOTAG)
157 errx(1, "must specify a tag for vlan create");
158 if (params.vlr_parent[0] == '\0')
159 errx(1, "must specify a parent device for vlan create");
160 ifr->ifr_data = (caddr_t) ¶ms;
162 ifcreate_ioctl(ctx, ifr);
166 vlan_cb(if_ctx *ctx __unused, void *arg __unused)
168 if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
169 errx(1, "both vlan and vlandev must be specified");
173 vlan_set(int s, struct ifreq *ifr)
175 if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
176 ifr->ifr_data = (caddr_t) ¶ms;
177 if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
178 err(1, "SIOCSETVLAN");
183 setvlantag(if_ctx *ctx, const char *val, int dummy __unused)
185 struct vlanreq vreq = {};
186 struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
190 ul = strtoul(val, &endp, 0);
192 errx(1, "invalid value for vlan");
194 /* check if the value can be represented in vlr_tag */
195 if (params.vlr_tag != ul)
196 errx(1, "value for vlan out of range");
198 if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) {
199 vreq.vlr_tag = params.vlr_tag;
200 memcpy(¶ms, &vreq, sizeof(params));
201 vlan_set(ctx->io_s, &ifr);
206 setvlandev(if_ctx *ctx, const char *val, int dummy __unused)
208 struct vlanreq vreq = {};
209 struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
211 strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
213 if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1)
214 vlan_set(ctx->io_s, &ifr);
218 setvlanproto(if_ctx *ctx, const char *val, int dummy __unused)
220 struct vlanreq vreq = {};
221 struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
223 if (strncasecmp(proto_8021Q, val,
224 strlen(proto_8021Q)) == 0) {
225 params.vlr_proto = ETHERTYPE_VLAN;
226 } else if ((strncasecmp(proto_8021ad, val, strlen(proto_8021ad)) == 0)
227 || (strncasecmp(proto_qinq, val, strlen(proto_qinq)) == 0)) {
228 params.vlr_proto = ETHERTYPE_QINQ;
230 errx(1, "invalid value for vlanproto");
232 if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) {
233 vreq.vlr_proto = params.vlr_proto;
234 memcpy(¶ms, &vreq, sizeof(params));
235 vlan_set(ctx->io_s, &ifr);
240 setvlanpcp(if_ctx *ctx, const char *val, int dummy __unused)
244 struct ifreq ifr = {};
246 ul = strtoul(val, &endp, 0);
248 errx(1, "invalid value for vlanpcp");
250 errx(1, "value for vlanpcp out of range");
251 ifr.ifr_vlan_pcp = ul;
252 if (ioctl_ctx_ifr(ctx, SIOCSVLANPCP, &ifr) == -1)
253 err(1, "SIOCSVLANPCP");
257 unsetvlandev(if_ctx *ctx, const char *val __unused, int dummy __unused)
259 struct vlanreq vreq = {};
260 struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
262 if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) == -1)
263 err(1, "SIOCGETVLAN");
265 bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
268 if (ioctl_ctx(ctx, SIOCSETVLAN, (caddr_t)&ifr) == -1)
269 err(1, "SIOCSETVLAN");
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, clearifcap),
285 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap),
286 DEF_CMD("-vlanhwtag", IFCAP_VLAN_HWTAGGING, clearifcap),
287 DEF_CMD("vlanhwfilter", IFCAP_VLAN_HWFILTER, setifcap),
288 DEF_CMD("-vlanhwfilter", IFCAP_VLAN_HWFILTER, clearifcap),
289 DEF_CMD("vlanhwtso", IFCAP_VLAN_HWTSO, setifcap),
290 DEF_CMD("-vlanhwtso", IFCAP_VLAN_HWTSO, clearifcap),
291 DEF_CMD("vlanhwcsum", IFCAP_VLAN_HWCSUM, setifcap),
292 DEF_CMD("-vlanhwcsum", IFCAP_VLAN_HWCSUM, clearifcap),
294 static struct afswtch af_vlan = {
295 .af_name = "af_vlan",
297 .af_other_status = vlan_status,
300 static __constructor void
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);