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