Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.62
      1 /*	$NetBSD: if_ethersubr.c,v 1.62 2000/10/01 23:43:44 thorpej 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. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)if_ethersubr.c	8.2 (Berkeley) 4/4/96
     65  */
     66 
     67 #include "opt_inet.h"
     68 #include "opt_atalk.h"
     69 #include "opt_ccitt.h"
     70 #include "opt_llc.h"
     71 #include "opt_iso.h"
     72 #include "opt_ns.h"
     73 #include "opt_gateway.h"
     74 #include "vlan.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/malloc.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/protosw.h>
     82 #include <sys/socket.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/errno.h>
     85 #include <sys/syslog.h>
     86 
     87 #include <machine/cpu.h>
     88 
     89 #include <net/if.h>
     90 #include <net/netisr.h>
     91 #include <net/route.h>
     92 #include <net/if_llc.h>
     93 #include <net/if_dl.h>
     94 #include <net/if_types.h>
     95 
     96 #include <net/if_ether.h>
     97 #if NVLAN > 0
     98 #include <net/if_vlanvar.h>
     99 #endif
    100 
    101 #include <netinet/in.h>
    102 #ifdef INET
    103 #include <netinet/in_var.h>
    104 #endif
    105 #include <netinet/if_inarp.h>
    106 
    107 #ifdef INET6
    108 #ifndef INET
    109 #include <netinet/in.h>
    110 #endif
    111 #include <netinet6/in6_var.h>
    112 #include <netinet6/nd6.h>
    113 #endif
    114 
    115 #ifdef NS
    116 #include <netns/ns.h>
    117 #include <netns/ns_if.h>
    118 #endif
    119 
    120 #ifdef IPX
    121 #include <netipx/ipx.h>
    122 #include <netipx/ipx_if.h>
    123 #endif
    124 
    125 #ifdef ISO
    126 #include <netiso/argo_debug.h>
    127 #include <netiso/iso.h>
    128 #include <netiso/iso_var.h>
    129 #include <netiso/iso_snpac.h>
    130 #endif
    131 
    132 #ifdef LLC
    133 #include <netccitt/dll.h>
    134 #include <netccitt/llc_var.h>
    135 #endif
    136 
    137 #if defined(LLC) && defined(CCITT)
    138 extern struct ifqueue pkintrq;
    139 #endif
    140 
    141 #ifdef NETATALK
    142 #include <netatalk/at.h>
    143 #include <netatalk/at_var.h>
    144 #include <netatalk/at_extern.h>
    145 
    146 #define llc_snap_org_code llc_un.type_snap.org_code
    147 #define llc_snap_ether_type llc_un.type_snap.ether_type
    148 
    149 extern u_char	at_org_code[3];
    150 extern u_char	aarp_org_code[3];
    151 #endif /* NETATALK */
    152 
    153 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    154 #define senderr(e) { error = (e); goto bad;}
    155 
    156 #define SIN(x) ((struct sockaddr_in *)x)
    157 
    158 static	int ether_output __P((struct ifnet *, struct mbuf *,
    159 	    struct sockaddr *, struct rtentry *));
    160 static	void ether_input __P((struct ifnet *, struct mbuf *));
    161 
    162 /*
    163  * Ethernet output routine.
    164  * Encapsulate a packet of type family for the local net.
    165  * Assumes that ifp is actually pointer to ethercom structure.
    166  */
    167 static int
    168 ether_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
    169 	struct rtentry *rt0)
    170 {
    171 	u_int16_t etype = 0;
    172 	int s, error = 0, hdrcmplt = 0;
    173  	u_char esrc[6], edst[6];
    174 	struct mbuf *m = m0;
    175 	struct rtentry *rt;
    176 	struct mbuf *mcopy = (struct mbuf *)0;
    177 	struct ether_header *eh;
    178 #ifdef INET
    179 	struct arphdr *ah;
    180 #endif /* INET */
    181 #ifdef NETATALK
    182 	struct at_ifaddr *aa;
    183 #endif /* NETATALK */
    184 
    185 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    186 		senderr(ENETDOWN);
    187 	ifp->if_lastchange = time;
    188 	if ((rt = rt0) != NULL) {
    189 		if ((rt->rt_flags & RTF_UP) == 0) {
    190 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
    191 				rt->rt_refcnt--;
    192 				if (rt->rt_ifp != ifp)
    193 					return (*rt->rt_ifp->if_output)
    194 							(ifp, m0, dst, rt);
    195 			} else
    196 				senderr(EHOSTUNREACH);
    197 		}
    198 		if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
    199 			if (rt->rt_gwroute == 0)
    200 				goto lookup;
    201 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    202 				rtfree(rt); rt = rt0;
    203 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    204 				if ((rt = rt->rt_gwroute) == 0)
    205 					senderr(EHOSTUNREACH);
    206 				/* the "G" test below also prevents rt == rt0 */
    207 				if ((rt->rt_flags & RTF_GATEWAY) ||
    208 				    (rt->rt_ifp != ifp)) {
    209 					rt->rt_refcnt--;
    210 					rt0->rt_gwroute = 0;
    211 					senderr(EHOSTUNREACH);
    212 				}
    213 			}
    214 		}
    215 		if (rt->rt_flags & RTF_REJECT)
    216 			if (rt->rt_rmx.rmx_expire == 0 ||
    217 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    218 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    219 	}
    220 	switch (dst->sa_family) {
    221 
    222 #ifdef INET
    223 	case AF_INET:
    224 		if (m->m_flags & M_BCAST)
    225                 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
    226 				sizeof(edst));
    227 
    228 		else if (m->m_flags & M_MCAST) {
    229 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr,
    230 			    (caddr_t)edst)
    231 
    232 		} else if (!arpresolve(ifp, rt, m, dst, edst))
    233 			return (0);	/* if not yet resolved */
    234 		/* If broadcasting on a simplex interface, loopback a copy */
    235 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    236 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    237 		etype = htons(ETHERTYPE_IP);
    238 		break;
    239 
    240 	case AF_ARP:
    241 		ah = mtod(m, struct arphdr *);
    242 		if (m->m_flags & M_BCAST)
    243                 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)edst,
    244 				sizeof(edst));
    245 		else
    246 			bcopy((caddr_t)ar_tha(ah),
    247 				(caddr_t)edst, sizeof(edst));
    248 
    249 		ah->ar_hrd = htons(ARPHRD_ETHER);
    250 
    251 		switch(ntohs(ah->ar_op)) {
    252 		case ARPOP_REVREQUEST:
    253 		case ARPOP_REVREPLY:
    254 			etype = htons(ETHERTYPE_REVARP);
    255 			break;
    256 
    257 		case ARPOP_REQUEST:
    258 		case ARPOP_REPLY:
    259 		default:
    260 			etype = htons(ETHERTYPE_ARP);
    261 		}
    262 
    263 		break;
    264 #endif
    265 #ifdef INET6
    266 	case AF_INET6:
    267 #ifdef OLDIP6OUTPUT
    268 		if (!nd6_resolve(ifp, rt, m, dst, (u_char *)edst))
    269 			return(0);	/* if not yet resolves */
    270 #else
    271 		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)){
    272 			/* this must be impossible, so we bark */
    273 			printf("nd6_storelladdr failed\n");
    274 			return(0);
    275 		}
    276 #endif /* OLDIP6OUTPUT */
    277 		etype = htons(ETHERTYPE_IPV6);
    278 		break;
    279 #endif
    280 #ifdef NETATALK
    281     case AF_APPLETALK:
    282 		if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
    283 #ifdef NETATALKDEBUG
    284 			printf("aarpresolv failed\n");
    285 #endif /* NETATALKDEBUG */
    286 			return (0);
    287 		}
    288 		/*
    289 		 * ifaddr is the first thing in at_ifaddr
    290 		 */
    291 		aa = (struct at_ifaddr *) at_ifawithnet(
    292 		    (struct sockaddr_at *)dst, ifp);
    293 		if (aa == NULL)
    294 		    goto bad;
    295 
    296 		/*
    297 		 * In the phase 2 case, we need to prepend an mbuf for the
    298 		 * llc header.  Since we must preserve the value of m,
    299 		 * which is passed to us by value, we m_copy() the first
    300 		 * mbuf, and use it for our llc header.
    301 		 */
    302 		if (aa->aa_flags & AFA_PHASE2) {
    303 			struct llc llc;
    304 
    305 			M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    306 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
    307 			llc.llc_control = LLC_UI;
    308 			bcopy(at_org_code, llc.llc_snap_org_code,
    309 			    sizeof(llc.llc_snap_org_code));
    310 			llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
    311 			bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
    312 		} else {
    313 			etype = htons(ETHERTYPE_ATALK);
    314 		}
    315 		break;
    316 #endif /* NETATALK */
    317 #ifdef NS
    318 	case AF_NS:
    319 		etype = htons(ETHERTYPE_NS);
    320  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
    321 		    (caddr_t)edst, sizeof (edst));
    322 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
    323 			return (looutput(ifp, m, dst, rt));
    324 		/* If broadcasting on a simplex interface, loopback a copy */
    325 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    326 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    327 		break;
    328 #endif
    329 #ifdef IPX
    330 	case AF_IPX:
    331 		etype = htons(ETHERTYPE_IPX);
    332  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
    333 		    (caddr_t)edst, sizeof (edst));
    334 		/* If broadcasting on a simplex interface, loopback a copy */
    335 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    336 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    337 		break;
    338 #endif
    339 #ifdef	ISO
    340 	case AF_ISO: {
    341 		int	snpalen;
    342 		struct	llc *l;
    343 		struct sockaddr_dl *sdl;
    344 
    345 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
    346 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
    347 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
    348 		} else {
    349 			error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
    350 						(char *)edst, &snpalen);
    351 			if (error)
    352 				goto bad; /* Not Resolved */
    353 		}
    354 		/* If broadcasting on a simplex interface, loopback a copy */
    355 		if (*edst & 1)
    356 			m->m_flags |= (M_BCAST|M_MCAST);
    357 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
    358 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    359 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    360 			if (mcopy) {
    361 				eh = mtod(mcopy, struct ether_header *);
    362 				bcopy((caddr_t)edst,
    363 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    364 				bcopy(LLADDR(ifp->if_sadl),
    365 				      (caddr_t)eh->ether_shost, sizeof (edst));
    366 			}
    367 		}
    368 		M_PREPEND(m, 3, M_DONTWAIT);
    369 		if (m == NULL)
    370 			return (0);
    371 		l = mtod(m, struct llc *);
    372 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
    373 		l->llc_control = LLC_UI;
    374 #ifdef ARGO_DEBUG
    375 		if (argo_debug[D_ETHER]) {
    376 			int i;
    377 			printf("unoutput: sending pkt to: ");
    378 			for (i=0; i<6; i++)
    379 				printf("%x ", edst[i] & 0xff);
    380 			printf("\n");
    381 		}
    382 #endif
    383 		} break;
    384 #endif /* ISO */
    385 #ifdef	LLC
    386 /*	case AF_NSAP: */
    387 	case AF_CCITT: {
    388 		struct sockaddr_dl *sdl =
    389 			(struct sockaddr_dl *) rt -> rt_gateway;
    390 
    391 		if (sdl && sdl->sdl_family == AF_LINK
    392 		    && sdl->sdl_alen > 0) {
    393 			bcopy(LLADDR(sdl), (char *)edst,
    394 				sizeof(edst));
    395 		} else goto bad; /* Not a link interface ? Funny ... */
    396 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
    397 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    398 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    399 			if (mcopy) {
    400 				eh = mtod(mcopy, struct ether_header *);
    401 				bcopy((caddr_t)edst,
    402 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    403 				bcopy(LLADDR(ifp->if_sadl),
    404 				      (caddr_t)eh->ether_shost, sizeof (edst));
    405 			}
    406 		}
    407 #ifdef LLC_DEBUG
    408 		{
    409 			int i;
    410 			struct llc *l = mtod(m, struct llc *);
    411 
    412 			printf("ether_output: sending LLC2 pkt to: ");
    413 			for (i=0; i<6; i++)
    414 				printf("%x ", edst[i] & 0xff);
    415 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
    416 			    m->m_pkthdr.len, l->llc_dsap & 0xff, l->llc_ssap &0xff,
    417 			    l->llc_control & 0xff);
    418 
    419 		}
    420 #endif /* LLC_DEBUG */
    421 		} break;
    422 #endif /* LLC */
    423 
    424 	case pseudo_AF_HDRCMPLT:
    425 		hdrcmplt = 1;
    426 		eh = (struct ether_header *)dst->sa_data;
    427 		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, sizeof (esrc));
    428 		/* FALLTHROUGH */
    429 
    430 	case AF_UNSPEC:
    431 		eh = (struct ether_header *)dst->sa_data;
    432  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
    433 		/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
    434 		etype = eh->ether_type;
    435 		break;
    436 
    437 	default:
    438 		printf("%s: can't handle af%d\n", ifp->if_xname,
    439 			dst->sa_family);
    440 		senderr(EAFNOSUPPORT);
    441 	}
    442 
    443 	if (mcopy)
    444 		(void) looutput(ifp, mcopy, dst, rt);
    445 
    446 	/* If no ether type is set, this must be a 802.2 formatted packet.
    447 	 */
    448 	if (etype == 0)
    449 		etype = htons(m->m_pkthdr.len);
    450 	/*
    451 	 * Add local net header.  If no space in first mbuf,
    452 	 * allocate another.
    453 	 */
    454 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
    455 	if (m == 0)
    456 		senderr(ENOBUFS);
    457 	eh = mtod(m, struct ether_header *);
    458 	bcopy((caddr_t)&etype,(caddr_t)&eh->ether_type,
    459 		sizeof(eh->ether_type));
    460  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
    461 	if (hdrcmplt)
    462 		bcopy((caddr_t)esrc, (caddr_t)eh->ether_shost,
    463 		    sizeof(eh->ether_shost));
    464 	else
    465 	 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)eh->ether_shost,
    466 		    sizeof(eh->ether_shost));
    467 	s = splimp();
    468 	/*
    469 	 * Queue message on interface, and start output if interface
    470 	 * not yet active.
    471 	 */
    472 	if (IF_QFULL(&ifp->if_snd)) {
    473 		IF_DROP(&ifp->if_snd);
    474 		splx(s);
    475 		senderr(ENOBUFS);
    476 	}
    477 	ifp->if_obytes += m->m_pkthdr.len;
    478 	if (m->m_flags & M_MCAST)
    479 		ifp->if_omcasts++;
    480 	IF_ENQUEUE(&ifp->if_snd, m);
    481 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    482 		(*ifp->if_start)(ifp);
    483 	splx(s);
    484 	return (error);
    485 
    486 bad:
    487 	if (m)
    488 		m_freem(m);
    489 	return (error);
    490 }
    491 
    492 /*
    493  * Process a received Ethernet packet;
    494  * the packet is in the mbuf chain m with
    495  * the ether header.
    496  */
    497 static void
    498 ether_input(struct ifnet *ifp, struct mbuf *m)
    499 {
    500 	struct ifqueue *inq;
    501 	u_int16_t etype;
    502 	int s;
    503 	struct ether_header *eh;
    504 #if defined (ISO) || defined (LLC) || defined(NETATALK)
    505 	struct llc *l;
    506 #endif
    507 
    508 	if ((ifp->if_flags & IFF_UP) == 0) {
    509 		m_freem(m);
    510 		return;
    511 	}
    512 
    513 	eh = mtod(m, struct ether_header *);
    514 
    515 	ifp->if_lastchange = time;
    516 	ifp->if_ibytes += m->m_pkthdr.len;
    517 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
    518 		if (memcmp(etherbroadcastaddr,
    519 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
    520 			m->m_flags |= M_BCAST;
    521 		else
    522 			m->m_flags |= M_MCAST;
    523 		ifp->if_imcasts++;
    524 	} else if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    525 		   memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
    526 			  ETHER_ADDR_LEN) != 0) {
    527 		m_freem(m);
    528 		return;
    529 	}
    530 
    531 	etype = ntohs(eh->ether_type);
    532 
    533 	/*
    534 	 * Handle protocols that expect to have the Ethernet header
    535 	 * (and possibly FCS) intact.
    536 	 */
    537 	switch (etype) {
    538 #if NVLAN > 0
    539 	case ETHERTYPE_VLAN:
    540 		/*
    541 		 * vlan_input() will either recursively call ether_input()
    542 		 * or drop the packet.
    543 		 */
    544 		vlan_input(ifp, m);
    545 		return;
    546 #endif /* NVLAN > 0 */
    547 	default:
    548 		/* Nothing. */
    549 	}
    550 
    551 	/* Strip off the Ethernet header. */
    552 	m_adj(m, sizeof(struct ether_header));
    553 
    554 	/* If the CRC is still on the packet, trim it off. */
    555 	if (m->m_flags & M_HASFCS)
    556 		m_adj(m, -ETHER_CRC_LEN);
    557 
    558 	switch (etype) {
    559 #ifdef INET
    560 	case ETHERTYPE_IP:
    561 #ifdef GATEWAY
    562 		if (ipflow_fastforward(m))
    563 			return;
    564 #endif
    565 		schednetisr(NETISR_IP);
    566 		inq = &ipintrq;
    567 		break;
    568 
    569 	case ETHERTYPE_ARP:
    570 		schednetisr(NETISR_ARP);
    571 		inq = &arpintrq;
    572 		break;
    573 
    574 	case ETHERTYPE_REVARP:
    575 		revarpinput(m);	/* XXX queue? */
    576 		return;
    577 #endif
    578 #ifdef INET6
    579 	case ETHERTYPE_IPV6:
    580 		schednetisr(NETISR_IPV6);
    581 		inq = &ip6intrq;
    582 		break;
    583 #endif
    584 #ifdef NS
    585 	case ETHERTYPE_NS:
    586 		schednetisr(NETISR_NS);
    587 		inq = &nsintrq;
    588 		break;
    589 
    590 #endif
    591 #ifdef IPX
    592 	case ETHERTYPE_IPX:
    593 		schednetisr(NETISR_IPX);
    594 		inq = &ipxintrq;
    595 		break;
    596 #endif
    597 #ifdef NETATALK
    598         case ETHERTYPE_ATALK:
    599                 schednetisr(NETISR_ATALK);
    600                 inq = &atintrq1;
    601                 break;
    602         case ETHERTYPE_AARP:
    603 		/* probably this should be done with a NETISR as well */
    604                 aarpinput(ifp, m); /* XXX */
    605                 return;
    606 #endif /* NETATALK */
    607 	default:
    608 #if defined (ISO) || defined (LLC) || defined (NETATALK)
    609 		if (etype > ETHERMTU)
    610 			goto dropanyway;
    611 		l = mtod(m, struct llc *);
    612 		switch (l->llc_dsap) {
    613 #ifdef NETATALK
    614 		case LLC_SNAP_LSAP:
    615 			switch (l->llc_control) {
    616 			case LLC_UI:
    617 				if (l->llc_ssap != LLC_SNAP_LSAP) {
    618 					goto dropanyway;
    619 				}
    620 
    621 				if (Bcmp(&(l->llc_snap_org_code)[0],
    622 				    at_org_code, sizeof(at_org_code)) == 0 &&
    623 				    ntohs(l->llc_snap_ether_type) ==
    624 				    ETHERTYPE_ATALK) {
    625 					inq = &atintrq2;
    626 					m_adj(m, sizeof(struct llc));
    627 					schednetisr(NETISR_ATALK);
    628 					break;
    629 				}
    630 
    631 				if (Bcmp(&(l->llc_snap_org_code)[0],
    632 				    aarp_org_code,
    633 				    sizeof(aarp_org_code)) == 0 &&
    634 				    ntohs(l->llc_snap_ether_type) ==
    635 				    ETHERTYPE_AARP) {
    636 					m_adj( m, sizeof(struct llc));
    637 					aarpinput(ifp, m); /* XXX */
    638 				    return;
    639 				}
    640 
    641 			default:
    642 				goto dropanyway;
    643 			}
    644 			break;
    645 #endif /* NETATALK */
    646 #ifdef	ISO
    647 		case LLC_ISO_LSAP:
    648 			switch (l->llc_control) {
    649 			case LLC_UI:
    650 				/* LLC_UI_P forbidden in class 1 service */
    651 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
    652 				    (l->llc_ssap == LLC_ISO_LSAP)) {
    653 					/* LSAP for ISO */
    654 					if (m->m_pkthdr.len > etype)
    655 						m_adj(m, etype - m->m_pkthdr.len);
    656 					m->m_data += 3;		/* XXX */
    657 					m->m_len -= 3;		/* XXX */
    658 					m->m_pkthdr.len -= 3;	/* XXX */
    659 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
    660 					if (m == 0)
    661 						return;
    662 					*mtod(m, struct ether_header *) = *eh;
    663 #ifdef ARGO_DEBUG
    664 					if (argo_debug[D_ETHER])
    665 						printf("clnp packet");
    666 #endif
    667 					schednetisr(NETISR_ISO);
    668 					inq = &clnlintrq;
    669 					break;
    670 				}
    671 				goto dropanyway;
    672 
    673 			case LLC_XID:
    674 			case LLC_XID_P:
    675 				if(m->m_len < 6)
    676 					goto dropanyway;
    677 				l->llc_window = 0;
    678 				l->llc_fid = 9;
    679 				l->llc_class = 1;
    680 				l->llc_dsap = l->llc_ssap = 0;
    681 				/* Fall through to */
    682 			case LLC_TEST:
    683 			case LLC_TEST_P:
    684 			{
    685 				struct sockaddr sa;
    686 				struct ether_header *eh2;
    687 				int i;
    688 				u_char c = l->llc_dsap;
    689 
    690 				l->llc_dsap = l->llc_ssap;
    691 				l->llc_ssap = c;
    692 				if (m->m_flags & (M_BCAST | M_MCAST))
    693 					bcopy(LLADDR(ifp->if_sadl),
    694 					      (caddr_t)eh->ether_dhost, 6);
    695 				sa.sa_family = AF_UNSPEC;
    696 				sa.sa_len = sizeof(sa);
    697 				eh2 = (struct ether_header *)sa.sa_data;
    698 				for (i = 0; i < 6; i++) {
    699 					eh2->ether_shost[i] = c =
    700 					    eh->ether_dhost[i];
    701 					eh2->ether_dhost[i] =
    702 					    eh->ether_dhost[i] =
    703 					    eh->ether_shost[i];
    704 					eh->ether_shost[i] = c;
    705 				}
    706 				ifp->if_output(ifp, m, &sa, NULL);
    707 				return;
    708 			}
    709 			default:
    710 				m_freem(m);
    711 				return;
    712 			}
    713 			break;
    714 #endif /* ISO */
    715 #ifdef LLC
    716 		case LLC_X25_LSAP:
    717 		{
    718 			if (m->m_pkthdr.len > etype)
    719 				m_adj(m, etype - m->m_pkthdr.len);
    720 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
    721 			if (m == 0)
    722 				return;
    723 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
    724 					    eh->ether_dhost, LLC_X25_LSAP, 6,
    725 					    mtod(m, struct sdl_hdr *)))
    726 				panic("ETHER cons addr failure");
    727 			mtod(m, struct sdl_hdr *)->sdlhdr_len = etype;
    728 #ifdef LLC_DEBUG
    729 				printf("llc packet\n");
    730 #endif /* LLC_DEBUG */
    731 			schednetisr(NETISR_CCITT);
    732 			inq = &llcintrq;
    733 			break;
    734 		}
    735 #endif /* LLC */
    736 		dropanyway:
    737 		default:
    738 			m_freem(m);
    739 			return;
    740 		}
    741 #else /* ISO || LLC  || NETATALK*/
    742 	    m_freem(m);
    743 	    return;
    744 #endif /* ISO || LLC || NETATALK*/
    745 	}
    746 
    747 	s = splimp();
    748 	if (IF_QFULL(inq)) {
    749 		IF_DROP(inq);
    750 		m_freem(m);
    751 	} else
    752 		IF_ENQUEUE(inq, m);
    753 	splx(s);
    754 }
    755 
    756 /*
    757  * Convert Ethernet address to printable (loggable) representation.
    758  */
    759 static char digits[] = "0123456789abcdef";
    760 char *
    761 ether_sprintf(const u_char *ap)
    762 {
    763 	static char etherbuf[18];
    764 	char *cp = etherbuf;
    765 	int i;
    766 
    767 	for (i = 0; i < 6; i++) {
    768 		*cp++ = digits[*ap >> 4];
    769 		*cp++ = digits[*ap++ & 0xf];
    770 		*cp++ = ':';
    771 	}
    772 	*--cp = 0;
    773 	return (etherbuf);
    774 }
    775 
    776 /*
    777  * Perform common duties while attaching to interface list
    778  */
    779 void
    780 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
    781 {
    782 	struct sockaddr_dl *sdl;
    783 
    784 	ifp->if_type = IFT_ETHER;
    785 	ifp->if_addrlen = 6;
    786 	ifp->if_hdrlen = 14;
    787 	ifp->if_mtu = ETHERMTU;
    788 	ifp->if_output = ether_output;
    789 	ifp->if_input = ether_input;
    790 	if (ifp->if_baudrate == 0)
    791 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
    792 	if ((sdl = ifp->if_sadl) &&
    793 	    sdl->sdl_family == AF_LINK) {
    794 		sdl->sdl_type = IFT_ETHER;
    795 		sdl->sdl_alen = ifp->if_addrlen;
    796 		bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
    797 	}
    798 	LIST_INIT(&((struct ethercom *)ifp)->ec_multiaddrs);
    799 	ifp->if_broadcastaddr = etherbroadcastaddr;
    800 }
    801 
    802 void
    803 ether_ifdetach(struct ifnet *ifp)
    804 {
    805 
    806 	/* Nothing. */
    807 }
    808 
    809 #if 0
    810 /*
    811  * This is for reference.  We have a table-driven version
    812  * of the little-endian crc32 generator, which is faster
    813  * than the double-loop.
    814  */
    815 u_int32_t
    816 ether_crc32_le(const u_int8_t *buf, size_t len)
    817 {
    818 	u_int32_t c, crc, carry;
    819 	size_t i, j;
    820 
    821 	crc = 0xffffffffU;	/* initial value */
    822 
    823 	for (i = 0; i < len; i++) {
    824 		c = buf[i];
    825 		for (j = 0; j < 8; j++) {
    826 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
    827 			crc >>= 1;
    828 			c >>= 1;
    829 			if (carry)
    830 				crc = (crc ^ ETHER_CRC_POLY_LE);
    831 		}
    832 	}
    833 
    834 	return (crc);
    835 }
    836 #else
    837 u_int32_t
    838 ether_crc32_le(const u_int8_t *buf, size_t len)
    839 {
    840 	static const u_int32_t crctab[] = {
    841 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    842 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    843 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    844 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
    845 	};
    846 	u_int32_t crc;
    847 	int i;
    848 
    849 	crc = 0xffffffffU;	/* initial value */
    850 
    851 	for (i = 0; i < len; i++) {
    852 		crc ^= buf[i];
    853 		crc = (crc >> 4) ^ crctab[crc & 0xf];
    854 		crc = (crc >> 4) ^ crctab[crc & 0xf];
    855 	}
    856 
    857 	return (crc);
    858 }
    859 #endif
    860 
    861 u_int32_t
    862 ether_crc32_be(const u_int8_t *buf, size_t len)
    863 {
    864 	u_int32_t c, crc, carry;
    865 	size_t i, j;
    866 
    867 	crc = 0xffffffffU;	/* initial value */
    868 
    869 	for (i = 0; i < len; i++) {
    870 		c = buf[i];
    871 		for (j = 0; j < 8; j++) {
    872 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
    873 			crc <<= 1;
    874 			c >>= 1;
    875 			if (carry)
    876 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
    877 		}
    878 	}
    879 
    880 	return (crc);
    881 }
    882 
    883 #ifdef INET
    884 u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
    885 u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
    886 #endif
    887 #ifdef INET6
    888 u_char	ether_ip6multicast_min[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
    889 u_char	ether_ip6multicast_max[6] = { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
    890 #endif
    891 
    892 /*
    893  * Convert a sockaddr into an Ethernet address or range of Ethernet
    894  * addresses.
    895  */
    896 int
    897 ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
    898     u_int8_t addrhi[ETHER_ADDR_LEN])
    899 {
    900 #ifdef INET
    901 	struct sockaddr_in *sin;
    902 #endif /* INET */
    903 #ifdef INET6
    904 	struct sockaddr_in6 *sin6;
    905 #endif /* INET6 */
    906 
    907 	switch (sa->sa_family) {
    908 
    909 	case AF_UNSPEC:
    910 		bcopy(sa->sa_data, addrlo, ETHER_ADDR_LEN);
    911 		bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
    912 		break;
    913 
    914 #ifdef INET
    915 	case AF_INET:
    916 		sin = satosin(sa);
    917 		if (sin->sin_addr.s_addr == INADDR_ANY) {
    918 			/*
    919 			 * An IP address of INADDR_ANY means listen to
    920 			 * or stop listening to all of the Ethernet
    921 			 * multicast addresses used for IP.
    922 			 * (This is for the sake of IP multicast routers.)
    923 			 */
    924 			bcopy(ether_ipmulticast_min, addrlo, ETHER_ADDR_LEN);
    925 			bcopy(ether_ipmulticast_max, addrhi, ETHER_ADDR_LEN);
    926 		}
    927 		else {
    928 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
    929 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
    930 		}
    931 		break;
    932 #endif
    933 #ifdef INET6
    934 	case AF_INET6:
    935 		sin6 = satosin6(sa);
    936 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    937 			/*
    938 			 * An IP6 address of 0 means listen to or stop
    939 			 * listening to all of the Ethernet multicast
    940 			 * address used for IP6.
    941 			 * (This is used for multicast routers.)
    942 			 */
    943 			bcopy(ether_ip6multicast_min, addrlo, ETHER_ADDR_LEN);
    944 			bcopy(ether_ip6multicast_max, addrhi, ETHER_ADDR_LEN);
    945 		} else {
    946 			ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
    947 			bcopy(addrlo, addrhi, ETHER_ADDR_LEN);
    948 		}
    949 		break;
    950 #endif
    951 
    952 	default:
    953 		return (EAFNOSUPPORT);
    954 	}
    955 	return (0);
    956 }
    957 
    958 /*
    959  * Add an Ethernet multicast address or range of addresses to the list for a
    960  * given interface.
    961  */
    962 int
    963 ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
    964 {
    965 	struct ether_multi *enm;
    966 	u_char addrlo[ETHER_ADDR_LEN];
    967 	u_char addrhi[ETHER_ADDR_LEN];
    968 	int s = splimp(), error;
    969 
    970 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
    971 	if (error != 0) {
    972 		splx(s);
    973 		return (error);
    974 	}
    975 
    976 	/*
    977 	 * Verify that we have valid Ethernet multicast addresses.
    978 	 */
    979 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
    980 		splx(s);
    981 		return (EINVAL);
    982 	}
    983 	/*
    984 	 * See if the address range is already in the list.
    985 	 */
    986 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
    987 	if (enm != NULL) {
    988 		/*
    989 		 * Found it; just increment the reference count.
    990 		 */
    991 		++enm->enm_refcount;
    992 		splx(s);
    993 		return (0);
    994 	}
    995 	/*
    996 	 * New address or range; malloc a new multicast record
    997 	 * and link it into the interface's multicast list.
    998 	 */
    999 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
   1000 	if (enm == NULL) {
   1001 		splx(s);
   1002 		return (ENOBUFS);
   1003 	}
   1004 	bcopy(addrlo, enm->enm_addrlo, 6);
   1005 	bcopy(addrhi, enm->enm_addrhi, 6);
   1006 	enm->enm_ec = ec;
   1007 	enm->enm_refcount = 1;
   1008 	LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
   1009 	ec->ec_multicnt++;
   1010 	splx(s);
   1011 	/*
   1012 	 * Return ENETRESET to inform the driver that the list has changed
   1013 	 * and its reception filter should be adjusted accordingly.
   1014 	 */
   1015 	return (ENETRESET);
   1016 }
   1017 
   1018 /*
   1019  * Delete a multicast address record.
   1020  */
   1021 int
   1022 ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
   1023 {
   1024 	struct ether_multi *enm;
   1025 	u_char addrlo[ETHER_ADDR_LEN];
   1026 	u_char addrhi[ETHER_ADDR_LEN];
   1027 	int s = splimp(), error;
   1028 
   1029 	error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
   1030 	if (error != 0) {
   1031 		splx(s);
   1032 		return (error);
   1033 	}
   1034 
   1035 	/*
   1036 	 * Look up the address in our list.
   1037 	 */
   1038 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
   1039 	if (enm == NULL) {
   1040 		splx(s);
   1041 		return (ENXIO);
   1042 	}
   1043 	if (--enm->enm_refcount != 0) {
   1044 		/*
   1045 		 * Still some claims to this record.
   1046 		 */
   1047 		splx(s);
   1048 		return (0);
   1049 	}
   1050 	/*
   1051 	 * No remaining claims to this record; unlink and free it.
   1052 	 */
   1053 	LIST_REMOVE(enm, enm_list);
   1054 	free(enm, M_IFMADDR);
   1055 	ec->ec_multicnt--;
   1056 	splx(s);
   1057 	/*
   1058 	 * Return ENETRESET to inform the driver that the list has changed
   1059 	 * and its reception filter should be adjusted accordingly.
   1060 	 */
   1061 	return (ENETRESET);
   1062 }
   1063