xref: /freebsd/sbin/ifconfig/ifvlan.c (revision a79b71281cd63ad7a6cc43a6d5673a2510b51630)
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$
33  */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/mbuf.h>
40 
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_vlan_var.h>
48 #include <net/route.h>
49 
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <err.h>
56 #include <errno.h>
57 
58 #include "ifconfig.h"
59 
60 #ifndef lint
61 static const char rcsid[] =
62   "$FreeBSD$";
63 #endif
64 static int			__tag = 0;
65 static int			__have_tag = 0;
66 
67 void vlan_status(s, info)
68 	int			s;
69 	struct rt_addrinfo *info __unused;
70 {
71 	struct vlanreq		vreq;
72 
73 	bzero((char *)&vreq, sizeof(struct vlanreq));
74 	ifr.ifr_data = (caddr_t)&vreq;
75 
76 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
77 		return;
78 
79 	printf("\tvlan: %d parent interface: %s\n",
80 	    vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
81 	    "<none>" : vreq.vlr_parent);
82 
83 	return;
84 }
85 
86 void setvlantag(val, d, s, afp)
87 	const char		*val;
88 	int			d, s;
89 	const struct afswtch	*afp;
90 {
91 	u_int16_t		tag;
92 	struct vlanreq		vreq;
93 
94 	__tag = tag = atoi(val);
95 	__have_tag = 1;
96 
97 	bzero((char *)&vreq, sizeof(struct vlanreq));
98 	ifr.ifr_data = (caddr_t)&vreq;
99 
100 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
101 		err(1, "SIOCGETVLAN");
102 
103 	vreq.vlr_tag = tag;
104 
105 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
106 		err(1, "SIOCSETVLAN");
107 
108 	return;
109 }
110 
111 void setvlandev(val, d, s, afp)
112 	const char		*val;
113 	int			d, s;
114 	const struct afswtch	*afp;
115 {
116 	struct vlanreq		vreq;
117 
118 	if (!__have_tag)
119 		errx(1, "must specify both vlan tag and device");
120 
121 	bzero((char *)&vreq, sizeof(struct vlanreq));
122 	ifr.ifr_data = (caddr_t)&vreq;
123 
124 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
125 		err(1, "SIOCGETVLAN");
126 
127 	strncpy(vreq.vlr_parent, val, sizeof(vreq.vlr_parent));
128 	vreq.vlr_tag = __tag;
129 
130 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
131 		err(1, "SIOCSETVLAN");
132 
133 	return;
134 }
135 
136 void unsetvlandev(val, d, s, afp)
137 	const char		*val;
138 	int			d, s;
139 	const struct afswtch	*afp;
140 {
141 	struct vlanreq		vreq;
142 
143 	bzero((char *)&vreq, sizeof(struct vlanreq));
144 	ifr.ifr_data = (caddr_t)&vreq;
145 
146 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
147 		err(1, "SIOCGETVLAN");
148 
149 	bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
150 	vreq.vlr_tag = 0;
151 
152 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
153 		err(1, "SIOCSETVLAN");
154 
155 	return;
156 }
157