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