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