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