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