vlan.c revision 1.2
1/* $NetBSD: vlan.c,v 1.2 2005/03/19 03:56:06 thorpej 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.2 2005/03/19 03:56:06 thorpej 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 <string.h> 47#include <stdlib.h> 48#include <stdio.h> 49 50#include "extern.h" 51#include "vlan.h" 52 53static u_int vlan_tag = (u_int)-1; 54 55static int 56checkifname(const char *name) 57{ 58 59 return strncmp(name, "vlan", 4) != 0 || 60 !isdigit((unsigned char)name[4]); 61} 62 63static void 64assertifname(const char *name) 65{ 66 67 if (checkifname(name)) { 68 errx(EXIT_FAILURE, "valid only with vlan(4) interfaces"); 69 } 70} 71 72void 73setvlan(const char *val, int d) 74{ 75 struct vlanreq vlr; 76 77 assertifname(ifr.ifr_name); 78 79 vlan_tag = atoi(val); 80 81 memset(&vlr, 0, sizeof(vlr)); 82 ifr.ifr_data = (void *)&vlr; 83 if (ioctl(s, SIOCGETVLAN, &ifr) == -1) 84 err(EXIT_FAILURE, "SIOCGETVLAN"); 85 86 vlr.vlr_tag = vlan_tag; 87 88 if (ioctl(s, SIOCSETVLAN, &ifr) == -1) 89 err(EXIT_FAILURE, "SIOCSETVLAN"); 90} 91 92void 93setvlanif(const char *val, int d) 94{ 95 struct vlanreq vlr; 96 97 assertifname(ifr.ifr_name); 98 99 if (vlan_tag == (u_int)-1) 100 errx(EXIT_FAILURE, 101 "must specify both ``vlan'' and ``vlanif''"); 102 103 memset(&vlr, 0, sizeof(vlr)); 104 ifr.ifr_data = (void *)&vlr; 105 106 if (ioctl(s, SIOCGETVLAN, &ifr) == -1) 107 err(EXIT_FAILURE, "SIOCGETVLAN"); 108 109 strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent)); 110 vlr.vlr_tag = vlan_tag; 111 112 if (ioctl(s, SIOCSETVLAN, &ifr) == -1) 113 err(EXIT_FAILURE, "SIOCSETVLAN"); 114} 115 116void 117unsetvlanif(const char *val, int d) 118{ 119 struct vlanreq vlr; 120 121 assertifname(ifr.ifr_name); 122 123 memset(&vlr, 0, sizeof(vlr)); 124 ifr.ifr_data = (void *)&vlr; 125 126 if (ioctl(s, SIOCGETVLAN, &ifr) == -1) 127 err(EXIT_FAILURE, "SIOCGETVLAN"); 128 129 vlr.vlr_parent[0] = '\0'; 130 vlr.vlr_tag = 0; 131 132 if (ioctl(s, SIOCSETVLAN, &ifr) == -1) 133 err(EXIT_FAILURE, "SIOCSETVLAN"); 134} 135 136void 137vlan_status(void) 138{ 139 struct vlanreq vlr; 140 141 if (checkifname(ifr.ifr_name)) 142 return; 143 144 memset(&vlr, 0, sizeof(vlr)); 145 ifr.ifr_data = (void *)&vlr; 146 147 if (ioctl(s, SIOCGETVLAN, &ifr) == -1) 148 return; 149 150 if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0') 151 printf("\tvlan: %d parent: %s\n", 152 vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ? 153 "<none>" : vlr.vlr_parent); 154} 155