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