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