Commit | Line | Data |
---|---|---|
b994ed6a | 1 | /* if_en.c 6.5 84/12/20 */ |
11720282 BJ |
2 | |
3 | #include "en.h" | |
f1b2fa5b | 4 | |
11720282 | 5 | /* |
f1b2fa5b | 6 | * Xerox prototype (3 Mb) Ethernet interface driver. |
11720282 | 7 | */ |
961945a8 | 8 | #include "../machine/pte.h" |
11720282 | 9 | |
a6e960e7 JB |
10 | #include "param.h" |
11 | #include "systm.h" | |
12 | #include "mbuf.h" | |
13 | #include "buf.h" | |
14 | #include "protosw.h" | |
15 | #include "socket.h" | |
16 | #include "vmmac.h" | |
17 | #include "errno.h" | |
18 | #include "ioctl.h" | |
eaa60542 BJ |
19 | |
20 | #include "../net/if.h" | |
21 | #include "../net/netisr.h" | |
22 | #include "../net/route.h" | |
d2cc167c BJ |
23 | #include "../netinet/in.h" |
24 | #include "../netinet/in_systm.h" | |
d2cc167c BJ |
25 | #include "../netinet/ip.h" |
26 | #include "../netinet/ip_var.h" | |
27 | #include "../netpup/pup.h" | |
3cb181be | 28 | #include "../netpup/ether.h" |
eaa60542 BJ |
29 | |
30 | #include "../vax/cpu.h" | |
31 | #include "../vax/mtpr.h" | |
a6e960e7 JB |
32 | #include "if_en.h" |
33 | #include "if_enreg.h" | |
34 | #include "if_uba.h" | |
eaa60542 BJ |
35 | #include "../vaxuba/ubareg.h" |
36 | #include "../vaxuba/ubavar.h" | |
8a13b737 | 37 | |
a453e1b5 | 38 | #define ENMTU (1024+512) |
dc39362e | 39 | #define ENMRU (1024+512+16) /* 16 is enough to receive trailer */ |
11720282 | 40 | |
11720282 BJ |
41 | int enprobe(), enattach(), enrint(), enxint(), encollide(); |
42 | struct uba_device *eninfo[NEN]; | |
43 | u_short enstd[] = { 0 }; | |
44 | struct uba_driver endriver = | |
b454c3ea | 45 | { enprobe, 0, enattach, 0, enstd, "en", eninfo }; |
11720282 BJ |
46 | #define ENUNIT(x) minor(x) |
47 | ||
47695a10 | 48 | int eninit(),enoutput(),enreset(),enioctl(); |
f1b2fa5b | 49 | |
c1b082c5 SL |
50 | #ifdef notdef |
51 | /* | |
52 | * If you need to byte swap IP's in the system, define | |
53 | * this and do a SIOCSIFFLAGS at boot time. | |
54 | */ | |
55 | #define ENF_SWABIPS 0x100 | |
56 | #endif | |
57 | ||
f1b2fa5b BJ |
58 | /* |
59 | * Ethernet software status per interface. | |
60 | * | |
61 | * Each interface is referenced by a network interface structure, | |
62 | * es_if, which the routing code uses to locate the interface. | |
63 | * This structure contains the output queue for the interface, its address, ... | |
64 | * We also have, for each interface, a UBA interface structure, which | |
65 | * contains information about the UNIBUS resources held by the interface: | |
66 | * map registers, buffered data paths, etc. Information is cached in this | |
67 | * structure for use by the if_uba.c routines in running the interface | |
68 | * efficiently. | |
69 | */ | |
8a13b737 | 70 | struct en_softc { |
f1b2fa5b BJ |
71 | struct ifnet es_if; /* network-visible interface */ |
72 | struct ifuba es_ifuba; /* UNIBUS resources */ | |
7fb445f3 | 73 | short es_host; /* hardware host number */ |
f1b2fa5b BJ |
74 | short es_delay; /* current output delay */ |
75 | short es_mask; /* mask for current output delay */ | |
0658a645 | 76 | short es_lastx; /* host last transmitted to */ |
f1b2fa5b BJ |
77 | short es_oactive; /* is output active? */ |
78 | short es_olen; /* length of last output */ | |
8a13b737 | 79 | } en_softc[NEN]; |
11720282 | 80 | |
f1b2fa5b BJ |
81 | /* |
82 | * Do output DMA to determine interface presence and | |
83 | * interrupt vector. DMA is too short to disturb other hosts. | |
84 | */ | |
11720282 BJ |
85 | enprobe(reg) |
86 | caddr_t reg; | |
87 | { | |
b454c3ea | 88 | register int br, cvec; /* r11, r10 value-result */ |
11720282 BJ |
89 | register struct endevice *addr = (struct endevice *)reg; |
90 | ||
91 | #ifdef lint | |
92 | br = 0; cvec = br; br = cvec; | |
2b4b57cd | 93 | enrint(0); enxint(0); encollide(0); |
11720282 | 94 | #endif |
11720282 | 95 | addr->en_istat = 0; |
11720282 BJ |
96 | addr->en_owc = -1; |
97 | addr->en_oba = 0; | |
941ee7ef | 98 | addr->en_ostat = EN_IEN|EN_GO; |
11720282 BJ |
99 | DELAY(100000); |
100 | addr->en_ostat = 0; | |
11720282 BJ |
101 | return (1); |
102 | } | |
103 | ||
f1b2fa5b BJ |
104 | /* |
105 | * Interface exists: make available by filling in network interface | |
106 | * record. System will initialize the interface when it is ready | |
107 | * to accept packets. | |
108 | */ | |
11720282 BJ |
109 | enattach(ui) |
110 | struct uba_device *ui; | |
111 | { | |
f1b2fa5b | 112 | register struct en_softc *es = &en_softc[ui->ui_unit]; |
f1b2fa5b BJ |
113 | |
114 | es->es_if.if_unit = ui->ui_unit; | |
b454c3ea | 115 | es->es_if.if_name = "en"; |
f1b2fa5b | 116 | es->es_if.if_mtu = ENMTU; |
f1b2fa5b | 117 | es->es_if.if_init = eninit; |
b454c3ea | 118 | es->es_if.if_output = enoutput; |
47695a10 | 119 | es->es_if.if_ioctl = enioctl; |
51595ca2 | 120 | es->es_if.if_reset = enreset; |
d9c0644f | 121 | es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT; |
b4e3e570 SL |
122 | #if defined(VAX750) |
123 | /* don't chew up 750 bdp's */ | |
124 | if (cpu == VAX_750 && ui->ui_unit > 0) | |
125 | es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP; | |
126 | #endif | |
405c9168 | 127 | if_attach(&es->es_if); |
11720282 BJ |
128 | } |
129 | ||
f1b2fa5b BJ |
130 | /* |
131 | * Reset of interface after UNIBUS reset. | |
132 | * If interface is on specified uba, reset its state. | |
133 | */ | |
134 | enreset(unit, uban) | |
135 | int unit, uban; | |
8a13b737 | 136 | { |
11720282 | 137 | register struct uba_device *ui; |
11720282 | 138 | |
b454c3ea BJ |
139 | if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || |
140 | ui->ui_ubanum != uban) | |
f1b2fa5b | 141 | return; |
b454c3ea | 142 | printf(" en%d", unit); |
f1b2fa5b BJ |
143 | eninit(unit); |
144 | } | |
145 | ||
146 | /* | |
147 | * Initialization of interface; clear recorded pending | |
148 | * operations, and reinitialize UNIBUS usage. | |
149 | */ | |
150 | eninit(unit) | |
151 | int unit; | |
152 | { | |
b454c3ea BJ |
153 | register struct en_softc *es = &en_softc[unit]; |
154 | register struct uba_device *ui = eninfo[unit]; | |
f1b2fa5b | 155 | register struct endevice *addr; |
732fb59b | 156 | struct sockaddr_in *sin = (struct sockaddr_in *)&es->es_if.if_addr; |
47695a10 | 157 | int s; |
f1b2fa5b | 158 | |
47695a10 | 159 | if (in_netof(sin->sin_addr) == 0) |
732fb59b | 160 | return; |
f1b2fa5b | 161 | if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, |
dc39362e | 162 | sizeof (struct en_header), (int)btoc(ENMRU)) == 0) { |
b454c3ea | 163 | printf("en%d: can't initialize\n", unit); |
ee787340 | 164 | es->es_if.if_flags &= ~IFF_UP; |
8a13b737 | 165 | return; |
11720282 | 166 | } |
11720282 | 167 | addr = (struct endevice *)ui->ui_addr; |
8a13b737 | 168 | addr->en_istat = addr->en_ostat = 0; |
11720282 | 169 | |
f1b2fa5b | 170 | /* |
b454c3ea BJ |
171 | * Hang a receive and start any |
172 | * pending writes by faking a transmit complete. | |
f1b2fa5b BJ |
173 | */ |
174 | s = splimp(); | |
b454c3ea | 175 | addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; |
dc39362e | 176 | addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; |
b454c3ea BJ |
177 | addr->en_istat = EN_IEN|EN_GO; |
178 | es->es_oactive = 1; | |
47695a10 | 179 | es->es_if.if_flags |= IFF_UP|IFF_RUNNING; |
f1b2fa5b BJ |
180 | enxint(unit); |
181 | splx(s); | |
a13c006d | 182 | if_rtinit(&es->es_if, RTF_UP); |
11720282 BJ |
183 | } |
184 | ||
0658a645 | 185 | int enalldelay = 0; |
4c073c48 | 186 | int enlastdel = 50; |
0658a645 | 187 | int enlastmask = (~0) << 5; |
8a13b737 | 188 | |
f1b2fa5b BJ |
189 | /* |
190 | * Start or restart output on interface. | |
191 | * If interface is already active, then this is a retransmit | |
192 | * after a collision, and just restuff registers and delay. | |
193 | * If interface is not already active, get another datagram | |
194 | * to send off of the interface queue, and map it to the interface | |
195 | * before starting the output. | |
196 | */ | |
ae348eea | 197 | enstart(dev) |
11720282 BJ |
198 | dev_t dev; |
199 | { | |
b454c3ea BJ |
200 | int unit = ENUNIT(dev); |
201 | struct uba_device *ui = eninfo[unit]; | |
202 | register struct en_softc *es = &en_softc[unit]; | |
8a13b737 | 203 | register struct endevice *addr; |
7fb445f3 | 204 | register struct en_header *en; |
8a13b737 BJ |
205 | struct mbuf *m; |
206 | int dest; | |
11720282 | 207 | |
8a13b737 BJ |
208 | if (es->es_oactive) |
209 | goto restart; | |
f1b2fa5b BJ |
210 | |
211 | /* | |
212 | * Not already active: dequeue another request | |
213 | * and map it to the UNIBUS. If no more requests, | |
214 | * just return. | |
215 | */ | |
216 | IF_DEQUEUE(&es->es_if.if_snd, m); | |
8a13b737 BJ |
217 | if (m == 0) { |
218 | es->es_oactive = 0; | |
11720282 BJ |
219 | return; |
220 | } | |
7fb445f3 MK |
221 | en = mtod(m, struct en_header *); |
222 | dest = en->en_dhost; | |
223 | en->en_shost = es->es_host; | |
8a13b737 | 224 | es->es_olen = if_wubaput(&es->es_ifuba, m); |
c1b082c5 SL |
225 | #ifdef ENF_SWABIPS |
226 | /* | |
227 | * The Xerox interface does word at a time DMA, so | |
228 | * someone must do byte swapping of user data if high | |
229 | * and low ender machines are to communicate. It doesn't | |
230 | * belong here, but certain people depend on it, so... | |
231 | * | |
232 | * Should swab everybody, but this is a kludge anyway. | |
233 | */ | |
234 | if (es->es_if.if_flags & ENF_SWABIPS) { | |
c1b082c5 SL |
235 | en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr; |
236 | if (en->en_type == ENTYPE_IP) | |
237 | enswab((caddr_t)(en + 1), (caddr_t)(en + 1), | |
238 | es->es_olen - sizeof (struct en_header) + 1); | |
239 | } | |
240 | #endif | |
f1b2fa5b BJ |
241 | |
242 | /* | |
243 | * Ethernet cannot take back-to-back packets (no | |
0658a645 BJ |
244 | * buffering in interface. To help avoid overrunning |
245 | * receivers, enforce a small delay (about 1ms) in interface: | |
246 | * * between all packets when enalldelay | |
247 | * * whenever last packet was broadcast | |
248 | * * whenever this packet is to same host as last packet | |
f1b2fa5b | 249 | */ |
0658a645 | 250 | if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) { |
8a13b737 | 251 | es->es_delay = enlastdel; |
0658a645 BJ |
252 | es->es_mask = enlastmask; |
253 | } | |
254 | es->es_lastx = dest; | |
f1b2fa5b | 255 | |
8a13b737 | 256 | restart: |
f1b2fa5b BJ |
257 | /* |
258 | * Have request mapped to UNIBUS for transmission. | |
259 | * Purge any stale data from this BDP, and start the otput. | |
260 | */ | |
ee787340 SL |
261 | if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) |
262 | UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); | |
11720282 | 263 | addr = (struct endevice *)ui->ui_addr; |
405c9168 | 264 | addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; |
8a13b737 BJ |
265 | addr->en_odelay = es->es_delay; |
266 | addr->en_owc = -((es->es_olen + 1) >> 1); | |
941ee7ef | 267 | addr->en_ostat = EN_IEN|EN_GO; |
8a13b737 | 268 | es->es_oactive = 1; |
11720282 BJ |
269 | } |
270 | ||
f1b2fa5b BJ |
271 | /* |
272 | * Ethernet interface transmitter interrupt. | |
273 | * Start another output if more data to send. | |
274 | */ | |
11720282 BJ |
275 | enxint(unit) |
276 | int unit; | |
277 | { | |
b454c3ea BJ |
278 | register struct uba_device *ui = eninfo[unit]; |
279 | register struct en_softc *es = &en_softc[unit]; | |
519a1ecf | 280 | register struct endevice *addr = (struct endevice *)ui->ui_addr; |
11720282 | 281 | |
8a13b737 BJ |
282 | if (es->es_oactive == 0) |
283 | return; | |
519a1ecf | 284 | if (es->es_mask && (addr->en_ostat&EN_OERROR)) { |
89413846 | 285 | es->es_if.if_oerrors++; |
519a1ecf BJ |
286 | endocoll(unit); |
287 | return; | |
89413846 | 288 | } |
519a1ecf BJ |
289 | es->es_if.if_opackets++; |
290 | es->es_oactive = 0; | |
291 | es->es_delay = 0; | |
292 | es->es_mask = ~0; | |
7a66118b BJ |
293 | if (es->es_ifuba.ifu_xtofree) { |
294 | m_freem(es->es_ifuba.ifu_xtofree); | |
295 | es->es_ifuba.ifu_xtofree = 0; | |
296 | } | |
f1b2fa5b | 297 | if (es->es_if.if_snd.ifq_head == 0) { |
0658a645 | 298 | es->es_lastx = 256; /* putatively illegal */ |
11720282 BJ |
299 | return; |
300 | } | |
8a13b737 | 301 | enstart(unit); |
11720282 BJ |
302 | } |
303 | ||
f1b2fa5b BJ |
304 | /* |
305 | * Collision on ethernet interface. Do exponential | |
306 | * backoff, and retransmit. If have backed off all | |
ee787340 | 307 | * the way print warning diagnostic, and drop packet. |
f1b2fa5b | 308 | */ |
11720282 BJ |
309 | encollide(unit) |
310 | int unit; | |
311 | { | |
519a1ecf | 312 | struct en_softc *es = &en_softc[unit]; |
11720282 | 313 | |
f1b2fa5b | 314 | es->es_if.if_collisions++; |
8a13b737 | 315 | if (es->es_oactive == 0) |
11720282 | 316 | return; |
519a1ecf BJ |
317 | endocoll(unit); |
318 | } | |
319 | ||
320 | endocoll(unit) | |
321 | int unit; | |
322 | { | |
323 | register struct en_softc *es = &en_softc[unit]; | |
324 | ||
b454c3ea BJ |
325 | /* |
326 | * Es_mask is a 16 bit number with n low zero bits, with | |
327 | * n the number of backoffs. When es_mask is 0 we have | |
328 | * backed off 16 times, and give up. | |
329 | */ | |
8a13b737 | 330 | if (es->es_mask == 0) { |
a453e1b5 | 331 | printf("en%d: send error\n", unit); |
8a13b737 | 332 | enxint(unit); |
b454c3ea | 333 | return; |
11720282 | 334 | } |
b454c3ea BJ |
335 | /* |
336 | * Another backoff. Restart with delay based on n low bits | |
337 | * of the interval timer. | |
338 | */ | |
339 | es->es_mask <<= 1; | |
340 | es->es_delay = mfpr(ICR) &~ es->es_mask; | |
341 | enstart(unit); | |
11720282 BJ |
342 | } |
343 | ||
3cb181be SL |
344 | #ifdef notdef |
345 | struct sockproto enproto = { AF_ETHERLINK }; | |
346 | struct sockaddr_en endst = { AF_ETHERLINK }; | |
347 | struct sockaddr_en ensrc = { AF_ETHERLINK }; | |
348 | #endif | |
f1b2fa5b BJ |
349 | /* |
350 | * Ethernet interface receiver interrupt. | |
351 | * If input error just drop packet. | |
352 | * Otherwise purge input buffered data path and examine | |
353 | * packet to determine type. If can't determine length | |
354 | * from type, then have to drop packet. Othewise decapsulate | |
355 | * packet based on type and pass to type specific higher-level | |
356 | * input routine. | |
357 | */ | |
11720282 BJ |
358 | enrint(unit) |
359 | int unit; | |
360 | { | |
b454c3ea BJ |
361 | register struct en_softc *es = &en_softc[unit]; |
362 | struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; | |
363 | register struct en_header *en; | |
8a13b737 | 364 | struct mbuf *m; |
038ee561 | 365 | int len; short resid; |
b454c3ea | 366 | register struct ifqueue *inq; |
51c7d639 | 367 | int off, s; |
11720282 | 368 | |
b454c3ea | 369 | es->es_if.if_ipackets++; |
f1b2fa5b BJ |
370 | |
371 | /* | |
b454c3ea | 372 | * Purge BDP; drop if input error indicated. |
f1b2fa5b | 373 | */ |
ee787340 SL |
374 | if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) |
375 | UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); | |
941ee7ef | 376 | if (addr->en_istat&EN_IERROR) { |
f1b2fa5b | 377 | es->es_if.if_ierrors++; |
8a13b737 | 378 | goto setup; |
11720282 | 379 | } |
f1b2fa5b BJ |
380 | |
381 | /* | |
0658a645 | 382 | * Calculate input data length. |
f1b2fa5b BJ |
383 | * Get pointer to ethernet header (in input buffer). |
384 | * Deal with trailer protocol: if type is PUP trailer | |
385 | * get true type from first 16-bit word past data. | |
386 | * Remember that type was trailer by setting off. | |
387 | */ | |
0658a645 BJ |
388 | resid = addr->en_iwc; |
389 | if (resid) | |
390 | resid |= 0176000; | |
dc39362e | 391 | len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1; |
0658a645 | 392 | len -= sizeof (struct en_header); |
dc39362e | 393 | if (len > ENMRU) |
0658a645 | 394 | goto setup; /* sanity */ |
f1b2fa5b | 395 | en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); |
991f6240 | 396 | en->en_type = ntohs(en->en_type); |
8a13b737 | 397 | #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) |
991f6240 SL |
398 | if (en->en_type >= ENTYPE_TRAIL && |
399 | en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) { | |
400 | off = (en->en_type - ENTYPE_TRAIL) * 512; | |
dc39362e | 401 | if (off > ENMTU) |
b454c3ea | 402 | goto setup; /* sanity */ |
991f6240 SL |
403 | en->en_type = ntohs(*endataaddr(en, off, u_short *)); |
404 | resid = ntohs(*(endataaddr(en, off+2, u_short *))); | |
0658a645 BJ |
405 | if (off + resid > len) |
406 | goto setup; /* sanity */ | |
407 | len = off + resid; | |
8a13b737 BJ |
408 | } else |
409 | off = 0; | |
8a13b737 BJ |
410 | if (len == 0) |
411 | goto setup; | |
c1b082c5 SL |
412 | #ifdef ENF_SWABIPS |
413 | if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP) | |
414 | enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len); | |
415 | #endif | |
f1b2fa5b BJ |
416 | /* |
417 | * Pull packet off interface. Off is nonzero if packet | |
418 | * has trailing header; if_rubaget will then force this header | |
419 | * information to be at the front, but we still have to drop | |
0658a645 | 420 | * the type and length which are at the front of any trailer data. |
f1b2fa5b BJ |
421 | */ |
422 | m = if_rubaget(&es->es_ifuba, len, off); | |
a453e1b5 BJ |
423 | if (m == 0) |
424 | goto setup; | |
f1b2fa5b | 425 | if (off) { |
0658a645 BJ |
426 | m->m_off += 2 * sizeof (u_short); |
427 | m->m_len -= 2 * sizeof (u_short); | |
f1b2fa5b | 428 | } |
31c2345c SL |
429 | switch (en->en_type) { |
430 | ||
431 | #ifdef INET | |
991f6240 | 432 | case ENTYPE_IP: |
9c8692e9 | 433 | schednetisr(NETISR_IP); |
31c2345c SL |
434 | inq = &ipintrq; |
435 | break; | |
436 | #endif | |
ee787340 | 437 | #ifdef PUP |
3cb181be SL |
438 | case ENTYPE_PUP: |
439 | rpup_input(m); | |
31c2345c | 440 | goto setup; |
ee787340 | 441 | #endif |
c8f8184f | 442 | default: |
3cb181be SL |
443 | #ifdef notdef |
444 | enproto.sp_protocol = en->en_type; | |
445 | endst.sen_host = en->en_dhost; | |
446 | endst.sen_net = ensrc.sen_net = es->es_if.if_net; | |
447 | ensrc.sen_host = en->en_shost; | |
448 | raw_input(m, &enproto, | |
449 | (struct sockaddr *)&ensrc, (struct sockaddr *)&endst); | |
450 | #else | |
c8f8184f | 451 | m_freem(m); |
3cb181be | 452 | #endif |
c8f8184f | 453 | goto setup; |
ee787340 SL |
454 | } |
455 | ||
51c7d639 | 456 | s = splimp(); |
1e977657 BJ |
457 | if (IF_QFULL(inq)) { |
458 | IF_DROP(inq); | |
ee787340 | 459 | m_freem(m); |
1e977657 BJ |
460 | } else |
461 | IF_ENQUEUE(inq, m); | |
51c7d639 | 462 | splx(s); |
f1b2fa5b | 463 | |
ae348eea | 464 | setup: |
f1b2fa5b BJ |
465 | /* |
466 | * Reset for next packet. | |
467 | */ | |
468 | addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; | |
dc39362e | 469 | addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; |
941ee7ef | 470 | addr->en_istat = EN_IEN|EN_GO; |
ae348eea | 471 | } |
11720282 | 472 | |
8a13b737 BJ |
473 | /* |
474 | * Ethernet output routine. | |
475 | * Encapsulate a packet of type family for the local net. | |
f1b2fa5b BJ |
476 | * Use trailer local net encapsulation if enough data in first |
477 | * packet leaves a multiple of 512 bytes of data in remainder. | |
8a13b737 | 478 | */ |
ee787340 | 479 | enoutput(ifp, m0, dst) |
8a13b737 BJ |
480 | struct ifnet *ifp; |
481 | struct mbuf *m0; | |
ee787340 | 482 | struct sockaddr *dst; |
11720282 | 483 | { |
8a2f82db | 484 | int type, dest, s, error; |
f1b2fa5b | 485 | register struct mbuf *m = m0; |
8a13b737 | 486 | register struct en_header *en; |
ee787340 | 487 | register int off; |
8a13b737 | 488 | |
ee787340 | 489 | switch (dst->sa_family) { |
11720282 | 490 | |
8a13b737 | 491 | #ifdef INET |
ee787340 | 492 | case AF_INET: |
4e818526 | 493 | dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; |
4fce3bf9 | 494 | if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) { |
8a2f82db | 495 | error = EPERM; /* ??? */ |
3734056d | 496 | goto bad; |
8a2f82db | 497 | } |
4e818526 | 498 | dest = (dest >> 24) & 0xff; |
ee787340 | 499 | off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; |
47695a10 SL |
500 | /* need per host negotiation */ |
501 | if ((ifp->if_flags & IFF_NOTRAILERS) == 0) | |
ee787340 | 502 | if (off > 0 && (off & 0x1ff) == 0 && |
0658a645 | 503 | m->m_off >= MMINOFF + 2 * sizeof (u_short)) { |
991f6240 | 504 | type = ENTYPE_TRAIL + (off>>9); |
0658a645 BJ |
505 | m->m_off -= 2 * sizeof (u_short); |
506 | m->m_len += 2 * sizeof (u_short); | |
991f6240 SL |
507 | *mtod(m, u_short *) = htons((u_short)ENTYPE_IP); |
508 | *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len); | |
f1b2fa5b | 509 | goto gottrailertype; |
8a13b737 | 510 | } |
991f6240 | 511 | type = ENTYPE_IP; |
f1b2fa5b BJ |
512 | off = 0; |
513 | goto gottype; | |
8a13b737 | 514 | #endif |
31c2345c | 515 | #ifdef PUP |
ee787340 | 516 | case AF_PUP: |
1c1d4563 | 517 | dest = ((struct sockaddr_pup *)dst)->spup_host; |
991f6240 | 518 | type = ENTYPE_PUP; |
31c2345c SL |
519 | off = 0; |
520 | goto gottype; | |
31c2345c | 521 | #endif |
8a13b737 | 522 | |
3cb181be SL |
523 | #ifdef notdef |
524 | case AF_ETHERLINK: | |
525 | goto gotheader; | |
526 | #endif | |
527 | ||
8a13b737 | 528 | default: |
ee787340 SL |
529 | printf("en%d: can't handle af%d\n", ifp->if_unit, |
530 | dst->sa_family); | |
8a2f82db SL |
531 | error = EAFNOSUPPORT; |
532 | goto bad; | |
8a13b737 | 533 | } |
f1b2fa5b | 534 | |
b454c3ea | 535 | gottrailertype: |
f1b2fa5b BJ |
536 | /* |
537 | * Packet to be sent as trailer: move first packet | |
538 | * (control information) to end of chain. | |
539 | */ | |
f1b2fa5b BJ |
540 | while (m->m_next) |
541 | m = m->m_next; | |
542 | m->m_next = m0; | |
543 | m = m0->m_next; | |
544 | m0->m_next = 0; | |
b454c3ea | 545 | m0 = m; |
f1b2fa5b | 546 | |
b454c3ea | 547 | gottype: |
f1b2fa5b BJ |
548 | /* |
549 | * Add local net header. If no space in first mbuf, | |
550 | * allocate another. | |
551 | */ | |
a453e1b5 BJ |
552 | if (m->m_off > MMAXOFF || |
553 | MMINOFF + sizeof (struct en_header) > m->m_off) { | |
b994ed6a | 554 | MGET(m, M_DONTWAIT, MT_HEADER); |
8a13b737 | 555 | if (m == 0) { |
8a2f82db SL |
556 | error = ENOBUFS; |
557 | goto bad; | |
8a13b737 BJ |
558 | } |
559 | m->m_next = m0; | |
560 | m->m_off = MMINOFF; | |
561 | m->m_len = sizeof (struct en_header); | |
562 | } else { | |
8a13b737 BJ |
563 | m->m_off -= sizeof (struct en_header); |
564 | m->m_len += sizeof (struct en_header); | |
11720282 | 565 | } |
8a13b737 | 566 | en = mtod(m, struct en_header *); |
7fb445f3 | 567 | /* add en_shost later */ |
8a13b737 | 568 | en->en_dhost = dest; |
991f6240 | 569 | en->en_type = htons((u_short)type); |
f1b2fa5b | 570 | |
3cb181be | 571 | gotheader: |
f1b2fa5b BJ |
572 | /* |
573 | * Queue message on interface, and start output if interface | |
574 | * not yet active. | |
575 | */ | |
8a13b737 | 576 | s = splimp(); |
1e977657 BJ |
577 | if (IF_QFULL(&ifp->if_snd)) { |
578 | IF_DROP(&ifp->if_snd); | |
8a2f82db SL |
579 | error = ENOBUFS; |
580 | goto qfull; | |
1e977657 | 581 | } |
8a13b737 | 582 | IF_ENQUEUE(&ifp->if_snd, m); |
8a13b737 BJ |
583 | if (en_softc[ifp->if_unit].es_oactive == 0) |
584 | enstart(ifp->if_unit); | |
89413846 | 585 | splx(s); |
4e818526 | 586 | return (0); |
8a2f82db SL |
587 | qfull: |
588 | m0 = m; | |
589 | splx(s); | |
590 | bad: | |
591 | m_freem(m0); | |
592 | return (error); | |
11720282 | 593 | } |
47695a10 SL |
594 | |
595 | /* | |
596 | * Process an ioctl request. | |
597 | */ | |
598 | enioctl(ifp, cmd, data) | |
599 | register struct ifnet *ifp; | |
600 | int cmd; | |
601 | caddr_t data; | |
602 | { | |
603 | struct ifreq *ifr = (struct ifreq *)data; | |
604 | int s = splimp(), error = 0; | |
605 | ||
606 | switch (cmd) { | |
607 | ||
608 | case SIOCSIFADDR: | |
609 | if (ifp->if_flags & IFF_RUNNING) | |
610 | if_rtinit(ifp, -1); /* delete previous route */ | |
611 | ensetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); | |
612 | if (ifp->if_flags & IFF_RUNNING) | |
613 | if_rtinit(ifp, RTF_UP); | |
614 | else | |
615 | eninit(ifp->if_unit); | |
616 | break; | |
617 | ||
618 | default: | |
619 | error = EINVAL; | |
620 | } | |
621 | splx(s); | |
622 | return (error); | |
623 | } | |
624 | ||
625 | ensetaddr(ifp, sin) | |
626 | register struct ifnet *ifp; | |
627 | register struct sockaddr_in *sin; | |
628 | { | |
629 | struct endevice *enaddr; | |
630 | ||
7fb445f3 MK |
631 | /* set address once for in_netof, so subnets will be recognized */ |
632 | ifp->if_addr = *(struct sockaddr *)sin; | |
47695a10 SL |
633 | ifp->if_net = in_netof(sin->sin_addr); |
634 | enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr; | |
7fb445f3 | 635 | ((struct en_softc *) ifp)->es_host = (~enaddr->en_addr) & 0xff; |
47695a10 SL |
636 | sin = (struct sockaddr_in *)&ifp->if_addr; |
637 | sin->sin_family = AF_INET; | |
7fb445f3 MK |
638 | sin->sin_addr = if_makeaddr(ifp->if_net, |
639 | ((struct en_softc *)ifp)->es_host); | |
47695a10 SL |
640 | sin = (struct sockaddr_in *)&ifp->if_broadaddr; |
641 | sin->sin_family = AF_INET; | |
642 | sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); | |
643 | ifp->if_flags |= IFF_BROADCAST; | |
644 | } | |
c1b082c5 SL |
645 | |
646 | #ifdef ENF_SWABIPS | |
647 | /* | |
648 | * Swab bytes | |
649 | * Jeffrey Mogul, Stanford | |
650 | */ | |
651 | enswab(from, to, n) | |
652 | register caddr_t *from, *to; | |
653 | register int n; | |
654 | { | |
655 | register unsigned long temp; | |
656 | ||
657 | n >>= 1; n++; | |
658 | #define STEP temp = *from++,*to++ = *from++,*to++ = temp | |
659 | /* round to multiple of 8 */ | |
660 | while ((--n) & 07) | |
661 | STEP; | |
662 | n >>= 3; | |
663 | while (--n >= 0) { | |
664 | STEP; STEP; STEP; STEP; | |
665 | STEP; STEP; STEP; STEP; | |
666 | } | |
667 | } | |
668 | #endif |