Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.297
      1 /*	$NetBSD: if_ethersubr.c,v 1.297 2021/09/30 03:54:04 yamaguchi 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.297 2021/09/30 03:54:04 yamaguchi 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 #include "lagg.h"
     82 
     83 #include <sys/sysctl.h>
     84 #include <sys/mbuf.h>
     85 #include <sys/mutex.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/errno.h>
     88 #include <sys/device.h>
     89 #include <sys/entropy.h>
     90 #include <sys/rndsource.h>
     91 #include <sys/cpu.h>
     92 #include <sys/kmem.h>
     93 #include <sys/hook.h>
     94 
     95 #include <net/if.h>
     96 #include <net/netisr.h>
     97 #include <net/route.h>
     98 #include <net/if_llc.h>
     99 #include <net/if_dl.h>
    100 #include <net/if_types.h>
    101 #include <net/pktqueue.h>
    102 
    103 #include <net/if_media.h>
    104 #include <dev/mii/mii.h>
    105 #include <dev/mii/miivar.h>
    106 
    107 #if NARP == 0
    108 /*
    109  * XXX there should really be a way to issue this warning from within config(8)
    110  */
    111 #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.
    112 #endif
    113 
    114 #include <net/bpf.h>
    115 
    116 #include <net/if_ether.h>
    117 #include <net/if_vlanvar.h>
    118 
    119 #if NPPPOE > 0
    120 #include <net/if_pppoe.h>
    121 #endif
    122 
    123 #if NAGR > 0
    124 #include <net/agr/ieee8023_slowprotocols.h>	/* XXX */
    125 #include <net/agr/ieee8023ad.h>
    126 #include <net/agr/if_agrvar.h>
    127 #endif
    128 
    129 #include <net/lagg/if_laggvar.h>
    130 
    131 #if NBRIDGE > 0
    132 #include <net/if_bridgevar.h>
    133 #endif
    134 
    135 #include <netinet/in.h>
    136 #ifdef INET
    137 #include <netinet/in_var.h>
    138 #endif
    139 #include <netinet/if_inarp.h>
    140 
    141 #ifdef INET6
    142 #ifndef INET
    143 #include <netinet/in.h>
    144 #endif
    145 #include <netinet6/in6_var.h>
    146 #include <netinet6/nd6.h>
    147 #endif
    148 
    149 #include "carp.h"
    150 #if NCARP > 0
    151 #include <netinet/ip_carp.h>
    152 #endif
    153 
    154 #ifdef NETATALK
    155 #include <netatalk/at.h>
    156 #include <netatalk/at_var.h>
    157 #include <netatalk/at_extern.h>
    158 
    159 #define llc_snap_org_code llc_un.type_snap.org_code
    160 #define llc_snap_ether_type llc_un.type_snap.ether_type
    161 
    162 extern u_char	at_org_code[3];
    163 extern u_char	aarp_org_code[3];
    164 #endif /* NETATALK */
    165 
    166 #ifdef MPLS
    167 #include <netmpls/mpls.h>
    168 #include <netmpls/mpls_var.h>
    169 #endif
    170 
    171 CTASSERT(sizeof(struct ether_addr) == 6);
    172 CTASSERT(sizeof(struct ether_header) == 14);
    173 
    174 #ifdef DIAGNOSTIC
    175 static struct timeval bigpktppslim_last;
    176 static int bigpktppslim = 2;	/* XXX */
    177 static int bigpktpps_count;
    178 static kmutex_t bigpktpps_lock __cacheline_aligned;
    179 #endif
    180 
    181 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
    182     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    183 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
    184     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
    185 #define senderr(e) { error = (e); goto bad;}
    186 
    187 /* if_lagg(4) support */
    188 struct mbuf *(*lagg_input_ethernet_p)(struct ifnet *, struct mbuf *);
    189 
    190 static int ether_output(struct ifnet *, struct mbuf *,
    191     const struct sockaddr *, const struct rtentry *);
    192 
    193 /*
    194  * Ethernet output routine.
    195  * Encapsulate a packet of type family for the local net.
    196  * Assumes that ifp is actually pointer to ethercom structure.
    197  */
    198 static int
    199 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
    200     const struct sockaddr * const dst, const struct rtentry *rt)
    201 {
    202 	uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
    203 	uint16_t etype = 0;
    204 	int error = 0, hdrcmplt = 0;
    205 	struct mbuf *m = m0;
    206 	struct mbuf *mcopy = NULL;
    207 	struct ether_header *eh;
    208 	struct ifnet *ifp = ifp0;
    209 #ifdef INET
    210 	struct arphdr *ah;
    211 #endif
    212 #ifdef NETATALK
    213 	struct at_ifaddr *aa;
    214 #endif
    215 
    216 #ifdef MBUFTRACE
    217 	m_claimm(m, ifp->if_mowner);
    218 #endif
    219 
    220 #if NCARP > 0
    221 	if (ifp->if_type == IFT_CARP) {
    222 		struct ifaddr *ifa;
    223 		int s = pserialize_read_enter();
    224 
    225 		/* loop back if this is going to the carp interface */
    226 		if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
    227 		    (ifa = ifa_ifwithaddr(dst)) != NULL) {
    228 			if (ifa->ifa_ifp == ifp0) {
    229 				pserialize_read_exit(s);
    230 				return looutput(ifp0, m, dst, rt);
    231 			}
    232 		}
    233 		pserialize_read_exit(s);
    234 
    235 		ifp = ifp->if_carpdev;
    236 		/* ac = (struct arpcom *)ifp; */
    237 
    238 		if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
    239 		    (IFF_UP | IFF_RUNNING))
    240 			senderr(ENETDOWN);
    241 	}
    242 #endif
    243 
    244 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
    245 		senderr(ENETDOWN);
    246 
    247 	switch (dst->sa_family) {
    248 
    249 #ifdef INET
    250 	case AF_INET:
    251 		if (m->m_flags & M_BCAST) {
    252 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    253 		} else if (m->m_flags & M_MCAST) {
    254 			ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
    255 		} else {
    256 			error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst));
    257 			if (error)
    258 				return (error == EWOULDBLOCK) ? 0 : error;
    259 		}
    260 		/* If broadcasting on a simplex interface, loopback a copy */
    261 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    262 			mcopy = m_copypacket(m, M_DONTWAIT);
    263 		etype = htons(ETHERTYPE_IP);
    264 		break;
    265 
    266 	case AF_ARP:
    267 		ah = mtod(m, struct arphdr *);
    268 		if (m->m_flags & M_BCAST) {
    269 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    270 		} else {
    271 			void *tha = ar_tha(ah);
    272 
    273 			if (tha == NULL) {
    274 				/* fake with ARPHRD_IEEE1394 */
    275 				m_freem(m);
    276 				return 0;
    277 			}
    278 			memcpy(edst, tha, sizeof(edst));
    279 		}
    280 
    281 		ah->ar_hrd = htons(ARPHRD_ETHER);
    282 
    283 		switch (ntohs(ah->ar_op)) {
    284 		case ARPOP_REVREQUEST:
    285 		case ARPOP_REVREPLY:
    286 			etype = htons(ETHERTYPE_REVARP);
    287 			break;
    288 
    289 		case ARPOP_REQUEST:
    290 		case ARPOP_REPLY:
    291 		default:
    292 			etype = htons(ETHERTYPE_ARP);
    293 		}
    294 		break;
    295 #endif
    296 
    297 #ifdef INET6
    298 	case AF_INET6:
    299 		if (m->m_flags & M_BCAST) {
    300 			memcpy(edst, etherbroadcastaddr, sizeof(edst));
    301 		} else if (m->m_flags & M_MCAST) {
    302 			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
    303 			    edst);
    304 		} else {
    305 			error = nd6_resolve(ifp0, rt, m, dst, edst,
    306 			    sizeof(edst));
    307 			if (error)
    308 				return (error == EWOULDBLOCK) ? 0 : error;
    309 		}
    310 		etype = htons(ETHERTYPE_IPV6);
    311 		break;
    312 #endif
    313 
    314 #ifdef NETATALK
    315 	case AF_APPLETALK: {
    316 		struct ifaddr *ifa;
    317 		int s;
    318 
    319 		KERNEL_LOCK(1, NULL);
    320 
    321 		if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
    322 			KERNEL_UNLOCK_ONE(NULL);
    323 			return 0;
    324 		}
    325 
    326 		/*
    327 		 * ifaddr is the first thing in at_ifaddr
    328 		 */
    329 		s = pserialize_read_enter();
    330 		ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
    331 		if (ifa == NULL) {
    332 			pserialize_read_exit(s);
    333 			KERNEL_UNLOCK_ONE(NULL);
    334 			senderr(EADDRNOTAVAIL);
    335 		}
    336 		aa = (struct at_ifaddr *)ifa;
    337 
    338 		/*
    339 		 * In the phase 2 case, we need to prepend an mbuf for the
    340 		 * llc header.
    341 		 */
    342 		if (aa->aa_flags & AFA_PHASE2) {
    343 			struct llc llc;
    344 
    345 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    346 			if (m == NULL) {
    347 				pserialize_read_exit(s);
    348 				KERNEL_UNLOCK_ONE(NULL);
    349 				senderr(ENOBUFS);
    350 			}
    351 
    352 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
    353 			llc.llc_control = LLC_UI;
    354 			memcpy(llc.llc_snap_org_code, at_org_code,
    355 			    sizeof(llc.llc_snap_org_code));
    356 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
    357 			memcpy(mtod(m, void *), &llc, sizeof(struct llc));
    358 		} else {
    359 			etype = htons(ETHERTYPE_ATALK);
    360 		}
    361 		pserialize_read_exit(s);
    362 		KERNEL_UNLOCK_ONE(NULL);
    363 		break;
    364 	}
    365 #endif /* NETATALK */
    366 
    367 	case pseudo_AF_HDRCMPLT:
    368 		hdrcmplt = 1;
    369 		memcpy(esrc,
    370 		    ((const struct ether_header *)dst->sa_data)->ether_shost,
    371 		    sizeof(esrc));
    372 		/* FALLTHROUGH */
    373 
    374 	case AF_UNSPEC:
    375 		memcpy(edst,
    376 		    ((const struct ether_header *)dst->sa_data)->ether_dhost,
    377 		    sizeof(edst));
    378 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
    379 		etype = ((const struct ether_header *)dst->sa_data)->ether_type;
    380 		break;
    381 
    382 	default:
    383 		printf("%s: can't handle af%d\n", ifp->if_xname,
    384 		    dst->sa_family);
    385 		senderr(EAFNOSUPPORT);
    386 	}
    387 
    388 #ifdef MPLS
    389 	{
    390 		struct m_tag *mtag;
    391 		mtag = m_tag_find(m, PACKET_TAG_MPLS);
    392 		if (mtag != NULL) {
    393 			/* Having the tag itself indicates it's MPLS */
    394 			etype = htons(ETHERTYPE_MPLS);
    395 			m_tag_delete(m, mtag);
    396 		}
    397 	}
    398 #endif
    399 
    400 	if (mcopy)
    401 		(void)looutput(ifp, mcopy, dst, rt);
    402 
    403 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    404 
    405 	/*
    406 	 * If no ether type is set, this must be a 802.2 formatted packet.
    407 	 */
    408 	if (etype == 0)
    409 		etype = htons(m->m_pkthdr.len);
    410 
    411 	/*
    412 	 * Add local net header. If no space in first mbuf, allocate another.
    413 	 */
    414 	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
    415 	if (m == NULL)
    416 		senderr(ENOBUFS);
    417 
    418 	eh = mtod(m, struct ether_header *);
    419 	/* Note: etype is already in network byte order. */
    420 	memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
    421 	memcpy(eh->ether_dhost, edst, sizeof(edst));
    422 	if (hdrcmplt) {
    423 		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
    424 	} else {
    425 	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
    426 		    sizeof(eh->ether_shost));
    427 	}
    428 
    429 #if NCARP > 0
    430 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
    431 	 	memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
    432 		    sizeof(eh->ether_shost));
    433 	}
    434 #endif
    435 
    436 	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
    437 		return error;
    438 	if (m == NULL)
    439 		return 0;
    440 
    441 #if NBRIDGE > 0
    442 	/*
    443 	 * Bridges require special output handling.
    444 	 */
    445 	if (ifp->if_bridge)
    446 		return bridge_output(ifp, m, NULL, NULL);
    447 #endif
    448 
    449 #if NCARP > 0
    450 	if (ifp != ifp0)
    451 		if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
    452 #endif
    453 
    454 #ifdef ALTQ
    455 	KERNEL_LOCK(1, NULL);
    456 	/*
    457 	 * If ALTQ is enabled on the parent interface, do
    458 	 * classification; the queueing discipline might not
    459 	 * require classification, but might require the
    460 	 * address family/header pointer in the pktattr.
    461 	 */
    462 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    463 		altq_etherclassify(&ifp->if_snd, m);
    464 	KERNEL_UNLOCK_ONE(NULL);
    465 #endif
    466 	return ifq_enqueue(ifp, m);
    467 
    468 bad:
    469 	if_statinc(ifp, if_oerrors);
    470 	if (m)
    471 		m_freem(m);
    472 	return error;
    473 }
    474 
    475 #ifdef ALTQ
    476 /*
    477  * This routine is a slight hack to allow a packet to be classified
    478  * if the Ethernet headers are present.  It will go away when ALTQ's
    479  * classification engine understands link headers.
    480  *
    481  * XXX: We may need to do m_pullups here. First to ensure struct ether_header
    482  * is indeed contiguous, then to read the LLC and so on.
    483  */
    484 void
    485 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
    486 {
    487 	struct ether_header *eh;
    488 	struct mbuf *mtop = m;
    489 	uint16_t ether_type;
    490 	int hlen, af, hdrsize;
    491 	void *hdr;
    492 
    493 	KASSERT((mtop->m_flags & M_PKTHDR) != 0);
    494 
    495 	hlen = ETHER_HDR_LEN;
    496 	eh = mtod(m, struct ether_header *);
    497 
    498 	ether_type = htons(eh->ether_type);
    499 
    500 	if (ether_type < ETHERMTU) {
    501 		/* LLC/SNAP */
    502 		struct llc *llc = (struct llc *)(eh + 1);
    503 		hlen += 8;
    504 
    505 		if (m->m_len < hlen ||
    506 		    llc->llc_dsap != LLC_SNAP_LSAP ||
    507 		    llc->llc_ssap != LLC_SNAP_LSAP ||
    508 		    llc->llc_control != LLC_UI) {
    509 			/* Not SNAP. */
    510 			goto bad;
    511 		}
    512 
    513 		ether_type = htons(llc->llc_un.type_snap.ether_type);
    514 	}
    515 
    516 	switch (ether_type) {
    517 	case ETHERTYPE_IP:
    518 		af = AF_INET;
    519 		hdrsize = 20;		/* sizeof(struct ip) */
    520 		break;
    521 
    522 	case ETHERTYPE_IPV6:
    523 		af = AF_INET6;
    524 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
    525 		break;
    526 
    527 	default:
    528 		af = AF_UNSPEC;
    529 		hdrsize = 0;
    530 		break;
    531 	}
    532 
    533 	while (m->m_len <= hlen) {
    534 		hlen -= m->m_len;
    535 		m = m->m_next;
    536 		if (m == NULL)
    537 			goto bad;
    538 	}
    539 
    540 	if (m->m_len < (hlen + hdrsize)) {
    541 		/*
    542 		 * protocol header not in a single mbuf.
    543 		 * We can't cope with this situation right
    544 		 * now (but it shouldn't ever happen, really, anyhow).
    545 		 */
    546 #ifdef DEBUG
    547 		printf("altq_etherclassify: headers span multiple mbufs: "
    548 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
    549 #endif
    550 		goto bad;
    551 	}
    552 
    553 	m->m_data += hlen;
    554 	m->m_len -= hlen;
    555 
    556 	hdr = mtod(m, void *);
    557 
    558 	if (ALTQ_NEEDS_CLASSIFY(ifq)) {
    559 		mtop->m_pkthdr.pattr_class =
    560 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
    561 	}
    562 	mtop->m_pkthdr.pattr_af = af;
    563 	mtop->m_pkthdr.pattr_hdr = hdr;
    564 
    565 	m->m_data -= hlen;
    566 	m->m_len += hlen;
    567 
    568 	return;
    569 
    570 bad:
    571 	mtop->m_pkthdr.pattr_class = NULL;
    572 	mtop->m_pkthdr.pattr_hdr = NULL;
    573 	mtop->m_pkthdr.pattr_af = AF_UNSPEC;
    574 }
    575 #endif /* ALTQ */
    576 
    577 #if defined (LLC) || defined (NETATALK)
    578 static void
    579 ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh)
    580 {
    581 	struct ifqueue *inq = NULL;
    582 	int isr = 0;
    583 	struct llc *l;
    584 
    585 	if (m->m_len < sizeof(*eh) + sizeof(struct llc))
    586 		goto drop;
    587 
    588 	l = (struct llc *)(eh+1);
    589 	switch (l->llc_dsap) {
    590 #ifdef NETATALK
    591 	case LLC_SNAP_LSAP:
    592 		switch (l->llc_control) {
    593 		case LLC_UI:
    594 			if (l->llc_ssap != LLC_SNAP_LSAP)
    595 				goto drop;
    596 
    597 			if (memcmp(&(l->llc_snap_org_code)[0],
    598 			    at_org_code, sizeof(at_org_code)) == 0 &&
    599 			    ntohs(l->llc_snap_ether_type) ==
    600 			    ETHERTYPE_ATALK) {
    601 				inq = &atintrq2;
    602 				m_adj(m, sizeof(struct ether_header)
    603 				    + sizeof(struct llc));
    604 				isr = NETISR_ATALK;
    605 				break;
    606 			}
    607 
    608 			if (memcmp(&(l->llc_snap_org_code)[0],
    609 			    aarp_org_code,
    610 			    sizeof(aarp_org_code)) == 0 &&
    611 			    ntohs(l->llc_snap_ether_type) ==
    612 			    ETHERTYPE_AARP) {
    613 				m_adj(m, sizeof(struct ether_header)
    614 				    + sizeof(struct llc));
    615 				aarpinput(ifp, m); /* XXX queue? */
    616 				return;
    617 			}
    618 
    619 		default:
    620 			goto drop;
    621 		}
    622 		break;
    623 #endif
    624 	default:
    625 		goto drop;
    626 	}
    627 
    628 	KASSERT(inq != NULL);
    629 	IFQ_ENQUEUE_ISR(inq, m, isr);
    630 	return;
    631 
    632 drop:
    633 	m_freem(m);
    634 	if_statinc(ifp, if_ierrors); /* XXX should have a dedicated counter? */
    635 	return;
    636 }
    637 #endif /* defined (LLC) || defined (NETATALK) */
    638 
    639 /*
    640  * Process a received Ethernet packet;
    641  * the packet is in the mbuf chain m with
    642  * the ether header.
    643  */
    644 void
    645 ether_input(struct ifnet *ifp, struct mbuf *m)
    646 {
    647 	struct ethercom *ec = (struct ethercom *) ifp;
    648 	pktqueue_t *pktq = NULL;
    649 	struct ifqueue *inq = NULL;
    650 	uint16_t etype;
    651 	struct ether_header *eh;
    652 	size_t ehlen;
    653 	static int earlypkts;
    654 	int isr = 0;
    655 #if NAGR > 0
    656 	void *agrprivate;
    657 #endif
    658 
    659 	KASSERT(!cpu_intr_p());
    660 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    661 
    662 	if ((ifp->if_flags & IFF_UP) == 0)
    663 		goto drop;
    664 
    665 #ifdef MBUFTRACE
    666 	m_claimm(m, &ec->ec_rx_mowner);
    667 #endif
    668 
    669 	if (__predict_false(m->m_len < sizeof(*eh))) {
    670 		if ((m = m_pullup(m, sizeof(*eh))) == NULL)
    671 			goto dropped;
    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 drop;
    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 NCARP > 0
    730 	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
    731 		/*
    732 		 * Clear M_PROMISC, in case the packet comes from a
    733 		 * vlan.
    734 		 */
    735 		m->m_flags &= ~M_PROMISC;
    736 		if (carp_input(m, (uint8_t *)&eh->ether_shost,
    737 		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
    738 			return;
    739 	}
    740 #endif
    741 
    742 	if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
    743 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
    744 	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
    745 	     ETHER_ADDR_LEN) != 0) {
    746 		m->m_flags |= M_PROMISC;
    747 	}
    748 
    749 	if ((m->m_flags & M_PROMISC) == 0) {
    750 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    751 			return;
    752 		if (m == NULL)
    753 			return;
    754 
    755 		eh = mtod(m, struct ether_header *);
    756 		etype = ntohs(eh->ether_type);
    757 	}
    758 
    759 #if NAGR > 0
    760 	if (ifp->if_type != IFT_IEEE8023ADLAG) {
    761 		agrprivate = ifp->if_lagg;
    762 	} else {
    763 		agrprivate = NULL;
    764 	}
    765 	if (agrprivate != NULL &&
    766 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
    767 		m->m_flags &= ~M_PROMISC;
    768 		agr_input(ifp, m);
    769 		return;
    770 	}
    771 #endif
    772 
    773 	/* Handle input from a lagg(4) port */
    774 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
    775 		KASSERT(lagg_input_ethernet_p != NULL);
    776 		m = (*lagg_input_ethernet_p)(ifp, m);
    777 		if (m == NULL)
    778 			return;
    779 	}
    780 
    781 	/*
    782 	 * If VLANs are configured on the interface, check to
    783 	 * see if the device performed the decapsulation and
    784 	 * provided us with the tag.
    785 	 */
    786 	if (ec->ec_nvlans && vlan_has_tag(m)) {
    787 #if NVLAN > 0
    788 		/*
    789 		 * vlan_input() will either recursively call ether_input()
    790 		 * or drop the packet.
    791 		 */
    792 		vlan_input(ifp, m);
    793 		return;
    794 #else
    795 		goto drop;
    796 #endif
    797 	}
    798 
    799 	/*
    800 	 * Handle protocols that expect to have the Ethernet header
    801 	 * (and possibly FCS) intact.
    802 	 */
    803 	switch (etype) {
    804 	case ETHERTYPE_VLAN: {
    805 		struct ether_vlan_header *evl = (void *)eh;
    806 
    807 		/*
    808 		 * If there is a tag of 0, then the VLAN header was probably
    809 		 * just being used to store the priority.  Extract the ether
    810 		 * type, and if IP or IPV6, let them deal with it.
    811 		 */
    812 		if (m->m_len >= sizeof(*evl) &&
    813 		    EVL_VLANOFTAG(evl->evl_tag) == 0) {
    814 			etype = ntohs(evl->evl_proto);
    815 			ehlen = sizeof(*evl);
    816 			if ((m->m_flags & M_PROMISC) == 0 &&
    817 			    (etype == ETHERTYPE_IP ||
    818 			     etype == ETHERTYPE_IPV6))
    819 				break;
    820 		}
    821 
    822 #if NVLAN > 0
    823 		/*
    824 		 * vlan_input() will either recursively call ether_input()
    825 		 * or drop the packet.
    826 		 */
    827 		if (ec->ec_nvlans != 0) {
    828 			vlan_input(ifp, m);
    829 			return;
    830 		} else
    831 #endif
    832 			goto drop;
    833 	}
    834 
    835 #if NPPPOE > 0
    836 	case ETHERTYPE_PPPOEDISC:
    837 		pppoedisc_input(ifp, m);
    838 		return;
    839 
    840 	case ETHERTYPE_PPPOE:
    841 		pppoe_input(ifp, m);
    842 		return;
    843 #endif
    844 
    845 	case ETHERTYPE_SLOWPROTOCOLS: {
    846 		uint8_t subtype;
    847 
    848 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
    849 			goto drop;
    850 
    851 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
    852 		switch (subtype) {
    853 #if NAGR > 0
    854 		case SLOWPROTOCOLS_SUBTYPE_LACP:
    855 			if (agrprivate != NULL) {
    856 				ieee8023ad_lacp_input(ifp, m);
    857 				return;
    858 			}
    859 			break;
    860 
    861 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
    862 			if (agrprivate != NULL) {
    863 				ieee8023ad_marker_input(ifp, m);
    864 				return;
    865 			}
    866 			break;
    867 #endif
    868 
    869 		default:
    870 			if (subtype == 0 || subtype > 10) {
    871 				/* illegal value */
    872 				goto drop;
    873 			}
    874 			/* unknown subtype */
    875 			break;
    876 		}
    877 	}
    878 	/* FALLTHROUGH */
    879 	default:
    880 		if (m->m_flags & M_PROMISC)
    881 			goto drop;
    882 	}
    883 
    884 	/* If the CRC is still on the packet, trim it off. */
    885 	if (m->m_flags & M_HASFCS) {
    886 		m_adj(m, -ETHER_CRC_LEN);
    887 		m->m_flags &= ~M_HASFCS;
    888 	}
    889 
    890 	/* etype represents the size of the payload in this case */
    891 	if (etype <= ETHERMTU + sizeof(struct ether_header)) {
    892 		KASSERT(ehlen == sizeof(*eh));
    893 #if defined (LLC) || defined (NETATALK)
    894 		ether_input_llc(ifp, m, eh);
    895 		return;
    896 #else
    897 		goto drop;
    898 #endif
    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 		break;
    913 
    914 	case ETHERTYPE_ARP:
    915 		isr = NETISR_ARP;
    916 		inq = &arpintrq;
    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 drop;
    928 #ifdef GATEWAY
    929 		if (ip6flow_fastforward(&m))
    930 			return;
    931 #endif
    932 		pktq = ip6_pktq;
    933 		break;
    934 #endif
    935 
    936 #ifdef NETATALK
    937 	case ETHERTYPE_ATALK:
    938 		isr = NETISR_ATALK;
    939 		inq = &atintrq1;
    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 		isr = NETISR_MPLS;
    950 		inq = &mplsintrq;
    951 		break;
    952 #endif
    953 
    954 	default:
    955 		goto drop;
    956 	}
    957 
    958 	if (__predict_true(pktq)) {
    959 #ifdef NET_MPSAFE
    960 		const u_int h = curcpu()->ci_index;
    961 #else
    962 		const uint32_t h = pktq_rps_hash(m);
    963 #endif
    964 		if (__predict_false(!pktq_enqueue(pktq, m, h))) {
    965 			m_freem(m);
    966 		}
    967 		return;
    968 	}
    969 
    970 	if (__predict_false(!inq)) {
    971 		/* Should not happen. */
    972 		goto drop;
    973 	}
    974 
    975 	IFQ_ENQUEUE_ISR(inq, m, isr);
    976 	return;
    977 
    978 drop:
    979 	m_freem(m);
    980 dropped:
    981 	if_statinc(ifp, if_ierrors); /* XXX should have a dedicated counter? */
    982 }
    983 
    984 /*
    985  * Convert Ethernet address to printable (loggable) representation.
    986  */
    987 char *
    988 ether_sprintf(const u_char *ap)
    989 {
    990 	static char etherbuf[3 * ETHER_ADDR_LEN];
    991 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
    992 }
    993 
    994 char *
    995 ether_snprintf(char *buf, size_t len, const u_char *ap)
    996 {
    997 	char *cp = buf;
    998 	size_t i;
    999 
   1000 	for (i = 0; i < len / 3; i++) {
   1001 		*cp++ = hexdigits[*ap >> 4];
   1002 		*cp++ = hexdigits[*ap++ & 0xf];
   1003 		*cp++ = ':';
   1004 	}
   1005 	*--cp = '\0';
   1006 	return buf;
   1007 }
   1008 
   1009 /*
   1010  * Perform common duties while attaching to interface list
   1011  */
   1012 void
   1013 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
   1014 {
   1015 	struct ethercom *ec = (struct ethercom *)ifp;
   1016 	char xnamebuf[HOOKNAMSIZ];
   1017 
   1018 	ifp->if_type = IFT_ETHER;
   1019 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1020 	ifp->if_dlt = DLT_EN10MB;
   1021 	ifp->if_mtu = ETHERMTU;
   1022 	ifp->if_output = ether_output;
   1023 	ifp->_if_input = ether_input;
   1024 	if (ifp->if_baudrate == 0)
   1025 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1026 
   1027 	if (lla != NULL)
   1028 		if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
   1029 
   1030 	LIST_INIT(&ec->ec_multiaddrs);
   1031 	SIMPLEQ_INIT(&ec->ec_vids);
   1032 	ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   1033 	ec->ec_flags = 0;
   1034 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1035 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1036 	snprintf(xnamebuf, sizeof(xnamebuf),
   1037 	    "%s-ether_ifdetachhooks", ifp->if_xname);
   1038 	ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
   1039 #ifdef MBUFTRACE
   1040 	mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
   1041 	mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
   1042 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1043 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1044 	ifp->if_mowner = &ec->ec_tx_mowner;
   1045 #endif
   1046 }
   1047 
   1048 void
   1049 ether_ifdetach(struct ifnet *ifp)
   1050 {
   1051 	struct ethercom *ec = (void *) ifp;
   1052 	struct ether_multi *enm;
   1053 
   1054 	IFNET_ASSERT_UNLOCKED(ifp);
   1055 	/*
   1056 	 * Prevent further calls to ioctl (for example turning off
   1057 	 * promiscuous mode from the bridge code), which eventually can
   1058 	 * call if_init() which can cause panics because the interface
   1059 	 * is in the process of being detached. Return device not configured
   1060 	 * instead.
   1061 	 */
   1062 	ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
   1063 	    enxio);
   1064 
   1065 	simplehook_dohooks(ec->ec_ifdetach_hooks);
   1066 	KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
   1067 	simplehook_destroy(ec->ec_ifdetach_hooks);
   1068 
   1069 #if NBRIDGE > 0
   1070 	if (ifp->if_bridge)
   1071 		bridge_ifdetach(ifp);
   1072 #endif
   1073 	bpf_detach(ifp);
   1074 #if NVLAN > 0
   1075 	if (ec->ec_nvlans)
   1076 		vlan_ifdetach(ifp);
   1077 #endif
   1078 
   1079 #if NLAGG > 0
   1080 	if (ifp->if_lagg)
   1081 		lagg_ifdetach(ifp);
   1082 #endif
   1083 
   1084 	ETHER_LOCK(ec);
   1085 	KASSERT(ec->ec_nvlans == 0);
   1086 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1087 		LIST_REMOVE(enm, enm_list);
   1088 		kmem_free(enm, sizeof(*enm));
   1089 		ec->ec_multicnt--;
   1090 	}
   1091 	ETHER_UNLOCK(ec);
   1092 
   1093 	mutex_obj_free(ec->ec_lock);
   1094 	ec->ec_lock = NULL;
   1095 
   1096 	ifp->if_mowner = NULL;
   1097 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1098 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1099 }
   1100 
   1101 void *
   1102 ether_ifdetachhook_establish(struct ifnet *ifp,
   1103     void (*fn)(void *), void *arg)
   1104 {
   1105 	struct ethercom *ec;
   1106 	khook_t *hk;
   1107 
   1108 	if (ifp->if_type != IFT_ETHER)
   1109 		return NULL;
   1110 
   1111 	ec = (struct ethercom *)ifp;
   1112 	hk = simplehook_establish(ec->ec_ifdetach_hooks,
   1113 	    fn, arg);
   1114 
   1115 	return (void *)hk;
   1116 }
   1117 
   1118 void
   1119 ether_ifdetachhook_disestablish(struct ifnet *ifp,
   1120     void *vhook, kmutex_t *lock)
   1121 {
   1122 	struct ethercom *ec;
   1123 
   1124 	if (vhook == NULL)
   1125 		return;
   1126 
   1127 	ec = (struct ethercom *)ifp;
   1128 	simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
   1129 }
   1130 
   1131 #if 0
   1132 /*
   1133  * This is for reference.  We have a table-driven version
   1134  * of the little-endian crc32 generator, which is faster
   1135  * than the double-loop.
   1136  */
   1137 uint32_t
   1138 ether_crc32_le(const uint8_t *buf, size_t len)
   1139 {
   1140 	uint32_t c, crc, carry;
   1141 	size_t i, j;
   1142 
   1143 	crc = 0xffffffffU;	/* initial value */
   1144 
   1145 	for (i = 0; i < len; i++) {
   1146 		c = buf[i];
   1147 		for (j = 0; j < 8; j++) {
   1148 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1149 			crc >>= 1;
   1150 			c >>= 1;
   1151 			if (carry)
   1152 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1153 		}
   1154 	}
   1155 
   1156 	return (crc);
   1157 }
   1158 #else
   1159 uint32_t
   1160 ether_crc32_le(const uint8_t *buf, size_t len)
   1161 {
   1162 	static const uint32_t crctab[] = {
   1163 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1164 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1165 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1166 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1167 	};
   1168 	uint32_t crc;
   1169 	size_t i;
   1170 
   1171 	crc = 0xffffffffU;	/* initial value */
   1172 
   1173 	for (i = 0; i < len; i++) {
   1174 		crc ^= buf[i];
   1175 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1176 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1177 	}
   1178 
   1179 	return (crc);
   1180 }
   1181 #endif
   1182 
   1183 uint32_t
   1184 ether_crc32_be(const uint8_t *buf, size_t len)
   1185 {
   1186 	uint32_t c, crc, carry;
   1187 	size_t i, j;
   1188 
   1189 	crc = 0xffffffffU;	/* initial value */
   1190 
   1191 	for (i = 0; i < len; i++) {
   1192 		c = buf[i];
   1193 		for (j = 0; j < 8; j++) {
   1194 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1195 			crc <<= 1;
   1196 			c >>= 1;
   1197 			if (carry)
   1198 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1199 		}
   1200 	}
   1201 
   1202 	return (crc);
   1203 }
   1204 
   1205 #ifdef INET
   1206 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1207     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1208 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1209     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1210 #endif
   1211 #ifdef INET6
   1212 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1213     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1214 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1215     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1216 #endif
   1217 
   1218 /*
   1219  * ether_aton implementation, not using a static buffer.
   1220  */
   1221 int
   1222 ether_aton_r(u_char *dest, size_t len, const char *str)
   1223 {
   1224 	const u_char *cp = (const void *)str;
   1225 	u_char *ep;
   1226 
   1227 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
   1228 
   1229 	if (len < ETHER_ADDR_LEN)
   1230 		return ENOSPC;
   1231 
   1232 	ep = dest + ETHER_ADDR_LEN;
   1233 
   1234 	while (*cp) {
   1235 		if (!isxdigit(*cp))
   1236 			return EINVAL;
   1237 
   1238 		*dest = atox(*cp);
   1239 		cp++;
   1240 		if (isxdigit(*cp)) {
   1241 			*dest = (*dest << 4) | atox(*cp);
   1242 			cp++;
   1243 		}
   1244 		dest++;
   1245 
   1246 		if (dest == ep)
   1247 			return (*cp == '\0') ? 0 : ENAMETOOLONG;
   1248 
   1249 		switch (*cp) {
   1250 		case ':':
   1251 		case '-':
   1252 		case '.':
   1253 			cp++;
   1254 			break;
   1255 		}
   1256 	}
   1257 	return ENOBUFS;
   1258 }
   1259 
   1260 /*
   1261  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1262  * addresses.
   1263  */
   1264 int
   1265 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
   1266     uint8_t addrhi[ETHER_ADDR_LEN])
   1267 {
   1268 #ifdef INET
   1269 	const struct sockaddr_in *sin;
   1270 #endif
   1271 #ifdef INET6
   1272 	const struct sockaddr_in6 *sin6;
   1273 #endif
   1274 
   1275 	switch (sa->sa_family) {
   1276 
   1277 	case AF_UNSPEC:
   1278 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
   1279 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1280 		break;
   1281 
   1282 #ifdef INET
   1283 	case AF_INET:
   1284 		sin = satocsin(sa);
   1285 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1286 			/*
   1287 			 * An IP address of INADDR_ANY means listen to
   1288 			 * or stop listening to all of the Ethernet
   1289 			 * multicast addresses used for IP.
   1290 			 * (This is for the sake of IP multicast routers.)
   1291 			 */
   1292 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
   1293 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
   1294 		} else {
   1295 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1296 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1297 		}
   1298 		break;
   1299 #endif
   1300 #ifdef INET6
   1301 	case AF_INET6:
   1302 		sin6 = satocsin6(sa);
   1303 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1304 			/*
   1305 			 * An IP6 address of 0 means listen to or stop
   1306 			 * listening to all of the Ethernet multicast
   1307 			 * address used for IP6.
   1308 			 * (This is used for multicast routers.)
   1309 			 */
   1310 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
   1311 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
   1312 		} else {
   1313 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1314 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1315 		}
   1316 		break;
   1317 #endif
   1318 
   1319 	default:
   1320 		return EAFNOSUPPORT;
   1321 	}
   1322 	return 0;
   1323 }
   1324 
   1325 /*
   1326  * Add an Ethernet multicast address or range of addresses to the list for a
   1327  * given interface.
   1328  */
   1329 int
   1330 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
   1331 {
   1332 	struct ether_multi *enm, *_enm;
   1333 	u_char addrlo[ETHER_ADDR_LEN];
   1334 	u_char addrhi[ETHER_ADDR_LEN];
   1335 	int error = 0;
   1336 
   1337 	/* Allocate out of lock */
   1338 	enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
   1339 
   1340 	ETHER_LOCK(ec);
   1341 	error = ether_multiaddr(sa, addrlo, addrhi);
   1342 	if (error != 0)
   1343 		goto out;
   1344 
   1345 	/*
   1346 	 * Verify that we have valid Ethernet multicast addresses.
   1347 	 */
   1348 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
   1349 		error = EINVAL;
   1350 		goto out;
   1351 	}
   1352 
   1353 	/*
   1354 	 * See if the address range is already in the list.
   1355 	 */
   1356 	_enm = ether_lookup_multi(addrlo, addrhi, ec);
   1357 	if (_enm != NULL) {
   1358 		/*
   1359 		 * Found it; just increment the reference count.
   1360 		 */
   1361 		++_enm->enm_refcount;
   1362 		error = 0;
   1363 		goto out;
   1364 	}
   1365 
   1366 	/*
   1367 	 * Link a new multicast record into the interface's multicast list.
   1368 	 */
   1369 	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
   1370 	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
   1371 	enm->enm_refcount = 1;
   1372 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1373 	ec->ec_multicnt++;
   1374 
   1375 	/*
   1376 	 * Return ENETRESET to inform the driver that the list has changed
   1377 	 * and its reception filter should be adjusted accordingly.
   1378 	 */
   1379 	error = ENETRESET;
   1380 	enm = NULL;
   1381 
   1382 out:
   1383 	ETHER_UNLOCK(ec);
   1384 	if (enm != NULL)
   1385 		kmem_free(enm, sizeof(*enm));
   1386 	return error;
   1387 }
   1388 
   1389 /*
   1390  * Delete a multicast address record.
   1391  */
   1392 int
   1393 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
   1394 {
   1395 	struct ether_multi *enm;
   1396 	u_char addrlo[ETHER_ADDR_LEN];
   1397 	u_char addrhi[ETHER_ADDR_LEN];
   1398 	int error;
   1399 
   1400 	ETHER_LOCK(ec);
   1401 	error = ether_multiaddr(sa, addrlo, addrhi);
   1402 	if (error != 0)
   1403 		goto error;
   1404 
   1405 	/*
   1406 	 * Look up the address in our list.
   1407 	 */
   1408 	enm = ether_lookup_multi(addrlo, addrhi, ec);
   1409 	if (enm == NULL) {
   1410 		error = ENXIO;
   1411 		goto error;
   1412 	}
   1413 	if (--enm->enm_refcount != 0) {
   1414 		/*
   1415 		 * Still some claims to this record.
   1416 		 */
   1417 		error = 0;
   1418 		goto error;
   1419 	}
   1420 
   1421 	/*
   1422 	 * No remaining claims to this record; unlink and free it.
   1423 	 */
   1424 	LIST_REMOVE(enm, enm_list);
   1425 	ec->ec_multicnt--;
   1426 	ETHER_UNLOCK(ec);
   1427 	kmem_free(enm, sizeof(*enm));
   1428 
   1429 	/*
   1430 	 * Return ENETRESET to inform the driver that the list has changed
   1431 	 * and its reception filter should be adjusted accordingly.
   1432 	 */
   1433 	return ENETRESET;
   1434 
   1435 error:
   1436 	ETHER_UNLOCK(ec);
   1437 	return error;
   1438 }
   1439 
   1440 void
   1441 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
   1442 {
   1443 	ec->ec_ifflags_cb = cb;
   1444 }
   1445 
   1446 void
   1447 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
   1448 {
   1449 
   1450 	ec->ec_vlan_cb = cb;
   1451 }
   1452 
   1453 static int
   1454 ether_ioctl_reinit(struct ethercom *ec)
   1455 {
   1456 	struct ifnet *ifp = &ec->ec_if;
   1457 	int error;
   1458 
   1459 	switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1460 	case IFF_RUNNING:
   1461 		/*
   1462 		 * If interface is marked down and it is running,
   1463 		 * then stop and disable it.
   1464 		 */
   1465 		(*ifp->if_stop)(ifp, 1);
   1466 		break;
   1467 	case IFF_UP:
   1468 		/*
   1469 		 * If interface is marked up and it is stopped, then
   1470 		 * start it.
   1471 		 */
   1472 		return (*ifp->if_init)(ifp);
   1473 	case IFF_UP | IFF_RUNNING:
   1474 		error = 0;
   1475 		if (ec->ec_ifflags_cb != NULL) {
   1476 			error = (*ec->ec_ifflags_cb)(ec);
   1477 			if (error == ENETRESET) {
   1478 				/*
   1479 				 * Reset the interface to pick up
   1480 				 * changes in any other flags that
   1481 				 * affect the hardware state.
   1482 				 */
   1483 				return (*ifp->if_init)(ifp);
   1484 			}
   1485 		} else
   1486 			error = (*ifp->if_init)(ifp);
   1487 		return error;
   1488 	case 0:
   1489 		break;
   1490 	}
   1491 
   1492 	return 0;
   1493 }
   1494 
   1495 /*
   1496  * Common ioctls for Ethernet interfaces.  Note, we must be
   1497  * called at splnet().
   1498  */
   1499 int
   1500 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1501 {
   1502 	struct ethercom *ec = (void *)ifp;
   1503 	struct eccapreq *eccr;
   1504 	struct ifreq *ifr = (struct ifreq *)data;
   1505 	struct if_laddrreq *iflr = data;
   1506 	const struct sockaddr_dl *sdl;
   1507 	static const uint8_t zero[ETHER_ADDR_LEN];
   1508 	int error;
   1509 
   1510 	switch (cmd) {
   1511 	case SIOCINITIFADDR:
   1512 	    {
   1513 		struct ifaddr *ifa = (struct ifaddr *)data;
   1514 		if (ifa->ifa_addr->sa_family != AF_LINK
   1515 		    && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
   1516 		       (IFF_UP | IFF_RUNNING)) {
   1517 			ifp->if_flags |= IFF_UP;
   1518 			if ((error = (*ifp->if_init)(ifp)) != 0)
   1519 				return error;
   1520 		}
   1521 #ifdef INET
   1522 		if (ifa->ifa_addr->sa_family == AF_INET)
   1523 			arp_ifinit(ifp, ifa);
   1524 #endif
   1525 		return 0;
   1526 	    }
   1527 
   1528 	case SIOCSIFMTU:
   1529 	    {
   1530 		int maxmtu;
   1531 
   1532 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1533 			maxmtu = ETHERMTU_JUMBO;
   1534 		else
   1535 			maxmtu = ETHERMTU;
   1536 
   1537 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1538 			return EINVAL;
   1539 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
   1540 			return error;
   1541 		else if (ifp->if_flags & IFF_UP) {
   1542 			/* Make sure the device notices the MTU change. */
   1543 			return (*ifp->if_init)(ifp);
   1544 		} else
   1545 			return 0;
   1546 	    }
   1547 
   1548 	case SIOCSIFFLAGS:
   1549 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1550 			return error;
   1551 		return ether_ioctl_reinit(ec);
   1552 	case SIOCGIFFLAGS:
   1553 		error = ifioctl_common(ifp, cmd, data);
   1554 		if (error == 0) {
   1555 			/* Set IFF_ALLMULTI for backcompat */
   1556 			ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
   1557 			    IFF_ALLMULTI : 0;
   1558 		}
   1559 		return error;
   1560 	case SIOCGETHERCAP:
   1561 		eccr = (struct eccapreq *)data;
   1562 		eccr->eccr_capabilities = ec->ec_capabilities;
   1563 		eccr->eccr_capenable = ec->ec_capenable;
   1564 		return 0;
   1565 	case SIOCSETHERCAP:
   1566 		eccr = (struct eccapreq *)data;
   1567 		if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
   1568 			return EINVAL;
   1569 		if (eccr->eccr_capenable == ec->ec_capenable)
   1570 			return 0;
   1571 #if 0 /* notyet */
   1572 		ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
   1573 		    | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
   1574 #else
   1575 		ec->ec_capenable = eccr->eccr_capenable;
   1576 #endif
   1577 		return ether_ioctl_reinit(ec);
   1578 	case SIOCADDMULTI:
   1579 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
   1580 	case SIOCDELMULTI:
   1581 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
   1582 	case SIOCSIFMEDIA:
   1583 	case SIOCGIFMEDIA:
   1584 		if (ec->ec_mii != NULL)
   1585 			return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
   1586 			    cmd);
   1587 		else if (ec->ec_ifmedia != NULL)
   1588 			return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
   1589 		else
   1590 			return ENOTTY;
   1591 		break;
   1592 	case SIOCALIFADDR:
   1593 		sdl = satocsdl(sstocsa(&iflr->addr));
   1594 		if (sdl->sdl_family != AF_LINK)
   1595 			;
   1596 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
   1597 			return EINVAL;
   1598 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
   1599 			return EINVAL;
   1600 		/*FALLTHROUGH*/
   1601 	default:
   1602 		return ifioctl_common(ifp, cmd, data);
   1603 	}
   1604 	return 0;
   1605 }
   1606 
   1607 /*
   1608  * Enable/disable passing VLAN packets if the parent interface supports it.
   1609  * Return:
   1610  * 	 0: Ok
   1611  *	-1: Parent interface does not support vlans
   1612  *	>0: Error
   1613  */
   1614 int
   1615 ether_enable_vlan_mtu(struct ifnet *ifp)
   1616 {
   1617 	int error;
   1618 	struct ethercom *ec = (void *)ifp;
   1619 
   1620 	/* Parent does not support VLAN's */
   1621 	if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
   1622 		return -1;
   1623 
   1624 	/*
   1625 	 * Parent supports the VLAN_MTU capability,
   1626 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
   1627 	 * enable it.
   1628 	 */
   1629 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1630 
   1631 	/* Interface is down, defer for later */
   1632 	if ((ifp->if_flags & IFF_UP) == 0)
   1633 		return 0;
   1634 
   1635 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1636 		return 0;
   1637 
   1638 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1639 	return error;
   1640 }
   1641 
   1642 int
   1643 ether_disable_vlan_mtu(struct ifnet *ifp)
   1644 {
   1645 	int error;
   1646 	struct ethercom *ec = (void *)ifp;
   1647 
   1648 	/* We still have VLAN's, defer for later */
   1649 	if (ec->ec_nvlans != 0)
   1650 		return 0;
   1651 
   1652 	/* Parent does not support VLAB's, nothing to do. */
   1653 	if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
   1654 		return -1;
   1655 
   1656 	/*
   1657 	 * Disable Tx/Rx of VLAN-sized frames.
   1658 	 */
   1659 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1660 
   1661 	/* Interface is down, defer for later */
   1662 	if ((ifp->if_flags & IFF_UP) == 0)
   1663 		return 0;
   1664 
   1665 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1666 		return 0;
   1667 
   1668 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1669 	return error;
   1670 }
   1671 
   1672 static int
   1673 ether_multicast_sysctl(SYSCTLFN_ARGS)
   1674 {
   1675 	struct ether_multi *enm;
   1676 	struct ifnet *ifp;
   1677 	struct ethercom *ec;
   1678 	int error = 0;
   1679 	size_t written;
   1680 	struct psref psref;
   1681 	int bound;
   1682 	unsigned int multicnt;
   1683 	struct ether_multi_sysctl *addrs;
   1684 	int i;
   1685 
   1686 	if (namelen != 1)
   1687 		return EINVAL;
   1688 
   1689 	bound = curlwp_bind();
   1690 	ifp = if_get_byindex(name[0], &psref);
   1691 	if (ifp == NULL) {
   1692 		error = ENODEV;
   1693 		goto out;
   1694 	}
   1695 	if (ifp->if_type != IFT_ETHER) {
   1696 		if_put(ifp, &psref);
   1697 		*oldlenp = 0;
   1698 		goto out;
   1699 	}
   1700 	ec = (struct ethercom *)ifp;
   1701 
   1702 	if (oldp == NULL) {
   1703 		if_put(ifp, &psref);
   1704 		*oldlenp = ec->ec_multicnt * sizeof(*addrs);
   1705 		goto out;
   1706 	}
   1707 
   1708 	/*
   1709 	 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
   1710 	 * is sleepable, while holding it. Copy data to a local buffer first
   1711 	 * with the lock taken and then call sysctl_copyout without holding it.
   1712 	 */
   1713 retry:
   1714 	multicnt = ec->ec_multicnt;
   1715 
   1716 	if (multicnt == 0) {
   1717 		if_put(ifp, &psref);
   1718 		*oldlenp = 0;
   1719 		goto out;
   1720 	}
   1721 
   1722 	addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
   1723 
   1724 	ETHER_LOCK(ec);
   1725 	if (multicnt != ec->ec_multicnt) {
   1726 		/* The number of multicast addresses has changed */
   1727 		ETHER_UNLOCK(ec);
   1728 		kmem_free(addrs, sizeof(*addrs) * multicnt);
   1729 		goto retry;
   1730 	}
   1731 
   1732 	i = 0;
   1733 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
   1734 		struct ether_multi_sysctl *addr = &addrs[i];
   1735 		addr->enm_refcount = enm->enm_refcount;
   1736 		memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
   1737 		memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
   1738 		i++;
   1739 	}
   1740 	ETHER_UNLOCK(ec);
   1741 
   1742 	error = 0;
   1743 	written = 0;
   1744 	for (i = 0; i < multicnt; i++) {
   1745 		struct ether_multi_sysctl *addr = &addrs[i];
   1746 
   1747 		if (written + sizeof(*addr) > *oldlenp)
   1748 			break;
   1749 		error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
   1750 		if (error)
   1751 			break;
   1752 		written += sizeof(*addr);
   1753 		oldp = (char *)oldp + sizeof(*addr);
   1754 	}
   1755 	kmem_free(addrs, sizeof(*addrs) * multicnt);
   1756 
   1757 	if_put(ifp, &psref);
   1758 
   1759 	*oldlenp = written;
   1760 out:
   1761 	curlwp_bindx(bound);
   1762 	return error;
   1763 }
   1764 
   1765 static void
   1766 ether_sysctl_setup(struct sysctllog **clog)
   1767 {
   1768 	const struct sysctlnode *rnode = NULL;
   1769 
   1770 	sysctl_createv(clog, 0, NULL, &rnode,
   1771 		       CTLFLAG_PERMANENT,
   1772 		       CTLTYPE_NODE, "ether",
   1773 		       SYSCTL_DESCR("Ethernet-specific information"),
   1774 		       NULL, 0, NULL, 0,
   1775 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1776 
   1777 	sysctl_createv(clog, 0, &rnode, NULL,
   1778 		       CTLFLAG_PERMANENT,
   1779 		       CTLTYPE_NODE, "multicast",
   1780 		       SYSCTL_DESCR("multicast addresses"),
   1781 		       ether_multicast_sysctl, 0, NULL, 0,
   1782 		       CTL_CREATE, CTL_EOL);
   1783 }
   1784 
   1785 void
   1786 etherinit(void)
   1787 {
   1788 
   1789 #ifdef DIAGNOSTIC
   1790 	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
   1791 #endif
   1792 	ether_sysctl_setup(NULL);
   1793 }
   1794