xref: /csrg-svn/sys/net/if_ethersubr.c (revision 41546)
1 /*
2  * Copyright (c) 1982, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)if_ethersubr.c	7.7 (Berkeley) 05/10/90
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "malloc.h"
23 #include "mbuf.h"
24 #include "protosw.h"
25 #include "socket.h"
26 #include "ioctl.h"
27 #include "errno.h"
28 #include "syslog.h"
29 
30 #include "if.h"
31 #include "netisr.h"
32 #include "route.h"
33 #include "if_llc.h"
34 #include "if_dl.h"
35 
36 #include "machine/mtpr.h"
37 
38 #ifdef INET
39 #include "../netinet/in.h"
40 #include "../netinet/in_var.h"
41 #include "../netinet/if_ether.h"
42 #endif
43 
44 #ifdef NS
45 #include "../netns/ns.h"
46 #include "../netns/ns_if.h"
47 #endif
48 
49 #ifdef ISO
50 #include "../netiso/argo_debug.h"
51 #include "../netiso/iso.h"
52 #include "../netiso/iso_var.h"
53 #endif
54 
55 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
56 extern	struct ifnet loif;
57 
58 /*
59  * Ethernet output routine.
60  * Encapsulate a packet of type family for the local net.
61  * Use trailer local net encapsulation if enough data in first
62  * packet leaves a multiple of 512 bytes of data in remainder.
63  * Assumes that ifp is actually pointer to arpcom structure.
64  */
65 ether_output(ifp, m0, dst, rt)
66 	register struct ifnet *ifp;
67 	struct mbuf *m0;
68 	struct sockaddr *dst;
69 	struct rtentry *rt;
70 {
71 	short type;
72 	int s, error = 0;
73  	u_char edst[6];
74 	struct in_addr idst;
75 	register struct mbuf *m = m0;
76 	struct mbuf *mcopy = (struct mbuf *)0;
77 	register struct ether_header *eh;
78 	int usetrailers, off, len = m->m_pkthdr.len;
79 	extern struct timeval time;
80 #define	ac ((struct arpcom *)ifp)
81 
82 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
83 		error = ENETDOWN;
84 		goto bad;
85 	}
86 	ifp->if_lastchange = time;
87 	switch (dst->sa_family) {
88 
89 #ifdef INET
90 	case AF_INET:
91 		idst = ((struct sockaddr_in *)dst)->sin_addr;
92  		if (!arpresolve(ac, m, &idst, edst, &usetrailers))
93 			return (0);	/* if not yet resolved */
94 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
95 			mcopy = m_copy(m, 0, (int)M_COPYALL);
96 		off = m->m_pkthdr.len - m->m_len;
97 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
98 		    (m->m_flags & M_EXT) == 0 &&
99 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
100 			type = ETHERTYPE_TRAIL + (off>>9);
101 			m->m_data -= 2 * sizeof (u_short);
102 			m->m_len += 2 * sizeof (u_short);
103 			len += 2 * sizeof (u_short);
104 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
105 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
106 			goto gottrailertype;
107 		}
108 		type = ETHERTYPE_IP;
109 		goto gottype;
110 #endif
111 #ifdef NS
112 	case AF_NS:
113 		type = ETHERTYPE_NS;
114  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
115 		    (caddr_t)edst, sizeof (edst));
116 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
117 			return(looutput(&loif, m, dst));
118 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
119 			mcopy = m_copy(m, 0, (int)M_COPYALL);
120 		goto gottype;
121 #endif
122 #ifdef	ISO
123 	case AF_ISO: {
124 		int	len;
125 		int	ret;
126 		struct	llc *l;
127 
128 		if (rt && rt->rt_gateway &&
129 		    rt->rt_gateway->sa_family == AF_LINK) {
130 			register struct sockaddr_dl *sdl;
131 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
132 			if (len = sdl->sdl_nlen) {
133 				bcopy(LLADDR(sdl), (char *)edst, len);
134 				goto iso_resolved;
135 			}
136 		}
137 		if ((ret = iso_tryloopback(m, (struct sockaddr_iso *)dst)) >= 0)
138 			return (ret);
139 		ret = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
140 					(char *)edst, &len);
141 		if (ret > 0) {
142 			m_freem(m); /* Not Resolved */
143 			return(ret);
144 		}
145 	iso_resolved:
146 		M_PREPEND(m, 3, M_DONTWAIT);
147 		if (m == NULL)
148 			return(0);
149 		type = m->m_pkthdr.len;
150 		l = mtod(m, struct llc *);
151 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
152 		l->llc_control = LLC_UI;
153 		len += 3;
154 		IFDEBUG(D_ETHER)
155 			int i;
156 			printf("unoutput: sending pkt to: ");
157 			for (i=0; i<6; i++)
158 				printf("%x ", edst[i] & 0xff);
159 			printf("\n");
160 		ENDDEBUG
161 		} goto gottype;
162 #endif	ISO
163 #ifdef RMP
164 	case AF_RMP:
165 		/*
166 		 *  This is IEEE 802.3 -- the Ethernet `type' field is
167 		 *  really a `length' field.
168 		 */
169 		type = m->m_len;
170  		bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
171 		break;
172 #endif
173 
174 	case AF_UNSPEC:
175 		eh = (struct ether_header *)dst->sa_data;
176  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
177 		type = eh->ether_type;
178 		goto gottype;
179 
180 	default:
181 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
182 			dst->sa_family);
183 		error = EAFNOSUPPORT;
184 		goto bad;
185 	}
186 
187 gottrailertype:
188 	/*
189 	 * Packet to be sent as trailer: move first packet
190 	 * (control information) to end of chain.
191 	 */
192 	while (m->m_next)
193 		m = m->m_next;
194 	m->m_next = m0;
195 	m = m0->m_next;
196 	m0->m_next = 0;
197 
198 gottype:
199 	/*
200 	 * Add local net header.  If no space in first mbuf,
201 	 * allocate another.
202 	 */
203 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
204 	if (m == 0) {
205 		error = ENOBUFS;
206 		goto bad;
207 	}
208 	eh = mtod(m, struct ether_header *);
209 	type = htons((u_short)type);
210 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
211 		sizeof(eh->ether_type));
212  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
213  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
214 	    sizeof(eh->ether_shost));
215 	/*
216 	 * Queue message on interface, and start output if interface
217 	 * not yet active.
218 	 */
219 	s = splimp();
220 	if (IF_QFULL(&ifp->if_snd)) {
221 		IF_DROP(&ifp->if_snd);
222 		splx(s);
223 		error = ENOBUFS;
224 		goto bad;
225 	}
226 	IF_ENQUEUE(&ifp->if_snd, m);
227 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
228 		(*ifp->if_start)(ifp);
229 	splx(s);
230 	if (mcopy)
231 		(void) looutput(&loif, mcopy, dst);
232 	ifp->if_obytes += len + sizeof (struct ether_header);
233 	if (edst[0] & 1)
234 		ifp->if_omcasts++;
235 	return (error);
236 
237 bad:
238 	if (mcopy)
239 		m_freem(mcopy);
240 	if (m)
241 		m_freem(m);
242 	return (error);
243 }
244 
245 /*
246  * Process a received Ethernet packet;
247  * the packet is in the mbuf chain m without
248  * the ether header, which is provided separately.
249  */
250 ether_input(ifp, eh, m)
251 	struct ifnet *ifp;
252 	register struct ether_header *eh;
253 	struct mbuf *m;
254 {
255 	register struct ifqueue *inq;
256 	register struct llc *l;
257 	int s;
258 
259 	ifp->if_lastchange = time;
260 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
261 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
262 	    sizeof(etherbroadcastaddr)) == 0)
263 		m->m_flags |= M_BCAST;
264 	else if (eh->ether_dhost[0] & 1)
265 		m->m_flags |= M_MCAST;
266 	if (m->m_flags & (M_BCAST|M_MCAST))
267 		ifp->if_imcasts++;
268 
269 	switch (eh->ether_type) {
270 #ifdef INET
271 	case ETHERTYPE_IP:
272 		schednetisr(NETISR_IP);
273 		inq = &ipintrq;
274 		break;
275 
276 	case ETHERTYPE_ARP:
277 		arpinput((struct arpcom *)ifp, m);
278 		return;
279 #endif
280 #ifdef NS
281 	case ETHERTYPE_NS:
282 		schednetisr(NETISR_NS);
283 		inq = &nsintrq;
284 		break;
285 
286 #endif
287 	default:
288 #ifdef	ISO
289 		if (eh->ether_type > ETHERMTU)
290 			goto dropanyway;
291 		l = mtod(m, struct llc *);
292 		switch (l->llc_control) {
293 		case LLC_UI:
294 		/* LLC_UI_P forbidden in class 1 service */
295 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
296 			(l->llc_ssap == LLC_ISO_LSAP)) {
297 				/* LSAP for ISO */
298 			m->m_data += 3;		/* XXX */
299 			m->m_len -= 3;		/* XXX */
300 			m->m_pkthdr.len -= 3;	/* XXX */
301 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
302 			if (m == 0)
303 				return;
304 			*mtod(m, struct ether_header *) = *eh;
305 			IFDEBUG(D_ETHER)
306 			    printf("clnp packet");
307 			ENDDEBUG
308 			schednetisr(NETISR_ISO);
309 			inq = &clnlintrq;
310 			break;
311 		    }
312 		    goto dropanyway;
313 
314 		case LLC_XID:
315 		case LLC_XID_P:
316 		    if(m->m_len < 6)
317 			goto dropanyway;
318 		    l->llc_window = 0;
319 		    l->llc_fid = 9;
320 		    l->llc_class = 1;
321 		    l->llc_dsap = l->llc_ssap = 0;
322 		    /* Fall through to */
323 		case LLC_TEST:
324 		case LLC_TEST_P:
325 		{
326 		    struct sockaddr sa;
327 		    register struct ether_header *eh2;
328 		    int i;
329 		    u_char c = l->llc_dsap;
330 		    l->llc_dsap = l->llc_ssap;
331 		    l->llc_ssap = c;
332 		    if (m->m_flags & (M_BCAST | M_MCAST))
333 			bcopy((caddr_t)ac->ac_enaddr,
334 			      (caddr_t)eh->ether_dhost, 6);
335 		    sa.sa_family = AF_UNSPEC;
336 		    sa.sa_len = sizeof(sa);
337 		    eh2 = (struct ether_header *)sa.sa_data;
338 		    for (i = 0; i < 6; i++) {
339 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
340 			eh2->ether_dhost[i] =
341 				eh->ether_dhost[i] = eh->ether_shost[i];
342 			eh->ether_shost[i] = c;
343 		    }
344 		    ifp->if_output(ifp, m, &sa);
345 		    return;
346 		}
347 		dropanyway:
348 		default:
349 		    m_freem(m);
350 		    return;
351 	    }
352 #else
353 	    m_freem(m);
354 	    return;
355 #endif	ISO
356 	}
357 
358 	s = splimp();
359 	if (IF_QFULL(inq)) {
360 		IF_DROP(inq);
361 		m_freem(m);
362 	} else
363 		IF_ENQUEUE(inq, m);
364 	splx(s);
365 }
366 
367 /*
368  * Convert Ethernet address to printable (loggable) representation.
369  */
370 static char digits[] = "0123456789abcdef";
371 char *
372 ether_sprintf(ap)
373 	register u_char *ap;
374 {
375 	register i;
376 	static char etherbuf[18];
377 	register char *cp = etherbuf;
378 
379 	for (i = 0; i < 6; i++) {
380 		*cp++ = digits[*ap >> 4];
381 		*cp++ = digits[*ap++ & 0xf];
382 		*cp++ = ':';
383 	}
384 	*--cp = 0;
385 	return (etherbuf);
386 }
387