aboutsummaryrefslogtreecommitdiff
path: root/sbin/ifconfig/ifvlan.c
diff options
context:
space:
mode:
Diffstat (limited to 'sbin/ifconfig/ifvlan.c')
-rw-r--r--sbin/ifconfig/ifvlan.c110
1 files changed, 54 insertions, 56 deletions
diff --git a/sbin/ifconfig/ifvlan.c b/sbin/ifconfig/ifvlan.c
index 8b7b6e9daf9a..a79ea35bc14b 100644
--- a/sbin/ifconfig/ifvlan.c
+++ b/sbin/ifconfig/ifvlan.c
@@ -4,7 +4,7 @@
* Copyright (c) 1999 Bill Paul <wpaul@ctr.columbia.edu>
* Copyright (c) 2012 ADARA Networks, Inc.
* All rights reserved.
- *
+ *
* Portions of this software were developed by Robert N. M. Watson under
* contract to ADARA Networks, Inc.
*
@@ -59,11 +59,6 @@
#include "ifconfig.h"
-#ifndef lint
-static const char rcsid[] =
- "$FreeBSD$";
-#endif
-
#define NOTAG ((u_short) -1)
static const char proto_8021Q[] = "802.1q";
@@ -75,21 +70,13 @@ static struct vlanreq params = {
.vlr_proto = ETHERTYPE_VLAN,
};
-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)
+vlan_status(if_ctx *ctx)
{
- struct vlanreq vreq;
+ struct vlanreq vreq = {};
+ struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
- if (getvlan(s, &ifr, &vreq) == -1)
+ if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) == -1)
return;
printf("\tvlan: %d", vreq.vlr_tag);
printf(" vlanproto: ");
@@ -103,7 +90,7 @@ vlan_status(int s)
default:
printf("0x%04x", vreq.vlr_proto);
}
- if (ioctl(s, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
+ if (ioctl_ctx_ifr(ctx, SIOCGVLANPCP, &ifr) != -1)
printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
"<none>" : vreq.vlr_parent);
@@ -121,7 +108,7 @@ vlan_parse_ethervid(const char *name)
{
char ifname[IFNAMSIZ];
char *cp;
- int vid;
+ unsigned int vid;
strlcpy(ifname, name, IFNAMSIZ);
if ((cp = strrchr(ifname, '.')) == NULL)
@@ -134,9 +121,12 @@ vlan_parse_ethervid(const char *name)
errx(1, "invalid vlan tag");
vid = *cp++ - '0';
- while ((*cp >= '0') && (*cp <= '9'))
+ while ((*cp >= '0') && (*cp <= '9')) {
vid = (vid * 10) + (*cp++ - '0');
- if ((*cp != '\0') || (vid & ~0xFFF))
+ if (vid >= 0xFFF)
+ errx(1, "invalid vlan tag");
+ }
+ if (*cp != '\0')
errx(1, "invalid vlan tag");
/*
@@ -155,7 +145,7 @@ vlan_parse_ethervid(const char *name)
}
static void
-vlan_create(int s, struct ifreq *ifr)
+vlan_create(if_ctx *ctx, struct ifreq *ifr)
{
vlan_parse_ethervid(ifr->ifr_name);
@@ -169,11 +159,11 @@ vlan_create(int s, struct ifreq *ifr)
errx(1, "must specify a parent device for vlan create");
ifr->ifr_data = (caddr_t) &params;
}
- ioctl_ifcreate(s, ifr);
+ ifcreate_ioctl(ctx, ifr);
}
static void
-vlan_cb(int s, void *arg)
+vlan_cb(if_ctx *ctx __unused, void *arg __unused)
{
if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
errx(1, "both vlan and vlandev must be specified");
@@ -189,10 +179,11 @@ vlan_set(int s, struct ifreq *ifr)
}
}
-static
-DECL_CMD_FUNC(setvlantag, val, d)
+static void
+setvlantag(if_ctx *ctx, const char *val, int dummy __unused)
{
- struct vlanreq vreq;
+ struct vlanreq vreq = {};
+ struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
u_long ul;
char *endp;
@@ -204,25 +195,30 @@ DECL_CMD_FUNC(setvlantag, val, d)
if (params.vlr_tag != ul)
errx(1, "value for vlan out of range");
- if (getvlan(s, &ifr, &vreq) != -1)
- vlan_set(s, &ifr);
+ if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) {
+ vreq.vlr_tag = params.vlr_tag;
+ memcpy(&params, &vreq, sizeof(params));
+ vlan_set(ctx->io_s, &ifr);
+ }
}
-static
-DECL_CMD_FUNC(setvlandev, val, d)
+static void
+setvlandev(if_ctx *ctx, const char *val, int dummy __unused)
{
- struct vlanreq vreq;
+ struct vlanreq vreq = {};
+ struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
- if (getvlan(s, &ifr, &vreq) != -1)
- vlan_set(s, &ifr);
+ if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1)
+ vlan_set(ctx->io_s, &ifr);
}
-static
-DECL_CMD_FUNC(setvlanproto, val, d)
+static void
+setvlanproto(if_ctx *ctx, const char *val, int dummy __unused)
{
- struct vlanreq vreq;
+ struct vlanreq vreq = {};
+ struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
if (strncasecmp(proto_8021Q, val,
strlen(proto_8021Q)) == 0) {
@@ -233,15 +229,19 @@ DECL_CMD_FUNC(setvlanproto, val, d)
} else
errx(1, "invalid value for vlanproto");
- if (getvlan(s, &ifr, &vreq) != -1)
- vlan_set(s, &ifr);
+ if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) {
+ vreq.vlr_proto = params.vlr_proto;
+ memcpy(&params, &vreq, sizeof(params));
+ vlan_set(ctx->io_s, &ifr);
+ }
}
-static
-DECL_CMD_FUNC(setvlanpcp, val, d)
+static void
+setvlanpcp(if_ctx *ctx, const char *val, int dummy __unused)
{
u_long ul;
char *endp;
+ struct ifreq ifr = {};
ul = strtoul(val, &endp, 0);
if (*endp != '\0')
@@ -249,25 +249,23 @@ DECL_CMD_FUNC(setvlanpcp, val, d)
if (ul > 7)
errx(1, "value for vlanpcp out of range");
ifr.ifr_vlan_pcp = ul;
- if (ioctl(s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
+ if (ioctl_ctx_ifr(ctx, SIOCSVLANPCP, &ifr) == -1)
err(1, "SIOCSVLANPCP");
}
-static
-DECL_CMD_FUNC(unsetvlandev, val, d)
+static void
+unsetvlandev(if_ctx *ctx, const char *val __unused, int dummy __unused)
{
- struct vlanreq vreq;
-
- bzero((char *)&vreq, sizeof(struct vlanreq));
- ifr.ifr_data = (caddr_t)&vreq;
+ struct vlanreq vreq = {};
+ struct ifreq ifr = { .ifr_data = (caddr_t)&vreq };
- if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
+ if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &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)
+ if (ioctl_ctx(ctx, SIOCSETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCSETVLAN");
}
@@ -283,15 +281,15 @@ static struct cmd vlan_cmds[] = {
/* XXX For compatibility. Should become DEF_CMD() some day. */
DEF_CMD_OPTARG("-vlandev", unsetvlandev),
DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap),
- DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap),
+ DEF_CMD("-vlanmtu", IFCAP_VLAN_MTU, clearifcap),
DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap),
- DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap),
+ DEF_CMD("-vlanhwtag", IFCAP_VLAN_HWTAGGING, clearifcap),
DEF_CMD("vlanhwfilter", IFCAP_VLAN_HWFILTER, setifcap),
- DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER, setifcap),
- DEF_CMD("-vlanhwtso", -IFCAP_VLAN_HWTSO, setifcap),
+ DEF_CMD("-vlanhwfilter", IFCAP_VLAN_HWFILTER, clearifcap),
DEF_CMD("vlanhwtso", IFCAP_VLAN_HWTSO, setifcap),
+ DEF_CMD("-vlanhwtso", IFCAP_VLAN_HWTSO, clearifcap),
DEF_CMD("vlanhwcsum", IFCAP_VLAN_HWCSUM, setifcap),
- DEF_CMD("-vlanhwcsum", -IFCAP_VLAN_HWCSUM, setifcap),
+ DEF_CMD("-vlanhwcsum", IFCAP_VLAN_HWCSUM, clearifcap),
};
static struct afswtch af_vlan = {
.af_name = "af_vlan",