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