xref: /csrg-svn/sys/net/if_ethersubr.c (revision 38845)
135379Skfall /*
235379Skfall  * Copyright (c) 1982, 1986, 1988 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*38845Sroot  *	@(#)if_ethersubr.c	7.4 (Berkeley) 08/29/89
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"
3435379Skfall 
3537517Smckusick #include "machine/mtpr.h"
3635379Skfall 
3735379Skfall #ifdef INET
3835379Skfall #include "../netinet/in.h"
3935379Skfall #include "../netinet/in_var.h"
4035379Skfall #include "../netinet/if_ether.h"
4135379Skfall #endif
4235379Skfall 
4335379Skfall #ifdef NS
4435379Skfall #include "../netns/ns.h"
4535379Skfall #include "../netns/ns_if.h"
4635379Skfall #endif
4735379Skfall 
4837472Ssklower #ifdef ISO
4937472Ssklower #include "../netiso/argo_debug.h"
5037472Ssklower #include "../netiso/iso.h"
5137472Ssklower #include "../netiso/iso_var.h"
5237472Ssklower #endif
5337472Ssklower 
5435379Skfall u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5535795Skarels extern	struct ifnet loif;
5635379Skfall 
5735379Skfall /*
5835379Skfall  * Ethernet output routine.
5935379Skfall  * Encapsulate a packet of type family for the local net.
6035379Skfall  * Use trailer local net encapsulation if enough data in first
6135379Skfall  * packet leaves a multiple of 512 bytes of data in remainder.
6235379Skfall  * Assumes that ifp is actually pointer to arpcom structure.
6335379Skfall  */
6437472Ssklower ether_output(ifp, m0, dst)
6535379Skfall 	register struct ifnet *ifp;
6635379Skfall 	struct mbuf *m0;
6735379Skfall 	struct sockaddr *dst;
6835379Skfall {
6935795Skarels 	short type;
7035795Skarels 	int s, error = 0;
7135379Skfall  	u_char edst[6];
7235379Skfall 	struct in_addr idst;
7335379Skfall 	register struct mbuf *m = m0;
7435795Skarels 	struct mbuf *mcopy = (struct mbuf *)0;
7535379Skfall 	register struct ether_header *eh;
7635795Skarels 	int usetrailers, off;
7735379Skfall #define	ac ((struct arpcom *)ifp)
7835379Skfall 
7935379Skfall 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
8035379Skfall 		error = ENETDOWN;
8135379Skfall 		goto bad;
8235379Skfall 	}
8337472Ssklower 	if (ifp->if_flags & IFF_SIMPLEX && dst->sa_family != AF_UNSPEC &&
8437472Ssklower 	    !bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr, sizeof (edst)))
8537472Ssklower 		mcopy = m_copy(m, 0, (int)M_COPYALL);
8635379Skfall 	switch (dst->sa_family) {
8735379Skfall 
8835379Skfall #ifdef INET
8935379Skfall 	case AF_INET:
9035379Skfall 		idst = ((struct sockaddr_in *)dst)->sin_addr;
9135379Skfall  		if (!arpresolve(ac, m, &idst, edst, &usetrailers))
9235379Skfall 			return (0);	/* if not yet resolved */
9337472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
9437472Ssklower 		    mcopy = m_copy(m, 0, (int)M_COPYALL);
9535379Skfall 		off = m->m_pkthdr.len - m->m_len;
9635379Skfall 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
9735379Skfall 		    (m->m_flags & M_EXT) == 0 &&
9835379Skfall 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
9935379Skfall 			type = ETHERTYPE_TRAIL + (off>>9);
10035379Skfall 			m->m_data -= 2 * sizeof (u_short);
10135379Skfall 			m->m_len += 2 * sizeof (u_short);
10235379Skfall 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
10335379Skfall 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
10435379Skfall 			goto gottrailertype;
10535379Skfall 		}
10635379Skfall 		type = ETHERTYPE_IP;
10735379Skfall 		goto gottype;
10835379Skfall #endif
10935379Skfall #ifdef NS
11035379Skfall 	case AF_NS:
11135379Skfall 		type = ETHERTYPE_NS;
11235795Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
11335795Skarels 			return(looutput(&loif, m, dst));
11435379Skfall  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
11535795Skarels 		    (caddr_t)edst, sizeof (edst));
11637472Ssklower 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
11737472Ssklower 		    mcopy = m_copy(m, 0, (int)M_COPYALL);
11835379Skfall 		goto gottype;
11935379Skfall #endif
12035795Skarels #ifdef	ISO
12135795Skarels 	case AF_ISO: {
12237472Ssklower 		int	len;
12335795Skarels 		int	ret;
12437472Ssklower 		struct	llc *l;
12537472Ssklower 
12637472Ssklower 		if ((ret = iso_tryloopback(m, (struct sockaddr_iso *)dst)) >= 0)
12737472Ssklower 			return (ret);
12837472Ssklower 		ret = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
12937472Ssklower 					(char *)edst, &len);
13037472Ssklower 		if (ret > 0) {
13137472Ssklower 			m_freem(m); /* Not Resolved */
13237472Ssklower 			return(ret);
13335795Skarels 		}
13435795Skarels 		M_PREPEND(m, 3, M_DONTWAIT);
13537472Ssklower 		if (m == NULL)
13635795Skarels 			return(0);
13735795Skarels 		type = m->m_pkthdr.len;
13835795Skarels 		l = mtod(m, struct llc *);
13935795Skarels 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
14035795Skarels 		l->llc_control = LLC_UI;
14135795Skarels 		IFDEBUG(D_ETHER)
14235795Skarels 			int i;
14335795Skarels 			printf("unoutput: sending pkt to: ");
14435795Skarels 			for (i=0; i<6; i++)
14535795Skarels 				printf("%x ", edst[i] & 0xff);
14635795Skarels 			printf("\n");
14735795Skarels 		ENDDEBUG
14835795Skarels 		} goto gottype;
14935795Skarels #endif	ISO
15035379Skfall 	case AF_UNSPEC:
15135379Skfall 		eh = (struct ether_header *)dst->sa_data;
15235379Skfall  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
15335379Skfall 		type = eh->ether_type;
15435379Skfall 		goto gottype;
15535379Skfall 
15635379Skfall 	default:
15735379Skfall 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
15835379Skfall 			dst->sa_family);
15935379Skfall 		error = EAFNOSUPPORT;
16035379Skfall 		goto bad;
16135379Skfall 	}
16235379Skfall 
16335379Skfall gottrailertype:
16435379Skfall 	/*
16535379Skfall 	 * Packet to be sent as trailer: move first packet
16635379Skfall 	 * (control information) to end of chain.
16735379Skfall 	 */
16835379Skfall 	while (m->m_next)
16935379Skfall 		m = m->m_next;
17035379Skfall 	m->m_next = m0;
17135379Skfall 	m = m0->m_next;
17235379Skfall 	m0->m_next = 0;
17335379Skfall 
17435379Skfall gottype:
17535379Skfall 	/*
17635379Skfall 	 * Add local net header.  If no space in first mbuf,
17735379Skfall 	 * allocate another.
17835379Skfall 	 */
17935379Skfall 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
18035379Skfall 	if (m == 0) {
18135379Skfall 		error = ENOBUFS;
18235379Skfall 		goto bad;
18335379Skfall 	}
18435379Skfall 	eh = mtod(m, struct ether_header *);
18535795Skarels 	type = htons((u_short)type);
18635795Skarels 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
18735795Skarels 		sizeof(eh->ether_type));
18835379Skfall  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
18935379Skfall  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
19035379Skfall 	    sizeof(eh->ether_shost));
19135379Skfall 	/*
19235379Skfall 	 * Queue message on interface, and start output if interface
19335379Skfall 	 * not yet active.
19435379Skfall 	 */
19535379Skfall 	s = splimp();
19635379Skfall 	if (IF_QFULL(&ifp->if_snd)) {
19735379Skfall 		IF_DROP(&ifp->if_snd);
19835379Skfall 		splx(s);
19935795Skarels 		error = ENOBUFS;
20035795Skarels 		goto bad;
20135379Skfall 	}
20235379Skfall 	IF_ENQUEUE(&ifp->if_snd, m);
20335379Skfall 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
204*38845Sroot 		(*ifp->if_start)(ifp);
20535379Skfall 	splx(s);
20635795Skarels 	if (mcopy)
20735795Skarels 		(void) looutput(&loif, mcopy, dst);
20835795Skarels 	return (error);
20935379Skfall 
21035379Skfall bad:
21135795Skarels 	if (mcopy)
21235795Skarels 		m_freem(mcopy);
21335795Skarels 	if (m)
21435795Skarels 		m_freem(m);
21535379Skfall 	return (error);
21635379Skfall }
21735379Skfall 
21835379Skfall /*
219*38845Sroot  * Process a received Ethernet packet;
220*38845Sroot  * the packet is in the mbuf chain m without
221*38845Sroot  * the ether header, which is provided separately.
22235379Skfall  */
22337472Ssklower ether_input(ifp, eh, m)
22435379Skfall 	struct ifnet *ifp;
22535379Skfall 	register struct ether_header *eh;
22635379Skfall 	struct mbuf *m;
22735379Skfall {
22835379Skfall 	register struct ifqueue *inq;
22935795Skarels 	register struct llc *l;
23035379Skfall 	int s;
23135379Skfall 
23235795Skarels 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
23335379Skfall 	    sizeof(etherbroadcastaddr)) == 0)
23435379Skfall 		m->m_flags |= M_BCAST;
23535795Skarels 	else if (eh->ether_dhost[0] & 1)
23635795Skarels 		m->m_flags |= M_MCAST;
23735379Skfall 
23835379Skfall 	switch (eh->ether_type) {
23935379Skfall #ifdef INET
24035379Skfall 	case ETHERTYPE_IP:
24135379Skfall 		schednetisr(NETISR_IP);
24235379Skfall 		inq = &ipintrq;
24335379Skfall 		break;
24435379Skfall 
24535379Skfall 	case ETHERTYPE_ARP:
24635379Skfall 		arpinput((struct arpcom *)ifp, m);
24735379Skfall 		return;
24835379Skfall #endif
24935379Skfall #ifdef NS
25035379Skfall 	case ETHERTYPE_NS:
25135379Skfall 		schednetisr(NETISR_NS);
25235379Skfall 		inq = &nsintrq;
25335379Skfall 		break;
25435379Skfall 
25535379Skfall #endif
25635379Skfall 	default:
257*38845Sroot #ifdef	ISO
25837472Ssklower 		if (eh->ether_type > ETHERMTU)
25935795Skarels 			goto dropanyway;
26035795Skarels 		l = mtod(m, struct llc *);
26135795Skarels 		switch (l->llc_control) {
26235795Skarels 		case LLC_UI:
26335795Skarels 		/* LLC_UI_P forbidden in class 1 service */
26435795Skarels 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
26535795Skarels 			(l->llc_ssap == LLC_ISO_LSAP)) {
26635795Skarels 				/* LSAP for ISO */
267*38845Sroot 			m->m_data += 3;		/* XXX */
268*38845Sroot 			m->m_len -= 3;		/* XXX */
269*38845Sroot 			m->m_pkthdr.len -= 3;	/* XXX */
27037472Ssklower 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
27137472Ssklower 			if (m == 0)
27237472Ssklower 				return;
27337472Ssklower 			*mtod(m, struct ether_header *) = *eh;
27437472Ssklower 			IFDEBUG(D_ETHER)
27537472Ssklower 			    printf("clnp packet");
27637472Ssklower 			ENDDEBUG
27737472Ssklower 			schednetisr(NETISR_ISO);
27835795Skarels 			inq = &clnlintrq;
279*38845Sroot 			break;
28035795Skarels 		    }
281*38845Sroot 		    goto dropanyway;
282*38845Sroot 
28335795Skarels 		case LLC_XID:
28435795Skarels 		case LLC_XID_P:
28535795Skarels 		    if(m->m_len < 6)
28635795Skarels 			goto dropanyway;
28735795Skarels 		    l->llc_window = 0;
28835795Skarels 		    l->llc_fid = 9;
28935795Skarels 		    l->llc_class = 1;
29035795Skarels 		    l->llc_dsap = l->llc_ssap = 0;
29135795Skarels 		    /* Fall through to */
29235795Skarels 		case LLC_TEST:
29335795Skarels 		case LLC_TEST_P:
29435795Skarels 		{
29535795Skarels 		    struct sockaddr sa;
29635795Skarels 		    register struct ether_header *eh2;
29735795Skarels 		    int i;
29835795Skarels 		    u_char c = l->llc_dsap;
29935795Skarels 		    l->llc_dsap = l->llc_ssap;
30035795Skarels 		    l->llc_ssap = c;
30137472Ssklower 		    if (m->m_flags & (M_BCAST | M_MCAST))
30237472Ssklower 			bcopy((caddr_t)ac->ac_enaddr,
30337472Ssklower 			      (caddr_t)eh->ether_dhost, 6);
30435795Skarels 		    sa.sa_family = AF_UNSPEC;
30537472Ssklower 		    sa.sa_len = sizeof(sa);
30635795Skarels 		    eh2 = (struct ether_header *)sa.sa_data;
30735795Skarels 		    for (i = 0; i < 6; i++) {
30835795Skarels 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
30935795Skarels 			eh2->ether_dhost[i] =
31035795Skarels 				eh->ether_dhost[i] = eh->ether_shost[i];
31135795Skarels 			eh->ether_shost[i] = c;
31235795Skarels 		    }
31335795Skarels 		    ifp->if_output(ifp, m, &sa);
31435795Skarels 		    return;
31535795Skarels 		}
31635795Skarels 		dropanyway:
31735795Skarels 		default:
31835795Skarels 		    m_freem(m);
31935795Skarels 		    return;
32035795Skarels 	    }
321*38845Sroot #else
322*38845Sroot 	    m_freem(m);
323*38845Sroot 	    return;
324*38845Sroot #endif	ISO
32535379Skfall 	}
32635379Skfall 
32735379Skfall 	s = splimp();
32835379Skfall 	if (IF_QFULL(inq)) {
32935379Skfall 		IF_DROP(inq);
33035379Skfall 		m_freem(m);
33135379Skfall 	} else
33235379Skfall 		IF_ENQUEUE(inq, m);
33335379Skfall 	splx(s);
33435379Skfall }
33535379Skfall 
33635379Skfall /*
33735379Skfall  * Convert Ethernet address to printable (loggable) representation.
33835379Skfall  */
33937472Ssklower static char digits[] = "0123456789abcdef";
34035379Skfall char *
34135379Skfall ether_sprintf(ap)
34235379Skfall 	register u_char *ap;
34335379Skfall {
34435379Skfall 	register i;
34535379Skfall 	static char etherbuf[18];
34635379Skfall 	register char *cp = etherbuf;
34735379Skfall 
34835379Skfall 	for (i = 0; i < 6; i++) {
34935379Skfall 		*cp++ = digits[*ap >> 4];
35035379Skfall 		*cp++ = digits[*ap++ & 0xf];
35135379Skfall 		*cp++ = ':';
35235379Skfall 	}
35335379Skfall 	*--cp = 0;
35435379Skfall 	return (etherbuf);
35535379Skfall }
356