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