ifvlan.c revision 186101
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_var.h>
44#include <net/if_vlan_var.h>
45#include <net/route.h>
46
47#include <ctype.h>
48#include <stdio.h>
49#include <string.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <err.h>
53#include <errno.h>
54
55#include "ifconfig.h"
56
57#ifndef lint
58static const char rcsid[] =
59  "$FreeBSD: head/sbin/ifconfig/ifvlan.c 186101 2008-12-15 01:06:49Z sam $";
60#endif
61
62#define	NOTAG	((u_short) -1)
63
64static 	struct vlanreq params = {
65	.vlr_tag	= NOTAG,
66};
67
68static int
69getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq)
70{
71	bzero((char *)vreq, sizeof(*vreq));
72	ifr->ifr_data = (caddr_t)vreq;
73
74	return ioctl(s, SIOCGETVLAN, (caddr_t)ifr);
75}
76
77static void
78vlan_status(int s)
79{
80	struct vlanreq		vreq;
81
82	if (getvlan(s, &ifr, &vreq) != -1)
83		printf("\tvlan: %d parent interface: %s\n",
84		    vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
85		    "<none>" : vreq.vlr_parent);
86}
87
88static void
89vlan_create(int s, struct ifreq *ifr)
90{
91	if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
92		/*
93		 * One or both parameters were specified, make sure both.
94		 */
95		if (params.vlr_tag == NOTAG)
96			errx(1, "must specify a tag for vlan create");
97		if (params.vlr_parent[0] == '\0')
98			errx(1, "must specify a parent device for vlan create");
99		ifr->ifr_data = (caddr_t) &params;
100	}
101	if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
102		err(1, "SIOCIFCREATE2");
103}
104
105static void
106vlan_cb(int s, void *arg)
107{
108	if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
109		errx(1, "both vlan and vlandev must be specified");
110}
111
112static void
113vlan_set(int s, struct ifreq *ifr)
114{
115	if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
116		ifr->ifr_data = (caddr_t) &params;
117		if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
118			err(1, "SIOCSETVLAN");
119	}
120}
121
122static void
123getvlantag(const char *val)
124{
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
137static
138DECL_CMD_FUNC(setvlantag_clone, val, d)
139{
140	getvlantag(val);
141	clone_setcallback(vlan_create);
142}
143
144static
145DECL_CMD_FUNC(setvlantag, val, d)
146{
147	struct vlanreq vreq;
148
149	getvlantag(val);
150	if (getvlan(s, &ifr, &vreq) == -1)
151		errx(1, "no existing vlan");
152	vlan_set(s, &ifr);
153}
154
155static
156DECL_CMD_FUNC(setvlandev_clone, val, d)
157{
158	strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
159	clone_setcallback(vlan_create);
160}
161
162static
163DECL_CMD_FUNC(setvlandev, val, d)
164{
165	struct vlanreq vreq;
166
167	strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
168	if (getvlan(s, &ifr, &vreq) != -1)
169		errx(1, "no existing vlan");
170	vlan_set(s, &ifr);
171}
172
173static
174DECL_CMD_FUNC(unsetvlandev, val, d)
175{
176	struct vlanreq		vreq;
177
178	bzero((char *)&vreq, sizeof(struct vlanreq));
179	ifr.ifr_data = (caddr_t)&vreq;
180
181	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
182		err(1, "SIOCGETVLAN");
183
184	bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
185	vreq.vlr_tag = 0;
186
187	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
188		err(1, "SIOCSETVLAN");
189}
190
191static struct cmd vlan_cmds[] = {
192	DEF_CLONE_CMD_ARG("vlan",			setvlantag_clone),
193	DEF_CLONE_CMD_ARG("vlandev",			setvlandev_clone),
194	/* NB: non-clone cmds */
195	DEF_CMD_ARG("vlan",				setvlantag),
196	DEF_CMD_ARG("vlandev",				setvlandev),
197	/* XXX For compatibility.  Should become DEF_CMD() some day. */
198	DEF_CMD_OPTARG("-vlandev",			unsetvlandev),
199	DEF_CMD("vlanmtu",	IFCAP_VLAN_MTU,		setifcap),
200	DEF_CMD("-vlanmtu",	-IFCAP_VLAN_MTU,	setifcap),
201	DEF_CMD("vlanhwtag",	IFCAP_VLAN_HWTAGGING,	setifcap),
202	DEF_CMD("-vlanhwtag",	-IFCAP_VLAN_HWTAGGING,	setifcap),
203	DEF_CMD("vlanhwfilter",	IFCAP_VLAN_HWFILTER,	setifcap),
204	DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER,	setifcap),
205};
206static struct afswtch af_vlan = {
207	.af_name	= "af_vlan",
208	.af_af		= AF_UNSPEC,
209	.af_other_status = vlan_status,
210};
211
212static __constructor void
213vlan_ctor(void)
214{
215#define	N(a)	(sizeof(a) / sizeof(a[0]))
216	int i;
217
218	for (i = 0; i < N(vlan_cmds);  i++)
219		cmd_register(&vlan_cmds[i]);
220	af_register(&af_vlan);
221	callback_register(vlan_cb, NULL);
222#undef N
223}
224