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