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