xref: /csrg-svn/sys/net/if_ethersubr.c (revision 37517)
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*37517Smckusick  *	@(#)if_ethersubr.c	7.3 (Berkeley) 04/25/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 
35*37517Smckusick #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)
20435795Skarels 		error = (*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 /*
21935379Skfall  * Pull packet off interface.  Off is nonzero if packet
22035379Skfall  * has trailing header; we still have to drop
22135379Skfall  * the type and length which are at the front of any trailer data.
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:
25737472Ssklower 		if (eh->ether_type > ETHERMTU)
25835795Skarels 			goto dropanyway;
25935795Skarels 		l = mtod(m, struct llc *);
26035795Skarels 		switch (l->llc_control) {
26135795Skarels 		case LLC_UI:
26235795Skarels 		/* LLC_UI_P forbidden in class 1 service */
26335795Skarels 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
26435795Skarels 			(l->llc_ssap == LLC_ISO_LSAP)) {
26535795Skarels #ifdef	ISO
26635795Skarels 				/* LSAP for ISO */
26737472Ssklower 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
26837472Ssklower 			if (m == 0)
26937472Ssklower 				return;
27037472Ssklower 			*mtod(m, struct ether_header *) = *eh;
27137472Ssklower 			IFDEBUG(D_ETHER)
27237472Ssklower 			    printf("clnp packet");
27337472Ssklower 			ENDDEBUG
27437472Ssklower 			schednetisr(NETISR_ISO);
27535795Skarels 			inq = &clnlintrq;
27635795Skarels 			if (IF_QFULL(inq)){
27737472Ssklower 				IFDEBUG(D_ETHER)
27837472Ssklower 				    printf(" qfull\n");
27937472Ssklower 				ENDDEBUG
28035795Skarels 				IF_DROP(inq);
28135795Skarels 				m_freem(m);
28235795Skarels 			} else {
28335795Skarels 				IF_ENQUEUE(inq, m);
28437472Ssklower 				IFDEBUG(D_ETHER)
28537472Ssklower 				    printf(" queued\n");
28637472Ssklower 				ENDDEBUG
28735795Skarels 			}
28835795Skarels 			return;
28935795Skarels #endif	ISO
29035795Skarels 		    }
29135795Skarels 		    break;
29235795Skarels 		case LLC_XID:
29335795Skarels 		case LLC_XID_P:
29435795Skarels 		    if(m->m_len < 6)
29535795Skarels 			goto dropanyway;
29635795Skarels 		    l->llc_window = 0;
29735795Skarels 		    l->llc_fid = 9;
29835795Skarels 		    l->llc_class = 1;
29935795Skarels 		    l->llc_dsap = l->llc_ssap = 0;
30035795Skarels 		    /* Fall through to */
30135795Skarels 		case LLC_TEST:
30235795Skarels 		case LLC_TEST_P:
30335795Skarels 		{
30435795Skarels 		    struct sockaddr sa;
30535795Skarels 		    register struct ether_header *eh2;
30635795Skarels 		    int i;
30735795Skarels 		    u_char c = l->llc_dsap;
30835795Skarels 		    l->llc_dsap = l->llc_ssap;
30935795Skarels 		    l->llc_ssap = c;
31037472Ssklower 		    if (m->m_flags & (M_BCAST | M_MCAST))
31137472Ssklower 			bcopy((caddr_t)ac->ac_enaddr,
31237472Ssklower 			      (caddr_t)eh->ether_dhost, 6);
31335795Skarels 		    sa.sa_family = AF_UNSPEC;
31437472Ssklower 		    sa.sa_len = sizeof(sa);
31535795Skarels 		    eh2 = (struct ether_header *)sa.sa_data;
31635795Skarels 		    for (i = 0; i < 6; i++) {
31735795Skarels 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
31835795Skarels 			eh2->ether_dhost[i] =
31935795Skarels 				eh->ether_dhost[i] = eh->ether_shost[i];
32035795Skarels 			eh->ether_shost[i] = c;
32135795Skarels 		    }
32235795Skarels 		    ifp->if_output(ifp, m, &sa);
32335795Skarels 		    return;
32435795Skarels 		}
32535795Skarels 		dropanyway:
32635795Skarels 		default:
32735795Skarels 		    m_freem(m);
32835795Skarels 		    return;
32935795Skarels 	    }
33035379Skfall 	}
33135379Skfall 
33235379Skfall 	s = splimp();
33335379Skfall 	if (IF_QFULL(inq)) {
33435379Skfall 		IF_DROP(inq);
33535379Skfall 		m_freem(m);
33635379Skfall 	} else
33735379Skfall 		IF_ENQUEUE(inq, m);
33835379Skfall 	splx(s);
33935379Skfall }
34035379Skfall 
34135379Skfall /*
34235379Skfall  * Convert Ethernet address to printable (loggable) representation.
34335379Skfall  */
34437472Ssklower static char digits[] = "0123456789abcdef";
34535379Skfall char *
34635379Skfall ether_sprintf(ap)
34735379Skfall 	register u_char *ap;
34835379Skfall {
34935379Skfall 	register i;
35035379Skfall 	static char etherbuf[18];
35135379Skfall 	register char *cp = etherbuf;
35235379Skfall 
35335379Skfall 	for (i = 0; i < 6; i++) {
35435379Skfall 		*cp++ = digits[*ap >> 4];
35535379Skfall 		*cp++ = digits[*ap++ & 0xf];
35635379Skfall 		*cp++ = ':';
35735379Skfall 	}
35835379Skfall 	*--cp = 0;
35935379Skfall 	return (etherbuf);
36035379Skfall }
361