many hacks to get mail.cs working well
[unix-history] / usr / src / sys / net / if_ethersubr.c
CommitLineData
132c65ba 1/*
5f08a978 2 * Copyright (c) 1982, 1989 Regents of the University of California.
132c65ba
KF
3 * All rights reserved.
4 *
dbf0c423 5 * %sccs.include.redist.c%
132c65ba 6 *
e6ff5635 7 * @(#)if_ethersubr.c 7.14 (Berkeley) %G%
132c65ba
KF
8 */
9
10#include "param.h"
11#include "systm.h"
c1ee97b0 12#include "kernel.h"
132c65ba
KF
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"
38a81509 24#include "if_llc.h"
11105e87 25#include "if_dl.h"
132c65ba 26
d301d150 27#include "machine/mtpr.h"
132c65ba
KF
28
29#ifdef INET
30#include "../netinet/in.h"
31#include "../netinet/in_var.h"
132c65ba 32#endif
0a219e9a 33#include "../netinet/if_ether.h"
132c65ba
KF
34
35#ifdef NS
36#include "../netns/ns.h"
37#include "../netns/ns_if.h"
38#endif
39
b72a6efb
KS
40#ifdef ISO
41#include "../netiso/argo_debug.h"
42#include "../netiso/iso.h"
43#include "../netiso/iso_var.h"
983eada9 44#include "../netiso/iso_snpac.h"
b72a6efb
KS
45#endif
46
132c65ba 47u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
38a81509 48extern struct ifnet loif;
e6ff5635 49#define senderr(e) { error = (e); goto bad;}
132c65ba
KF
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 */
e6ff5635 58ether_output(ifp, m0, dst, rt0)
132c65ba
KF
59 register struct ifnet *ifp;
60 struct mbuf *m0;
61 struct sockaddr *dst;
e6ff5635 62 struct rtentry *rt0;
132c65ba 63{
38a81509
MK
64 short type;
65 int s, error = 0;
132c65ba 66 u_char edst[6];
132c65ba 67 register struct mbuf *m = m0;
e6ff5635 68 register struct rtentry *rt;
38a81509 69 struct mbuf *mcopy = (struct mbuf *)0;
132c65ba 70 register struct ether_header *eh;
5f08a978 71 int usetrailers, off, len = m->m_pkthdr.len;
132c65ba
KF
72#define ac ((struct arpcom *)ifp)
73
e6ff5635
KS
74 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
75 senderr(ENETDOWN);
5f08a978 76 ifp->if_lastchange = time;
e6ff5635
KS
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 }
132c65ba
KF
99 switch (dst->sa_family) {
100
101#ifdef INET
102 case AF_INET:
e6ff5635
KS
103 if (!arpresolve(ac, rt, m, (struct sockaddr_in *)dst,
104 edst, &usetrailers))
132c65ba 105 return (0); /* if not yet resolved */
b72a6efb 106 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
a927e5cb 107 mcopy = m_copy(m, 0, (int)M_COPYALL);
132c65ba
KF
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);
5f08a978 115 len += 2 * sizeof (u_short);
132c65ba
KF
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;
132c65ba
KF
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),
38a81509 127 (caddr_t)edst, sizeof (edst));
5f08a978 128 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
fe7efead 129 return (looutput(ifp, m, dst, rt));
b72a6efb 130 if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
a927e5cb 131 mcopy = m_copy(m, 0, (int)M_COPYALL);
132c65ba
KF
132 goto gottype;
133#endif
38a81509
MK
134#ifdef ISO
135 case AF_ISO: {
983eada9 136 int snpalen;
b72a6efb 137 struct llc *l;
e6ff5635 138 register struct sockaddr_dl *sdl;
b72a6efb 139
e6ff5635
KS
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))
983eada9 146 goto bad; /* Not Resolved */
fe7efead
KS
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 }
38a81509 158 M_PREPEND(m, 3, M_DONTWAIT);
b72a6efb 159 if (m == NULL)
983eada9 160 return (0);
38a81509
MK
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;
5f08a978 165 len += 3;
38a81509
MK
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
a927e5cb
KM
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
132c65ba
KF
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);
e6ff5635 195 senderr(EAFNOSUPPORT);
132c65ba
KF
196 }
197
198gottrailertype:
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;
132c65ba
KF
208
209gottype:
fe7efead
KS
210 if (mcopy)
211 (void) looutput(ifp, mcopy, dst, rt);
132c65ba
KF
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);
e6ff5635
KS
217 if (m == 0)
218 senderr(ENOBUFS);
132c65ba 219 eh = mtod(m, struct ether_header *);
38a81509
MK
220 type = htons((u_short)type);
221 bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
222 sizeof(eh->ether_type));
132c65ba
KF
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));
fe7efead 226 s = splimp();
132c65ba
KF
227 /*
228 * Queue message on interface, and start output if interface
229 * not yet active.
230 */
132c65ba
KF
231 if (IF_QFULL(&ifp->if_snd)) {
232 IF_DROP(&ifp->if_snd);
233 splx(s);
e6ff5635 234 senderr(ENOBUFS);
132c65ba
KF
235 }
236 IF_ENQUEUE(&ifp->if_snd, m);
237 if ((ifp->if_flags & IFF_OACTIVE) == 0)
ae4c8bc9 238 (*ifp->if_start)(ifp);
132c65ba 239 splx(s);
5f08a978
KS
240 ifp->if_obytes += len + sizeof (struct ether_header);
241 if (edst[0] & 1)
242 ifp->if_omcasts++;
38a81509 243 return (error);
132c65ba
KF
244
245bad:
38a81509
MK
246 if (m)
247 m_freem(m);
132c65ba
KF
248 return (error);
249}
250
251/*
ae4c8bc9
BJ
252 * Process a received Ethernet packet;
253 * the packet is in the mbuf chain m without
254 * the ether header, which is provided separately.
132c65ba 255 */
b72a6efb 256ether_input(ifp, eh, m)
132c65ba
KF
257 struct ifnet *ifp;
258 register struct ether_header *eh;
259 struct mbuf *m;
260{
261 register struct ifqueue *inq;
38a81509 262 register struct llc *l;
132c65ba
KF
263 int s;
264
5f08a978
KS
265 ifp->if_lastchange = time;
266 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
38a81509 267 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
132c65ba
KF
268 sizeof(etherbroadcastaddr)) == 0)
269 m->m_flags |= M_BCAST;
38a81509
MK
270 else if (eh->ether_dhost[0] & 1)
271 m->m_flags |= M_MCAST;
5f08a978
KS
272 if (m->m_flags & (M_BCAST|M_MCAST))
273 ifp->if_imcasts++;
132c65ba
KF
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:
e6ff5635
KS
283 schednetisr(NETISR_ARP);
284 inq = &arpintrq;
285 break;
132c65ba
KF
286#endif
287#ifdef NS
288 case ETHERTYPE_NS:
289 schednetisr(NETISR_NS);
290 inq = &nsintrq;
291 break;
292
293#endif
294 default:
ae4c8bc9 295#ifdef ISO
b72a6efb 296 if (eh->ether_type > ETHERMTU)
38a81509
MK
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)) {
38a81509 304 /* LSAP for ISO */
fe7efead
KS
305 if (m->m_pkthdr.len > eh->ether_type)
306 m_adj(m, eh->ether_type - m->m_pkthdr.len);
ae4c8bc9
BJ
307 m->m_data += 3; /* XXX */
308 m->m_len -= 3; /* XXX */
309 m->m_pkthdr.len -= 3; /* XXX */
b72a6efb
KS
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);
38a81509 318 inq = &clnlintrq;
ae4c8bc9 319 break;
38a81509 320 }
ae4c8bc9
BJ
321 goto dropanyway;
322
38a81509
MK
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;
b72a6efb
KS
341 if (m->m_flags & (M_BCAST | M_MCAST))
342 bcopy((caddr_t)ac->ac_enaddr,
343 (caddr_t)eh->ether_dhost, 6);
38a81509 344 sa.sa_family = AF_UNSPEC;
b72a6efb 345 sa.sa_len = sizeof(sa);
38a81509
MK
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 }
ae4c8bc9
BJ
361#else
362 m_freem(m);
363 return;
364#endif ISO
132c65ba
KF
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 */
b72a6efb 379static char digits[] = "0123456789abcdef";
132c65ba
KF
380char *
381ether_sprintf(ap)
382 register u_char *ap;
383{
384 register i;
385 static char etherbuf[18];
386 register char *cp = etherbuf;
132c65ba
KF
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}