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