xref: /csrg-svn/sys/net/if_ethersubr.c (revision 43073)
135379Skfall /*
239187Ssklower  * Copyright (c) 1982, 1989 Regents of the University of California.
335379Skfall  * All rights reserved.
435379Skfall  *
535379Skfall  * Redistribution and use in source and binary forms are permitted
635379Skfall  * provided that the above copyright notice and this paragraph are
735379Skfall  * duplicated in all such forms and that any documentation,
835379Skfall  * advertising materials, and other materials related to such
935379Skfall  * distribution and use acknowledge that the software was developed
1035379Skfall  * by the University of California, Berkeley.  The name of the
1135379Skfall  * University may not be used to endorse or promote products derived
1235379Skfall  * from this software without specific prior written permission.
1335379Skfall  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435379Skfall  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535379Skfall  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1635379Skfall  *
17*43073Ssklower  *	@(#)if_ethersubr.c	7.8 (Berkeley) 06/09/90
1835379Skfall  */
1935379Skfall 
2035379Skfall #include "param.h"
2135379Skfall #include "systm.h"
2235379Skfall #include "malloc.h"
2335379Skfall #include "mbuf.h"
2435379Skfall #include "protosw.h"
2535379Skfall #include "socket.h"
2635379Skfall #include "ioctl.h"
2735379Skfall #include "errno.h"
2835379Skfall #include "syslog.h"
2935379Skfall 
3035379Skfall #include "if.h"
3135379Skfall #include "netisr.h"
3235379Skfall #include "route.h"
3335795Skarels #include "if_llc.h"
3440790Ssklower #include "if_dl.h"
3535379Skfall 
3637517Smckusick #include "machine/mtpr.h"
3735379Skfall 
3835379Skfall #ifdef INET
3935379Skfall #include "../netinet/in.h"
4035379Skfall #include "../netinet/in_var.h"
4135379Skfall #include "../netinet/if_ether.h"
4235379Skfall #endif
4335379Skfall 
4435379Skfall #ifdef NS
4535379Skfall #include "../netns/ns.h"
4635379Skfall #include "../netns/ns_if.h"
4735379Skfall #endif
4835379Skfall 
4937472Ssklower #ifdef ISO
5037472Ssklower #include "../netiso/argo_debug.h"
5137472Ssklower #include "../netiso/iso.h"
5237472Ssklower #include "../netiso/iso_var.h"
53*43073Ssklower #include "../netiso/iso_snpac.h"
5437472Ssklower #endif
5537472Ssklower 
5635379Skfall u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5735795Skarels extern	struct ifnet loif;
5835379Skfall 
5935379Skfall /*
6035379Skfall  * Ethernet output routine.
6135379Skfall  * Encapsulate a packet of type family for the local net.
6235379Skfall  * Use trailer local net encapsulation if enough data in first
6335379Skfall  * packet leaves a multiple of 512 bytes of data in remainder.
6435379Skfall  * Assumes that ifp is actually pointer to arpcom structure.
6535379Skfall  */
6640790Ssklower ether_output(ifp, m0, dst, rt)
6735379Skfall 	register struct ifnet *ifp;
6835379Skfall 	struct mbuf *m0;
6935379Skfall 	struct sockaddr *dst;
7040790Ssklower 	struct rtentry *rt;
7135379Skfall {
7235795Skarels 	short type;
7335795Skarels 	int s, error = 0;
7435379Skfall  	u_char edst[6];
7535379Skfall 	struct in_addr idst;
7635379Skfall 	register struct mbuf *m = m0;
7735795Skarels 	struct mbuf *mcopy = (struct mbuf *)0;
7835379Skfall 	register struct ether_header *eh;
7939187Ssklower 	int usetrailers, off, len = m->m_pkthdr.len;
8039187Ssklower 	extern struct timeval time;
8135379Skfall #define	ac ((struct arpcom *)ifp)
8235379Skfall 
8335379Skfall 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
8435379Skfall 		error = ENETDOWN;
8535379Skfall 		goto bad;
8635379Skfall 	}
8739187Ssklower 	ifp->if_lastchange = time;
8835379Skfall 	switch (dst->sa_family) {
8935379Skfall 
9035379Skfall #ifdef INET
9135379Skfall 	case AF_INET:
9235379Skfall 		idst = ((struct sockaddr_in *)dst)->sin_addr;
9335379Skfall  		if (!arpresolve(ac, m, &idst, edst, &usetrailers))
9435379Skfall 			return (0);	/* if not yet resolved */
9537472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
9641546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
9735379Skfall 		off = m->m_pkthdr.len - m->m_len;
9835379Skfall 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
9935379Skfall 		    (m->m_flags & M_EXT) == 0 &&
10035379Skfall 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
10135379Skfall 			type = ETHERTYPE_TRAIL + (off>>9);
10235379Skfall 			m->m_data -= 2 * sizeof (u_short);
10335379Skfall 			m->m_len += 2 * sizeof (u_short);
10439187Ssklower 			len += 2 * sizeof (u_short);
10535379Skfall 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
10635379Skfall 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
10735379Skfall 			goto gottrailertype;
10835379Skfall 		}
10935379Skfall 		type = ETHERTYPE_IP;
11035379Skfall 		goto gottype;
11135379Skfall #endif
11235379Skfall #ifdef NS
11335379Skfall 	case AF_NS:
11435379Skfall 		type = ETHERTYPE_NS;
11539187Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
11639187Ssklower 		    (caddr_t)edst, sizeof (edst));
11735795Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
118*43073Ssklower 			return (looutput(&loif, m, dst));
11937472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
12041546Smckusick 			mcopy = m_copy(m, 0, (int)M_COPYALL);
12135379Skfall 		goto gottype;
12235379Skfall #endif
12335795Skarels #ifdef	ISO
12435795Skarels 	case AF_ISO: {
125*43073Ssklower 		int	snpalen;
12637472Ssklower 		struct	llc *l;
12737472Ssklower 
128*43073Ssklower 	iso_again:
129*43073Ssklower 		if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) {
130*43073Ssklower 			if (rt->rt_flags & RTF_GATEWAY) {
131*43073Ssklower 				register struct llinfo_llc *lc =
132*43073Ssklower 					(struct llinfo_llc *)rt->rt_llinfo;
133*43073Ssklower 				if (lc && lc->lc_rtgate) {
134*43073Ssklower 					rt = lc->lc_rtgate;
135*43073Ssklower 					goto iso_again;
136*43073Ssklower 				}
137*43073Ssklower 			} else {
138*43073Ssklower 				register struct sockaddr_dl *sdl =
139*43073Ssklower 					(struct sockaddr_dl *)rt->rt_gateway;
140*43073Ssklower 				if (sdl && sdl->sdl_family == AF_LINK
141*43073Ssklower 				    && sdl->sdl_alen > 0) {
142*43073Ssklower 					bcopy(LLADDR(sdl), (char *)edst,
143*43073Ssklower 								sizeof(edst));
144*43073Ssklower 					goto iso_resolved;
145*43073Ssklower 				}
14640790Ssklower 			}
14740790Ssklower 		}
148*43073Ssklower 		if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
149*43073Ssklower 					(char *)edst, &snpalen)) > 0)
150*43073Ssklower 			goto bad; /* Not Resolved */
15140790Ssklower 	iso_resolved:
15235795Skarels 		M_PREPEND(m, 3, M_DONTWAIT);
15337472Ssklower 		if (m == NULL)
154*43073Ssklower 			return (0);
15535795Skarels 		type = m->m_pkthdr.len;
15635795Skarels 		l = mtod(m, struct llc *);
15735795Skarels 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
15835795Skarels 		l->llc_control = LLC_UI;
15939187Ssklower 		len += 3;
16035795Skarels 		IFDEBUG(D_ETHER)
16135795Skarels 			int i;
16235795Skarels 			printf("unoutput: sending pkt to: ");
16335795Skarels 			for (i=0; i<6; i++)
16435795Skarels 				printf("%x ", edst[i] & 0xff);
16535795Skarels 			printf("\n");
16635795Skarels 		ENDDEBUG
16735795Skarels 		} goto gottype;
16835795Skarels #endif	ISO
16941546Smckusick #ifdef RMP
17041546Smckusick 	case AF_RMP:
17141546Smckusick 		/*
17241546Smckusick 		 *  This is IEEE 802.3 -- the Ethernet `type' field is
17341546Smckusick 		 *  really a `length' field.
17441546Smckusick 		 */
17541546Smckusick 		type = m->m_len;
17641546Smckusick  		bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
17741546Smckusick 		break;
17841546Smckusick #endif
17941546Smckusick 
18035379Skfall 	case AF_UNSPEC:
18135379Skfall 		eh = (struct ether_header *)dst->sa_data;
18235379Skfall  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
18335379Skfall 		type = eh->ether_type;
18435379Skfall 		goto gottype;
18535379Skfall 
18635379Skfall 	default:
18735379Skfall 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
18835379Skfall 			dst->sa_family);
18935379Skfall 		error = EAFNOSUPPORT;
19035379Skfall 		goto bad;
19135379Skfall 	}
19235379Skfall 
19335379Skfall gottrailertype:
19435379Skfall 	/*
19535379Skfall 	 * Packet to be sent as trailer: move first packet
19635379Skfall 	 * (control information) to end of chain.
19735379Skfall 	 */
19835379Skfall 	while (m->m_next)
19935379Skfall 		m = m->m_next;
20035379Skfall 	m->m_next = m0;
20135379Skfall 	m = m0->m_next;
20235379Skfall 	m0->m_next = 0;
20335379Skfall 
20435379Skfall gottype:
20535379Skfall 	/*
20635379Skfall 	 * Add local net header.  If no space in first mbuf,
20735379Skfall 	 * allocate another.
20835379Skfall 	 */
20935379Skfall 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
21035379Skfall 	if (m == 0) {
21135379Skfall 		error = ENOBUFS;
21235379Skfall 		goto bad;
21335379Skfall 	}
21435379Skfall 	eh = mtod(m, struct ether_header *);
21535795Skarels 	type = htons((u_short)type);
21635795Skarels 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
21735795Skarels 		sizeof(eh->ether_type));
21835379Skfall  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
21935379Skfall  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
22035379Skfall 	    sizeof(eh->ether_shost));
22135379Skfall 	/*
22235379Skfall 	 * Queue message on interface, and start output if interface
22335379Skfall 	 * not yet active.
22435379Skfall 	 */
22535379Skfall 	s = splimp();
22635379Skfall 	if (IF_QFULL(&ifp->if_snd)) {
22735379Skfall 		IF_DROP(&ifp->if_snd);
22835379Skfall 		splx(s);
22935795Skarels 		error = ENOBUFS;
23035795Skarels 		goto bad;
23135379Skfall 	}
23235379Skfall 	IF_ENQUEUE(&ifp->if_snd, m);
23335379Skfall 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
23438845Sroot 		(*ifp->if_start)(ifp);
23535379Skfall 	splx(s);
23635795Skarels 	if (mcopy)
23735795Skarels 		(void) looutput(&loif, mcopy, dst);
23839187Ssklower 	ifp->if_obytes += len + sizeof (struct ether_header);
23939187Ssklower 	if (edst[0] & 1)
24039187Ssklower 		ifp->if_omcasts++;
24135795Skarels 	return (error);
24235379Skfall 
24335379Skfall bad:
24435795Skarels 	if (mcopy)
24535795Skarels 		m_freem(mcopy);
24635795Skarels 	if (m)
24735795Skarels 		m_freem(m);
24835379Skfall 	return (error);
24935379Skfall }
25035379Skfall 
25135379Skfall /*
25238845Sroot  * Process a received Ethernet packet;
25338845Sroot  * the packet is in the mbuf chain m without
25438845Sroot  * the ether header, which is provided separately.
25535379Skfall  */
25637472Ssklower ether_input(ifp, eh, m)
25735379Skfall 	struct ifnet *ifp;
25835379Skfall 	register struct ether_header *eh;
25935379Skfall 	struct mbuf *m;
26035379Skfall {
26135379Skfall 	register struct ifqueue *inq;
26235795Skarels 	register struct llc *l;
26335379Skfall 	int s;
26435379Skfall 
26539187Ssklower 	ifp->if_lastchange = time;
26639187Ssklower 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
26735795Skarels 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
26835379Skfall 	    sizeof(etherbroadcastaddr)) == 0)
26935379Skfall 		m->m_flags |= M_BCAST;
27035795Skarels 	else if (eh->ether_dhost[0] & 1)
27135795Skarels 		m->m_flags |= M_MCAST;
27239187Ssklower 	if (m->m_flags & (M_BCAST|M_MCAST))
27339187Ssklower 		ifp->if_imcasts++;
27435379Skfall 
27535379Skfall 	switch (eh->ether_type) {
27635379Skfall #ifdef INET
27735379Skfall 	case ETHERTYPE_IP:
27835379Skfall 		schednetisr(NETISR_IP);
27935379Skfall 		inq = &ipintrq;
28035379Skfall 		break;
28135379Skfall 
28235379Skfall 	case ETHERTYPE_ARP:
28335379Skfall 		arpinput((struct arpcom *)ifp, m);
28435379Skfall 		return;
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 */
30438845Sroot 			m->m_data += 3;		/* XXX */
30538845Sroot 			m->m_len -= 3;		/* XXX */
30638845Sroot 			m->m_pkthdr.len -= 3;	/* XXX */
30737472Ssklower 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
30837472Ssklower 			if (m == 0)
30937472Ssklower 				return;
31037472Ssklower 			*mtod(m, struct ether_header *) = *eh;
31137472Ssklower 			IFDEBUG(D_ETHER)
31237472Ssklower 			    printf("clnp packet");
31337472Ssklower 			ENDDEBUG
31437472Ssklower 			schednetisr(NETISR_ISO);
31535795Skarels 			inq = &clnlintrq;
31638845Sroot 			break;
31735795Skarels 		    }
31838845Sroot 		    goto dropanyway;
31938845Sroot 
32035795Skarels 		case LLC_XID:
32135795Skarels 		case LLC_XID_P:
32235795Skarels 		    if(m->m_len < 6)
32335795Skarels 			goto dropanyway;
32435795Skarels 		    l->llc_window = 0;
32535795Skarels 		    l->llc_fid = 9;
32635795Skarels 		    l->llc_class = 1;
32735795Skarels 		    l->llc_dsap = l->llc_ssap = 0;
32835795Skarels 		    /* Fall through to */
32935795Skarels 		case LLC_TEST:
33035795Skarels 		case LLC_TEST_P:
33135795Skarels 		{
33235795Skarels 		    struct sockaddr sa;
33335795Skarels 		    register struct ether_header *eh2;
33435795Skarels 		    int i;
33535795Skarels 		    u_char c = l->llc_dsap;
33635795Skarels 		    l->llc_dsap = l->llc_ssap;
33735795Skarels 		    l->llc_ssap = c;
33837472Ssklower 		    if (m->m_flags & (M_BCAST | M_MCAST))
33937472Ssklower 			bcopy((caddr_t)ac->ac_enaddr,
34037472Ssklower 			      (caddr_t)eh->ether_dhost, 6);
34135795Skarels 		    sa.sa_family = AF_UNSPEC;
34237472Ssklower 		    sa.sa_len = sizeof(sa);
34335795Skarels 		    eh2 = (struct ether_header *)sa.sa_data;
34435795Skarels 		    for (i = 0; i < 6; i++) {
34535795Skarels 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
34635795Skarels 			eh2->ether_dhost[i] =
34735795Skarels 				eh->ether_dhost[i] = eh->ether_shost[i];
34835795Skarels 			eh->ether_shost[i] = c;
34935795Skarels 		    }
35035795Skarels 		    ifp->if_output(ifp, m, &sa);
35135795Skarels 		    return;
35235795Skarels 		}
35335795Skarels 		dropanyway:
35435795Skarels 		default:
35535795Skarels 		    m_freem(m);
35635795Skarels 		    return;
35735795Skarels 	    }
35838845Sroot #else
35938845Sroot 	    m_freem(m);
36038845Sroot 	    return;
36138845Sroot #endif	ISO
36235379Skfall 	}
36335379Skfall 
36435379Skfall 	s = splimp();
36535379Skfall 	if (IF_QFULL(inq)) {
36635379Skfall 		IF_DROP(inq);
36735379Skfall 		m_freem(m);
36835379Skfall 	} else
36935379Skfall 		IF_ENQUEUE(inq, m);
37035379Skfall 	splx(s);
37135379Skfall }
37235379Skfall 
37335379Skfall /*
37435379Skfall  * Convert Ethernet address to printable (loggable) representation.
37535379Skfall  */
37637472Ssklower static char digits[] = "0123456789abcdef";
37735379Skfall char *
37835379Skfall ether_sprintf(ap)
37935379Skfall 	register u_char *ap;
38035379Skfall {
38135379Skfall 	register i;
38235379Skfall 	static char etherbuf[18];
38335379Skfall 	register char *cp = etherbuf;
38435379Skfall 
38535379Skfall 	for (i = 0; i < 6; i++) {
38635379Skfall 		*cp++ = digits[*ap >> 4];
38735379Skfall 		*cp++ = digits[*ap++ & 0xf];
38835379Skfall 		*cp++ = ':';
38935379Skfall 	}
39035379Skfall 	*--cp = 0;
39135379Skfall 	return (etherbuf);
39235379Skfall }
393