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