Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.185.2.1
      1 /*	$NetBSD: if_ethersubr.c,v 1.185.2.1 2011/06/06 09:09:52 jruoho 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.185.2.1 2011/06/06 09:09:52 jruoho 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 		union mpls_shim msh;
    458 		msh.s_addr = MPLS_GETSADDR(rt0);
    459 		if (msh.shim.label != MPLS_LABEL_IMPLNULL)
    460 			etype = htons(ETHERTYPE_MPLS);
    461 	}
    462 #endif
    463 
    464 	if (mcopy)
    465 		(void)looutput(ifp, mcopy, dst, rt);
    466 
    467 	/* If no ether type is set, this must be a 802.2 formatted packet.
    468 	 */
    469 	if (etype == 0)
    470 		etype = htons(m->m_pkthdr.len);
    471 	/*
    472 	 * Add local net header.  If no space in first mbuf,
    473 	 * allocate another.
    474 	 */
    475 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
    476 	if (m == 0)
    477 		senderr(ENOBUFS);
    478 	eh = mtod(m, struct ether_header *);
    479 	/* Note: etype is already in network byte order. */
    480 	(void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
    481  	memcpy(eh->ether_dhost, edst, sizeof(edst));
    482 	if (hdrcmplt)
    483 		memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
    484 	else
    485 	 	memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
    486 		    sizeof(eh->ether_shost));
    487 
    488 #if NCARP > 0
    489 	if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
    490 	 	memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
    491 		    sizeof(eh->ether_shost));
    492 	}
    493 #endif /* NCARP > 0 */
    494 
    495 #ifdef PFIL_HOOKS
    496 	if ((error = pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
    497 		return (error);
    498 	if (m == NULL)
    499 		return (0);
    500 #endif
    501 
    502 #if NBRIDGE > 0
    503 	/*
    504 	 * Bridges require special output handling.
    505 	 */
    506 	if (ifp->if_bridge)
    507 		return (bridge_output(ifp, m, NULL, NULL));
    508 #endif
    509 
    510 #if NCARP > 0
    511 	if (ifp != ifp0)
    512 		ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
    513 #endif /* NCARP > 0 */
    514 
    515 #ifdef ALTQ
    516 	/*
    517 	 * If ALTQ is enabled on the parent interface, do
    518 	 * classification; the queueing discipline might not
    519 	 * require classification, but might require the
    520 	 * address family/header pointer in the pktattr.
    521 	 */
    522 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
    523 		altq_etherclassify(&ifp->if_snd, m, &pktattr);
    524 #endif
    525 	return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
    526 
    527 bad:
    528 	if (m)
    529 		m_freem(m);
    530 	return (error);
    531 }
    532 
    533 #ifdef ALTQ
    534 /*
    535  * This routine is a slight hack to allow a packet to be classified
    536  * if the Ethernet headers are present.  It will go away when ALTQ's
    537  * classification engine understands link headers.
    538  */
    539 void
    540 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
    541     struct altq_pktattr *pktattr)
    542 {
    543 	struct ether_header *eh;
    544 	uint16_t ether_type;
    545 	int hlen, af, hdrsize;
    546 	void *hdr;
    547 
    548 	hlen = ETHER_HDR_LEN;
    549 	eh = mtod(m, struct ether_header *);
    550 
    551 	ether_type = htons(eh->ether_type);
    552 
    553 	if (ether_type < ETHERMTU) {
    554 		/* LLC/SNAP */
    555 		struct llc *llc = (struct llc *)(eh + 1);
    556 		hlen += 8;
    557 
    558 		if (m->m_len < hlen ||
    559 		    llc->llc_dsap != LLC_SNAP_LSAP ||
    560 		    llc->llc_ssap != LLC_SNAP_LSAP ||
    561 		    llc->llc_control != LLC_UI) {
    562 			/* Not SNAP. */
    563 			goto bad;
    564 		}
    565 
    566 		ether_type = htons(llc->llc_un.type_snap.ether_type);
    567 	}
    568 
    569 	switch (ether_type) {
    570 	case ETHERTYPE_IP:
    571 		af = AF_INET;
    572 		hdrsize = 20;		/* sizeof(struct ip) */
    573 		break;
    574 
    575 	case ETHERTYPE_IPV6:
    576 		af = AF_INET6;
    577 		hdrsize = 40;		/* sizeof(struct ip6_hdr) */
    578 		break;
    579 
    580 	default:
    581 		af = AF_UNSPEC;
    582 		hdrsize = 0;
    583 		break;
    584 	}
    585 
    586 	while (m->m_len <= hlen) {
    587 		hlen -= m->m_len;
    588 		m = m->m_next;
    589 	}
    590 	if (m->m_len < (hlen + hdrsize)) {
    591 		/*
    592 		 * protocol header not in a single mbuf.
    593 		 * We can't cope with this situation right
    594 		 * now (but it shouldn't ever happen, really, anyhow).
    595 		 */
    596 #ifdef DEBUG
    597 		printf("altq_etherclassify: headers span multiple mbufs: "
    598 		    "%d < %d\n", m->m_len, (hlen + hdrsize));
    599 #endif
    600 		goto bad;
    601 	}
    602 
    603 	m->m_data += hlen;
    604 	m->m_len -= hlen;
    605 
    606 	hdr = mtod(m, void *);
    607 
    608 	if (ALTQ_NEEDS_CLASSIFY(ifq))
    609 		pktattr->pattr_class =
    610 		    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
    611 	pktattr->pattr_af = af;
    612 	pktattr->pattr_hdr = hdr;
    613 
    614 	m->m_data -= hlen;
    615 	m->m_len += hlen;
    616 
    617 	return;
    618 
    619  bad:
    620 	pktattr->pattr_class = NULL;
    621 	pktattr->pattr_hdr = NULL;
    622 	pktattr->pattr_af = AF_UNSPEC;
    623 }
    624 #endif /* ALTQ */
    625 
    626 /*
    627  * Process a received Ethernet packet;
    628  * the packet is in the mbuf chain m with
    629  * the ether header.
    630  */
    631 void
    632 ether_input(struct ifnet *ifp, struct mbuf *m)
    633 {
    634 	struct ethercom *ec = (struct ethercom *) ifp;
    635 	struct ifqueue *inq;
    636 	uint16_t etype;
    637 	struct ether_header *eh;
    638 	size_t ehlen;
    639 #if defined (ISO) || defined (LLC) || defined(NETATALK)
    640 	struct llc *l;
    641 #endif
    642 
    643 	if ((ifp->if_flags & IFF_UP) == 0) {
    644 		m_freem(m);
    645 		return;
    646 	}
    647 
    648 #ifdef MBUFTRACE
    649 	m_claimm(m, &ec->ec_rx_mowner);
    650 #endif
    651 	eh = mtod(m, struct ether_header *);
    652 	etype = ntohs(eh->ether_type);
    653 	ehlen = sizeof(*eh);
    654 
    655 	/*
    656 	 * Determine if the packet is within its size limits.
    657 	 */
    658 	if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
    659 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
    660 		if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
    661 			    bigpktppslim)) {
    662 			printf("%s: discarding oversize frame (len=%d)\n",
    663 			    ifp->if_xname, m->m_pkthdr.len);
    664 		}
    665 		m_freem(m);
    666 		return;
    667 	}
    668 
    669 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    670 		/*
    671 		 * If this is not a simplex interface, drop the packet
    672 		 * if it came from us.
    673 		 */
    674 		if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
    675 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
    676 		    ETHER_ADDR_LEN) == 0) {
    677 			m_freem(m);
    678 			return;
    679 		}
    680 
    681 		if (memcmp(etherbroadcastaddr,
    682 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    683 			m->m_flags |= M_BCAST;
    684 		else
    685 			m->m_flags |= M_MCAST;
    686 		ifp->if_imcasts++;
    687 	}
    688 
    689 	/* If the CRC is still on the packet, trim it off. */
    690 	if (m->m_flags & M_HASFCS) {
    691 		m_adj(m, -ETHER_CRC_LEN);
    692 		m->m_flags &= ~M_HASFCS;
    693 	}
    694 
    695 	ifp->if_ibytes += m->m_pkthdr.len;
    696 
    697 #if NBRIDGE > 0
    698 	/*
    699 	 * Tap the packet off here for a bridge.  bridge_input()
    700 	 * will return NULL if it has consumed the packet, otherwise
    701 	 * it gets processed as normal.  Note that bridge_input()
    702 	 * will always return the original packet if we need to
    703 	 * process it locally.
    704 	 */
    705 	if (ifp->if_bridge) {
    706 		/* clear M_PROMISC, in case the packets comes from a vlan */
    707 		m->m_flags &= ~M_PROMISC;
    708 		m = bridge_input(ifp, m);
    709 		if (m == NULL)
    710 			return;
    711 
    712 		/*
    713 		 * Bridge has determined that the packet is for us.
    714 		 * Update our interface pointer -- we may have had
    715 		 * to "bridge" the packet locally.
    716 		 */
    717 		ifp = m->m_pkthdr.rcvif;
    718 	} else
    719 #endif /* NBRIDGE > 0 */
    720 	{
    721 
    722 #if NCARP > 0
    723 		if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
    724 			/*
    725 			 * clear M_PROMISC, in case the packets comes from a
    726 			 * vlan
    727 			 */
    728 			m->m_flags &= ~M_PROMISC;
    729 			if (carp_input(m, (uint8_t *)&eh->ether_shost,
    730 			    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
    731 				return;
    732 		}
    733 #endif /* NCARP > 0 */
    734 		if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
    735 		    (ifp->if_flags & IFF_PROMISC) != 0 &&
    736 		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
    737 			   ETHER_ADDR_LEN) != 0) {
    738 			m->m_flags |= M_PROMISC;
    739 		}
    740 	}
    741 
    742 #ifdef PFIL_HOOKS
    743 	if ((m->m_flags & M_PROMISC) == 0) {
    744 		if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    745 			return;
    746 		if (m == NULL)
    747 			return;
    748 
    749 		eh = mtod(m, struct ether_header *);
    750 		etype = ntohs(eh->ether_type);
    751 		ehlen = sizeof(*eh);
    752 	}
    753 #endif
    754 
    755 #if NAGR > 0
    756 	if (ifp->if_agrprivate &&
    757 	    __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
    758 		m->m_flags &= ~M_PROMISC;
    759 		agr_input(ifp, m);
    760 		return;
    761 	}
    762 #endif /* NAGR > 0 */
    763 
    764 	/*
    765 	 * If VLANs are configured on the interface, check to
    766 	 * see if the device performed the decapsulation and
    767 	 * provided us with the tag.
    768 	 */
    769 	if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
    770 #if NVLAN > 0
    771 		/*
    772 		 * vlan_input() will either recursively call ether_input()
    773 		 * or drop the packet.
    774 		 */
    775 		vlan_input(ifp, m);
    776 #else
    777 		m_freem(m);
    778 #endif
    779 		return;
    780 	}
    781 
    782 	/*
    783 	 * Handle protocols that expect to have the Ethernet header
    784 	 * (and possibly FCS) intact.
    785 	 */
    786 	switch (etype) {
    787 	case ETHERTYPE_VLAN: {
    788 		struct ether_vlan_header *evl = (void *)eh;
    789 		/*
    790 		 * If there is a tag of 0, then the VLAN header was probably
    791 		 * just being used to store the priority.  Extract the ether
    792 		 * type, and if IP or IPV6, let them deal with it.
    793 		 */
    794 		if (m->m_len <= sizeof(*evl)
    795 		    && EVL_VLANOFTAG(evl->evl_tag) == 0) {
    796 			etype = ntohs(evl->evl_proto);
    797 			ehlen = sizeof(*evl);
    798 			if ((m->m_flags & M_PROMISC) == 0
    799 			    && (etype == ETHERTYPE_IP
    800 				|| etype == ETHERTYPE_IPV6))
    801 				break;
    802 		}
    803 #if NVLAN > 0
    804 		/*
    805 		 * vlan_input() will either recursively call ether_input()
    806 		 * or drop the packet.
    807 		 */
    808 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
    809 			vlan_input(ifp, m);
    810 		else
    811 #endif /* NVLAN > 0 */
    812 			m_freem(m);
    813 		return;
    814 	}
    815 #if NPPPOE > 0
    816 	case ETHERTYPE_PPPOEDISC:
    817 	case ETHERTYPE_PPPOE:
    818 		if (m->m_flags & M_PROMISC) {
    819 			m_freem(m);
    820 			return;
    821 		}
    822 #ifndef PPPOE_SERVER
    823 		if (m->m_flags & (M_MCAST | M_BCAST)) {
    824 			m_freem(m);
    825 			return;
    826 		}
    827 #endif
    828 
    829 		if (etype == ETHERTYPE_PPPOEDISC)
    830 			inq = &ppoediscinq;
    831 		else
    832 			inq = &ppoeinq;
    833 		if (IF_QFULL(inq)) {
    834 			IF_DROP(inq);
    835 			m_freem(m);
    836 		} else
    837 			IF_ENQUEUE(inq, m);
    838 		softint_schedule(pppoe_softintr);
    839 		return;
    840 #endif /* NPPPOE > 0 */
    841 	case ETHERTYPE_SLOWPROTOCOLS: {
    842 		uint8_t subtype;
    843 
    844 #if defined(DIAGNOSTIC)
    845 		if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
    846 			panic("ether_input: too short slow protocol packet");
    847 		}
    848 #endif
    849 		m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
    850 		switch (subtype) {
    851 #if NAGR > 0
    852 		case SLOWPROTOCOLS_SUBTYPE_LACP:
    853 			if (ifp->if_agrprivate) {
    854 				ieee8023ad_lacp_input(ifp, m);
    855 				return;
    856 			}
    857 			break;
    858 
    859 		case SLOWPROTOCOLS_SUBTYPE_MARKER:
    860 			if (ifp->if_agrprivate) {
    861 				ieee8023ad_marker_input(ifp, m);
    862 				return;
    863 			}
    864 			break;
    865 #endif /* NAGR > 0 */
    866 		default:
    867 			if (subtype == 0 || subtype > 10) {
    868 				/* illegal value */
    869 				m_freem(m);
    870 				return;
    871 			}
    872 			/* unknown subtype */
    873 			break;
    874 		}
    875 		/* FALLTHROUGH */
    876 	}
    877 	default:
    878 		if (m->m_flags & M_PROMISC) {
    879 			m_freem(m);
    880 			return;
    881 		}
    882 	}
    883 
    884 	/* If the CRC is still on the packet, trim it off. */
    885 	if (m->m_flags & M_HASFCS) {
    886 		m_adj(m, -ETHER_CRC_LEN);
    887 		m->m_flags &= ~M_HASFCS;
    888 	}
    889 
    890 	if (etype > ETHERMTU + sizeof (struct ether_header)) {
    891 		/* Strip off the Ethernet header. */
    892 		m_adj(m, ehlen);
    893 
    894 		switch (etype) {
    895 #ifdef INET
    896 		case ETHERTYPE_IP:
    897 #ifdef GATEWAY
    898 			if (ipflow_fastforward(m))
    899 				return;
    900 #endif
    901 			schednetisr(NETISR_IP);
    902 			inq = &ipintrq;
    903 			break;
    904 
    905 		case ETHERTYPE_ARP:
    906 			schednetisr(NETISR_ARP);
    907 			inq = &arpintrq;
    908 			break;
    909 
    910 		case ETHERTYPE_REVARP:
    911 			revarpinput(m);	/* XXX queue? */
    912 			return;
    913 #endif
    914 #ifdef INET6
    915 		case ETHERTYPE_IPV6:
    916 #ifdef GATEWAY
    917 			if (ip6flow_fastforward(m))
    918 				return;
    919 #endif
    920 			schednetisr(NETISR_IPV6);
    921 			inq = &ip6intrq;
    922 			break;
    923 #endif
    924 #ifdef IPX
    925 		case ETHERTYPE_IPX:
    926 			schednetisr(NETISR_IPX);
    927 			inq = &ipxintrq;
    928 			break;
    929 #endif
    930 #ifdef NETATALK
    931 		case ETHERTYPE_ATALK:
    932 			schednetisr(NETISR_ATALK);
    933 			inq = &atintrq1;
    934 			break;
    935 		case ETHERTYPE_AARP:
    936 			/* probably this should be done with a NETISR as well */
    937 			aarpinput(ifp, m); /* XXX */
    938 			return;
    939 #endif /* NETATALK */
    940 #ifdef MPLS
    941 		case ETHERTYPE_MPLS:
    942 			schednetisr(NETISR_MPLS);
    943 			inq = &mplsintrq;
    944 			break;
    945 #endif
    946 		default:
    947 			m_freem(m);
    948 			return;
    949 		}
    950 	} else {
    951 #if defined (ISO) || defined (LLC) || defined (NETATALK)
    952 		l = (struct llc *)(eh+1);
    953 		switch (l->llc_dsap) {
    954 #ifdef NETATALK
    955 		case LLC_SNAP_LSAP:
    956 			switch (l->llc_control) {
    957 			case LLC_UI:
    958 				if (l->llc_ssap != LLC_SNAP_LSAP) {
    959 					goto dropanyway;
    960 				}
    961 
    962 				if (memcmp(&(l->llc_snap_org_code)[0],
    963 				    at_org_code, sizeof(at_org_code)) == 0 &&
    964 				    ntohs(l->llc_snap_ether_type) ==
    965 				    ETHERTYPE_ATALK) {
    966 					inq = &atintrq2;
    967 					m_adj(m, sizeof(struct ether_header)
    968 					    + sizeof(struct llc));
    969 					schednetisr(NETISR_ATALK);
    970 					break;
    971 				}
    972 
    973 				if (memcmp(&(l->llc_snap_org_code)[0],
    974 				    aarp_org_code,
    975 				    sizeof(aarp_org_code)) == 0 &&
    976 				    ntohs(l->llc_snap_ether_type) ==
    977 				    ETHERTYPE_AARP) {
    978 					m_adj( m, sizeof(struct ether_header)
    979 					    + sizeof(struct llc));
    980 					aarpinput(ifp, m); /* XXX */
    981 				    return;
    982 				}
    983 
    984 			default:
    985 				goto dropanyway;
    986 			}
    987 			break;
    988 #endif /* NETATALK */
    989 #ifdef	ISO
    990 		case LLC_ISO_LSAP:
    991 			switch (l->llc_control) {
    992 			case LLC_UI:
    993 				/* LLC_UI_P forbidden in class 1 service */
    994 				if ((l->llc_dsap == LLC_ISO_LSAP) &&	/* XXX? case tested */
    995 				    (l->llc_ssap == LLC_ISO_LSAP)) {
    996 					/* LSAP for ISO */
    997 					/* XXX length computation?? */
    998 					if (m->m_pkthdr.len > etype + sizeof(struct ether_header))
    999 						m_adj(m, etype - m->m_pkthdr.len);
   1000 
   1001 #ifdef ARGO_DEBUG
   1002 					if (argo_debug[D_ETHER])
   1003 						printf("clnp packet");
   1004 #endif
   1005 					schednetisr(NETISR_ISO);
   1006 					inq = &clnlintrq;
   1007 					break;
   1008 				}
   1009 				goto dropanyway;
   1010 
   1011 			case LLC_XID:
   1012 			case LLC_XID_P:
   1013 				if(m->m_len < LLC_XID_BASIC_MINLEN + sizeof(struct ether_header))
   1014 					/* XXX m_pullup? */
   1015 					goto dropanyway;
   1016 				l->llc_window = 0;
   1017 				l->llc_fid = LLC_XID_FORMAT_BASIC;
   1018 				l->llc_class = LLC_XID_CLASS_I;
   1019 				l->llc_dsap = l->llc_ssap = 0;
   1020 				/* Fall through to */
   1021 			case LLC_TEST:
   1022 			case LLC_TEST_P:
   1023 			{
   1024 				struct sockaddr sa;
   1025 				struct ether_header *eh2;
   1026 				int i;
   1027 				u_char c = l->llc_dsap;
   1028 
   1029 				l->llc_dsap = l->llc_ssap;
   1030 				l->llc_ssap = c;
   1031 				m_adj(m, sizeof(struct ether_header));
   1032 				/* XXX we can optimize here? */
   1033 				if (m->m_flags & (M_BCAST | M_MCAST))
   1034 					memcpy(eh->ether_dhost,
   1035 					    CLLADDR(ifp->if_sadl),
   1036 					    ETHER_ADDR_LEN);
   1037 				sa.sa_family = AF_UNSPEC;
   1038 				sa.sa_len = sizeof(sa);
   1039 				eh2 = (struct ether_header *)sa.sa_data;
   1040 				for (i = 0; i < 6; i++) {
   1041 					eh2->ether_shost[i] = c =
   1042 					    eh->ether_dhost[i];
   1043 					eh2->ether_dhost[i] =
   1044 					    eh->ether_dhost[i] =
   1045 					    eh->ether_shost[i];
   1046 					eh->ether_shost[i] = c;
   1047 				}
   1048 				ifp->if_output(ifp, m, &sa, NULL);
   1049 				return;
   1050 			}
   1051 			default:
   1052 				m_freem(m);
   1053 				return;
   1054 			}
   1055 			break;
   1056 #endif /* ISO */
   1057 #if defined (ISO) || defined (NETATALK)
   1058 		dropanyway:
   1059 #endif
   1060 		default:
   1061 			m_freem(m);
   1062 			return;
   1063 		}
   1064 #else /* ISO || LLC || NETATALK*/
   1065 		m_freem(m);
   1066 		return;
   1067 #endif /* ISO || LLC || NETATALK*/
   1068 	}
   1069 
   1070 	if (IF_QFULL(inq)) {
   1071 		IF_DROP(inq);
   1072 		m_freem(m);
   1073 	} else
   1074 		IF_ENQUEUE(inq, m);
   1075 }
   1076 
   1077 /*
   1078  * Convert Ethernet address to printable (loggable) representation.
   1079  */
   1080 char *
   1081 ether_sprintf(const u_char *ap)
   1082 {
   1083 	static char etherbuf[3 * ETHER_ADDR_LEN];
   1084 	return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
   1085 }
   1086 
   1087 char *
   1088 ether_snprintf(char *buf, size_t len, const u_char *ap)
   1089 {
   1090 	char *cp = buf;
   1091 	size_t i;
   1092 
   1093 	for (i = 0; i < len / 3; i++) {
   1094 		*cp++ = hexdigits[*ap >> 4];
   1095 		*cp++ = hexdigits[*ap++ & 0xf];
   1096 		*cp++ = ':';
   1097 	}
   1098 	*--cp = '\0';
   1099 	return buf;
   1100 }
   1101 
   1102 /*
   1103  * Perform common duties while attaching to interface list
   1104  */
   1105 void
   1106 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
   1107 {
   1108 	struct ethercom *ec = (struct ethercom *)ifp;
   1109 
   1110 	ifp->if_type = IFT_ETHER;
   1111 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1112 	ifp->if_dlt = DLT_EN10MB;
   1113 	ifp->if_mtu = ETHERMTU;
   1114 	ifp->if_output = ether_output;
   1115 	ifp->if_input = ether_input;
   1116 	if (ifp->if_baudrate == 0)
   1117 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1118 
   1119 	if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
   1120 
   1121 	LIST_INIT(&ec->ec_multiaddrs);
   1122 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1123 	bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1124 #ifdef MBUFTRACE
   1125 	strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
   1126 	    sizeof(ec->ec_tx_mowner.mo_name));
   1127 	strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
   1128 	    sizeof(ec->ec_tx_mowner.mo_descr));
   1129 	strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
   1130 	    sizeof(ec->ec_rx_mowner.mo_name));
   1131 	strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
   1132 	    sizeof(ec->ec_rx_mowner.mo_descr));
   1133 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1134 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1135 	ifp->if_mowner = &ec->ec_tx_mowner;
   1136 #endif
   1137 }
   1138 
   1139 void
   1140 ether_ifdetach(struct ifnet *ifp)
   1141 {
   1142 	struct ethercom *ec = (void *) ifp;
   1143 	struct ether_multi *enm;
   1144 	int s;
   1145 
   1146 #if NBRIDGE > 0
   1147 	if (ifp->if_bridge)
   1148 		bridge_ifdetach(ifp);
   1149 #endif
   1150 
   1151 	bpf_detach(ifp);
   1152 
   1153 #if NVLAN > 0
   1154 	if (ec->ec_nvlans)
   1155 		vlan_ifdetach(ifp);
   1156 #endif
   1157 
   1158 	s = splnet();
   1159 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1160 		LIST_REMOVE(enm, enm_list);
   1161 		free(enm, M_IFMADDR);
   1162 		ec->ec_multicnt--;
   1163 	}
   1164 	splx(s);
   1165 
   1166 #if 0	/* done in if_detach() */
   1167 	if_free_sadl(ifp);
   1168 #endif
   1169 
   1170 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1171 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1172 }
   1173 
   1174 #if 0
   1175 /*
   1176  * This is for reference.  We have a table-driven version
   1177  * of the little-endian crc32 generator, which is faster
   1178  * than the double-loop.
   1179  */
   1180 uint32_t
   1181 ether_crc32_le(const uint8_t *buf, size_t len)
   1182 {
   1183 	uint32_t c, crc, carry;
   1184 	size_t i, j;
   1185 
   1186 	crc = 0xffffffffU;	/* initial value */
   1187 
   1188 	for (i = 0; i < len; i++) {
   1189 		c = buf[i];
   1190 		for (j = 0; j < 8; j++) {
   1191 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1192 			crc >>= 1;
   1193 			c >>= 1;
   1194 			if (carry)
   1195 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1196 		}
   1197 	}
   1198 
   1199 	return (crc);
   1200 }
   1201 #else
   1202 uint32_t
   1203 ether_crc32_le(const uint8_t *buf, size_t len)
   1204 {
   1205 	static const uint32_t crctab[] = {
   1206 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1207 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1208 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1209 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1210 	};
   1211 	uint32_t crc;
   1212 	size_t i;
   1213 
   1214 	crc = 0xffffffffU;	/* initial value */
   1215 
   1216 	for (i = 0; i < len; i++) {
   1217 		crc ^= buf[i];
   1218 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1219 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1220 	}
   1221 
   1222 	return (crc);
   1223 }
   1224 #endif
   1225 
   1226 uint32_t
   1227 ether_crc32_be(const uint8_t *buf, size_t len)
   1228 {
   1229 	uint32_t c, crc, carry;
   1230 	size_t i, j;
   1231 
   1232 	crc = 0xffffffffU;	/* initial value */
   1233 
   1234 	for (i = 0; i < len; i++) {
   1235 		c = buf[i];
   1236 		for (j = 0; j < 8; j++) {
   1237 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1238 			crc <<= 1;
   1239 			c >>= 1;
   1240 			if (carry)
   1241 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1242 		}
   1243 	}
   1244 
   1245 	return (crc);
   1246 }
   1247 
   1248 #ifdef INET
   1249 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1250     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1251 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1252     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1253 #endif
   1254 #ifdef INET6
   1255 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1256     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1257 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1258     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1259 #endif
   1260 
   1261 /*
   1262  * ether_aton implementation, not using a static buffer.
   1263  */
   1264 int
   1265 ether_aton_r(u_char *dest, size_t len, const char *str)
   1266 {
   1267         const u_char *cp = (const void *)str;
   1268 	u_char *ep;
   1269 
   1270 #define atox(c)	(((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
   1271 
   1272 	if (len < ETHER_ADDR_LEN)
   1273 		return ENOSPC;
   1274 
   1275 	ep = dest + ETHER_ADDR_LEN;
   1276 
   1277 	while (*cp) {
   1278                 if (!isxdigit(*cp))
   1279                         return EINVAL;
   1280 		*dest = atox(*cp);
   1281 		cp++;
   1282                 if (isxdigit(*cp)) {
   1283                         *dest = (*dest << 4) | atox(*cp);
   1284 			dest++;
   1285 			cp++;
   1286                 } else
   1287 			dest++;
   1288 		if (dest == ep)
   1289 			return *cp == '\0' ? 0 : ENAMETOOLONG;
   1290 		switch (*cp) {
   1291 		case ':':
   1292 		case '-':
   1293 		case '.':
   1294 			cp++;
   1295 			break;
   1296 		}
   1297         }
   1298 	return ENOBUFS;
   1299 }
   1300 
   1301 /*
   1302  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1303  * addresses.
   1304  */
   1305 int
   1306 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
   1307     uint8_t addrhi[ETHER_ADDR_LEN])
   1308 {
   1309 #ifdef INET
   1310 	const struct sockaddr_in *sin;
   1311 #endif /* INET */
   1312 #ifdef INET6
   1313 	const struct sockaddr_in6 *sin6;
   1314 #endif /* INET6 */
   1315 
   1316 	switch (sa->sa_family) {
   1317 
   1318 	case AF_UNSPEC:
   1319 		memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
   1320 		memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1321 		break;
   1322 
   1323 #ifdef INET
   1324 	case AF_INET:
   1325 		sin = satocsin(sa);
   1326 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1327 			/*
   1328 			 * An IP address of INADDR_ANY means listen to
   1329 			 * or stop listening to all of the Ethernet
   1330 			 * multicast addresses used for IP.
   1331 			 * (This is for the sake of IP multicast routers.)
   1332 			 */
   1333 			memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
   1334 			memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
   1335 		}
   1336 		else {
   1337 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1338 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1339 		}
   1340 		break;
   1341 #endif
   1342 #ifdef INET6
   1343 	case AF_INET6:
   1344 		sin6 = satocsin6(sa);
   1345 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1346 			/*
   1347 			 * An IP6 address of 0 means listen to or stop
   1348 			 * listening to all of the Ethernet multicast
   1349 			 * address used for IP6.
   1350 			 * (This is used for multicast routers.)
   1351 			 */
   1352 			memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
   1353 			memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
   1354 		} else {
   1355 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1356 			memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
   1357 		}
   1358 		break;
   1359 #endif
   1360 
   1361 	default:
   1362 		return EAFNOSUPPORT;
   1363 	}
   1364 	return 0;
   1365 }
   1366 
   1367 /*
   1368  * Add an Ethernet multicast address or range of addresses to the list for a
   1369  * given interface.
   1370  */
   1371 int
   1372 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
   1373 {
   1374 	struct ether_multi *enm;
   1375 	u_char addrlo[ETHER_ADDR_LEN];
   1376 	u_char addrhi[ETHER_ADDR_LEN];
   1377 	int s = splnet(), error;
   1378 
   1379 	error = ether_multiaddr(sa, addrlo, addrhi);
   1380 	if (error != 0) {
   1381 		splx(s);
   1382 		return error;
   1383 	}
   1384 
   1385 	/*
   1386 	 * Verify that we have valid Ethernet multicast addresses.
   1387 	 */
   1388 	if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
   1389 		splx(s);
   1390 		return EINVAL;
   1391 	}
   1392 	/*
   1393 	 * See if the address range is already in the list.
   1394 	 */
   1395 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1396 	if (enm != NULL) {
   1397 		/*
   1398 		 * Found it; just increment the reference count.
   1399 		 */
   1400 		++enm->enm_refcount;
   1401 		splx(s);
   1402 		return 0;
   1403 	}
   1404 	/*
   1405 	 * New address or range; malloc a new multicast record
   1406 	 * and link it into the interface's multicast list.
   1407 	 */
   1408 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
   1409 	if (enm == NULL) {
   1410 		splx(s);
   1411 		return ENOBUFS;
   1412 	}
   1413 	memcpy(enm->enm_addrlo, addrlo, 6);
   1414 	memcpy(enm->enm_addrhi, addrhi, 6);
   1415 	enm->enm_refcount = 1;
   1416 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1417 	ec->ec_multicnt++;
   1418 	splx(s);
   1419 	/*
   1420 	 * Return ENETRESET to inform the driver that the list has changed
   1421 	 * and its reception filter should be adjusted accordingly.
   1422 	 */
   1423 	return ENETRESET;
   1424 }
   1425 
   1426 /*
   1427  * Delete a multicast address record.
   1428  */
   1429 int
   1430 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
   1431 {
   1432 	struct ether_multi *enm;
   1433 	u_char addrlo[ETHER_ADDR_LEN];
   1434 	u_char addrhi[ETHER_ADDR_LEN];
   1435 	int s = splnet(), error;
   1436 
   1437 	error = ether_multiaddr(sa, addrlo, addrhi);
   1438 	if (error != 0) {
   1439 		splx(s);
   1440 		return (error);
   1441 	}
   1442 
   1443 	/*
   1444 	 * Look ur the address in our list.
   1445 	 */
   1446 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1447 	if (enm == NULL) {
   1448 		splx(s);
   1449 		return (ENXIO);
   1450 	}
   1451 	if (--enm->enm_refcount != 0) {
   1452 		/*
   1453 		 * Still some claims to this record.
   1454 		 */
   1455 		splx(s);
   1456 		return (0);
   1457 	}
   1458 	/*
   1459 	 * No remaining claims to this record; unlink and free it.
   1460 	 */
   1461 	LIST_REMOVE(enm, enm_list);
   1462 	free(enm, M_IFMADDR);
   1463 	ec->ec_multicnt--;
   1464 	splx(s);
   1465 	/*
   1466 	 * Return ENETRESET to inform the driver that the list has changed
   1467 	 * and its reception filter should be adjusted accordingly.
   1468 	 */
   1469 	return (ENETRESET);
   1470 }
   1471 
   1472 void
   1473 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
   1474 {
   1475 	ec->ec_ifflags_cb = cb;
   1476 }
   1477 
   1478 /*
   1479  * Common ioctls for Ethernet interfaces.  Note, we must be
   1480  * called at splnet().
   1481  */
   1482 int
   1483 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1484 {
   1485 	struct ethercom *ec = (void *) ifp;
   1486 	struct ifreq *ifr = (struct ifreq *)data;
   1487 	struct if_laddrreq *iflr = data;
   1488 	const struct sockaddr_dl *sdl;
   1489 	static const uint8_t zero[ETHER_ADDR_LEN];
   1490 	int error;
   1491 
   1492 	switch (cmd) {
   1493 	case SIOCINITIFADDR:
   1494 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
   1495 		    (IFF_UP|IFF_RUNNING)) {
   1496 			ifp->if_flags |= IFF_UP;
   1497 			if ((error = (*ifp->if_init)(ifp)) != 0)
   1498 				return error;
   1499 		}
   1500 #ifdef INET
   1501 		{
   1502 			struct ifaddr *ifa = (struct ifaddr *)data;
   1503 
   1504 			if (ifa->ifa_addr->sa_family == AF_INET)
   1505 				arp_ifinit(ifp, ifa);
   1506 		}
   1507 #endif /* INET */
   1508 		return 0;
   1509 
   1510 	case SIOCSIFMTU:
   1511 	    {
   1512 		int maxmtu;
   1513 
   1514 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1515 			maxmtu = ETHERMTU_JUMBO;
   1516 		else
   1517 			maxmtu = ETHERMTU;
   1518 
   1519 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1520 			return EINVAL;
   1521 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
   1522 			return error;
   1523 		else if (ifp->if_flags & IFF_UP) {
   1524 			/* Make sure the device notices the MTU change. */
   1525 			return (*ifp->if_init)(ifp);
   1526 		} else
   1527 			return 0;
   1528 	    }
   1529 
   1530 	case SIOCSIFFLAGS:
   1531 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1532 			return error;
   1533 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   1534 		case IFF_RUNNING:
   1535 			/*
   1536 			 * If interface is marked down and it is running,
   1537 			 * then stop and disable it.
   1538 			 */
   1539 			(*ifp->if_stop)(ifp, 1);
   1540 			break;
   1541 		case IFF_UP:
   1542 			/*
   1543 			 * If interface is marked up and it is stopped, then
   1544 			 * start it.
   1545 			 */
   1546 			return (*ifp->if_init)(ifp);
   1547 		case IFF_UP|IFF_RUNNING:
   1548 			error = 0;
   1549 			if (ec->ec_ifflags_cb == NULL ||
   1550 			    (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
   1551 				/*
   1552 				 * Reset the interface to pick up
   1553 				 * changes in any other flags that
   1554 				 * affect the hardware state.
   1555 				 */
   1556 				return (*ifp->if_init)(ifp);
   1557 			} else
   1558 				return error;
   1559 		case 0:
   1560 			break;
   1561 		}
   1562 		return 0;
   1563 	case SIOCADDMULTI:
   1564 		return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
   1565 	case SIOCDELMULTI:
   1566 		return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
   1567 	case SIOCSIFMEDIA:
   1568 	case SIOCGIFMEDIA:
   1569 		if (ec->ec_mii == NULL)
   1570 			return ENOTTY;
   1571 		return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
   1572 	case SIOCALIFADDR:
   1573 		sdl = satocsdl(sstocsa(&iflr->addr));
   1574 		if (sdl->sdl_family != AF_LINK)
   1575 			;
   1576 		else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
   1577 			return EINVAL;
   1578 		else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
   1579 			return EINVAL;
   1580 		/*FALLTHROUGH*/
   1581 	default:
   1582 		return ifioctl_common(ifp, cmd, data);
   1583 	}
   1584 	return 0;
   1585 }
   1586