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