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