xref: /csrg-svn/sys/net/if_ethersubr.c (revision 48455)
135379Skfall /*
239187Ssklower  * Copyright (c) 1982, 1989 Regents of the University of California.
335379Skfall  * All rights reserved.
435379Skfall  *
544464Sbostic  * %sccs.include.redist.c%
635379Skfall  *
7*48455Skarels  *	@(#)if_ethersubr.c	7.13 (Berkeley) 04/20/91
835379Skfall  */
935379Skfall 
1035379Skfall #include "param.h"
1135379Skfall #include "systm.h"
12*48455Skarels #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"
2035379Skfall 
2135379Skfall #include "if.h"
2235379Skfall #include "netisr.h"
2335379Skfall #include "route.h"
2435795Skarels #include "if_llc.h"
2540790Ssklower #include "if_dl.h"
2635379Skfall 
2737517Smckusick #include "machine/mtpr.h"
2835379Skfall 
2935379Skfall #ifdef INET
3035379Skfall #include "../netinet/in.h"
3135379Skfall #include "../netinet/in_var.h"
3245930Sbostic #endif
3335379Skfall #include "../netinet/if_ether.h"
3435379Skfall 
3535379Skfall #ifdef NS
3635379Skfall #include "../netns/ns.h"
3735379Skfall #include "../netns/ns_if.h"
3835379Skfall #endif
3935379Skfall 
4037472Ssklower #ifdef ISO
4137472Ssklower #include "../netiso/argo_debug.h"
4237472Ssklower #include "../netiso/iso.h"
4337472Ssklower #include "../netiso/iso_var.h"
4443073Ssklower #include "../netiso/iso_snpac.h"
4537472Ssklower #endif
4637472Ssklower 
4735379Skfall u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4835795Skarels extern	struct ifnet loif;
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  */
5740790Ssklower ether_output(ifp, m0, dst, rt)
5835379Skfall 	register struct ifnet *ifp;
5935379Skfall 	struct mbuf *m0;
6035379Skfall 	struct sockaddr *dst;
6140790Ssklower 	struct rtentry *rt;
6235379Skfall {
6335795Skarels 	short type;
6435795Skarels 	int s, error = 0;
6535379Skfall  	u_char edst[6];
6635379Skfall 	struct in_addr idst;
6735379Skfall 	register struct mbuf *m = m0;
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 
7335379Skfall 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
7435379Skfall 		error = ENETDOWN;
7535379Skfall 		goto bad;
7635379Skfall 	}
7739187Ssklower 	ifp->if_lastchange = time;
7835379Skfall 	switch (dst->sa_family) {
7935379Skfall 
8035379Skfall #ifdef INET
8135379Skfall 	case AF_INET:
8235379Skfall 		idst = ((struct sockaddr_in *)dst)->sin_addr;
8335379Skfall  		if (!arpresolve(ac, m, &idst, edst, &usetrailers))
8435379Skfall 			return (0);	/* if not yet resolved */
8537472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
8641546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
8735379Skfall 		off = m->m_pkthdr.len - m->m_len;
8835379Skfall 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
8935379Skfall 		    (m->m_flags & M_EXT) == 0 &&
9035379Skfall 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
9135379Skfall 			type = ETHERTYPE_TRAIL + (off>>9);
9235379Skfall 			m->m_data -= 2 * sizeof (u_short);
9335379Skfall 			m->m_len += 2 * sizeof (u_short);
9439187Ssklower 			len += 2 * sizeof (u_short);
9535379Skfall 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
9635379Skfall 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
9735379Skfall 			goto gottrailertype;
9835379Skfall 		}
9935379Skfall 		type = ETHERTYPE_IP;
10035379Skfall 		goto gottype;
10135379Skfall #endif
10235379Skfall #ifdef NS
10335379Skfall 	case AF_NS:
10435379Skfall 		type = ETHERTYPE_NS;
10539187Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
10639187Ssklower 		    (caddr_t)edst, sizeof (edst));
10735795Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
10845632Ssklower 			return (looutput(ifp, m, dst, rt));
10937472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
11041546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
11135379Skfall 		goto gottype;
11235379Skfall #endif
11335795Skarels #ifdef	ISO
11435795Skarels 	case AF_ISO: {
11543073Ssklower 		int	snpalen;
11637472Ssklower 		struct	llc *l;
11737472Ssklower 
11843073Ssklower 	iso_again:
11943073Ssklower 		if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) {
12043073Ssklower 			if (rt->rt_flags & RTF_GATEWAY) {
12143336Ssklower 				if (rt->rt_llinfo) {
12243336Ssklower 					rt = (struct rtentry *)rt->rt_llinfo;
12343073Ssklower 					goto iso_again;
12443073Ssklower 				}
12543073Ssklower 			} else {
12643073Ssklower 				register struct sockaddr_dl *sdl =
12743073Ssklower 					(struct sockaddr_dl *)rt->rt_gateway;
12843073Ssklower 				if (sdl && sdl->sdl_family == AF_LINK
12943073Ssklower 				    && sdl->sdl_alen > 0) {
13043073Ssklower 					bcopy(LLADDR(sdl), (char *)edst,
13143073Ssklower 								sizeof(edst));
13243073Ssklower 					goto iso_resolved;
13343073Ssklower 				}
13440790Ssklower 			}
13540790Ssklower 		}
13643073Ssklower 		if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
13743073Ssklower 					(char *)edst, &snpalen)) > 0)
13843073Ssklower 			goto bad; /* Not Resolved */
13940790Ssklower 	iso_resolved:
14045632Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
14145632Ssklower 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
14245632Ssklower 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
14345632Ssklower 			if (mcopy) {
14445632Ssklower 				eh = mtod(mcopy, struct ether_header *);
14545632Ssklower 				bcopy((caddr_t)edst,
14645632Ssklower 				      (caddr_t)eh->ether_dhost, sizeof (edst));
14745632Ssklower 				bcopy((caddr_t)ac->ac_enaddr,
14845632Ssklower 				      (caddr_t)eh->ether_shost, sizeof (edst));
14945632Ssklower 			}
15045632Ssklower 		}
15135795Skarels 		M_PREPEND(m, 3, M_DONTWAIT);
15237472Ssklower 		if (m == NULL)
15343073Ssklower 			return (0);
15435795Skarels 		type = m->m_pkthdr.len;
15535795Skarels 		l = mtod(m, struct llc *);
15635795Skarels 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
15735795Skarels 		l->llc_control = LLC_UI;
15839187Ssklower 		len += 3;
15935795Skarels 		IFDEBUG(D_ETHER)
16035795Skarels 			int i;
16135795Skarels 			printf("unoutput: sending pkt to: ");
16235795Skarels 			for (i=0; i<6; i++)
16335795Skarels 				printf("%x ", edst[i] & 0xff);
16435795Skarels 			printf("\n");
16535795Skarels 		ENDDEBUG
16635795Skarels 		} goto gottype;
16735795Skarels #endif	ISO
16841546Smckusick #ifdef RMP
16941546Smckusick 	case AF_RMP:
17041546Smckusick 		/*
17141546Smckusick 		 *  This is IEEE 802.3 -- the Ethernet `type' field is
17241546Smckusick 		 *  really a `length' field.
17341546Smckusick 		 */
17441546Smckusick 		type = m->m_len;
17541546Smckusick  		bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
17641546Smckusick 		break;
17741546Smckusick #endif
17841546Smckusick 
17935379Skfall 	case AF_UNSPEC:
18035379Skfall 		eh = (struct ether_header *)dst->sa_data;
18135379Skfall  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
18235379Skfall 		type = eh->ether_type;
18335379Skfall 		goto gottype;
18435379Skfall 
18535379Skfall 	default:
18635379Skfall 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
18735379Skfall 			dst->sa_family);
18835379Skfall 		error = EAFNOSUPPORT;
18935379Skfall 		goto bad;
19035379Skfall 	}
19135379Skfall 
19235379Skfall gottrailertype:
19335379Skfall 	/*
19435379Skfall 	 * Packet to be sent as trailer: move first packet
19535379Skfall 	 * (control information) to end of chain.
19635379Skfall 	 */
19735379Skfall 	while (m->m_next)
19835379Skfall 		m = m->m_next;
19935379Skfall 	m->m_next = m0;
20035379Skfall 	m = m0->m_next;
20135379Skfall 	m0->m_next = 0;
20235379Skfall 
20335379Skfall gottype:
20445632Ssklower 	if (mcopy)
20545632Ssklower 		(void) looutput(ifp, mcopy, dst, rt);
20635379Skfall 	/*
20735379Skfall 	 * Add local net header.  If no space in first mbuf,
20835379Skfall 	 * allocate another.
20935379Skfall 	 */
21035379Skfall 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
21135379Skfall 	if (m == 0) {
21235379Skfall 		error = ENOBUFS;
21335379Skfall 		goto bad;
21435379Skfall 	}
21535379Skfall 	eh = mtod(m, struct ether_header *);
21635795Skarels 	type = htons((u_short)type);
21735795Skarels 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
21835795Skarels 		sizeof(eh->ether_type));
21935379Skfall  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
22035379Skfall  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
22135379Skfall 	    sizeof(eh->ether_shost));
22245632Ssklower 	s = splimp();
22335379Skfall 	/*
22435379Skfall 	 * Queue message on interface, and start output if interface
22535379Skfall 	 * not yet active.
22635379Skfall 	 */
22735379Skfall 	if (IF_QFULL(&ifp->if_snd)) {
22835379Skfall 		IF_DROP(&ifp->if_snd);
22935379Skfall 		splx(s);
23035795Skarels 		error = ENOBUFS;
23135795Skarels 		goto bad;
23235379Skfall 	}
23335379Skfall 	IF_ENQUEUE(&ifp->if_snd, m);
23435379Skfall 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
23538845Sroot 		(*ifp->if_start)(ifp);
23635379Skfall 	splx(s);
23739187Ssklower 	ifp->if_obytes += len + sizeof (struct ether_header);
23839187Ssklower 	if (edst[0] & 1)
23939187Ssklower 		ifp->if_omcasts++;
24035795Skarels 	return (error);
24135379Skfall 
24235379Skfall bad:
24335795Skarels 	if (m)
24435795Skarels 		m_freem(m);
24535379Skfall 	return (error);
24635379Skfall }
24735379Skfall 
24835379Skfall /*
24938845Sroot  * Process a received Ethernet packet;
25038845Sroot  * the packet is in the mbuf chain m without
25138845Sroot  * the ether header, which is provided separately.
25235379Skfall  */
25337472Ssklower ether_input(ifp, eh, m)
25435379Skfall 	struct ifnet *ifp;
25535379Skfall 	register struct ether_header *eh;
25635379Skfall 	struct mbuf *m;
25735379Skfall {
25835379Skfall 	register struct ifqueue *inq;
25935795Skarels 	register struct llc *l;
26035379Skfall 	int s;
26135379Skfall 
26239187Ssklower 	ifp->if_lastchange = time;
26339187Ssklower 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
26435795Skarels 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
26535379Skfall 	    sizeof(etherbroadcastaddr)) == 0)
26635379Skfall 		m->m_flags |= M_BCAST;
26735795Skarels 	else if (eh->ether_dhost[0] & 1)
26835795Skarels 		m->m_flags |= M_MCAST;
26939187Ssklower 	if (m->m_flags & (M_BCAST|M_MCAST))
27039187Ssklower 		ifp->if_imcasts++;
27135379Skfall 
27235379Skfall 	switch (eh->ether_type) {
27335379Skfall #ifdef INET
27435379Skfall 	case ETHERTYPE_IP:
27535379Skfall 		schednetisr(NETISR_IP);
27635379Skfall 		inq = &ipintrq;
27735379Skfall 		break;
27835379Skfall 
27935379Skfall 	case ETHERTYPE_ARP:
28035379Skfall 		arpinput((struct arpcom *)ifp, m);
28135379Skfall 		return;
28235379Skfall #endif
28335379Skfall #ifdef NS
28435379Skfall 	case ETHERTYPE_NS:
28535379Skfall 		schednetisr(NETISR_NS);
28635379Skfall 		inq = &nsintrq;
28735379Skfall 		break;
28835379Skfall 
28935379Skfall #endif
29035379Skfall 	default:
29138845Sroot #ifdef	ISO
29237472Ssklower 		if (eh->ether_type > ETHERMTU)
29335795Skarels 			goto dropanyway;
29435795Skarels 		l = mtod(m, struct llc *);
29535795Skarels 		switch (l->llc_control) {
29635795Skarels 		case LLC_UI:
29735795Skarels 		/* LLC_UI_P forbidden in class 1 service */
29835795Skarels 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
29935795Skarels 			(l->llc_ssap == LLC_ISO_LSAP)) {
30035795Skarels 				/* LSAP for ISO */
30145632Ssklower 			if (m->m_pkthdr.len > eh->ether_type)
30245632Ssklower 				m_adj(m, eh->ether_type - m->m_pkthdr.len);
30338845Sroot 			m->m_data += 3;		/* XXX */
30438845Sroot 			m->m_len -= 3;		/* XXX */
30538845Sroot 			m->m_pkthdr.len -= 3;	/* XXX */
30637472Ssklower 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
30737472Ssklower 			if (m == 0)
30837472Ssklower 				return;
30937472Ssklower 			*mtod(m, struct ether_header *) = *eh;
31037472Ssklower 			IFDEBUG(D_ETHER)
31137472Ssklower 			    printf("clnp packet");
31237472Ssklower 			ENDDEBUG
31337472Ssklower 			schednetisr(NETISR_ISO);
31435795Skarels 			inq = &clnlintrq;
31538845Sroot 			break;
31635795Skarels 		    }
31738845Sroot 		    goto dropanyway;
31838845Sroot 
31935795Skarels 		case LLC_XID:
32035795Skarels 		case LLC_XID_P:
32135795Skarels 		    if(m->m_len < 6)
32235795Skarels 			goto dropanyway;
32335795Skarels 		    l->llc_window = 0;
32435795Skarels 		    l->llc_fid = 9;
32535795Skarels 		    l->llc_class = 1;
32635795Skarels 		    l->llc_dsap = l->llc_ssap = 0;
32735795Skarels 		    /* Fall through to */
32835795Skarels 		case LLC_TEST:
32935795Skarels 		case LLC_TEST_P:
33035795Skarels 		{
33135795Skarels 		    struct sockaddr sa;
33235795Skarels 		    register struct ether_header *eh2;
33335795Skarels 		    int i;
33435795Skarels 		    u_char c = l->llc_dsap;
33535795Skarels 		    l->llc_dsap = l->llc_ssap;
33635795Skarels 		    l->llc_ssap = c;
33737472Ssklower 		    if (m->m_flags & (M_BCAST | M_MCAST))
33837472Ssklower 			bcopy((caddr_t)ac->ac_enaddr,
33937472Ssklower 			      (caddr_t)eh->ether_dhost, 6);
34035795Skarels 		    sa.sa_family = AF_UNSPEC;
34137472Ssklower 		    sa.sa_len = sizeof(sa);
34235795Skarels 		    eh2 = (struct ether_header *)sa.sa_data;
34335795Skarels 		    for (i = 0; i < 6; i++) {
34435795Skarels 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
34535795Skarels 			eh2->ether_dhost[i] =
34635795Skarels 				eh->ether_dhost[i] = eh->ether_shost[i];
34735795Skarels 			eh->ether_shost[i] = c;
34835795Skarels 		    }
34935795Skarels 		    ifp->if_output(ifp, m, &sa);
35035795Skarels 		    return;
35135795Skarels 		}
35235795Skarels 		dropanyway:
35335795Skarels 		default:
35435795Skarels 		    m_freem(m);
35535795Skarels 		    return;
35635795Skarels 	    }
35738845Sroot #else
35838845Sroot 	    m_freem(m);
35938845Sroot 	    return;
36038845Sroot #endif	ISO
36135379Skfall 	}
36235379Skfall 
36335379Skfall 	s = splimp();
36435379Skfall 	if (IF_QFULL(inq)) {
36535379Skfall 		IF_DROP(inq);
36635379Skfall 		m_freem(m);
36735379Skfall 	} else
36835379Skfall 		IF_ENQUEUE(inq, m);
36935379Skfall 	splx(s);
37035379Skfall }
37135379Skfall 
37235379Skfall /*
37335379Skfall  * Convert Ethernet address to printable (loggable) representation.
37435379Skfall  */
37537472Ssklower static char digits[] = "0123456789abcdef";
37635379Skfall char *
37735379Skfall ether_sprintf(ap)
37835379Skfall 	register u_char *ap;
37935379Skfall {
38035379Skfall 	register i;
38135379Skfall 	static char etherbuf[18];
38235379Skfall 	register char *cp = etherbuf;
38335379Skfall 
38435379Skfall 	for (i = 0; i < 6; i++) {
38535379Skfall 		*cp++ = digits[*ap >> 4];
38635379Skfall 		*cp++ = digits[*ap++ & 0xf];
38735379Skfall 		*cp++ = ':';
38835379Skfall 	}
38935379Skfall 	*--cp = 0;
39035379Skfall 	return (etherbuf);
39135379Skfall }
392