Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.119
      1 /*	$NetBSD: if_ethersubr.c,v 1.119 2005/01/31 23:49:36 kim 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.119 2005/01/31 23:49:36 kim 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 		if(m->m_flags & M_PROTO1) {
    730 			m->m_flags &= ~M_PROTO1;
    731 		} else {
    732 			/* clear M_PROMISC, in case the packets comes from a vlan */
    733 			m->m_flags &= ~M_PROMISC;
    734 			m = bridge_input(ifp, m);
    735 			if (m == NULL)
    736 				return;
    737 
    738 			/*
    739 			 * Bridge has determined that the packet is for us.
    740 			 * Update our interface pointer -- we may have had
    741 			 * to "bridge" the packet locally.
    742 			 */
    743 			ifp = m->m_pkthdr.rcvif;
    744 		}
    745 	} else
    746 #endif /* NBRIDGE > 0 */
    747 	{
    748 		if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 &&
    749 		    (ifp->if_flags & IFF_PROMISC) != 0 &&
    750 		    memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
    751 			   ETHER_ADDR_LEN) != 0) {
    752 			m->m_flags |= M_PROMISC;
    753 		}
    754 	}
    755 
    756 #ifdef PFIL_HOOKS
    757 	if ((m->m_flags & M_PROMISC) == 0) {
    758 		if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
    759 			return;
    760 		if (m == NULL)
    761 			return;
    762 
    763 		eh = mtod(m, struct ether_header *);
    764 		etype = ntohs(eh->ether_type);
    765 	}
    766 #endif
    767 
    768 	/*
    769 	 * If VLANs are configured on the interface, check to
    770 	 * see if the device performed the decapsulation and
    771 	 * provided us with the tag.
    772 	 */
    773 	if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
    774 #if NVLAN > 0
    775 		/*
    776 		 * vlan_input() will either recursively call ether_input()
    777 		 * or drop the packet.
    778 		 */
    779 		vlan_input(ifp, m);
    780 #else
    781 		m_freem(m);
    782 #endif
    783 		return;
    784 	}
    785 
    786 	/*
    787 	 * Handle protocols that expect to have the Ethernet header
    788 	 * (and possibly FCS) intact.
    789 	 */
    790 	switch (etype) {
    791 #if NVLAN > 0
    792 	case ETHERTYPE_VLAN:
    793 		/*
    794 		 * vlan_input() will either recursively call ether_input()
    795 		 * or drop the packet.
    796 		 */
    797 		if (((struct ethercom *)ifp)->ec_nvlans != 0)
    798 			vlan_input(ifp, m);
    799 		else
    800 			m_freem(m);
    801 		return;
    802 #endif /* NVLAN > 0 */
    803 #if NPPPOE > 0
    804 	case ETHERTYPE_PPPOEDISC:
    805 	case ETHERTYPE_PPPOE:
    806 		if (m->m_flags & M_PROMISC) {
    807 			m_freem(m);
    808 			return;
    809 		}
    810 #ifndef PPPOE_SERVER
    811 		if (m->m_flags & (M_MCAST | M_BCAST)) {
    812 			m_freem(m);
    813 			return;
    814 		}
    815 #endif
    816 
    817 		if (etype == ETHERTYPE_PPPOEDISC)
    818 			inq = &ppoediscinq;
    819 		else
    820 			inq = &ppoeinq;
    821 		s = splnet();
    822 		if (IF_QFULL(inq)) {
    823 			IF_DROP(inq);
    824 			m_freem(m);
    825 		} else
    826 			IF_ENQUEUE(inq, m);
    827 		splx(s);
    828 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    829 		if (!callout_pending(&pppoe_softintr))
    830 			callout_reset(&pppoe_softintr, 1, pppoe_softintr_handler, NULL);
    831 #else
    832 		softintr_schedule(pppoe_softintr);
    833 #endif
    834 		return;
    835 #endif /* NPPPOE > 0 */
    836 	default:
    837 		if (m->m_flags & M_PROMISC) {
    838 			m_freem(m);
    839 			return;
    840 		}
    841 	}
    842 
    843 	/* Strip off the Ethernet header. */
    844 	m_adj(m, sizeof(struct ether_header));
    845 
    846 	/* If the CRC is still on the packet, trim it off. */
    847 	if (m->m_flags & M_HASFCS) {
    848 		m_adj(m, -ETHER_CRC_LEN);
    849 		m->m_flags &= ~M_HASFCS;
    850 	}
    851 
    852 	switch (etype) {
    853 #ifdef INET
    854 	case ETHERTYPE_IP:
    855 #ifdef GATEWAY
    856 		if (ipflow_fastforward(m))
    857 			return;
    858 #endif
    859 		schednetisr(NETISR_IP);
    860 		inq = &ipintrq;
    861 		break;
    862 
    863 	case ETHERTYPE_ARP:
    864 		schednetisr(NETISR_ARP);
    865 		inq = &arpintrq;
    866 		break;
    867 
    868 	case ETHERTYPE_REVARP:
    869 		revarpinput(m);	/* XXX queue? */
    870 		return;
    871 #endif
    872 #ifdef INET6
    873 	case ETHERTYPE_IPV6:
    874 		schednetisr(NETISR_IPV6);
    875 		inq = &ip6intrq;
    876 		break;
    877 #endif
    878 #ifdef NS
    879 	case ETHERTYPE_NS:
    880 		schednetisr(NETISR_NS);
    881 		inq = &nsintrq;
    882 		break;
    883 
    884 #endif
    885 #ifdef IPX
    886 	case ETHERTYPE_IPX:
    887 		schednetisr(NETISR_IPX);
    888 		inq = &ipxintrq;
    889 		break;
    890 #endif
    891 #ifdef NETATALK
    892         case ETHERTYPE_ATALK:
    893                 schednetisr(NETISR_ATALK);
    894                 inq = &atintrq1;
    895                 break;
    896         case ETHERTYPE_AARP:
    897 		/* probably this should be done with a NETISR as well */
    898                 aarpinput(ifp, m); /* XXX */
    899                 return;
    900 #endif /* NETATALK */
    901 	default:
    902 #if defined (ISO) || defined (LLC) || defined (NETATALK)
    903 		if (etype > ETHERMTU)
    904 			goto dropanyway;
    905 		l = mtod(m, struct llc *);
    906 		switch (l->llc_dsap) {
    907 #ifdef NETATALK
    908 		case LLC_SNAP_LSAP:
    909 			switch (l->llc_control) {
    910 			case LLC_UI:
    911 				if (l->llc_ssap != LLC_SNAP_LSAP) {
    912 					goto dropanyway;
    913 				}
    914 
    915 				if (Bcmp(&(l->llc_snap_org_code)[0],
    916 				    at_org_code, sizeof(at_org_code)) == 0 &&
    917 				    ntohs(l->llc_snap_ether_type) ==
    918 				    ETHERTYPE_ATALK) {
    919 					inq = &atintrq2;
    920 					m_adj(m, sizeof(struct llc));
    921 					schednetisr(NETISR_ATALK);
    922 					break;
    923 				}
    924 
    925 				if (Bcmp(&(l->llc_snap_org_code)[0],
    926 				    aarp_org_code,
    927 				    sizeof(aarp_org_code)) == 0 &&
    928 				    ntohs(l->llc_snap_ether_type) ==
    929 				    ETHERTYPE_AARP) {
    930 					m_adj( m, sizeof(struct llc));
    931 					aarpinput(ifp, m); /* XXX */
    932 				    return;
    933 				}
    934 
    935 			default:
    936 				goto dropanyway;
    937 			}
    938 			break;
    939 #endif /* NETATALK */
    940 #ifdef	ISO
    941 		case LLC_ISO_LSAP:
    942 			switch (l->llc_control) {
    943 			case LLC_UI:
    944 				/* LLC_UI_P forbidden in class 1 service */
    945 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
    946 				    (l->llc_ssap == LLC_ISO_LSAP)) {
    947 					/* LSAP for ISO */
    948 					if (m->m_pkthdr.len > etype)
    949 						m_adj(m, etype - m->m_pkthdr.len);
    950 					m->m_data += 3;		/* XXX */
    951 					m->m_len -= 3;		/* XXX */
    952 					m->m_pkthdr.len -= 3;	/* XXX */
    953 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
    954 					if (m == 0)
    955 						return;
    956 					*mtod(m, struct ether_header *) = *eh;
    957 #ifdef ARGO_DEBUG
    958 					if (argo_debug[D_ETHER])
    959 						printf("clnp packet");
    960 #endif
    961 					schednetisr(NETISR_ISO);
    962 					inq = &clnlintrq;
    963 					break;
    964 				}
    965 				goto dropanyway;
    966 
    967 			case LLC_XID:
    968 			case LLC_XID_P:
    969 				if(m->m_len < 6)
    970 					goto dropanyway;
    971 				l->llc_window = 0;
    972 				l->llc_fid = 9;
    973 				l->llc_class = 1;
    974 				l->llc_dsap = l->llc_ssap = 0;
    975 				/* Fall through to */
    976 			case LLC_TEST:
    977 			case LLC_TEST_P:
    978 			{
    979 				struct sockaddr sa;
    980 				struct ether_header *eh2;
    981 				int i;
    982 				u_char c = l->llc_dsap;
    983 
    984 				l->llc_dsap = l->llc_ssap;
    985 				l->llc_ssap = c;
    986 				if (m->m_flags & (M_BCAST | M_MCAST))
    987 					bcopy(LLADDR(ifp->if_sadl),
    988 					      (caddr_t)eh->ether_dhost, 6);
    989 				sa.sa_family = AF_UNSPEC;
    990 				sa.sa_len = sizeof(sa);
    991 				eh2 = (struct ether_header *)sa.sa_data;
    992 				for (i = 0; i < 6; i++) {
    993 					eh2->ether_shost[i] = c =
    994 					    eh->ether_dhost[i];
    995 					eh2->ether_dhost[i] =
    996 					    eh->ether_dhost[i] =
    997 					    eh->ether_shost[i];
    998 					eh->ether_shost[i] = c;
    999 				}
   1000 				ifp->if_output(ifp, m, &sa, NULL);
   1001 				return;
   1002 			}
   1003 			default:
   1004 				m_freem(m);
   1005 				return;
   1006 			}
   1007 			break;
   1008 #endif /* ISO */
   1009 #ifdef LLC
   1010 		case LLC_X25_LSAP:
   1011 		{
   1012 			if (m->m_pkthdr.len > etype)
   1013 				m_adj(m, etype - m->m_pkthdr.len);
   1014 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
   1015 			if (m == 0)
   1016 				return;
   1017 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
   1018 					    eh->ether_dhost, LLC_X25_LSAP, 6,
   1019 					    mtod(m, struct sdl_hdr *)))
   1020 				panic("ETHER cons addr failure");
   1021 			mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
   1022 #ifdef LLC_DEBUG
   1023 				printf("llc packet\n");
   1024 #endif /* LLC_DEBUG */
   1025 			schednetisr(NETISR_CCITT);
   1026 			inq = &llcintrq;
   1027 			break;
   1028 		}
   1029 #endif /* LLC */
   1030 		dropanyway:
   1031 		default:
   1032 			m_freem(m);
   1033 			return;
   1034 		}
   1035 #else /* ISO || LLC  || NETATALK*/
   1036 	    m_freem(m);
   1037 	    return;
   1038 #endif /* ISO || LLC || NETATALK*/
   1039 	}
   1040 
   1041 	s = splnet();
   1042 	if (IF_QFULL(inq)) {
   1043 		IF_DROP(inq);
   1044 		m_freem(m);
   1045 	} else
   1046 		IF_ENQUEUE(inq, m);
   1047 	splx(s);
   1048 }
   1049 
   1050 /*
   1051  * Convert Ethernet address to printable (loggable) representation.
   1052  */
   1053 static char digits[] = "0123456789abcdef";
   1054 char *
   1055 ether_sprintf(const u_char *ap)
   1056 {
   1057 	static char etherbuf[18];
   1058 	char *cp = etherbuf;
   1059 	int i;
   1060 
   1061 	for (i = 0; i < 6; i++) {
   1062 		*cp++ = digits[*ap >> 4];
   1063 		*cp++ = digits[*ap++ & 0xf];
   1064 		*cp++ = ':';
   1065 	}
   1066 	*--cp = 0;
   1067 	return (etherbuf);
   1068 }
   1069 
   1070 /*
   1071  * Perform common duties while attaching to interface list
   1072  */
   1073 void
   1074 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
   1075 {
   1076 	struct ethercom *ec = (struct ethercom *)ifp;
   1077 
   1078 	ifp->if_type = IFT_ETHER;
   1079 	ifp->if_addrlen = ETHER_ADDR_LEN;
   1080 	ifp->if_hdrlen = ETHER_HDR_LEN;
   1081 	ifp->if_dlt = DLT_EN10MB;
   1082 	ifp->if_mtu = ETHERMTU;
   1083 	ifp->if_output = ether_output;
   1084 	ifp->if_input = ether_input;
   1085 	if (ifp->if_baudrate == 0)
   1086 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
   1087 
   1088 	if_alloc_sadl(ifp);
   1089 	memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen);
   1090 
   1091 	LIST_INIT(&ec->ec_multiaddrs);
   1092 	ifp->if_broadcastaddr = etherbroadcastaddr;
   1093 #if NBPFILTER > 0
   1094 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
   1095 #endif
   1096 #ifdef MBUFTRACE
   1097 	strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
   1098 	    sizeof(ec->ec_tx_mowner.mo_name));
   1099 	strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
   1100 	    sizeof(ec->ec_tx_mowner.mo_descr));
   1101 	strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
   1102 	    sizeof(ec->ec_rx_mowner.mo_name));
   1103 	strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
   1104 	    sizeof(ec->ec_rx_mowner.mo_descr));
   1105 	MOWNER_ATTACH(&ec->ec_tx_mowner);
   1106 	MOWNER_ATTACH(&ec->ec_rx_mowner);
   1107 	ifp->if_mowner = &ec->ec_tx_mowner;
   1108 #endif
   1109 }
   1110 
   1111 void
   1112 ether_ifdetach(struct ifnet *ifp)
   1113 {
   1114 	struct ethercom *ec = (void *) ifp;
   1115 	struct ether_multi *enm;
   1116 	int s;
   1117 
   1118 #if NBRIDGE > 0
   1119 	if (ifp->if_bridge)
   1120 		bridge_ifdetach(ifp);
   1121 #endif
   1122 
   1123 #if NBPFILTER > 0
   1124 	bpfdetach(ifp);
   1125 #endif
   1126 
   1127 #if NVLAN > 0
   1128 	if (ec->ec_nvlans)
   1129 		vlan_ifdetach(ifp);
   1130 #endif
   1131 
   1132 	s = splnet();
   1133 	while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
   1134 		LIST_REMOVE(enm, enm_list);
   1135 		free(enm, M_IFMADDR);
   1136 		ec->ec_multicnt--;
   1137 	}
   1138 	splx(s);
   1139 
   1140 #if 0	/* done in if_detach() */
   1141 	if_free_sadl(ifp);
   1142 #endif
   1143 
   1144 	MOWNER_DETACH(&ec->ec_rx_mowner);
   1145 	MOWNER_DETACH(&ec->ec_tx_mowner);
   1146 }
   1147 
   1148 #if 0
   1149 /*
   1150  * This is for reference.  We have a table-driven version
   1151  * of the little-endian crc32 generator, which is faster
   1152  * than the double-loop.
   1153  */
   1154 u_int32_t
   1155 ether_crc32_le(const u_int8_t *buf, size_t len)
   1156 {
   1157 	u_int32_t c, crc, carry;
   1158 	size_t i, j;
   1159 
   1160 	crc = 0xffffffffU;	/* initial value */
   1161 
   1162 	for (i = 0; i < len; i++) {
   1163 		c = buf[i];
   1164 		for (j = 0; j < 8; j++) {
   1165 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
   1166 			crc >>= 1;
   1167 			c >>= 1;
   1168 			if (carry)
   1169 				crc = (crc ^ ETHER_CRC_POLY_LE);
   1170 		}
   1171 	}
   1172 
   1173 	return (crc);
   1174 }
   1175 #else
   1176 u_int32_t
   1177 ether_crc32_le(const u_int8_t *buf, size_t len)
   1178 {
   1179 	static const u_int32_t crctab[] = {
   1180 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1181 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1182 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1183 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1184 	};
   1185 	u_int32_t crc;
   1186 	size_t i;
   1187 
   1188 	crc = 0xffffffffU;	/* initial value */
   1189 
   1190 	for (i = 0; i < len; i++) {
   1191 		crc ^= buf[i];
   1192 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1193 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1194 	}
   1195 
   1196 	return (crc);
   1197 }
   1198 #endif
   1199 
   1200 u_int32_t
   1201 ether_crc32_be(const u_int8_t *buf, size_t len)
   1202 {
   1203 	u_int32_t c, crc, carry;
   1204 	size_t i, j;
   1205 
   1206 	crc = 0xffffffffU;	/* initial value */
   1207 
   1208 	for (i = 0; i < len; i++) {
   1209 		c = buf[i];
   1210 		for (j = 0; j < 8; j++) {
   1211 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
   1212 			crc <<= 1;
   1213 			c >>= 1;
   1214 			if (carry)
   1215 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
   1216 		}
   1217 	}
   1218 
   1219 	return (crc);
   1220 }
   1221 
   1222 #ifdef INET
   1223 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
   1224     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
   1225 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
   1226     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
   1227 #endif
   1228 #ifdef INET6
   1229 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
   1230     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
   1231 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
   1232     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
   1233 #endif
   1234 
   1235 /*
   1236  * Convert a sockaddr into an Ethernet address or range of Ethernet
   1237  * addresses.
   1238  */
   1239 int
   1240 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
   1241     u_int8_t addrhi[ETHER_ADDR_LEN])
   1242 {
   1243 #ifdef INET
   1244 	struct sockaddr_in *sin;
   1245 #endif /* INET */
   1246 #ifdef INET6
   1247 	struct sockaddr_in6 *sin6;
   1248 #endif /* INET6 */
   1249 
   1250 	switch (sa->sa_family) {
   1251 
   1252 	case AF_UNSPEC:
   1253 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
   1254 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1255 		break;
   1256 
   1257 #ifdef INET
   1258 	case AF_INET:
   1259 		sin = satosin(sa);
   1260 		if (sin->sin_addr.s_addr == INADDR_ANY) {
   1261 			/*
   1262 			 * An IP address of INADDR_ANY means listen to
   1263 			 * or stop listening to all of the Ethernet
   1264 			 * multicast addresses used for IP.
   1265 			 * (This is for the sake of IP multicast routers.)
   1266 			 */
   1267 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
   1268 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
   1269 		}
   1270 		else {
   1271 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
   1272 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1273 		}
   1274 		break;
   1275 #endif
   1276 #ifdef INET6
   1277 	case AF_INET6:
   1278 		sin6 = satosin6(sa);
   1279 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1280 			/*
   1281 			 * An IP6 address of 0 means listen to or stop
   1282 			 * listening to all of the Ethernet multicast
   1283 			 * address used for IP6.
   1284 			 * (This is used for multicast routers.)
   1285 			 */
   1286 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
   1287 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
   1288 		} else {
   1289 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
   1290 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
   1291 		}
   1292 		break;
   1293 #endif
   1294 
   1295 	default:
   1296 		return (EAFNOSUPPORT);
   1297 	}
   1298 	return (0);
   1299 }
   1300 
   1301 /*
   1302  * Add an Ethernet multicast address or range of addresses to the list for a
   1303  * given interface.
   1304  */
   1305 int
   1306 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
   1307 {
   1308 	struct ether_multi *enm;
   1309 	u_char addrlo[ETHER_ADDR_LEN];
   1310 	u_char addrhi[ETHER_ADDR_LEN];
   1311 	int s = splnet(), error;
   1312 
   1313 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1314 	if (error != 0) {
   1315 		splx(s);
   1316 		return (error);
   1317 	}
   1318 
   1319 	/*
   1320 	 * Verify that we have valid Ethernet multicast addresses.
   1321 	 */
   1322 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
   1323 		splx(s);
   1324 		return (EINVAL);
   1325 	}
   1326 	/*
   1327 	 * See if the address range is already in the list.
   1328 	 */
   1329 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1330 	if (enm != NULL) {
   1331 		/*
   1332 		 * Found it; just increment the reference count.
   1333 		 */
   1334 		++enm->enm_refcount;
   1335 		splx(s);
   1336 		return (0);
   1337 	}
   1338 	/*
   1339 	 * New address or range; malloc a new multicast record
   1340 	 * and link it into the interface's multicast list.
   1341 	 */
   1342 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
   1343 	if (enm == NULL) {
   1344 		splx(s);
   1345 		return (ENOBUFS);
   1346 	}
   1347 	bcopy(addrlo, enm->enm_addrlo, 6);
   1348 	bcopy(addrhi, enm->enm_addrhi, 6);
   1349 	enm->enm_refcount = 1;
   1350 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1351 	ec->ec_multicnt++;
   1352 	splx(s);
   1353 	/*
   1354 	 * Return ENETRESET to inform the driver that the list has changed
   1355 	 * and its reception filter should be adjusted accordingly.
   1356 	 */
   1357 	return (ENETRESET);
   1358 }
   1359 
   1360 /*
   1361  * Delete a multicast address record.
   1362  */
   1363 int
   1364 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
   1365 {
   1366 	struct ether_multi *enm;
   1367 	u_char addrlo[ETHER_ADDR_LEN];
   1368 	u_char addrhi[ETHER_ADDR_LEN];
   1369 	int s = splnet(), error;
   1370 
   1371 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1372 	if (error != 0) {
   1373 		splx(s);
   1374 		return (error);
   1375 	}
   1376 
   1377 	/*
   1378 	 * Look ur the address in our list.
   1379 	 */
   1380 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1381 	if (enm == NULL) {
   1382 		splx(s);
   1383 		return (ENXIO);
   1384 	}
   1385 	if (--enm->enm_refcount != 0) {
   1386 		/*
   1387 		 * Still some claims to this record.
   1388 		 */
   1389 		splx(s);
   1390 		return (0);
   1391 	}
   1392 	/*
   1393 	 * No remaining claims to this record; unlink and free it.
   1394 	 */
   1395 	LIST_REMOVE(enm, enm_list);
   1396 	free(enm, M_IFMADDR);
   1397 	ec->ec_multicnt--;
   1398 	splx(s);
   1399 	/*
   1400 	 * Return ENETRESET to inform the driver that the list has changed
   1401 	 * and its reception filter should be adjusted accordingly.
   1402 	 */
   1403 	return (ENETRESET);
   1404 }
   1405 
   1406 /*
   1407  * Common ioctls for Ethernet interfaces.  Note, we must be
   1408  * called at splnet().
   1409  */
   1410 int
   1411 ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1412 {
   1413 	struct ethercom *ec = (void *) ifp;
   1414 	struct ifreq *ifr = (struct ifreq *)data;
   1415 	struct ifaddr *ifa = (struct ifaddr *)data;
   1416 	int error = 0;
   1417 
   1418 	switch (cmd) {
   1419 	case SIOCSIFADDR:
   1420 		ifp->if_flags |= IFF_UP;
   1421 		switch (ifa->ifa_addr->sa_family) {
   1422 		case AF_LINK:
   1423 		    {
   1424 			struct sockaddr_dl *sdl =
   1425 			    (struct sockaddr_dl *) ifa->ifa_addr;
   1426 
   1427 			if (sdl->sdl_type != IFT_ETHER ||
   1428 			    sdl->sdl_alen != ifp->if_addrlen) {
   1429 				error = EINVAL;
   1430 				break;
   1431 			}
   1432 
   1433 			memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
   1434 			    ifp->if_addrlen);
   1435 
   1436 			/* Set new address. */
   1437 			error = (*ifp->if_init)(ifp);
   1438 			break;
   1439 		    }
   1440 #ifdef INET
   1441 		case AF_INET:
   1442 			if ((ifp->if_flags & IFF_RUNNING) == 0 &&
   1443 			    (error = (*ifp->if_init)(ifp)) != 0)
   1444 				break;
   1445 			arp_ifinit(ifp, ifa);
   1446 			break;
   1447 #endif /* INET */
   1448 #ifdef NS
   1449 		case AF_NS:
   1450 		    {
   1451 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1452 
   1453 			if (ns_nullhost(*ina))
   1454 				ina->x_host = *(union ns_host *)
   1455 				    LLADDR(ifp->if_sadl);
   1456 			else
   1457 				memcpy(LLADDR(ifp->if_sadl),
   1458 				    ina->x_host.c_host, ifp->if_addrlen);
   1459 			/* Set new address. */
   1460 			error = (*ifp->if_init)(ifp);
   1461 			break;
   1462 		    }
   1463 #endif /* NS */
   1464 		default:
   1465 			if ((ifp->if_flags & IFF_RUNNING) == 0)
   1466 				error = (*ifp->if_init)(ifp);
   1467 			break;
   1468 		}
   1469 		break;
   1470 
   1471 	case SIOCGIFADDR:
   1472 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
   1473 		    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1474 		break;
   1475 
   1476 	case SIOCSIFMTU:
   1477 	    {
   1478 		int maxmtu;
   1479 
   1480 		if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
   1481 			maxmtu = ETHERMTU_JUMBO;
   1482 		else
   1483 			maxmtu = ETHERMTU;
   1484 
   1485 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
   1486 			error = EINVAL;
   1487 		else {
   1488 			ifp->if_mtu = ifr->ifr_mtu;
   1489 
   1490 			/* Make sure the device notices the MTU change. */
   1491 			if (ifp->if_flags & IFF_UP)
   1492 				error = (*ifp->if_init)(ifp);
   1493 		}
   1494 		break;
   1495 	    }
   1496 
   1497 	case SIOCSIFFLAGS:
   1498 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
   1499 			/*
   1500 			 * If interface is marked down and it is running,
   1501 			 * then stop and disable it.
   1502 			 */
   1503 			(*ifp->if_stop)(ifp, 1);
   1504 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
   1505 			/*
   1506 			 * If interface is marked up and it is stopped, then
   1507 			 * start it.
   1508 			 */
   1509 			error = (*ifp->if_init)(ifp);
   1510 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1511 			/*
   1512 			 * Reset the interface to pick up changes in any other
   1513 			 * flags that affect the hardware state.
   1514 			 */
   1515 			error = (*ifp->if_init)(ifp);
   1516 		}
   1517 		break;
   1518 
   1519 	case SIOCADDMULTI:
   1520 		error = ether_addmulti(ifr, ec);
   1521 		break;
   1522 
   1523 	case SIOCDELMULTI:
   1524 		error = ether_delmulti(ifr, ec);
   1525 		break;
   1526 
   1527 	default:
   1528 		error = ENOTTY;
   1529 	}
   1530 
   1531 	return (error);
   1532 }
   1533