]>
Commit | Line | Data |
---|---|---|
7902cf7e A |
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> | |
7902cf7e | 34 | #include <sys/ioctl.h> |
7902cf7e A |
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 | ||
342c141e | 57 | #include <sys/cdefs.h> |
7902cf7e | 58 | |
8d01c344 | 59 | #define NOTAG ((u_short) -1) |
7902cf7e | 60 | |
8d01c344 A |
61 | static struct vlanreq params = { |
62 | .vlr_tag = NOTAG, | |
63 | }; | |
7902cf7e | 64 | |
8d01c344 A |
65 | static int |
66 | getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq) | |
67 | { | |
68 | bzero((char *)vreq, sizeof(*vreq)); | |
69 | ifr->ifr_data = (caddr_t)vreq; | |
7902cf7e | 70 | |
8d01c344 | 71 | return ioctl(s, SIOCGETVLAN, (caddr_t)ifr); |
7902cf7e A |
72 | } |
73 | ||
8d01c344 A |
74 | static void |
75 | vlan_status(int s) | |
7902cf7e | 76 | { |
7902cf7e A |
77 | struct vlanreq vreq; |
78 | ||
8d01c344 A |
79 | if (getvlan(s, &ifr, &vreq) != -1) |
80 | printf("\tvlan: %d parent interface: %s\n", | |
81 | vreq.vlr_tag, vreq.vlr_parent[0] == '\0' ? | |
82 | "<none>" : vreq.vlr_parent); | |
7902cf7e A |
83 | } |
84 | ||
8d01c344 A |
85 | static void |
86 | vlan_create(int s, struct ifreq *ifr) | |
7902cf7e | 87 | { |
8d01c344 A |
88 | if (params.vlr_tag != NOTAG || params.vlr_parent[0] != '\0') { |
89 | /* | |
90 | * One or both parameters were specified, make sure both. | |
91 | */ | |
92 | if (params.vlr_tag == NOTAG) | |
93 | errx(1, "must specify a tag for vlan create"); | |
94 | if (params.vlr_parent[0] == '\0') | |
95 | errx(1, "must specify a parent device for vlan create"); | |
96 | ifr->ifr_data = (caddr_t) ¶ms; | |
97 | } | |
7f5b2e89 | 98 | #ifdef SIOCIFCREATE2 |
8d01c344 A |
99 | if (ioctl(s, SIOCIFCREATE2, ifr) < 0) |
100 | err(1, "SIOCIFCREATE2"); | |
101 | #else | |
102 | if (ioctl(s, SIOCIFCREATE, ifr) < 0) | |
103 | err(1, "SIOCIFCREATE"); | |
104 | #endif | |
105 | } | |
7902cf7e | 106 | |
8d01c344 A |
107 | static void |
108 | vlan_cb(int s, void *arg) | |
109 | { | |
110 | if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0')) | |
111 | errx(1, "both vlan and vlandev must be specified"); | |
112 | } | |
7902cf7e | 113 | |
8d01c344 A |
114 | static void |
115 | vlan_set(int s, struct ifreq *ifr) | |
116 | { | |
117 | if (params.vlr_tag != NOTAG && params.vlr_parent[0] != '\0') { | |
118 | ifr->ifr_data = (caddr_t) ¶ms; | |
119 | if (ioctl(s, SIOCSETVLAN, (caddr_t)ifr) == -1) | |
120 | err(1, "SIOCSETVLAN"); | |
121 | } | |
122 | } | |
7902cf7e | 123 | |
8d01c344 A |
124 | static |
125 | DECL_CMD_FUNC(setvlantag, val, d) | |
126 | { | |
127 | struct vlanreq vreq; | |
128 | u_long ul; | |
129 | char *endp; | |
130 | ||
131 | ul = strtoul(val, &endp, 0); | |
132 | if (*endp != '\0') | |
133 | errx(1, "invalid value for vlan"); | |
134 | params.vlr_tag = ul; | |
135 | /* check if the value can be represented in vlr_tag */ | |
136 | if (params.vlr_tag != ul) | |
137 | errx(1, "value for vlan out of range"); | |
138 | ||
139 | if (getvlan(s, &ifr, &vreq) != -1) | |
140 | vlan_set(s, &ifr); | |
141 | else | |
142 | clone_setcallback(vlan_create); | |
143 | } | |
7902cf7e | 144 | |
8d01c344 A |
145 | static |
146 | DECL_CMD_FUNC(setvlandev, val, d) | |
147 | { | |
148 | struct vlanreq vreq; | |
7902cf7e | 149 | |
8d01c344 | 150 | strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent)); |
7902cf7e | 151 | |
8d01c344 A |
152 | if (getvlan(s, &ifr, &vreq) != -1) |
153 | vlan_set(s, &ifr); | |
154 | else | |
155 | clone_setcallback(vlan_create); | |
7902cf7e A |
156 | } |
157 | ||
8d01c344 A |
158 | static |
159 | DECL_CMD_FUNC(unsetvlandev, val, d) | |
7902cf7e A |
160 | { |
161 | struct vlanreq vreq; | |
162 | ||
163 | bzero((char *)&vreq, sizeof(struct vlanreq)); | |
164 | ifr.ifr_data = (caddr_t)&vreq; | |
165 | ||
166 | if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1) | |
167 | err(1, "SIOCGETVLAN"); | |
168 | ||
169 | bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent)); | |
170 | vreq.vlr_tag = 0; | |
171 | ||
172 | if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1) | |
173 | err(1, "SIOCSETVLAN"); | |
8d01c344 | 174 | } |
7902cf7e | 175 | |
8d01c344 A |
176 | static struct cmd vlan_cmds[] = { |
177 | DEF_CLONE_CMD_ARG("vlan", setvlantag), | |
178 | DEF_CLONE_CMD_ARG("vlandev", setvlandev), | |
179 | /* XXX For compatibility. Should become DEF_CMD() some day. */ | |
180 | DEF_CMD_OPTARG("-vlandev", unsetvlandev), | |
fdfd5971 | 181 | #ifdef IFCAP_VLAN_MTU |
8d01c344 A |
182 | DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap), |
183 | DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap), | |
fdfd5971 A |
184 | #endif /* IFCAP_VLAN_MTU */ |
185 | #ifdef IFCAP_VLAN_HWTAGGING | |
8d01c344 A |
186 | DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap), |
187 | DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap), | |
fdfd5971 | 188 | #endif /* IFCAP_VLAN_HWTAGGING */ |
8d01c344 A |
189 | }; |
190 | static struct afswtch af_vlan = { | |
191 | .af_name = "af_vlan", | |
192 | .af_af = AF_UNSPEC, | |
193 | .af_other_status = vlan_status, | |
194 | }; | |
195 | ||
196 | static __constructor void | |
197 | vlan_ctor(void) | |
198 | { | |
199 | #define N(a) (sizeof(a) / sizeof(a[0])) | |
200 | int i; | |
201 | ||
202 | for (i = 0; i < N(vlan_cmds); i++) | |
203 | cmd_register(&vlan_cmds[i]); | |
204 | af_register(&af_vlan); | |
205 | callback_register(vlan_cb, NULL); | |
206 | #undef N | |
7902cf7e | 207 | } |