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