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