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