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