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