1999-03-15 01:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1999
|
|
|
|
* Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Bill Paul.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_var.h>
|
|
|
|
#include <net/if_vlan_var.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "ifconfig.h"
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1999-03-15 01:22:01 +00:00
|
|
|
#endif
|
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
#define NOTAG ((u_short) -1)
|
|
|
|
|
|
|
|
static struct vlanreq params = {
|
|
|
|
.vlr_tag = NOTAG,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq)
|
|
|
|
{
|
|
|
|
bzero((char *)vreq, sizeof(*vreq));
|
|
|
|
ifr->ifr_data = (caddr_t)vreq;
|
|
|
|
|
|
|
|
return ioctl(s, SIOCGETVLAN, (caddr_t)ifr);
|
|
|
|
}
|
2006-03-09 14:58:09 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2004-12-31 19:46:27 +00:00
|
|
|
vlan_status(int s)
|
1999-03-15 01:22:01 +00:00
|
|
|
{
|
|
|
|
struct vlanreq vreq;
|
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
if (getvlan(s, &ifr, &vreq) != -1)
|
|
|
|
printf("\tvlan: %d parent interface: %s\n",
|
|
|
|
vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
|
|
|
|
"<none>" : vreq.vlr_parent);
|
|
|
|
}
|
1999-03-15 01:22:01 +00:00
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
static void
|
|
|
|
vlan_create(int s, struct ifreq *ifr)
|
|
|
|
{
|
|
|
|
if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
|
|
|
|
/*
|
|
|
|
* One or both parameters were specified, make sure both.
|
|
|
|
*/
|
|
|
|
if (params.vlr_tag == NOTAG)
|
|
|
|
errx(1, "must specify a tag for vlan create");
|
|
|
|
if (params.vlr_parent[0] == '\0')
|
|
|
|
errx(1, "must specify a parent device for vlan create");
|
|
|
|
ifr->ifr_data = (caddr_t) ¶ms;
|
|
|
|
}
|
|
|
|
if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
|
|
|
|
err(1, "SIOCIFCREATE2");
|
|
|
|
}
|
1999-03-15 01:22:01 +00:00
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
static void
|
|
|
|
vlan_cb(int s, void *arg)
|
|
|
|
{
|
|
|
|
if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
|
|
|
|
errx(1, "both vlan and vlandev must be specified");
|
1999-03-15 01:22:01 +00:00
|
|
|
}
|
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
static void
|
2006-07-09 06:10:23 +00:00
|
|
|
vlan_set(int s, struct ifreq *ifr)
|
|
|
|
{
|
|
|
|
if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') {
|
|
|
|
ifr->ifr_data = (caddr_t) ¶ms;
|
|
|
|
if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1)
|
|
|
|
err(1, "SIOCSETVLAN");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
DECL_CMD_FUNC(setvlantag, val, d)
|
1999-03-15 01:22:01 +00:00
|
|
|
{
|
2006-07-09 06:10:23 +00:00
|
|
|
struct vlanreq vreq;
|
|
|
|
u_long ul;
|
|
|
|
char *endp;
|
2005-10-04 11:48:03 +00:00
|
|
|
|
|
|
|
ul = strtoul(val, &endp, 0);
|
|
|
|
if (*endp != '\0')
|
|
|
|
errx(1, "invalid value for vlan");
|
2006-07-09 06:10:23 +00:00
|
|
|
params.vlr_tag = ul;
|
2005-10-04 11:48:03 +00:00
|
|
|
/* check if the value can be represented in vlr_tag */
|
2006-07-09 06:10:23 +00:00
|
|
|
if (params.vlr_tag != ul)
|
2005-10-04 11:48:03 +00:00
|
|
|
errx(1, "value for vlan out of range");
|
2006-07-09 06:10:23 +00:00
|
|
|
|
|
|
|
if (getvlan(s, &ifr, &vreq) != -1)
|
|
|
|
vlan_set(s, &ifr);
|
|
|
|
else
|
|
|
|
clone_setcallback(vlan_create);
|
1999-03-15 01:22:01 +00:00
|
|
|
}
|
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
static
|
|
|
|
DECL_CMD_FUNC(setvlandev, val, d)
|
1999-03-15 01:22:01 +00:00
|
|
|
{
|
2006-07-09 06:10:23 +00:00
|
|
|
struct vlanreq vreq;
|
|
|
|
|
|
|
|
strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
|
1999-03-15 01:22:01 +00:00
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
if (getvlan(s, &ifr, &vreq) != -1)
|
|
|
|
vlan_set(s, &ifr);
|
|
|
|
else
|
|
|
|
clone_setcallback(vlan_create);
|
1999-03-15 01:22:01 +00:00
|
|
|
}
|
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
static
|
|
|
|
DECL_CMD_FUNC(unsetvlandev, val, d)
|
1999-03-15 01:22:01 +00:00
|
|
|
{
|
2006-07-09 06:10:23 +00:00
|
|
|
struct vlanreq vreq;
|
1999-03-15 01:22:01 +00:00
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
bzero((char *)&vreq, sizeof(struct vlanreq));
|
|
|
|
ifr.ifr_data = (caddr_t)&vreq;
|
2005-09-29 23:38:24 +00:00
|
|
|
|
1999-03-15 01:22:01 +00:00
|
|
|
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
|
|
|
|
err(1, "SIOCGETVLAN");
|
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
|
|
|
|
vreq.vlr_tag = 0;
|
|
|
|
|
1999-03-15 01:22:01 +00:00
|
|
|
if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
|
|
|
|
err(1, "SIOCSETVLAN");
|
|
|
|
}
|
2004-12-08 19:18:07 +00:00
|
|
|
|
|
|
|
static struct cmd vlan_cmds[] = {
|
|
|
|
DEF_CMD_ARG("vlan", setvlantag),
|
|
|
|
DEF_CMD_ARG("vlandev", setvlandev),
|
2005-09-29 23:38:24 +00:00
|
|
|
/* XXX For compatibility. Should become DEF_CMD() some day. */
|
|
|
|
DEF_CMD_OPTARG("-vlandev", unsetvlandev),
|
2004-12-08 19:18:07 +00:00
|
|
|
DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap),
|
|
|
|
DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap),
|
|
|
|
DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap),
|
|
|
|
DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap),
|
|
|
|
};
|
|
|
|
static struct afswtch af_vlan = {
|
|
|
|
.af_name = "af_vlan",
|
|
|
|
.af_af = AF_UNSPEC,
|
2004-12-31 19:46:27 +00:00
|
|
|
.af_other_status = vlan_status,
|
2004-12-08 19:18:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static __constructor void
|
|
|
|
vlan_ctor(void)
|
|
|
|
{
|
|
|
|
#define N(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < N(vlan_cmds); i++)
|
|
|
|
cmd_register(&vlan_cmds[i]);
|
|
|
|
af_register(&af_vlan);
|
2005-10-04 11:48:03 +00:00
|
|
|
callback_register(vlan_cb, NULL);
|
2004-12-08 19:18:07 +00:00
|
|
|
#undef N
|
|
|
|
}
|