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