Home | History | Annotate | Line # | Download | only in ifconfig
vlan.c revision 1.3.20.1
      1  1.3.20.1      mjf /*	$NetBSD: vlan.c,v 1.3.20.1 2008/06/02 13:21:22 mjf 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.3.20.1      mjf __RCSID("$NetBSD: vlan.c,v 1.3.20.1 2008/06/02 13:21:22 mjf 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.3.20.1      mjf #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.3.20.1      mjf #include <util.h>
     51       1.1  thorpej 
     52  1.3.20.1      mjf #include "env.h"
     53       1.2  thorpej #include "extern.h"
     54  1.3.20.1      mjf #include "util.h"
     55       1.1  thorpej #include "vlan.h"
     56       1.1  thorpej 
     57  1.3.20.1      mjf struct pinteger vlantag = PINTEGER_INITIALIZER1(&vlantag, "VLAN tag",
     58  1.3.20.1      mjf     0, USHRT_MAX, 10, setvlan, "vlantag", &command_root.pb_parser);
     59  1.3.20.1      mjf 
     60  1.3.20.1      mjf struct piface vlanif = PIFACE_INITIALIZER(&vlanif, "vlanif", setvlanif,
     61  1.3.20.1      mjf     "vlanif", &command_root.pb_parser);
     62  1.3.20.1      mjf 
     63  1.3.20.1      mjf static const struct kwinst vlankw[] = {
     64  1.3.20.1      mjf 	  {.k_word = "vlan", .k_nextparser = &vlantag.pi_parser}
     65  1.3.20.1      mjf 	, {.k_word = "vlanif", .k_act = "vlantag",
     66  1.3.20.1      mjf 	   .k_nextparser = &vlanif.pif_parser}
     67  1.3.20.1      mjf 	, {.k_word = "-vlanif", .k_key = "vlanif", .k_type = KW_T_STR,
     68  1.3.20.1      mjf 	   .k_str = "", .k_exec = setvlanif}
     69  1.3.20.1      mjf };
     70  1.3.20.1      mjf 
     71  1.3.20.1      mjf struct pkw vlan = PKW_INITIALIZER(&vlan, "vlan", NULL, NULL,
     72  1.3.20.1      mjf     vlankw, __arraycount(vlankw), NULL);
     73       1.1  thorpej 
     74       1.1  thorpej static int
     75  1.3.20.1      mjf checkifname(prop_dictionary_t env)
     76       1.1  thorpej {
     77  1.3.20.1      mjf 	const char *ifname;
     78  1.3.20.1      mjf 
     79  1.3.20.1      mjf 	if ((ifname = getifname(env)) == NULL)
     80  1.3.20.1      mjf 		return 1;
     81       1.1  thorpej 
     82       1.3  thorpej 	return strncmp(ifname, "vlan", 4) != 0 ||
     83       1.3  thorpej 	    !isdigit((unsigned char)ifname[4]);
     84       1.1  thorpej }
     85       1.1  thorpej 
     86  1.3.20.1      mjf static int
     87  1.3.20.1      mjf getvlan(prop_dictionary_t env, struct vlanreq *vlr, bool quiet)
     88       1.1  thorpej {
     89  1.3.20.1      mjf 	memset(vlr, 0, sizeof(*vlr));
     90       1.1  thorpej 
     91  1.3.20.1      mjf 	if (checkifname(env)) {
     92  1.3.20.1      mjf 		if (quiet)
     93  1.3.20.1      mjf 			return -1;
     94       1.1  thorpej 		errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
     95       1.1  thorpej 	}
     96       1.1  thorpej 
     97  1.3.20.1      mjf 	if (indirect_ioctl(env, SIOCGETVLAN, vlr) == -1)
     98  1.3.20.1      mjf 		return -1;
     99       1.1  thorpej 
    100  1.3.20.1      mjf 	return 0;
    101       1.1  thorpej }
    102       1.1  thorpej 
    103  1.3.20.1      mjf int
    104  1.3.20.1      mjf setvlan(prop_dictionary_t env, prop_dictionary_t xenv)
    105       1.1  thorpej {
    106       1.1  thorpej 	struct vlanreq vlr;
    107  1.3.20.1      mjf 	int64_t tag;
    108       1.1  thorpej 
    109  1.3.20.1      mjf 	if (getvlan(env, &vlr, false) == -1)
    110  1.3.20.1      mjf 		err(EXIT_FAILURE, "%s: getvlan", __func__);
    111       1.1  thorpej 
    112  1.3.20.1      mjf 	if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
    113  1.3.20.1      mjf 		errno = ENOENT;
    114  1.3.20.1      mjf 		return -1;
    115  1.3.20.1      mjf 	}
    116       1.1  thorpej 
    117  1.3.20.1      mjf 	vlr.vlr_tag = tag;
    118       1.1  thorpej 
    119  1.3.20.1      mjf 	if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
    120       1.1  thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    121  1.3.20.1      mjf 	return 0;
    122       1.1  thorpej }
    123       1.1  thorpej 
    124  1.3.20.1      mjf int
    125  1.3.20.1      mjf setvlanif(prop_dictionary_t env, prop_dictionary_t xenv)
    126       1.1  thorpej {
    127       1.1  thorpej 	struct vlanreq vlr;
    128  1.3.20.1      mjf 	const char *parent;
    129  1.3.20.1      mjf 	int64_t tag;
    130       1.1  thorpej 
    131  1.3.20.1      mjf 	if (getvlan(env, &vlr, false) == -1)
    132  1.3.20.1      mjf 		err(EXIT_FAILURE, "%s: getsock", __func__);
    133       1.1  thorpej 
    134  1.3.20.1      mjf 	if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
    135  1.3.20.1      mjf 		errno = ENOENT;
    136  1.3.20.1      mjf 		return -1;
    137  1.3.20.1      mjf 	}
    138       1.1  thorpej 
    139  1.3.20.1      mjf 	if (!prop_dictionary_get_cstring_nocopy(env, "vlanif", &parent)) {
    140  1.3.20.1      mjf 		errno = ENOENT;
    141  1.3.20.1      mjf 		return -1;
    142  1.3.20.1      mjf 	}
    143  1.3.20.1      mjf 	strlcpy(vlr.vlr_parent, parent, sizeof(vlr.vlr_parent));
    144  1.3.20.1      mjf 	if (strcmp(parent, "") != 0)
    145  1.3.20.1      mjf 		vlr.vlr_tag = (unsigned short)tag;
    146       1.1  thorpej 
    147  1.3.20.1      mjf 	if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
    148       1.1  thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    149  1.3.20.1      mjf 	return 0;
    150       1.1  thorpej }
    151       1.1  thorpej 
    152       1.1  thorpej void
    153  1.3.20.1      mjf vlan_status(prop_dictionary_t env, prop_dictionary_t oenv)
    154       1.1  thorpej {
    155       1.1  thorpej 	struct vlanreq vlr;
    156       1.1  thorpej 
    157  1.3.20.1      mjf 	if (getvlan(env, &vlr, true) == -1)
    158       1.1  thorpej 		return;
    159       1.1  thorpej 
    160       1.1  thorpej 	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
    161       1.1  thorpej 		printf("\tvlan: %d parent: %s\n",
    162       1.1  thorpej 		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
    163       1.1  thorpej 		    "<none>" : vlr.vlr_parent);
    164       1.1  thorpej }
    165