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