Home | History | Annotate | Line # | Download | only in ifconfig
vlan.c revision 1.14.16.1
      1  1.14.16.1   martin /*	$NetBSD: vlan.c,v 1.14.16.1 2020/04/13 08:03:20 martin 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.14.16.1   martin __RCSID("$NetBSD: vlan.c,v 1.14.16.1 2020/04/13 08:03:20 martin Exp $");
     35        1.1  thorpej #endif /* not lint */
     36        1.1  thorpej 
     37  1.14.16.1   martin #include <sys/param.h>
     38  1.14.16.1   martin #include <sys/ioctl.h>
     39        1.1  thorpej 
     40  1.14.16.1   martin #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.8   dyoung #include "util.h"
     55       1.10   dyoung 
     56       1.10   dyoung static status_func_t status;
     57       1.11   dyoung static usage_func_t usage;
     58       1.10   dyoung static cmdloop_branch_t branch;
     59       1.10   dyoung 
     60       1.10   dyoung static void vlan_constructor(void) __attribute__((constructor));
     61       1.10   dyoung static void vlan_status(prop_dictionary_t, prop_dictionary_t);
     62       1.10   dyoung 
     63       1.10   dyoung static int setvlan(prop_dictionary_t, prop_dictionary_t);
     64       1.10   dyoung static int setvlanif(prop_dictionary_t, prop_dictionary_t);
     65        1.1  thorpej 
     66        1.7   dyoung struct pinteger vlantag = PINTEGER_INITIALIZER1(&vlantag, "VLAN tag",
     67        1.7   dyoung     0, USHRT_MAX, 10, setvlan, "vlantag", &command_root.pb_parser);
     68        1.4   dyoung 
     69        1.4   dyoung struct piface vlanif = PIFACE_INITIALIZER(&vlanif, "vlanif", setvlanif,
     70        1.4   dyoung     "vlanif", &command_root.pb_parser);
     71        1.1  thorpej 
     72        1.7   dyoung static const struct kwinst vlankw[] = {
     73        1.7   dyoung 	  {.k_word = "vlan", .k_nextparser = &vlantag.pi_parser}
     74        1.9   dyoung 	, {.k_word = "vlanif", .k_act = "vlantag",
     75        1.7   dyoung 	   .k_nextparser = &vlanif.pif_parser}
     76        1.7   dyoung 	, {.k_word = "-vlanif", .k_key = "vlanif", .k_type = KW_T_STR,
     77        1.7   dyoung 	   .k_str = "", .k_exec = setvlanif}
     78        1.7   dyoung };
     79        1.7   dyoung 
     80        1.7   dyoung struct pkw vlan = PKW_INITIALIZER(&vlan, "vlan", NULL, NULL,
     81        1.7   dyoung     vlankw, __arraycount(vlankw), NULL);
     82        1.7   dyoung 
     83        1.1  thorpej static int
     84        1.8   dyoung checkifname(prop_dictionary_t env)
     85        1.1  thorpej {
     86        1.8   dyoung 	const char *ifname;
     87        1.8   dyoung 
     88        1.8   dyoung 	if ((ifname = getifname(env)) == NULL)
     89        1.8   dyoung 		return 1;
     90        1.1  thorpej 
     91        1.3  thorpej 	return strncmp(ifname, "vlan", 4) != 0 ||
     92        1.3  thorpej 	    !isdigit((unsigned char)ifname[4]);
     93        1.1  thorpej }
     94        1.1  thorpej 
     95        1.4   dyoung static int
     96        1.8   dyoung getvlan(prop_dictionary_t env, struct vlanreq *vlr, bool quiet)
     97        1.1  thorpej {
     98        1.4   dyoung 	memset(vlr, 0, sizeof(*vlr));
     99        1.1  thorpej 
    100        1.8   dyoung 	if (checkifname(env)) {
    101        1.4   dyoung 		if (quiet)
    102        1.4   dyoung 			return -1;
    103        1.1  thorpej 		errx(EXIT_FAILURE, "valid only with vlan(4) interfaces");
    104        1.1  thorpej 	}
    105        1.1  thorpej 
    106        1.8   dyoung 	if (indirect_ioctl(env, SIOCGETVLAN, vlr) == -1)
    107        1.4   dyoung 		return -1;
    108        1.1  thorpej 
    109        1.8   dyoung 	return 0;
    110        1.1  thorpej }
    111        1.1  thorpej 
    112        1.4   dyoung int
    113       1.12   dyoung setvlan(prop_dictionary_t env, prop_dictionary_t oenv)
    114        1.1  thorpej {
    115        1.1  thorpej 	struct vlanreq vlr;
    116        1.6   dyoung 	int64_t tag;
    117        1.4   dyoung 
    118        1.8   dyoung 	if (getvlan(env, &vlr, false) == -1)
    119        1.4   dyoung 		err(EXIT_FAILURE, "%s: getvlan", __func__);
    120        1.4   dyoung 
    121        1.6   dyoung 	if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
    122        1.4   dyoung 		errno = ENOENT;
    123        1.4   dyoung 		return -1;
    124        1.4   dyoung 	}
    125        1.1  thorpej 
    126        1.4   dyoung 	vlr.vlr_tag = tag;
    127        1.1  thorpej 
    128        1.8   dyoung 	if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -1)
    129        1.1  thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    130        1.4   dyoung 	return 0;
    131        1.1  thorpej }
    132        1.1  thorpej 
    133        1.4   dyoung int
    134       1.12   dyoung setvlanif(prop_dictionary_t env, prop_dictionary_t oenv)
    135        1.1  thorpej {
    136        1.1  thorpej 	struct vlanreq vlr;
    137        1.6   dyoung 	const char *parent;
    138        1.6   dyoung 	int64_t tag;
    139        1.4   dyoung 
    140        1.8   dyoung 	if (getvlan(env, &vlr, false) == -1)
    141        1.4   dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
    142        1.4   dyoung 
    143       1.13   dyoung 	if (!prop_dictionary_get_cstring_nocopy(env, "vlanif", &parent)) {
    144        1.4   dyoung 		errno = ENOENT;
    145        1.4   dyoung 		return -1;
    146        1.4   dyoung 	}
    147       1.13   dyoung 	strlcpy(vlr.vlr_parent, parent, sizeof(vlr.vlr_parent));
    148       1.13   dyoung 	if (strcmp(parent, "") == 0)
    149       1.13   dyoung 		;
    150       1.13   dyoung 	else if (!prop_dictionary_get_int64(env, "vlantag", &tag)) {
    151        1.4   dyoung 		errno = ENOENT;
    152        1.4   dyoung 		return -1;
    153       1.13   dyoung 	} else
    154        1.6   dyoung 		vlr.vlr_tag = (unsigned short)tag;
    155        1.1  thorpej 
    156        1.8   dyoung 	if (indirect_ioctl(env, SIOCSETVLAN, &vlr) == -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.10   dyoung static void
    162        1.5   dyoung vlan_status(prop_dictionary_t env, prop_dictionary_t oenv)
    163        1.1  thorpej {
    164        1.1  thorpej 	struct vlanreq vlr;
    165        1.1  thorpej 
    166        1.8   dyoung 	if (getvlan(env, &vlr, true) == -1)
    167        1.1  thorpej 		return;
    168        1.1  thorpej 
    169        1.1  thorpej 	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
    170        1.1  thorpej 		printf("\tvlan: %d parent: %s\n",
    171        1.1  thorpej 		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
    172        1.1  thorpej 		    "<none>" : vlr.vlr_parent);
    173        1.1  thorpej }
    174       1.10   dyoung 
    175       1.10   dyoung static void
    176       1.11   dyoung vlan_usage(prop_dictionary_t env)
    177       1.11   dyoung {
    178       1.14    ozaki 	fprintf(stderr, "\t[ vlan n vlanif i ] [ -vlanif i ]\n");
    179       1.11   dyoung }
    180       1.11   dyoung 
    181       1.11   dyoung static void
    182       1.10   dyoung vlan_constructor(void)
    183       1.10   dyoung {
    184       1.10   dyoung 	cmdloop_branch_init(&branch, &vlan.pk_parser);
    185       1.10   dyoung 	register_cmdloop_branch(&branch);
    186       1.10   dyoung 	status_func_init(&status, vlan_status);
    187       1.11   dyoung 	usage_func_init(&usage, vlan_usage);
    188       1.10   dyoung 	register_status(&status);
    189       1.11   dyoung 	register_usage(&usage);
    190       1.10   dyoung }
    191