1*7199Ssam /*	if_imp.c	4.35	82/06/15	*/
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"
185640Sroot #include "../h/ubareg.h"
195640Sroot #include "../h/ubavar.h"
205640Sroot #include "../h/cpu.h"
215640Sroot #include "../h/mtpr.h"
225640Sroot #include "../h/vmmac.h"
235640Sroot #include "../net/in.h"
245640Sroot #include "../net/in_systm.h"
255640Sroot #include "../net/if.h"
26*7199Ssam /* define IMPLEADERS here to get leader printing code */
275640Sroot #include "../net/if_imp.h"
285859Sroot #include "../net/if_imphost.h"
295640Sroot #include "../net/ip.h"
305640Sroot #include "../net/ip_var.h"
316365Ssam #include "../net/route.h"
326504Ssam #include <errno.h>
335640Sroot 
345640Sroot /*
355640Sroot  * IMP software status per interface.
365640Sroot  * (partially shared with the hardware specific module)
375640Sroot  *
385640Sroot  * Each interface is referenced by a network interface structure,
395640Sroot  * imp_if, which the routing code uses to locate the interface.
405640Sroot  * This structure contains the output queue for the interface, its
415640Sroot  * address, ...  IMP specific structures used in connecting the
425640Sroot  * IMP software modules to the hardware specific interface routines
435771Swnj  * are stored here.  The common structures are made visible to the
445771Swnj  * interface driver by passing a pointer to the hardware routine
455771Swnj  * at "attach" time.
465640Sroot  *
475640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
485640Sroot  */
495640Sroot struct imp_softc {
505640Sroot 	struct	ifnet imp_if;		/* network visible interface */
515640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
525640Sroot 	u_char	imp_state;		/* current state of IMP */
535640Sroot 	char	imp_dropcnt;		/* used during initialization */
545640Sroot } imp_softc[NIMP];
555640Sroot 
565640Sroot /*
575640Sroot  * Messages from IMP regarding why
585640Sroot  * it's going down.
595640Sroot  */
605924Sroot static char *impmessage[] = {
615640Sroot 	"in 30 seconds",
625640Sroot 	"for hardware PM",
635640Sroot 	"to reload software",
645640Sroot 	"for emergency reset"
655640Sroot };
665640Sroot 
675771Swnj int	impdown(), impinit(), impoutput();
685771Swnj 
695640Sroot /*
705640Sroot  * IMP attach routine.  Called from hardware device attach routine
715640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
725640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
735640Sroot  * structures.  This is then used by the device's attach routine
745640Sroot  * set up its back pointers.
755640Sroot  */
765640Sroot impattach(ui)
775640Sroot 	struct uba_device *ui;
785640Sroot {
795640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
805640Sroot 	register struct ifnet *ifp = &sc->imp_if;
816336Ssam 	struct sockaddr_in *sin;
825640Sroot 
835640Sroot COUNT(IMPATTACH);
845640Sroot 	/* UNIT COULD BE AMBIGUOUS */
855640Sroot 	ifp->if_unit = ui->ui_unit;
865640Sroot 	ifp->if_name = "imp";
876271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
885640Sroot 	ifp->if_net = ui->ui_flags;
895989Sroot 	/* the host and imp fields will be filled in by the imp */
906336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
916336Ssam 	sin->sin_family = AF_INET;
926336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
935771Swnj 	ifp->if_init = impinit;
945771Swnj 	ifp->if_output = impoutput;
955771Swnj 	/* reset is handled at the hardware level */
965640Sroot 	if_attach(ifp);
975640Sroot 	return ((int)&sc->imp_if);
985640Sroot }
995640Sroot 
1005640Sroot /*
1015640Sroot  * IMP initialization routine: call hardware module to
1025640Sroot  * setup UNIBUS resources, init state and get ready for
1035640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1045640Sroot  */
1055640Sroot impinit(unit)
1065640Sroot 	int unit;
1075640Sroot {
1086500Ssam 	int s = splimp();
1095640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1105640Sroot 
1116500Ssam COUNT(IMPINIT);
1125771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1135771Swnj 		sc->imp_state = IMPS_DOWN;
1146336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1156500Ssam 		splx(s);
1165771Swnj 		return;
1175771Swnj 	}
1185640Sroot 	sc->imp_state = IMPS_INIT;
1195771Swnj 	impnoops(sc);
1207153Swnj 	if_rtinit(&sc->imp_if, RTF_UP);
1216500Ssam 	splx(s);
1225640Sroot }
1235640Sroot 
1245640Sroot struct sockproto impproto = { PF_IMPLINK };
1255645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1265645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
127*7199Ssam #ifdef IMPLEADERS
1287170Ssam int	impprintfs = 0;
129*7199Ssam #endif
1305640Sroot 
1315640Sroot /*
1325640Sroot  * ARPAnet 1822 input routine.
1335640Sroot  * Called from hardware input interrupt routine to handle 1822
1345640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1355640Sroot  * passed to higher level protocol processors on the basis
1365640Sroot  * of link number.  Other type messages (control) are handled here.
1375640Sroot  */
1385771Swnj impinput(unit, m)
1395640Sroot 	int unit;
1405771Swnj 	register struct mbuf *m;
1415640Sroot {
1425640Sroot 	register struct imp_leader *ip;
1435640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1445640Sroot 	register struct host *hp;
1455640Sroot 	register struct ifqueue *inq;
1465771Swnj 	struct control_leader *cp;
1475640Sroot 	struct in_addr addr;
1485924Sroot 	struct mbuf *next;
1496336Ssam 	struct sockaddr_in *sin;
1505640Sroot 
1515932Sroot COUNT(IMPINPUT);
1526257Sroot 	/* verify leader length. */
1535771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1545771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1555771Swnj 		return;
1565771Swnj 	cp = mtod(m, struct control_leader *);
1575771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1585771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1595771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1605771Swnj 			return;
1615640Sroot 	ip = mtod(m, struct imp_leader *);
162*7199Ssam #ifdef IMPLEADERS
1637170Ssam 	if (impprintfs)
1647170Ssam 		printleader("impinput", ip);
165*7199Ssam #endif
1665640Sroot 
1676257Sroot 	/* check leader type */
1685771Swnj 	if (ip->il_format != IMP_NFF) {
1695771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1705640Sroot 		goto drop;
1715771Swnj 	}
1725640Sroot 
1736588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1745859Sroot #ifdef notdef
1755771Swnj 		addr.s_net = ip->il_network;
1765859Sroot #else
1776588Ssam 		addr.s_net = sc->imp_if.if_net;
1785859Sroot #endif
1795771Swnj 		addr.s_imp = ip->il_imp;
1805771Swnj 		addr.s_host = ip->il_host;
1815640Sroot 	}
1825640Sroot 	switch (ip->il_mtype) {
1835640Sroot 
1845640Sroot 	case IMPTYPE_DATA:
1855640Sroot 		break;
1865640Sroot 
1875640Sroot 	/*
1885640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1895640Sroot 	 */
1905640Sroot 	case IMPTYPE_BADLEADER:
1915647Ssam 		/*
1925647Ssam 		 * According to 1822 document, this message
1935647Ssam 		 * will be generated in response to the
1945647Ssam 		 * first noop sent to the IMP after
1955647Ssam 		 * the host resets the IMP interface.
1965647Ssam 		 */
1975771Swnj 		if (sc->imp_state != IMPS_INIT) {
1985924Sroot 			impmsg(sc, "leader error");
1996582Ssam 			hostreset(sc->imp_if.if_net);
2005647Ssam 			impnoops(sc);
2015647Ssam 		}
2026582Ssam 		goto drop;
2035640Sroot 
2045640Sroot 	/*
2055640Sroot 	 * IMP going down.  Print message, and if not immediate,
2065640Sroot 	 * set off a timer to insure things will be reset at the
2075640Sroot 	 * appropriate time.
2085640Sroot 	 */
2095640Sroot 	case IMPTYPE_DOWN:
2106599Ssam 		if (sc->imp_state < IMPS_INIT)
2116598Ssam 			goto drop;
2125640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2135640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2146160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2155640Sroot 		}
2166160Ssam 		impmsg(sc, "going down %s",
2176160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2186582Ssam 		goto drop;
2195640Sroot 
2205640Sroot 	/*
2215640Sroot 	 * A NOP usually seen during the initialization sequence.
2225640Sroot 	 * Compare the local address with that in the message.
2235640Sroot 	 * Reset the local address notion if it doesn't match.
2245640Sroot 	 */
2256336Ssam 	case IMPTYPE_NOOP:
2265647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2275647Ssam 			sc->imp_state = IMPS_INIT;
2285647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2295647Ssam 		}
2306500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2316271Sroot 			goto drop;
2326500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2336500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2346500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2356500Ssam 			sc->imp_if.if_host[0] =
2366500Ssam 				sin->sin_addr.s_host = ip->il_host;
2376500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2386500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2396500Ssam 				ntohs(ip->il_imp));
2406500Ssam 		}
2415771Swnj 		sc->imp_state = IMPS_UP;
2426336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2436271Sroot 		goto drop;
2445640Sroot 
2455640Sroot 	/*
2466582Ssam 	 * RFNM or INCOMPLETE message, send next
2476582Ssam 	 * message on the q.  We could pass incomplete's
2486582Ssam 	 * up to the next level, but this currently isn't
2496582Ssam 	 * needed.
2505640Sroot 	 */
2515640Sroot 	case IMPTYPE_RFNM:
2525640Sroot 	case IMPTYPE_INCOMPLETE:
2536588Ssam 		if (hp = hostlookup(addr)) {
2546588Ssam 			if (hp->h_rfnm == 0)
2556588Ssam 				hp->h_flags &= ~HF_INUSE;
2566588Ssam 			else if (next = hostdeque(hp))
2576588Ssam 				(void) impsnd(&sc->imp_if, next);
2586588Ssam 		}
2596271Sroot 		goto drop;
2605640Sroot 
2615640Sroot 	/*
2625640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2635640Sroot 	 * awaiting transmission and release the host structure.
2645640Sroot 	 */
2655640Sroot 	case IMPTYPE_HOSTDEAD:
2666588Ssam 	case IMPTYPE_HOSTUNREACH: {
2676588Ssam 		int s = splnet();
2686588Ssam 		impnotify(ip->il_mtype, ip, hostlookup(addr));
2696588Ssam 		splx(s);
2705859Sroot 		goto rawlinkin;
2716588Ssam 	}
2725640Sroot 
2735640Sroot 	/*
2745640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2755640Sroot 	 * noops to the IMP to clear the interface.
2765640Sroot 	 */
2776588Ssam 	case IMPTYPE_BADDATA: {
2786588Ssam 		int s;
2796588Ssam 
2805924Sroot 		impmsg(sc, "data error");
2816588Ssam 		s = splnet();
2826588Ssam 		if (hp = hostlookup(addr))
2835640Sroot 			hp->h_rfnm = 0;
2846588Ssam 		splx(s);
2855640Sroot 		impnoops(sc);
2866582Ssam 		goto drop;
2876588Ssam 	}
2885640Sroot 
2895640Sroot 	/*
2905647Ssam 	 * Interface reset.
2915640Sroot 	 */
2925640Sroot 	case IMPTYPE_RESET:
2935924Sroot 		impmsg(sc, "interface reset");
2945647Ssam 		impnoops(sc);
2956582Ssam 		goto drop;
2965640Sroot 
2975640Sroot 	default:
2985640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
2996582Ssam 		goto drop;
3005640Sroot 	}
3015640Sroot 
3025640Sroot 	/*
3036257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3046257Sroot 	 * protocol routine (running at software interrupt).
3056257Sroot 	 * If this isn't a raw interface, advance pointer
3066257Sroot 	 * into mbuf past leader.
3075640Sroot 	 */
3085640Sroot 	switch (ip->il_link) {
3095640Sroot 
3105640Sroot #ifdef INET
3115640Sroot 	case IMPLINK_IP:
3125640Sroot 		m->m_len -= sizeof(struct imp_leader);
3135640Sroot 		m->m_off += sizeof(struct imp_leader);
3146261Swnj 		schednetisr(NETISR_IP);
3155640Sroot 		inq = &ipintrq;
3165640Sroot 		break;
3175640Sroot #endif
3185640Sroot 
3195640Sroot 	default:
3205868Sroot 	rawlinkin:
3215640Sroot 		impproto.sp_protocol = ip->il_link;
3226336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3236336Ssam 		impdst.sin_addr = sin->sin_addr;;
3245645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3255645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3265645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3276527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3286527Ssam 		  (struct sockaddr *)&impdst);
3295640Sroot 		return;
3305640Sroot 	}
3316208Swnj 	if (IF_QFULL(inq)) {
3326208Swnj 		IF_DROP(inq);
3336208Swnj 		goto drop;
3346208Swnj 	}
3355640Sroot 	IF_ENQUEUE(inq, m);
3365640Sroot 	return;
3375640Sroot 
3385640Sroot drop:
3395640Sroot 	m_freem(m);
3405640Sroot }
3415640Sroot 
3425647Ssam /*
3435647Ssam  * Bring the IMP down after notification.
3445647Ssam  */
3455647Ssam impdown(sc)
3465647Ssam 	struct imp_softc *sc;
3475647Ssam {
3486208Swnj 
3496588Ssam COUNT(IMPDOWN);
3505647Ssam 	sc->imp_state = IMPS_DOWN;
3515924Sroot 	impmsg(sc, "marked down");
3526588Ssam 	hostreset(sc->imp_if.if_net);
3536582Ssam 	if_down(&sc->imp_if);
3545647Ssam }
3555647Ssam 
3565640Sroot /*VARARGS*/
3575924Sroot impmsg(sc, fmt, a1, a2)
3585640Sroot 	struct imp_softc *sc;
3595640Sroot 	char *fmt;
3606160Ssam 	u_int a1;
3615640Sroot {
3626208Swnj 
3636588Ssam COUNT(IMPMSG);
3645640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3655640Sroot 	printf(fmt, a1, a2);
3665640Sroot 	printf("\n");
3675640Sroot }
3685640Sroot 
3695640Sroot /*
3706582Ssam  * Process an IMP "error" message, passing this
3716582Ssam  * up to the higher level protocol.
3726582Ssam  */
3736582Ssam impnotify(what, cp, hp)
3746582Ssam 	int what;
3756582Ssam 	struct control_leader *cp;
3766582Ssam 	struct host *hp;
3776582Ssam {
3786582Ssam 	struct in_addr in;
3796582Ssam 
3806588Ssam COUNT(IMPNOTIFY);
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;
4116244Sroot 	int x, dhost, dimp, dlink, len, dnet;
4126504Ssam 	int error = 0;
4135640Sroot 
4145771Swnj COUNT(IMPOUTPUT);
4155640Sroot 	/*
4165640Sroot 	 * Don't even try if the IMP is unavailable.
4175640Sroot 	 */
4186504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4196504Ssam 		error = ENETDOWN;
4205647Ssam 		goto drop;
4216504Ssam 	}
4225640Sroot 
4236336Ssam 	switch (dst->sa_family) {
4245640Sroot 
4255640Sroot #ifdef INET
4266336Ssam 	case AF_INET: {
4276336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4286336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4295640Sroot 
4306336Ssam 		dhost = sin->sin_addr.s_host;
4316336Ssam 		dimp = sin->sin_addr.s_impno;
4325640Sroot 		dlink = IMPLINK_IP;
4336244Sroot 		dnet = 0;
4346160Ssam 		len = ntohs((u_short)ip->ip_len);
4355640Sroot 		break;
4365640Sroot 	}
4375640Sroot #endif
4386336Ssam 	case AF_IMPLINK:
4395640Sroot 		goto leaderexists;
4405640Sroot 
4415640Sroot 	default:
4426336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4436336Ssam 			dst->sa_family);
4446504Ssam 		error = EAFNOSUPPORT;
4455647Ssam 		goto drop;
4465640Sroot 	}
4475640Sroot 
4485640Sroot 	/*
4495640Sroot 	 * Add IMP leader.  If there's not enough space in the
4505640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4515640Sroot 	 * drop this sucker.
4525640Sroot 	 */
4535640Sroot 	if (m->m_off > MMAXOFF ||
4545640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4555640Sroot 		m = m_get(M_DONTWAIT);
4566504Ssam 		if (m == 0) {
4576504Ssam 			error = ENOBUFS;
4585647Ssam 			goto drop;
4596504Ssam 		}
4605640Sroot 		m->m_next = m0;
4615640Sroot 		m->m_off = MMINOFF;
4625640Sroot 		m->m_len = sizeof(struct imp_leader);
4635640Sroot 	} else {
4645640Sroot 		m->m_off -= sizeof(struct imp_leader);
4655640Sroot 		m->m_len += sizeof(struct imp_leader);
4665640Sroot 	}
4675640Sroot 	imp = mtod(m, struct imp_leader *);
4685640Sroot 	imp->il_format = IMP_NFF;
4695859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4706244Sroot 	imp->il_network = dnet;
4715640Sroot 	imp->il_host = dhost;
4726244Sroot 	imp->il_imp = htons((u_short)dimp);
4736160Ssam 	imp->il_length =
4746160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4755640Sroot 	imp->il_link = dlink;
4765859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4775640Sroot 
4785640Sroot leaderexists:
4795640Sroot 	return (impsnd(ifp, m));
4805647Ssam drop:
4815647Ssam 	m_freem(m0);
4826504Ssam 	return (error);
4835640Sroot }
4845640Sroot 
4855640Sroot /*
4865640Sroot  * Put a message on an interface's output queue.
4875640Sroot  * Perform RFNM counting: no more than 8 message may be
4885640Sroot  * in flight to any one host.
4895640Sroot  */
4905640Sroot impsnd(ifp, m)
4915640Sroot 	struct ifnet *ifp;
4925640Sroot 	struct mbuf *m;
4935640Sroot {
4945640Sroot 	register struct imp_leader *ip;
4955640Sroot 	register struct host *hp;
4965640Sroot 	struct impcb *icp;
4976588Ssam 	int s, error;
4985640Sroot 
4995771Swnj COUNT(IMPSND);
5005640Sroot 	ip = mtod(m, struct imp_leader *);
5015640Sroot 
5025640Sroot 	/*
5035640Sroot 	 * Do RFNM counting for data messages
5045640Sroot 	 * (no more than 8 outstanding to any host)
5055640Sroot 	 */
5066588Ssam 	s = splimp();
5075640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5085640Sroot 		struct in_addr addr;
5095640Sroot 
5105859Sroot #ifdef notdef
5115771Swnj                 addr.s_net = ip->il_network;
5125859Sroot #else
5136588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5145859Sroot #endif
5155640Sroot                 addr.s_host = ip->il_host;
5165640Sroot                 addr.s_imp = ip->il_imp;
5175771Swnj 		if ((hp = hostlookup(addr)) == 0)
5185771Swnj 			hp = hostenter(addr);
5196588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5206607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5216588Ssam 			hp->h_timer = HOSTTIMER;
5226588Ssam 			hp->h_flags &= ~HF_INUSE;
5236588Ssam 			goto bad;
5246588Ssam 		}
5255640Sroot 
5265640Sroot 		/*
5275647Ssam 		 * If IMP would block, queue until RFNM
5285640Sroot 		 */
5295640Sroot 		if (hp) {
5305640Sroot 			if (hp->h_rfnm < 8) {
5315640Sroot 				hp->h_rfnm++;
5325640Sroot 				goto enque;
5335640Sroot 			}
5346095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5356095Swnj 				HOST_ENQUE(hp, m);
5366095Swnj 				goto start;
5376095Swnj 			}
5385640Sroot 		}
5396588Ssam 		error = ENOBUFS;
5406588Ssam 		goto bad;
5415640Sroot 	}
5425640Sroot enque:
5436208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5446208Swnj 		IF_DROP(&ifp->if_snd);
5456588Ssam 		error = ENOBUFS;
5466588Ssam bad:
5476208Swnj 		m_freem(m);
5486588Ssam 		splx(s);
5496588Ssam 		return (error);
5506208Swnj 	}
5515640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5526095Swnj start:
5535640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5545640Sroot 	if (icp->ic_oactive == 0)
5555640Sroot 		(*icp->ic_start)(ifp->if_unit);
5566630Ssam 	splx(s);
5576504Ssam 	return (0);
5585640Sroot }
5595640Sroot 
5605640Sroot /*
5615640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5625640Sroot  * Part of host-IMP initialization procedure.
5635640Sroot  * (Should return success/failure, but noone knows
5645640Sroot  * what to do with this, so why bother?)
5656818Ssam  * This routine is always called at splimp, so we don't
5666818Ssam  * protect the call to IF_PREPEND.
5675640Sroot  */
5685640Sroot impnoops(sc)
5695640Sroot 	register struct imp_softc *sc;
5705640Sroot {
5715640Sroot 	register i;
5725640Sroot 	register struct mbuf *m;
5735771Swnj 	register struct control_leader *cp;
5745640Sroot 	int x;
5755640Sroot 
5765771Swnj COUNT(IMPNOOPS);
5775640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5785771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5795640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5805640Sroot 			return;
5815640Sroot 		m->m_off = MMINOFF;
5825771Swnj 		m->m_len = sizeof(struct control_leader);
5835771Swnj 		cp = mtod(m, struct control_leader *);
5845771Swnj 		cp->dl_format = IMP_NFF;
5855771Swnj                 cp->dl_link = i;
5865771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5875640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5885640Sroot 	}
5895640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5905640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5915640Sroot }
5927170Ssam 
5937170Ssam #ifdef IMPLEADERS
5947170Ssam printleader(routine, ip)
5957170Ssam 	char *routine;
5967170Ssam 	register struct imp_leader *ip;
5977170Ssam {
5987170Ssam 	printf("%s: ", routine);
5997170Ssam 	printbyte((char *)ip, 12);
6007170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6017170Ssam 		ip->il_flags);
6027170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6037170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6047170Ssam 	else
6057170Ssam 		printf("%x,", ip->il_mtype);
6067170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6077170Ssam 		ntohs(ip->il_imp));
6087170Ssam 	if (ip->il_link == IMPLINK_IP)
6097170Ssam 		printf("ip,");
6107170Ssam 	else
6117170Ssam 		printf("%x,", ip->il_link);
6127170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6137170Ssam }
6147170Ssam 
6157170Ssam printbyte(cp, n)
6167170Ssam 	register char *cp;
6177170Ssam 	int n;
6187170Ssam {
6197170Ssam 	register i, j, c;
6207170Ssam 
6217170Ssam 	for (i=0; i<n; i++) {
6227170Ssam 		c = *cp++;
6237170Ssam 		for (j=0; j<2; j++)
6247170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6257170Ssam 		putchar(' ');
6267170Ssam 	}
6277170Ssam 	putchar('\n');
6287170Ssam }
6295640Sroot #endif
6307170Ssam #endif
631