1 /*	if_imp.c	4.8	82/02/21	*/
2 
3 #include "imp.h"
4 #if NIMP > 0
5 /*
6  * ARPAnet IMP interface driver.
7  *
8  * The IMP-host protocol is handled here, leaving
9  * hardware specifics to the lower level interface driver.
10  *
11  * TODO:
12  *	rethink coupling between this module and device driver
13  *	pass more error indications up to protocol modules
14  */
15 #include "../h/param.h"
16 #include "../h/systm.h"
17 #include "../h/mbuf.h"
18 #include "../h/pte.h"
19 #include "../h/buf.h"
20 #include "../h/protosw.h"
21 #include "../h/socket.h"
22 #include "../h/ubareg.h"
23 #include "../h/ubavar.h"
24 #include "../h/cpu.h"
25 #include "../h/mtpr.h"
26 #include "../h/vmmac.h"
27 #include "../net/in.h"
28 #include "../net/in_systm.h"
29 #include "../net/if.h"
30 #include "../net/if_imp.h"
31 #include "../net/if_imphost.h"
32 #include "../net/ip.h"
33 #include "../net/ip_var.h"
34 
35 /*
36  * IMP software status per interface.
37  * (partially shared with the hardware specific module)
38  *
39  * Each interface is referenced by a network interface structure,
40  * imp_if, which the routing code uses to locate the interface.
41  * This structure contains the output queue for the interface, its
42  * address, ...  IMP specific structures used in connecting the
43  * IMP software modules to the hardware specific interface routines
44  * are stored here.  The common structures are made visible to the
45  * interface driver by passing a pointer to the hardware routine
46  * at "attach" time.
47  *
48  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
49  */
50 struct imp_softc {
51 	struct	ifnet imp_if;		/* network visible interface */
52 	struct	impcb imp_cb;		/* hooks to hardware module */
53 	u_char	imp_state;		/* current state of IMP */
54 	char	imp_dropcnt;		/* used during initialization */
55 } imp_softc[NIMP];
56 
57 /*
58  * Messages from IMP regarding why
59  * it's going down.
60  */
61 static char *impmessage[] = {
62 	"in 30 seconds",
63 	"for hardware PM",
64 	"to reload software",
65 	"for emergency reset"
66 };
67 
68 int	impdown(), impinit(), impoutput();
69 
70 /*
71  * IMP attach routine.  Called from hardware device attach routine
72  * at configuration time with a pointer to the UNIBUS device structure.
73  * Sets up local state and returns pointer to base of ifnet+impcb
74  * structures.  This is then used by the device's attach routine
75  * set up its back pointers.
76  */
77 impattach(ui)
78 	struct uba_device *ui;
79 {
80 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
81 	register struct ifnet *ifp = &sc->imp_if;
82 
83 COUNT(IMPATTACH);
84 	/* UNIT COULD BE AMBIGUOUS */
85 	ifp->if_unit = ui->ui_unit;
86 	ifp->if_name = "imp";
87 	ifp->if_mtu = IMP_MTU;
88 	ifp->if_net = ui->ui_flags;
89 	/* this should be found by talking to the imp */
90 	ifp->if_addr.s_addr = 0x4e00000a;
91 	ifp->if_init = impinit;
92 	ifp->if_output = impoutput;
93 	/* reset is handled at the hardware level */
94 	if_attach(ifp);
95 	/* kludge to hand pointers back to hardware attach routine */
96 	return ((int)&sc->imp_if);
97 }
98 
99 /*
100  * IMP initialization routine: call hardware module to
101  * setup UNIBUS resources, init state and get ready for
102  * NOOPs the IMP should send us, and that we want to drop.
103  */
104 impinit(unit)
105 	int unit;
106 {
107 	register struct imp_softc *sc = &imp_softc[unit];
108 
109 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
110 		sc->imp_state = IMPS_DOWN;
111 		return;
112 	}
113 	sc->imp_state = IMPS_INIT;
114 	sc->imp_dropcnt = IMP_DROPCNT;
115 	impnoops(sc);
116 }
117 
118 struct sockproto impproto = { PF_IMPLINK };
119 struct sockaddr_in impdst = { AF_IMPLINK };
120 struct sockaddr_in impsrc = { AF_IMPLINK };
121 
122 /*
123  * ARPAnet 1822 input routine.
124  * Called from hardware input interrupt routine to handle 1822
125  * IMP-host messages.  Type 0 messages (non-control) are
126  * passed to higher level protocol processors on the basis
127  * of link number.  Other type messages (control) are handled here.
128  */
129 impinput(unit, m)
130 	int unit;
131 	register struct mbuf *m;
132 {
133 	int s;
134 	register struct imp_leader *ip;
135 	register struct imp_softc *sc = &imp_softc[unit];
136 	register struct host *hp;
137 	register struct ifqueue *inq;
138 	struct control_leader *cp;
139 	struct in_addr addr;
140 	struct mbuf *next;
141 
142 COUNT(IMP_INPUT);
143 	/*
144 	 * Verify leader length.  Be careful with control
145 	 * message which don't get a length included.
146 	 * We should generate a "bad leader" message
147 	 * to the IMP about messages too short.
148 	 */
149 	if (m->m_len < sizeof(struct control_leader) &&
150 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
151 		return;
152 	cp = mtod(m, struct control_leader *);
153 	if (cp->dl_mtype == IMPTYPE_DATA)
154 		if (m->m_len < sizeof(struct imp_leader) &&
155 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
156 			return;
157 	ip = mtod(m, struct imp_leader *);
158 
159 	/*
160 	 * Check leader type -- should notify IMP
161 	 * in case of failure...
162 	 */
163 	if (ip->il_format != IMP_NFF) {
164 		sc->imp_if.if_collisions++;	/* XXX */
165 		goto drop;
166 	}
167 
168 	/*
169 	 * Certain messages require a host structure.
170 	 * Do this in one shot here.
171 	 */
172 	switch (ip->il_mtype) {
173 
174 	case IMPTYPE_RFNM:
175 	case IMPTYPE_INCOMPLETE:
176 	case IMPTYPE_HOSTDEAD:
177 	case IMPTYPE_HOSTUNREACH:
178 	case IMPTYPE_BADDATA:
179 #ifdef notdef
180 		addr.s_net = ip->il_network;
181 #else
182 		addr.s_net = 0;
183 #endif
184 		addr.s_imp = ip->il_imp;
185 		addr.s_host = ip->il_host;
186 		hp = hostlookup(addr);
187 		break;
188 	}
189 
190 	switch (ip->il_mtype) {
191 
192 	/*
193 	 * Data for a protocol.  Dispatch to the appropriate
194 	 * protocol routine (running at software interrupt).
195 	 * If this isn't a raw interface, advance pointer
196 	 * into mbuf past leader (done below).
197 	 */
198 	case IMPTYPE_DATA:
199 		ip->il_length =
200 			(ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader);
201 		break;
202 
203 	/*
204 	 * IMP leader error.  Reset the IMP and discard the packet.
205 	 */
206 	case IMPTYPE_BADLEADER:
207 		/*
208 		 * According to 1822 document, this message
209 		 * will be generated in response to the
210 		 * first noop sent to the IMP after
211 		 * the host resets the IMP interface.
212 		 */
213 		if (sc->imp_state != IMPS_INIT) {
214 			impmsg(sc, "leader error");
215 			hostreset(sc->imp_if.if_net);	/* XXX */
216 			impnoops(sc);
217 		}
218 		goto drop;
219 
220 	/*
221 	 * IMP going down.  Print message, and if not immediate,
222 	 * set off a timer to insure things will be reset at the
223 	 * appropriate time.
224 	 */
225 	case IMPTYPE_DOWN:
226 		if ((ip->il_link & IMP_DMASK) == 0) {
227 			sc->imp_state = IMPS_GOINGDOWN;
228 			timeout(impdown, sc, 30 * hz);
229 		}
230 		impmsg(sc, "going down %s", impmessage[ip->il_link&IMP_DMASK]);
231 		goto drop;
232 
233 	/*
234 	 * A NOP usually seen during the initialization sequence.
235 	 * Compare the local address with that in the message.
236 	 * Reset the local address notion if it doesn't match.
237 	 */
238 	case IMPTYPE_NOOP: {
239 		register struct in_addr *sin;
240 
241 		if (sc->imp_state == IMPS_DOWN) {
242 			sc->imp_state = IMPS_INIT;
243 			sc->imp_dropcnt = IMP_DROPCNT;
244 		}
245 		if (sc->imp_state != IMPS_INIT)
246 			goto drop;
247 		if (--sc->imp_dropcnt > 0)
248 			goto drop;
249 		sc->imp_state = IMPS_UP;
250 		sin = &sc->imp_if.if_addr;
251 		sc->imp_if.if_host[0] = sin->s_host = ip->il_host;
252 		sin->s_imp = ip->il_imp;
253 		impmsg(sc, "reset (host %d/imp %d)", ip->il_host,
254 			ntohs(ip->il_imp));
255 		/* restart output in case something was q'd */
256 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
257 		goto drop;
258 		}
259 
260 	/*
261 	 * RFNM or INCOMPLETE message, record in
262 	 * host table and prime output routine.
263 	 *
264 	 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES.
265 	 */
266 	case IMPTYPE_RFNM:
267 	case IMPTYPE_INCOMPLETE:
268 		if (hp && hp->h_rfnm)
269 			if (next = hostdeque(hp))
270 				(void) impsnd(sc, next);
271 		goto rawlinkin;
272 
273 	/*
274 	 * Host or IMP can't be reached.  Flush any packets
275 	 * awaiting transmission and release the host structure.
276 	 *
277 	 * TODO: NOTIFY THE PROTOCOL
278 	 */
279 	case IMPTYPE_HOSTDEAD:
280 		impmsg(sc, "host dead");	/* XXX */
281 		goto common;			/* XXX */
282 
283 	/* SHOULD SIGNAL ROUTING DAEMON */
284 	case IMPTYPE_HOSTUNREACH:
285 		impmsg(sc, "host unreachable");	/* XXX */
286 	common:
287 		if (hp)
288 			hostfree(hp);		/* won't work right */
289 		goto rawlinkin;
290 
291 	/*
292 	 * Error in data.  Clear RFNM status for this host and send
293 	 * noops to the IMP to clear the interface.
294 	 */
295 	case IMPTYPE_BADDATA:
296 		impmsg(sc, "data error");
297 		if (hp)
298 			hp->h_rfnm = 0;
299 		impnoops(sc);
300 		goto rawlinkin;
301 
302 	/*
303 	 * Interface reset.
304 	 */
305 	case IMPTYPE_RESET:
306 		impmsg(sc, "interface reset");
307 		impnoops(sc);
308 		goto drop;
309 
310 	default:
311 		sc->imp_if.if_collisions++;		/* XXX */
312 		goto drop;
313 	}
314 
315 	/*
316 	 * Queue on protocol's input queue.
317 	 */
318 	switch (ip->il_link) {
319 
320 #ifdef INET
321 	case IMPLINK_IP:
322 		m->m_len -= sizeof(struct imp_leader);
323 		m->m_off += sizeof(struct imp_leader);
324 		setipintr();
325 		inq = &ipintrq;
326 		break;
327 #endif
328 
329 	default:
330 	rawlinkin:
331 		impproto.sp_protocol = ip->il_link;
332 		impdst.sin_addr = sc->imp_if.if_addr;
333 		impsrc.sin_addr.s_net = ip->il_network;
334 		impsrc.sin_addr.s_host = ip->il_host;
335 		impsrc.sin_addr.s_imp = ip->il_imp;
336 		raw_input(m, &impproto, &impdst, &impsrc);
337 		return;
338 	}
339 	IF_ENQUEUE(inq, m);
340 	return;
341 
342 drop:
343 	m_freem(m);
344 }
345 
346 /*
347  * Bring the IMP down after notification.
348  */
349 impdown(sc)
350 	struct imp_softc *sc;
351 {
352 	sc->imp_state = IMPS_DOWN;
353 	impmsg(sc, "marked down");
354 	/* notify protocols with messages waiting? */
355 }
356 
357 /*VARARGS*/
358 impmsg(sc, fmt, a1, a2)
359 	struct imp_softc *sc;
360 	char *fmt;
361 {
362 	printf("imp%d: ", sc->imp_if.if_unit);
363 	printf(fmt, a1, a2);
364 	printf("\n");
365 }
366 
367 /*
368  * ARPAnet 1822 output routine.
369  * Called from higher level protocol routines to set up messages for
370  * transmission to the imp.  Sets up the header and calls impsnd to
371  * enqueue the message for this IMP's hardware driver.
372  */
373 impoutput(ifp, m0, pf)
374 	register struct ifnet *ifp;
375 	struct mbuf *m0;
376 {
377 	register struct imp_leader *imp;
378 	register struct mbuf *m = m0;
379 	int x, dhost, dimp, dlink, len, dnet;
380 
381 COUNT(IMPOUTPUT);
382 #ifdef notdef
383 	/*
384 	 * Don't even try if the IMP is unavailable.
385 	 */
386 	x = imp_softc[ifp->if_unit].imp_state;
387 	if (x == IMPS_DOWN || x == IMPS_GOINGDOWN)
388 		goto drop;
389 #endif
390 
391 	switch (pf) {
392 
393 #ifdef INET
394 	case PF_INET: {
395 		register struct ip *ip = mtod(m0, struct ip *);
396 
397 		dnet = ip->ip_dst.s_net;
398 		dhost = ip->ip_dst.s_host;
399 		dimp = ip->ip_dst.s_imp;
400 		dlink = IMPLINK_IP;
401 		len = ntohs(ip->ip_len);
402 		break;
403 	}
404 #endif
405 	case PF_IMPLINK:
406 		goto leaderexists;
407 
408 	default:
409 		printf("imp%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
410 		goto drop;
411 	}
412 
413 	/*
414 	 * Add IMP leader.  If there's not enough space in the
415 	 * first mbuf, allocate another.  If that should fail, we
416 	 * drop this sucker.
417 	 */
418 	if (m->m_off > MMAXOFF ||
419 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
420 		m = m_get(M_DONTWAIT);
421 		if (m == 0)
422 			goto drop;
423 		m->m_next = m0;
424 		m->m_off = MMINOFF;
425 		m->m_len = sizeof(struct imp_leader);
426 	} else {
427 		m->m_off -= sizeof(struct imp_leader);
428 		m->m_len += sizeof(struct imp_leader);
429 	}
430 	imp = mtod(m, struct imp_leader *);
431 	imp->il_format = IMP_NFF;
432 	imp->il_mtype = IMPTYPE_DATA;
433 #ifdef notdef
434 	imp->il_network = dnet;
435 #else
436 	imp->il_network = 0;
437 #endif
438 	imp->il_host = dhost;
439 	imp->il_imp = dimp;
440 	imp->il_length = htons((len + sizeof(struct imp_leader)) << 3);
441 	imp->il_link = dlink;
442 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
443 
444 leaderexists:
445 	/*
446 	 * Hand message to impsnd to perform RFNM counting
447 	 * and eventual transmission.
448 	 */
449 	return (impsnd(ifp, m));
450 drop:
451 	m_freem(m0);
452 	return (0);
453 }
454 
455 /*
456  * Put a message on an interface's output queue.
457  * Perform RFNM counting: no more than 8 message may be
458  * in flight to any one host.
459  */
460 impsnd(ifp, m)
461 	struct ifnet *ifp;
462 	struct mbuf *m;
463 {
464 	register struct imp_leader *ip;
465 	register struct host *hp;
466 	struct impcb *icp;
467 	int x;
468 
469 COUNT(IMPSND);
470 	ip = mtod(m, struct imp_leader *);
471 
472 	/*
473 	 * Do RFNM counting for data messages
474 	 * (no more than 8 outstanding to any host)
475 	 */
476 	if (ip->il_mtype == IMPTYPE_DATA) {
477 		struct in_addr addr;
478 
479 #ifdef notdef
480                 addr.s_net = ip->il_network;
481 #else
482 		addr.s_net = 0;
483 #endif
484                 addr.s_host = ip->il_host;
485                 addr.s_imp = ip->il_imp;
486 		x = splimp();
487 		if ((hp = hostlookup(addr)) == 0)
488 			hp = hostenter(addr);
489 
490 		/*
491 		 * If IMP would block, queue until RFNM
492 		 */
493 		if (hp) {
494 			if (hp->h_rfnm < 8) {
495 				hp->h_rfnm++;
496 				splx(x);
497 				goto enque;
498 			}
499 			if (hp->h_qcnt >= 8)	/* high water mark */
500 				goto drop;
501 			HOST_ENQUE(hp, m);
502 			splx(x);
503 			goto start;
504 		}
505 drop:
506 		m_freem(m);
507 		splx(x);
508 		return (0);
509 	}
510 enque:
511         x = splimp();
512 	IF_ENQUEUE(&ifp->if_snd, m);
513 	splx(x);
514 
515 start:
516 	icp = &imp_softc[ifp->if_unit].imp_cb;
517 	if (icp->ic_oactive == 0)
518 		(*icp->ic_start)(ifp->if_unit);
519 	return (1);
520 }
521 
522 /*
523  * Put three 1822 NOOPs at the head of the output queue.
524  * Part of host-IMP initialization procedure.
525  * (Should return success/failure, but noone knows
526  * what to do with this, so why bother?)
527  */
528 impnoops(sc)
529 	register struct imp_softc *sc;
530 {
531 	register i;
532 	register struct mbuf *m;
533 	register struct control_leader *cp;
534 	int x;
535 
536 COUNT(IMPNOOPS);
537 	sc->imp_state = IMPS_INIT;
538 	sc->imp_dropcnt = IMP_DROPCNT;
539 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
540 		if ((m = m_getclr(M_DONTWAIT)) == 0)
541 			return;
542 		m->m_off = MMINOFF;
543 		m->m_len = sizeof(struct control_leader);
544 		cp = mtod(m, struct control_leader *);
545 		cp->dl_format = IMP_NFF;
546                 cp->dl_link = i;
547                 cp->dl_mtype = IMPTYPE_NOOP;
548 		x = splimp();
549 		IF_PREPEND(&sc->imp_if.if_snd, m);
550 		splx(x);
551 	}
552 	if (sc->imp_cb.ic_oactive == 0)
553 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
554 }
555 
556 #ifdef IMPLEADERS
557 printleader(routine, ip)
558 	char *routine;
559 	register struct imp_leader *ip;
560 {
561 	printf("%s: ", routine);
562 	printbyte((char *)ip, 12);
563 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
564 		ip->il_flags);
565 	if (ip->il_mtype <= IMPTYPE_READY)
566 		printf("%s,", impleaders[ip->il_mtype]);
567 	else
568 		printf("%x,", ip->il_mtype);
569 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
570 		ip->il_impno);
571 	if (ip->il_link == IMPLINK_IP)
572 		printf("ip,");
573 	else
574 		printf("%x,", ip->il_link);
575 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
576 }
577 
578 printbyte(cp, n)
579 	register char *cp;
580 	int n;
581 {
582 	register i, j, c;
583 
584 	for (i=0; i<n; i++) {
585 		c = *cp++;
586 		for (j=0; j<2; j++)
587 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
588 		putchar(' ');
589 	}
590 	putchar('\n');
591 }
592 #endif
593 #endif
594