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