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