xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 11230)
1 /*	if_imp.c	4.49	83/02/23	*/
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 #include "../h/errno.h"
23 
24 #include "../vax/cpu.h"
25 #include "../vax/mtpr.h"
26 #include "../vaxuba/ubareg.h"
27 #include "../vaxuba/ubavar.h"
28 
29 #include "../net/if.h"
30 #include "../net/route.h"
31 
32 #include "../net/netisr.h"
33 #include "../netinet/in.h"
34 #include "../netinet/in_systm.h"
35 #include "../netinet/ip.h"
36 #include "../netinet/ip_var.h"
37 /* define IMPLEADERS here to get leader printing code */
38 #include "../netimp/if_imp.h"
39 #include "../netimp/if_imphost.h"
40 
41 /*
42  * IMP software status per interface.
43  * (partially shared with the hardware specific module)
44  *
45  * Each interface is referenced by a network interface structure,
46  * imp_if, which the routing code uses to locate the interface.
47  * This structure contains the output queue for the interface, its
48  * address, ...  IMP specific structures used in connecting the
49  * IMP software modules to the hardware specific interface routines
50  * are stored here.  The common structures are made visible to the
51  * interface driver by passing a pointer to the hardware routine
52  * at "attach" time.
53  *
54  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
55  */
56 struct imp_softc {
57 	struct	ifnet imp_if;		/* network visible interface */
58 	struct	impcb imp_cb;		/* hooks to hardware module */
59 	u_char	imp_state;		/* current state of IMP */
60 	char	imp_dropcnt;		/* used during initialization */
61 } imp_softc[NIMP];
62 
63 /*
64  * Messages from IMP regarding why
65  * it's going down.
66  */
67 static char *impmessage[] = {
68 	"in 30 seconds",
69 	"for hardware PM",
70 	"to reload software",
71 	"for emergency reset"
72 };
73 
74 int	impdown(), impinit(), impoutput();
75 
76 /*
77  * IMP attach routine.  Called from hardware device attach routine
78  * at configuration time with a pointer to the UNIBUS device structure.
79  * Sets up local state and returns pointer to base of ifnet+impcb
80  * structures.  This is then used by the device's attach routine
81  * set up its back pointers.
82  */
83 impattach(ui, reset)
84 	struct uba_device *ui;
85 	int (*reset)();
86 {
87 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
88 	register struct ifnet *ifp = &sc->imp_if;
89 	struct sockaddr_in *sin;
90 
91 	/* UNIT COULD BE AMBIGUOUS */
92 	ifp->if_unit = ui->ui_unit;
93 	ifp->if_name = "imp";
94 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
95 	ifp->if_net = ui->ui_flags;
96 	ifp->if_reset = reset;
97 	/* the host and imp fields will be filled in by the imp */
98 	sin = (struct sockaddr_in *)&ifp->if_addr;
99 	sin->sin_family = AF_INET;
100 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
101 	ifp->if_init = impinit;
102 	ifp->if_output = impoutput;
103 	/* reset is handled at the hardware level */
104 	if_attach(ifp);
105 	return ((int)&sc->imp_if);
106 }
107 
108 /*
109  * IMP initialization routine: call hardware module to
110  * setup UNIBUS resources, init state and get ready for
111  * NOOPs the IMP should send us, and that we want to drop.
112  */
113 impinit(unit)
114 	int unit;
115 {
116 	int s = splimp();
117 	register struct imp_softc *sc = &imp_softc[unit];
118 
119 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
120 		sc->imp_state = IMPS_DOWN;
121 		sc->imp_if.if_flags &= ~IFF_UP;
122 		splx(s);
123 		return;
124 	}
125 	sc->imp_state = IMPS_INIT;
126 	impnoops(sc);
127 	splx(s);
128 }
129 
130 struct sockproto impproto = { PF_IMPLINK };
131 struct sockaddr_in impdst = { AF_IMPLINK };
132 struct sockaddr_in impsrc = { AF_IMPLINK };
133 #ifdef IMPLEADERS
134 int	impprintfs = 0;
135 #endif
136 
137 /*
138  * ARPAnet 1822 input routine.
139  * Called from hardware input interrupt routine to handle 1822
140  * IMP-host messages.  Type 0 messages (non-control) are
141  * passed to higher level protocol processors on the basis
142  * of link number.  Other type messages (control) are handled here.
143  */
144 impinput(unit, m)
145 	int unit;
146 	register struct mbuf *m;
147 {
148 	register struct imp_leader *ip;
149 	register struct imp_softc *sc = &imp_softc[unit];
150 	register struct host *hp;
151 	register struct ifqueue *inq;
152 	struct control_leader *cp;
153 	struct in_addr addr;
154 	struct mbuf *next;
155 	struct sockaddr_in *sin;
156 
157 	/* verify leader length. */
158 	if (m->m_len < sizeof(struct control_leader) &&
159 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
160 		return;
161 	cp = mtod(m, struct control_leader *);
162 	if (cp->dl_mtype == IMPTYPE_DATA)
163 		if (m->m_len < sizeof(struct imp_leader) &&
164 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
165 			return;
166 	ip = mtod(m, struct imp_leader *);
167 #ifdef IMPLEADERS
168 	if (impprintfs)
169 		printleader("impinput", ip);
170 #endif
171 
172 	/* check leader type */
173 	if (ip->il_format != IMP_NFF) {
174 		sc->imp_if.if_collisions++;	/* XXX */
175 		goto drop;
176 	}
177 
178 	if (ip->il_mtype != IMPTYPE_DATA) {
179 #ifdef notdef
180 		addr.s_net = ip->il_network;
181 #else
182 		addr.s_net = sc->imp_if.if_net;
183 #endif
184 		addr.s_imp = ip->il_imp;
185 		addr.s_host = ip->il_host;
186 	}
187 	switch (ip->il_mtype) {
188 
189 	case IMPTYPE_DATA:
190 		break;
191 
192 	/*
193 	 * IMP leader error.  Reset the IMP and discard the packet.
194 	 */
195 	case IMPTYPE_BADLEADER:
196 		/*
197 		 * According to 1822 document, this message
198 		 * will be generated in response to the
199 		 * first noop sent to the IMP after
200 		 * the host resets the IMP interface.
201 		 */
202 		if (sc->imp_state != IMPS_INIT) {
203 			impmsg(sc, "leader error");
204 			hostreset(sc->imp_if.if_net);
205 			impnoops(sc);
206 		}
207 		goto drop;
208 
209 	/*
210 	 * IMP going down.  Print message, and if not immediate,
211 	 * set off a timer to insure things will be reset at the
212 	 * appropriate time.
213 	 */
214 	case IMPTYPE_DOWN:
215 		if (sc->imp_state < IMPS_INIT)
216 			goto drop;
217 		if ((ip->il_link & IMP_DMASK) == 0) {
218 			sc->imp_state = IMPS_GOINGDOWN;
219 			timeout(impdown, (caddr_t)sc, 30 * hz);
220 		}
221 		impmsg(sc, "going down %s",
222 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
223 		goto drop;
224 
225 	/*
226 	 * A NOP usually seen during the initialization sequence.
227 	 * Compare the local address with that in the message.
228 	 * Reset the local address notion if it doesn't match.
229 	 */
230 	case IMPTYPE_NOOP:
231 		if (sc->imp_state == IMPS_DOWN) {
232 			sc->imp_state = IMPS_INIT;
233 			sc->imp_dropcnt = IMP_DROPCNT;
234 		}
235 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
236 			goto drop;
237 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
238 		if (sin->sin_addr.s_host != ip->il_host ||
239 		    sin->sin_addr.s_imp != ip->il_imp) {
240 			sc->imp_if.if_host[0] =
241 				sin->sin_addr.s_host = ip->il_host;
242 			sin->sin_addr.s_imp = ip->il_imp;
243 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
244 				ntohs(ip->il_imp));
245 		}
246 		sc->imp_state = IMPS_UP;
247 		sc->imp_if.if_flags |= IFF_UP;
248 		if_rtinit(&sc->imp_if, RTF_UP);
249 		goto drop;
250 
251 	/*
252 	 * RFNM or INCOMPLETE message, send next
253 	 * message on the q.  We could pass incomplete's
254 	 * up to the next level, but this currently isn't
255 	 * needed.
256 	 */
257 	case IMPTYPE_RFNM:
258 	case IMPTYPE_INCOMPLETE:
259 		if (hp = hostlookup(addr)) {
260 			if (hp->h_rfnm == 0)
261 				hp->h_flags &= ~HF_INUSE;
262 			else if (next = hostdeque(hp))
263 				(void) impsnd(&sc->imp_if, next);
264 		}
265 		goto drop;
266 
267 	/*
268 	 * Host or IMP can't be reached.  Flush any packets
269 	 * awaiting transmission and release the host structure.
270 	 */
271 	case IMPTYPE_HOSTDEAD:
272 	case IMPTYPE_HOSTUNREACH:
273 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
274 		    hostlookup(addr));
275 		goto rawlinkin;
276 
277 	/*
278 	 * Error in data.  Clear RFNM status for this host and send
279 	 * noops to the IMP to clear the interface.
280 	 */
281 	case IMPTYPE_BADDATA:
282 		impmsg(sc, "data error");
283 		if (hp = hostlookup(addr))
284 			hp->h_rfnm = 0;
285 		impnoops(sc);
286 		goto drop;
287 
288 	/*
289 	 * Interface reset.
290 	 */
291 	case IMPTYPE_RESET:
292 		impmsg(sc, "interface reset");
293 		impnoops(sc);
294 		goto drop;
295 
296 	default:
297 		sc->imp_if.if_collisions++;		/* XXX */
298 		goto drop;
299 	}
300 
301 	/*
302 	 * Data for a protocol.  Dispatch to the appropriate
303 	 * protocol routine (running at software interrupt).
304 	 * If this isn't a raw interface, advance pointer
305 	 * into mbuf past leader.
306 	 */
307 	switch (ip->il_link) {
308 
309 #ifdef INET
310 	case IMPLINK_IP:
311 		m->m_len -= sizeof(struct imp_leader);
312 		m->m_off += sizeof(struct imp_leader);
313 		schednetisr(NETISR_IP);
314 		inq = &ipintrq;
315 		break;
316 #endif
317 
318 	default:
319 	rawlinkin:
320 		impproto.sp_protocol = ip->il_link;
321 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
322 		impdst.sin_addr = sin->sin_addr;;
323 		impsrc.sin_addr.s_net = ip->il_network;
324 		impsrc.sin_addr.s_host = ip->il_host;
325 		impsrc.sin_addr.s_imp = ip->il_imp;
326 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
327 		  (struct sockaddr *)&impdst);
328 		return;
329 	}
330 	if (IF_QFULL(inq)) {
331 		IF_DROP(inq);
332 		goto drop;
333 	}
334 	IF_ENQUEUE(inq, m);
335 	return;
336 
337 drop:
338 	m_freem(m);
339 }
340 
341 /*
342  * Bring the IMP down after notification.
343  */
344 impdown(sc)
345 	struct imp_softc *sc;
346 {
347 	int s = splimp();
348 
349 	sc->imp_state = IMPS_DOWN;
350 	impmsg(sc, "marked down");
351 	hostreset(sc->imp_if.if_net);
352 	if_down(&sc->imp_if);
353 	splx(s);
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 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, MT_HEADER);
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 
570 	sc->imp_dropcnt = IMP_DROPCNT;
571 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
572 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
573 			return;
574 		m->m_len = sizeof(struct control_leader);
575 		cp = mtod(m, struct control_leader *);
576 		cp->dl_format = IMP_NFF;
577                 cp->dl_link = i;
578                 cp->dl_mtype = IMPTYPE_NOOP;
579 		IF_PREPEND(&sc->imp_if.if_snd, m);
580 	}
581 	if (sc->imp_cb.ic_oactive == 0)
582 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
583 }
584 
585 #ifdef IMPLEADERS
586 printleader(routine, ip)
587 	char *routine;
588 	register struct imp_leader *ip;
589 {
590 	printf("%s: ", routine);
591 	printbyte((char *)ip, 12);
592 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
593 		ip->il_flags);
594 	if (ip->il_mtype <= IMPTYPE_READY)
595 		printf("%s,", impleaders[ip->il_mtype]);
596 	else
597 		printf("%x,", ip->il_mtype);
598 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
599 		ntohs(ip->il_imp));
600 	if (ip->il_link == IMPLINK_IP)
601 		printf("ip,");
602 	else
603 		printf("%x,", ip->il_link);
604 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
605 }
606 
607 printbyte(cp, n)
608 	register char *cp;
609 	int n;
610 {
611 	register i, j, c;
612 
613 	for (i=0; i<n; i++) {
614 		c = *cp++;
615 		for (j=0; j<2; j++)
616 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
617 		putchar(' ');
618 	}
619 	putchar('\n');
620 }
621 #endif
622 #endif
623