xref: /csrg-svn/sys/net/if_ethersubr.c (revision 58231)
135379Skfall /*
239187Ssklower  * Copyright (c) 1982, 1989 Regents of the University of California.
335379Skfall  * All rights reserved.
435379Skfall  *
544464Sbostic  * %sccs.include.redist.c%
635379Skfall  *
7*58231Ssklower  *	@(#)if_ethersubr.c	7.23 (Berkeley) 02/25/93
835379Skfall  */
935379Skfall 
1056529Sbostic #include <sys/param.h>
1156529Sbostic #include <sys/systm.h>
1256529Sbostic #include <sys/kernel.h>
1356529Sbostic #include <sys/malloc.h>
1456529Sbostic #include <sys/mbuf.h>
1556529Sbostic #include <sys/protosw.h>
1656529Sbostic #include <sys/socket.h>
1756529Sbostic #include <sys/ioctl.h>
1856529Sbostic #include <sys/errno.h>
1956529Sbostic #include <sys/syslog.h>
2035379Skfall 
2156529Sbostic #include <machine/cpu.h>
2235379Skfall 
2356529Sbostic #include <net/if.h>
2456529Sbostic #include <net/netisr.h>
2556529Sbostic #include <net/route.h>
2656529Sbostic #include <net/if_llc.h>
2756529Sbostic #include <net/if_dl.h>
2856529Sbostic #include <net/if_types.h>
2956529Sbostic 
3035379Skfall #ifdef INET
3156529Sbostic #include <netinet/in.h>
3256529Sbostic #include <netinet/in_var.h>
3345930Sbostic #endif
3456529Sbostic #include <netinet/if_ether.h>
3535379Skfall 
3635379Skfall #ifdef NS
3756529Sbostic #include <netns/ns.h>
3856529Sbostic #include <netns/ns_if.h>
3935379Skfall #endif
4035379Skfall 
4137472Ssklower #ifdef ISO
4256529Sbostic #include <netiso/argo_debug.h>
4356529Sbostic #include <netiso/iso.h>
4456529Sbostic #include <netiso/iso_var.h>
4556529Sbostic #include <netiso/iso_snpac.h>
4637472Ssklower #endif
4737472Ssklower 
48*58231Ssklower #ifdef LLC
49*58231Ssklower #include <netccitt/dll.h>
50*58231Ssklower #include <netccitt/llc_var.h>
51*58231Ssklower #endif
52*58231Ssklower 
53*58231Ssklower #if defined(LLC) && defined(CCITT)
54*58231Ssklower extern struct ifqueue pkintrq;
55*58231Ssklower #endif
56*58231Ssklower 
5735379Skfall u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5835795Skarels extern	struct ifnet loif;
5950133Ssklower #define senderr(e) { error = (e); goto bad;}
6035379Skfall 
6135379Skfall /*
6235379Skfall  * Ethernet output routine.
6335379Skfall  * Encapsulate a packet of type family for the local net.
6435379Skfall  * Use trailer local net encapsulation if enough data in first
6535379Skfall  * packet leaves a multiple of 512 bytes of data in remainder.
6635379Skfall  * Assumes that ifp is actually pointer to arpcom structure.
6735379Skfall  */
6850133Ssklower ether_output(ifp, m0, dst, rt0)
6935379Skfall 	register struct ifnet *ifp;
7035379Skfall 	struct mbuf *m0;
7135379Skfall 	struct sockaddr *dst;
7250133Ssklower 	struct rtentry *rt0;
7335379Skfall {
7435795Skarels 	short type;
7535795Skarels 	int s, error = 0;
7635379Skfall  	u_char edst[6];
7735379Skfall 	register struct mbuf *m = m0;
7850133Ssklower 	register struct rtentry *rt;
7935795Skarels 	struct mbuf *mcopy = (struct mbuf *)0;
8035379Skfall 	register struct ether_header *eh;
8154718Ssklower 	int off, len = m->m_pkthdr.len;
8254718Ssklower 	struct arpcom *ac = (struct arpcom *)ifp;
8335379Skfall 
8450133Ssklower 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
8550133Ssklower 		senderr(ENETDOWN);
8650133Ssklower 	ifp->if_lastchange = time;
8750133Ssklower 	if (rt = rt0) {
8850133Ssklower 		if ((rt->rt_flags & RTF_UP) == 0) {
8950133Ssklower 			if (rt0 = rt = rtalloc1(dst, 1))
9050133Ssklower 				rt->rt_refcnt--;
9150133Ssklower 			else
9253060Ssklower 				senderr(EHOSTUNREACH);
9350133Ssklower 		}
9450133Ssklower 		if (rt->rt_flags & RTF_GATEWAY) {
9550133Ssklower 			if (rt->rt_gwroute == 0)
9650133Ssklower 				goto lookup;
9750133Ssklower 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
9850133Ssklower 				rtfree(rt); rt = rt0;
9950133Ssklower 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
10050133Ssklower 				if ((rt = rt->rt_gwroute) == 0)
10153060Ssklower 					senderr(EHOSTUNREACH);
10250133Ssklower 			}
10350133Ssklower 		}
10450133Ssklower 		if (rt->rt_flags & RTF_REJECT)
10550133Ssklower 			if (rt->rt_rmx.rmx_expire == 0 ||
10650133Ssklower 			    time.tv_sec < rt->rt_rmx.rmx_expire)
10753060Ssklower 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
10835379Skfall 	}
10935379Skfall 	switch (dst->sa_family) {
11035379Skfall 
11135379Skfall #ifdef INET
11235379Skfall 	case AF_INET:
11350133Ssklower 		if (!arpresolve(ac, rt, m, (struct sockaddr_in *)dst,
11454718Ssklower 				edst))
11535379Skfall 			return (0);	/* if not yet resolved */
11654718Ssklower 		/* If broadcasting on a simplex interface, loopback a copy */
11754718Ssklower 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
11841546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
11935379Skfall 		off = m->m_pkthdr.len - m->m_len;
12035379Skfall 		type = ETHERTYPE_IP;
12154718Ssklower 		break;
12235379Skfall #endif
12335379Skfall #ifdef NS
12435379Skfall 	case AF_NS:
12535379Skfall 		type = ETHERTYPE_NS;
12639187Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
12739187Ssklower 		    (caddr_t)edst, sizeof (edst));
12835795Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
12945632Ssklower 			return (looutput(ifp, m, dst, rt));
13054718Ssklower 		/* If broadcasting on a simplex interface, loopback a copy */
13154718Ssklower 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
13241546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
13354718Ssklower 		break;
13435379Skfall #endif
13535795Skarels #ifdef	ISO
13635795Skarels 	case AF_ISO: {
13743073Ssklower 		int	snpalen;
13837472Ssklower 		struct	llc *l;
13950133Ssklower 		register struct sockaddr_dl *sdl;
14037472Ssklower 
14150133Ssklower 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
14250133Ssklower 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
14350133Ssklower 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
14450133Ssklower 		} else if (error =
14550133Ssklower 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
14650133Ssklower 					    (char *)edst, &snpalen))
14743073Ssklower 			goto bad; /* Not Resolved */
14854718Ssklower 		/* If broadcasting on a simplex interface, loopback a copy */
14954718Ssklower 		if (*edst & 1)
15054718Ssklower 			m->m_flags |= (M_BCAST|M_MCAST);
15154718Ssklower 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
15245632Ssklower 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
15345632Ssklower 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
15445632Ssklower 			if (mcopy) {
15545632Ssklower 				eh = mtod(mcopy, struct ether_header *);
15645632Ssklower 				bcopy((caddr_t)edst,
15745632Ssklower 				      (caddr_t)eh->ether_dhost, sizeof (edst));
15845632Ssklower 				bcopy((caddr_t)ac->ac_enaddr,
15945632Ssklower 				      (caddr_t)eh->ether_shost, sizeof (edst));
16045632Ssklower 			}
16145632Ssklower 		}
16235795Skarels 		M_PREPEND(m, 3, M_DONTWAIT);
16337472Ssklower 		if (m == NULL)
16443073Ssklower 			return (0);
16535795Skarels 		type = m->m_pkthdr.len;
16635795Skarels 		l = mtod(m, struct llc *);
16735795Skarels 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
16835795Skarels 		l->llc_control = LLC_UI;
16939187Ssklower 		len += 3;
17035795Skarels 		IFDEBUG(D_ETHER)
17135795Skarels 			int i;
17235795Skarels 			printf("unoutput: sending pkt to: ");
17335795Skarels 			for (i=0; i<6; i++)
17435795Skarels 				printf("%x ", edst[i] & 0xff);
17535795Skarels 			printf("\n");
17635795Skarels 		ENDDEBUG
17754718Ssklower 		} break;
17835795Skarels #endif	ISO
179*58231Ssklower #ifdef	LLC
180*58231Ssklower /*	case AF_NSAP: */
181*58231Ssklower 	case AF_CCITT: {
182*58231Ssklower 		register struct sockaddr_dl *sdl =
183*58231Ssklower 			(struct sockaddr_dl *) rt -> rt_gateway;
18441546Smckusick 
185*58231Ssklower 		if (sdl && sdl->sdl_family == AF_LINK
186*58231Ssklower 		    && sdl->sdl_alen > 0) {
187*58231Ssklower 			bcopy(LLADDR(sdl), (char *)edst,
188*58231Ssklower 				sizeof(edst));
189*58231Ssklower 		} else goto bad; /* Not a link interface ? Funny ... */
190*58231Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
191*58231Ssklower 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
192*58231Ssklower 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
193*58231Ssklower 			if (mcopy) {
194*58231Ssklower 				eh = mtod(mcopy, struct ether_header *);
195*58231Ssklower 				bcopy((caddr_t)edst,
196*58231Ssklower 				      (caddr_t)eh->ether_dhost, sizeof (edst));
197*58231Ssklower 				bcopy((caddr_t)ac->ac_enaddr,
198*58231Ssklower 				      (caddr_t)eh->ether_shost, sizeof (edst));
199*58231Ssklower 			}
200*58231Ssklower 		}
201*58231Ssklower 		type = m->m_pkthdr.len;
202*58231Ssklower #ifdef LLC_DEBUG
203*58231Ssklower 		{
204*58231Ssklower 			int i;
205*58231Ssklower 			register struct llc *l = mtod(m, struct llc *);
206*58231Ssklower 
207*58231Ssklower 			printf("ether_output: sending LLC2 pkt to: ");
208*58231Ssklower 			for (i=0; i<6; i++)
209*58231Ssklower 				printf("%x ", edst[i] & 0xff);
210*58231Ssklower 			printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
211*58231Ssklower 			       type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
212*58231Ssklower 			       l->llc_control & 0xff);
213*58231Ssklower 
214*58231Ssklower 		}
215*58231Ssklower #endif LLC_DEBUG
216*58231Ssklower 		} break;
217*58231Ssklower #endif/* LLC */
218*58231Ssklower #ifdef RMP
219*58231Ssklower 	case AF_RMP:
220*58231Ssklower 		/*
221*58231Ssklower 		 *  This is IEEE 802.3 -- the Ethernet `type' field is
222*58231Ssklower 		 *  really a `length' field.
223*58231Ssklower 		 */
224*58231Ssklower 		type = m->m_len;
225*58231Ssklower  		bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
226*58231Ssklower 		break;
227*58231Ssklower #endif
228*58231Ssklower 
22935379Skfall 	case AF_UNSPEC:
23035379Skfall 		eh = (struct ether_header *)dst->sa_data;
23135379Skfall  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
23235379Skfall 		type = eh->ether_type;
23354718Ssklower 		break;
23435379Skfall 
23535379Skfall 	default:
23635379Skfall 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
23735379Skfall 			dst->sa_family);
23850133Ssklower 		senderr(EAFNOSUPPORT);
23935379Skfall 	}
24035379Skfall 
24135379Skfall 
24245632Ssklower 	if (mcopy)
24345632Ssklower 		(void) looutput(ifp, mcopy, dst, rt);
24435379Skfall 	/*
24535379Skfall 	 * Add local net header.  If no space in first mbuf,
24635379Skfall 	 * allocate another.
24735379Skfall 	 */
24835379Skfall 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
24950133Ssklower 	if (m == 0)
25050133Ssklower 		senderr(ENOBUFS);
25135379Skfall 	eh = mtod(m, struct ether_header *);
25235795Skarels 	type = htons((u_short)type);
25335795Skarels 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
25435795Skarels 		sizeof(eh->ether_type));
25535379Skfall  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
25635379Skfall  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
25735379Skfall 	    sizeof(eh->ether_shost));
25845632Ssklower 	s = splimp();
25935379Skfall 	/*
26035379Skfall 	 * Queue message on interface, and start output if interface
26135379Skfall 	 * not yet active.
26235379Skfall 	 */
26335379Skfall 	if (IF_QFULL(&ifp->if_snd)) {
26435379Skfall 		IF_DROP(&ifp->if_snd);
26535379Skfall 		splx(s);
26650133Ssklower 		senderr(ENOBUFS);
26735379Skfall 	}
26835379Skfall 	IF_ENQUEUE(&ifp->if_snd, m);
26935379Skfall 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
27038845Sroot 		(*ifp->if_start)(ifp);
27135379Skfall 	splx(s);
27239187Ssklower 	ifp->if_obytes += len + sizeof (struct ether_header);
27354718Ssklower 	if (m->m_flags & M_MCAST)
27439187Ssklower 		ifp->if_omcasts++;
27535795Skarels 	return (error);
27635379Skfall 
27735379Skfall bad:
27835795Skarels 	if (m)
27935795Skarels 		m_freem(m);
28035379Skfall 	return (error);
28135379Skfall }
28235379Skfall 
28335379Skfall /*
28438845Sroot  * Process a received Ethernet packet;
28538845Sroot  * the packet is in the mbuf chain m without
28638845Sroot  * the ether header, which is provided separately.
28735379Skfall  */
28837472Ssklower ether_input(ifp, eh, m)
28935379Skfall 	struct ifnet *ifp;
29035379Skfall 	register struct ether_header *eh;
29135379Skfall 	struct mbuf *m;
29235379Skfall {
29335379Skfall 	register struct ifqueue *inq;
29435795Skarels 	register struct llc *l;
295*58231Ssklower 	struct arpcom *ac = (struct arpcom *)ifp;
29635379Skfall 	int s;
29735379Skfall 
29853696Ssklower 	if ((ifp->if_flags & IFF_UP) == 0) {
29953696Ssklower 		m_freem(m);
30053696Ssklower 		return;
30153696Ssklower 	}
30239187Ssklower 	ifp->if_lastchange = time;
30339187Ssklower 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
30435795Skarels 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
30535379Skfall 	    sizeof(etherbroadcastaddr)) == 0)
30635379Skfall 		m->m_flags |= M_BCAST;
30735795Skarels 	else if (eh->ether_dhost[0] & 1)
30835795Skarels 		m->m_flags |= M_MCAST;
30939187Ssklower 	if (m->m_flags & (M_BCAST|M_MCAST))
31039187Ssklower 		ifp->if_imcasts++;
31135379Skfall 
31235379Skfall 	switch (eh->ether_type) {
31335379Skfall #ifdef INET
31435379Skfall 	case ETHERTYPE_IP:
31535379Skfall 		schednetisr(NETISR_IP);
31635379Skfall 		inq = &ipintrq;
31735379Skfall 		break;
31835379Skfall 
31935379Skfall 	case ETHERTYPE_ARP:
32050133Ssklower 		schednetisr(NETISR_ARP);
32150133Ssklower 		inq = &arpintrq;
32250133Ssklower 		break;
32335379Skfall #endif
32435379Skfall #ifdef NS
32535379Skfall 	case ETHERTYPE_NS:
32635379Skfall 		schednetisr(NETISR_NS);
32735379Skfall 		inq = &nsintrq;
32835379Skfall 		break;
32935379Skfall 
33035379Skfall #endif
33135379Skfall 	default:
332*58231Ssklower #if defined (ISO) || defined (LLC)
33337472Ssklower 		if (eh->ether_type > ETHERMTU)
33435795Skarels 			goto dropanyway;
33535795Skarels 		l = mtod(m, struct llc *);
336*58231Ssklower 		switch (l->llc_dsap) {
337*58231Ssklower #ifdef	ISO
338*58231Ssklower 		case LLC_ISO_LSAP:
339*58231Ssklower 			switch (l->llc_control) {
340*58231Ssklower 			case LLC_UI:
341*58231Ssklower 				/* LLC_UI_P forbidden in class 1 service */
342*58231Ssklower 				if ((l->llc_dsap == LLC_ISO_LSAP) &&
343*58231Ssklower 				    (l->llc_ssap == LLC_ISO_LSAP)) {
344*58231Ssklower 					/* LSAP for ISO */
345*58231Ssklower 					if (m->m_pkthdr.len > eh->ether_type)
346*58231Ssklower 						m_adj(m, eh->ether_type - m->m_pkthdr.len);
347*58231Ssklower 					m->m_data += 3;		/* XXX */
348*58231Ssklower 					m->m_len -= 3;		/* XXX */
349*58231Ssklower 					m->m_pkthdr.len -= 3;	/* XXX */
350*58231Ssklower 					M_PREPEND(m, sizeof *eh, M_DONTWAIT);
351*58231Ssklower 					if (m == 0)
352*58231Ssklower 						return;
353*58231Ssklower 					*mtod(m, struct ether_header *) = *eh;
354*58231Ssklower 					IFDEBUG(D_ETHER)
355*58231Ssklower 						printf("clnp packet");
356*58231Ssklower 					ENDDEBUG
357*58231Ssklower 					schednetisr(NETISR_ISO);
358*58231Ssklower 					inq = &clnlintrq;
359*58231Ssklower 					break;
360*58231Ssklower 				}
361*58231Ssklower 				goto dropanyway;
362*58231Ssklower 
363*58231Ssklower 			case LLC_XID:
364*58231Ssklower 			case LLC_XID_P:
365*58231Ssklower 				if(m->m_len < 6)
366*58231Ssklower 					goto dropanyway;
367*58231Ssklower 				l->llc_window = 0;
368*58231Ssklower 				l->llc_fid = 9;
369*58231Ssklower 				l->llc_class = 1;
370*58231Ssklower 				l->llc_dsap = l->llc_ssap = 0;
371*58231Ssklower 				/* Fall through to */
372*58231Ssklower 			case LLC_TEST:
373*58231Ssklower 			case LLC_TEST_P:
374*58231Ssklower 			{
375*58231Ssklower 				struct sockaddr sa;
376*58231Ssklower 				register struct ether_header *eh2;
377*58231Ssklower 				int i;
378*58231Ssklower 				u_char c = l->llc_dsap;
379*58231Ssklower 
380*58231Ssklower 				l->llc_dsap = l->llc_ssap;
381*58231Ssklower 				l->llc_ssap = c;
382*58231Ssklower 				if (m->m_flags & (M_BCAST | M_MCAST))
383*58231Ssklower 					bcopy((caddr_t)ac->ac_enaddr,
384*58231Ssklower 					      (caddr_t)eh->ether_dhost, 6);
385*58231Ssklower 				sa.sa_family = AF_UNSPEC;
386*58231Ssklower 				sa.sa_len = sizeof(sa);
387*58231Ssklower 				eh2 = (struct ether_header *)sa.sa_data;
388*58231Ssklower 				for (i = 0; i < 6; i++) {
389*58231Ssklower 					eh2->ether_shost[i] = c = eh->ether_dhost[i];
390*58231Ssklower 					eh2->ether_dhost[i] =
391*58231Ssklower 						eh->ether_dhost[i] = eh->ether_shost[i];
392*58231Ssklower 					eh->ether_shost[i] = c;
393*58231Ssklower 				}
394*58231Ssklower 				ifp->if_output(ifp, m, &sa, NULL);
395*58231Ssklower 				return;
396*58231Ssklower 			}
397*58231Ssklower 			default:
398*58231Ssklower 				m_freem(m);
399*58231Ssklower 				return;
400*58231Ssklower 			}
401*58231Ssklower 			break;
402*58231Ssklower #endif /* ISO */
403*58231Ssklower #ifdef LLC
404*58231Ssklower 		case LLC_X25_LSAP:
405*58231Ssklower 		{
40645632Ssklower 			if (m->m_pkthdr.len > eh->ether_type)
40745632Ssklower 				m_adj(m, eh->ether_type - m->m_pkthdr.len);
408*58231Ssklower 			M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
40937472Ssklower 			if (m == 0)
41037472Ssklower 				return;
411*58231Ssklower 			if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
412*58231Ssklower 					    eh->ether_dhost, LLC_X25_LSAP, 6,
413*58231Ssklower 					    mtod(m, struct sdl_hdr *)))
414*58231Ssklower 				panic("ETHER cons addr failure");
415*58231Ssklower 			mtod(m, struct sdl_hdr *)->sdlhdr_len = eh->ether_type;
416*58231Ssklower #ifdef LLC_DEBUG
417*58231Ssklower 				printf("llc packet\n");
418*58231Ssklower #endif LLC_DEBUG
419*58231Ssklower 			schednetisr(NETISR_CCITT);
420*58231Ssklower 			inq = &llcintrq;
42138845Sroot 			break;
42235795Skarels 		}
423*58231Ssklower #endif /* LLC */
42435795Skarels 		dropanyway:
42535795Skarels 		default:
426*58231Ssklower 			m_freem(m);
427*58231Ssklower 			return;
428*58231Ssklower 		}
429*58231Ssklower #else /* ISO || LLC */
43038845Sroot 	    m_freem(m);
43138845Sroot 	    return;
432*58231Ssklower #endif /* ISO || LLC */
43335379Skfall 	}
43435379Skfall 
43535379Skfall 	s = splimp();
43635379Skfall 	if (IF_QFULL(inq)) {
43735379Skfall 		IF_DROP(inq);
43835379Skfall 		m_freem(m);
43935379Skfall 	} else
44035379Skfall 		IF_ENQUEUE(inq, m);
44135379Skfall 	splx(s);
44235379Skfall }
44335379Skfall 
44435379Skfall /*
44535379Skfall  * Convert Ethernet address to printable (loggable) representation.
44635379Skfall  */
44737472Ssklower static char digits[] = "0123456789abcdef";
44835379Skfall char *
44935379Skfall ether_sprintf(ap)
45035379Skfall 	register u_char *ap;
45135379Skfall {
45235379Skfall 	register i;
45335379Skfall 	static char etherbuf[18];
45435379Skfall 	register char *cp = etherbuf;
45535379Skfall 
45635379Skfall 	for (i = 0; i < 6; i++) {
45735379Skfall 		*cp++ = digits[*ap >> 4];
45835379Skfall 		*cp++ = digits[*ap++ & 0xf];
45935379Skfall 		*cp++ = ':';
46035379Skfall 	}
46135379Skfall 	*--cp = 0;
46235379Skfall 	return (etherbuf);
46335379Skfall }
46452567Ssklower 
46552567Ssklower /*
46652567Ssklower  * Perform common duties while attaching to interface list
46752567Ssklower  */
46852567Ssklower ether_ifattach(ifp)
46952567Ssklower 	register struct ifnet *ifp;
47052567Ssklower {
47152567Ssklower 	register struct ifaddr *ifa;
47252567Ssklower 	register struct sockaddr_dl *sdl;
47352567Ssklower 
47452567Ssklower 	ifp->if_type = IFT_ETHER;
47552567Ssklower 	ifp->if_addrlen = 6;
47652567Ssklower 	ifp->if_hdrlen = 14;
47752567Ssklower 	ifp->if_mtu = ETHERMTU;
47852567Ssklower 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
47952567Ssklower 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
48052567Ssklower 		    sdl->sdl_family == AF_LINK) {
48152567Ssklower 			sdl->sdl_type = IFT_ETHER;
48252567Ssklower 			sdl->sdl_alen = ifp->if_addrlen;
48352567Ssklower 			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
48452567Ssklower 			      LLADDR(sdl), ifp->if_addrlen);
48552567Ssklower 			break;
48652567Ssklower 		}
48752567Ssklower }
48854718Ssklower 
48954718Ssklower u_char	ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
49054718Ssklower u_char	ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
49154718Ssklower /*
49254718Ssklower  * Add an Ethernet multicast address or range of addresses to the list for a
49354718Ssklower  * given interface.
49454718Ssklower  */
49554718Ssklower int
49654718Ssklower ether_addmulti(ifr, ac)
49754718Ssklower 	struct ifreq *ifr;
49854718Ssklower 	register struct arpcom *ac;
49954718Ssklower {
50054718Ssklower 	register struct ether_multi *enm;
50154718Ssklower 	struct sockaddr_in *sin;
50254718Ssklower 	u_char addrlo[6];
50354718Ssklower 	u_char addrhi[6];
50454718Ssklower 	int s = splimp();
50554718Ssklower 
50654718Ssklower 	switch (ifr->ifr_addr.sa_family) {
50754718Ssklower 
50854718Ssklower 	case AF_UNSPEC:
50954718Ssklower 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
51054718Ssklower 		bcopy(addrlo, addrhi, 6);
51154718Ssklower 		break;
51254718Ssklower 
51354718Ssklower #ifdef INET
51454718Ssklower 	case AF_INET:
51554718Ssklower 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
51654718Ssklower 		if (sin->sin_addr.s_addr == INADDR_ANY) {
51754718Ssklower 			/*
51854718Ssklower 			 * An IP address of INADDR_ANY means listen to all
51954718Ssklower 			 * of the Ethernet multicast addresses used for IP.
52054718Ssklower 			 * (This is for the sake of IP multicast routers.)
52154718Ssklower 			 */
52254718Ssklower 			bcopy(ether_ipmulticast_min, addrlo, 6);
52354718Ssklower 			bcopy(ether_ipmulticast_max, addrhi, 6);
52454718Ssklower 		}
52554718Ssklower 		else {
52654718Ssklower 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
52754718Ssklower 			bcopy(addrlo, addrhi, 6);
52854718Ssklower 		}
52954718Ssklower 		break;
53054718Ssklower #endif
53154718Ssklower 
53254718Ssklower 	default:
53354718Ssklower 		splx(s);
53454718Ssklower 		return (EAFNOSUPPORT);
53554718Ssklower 	}
53654718Ssklower 
53754718Ssklower 	/*
53854718Ssklower 	 * Verify that we have valid Ethernet multicast addresses.
53954718Ssklower 	 */
54054718Ssklower 	if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
54154718Ssklower 		splx(s);
54254718Ssklower 		return (EINVAL);
54354718Ssklower 	}
54454718Ssklower 	/*
54554718Ssklower 	 * See if the address range is already in the list.
54654718Ssklower 	 */
54754718Ssklower 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
54854718Ssklower 	if (enm != NULL) {
54954718Ssklower 		/*
55054718Ssklower 		 * Found it; just increment the reference count.
55154718Ssklower 		 */
55254718Ssklower 		++enm->enm_refcount;
55354718Ssklower 		splx(s);
55454718Ssklower 		return (0);
55554718Ssklower 	}
55654718Ssklower 	/*
55754718Ssklower 	 * New address or range; malloc a new multicast record
55854718Ssklower 	 * and link it into the interface's multicast list.
55954718Ssklower 	 */
56054718Ssklower 	enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
56154718Ssklower 	if (enm == NULL) {
56254718Ssklower 		splx(s);
56354718Ssklower 		return (ENOBUFS);
56454718Ssklower 	}
56554718Ssklower 	bcopy(addrlo, enm->enm_addrlo, 6);
56654718Ssklower 	bcopy(addrhi, enm->enm_addrhi, 6);
56754718Ssklower 	enm->enm_ac = ac;
56854718Ssklower 	enm->enm_refcount = 1;
56954718Ssklower 	enm->enm_next = ac->ac_multiaddrs;
57054718Ssklower 	ac->ac_multiaddrs = enm;
57154718Ssklower 	ac->ac_multicnt++;
57254718Ssklower 	splx(s);
57354718Ssklower 	/*
57454718Ssklower 	 * Return ENETRESET to inform the driver that the list has changed
57554718Ssklower 	 * and its reception filter should be adjusted accordingly.
57654718Ssklower 	 */
57754718Ssklower 	return (ENETRESET);
57854718Ssklower }
57954718Ssklower 
58054718Ssklower /*
58154718Ssklower  * Delete a multicast address record.
58254718Ssklower  */
58354718Ssklower int
58454718Ssklower ether_delmulti(ifr, ac)
58554718Ssklower 	struct ifreq *ifr;
58654718Ssklower 	register struct arpcom *ac;
58754718Ssklower {
58854718Ssklower 	register struct ether_multi *enm;
58954718Ssklower 	register struct ether_multi **p;
59054718Ssklower 	struct sockaddr_in *sin;
59154718Ssklower 	u_char addrlo[6];
59254718Ssklower 	u_char addrhi[6];
59354718Ssklower 	int s = splimp();
59454718Ssklower 
59554718Ssklower 	switch (ifr->ifr_addr.sa_family) {
59654718Ssklower 
59754718Ssklower 	case AF_UNSPEC:
59854718Ssklower 		bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
59954718Ssklower 		bcopy(addrlo, addrhi, 6);
60054718Ssklower 		break;
60154718Ssklower 
60254718Ssklower #ifdef INET
60354718Ssklower 	case AF_INET:
60454718Ssklower 		sin = (struct sockaddr_in *)&(ifr->ifr_addr);
60554718Ssklower 		if (sin->sin_addr.s_addr == INADDR_ANY) {
60654718Ssklower 			/*
60754718Ssklower 			 * An IP address of INADDR_ANY means stop listening
60854718Ssklower 			 * to the range of Ethernet multicast addresses used
60954718Ssklower 			 * for IP.
61054718Ssklower 			 */
61154718Ssklower 			bcopy(ether_ipmulticast_min, addrlo, 6);
61254718Ssklower 			bcopy(ether_ipmulticast_max, addrhi, 6);
61354718Ssklower 		}
61454718Ssklower 		else {
61554718Ssklower 			ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
61654718Ssklower 			bcopy(addrlo, addrhi, 6);
61754718Ssklower 		}
61854718Ssklower 		break;
61954718Ssklower #endif
62054718Ssklower 
62154718Ssklower 	default:
62254718Ssklower 		splx(s);
62354718Ssklower 		return (EAFNOSUPPORT);
62454718Ssklower 	}
62554718Ssklower 
62654718Ssklower 	/*
62754718Ssklower 	 * Look up the address in our list.
62854718Ssklower 	 */
62954718Ssklower 	ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
63054718Ssklower 	if (enm == NULL) {
63154718Ssklower 		splx(s);
63254718Ssklower 		return (ENXIO);
63354718Ssklower 	}
63454718Ssklower 	if (--enm->enm_refcount != 0) {
63554718Ssklower 		/*
63654718Ssklower 		 * Still some claims to this record.
63754718Ssklower 		 */
63854718Ssklower 		splx(s);
63954718Ssklower 		return (0);
64054718Ssklower 	}
64154718Ssklower 	/*
64254718Ssklower 	 * No remaining claims to this record; unlink and free it.
64354718Ssklower 	 */
64454718Ssklower 	for (p = &enm->enm_ac->ac_multiaddrs;
64554718Ssklower 	     *p != enm;
64654718Ssklower 	     p = &(*p)->enm_next)
64754718Ssklower 		continue;
64854718Ssklower 	*p = (*p)->enm_next;
64954718Ssklower 	free(enm, M_IFMADDR);
65054718Ssklower 	ac->ac_multicnt--;
65154718Ssklower 	splx(s);
65254718Ssklower 	/*
65354718Ssklower 	 * Return ENETRESET to inform the driver that the list has changed
65454718Ssklower 	 * and its reception filter should be adjusted accordingly.
65554718Ssklower 	 */
65654718Ssklower 	return (ENETRESET);
65754718Ssklower }
658