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