1 /* 2 * Copyright (c) 1999 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD: src/sbin/ifconfig/ifvlan.c,v 1.7.2.4 2006/02/09 10:48:43 yar Exp $ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/ioctl.h> 37 #include <sys/socket.h> 38 #include <sys/sockio.h> 39 40 #include <net/if.h> 41 #include <net/if_var.h> 42 #include <net/route.h> 43 #include <net/ethernet.h> 44 #include <net/vlan/if_vlan_var.h> 45 46 #include <ctype.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <err.h> 52 #include <errno.h> 53 54 #include "ifconfig.h" 55 56 static struct vlanreq __vreq; 57 static int __have_dev = 0; 58 static int __have_tag = 0; 59 60 static void 61 vlan_status(int s) 62 { 63 struct vlanreq vreq; 64 struct ifreq ifr; 65 66 memset(&ifr, 0, sizeof(ifr)); 67 memset(&vreq, 0, sizeof(vreq)); 68 69 strlcpy(ifr.ifr_name, IfName, sizeof(ifr.ifr_name)); 70 ifr.ifr_data = &vreq; 71 72 if (ioctl(s, SIOCGETVLAN, &ifr) == -1) 73 return; 74 75 printf("\tvlan: %d parent interface: %s\n", vreq.vlr_tag, 76 vreq.vlr_parent[0] == '\0' ? "<none>" : vreq.vlr_parent); 77 } 78 79 static void 80 setvlantag(const char *val, int d __unused, int s __unused, 81 const struct afswtch *afp __unused) 82 { 83 char *endp; 84 u_long ul; 85 86 ul = strtoul(val, &endp, 0); 87 if (*endp != '\0') 88 errx(1, "invalid value for vlan"); 89 __vreq.vlr_tag = ul; 90 /* check if the value can be represented in vlr_tag */ 91 if (__vreq.vlr_tag != ul) 92 errx(1, "value for vlan out of range"); 93 /* the kernel will do more specific checks on vlr_tag */ 94 __have_tag = 1; 95 } 96 97 static void 98 setvlandev(const char *val, int d __unused, int s __unused, 99 const struct afswtch *afp __unused) 100 { 101 strlcpy(__vreq.vlr_parent, val, sizeof(__vreq.vlr_parent)); 102 __have_dev = 1; 103 } 104 105 static void 106 unsetvlandev(const char *val, int d __unused, int s, 107 const struct afswtch *afp __unused) 108 { 109 struct ifreq ifr; 110 111 if (val != NULL) 112 warnx("argument to -vlandev is useless and hence deprecated"); 113 114 memset(&ifr, 0, sizeof(ifr)); 115 memset(&__vreq, 0, sizeof(__vreq)); 116 117 strlcpy(ifr.ifr_name, IfName, sizeof(ifr.ifr_name)); 118 ifr.ifr_data = &__vreq; 119 120 #if 0 /* this code will be of use when we can alter vlan or vlandev only */ 121 if (ioctl(s, SIOCGETVLAN, &ifr) == -1) 122 err(1, "SIOCGETVLAN"); 123 124 memset(&__vreq.vlr_parent, 0, sizeof(__vreq.vlr_parent)); 125 __vreq.vlr_tag = 0; /* XXX clear parent only (no kernel support now) */ 126 #endif 127 128 if (ioctl(s, SIOCSETVLAN, &ifr) == -1) 129 err(1, "SIOCSETVLAN"); 130 __have_dev = __have_tag = 0; 131 } 132 133 static void 134 vlan_cb(int s, void *arg __unused) 135 { 136 struct ifreq ifr; 137 138 if (__have_tag ^ __have_dev) 139 errx(1, "both vlan and vlandev must be specified"); 140 141 if (__have_tag && __have_dev) { 142 memset(&ifr, 0, sizeof(ifr)); 143 strlcpy(ifr.ifr_name, IfName, sizeof(ifr.ifr_name)); 144 ifr.ifr_data = &__vreq; 145 if (ioctl(s, SIOCSETVLAN, &ifr) == -1) 146 err(1, "SIOCSETVLAN"); 147 } 148 } 149 150 static struct cmd vlan_cmds[] = { 151 DEF_CMD_ARG("vlan", setvlantag), 152 DEF_CMD_ARG("vlandev", setvlandev), 153 /* XXX For compatibility. Should become DEF_CMD() some day. */ 154 DEF_CMD_OPTARG("-vlandev", unsetvlandev), 155 DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap), 156 DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap), 157 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap), 158 DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap), 159 }; 160 static struct afswtch af_vlan = { 161 .af_name = "af_vlan", 162 .af_af = AF_UNSPEC, 163 .af_other_status = vlan_status, 164 }; 165 166 __constructor(124) 167 static void 168 vlan_ctor(void) 169 { 170 size_t i; 171 172 for (i = 0; i < nitems(vlan_cmds); i++) 173 cmd_register(&vlan_cmds[i]); 174 175 af_register(&af_vlan); 176 callback_register(vlan_cb, NULL); 177 } 178