if_vlan.c revision 1.169 1 1.169 yamaguch /* $NetBSD: if_vlan.c,v 1.169 2022/06/20 08:09:13 yamaguchi Exp $ */
2 1.1 thorpej
3 1.124 maxv /*
4 1.31 thorpej * Copyright (c) 2000, 2001 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 *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej /*
33 1.1 thorpej * Copyright 1998 Massachusetts Institute of Technology
34 1.1 thorpej *
35 1.1 thorpej * Permission to use, copy, modify, and distribute this software and
36 1.1 thorpej * its documentation for any purpose and without fee is hereby
37 1.1 thorpej * granted, provided that both the above copyright notice and this
38 1.1 thorpej * permission notice appear in all copies, that both the above
39 1.1 thorpej * copyright notice and this permission notice appear in all
40 1.1 thorpej * supporting documentation, and that the name of M.I.T. not be used
41 1.1 thorpej * in advertising or publicity pertaining to distribution of the
42 1.1 thorpej * software without specific, written prior permission. M.I.T. makes
43 1.1 thorpej * no representations about the suitability of this software for any
44 1.1 thorpej * purpose. It is provided "as is" without express or implied
45 1.1 thorpej * warranty.
46 1.44 perry *
47 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
48 1.1 thorpej * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
49 1.1 thorpej * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 1.1 thorpej * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
51 1.1 thorpej * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 1.1 thorpej * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 1.1 thorpej * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 1.1 thorpej * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 1.1 thorpej * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 1.1 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 1.1 thorpej * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 thorpej * SUCH DAMAGE.
59 1.1 thorpej *
60 1.1 thorpej * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
61 1.1 thorpej * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
62 1.1 thorpej */
63 1.1 thorpej
64 1.1 thorpej /*
65 1.1 thorpej * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
66 1.1 thorpej * extended some day to also handle IEEE 802.1P priority tagging. This is
67 1.1 thorpej * sort of sneaky in the implementation, since we need to pretend to be
68 1.1 thorpej * enough of an Ethernet implementation to make ARP work. The way we do
69 1.1 thorpej * this is by telling everyone that we are an Ethernet interface, and then
70 1.1 thorpej * catch the packets that ether_output() left on our output queue when it
71 1.1 thorpej * calls if_start(), rewrite them for use by the real outgoing interface,
72 1.1 thorpej * and ask it to send them.
73 1.1 thorpej *
74 1.1 thorpej * TODO:
75 1.1 thorpej *
76 1.1 thorpej * - Need some way to notify vlan interfaces when the parent
77 1.1 thorpej * interface changes MTU.
78 1.1 thorpej */
79 1.33 lukem
80 1.33 lukem #include <sys/cdefs.h>
81 1.169 yamaguch __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.169 2022/06/20 08:09:13 yamaguchi Exp $");
82 1.1 thorpej
83 1.79 ozaki #ifdef _KERNEL_OPT
84 1.1 thorpej #include "opt_inet.h"
85 1.121 ozaki #include "opt_net_mpsafe.h"
86 1.79 ozaki #endif
87 1.1 thorpej
88 1.1 thorpej #include <sys/param.h>
89 1.98 knakahar #include <sys/systm.h>
90 1.1 thorpej #include <sys/kernel.h>
91 1.1 thorpej #include <sys/mbuf.h>
92 1.1 thorpej #include <sys/queue.h>
93 1.1 thorpej #include <sys/socket.h>
94 1.1 thorpej #include <sys/sockio.h>
95 1.1 thorpej #include <sys/systm.h>
96 1.1 thorpej #include <sys/proc.h>
97 1.48 elad #include <sys/kauth.h>
98 1.76 ozaki #include <sys/mutex.h>
99 1.98 knakahar #include <sys/kmem.h>
100 1.98 knakahar #include <sys/cpu.h>
101 1.98 knakahar #include <sys/pserialize.h>
102 1.98 knakahar #include <sys/psref.h>
103 1.98 knakahar #include <sys/pslist.h>
104 1.98 knakahar #include <sys/atomic.h>
105 1.91 christos #include <sys/device.h>
106 1.91 christos #include <sys/module.h>
107 1.1 thorpej
108 1.1 thorpej #include <net/bpf.h>
109 1.1 thorpej #include <net/if.h>
110 1.1 thorpej #include <net/if_dl.h>
111 1.1 thorpej #include <net/if_types.h>
112 1.1 thorpej #include <net/if_ether.h>
113 1.1 thorpej #include <net/if_vlanvar.h>
114 1.1 thorpej
115 1.1 thorpej #ifdef INET
116 1.1 thorpej #include <netinet/in.h>
117 1.1 thorpej #include <netinet/if_inarp.h>
118 1.1 thorpej #endif
119 1.74 ozaki #ifdef INET6
120 1.74 ozaki #include <netinet6/in6_ifattach.h>
121 1.97 ozaki #include <netinet6/in6_var.h>
122 1.148 yamaguch #include <netinet6/nd6.h>
123 1.74 ozaki #endif
124 1.1 thorpej
125 1.82 christos #include "ioconf.h"
126 1.82 christos
127 1.10 thorpej struct vlan_mc_entry {
128 1.10 thorpej LIST_ENTRY(vlan_mc_entry) mc_entries;
129 1.10 thorpej /*
130 1.10 thorpej * A key to identify this entry. The mc_addr below can't be
131 1.10 thorpej * used since multiple sockaddr may mapped into the same
132 1.10 thorpej * ether_multi (e.g., AF_UNSPEC).
133 1.10 thorpej */
134 1.141 msaitoh struct ether_multi *mc_enm;
135 1.10 thorpej struct sockaddr_storage mc_addr;
136 1.10 thorpej };
137 1.10 thorpej
138 1.98 knakahar struct ifvlan_linkmib {
139 1.98 knakahar struct ifvlan *ifvm_ifvlan;
140 1.98 knakahar const struct vlan_multisw *ifvm_msw;
141 1.98 knakahar int ifvm_mtufudge; /* MTU fudged by this much */
142 1.98 knakahar int ifvm_mintu; /* min transmission unit */
143 1.98 knakahar uint16_t ifvm_proto; /* encapsulation ethertype */
144 1.98 knakahar uint16_t ifvm_tag; /* tag to apply on packets */
145 1.137 msaitoh struct ifnet *ifvm_p; /* parent interface of this vlan */
146 1.98 knakahar
147 1.98 knakahar struct psref_target ifvm_psref;
148 1.98 knakahar };
149 1.98 knakahar
150 1.10 thorpej struct ifvlan {
151 1.141 msaitoh struct ethercom ifv_ec;
152 1.98 knakahar struct ifvlan_linkmib *ifv_mib; /*
153 1.98 knakahar * reader must use vlan_getref_linkmib()
154 1.98 knakahar * instead of direct dereference
155 1.98 knakahar */
156 1.98 knakahar kmutex_t ifv_lock; /* writer lock for ifv_mib */
157 1.131 jmcneill pserialize_t ifv_psz;
158 1.162 yamaguch void *ifv_linkstate_hook;
159 1.163 yamaguch void *ifv_ifdetach_hook;
160 1.98 knakahar
161 1.10 thorpej LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
162 1.98 knakahar struct pslist_entry ifv_hash;
163 1.17 thorpej int ifv_flags;
164 1.163 yamaguch bool ifv_stopping;
165 1.10 thorpej };
166 1.10 thorpej
167 1.17 thorpej #define IFVF_PROMISC 0x01 /* promiscuous mode enabled */
168 1.17 thorpej
169 1.10 thorpej #define ifv_if ifv_ec.ec_if
170 1.10 thorpej
171 1.10 thorpej #define ifv_msw ifv_mib.ifvm_msw
172 1.10 thorpej #define ifv_mtufudge ifv_mib.ifvm_mtufudge
173 1.10 thorpej #define ifv_mintu ifv_mib.ifvm_mintu
174 1.10 thorpej #define ifv_tag ifv_mib.ifvm_tag
175 1.10 thorpej
176 1.10 thorpej struct vlan_multisw {
177 1.10 thorpej int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
178 1.10 thorpej int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
179 1.10 thorpej void (*vmsw_purgemulti)(struct ifvlan *);
180 1.10 thorpej };
181 1.10 thorpej
182 1.10 thorpej static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
183 1.10 thorpej static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
184 1.10 thorpej static void vlan_ether_purgemulti(struct ifvlan *);
185 1.10 thorpej
186 1.10 thorpej const struct vlan_multisw vlan_ether_multisw = {
187 1.123 maxv .vmsw_addmulti = vlan_ether_addmulti,
188 1.123 maxv .vmsw_delmulti = vlan_ether_delmulti,
189 1.123 maxv .vmsw_purgemulti = vlan_ether_purgemulti,
190 1.10 thorpej };
191 1.10 thorpej
192 1.1 thorpej static int vlan_clone_create(struct if_clone *, int);
193 1.42 peter static int vlan_clone_destroy(struct ifnet *);
194 1.137 msaitoh static int vlan_config(struct ifvlan *, struct ifnet *, uint16_t);
195 1.53 christos static int vlan_ioctl(struct ifnet *, u_long, void *);
196 1.1 thorpej static void vlan_start(struct ifnet *);
197 1.98 knakahar static int vlan_transmit(struct ifnet *, struct mbuf *);
198 1.162 yamaguch static void vlan_link_state_changed(void *);
199 1.163 yamaguch static void vlan_ifdetach(void *);
200 1.11 thorpej static void vlan_unconfig(struct ifnet *);
201 1.137 msaitoh static int vlan_unconfig_locked(struct ifvlan *, struct ifvlan_linkmib *);
202 1.98 knakahar static void vlan_hash_init(void);
203 1.98 knakahar static int vlan_hash_fini(void);
204 1.110 msaitoh static int vlan_tag_hash(uint16_t, u_long);
205 1.168 yamaguch static struct ifvlan_linkmib*
206 1.168 yamaguch vlan_getref_linkmib(struct ifvlan *, struct psref *);
207 1.137 msaitoh static void vlan_putref_linkmib(struct ifvlan_linkmib *, struct psref *);
208 1.137 msaitoh static void vlan_linkmib_update(struct ifvlan *, struct ifvlan_linkmib *);
209 1.168 yamaguch static struct ifvlan_linkmib*
210 1.168 yamaguch vlan_lookup_tag_psref(struct ifnet *, uint16_t,
211 1.168 yamaguch struct psref *);
212 1.98 knakahar
213 1.98 knakahar #if !defined(VLAN_TAG_HASH_SIZE)
214 1.98 knakahar #define VLAN_TAG_HASH_SIZE 32
215 1.98 knakahar #endif
216 1.98 knakahar static struct {
217 1.98 knakahar kmutex_t lock;
218 1.98 knakahar struct pslist_head *lists;
219 1.98 knakahar u_long mask;
220 1.98 knakahar } ifv_hash __cacheline_aligned = {
221 1.98 knakahar .lists = NULL,
222 1.98 knakahar .mask = 0,
223 1.98 knakahar };
224 1.98 knakahar
225 1.98 knakahar pserialize_t vlan_psz __read_mostly;
226 1.98 knakahar static struct psref_class *ifvm_psref_class __read_mostly;
227 1.76 ozaki
228 1.1 thorpej struct if_clone vlan_cloner =
229 1.1 thorpej IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
230 1.1 thorpej
231 1.38 scw /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
232 1.38 scw static char vlan_zero_pad_buff[ETHER_MIN_LEN];
233 1.38 scw
234 1.164 yamaguch static uint32_t nvlanifs;
235 1.164 yamaguch
236 1.109 ozaki static inline int
237 1.109 ozaki vlan_safe_ifpromisc(struct ifnet *ifp, int pswitch)
238 1.109 ozaki {
239 1.109 ozaki int e;
240 1.124 maxv
241 1.109 ozaki KERNEL_LOCK_UNLESS_NET_MPSAFE();
242 1.109 ozaki e = ifpromisc(ifp, pswitch);
243 1.109 ozaki KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
244 1.124 maxv
245 1.109 ozaki return e;
246 1.109 ozaki }
247 1.109 ozaki
248 1.161 hannken __unused static inline int
249 1.115 ozaki vlan_safe_ifpromisc_locked(struct ifnet *ifp, int pswitch)
250 1.115 ozaki {
251 1.115 ozaki int e;
252 1.124 maxv
253 1.115 ozaki KERNEL_LOCK_UNLESS_NET_MPSAFE();
254 1.115 ozaki e = ifpromisc_locked(ifp, pswitch);
255 1.115 ozaki KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
256 1.124 maxv
257 1.115 ozaki return e;
258 1.115 ozaki }
259 1.115 ozaki
260 1.1 thorpej void
261 1.52 christos vlanattach(int n)
262 1.1 thorpej {
263 1.1 thorpej
264 1.91 christos /*
265 1.91 christos * Nothing to do here, initialization is handled by the
266 1.124 maxv * module initialization code in vlaninit() below.
267 1.91 christos */
268 1.91 christos }
269 1.91 christos
270 1.91 christos static void
271 1.91 christos vlaninit(void)
272 1.91 christos {
273 1.164 yamaguch nvlanifs = 0;
274 1.91 christos
275 1.98 knakahar mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE);
276 1.98 knakahar vlan_psz = pserialize_create();
277 1.98 knakahar ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET);
278 1.1 thorpej if_clone_attach(&vlan_cloner);
279 1.98 knakahar
280 1.98 knakahar vlan_hash_init();
281 1.149 pgoyette MODULE_HOOK_SET(if_vlan_vlan_input_hook, vlan_input);
282 1.1 thorpej }
283 1.1 thorpej
284 1.91 christos static int
285 1.91 christos vlandetach(void)
286 1.91 christos {
287 1.124 maxv int error;
288 1.91 christos
289 1.164 yamaguch if (nvlanifs > 0)
290 1.98 knakahar return EBUSY;
291 1.98 knakahar
292 1.98 knakahar error = vlan_hash_fini();
293 1.98 knakahar if (error != 0)
294 1.98 knakahar return error;
295 1.91 christos
296 1.98 knakahar if_clone_detach(&vlan_cloner);
297 1.98 knakahar psref_class_destroy(ifvm_psref_class);
298 1.98 knakahar pserialize_destroy(vlan_psz);
299 1.98 knakahar mutex_destroy(&ifv_hash.lock);
300 1.91 christos
301 1.134 pgoyette MODULE_HOOK_UNSET(if_vlan_vlan_input_hook);
302 1.98 knakahar return 0;
303 1.91 christos }
304 1.91 christos
305 1.30 thorpej static void
306 1.30 thorpej vlan_reset_linkname(struct ifnet *ifp)
307 1.30 thorpej {
308 1.30 thorpej
309 1.30 thorpej /*
310 1.30 thorpej * We start out with a "802.1Q VLAN" type and zero-length
311 1.30 thorpej * addresses. When we attach to a parent interface, we
312 1.30 thorpej * inherit its type, address length, address, and data link
313 1.30 thorpej * type.
314 1.30 thorpej */
315 1.30 thorpej
316 1.30 thorpej ifp->if_type = IFT_L2VLAN;
317 1.30 thorpej ifp->if_addrlen = 0;
318 1.30 thorpej ifp->if_dlt = DLT_NULL;
319 1.30 thorpej if_alloc_sadl(ifp);
320 1.30 thorpej }
321 1.30 thorpej
322 1.1 thorpej static int
323 1.1 thorpej vlan_clone_create(struct if_clone *ifc, int unit)
324 1.1 thorpej {
325 1.1 thorpej struct ifvlan *ifv;
326 1.1 thorpej struct ifnet *ifp;
327 1.98 knakahar struct ifvlan_linkmib *mib;
328 1.1 thorpej
329 1.137 msaitoh ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK | M_ZERO);
330 1.98 knakahar mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP);
331 1.14 enami ifp = &ifv->ifv_if;
332 1.5 enami LIST_INIT(&ifv->ifv_mc_listhead);
333 1.11 thorpej
334 1.98 knakahar mib->ifvm_ifvlan = ifv;
335 1.98 knakahar mib->ifvm_p = NULL;
336 1.98 knakahar psref_target_init(&mib->ifvm_psref, ifvm_psref_class);
337 1.98 knakahar
338 1.98 knakahar mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE);
339 1.131 jmcneill ifv->ifv_psz = pserialize_create();
340 1.98 knakahar ifv->ifv_mib = mib;
341 1.98 knakahar
342 1.164 yamaguch atomic_inc_uint(&nvlanifs);
343 1.1 thorpej
344 1.59 christos if_initname(ifp, ifc->ifc_name, unit);
345 1.1 thorpej ifp->if_softc = ifv;
346 1.1 thorpej ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
347 1.121 ozaki #ifdef NET_MPSAFE
348 1.153 roy ifp->if_extflags = IFEF_MPSAFE;
349 1.121 ozaki #endif
350 1.1 thorpej ifp->if_start = vlan_start;
351 1.98 knakahar ifp->if_transmit = vlan_transmit;
352 1.1 thorpej ifp->if_ioctl = vlan_ioctl;
353 1.31 thorpej IFQ_SET_READY(&ifp->if_snd);
354 1.154 riastrad if_initialize(ifp);
355 1.153 roy /*
356 1.153 roy * Set the link state to down.
357 1.153 roy * When the parent interface attaches we will use that link state.
358 1.153 roy * When the parent interface link state changes, so will ours.
359 1.155 yamaguch * When the parent interface detaches, set the link state to down.
360 1.153 roy */
361 1.153 roy ifp->if_link_state = LINK_STATE_DOWN;
362 1.153 roy
363 1.30 thorpej vlan_reset_linkname(ifp);
364 1.84 ozaki if_register(ifp);
365 1.105 msaitoh return 0;
366 1.1 thorpej }
367 1.1 thorpej
368 1.42 peter static int
369 1.1 thorpej vlan_clone_destroy(struct ifnet *ifp)
370 1.1 thorpej {
371 1.11 thorpej struct ifvlan *ifv = ifp->if_softc;
372 1.1 thorpej
373 1.164 yamaguch atomic_dec_uint(&nvlanifs);
374 1.98 knakahar
375 1.119 ozaki IFNET_LOCK(ifp);
376 1.1 thorpej vlan_unconfig(ifp);
377 1.119 ozaki IFNET_UNLOCK(ifp);
378 1.78 ozaki if_detach(ifp);
379 1.1 thorpej
380 1.98 knakahar psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class);
381 1.98 knakahar kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib));
382 1.133 knakahar pserialize_destroy(ifv->ifv_psz);
383 1.98 knakahar mutex_destroy(&ifv->ifv_lock);
384 1.1 thorpej free(ifv, M_DEVBUF);
385 1.42 peter
386 1.124 maxv return 0;
387 1.1 thorpej }
388 1.1 thorpej
389 1.11 thorpej /*
390 1.98 knakahar * Configure a VLAN interface.
391 1.11 thorpej */
392 1.1 thorpej static int
393 1.98 knakahar vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
394 1.1 thorpej {
395 1.10 thorpej struct ifnet *ifp = &ifv->ifv_if;
396 1.98 knakahar struct ifvlan_linkmib *nmib = NULL;
397 1.98 knakahar struct ifvlan_linkmib *omib = NULL;
398 1.124 maxv struct ifvlan_linkmib *checkmib;
399 1.98 knakahar struct psref_target *nmib_psref = NULL;
400 1.124 maxv const uint16_t vid = EVL_VLANOFTAG(tag);
401 1.98 knakahar int error = 0;
402 1.98 knakahar int idx;
403 1.98 knakahar bool omib_cleanup = false;
404 1.101 msaitoh struct psref psref;
405 1.98 knakahar
406 1.112 msaitoh /* VLAN ID 0 and 4095 are reserved in the spec */
407 1.111 msaitoh if ((vid == 0) || (vid == 0xfff))
408 1.111 msaitoh return EINVAL;
409 1.111 msaitoh
410 1.98 knakahar nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
411 1.98 knakahar mutex_enter(&ifv->ifv_lock);
412 1.98 knakahar omib = ifv->ifv_mib;
413 1.98 knakahar
414 1.98 knakahar if (omib->ifvm_p != NULL) {
415 1.98 knakahar error = EBUSY;
416 1.98 knakahar goto done;
417 1.98 knakahar }
418 1.98 knakahar
419 1.101 msaitoh /* Duplicate check */
420 1.108 msaitoh checkmib = vlan_lookup_tag_psref(p, vid, &psref);
421 1.101 msaitoh if (checkmib != NULL) {
422 1.101 msaitoh vlan_putref_linkmib(checkmib, &psref);
423 1.101 msaitoh error = EEXIST;
424 1.101 msaitoh goto done;
425 1.101 msaitoh }
426 1.101 msaitoh
427 1.98 knakahar *nmib = *omib;
428 1.98 knakahar nmib_psref = &nmib->ifvm_psref;
429 1.1 thorpej
430 1.98 knakahar psref_target_init(nmib_psref, ifvm_psref_class);
431 1.10 thorpej
432 1.10 thorpej switch (p->if_type) {
433 1.10 thorpej case IFT_ETHER:
434 1.10 thorpej {
435 1.124 maxv struct ethercom *ec = (void *)p;
436 1.141 msaitoh
437 1.98 knakahar nmib->ifvm_msw = &vlan_ether_multisw;
438 1.98 knakahar nmib->ifvm_mintu = ETHERMIN;
439 1.10 thorpej
440 1.166 yamaguch error = ether_add_vlantag(p, tag, NULL);
441 1.165 yamaguch if (error != 0)
442 1.165 yamaguch goto done;
443 1.144 msaitoh
444 1.166 yamaguch if (ec->ec_capenable & ETHERCAP_VLAN_MTU) {
445 1.165 yamaguch nmib->ifvm_mtufudge = 0;
446 1.165 yamaguch } else {
447 1.144 msaitoh /*
448 1.165 yamaguch * Fudge the MTU by the encapsulation size. This
449 1.165 yamaguch * makes us incompatible with strictly compliant
450 1.165 yamaguch * 802.1Q implementations, but allows us to use
451 1.165 yamaguch * the feature with other NetBSD
452 1.165 yamaguch * implementations, which might still be useful.
453 1.144 msaitoh */
454 1.169 yamaguch nmib->ifvm_mtufudge = ETHER_VLAN_ENCAP_LEN;
455 1.141 msaitoh }
456 1.165 yamaguch
457 1.10 thorpej /*
458 1.32 thorpej * If the parent interface can do hardware-assisted
459 1.32 thorpej * VLAN encapsulation, then propagate its hardware-
460 1.63 darran * assisted checksumming flags and tcp segmentation
461 1.63 darran * offload.
462 1.32 thorpej */
463 1.67 sborrill if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
464 1.32 thorpej ifp->if_capabilities = p->if_capabilities &
465 1.65 darran (IFCAP_TSOv4 | IFCAP_TSOv6 |
466 1.137 msaitoh IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
467 1.137 msaitoh IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
468 1.137 msaitoh IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
469 1.137 msaitoh IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
470 1.137 msaitoh IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx);
471 1.124 maxv }
472 1.124 maxv
473 1.32 thorpej /*
474 1.10 thorpej * We inherit the parent's Ethernet address.
475 1.10 thorpej */
476 1.54 dyoung ether_ifattach(ifp, CLLADDR(p->if_sadl));
477 1.10 thorpej ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
478 1.10 thorpej break;
479 1.10 thorpej }
480 1.10 thorpej
481 1.10 thorpej default:
482 1.98 knakahar error = EPROTONOSUPPORT;
483 1.98 knakahar goto done;
484 1.10 thorpej }
485 1.10 thorpej
486 1.98 knakahar nmib->ifvm_p = p;
487 1.108 msaitoh nmib->ifvm_tag = vid;
488 1.98 knakahar ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge;
489 1.21 bouyer ifv->ifv_if.if_flags = p->if_flags &
490 1.23 bouyer (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
491 1.1 thorpej
492 1.1 thorpej /*
493 1.10 thorpej * Inherit the if_type from the parent. This allows us
494 1.10 thorpej * to participate in bridges of that type.
495 1.1 thorpej */
496 1.10 thorpej ifv->ifv_if.if_type = p->if_type;
497 1.10 thorpej
498 1.104 knakahar PSLIST_ENTRY_INIT(ifv, ifv_hash);
499 1.110 msaitoh idx = vlan_tag_hash(vid, ifv_hash.mask);
500 1.98 knakahar
501 1.98 knakahar mutex_enter(&ifv_hash.lock);
502 1.98 knakahar PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash);
503 1.98 knakahar mutex_exit(&ifv_hash.lock);
504 1.98 knakahar
505 1.98 knakahar vlan_linkmib_update(ifv, nmib);
506 1.98 knakahar nmib = NULL;
507 1.98 knakahar nmib_psref = NULL;
508 1.98 knakahar omib_cleanup = true;
509 1.98 knakahar
510 1.163 yamaguch ifv->ifv_ifdetach_hook = ether_ifdetachhook_establish(p,
511 1.163 yamaguch vlan_ifdetach, ifp);
512 1.163 yamaguch
513 1.153 roy /*
514 1.153 roy * We inherit the parents link state.
515 1.153 roy */
516 1.162 yamaguch ifv->ifv_linkstate_hook = if_linkstate_change_establish(p,
517 1.162 yamaguch vlan_link_state_changed, ifv);
518 1.153 roy if_link_state_change(&ifv->ifv_if, p->if_link_state);
519 1.153 roy
520 1.98 knakahar done:
521 1.98 knakahar mutex_exit(&ifv->ifv_lock);
522 1.98 knakahar
523 1.98 knakahar if (nmib_psref)
524 1.98 knakahar psref_target_destroy(nmib_psref, ifvm_psref_class);
525 1.98 knakahar if (nmib)
526 1.98 knakahar kmem_free(nmib, sizeof(*nmib));
527 1.98 knakahar if (omib_cleanup)
528 1.98 knakahar kmem_free(omib, sizeof(*omib));
529 1.98 knakahar
530 1.98 knakahar return error;
531 1.1 thorpej }
532 1.1 thorpej
533 1.11 thorpej /*
534 1.98 knakahar * Unconfigure a VLAN interface.
535 1.11 thorpej */
536 1.11 thorpej static void
537 1.1 thorpej vlan_unconfig(struct ifnet *ifp)
538 1.1 thorpej {
539 1.10 thorpej struct ifvlan *ifv = ifp->if_softc;
540 1.98 knakahar struct ifvlan_linkmib *nmib = NULL;
541 1.98 knakahar int error;
542 1.98 knakahar
543 1.119 ozaki KASSERT(IFNET_LOCKED(ifp));
544 1.115 ozaki
545 1.98 knakahar nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
546 1.98 knakahar
547 1.98 knakahar mutex_enter(&ifv->ifv_lock);
548 1.98 knakahar error = vlan_unconfig_locked(ifv, nmib);
549 1.98 knakahar mutex_exit(&ifv->ifv_lock);
550 1.98 knakahar
551 1.98 knakahar if (error)
552 1.98 knakahar kmem_free(nmib, sizeof(*nmib));
553 1.98 knakahar }
554 1.98 knakahar static int
555 1.98 knakahar vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
556 1.98 knakahar {
557 1.77 ozaki struct ifnet *p;
558 1.98 knakahar struct ifnet *ifp = &ifv->ifv_if;
559 1.98 knakahar struct psref_target *nmib_psref = NULL;
560 1.98 knakahar struct ifvlan_linkmib *omib;
561 1.98 knakahar int error = 0;
562 1.98 knakahar
563 1.119 ozaki KASSERT(IFNET_LOCKED(ifp));
564 1.98 knakahar KASSERT(mutex_owned(&ifv->ifv_lock));
565 1.1 thorpej
566 1.163 yamaguch if (ifv->ifv_stopping) {
567 1.163 yamaguch error = -1;
568 1.163 yamaguch goto done;
569 1.163 yamaguch }
570 1.163 yamaguch
571 1.137 msaitoh ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
572 1.116 ozaki
573 1.98 knakahar omib = ifv->ifv_mib;
574 1.98 knakahar p = omib->ifvm_p;
575 1.76 ozaki
576 1.77 ozaki if (p == NULL) {
577 1.98 knakahar error = -1;
578 1.98 knakahar goto done;
579 1.76 ozaki }
580 1.1 thorpej
581 1.98 knakahar *nmib = *omib;
582 1.98 knakahar nmib_psref = &nmib->ifvm_psref;
583 1.98 knakahar psref_target_init(nmib_psref, ifvm_psref_class);
584 1.98 knakahar
585 1.1 thorpej /*
586 1.137 msaitoh * Since the interface is being unconfigured, we need to empty the
587 1.1 thorpej * list of multicast groups that we may have joined while we were
588 1.1 thorpej * alive and remove them from the parent's list also.
589 1.1 thorpej */
590 1.98 knakahar (*nmib->ifvm_msw->vmsw_purgemulti)(ifv);
591 1.1 thorpej
592 1.1 thorpej /* Disconnect from parent. */
593 1.77 ozaki switch (p->if_type) {
594 1.10 thorpej case IFT_ETHER:
595 1.10 thorpej {
596 1.165 yamaguch (void)ether_del_vlantag(p, nmib->ifvm_tag);
597 1.10 thorpej
598 1.126 ozaki /* XXX ether_ifdetach must not be called with IFNET_LOCK */
599 1.163 yamaguch ifv->ifv_stopping = true;
600 1.126 ozaki mutex_exit(&ifv->ifv_lock);
601 1.126 ozaki IFNET_UNLOCK(ifp);
602 1.10 thorpej ether_ifdetach(ifp);
603 1.126 ozaki IFNET_LOCK(ifp);
604 1.126 ozaki mutex_enter(&ifv->ifv_lock);
605 1.163 yamaguch ifv->ifv_stopping = false;
606 1.126 ozaki
607 1.132 knakahar /* if_free_sadl must be called with IFNET_LOCK */
608 1.132 knakahar if_free_sadl(ifp, 1);
609 1.132 knakahar
610 1.71 ozaki /* Restore vlan_ioctl overwritten by ether_ifdetach */
611 1.71 ozaki ifp->if_ioctl = vlan_ioctl;
612 1.30 thorpej vlan_reset_linkname(ifp);
613 1.10 thorpej break;
614 1.10 thorpej }
615 1.10 thorpej
616 1.10 thorpej default:
617 1.124 maxv panic("%s: impossible", __func__);
618 1.10 thorpej }
619 1.10 thorpej
620 1.98 knakahar nmib->ifvm_p = NULL;
621 1.10 thorpej ifv->ifv_if.if_mtu = 0;
622 1.17 thorpej ifv->ifv_flags = 0;
623 1.1 thorpej
624 1.98 knakahar mutex_enter(&ifv_hash.lock);
625 1.98 knakahar PSLIST_WRITER_REMOVE(ifv, ifv_hash);
626 1.98 knakahar pserialize_perform(vlan_psz);
627 1.98 knakahar mutex_exit(&ifv_hash.lock);
628 1.104 knakahar PSLIST_ENTRY_DESTROY(ifv, ifv_hash);
629 1.162 yamaguch if_linkstate_change_disestablish(p,
630 1.162 yamaguch ifv->ifv_linkstate_hook, NULL);
631 1.98 knakahar
632 1.98 knakahar vlan_linkmib_update(ifv, nmib);
633 1.155 yamaguch if_link_state_change(ifp, LINK_STATE_DOWN);
634 1.98 knakahar
635 1.163 yamaguch /*XXX ether_ifdetachhook_disestablish must not called with IFNET_LOCK */
636 1.163 yamaguch IFNET_UNLOCK(ifp);
637 1.163 yamaguch ether_ifdetachhook_disestablish(p, ifv->ifv_ifdetach_hook,
638 1.163 yamaguch &ifv->ifv_lock);
639 1.98 knakahar mutex_exit(&ifv->ifv_lock);
640 1.163 yamaguch IFNET_LOCK(ifp);
641 1.98 knakahar
642 1.98 knakahar nmib_psref = NULL;
643 1.98 knakahar kmem_free(omib, sizeof(*omib));
644 1.98 knakahar
645 1.74 ozaki #ifdef INET6
646 1.109 ozaki KERNEL_LOCK_UNLESS_NET_MPSAFE();
647 1.74 ozaki /* To delete v6 link local addresses */
648 1.97 ozaki if (in6_present)
649 1.97 ozaki in6_ifdetach(ifp);
650 1.109 ozaki KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
651 1.74 ozaki #endif
652 1.98 knakahar
653 1.117 ozaki if_down_locked(ifp);
654 1.32 thorpej ifp->if_capabilities = 0;
655 1.98 knakahar mutex_enter(&ifv->ifv_lock);
656 1.98 knakahar done:
657 1.98 knakahar if (nmib_psref)
658 1.98 knakahar psref_target_destroy(nmib_psref, ifvm_psref_class);
659 1.76 ozaki
660 1.98 knakahar return error;
661 1.98 knakahar }
662 1.98 knakahar
663 1.98 knakahar static void
664 1.98 knakahar vlan_hash_init(void)
665 1.98 knakahar {
666 1.98 knakahar
667 1.98 knakahar ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true,
668 1.98 knakahar &ifv_hash.mask);
669 1.98 knakahar }
670 1.98 knakahar
671 1.98 knakahar static int
672 1.98 knakahar vlan_hash_fini(void)
673 1.98 knakahar {
674 1.98 knakahar int i;
675 1.98 knakahar
676 1.98 knakahar mutex_enter(&ifv_hash.lock);
677 1.98 knakahar
678 1.98 knakahar for (i = 0; i < ifv_hash.mask + 1; i++) {
679 1.98 knakahar if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan,
680 1.98 knakahar ifv_hash) != NULL) {
681 1.98 knakahar mutex_exit(&ifv_hash.lock);
682 1.98 knakahar return EBUSY;
683 1.98 knakahar }
684 1.98 knakahar }
685 1.98 knakahar
686 1.98 knakahar for (i = 0; i < ifv_hash.mask + 1; i++)
687 1.98 knakahar PSLIST_DESTROY(&ifv_hash.lists[i]);
688 1.98 knakahar
689 1.98 knakahar mutex_exit(&ifv_hash.lock);
690 1.98 knakahar
691 1.98 knakahar hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask);
692 1.98 knakahar
693 1.98 knakahar ifv_hash.lists = NULL;
694 1.98 knakahar ifv_hash.mask = 0;
695 1.98 knakahar
696 1.98 knakahar return 0;
697 1.98 knakahar }
698 1.98 knakahar
699 1.98 knakahar static int
700 1.110 msaitoh vlan_tag_hash(uint16_t tag, u_long mask)
701 1.98 knakahar {
702 1.98 knakahar uint32_t hash;
703 1.98 knakahar
704 1.98 knakahar hash = (tag >> 8) ^ tag;
705 1.98 knakahar hash = (hash >> 2) ^ hash;
706 1.98 knakahar
707 1.98 knakahar return hash & mask;
708 1.98 knakahar }
709 1.98 knakahar
710 1.98 knakahar static struct ifvlan_linkmib *
711 1.98 knakahar vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref)
712 1.98 knakahar {
713 1.98 knakahar struct ifvlan_linkmib *mib;
714 1.98 knakahar int s;
715 1.98 knakahar
716 1.98 knakahar s = pserialize_read_enter();
717 1.151 riastrad mib = atomic_load_consume(&sc->ifv_mib);
718 1.98 knakahar if (mib == NULL) {
719 1.98 knakahar pserialize_read_exit(s);
720 1.98 knakahar return NULL;
721 1.98 knakahar }
722 1.98 knakahar psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
723 1.98 knakahar pserialize_read_exit(s);
724 1.98 knakahar
725 1.98 knakahar return mib;
726 1.98 knakahar }
727 1.98 knakahar
728 1.98 knakahar static void
729 1.98 knakahar vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref)
730 1.98 knakahar {
731 1.98 knakahar if (mib == NULL)
732 1.98 knakahar return;
733 1.98 knakahar psref_release(psref, &mib->ifvm_psref, ifvm_psref_class);
734 1.98 knakahar }
735 1.98 knakahar
736 1.98 knakahar static struct ifvlan_linkmib *
737 1.98 knakahar vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref)
738 1.98 knakahar {
739 1.98 knakahar int idx;
740 1.98 knakahar int s;
741 1.98 knakahar struct ifvlan *sc;
742 1.98 knakahar
743 1.110 msaitoh idx = vlan_tag_hash(tag, ifv_hash.mask);
744 1.98 knakahar
745 1.98 knakahar s = pserialize_read_enter();
746 1.98 knakahar PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan,
747 1.98 knakahar ifv_hash) {
748 1.151 riastrad struct ifvlan_linkmib *mib = atomic_load_consume(&sc->ifv_mib);
749 1.98 knakahar if (mib == NULL)
750 1.98 knakahar continue;
751 1.98 knakahar if (mib->ifvm_tag != tag)
752 1.98 knakahar continue;
753 1.98 knakahar if (mib->ifvm_p != ifp)
754 1.98 knakahar continue;
755 1.98 knakahar
756 1.98 knakahar psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
757 1.98 knakahar pserialize_read_exit(s);
758 1.98 knakahar return mib;
759 1.98 knakahar }
760 1.98 knakahar pserialize_read_exit(s);
761 1.98 knakahar return NULL;
762 1.98 knakahar }
763 1.98 knakahar
764 1.98 knakahar static void
765 1.98 knakahar vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
766 1.98 knakahar {
767 1.98 knakahar struct ifvlan_linkmib *omib = ifv->ifv_mib;
768 1.98 knakahar
769 1.98 knakahar KASSERT(mutex_owned(&ifv->ifv_lock));
770 1.98 knakahar
771 1.151 riastrad atomic_store_release(&ifv->ifv_mib, nmib);
772 1.98 knakahar
773 1.131 jmcneill pserialize_perform(ifv->ifv_psz);
774 1.98 knakahar psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class);
775 1.11 thorpej }
776 1.11 thorpej
777 1.11 thorpej /*
778 1.11 thorpej * Called when a parent interface is detaching; destroy any VLAN
779 1.11 thorpej * configuration for the parent interface.
780 1.11 thorpej */
781 1.163 yamaguch static void
782 1.163 yamaguch vlan_ifdetach(void *xifp)
783 1.11 thorpej {
784 1.163 yamaguch struct ifnet *ifp;
785 1.98 knakahar
786 1.163 yamaguch ifp = (struct ifnet *)xifp;
787 1.122 maxv
788 1.163 yamaguch /* IFNET_LOCK must be held before ifv_lock. */
789 1.163 yamaguch IFNET_LOCK(ifp);
790 1.163 yamaguch vlan_unconfig(ifp);
791 1.163 yamaguch IFNET_UNLOCK(ifp);
792 1.1 thorpej }
793 1.1 thorpej
794 1.1 thorpej static int
795 1.17 thorpej vlan_set_promisc(struct ifnet *ifp)
796 1.17 thorpej {
797 1.17 thorpej struct ifvlan *ifv = ifp->if_softc;
798 1.98 knakahar struct ifvlan_linkmib *mib;
799 1.98 knakahar struct psref psref;
800 1.20 enami int error = 0;
801 1.98 knakahar int bound;
802 1.98 knakahar
803 1.98 knakahar bound = curlwp_bind();
804 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
805 1.98 knakahar if (mib == NULL) {
806 1.98 knakahar curlwp_bindx(bound);
807 1.98 knakahar return EBUSY;
808 1.98 knakahar }
809 1.17 thorpej
810 1.17 thorpej if ((ifp->if_flags & IFF_PROMISC) != 0) {
811 1.17 thorpej if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
812 1.109 ozaki error = vlan_safe_ifpromisc(mib->ifvm_p, 1);
813 1.17 thorpej if (error == 0)
814 1.17 thorpej ifv->ifv_flags |= IFVF_PROMISC;
815 1.17 thorpej }
816 1.17 thorpej } else {
817 1.17 thorpej if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
818 1.109 ozaki error = vlan_safe_ifpromisc(mib->ifvm_p, 0);
819 1.17 thorpej if (error == 0)
820 1.17 thorpej ifv->ifv_flags &= ~IFVF_PROMISC;
821 1.17 thorpej }
822 1.17 thorpej }
823 1.98 knakahar vlan_putref_linkmib(mib, &psref);
824 1.98 knakahar curlwp_bindx(bound);
825 1.17 thorpej
826 1.124 maxv return error;
827 1.17 thorpej }
828 1.17 thorpej
829 1.17 thorpej static int
830 1.53 christos vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
831 1.1 thorpej {
832 1.124 maxv struct lwp *l = curlwp;
833 1.11 thorpej struct ifvlan *ifv = ifp->if_softc;
834 1.11 thorpej struct ifaddr *ifa = (struct ifaddr *) data;
835 1.11 thorpej struct ifreq *ifr = (struct ifreq *) data;
836 1.1 thorpej struct ifnet *pr;
837 1.60 bouyer struct ifcapreq *ifcr;
838 1.1 thorpej struct vlanreq vlr;
839 1.98 knakahar struct ifvlan_linkmib *mib;
840 1.98 knakahar struct psref psref;
841 1.98 knakahar int error = 0;
842 1.98 knakahar int bound;
843 1.1 thorpej
844 1.1 thorpej switch (cmd) {
845 1.1 thorpej case SIOCSIFMTU:
846 1.98 knakahar bound = curlwp_bind();
847 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
848 1.98 knakahar if (mib == NULL) {
849 1.98 knakahar curlwp_bindx(bound);
850 1.98 knakahar error = EBUSY;
851 1.98 knakahar break;
852 1.98 knakahar }
853 1.98 knakahar
854 1.98 knakahar if (mib->ifvm_p == NULL) {
855 1.98 knakahar vlan_putref_linkmib(mib, &psref);
856 1.98 knakahar curlwp_bindx(bound);
857 1.56 dyoung error = EINVAL;
858 1.98 knakahar } else if (
859 1.98 knakahar ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) ||
860 1.98 knakahar ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) {
861 1.98 knakahar vlan_putref_linkmib(mib, &psref);
862 1.98 knakahar curlwp_bindx(bound);
863 1.1 thorpej error = EINVAL;
864 1.98 knakahar } else {
865 1.98 knakahar vlan_putref_linkmib(mib, &psref);
866 1.98 knakahar curlwp_bindx(bound);
867 1.98 knakahar
868 1.98 knakahar error = ifioctl_common(ifp, cmd, data);
869 1.98 knakahar if (error == ENETRESET)
870 1.124 maxv error = 0;
871 1.98 knakahar }
872 1.98 knakahar
873 1.1 thorpej break;
874 1.1 thorpej
875 1.1 thorpej case SIOCSETVLAN:
876 1.51 elad if ((error = kauth_authorize_network(l->l_cred,
877 1.51 elad KAUTH_NETWORK_INTERFACE,
878 1.51 elad KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
879 1.51 elad NULL)) != 0)
880 1.1 thorpej break;
881 1.1 thorpej if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
882 1.1 thorpej break;
883 1.98 knakahar
884 1.1 thorpej if (vlr.vlr_parent[0] == '\0') {
885 1.98 knakahar bound = curlwp_bind();
886 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
887 1.98 knakahar if (mib == NULL) {
888 1.98 knakahar curlwp_bindx(bound);
889 1.98 knakahar error = EBUSY;
890 1.98 knakahar break;
891 1.98 knakahar }
892 1.98 knakahar
893 1.98 knakahar if (mib->ifvm_p != NULL &&
894 1.73 ozaki (ifp->if_flags & IFF_PROMISC) != 0)
895 1.109 ozaki error = vlan_safe_ifpromisc(mib->ifvm_p, 0);
896 1.98 knakahar
897 1.98 knakahar vlan_putref_linkmib(mib, &psref);
898 1.98 knakahar curlwp_bindx(bound);
899 1.98 knakahar
900 1.1 thorpej vlan_unconfig(ifp);
901 1.1 thorpej break;
902 1.1 thorpej }
903 1.1 thorpej if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
904 1.1 thorpej error = EINVAL; /* check for valid tag */
905 1.1 thorpej break;
906 1.1 thorpej }
907 1.110 msaitoh if ((pr = ifunit(vlr.vlr_parent)) == NULL) {
908 1.1 thorpej error = ENOENT;
909 1.1 thorpej break;
910 1.1 thorpej }
911 1.146 msaitoh
912 1.98 knakahar error = vlan_config(ifv, pr, vlr.vlr_tag);
913 1.140 msaitoh if (error != 0)
914 1.1 thorpej break;
915 1.17 thorpej
916 1.17 thorpej /* Update promiscuous mode, if necessary. */
917 1.17 thorpej vlan_set_promisc(ifp);
918 1.116 ozaki
919 1.116 ozaki ifp->if_flags |= IFF_RUNNING;
920 1.1 thorpej break;
921 1.1 thorpej
922 1.1 thorpej case SIOCGETVLAN:
923 1.1 thorpej memset(&vlr, 0, sizeof(vlr));
924 1.98 knakahar bound = curlwp_bind();
925 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
926 1.98 knakahar if (mib == NULL) {
927 1.98 knakahar curlwp_bindx(bound);
928 1.98 knakahar error = EBUSY;
929 1.98 knakahar break;
930 1.98 knakahar }
931 1.98 knakahar if (mib->ifvm_p != NULL) {
932 1.1 thorpej snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
933 1.98 knakahar mib->ifvm_p->if_xname);
934 1.98 knakahar vlr.vlr_tag = mib->ifvm_tag;
935 1.1 thorpej }
936 1.98 knakahar vlan_putref_linkmib(mib, &psref);
937 1.98 knakahar curlwp_bindx(bound);
938 1.1 thorpej error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
939 1.1 thorpej break;
940 1.1 thorpej
941 1.1 thorpej case SIOCSIFFLAGS:
942 1.61 dyoung if ((error = ifioctl_common(ifp, cmd, data)) != 0)
943 1.61 dyoung break;
944 1.1 thorpej /*
945 1.17 thorpej * For promiscuous mode, we enable promiscuous mode on
946 1.17 thorpej * the parent if we need promiscuous on the VLAN interface.
947 1.1 thorpej */
948 1.98 knakahar bound = curlwp_bind();
949 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
950 1.98 knakahar if (mib == NULL) {
951 1.98 knakahar curlwp_bindx(bound);
952 1.98 knakahar error = EBUSY;
953 1.98 knakahar break;
954 1.98 knakahar }
955 1.98 knakahar
956 1.98 knakahar if (mib->ifvm_p != NULL)
957 1.17 thorpej error = vlan_set_promisc(ifp);
958 1.98 knakahar vlan_putref_linkmib(mib, &psref);
959 1.98 knakahar curlwp_bindx(bound);
960 1.1 thorpej break;
961 1.1 thorpej
962 1.1 thorpej case SIOCADDMULTI:
963 1.98 knakahar mutex_enter(&ifv->ifv_lock);
964 1.98 knakahar mib = ifv->ifv_mib;
965 1.98 knakahar if (mib == NULL) {
966 1.98 knakahar error = EBUSY;
967 1.98 knakahar mutex_exit(&ifv->ifv_lock);
968 1.98 knakahar break;
969 1.98 knakahar }
970 1.98 knakahar
971 1.98 knakahar error = (mib->ifvm_p != NULL) ?
972 1.98 knakahar (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
973 1.98 knakahar mib = NULL;
974 1.98 knakahar mutex_exit(&ifv->ifv_lock);
975 1.6 enami break;
976 1.6 enami
977 1.1 thorpej case SIOCDELMULTI:
978 1.98 knakahar mutex_enter(&ifv->ifv_lock);
979 1.98 knakahar mib = ifv->ifv_mib;
980 1.98 knakahar if (mib == NULL) {
981 1.98 knakahar error = EBUSY;
982 1.98 knakahar mutex_exit(&ifv->ifv_lock);
983 1.98 knakahar break;
984 1.98 knakahar }
985 1.98 knakahar error = (mib->ifvm_p != NULL) ?
986 1.98 knakahar (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
987 1.98 knakahar mib = NULL;
988 1.98 knakahar mutex_exit(&ifv->ifv_lock);
989 1.1 thorpej break;
990 1.1 thorpej
991 1.60 bouyer case SIOCSIFCAP:
992 1.60 bouyer ifcr = data;
993 1.60 bouyer /* make sure caps are enabled on parent */
994 1.98 knakahar bound = curlwp_bind();
995 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
996 1.98 knakahar if (mib == NULL) {
997 1.98 knakahar curlwp_bindx(bound);
998 1.98 knakahar error = EBUSY;
999 1.98 knakahar break;
1000 1.98 knakahar }
1001 1.98 knakahar
1002 1.98 knakahar if (mib->ifvm_p == NULL) {
1003 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1004 1.98 knakahar curlwp_bindx(bound);
1005 1.92 joerg error = EINVAL;
1006 1.92 joerg break;
1007 1.92 joerg }
1008 1.98 knakahar if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) !=
1009 1.60 bouyer ifcr->ifcr_capenable) {
1010 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1011 1.98 knakahar curlwp_bindx(bound);
1012 1.60 bouyer error = EINVAL;
1013 1.60 bouyer break;
1014 1.60 bouyer }
1015 1.98 knakahar
1016 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1017 1.98 knakahar curlwp_bindx(bound);
1018 1.98 knakahar
1019 1.60 bouyer if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1020 1.60 bouyer error = 0;
1021 1.60 bouyer break;
1022 1.68 dyoung case SIOCINITIFADDR:
1023 1.98 knakahar bound = curlwp_bind();
1024 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
1025 1.98 knakahar if (mib == NULL) {
1026 1.98 knakahar curlwp_bindx(bound);
1027 1.98 knakahar error = EBUSY;
1028 1.98 knakahar break;
1029 1.98 knakahar }
1030 1.98 knakahar
1031 1.98 knakahar if (mib->ifvm_p == NULL) {
1032 1.68 dyoung error = EINVAL;
1033 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1034 1.98 knakahar curlwp_bindx(bound);
1035 1.68 dyoung break;
1036 1.68 dyoung }
1037 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1038 1.98 knakahar curlwp_bindx(bound);
1039 1.68 dyoung
1040 1.68 dyoung ifp->if_flags |= IFF_UP;
1041 1.68 dyoung #ifdef INET
1042 1.68 dyoung if (ifa->ifa_addr->sa_family == AF_INET)
1043 1.68 dyoung arp_ifinit(ifp, ifa);
1044 1.68 dyoung #endif
1045 1.68 dyoung break;
1046 1.68 dyoung
1047 1.1 thorpej default:
1048 1.61 dyoung error = ether_ioctl(ifp, cmd, data);
1049 1.1 thorpej }
1050 1.11 thorpej
1051 1.124 maxv return error;
1052 1.1 thorpej }
1053 1.1 thorpej
1054 1.1 thorpej static int
1055 1.10 thorpej vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
1056 1.1 thorpej {
1057 1.55 dyoung const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
1058 1.1 thorpej struct vlan_mc_entry *mc;
1059 1.57 matt uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
1060 1.98 knakahar struct ifvlan_linkmib *mib;
1061 1.1 thorpej int error;
1062 1.1 thorpej
1063 1.98 knakahar KASSERT(mutex_owned(&ifv->ifv_lock));
1064 1.98 knakahar
1065 1.55 dyoung if (sa->sa_len > sizeof(struct sockaddr_storage))
1066 1.124 maxv return EINVAL;
1067 1.5 enami
1068 1.55 dyoung error = ether_addmulti(sa, &ifv->ifv_ec);
1069 1.5 enami if (error != ENETRESET)
1070 1.124 maxv return error;
1071 1.1 thorpej
1072 1.5 enami /*
1073 1.124 maxv * This is a new multicast address. We have to tell parent
1074 1.5 enami * about it. Also, remember this multicast address so that
1075 1.124 maxv * we can delete it on unconfigure.
1076 1.5 enami */
1077 1.62 cegger mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
1078 1.5 enami if (mc == NULL) {
1079 1.5 enami error = ENOMEM;
1080 1.5 enami goto alloc_failed;
1081 1.1 thorpej }
1082 1.1 thorpej
1083 1.5 enami /*
1084 1.124 maxv * Since ether_addmulti() returned ENETRESET, the following two
1085 1.123 maxv * statements shouldn't fail. Here ifv_ec is implicitly protected
1086 1.123 maxv * by the ifv_lock lock.
1087 1.5 enami */
1088 1.123 maxv error = ether_multiaddr(sa, addrlo, addrhi);
1089 1.123 maxv KASSERT(error == 0);
1090 1.128 yamaguch
1091 1.128 yamaguch ETHER_LOCK(&ifv->ifv_ec);
1092 1.127 yamaguch mc->mc_enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec);
1093 1.128 yamaguch ETHER_UNLOCK(&ifv->ifv_ec);
1094 1.128 yamaguch
1095 1.123 maxv KASSERT(mc->mc_enm != NULL);
1096 1.123 maxv
1097 1.55 dyoung memcpy(&mc->mc_addr, sa, sa->sa_len);
1098 1.5 enami LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
1099 1.5 enami
1100 1.98 knakahar mib = ifv->ifv_mib;
1101 1.109 ozaki
1102 1.109 ozaki KERNEL_LOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p);
1103 1.98 knakahar error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa);
1104 1.109 ozaki KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p);
1105 1.98 knakahar
1106 1.5 enami if (error != 0)
1107 1.5 enami goto ioctl_failed;
1108 1.124 maxv return error;
1109 1.5 enami
1110 1.124 maxv ioctl_failed:
1111 1.5 enami LIST_REMOVE(mc, mc_entries);
1112 1.62 cegger free(mc, M_DEVBUF);
1113 1.124 maxv
1114 1.124 maxv alloc_failed:
1115 1.55 dyoung (void)ether_delmulti(sa, &ifv->ifv_ec);
1116 1.124 maxv return error;
1117 1.5 enami }
1118 1.5 enami
1119 1.5 enami static int
1120 1.10 thorpej vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
1121 1.5 enami {
1122 1.55 dyoung const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
1123 1.5 enami struct ether_multi *enm;
1124 1.5 enami struct vlan_mc_entry *mc;
1125 1.98 knakahar struct ifvlan_linkmib *mib;
1126 1.57 matt uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
1127 1.5 enami int error;
1128 1.5 enami
1129 1.98 knakahar KASSERT(mutex_owned(&ifv->ifv_lock));
1130 1.98 knakahar
1131 1.5 enami /*
1132 1.5 enami * Find a key to lookup vlan_mc_entry. We have to do this
1133 1.124 maxv * before calling ether_delmulti for obvious reasons.
1134 1.5 enami */
1135 1.55 dyoung if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
1136 1.124 maxv return error;
1137 1.128 yamaguch
1138 1.128 yamaguch ETHER_LOCK(&ifv->ifv_ec);
1139 1.127 yamaguch enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec);
1140 1.128 yamaguch ETHER_UNLOCK(&ifv->ifv_ec);
1141 1.129 yamaguch if (enm == NULL)
1142 1.129 yamaguch return EINVAL;
1143 1.129 yamaguch
1144 1.129 yamaguch LIST_FOREACH(mc, &ifv->ifv_mc_listhead, mc_entries) {
1145 1.129 yamaguch if (mc->mc_enm == enm)
1146 1.129 yamaguch break;
1147 1.129 yamaguch }
1148 1.129 yamaguch
1149 1.129 yamaguch /* We woun't delete entries we didn't add */
1150 1.129 yamaguch if (mc == NULL)
1151 1.129 yamaguch return EINVAL;
1152 1.5 enami
1153 1.55 dyoung error = ether_delmulti(sa, &ifv->ifv_ec);
1154 1.5 enami if (error != ENETRESET)
1155 1.124 maxv return error;
1156 1.5 enami
1157 1.5 enami /* We no longer use this multicast address. Tell parent so. */
1158 1.98 knakahar mib = ifv->ifv_mib;
1159 1.98 knakahar error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa);
1160 1.98 knakahar
1161 1.5 enami if (error == 0) {
1162 1.5 enami /* And forget about this address. */
1163 1.129 yamaguch LIST_REMOVE(mc, mc_entries);
1164 1.129 yamaguch free(mc, M_DEVBUF);
1165 1.129 yamaguch } else {
1166 1.55 dyoung (void)ether_addmulti(sa, &ifv->ifv_ec);
1167 1.129 yamaguch }
1168 1.124 maxv
1169 1.124 maxv return error;
1170 1.5 enami }
1171 1.5 enami
1172 1.5 enami /*
1173 1.34 pooka * Delete any multicast address we have asked to add from parent
1174 1.5 enami * interface. Called when the vlan is being unconfigured.
1175 1.5 enami */
1176 1.5 enami static void
1177 1.10 thorpej vlan_ether_purgemulti(struct ifvlan *ifv)
1178 1.5 enami {
1179 1.5 enami struct vlan_mc_entry *mc;
1180 1.98 knakahar struct ifvlan_linkmib *mib;
1181 1.98 knakahar
1182 1.98 knakahar KASSERT(mutex_owned(&ifv->ifv_lock));
1183 1.98 knakahar mib = ifv->ifv_mib;
1184 1.98 knakahar if (mib == NULL) {
1185 1.98 knakahar return;
1186 1.98 knakahar }
1187 1.5 enami
1188 1.5 enami while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
1189 1.98 knakahar (void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI,
1190 1.129 yamaguch sstocsa(&mc->mc_addr));
1191 1.5 enami LIST_REMOVE(mc, mc_entries);
1192 1.62 cegger free(mc, M_DEVBUF);
1193 1.1 thorpej }
1194 1.1 thorpej }
1195 1.1 thorpej
1196 1.1 thorpej static void
1197 1.1 thorpej vlan_start(struct ifnet *ifp)
1198 1.1 thorpej {
1199 1.14 enami struct ifvlan *ifv = ifp->if_softc;
1200 1.98 knakahar struct ifnet *p;
1201 1.98 knakahar struct ethercom *ec;
1202 1.1 thorpej struct mbuf *m;
1203 1.98 knakahar struct ifvlan_linkmib *mib;
1204 1.98 knakahar struct psref psref;
1205 1.160 yamaguch struct ether_header *eh;
1206 1.167 yamaguch int error, bound;
1207 1.1 thorpej
1208 1.167 yamaguch bound = curlwp_bind();
1209 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
1210 1.167 yamaguch if (mib == NULL) {
1211 1.167 yamaguch curlwp_bindx(bound);
1212 1.98 knakahar return;
1213 1.167 yamaguch }
1214 1.156 yamaguch
1215 1.156 yamaguch if (__predict_false(mib->ifvm_p == NULL)) {
1216 1.156 yamaguch vlan_putref_linkmib(mib, &psref);
1217 1.167 yamaguch curlwp_bindx(bound);
1218 1.156 yamaguch return;
1219 1.156 yamaguch }
1220 1.156 yamaguch
1221 1.98 knakahar p = mib->ifvm_p;
1222 1.98 knakahar ec = (void *)mib->ifvm_p;
1223 1.98 knakahar
1224 1.1 thorpej ifp->if_flags |= IFF_OACTIVE;
1225 1.1 thorpej
1226 1.1 thorpej for (;;) {
1227 1.31 thorpej IFQ_DEQUEUE(&ifp->if_snd, m);
1228 1.1 thorpej if (m == NULL)
1229 1.1 thorpej break;
1230 1.1 thorpej
1231 1.160 yamaguch if (m->m_len < sizeof(*eh)) {
1232 1.160 yamaguch m = m_pullup(m, sizeof(*eh));
1233 1.160 yamaguch if (m == NULL) {
1234 1.160 yamaguch if_statinc(ifp, if_oerrors);
1235 1.160 yamaguch continue;
1236 1.160 yamaguch }
1237 1.160 yamaguch }
1238 1.160 yamaguch
1239 1.160 yamaguch eh = mtod(m, struct ether_header *);
1240 1.160 yamaguch if (ntohs(eh->ether_type) == ETHERTYPE_VLAN) {
1241 1.160 yamaguch m_freem(m);
1242 1.160 yamaguch if_statinc(ifp, if_noproto);
1243 1.160 yamaguch continue;
1244 1.160 yamaguch }
1245 1.160 yamaguch
1246 1.31 thorpej #ifdef ALTQ
1247 1.31 thorpej /*
1248 1.124 maxv * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is
1249 1.124 maxv * defined.
1250 1.90 knakahar */
1251 1.90 knakahar KERNEL_LOCK(1, NULL);
1252 1.90 knakahar /*
1253 1.31 thorpej * If ALTQ is enabled on the parent interface, do
1254 1.31 thorpej * classification; the queueing discipline might
1255 1.31 thorpej * not require classification, but might require
1256 1.31 thorpej * the address family/header pointer in the pktattr.
1257 1.31 thorpej */
1258 1.31 thorpej if (ALTQ_IS_ENABLED(&p->if_snd)) {
1259 1.31 thorpej switch (p->if_type) {
1260 1.31 thorpej case IFT_ETHER:
1261 1.85 knakahar altq_etherclassify(&p->if_snd, m);
1262 1.31 thorpej break;
1263 1.31 thorpej default:
1264 1.124 maxv panic("%s: impossible (altq)", __func__);
1265 1.31 thorpej }
1266 1.31 thorpej }
1267 1.90 knakahar KERNEL_UNLOCK_ONE(NULL);
1268 1.31 thorpej #endif /* ALTQ */
1269 1.31 thorpej
1270 1.130 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
1271 1.1 thorpej /*
1272 1.24 bouyer * If the parent can insert the tag itself, just mark
1273 1.24 bouyer * the tag in the mbuf header.
1274 1.1 thorpej */
1275 1.142 msaitoh if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
1276 1.100 knakahar vlan_set_tag(m, mib->ifvm_tag);
1277 1.24 bouyer } else {
1278 1.24 bouyer /*
1279 1.34 pooka * insert the tag ourselves
1280 1.24 bouyer */
1281 1.10 thorpej
1282 1.24 bouyer switch (p->if_type) {
1283 1.24 bouyer case IFT_ETHER:
1284 1.24 bouyer {
1285 1.24 bouyer struct ether_vlan_header *evl;
1286 1.24 bouyer
1287 1.169 yamaguch M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1288 1.169 yamaguch if (m == NULL) {
1289 1.169 yamaguch printf("%s: unable to prepend encap header",
1290 1.169 yamaguch p->if_xname);
1291 1.169 yamaguch if_statinc(ifp, if_oerrors);
1292 1.169 yamaguch continue;
1293 1.169 yamaguch }
1294 1.24 bouyer if (m->m_len < sizeof(struct ether_vlan_header))
1295 1.24 bouyer m = m_pullup(m,
1296 1.24 bouyer sizeof(struct ether_vlan_header));
1297 1.24 bouyer if (m == NULL) {
1298 1.24 bouyer printf("%s: unable to pullup encap "
1299 1.98 knakahar "header", p->if_xname);
1300 1.150 thorpej if_statinc(ifp, if_oerrors);
1301 1.24 bouyer continue;
1302 1.24 bouyer }
1303 1.24 bouyer
1304 1.24 bouyer /*
1305 1.24 bouyer * Transform the Ethernet header into an
1306 1.24 bouyer * Ethernet header with 802.1Q encapsulation.
1307 1.24 bouyer */
1308 1.53 christos memmove(mtod(m, void *),
1309 1.169 yamaguch mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
1310 1.24 bouyer sizeof(struct ether_header));
1311 1.24 bouyer evl = mtod(m, struct ether_vlan_header *);
1312 1.24 bouyer evl->evl_proto = evl->evl_encap_proto;
1313 1.24 bouyer evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1314 1.98 knakahar evl->evl_tag = htons(mib->ifvm_tag);
1315 1.38 scw
1316 1.38 scw /*
1317 1.38 scw * To cater for VLAN-aware layer 2 ethernet
1318 1.38 scw * switches which may need to strip the tag
1319 1.38 scw * before forwarding the packet, make sure
1320 1.38 scw * the packet+tag is at least 68 bytes long.
1321 1.38 scw * This is necessary because our parent will
1322 1.38 scw * only pad to 64 bytes (ETHER_MIN_LEN) and
1323 1.38 scw * some switches will not pad by themselves
1324 1.38 scw * after deleting a tag.
1325 1.38 scw */
1326 1.124 maxv const size_t min_data_len = ETHER_MIN_LEN -
1327 1.124 maxv ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1328 1.124 maxv if (m->m_pkthdr.len < min_data_len) {
1329 1.38 scw m_copyback(m, m->m_pkthdr.len,
1330 1.124 maxv min_data_len - m->m_pkthdr.len,
1331 1.38 scw vlan_zero_pad_buff);
1332 1.38 scw }
1333 1.24 bouyer break;
1334 1.24 bouyer }
1335 1.10 thorpej
1336 1.24 bouyer default:
1337 1.124 maxv panic("%s: impossible", __func__);
1338 1.24 bouyer }
1339 1.1 thorpej }
1340 1.1 thorpej
1341 1.96 ozaki if ((p->if_flags & IFF_RUNNING) == 0) {
1342 1.96 ozaki m_freem(m);
1343 1.96 ozaki continue;
1344 1.1 thorpej }
1345 1.31 thorpej
1346 1.96 ozaki error = if_transmit_lock(p, m);
1347 1.96 ozaki if (error) {
1348 1.96 ozaki /* mbuf is already freed */
1349 1.150 thorpej if_statinc(ifp, if_oerrors);
1350 1.96 ozaki continue;
1351 1.96 ozaki }
1352 1.150 thorpej if_statinc(ifp, if_opackets);
1353 1.1 thorpej }
1354 1.1 thorpej
1355 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
1356 1.98 knakahar
1357 1.98 knakahar /* Remove reference to mib before release */
1358 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1359 1.167 yamaguch curlwp_bindx(bound);
1360 1.98 knakahar }
1361 1.98 knakahar
1362 1.98 knakahar static int
1363 1.98 knakahar vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1364 1.98 knakahar {
1365 1.98 knakahar struct ifvlan *ifv = ifp->if_softc;
1366 1.98 knakahar struct ifnet *p;
1367 1.98 knakahar struct ethercom *ec;
1368 1.98 knakahar struct ifvlan_linkmib *mib;
1369 1.98 knakahar struct psref psref;
1370 1.160 yamaguch struct ether_header *eh;
1371 1.167 yamaguch int error, bound;
1372 1.99 knakahar size_t pktlen = m->m_pkthdr.len;
1373 1.99 knakahar bool mcast = (m->m_flags & M_MCAST) != 0;
1374 1.98 knakahar
1375 1.160 yamaguch if (m->m_len < sizeof(*eh)) {
1376 1.160 yamaguch m = m_pullup(m, sizeof(*eh));
1377 1.160 yamaguch if (m == NULL) {
1378 1.160 yamaguch if_statinc(ifp, if_oerrors);
1379 1.160 yamaguch return ENOBUFS;
1380 1.160 yamaguch }
1381 1.160 yamaguch }
1382 1.160 yamaguch
1383 1.160 yamaguch eh = mtod(m, struct ether_header *);
1384 1.160 yamaguch if (ntohs(eh->ether_type) == ETHERTYPE_VLAN) {
1385 1.160 yamaguch m_freem(m);
1386 1.160 yamaguch if_statinc(ifp, if_noproto);
1387 1.160 yamaguch return EPROTONOSUPPORT;
1388 1.160 yamaguch }
1389 1.160 yamaguch
1390 1.167 yamaguch bound = curlwp_bind();
1391 1.98 knakahar mib = vlan_getref_linkmib(ifv, &psref);
1392 1.98 knakahar if (mib == NULL) {
1393 1.167 yamaguch curlwp_bindx(bound);
1394 1.98 knakahar m_freem(m);
1395 1.98 knakahar return ENETDOWN;
1396 1.98 knakahar }
1397 1.98 knakahar
1398 1.156 yamaguch if (__predict_false(mib->ifvm_p == NULL)) {
1399 1.156 yamaguch vlan_putref_linkmib(mib, &psref);
1400 1.167 yamaguch curlwp_bindx(bound);
1401 1.156 yamaguch m_freem(m);
1402 1.156 yamaguch return ENETDOWN;
1403 1.156 yamaguch }
1404 1.156 yamaguch
1405 1.98 knakahar p = mib->ifvm_p;
1406 1.98 knakahar ec = (void *)mib->ifvm_p;
1407 1.98 knakahar
1408 1.130 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
1409 1.113 jmcneill
1410 1.125 tih if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
1411 1.125 tih goto out;
1412 1.125 tih if (m == NULL)
1413 1.113 jmcneill goto out;
1414 1.113 jmcneill
1415 1.98 knakahar /*
1416 1.98 knakahar * If the parent can insert the tag itself, just mark
1417 1.98 knakahar * the tag in the mbuf header.
1418 1.98 knakahar */
1419 1.142 msaitoh if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
1420 1.100 knakahar vlan_set_tag(m, mib->ifvm_tag);
1421 1.98 knakahar } else {
1422 1.98 knakahar /*
1423 1.98 knakahar * insert the tag ourselves
1424 1.98 knakahar */
1425 1.98 knakahar switch (p->if_type) {
1426 1.98 knakahar case IFT_ETHER:
1427 1.98 knakahar {
1428 1.98 knakahar struct ether_vlan_header *evl;
1429 1.98 knakahar
1430 1.169 yamaguch M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1431 1.169 yamaguch if (m == NULL) {
1432 1.169 yamaguch printf("%s: unable to prepend encap header",
1433 1.169 yamaguch p->if_xname);
1434 1.169 yamaguch if_statinc(ifp, if_oerrors);
1435 1.169 yamaguch error = ENOBUFS;
1436 1.169 yamaguch goto out;
1437 1.169 yamaguch }
1438 1.98 knakahar if (m->m_len < sizeof(struct ether_vlan_header))
1439 1.98 knakahar m = m_pullup(m,
1440 1.98 knakahar sizeof(struct ether_vlan_header));
1441 1.98 knakahar if (m == NULL) {
1442 1.98 knakahar printf("%s: unable to pullup encap "
1443 1.98 knakahar "header", p->if_xname);
1444 1.150 thorpej if_statinc(ifp, if_oerrors);
1445 1.98 knakahar error = ENOBUFS;
1446 1.98 knakahar goto out;
1447 1.98 knakahar }
1448 1.98 knakahar
1449 1.98 knakahar /*
1450 1.98 knakahar * Transform the Ethernet header into an
1451 1.98 knakahar * Ethernet header with 802.1Q encapsulation.
1452 1.98 knakahar */
1453 1.98 knakahar memmove(mtod(m, void *),
1454 1.169 yamaguch mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
1455 1.98 knakahar sizeof(struct ether_header));
1456 1.98 knakahar evl = mtod(m, struct ether_vlan_header *);
1457 1.98 knakahar evl->evl_proto = evl->evl_encap_proto;
1458 1.98 knakahar evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1459 1.98 knakahar evl->evl_tag = htons(mib->ifvm_tag);
1460 1.98 knakahar
1461 1.98 knakahar /*
1462 1.98 knakahar * To cater for VLAN-aware layer 2 ethernet
1463 1.98 knakahar * switches which may need to strip the tag
1464 1.98 knakahar * before forwarding the packet, make sure
1465 1.98 knakahar * the packet+tag is at least 68 bytes long.
1466 1.98 knakahar * This is necessary because our parent will
1467 1.98 knakahar * only pad to 64 bytes (ETHER_MIN_LEN) and
1468 1.98 knakahar * some switches will not pad by themselves
1469 1.98 knakahar * after deleting a tag.
1470 1.98 knakahar */
1471 1.124 maxv const size_t min_data_len = ETHER_MIN_LEN -
1472 1.124 maxv ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1473 1.124 maxv if (m->m_pkthdr.len < min_data_len) {
1474 1.98 knakahar m_copyback(m, m->m_pkthdr.len,
1475 1.124 maxv min_data_len - m->m_pkthdr.len,
1476 1.98 knakahar vlan_zero_pad_buff);
1477 1.98 knakahar }
1478 1.98 knakahar break;
1479 1.98 knakahar }
1480 1.98 knakahar
1481 1.98 knakahar default:
1482 1.124 maxv panic("%s: impossible", __func__);
1483 1.98 knakahar }
1484 1.98 knakahar }
1485 1.98 knakahar
1486 1.98 knakahar if ((p->if_flags & IFF_RUNNING) == 0) {
1487 1.98 knakahar m_freem(m);
1488 1.98 knakahar error = ENETDOWN;
1489 1.98 knakahar goto out;
1490 1.98 knakahar }
1491 1.98 knakahar
1492 1.98 knakahar error = if_transmit_lock(p, m);
1493 1.150 thorpej net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1494 1.98 knakahar if (error) {
1495 1.98 knakahar /* mbuf is already freed */
1496 1.150 thorpej if_statinc_ref(nsr, if_oerrors);
1497 1.98 knakahar } else {
1498 1.150 thorpej if_statinc_ref(nsr, if_opackets);
1499 1.150 thorpej if_statadd_ref(nsr, if_obytes, pktlen);
1500 1.99 knakahar if (mcast)
1501 1.150 thorpej if_statinc_ref(nsr, if_omcasts);
1502 1.98 knakahar }
1503 1.150 thorpej IF_STAT_PUTREF(ifp);
1504 1.98 knakahar
1505 1.98 knakahar out:
1506 1.98 knakahar /* Remove reference to mib before release */
1507 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1508 1.167 yamaguch curlwp_bindx(bound);
1509 1.167 yamaguch
1510 1.98 knakahar return error;
1511 1.1 thorpej }
1512 1.1 thorpej
1513 1.1 thorpej /*
1514 1.1 thorpej * Given an Ethernet frame, find a valid vlan interface corresponding to the
1515 1.44 perry * given source interface and tag, then run the real packet through the
1516 1.40 simonb * parent's input routine.
1517 1.1 thorpej */
1518 1.168 yamaguch struct mbuf *
1519 1.1 thorpej vlan_input(struct ifnet *ifp, struct mbuf *m)
1520 1.1 thorpej {
1521 1.1 thorpej struct ifvlan *ifv;
1522 1.108 msaitoh uint16_t vid;
1523 1.98 knakahar struct ifvlan_linkmib *mib;
1524 1.98 knakahar struct psref psref;
1525 1.24 bouyer
1526 1.169 yamaguch if (vlan_has_tag(m)) {
1527 1.108 msaitoh vid = EVL_VLANOFTAG(vlan_get_tag(m));
1528 1.24 bouyer } else {
1529 1.124 maxv struct ether_vlan_header *evl;
1530 1.24 bouyer
1531 1.124 maxv if (ifp->if_type != IFT_ETHER) {
1532 1.124 maxv panic("%s: impossible", __func__);
1533 1.124 maxv }
1534 1.24 bouyer
1535 1.124 maxv if (m->m_len < sizeof(struct ether_vlan_header) &&
1536 1.124 maxv (m = m_pullup(m,
1537 1.124 maxv sizeof(struct ether_vlan_header))) == NULL) {
1538 1.124 maxv printf("%s: no memory for VLAN header, "
1539 1.124 maxv "dropping packet.\n", ifp->if_xname);
1540 1.168 yamaguch return NULL;
1541 1.124 maxv }
1542 1.158 yamaguch
1543 1.158 yamaguch if (m_makewritable(&m, 0,
1544 1.158 yamaguch sizeof(struct ether_vlan_header), M_DONTWAIT)) {
1545 1.158 yamaguch m_freem(m);
1546 1.158 yamaguch if_statinc(ifp, if_ierrors);
1547 1.168 yamaguch return NULL;
1548 1.158 yamaguch }
1549 1.158 yamaguch
1550 1.124 maxv evl = mtod(m, struct ether_vlan_header *);
1551 1.124 maxv KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
1552 1.1 thorpej
1553 1.124 maxv vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1554 1.169 yamaguch vlan_set_tag(m, ntohs(evl->evl_tag));
1555 1.10 thorpej
1556 1.124 maxv /*
1557 1.124 maxv * Restore the original ethertype. We'll remove
1558 1.124 maxv * the encapsulation after we've found the vlan
1559 1.124 maxv * interface corresponding to the tag.
1560 1.124 maxv */
1561 1.124 maxv evl->evl_encap_proto = evl->evl_proto;
1562 1.169 yamaguch
1563 1.169 yamaguch /*
1564 1.169 yamaguch * Remove the encapsulation header and append tag.
1565 1.169 yamaguch * The original header has already been fixed up above.
1566 1.169 yamaguch */
1567 1.169 yamaguch memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
1568 1.169 yamaguch offsetof(struct ether_vlan_header, evl_encap_proto));
1569 1.169 yamaguch m_adj(m, ETHER_VLAN_ENCAP_LEN);
1570 1.43 christos }
1571 1.10 thorpej
1572 1.168 yamaguch KASSERT(vid != 0);
1573 1.108 msaitoh mib = vlan_lookup_tag_psref(ifp, vid, &psref);
1574 1.98 knakahar if (mib == NULL) {
1575 1.168 yamaguch return m;
1576 1.98 knakahar }
1577 1.1 thorpej
1578 1.98 knakahar ifv = mib->ifvm_ifvlan;
1579 1.137 msaitoh if ((ifv->ifv_if.if_flags & (IFF_UP | IFF_RUNNING)) !=
1580 1.137 msaitoh (IFF_UP | IFF_RUNNING)) {
1581 1.37 itojun m_freem(m);
1582 1.150 thorpej if_statinc(ifp, if_noproto);
1583 1.98 knakahar goto out;
1584 1.1 thorpej }
1585 1.43 christos
1586 1.43 christos /*
1587 1.169 yamaguch * Having found a valid vlan interface corresponding to
1588 1.169 yamaguch * the given source interface and vlan tag.
1589 1.169 yamaguch * remove the vlan tag.
1590 1.43 christos */
1591 1.169 yamaguch m->m_flags &= ~M_VLANTAG;
1592 1.43 christos
1593 1.157 yamaguch /*
1594 1.157 yamaguch * Drop promiscuously received packets if we are not in
1595 1.157 yamaguch * promiscuous mode
1596 1.157 yamaguch */
1597 1.157 yamaguch if ((m->m_flags & (M_BCAST | M_MCAST)) == 0 &&
1598 1.157 yamaguch (ifp->if_flags & IFF_PROMISC) &&
1599 1.157 yamaguch (ifv->ifv_if.if_flags & IFF_PROMISC) == 0) {
1600 1.157 yamaguch struct ether_header *eh;
1601 1.157 yamaguch
1602 1.157 yamaguch eh = mtod(m, struct ether_header *);
1603 1.157 yamaguch if (memcmp(CLLADDR(ifv->ifv_if.if_sadl),
1604 1.157 yamaguch eh->ether_dhost, ETHER_ADDR_LEN) != 0) {
1605 1.157 yamaguch m_freem(m);
1606 1.157 yamaguch if_statinc(&ifv->ifv_if, if_ierrors);
1607 1.157 yamaguch goto out;
1608 1.157 yamaguch }
1609 1.157 yamaguch }
1610 1.157 yamaguch
1611 1.89 ozaki m_set_rcvif(m, &ifv->ifv_if);
1612 1.1 thorpej
1613 1.125 tih if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
1614 1.125 tih goto out;
1615 1.125 tih if (m == NULL)
1616 1.113 jmcneill goto out;
1617 1.113 jmcneill
1618 1.72 ozaki m->m_flags &= ~M_PROMISC;
1619 1.84 ozaki if_input(&ifv->ifv_if, m);
1620 1.98 knakahar out:
1621 1.98 knakahar vlan_putref_linkmib(mib, &psref);
1622 1.168 yamaguch return NULL;
1623 1.1 thorpej }
1624 1.91 christos
1625 1.91 christos /*
1626 1.153 roy * If the parent link state changed, the vlan link state should change also.
1627 1.153 roy */
1628 1.162 yamaguch static void
1629 1.162 yamaguch vlan_link_state_changed(void *xifv)
1630 1.153 roy {
1631 1.162 yamaguch struct ifvlan *ifv = xifv;
1632 1.162 yamaguch struct ifnet *ifp, *p;
1633 1.153 roy struct ifvlan_linkmib *mib;
1634 1.153 roy struct psref psref;
1635 1.167 yamaguch int bound;
1636 1.153 roy
1637 1.167 yamaguch bound = curlwp_bind();
1638 1.162 yamaguch mib = vlan_getref_linkmib(ifv, &psref);
1639 1.167 yamaguch if (mib == NULL) {
1640 1.167 yamaguch curlwp_bindx(bound);
1641 1.162 yamaguch return;
1642 1.167 yamaguch }
1643 1.153 roy
1644 1.162 yamaguch if (mib->ifvm_p == NULL) {
1645 1.153 roy vlan_putref_linkmib(mib, &psref);
1646 1.167 yamaguch curlwp_bindx(bound);
1647 1.162 yamaguch return;
1648 1.153 roy }
1649 1.153 roy
1650 1.162 yamaguch ifp = &ifv->ifv_if;
1651 1.162 yamaguch p = mib->ifvm_p;
1652 1.162 yamaguch if_link_state_change(ifp, p->if_link_state);
1653 1.162 yamaguch
1654 1.162 yamaguch vlan_putref_linkmib(mib, &psref);
1655 1.167 yamaguch curlwp_bindx(bound);
1656 1.153 roy }
1657 1.153 roy
1658 1.153 roy /*
1659 1.91 christos * Module infrastructure
1660 1.91 christos */
1661 1.91 christos #include "if_module.h"
1662 1.91 christos
1663 1.135 pgoyette IF_MODULE(MODULE_CLASS_DRIVER, vlan, NULL)
1664