Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.304
      1 /*	$NetBSD: if_ethersubr.c,v 1.304 2021/11/15 07:07:05 yamaguchi Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.304 2021/11/15 07:07:05 yamaguchi Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_inet.h"
     68 #include "opt_atalk.h"
     69 #include "opt_mbuftrace.h"
     70 #include "opt_mpls.h"
     71 #include "opt_gateway.h"
     72 #include "opt_pppoe.h"
     73 #include "opt_net_mpsafe.h"
     74 #endif
     75 
     76 #include "vlan.h"
     77 #include "pppoe.h"
     78 #include "bridge.h"
     79 #include "arp.h"
     80 #include "agr.h"
     81 
     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/agr/ieee8023_slowprotocols.h>	/* XXX */
    124 #include <net/agr/ieee8023ad.h>
    125 #include <net/agr/if_agrvar.h>
    126 #endif
    127 
    128 #include <net/lagg/if_laggvar.h>
    129 
    130 #if NBRIDGE > 0
    131 #include <net/if_bridgevar.h>
    132 #endif
    133 
    134 #include <netinet/in.h>
    135 #ifdef INET
    136 #include <netinet/in_var.h>
    137 #endif
    138 #include <netinet/if_inarp.h>
    139 
    140 #ifdef INET6
    141 #ifndef INET
    142 #include <netinet/in.h>
    143 #endif
    144 #include <netinet6/in6_var.h>
    145 #include <netinet6/nd6.h>
    146 #endif
    147 
    148 #include "carp.h"
    149 #if NCARP > 0
    150 #include <netinet/ip_carp.h>
    151 #endif
    152 
    153 #ifdef NETATALK
    154 #include <netatalk/at.h>
    155 #include <netatalk/at_var.h>
    156 #include <netatalk/at_extern.h>
    157 
    158 #define llc_snap_org_code llc_un.type_snap.org_code
    159 #define llc_snap_ether_type llc_un.type_snap.ether_type
    160 
    161 extern u_char	at_org_code[3];
    162 extern u_char	aarp_org_code[3];
    163 #endif /* NETATALK */
    164 
    165 #ifdef MPLS
    166 #include <netmpls/mpls.h>
    167 #include <netmpls/mpls_var.h>
    168 #endif
    169 
    170 CTASSERT(sizeof(struct ether_addr) == 6);
    171 CTASSERT(sizeof(struct ether_header) == 14);
    172 
    173 #ifdef DIAGNOSTIC
    174 static struct timeval bigpktppslim_last;
    175 static int bigpktppslim = 2;	/* XXX */
    176 static int bigpktpps_count;
    177 static kmutex_t bigpktpps_lock __cacheline_aligned;
    178 #endif
    179 
    180 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
    181     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    182 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
    183     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
    184 #define senderr(e) { error = (e); goto bad;}
    185 
    186 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 drop;
    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 drop;
    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 drop;
    622 		}
    623 		break;
    624 #endif
    625 	default:
    626 		goto drop;
    627 	}
    628 
    629 	KASSERT(inq != NULL);
    630 	IFQ_ENQUEUE_ISR(inq, m, isr);
    631 	return;
    632 
    633 drop:
    634 	m_freem(m);
    635 	if_statinc(ifp, if_ierrors); /* XXX should have a dedicated counter? */
    636 	return;
    637 }
    638 #endif /* defined (LLC) || defined (NETATALK) */
    639 
    640 /*
    641  * Process a received Ethernet packet;
    642  * the packet is in the mbuf chain m with
    643  * the ether header.
    644  */
    645 void
    646 ether_input(struct ifnet *ifp, struct mbuf *m)
    647 {
    648 	struct ethercom *ec = (struct ethercom *) ifp;
    649 	pktqueue_t *pktq = NULL;
    650 	struct ifqueue *inq = NULL;
    651 	uint16_t etype;
    652 	struct ether_header *eh;
    653 	size_t ehlen;
    654 	static int earlypkts;
    655 	int isr = 0;
    656 #if NAGR > 0
    657 	void *agrprivate;
    658 #endif
    659 
    660 	KASSERT(!cpu_intr_p());
    661 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    662 
    663 	if ((ifp->if_flags & IFF_UP) == 0)
    664 		goto drop;
    665 
    666 #ifdef MBUFTRACE
    667 	m_claimm(m, &ec->ec_rx_mowner);
    668 #endif
    669 
    670 	if (__predict_false(m->m_len < sizeof(*eh))) {
    671 		if ((m = m_pullup(m, sizeof(*eh))) == NULL) {
    672 			if_statinc(ifp, if_ierrors);
    673 			return;
    674 		}
    675 	}
    676 
    677 	eh = mtod(m, struct ether_header *);
    678 	etype = ntohs(eh->ether_type);
    679 	ehlen = sizeof(*eh);
    680 
    681 	if (__predict_false(earlypkts < 100 ||
    682 		entropy_epoch() == (unsigned)-1)) {
    683 		rnd_add_data(NULL, eh, ehlen, 0);
    684 		earlypkts++;
    685 	}
    686 
    687 	/*
    688 	 * Determine if the packet is within its size limits. For MPLS the
    689 	 * header length is variable, so we skip the check.
    690 	 */
    691 	if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
    692 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
    693 #ifdef DIAGNOSTIC
    694 		mutex_enter(&bigpktpps_lock);
    695 		if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
    696 		    bigpktppslim)) {
    697 			printf("%s: discarding oversize frame (len=%d)\n",
    698 			    ifp->if_xname, m->m_pkthdr.len);
    699 		}
    700 		mutex_exit(&bigpktpps_lock);
    701 #endif
    702 		goto drop;
    703 	}
    704 
    705 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    706 		/*
    707 		 * If this is not a simplex interface, drop the packet
    708 		 * if it came from us.
    709 		 */
    710 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
    711 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
    712 		    ETHER_ADDR_LEN) == 0) {
    713 			goto drop;
    714 		}
    715 
    716 		if (memcmp(etherbroadcastaddr,
    717 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    718 			m->m_flags |= M_BCAST;
    719 		else
    720 			m->m_flags |= M_MCAST;
    721 		if_statinc(ifp, if_imcasts);
    722 	}
    723 
    724 	/* If the CRC is still on the packet, trim it off. */
    725 	if (m->m_flags & M_HASFCS) {
    726 		m_adj(m, -ETHER_CRC_LEN);
    727 		m->m_flags &= ~M_HASFCS;
    728 	}
    729 
    730 	if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
    731 
    732 #if NCARP > 0
    733 	if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
    734 		/*
    735 		 * Clear M_PROMISC, in case the packet comes from a
    736 		 * vlan.
    737 		 */
    738 		m->m_flags &= ~M_PROMISC;
    739 		if (carp_input(m, (uint8_t *)&eh->ether_shost,
    740 		    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
    741 			return;
    742 	}
    743 #endif
    744 
    745 	if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
    746 	    (ifp->if_flags & IFF_PROMISC) != 0 &&
    747 	    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
    748 	     ETHER_ADDR_LEN) != 0) {
    749 		m->m_flags |= M_PROMISC;
    750 	}
    751 
    752 	if ((m->m_flags & M_PROMISC) == 0) {
    753 		if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    754 			return;
    755 		if (m == NULL)
    756 			return;
    757 
    758 		eh = mtod(m, struct ether_header *);
    759 		etype = ntohs(eh->ether_type);
    760 	}
    761 
    762 #if NAGR > 0
    763 	if (ifp->if_type != IFT_IEEE8023ADLAG) {
    764 		agrprivate = ifp->if_lagg;
    765 	} else {
    766 		agrprivate = NULL;
    767 	}
    768 	if (agrprivate != NULL &&
    769 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
    770 		m->m_flags &= ~M_PROMISC;
    771 		agr_input(ifp, m);
    772 		return;
    773 	}
    774 #endif
    775 
    776 	/* Handle input from a lagg(4) port */
    777 	if (ifp->if_type == IFT_IEEE8023ADLAG) {
    778 		KASSERT(lagg_input_ethernet_p != NULL);
    779 		m = (*lagg_input_ethernet_p)(ifp, m);
    780 		if (m == NULL)
    781 			return;
    782 	}
    783 
    784 	/*
    785 	 * If VLANs are configured on the interface, check to
    786 	 * see if the device performed the decapsulation and
    787 	 * provided us with the tag.
    788 	 */
    789 	if (ec->ec_nvlans && vlan_has_tag(m)) {
    790 #if NVLAN > 0
    791 		/*
    792 		 * vlan_input() will either recursively call ether_input()
    793 		 * or drop the packet.
    794 		 */
    795 		vlan_input(ifp, m);
    796 		return;
    797 #else
    798 		goto drop;
    799 #endif
    800 	}
    801 
    802 	/*
    803 	 * Handle protocols that expect to have the Ethernet header
    804 	 * (and possibly FCS) intact.
    805 	 */
    806 	switch (etype) {
    807 	case ETHERTYPE_VLAN: {
    808 		struct ether_vlan_header *evl = (void *)eh;
    809 
    810 		/*
    811 		 * If there is a tag of 0, then the VLAN header was probably
    812 		 * just being used to store the priority.  Extract the ether
    813 		 * type, and if IP or IPV6, let them deal with it.
    814 		 */
    815 		if (m->m_len >= sizeof(*evl) &&
    816 		    EVL_VLANOFTAG(ntohs(evl->evl_tag)) == 0) {
    817 			etype = ntohs(evl->evl_proto);
    818 			ehlen = sizeof(*evl);
    819 			if ((m->m_flags & M_PROMISC) == 0 &&
    820 			    (etype == ETHERTYPE_IP ||
    821 			     etype == ETHERTYPE_IPV6))
    822 				break;
    823 		}
    824 
    825 #if NVLAN > 0
    826 		/*
    827 		 * vlan_input() will either recursively call ether_input()
    828 		 * or drop the packet.
    829 		 */
    830 		if (ec->ec_nvlans != 0) {
    831 			vlan_input(ifp, m);
    832 			return;
    833 		} else
    834 #endif
    835 			goto drop;
    836 	}
    837 
    838 #if NPPPOE > 0
    839 	case ETHERTYPE_PPPOEDISC:
    840 		pppoedisc_input(ifp, m);
    841 		return;
    842 
    843 	case ETHERTYPE_PPPOE:
    844 		pppoe_input(ifp, m);
    845 		return;
    846 #endif
    847 
    848 	case ETHERTYPE_SLOWPROTOCOLS: {
    849 		uint8_t subtype;
    850 
    851 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
    852 			goto drop;
    853 
    854 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
    855 		switch (subtype) {
    856 #if NAGR > 0
    857 		case SLOWPROTOCOLS_SUBTYPE_LACP:
    858 			if (agrprivate != NULL) {
    859 				ieee8023ad_lacp_input(ifp, m);
    860 				return;
    861 			}
    862 			break;
    863 
    864 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
    865 			if (agrprivate != NULL) {
    866 				ieee8023ad_marker_input(ifp, m);
    867 				return;
    868 			}
    869 			break;
    870 #endif
    871 
    872 		default:
    873 			if (subtype == 0 || subtype > 10) {
    874 				/* illegal value */
    875 				goto error;
    876 			}
    877 			/* unknown subtype */
    878 			break;
    879 		}
    880 	}
    881 	/* FALLTHROUGH */
    882 	default:
    883 		if (m->m_flags & M_PROMISC)
    884 			goto drop;
    885 	}
    886 
    887 	/* If the CRC is still on the packet, trim it off. */
    888 	if (m->m_flags & M_HASFCS) {
    889 		m_adj(m, -ETHER_CRC_LEN);
    890 		m->m_flags &= ~M_HASFCS;
    891 	}
    892 
    893 	/* etype represents the size of the payload in this case */
    894 	if (etype <= ETHERMTU + sizeof(struct ether_header)) {
    895 		KASSERT(ehlen == sizeof(*eh));
    896 #if defined (LLC) || defined (NETATALK)
    897 		ether_input_llc(ifp, m, eh);
    898 		return;
    899 #else
    900 		goto error;
    901 #endif
    902 	}
    903 
    904 	/* Strip off the Ethernet header. */
    905 	m_adj(m, ehlen);
    906 
    907 	switch (etype) {
    908 #ifdef INET
    909 	case ETHERTYPE_IP:
    910 #ifdef GATEWAY
    911 		if (ipflow_fastforward(m))
    912 			return;
    913 #endif
    914 		pktq = ip_pktq;
    915 		break;
    916 
    917 	case ETHERTYPE_ARP:
    918 		isr = NETISR_ARP;
    919 		inq = &arpintrq;
    920 		break;
    921 
    922 	case ETHERTYPE_REVARP:
    923 		revarpinput(m);	/* XXX queue? */
    924 		return;
    925 #endif
    926 
    927 #ifdef INET6
    928 	case ETHERTYPE_IPV6:
    929 		if (__predict_false(!in6_present))
    930 			goto drop;
    931 #ifdef GATEWAY
    932 		if (ip6flow_fastforward(&m))
    933 			return;
    934 #endif
    935 		pktq = ip6_pktq;
    936 		break;
    937 #endif
    938 
    939 #ifdef NETATALK
    940 	case ETHERTYPE_ATALK:
    941 		isr = NETISR_ATALK;
    942 		inq = &atintrq1;
    943 		break;
    944 
    945 	case ETHERTYPE_AARP:
    946 		aarpinput(ifp, m); /* XXX queue? */
    947 		return;
    948 #endif
    949 
    950 #ifdef MPLS
    951 	case ETHERTYPE_MPLS:
    952 		isr = NETISR_MPLS;
    953 		inq = &mplsintrq;
    954 		break;
    955 #endif
    956 
    957 	default:
    958 		goto drop;
    959 	}
    960 
    961 	if (__predict_true(pktq)) {
    962 		const uint32_t h = pktq_rps_hash(&ether_pktq_rps_hash_p, m);
    963 		if (__predict_false(!pktq_enqueue(pktq, m, h))) {
    964 			m_freem(m);
    965 		}
    966 		return;
    967 	}
    968 
    969 	if (__predict_false(!inq)) {
    970 		/* Should not happen. */
    971 		goto error;
    972 	}
    973 
    974 	IFQ_ENQUEUE_ISR(inq, m, isr);
    975 	return;
    976 
    977 drop:
    978 	m_freem(m);
    979 	if_statinc(ifp, if_iqdrops); /* XXX should have a dedicated counter? */
    980 	return;
    981 error:
    982 	m_freem(m);
    983 	if_statinc(ifp, if_ierrors); /* XXX should have a dedicated counter? */
    984 	return;
    985 }
    986 
    987 /*
    988  * Convert Ethernet address to printable (loggable) representation.
    989  */
    990 char *
    991 ether_sprintf(const u_char *ap)
    992 {
    993 	static char etherbuf[3 * ETHER_ADDR_LEN];
    994 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
    995 }
    996 
    997 char *
    998 ether_snprintf(char *buf, size_t len, const u_char *ap)
    999 {
   1000 	char *cp = buf;
   1001 	size_t i;
   1002 
   1003 	for (i = 0; i < len / 3; i++) {
   1004 		*cp++ = hexdigits[*ap >> 4];
   1005 		*cp++ = hexdigits[*ap++ & 0xf];
   1006 		*cp++ = ':';
   1007 	}
   1008 	*--cp = '\0';
   1009 	return buf;
   1010 }
   1011 
   1012 /*
   1013  * Perform common duties while attaching to interface list
   1014  */
   1015 void
   1016 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
   1017 {
   1018 	struct ethercom *ec = (struct ethercom *)ifp;
   1019 	char xnamebuf[HOOKNAMSIZ];
   1020 
   1021 	ifp->if_type = IFT_ETHER;
   1022 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1023 	ifp->if_dlt = DLT_EN10MB;
   1024 	ifp->if_mtu = ETHERMTU;
   1025 	ifp->if_output = ether_output;
   1026 	ifp->_if_input = ether_input;
   1027 	if (ifp->if_baudrate == 0)
   1028 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1029 
   1030 	if (lla != NULL)
   1031 		if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
   1032 
   1033 	LIST_INIT(&ec->ec_multiaddrs);
   1034 	SIMPLEQ_INIT(&ec->ec_vids);
   1035 	ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   1036 	ec->ec_flags = 0;
   1037 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1038 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1039 	snprintf(xnamebuf, sizeof(xnamebuf),
   1040 	    "%s-ether_ifdetachhooks", ifp->if_xname);
   1041 	ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
   1042 #ifdef MBUFTRACE
   1043 	mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
   1044 	mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
   1045 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1046 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1047 	ifp->if_mowner = &ec->ec_tx_mowner;
   1048 #endif
   1049 }
   1050 
   1051 void
   1052 ether_ifdetach(struct ifnet *ifp)
   1053 {
   1054 	struct ethercom *ec = (void *) ifp;
   1055 	struct ether_multi *enm;
   1056 
   1057 	IFNET_ASSERT_UNLOCKED(ifp);
   1058 	/*
   1059 	 * Prevent further calls to ioctl (for example turning off
   1060 	 * promiscuous mode from the bridge code), which eventually can
   1061 	 * call if_init() which can cause panics because the interface
   1062 	 * is in the process of being detached. Return device not configured
   1063 	 * instead.
   1064 	 */
   1065 	ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
   1066 	    enxio);
   1067 
   1068 	simplehook_dohooks(ec->ec_ifdetach_hooks);
   1069 	KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
   1070 	simplehook_destroy(ec->ec_ifdetach_hooks);
   1071 
   1072 	bpf_detach(ifp);
   1073 
   1074 	ETHER_LOCK(ec);
   1075 	KASSERT(ec->ec_nvlans == 0);
   1076 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1077 		LIST_REMOVE(enm, enm_list);
   1078 		kmem_free(enm, sizeof(*enm));
   1079 		ec->ec_multicnt--;
   1080 	}
   1081 	ETHER_UNLOCK(ec);
   1082 
   1083 	mutex_obj_free(ec->ec_lock);
   1084 	ec->ec_lock = NULL;
   1085 
   1086 	ifp->if_mowner = NULL;
   1087 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1088 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1089 }
   1090 
   1091 void *
   1092 ether_ifdetachhook_establish(struct ifnet *ifp,
   1093     void (*fn)(void *), void *arg)
   1094 {
   1095 	struct ethercom *ec;
   1096 	khook_t *hk;
   1097 
   1098 	if (ifp->if_type != IFT_ETHER)
   1099 		return NULL;
   1100 
   1101 	ec = (struct ethercom *)ifp;
   1102 	hk = simplehook_establish(ec->ec_ifdetach_hooks,
   1103 	    fn, arg);
   1104 
   1105 	return (void *)hk;
   1106 }
   1107 
   1108 void
   1109 ether_ifdetachhook_disestablish(struct ifnet *ifp,
   1110     void *vhook, kmutex_t *lock)
   1111 {
   1112 	struct ethercom *ec;
   1113 
   1114 	if (vhook == NULL)
   1115 		return;
   1116 
   1117 	ec = (struct ethercom *)ifp;
   1118 	simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
   1119 }
   1120 
   1121 #if 0
   1122 /*
   1123  * This is for reference.  We have a table-driven version
   1124  * of the little-endian crc32 generator, which is faster
   1125  * than the double-loop.
   1126  */
   1127 uint32_t
   1128 ether_crc32_le(const uint8_t *buf, size_t len)
   1129 {
   1130 	uint32_t c, crc, carry;
   1131 	size_t i, j;
   1132 
   1133 	crc = 0xffffffffU;	/* initial value */
   1134 
   1135 	for (i = 0; i < len; i++) {
   1136 		c = buf[i];
   1137 		for (j = 0; j < 8; j++) {
   1138 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1139 			crc >>= 1;
   1140 			c >>= 1;
   1141 			if (carry)
   1142 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1143 		}
   1144 	}
   1145 
   1146 	return (crc);
   1147 }
   1148 #else
   1149 uint32_t
   1150 ether_crc32_le(const uint8_t *buf, size_t len)
   1151 {
   1152 	static const uint32_t crctab[] = {
   1153 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1154 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1155 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1156 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1157 	};
   1158 	uint32_t crc;
   1159 	size_t i;
   1160 
   1161 	crc = 0xffffffffU;	/* initial value */
   1162 
   1163 	for (i = 0; i < len; i++) {
   1164 		crc ^= buf[i];
   1165 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1166 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1167 	}
   1168 
   1169 	return (crc);
   1170 }
   1171 #endif
   1172 
   1173 uint32_t
   1174 ether_crc32_be(const uint8_t *buf, size_t len)
   1175 {
   1176 	uint32_t c, crc, carry;
   1177 	size_t i, j;
   1178 
   1179 	crc = 0xffffffffU;	/* initial value */
   1180 
   1181 	for (i = 0; i < len; i++) {
   1182 		c = buf[i];
   1183 		for (j = 0; j < 8; j++) {
   1184 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1185 			crc <<= 1;
   1186 			c >>= 1;
   1187 			if (carry)
   1188 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1189 		}
   1190 	}
   1191 
   1192 	return (crc);
   1193 }
   1194 
   1195 #ifdef INET
   1196 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1197     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1198 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1199     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1200 #endif
   1201 #ifdef INET6
   1202 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1203     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1204 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1205     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1206 #endif
   1207 
   1208 /*
   1209  * ether_aton implementation, not using a static buffer.
   1210  */
   1211 int
   1212 ether_aton_r(u_char *dest, size_t len, const char *str)
   1213 {
   1214 	const u_char *cp = (const void *)str;
   1215 	u_char *ep;
   1216 
   1217 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
   1218 
   1219 	if (len < ETHER_ADDR_LEN)
   1220 		return ENOSPC;
   1221 
   1222 	ep = dest + ETHER_ADDR_LEN;
   1223 
   1224 	while (*cp) {
   1225 		if (!isxdigit(*cp))
   1226 			return EINVAL;
   1227 
   1228 		*dest = atox(*cp);
   1229 		cp++;
   1230 		if (isxdigit(*cp)) {
   1231 			*dest = (*dest << 4) | atox(*cp);
   1232 			cp++;
   1233 		}
   1234 		dest++;
   1235 
   1236 		if (dest == ep)
   1237 			return (*cp == '\0') ? 0 : ENAMETOOLONG;
   1238 
   1239 		switch (*cp) {
   1240 		case ':':
   1241 		case '-':
   1242 		case '.':
   1243 			cp++;
   1244 			break;
   1245 		}
   1246 	}
   1247 	return ENOBUFS;
   1248 }
   1249 
   1250 /*
   1251  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1252  * addresses.
   1253  */
   1254 int
   1255 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
   1256     uint8_t addrhi[ETHER_ADDR_LEN])
   1257 {
   1258 #ifdef INET
   1259 	const struct sockaddr_in *sin;
   1260 #endif
   1261 #ifdef INET6
   1262 	const struct sockaddr_in6 *sin6;
   1263 #endif
   1264 
   1265 	switch (sa->sa_family) {
   1266 
   1267 	case AF_UNSPEC:
   1268 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
   1269 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1270 		break;
   1271 
   1272 #ifdef INET
   1273 	case AF_INET:
   1274 		sin = satocsin(sa);
   1275 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1276 			/*
   1277 			 * An IP address of INADDR_ANY means listen to
   1278 			 * or stop listening to all of the Ethernet
   1279 			 * multicast addresses used for IP.
   1280 			 * (This is for the sake of IP multicast routers.)
   1281 			 */
   1282 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
   1283 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
   1284 		} else {
   1285 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1286 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1287 		}
   1288 		break;
   1289 #endif
   1290 #ifdef INET6
   1291 	case AF_INET6:
   1292 		sin6 = satocsin6(sa);
   1293 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1294 			/*
   1295 			 * An IP6 address of 0 means listen to or stop
   1296 			 * listening to all of the Ethernet multicast
   1297 			 * address used for IP6.
   1298 			 * (This is used for multicast routers.)
   1299 			 */
   1300 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
   1301 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
   1302 		} else {
   1303 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1304 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1305 		}
   1306 		break;
   1307 #endif
   1308 
   1309 	default:
   1310 		return EAFNOSUPPORT;
   1311 	}
   1312 	return 0;
   1313 }
   1314 
   1315 /*
   1316  * Add an Ethernet multicast address or range of addresses to the list for a
   1317  * given interface.
   1318  */
   1319 int
   1320 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
   1321 {
   1322 	struct ether_multi *enm, *_enm;
   1323 	u_char addrlo[ETHER_ADDR_LEN];
   1324 	u_char addrhi[ETHER_ADDR_LEN];
   1325 	int error = 0;
   1326 
   1327 	/* Allocate out of lock */
   1328 	enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
   1329 
   1330 	ETHER_LOCK(ec);
   1331 	error = ether_multiaddr(sa, addrlo, addrhi);
   1332 	if (error != 0)
   1333 		goto out;
   1334 
   1335 	/*
   1336 	 * Verify that we have valid Ethernet multicast addresses.
   1337 	 */
   1338 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
   1339 		error = EINVAL;
   1340 		goto out;
   1341 	}
   1342 
   1343 	/*
   1344 	 * See if the address range is already in the list.
   1345 	 */
   1346 	_enm = ether_lookup_multi(addrlo, addrhi, ec);
   1347 	if (_enm != NULL) {
   1348 		/*
   1349 		 * Found it; just increment the reference count.
   1350 		 */
   1351 		++_enm->enm_refcount;
   1352 		error = 0;
   1353 		goto out;
   1354 	}
   1355 
   1356 	/*
   1357 	 * Link a new multicast record into the interface's multicast list.
   1358 	 */
   1359 	memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
   1360 	memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
   1361 	enm->enm_refcount = 1;
   1362 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1363 	ec->ec_multicnt++;
   1364 
   1365 	/*
   1366 	 * Return ENETRESET to inform the driver that the list has changed
   1367 	 * and its reception filter should be adjusted accordingly.
   1368 	 */
   1369 	error = ENETRESET;
   1370 	enm = NULL;
   1371 
   1372 out:
   1373 	ETHER_UNLOCK(ec);
   1374 	if (enm != NULL)
   1375 		kmem_free(enm, sizeof(*enm));
   1376 	return error;
   1377 }
   1378 
   1379 /*
   1380  * Delete a multicast address record.
   1381  */
   1382 int
   1383 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
   1384 {
   1385 	struct ether_multi *enm;
   1386 	u_char addrlo[ETHER_ADDR_LEN];
   1387 	u_char addrhi[ETHER_ADDR_LEN];
   1388 	int error;
   1389 
   1390 	ETHER_LOCK(ec);
   1391 	error = ether_multiaddr(sa, addrlo, addrhi);
   1392 	if (error != 0)
   1393 		goto error;
   1394 
   1395 	/*
   1396 	 * Look up the address in our list.
   1397 	 */
   1398 	enm = ether_lookup_multi(addrlo, addrhi, ec);
   1399 	if (enm == NULL) {
   1400 		error = ENXIO;
   1401 		goto error;
   1402 	}
   1403 	if (--enm->enm_refcount != 0) {
   1404 		/*
   1405 		 * Still some claims to this record.
   1406 		 */
   1407 		error = 0;
   1408 		goto error;
   1409 	}
   1410 
   1411 	/*
   1412 	 * No remaining claims to this record; unlink and free it.
   1413 	 */
   1414 	LIST_REMOVE(enm, enm_list);
   1415 	ec->ec_multicnt--;
   1416 	ETHER_UNLOCK(ec);
   1417 	kmem_free(enm, sizeof(*enm));
   1418 
   1419 	/*
   1420 	 * Return ENETRESET to inform the driver that the list has changed
   1421 	 * and its reception filter should be adjusted accordingly.
   1422 	 */
   1423 	return ENETRESET;
   1424 
   1425 error:
   1426 	ETHER_UNLOCK(ec);
   1427 	return error;
   1428 }
   1429 
   1430 void
   1431 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
   1432 {
   1433 	ec->ec_ifflags_cb = cb;
   1434 }
   1435 
   1436 void
   1437 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
   1438 {
   1439 
   1440 	ec->ec_vlan_cb = cb;
   1441 }
   1442 
   1443 static int
   1444 ether_ioctl_reinit(struct ethercom *ec)
   1445 {
   1446 	struct ifnet *ifp = &ec->ec_if;
   1447 	int error;
   1448 
   1449 	switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1450 	case IFF_RUNNING:
   1451 		/*
   1452 		 * If interface is marked down and it is running,
   1453 		 * then stop and disable it.
   1454 		 */
   1455 		(*ifp->if_stop)(ifp, 1);
   1456 		break;
   1457 	case IFF_UP:
   1458 		/*
   1459 		 * If interface is marked up and it is stopped, then
   1460 		 * start it.
   1461 		 */
   1462 		return (*ifp->if_init)(ifp);
   1463 	case IFF_UP | IFF_RUNNING:
   1464 		error = 0;
   1465 		if (ec->ec_ifflags_cb != NULL) {
   1466 			error = (*ec->ec_ifflags_cb)(ec);
   1467 			if (error == ENETRESET) {
   1468 				/*
   1469 				 * Reset the interface to pick up
   1470 				 * changes in any other flags that
   1471 				 * affect the hardware state.
   1472 				 */
   1473 				return (*ifp->if_init)(ifp);
   1474 			}
   1475 		} else
   1476 			error = (*ifp->if_init)(ifp);
   1477 		return error;
   1478 	case 0:
   1479 		break;
   1480 	}
   1481 
   1482 	return 0;
   1483 }
   1484 
   1485 /*
   1486  * Common ioctls for Ethernet interfaces.  Note, we must be
   1487  * called at splnet().
   1488  */
   1489 int
   1490 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1491 {
   1492 	struct ethercom *ec = (void *)ifp;
   1493 	struct eccapreq *eccr;
   1494 	struct ifreq *ifr = (struct ifreq *)data;
   1495 	struct if_laddrreq *iflr = data;
   1496 	const struct sockaddr_dl *sdl;
   1497 	static const uint8_t zero[ETHER_ADDR_LEN];
   1498 	int error;
   1499 
   1500 	switch (cmd) {
   1501 	case SIOCINITIFADDR:
   1502 	    {
   1503 		struct ifaddr *ifa = (struct ifaddr *)data;
   1504 		if (ifa->ifa_addr->sa_family != AF_LINK
   1505 		    && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
   1506 		       (IFF_UP | IFF_RUNNING)) {
   1507 			ifp->if_flags |= IFF_UP;
   1508 			if ((error = (*ifp->if_init)(ifp)) != 0)
   1509 				return error;
   1510 		}
   1511 #ifdef INET
   1512 		if (ifa->ifa_addr->sa_family == AF_INET)
   1513 			arp_ifinit(ifp, ifa);
   1514 #endif
   1515 		return 0;
   1516 	    }
   1517 
   1518 	case SIOCSIFMTU:
   1519 	    {
   1520 		int maxmtu;
   1521 
   1522 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1523 			maxmtu = ETHERMTU_JUMBO;
   1524 		else
   1525 			maxmtu = ETHERMTU;
   1526 
   1527 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1528 			return EINVAL;
   1529 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
   1530 			return error;
   1531 		else if (ifp->if_flags & IFF_UP) {
   1532 			/* Make sure the device notices the MTU change. */
   1533 			return (*ifp->if_init)(ifp);
   1534 		} else
   1535 			return 0;
   1536 	    }
   1537 
   1538 	case SIOCSIFFLAGS:
   1539 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1540 			return error;
   1541 		return ether_ioctl_reinit(ec);
   1542 	case SIOCGIFFLAGS:
   1543 		error = ifioctl_common(ifp, cmd, data);
   1544 		if (error == 0) {
   1545 			/* Set IFF_ALLMULTI for backcompat */
   1546 			ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
   1547 			    IFF_ALLMULTI : 0;
   1548 		}
   1549 		return error;
   1550 	case SIOCGETHERCAP:
   1551 		eccr = (struct eccapreq *)data;
   1552 		eccr->eccr_capabilities = ec->ec_capabilities;
   1553 		eccr->eccr_capenable = ec->ec_capenable;
   1554 		return 0;
   1555 	case SIOCSETHERCAP:
   1556 		eccr = (struct eccapreq *)data;
   1557 		if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
   1558 			return EINVAL;
   1559 		if (eccr->eccr_capenable == ec->ec_capenable)
   1560 			return 0;
   1561 #if 0 /* notyet */
   1562 		ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
   1563 		    | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
   1564 #else
   1565 		ec->ec_capenable = eccr->eccr_capenable;
   1566 #endif
   1567 		return ether_ioctl_reinit(ec);
   1568 	case SIOCADDMULTI:
   1569 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
   1570 	case SIOCDELMULTI:
   1571 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
   1572 	case SIOCSIFMEDIA:
   1573 	case SIOCGIFMEDIA:
   1574 		if (ec->ec_mii != NULL)
   1575 			return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
   1576 			    cmd);
   1577 		else if (ec->ec_ifmedia != NULL)
   1578 			return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
   1579 		else
   1580 			return ENOTTY;
   1581 		break;
   1582 	case SIOCALIFADDR:
   1583 		sdl = satocsdl(sstocsa(&iflr->addr));
   1584 		if (sdl->sdl_family != AF_LINK)
   1585 			;
   1586 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
   1587 			return EINVAL;
   1588 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
   1589 			return EINVAL;
   1590 		/*FALLTHROUGH*/
   1591 	default:
   1592 		return ifioctl_common(ifp, cmd, data);
   1593 	}
   1594 	return 0;
   1595 }
   1596 
   1597 /*
   1598  * Enable/disable passing VLAN packets if the parent interface supports it.
   1599  * Return:
   1600  * 	 0: Ok
   1601  *	-1: Parent interface does not support vlans
   1602  *	>0: Error
   1603  */
   1604 int
   1605 ether_enable_vlan_mtu(struct ifnet *ifp)
   1606 {
   1607 	int error;
   1608 	struct ethercom *ec = (void *)ifp;
   1609 
   1610 	/* Parent does not support VLAN's */
   1611 	if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
   1612 		return -1;
   1613 
   1614 	/*
   1615 	 * Parent supports the VLAN_MTU capability,
   1616 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
   1617 	 * enable it.
   1618 	 */
   1619 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1620 
   1621 	/* Interface is down, defer for later */
   1622 	if ((ifp->if_flags & IFF_UP) == 0)
   1623 		return 0;
   1624 
   1625 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1626 		return 0;
   1627 
   1628 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1629 	return error;
   1630 }
   1631 
   1632 int
   1633 ether_disable_vlan_mtu(struct ifnet *ifp)
   1634 {
   1635 	int error;
   1636 	struct ethercom *ec = (void *)ifp;
   1637 
   1638 	/* We still have VLAN's, defer for later */
   1639 	if (ec->ec_nvlans != 0)
   1640 		return 0;
   1641 
   1642 	/* Parent does not support VLAB's, nothing to do. */
   1643 	if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
   1644 		return -1;
   1645 
   1646 	/*
   1647 	 * Disable Tx/Rx of VLAN-sized frames.
   1648 	 */
   1649 	ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
   1650 
   1651 	/* Interface is down, defer for later */
   1652 	if ((ifp->if_flags & IFF_UP) == 0)
   1653 		return 0;
   1654 
   1655 	if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
   1656 		return 0;
   1657 
   1658 	ec->ec_capenable |= ETHERCAP_VLAN_MTU;
   1659 	return error;
   1660 }
   1661 
   1662 /*
   1663  * Add and delete VLAN TAG
   1664  */
   1665 int
   1666 ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
   1667 {
   1668 	struct ethercom *ec = (void *)ifp;
   1669 	struct vlanid_list *vidp;
   1670 	bool vlanmtu_enabled;
   1671 	uint16_t vid = EVL_VLANOFTAG(vtag);
   1672 	int error;
   1673 
   1674 	vlanmtu_enabled = false;
   1675 
   1676 	/* Add a vid to the list */
   1677 	vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
   1678 	vidp->vid = vid;
   1679 
   1680 	ETHER_LOCK(ec);
   1681 	ec->ec_nvlans++;
   1682 	SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
   1683 	ETHER_UNLOCK(ec);
   1684 
   1685 	if (ec->ec_nvlans == 1) {
   1686 		IFNET_LOCK(ifp);
   1687 		error = ether_enable_vlan_mtu(ifp);
   1688 		IFNET_UNLOCK(ifp);
   1689 
   1690 		if (error == 0) {
   1691 			vlanmtu_enabled = true;
   1692 		} else if (error != -1) {
   1693 			goto fail;
   1694 		}
   1695 	}
   1696 
   1697 	if (ec->ec_vlan_cb != NULL) {
   1698 		error = (*ec->ec_vlan_cb)(ec, vid, true);
   1699 		if (error != 0)
   1700 			goto fail;
   1701 	}
   1702 
   1703 	if (vlanmtu_status != NULL)
   1704 		*vlanmtu_status = vlanmtu_enabled;
   1705 
   1706 	return 0;
   1707 fail:
   1708 	ETHER_LOCK(ec);
   1709 	ec->ec_nvlans--;
   1710 	SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
   1711 	ETHER_UNLOCK(ec);
   1712 
   1713 	if (vlanmtu_enabled) {
   1714 		IFNET_LOCK(ifp);
   1715 		(void)ether_disable_vlan_mtu(ifp);
   1716 		IFNET_UNLOCK(ifp);
   1717 	}
   1718 
   1719 	kmem_free(vidp, sizeof(*vidp));
   1720 
   1721 	return error;
   1722 }
   1723 
   1724 int
   1725 ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
   1726 {
   1727 	struct ethercom *ec = (void *)ifp;
   1728 	struct vlanid_list *vidp;
   1729 	uint16_t vid = EVL_VLANOFTAG(vtag);
   1730 
   1731 	ETHER_LOCK(ec);
   1732 	SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
   1733 		if (vidp->vid == vid) {
   1734 			SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
   1735 			    vlanid_list, vid_list);
   1736 			ec->ec_nvlans--;
   1737 			break;
   1738 		}
   1739 	}
   1740 	ETHER_UNLOCK(ec);
   1741 
   1742 	if (vidp == NULL)
   1743 		return ENOENT;
   1744 
   1745 	if (ec->ec_vlan_cb != NULL) {
   1746 		(void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
   1747 	}
   1748 
   1749 	if (ec->ec_nvlans == 0) {
   1750 		IFNET_LOCK(ifp);
   1751 		(void)ether_disable_vlan_mtu(ifp);
   1752 		IFNET_UNLOCK(ifp);
   1753 	}
   1754 
   1755 	kmem_free(vidp, sizeof(*vidp));
   1756 
   1757 	return 0;
   1758 }
   1759 
   1760 static int
   1761 ether_multicast_sysctl(SYSCTLFN_ARGS)
   1762 {
   1763 	struct ether_multi *enm;
   1764 	struct ifnet *ifp;
   1765 	struct ethercom *ec;
   1766 	int error = 0;
   1767 	size_t written;
   1768 	struct psref psref;
   1769 	int bound;
   1770 	unsigned int multicnt;
   1771 	struct ether_multi_sysctl *addrs;
   1772 	int i;
   1773 
   1774 	if (namelen != 1)
   1775 		return EINVAL;
   1776 
   1777 	bound = curlwp_bind();
   1778 	ifp = if_get_byindex(name[0], &psref);
   1779 	if (ifp == NULL) {
   1780 		error = ENODEV;
   1781 		goto out;
   1782 	}
   1783 	if (ifp->if_type != IFT_ETHER) {
   1784 		if_put(ifp, &psref);
   1785 		*oldlenp = 0;
   1786 		goto out;
   1787 	}
   1788 	ec = (struct ethercom *)ifp;
   1789 
   1790 	if (oldp == NULL) {
   1791 		if_put(ifp, &psref);
   1792 		*oldlenp = ec->ec_multicnt * sizeof(*addrs);
   1793 		goto out;
   1794 	}
   1795 
   1796 	/*
   1797 	 * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
   1798 	 * is sleepable, while holding it. Copy data to a local buffer first
   1799 	 * with the lock taken and then call sysctl_copyout without holding it.
   1800 	 */
   1801 retry:
   1802 	multicnt = ec->ec_multicnt;
   1803 
   1804 	if (multicnt == 0) {
   1805 		if_put(ifp, &psref);
   1806 		*oldlenp = 0;
   1807 		goto out;
   1808 	}
   1809 
   1810 	addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
   1811 
   1812 	ETHER_LOCK(ec);
   1813 	if (multicnt != ec->ec_multicnt) {
   1814 		/* The number of multicast addresses has changed */
   1815 		ETHER_UNLOCK(ec);
   1816 		kmem_free(addrs, sizeof(*addrs) * multicnt);
   1817 		goto retry;
   1818 	}
   1819 
   1820 	i = 0;
   1821 	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
   1822 		struct ether_multi_sysctl *addr = &addrs[i];
   1823 		addr->enm_refcount = enm->enm_refcount;
   1824 		memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
   1825 		memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
   1826 		i++;
   1827 	}
   1828 	ETHER_UNLOCK(ec);
   1829 
   1830 	error = 0;
   1831 	written = 0;
   1832 	for (i = 0; i < multicnt; i++) {
   1833 		struct ether_multi_sysctl *addr = &addrs[i];
   1834 
   1835 		if (written + sizeof(*addr) > *oldlenp)
   1836 			break;
   1837 		error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
   1838 		if (error)
   1839 			break;
   1840 		written += sizeof(*addr);
   1841 		oldp = (char *)oldp + sizeof(*addr);
   1842 	}
   1843 	kmem_free(addrs, sizeof(*addrs) * multicnt);
   1844 
   1845 	if_put(ifp, &psref);
   1846 
   1847 	*oldlenp = written;
   1848 out:
   1849 	curlwp_bindx(bound);
   1850 	return error;
   1851 }
   1852 
   1853 static void
   1854 ether_sysctl_setup(struct sysctllog **clog)
   1855 {
   1856 	const struct sysctlnode *rnode = NULL;
   1857 
   1858 	sysctl_createv(clog, 0, NULL, &rnode,
   1859 		       CTLFLAG_PERMANENT,
   1860 		       CTLTYPE_NODE, "ether",
   1861 		       SYSCTL_DESCR("Ethernet-specific information"),
   1862 		       NULL, 0, NULL, 0,
   1863 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1864 
   1865 	sysctl_createv(clog, 0, &rnode, NULL,
   1866 		       CTLFLAG_PERMANENT,
   1867 		       CTLTYPE_NODE, "multicast",
   1868 		       SYSCTL_DESCR("multicast addresses"),
   1869 		       ether_multicast_sysctl, 0, NULL, 0,
   1870 		       CTL_CREATE, CTL_EOL);
   1871 
   1872 	sysctl_createv(clog, 0, &rnode, NULL,
   1873 		       CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1874 		       CTLTYPE_STRING, "rps_hash",
   1875 		       SYSCTL_DESCR("Interface rps hash function control"),
   1876 		       sysctl_pktq_rps_hash_handler, 0, (void *)&ether_pktq_rps_hash_p,
   1877 		       PKTQ_RPS_HASH_NAME_LEN,
   1878 		       CTL_CREATE, CTL_EOL);
   1879 }
   1880 
   1881 void
   1882 etherinit(void)
   1883 {
   1884 
   1885 #ifdef DIAGNOSTIC
   1886 	mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
   1887 #endif
   1888 	ether_pktq_rps_hash_p = pktq_rps_hash_default;
   1889 	ether_sysctl_setup(NULL);
   1890 }
   1891