xref: /csrg-svn/sys/net/if_ethersubr.c (revision 35795)
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*35795Skarels  *	@(#)if_ethersubr.c	7.1 (Berkeley) 10/12/88
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"
33*35795Skarels #include "if_llc.h"
3435379Skfall 
3535379Skfall #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 
4835379Skfall u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
49*35795Skarels extern	struct ifnet loif;
5035379Skfall 
5135379Skfall /*
5235379Skfall  * Ethernet output routine.
5335379Skfall  * Encapsulate a packet of type family for the local net.
5435379Skfall  * Use trailer local net encapsulation if enough data in first
5535379Skfall  * packet leaves a multiple of 512 bytes of data in remainder.
5635379Skfall  * Assumes that ifp is actually pointer to arpcom structure.
5735379Skfall  */
5835379Skfall enoutput(ifp, m0, dst)
5935379Skfall 	register struct ifnet *ifp;
6035379Skfall 	struct mbuf *m0;
6135379Skfall 	struct sockaddr *dst;
6235379Skfall {
63*35795Skarels 	short type;
64*35795Skarels 	int s, error = 0;
6535379Skfall  	u_char edst[6];
6635379Skfall 	struct in_addr idst;
6735379Skfall 	register struct mbuf *m = m0;
68*35795Skarels 	struct mbuf *mcopy = (struct mbuf *)0;
6935379Skfall 	register struct ether_header *eh;
70*35795Skarels 	int usetrailers, off;
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 	}
7735379Skfall 	switch (dst->sa_family) {
7835379Skfall 
7935379Skfall #ifdef INET
8035379Skfall 	case AF_INET:
8135379Skfall 		idst = ((struct sockaddr_in *)dst)->sin_addr;
8235379Skfall  		if (!arpresolve(ac, m, &idst, edst, &usetrailers))
8335379Skfall 			return (0);	/* if not yet resolved */
8435379Skfall 		off = m->m_pkthdr.len - m->m_len;
8535379Skfall 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
8635379Skfall 		    (m->m_flags & M_EXT) == 0 &&
8735379Skfall 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
8835379Skfall 			type = ETHERTYPE_TRAIL + (off>>9);
8935379Skfall 			m->m_data -= 2 * sizeof (u_short);
9035379Skfall 			m->m_len += 2 * sizeof (u_short);
9135379Skfall 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
9235379Skfall 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
9335379Skfall 			goto gottrailertype;
9435379Skfall 		}
9535379Skfall 		type = ETHERTYPE_IP;
9635379Skfall 		goto gottype;
9735379Skfall #endif
9835379Skfall #ifdef NS
9935379Skfall 	case AF_NS:
10035379Skfall 		type = ETHERTYPE_NS;
101*35795Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
102*35795Skarels 			return(looutput(&loif, m, dst));
10335379Skfall  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
104*35795Skarels 		    (caddr_t)edst, sizeof (edst));
10535379Skfall 		goto gottype;
10635379Skfall #endif
107*35795Skarels #ifdef	ISO
108*35795Skarels 	case AF_ISO: {
109*35795Skarels 		int	ret;
110*35795Skarels 		ret = clnp_arpresolve(&us->us_ac.ac_if, m, dst, edst);
111*35795Skarels 		struct llc *l;
112*35795Skarels 		if (ret <= 0) {
113*35795Skarels 			if (ret == -1) {
114*35795Skarels 			/* not resolved */
115*35795Skarels 				IFDEBUG(D_ETHER)
116*35795Skarels 					printf("unoutput: clnp packet dropped\n");
117*35795Skarels 				ENDDEBUG
118*35795Skarels 			}
119*35795Skarels 			return(0);
120*35795Skarels 		}
121*35795Skarels 		M_PREPEND(m, 3, M_DONTWAIT);
122*35795Skarels 		if (m == NULL) {
123*35795Skarels 			m_freem(mm);
124*35795Skarels 			return(0);
125*35795Skarels 		}
126*35795Skarels 		type = m->m_pkthdr.len;
127*35795Skarels 		l = mtod(m, struct llc *);
128*35795Skarels 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
129*35795Skarels 		l->llc_control = LLC_UI;
130*35795Skarels 		IFDEBUG(D_ETHER)
131*35795Skarels 			int i;
132*35795Skarels 			printf("unoutput: sending pkt to: ");
133*35795Skarels 			for (i=0; i<6; i++)
134*35795Skarels 				printf("%x ", edst[i] & 0xff);
135*35795Skarels 			printf("\n");
136*35795Skarels 		ENDDEBUG
137*35795Skarels 		} goto gottype;
138*35795Skarels #endif	ISO
13935379Skfall 
14035379Skfall 	case AF_UNSPEC:
14135379Skfall 		eh = (struct ether_header *)dst->sa_data;
14235379Skfall  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
14335379Skfall 		type = eh->ether_type;
14435379Skfall 		goto gottype;
14535379Skfall 
14635379Skfall 	default:
14735379Skfall 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
14835379Skfall 			dst->sa_family);
14935379Skfall 		error = EAFNOSUPPORT;
15035379Skfall 		goto bad;
15135379Skfall 	}
15235379Skfall 
15335379Skfall gottrailertype:
15435379Skfall 	/*
15535379Skfall 	 * Packet to be sent as trailer: move first packet
15635379Skfall 	 * (control information) to end of chain.
15735379Skfall 	 */
15835379Skfall 	while (m->m_next)
15935379Skfall 		m = m->m_next;
16035379Skfall 	m->m_next = m0;
16135379Skfall 	m = m0->m_next;
16235379Skfall 	m0->m_next = 0;
16335379Skfall 
16435379Skfall gottype:
16535379Skfall 	/*
16635379Skfall 	 * Add local net header.  If no space in first mbuf,
16735379Skfall 	 * allocate another.
16835379Skfall 	 */
16935379Skfall 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
17035379Skfall 	if (m == 0) {
17135379Skfall 		error = ENOBUFS;
17235379Skfall 		goto bad;
17335379Skfall 	}
17435379Skfall 	eh = mtod(m, struct ether_header *);
175*35795Skarels 	type = htons((u_short)type);
176*35795Skarels 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
177*35795Skarels 		sizeof(eh->ether_type));
17835379Skfall  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
17935379Skfall  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
18035379Skfall 	    sizeof(eh->ether_shost));
181*35795Skarels 	if (ifp->if_flags & IFF_SIMPLEX && dst->sa_family != AF_UNSPEC &&
182*35795Skarels 	    !bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr, sizeof (edst)))
183*35795Skarels 		mcopy = m_copy(m, 0, (int)M_COPYALL);
18435379Skfall 
18535379Skfall 	/*
18635379Skfall 	 * Queue message on interface, and start output if interface
18735379Skfall 	 * not yet active.
18835379Skfall 	 */
18935379Skfall 	s = splimp();
19035379Skfall 	if (IF_QFULL(&ifp->if_snd)) {
19135379Skfall 		IF_DROP(&ifp->if_snd);
19235379Skfall 		splx(s);
193*35795Skarels 		error = ENOBUFS;
194*35795Skarels 		goto bad;
19535379Skfall 	}
19635379Skfall 	IF_ENQUEUE(&ifp->if_snd, m);
19735379Skfall 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
198*35795Skarels 		error = (*ifp->if_start)(ifp);
19935379Skfall 	splx(s);
200*35795Skarels 	if (mcopy)
201*35795Skarels 		(void) looutput(&loif, mcopy, dst);
202*35795Skarels 	return (error);
20335379Skfall 
20435379Skfall bad:
205*35795Skarels 	if (mcopy)
206*35795Skarels 		m_freem(mcopy);
207*35795Skarels 	if (m)
208*35795Skarels 		m_freem(m);
20935379Skfall 	return (error);
21035379Skfall }
21135379Skfall 
21235379Skfall /*
21335379Skfall  * Pull packet off interface.  Off is nonzero if packet
21435379Skfall  * has trailing header; we still have to drop
21535379Skfall  * the type and length which are at the front of any trailer data.
21635379Skfall  */
21735379Skfall en_doproto(ifp, eh, m)
21835379Skfall 	struct ifnet *ifp;
21935379Skfall 	register struct ether_header *eh;
22035379Skfall 	struct mbuf *m;
22135379Skfall {
22235379Skfall 	register struct ifqueue *inq;
223*35795Skarels 	register struct llc *l;
22435379Skfall 	int s;
22535379Skfall 
226*35795Skarels 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
22735379Skfall 	    sizeof(etherbroadcastaddr)) == 0)
22835379Skfall 		m->m_flags |= M_BCAST;
229*35795Skarels 	else if (eh->ether_dhost[0] & 1)
230*35795Skarels 		m->m_flags |= M_MCAST;
23135379Skfall 
23235379Skfall 	switch (eh->ether_type) {
23335379Skfall #ifdef INET
23435379Skfall 	case ETHERTYPE_IP:
23535379Skfall 		schednetisr(NETISR_IP);
23635379Skfall 		inq = &ipintrq;
23735379Skfall 		break;
23835379Skfall 
23935379Skfall 	case ETHERTYPE_ARP:
24035379Skfall 		arpinput((struct arpcom *)ifp, m);
24135379Skfall 		return;
24235379Skfall #endif
24335379Skfall #ifdef NS
24435379Skfall 	case ETHERTYPE_NS:
24535379Skfall 		schednetisr(NETISR_NS);
24635379Skfall 		inq = &nsintrq;
24735379Skfall 		break;
24835379Skfall 
24935379Skfall #endif
25035379Skfall 	default:
251*35795Skarels 		if (eh->ether_type > 1500)
252*35795Skarels 			goto dropanyway;
253*35795Skarels 		l = mtod(m, struct llc *);
254*35795Skarels 		switch (l->llc_control) {
255*35795Skarels 		case LLC_UI:
256*35795Skarels 		/* LLC_UI_P forbidden in class 1 service */
257*35795Skarels 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
258*35795Skarels 			(l->llc_ssap == LLC_ISO_LSAP)) {
259*35795Skarels #ifdef	ISO
260*35795Skarels 				/* LSAP for ISO */
261*35795Skarels 			m->m_data += 3;
262*35795Skarels 			m->m_len -= 3;
263*35795Skarels 			if (m->m_flags & M_PKTHDR)
264*35795Skarels 				m->m_pkthdr.len -= 3;
265*35795Skarels 			DEBUGF(undebug & 0x2, printf("clnp packet\n");)
266*35795Skarels 			schednetisr(NETISR_CLNP);
267*35795Skarels 			inq = &clnlintrq;
268*35795Skarels 			if (IF_QFULL(inq)){
269*35795Skarels 				DEBUGF(undebug & 0x2, printf(" qfull\n");)
270*35795Skarels 				IF_DROP(inq);
271*35795Skarels 				m_freem(m);
272*35795Skarels 			} else {
273*35795Skarels 				IF_ENQUEUE(inq, m);
274*35795Skarels 				DEBUGF(undebug & 0x2, printf(" queued\n");)
275*35795Skarels 			}
276*35795Skarels 			return;
277*35795Skarels #endif	ISO
278*35795Skarels 		    }
279*35795Skarels 		    break;
280*35795Skarels 		case LLC_XID:
281*35795Skarels 		case LLC_XID_P:
282*35795Skarels 		    if(m->m_len < 6)
283*35795Skarels 			goto dropanyway;
284*35795Skarels 		    l->llc_window = 0;
285*35795Skarels 		    l->llc_fid = 9;
286*35795Skarels 		    l->llc_class = 1;
287*35795Skarels 		    l->llc_dsap = l->llc_ssap = 0;
288*35795Skarels 		    /* Fall through to */
289*35795Skarels 		case LLC_TEST:
290*35795Skarels 		case LLC_TEST_P:
291*35795Skarels 		{
292*35795Skarels 		    struct sockaddr sa;
293*35795Skarels 		    register struct ether_header *eh2;
294*35795Skarels 		    int i;
295*35795Skarels 		    u_char c = l->llc_dsap;
296*35795Skarels 		    l->llc_dsap = l->llc_ssap;
297*35795Skarels 		    l->llc_ssap = c;
298*35795Skarels 		    sa.sa_family = AF_UNSPEC;
299*35795Skarels 		    eh2 = (struct ether_header *)sa.sa_data;
300*35795Skarels 		    for (i = 0; i < 6; i++) {
301*35795Skarels 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
302*35795Skarels 			eh2->ether_dhost[i] =
303*35795Skarels 				eh->ether_dhost[i] = eh->ether_shost[i];
304*35795Skarels 			eh->ether_shost[i] = c;
305*35795Skarels 		    }
306*35795Skarels 		    ifp->if_output(ifp, m, &sa);
307*35795Skarels 		    return;
308*35795Skarels 		}
309*35795Skarels 		dropanyway:
310*35795Skarels 		default:
311*35795Skarels 		    m_freem(m);
312*35795Skarels 		    return;
313*35795Skarels 	    }
31435379Skfall 	}
31535379Skfall 
31635379Skfall 	s = splimp();
31735379Skfall 	if (IF_QFULL(inq)) {
31835379Skfall 		IF_DROP(inq);
31935379Skfall 		m_freem(m);
32035379Skfall 	} else
32135379Skfall 		IF_ENQUEUE(inq, m);
32235379Skfall 	splx(s);
32335379Skfall }
32435379Skfall 
32535379Skfall /*
32635379Skfall  * Convert Ethernet address to printable (loggable) representation.
32735379Skfall  */
32835379Skfall char *
32935379Skfall ether_sprintf(ap)
33035379Skfall 	register u_char *ap;
33135379Skfall {
33235379Skfall 	register i;
33335379Skfall 	static char etherbuf[18];
33435379Skfall 	register char *cp = etherbuf;
33535379Skfall 	static char digits[] = "0123456789abcdef";
33635379Skfall 
33735379Skfall 	for (i = 0; i < 6; i++) {
33835379Skfall 		*cp++ = digits[*ap >> 4];
33935379Skfall 		*cp++ = digits[*ap++ & 0xf];
34035379Skfall 		*cp++ = ':';
34135379Skfall 	}
34235379Skfall 	*--cp = 0;
34335379Skfall 	return (etherbuf);
34435379Skfall }
345