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