From 0fded9bea274774239644205266251f08c1c21ef Mon Sep 17 00:00:00 2001 From: thorpej Date: Sat, 19 Mar 2005 03:53:55 +0000 Subject: [PATCH] Split VLAN configuration bits out into a separate file. --- sbin/ifconfig/Makefile | 3 +- sbin/ifconfig/ifconfig.c | 106 +------------------------- sbin/ifconfig/vlan.c | 156 +++++++++++++++++++++++++++++++++++++++ sbin/ifconfig/vlan.h | 35 +++++++++ 4 files changed, 196 insertions(+), 104 deletions(-) create mode 100644 sbin/ifconfig/vlan.c create mode 100644 sbin/ifconfig/vlan.h diff --git a/sbin/ifconfig/Makefile b/sbin/ifconfig/Makefile index fe20173a4ce7..280915ce98a2 100644 --- a/sbin/ifconfig/Makefile +++ b/sbin/ifconfig/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.18 2005/03/18 11:11:51 yamt Exp $ +# $NetBSD: Makefile,v 1.19 2005/03/19 03:53:55 thorpej Exp $ # @(#)Makefile 8.1 (Berkeley) 6/5/93 .include @@ -21,5 +21,6 @@ LDADD+=-lutil SRCS= ifconfig.c SRCS+= agr.c +SRCS+= vlan.c .include diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index ef6ce0c66f02..8ab91d8bed3d 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -1,4 +1,4 @@ -/* $NetBSD: ifconfig.c,v 1.154 2005/03/18 14:30:08 yamt Exp $ */ +/* $NetBSD: ifconfig.c,v 1.155 2005/03/19 03:53:55 thorpej Exp $ */ /*- * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc. @@ -76,7 +76,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ #if 0 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94"; #else -__RCSID("$NetBSD: ifconfig.c,v 1.154 2005/03/18 14:30:08 yamt Exp $"); +__RCSID("$NetBSD: ifconfig.c,v 1.155 2005/03/19 03:53:55 thorpej Exp $"); #endif #endif /* not lint */ @@ -90,7 +90,6 @@ __RCSID("$NetBSD: ifconfig.c,v 1.154 2005/03/18 14:30:08 yamt Exp $"); #include #include #include -#include #include #include #ifdef INET6 @@ -122,6 +121,7 @@ __RCSID("$NetBSD: ifconfig.c,v 1.154 2005/03/18 14:30:08 yamt Exp $"); #include #include "agr.h" +#include "vlan.h" struct ifreq ifr, ridreq; struct ifaliasreq addreq __attribute__((aligned(4))); @@ -150,7 +150,6 @@ int aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag, vflag, zflag; int Lflag; #endif int explicit_prefix = 0; -u_int vlan_tag = (u_int)-1; struct ifcapreq g_ifcr; int g_ifcr_updated; @@ -195,9 +194,6 @@ void setmediainst(const char *, int); void clone_create(const char *, int); void clone_destroy(const char *, int); void fixnsel(struct sockaddr_iso *); -void setvlan(const char *, int); -void setvlanif(const char *, int); -void unsetvlanif(const char *, int); int main(int, char *[]); /* @@ -367,7 +363,6 @@ void iso_getaddr(const char *, int); void ieee80211_statistics(void); void ieee80211_status(void); void tunnel_status(void); -void vlan_status(void); /* Known address families */ struct afswtch { @@ -976,80 +971,6 @@ deletetunnel(const char *vname, int param) err(EXIT_FAILURE, "SIOCDIFPHYADDR"); } -void -setvlan(const char *val, int d) -{ - struct vlanreq vlr; - - if (strncmp(ifr.ifr_name, "vlan", 4) != 0 || - !isdigit((unsigned char)ifr.ifr_name[4])) - errx(EXIT_FAILURE, - "``vlan'' valid only with vlan(4) interfaces"); - - vlan_tag = atoi(val); - - memset(&vlr, 0, sizeof(vlr)); - ifr.ifr_data = (void *)&vlr; - - if (ioctl(s, SIOCGETVLAN, &ifr) == -1) - err(EXIT_FAILURE, "SIOCGETVLAN"); - - vlr.vlr_tag = vlan_tag; - - if (ioctl(s, SIOCSETVLAN, &ifr) == -1) - err(EXIT_FAILURE, "SIOCSETVLAN"); -} - -void -setvlanif(const char *val, int d) -{ - struct vlanreq vlr; - - if (strncmp(ifr.ifr_name, "vlan", 4) != 0 || - !isdigit((unsigned char)ifr.ifr_name[4])) - errx(EXIT_FAILURE, - "``vlanif'' valid only with vlan(4) interfaces"); - - if (vlan_tag == (u_int)-1) - errx(EXIT_FAILURE, - "must specify both ``vlan'' and ``vlanif''"); - - memset(&vlr, 0, sizeof(vlr)); - ifr.ifr_data = (void *)&vlr; - - if (ioctl(s, SIOCGETVLAN, &ifr) == -1) - err(EXIT_FAILURE, "SIOCGETVLAN"); - - strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent)); - vlr.vlr_tag = vlan_tag; - - if (ioctl(s, SIOCSETVLAN, &ifr) == -1) - err(EXIT_FAILURE, "SIOCSETVLAN"); -} - -void -unsetvlanif(const char *val, int d) -{ - struct vlanreq vlr; - - if (strncmp(ifr.ifr_name, "vlan", 4) != 0 || - !isdigit((unsigned char)ifr.ifr_name[4])) - errx(EXIT_FAILURE, - "``vlanif'' valid only with vlan(4) interfaces"); - - memset(&vlr, 0, sizeof(vlr)); - ifr.ifr_data = (void *)&vlr; - - if (ioctl(s, SIOCGETVLAN, &ifr) == -1) - err(EXIT_FAILURE, "SIOCGETVLAN"); - - vlr.vlr_parent[0] = '\0'; - vlr.vlr_tag = 0; - - if (ioctl(s, SIOCSETVLAN, &ifr) == -1) - err(EXIT_FAILURE, "SIOCSETVLAN"); -} - void setifnetmask(const char *addr, int d) { @@ -2138,27 +2059,6 @@ tunnel_status(void) printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr); } -void -vlan_status(void) -{ - struct vlanreq vlr; - - if (strncmp(ifr.ifr_name, "vlan", 4) != 0 || - !isdigit((unsigned char)ifr.ifr_name[4])) - return; - - memset(&vlr, 0, sizeof(vlr)); - ifr.ifr_data = (void *)&vlr; - - if (ioctl(s, SIOCGETVLAN, &ifr) == -1) - return; - - if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0') - printf("\tvlan: %d parent: %s\n", - vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ? - "" : vlr.vlr_parent); -} - void in_alias(struct ifreq *creq) { diff --git a/sbin/ifconfig/vlan.c b/sbin/ifconfig/vlan.c new file mode 100644 index 000000000000..753c95bddd12 --- /dev/null +++ b/sbin/ifconfig/vlan.c @@ -0,0 +1,156 @@ +/* $NetBSD: vlan.c,v 1.1 2005/03/19 03:53:55 thorpej Exp $ */ + +/* + * 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 +#ifndef lint +__RCSID("$NetBSD: vlan.c,v 1.1 2005/03/19 03:53:55 thorpej Exp $"); +#endif /* not lint */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "vlan.h" + +extern struct ifreq ifr; +extern int s; + +static u_int vlan_tag = (u_int)-1; + +static int +checkifname(const char *name) +{ + + return strncmp(name, "vlan", 4) != 0 || + !isdigit((unsigned char)name[4]); +} + +static void +assertifname(const char *name) +{ + + if (checkifname(name)) { + errx(EXIT_FAILURE, "valid only with vlan(4) interfaces"); + } +} + +void +setvlan(const char *val, int d) +{ + struct vlanreq vlr; + + assertifname(ifr.ifr_name); + + vlan_tag = atoi(val); + + memset(&vlr, 0, sizeof(vlr)); + ifr.ifr_data = (void *)&vlr; + if (ioctl(s, SIOCGETVLAN, &ifr) == -1) + err(EXIT_FAILURE, "SIOCGETVLAN"); + + vlr.vlr_tag = vlan_tag; + + if (ioctl(s, SIOCSETVLAN, &ifr) == -1) + err(EXIT_FAILURE, "SIOCSETVLAN"); +} + +void +setvlanif(const char *val, int d) +{ + struct vlanreq vlr; + + assertifname(ifr.ifr_name); + + if (vlan_tag == (u_int)-1) + errx(EXIT_FAILURE, + "must specify both ``vlan'' and ``vlanif''"); + + memset(&vlr, 0, sizeof(vlr)); + ifr.ifr_data = (void *)&vlr; + + if (ioctl(s, SIOCGETVLAN, &ifr) == -1) + err(EXIT_FAILURE, "SIOCGETVLAN"); + + strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent)); + vlr.vlr_tag = vlan_tag; + + if (ioctl(s, SIOCSETVLAN, &ifr) == -1) + err(EXIT_FAILURE, "SIOCSETVLAN"); +} + +void +unsetvlanif(const char *val, int d) +{ + struct vlanreq vlr; + + assertifname(ifr.ifr_name); + + memset(&vlr, 0, sizeof(vlr)); + ifr.ifr_data = (void *)&vlr; + + if (ioctl(s, SIOCGETVLAN, &ifr) == -1) + err(EXIT_FAILURE, "SIOCGETVLAN"); + + vlr.vlr_parent[0] = '\0'; + vlr.vlr_tag = 0; + + if (ioctl(s, SIOCSETVLAN, &ifr) == -1) + err(EXIT_FAILURE, "SIOCSETVLAN"); +} + +void +vlan_status(void) +{ + struct vlanreq vlr; + + if (checkifname(ifr.ifr_name)) + return; + + memset(&vlr, 0, sizeof(vlr)); + ifr.ifr_data = (void *)&vlr; + + if (ioctl(s, SIOCGETVLAN, &ifr) == -1) + return; + + if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0') + printf("\tvlan: %d parent: %s\n", + vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ? + "" : vlr.vlr_parent); +} diff --git a/sbin/ifconfig/vlan.h b/sbin/ifconfig/vlan.h new file mode 100644 index 000000000000..632d912831c6 --- /dev/null +++ b/sbin/ifconfig/vlan.h @@ -0,0 +1,35 @@ +/* $NetBSD: vlan.h,v 1.1 2005/03/19 03:53:55 thorpej Exp $ */ + +/* + * 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. + */ + +void setvlan(const char *, int); +void setvlanif(const char *, int); +void unsetvlanif(const char *, int); +void vlan_status(void);