xref: /dragonfly/sbin/ifconfig/ifvlan.c (revision 0db87cb7)
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.7.2.4 2006/02/09 10:48:43 yar Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 #include <net/ethernet.h>
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/vlan/if_vlan_var.h>
47 #include <net/route.h>
48 
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <err.h>
53 #include <errno.h>
54 
55 #include "ifconfig.h"
56 
57 static struct vlanreq		__vreq;
58 static int			__have_dev = 0;
59 static int			__have_tag = 0;
60 
61 static void
62 vlan_status(int s)
63 {
64 	struct vlanreq		vreq;
65 
66 	bzero((char *)&vreq, sizeof(vreq));
67 	ifr.ifr_data = (caddr_t)&vreq;
68 
69 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
70 		return;
71 
72 	printf("\tvlan: %d parent interface: %s\n",
73 	    vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ?
74 	    "<none>" : vreq.vlr_parent);
75 }
76 
77 static void
78 setvlantag(const char *val, int d, int s, const struct afswtch	*afp)
79 {
80 	char			*endp;
81 	u_long			ul;
82 
83 	ul = strtoul(val, &endp, 0);
84 	if (*endp != '\0')
85 		errx(1, "invalid value for vlan");
86 	__vreq.vlr_tag = ul;
87 	/* check if the value can be represented in vlr_tag */
88 	if (__vreq.vlr_tag != ul)
89 		errx(1, "value for vlan out of range");
90 	/* the kernel will do more specific checks on vlr_tag */
91 	__have_tag = 1;
92 }
93 
94 static void
95 setvlandev(const char *val, int d, int s, const struct afswtch	*afp)
96 {
97 
98 	strncpy(__vreq.vlr_parent, val, sizeof(__vreq.vlr_parent));
99 	__have_dev = 1;
100 }
101 
102 static void
103 unsetvlandev(const char *val, int d, int s, const struct afswtch *afp)
104 {
105 
106 	if (val != NULL)
107 		warnx("argument to -vlandev is useless and hence deprecated");
108 
109 	bzero((char *)&__vreq, sizeof(__vreq));
110 	ifr.ifr_data = (caddr_t)&__vreq;
111 #if 0	/* this code will be of use when we can alter vlan or vlandev only */
112 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
113 		err(1, "SIOCGETVLAN");
114 
115 	bzero((char *)&__vreq.vlr_parent, sizeof(__vreq.vlr_parent));
116 	__vreq.vlr_tag = 0; /* XXX clear parent only (no kernel support now) */
117 #endif
118 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
119 		err(1, "SIOCSETVLAN");
120 	__have_dev = __have_tag = 0;
121 }
122 
123 static void
124 vlan_cb(int s, void *arg)
125 {
126 
127 	if (__have_tag ^ __have_dev)
128 		errx(1, "both vlan and vlandev must be specified");
129 
130 	if (__have_tag && __have_dev) {
131 		ifr.ifr_data = (caddr_t)&__vreq;
132 		if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
133 			err(1, "SIOCSETVLAN");
134 	}
135 }
136 
137 static struct cmd vlan_cmds[] = {
138 	DEF_CMD_ARG("vlan",				setvlantag),
139 	DEF_CMD_ARG("vlandev",				setvlandev),
140 	/* XXX For compatibility.  Should become DEF_CMD() some day. */
141 	DEF_CMD_OPTARG("-vlandev",			unsetvlandev),
142 	DEF_CMD("vlanmtu",	IFCAP_VLAN_MTU,		setifcap),
143 	DEF_CMD("-vlanmtu",	-IFCAP_VLAN_MTU,	setifcap),
144 	DEF_CMD("vlanhwtag",	IFCAP_VLAN_HWTAGGING,	setifcap),
145 	DEF_CMD("-vlanhwtag",	-IFCAP_VLAN_HWTAGGING,	setifcap),
146 };
147 static struct afswtch af_vlan = {
148 	.af_name	= "af_vlan",
149 	.af_af		= AF_UNSPEC,
150 	.af_other_status = vlan_status,
151 };
152 
153 static __constructor(101) void
154 vlan_ctor(void)
155 {
156 #define	N(a)	(sizeof(a) / sizeof(a[0]))
157 	int i;
158 
159 	for (i = 0; i < N(vlan_cmds);  i++)
160 		cmd_register(&vlan_cmds[i]);
161 	af_register(&af_vlan);
162 	callback_register(vlan_cb, NULL);
163 #undef N
164 }
165