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