Home | History | Annotate | Line # | Download | only in ifconfig
vlan.c revision 1.4
      1  1.4   dyoung /*	$NetBSD: vlan.c,v 1.4 2008/05/06 04:33:42 dyoung Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1983, 1993
      5  1.1  thorpej  *      The Regents of the University of California.  All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     17  1.1  thorpej  *    without specific prior written permission.
     18  1.1  thorpej  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  thorpej  * SUCH DAMAGE.
     30  1.1  thorpej  */
     31  1.1  thorpej 
     32  1.1  thorpej #include <sys/cdefs.h>
     33  1.1  thorpej #ifndef lint
     34  1.4   dyoung __RCSID("$NetBSD: vlan.c,v 1.4 2008/05/06 04:33:42 dyoung Exp $");
     35  1.1  thorpej #endif /* not lint */
     36  1.1  thorpej 
     37  1.1  thorpej #include <sys/param.h>
     38  1.1  thorpej #include <sys/ioctl.h>
     39  1.1  thorpej 
     40  1.1  thorpej #include <net/if.h>
     41  1.1  thorpej #include <net/if_ether.h>
     42  1.1  thorpej #include <net/if_vlanvar.h>
     43  1.1  thorpej 
     44  1.1  thorpej #include <ctype.h>
     45  1.1  thorpej #include <err.h>
     46  1.4   dyoung #include <errno.h>
     47  1.1  thorpej #include <string.h>
     48  1.1  thorpej #include <stdlib.h>
     49  1.1  thorpej #include <stdio.h>
     50  1.4   dyoung #include <util.h>
     51  1.1  thorpej 
     52  1.4   dyoung #include "env.h"
     53  1.2  thorpej #include "extern.h"
     54  1.1  thorpej #include "vlan.h"
     55  1.1  thorpej 
     56  1.4   dyoung struct pinteger vlan = PINTEGER_INITIALIZER1(&vlan, "vlan", 0, USHRT_MAX, 10,
     57  1.4   dyoung     setvlan, "vlan", &command_root.pb_parser);
     58  1.4   dyoung 
     59  1.4   dyoung struct piface vlanif = PIFACE_INITIALIZER(&vlanif, "vlanif", setvlanif,
     60  1.4   dyoung     "vlanif", &command_root.pb_parser);
     61  1.1  thorpej 
     62  1.1  thorpej static int
     63  1.3  thorpej checkifname(const char *ifname)
     64  1.1  thorpej {
     65  1.1  thorpej 
     66  1.3  thorpej 	return strncmp(ifname, "vlan", 4) != 0 ||
     67  1.3  thorpej 	    !isdigit((unsigned char)ifname[4]);
     68  1.1  thorpej }
     69  1.1  thorpej 
     70  1.4   dyoung static int
     71  1.4   dyoung getvlan(prop_dictionary_t env, struct ifreq *ifr, struct vlanreq *vlr,
     72  1.4   dyoung     bool quiet)
     73  1.1  thorpej {
     74  1.4   dyoung 	int s;
     75  1.4   dyoung 	const char *ifname;
     76  1.4   dyoung 
     77  1.4   dyoung 	if ((s = getsock(AF_UNSPEC)) == -1)
     78  1.4   dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
     79  1.4   dyoung 	if ((ifname = getifname(env)) == NULL)
     80  1.4   dyoung 		err(EXIT_FAILURE, "%s: getifname", __func__);
     81  1.4   dyoung 
     82  1.4   dyoung 	memset(ifr, 0, sizeof(*ifr));
     83  1.4   dyoung 	memset(vlr, 0, sizeof(*vlr));
     84  1.1  thorpej 
     85  1.3  thorpej 	if (checkifname(ifname)) {
     86  1.4   dyoung 		if (quiet)
     87  1.4   dyoung 			return -1;
     88  1.1  thorpej 		errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
     89  1.1  thorpej 	}
     90  1.1  thorpej 
     91  1.4   dyoung 	estrlcpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
     92  1.4   dyoung 	ifr->ifr_data = vlr;
     93  1.1  thorpej 
     94  1.4   dyoung 	if (ioctl(s, SIOCGETVLAN, ifr) == -1)
     95  1.4   dyoung 		return -1;
     96  1.1  thorpej 
     97  1.4   dyoung 	return s;
     98  1.1  thorpej }
     99  1.1  thorpej 
    100  1.4   dyoung int
    101  1.4   dyoung setvlan(prop_dictionary_t env, prop_dictionary_t xenv)
    102  1.1  thorpej {
    103  1.1  thorpej 	struct vlanreq vlr;
    104  1.4   dyoung 	int s;
    105  1.4   dyoung 	prop_number_t num;
    106  1.4   dyoung 	unsigned short tag;
    107  1.4   dyoung 	struct ifreq ifr;
    108  1.4   dyoung 
    109  1.4   dyoung 	if ((s = getvlan(env, &ifr, &vlr, false)) == -1)
    110  1.4   dyoung 		err(EXIT_FAILURE, "%s: getvlan", __func__);
    111  1.4   dyoung 
    112  1.4   dyoung 	num = (prop_number_t)prop_dictionary_get(env, "vlantag");
    113  1.4   dyoung 	if (num == NULL) {
    114  1.4   dyoung 		errno = ENOENT;
    115  1.4   dyoung 		return -1;
    116  1.4   dyoung 	}
    117  1.4   dyoung 	tag = (unsigned short)prop_number_integer_value(num);
    118  1.1  thorpej 
    119  1.4   dyoung 	vlr.vlr_tag = tag;
    120  1.1  thorpej 
    121  1.1  thorpej 	if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
    122  1.1  thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    123  1.4   dyoung 	return 0;
    124  1.1  thorpej }
    125  1.1  thorpej 
    126  1.4   dyoung int
    127  1.4   dyoung setvlanif(prop_dictionary_t env, prop_dictionary_t xenv)
    128  1.1  thorpej {
    129  1.1  thorpej 	struct vlanreq vlr;
    130  1.4   dyoung 	int s;
    131  1.4   dyoung 	prop_number_t num;
    132  1.4   dyoung 	prop_string_t str;
    133  1.4   dyoung 	unsigned short tag;
    134  1.4   dyoung 	struct ifreq ifr;
    135  1.4   dyoung 
    136  1.4   dyoung 	if ((s = getvlan(env, &ifr, &vlr, false)) == -1)
    137  1.4   dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
    138  1.4   dyoung 
    139  1.4   dyoung 	num = (prop_number_t)prop_dictionary_get(env, "vlantag");
    140  1.4   dyoung 	if (num == NULL) {
    141  1.4   dyoung 		errno = ENOENT;
    142  1.4   dyoung 		return -1;
    143  1.4   dyoung 	}
    144  1.4   dyoung 	tag = (unsigned short)prop_number_integer_value(num);
    145  1.1  thorpej 
    146  1.4   dyoung 	str = (prop_string_t)prop_dictionary_get(env, "vlanif");
    147  1.4   dyoung 	if (str == NULL) {
    148  1.4   dyoung 		errno = ENOENT;
    149  1.4   dyoung 		return -1;
    150  1.4   dyoung 	}
    151  1.4   dyoung 	strlcpy(vlr.vlr_parent, prop_string_cstring_nocopy(str),
    152  1.4   dyoung 	    sizeof(vlr.vlr_parent));
    153  1.4   dyoung 	if (!prop_string_equals_cstring(str, ""))
    154  1.4   dyoung 		vlr.vlr_tag = tag;
    155  1.1  thorpej 
    156  1.1  thorpej 	if (ioctl(s, SIOCSETVLAN, &ifr) == -1)
    157  1.1  thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    158  1.4   dyoung 	return 0;
    159  1.1  thorpej }
    160  1.1  thorpej 
    161  1.1  thorpej void
    162  1.4   dyoung vlan_status(prop_dictionary_t env)
    163  1.1  thorpej {
    164  1.1  thorpej 	struct vlanreq vlr;
    165  1.4   dyoung 	int s;
    166  1.4   dyoung 	struct ifreq ifr;
    167  1.1  thorpej 
    168  1.4   dyoung 	if ((s = getvlan(env, &ifr, &vlr, true)) == -1)
    169  1.1  thorpej 		return;
    170  1.1  thorpej 
    171  1.1  thorpej 	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
    172  1.1  thorpej 		printf("\tvlan: %d parent: %s\n",
    173  1.1  thorpej 		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
    174  1.1  thorpej 		    "<none>" : vlr.vlr_parent);
    175  1.1  thorpej }
    176