#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[] =
"$FreeBSD: src/sbin/ifconfig/ifvlan.c,v 1.12.2.1.2.1 2008/11/25 02:59:29 kensmith Exp $";
#endif
#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);
}
static void
vlan_status(int s)
{
struct vlanreq vreq;
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);
}
static void
vlan_create(int s, struct ifreq *ifr)
{
if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') {
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;
}
#ifdef SIOCIFCREATE2
if (ioctl(s, SIOCIFCREATE2, ifr) < 0)
err(1, "SIOCIFCREATE2");
#else
if (ioctl(s, SIOCIFCREATE, ifr) < 0)
err(1, "SIOCIFCREATE");
#endif
}
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");
}
static void
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)
{
struct vlanreq vreq;
u_long ul;
char *endp;
ul = strtoul(val, &endp, 0);
if (*endp != '\0')
errx(1, "invalid value for vlan");
params.vlr_tag = ul;
if (params.vlr_tag != ul)
errx(1, "value for vlan out of range");
if (getvlan(s, &ifr, &vreq) != -1)
vlan_set(s, &ifr);
else
clone_setcallback(vlan_create);
}
static
DECL_CMD_FUNC(setvlandev, val, d)
{
struct vlanreq vreq;
strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
if (getvlan(s, &ifr, &vreq) != -1)
vlan_set(s, &ifr);
else
clone_setcallback(vlan_create);
}
static
DECL_CMD_FUNC(unsetvlandev, val, d)
{
struct vlanreq vreq;
bzero((char *)&vreq, sizeof(struct vlanreq));
ifr.ifr_data = (caddr_t)&vreq;
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCGETVLAN");
bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
vreq.vlr_tag = 0;
if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCSETVLAN");
}
static struct cmd vlan_cmds[] = {
DEF_CLONE_CMD_ARG("vlan", setvlantag),
DEF_CLONE_CMD_ARG("vlandev", setvlandev),
DEF_CMD_OPTARG("-vlandev", unsetvlandev),
#ifdef notdef
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),
#endif
};
static struct afswtch af_vlan = {
.af_name = "af_vlan",
.af_af = AF_UNSPEC,
.af_other_status = vlan_status,
};
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);
callback_register(vlan_cb, NULL);
#undef N
}