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