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