if_vlan.c revision 1.15 1 1.15 ad /* $NetBSD: if_vlan.c,v 1.15 2000/10/10 10:07:35 ad Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.11 thorpej * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*
40 1.1 thorpej * Copyright 1998 Massachusetts Institute of Technology
41 1.1 thorpej *
42 1.1 thorpej * Permission to use, copy, modify, and distribute this software and
43 1.1 thorpej * its documentation for any purpose and without fee is hereby
44 1.1 thorpej * granted, provided that both the above copyright notice and this
45 1.1 thorpej * permission notice appear in all copies, that both the above
46 1.1 thorpej * copyright notice and this permission notice appear in all
47 1.1 thorpej * supporting documentation, and that the name of M.I.T. not be used
48 1.1 thorpej * in advertising or publicity pertaining to distribution of the
49 1.1 thorpej * software without specific, written prior permission. M.I.T. makes
50 1.1 thorpej * no representations about the suitability of this software for any
51 1.1 thorpej * purpose. It is provided "as is" without express or implied
52 1.1 thorpej * warranty.
53 1.1 thorpej *
54 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
55 1.1 thorpej * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
56 1.1 thorpej * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
57 1.1 thorpej * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
58 1.1 thorpej * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 1.1 thorpej * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 1.1 thorpej * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
61 1.1 thorpej * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 1.1 thorpej * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63 1.1 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
64 1.1 thorpej * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 1.1 thorpej * SUCH DAMAGE.
66 1.1 thorpej *
67 1.1 thorpej * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
68 1.1 thorpej * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
69 1.1 thorpej */
70 1.1 thorpej
71 1.1 thorpej /*
72 1.1 thorpej * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
73 1.1 thorpej * extended some day to also handle IEEE 802.1P priority tagging. This is
74 1.1 thorpej * sort of sneaky in the implementation, since we need to pretend to be
75 1.1 thorpej * enough of an Ethernet implementation to make ARP work. The way we do
76 1.1 thorpej * this is by telling everyone that we are an Ethernet interface, and then
77 1.1 thorpej * catch the packets that ether_output() left on our output queue when it
78 1.1 thorpej * calls if_start(), rewrite them for use by the real outgoing interface,
79 1.1 thorpej * and ask it to send them.
80 1.1 thorpej *
81 1.1 thorpej * TODO:
82 1.1 thorpej *
83 1.1 thorpej * - Need some way to notify vlan interfaces when the parent
84 1.1 thorpej * interface changes MTU.
85 1.10 thorpej *
86 1.8 ad * - Need a way to facilitate parent interfaces that can do
87 1.8 ad * tag insertion and/or extraction in hardware.
88 1.1 thorpej *
89 1.1 thorpej * - Need to make promiscuous mode work.
90 1.1 thorpej */
91 1.1 thorpej
92 1.1 thorpej #include "opt_inet.h"
93 1.1 thorpej #include "bpfilter.h"
94 1.1 thorpej
95 1.1 thorpej #include <sys/param.h>
96 1.1 thorpej #include <sys/kernel.h>
97 1.1 thorpej #include <sys/mbuf.h>
98 1.1 thorpej #include <sys/queue.h>
99 1.1 thorpej #include <sys/socket.h>
100 1.1 thorpej #include <sys/sockio.h>
101 1.1 thorpej #include <sys/systm.h>
102 1.1 thorpej #include <sys/proc.h>
103 1.1 thorpej
104 1.1 thorpej #if NBPFILTER > 0
105 1.1 thorpej #include <net/bpf.h>
106 1.1 thorpej #endif
107 1.1 thorpej #include <net/if.h>
108 1.1 thorpej #include <net/if_dl.h>
109 1.1 thorpej #include <net/if_types.h>
110 1.1 thorpej #include <net/if_ether.h>
111 1.1 thorpej #include <net/if_vlanvar.h>
112 1.1 thorpej
113 1.1 thorpej #ifdef INET
114 1.1 thorpej #include <netinet/in.h>
115 1.1 thorpej #include <netinet/if_inarp.h>
116 1.1 thorpej #endif
117 1.1 thorpej
118 1.1 thorpej extern struct ifaddr **ifnet_addrs; /* XXX if.c */
119 1.1 thorpej
120 1.10 thorpej struct vlan_mc_entry {
121 1.10 thorpej LIST_ENTRY(vlan_mc_entry) mc_entries;
122 1.10 thorpej /*
123 1.10 thorpej * A key to identify this entry. The mc_addr below can't be
124 1.10 thorpej * used since multiple sockaddr may mapped into the same
125 1.10 thorpej * ether_multi (e.g., AF_UNSPEC).
126 1.10 thorpej */
127 1.10 thorpej union {
128 1.10 thorpej struct ether_multi *mcu_enm;
129 1.10 thorpej } mc_u;
130 1.10 thorpej struct sockaddr_storage mc_addr;
131 1.10 thorpej };
132 1.10 thorpej
133 1.10 thorpej #define mc_enm mc_u.mcu_enm
134 1.10 thorpej
135 1.10 thorpej struct ifvlan {
136 1.10 thorpej union {
137 1.10 thorpej struct ethercom ifvu_ec;
138 1.10 thorpej } ifv_u;
139 1.10 thorpej struct ifnet *ifv_p; /* parent interface of this vlan */
140 1.10 thorpej struct ifv_linkmib {
141 1.10 thorpej const struct vlan_multisw *ifvm_msw;
142 1.10 thorpej int ifvm_encaplen; /* encapsulation length */
143 1.10 thorpej int ifvm_mtufudge; /* MTU fudged by this much */
144 1.10 thorpej int ifvm_mintu; /* min transmission unit */
145 1.10 thorpej u_int16_t ifvm_proto; /* encapsulation ethertype */
146 1.10 thorpej u_int16_t ifvm_tag; /* tag to apply on packets */
147 1.10 thorpej } ifv_mib;
148 1.10 thorpej LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
149 1.10 thorpej LIST_ENTRY(ifvlan) ifv_list;
150 1.10 thorpej };
151 1.10 thorpej
152 1.10 thorpej #define ifv_ec ifv_u.ifvu_ec
153 1.10 thorpej
154 1.10 thorpej #define ifv_if ifv_ec.ec_if
155 1.10 thorpej
156 1.10 thorpej #define ifv_msw ifv_mib.ifvm_msw
157 1.10 thorpej #define ifv_encaplen ifv_mib.ifvm_encaplen
158 1.10 thorpej #define ifv_mtufudge ifv_mib.ifvm_mtufudge
159 1.10 thorpej #define ifv_mintu ifv_mib.ifvm_mintu
160 1.10 thorpej #define ifv_tag ifv_mib.ifvm_tag
161 1.10 thorpej
162 1.10 thorpej struct vlan_multisw {
163 1.10 thorpej int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
164 1.10 thorpej int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
165 1.10 thorpej void (*vmsw_purgemulti)(struct ifvlan *);
166 1.10 thorpej };
167 1.10 thorpej
168 1.10 thorpej static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
169 1.10 thorpej static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
170 1.10 thorpej static void vlan_ether_purgemulti(struct ifvlan *);
171 1.10 thorpej
172 1.10 thorpej const struct vlan_multisw vlan_ether_multisw = {
173 1.10 thorpej vlan_ether_addmulti,
174 1.10 thorpej vlan_ether_delmulti,
175 1.10 thorpej vlan_ether_purgemulti,
176 1.10 thorpej };
177 1.10 thorpej
178 1.1 thorpej static int vlan_clone_create(struct if_clone *, int);
179 1.1 thorpej static void vlan_clone_destroy(struct ifnet *);
180 1.1 thorpej static int vlan_config(struct ifvlan *, struct ifnet *);
181 1.1 thorpej static int vlan_ioctl(struct ifnet *, u_long, caddr_t);
182 1.1 thorpej static void vlan_start(struct ifnet *);
183 1.11 thorpej static void vlan_unconfig(struct ifnet *);
184 1.10 thorpej
185 1.10 thorpej void vlanattach(int);
186 1.1 thorpej
187 1.1 thorpej /* XXX This should be a hash table with the tag as the basis of the key. */
188 1.1 thorpej static LIST_HEAD(, ifvlan) ifv_list;
189 1.1 thorpej
190 1.1 thorpej struct if_clone vlan_cloner =
191 1.1 thorpej IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
192 1.1 thorpej
193 1.1 thorpej void
194 1.1 thorpej vlanattach(int n)
195 1.1 thorpej {
196 1.1 thorpej
197 1.1 thorpej LIST_INIT(&ifv_list);
198 1.1 thorpej if_clone_attach(&vlan_cloner);
199 1.1 thorpej }
200 1.1 thorpej
201 1.1 thorpej static int
202 1.1 thorpej vlan_clone_create(struct if_clone *ifc, int unit)
203 1.1 thorpej {
204 1.1 thorpej struct ifvlan *ifv;
205 1.1 thorpej struct ifnet *ifp;
206 1.11 thorpej int s;
207 1.1 thorpej
208 1.10 thorpej ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK);
209 1.1 thorpej memset(ifv, 0, sizeof(struct ifvlan));
210 1.14 enami ifp = &ifv->ifv_if;
211 1.5 enami LIST_INIT(&ifv->ifv_mc_listhead);
212 1.11 thorpej
213 1.11 thorpej s = splnet();
214 1.1 thorpej LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
215 1.11 thorpej splx(s);
216 1.1 thorpej
217 1.1 thorpej sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
218 1.1 thorpej ifp->if_softc = ifv;
219 1.1 thorpej ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
220 1.1 thorpej ifp->if_start = vlan_start;
221 1.1 thorpej ifp->if_ioctl = vlan_ioctl;
222 1.1 thorpej
223 1.1 thorpej if_attach(ifp);
224 1.1 thorpej
225 1.1 thorpej return (0);
226 1.1 thorpej }
227 1.1 thorpej
228 1.1 thorpej static void
229 1.1 thorpej vlan_clone_destroy(struct ifnet *ifp)
230 1.1 thorpej {
231 1.11 thorpej struct ifvlan *ifv = ifp->if_softc;
232 1.1 thorpej int s;
233 1.1 thorpej
234 1.11 thorpej s = splnet();
235 1.1 thorpej LIST_REMOVE(ifv, ifv_list);
236 1.1 thorpej vlan_unconfig(ifp);
237 1.11 thorpej splx(s);
238 1.1 thorpej
239 1.1 thorpej if_detach(ifp);
240 1.1 thorpej free(ifv, M_DEVBUF);
241 1.1 thorpej }
242 1.1 thorpej
243 1.11 thorpej /*
244 1.11 thorpej * Configure a VLAN interface. Must be called at splnet().
245 1.11 thorpej */
246 1.1 thorpej static int
247 1.1 thorpej vlan_config(struct ifvlan *ifv, struct ifnet *p)
248 1.1 thorpej {
249 1.10 thorpej struct ifnet *ifp = &ifv->ifv_if;
250 1.10 thorpej int error;
251 1.1 thorpej
252 1.1 thorpej if (ifv->ifv_p != NULL)
253 1.1 thorpej return (EBUSY);
254 1.10 thorpej
255 1.10 thorpej switch (p->if_type) {
256 1.10 thorpej case IFT_ETHER:
257 1.10 thorpej {
258 1.10 thorpej struct ethercom *ec = (void *) p;
259 1.10 thorpej
260 1.10 thorpej ifv->ifv_msw = &vlan_ether_multisw;
261 1.10 thorpej ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
262 1.10 thorpej ifv->ifv_mintu = ETHERMIN;
263 1.10 thorpej
264 1.10 thorpej /*
265 1.10 thorpej * If the parent supports the VLAN_MTU capability,
266 1.10 thorpej * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
267 1.10 thorpej * enable it.
268 1.10 thorpej */
269 1.10 thorpej if (ec->ec_nvlans++ == 0 &&
270 1.10 thorpej (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
271 1.10 thorpej /*
272 1.10 thorpej * Enable Tx/Rx of VLAN-sized frames.
273 1.10 thorpej */
274 1.10 thorpej ec->ec_capenable |= ETHERCAP_VLAN_MTU;
275 1.10 thorpej if (p->if_flags & IFF_UP) {
276 1.10 thorpej struct ifreq ifr;
277 1.10 thorpej
278 1.10 thorpej ifr.ifr_flags = p->if_flags;
279 1.10 thorpej error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
280 1.10 thorpej (caddr_t) &ifr);
281 1.10 thorpej if (error) {
282 1.10 thorpej if (ec->ec_nvlans-- == 1)
283 1.10 thorpej ec->ec_capenable &=
284 1.10 thorpej ~ETHERCAP_VLAN_MTU;
285 1.10 thorpej return (error);
286 1.10 thorpej }
287 1.10 thorpej }
288 1.10 thorpej ifv->ifv_mtufudge = 0;
289 1.10 thorpej } else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
290 1.10 thorpej /*
291 1.10 thorpej * Fudge the MTU by the encapsulation size. This
292 1.10 thorpej * makes us incompatible with strictly compliant
293 1.10 thorpej * 802.1Q implementations, but allows us to use
294 1.10 thorpej * the feature with other NetBSD implementations,
295 1.10 thorpej * which might still be useful.
296 1.10 thorpej */
297 1.10 thorpej ifv->ifv_mtufudge = ifv->ifv_encaplen;
298 1.10 thorpej }
299 1.10 thorpej
300 1.10 thorpej /*
301 1.10 thorpej * We inherit the parent's Ethernet address.
302 1.10 thorpej */
303 1.10 thorpej ether_ifattach(ifp, LLADDR(p->if_sadl));
304 1.10 thorpej ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
305 1.10 thorpej #if NBPFILTER > 0
306 1.10 thorpej bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
307 1.10 thorpej sizeof(struct ether_header));
308 1.10 thorpej #endif
309 1.10 thorpej break;
310 1.10 thorpej }
311 1.10 thorpej
312 1.10 thorpej default:
313 1.10 thorpej return (EPROTONOSUPPORT);
314 1.10 thorpej }
315 1.10 thorpej
316 1.1 thorpej ifv->ifv_p = p;
317 1.10 thorpej ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
318 1.1 thorpej ifv->ifv_if.if_flags = p->if_flags;
319 1.1 thorpej
320 1.1 thorpej /*
321 1.10 thorpej * Inherit the if_type from the parent. This allows us
322 1.10 thorpej * to participate in bridges of that type.
323 1.1 thorpej */
324 1.10 thorpej ifv->ifv_if.if_type = p->if_type;
325 1.10 thorpej
326 1.1 thorpej return (0);
327 1.1 thorpej }
328 1.1 thorpej
329 1.11 thorpej /*
330 1.11 thorpej * Unconfigure a VLAN interface. Must be called at splnet().
331 1.11 thorpej */
332 1.11 thorpej static void
333 1.1 thorpej vlan_unconfig(struct ifnet *ifp)
334 1.1 thorpej {
335 1.10 thorpej struct ifvlan *ifv = ifp->if_softc;
336 1.1 thorpej
337 1.7 enami if (ifv->ifv_p == NULL)
338 1.11 thorpej return;
339 1.1 thorpej
340 1.1 thorpej /*
341 1.1 thorpej * Since the interface is being unconfigured, we need to empty the
342 1.1 thorpej * list of multicast groups that we may have joined while we were
343 1.1 thorpej * alive and remove them from the parent's list also.
344 1.1 thorpej */
345 1.10 thorpej (*ifv->ifv_msw->vmsw_purgemulti)(ifv);
346 1.1 thorpej
347 1.1 thorpej /* Disconnect from parent. */
348 1.10 thorpej switch (ifv->ifv_p->if_type) {
349 1.10 thorpej case IFT_ETHER:
350 1.10 thorpej {
351 1.10 thorpej struct ethercom *ec = (void *) ifv->ifv_p;
352 1.10 thorpej
353 1.10 thorpej if (ec->ec_nvlans-- == 1) {
354 1.10 thorpej /*
355 1.10 thorpej * Disable Tx/Rx of VLAN-sized frames.
356 1.10 thorpej */
357 1.10 thorpej ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
358 1.10 thorpej if (ifv->ifv_p->if_flags & IFF_UP) {
359 1.10 thorpej struct ifreq ifr;
360 1.10 thorpej
361 1.10 thorpej ifr.ifr_flags = ifv->ifv_p->if_flags;
362 1.10 thorpej (void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
363 1.10 thorpej SIOCSIFFLAGS, (caddr_t) &ifr);
364 1.10 thorpej }
365 1.10 thorpej }
366 1.10 thorpej
367 1.10 thorpej #if NBPFILTER > 0
368 1.10 thorpej bpfdetach(ifp);
369 1.10 thorpej #endif
370 1.10 thorpej ether_ifdetach(ifp);
371 1.10 thorpej break;
372 1.10 thorpej }
373 1.10 thorpej
374 1.10 thorpej #ifdef DIAGNOSTIC
375 1.10 thorpej default:
376 1.10 thorpej panic("vlan_unconfig: impossible");
377 1.10 thorpej #endif
378 1.10 thorpej }
379 1.10 thorpej
380 1.1 thorpej ifv->ifv_p = NULL;
381 1.10 thorpej ifv->ifv_if.if_mtu = 0;
382 1.1 thorpej
383 1.11 thorpej if_down(ifp);
384 1.11 thorpej ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
385 1.11 thorpej }
386 1.11 thorpej
387 1.11 thorpej /*
388 1.11 thorpej * Called when a parent interface is detaching; destroy any VLAN
389 1.11 thorpej * configuration for the parent interface.
390 1.11 thorpej */
391 1.11 thorpej void
392 1.11 thorpej vlan_ifdetach(struct ifnet *p)
393 1.11 thorpej {
394 1.11 thorpej struct ifvlan *ifv;
395 1.11 thorpej int s;
396 1.11 thorpej
397 1.11 thorpej s = splnet();
398 1.11 thorpej
399 1.11 thorpej for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
400 1.11 thorpej ifv = LIST_NEXT(ifv, ifv_list)) {
401 1.11 thorpej if (ifv->ifv_p == p)
402 1.11 thorpej vlan_unconfig(&ifv->ifv_if);
403 1.11 thorpej }
404 1.11 thorpej
405 1.1 thorpej splx(s);
406 1.1 thorpej }
407 1.1 thorpej
408 1.1 thorpej static int
409 1.1 thorpej vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
410 1.1 thorpej {
411 1.1 thorpej struct proc *p = curproc; /* XXX */
412 1.11 thorpej struct ifvlan *ifv = ifp->if_softc;
413 1.11 thorpej struct ifaddr *ifa = (struct ifaddr *) data;
414 1.11 thorpej struct ifreq *ifr = (struct ifreq *) data;
415 1.1 thorpej struct ifnet *pr;
416 1.1 thorpej struct vlanreq vlr;
417 1.1 thorpej struct sockaddr *sa;
418 1.11 thorpej int s, error = 0;
419 1.1 thorpej
420 1.11 thorpej s = splnet();
421 1.1 thorpej
422 1.1 thorpej switch (cmd) {
423 1.1 thorpej case SIOCSIFADDR:
424 1.1 thorpej ifp->if_flags |= IFF_UP;
425 1.1 thorpej
426 1.1 thorpej switch (ifa->ifa_addr->sa_family) {
427 1.1 thorpej #ifdef INET
428 1.1 thorpej case AF_INET:
429 1.1 thorpej arp_ifinit(ifp, ifa);
430 1.1 thorpej break;
431 1.1 thorpej #endif
432 1.1 thorpej default:
433 1.1 thorpej break;
434 1.1 thorpej }
435 1.1 thorpej break;
436 1.1 thorpej
437 1.1 thorpej case SIOCGIFADDR:
438 1.1 thorpej sa = (struct sockaddr *)&ifr->ifr_data;
439 1.1 thorpej memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
440 1.1 thorpej break;
441 1.1 thorpej
442 1.1 thorpej case SIOCSIFMTU:
443 1.1 thorpej if (ifv->ifv_p != NULL) {
444 1.10 thorpej if (ifr->ifr_mtu >
445 1.10 thorpej (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
446 1.10 thorpej ifr->ifr_mtu <
447 1.10 thorpej (ifv->ifv_mintu - ifv->ifv_mtufudge))
448 1.1 thorpej error = EINVAL;
449 1.1 thorpej else
450 1.1 thorpej ifp->if_mtu = ifr->ifr_mtu;
451 1.1 thorpej } else
452 1.1 thorpej error = EINVAL;
453 1.1 thorpej break;
454 1.1 thorpej
455 1.1 thorpej case SIOCSETVLAN:
456 1.1 thorpej if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
457 1.1 thorpej break;
458 1.1 thorpej if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
459 1.1 thorpej break;
460 1.1 thorpej if (vlr.vlr_parent[0] == '\0') {
461 1.1 thorpej vlan_unconfig(ifp);
462 1.1 thorpej break;
463 1.1 thorpej }
464 1.1 thorpej if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
465 1.1 thorpej error = EINVAL; /* check for valid tag */
466 1.1 thorpej break;
467 1.1 thorpej }
468 1.1 thorpej if ((pr = ifunit(vlr.vlr_parent)) == 0) {
469 1.1 thorpej error = ENOENT;
470 1.1 thorpej break;
471 1.1 thorpej }
472 1.1 thorpej if ((error = vlan_config(ifv, pr)) != 0)
473 1.1 thorpej break;
474 1.1 thorpej ifv->ifv_tag = vlr.vlr_tag;
475 1.1 thorpej ifp->if_flags |= IFF_RUNNING;
476 1.1 thorpej break;
477 1.1 thorpej
478 1.1 thorpej case SIOCGETVLAN:
479 1.1 thorpej memset(&vlr, 0, sizeof(vlr));
480 1.1 thorpej if (ifv->ifv_p != NULL) {
481 1.1 thorpej snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
482 1.1 thorpej ifv->ifv_p->if_xname);
483 1.1 thorpej vlr.vlr_tag = ifv->ifv_tag;
484 1.1 thorpej }
485 1.1 thorpej error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
486 1.1 thorpej break;
487 1.1 thorpej
488 1.1 thorpej case SIOCSIFFLAGS:
489 1.1 thorpej /*
490 1.1 thorpej * XXX We don't support promiscuous mode right now because
491 1.1 thorpej * it would require help from the underlying drivers, which
492 1.1 thorpej * hasn't been implemented.
493 1.1 thorpej */
494 1.1 thorpej if ((ifr->ifr_flags & IFF_PROMISC) != 0) {
495 1.1 thorpej ifp->if_flags &= ~(IFF_PROMISC);
496 1.1 thorpej error = EINVAL;
497 1.1 thorpej }
498 1.1 thorpej break;
499 1.1 thorpej
500 1.1 thorpej case SIOCADDMULTI:
501 1.10 thorpej error = (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr);
502 1.6 enami break;
503 1.6 enami
504 1.1 thorpej case SIOCDELMULTI:
505 1.10 thorpej error = (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr);
506 1.1 thorpej break;
507 1.1 thorpej
508 1.1 thorpej default:
509 1.1 thorpej error = EINVAL;
510 1.1 thorpej }
511 1.11 thorpej
512 1.11 thorpej splx(s);
513 1.1 thorpej
514 1.1 thorpej return (error);
515 1.1 thorpej }
516 1.1 thorpej
517 1.1 thorpej static int
518 1.10 thorpej vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
519 1.1 thorpej {
520 1.1 thorpej struct vlan_mc_entry *mc;
521 1.5 enami u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
522 1.1 thorpej int error;
523 1.1 thorpej
524 1.5 enami if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
525 1.5 enami return (EINVAL);
526 1.5 enami
527 1.5 enami error = ether_addmulti(ifr, &ifv->ifv_ec);
528 1.5 enami if (error != ENETRESET)
529 1.5 enami return (error);
530 1.1 thorpej
531 1.5 enami /*
532 1.5 enami * This is new multicast address. We have to tell parent
533 1.5 enami * about it. Also, remember this multicast address so that
534 1.5 enami * we can delete them on unconfigure.
535 1.5 enami */
536 1.5 enami MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
537 1.5 enami M_DEVBUF, M_NOWAIT);
538 1.5 enami if (mc == NULL) {
539 1.5 enami error = ENOMEM;
540 1.5 enami goto alloc_failed;
541 1.1 thorpej }
542 1.1 thorpej
543 1.5 enami /*
544 1.5 enami * As ether_addmulti() returns ENETRESET, following two
545 1.5 enami * statement shouldn't fail.
546 1.5 enami */
547 1.5 enami (void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
548 1.5 enami ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
549 1.5 enami memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
550 1.5 enami LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
551 1.5 enami
552 1.5 enami error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
553 1.5 enami (caddr_t)ifr);
554 1.5 enami if (error != 0)
555 1.5 enami goto ioctl_failed;
556 1.5 enami return (error);
557 1.5 enami
558 1.5 enami ioctl_failed:
559 1.5 enami LIST_REMOVE(mc, mc_entries);
560 1.5 enami FREE(mc, M_DEVBUF);
561 1.5 enami alloc_failed:
562 1.5 enami (void)ether_delmulti(ifr, &ifv->ifv_ec);
563 1.5 enami return (error);
564 1.5 enami }
565 1.5 enami
566 1.5 enami static int
567 1.10 thorpej vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
568 1.5 enami {
569 1.5 enami struct ether_multi *enm;
570 1.5 enami struct vlan_mc_entry *mc;
571 1.5 enami u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
572 1.5 enami int error;
573 1.5 enami
574 1.5 enami /*
575 1.5 enami * Find a key to lookup vlan_mc_entry. We have to do this
576 1.5 enami * before calling ether_delmulti for obvious reason.
577 1.5 enami */
578 1.5 enami if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
579 1.5 enami return (error);
580 1.5 enami ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
581 1.5 enami
582 1.5 enami error = ether_delmulti(ifr, &ifv->ifv_ec);
583 1.5 enami if (error != ENETRESET)
584 1.5 enami return (error);
585 1.5 enami
586 1.5 enami /* We no longer use this multicast address. Tell parent so. */
587 1.5 enami error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
588 1.5 enami (caddr_t)ifr);
589 1.5 enami if (error == 0) {
590 1.5 enami /* And forget about this address. */
591 1.5 enami for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
592 1.5 enami mc = LIST_NEXT(mc, mc_entries)) {
593 1.5 enami if (mc->mc_enm == enm) {
594 1.5 enami LIST_REMOVE(mc, mc_entries);
595 1.5 enami FREE(mc, M_DEVBUF);
596 1.5 enami break;
597 1.5 enami }
598 1.5 enami }
599 1.5 enami KASSERT(mc != NULL);
600 1.5 enami } else
601 1.5 enami (void)ether_addmulti(ifr, &ifv->ifv_ec);
602 1.5 enami return (error);
603 1.5 enami }
604 1.5 enami
605 1.5 enami /*
606 1.5 enami * Delete any multicast address we have asked to add form parent
607 1.5 enami * interface. Called when the vlan is being unconfigured.
608 1.5 enami */
609 1.5 enami static void
610 1.10 thorpej vlan_ether_purgemulti(struct ifvlan *ifv)
611 1.5 enami {
612 1.5 enami struct ifnet *ifp = ifv->ifv_p; /* Parent. */
613 1.5 enami struct vlan_mc_entry *mc;
614 1.5 enami union {
615 1.5 enami struct ifreq ifreq;
616 1.5 enami struct {
617 1.5 enami char ifr_name[IFNAMSIZ];
618 1.5 enami struct sockaddr_storage;
619 1.5 enami } ifreq_storage;
620 1.5 enami } ifreq;
621 1.5 enami struct ifreq *ifr = &ifreq.ifreq;
622 1.5 enami
623 1.5 enami memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
624 1.5 enami while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
625 1.5 enami memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
626 1.5 enami (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
627 1.5 enami LIST_REMOVE(mc->mc_enm, enm_list);
628 1.5 enami free(mc->mc_enm, M_IFMADDR);
629 1.5 enami LIST_REMOVE(mc, mc_entries);
630 1.5 enami FREE(mc, M_DEVBUF);
631 1.1 thorpej }
632 1.1 thorpej
633 1.5 enami KASSERT(LIST_FIRST(&ifv->ifv_ec.ec_multiaddrs) == NULL);
634 1.1 thorpej }
635 1.1 thorpej
636 1.1 thorpej static void
637 1.1 thorpej vlan_start(struct ifnet *ifp)
638 1.1 thorpej {
639 1.14 enami struct ifvlan *ifv = ifp->if_softc;
640 1.14 enami struct ifnet *p = ifv->ifv_p;
641 1.1 thorpej struct mbuf *m;
642 1.1 thorpej
643 1.1 thorpej ifp->if_flags |= IFF_OACTIVE;
644 1.1 thorpej
645 1.1 thorpej for (;;) {
646 1.1 thorpej IF_DEQUEUE(&ifp->if_snd, m);
647 1.1 thorpej if (m == NULL)
648 1.1 thorpej break;
649 1.1 thorpej
650 1.1 thorpej #if NBPFILTER > 0
651 1.1 thorpej if (ifp->if_bpf)
652 1.1 thorpej bpf_mtap(ifp->if_bpf, m);
653 1.1 thorpej #endif
654 1.1 thorpej
655 1.1 thorpej /*
656 1.1 thorpej * XXX Should handle the case where the underlying hardware
657 1.1 thorpej * interface can do VLAN tag insertion itself.
658 1.1 thorpej */
659 1.10 thorpej M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
660 1.1 thorpej if (m == NULL) {
661 1.10 thorpej printf("%s: unable to prepend encap header",
662 1.10 thorpej ifv->ifv_p->if_xname);
663 1.10 thorpej ifp->if_oerrors++;
664 1.1 thorpej continue;
665 1.1 thorpej }
666 1.1 thorpej
667 1.10 thorpej switch (p->if_type) {
668 1.10 thorpej case IFT_ETHER:
669 1.10 thorpej {
670 1.10 thorpej struct ether_vlan_header *evl;
671 1.10 thorpej
672 1.10 thorpej if (m->m_len < sizeof(struct ether_vlan_header) &&
673 1.10 thorpej (m = m_pullup(m,
674 1.10 thorpej sizeof(struct ether_vlan_header))) == NULL) {
675 1.10 thorpej printf("%s: unable to pullup encap header",
676 1.10 thorpej ifv->ifv_p->if_xname);
677 1.10 thorpej ifp->if_oerrors++;
678 1.10 thorpej continue;
679 1.10 thorpej }
680 1.10 thorpej
681 1.10 thorpej /*
682 1.10 thorpej * Transform the Ethernet header into an Ethernet
683 1.10 thorpej * header with 802.1Q encapsulation.
684 1.10 thorpej */
685 1.10 thorpej memmove(mtod(m, caddr_t),
686 1.10 thorpej mtod(m, caddr_t) + ifv->ifv_encaplen,
687 1.10 thorpej sizeof(struct ether_header));
688 1.10 thorpej evl = mtod(m, struct ether_vlan_header *);
689 1.10 thorpej evl->evl_proto = evl->evl_encap_proto;
690 1.10 thorpej evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
691 1.10 thorpej evl->evl_tag = htons(ifv->ifv_tag);
692 1.10 thorpej break;
693 1.10 thorpej }
694 1.10 thorpej
695 1.10 thorpej #ifdef DIAGNOSTIC
696 1.10 thorpej default:
697 1.10 thorpej panic("vlan_start: impossible");
698 1.10 thorpej #endif
699 1.1 thorpej }
700 1.1 thorpej
701 1.1 thorpej /*
702 1.10 thorpej * Send it, precisely as the parent's output routine
703 1.10 thorpej * would have. We are already running at splimp.
704 1.1 thorpej */
705 1.1 thorpej if (IF_QFULL(&p->if_snd)) {
706 1.1 thorpej IF_DROP(&p->if_snd);
707 1.1 thorpej /* XXX stats */
708 1.1 thorpej ifp->if_oerrors++;
709 1.1 thorpej m_freem(m);
710 1.1 thorpej continue;
711 1.1 thorpej }
712 1.1 thorpej
713 1.1 thorpej IF_ENQUEUE(&p->if_snd, m);
714 1.1 thorpej if ((p->if_flags & IFF_OACTIVE) == 0) {
715 1.14 enami (*p->if_start)(p);
716 1.1 thorpej ifp->if_opackets++;
717 1.1 thorpej }
718 1.1 thorpej }
719 1.1 thorpej
720 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
721 1.1 thorpej }
722 1.1 thorpej
723 1.1 thorpej /*
724 1.1 thorpej * Given an Ethernet frame, find a valid vlan interface corresponding to the
725 1.1 thorpej * given source interface and tag, then run the the real packet through
726 1.1 thorpej * the parent's input routine.
727 1.1 thorpej */
728 1.1 thorpej void
729 1.1 thorpej vlan_input(struct ifnet *ifp, struct mbuf *m)
730 1.1 thorpej {
731 1.1 thorpej struct ifvlan *ifv;
732 1.1 thorpej u_int tag;
733 1.1 thorpej
734 1.10 thorpej switch (ifp->if_type) {
735 1.10 thorpej case IFT_ETHER:
736 1.10 thorpej {
737 1.10 thorpej struct ether_vlan_header *evl;
738 1.10 thorpej
739 1.10 thorpej if (m->m_len < sizeof(struct ether_vlan_header) &&
740 1.10 thorpej (m = m_pullup(m,
741 1.10 thorpej sizeof(struct ether_vlan_header))) == NULL) {
742 1.10 thorpej printf("%s: no memory for VLAN header, "
743 1.10 thorpej "dropping packet.\n", ifp->if_xname);
744 1.10 thorpej return;
745 1.10 thorpej }
746 1.10 thorpej evl = mtod(m, struct ether_vlan_header *);
747 1.10 thorpej KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
748 1.10 thorpej
749 1.10 thorpej tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
750 1.10 thorpej
751 1.10 thorpej /*
752 1.10 thorpej * Restore the original ethertype. We'll remove
753 1.10 thorpej * the encapsulation after we've found the vlan
754 1.10 thorpej * interface corresponding to the tag.
755 1.10 thorpej */
756 1.10 thorpej evl->evl_encap_proto = evl->evl_proto;
757 1.10 thorpej break;
758 1.10 thorpej }
759 1.10 thorpej
760 1.10 thorpej default:
761 1.10 thorpej tag = (u_int) -1; /* XXX GCC */
762 1.10 thorpej #ifdef DIAGNOSTIC
763 1.10 thorpej panic("vlan_input: impossible");
764 1.10 thorpej #endif
765 1.1 thorpej }
766 1.1 thorpej
767 1.1 thorpej for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
768 1.10 thorpej ifv = LIST_NEXT(ifv, ifv_list))
769 1.1 thorpej if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
770 1.1 thorpej break;
771 1.1 thorpej
772 1.10 thorpej if (ifv == NULL ||
773 1.10 thorpej (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
774 1.10 thorpej (IFF_UP|IFF_RUNNING)) {
775 1.1 thorpej m_free(m);
776 1.14 enami ifp->if_noproto++;
777 1.1 thorpej return;
778 1.1 thorpej }
779 1.1 thorpej
780 1.1 thorpej /*
781 1.10 thorpej * Now, remove the encapsulation header. The original
782 1.10 thorpej * header has already been fixed up above.
783 1.1 thorpej */
784 1.10 thorpej memmove(mtod(m, caddr_t) + ifv->ifv_encaplen, mtod(m, caddr_t),
785 1.10 thorpej ifv->ifv_encaplen);
786 1.10 thorpej m_adj(m, ifv->ifv_encaplen);
787 1.1 thorpej
788 1.1 thorpej m->m_pkthdr.rcvif = &ifv->ifv_if;
789 1.1 thorpej ifv->ifv_if.if_ipackets++;
790 1.1 thorpej
791 1.1 thorpej #if NBPFILTER > 0
792 1.1 thorpej if (ifv->ifv_if.if_bpf)
793 1.1 thorpej bpf_mtap(ifv->ifv_if.if_bpf, m);
794 1.1 thorpej #endif
795 1.1 thorpej
796 1.1 thorpej /* Pass it back through the parent's input routine. */
797 1.1 thorpej (*ifp->if_input)(&ifv->ifv_if, m);
798 1.1 thorpej }
799