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