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