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