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