xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 34209)
1 /*
2  * Copyright (c) 1982,1986,1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  *	@(#)if_imp.c	7.5 (Berkeley) 05/06/88
13  */
14 
15 #include "imp.h"
16 #if NIMP > 0
17 /*
18  * ARPANET IMP (PSN) interface driver.
19  *
20  * The IMP-host protocol (AHIP) is handled here, leaving
21  * hardware specifics to the lower level interface driver.
22  */
23 #include "param.h"
24 #include "systm.h"
25 #include "mbuf.h"
26 #include "buf.h"
27 #include "protosw.h"
28 #include "socket.h"
29 #include "time.h"
30 #include "kernel.h"
31 #include "errno.h"
32 #include "ioctl.h"
33 #include "syslog.h"
34 
35 #include "../machine/mtpr.h"
36 
37 #include "../net/if.h"
38 #include "../net/netisr.h"
39 #include "../netinet/in.h"
40 #include "../netinet/in_systm.h"
41 #include "../netinet/in_var.h"
42 #include "../netinet/ip.h"
43 #include "../netinet/ip_var.h"
44 #define IMPMESSAGES
45 /* define IMPLEADERS here to get leader printing code */
46 #include "if_imp.h"
47 #include "if_imphost.h"
48 
49 struct	imp_softc imp_softc[NIMP];
50 #ifndef lint
51 int	nimp = NIMP;			/* for netstat */
52 #endif
53 struct	ifqueue impintrq;
54 int	impqmaxlen = IFQ_MAXLEN;
55 int	imphqlen = 12 + IMP_MAXHOSTMSG;	/* max packets to queue per host */
56 
57 int	imppri = LOG_ERR;
58 #ifdef IMPLEADERS
59 int	impprintfs = 0;
60 #endif
61 #ifdef IMPINIT
62 int	imptraceinit = 0;
63 #endif
64 
65 
66 #define HOSTDEADTIMER	(30 * PR_SLOWHZ)	/* How long to wait when down */
67 
68 int	impdown(), impinit(), impioctl(), impoutput(), imptimo();
69 
70 /*
71  * IMP attach routine.  Called from hardware device attach routine
72  * at configuration time with a pointer to the 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 struct imp_softc *
78 impattach(hwname, hwunit, reset)
79 	char *hwname;
80 	int hwunit;
81 	int (*reset)();
82 {
83 	struct imp_softc *sc;
84 	register struct ifnet *ifp;
85 	static int impunit;
86 
87 #ifdef lint
88 	impintr();
89 #endif
90 	if (impunit >= NIMP) {
91 		printf("imp%d: not configured\n", impunit++);
92 		return (0);
93 	}
94 	sc = &imp_softc[impunit];
95 	ifp = &sc->imp_if;
96 	sc->imp_cb.ic_hwname = hwname;
97 	sc->imp_cb.ic_hwunit = hwunit;
98 	ifp->if_unit = impunit;
99 	ifp->if_name = "imp";
100 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
101 	ifp->if_reset = reset;
102 	ifp->if_init = impinit;
103 	ifp->if_ioctl = impioctl;
104 	ifp->if_output = impoutput;
105 	ifp->if_watchdog = imptimo;
106 	if_attach(ifp);
107 	impunit++;
108 	return (sc);
109 }
110 
111 /*
112  * IMP initialization routine: call hardware module to
113  * setup resources, init state and get ready for
114  * NOOPs the IMP should send us, and that we want to drop.
115  */
116 impinit(unit)
117 	int unit;
118 {
119 	int s;
120 	register struct imp_softc *sc = &imp_softc[unit];
121 
122 	if (sc->imp_if.if_addrlist == 0)
123 		return;
124 	s = splimp();
125 #ifdef IMPINIT
126 	if (imptraceinit)
127 		log(imppri, "impinit\n");
128 #endif
129 	sc->imp_state = IMPS_WINIT;
130 	if ((*sc->imp_cb.ic_init)(sc->imp_cb.ic_hwunit) == 0)
131 		sc->imp_if.if_flags &= ~IFF_UP;
132 	impintrq.ifq_maxlen = impqmaxlen;
133 	splx(s);
134 }
135 
136 /*
137  * ARPAnet 1822/AHIP input routine.
138  * Called from hardware input interrupt routine to handle 1822
139  * IMP-host messages.  Data messages are passed to higher-level
140  * protocol processors on the basis of link number.
141  * Other type messages (control) are handled here.
142  */
143 impinput(unit, m)
144 	int unit;
145 	register struct mbuf *m;
146 {
147 	register struct control_leader *cp;
148 #define	ip	((struct imp_leader *)cp)
149 	register struct imp_softc *sc = &imp_softc[unit];
150 	struct ifnet *ifp;
151 	register struct host *hp;
152 	register struct ifqueue *inq;
153 	struct mbuf *next;
154 	struct sockaddr_in *sin;
155 	int s;
156 
157 	/*
158 	 * Pull the interface pointer out of the mbuf
159 	 * and save for later; adjust mbuf to look at rest of data.
160 	 */
161 	ifp = *(mtod(m, struct ifnet **));
162 	IF_ADJ(m);
163 	/* verify leader length. */
164 	if (m->m_len < sizeof(struct control_leader) &&
165 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
166 		return;
167 	cp = mtod(m, struct control_leader *);
168 	if (cp->dl_mtype == IMPTYPE_DATA &&
169 	    m->m_len < sizeof(struct imp_leader)) {
170 		if ((m = m_pullup(m, sizeof(struct imp_leader))) == 0)
171 			return;
172 		cp = mtod(m, struct control_leader *);
173 	}
174 #ifdef IMPLEADERS
175 	if (impprintfs)
176 		printleader("impinput", ip);
177 #endif
178 	inq = &impintrq;
179 
180 	/* check leader type */
181 	if (cp->dl_format != IMP_NFF) {
182 		/*
183 		 * We get 1822L NOOPs and RESET
184 		 * at initialization.
185 		 */
186 #ifdef IMPINIT
187 		if (imptraceinit)
188 			log(imppri, "input, format %x mtype %d\n",
189 			    cp->dl_format, cp->dl_mtype);
190 #endif
191 		if (cp->dl_format != IMP_1822L_I2H ||
192 		    (cp->dl_mtype != IMPTYPE_NOOP &&
193 		    cp->dl_mtype != IMPTYPE_RESET)) {
194 			sc->imp_garbage++;
195 			sc->imp_if.if_collisions++;	/* XXX */
196 		}
197 	} else switch (cp->dl_mtype) {
198 
199 	case IMPTYPE_DATA:
200 		/*
201 		 * Data for a protocol.  Dispatch to the appropriate
202 		 * protocol routine (running at software interrupt).
203 		 * If this isn't a raw interface, advance pointer
204 		 * into mbuf past leader.
205 		 */
206 		switch (cp->dl_link) {
207 
208 		case IMPLINK_IP:
209 			m->m_len -= sizeof(struct imp_leader);
210 			m->m_off += sizeof(struct imp_leader);
211 			schednetisr(NETISR_IP);
212 			inq = &ipintrq;
213 			break;
214 
215 		default:
216 			break;
217 		}
218 		break;
219 
220 	/*
221 	 * IMP leader error.  Reset the IMP and discard the packet.
222 	 */
223 	case IMPTYPE_BADLEADER:
224 		/*
225 		 * According to 1822 document, this message
226 		 * will be generated in response to the
227 		 * first noop sent to the IMP after
228 		 * the host resets the IMP interface.
229 		 */
230 #ifdef IMPINIT
231 		if (imptraceinit)
232 			log(imppri, "badleader\n");
233 #endif
234 		if (sc->imp_state != IMPS_INIT) {
235 			impmsg(sc, "leader error");
236 			sc->imp_msgready = 0;
237 			hostreset(unit);
238 			impnoops(sc);
239 			sc->imp_garbage++;
240 		}
241 		break;
242 
243 	/*
244 	 * IMP going down.  Print message, and if not immediate,
245 	 * set off a timer to insure things will be reset at the
246 	 * appropriate time.
247 	 */
248 	case IMPTYPE_DOWN:
249 	    {	int type, when;
250 
251 		type = cp->dl_link & IMP_DMASK;
252 		when = (cp->dl_link & IMPDOWN_WHENMASK) >> IMPDOWN_WHENSHIFT;
253 #ifdef IMPINIT
254 		if (imptraceinit)
255 			log(imppri, "input DOWN %s %d\n",
256 			    impmessage[type], when * IMPDOWN_WHENUNIT);
257 #endif
258 		if (type != IMPDOWN_GOING && when)
259 			impmsg(sc, "going down %s in %d minutes",
260 			    (u_int)impmessage[type], when * IMPDOWN_WHENUNIT);
261 		else
262 			impmsg(sc, "going down %s", (u_int)impmessage[type]);
263 		if (sc->imp_state != IMPS_UP)
264 			break;
265 		if (type == IMPDOWN_GOING) {
266 			sc->imp_state = IMPS_GOINGDOWN;
267 			timeout(impdown, (caddr_t)sc, IMPTV_DOWN * hz);
268 		} else if (when == 0)
269 			sc->imp_state = IMPS_WINIT;
270 		sc->imp_dropcnt = 0;
271 		break;
272 	    }
273 
274 	/*
275 	 * A NOP, usually seen during the initialization sequence.
276 	 * Compare the local address with that in the message.
277 	 * Reset the local address notion if it doesn't match.
278 	 */
279 	case IMPTYPE_NOOP:
280 #ifdef IMPINIT
281 		if (imptraceinit)
282 			log(imppri, "noop\n");
283 #endif
284 		if (sc->imp_state == IMPS_WINIT) {
285 			sc->imp_dropcnt = 0;
286 			impnoops(sc);
287 			sc->imp_state = IMPS_INIT;
288 		}
289 		sc->imp_dropcnt++;
290 		if (sc->imp_state == IMPS_INIT && cp->dl_imp != 0) {
291 			struct in_addr leader_addr;
292 
293 			sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
294 			imp_leader_to_addr(&leader_addr, cp, &sc->imp_if);
295 			if (sin->sin_addr.s_addr != leader_addr.s_addr) {
296 				impmsg(sc, "address reset to x%x (%d/%d)",
297 					ntohl(leader_addr.s_addr),
298 					(u_int)cp->dl_host,
299 					ntohs(cp->dl_imp));
300 				sin->sin_addr.s_addr = leader_addr.s_addr;
301 			}
302 		}
303 		break;
304 
305 	/*
306 	 * RFNM or INCOMPLETE message, decrement rfnm count
307 	 * and prepare to send next message.
308 	 * If the rfnm allows another queued
309 	 * message to be sent, bump msgready
310 	 * and start IMP if idle.
311 	 * We could pass incomplete's up to the next level,
312 	 * but this currently isn't needed.
313 	 * Pass "bad" incompletes and rfnms to the raw socket.
314 	 */
315 	case IMPTYPE_INCOMPLETE:
316 		sc->imp_incomplete++;
317 		/* FALL THROUGH */
318 	case IMPTYPE_RFNM:
319 		if ((hp = hostlookup(cp->dl_imp, cp->dl_host, unit)) == 0 ||
320 		    hp->h_rfnm == 0) {
321 			sc->imp_badrfnm++;
322 			if (hp)
323 				hostfree(hp);
324 			break;
325 		}
326 		imprestarthost(sc, hp);
327 		if (cp->dl_mtype == IMPTYPE_RFNM)
328 			goto drop;
329 		break;
330 
331 	/*
332 	 * Host or IMP can't be reached.  Flush any packets
333 	 * awaiting transmission and release the host structure.
334 	 * Enqueue for notifying protocols at software interrupt time.
335 	 */
336 	case IMPTYPE_HOSTDEAD:
337 	case IMPTYPE_HOSTUNREACH:
338 		if (hp = hostlookup(cp->dl_imp, cp->dl_host, unit)) {
339 			hp->h_flags |= (1 << (int)cp->dl_mtype);
340 			sc->imp_msgready -=
341 			   MIN(hp->h_qcnt, IMP_MAXHOSTMSG - hp->h_rfnm);
342 			hp->h_rfnm = 0;
343 			hostflush(hp);
344 			hp->h_timer = HOSTDEADTIMER;
345 		}
346 		break;
347 
348 	/*
349 	 * Error in data.  Clear RFNM status for this host and send
350 	 * noops to the IMP to clear the interface.
351 	 */
352 	case IMPTYPE_BADDATA:
353 		impmsg(sc, "data error");
354 		if (hp = hostlookup(cp->dl_imp, cp->dl_host, unit)) {
355 			sc->imp_msgready -=
356 			   MIN(hp->h_qcnt, IMP_MAXHOSTMSG - hp->h_rfnm);
357 			if (hp->h_rfnm)
358 				hostrelease(hp);
359 			else
360 				hostfree(hp);
361 		}
362 		impnoops(sc);
363 		break;
364 
365 	/*
366 	 * Interface reset.
367 	 */
368 	case IMPTYPE_RESET:
369 #ifdef IMPINIT
370 		if (imptraceinit)
371 			log(imppri, "reset complete\n");
372 #endif
373 		if (sc->imp_state != IMPS_INIT) {
374 			impmsg(sc, "interface reset");
375 			impnoops(sc);
376 		}
377 		/* clear RFNM counts */
378 		sc->imp_msgready = 0;
379 		hostreset(unit);
380 		if (sc->imp_state != IMPS_DOWN) {
381 			sc->imp_state = IMPS_UP;
382 			sc->imp_if.if_flags |= IFF_UP;
383 #ifdef IMPINIT
384 			if (imptraceinit)
385 				log(imppri, "IMP UP\n");
386 #endif
387 		}
388 		break;
389 
390 	default:
391 		sc->imp_garbage++;
392 		sc->imp_if.if_collisions++;		/* XXX */
393 		break;
394 	}
395 
396 	if (inq == &impintrq)
397 		schednetisr(NETISR_IMP);
398 	/*
399 	 * Re-insert interface pointer in the mbuf chain
400 	 * for the next protocol up.
401 	 */
402 	if (M_HASCL(m) && (mtod(m, int) & CLOFSET) < sizeof(struct ifnet *)) {
403 		struct mbuf *n;
404 
405 		MGET(n, M_DONTWAIT, MT_HEADER);
406 		if (n == 0)
407 			goto drop;
408 		n->m_next = m;
409 		m = n;
410 		m->m_len = 0;
411 		m->m_off = MMINOFF + sizeof(struct ifnet  *);
412 	}
413 	m->m_off -= sizeof(struct ifnet *);
414 	m->m_len += sizeof(struct ifnet *);
415 	*(mtod(m, struct ifnet **)) = ifp;
416 
417 	s = splimp();
418 	if (!IF_QFULL(inq)) {
419 		IF_ENQUEUE(inq, m);
420 		splx(s);
421 		return;
422 	}
423 	splx(s);
424 	IF_DROP(inq);
425 drop:
426 	m_freem(m);
427 #undef ip
428 }
429 
430 /*
431  * Bring the IMP down after notification.
432  */
433 impdown(sc)
434 	struct imp_softc *sc;
435 {
436 	int s = splimp();
437 
438 	if (sc->imp_state == IMPS_GOINGDOWN) {
439 		sc->imp_state = IMPS_WINIT;
440 		impmsg(sc, "marked down");
441 		sc->imp_msgready = 0;
442 		hostreset(sc->imp_if.if_unit);
443 		if_down(&sc->imp_if);
444 	}
445 #ifdef IMPINIT
446 	else if (imptraceinit)
447 		log(imppri, "impdown, state now %d (ignored)\n", sc->imp_state);
448 #endif
449 	splx(s);
450 }
451 
452 /*VARARGS2*/
453 impmsg(sc, fmt, a1, a2, a3)
454 	struct imp_softc *sc;
455 	char *fmt;
456 	u_int a1;
457 {
458 
459 	log(imppri, "imp%d: %r\n", sc->imp_if.if_unit, fmt, &a1);
460 }
461 
462 struct sockproto impproto = { PF_IMPLINK };
463 struct sockaddr_in impdst = { AF_IMPLINK };
464 struct sockaddr_in impsrc = { AF_IMPLINK };
465 
466 /*
467  * Pick up the IMP "error" messages enqueued earlier,
468  * passing these up to the higher level protocol
469  * and the raw interface.
470  */
471 impintr()
472 {
473 	register struct mbuf *m;
474 	register struct control_leader *cp;
475 	struct ifnet *ifp;
476 	int s;
477 
478 	for (;;) {
479 		s = splimp();
480 		IF_DEQUEUEIF(&impintrq, m, ifp);
481 		splx(s);
482 		if (m == 0)
483 			return;
484 
485 		cp = mtod(m, struct control_leader *);
486 		imp_leader_to_addr(&impsrc.sin_addr, cp, ifp);
487 		impproto.sp_protocol = cp->dl_link;
488 		impdst.sin_addr = IA_SIN(ifp->if_addrlist)->sin_addr;
489 
490 		if (cp->dl_mtype == IMPTYPE_HOSTDEAD ||
491 		    cp->dl_mtype == IMPTYPE_HOSTUNREACH)
492 			switch (cp->dl_link) {
493 
494 			case IMPLINK_IP:
495 				pfctlinput((int)cp->dl_mtype,
496 				    (struct sockaddr *)&impsrc);
497 				break;
498 			default:
499 				raw_ctlinput((int)cp->dl_mtype,
500 				    (struct sockaddr *)&impsrc);
501 				break;
502 			}
503 
504 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
505 		  (struct sockaddr *)&impdst);
506 	}
507 }
508 
509 /*
510  * ARPAnet 1822 output routine.
511  * Called from higher level protocol routines to set up messages for
512  * transmission to the imp.  Sets up the header and calls impsnd to
513  * enqueue the message for this IMP's hardware driver.
514  */
515 impoutput(ifp, m0, dst)
516 	register struct ifnet *ifp;
517 	struct mbuf *m0;
518 	struct sockaddr *dst;
519 {
520 	register struct imp_leader *imp;
521 	register struct mbuf *m = m0;
522 	caddr_t pkt = mtod(m, caddr_t);
523 	int error = 0;
524 
525 	/*
526 	 * Don't even try if the IMP is unavailable.
527 	 */
528 	if (!IMPS_RUNNING(imp_softc[ifp->if_unit].imp_state)) {
529 		error = ENETDOWN;
530 		goto drop;
531 	}
532 
533 	/*
534 	 * If AF_IMPLINK, leader exists; just send.
535 	 * Otherwise, construct leader according to address family.
536 	 */
537 	if (dst->sa_family != AF_IMPLINK) {
538 		/*
539 		 * Add IMP leader.  If there's not enough space in the
540 		 * first mbuf, allocate another.  If that should fail, we
541 		 * drop this sucker.
542 		 */
543 		if (m->m_off > MMAXOFF ||
544 		    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
545 			MGET(m, M_DONTWAIT, MT_HEADER);
546 			if (m == 0) {
547 				error = ENOBUFS;
548 				goto drop;
549 			}
550 			m->m_next = m0;
551 			m->m_len = sizeof(struct imp_leader);
552 		} else {
553 			m->m_off -= sizeof(struct imp_leader);
554 			m->m_len += sizeof(struct imp_leader);
555 		}
556 		imp = mtod(m, struct imp_leader *);
557 		imp->il_format = IMP_NFF;
558 		imp->il_mtype = IMPTYPE_DATA;
559 		imp->il_flags = 0;
560 		imp->il_htype = 0;
561 		imp->il_subtype = 0;
562 
563 		switch (dst->sa_family) {
564 
565 		case AF_INET:
566 			imp->il_link = IMPLINK_IP;
567 			imp_addr_to_leader((struct control_leader *)imp,
568 				((struct sockaddr_in *)dst)->sin_addr.s_addr);
569 			imp->il_length = htons(ntohs((u_short)
570 			    ((struct ip *)pkt)->ip_len) << 3);
571 			break;
572 
573 		default:
574 			printf("imp%d: can't handle af%d\n", ifp->if_unit,
575 				dst->sa_family);
576 			error = EAFNOSUPPORT;
577 			m0 = m;
578 			goto drop;
579 		}
580 	}
581 	return (impsnd(ifp, m));
582 drop:
583 	m_freem(m0);
584 	return (error);
585 }
586 
587 /*
588  * Put a message on an interface's output queue.
589  * Perform RFNM counting: no more than 8 message may be
590  * in flight to any one host.
591  */
592 impsnd(ifp, m)
593 	struct ifnet *ifp;
594 	struct mbuf *m;
595 {
596 	register struct control_leader *imp;
597 	register struct host *hp;
598 	register struct imp_softc *sc = &imp_softc[ifp->if_unit];
599 	int s, error = 0;
600 
601 	imp = mtod(m, struct control_leader *);
602 
603 	/*
604 	 * Do RFNM counting for data messages
605 	 * (no more than 8 outstanding to any host).
606 	 * Queue data messages per host if 8 are already outstanding
607 	 * or if the hardware interface is already doing output.
608 	 * Increment imp_msgready if the message could be sent now,
609 	 * but must be queued because the imp output is busy.
610 	 */
611 	s = splimp();
612 	if (imp->dl_mtype == IMPTYPE_DATA) {
613 		hp = hostenter(imp->dl_imp, imp->dl_host, ifp->if_unit);
614 		if (hp) {
615 			if (hp->h_flags & (HF_DEAD|HF_UNREACH))
616 				error = hp->h_flags & HF_DEAD ?
617 				    EHOSTDOWN : EHOSTUNREACH;
618 			else if (hp->h_rfnm < IMP_MAXHOSTMSG &&
619 			    sc->imp_cb.ic_oactive == 0) {
620 				/*
621 				 * Send without queuing;
622 				 * adjust rfnm count and timer.
623 				 */
624 				if (hp->h_rfnm++ == 0)
625 				    hp->h_timer = RFNMTIMER;
626 				goto send;
627 			} else if (hp->h_rfnm + hp->h_qcnt < imphqlen) {
628 				HOST_ENQUE(hp, m);
629 				if (hp->h_rfnm + hp->h_qcnt <= IMP_MAXHOSTMSG)
630 					sc->imp_msgready++;
631 			} else
632 				error = ENOBUFS;
633 		} else
634 			error = ENOBUFS;
635 	} else if (sc->imp_cb.ic_oactive == 0)
636 		goto send;
637 	else
638 		IF_ENQUEUE(&ifp->if_snd, m);
639 
640 	splx(s);
641 	if (error)
642 		m_freem(m);
643 	return (error);
644 
645 send:
646 	sc->imp_if.if_timer = IMP_OTIMER;
647 	(*sc->imp_cb.ic_output)(sc->imp_cb.ic_hwunit, m);
648 	splx(s);
649 	return (0);
650 }
651 
652 /*
653  * Start another output operation on IMP; called from hardware
654  * transmit-complete interrupt routine at splimp or from imp routines
655  * when output is not in progress.  If there are any packets on shared
656  * output queue, send them, otherwise send the next data packet for a host.
657  * Host data packets are sent round-robin based on destination by walking
658  * the host list.
659  */
660 impstart(sc)
661 	register struct imp_softc *sc;
662 {
663 	register struct mbuf *m;
664 	int first = 1;				/* XXX */
665 	register struct host *hp;
666 	int index;
667 
668 	IF_DEQUEUE(&sc->imp_if.if_snd, m);
669 	if (m) {
670 		sc->imp_if.if_timer = IMP_OTIMER;
671 		(*sc->imp_cb.ic_output)(sc->imp_cb.ic_hwunit, m);
672 		return;
673 	}
674 	if (sc->imp_msgready) {
675 		if ((m = sc->imp_hostq) == 0 && (m = sc->imp_hosts) == 0)
676 			panic("imp msgready");
677 		index = sc->imp_hostent;
678 		for (hp = &mtod(m, struct hmbuf *)->hm_hosts[index]; ;
679 		    hp++, index++) {
680 			if (index >= HPMBUF) {
681 				if ((m = m->m_next) == 0)
682 					m = sc->imp_hosts;
683 				index = 0;
684 				hp = mtod(m, struct hmbuf *)->hm_hosts;
685 				first = 0;		/* XXX */
686 			}
687 			if (hp->h_qcnt && hp->h_rfnm < IMP_MAXHOSTMSG) {
688 				/*
689 				 * Found host entry with another message
690 				 * to send.  Deliver it to the IMP.
691 				 * Start with succeeding host next time.
692 				 */
693 				impstarthost(sc, hp);
694 				sc->imp_hostq = m;
695 				sc->imp_hostent = index + 1;
696 				return;
697 			}
698 			if (m == sc->imp_hostq && !first &&
699 			    index + 1 >= sc->imp_hostent) {	/* XXX */
700 				log(imppri, "imp: can't find %d msgready\n",
701 				    sc->imp_msgready);
702 				sc->imp_msgready = 0;
703 				break;
704 			}
705 		}
706 	}
707 	sc->imp_if.if_timer = 0;
708 }
709 
710 /*
711  * Restart output for a host that has received a RFNM
712  * or incomplete or has timed out while waiting for a RFNM.
713  * Must be called at splimp.
714  */
715 imprestarthost(sc, hp)
716 	register struct imp_softc *sc;
717 	struct host *hp;
718 {
719 
720 	if (--hp->h_rfnm > 0)
721 		hp->h_timer = RFNMTIMER;
722 	/*
723 	 * If the RFNM moved a queued message into the window,
724 	 * update msgready and start IMP if idle.
725 	 */
726 	if (hp->h_qcnt > IMP_MAXHOSTMSG - 1 - hp->h_rfnm) {
727 		sc->imp_msgready++;
728 		if (sc->imp_cb.ic_oactive == 0)
729 			impstarthost(sc, hp);
730 	}
731 	if (hp->h_rfnm == 0 && hp->h_qcnt == 0)
732 		hostidle(hp);
733 }
734 
735 /*
736  * Send the next message queued for a host
737  * when ready to send another message to the IMP.
738  * Called only when output is not in progress.
739  * Bump RFNM counter and start RFNM timer
740  * when we send the message to the IMP.
741  * Must be called at splimp.
742  */
743 impstarthost(sc, hp)
744 	register struct imp_softc *sc;
745 	register struct host *hp;
746 {
747 	struct mbuf *m;
748 
749 	if (hp->h_rfnm++ == 0)
750 		hp->h_timer = RFNMTIMER;
751 	HOST_DEQUE(hp, m);
752 	sc->imp_if.if_timer = IMP_OTIMER;
753 	(*sc->imp_cb.ic_output)(sc->imp_cb.ic_hwunit, m);
754 	sc->imp_msgready--;
755 }
756 
757 /*
758  * "Watchdog" timeout.  When the output timer expires,
759  * we assume we have been blocked by the imp.
760  * No need to restart, just collect statistics.
761  */
762 imptimo(unit)
763 	int unit;
764 {
765 
766 	imp_softc[unit].imp_block++;
767 }
768 
769 /*
770  * Put three 1822 NOOPs at the head of the output queue.
771  * Part of host-IMP initialization procedure.
772  * (Should return success/failure, but noone knows
773  * what to do with this, so why bother?)
774  * This routine is always called at splimp, so we don't
775  * protect the call to IF_PREPEND.
776  */
777 impnoops(sc)
778 	register struct imp_softc *sc;
779 {
780 	register i;
781 	register struct mbuf *m;
782 	register struct control_leader *cp;
783 
784 #ifdef IMPINIT
785 	if (imptraceinit)
786 		log(imppri, "impnoops\n");
787 #endif
788 	for (i = 0; i < IMP_NOOPCNT; i++) {
789 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
790 			return;
791 		m->m_len = sizeof(struct control_leader);
792 		cp = mtod(m, struct control_leader *);
793 		cp->dl_format = IMP_NFF;
794 		cp->dl_link = i;
795 		cp->dl_mtype = IMPTYPE_NOOP;
796 		IF_PREPEND(&sc->imp_if.if_snd, m);
797 	}
798 	if (sc->imp_cb.ic_oactive == 0)
799 		impstart(sc);
800 }
801 
802 /*
803  * Process an ioctl request.
804  */
805 impioctl(ifp, cmd, data)
806 	register struct ifnet *ifp;
807 	int cmd;
808 	caddr_t data;
809 {
810 	struct ifaddr *ifa = (struct ifaddr *) data;
811 	int s = splimp(), error = 0;
812 #define sc	((struct imp_softc *)ifp)
813 
814 	switch (cmd) {
815 
816 	case SIOCSIFADDR:
817 		if (ifa->ifa_addr.sa_family != AF_INET) {
818 			error = EINVAL;
819 			break;
820 		}
821 		if ((ifp->if_flags & IFF_UP) == 0)
822 			impinit(ifp->if_unit);
823 		break;
824 
825 	case SIOCSIFFLAGS:
826 		if ((ifp->if_flags & IFF_UP) == 0 &&
827 		    sc->imp_state != IMPS_DOWN) {
828 			if (sc->imp_cb.ic_down &&
829 			    (*sc->imp_cb.ic_down)(sc->imp_cb.ic_hwunit)) {
830 				sc->imp_state = IMPS_DOWN;
831 				sc->imp_msgready = 0;
832 				hostreset(ifp->if_unit);
833 			}
834 		} else if (ifp->if_flags & IFF_UP && sc->imp_state == IMPS_DOWN)
835 			impinit(ifp->if_unit);
836 		break;
837 
838 	default:
839 		error = EINVAL;
840 		break;
841 	}
842 	splx(s);
843 	return (error);
844 }
845 
846 #ifdef IMPLEADERS
847 printleader(routine, ip)
848 	char *routine;
849 	register struct imp_leader *ip;
850 {
851 	printf("%s: ", routine);
852 	printbyte((char *)ip, 12);
853 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
854 		ip->il_flags);
855 	if (ip->il_mtype <= IMPTYPE_READY)
856 		printf("%s,", impleaders[ip->il_mtype]);
857 	else
858 		printf("%x,", ip->il_mtype);
859 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
860 		ntohs(ip->il_imp));
861 	if (ip->il_link == IMPLINK_IP)
862 		printf("ip,");
863 	else
864 		printf("%x,", ip->il_link);
865 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
866 }
867 
868 printbyte(cp, n)
869 	register char *cp;
870 	int n;
871 {
872 	register i, j, c;
873 
874 	for (i=0; i<n; i++) {
875 		c = *cp++;
876 		for (j=0; j<2; j++)
877 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf], 0);
878 		putchar(' ', 0);
879 	}
880 	putchar('\n', 0);
881 }
882 #endif
883 
884 /*
885  * Routine to convert from IMP Leader to InterNet Address.
886  *
887  * This procedure is necessary because IMPs may be assigned Class A, B, or C
888  * network numbers, but only have 8 bits in the leader to reflect the
889  * IMP "network number".  The strategy is to take the network number from
890  * the ifnet structure, and blend in the host-on-imp and imp-on-net numbers
891  * from the leader.
892  *
893  * There is no support for "Logical Hosts".
894  *
895  * Class A:	Net.Host.0.Imp
896  * Class B:	Net.net.Host.Imp
897  * Class C:	Net.net.net.(Host4|Imp4)
898  */
899 imp_leader_to_addr(ap, cp, ifp)
900 	struct in_addr *ap;
901 	register struct control_leader *cp;
902 	struct ifnet *ifp;
903 {
904 	register u_long final;
905 	register struct sockaddr_in *sin;
906 	int imp = ntohs(cp->dl_imp);
907 
908 	sin = (struct sockaddr_in *)(&ifp->if_addrlist->ifa_addr);
909 	final = ntohl(sin->sin_addr.s_addr);
910 
911 	if (IN_CLASSA(final)) {
912 		final &= IN_CLASSA_NET;
913 		final |= (imp & 0xFF) | ((cp->dl_host & 0xFF)<<16);
914 	} else if (IN_CLASSB(final)) {
915 		final &= IN_CLASSB_NET;
916 		final |= (imp & 0xFF) | ((cp->dl_host & 0xFF)<<8);
917 	} else {
918 		final &= IN_CLASSC_NET;
919 		final |= (imp & 0x0F) | ((cp->dl_host & 0x0F)<<4);
920 	}
921 	ap->s_addr = htonl(final);
922 }
923 
924 /*
925  * Function to take InterNet address and fill in IMP leader fields.
926  */
927 imp_addr_to_leader(imp, a)
928 	register struct control_leader *imp;
929 	u_long a;
930 {
931 	register u_long addr = ntohl(a);
932 
933 	imp->dl_network = 0;	/* !! */
934 
935 	if (IN_CLASSA(addr)) {
936 		imp->dl_host = ((addr>>16) & 0xFF);
937 		imp->dl_imp = addr & 0xFF;
938 	} else if (IN_CLASSB(addr)) {
939 		imp->dl_host = ((addr>>8) & 0xFF);
940 		imp->dl_imp = addr & 0xFF;
941 	} else {
942 		imp->dl_host = ((addr>>4) & 0xF);
943 		imp->dl_imp = addr & 0xF;
944 	}
945 	imp->dl_imp = htons(imp->dl_imp);
946 }
947 #endif
948