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