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