xref: /csrg-svn/sys/net/if_ethersubr.c (revision 52270)
135379Skfall /*
239187Ssklower  * Copyright (c) 1982, 1989 Regents of the University of California.
335379Skfall  * All rights reserved.
435379Skfall  *
544464Sbostic  * %sccs.include.redist.c%
635379Skfall  *
7*52270Storek  *	@(#)if_ethersubr.c	7.15 (Berkeley) 01/30/92
835379Skfall  */
935379Skfall 
1035379Skfall #include "param.h"
1135379Skfall #include "systm.h"
1248455Skarels #include "kernel.h"
1335379Skfall #include "malloc.h"
1435379Skfall #include "mbuf.h"
1535379Skfall #include "protosw.h"
1635379Skfall #include "socket.h"
1735379Skfall #include "ioctl.h"
1835379Skfall #include "errno.h"
1935379Skfall #include "syslog.h"
20*52270Storek #include "machine/cpu.h"
2135379Skfall 
2235379Skfall #include "if.h"
2335379Skfall #include "netisr.h"
2435379Skfall #include "route.h"
2535795Skarels #include "if_llc.h"
2640790Ssklower #include "if_dl.h"
2735379Skfall 
2835379Skfall #ifdef INET
2935379Skfall #include "../netinet/in.h"
3035379Skfall #include "../netinet/in_var.h"
3145930Sbostic #endif
3235379Skfall #include "../netinet/if_ether.h"
3335379Skfall 
3435379Skfall #ifdef NS
3535379Skfall #include "../netns/ns.h"
3635379Skfall #include "../netns/ns_if.h"
3735379Skfall #endif
3835379Skfall 
3937472Ssklower #ifdef ISO
4037472Ssklower #include "../netiso/argo_debug.h"
4137472Ssklower #include "../netiso/iso.h"
4237472Ssklower #include "../netiso/iso_var.h"
4343073Ssklower #include "../netiso/iso_snpac.h"
4437472Ssklower #endif
4537472Ssklower 
4635379Skfall u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4735795Skarels extern	struct ifnet loif;
4850133Ssklower #define senderr(e) { error = (e); goto bad;}
4935379Skfall 
5035379Skfall /*
5135379Skfall  * Ethernet output routine.
5235379Skfall  * Encapsulate a packet of type family for the local net.
5335379Skfall  * Use trailer local net encapsulation if enough data in first
5435379Skfall  * packet leaves a multiple of 512 bytes of data in remainder.
5535379Skfall  * Assumes that ifp is actually pointer to arpcom structure.
5635379Skfall  */
5750133Ssklower ether_output(ifp, m0, dst, rt0)
5835379Skfall 	register struct ifnet *ifp;
5935379Skfall 	struct mbuf *m0;
6035379Skfall 	struct sockaddr *dst;
6150133Ssklower 	struct rtentry *rt0;
6235379Skfall {
6335795Skarels 	short type;
6435795Skarels 	int s, error = 0;
6535379Skfall  	u_char edst[6];
6635379Skfall 	register struct mbuf *m = m0;
6750133Ssklower 	register struct rtentry *rt;
6835795Skarels 	struct mbuf *mcopy = (struct mbuf *)0;
6935379Skfall 	register struct ether_header *eh;
7039187Ssklower 	int usetrailers, off, len = m->m_pkthdr.len;
7135379Skfall #define	ac ((struct arpcom *)ifp)
7235379Skfall 
7350133Ssklower 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
7450133Ssklower 		senderr(ENETDOWN);
7550133Ssklower 	ifp->if_lastchange = time;
7650133Ssklower 	if (rt = rt0) {
7750133Ssklower 		if ((rt->rt_flags & RTF_UP) == 0) {
7850133Ssklower 			if (rt0 = rt = rtalloc1(dst, 1))
7950133Ssklower 				rt->rt_refcnt--;
8050133Ssklower 			else
8150133Ssklower 				return (EHOSTUNREACH);
8250133Ssklower 		}
8350133Ssklower 		if (rt->rt_flags & RTF_GATEWAY) {
8450133Ssklower 			if (rt->rt_gwroute == 0)
8550133Ssklower 				goto lookup;
8650133Ssklower 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
8750133Ssklower 				rtfree(rt); rt = rt0;
8850133Ssklower 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
8950133Ssklower 				if ((rt = rt->rt_gwroute) == 0)
9050133Ssklower 					return (EHOSTUNREACH);
9150133Ssklower 			}
9250133Ssklower 		}
9350133Ssklower 		if (rt->rt_flags & RTF_REJECT)
9450133Ssklower 			if (rt->rt_rmx.rmx_expire == 0 ||
9550133Ssklower 			    time.tv_sec < rt->rt_rmx.rmx_expire)
9650133Ssklower 				return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
9735379Skfall 	}
9835379Skfall 	switch (dst->sa_family) {
9935379Skfall 
10035379Skfall #ifdef INET
10135379Skfall 	case AF_INET:
10250133Ssklower 		if (!arpresolve(ac, rt, m, (struct sockaddr_in *)dst,
10350133Ssklower 				edst, &usetrailers))
10435379Skfall 			return (0);	/* if not yet resolved */
10537472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
10641546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
10735379Skfall 		off = m->m_pkthdr.len - m->m_len;
10835379Skfall 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
10935379Skfall 		    (m->m_flags & M_EXT) == 0 &&
11035379Skfall 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
11135379Skfall 			type = ETHERTYPE_TRAIL + (off>>9);
11235379Skfall 			m->m_data -= 2 * sizeof (u_short);
11335379Skfall 			m->m_len += 2 * sizeof (u_short);
11439187Ssklower 			len += 2 * sizeof (u_short);
11535379Skfall 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
11635379Skfall 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
11735379Skfall 			goto gottrailertype;
11835379Skfall 		}
11935379Skfall 		type = ETHERTYPE_IP;
12035379Skfall 		goto gottype;
12135379Skfall #endif
12235379Skfall #ifdef NS
12335379Skfall 	case AF_NS:
12435379Skfall 		type = ETHERTYPE_NS;
12539187Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
12639187Ssklower 		    (caddr_t)edst, sizeof (edst));
12735795Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
12845632Ssklower 			return (looutput(ifp, m, dst, rt));
12937472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
13041546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
13135379Skfall 		goto gottype;
13235379Skfall #endif
13335795Skarels #ifdef	ISO
13435795Skarels 	case AF_ISO: {
13543073Ssklower 		int	snpalen;
13637472Ssklower 		struct	llc *l;
13750133Ssklower 		register struct sockaddr_dl *sdl;
13837472Ssklower 
13950133Ssklower 		if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
14050133Ssklower 		    sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
14150133Ssklower 			bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
14250133Ssklower 		} else if (error =
14350133Ssklower 			    iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
14450133Ssklower 					    (char *)edst, &snpalen))
14543073Ssklower 			goto bad; /* Not Resolved */
14645632Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
14745632Ssklower 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
14845632Ssklower 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
14945632Ssklower 			if (mcopy) {
15045632Ssklower 				eh = mtod(mcopy, struct ether_header *);
15145632Ssklower 				bcopy((caddr_t)edst,
15245632Ssklower 				      (caddr_t)eh->ether_dhost, sizeof (edst));
15345632Ssklower 				bcopy((caddr_t)ac->ac_enaddr,
15445632Ssklower 				      (caddr_t)eh->ether_shost, sizeof (edst));
15545632Ssklower 			}
15645632Ssklower 		}
15735795Skarels 		M_PREPEND(m, 3, M_DONTWAIT);
15837472Ssklower 		if (m == NULL)
15943073Ssklower 			return (0);
16035795Skarels 		type = m->m_pkthdr.len;
16135795Skarels 		l = mtod(m, struct llc *);
16235795Skarels 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
16335795Skarels 		l->llc_control = LLC_UI;
16439187Ssklower 		len += 3;
16535795Skarels 		IFDEBUG(D_ETHER)
16635795Skarels 			int i;
16735795Skarels 			printf("unoutput: sending pkt to: ");
16835795Skarels 			for (i=0; i<6; i++)
16935795Skarels 				printf("%x ", edst[i] & 0xff);
17035795Skarels 			printf("\n");
17135795Skarels 		ENDDEBUG
17235795Skarels 		} goto gottype;
17335795Skarels #endif	ISO
17441546Smckusick #ifdef RMP
17541546Smckusick 	case AF_RMP:
17641546Smckusick 		/*
17741546Smckusick 		 *  This is IEEE 802.3 -- the Ethernet `type' field is
17841546Smckusick 		 *  really a `length' field.
17941546Smckusick 		 */
18041546Smckusick 		type = m->m_len;
18141546Smckusick  		bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
18241546Smckusick 		break;
18341546Smckusick #endif
18441546Smckusick 
18535379Skfall 	case AF_UNSPEC:
18635379Skfall 		eh = (struct ether_header *)dst->sa_data;
18735379Skfall  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
18835379Skfall 		type = eh->ether_type;
18935379Skfall 		goto gottype;
19035379Skfall 
19135379Skfall 	default:
19235379Skfall 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
19335379Skfall 			dst->sa_family);
19450133Ssklower 		senderr(EAFNOSUPPORT);
19535379Skfall 	}
19635379Skfall 
19735379Skfall gottrailertype:
19835379Skfall 	/*
19935379Skfall 	 * Packet to be sent as trailer: move first packet
20035379Skfall 	 * (control information) to end of chain.
20135379Skfall 	 */
20235379Skfall 	while (m->m_next)
20335379Skfall 		m = m->m_next;
20435379Skfall 	m->m_next = m0;
20535379Skfall 	m = m0->m_next;
20635379Skfall 	m0->m_next = 0;
20735379Skfall 
20835379Skfall gottype:
20945632Ssklower 	if (mcopy)
21045632Ssklower 		(void) looutput(ifp, mcopy, dst, rt);
21135379Skfall 	/*
21235379Skfall 	 * Add local net header.  If no space in first mbuf,
21335379Skfall 	 * allocate another.
21435379Skfall 	 */
21535379Skfall 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
21650133Ssklower 	if (m == 0)
21750133Ssklower 		senderr(ENOBUFS);
21835379Skfall 	eh = mtod(m, struct ether_header *);
21935795Skarels 	type = htons((u_short)type);
22035795Skarels 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
22135795Skarels 		sizeof(eh->ether_type));
22235379Skfall  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
22335379Skfall  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
22435379Skfall 	    sizeof(eh->ether_shost));
22545632Ssklower 	s = splimp();
22635379Skfall 	/*
22735379Skfall 	 * Queue message on interface, and start output if interface
22835379Skfall 	 * not yet active.
22935379Skfall 	 */
23035379Skfall 	if (IF_QFULL(&ifp->if_snd)) {
23135379Skfall 		IF_DROP(&ifp->if_snd);
23235379Skfall 		splx(s);
23350133Ssklower 		senderr(ENOBUFS);
23435379Skfall 	}
23535379Skfall 	IF_ENQUEUE(&ifp->if_snd, m);
23635379Skfall 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
23738845Sroot 		(*ifp->if_start)(ifp);
23835379Skfall 	splx(s);
23939187Ssklower 	ifp->if_obytes += len + sizeof (struct ether_header);
24039187Ssklower 	if (edst[0] & 1)
24139187Ssklower 		ifp->if_omcasts++;
24235795Skarels 	return (error);
24335379Skfall 
24435379Skfall bad:
24535795Skarels 	if (m)
24635795Skarels 		m_freem(m);
24735379Skfall 	return (error);
24835379Skfall }
24935379Skfall 
25035379Skfall /*
25138845Sroot  * Process a received Ethernet packet;
25238845Sroot  * the packet is in the mbuf chain m without
25338845Sroot  * the ether header, which is provided separately.
25435379Skfall  */
25537472Ssklower ether_input(ifp, eh, m)
25635379Skfall 	struct ifnet *ifp;
25735379Skfall 	register struct ether_header *eh;
25835379Skfall 	struct mbuf *m;
25935379Skfall {
26035379Skfall 	register struct ifqueue *inq;
26135795Skarels 	register struct llc *l;
26235379Skfall 	int s;
26335379Skfall 
26439187Ssklower 	ifp->if_lastchange = time;
26539187Ssklower 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
26635795Skarels 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
26735379Skfall 	    sizeof(etherbroadcastaddr)) == 0)
26835379Skfall 		m->m_flags |= M_BCAST;
26935795Skarels 	else if (eh->ether_dhost[0] & 1)
27035795Skarels 		m->m_flags |= M_MCAST;
27139187Ssklower 	if (m->m_flags & (M_BCAST|M_MCAST))
27239187Ssklower 		ifp->if_imcasts++;
27335379Skfall 
27435379Skfall 	switch (eh->ether_type) {
27535379Skfall #ifdef INET
27635379Skfall 	case ETHERTYPE_IP:
27735379Skfall 		schednetisr(NETISR_IP);
27835379Skfall 		inq = &ipintrq;
27935379Skfall 		break;
28035379Skfall 
28135379Skfall 	case ETHERTYPE_ARP:
28250133Ssklower 		schednetisr(NETISR_ARP);
28350133Ssklower 		inq = &arpintrq;
28450133Ssklower 		break;
28535379Skfall #endif
28635379Skfall #ifdef NS
28735379Skfall 	case ETHERTYPE_NS:
28835379Skfall 		schednetisr(NETISR_NS);
28935379Skfall 		inq = &nsintrq;
29035379Skfall 		break;
29135379Skfall 
29235379Skfall #endif
29335379Skfall 	default:
29438845Sroot #ifdef	ISO
29537472Ssklower 		if (eh->ether_type > ETHERMTU)
29635795Skarels 			goto dropanyway;
29735795Skarels 		l = mtod(m, struct llc *);
29835795Skarels 		switch (l->llc_control) {
29935795Skarels 		case LLC_UI:
30035795Skarels 		/* LLC_UI_P forbidden in class 1 service */
30135795Skarels 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
30235795Skarels 			(l->llc_ssap == LLC_ISO_LSAP)) {
30335795Skarels 				/* LSAP for ISO */
30445632Ssklower 			if (m->m_pkthdr.len > eh->ether_type)
30545632Ssklower 				m_adj(m, eh->ether_type - m->m_pkthdr.len);
30638845Sroot 			m->m_data += 3;		/* XXX */
30738845Sroot 			m->m_len -= 3;		/* XXX */
30838845Sroot 			m->m_pkthdr.len -= 3;	/* XXX */
30937472Ssklower 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
31037472Ssklower 			if (m == 0)
31137472Ssklower 				return;
31237472Ssklower 			*mtod(m, struct ether_header *) = *eh;
31337472Ssklower 			IFDEBUG(D_ETHER)
31437472Ssklower 			    printf("clnp packet");
31537472Ssklower 			ENDDEBUG
31637472Ssklower 			schednetisr(NETISR_ISO);
31735795Skarels 			inq = &clnlintrq;
31838845Sroot 			break;
31935795Skarels 		    }
32038845Sroot 		    goto dropanyway;
32138845Sroot 
32235795Skarels 		case LLC_XID:
32335795Skarels 		case LLC_XID_P:
32435795Skarels 		    if(m->m_len < 6)
32535795Skarels 			goto dropanyway;
32635795Skarels 		    l->llc_window = 0;
32735795Skarels 		    l->llc_fid = 9;
32835795Skarels 		    l->llc_class = 1;
32935795Skarels 		    l->llc_dsap = l->llc_ssap = 0;
33035795Skarels 		    /* Fall through to */
33135795Skarels 		case LLC_TEST:
33235795Skarels 		case LLC_TEST_P:
33335795Skarels 		{
33435795Skarels 		    struct sockaddr sa;
33535795Skarels 		    register struct ether_header *eh2;
33635795Skarels 		    int i;
33735795Skarels 		    u_char c = l->llc_dsap;
33835795Skarels 		    l->llc_dsap = l->llc_ssap;
33935795Skarels 		    l->llc_ssap = c;
34037472Ssklower 		    if (m->m_flags & (M_BCAST | M_MCAST))
34137472Ssklower 			bcopy((caddr_t)ac->ac_enaddr,
34237472Ssklower 			      (caddr_t)eh->ether_dhost, 6);
34335795Skarels 		    sa.sa_family = AF_UNSPEC;
34437472Ssklower 		    sa.sa_len = sizeof(sa);
34535795Skarels 		    eh2 = (struct ether_header *)sa.sa_data;
34635795Skarels 		    for (i = 0; i < 6; i++) {
34735795Skarels 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
34835795Skarels 			eh2->ether_dhost[i] =
34935795Skarels 				eh->ether_dhost[i] = eh->ether_shost[i];
35035795Skarels 			eh->ether_shost[i] = c;
35135795Skarels 		    }
35235795Skarels 		    ifp->if_output(ifp, m, &sa);
35335795Skarels 		    return;
35435795Skarels 		}
35535795Skarels 		dropanyway:
35635795Skarels 		default:
35735795Skarels 		    m_freem(m);
35835795Skarels 		    return;
35935795Skarels 	    }
36038845Sroot #else
36138845Sroot 	    m_freem(m);
36238845Sroot 	    return;
36338845Sroot #endif	ISO
36435379Skfall 	}
36535379Skfall 
36635379Skfall 	s = splimp();
36735379Skfall 	if (IF_QFULL(inq)) {
36835379Skfall 		IF_DROP(inq);
36935379Skfall 		m_freem(m);
37035379Skfall 	} else
37135379Skfall 		IF_ENQUEUE(inq, m);
37235379Skfall 	splx(s);
37335379Skfall }
37435379Skfall 
37535379Skfall /*
37635379Skfall  * Convert Ethernet address to printable (loggable) representation.
37735379Skfall  */
37837472Ssklower static char digits[] = "0123456789abcdef";
37935379Skfall char *
38035379Skfall ether_sprintf(ap)
38135379Skfall 	register u_char *ap;
38235379Skfall {
38335379Skfall 	register i;
38435379Skfall 	static char etherbuf[18];
38535379Skfall 	register char *cp = etherbuf;
38635379Skfall 
38735379Skfall 	for (i = 0; i < 6; i++) {
38835379Skfall 		*cp++ = digits[*ap >> 4];
38935379Skfall 		*cp++ = digits[*ap++ & 0xf];
39035379Skfall 		*cp++ = ':';
39135379Skfall 	}
39235379Skfall 	*--cp = 0;
39335379Skfall 	return (etherbuf);
39435379Skfall }
395