ifvlan.c revision 44764
144764Swpaul/* 244764Swpaul * Copyright (c) 1999 344764Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 444764Swpaul * 544764Swpaul * Redistribution and use in source and binary forms, with or without 644764Swpaul * modification, are permitted provided that the following conditions 744764Swpaul * are met: 844764Swpaul * 1. Redistributions of source code must retain the above copyright 944764Swpaul * notice, this list of conditions and the following disclaimer. 1044764Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1144764Swpaul * notice, this list of conditions and the following disclaimer in the 1244764Swpaul * documentation and/or other materials provided with the distribution. 1344764Swpaul * 3. All advertising materials mentioning features or use of this software 1444764Swpaul * must display the following acknowledgement: 1544764Swpaul * This product includes software developed by Bill Paul. 1644764Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1744764Swpaul * may be used to endorse or promote products derived from this software 1844764Swpaul * without specific prior written permission. 1944764Swpaul * 2044764Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2144764Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2244764Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2344764Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2444764Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2544764Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2644764Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2744764Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2844764Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2944764Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3044764Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 3144764Swpaul * 3244764Swpaul * $Id$ 3344764Swpaul */ 3444764Swpaul 3544764Swpaul#include <sys/param.h> 3644764Swpaul#include <sys/ioctl.h> 3744764Swpaul#include <sys/socket.h> 3844764Swpaul#include <sys/sockio.h> 3944764Swpaul#include <sys/mbuf.h> 4044764Swpaul 4144764Swpaul#include <stdlib.h> 4244764Swpaul#include <unistd.h> 4344764Swpaul 4444764Swpaul#include <net/ethernet.h> 4544764Swpaul#include <net/if.h> 4644764Swpaul#include <net/if_var.h> 4744764Swpaul#include <net/if_vlan_var.h> 4844764Swpaul#include <net/route.h> 4944764Swpaul 5044764Swpaul#include <ctype.h> 5144764Swpaul#include <stdio.h> 5244764Swpaul#include <string.h> 5344764Swpaul#include <stdlib.h> 5444764Swpaul#include <unistd.h> 5544764Swpaul#include <err.h> 5644764Swpaul#include <errno.h> 5744764Swpaul 5844764Swpaul#include "ifconfig.h" 5944764Swpaul 6044764Swpaul#ifndef lint 6144764Swpaulstatic const char rcsid[] = 6244764Swpaul "$Id$"; 6344764Swpaul#endif 6444764Swpaulstatic int __tag = 0; 6544764Swpaulstatic int __have_tag = 0; 6644764Swpaul 6744764Swpaulvoid vlan_status(s, info) 6844764Swpaul int s; 6944764Swpaul struct rt_addrinfo *info __unused; 7044764Swpaul{ 7144764Swpaul struct vlanreq vreq; 7244764Swpaul 7344764Swpaul bzero((char *)&vreq, sizeof(struct vlanreq)); 7444764Swpaul ifr.ifr_data = (caddr_t)&vreq; 7544764Swpaul 7644764Swpaul if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1) 7744764Swpaul return; 7844764Swpaul 7944764Swpaul printf("\tvlan: %d parent interface: %s\n", 8044764Swpaul vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ? 8144764Swpaul "<none>" : vreq.vlr_parent); 8244764Swpaul 8344764Swpaul return; 8444764Swpaul} 8544764Swpaul 8644764Swpaulvoid setvlantag(val, d, s, afp) 8744764Swpaul const char *val; 8844764Swpaul int d, s; 8944764Swpaul const struct afswtch *afp; 9044764Swpaul{ 9144764Swpaul u_int16_t tag; 9244764Swpaul struct vlanreq vreq; 9344764Swpaul 9444764Swpaul __tag = tag = atoi(val); 9544764Swpaul __have_tag = 1; 9644764Swpaul 9744764Swpaul bzero((char *)&vreq, sizeof(struct vlanreq)); 9844764Swpaul ifr.ifr_data = (caddr_t)&vreq; 9944764Swpaul 10044764Swpaul if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1) 10144764Swpaul err(1, "SIOCGETVLAN"); 10244764Swpaul 10344764Swpaul vreq.vlr_tag = tag; 10444764Swpaul 10544764Swpaul if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1) 10644764Swpaul err(1, "SIOCSETVLAN"); 10744764Swpaul 10844764Swpaul return; 10944764Swpaul} 11044764Swpaul 11144764Swpaulvoid setvlandev(val, d, s, afp) 11244764Swpaul const char *val; 11344764Swpaul int d, s; 11444764Swpaul const struct afswtch *afp; 11544764Swpaul{ 11644764Swpaul struct vlanreq vreq; 11744764Swpaul 11844764Swpaul if (!__have_tag) 11944764Swpaul errx(1, "must specify both vlan tag and device"); 12044764Swpaul 12144764Swpaul bzero((char *)&vreq, sizeof(struct vlanreq)); 12244764Swpaul ifr.ifr_data = (caddr_t)&vreq; 12344764Swpaul 12444764Swpaul if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1) 12544764Swpaul err(1, "SIOCGETVLAN"); 12644764Swpaul 12744764Swpaul strncpy(vreq.vlr_parent, val, sizeof(vreq.vlr_parent)); 12844764Swpaul vreq.vlr_tag = __tag; 12944764Swpaul 13044764Swpaul if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1) 13144764Swpaul err(1, "SIOCSETVLAN"); 13244764Swpaul 13344764Swpaul return; 13444764Swpaul} 13544764Swpaul 13644764Swpaulvoid unsetvlandev(val, d, s, afp) 13744764Swpaul const char *val; 13844764Swpaul int d, s; 13944764Swpaul const struct afswtch *afp; 14044764Swpaul{ 14144764Swpaul struct vlanreq vreq; 14244764Swpaul 14344764Swpaul bzero((char *)&vreq, sizeof(struct vlanreq)); 14444764Swpaul ifr.ifr_data = (caddr_t)&vreq; 14544764Swpaul 14644764Swpaul if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1) 14744764Swpaul err(1, "SIOCGETVLAN"); 14844764Swpaul 14944764Swpaul bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent)); 15044764Swpaul vreq.vlr_tag = 0; 15144764Swpaul 15244764Swpaul if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1) 15344764Swpaul err(1, "SIOCSETVLAN"); 15444764Swpaul 15544764Swpaul return; 15644764Swpaul} 157