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