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