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