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