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