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