2008-07-16 01:27:58 +04:00
|
|
|
/* $NetBSD: vlan.c,v 1.12 2008/07/15 21:27:58 dyoung Exp $ */
|
2005-03-19 06:53:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS OR CONTRIBUTORS 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/cdefs.h>
|
|
|
|
#ifndef lint
|
2008-07-16 01:27:58 +04:00
|
|
|
__RCSID("$NetBSD: vlan.c,v 1.12 2008/07/15 21:27:58 dyoung Exp $");
|
2005-03-19 06:53:55 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#include <net/if_vlanvar.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2008-05-06 08:33:42 +04:00
|
|
|
#include <errno.h>
|
2005-03-19 06:53:55 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2008-05-06 08:33:42 +04:00
|
|
|
#include <util.h>
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-06 08:33:42 +04:00
|
|
|
#include "env.h"
|
2005-03-19 06:56:06 +03:00
|
|
|
#include "extern.h"
|
2008-05-08 03:55:06 +04:00
|
|
|
#include "util.h"
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
|
|
|
|
static status_func_t status;
|
2008-07-16 00:56:13 +04:00
|
|
|
static usage_func_t usage;
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
static cmdloop_branch_t branch;
|
|
|
|
|
|
|
|
static void vlan_constructor(void) __attribute__((constructor));
|
|
|
|
static void vlan_status(prop_dictionary_t, prop_dictionary_t);
|
|
|
|
|
|
|
|
static int setvlan(prop_dictionary_t, prop_dictionary_t);
|
|
|
|
static int setvlanif(prop_dictionary_t, prop_dictionary_t);
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-08 00:11:15 +04:00
|
|
|
struct pinteger vlantag = PINTEGER_INITIALIZER1(&vlantag, "VLAN tag",
|
|
|
|
0, USHRT_MAX, 10, setvlan, "vlantag", &command_root.pb_parser);
|
2008-05-06 08:33:42 +04:00
|
|
|
|
|
|
|
struct piface vlanif = PIFACE_INITIALIZER(&vlanif, "vlanif", setvlanif,
|
|
|
|
"vlanif", &command_root.pb_parser);
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-08 00:11:15 +04:00
|
|
|
static const struct kwinst vlankw[] = {
|
|
|
|
{.k_word = "vlan", .k_nextparser = &vlantag.pi_parser}
|
2008-05-17 00:53:35 +04:00
|
|
|
, {.k_word = "vlanif", .k_act = "vlantag",
|
2008-05-08 00:11:15 +04:00
|
|
|
.k_nextparser = &vlanif.pif_parser}
|
|
|
|
, {.k_word = "-vlanif", .k_key = "vlanif", .k_type = KW_T_STR,
|
|
|
|
.k_str = "", .k_exec = setvlanif}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pkw vlan = PKW_INITIALIZER(&vlan, "vlan", NULL, NULL,
|
|
|
|
vlankw, __arraycount(vlankw), NULL);
|
|
|
|
|
2005-03-19 06:53:55 +03:00
|
|
|
static int
|
2008-05-08 03:55:06 +04:00
|
|
|
checkifname(prop_dictionary_t env)
|
2005-03-19 06:53:55 +03:00
|
|
|
{
|
2008-05-08 03:55:06 +04:00
|
|
|
const char *ifname;
|
|
|
|
|
|
|
|
if ((ifname = getifname(env)) == NULL)
|
|
|
|
return 1;
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2005-03-19 20:31:48 +03:00
|
|
|
return strncmp(ifname, "vlan", 4) != 0 ||
|
|
|
|
!isdigit((unsigned char)ifname[4]);
|
2005-03-19 06:53:55 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 08:33:42 +04:00
|
|
|
static int
|
2008-05-08 03:55:06 +04:00
|
|
|
getvlan(prop_dictionary_t env, struct vlanreq *vlr, bool quiet)
|
2005-03-19 06:53:55 +03:00
|
|
|
{
|
2008-05-06 08:33:42 +04:00
|
|
|
memset(vlr, 0, sizeof(*vlr));
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (checkifname(env)) {
|
2008-05-06 08:33:42 +04:00
|
|
|
if (quiet)
|
|
|
|
return -1;
|
2005-03-19 06:53:55 +03:00
|
|
|
errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
|
|
|
|
}
|
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (indirect_ioctl(env, SIOCGETVLAN, vlr) == -1)
|
2008-05-06 08:33:42 +04:00
|
|
|
return -1;
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
return 0;
|
2005-03-19 06:53:55 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 08:33:42 +04:00
|
|
|
int
|
2008-07-16 01:27:58 +04:00
|
|
|
setvlan(prop_dictionary_t env, prop_dictionary_t oenv)
|
2005-03-19 06:53:55 +03:00
|
|
|
{
|
|
|
|
struct vlanreq vlr;
|
2008-05-06 22:58:47 +04:00
|
|
|
int64_t tag;
|
2008-05-06 08:33:42 +04:00
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (getvlan(env, &vlr, false) == -1)
|
2008-05-06 08:33:42 +04:00
|
|
|
err(EXIT_FAILURE, "%s: getvlan", __func__);
|
|
|
|
|
2008-05-06 22:58:47 +04:00
|
|
|
if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
|
2008-05-06 08:33:42 +04:00
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-06 08:33:42 +04:00
|
|
|
vlr.vlr_tag = tag;
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
|
2005-03-19 06:53:55 +03:00
|
|
|
err(EXIT_FAILURE, "SIOCSETVLAN");
|
2008-05-06 08:33:42 +04:00
|
|
|
return 0;
|
2005-03-19 06:53:55 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 08:33:42 +04:00
|
|
|
int
|
2008-07-16 01:27:58 +04:00
|
|
|
setvlanif(prop_dictionary_t env, prop_dictionary_t oenv)
|
2005-03-19 06:53:55 +03:00
|
|
|
{
|
|
|
|
struct vlanreq vlr;
|
2008-05-06 22:58:47 +04:00
|
|
|
const char *parent;
|
|
|
|
int64_t tag;
|
2008-05-06 08:33:42 +04:00
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (getvlan(env, &vlr, false) == -1)
|
2008-05-06 08:33:42 +04:00
|
|
|
err(EXIT_FAILURE, "%s: getsock", __func__);
|
|
|
|
|
2008-05-06 22:58:47 +04:00
|
|
|
if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
|
2008-05-06 08:33:42 +04:00
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-06 22:58:47 +04:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(env, "vlanif", &parent)) {
|
2008-05-06 08:33:42 +04:00
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2008-05-06 22:58:47 +04:00
|
|
|
strlcpy(vlr.vlr_parent, parent, sizeof(vlr.vlr_parent));
|
|
|
|
if (strcmp(parent, "") != 0)
|
|
|
|
vlr.vlr_tag = (unsigned short)tag;
|
2005-03-19 06:53:55 +03:00
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
|
2005-03-19 06:53:55 +03:00
|
|
|
err(EXIT_FAILURE, "SIOCSETVLAN");
|
2008-05-06 08:33:42 +04:00
|
|
|
return 0;
|
2005-03-19 06:53:55 +03:00
|
|
|
}
|
|
|
|
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
static void
|
2008-05-06 21:29:04 +04:00
|
|
|
vlan_status(prop_dictionary_t env, prop_dictionary_t oenv)
|
2005-03-19 06:53:55 +03:00
|
|
|
{
|
|
|
|
struct vlanreq vlr;
|
|
|
|
|
2008-05-08 03:55:06 +04:00
|
|
|
if (getvlan(env, &vlr, true) == -1)
|
2005-03-19 06:53:55 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
|
|
|
|
printf("\tvlan: %d parent: %s\n",
|
|
|
|
vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
|
|
|
|
"<none>" : vlr.vlr_parent);
|
|
|
|
}
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
|
2008-07-16 00:56:13 +04:00
|
|
|
static void
|
|
|
|
vlan_usage(prop_dictionary_t env)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\t[ vlan n vlanif i ]\n");
|
|
|
|
}
|
|
|
|
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
static void
|
|
|
|
vlan_constructor(void)
|
|
|
|
{
|
|
|
|
cmdloop_branch_init(&branch, &vlan.pk_parser);
|
|
|
|
register_cmdloop_branch(&branch);
|
|
|
|
status_func_init(&status, vlan_status);
|
2008-07-16 00:56:13 +04:00
|
|
|
usage_func_init(&usage, vlan_usage);
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
register_status(&status);
|
2008-07-16 00:56:13 +04:00
|
|
|
register_usage(&usage);
|
Let us add/remove features from ifconfig, such as support for
various address families (inet, inet6, iso, atalk) and protocols
(802.11, 802.3ad, CARP), simply by trimming the list of sources in
the Makefile. This helps one customize ifconfig for an embedded
device or for install media, and it eliminates a lot of grotty
#ifdef'age. Now, the ifconfig syntax and semantics are finalized
at run-time using the constructor routines in each address-family/protocol
module.
(In principle, ifconfig could load virtually all of its syntax from
shared objects.)
Extract a lot of common code into subroutines, in order to shrink
the ifconfig binary a bit. Make all of the address families share
code for address addition/replacement/removal, and delete "legacy"
code for manipulating addresses. That may have broken atalk and
iso, despite my best efforts.
Extract an include file, Makefile.inc, containing the make-fu that
both ifconfig and x_ifconfig share.
Sprinkle static. Change some int's to bool's. Constify.
Add RCS Ids to carp.c and env.c. Move media code to a new file,
media.c. Delete several unneeded header files.
Set, reset, and display the IEEE 802.11 attribute, 'dot11RTSThreshold'.
Bug fix: do not require both a interface address and a destination
address for point-to-point interfaces, but accept a interface
address by itself.
2008-07-02 11:44:13 +04:00
|
|
|
}
|