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