1*9001Sroot /*	if_imp.c	4.44	82/10/31	*/
25640Sroot 
35640Sroot #include "imp.h"
45640Sroot #if NIMP > 0
55640Sroot /*
66582Ssam  * ARPANET IMP interface driver.
75640Sroot  *
85640Sroot  * The IMP-host protocol is handled here, leaving
95640Sroot  * hardware specifics to the lower level interface driver.
105640Sroot  */
115640Sroot #include "../h/param.h"
125640Sroot #include "../h/systm.h"
135640Sroot #include "../h/mbuf.h"
145640Sroot #include "../h/pte.h"
155640Sroot #include "../h/buf.h"
165640Sroot #include "../h/protosw.h"
175640Sroot #include "../h/socket.h"
188705Sroot #include "../h/vmmac.h"
198705Sroot 
208705Sroot #include "../vax/cpu.h"
218533Sroot #include "../vax/mtpr.h"
228782Sroot #include "../vaxuba/ubareg.h"
238782Sroot #include "../vaxuba/ubavar.h"
248705Sroot 
258705Sroot #include "../net/if.h"
268705Sroot #include "../net/route.h"
278705Sroot #include "../net/netisr.h"
288408Swnj #include "../netinet/in.h"
298408Swnj #include "../netinet/in_systm.h"
308705Sroot #include "../netinet/ip.h"
318705Sroot #include "../netinet/ip_var.h"
327199Ssam /* define IMPLEADERS here to get leader printing code */
338408Swnj #include "../netimp/if_imp.h"
348408Swnj #include "../netimp/if_imphost.h"
356504Ssam #include <errno.h>
365640Sroot 
375640Sroot /*
385640Sroot  * IMP software status per interface.
395640Sroot  * (partially shared with the hardware specific module)
405640Sroot  *
415640Sroot  * Each interface is referenced by a network interface structure,
425640Sroot  * imp_if, which the routing code uses to locate the interface.
435640Sroot  * This structure contains the output queue for the interface, its
445640Sroot  * address, ...  IMP specific structures used in connecting the
455640Sroot  * IMP software modules to the hardware specific interface routines
465771Swnj  * are stored here.  The common structures are made visible to the
475771Swnj  * interface driver by passing a pointer to the hardware routine
485771Swnj  * at "attach" time.
495640Sroot  *
505640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
515640Sroot  */
525640Sroot struct imp_softc {
535640Sroot 	struct	ifnet imp_if;		/* network visible interface */
545640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
555640Sroot 	u_char	imp_state;		/* current state of IMP */
565640Sroot 	char	imp_dropcnt;		/* used during initialization */
575640Sroot } imp_softc[NIMP];
585640Sroot 
595640Sroot /*
605640Sroot  * Messages from IMP regarding why
615640Sroot  * it's going down.
625640Sroot  */
635924Sroot static char *impmessage[] = {
645640Sroot 	"in 30 seconds",
655640Sroot 	"for hardware PM",
665640Sroot 	"to reload software",
675640Sroot 	"for emergency reset"
685640Sroot };
695640Sroot 
705771Swnj int	impdown(), impinit(), impoutput();
715771Swnj 
725640Sroot /*
735640Sroot  * IMP attach routine.  Called from hardware device attach routine
745640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
755640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
765640Sroot  * structures.  This is then used by the device's attach routine
775640Sroot  * set up its back pointers.
785640Sroot  */
798815Sroot impattach(ui, reset)
805640Sroot 	struct uba_device *ui;
818815Sroot 	int (*reset)();
825640Sroot {
835640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
845640Sroot 	register struct ifnet *ifp = &sc->imp_if;
856336Ssam 	struct sockaddr_in *sin;
865640Sroot 
875640Sroot 	/* UNIT COULD BE AMBIGUOUS */
885640Sroot 	ifp->if_unit = ui->ui_unit;
895640Sroot 	ifp->if_name = "imp";
906271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
915640Sroot 	ifp->if_net = ui->ui_flags;
92*9001Sroot 	ifp->if_reset = reset;
935989Sroot 	/* the host and imp fields will be filled in by the imp */
946336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
956336Ssam 	sin->sin_family = AF_INET;
966336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
975771Swnj 	ifp->if_init = impinit;
985771Swnj 	ifp->if_output = impoutput;
995771Swnj 	/* reset is handled at the hardware level */
1005640Sroot 	if_attach(ifp);
1015640Sroot 	return ((int)&sc->imp_if);
1025640Sroot }
1035640Sroot 
1045640Sroot /*
1055640Sroot  * IMP initialization routine: call hardware module to
1065640Sroot  * setup UNIBUS resources, init state and get ready for
1075640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1085640Sroot  */
1095640Sroot impinit(unit)
1105640Sroot 	int unit;
1115640Sroot {
1126500Ssam 	int s = splimp();
1135640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1145640Sroot 
1155771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1165771Swnj 		sc->imp_state = IMPS_DOWN;
1176336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1186500Ssam 		splx(s);
1195771Swnj 		return;
1205771Swnj 	}
1215640Sroot 	sc->imp_state = IMPS_INIT;
1225771Swnj 	impnoops(sc);
1236500Ssam 	splx(s);
1245640Sroot }
1255640Sroot 
1265640Sroot struct sockproto impproto = { PF_IMPLINK };
1275645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1285645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1297199Ssam #ifdef IMPLEADERS
1307170Ssam int	impprintfs = 0;
1317199Ssam #endif
1325640Sroot 
1335640Sroot /*
1345640Sroot  * ARPAnet 1822 input routine.
1355640Sroot  * Called from hardware input interrupt routine to handle 1822
1365640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1375640Sroot  * passed to higher level protocol processors on the basis
1385640Sroot  * of link number.  Other type messages (control) are handled here.
1395640Sroot  */
1405771Swnj impinput(unit, m)
1415640Sroot 	int unit;
1425771Swnj 	register struct mbuf *m;
1435640Sroot {
1445640Sroot 	register struct imp_leader *ip;
1455640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1465640Sroot 	register struct host *hp;
1475640Sroot 	register struct ifqueue *inq;
1485771Swnj 	struct control_leader *cp;
1495640Sroot 	struct in_addr addr;
1505924Sroot 	struct mbuf *next;
1516336Ssam 	struct sockaddr_in *sin;
1525640Sroot 
1536257Sroot 	/* verify leader length. */
1545771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1555771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1565771Swnj 		return;
1575771Swnj 	cp = mtod(m, struct control_leader *);
1585771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1595771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1605771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1615771Swnj 			return;
1625640Sroot 	ip = mtod(m, struct imp_leader *);
1637199Ssam #ifdef IMPLEADERS
1647170Ssam 	if (impprintfs)
1657170Ssam 		printleader("impinput", ip);
1667199Ssam #endif
1675640Sroot 
1686257Sroot 	/* check leader type */
1695771Swnj 	if (ip->il_format != IMP_NFF) {
1705771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1715640Sroot 		goto drop;
1725771Swnj 	}
1735640Sroot 
1746588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1755859Sroot #ifdef notdef
1765771Swnj 		addr.s_net = ip->il_network;
1775859Sroot #else
1786588Ssam 		addr.s_net = sc->imp_if.if_net;
1795859Sroot #endif
1805771Swnj 		addr.s_imp = ip->il_imp;
1815771Swnj 		addr.s_host = ip->il_host;
1825640Sroot 	}
1835640Sroot 	switch (ip->il_mtype) {
1845640Sroot 
1855640Sroot 	case IMPTYPE_DATA:
1865640Sroot 		break;
1875640Sroot 
1885640Sroot 	/*
1895640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1905640Sroot 	 */
1915640Sroot 	case IMPTYPE_BADLEADER:
1925647Ssam 		/*
1935647Ssam 		 * According to 1822 document, this message
1945647Ssam 		 * will be generated in response to the
1955647Ssam 		 * first noop sent to the IMP after
1965647Ssam 		 * the host resets the IMP interface.
1975647Ssam 		 */
1985771Swnj 		if (sc->imp_state != IMPS_INIT) {
1995924Sroot 			impmsg(sc, "leader error");
2006582Ssam 			hostreset(sc->imp_if.if_net);
2015647Ssam 			impnoops(sc);
2025647Ssam 		}
2036582Ssam 		goto drop;
2045640Sroot 
2055640Sroot 	/*
2065640Sroot 	 * IMP going down.  Print message, and if not immediate,
2075640Sroot 	 * set off a timer to insure things will be reset at the
2085640Sroot 	 * appropriate time.
2095640Sroot 	 */
2105640Sroot 	case IMPTYPE_DOWN:
2116599Ssam 		if (sc->imp_state < IMPS_INIT)
2126598Ssam 			goto drop;
2135640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2145640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2156160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2165640Sroot 		}
2176160Ssam 		impmsg(sc, "going down %s",
2186160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2196582Ssam 		goto drop;
2205640Sroot 
2215640Sroot 	/*
2225640Sroot 	 * A NOP usually seen during the initialization sequence.
2235640Sroot 	 * Compare the local address with that in the message.
2245640Sroot 	 * Reset the local address notion if it doesn't match.
2255640Sroot 	 */
2266336Ssam 	case IMPTYPE_NOOP:
2275647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2285647Ssam 			sc->imp_state = IMPS_INIT;
2295647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2305647Ssam 		}
2316500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2326271Sroot 			goto drop;
2336500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2346500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2356500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2366500Ssam 			sc->imp_if.if_host[0] =
2376500Ssam 				sin->sin_addr.s_host = ip->il_host;
2386500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2396500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2406500Ssam 				ntohs(ip->il_imp));
2416500Ssam 		}
2425771Swnj 		sc->imp_state = IMPS_UP;
2436336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2447201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2456271Sroot 		goto drop;
2465640Sroot 
2475640Sroot 	/*
2486582Ssam 	 * RFNM or INCOMPLETE message, send next
2496582Ssam 	 * message on the q.  We could pass incomplete's
2506582Ssam 	 * up to the next level, but this currently isn't
2516582Ssam 	 * needed.
2525640Sroot 	 */
2535640Sroot 	case IMPTYPE_RFNM:
2545640Sroot 	case IMPTYPE_INCOMPLETE:
2556588Ssam 		if (hp = hostlookup(addr)) {
2566588Ssam 			if (hp->h_rfnm == 0)
2576588Ssam 				hp->h_flags &= ~HF_INUSE;
2586588Ssam 			else if (next = hostdeque(hp))
2596588Ssam 				(void) impsnd(&sc->imp_if, next);
2606588Ssam 		}
2616271Sroot 		goto drop;
2625640Sroot 
2635640Sroot 	/*
2645640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2655640Sroot 	 * awaiting transmission and release the host structure.
2665640Sroot 	 */
2675640Sroot 	case IMPTYPE_HOSTDEAD:
2686588Ssam 	case IMPTYPE_HOSTUNREACH: {
2696588Ssam 		int s = splnet();
2708705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
2718705Sroot 		    hostlookup(addr));
2726588Ssam 		splx(s);
2735859Sroot 		goto rawlinkin;
2746588Ssam 	}
2755640Sroot 
2765640Sroot 	/*
2775640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2785640Sroot 	 * noops to the IMP to clear the interface.
2795640Sroot 	 */
2806588Ssam 	case IMPTYPE_BADDATA: {
2816588Ssam 		int s;
2826588Ssam 
2835924Sroot 		impmsg(sc, "data error");
2846588Ssam 		s = splnet();
2856588Ssam 		if (hp = hostlookup(addr))
2865640Sroot 			hp->h_rfnm = 0;
2876588Ssam 		splx(s);
2885640Sroot 		impnoops(sc);
2896582Ssam 		goto drop;
2906588Ssam 	}
2915640Sroot 
2925640Sroot 	/*
2935647Ssam 	 * Interface reset.
2945640Sroot 	 */
2955640Sroot 	case IMPTYPE_RESET:
2965924Sroot 		impmsg(sc, "interface reset");
2975647Ssam 		impnoops(sc);
2986582Ssam 		goto drop;
2995640Sroot 
3005640Sroot 	default:
3015640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3026582Ssam 		goto drop;
3035640Sroot 	}
3045640Sroot 
3055640Sroot 	/*
3066257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3076257Sroot 	 * protocol routine (running at software interrupt).
3086257Sroot 	 * If this isn't a raw interface, advance pointer
3096257Sroot 	 * into mbuf past leader.
3105640Sroot 	 */
3115640Sroot 	switch (ip->il_link) {
3125640Sroot 
3135640Sroot #ifdef INET
3145640Sroot 	case IMPLINK_IP:
3155640Sroot 		m->m_len -= sizeof(struct imp_leader);
3165640Sroot 		m->m_off += sizeof(struct imp_leader);
3176261Swnj 		schednetisr(NETISR_IP);
3185640Sroot 		inq = &ipintrq;
3195640Sroot 		break;
3205640Sroot #endif
3215640Sroot 
3225640Sroot 	default:
3235868Sroot 	rawlinkin:
3245640Sroot 		impproto.sp_protocol = ip->il_link;
3256336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3266336Ssam 		impdst.sin_addr = sin->sin_addr;;
3275645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3285645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3295645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3306527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3316527Ssam 		  (struct sockaddr *)&impdst);
3325640Sroot 		return;
3335640Sroot 	}
3346208Swnj 	if (IF_QFULL(inq)) {
3356208Swnj 		IF_DROP(inq);
3366208Swnj 		goto drop;
3376208Swnj 	}
3385640Sroot 	IF_ENQUEUE(inq, m);
3395640Sroot 	return;
3405640Sroot 
3415640Sroot drop:
3425640Sroot 	m_freem(m);
3435640Sroot }
3445640Sroot 
3455647Ssam /*
3465647Ssam  * Bring the IMP down after notification.
3475647Ssam  */
3485647Ssam impdown(sc)
3495647Ssam 	struct imp_softc *sc;
3505647Ssam {
3516208Swnj 
3525647Ssam 	sc->imp_state = IMPS_DOWN;
3535924Sroot 	impmsg(sc, "marked down");
3546588Ssam 	hostreset(sc->imp_if.if_net);
3556582Ssam 	if_down(&sc->imp_if);
3565647Ssam }
3575647Ssam 
3585640Sroot /*VARARGS*/
3595924Sroot impmsg(sc, fmt, a1, a2)
3605640Sroot 	struct imp_softc *sc;
3615640Sroot 	char *fmt;
3626160Ssam 	u_int a1;
3635640Sroot {
3646208Swnj 
3655640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3665640Sroot 	printf(fmt, a1, a2);
3675640Sroot 	printf("\n");
3685640Sroot }
3695640Sroot 
3705640Sroot /*
3716582Ssam  * Process an IMP "error" message, passing this
3726582Ssam  * up to the higher level protocol.
3736582Ssam  */
3746582Ssam impnotify(what, cp, hp)
3756582Ssam 	int what;
3766582Ssam 	struct control_leader *cp;
3776582Ssam 	struct host *hp;
3786582Ssam {
3796582Ssam 	struct in_addr in;
3806582Ssam 
3816582Ssam #ifdef notdef
3826582Ssam 	in.s_net = cp->dl_network;
3836582Ssam #else
3846588Ssam 	in.s_net = 10;			/* XXX */
3856582Ssam #endif
3866582Ssam 	in.s_host = cp->dl_host;
3876582Ssam 	in.s_imp = cp->dl_imp;
3886582Ssam 	if (cp->dl_link != IMPLINK_IP)
3896582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3906582Ssam 	else
3916582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3926588Ssam 	if (hp) {
3936588Ssam 		hp->h_flags |= (1 << what);
3946582Ssam 		hostfree(hp);
3956588Ssam 	}
3966582Ssam }
3976582Ssam 
3986582Ssam /*
3995640Sroot  * ARPAnet 1822 output routine.
4005640Sroot  * Called from higher level protocol routines to set up messages for
4015640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4025640Sroot  * enqueue the message for this IMP's hardware driver.
4035640Sroot  */
4046336Ssam impoutput(ifp, m0, dst)
4055640Sroot 	register struct ifnet *ifp;
4065640Sroot 	struct mbuf *m0;
4076336Ssam 	struct sockaddr *dst;
4085640Sroot {
4095640Sroot 	register struct imp_leader *imp;
4105640Sroot 	register struct mbuf *m = m0;
4118782Sroot 	int dhost, dimp, dlink, len, dnet;
4126504Ssam 	int error = 0;
4135640Sroot 
4145640Sroot 	/*
4155640Sroot 	 * Don't even try if the IMP is unavailable.
4165640Sroot 	 */
4176504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4186504Ssam 		error = ENETDOWN;
4195647Ssam 		goto drop;
4206504Ssam 	}
4215640Sroot 
4226336Ssam 	switch (dst->sa_family) {
4235640Sroot 
4245640Sroot #ifdef INET
4256336Ssam 	case AF_INET: {
4266336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4276336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4285640Sroot 
4296336Ssam 		dhost = sin->sin_addr.s_host;
4306336Ssam 		dimp = sin->sin_addr.s_impno;
4315640Sroot 		dlink = IMPLINK_IP;
4326244Sroot 		dnet = 0;
4336160Ssam 		len = ntohs((u_short)ip->ip_len);
4345640Sroot 		break;
4355640Sroot 	}
4365640Sroot #endif
4376336Ssam 	case AF_IMPLINK:
4385640Sroot 		goto leaderexists;
4395640Sroot 
4405640Sroot 	default:
4416336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4426336Ssam 			dst->sa_family);
4436504Ssam 		error = EAFNOSUPPORT;
4445647Ssam 		goto drop;
4455640Sroot 	}
4465640Sroot 
4475640Sroot 	/*
4485640Sroot 	 * Add IMP leader.  If there's not enough space in the
4495640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4505640Sroot 	 * drop this sucker.
4515640Sroot 	 */
4525640Sroot 	if (m->m_off > MMAXOFF ||
4535640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4545640Sroot 		m = m_get(M_DONTWAIT);
4556504Ssam 		if (m == 0) {
4566504Ssam 			error = ENOBUFS;
4575647Ssam 			goto drop;
4586504Ssam 		}
4595640Sroot 		m->m_next = m0;
4605640Sroot 		m->m_len = sizeof(struct imp_leader);
4615640Sroot 	} else {
4625640Sroot 		m->m_off -= sizeof(struct imp_leader);
4635640Sroot 		m->m_len += sizeof(struct imp_leader);
4645640Sroot 	}
4655640Sroot 	imp = mtod(m, struct imp_leader *);
4665640Sroot 	imp->il_format = IMP_NFF;
4675859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4686244Sroot 	imp->il_network = dnet;
4695640Sroot 	imp->il_host = dhost;
4706244Sroot 	imp->il_imp = htons((u_short)dimp);
4716160Ssam 	imp->il_length =
4726160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4735640Sroot 	imp->il_link = dlink;
4745859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4755640Sroot 
4765640Sroot leaderexists:
4775640Sroot 	return (impsnd(ifp, m));
4785647Ssam drop:
4795647Ssam 	m_freem(m0);
4806504Ssam 	return (error);
4815640Sroot }
4825640Sroot 
4835640Sroot /*
4845640Sroot  * Put a message on an interface's output queue.
4855640Sroot  * Perform RFNM counting: no more than 8 message may be
4865640Sroot  * in flight to any one host.
4875640Sroot  */
4885640Sroot impsnd(ifp, m)
4895640Sroot 	struct ifnet *ifp;
4905640Sroot 	struct mbuf *m;
4915640Sroot {
4925640Sroot 	register struct imp_leader *ip;
4935640Sroot 	register struct host *hp;
4945640Sroot 	struct impcb *icp;
4956588Ssam 	int s, error;
4965640Sroot 
4975640Sroot 	ip = mtod(m, struct imp_leader *);
4985640Sroot 
4995640Sroot 	/*
5005640Sroot 	 * Do RFNM counting for data messages
5015640Sroot 	 * (no more than 8 outstanding to any host)
5025640Sroot 	 */
5036588Ssam 	s = splimp();
5045640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5055640Sroot 		struct in_addr addr;
5065640Sroot 
5075859Sroot #ifdef notdef
5085771Swnj                 addr.s_net = ip->il_network;
5095859Sroot #else
5106588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5115859Sroot #endif
5125640Sroot                 addr.s_host = ip->il_host;
5135640Sroot                 addr.s_imp = ip->il_imp;
5145771Swnj 		if ((hp = hostlookup(addr)) == 0)
5155771Swnj 			hp = hostenter(addr);
5166588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5176607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5186588Ssam 			hp->h_timer = HOSTTIMER;
5196588Ssam 			hp->h_flags &= ~HF_INUSE;
5206588Ssam 			goto bad;
5216588Ssam 		}
5225640Sroot 
5235640Sroot 		/*
5245647Ssam 		 * If IMP would block, queue until RFNM
5255640Sroot 		 */
5265640Sroot 		if (hp) {
5275640Sroot 			if (hp->h_rfnm < 8) {
5285640Sroot 				hp->h_rfnm++;
5295640Sroot 				goto enque;
5305640Sroot 			}
5316095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5326095Swnj 				HOST_ENQUE(hp, m);
5336095Swnj 				goto start;
5346095Swnj 			}
5355640Sroot 		}
5366588Ssam 		error = ENOBUFS;
5376588Ssam 		goto bad;
5385640Sroot 	}
5395640Sroot enque:
5406208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5416208Swnj 		IF_DROP(&ifp->if_snd);
5426588Ssam 		error = ENOBUFS;
5436588Ssam bad:
5446208Swnj 		m_freem(m);
5456588Ssam 		splx(s);
5466588Ssam 		return (error);
5476208Swnj 	}
5485640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5496095Swnj start:
5505640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5515640Sroot 	if (icp->ic_oactive == 0)
5525640Sroot 		(*icp->ic_start)(ifp->if_unit);
5536630Ssam 	splx(s);
5546504Ssam 	return (0);
5555640Sroot }
5565640Sroot 
5575640Sroot /*
5585640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5595640Sroot  * Part of host-IMP initialization procedure.
5605640Sroot  * (Should return success/failure, but noone knows
5615640Sroot  * what to do with this, so why bother?)
5626818Ssam  * This routine is always called at splimp, so we don't
5636818Ssam  * protect the call to IF_PREPEND.
5645640Sroot  */
5655640Sroot impnoops(sc)
5665640Sroot 	register struct imp_softc *sc;
5675640Sroot {
5685640Sroot 	register i;
5695640Sroot 	register struct mbuf *m;
5705771Swnj 	register struct control_leader *cp;
5715640Sroot 
5725640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5735771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5745640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5755640Sroot 			return;
5765771Swnj 		m->m_len = sizeof(struct control_leader);
5775771Swnj 		cp = mtod(m, struct control_leader *);
5785771Swnj 		cp->dl_format = IMP_NFF;
5795771Swnj                 cp->dl_link = i;
5805771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5815640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5825640Sroot 	}
5835640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5845640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5855640Sroot }
5867170Ssam 
5877170Ssam #ifdef IMPLEADERS
5887170Ssam printleader(routine, ip)
5897170Ssam 	char *routine;
5907170Ssam 	register struct imp_leader *ip;
5917170Ssam {
5927170Ssam 	printf("%s: ", routine);
5937170Ssam 	printbyte((char *)ip, 12);
5947170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5957170Ssam 		ip->il_flags);
5967170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
5977170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
5987170Ssam 	else
5997170Ssam 		printf("%x,", ip->il_mtype);
6007170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6017170Ssam 		ntohs(ip->il_imp));
6027170Ssam 	if (ip->il_link == IMPLINK_IP)
6037170Ssam 		printf("ip,");
6047170Ssam 	else
6057170Ssam 		printf("%x,", ip->il_link);
6067170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6077170Ssam }
6087170Ssam 
6097170Ssam printbyte(cp, n)
6107170Ssam 	register char *cp;
6117170Ssam 	int n;
6127170Ssam {
6137170Ssam 	register i, j, c;
6147170Ssam 
6157170Ssam 	for (i=0; i<n; i++) {
6167170Ssam 		c = *cp++;
6177170Ssam 		for (j=0; j<2; j++)
6187170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6197170Ssam 		putchar(' ');
6207170Ssam 	}
6217170Ssam 	putchar('\n');
6227170Ssam }
6235640Sroot #endif
6247170Ssam #endif
625