Home | History | Annotate | Line # | Download | only in net
      1 /*	$NetBSD: if_ethersubr.c,v 1.331 2025/09/21 15:11:52 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.331 2025/09/21 15:11:52 christos Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_inet.h"
     68 #include "opt_atalk.h"
     69 #include "opt_mbuftrace.h"
     70 #include "opt_mpls.h"
     71 #include "opt_gateway.h"
     72 #include "opt_pppoe.h"
     73 #include "opt_net_mpsafe.h"
     74 #endif
     75 
     76 #include "vlan.h"
     77 #include "pppoe.h"
     78 #include "bridge.h"
     79 #include "arp.h"
     80 #include "agr.h"
     81 
     82 #include <sys/sysctl.h>
     83 #include <sys/mbuf.h>
     84 #include <sys/mutex.h>
     85 #include <sys/ioctl.h>
     86 #include <sys/errno.h>
     87 #include <sys/device.h>
     88 #include <sys/entropy.h>
     89 #include <sys/rndsource.h>
     90 #include <sys/cpu.h>
     91 #include <sys/kmem.h>
     92 #include <sys/hook.h>
     93 
     94 #include <net/if.h>
     95 #include <net/route.h>
     96 #include <net/if_llc.h>
     97 #include <net/if_dl.h>
     98 #include <net/if_types.h>
     99 #include <net/pktqueue.h>
    100 
    101 #include <net/if_media.h>
    102 #include <dev/mii/mii.h>
    103 #include <dev/mii/miivar.h>
    104 
    105 #if NARP == 0
    106 /*
    107  * XXX there should really be a way to issue this warning from within config(8)
    108  */
    109 #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
    110 #endif
    111 
    112 #include <net/bpf.h>
    113 
    114 #include <net/if_ether.h>
    115 #include <net/if_vlanvar.h>
    116 
    117 #if NPPPOE > 0
    118 #include <net/if_pppoe.h>
    119 #endif
    120 
    121 #if NAGR > 0
    122 #include <net/ether_slowprotocols.h>
    123 #include <net/agr/ieee8023ad.h>
    124 #include <net/agr/if_agrvar.h>
    125 #endif
    126 
    127 #if NBRIDGE > 0
    128 #include <net/if_bridgevar.h>
    129 #endif
    130 
    131 #include <netinet/in.h>
    132 #ifdef INET
    133 #include <netinet/in_var.h>
    134 #endif
    135 #include <netinet/if_inarp.h>
    136 
    137 #ifdef INET6
    138 #ifndef INET
    139 #include <netinet/in.h>
    140 #endif
    141 #include <netinet6/in6_var.h>
    142 #include <netinet6/nd6.h>
    143 #endif
    144 
    145 #include "carp.h"
    146 #if NCARP > 0
    147 #include <netinet/ip_carp.h>
    148 #endif
    149 
    150 #ifdef NETATALK
    151 #include <netatalk/at.h>
    152 #include <netatalk/at_var.h>
    153 #include <netatalk/at_extern.h>
    154 
    155 #define llc_snap_org_code llc_un.type_snap.org_code
    156 #define llc_snap_ether_type llc_un.type_snap.ether_type
    157 
    158 extern u_char	at_org_code[3];
    159 extern u_char	aarp_org_code[3];
    160 #endif /* NETATALK */
    161 
    162 #ifdef MPLS
    163 #include <netmpls/mpls.h>
    164 #include <netmpls/mpls_var.h>
    165 #endif
    166 
    167 CTASSERT(sizeof(struct ether_addr) == 6);
    168 CTASSERT(sizeof(struct ether_header) == 14);
    169 
    170 #ifdef DIAGNOSTIC
    171 static struct timeval bigpktppslim_last;
    172 static int bigpktppslim = 2;	/* XXX */
    173 static int bigpktpps_count;
    174 static kmutex_t bigpktpps_lock __cacheline_aligned;
    175 #endif
    176 
    177 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
    178     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    179 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
    180     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
    181 #define senderr(e) { error = (e); goto bad;}
    182 
    183 static pktq_rps_hash_func_t ether_pktq_rps_hash_p;
    184 
    185 static int ether_output(struct ifnet *, struct mbuf *,
    186     const struct sockaddr *, const struct rtentry *);
    187 
    188 /*
    189  * Ethernet output routine.
    190  * Encapsulate a packet of type family for the local net.
    191  * Assumes that ifp is actually pointer to ethercom structure.
    192  */
    193 static int
    194 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
    195     const struct sockaddr * const dst, const struct rtentry *rt)
    196 {
    197 	uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
    198 	uint16_t etype = 0;
    199 	int error = 0, hdrcmplt = 0;
    200 	struct mbuf *m = m0;
    201 	struct mbuf *mcopy = NULL;
    202 	struct ether_header *eh;
    203 	struct ifnet *ifp = ifp0;
    204 #ifdef INET
    205 	struct arphdr *ah;
    206 #endif
    207 #ifdef NETATALK
    208 	struct at_ifaddr *aa;
    209 #endif
    210 
    211 #ifdef MBUFTRACE
    212 	m_claimm(m, ifp->if_mowner);
    213 #endif
    214 
    215 #if NCARP > 0
    216 	if (ifp->if_type == IFT_CARP) {
    217 		struct ifaddr *ifa;
    218 		int s = pserialize_read_enter();
    219 
    220 		/* loop back if this is going to the carp interface */
    221 		if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
    222 		    (ifa = ifa_ifwithaddr(dst)) != NULL) {
    223 			if (ifa->ifa_ifp == ifp0) {
    224 				pserialize_read_exit(s);
    225 				return looutput(ifp0, m, dst, rt);
    226 			}
    227 		}
    228 		pserialize_read_exit(s);
    229 
    230 		ifp = ifp->if_carpdev;
    231 		/* ac = (struct arpcom *)ifp; */
    232 
    233 		if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
    234 		    (IFF_UP | IFF_RUNNING))
    235 			senderr(ENETDOWN);
    236 	}
    237 #endif
    238 
    239 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
    240 		senderr(ENETDOWN);
    241 
    242 	switch (dst->sa_family) {
    243 
    244 #ifdef INET
    245 	case AF_INET:
    246 		if (m->m_flags & M_BCAST) {
    247 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    248 		} else if (m->m_flags & M_MCAST) {
    249 			ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
    250 		} else {
    251 			error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst));
    252 			if (error)
    253 				return (error == EWOULDBLOCK) ? 0 : error;
    254 		}
    255 		/* If broadcasting on a simplex interface, loopback a copy */
    256 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    257 			mcopy = m_copypacket(m, M_DONTWAIT);
    258 		etype = htons(ETHERTYPE_IP);
    259 		break;
    260 
    261 	case AF_ARP:
    262 		ah = mtod(m, struct arphdr *);
    263 		if (m->m_flags & M_BCAST) {
    264 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    265 		} else {
    266 			void *tha = ar_tha(ah);
    267 
    268 			if (tha == NULL) {
    269 				/* fake with ARPHRD_IEEE1394 */
    270 				m_freem(m);
    271 				return 0;
    272 			}
    273 			memcpy(edst, tha, sizeof(edst));
    274 		}
    275 
    276 		ah->ar_hrd = htons(ARPHRD_ETHER);
    277 
    278 		switch (ntohs(ah->ar_op)) {
    279 		case ARPOP_REVREQUEST:
    280 		case ARPOP_REVREPLY:
    281 			etype = htons(ETHERTYPE_REVARP);
    282 			break;
    283 
    284 		case ARPOP_REQUEST:
    285 		case ARPOP_REPLY:
    286 		default:
    287 			etype = htons(ETHERTYPE_ARP);
    288 		}
    289 		break;
    290 #endif
    291 
    292 #ifdef INET6
    293 	case AF_INET6:
    294 		if (m->m_flags & M_BCAST) {
    295 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    296 		} else if (m->m_flags & M_MCAST) {
    297 			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
    298 			    edst);
    299 		} else {
    300 			error = nd6_resolve(ifp0, rt, m, dst, edst,
    301 			    sizeof(edst));
    302 			if (error)
    303 				return (error == EWOULDBLOCK) ? 0 : error;
    304 		}
    305 		etype = htons(ETHERTYPE_IPV6);
    306 		break;
    307 #endif
    308 
    309 #ifdef NETATALK
    310 	case AF_APPLETALK: {
    311 		struct ifaddr *ifa;
    312 		int s;
    313 
    314 		KERNEL_LOCK(1, NULL);
    315 
    316 		if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
    317 			KERNEL_UNLOCK_ONE(NULL);
    318 			return 0;
    319 		}
    320 
    321 		/*
    322 		 * ifaddr is the first thing in at_ifaddr
    323 		 */
    324 		s = pserialize_read_enter();
    325 		ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
    326 		if (ifa == NULL) {
    327 			pserialize_read_exit(s);
    328 			KERNEL_UNLOCK_ONE(NULL);
    329 			senderr(EADDRNOTAVAIL);
    330 		}
    331 		aa = (struct at_ifaddr *)ifa;
    332 
    333 		/*
    334 		 * In the phase 2 case, we need to prepend an mbuf for the
    335 		 * llc header.
    336 		 */
    337 		if (aa->aa_flags & AFA_PHASE2) {
    338 			struct llc llc;
    339 
    340 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    341 			if (m == NULL) {
    342 				pserialize_read_exit(s);
    343 				KERNEL_UNLOCK_ONE(NULL);
    344 				senderr(ENOBUFS);
    345 			}
    346 
    347 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
    348 			llc.llc_control = LLC_UI;
    349 			memcpy(llc.llc_snap_org_code, at_org_code,
    350 			    sizeof(llc.llc_snap_org_code));
    351 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
    352 			memcpy(mtod(m, void *), &llc, sizeof(struct llc));
    353 		} else {
    354 			etype = htons(ETHERTYPE_ATALK);
    355 		}
    356 		pserialize_read_exit(s);
    357 		KERNEL_UNLOCK_ONE(NULL);
    358 		break;
    359 	}
    360 #endif /* NETATALK */
    361 
    362 	case pseudo_AF_HDRCMPLT:
    363 		hdrcmplt = 1;
    364 		memcpy(esrc,
    365 		    ((const struct ether_header *)dst->sa_data)->ether_shost,
    366 		    sizeof(esrc));
    367 		/* FALLTHROUGH */
    368 
    369 	case AF_UNSPEC:
    370 		memcpy(edst,
    371 		    ((const struct ether_header *)dst->sa_data)->ether_dhost,
    372 		    sizeof(edst));
    373 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
    374 		etype = ((const struct ether_header *)dst->sa_data)->ether_type;
    375 		break;
    376 
    377 	default:
    378 		rt_unhandled(__func__, ifp, dst);
    379 		senderr(EAFNOSUPPORT);
    380 	}
    381 
    382 #ifdef MPLS
    383 	{
    384 		struct m_tag *mtag;
    385 		mtag = m_tag_find(m, PACKET_TAG_MPLS);
    386 		if (mtag != NULL) {
    387 			/* Having the tag itself indicates it's MPLS */
    388 			etype = htons(ETHERTYPE_MPLS);
    389 			m_tag_delete(m, mtag);
    390 		}
    391 	}
    392 #endif
    393 
    394 	if (mcopy)
    395 		(void)looutput(ifp, mcopy, dst, rt);
    396 
    397 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    398 
    399 	/*
    400 	 * If no ether type is set, this must be a 802.2 formatted packet.
    401 	 */
    402 	if (etype == 0)
    403 		etype = htons(m->m_pkthdr.len);
    404 
    405 	/*
    406 	 * Add local net header. If no space in first mbuf, allocate another.
    407 	 */
    408 	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
    409 	if (m == NULL)
    410 		senderr(ENOBUFS);
    411 
    412 	eh = mtod(m, struct ether_header *);
    413 	/* Note: etype is already in network byte order. */
    414 	memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
    415 	memcpy(eh->ether_dhost, edst, sizeof(edst));
    416 	if (hdrcmplt) {
    417 		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
    418 	} else {
    419 	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
    420 		    sizeof(eh->ether_shost));
    421 	}
    422 
    423 #if NCARP > 0
    424 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
    425 		/* update with virtual MAC */
    426 		memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
    427 		    sizeof(eh->ether_shost));
    428 	}
    429 #endif
    430 
    431 	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
    432 		return error;
    433 	if (m == NULL)
    434 		return 0;
    435 
    436 #if NBRIDGE > 0
    437 	/*
    438 	 * Bridges require special output handling.
    439 	 */
    440 	if (ifp->if_bridge)
    441 		return bridge_output(ifp, m, NULL, NULL);
    442 #endif
    443 
    444 #if NCARP > 0
    445 	if (ifp != ifp0)
    446 		if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
    447 #endif
    448 
    449 #ifdef ALTQ
    450 	KERNEL_LOCK(1, NULL);
    451 	/*
    452 	 * If ALTQ is enabled on the parent interface, do
    453 	 * classification; the queueing discipline might not
    454 	 * require classification, but might require the
    455 	 * address family/header pointer in the pktattr.
    456 	 */
    457 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    458 		altq_etherclassify(&ifp->if_snd, m);
    459 	KERNEL_UNLOCK_ONE(NULL);
    460 #endif
    461 	return ifq_enqueue(ifp, m);
    462 
    463 bad:
    464 	if_statinc(ifp, if_oerrors);
    465 	m_freem(m);
    466 	return error;
    467 }
    468 
    469 #ifdef ALTQ
    470 /*
    471  * This routine is a slight hack to allow a packet to be classified
    472  * if the Ethernet headers are present.  It will go away when ALTQ's
    473  * classification engine understands link headers.
    474  *
    475  * XXX: We may need to do m_pullups here. First to ensure struct ether_header
    476  * is indeed contiguous, then to read the LLC and so on.
    477  */
    478 void
    479 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
    480 {
    481 	struct ether_header *eh;
    482 	struct mbuf *mtop = m;
    483 	uint16_t ether_type;
    484 	int hlen, af, hdrsize;
    485 	void *hdr;
    486 
    487 	KASSERT((mtop->m_flags & M_PKTHDR) != 0);
    488 
    489 	hlen = ETHER_HDR_LEN;
    490 	eh = mtod(m, struct ether_header *);
    491 
    492 	ether_type = htons(eh->ether_type);
    493 
    494 	if (ether_type < ETHERMTU) {
    495 		/* LLC/SNAP */
    496 		struct llc *llc = (struct llc *)(eh + 1);
    497 		hlen += 8;
    498 
    499 		if (m->m_len < hlen ||
    500 		    llc->llc_dsap != LLC_SNAP_LSAP ||
    501 		    llc->llc_ssap != LLC_SNAP_LSAP ||
    502 		    llc->llc_control != LLC_UI) {
    503 			/* Not SNAP. */
    504 			goto bad;
    505 		}
    506 
    507 		ether_type = htons(llc->llc_un.type_snap.ether_type);
    508 	}
    509 
    510 	switch (ether_type) {
    511 	case ETHERTYPE_IP:
    512 		af = AF_INET;
    513 		hdrsize = 20;		/* sizeof(struct ip) */
    514 		break;
    515 
    516 	case ETHERTYPE_IPV6:
    517 		af = AF_INET6;
    518 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
    519 		break;
    520 
    521 	default:
    522 		af = AF_UNSPEC;
    523 		hdrsize = 0;
    524 		break;
    525 	}
    526 
    527 	while (m->m_len <= hlen) {
    528 		hlen -= m->m_len;
    529 		m = m->m_next;
    530 		if (m == NULL)
    531 			goto bad;
    532 	}
    533 
    534 	if (m->m_len < (hlen + hdrsize)) {
    535 		/*
    536 		 * protocol header not in a single mbuf.
    537 		 * We can't cope with this situation right
    538 		 * now (but it shouldn't ever happen, really, anyhow).
    539 		 */
    540 #ifdef DEBUG
    541 		printf("altq_etherclassify: headers span multiple mbufs: "
    542 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
    543 #endif
    544 		goto bad;
    545 	}
    546 
    547 	m->m_data += hlen;
    548 	m->m_len -= hlen;
    549 
    550 	hdr = mtod(m, void *);
    551 
    552 	if (ALTQ_NEEDS_CLASSIFY(ifq)) {
    553 		mtop->m_pkthdr.pattr_class =
    554 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
    555 	}
    556 	mtop->m_pkthdr.pattr_af = af;
    557 	mtop->m_pkthdr.pattr_hdr = hdr;
    558 
    559 	m->m_data -= hlen;
    560 	m->m_len += hlen;
    561 
    562 	return;
    563 
    564 bad:
    565 	mtop->m_pkthdr.pattr_class = NULL;
    566 	mtop->m_pkthdr.pattr_hdr = NULL;
    567 	mtop->m_pkthdr.pattr_af = AF_UNSPEC;
    568 }
    569 #endif /* ALTQ */
    570 
    571 #if defined (LLC) || defined (NETATALK)
    572 static void
    573 ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh)
    574 {
    575 	pktqueue_t *pktq = NULL;
    576 	struct llc *l;
    577 
    578 	if (m->m_len < sizeof(*eh) + sizeof(struct llc))
    579 		goto error;
    580 
    581 	l = (struct llc *)(eh+1);
    582 	switch (l->llc_dsap) {
    583 #ifdef NETATALK
    584 	case LLC_SNAP_LSAP:
    585 		switch (l->llc_control) {
    586 		case LLC_UI:
    587 			if (l->llc_ssap != LLC_SNAP_LSAP)
    588 				goto error;
    589 
    590 			if (memcmp(&(l->llc_snap_org_code)[0],
    591 			    at_org_code, sizeof(at_org_code)) == 0 &&
    592 			    ntohs(l->llc_snap_ether_type) ==
    593 			    ETHERTYPE_ATALK) {
    594 				pktq = at_pktq2;
    595 				m_adj(m, sizeof(struct ether_header)
    596 				    + sizeof(struct llc));
    597 				break;
    598 			}
    599 
    600 			if (memcmp(&(l->llc_snap_org_code)[0],
    601 			    aarp_org_code,
    602 			    sizeof(aarp_org_code)) == 0 &&
    603 			    ntohs(l->llc_snap_ether_type) ==
    604 			    ETHERTYPE_AARP) {
    605 				m_adj(m, sizeof(struct ether_header)
    606 				    + sizeof(struct llc));
    607 				aarpinput(ifp, m); /* XXX queue? */
    608 				return;
    609 			}
    610 
    611 		default:
    612 			goto error;
    613 		}
    614 		break;
    615 #endif
    616 	default:
    617 		goto noproto;
    618 	}
    619 
    620 	KASSERT(pktq != NULL);
    621 	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    622 		m_freem(m);
    623 	}
    624 	return;
    625 
    626 noproto:
    627 	m_freem(m);
    628 	if_statinc(ifp, if_noproto);
    629 	return;
    630 error:
    631 	m_freem(m);
    632 	if_statinc(ifp, if_ierrors);
    633 	return;
    634 }
    635 #endif /* defined (LLC) || defined (NETATALK) */
    636 
    637 /*
    638  * Process a received Ethernet packet;
    639  * the packet is in the mbuf chain m with
    640  * the ether header.
    641  */
    642 void
    643 ether_input(struct ifnet *ifp, struct mbuf *m)
    644 {
    645 #if NVLAN > 0 || defined(MBUFTRACE)
    646 	struct ethercom *ec = (struct ethercom *) ifp;
    647 #endif
    648 	pktqueue_t *pktq = NULL;
    649 	uint16_t etype;
    650 	struct ether_header *eh;
    651 	size_t ehlen;
    652 	static int earlypkts;
    653 
    654 	/* No RPS for not-IP. */
    655 	pktq_rps_hash_func_t rps_hash = NULL;
    656 
    657 	KASSERT(!cpu_intr_p());
    658 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    659 
    660 	if ((ifp->if_flags & IFF_UP) == 0)
    661 		goto drop;
    662 
    663 #ifdef MBUFTRACE
    664 	m_claimm(m, &ec->ec_rx_mowner);
    665 #endif
    666 
    667 	if (__predict_false(m->m_len < sizeof(*eh))) {
    668 		if ((m = m_pullup(m, sizeof(*eh))) == NULL) {
    669 			if_statinc(ifp, if_ierrors);
    670 			return;
    671 		}
    672 	}
    673 
    674 	eh = mtod(m, struct ether_header *);
    675 	etype = ntohs(eh->ether_type);
    676 	ehlen = sizeof(*eh);
    677 
    678 	if (__predict_false(earlypkts < 100 ||
    679 		entropy_epoch() == (unsigned)-1)) {
    680 		rnd_add_data(NULL, eh, ehlen, 0);
    681 		earlypkts++;
    682 	}
    683 
    684 	/*
    685 	 * Determine if the packet is within its size limits. For MPLS the
    686 	 * header length is variable, so we skip the check.
    687 	 */
    688 	if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
    689 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
    690 #ifdef DIAGNOSTIC
    691 		mutex_enter(&bigpktpps_lock);
    692 		if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
    693 		    bigpktppslim)) {
    694 			printf("%s: discarding oversize frame (len=%d)\n",
    695 			    ifp->if_xname, m->m_pkthdr.len);
    696 		}
    697 		mutex_exit(&bigpktpps_lock);
    698 #endif
    699 		goto error;
    700 	}
    701 
    702 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    703 		/*
    704 		 * If this is not a simplex interface, drop the packet
    705 		 * if it came from us.
    706 		 */
    707 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
    708 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
    709 		    ETHER_ADDR_LEN) == 0) {
    710 			goto drop;
    711 		}
    712 
    713 		if (memcmp(etherbroadcastaddr,
    714 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    715 			m->m_flags |= M_BCAST;
    716 		else
    717 			m->m_flags |= M_MCAST;
    718 		if_statinc(ifp, if_imcasts);
    719 	}
    720 
    721 	/* If the CRC is still on the packet, trim it off. */
    722 	if (m->m_flags & M_HASFCS) {
    723 		m_adj(m, -ETHER_CRC_LEN);
    724 		m->m_flags &= ~M_HASFCS;
    725 	}
    726 
    727 	if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
    728 
    729 	if (!vlan_has_tag(m) && etype == ETHERTYPE_VLAN) {
    730 		m = ether_strip_vlantag(m);
    731 		if (m == NULL) {
    732 			if_statinc(ifp, if_ierrors);
    733 			return;
    734 		}
    735 
    736 		eh = mtod(m, struct ether_header *);
    737 		etype = ntohs(eh->ether_type);
    738 		ehlen = sizeof(*eh);
    739 	}
    740 
    741 	if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
    742 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
    743 	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
    744 	     ETHER_ADDR_LEN) != 0) {
    745 		m->m_flags |= M_PROMISC;
    746 	}
    747 
    748 	if ((m->m_flags & M_PROMISC) == 0) {
    749 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    750 			return;
    751 		if (m == NULL)
    752 			return;
    753 
    754 		eh = mtod(m, struct ether_header *);
    755 		etype = ntohs(eh->ether_type);
    756 	}
    757 
    758 	/*
    759 	 * Processing a logical interfaces that are able
    760 	 * to configure vlan(4).
    761 	*/
    762 #if NAGR > 0
    763 	if (ifp->if_lagg != NULL &&
    764 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
    765 		m->m_flags &= ~M_PROMISC;
    766 		agr_input(ifp, m);
    767 		return;
    768 	}
    769 #endif
    770 
    771 	/*
    772 	 * VLAN processing.
    773 	 *
    774 	 * VLAN provides service delimiting so the frames are
    775 	 * processed before other handlings. If a VLAN interface
    776 	 * does not exist to take those frames, they're returned
    777 	 * to ether_input().
    778 	 */
    779 
    780 	if (vlan_has_tag(m)) {
    781 		if (EVL_VLANOFTAG(vlan_get_tag(m)) == 0) {
    782 			if (etype == ETHERTYPE_VLAN ||
    783 			     etype == ETHERTYPE_QINQ)
    784 				goto drop;
    785 
    786 			/* XXX we should actually use the prio value? */
    787 			m->m_flags &= ~M_VLANTAG;
    788 		} else {
    789 #if NVLAN > 0
    790 			if (ec->ec_nvlans > 0) {
    791 				m = vlan_input(ifp, m);
    792 
    793 				/* vlan_input() called ether_input() recursively */
    794 				if (m == NULL)
    795 					return;
    796 			}
    797 #endif
    798 			/* drop VLAN frames not for this port. */
    799 			goto noproto;
    800 		}
    801 	}
    802 
    803 #if NCARP > 0
    804 	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
    805 		/*
    806 		 * Clear M_PROMISC, in case the packet comes from a
    807 		 * vlan.
    808 		 */
    809 		m->m_flags &= ~M_PROMISC;
    810 		if (carp_input(m, (uint8_t *)&eh->ether_shost,
    811 		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
    812 			return;
    813 	}
    814 #endif
    815 
    816 	/*
    817 	 * Handle protocols that expect to have the Ethernet header
    818 	 * (and possibly FCS) intact.
    819 	 */
    820 	switch (etype) {
    821 #if NPPPOE > 0
    822 	case ETHERTYPE_PPPOEDISC:
    823 		pppoedisc_input(ifp, m);
    824 		return;
    825 
    826 	case ETHERTYPE_PPPOE:
    827 		pppoe_input(ifp, m);
    828 		return;
    829 #endif
    830 
    831 	case ETHERTYPE_SLOWPROTOCOLS: {
    832 		uint8_t subtype;
    833 
    834 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
    835 			goto error;
    836 
    837 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
    838 		switch (subtype) {
    839 #if NAGR > 0
    840 		case SLOWPROTOCOLS_SUBTYPE_LACP:
    841 			if (ifp->if_lagg != NULL) {
    842 				ieee8023ad_lacp_input(ifp, m);
    843 				return;
    844 			}
    845 			break;
    846 
    847 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
    848 			if (ifp->if_lagg != NULL) {
    849 				ieee8023ad_marker_input(ifp, m);
    850 				return;
    851 			}
    852 			break;
    853 #endif
    854 
    855 		default:
    856 			if (subtype == 0 || subtype > 10) {
    857 				/* illegal value */
    858 				goto noproto;
    859 			}
    860 			/* unknown subtype */
    861 			break;
    862 		}
    863 	}
    864 	/* FALLTHROUGH */
    865 	default:
    866 		if (m->m_flags & M_PROMISC)
    867 			goto drop;
    868 	}
    869 
    870 	/* If the CRC is still on the packet, trim it off. */
    871 	if (m->m_flags & M_HASFCS) {
    872 		m_adj(m, -ETHER_CRC_LEN);
    873 		m->m_flags &= ~M_HASFCS;
    874 	}
    875 
    876 	/* etype represents the size of the payload in this case */
    877 	if (etype <= ETHERMTU + sizeof(struct ether_header)) {
    878 		KASSERT(ehlen == sizeof(*eh));
    879 #if defined (LLC) || defined (NETATALK)
    880 		ether_input_llc(ifp, m, eh);
    881 		return;
    882 #else
    883 		/* ethertype of 0-1500 is regarded as noproto */
    884 		goto noproto;
    885 #endif
    886 	}
    887 
    888 	/* For ARP packets, store the source address so that
    889 	 * ARP DAD probes can be validated. */
    890 	if (etype == ETHERTYPE_ARP) {
    891 		struct m_tag *mtag;
    892 
    893 		mtag = m_tag_get(PACKET_TAG_ETHERNET_SRC, ETHER_ADDR_LEN,
    894 		    M_NOWAIT);
    895 		if (mtag != NULL) {
    896 			memcpy(mtag + 1, &eh->ether_shost, ETHER_ADDR_LEN);
    897 			m_tag_prepend(m, mtag);
    898 		}
    899 	}
    900 
    901 	/* Strip off the Ethernet header. */
    902 	m_adj(m, ehlen);
    903 
    904 	switch (etype) {
    905 #ifdef INET
    906 	case ETHERTYPE_IP:
    907 #ifdef GATEWAY
    908 		if (ipflow_fastforward(m))
    909 			return;
    910 #endif
    911 		pktq = ip_pktq;
    912 		rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
    913 		break;
    914 
    915 	case ETHERTYPE_ARP:
    916 		pktq = arp_pktq;
    917 		break;
    918 
    919 	case ETHERTYPE_REVARP:
    920 		revarpinput(m);	/* XXX queue? */
    921 		return;
    922 #endif
    923 
    924 #ifdef INET6
    925 	case ETHERTYPE_IPV6:
    926 		if (__predict_false(!in6_present))
    927 			goto noproto;
    928 #ifdef GATEWAY
    929 		if (ip6flow_fastforward(&m))
    930 			return;
    931 #endif
    932 		pktq = ip6_pktq;
    933 		rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
    934 		break;
    935 #endif
    936 
    937 #ifdef NETATALK
    938 	case ETHERTYPE_ATALK:
    939 		pktq = at_pktq1;
    940 		break;
    941 
    942 	case ETHERTYPE_AARP:
    943 		aarpinput(ifp, m); /* XXX queue? */
    944 		return;
    945 #endif
    946 
    947 #ifdef MPLS
    948 	case ETHERTYPE_MPLS:
    949 		pktq = mpls_pktq;
    950 		break;
    951 #endif
    952 
    953 	default:
    954 		goto noproto;
    955 	}
    956 
    957 	KASSERT(pktq != NULL);
    958 	const uint32_t h = rps_hash ? pktq_rps_hash(&rps_hash, m) : 0;
    959 	if (__predict_false(!pktq_enqueue(pktq, m, h))) {
    960 		m_freem(m);
    961 	}
    962 	return;
    963 
    964 drop:
    965 	m_freem(m);
    966 	if_statinc(ifp, if_iqdrops);
    967 	return;
    968 noproto:
    969 	m_freem(m);
    970 	if_statinc(ifp, if_noproto);
    971 	return;
    972 error:
    973 	m_freem(m);
    974 	if_statinc(ifp, if_ierrors);
    975 	return;
    976 }
    977 
    978 static void
    979 ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
    980 {
    981 	struct ether_vlan_header evl;
    982 	struct m_hdr mh, md;
    983 
    984 	KASSERT(bp != NULL);
    985 
    986 	if (!vlan_has_tag(m)) {
    987 		bpf_mtap3(bp, m, direction);
    988 		return;
    989 	}
    990 
    991 	memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN);
    992 	evl.evl_proto = evl.evl_encap_proto;
    993 	evl.evl_encap_proto = htons(ETHERTYPE_VLAN);
    994 	evl.evl_tag = htons(vlan_get_tag(m));
    995 
    996 	md.mh_flags = 0;
    997 	md.mh_data = m->m_data + ETHER_HDR_LEN;
    998 	md.mh_len = m->m_len - ETHER_HDR_LEN;
    999 	md.mh_next = m->m_next;
   1000 
   1001 	mh.mh_flags = 0;
   1002 	mh.mh_data = (char *)&evl;
   1003 	mh.mh_len = sizeof(evl);
   1004 	mh.mh_next = (struct mbuf *)&md;
   1005 
   1006 	bpf_mtap3(bp, (struct mbuf *)&mh, direction);
   1007 }
   1008 
   1009 /*
   1010  * Convert Ethernet address to printable (loggable) representation.
   1011  */
   1012 char *
   1013 ether_sprintf(const u_char *ap)
   1014 {
   1015 	static char etherbuf[3 * ETHER_ADDR_LEN];
   1016 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
   1017 }
   1018 
   1019 char *
   1020 ether_snprintf(char *buf, size_t len, const u_char *ap)
   1021 {
   1022 	char *cp = buf;
   1023 	size_t i;
   1024 
   1025 	for (i = 0; i < len / 3; i++) {
   1026 		*cp++ = hexdigits[*ap >> 4];
   1027 		*cp++ = hexdigits[*ap++ & 0xf];
   1028 		*cp++ = ':';
   1029 	}
   1030 	*--cp = '\0';
   1031 	return buf;
   1032 }
   1033 
   1034 /*
   1035  * Perform common duties while attaching to interface list
   1036  */
   1037 void
   1038 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
   1039 {
   1040 	struct ethercom *ec = (struct ethercom *)ifp;
   1041 	char xnamebuf[HOOKNAMSIZ];
   1042 
   1043 	if (lla != NULL && ETHER_IS_MULTICAST(lla))
   1044 		aprint_error("The multicast bit is set in the MAC address. "
   1045 			"It's wrong.\n");
   1046 
   1047 	ifp->if_type = IFT_ETHER;
   1048 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1049 	ifp->if_dlt = DLT_EN10MB;
   1050 	ifp->if_mtu = ETHERMTU;
   1051 	ifp->if_output = ether_output;
   1052 	ifp->_if_input = ether_input;
   1053 	if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING)
   1054 		ifp->if_bpf_mtap = ether_bpf_mtap;
   1055 	if (ifp->if_baudrate == 0)
   1056 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1057 
   1058 	if (lla != NULL)
   1059 		if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
   1060 
   1061 	LIST_INIT(&ec->ec_multiaddrs);
   1062 	SIMPLEQ_INIT(&ec->ec_vids);
   1063 	ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   1064 	ec->ec_flags = 0;
   1065 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1066 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1067 	snprintf(xnamebuf, sizeof(xnamebuf),
   1068 	    "%s-ether_ifdetachhooks", ifp->if_xname);
   1069 	ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
   1070 #ifdef MBUFTRACE
   1071 	mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
   1072 	mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
   1073 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1074 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1075 	ifp->if_mowner = &ec->ec_tx_mowner;
   1076 #endif
   1077 }
   1078 
   1079 void
   1080 ether_ifdetach(struct ifnet *ifp)
   1081 {
   1082 	struct ethercom *ec = (void *) ifp;
   1083 	struct ether_multi *enm;
   1084 
   1085 	IFNET_ASSERT_UNLOCKED(ifp);
   1086 	/*
   1087 	 * Prevent further calls to ioctl (for example turning off
   1088 	 * promiscuous mode from the bridge code), which eventually can
   1089 	 * call if_init() which can cause panics because the interface
   1090 	 * is in the process of being detached. Return device not configured
   1091 	 * instead.
   1092 	 */
   1093 	ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
   1094 	    enxio);
   1095 
   1096 	simplehook_dohooks(ec->ec_ifdetach_hooks);
   1097 	KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
   1098 	simplehook_destroy(ec->ec_ifdetach_hooks);
   1099 
   1100 	bpf_detach(ifp);
   1101 
   1102 	ETHER_LOCK(ec);
   1103 	KASSERT(ec->ec_nvlans == 0);
   1104 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1105 		LIST_REMOVE(enm, enm_list);
   1106 		kmem_free(enm, sizeof(*enm));
   1107 		ec->ec_multicnt--;
   1108 	}
   1109 	ETHER_UNLOCK(ec);
   1110 
   1111 	mutex_obj_free(ec->ec_lock);
   1112 	ec->ec_lock = NULL;
   1113 
   1114 	ifp->if_mowner = NULL;
   1115 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1116 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1117 }
   1118 
   1119 void *
   1120 ether_ifdetachhook_establish(struct ifnet *ifp,
   1121     void (*fn)(void *), void *arg)
   1122 {
   1123 	struct ethercom *ec;
   1124 	khook_t *hk;
   1125 
   1126 	if (ifp->if_type != IFT_ETHER)
   1127 		return NULL;
   1128 
   1129 	ec = (struct ethercom *)ifp;
   1130 	hk = simplehook_establish(ec->ec_ifdetach_hooks,
   1131 	    fn, arg);
   1132 
   1133 	return (void *)hk;
   1134 }
   1135 
   1136 void
   1137 ether_ifdetachhook_disestablish(struct ifnet *ifp,
   1138     void *vhook, kmutex_t *lock)
   1139 {
   1140 	struct ethercom *ec;
   1141 
   1142 	if (vhook == NULL)
   1143 		return;
   1144 
   1145 	ec = (struct ethercom *)ifp;
   1146 	simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
   1147 }
   1148 
   1149 #if 0
   1150 /*
   1151  * This is for reference.  We have a table-driven version
   1152  * of the little-endian crc32 generator, which is faster
   1153  * than the double-loop.
   1154  */
   1155 uint32_t
   1156 ether_crc32_le(const uint8_t *buf, size_t len)
   1157 {
   1158 	uint32_t c, crc, carry;
   1159 	size_t i, j;
   1160 
   1161 	crc = 0xffffffffU;	/* initial value */
   1162 
   1163 	for (i = 0; i < len; i++) {
   1164 		c = buf[i];
   1165 		for (j = 0; j < 8; j++) {
   1166 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1167 			crc >>= 1;
   1168 			c >>= 1;
   1169 			if (carry)
   1170 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1171 		}
   1172 	}
   1173 
   1174 	return (crc);
   1175 }
   1176 #else
   1177 uint32_t
   1178 ether_crc32_le(const uint8_t *buf, size_t len)
   1179 {
   1180 	static const uint32_t crctab[] = {
   1181 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1182 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1183 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1184 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1185 	};
   1186 	uint32_t crc;
   1187 	size_t i;
   1188 
   1189 	crc = 0xffffffffU;	/* initial value */
   1190 
   1191 	for (i = 0; i < len; i++) {
   1192 		crc ^= buf[i];
   1193 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1194 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1195 	}
   1196 
   1197 	return (crc);
   1198 }
   1199 #endif
   1200 
   1201 uint32_t
   1202 ether_crc32_be(const uint8_t *buf, size_t len)
   1203 {
   1204 	uint32_t c, crc, carry;
   1205 	size_t i, j;
   1206 
   1207 	crc = 0xffffffffU;	/* initial value */
   1208 
   1209 	for (i = 0; i < len; i++) {
   1210 		c = buf[i];
   1211 		for (j = 0; j < 8; j++) {
   1212 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1213 			crc <<= 1;
   1214 			c >>= 1;
   1215 			if (carry)
   1216 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1217 		}
   1218 	}
   1219 
   1220 	return (crc);
   1221 }
   1222 
   1223 #ifdef INET
   1224 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1225     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1226 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1227     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1228 #endif
   1229 #ifdef INET6
   1230 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1231     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1232 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1233     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1234 #endif
   1235 
   1236 /*
   1237  * ether_aton implementation, not using a static buffer.
   1238  */
   1239 int
   1240 ether_aton_r(u_char *dest, size_t len, const char *str)
   1241 {
   1242 	const u_char *cp = (const void *)str;
   1243 	u_char *ep;
   1244 
   1245 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
   1246 
   1247 	if (len < ETHER_ADDR_LEN)
   1248 		return ENOSPC;
   1249 
   1250 	ep = dest + ETHER_ADDR_LEN;
   1251 
   1252 	while (*cp) {
   1253 		if (!isxdigit(*cp))
   1254 			return EINVAL;
   1255 
   1256 		*dest = atox(*cp);
   1257 		cp++;
   1258 		if (isxdigit(*cp)) {
   1259 			*dest = (*dest << 4) | atox(*cp);
   1260 			cp++;
   1261 		}
   1262 		dest++;
   1263 
   1264 		if (dest == ep)
   1265 			return (*cp == '\0') ? 0 : ENAMETOOLONG;
   1266 
   1267 		switch (*cp) {
   1268 		case ':':
   1269 		case '-':
   1270 		case '.':
   1271 			cp++;
   1272 			break;
   1273 		}
   1274 	}
   1275 	return ENOBUFS;
   1276 }
   1277 
   1278 /*
   1279  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1280  * addresses.
   1281  */
   1282 int
   1283 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
   1284     uint8_t addrhi[ETHER_ADDR_LEN])
   1285 {
   1286 #ifdef INET
   1287 	const struct sockaddr_in *sin;
   1288 #endif
   1289 #ifdef INET6
   1290 	const struct sockaddr_in6 *sin6;
   1291 #endif
   1292 
   1293 	switch (sa->sa_family) {
   1294 
   1295 	case AF_UNSPEC:
   1296 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
   1297 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1298 		break;
   1299 
   1300 #ifdef INET
   1301 	case AF_INET:
   1302 		sin = satocsin(sa);
   1303 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1304 			/*
   1305 			 * An IP address of INADDR_ANY means listen to
   1306 			 * or stop listening to all of the Ethernet
   1307 			 * multicast addresses used for IP.
   1308 			 * (This is for the sake of IP multicast routers.)
   1309 			 */
   1310 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
   1311 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
   1312 		} else {
   1313 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1314 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1315 		}
   1316 		break;
   1317 #endif
   1318 #ifdef INET6
   1319 	case AF_INET6:
   1320 		sin6 = satocsin6(sa);
   1321 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1322 			/*
   1323 			 * An IP6 address of 0 means listen to or stop
   1324 			 * listening to all of the Ethernet multicast
   1325 			 * address used for IP6.
   1326 			 * (This is used for multicast routers.)
   1327 			 */
   1328 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
   1329 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
   1330 		} else {
   1331 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1332 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1333 		}
   1334 		break;
   1335 #endif
   1336 
   1337 	default:
   1338 		return EAFNOSUPPORT;
   1339 	}
   1340 	return 0;
   1341 }
   1342 
   1343 /*
   1344  * Add an Ethernet multicast address or range of addresses to the list for a
   1345  * given interface.
   1346  */
   1347 int
   1348 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
   1349 {
   1350 	struct ether_multi *enm, *_enm;
   1351 	u_char addrlo[ETHER_ADDR_LEN];
   1352 	u_char addrhi[ETHER_ADDR_LEN];
   1353 	int error = 0;
   1354 
   1355 	/* Allocate out of lock */
   1356 	enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
   1357 
   1358 	ETHER_LOCK(ec);
   1359 	error = ether_multiaddr(sa, addrlo, addrhi);
   1360 	if (error != 0)
   1361 		goto out;
   1362 
   1363 	/*
   1364 	 * Verify that we have valid Ethernet multicast addresses.
   1365 	 */
   1366 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
   1367 		error = EINVAL;
   1368 		goto out;
   1369 	}
   1370 
   1371 	/*
   1372 	 * See if the address range is already in the list.
   1373 	 */
   1374 	_enm = ether_lookup_multi(addrlo, addrhi, ec);
   1375 	if (_enm != NULL) {
   1376 		/*
   1377 		 * Found it; just increment the reference count.
   1378 		 */
   1379 		++_enm->enm_refcount;
   1380 		error = 0;
   1381 		goto out;
   1382 	}
   1383 
   1384 	/*
   1385 	 * Link a new multicast record into the interface's multicast list.
   1386 	 */
   1387 	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
   1388 	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
   1389 	enm->enm_refcount = 1;
   1390 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1391 	ec->ec_multicnt++;
   1392 
   1393 	/*
   1394 	 * Return ENETRESET to inform the driver that the list has changed
   1395 	 * and its reception filter should be adjusted accordingly.
   1396 	 */
   1397 	error = ENETRESET;
   1398 	enm = NULL;
   1399 
   1400 out:
   1401 	ETHER_UNLOCK(ec);
   1402 	if (enm != NULL)
   1403 		kmem_free(enm, sizeof(*enm));
   1404 	return error;
   1405 }
   1406 
   1407 /*
   1408  * Delete a multicast address record.
   1409  */
   1410 int
   1411 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
   1412 {
   1413 	struct ether_multi *enm;
   1414 	u_char addrlo[ETHER_ADDR_LEN];
   1415 	u_char addrhi[ETHER_ADDR_LEN];
   1416 	int error;
   1417 
   1418 	ETHER_LOCK(ec);
   1419 	error = ether_multiaddr(sa, addrlo, addrhi);
   1420 	if (error != 0)
   1421 		goto error;
   1422 
   1423 	/*
   1424 	 * Look up the address in our list.
   1425 	 */
   1426 	enm = ether_lookup_multi(addrlo, addrhi, ec);
   1427 	if (enm == NULL) {
   1428 		error = ENXIO;
   1429 		goto error;
   1430 	}
   1431 	if (--enm->enm_refcount != 0) {
   1432 		/*
   1433 		 * Still some claims to this record.
   1434 		 */
   1435 		error = 0;
   1436 		goto error;
   1437 	}
   1438 
   1439 	/*
   1440 	 * No remaining claims to this record; unlink and free it.
   1441 	 */
   1442 	LIST_REMOVE(enm, enm_list);
   1443 	ec->ec_multicnt--;
   1444 	ETHER_UNLOCK(ec);
   1445 	kmem_free(enm, sizeof(*enm));
   1446 
   1447 	/*
   1448 	 * Return ENETRESET to inform the driver that the list has changed
   1449 	 * and its reception filter should be adjusted accordingly.
   1450 	 */
   1451 	return ENETRESET;
   1452 
   1453 error:
   1454 	ETHER_UNLOCK(ec);
   1455 	return error;
   1456 }
   1457 
   1458 void
   1459 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
   1460 {
   1461 	ec->ec_ifflags_cb = cb;
   1462 }
   1463 
   1464 void
   1465 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
   1466 {
   1467 
   1468 	ec->ec_vlan_cb = cb;
   1469 }
   1470 
   1471 static int
   1472 ether_ioctl_reinit(struct ethercom *ec)
   1473 {
   1474 	struct ifnet *ifp = &ec->ec_if;
   1475 	int error;
   1476 
   1477 	KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
   1478 
   1479 	switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1480 	case IFF_RUNNING:
   1481 		/*
   1482 		 * If interface is marked down and it is running,
   1483 		 * then stop and disable it.
   1484 		 */
   1485 		if_stop(ifp, 1);
   1486 		break;
   1487 	case IFF_UP:
   1488 		/*
   1489 		 * If interface is marked up and it is stopped, then
   1490 		 * start it.
   1491 		 */
   1492 		return if_init(ifp);
   1493 	case IFF_UP | IFF_RUNNING:
   1494 		error = 0;
   1495 		if (ec->ec_ifflags_cb != NULL) {
   1496 			error = (*ec->ec_ifflags_cb)(ec);
   1497 			if (error == ENETRESET) {
   1498 				/*
   1499 				 * Reset the interface to pick up
   1500 				 * changes in any other flags that
   1501 				 * affect the hardware state.
   1502 				 */
   1503 				return if_init(ifp);
   1504 			}
   1505 		} else
   1506 			error = if_init(ifp);
   1507 		return error;
   1508 	case 0:
   1509 		break;
   1510 	}
   1511 
   1512 	return 0;
   1513 }
   1514 
   1515 /*
   1516  * Common ioctls for Ethernet interfaces.  Note, we must be
   1517  * called at splnet().
   1518  */
   1519 int
   1520 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1521 {
   1522 	struct ethercom *ec = (void *)ifp;
   1523 	struct eccapreq *eccr;
   1524 	struct ifreq *ifr = (struct ifreq *)data;
   1525 	struct if_laddrreq *iflr = data;
   1526 	const struct sockaddr_dl *sdl;
   1527 	static const uint8_t zero[ETHER_ADDR_LEN];
   1528 	int error;
   1529 
   1530 	switch (cmd) {
   1531 	case SIOCINITIFADDR:
   1532 	    {
   1533 		struct ifaddr *ifa = (struct ifaddr *)data;
   1534 		if (ifa->ifa_addr->sa_family != AF_LINK
   1535 		    && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
   1536 		       (IFF_UP | IFF_RUNNING)) {
   1537 			ifp->if_flags |= IFF_UP;
   1538 			if ((error = if_init(ifp)) != 0)
   1539 				return error;
   1540 		}
   1541 #ifdef INET
   1542 		if (ifa->ifa_addr->sa_family == AF_INET)
   1543 			arp_ifinit(ifp, ifa);
   1544 #endif
   1545 		return 0;
   1546 	    }
   1547 
   1548 	case SIOCSIFMTU:
   1549 	    {
   1550 		int maxmtu;
   1551 
   1552 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1553 			maxmtu = ETHERMTU_JUMBO;
   1554 		else
   1555 			maxmtu = ETHERMTU;
   1556 
   1557 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1558 			return EINVAL;
   1559 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
   1560 			return error;
   1561 		else if (ifp->if_flags & IFF_UP) {
   1562 			/* Make sure the device notices the MTU change. */
   1563 			return if_init(ifp);
   1564 		} else
   1565 			return 0;
   1566 	    }
   1567 
   1568 	case SIOCSIFFLAGS:
   1569 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1570 			return error;
   1571 		return ether_ioctl_reinit(ec);
   1572 	case SIOCGIFFLAGS:
   1573 		error = ifioctl_common(ifp, cmd, data);
   1574 		if (error == 0) {
   1575 			/* Set IFF_ALLMULTI for backcompat */
   1576 			ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
   1577 			    IFF_ALLMULTI : 0;
   1578 		}
   1579 		return error;
   1580 	case SIOCGETHERCAP:
   1581 		eccr = (struct eccapreq *)data;
   1582 		eccr->eccr_capabilities = ec->ec_capabilities;
   1583 		eccr->eccr_capenable = ec->ec_capenable;
   1584 		return 0;
   1585 	case SIOCSETHERCAP:
   1586 		eccr = (struct eccapreq *)data;
   1587 		if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
   1588 			return EINVAL;
   1589 		if (eccr->eccr_capenable == ec->ec_capenable)
   1590 			return 0;
   1591 #if 0 /* notyet */
   1592 		ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
   1593 		    | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
   1594 #else
   1595 		ec->ec_capenable = eccr->eccr_capenable;
   1596 #endif
   1597 		return ether_ioctl_reinit(ec);
   1598 	case SIOCADDMULTI:
   1599 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
   1600 	case SIOCDELMULTI:
   1601 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
   1602 	case SIOCSIFMEDIA:
   1603 	case SIOCGIFMEDIA:
   1604 		if (ec->ec_mii != NULL)
   1605 			return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
   1606 			    cmd);
   1607 		else if (ec->ec_ifmedia != NULL)
   1608 			return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
   1609 		else
   1610 			return ENOTTY;
   1611 		break;
   1612 	case SIOCALIFADDR:
   1613 		sdl = satocsdl(sstocsa(&iflr->addr));
   1614 		if (sdl->sdl_family != AF_LINK)
   1615 			;
   1616 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
   1617 			return EINVAL;
   1618 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
   1619 			return EINVAL;
   1620 		/*FALLTHROUGH*/
   1621 	default:
   1622 		return ifioctl_common(ifp, cmd, data);
   1623 	}
   1624 	return 0;
   1625 }
   1626 
   1627 /*
   1628  * Enable/disable passing VLAN packets if the parent interface supports it.
   1629  * Return:
   1630  * 	 0: Ok
   1631  *	-1: Parent interface does not support vlans
   1632  *	>0: Error
   1633  */
   1634 int
   1635 ether_enable_vlan_mtu(struct ifnet *ifp)
   1636 {
   1637 	int error;
   1638 	struct ethercom *ec = (void *)ifp;
   1639 
   1640 	/* Parent does not support VLAN's */
   1641 	if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
   1642 		return -1;
   1643 
   1644 	/*
   1645 	 * Parent supports the VLAN_MTU capability,
   1646 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
   1647 	 * enable it.
   1648 	 */
   1649 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1650 
   1651 	/* Interface is down, defer for later */
   1652 	if ((ifp->if_flags & IFF_UP) == 0)
   1653 		return 0;
   1654 
   1655 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1656 		return 0;
   1657 
   1658 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1659 	return error;
   1660 }
   1661 
   1662 int
   1663 ether_disable_vlan_mtu(struct ifnet *ifp)
   1664 {
   1665 	int error;
   1666 	struct ethercom *ec = (void *)ifp;
   1667 
   1668 	/* We still have VLAN's, defer for later */
   1669 	if (ec->ec_nvlans != 0)
   1670 		return 0;
   1671 
   1672 	/* Parent does not support VLAB's, nothing to do. */
   1673 	if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
   1674 		return -1;
   1675 
   1676 	/*
   1677 	 * Disable Tx/Rx of VLAN-sized frames.
   1678 	 */
   1679 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1680 
   1681 	/* Interface is down, defer for later */
   1682 	if ((ifp->if_flags & IFF_UP) == 0)
   1683 		return 0;
   1684 
   1685 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1686 		return 0;
   1687 
   1688 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1689 	return error;
   1690 }
   1691 
   1692 /*
   1693  * Add and delete VLAN TAG
   1694  */
   1695 int
   1696 ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
   1697 {
   1698 	struct ethercom *ec = (void *)ifp;
   1699 	struct vlanid_list *vidp;
   1700 	bool vlanmtu_enabled;
   1701 	uint16_t vid = EVL_VLANOFTAG(vtag);
   1702 	int error;
   1703 
   1704 	vlanmtu_enabled = false;
   1705 
   1706 	/* Add a vid to the list */
   1707 	vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
   1708 	vidp->vid = vid;
   1709 
   1710 	ETHER_LOCK(ec);
   1711 	ec->ec_nvlans++;
   1712 	SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
   1713 	ETHER_UNLOCK(ec);
   1714 
   1715 	if (ec->ec_nvlans == 1) {
   1716 		IFNET_LOCK(ifp);
   1717 		error = ether_enable_vlan_mtu(ifp);
   1718 		IFNET_UNLOCK(ifp);
   1719 
   1720 		if (error == 0) {
   1721 			vlanmtu_enabled = true;
   1722 		} else if (error != -1) {
   1723 			goto fail;
   1724 		}
   1725 	}
   1726 
   1727 	if (ec->ec_vlan_cb != NULL) {
   1728 		error = (*ec->ec_vlan_cb)(ec, vid, true);
   1729 		if (error != 0)
   1730 			goto fail;
   1731 	}
   1732 
   1733 	if (vlanmtu_status != NULL)
   1734 		*vlanmtu_status = vlanmtu_enabled;
   1735 
   1736 	return 0;
   1737 fail:
   1738 	ETHER_LOCK(ec);
   1739 	ec->ec_nvlans--;
   1740 	SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
   1741 	ETHER_UNLOCK(ec);
   1742 
   1743 	if (vlanmtu_enabled) {
   1744 		IFNET_LOCK(ifp);
   1745 		(void)ether_disable_vlan_mtu(ifp);
   1746 		IFNET_UNLOCK(ifp);
   1747 	}
   1748 
   1749 	kmem_free(vidp, sizeof(*vidp));
   1750 
   1751 	return error;
   1752 }
   1753 
   1754 int
   1755 ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
   1756 {
   1757 	struct ethercom *ec = (void *)ifp;
   1758 	struct vlanid_list *vidp;
   1759 	uint16_t vid = EVL_VLANOFTAG(vtag);
   1760 
   1761 	ETHER_LOCK(ec);
   1762 	SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
   1763 		if (vidp->vid == vid) {
   1764 			SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
   1765 			    vlanid_list, vid_list);
   1766 			ec->ec_nvlans--;
   1767 			break;
   1768 		}
   1769 	}
   1770 	ETHER_UNLOCK(ec);
   1771 
   1772 	if (vidp == NULL)
   1773 		return ENOENT;
   1774 
   1775 	if (ec->ec_vlan_cb != NULL) {
   1776 		(void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
   1777 	}
   1778 
   1779 	if (ec->ec_nvlans == 0) {
   1780 		IFNET_LOCK(ifp);
   1781 		(void)ether_disable_vlan_mtu(ifp);
   1782 		IFNET_UNLOCK(ifp);
   1783 	}
   1784 
   1785 	kmem_free(vidp, sizeof(*vidp));
   1786 
   1787 	return 0;
   1788 }
   1789 
   1790 int
   1791 ether_inject_vlantag(struct mbuf **mp, uint16_t etype, uint16_t tag)
   1792 {
   1793 	static const size_t min_data_len =
   1794 	    ETHER_MIN_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
   1795 	/* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
   1796 	static const char vlan_zero_pad_buff[ETHER_MIN_LEN] = { 0 };
   1797 
   1798 	struct ether_vlan_header *evl;
   1799 	struct mbuf *m = *mp;
   1800 	int error;
   1801 
   1802 	error = 0;
   1803 
   1804 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
   1805 	if (m == NULL) {
   1806 		error = ENOBUFS;
   1807 		goto out;
   1808 	}
   1809 
   1810 	if (m->m_len < sizeof(*evl)) {
   1811 		m = m_pullup(m, sizeof(*evl));
   1812 		if (m == NULL) {
   1813 			error = ENOBUFS;
   1814 			goto out;
   1815 		}
   1816 	}
   1817 
   1818 	/*
   1819 	 * Transform the Ethernet header into an
   1820 	 * Ethernet header with 802.1Q encapsulation.
   1821 	 */
   1822 	memmove(mtod(m, void *),
   1823 	    mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
   1824 	    sizeof(struct ether_header));
   1825 	evl = mtod(m, struct ether_vlan_header *);
   1826 	evl->evl_proto = evl->evl_encap_proto;
   1827 	evl->evl_encap_proto = htons(etype);
   1828 	evl->evl_tag = htons(tag);
   1829 
   1830 	/*
   1831 	 * To cater for VLAN-aware layer 2 ethernet
   1832 	 * switches which may need to strip the tag
   1833 	 * before forwarding the packet, make sure
   1834 	 * the packet+tag is at least 68 bytes long.
   1835 	 * This is necessary because our parent will
   1836 	 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1837 	 * some switches will not pad by themselves
   1838 	 * after deleting a tag.
   1839 	 */
   1840 	if (m->m_pkthdr.len < min_data_len) {
   1841 		m_copyback(m, m->m_pkthdr.len,
   1842 		    min_data_len - m->m_pkthdr.len,
   1843 		    vlan_zero_pad_buff);
   1844 	}
   1845 
   1846 	m->m_flags &= ~M_VLANTAG;
   1847 
   1848 out:
   1849 	*mp = m;
   1850 	return error;
   1851 }
   1852 
   1853 struct mbuf *
   1854 ether_strip_vlantag(struct mbuf *m)
   1855 {
   1856 	struct ether_vlan_header *evl;
   1857 
   1858 	if (m->m_len < sizeof(*evl) &&
   1859 	    (m = m_pullup(m, sizeof(*evl))) == NULL) {
   1860 		return NULL;
   1861 	}
   1862 
   1863 	if (m_makewritable(&m, 0, sizeof(*evl), M_DONTWAIT)) {
   1864 		m_freem(m);
   1865 		return NULL;
   1866 	}
   1867 
   1868 	evl = mtod(m, struct ether_vlan_header *);
   1869 	KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
   1870 
   1871 	vlan_set_tag(m, ntohs(evl->evl_tag));
   1872 
   1873 	/*
   1874 	 * Restore the original ethertype.  We'll remove
   1875 	 * the encapsulation after we've found the vlan
   1876 	 * interface corresponding to the tag.
   1877 	 */
   1878 	evl->evl_encap_proto = evl->evl_proto;
   1879 
   1880 	/*
   1881 	 * Remove the encapsulation header and append tag.
   1882 	 * The original header has already been fixed up above.
   1883 	 */
   1884 	vlan_set_tag(m, ntohs(evl->evl_tag));
   1885 	memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
   1886 	    offsetof(struct ether_vlan_header, evl_encap_proto));
   1887 	m_adj(m, ETHER_VLAN_ENCAP_LEN);
   1888 
   1889 	return m;
   1890 }
   1891 
   1892 static int
   1893 ether_multicast_sysctl(SYSCTLFN_ARGS)
   1894 {
   1895 	struct ether_multi *enm;
   1896 	struct ifnet *ifp;
   1897 	struct ethercom *ec;
   1898 	int error = 0;
   1899 	size_t written;
   1900 	struct psref psref;
   1901 	int bound;
   1902 	unsigned int multicnt;
   1903 	struct ether_multi_sysctl *addrs;
   1904 	int i;
   1905 
   1906 	if (namelen != 1)
   1907 		return EINVAL;
   1908 
   1909 	bound = curlwp_bind();
   1910 	ifp = if_get_byindex(name[0], &psref);
   1911 	if (ifp == NULL) {
   1912 		error = ENODEV;
   1913 		goto out;
   1914 	}
   1915 	if (ifp->if_type != IFT_ETHER) {
   1916 		if_put(ifp, &psref);
   1917 		*oldlenp = 0;
   1918 		goto out;
   1919 	}
   1920 	ec = (struct ethercom *)ifp;
   1921 
   1922 	if (oldp == NULL) {
   1923 		if_put(ifp, &psref);
   1924 		*oldlenp = ec->ec_multicnt * sizeof(*addrs);
   1925 		goto out;
   1926 	}
   1927 
   1928 	/*
   1929 	 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
   1930 	 * is sleepable, while holding it. Copy data to a local buffer first
   1931 	 * with the lock taken and then call sysctl_copyout without holding it.
   1932 	 */
   1933 retry:
   1934 	multicnt = ec->ec_multicnt;
   1935 
   1936 	if (multicnt == 0) {
   1937 		if_put(ifp, &psref);
   1938 		*oldlenp = 0;
   1939 		goto out;
   1940 	}
   1941 
   1942 	addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
   1943 
   1944 	ETHER_LOCK(ec);
   1945 	if (multicnt != ec->ec_multicnt) {
   1946 		/* The number of multicast addresses has changed */
   1947 		ETHER_UNLOCK(ec);
   1948 		kmem_free(addrs, sizeof(*addrs) * multicnt);
   1949 		goto retry;
   1950 	}
   1951 
   1952 	i = 0;
   1953 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
   1954 		struct ether_multi_sysctl *addr = &addrs[i];
   1955 		addr->enm_refcount = enm->enm_refcount;
   1956 		memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
   1957 		memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
   1958 		i++;
   1959 	}
   1960 	ETHER_UNLOCK(ec);
   1961 
   1962 	error = 0;
   1963 	written = 0;
   1964 	for (i = 0; i < multicnt; i++) {
   1965 		struct ether_multi_sysctl *addr = &addrs[i];
   1966 
   1967 		if (written + sizeof(*addr) > *oldlenp)
   1968 			break;
   1969 		error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
   1970 		if (error)
   1971 			break;
   1972 		written += sizeof(*addr);
   1973 		oldp = (char *)oldp + sizeof(*addr);
   1974 	}
   1975 	kmem_free(addrs, sizeof(*addrs) * multicnt);
   1976 
   1977 	if_put(ifp, &psref);
   1978 
   1979 	*oldlenp = written;
   1980 out:
   1981 	curlwp_bindx(bound);
   1982 	return error;
   1983 }
   1984 
   1985 static void
   1986 ether_sysctl_setup(struct sysctllog **clog)
   1987 {
   1988 	const struct sysctlnode *rnode = NULL;
   1989 
   1990 	sysctl_createv(clog, 0, NULL, &rnode,
   1991 		       CTLFLAG_PERMANENT,
   1992 		       CTLTYPE_NODE, "ether",
   1993 		       SYSCTL_DESCR("Ethernet-specific information"),
   1994 		       NULL, 0, NULL, 0,
   1995 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1996 
   1997 	sysctl_createv(clog, 0, &rnode, NULL,
   1998 		       CTLFLAG_PERMANENT,
   1999 		       CTLTYPE_NODE, "multicast",
   2000 		       SYSCTL_DESCR("multicast addresses"),
   2001 		       ether_multicast_sysctl, 0, NULL, 0,
   2002 		       CTL_CREATE, CTL_EOL);
   2003 
   2004 	sysctl_createv(clog, 0, &rnode, NULL,
   2005 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2006 		       CTLTYPE_STRING, "rps_hash",
   2007 		       SYSCTL_DESCR("Interface rps hash function control"),
   2008 		       sysctl_pktq_rps_hash_handler, 0, (void *)&ether_pktq_rps_hash_p,
   2009 		       PKTQ_RPS_HASH_NAME_LEN,
   2010 		       CTL_CREATE, CTL_EOL);
   2011 }
   2012 
   2013 void
   2014 etherinit(void)
   2015 {
   2016 
   2017 #ifdef DIAGNOSTIC
   2018 	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
   2019 #endif
   2020 	ether_pktq_rps_hash_p = pktq_rps_hash_default;
   2021 	ether_sysctl_setup(NULL);
   2022 }
   2023