ifvlan.c revision 182414
1185380Ssam/* 2185380Ssam * Copyright (c) 1999 3185380Ssam * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4185380Ssam * 5185380Ssam * Redistribution and use in source and binary forms, with or without 6185380Ssam * modification, are permitted provided that the following conditions 7185380Ssam * are met: 8185380Ssam * 1. Redistributions of source code must retain the above copyright 9185380Ssam * notice, this list of conditions and the following disclaimer. 10185380Ssam * 2. Redistributions in binary form must reproduce the above copyright 11185380Ssam * notice, this list of conditions and the following disclaimer in the 12185380Ssam * documentation and/or other materials provided with the distribution. 13185380Ssam * 3. All advertising materials mentioning features or use of this software 14185380Ssam * must display the following acknowledgement: 15185380Ssam * This product includes software developed by Bill Paul. 16185380Ssam * 4. Neither the name of the author nor the names of any co-contributors 17203158Srpaulo * may be used to endorse or promote products derived from this software 18185380Ssam * without specific prior written permission. 19185380Ssam * 20185380Ssam * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21185380Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22185380Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23185380Ssam * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24185380Ssam * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25185380Ssam * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26185380Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27185380Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28185380Ssam * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29185380Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30185380Ssam * THE POSSIBILITY OF SUCH DAMAGE. 31185380Ssam */ 32185380Ssam 33185380Ssam#include <sys/param.h> 34185380Ssam#include <sys/ioctl.h> 35185380Ssam#include <sys/socket.h> 36185380Ssam#include <sys/sockio.h> 37185380Ssam 38185380Ssam#include <stdlib.h> 39185380Ssam#include <unistd.h> 40185380Ssam 41185380Ssam#include <net/ethernet.h> 42185380Ssam#include <net/if.h> 43185380Ssam#include <net/if_var.h> 44185380Ssam#include <net/if_vlan_var.h> 45185380Ssam#include <net/route.h> 46185380Ssam 47185380Ssam#include <ctype.h> 48185380Ssam#include <stdio.h> 49185380Ssam#include <string.h> 50185380Ssam#include <stdlib.h> 51185380Ssam#include <unistd.h> 52185380Ssam#include <err.h> 53185380Ssam#include <errno.h> 54185380Ssam 55185380Ssam#include "ifconfig.h" 56185380Ssam 57185380Ssam#ifndef lint 58185380Ssamstatic const char rcsid[] = 59185380Ssam "$FreeBSD: head/sbin/ifconfig/ifvlan.c 182414 2008-08-28 22:13:44Z jfv $"; 60185380Ssam#endif 61185380Ssam 62185380Ssam#define NOTAG ((u_short) -1) 63185380Ssam 64185380Ssamstatic struct vlanreq params = { 65185380Ssam .vlr_tag = NOTAG, 66185380Ssam}; 67185380Ssam 68185380Ssamstatic int 69185380Ssamgetvlan(int s, struct ifreq *ifr, struct vlanreq *vreq) 70185380Ssam{ 71185380Ssam bzero((char *)vreq, sizeof(*vreq)); 72185380Ssam ifr->ifr_data = (caddr_t)vreq; 73185380Ssam 74185380Ssam return ioctl(s, SIOCGETVLAN, (caddr_t)ifr); 75185380Ssam} 76185380Ssam 77185380Ssamstatic void 78185380Ssamvlan_status(int s) 79185380Ssam{ 80185380Ssam struct vlanreq vreq; 81185380Ssam 82185380Ssam if (getvlan(s, &ifr, &vreq) != -1) 83185380Ssam printf("\tvlan: %d parent interface: %s\n", 84185380Ssam vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ? 85185380Ssam "<none>" : vreq.vlr_parent); 86185380Ssam} 87185380Ssam 88185380Ssamstatic void 89185380Ssamvlan_create(int s, struct ifreq *ifr) 90185380Ssam{ 91185380Ssam if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') { 92185380Ssam /* 93185380Ssam * One or both parameters were specified, make sure both. 94185380Ssam */ 95185380Ssam if (params.vlr_tag == NOTAG) 96185380Ssam errx(1, "must specify a tag for vlan create"); 97185380Ssam if (params.vlr_parent[0] == '\0') 98185380Ssam errx(1, "must specify a parent device for vlan create"); 99185380Ssam ifr->ifr_data = (caddr_t) ¶ms; 100185380Ssam } 101185380Ssam if (ioctl(s, SIOCIFCREATE2, ifr) < 0) 102185380Ssam err(1, "SIOCIFCREATE2"); 103185380Ssam} 104185380Ssam 105185380Ssamstatic void 106185380Ssamvlan_cb(int s, void *arg) 107185380Ssam{ 108185380Ssam if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0')) 109185380Ssam errx(1, "both vlan and vlandev must be specified"); 110185380Ssam} 111185380Ssam 112185380Ssamstatic void 113185380Ssamvlan_set(int s, struct ifreq *ifr) 114185380Ssam{ 115185380Ssam if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') { 116185380Ssam ifr->ifr_data = (caddr_t) ¶ms; 117 if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1) 118 err(1, "SIOCSETVLAN"); 119 } 120} 121 122static 123DECL_CMD_FUNC(setvlantag, val, d) 124{ 125 struct vlanreq vreq; 126 u_long ul; 127 char *endp; 128 129 ul = strtoul(val, &endp, 0); 130 if (*endp != '\0') 131 errx(1, "invalid value for vlan"); 132 params.vlr_tag = ul; 133 /* check if the value can be represented in vlr_tag */ 134 if (params.vlr_tag != ul) 135 errx(1, "value for vlan out of range"); 136 137 if (getvlan(s, &ifr, &vreq) != -1) 138 vlan_set(s, &ifr); 139 else 140 clone_setcallback(vlan_create); 141} 142 143static 144DECL_CMD_FUNC(setvlandev, val, d) 145{ 146 struct vlanreq vreq; 147 148 strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent)); 149 150 if (getvlan(s, &ifr, &vreq) != -1) 151 vlan_set(s, &ifr); 152 else 153 clone_setcallback(vlan_create); 154} 155 156static 157DECL_CMD_FUNC(unsetvlandev, val, d) 158{ 159 struct vlanreq vreq; 160 161 bzero((char *)&vreq, sizeof(struct vlanreq)); 162 ifr.ifr_data = (caddr_t)&vreq; 163 164 if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1) 165 err(1, "SIOCGETVLAN"); 166 167 bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent)); 168 vreq.vlr_tag = 0; 169 170 if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1) 171 err(1, "SIOCSETVLAN"); 172} 173 174static struct cmd vlan_cmds[] = { 175 DEF_CLONE_CMD_ARG("vlan", setvlantag), 176 DEF_CLONE_CMD_ARG("vlandev", setvlandev), 177 /* XXX For compatibility. Should become DEF_CMD() some day. */ 178 DEF_CMD_OPTARG("-vlandev", unsetvlandev), 179 DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap), 180 DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap), 181 DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap), 182 DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap), 183 DEF_CMD("vlanhwfilter", IFCAP_VLAN_HWFILTER, setifcap), 184 DEF_CMD("-vlanhwfilter", -IFCAP_VLAN_HWFILTER, setifcap), 185}; 186static struct afswtch af_vlan = { 187 .af_name = "af_vlan", 188 .af_af = AF_UNSPEC, 189 .af_other_status = vlan_status, 190}; 191 192static __constructor void 193vlan_ctor(void) 194{ 195#define N(a) (sizeof(a) / sizeof(a[0])) 196 int i; 197 198 for (i = 0; i < N(vlan_cmds); i++) 199 cmd_register(&vlan_cmds[i]); 200 af_register(&af_vlan); 201 callback_register(vlan_cb, NULL); 202#undef N 203} 204