xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 11230)
1*11230Ssam /*	if_imp.c	4.49	83/02/23	*/
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  */
119800Ssam #include "../machine/pte.h"
129800Ssam 
135640Sroot #include "../h/param.h"
145640Sroot #include "../h/systm.h"
155640Sroot #include "../h/mbuf.h"
165640Sroot #include "../h/buf.h"
175640Sroot #include "../h/protosw.h"
185640Sroot #include "../h/socket.h"
198705Sroot #include "../h/vmmac.h"
2010874Ssam #include "../h/time.h"
2110874Ssam #include "../h/kernel.h"
2210899Ssam #include "../h/errno.h"
238705Sroot 
248705Sroot #include "../vax/cpu.h"
258533Sroot #include "../vax/mtpr.h"
268782Sroot #include "../vaxuba/ubareg.h"
278782Sroot #include "../vaxuba/ubavar.h"
288705Sroot 
298705Sroot #include "../net/if.h"
308705Sroot #include "../net/route.h"
3110899Ssam 
328705Sroot #include "../net/netisr.h"
338408Swnj #include "../netinet/in.h"
348408Swnj #include "../netinet/in_systm.h"
358705Sroot #include "../netinet/ip.h"
368705Sroot #include "../netinet/ip_var.h"
377199Ssam /* define IMPLEADERS here to get leader printing code */
388408Swnj #include "../netimp/if_imp.h"
398408Swnj #include "../netimp/if_imphost.h"
405640Sroot 
415640Sroot /*
425640Sroot  * IMP software status per interface.
435640Sroot  * (partially shared with the hardware specific module)
445640Sroot  *
455640Sroot  * Each interface is referenced by a network interface structure,
465640Sroot  * imp_if, which the routing code uses to locate the interface.
475640Sroot  * This structure contains the output queue for the interface, its
485640Sroot  * address, ...  IMP specific structures used in connecting the
495640Sroot  * IMP software modules to the hardware specific interface routines
505771Swnj  * are stored here.  The common structures are made visible to the
515771Swnj  * interface driver by passing a pointer to the hardware routine
525771Swnj  * at "attach" time.
535640Sroot  *
545640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
555640Sroot  */
565640Sroot struct imp_softc {
575640Sroot 	struct	ifnet imp_if;		/* network visible interface */
585640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
595640Sroot 	u_char	imp_state;		/* current state of IMP */
605640Sroot 	char	imp_dropcnt;		/* used during initialization */
615640Sroot } imp_softc[NIMP];
625640Sroot 
635640Sroot /*
645640Sroot  * Messages from IMP regarding why
655640Sroot  * it's going down.
665640Sroot  */
675924Sroot static char *impmessage[] = {
685640Sroot 	"in 30 seconds",
695640Sroot 	"for hardware PM",
705640Sroot 	"to reload software",
715640Sroot 	"for emergency reset"
725640Sroot };
735640Sroot 
745771Swnj int	impdown(), impinit(), impoutput();
755771Swnj 
765640Sroot /*
775640Sroot  * IMP attach routine.  Called from hardware device attach routine
785640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
795640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
805640Sroot  * structures.  This is then used by the device's attach routine
815640Sroot  * set up its back pointers.
825640Sroot  */
838815Sroot impattach(ui, reset)
845640Sroot 	struct uba_device *ui;
858815Sroot 	int (*reset)();
865640Sroot {
875640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
885640Sroot 	register struct ifnet *ifp = &sc->imp_if;
896336Ssam 	struct sockaddr_in *sin;
905640Sroot 
915640Sroot 	/* UNIT COULD BE AMBIGUOUS */
925640Sroot 	ifp->if_unit = ui->ui_unit;
935640Sroot 	ifp->if_name = "imp";
946271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
955640Sroot 	ifp->if_net = ui->ui_flags;
969001Sroot 	ifp->if_reset = reset;
975989Sroot 	/* the host and imp fields will be filled in by the imp */
986336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
996336Ssam 	sin->sin_family = AF_INET;
1006336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
1015771Swnj 	ifp->if_init = impinit;
1025771Swnj 	ifp->if_output = impoutput;
1035771Swnj 	/* reset is handled at the hardware level */
1045640Sroot 	if_attach(ifp);
1055640Sroot 	return ((int)&sc->imp_if);
1065640Sroot }
1075640Sroot 
1085640Sroot /*
1095640Sroot  * IMP initialization routine: call hardware module to
1105640Sroot  * setup UNIBUS resources, init state and get ready for
1115640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1125640Sroot  */
1135640Sroot impinit(unit)
1145640Sroot 	int unit;
1155640Sroot {
1166500Ssam 	int s = splimp();
1175640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1185640Sroot 
1195771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1205771Swnj 		sc->imp_state = IMPS_DOWN;
1216336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1226500Ssam 		splx(s);
1235771Swnj 		return;
1245771Swnj 	}
1255640Sroot 	sc->imp_state = IMPS_INIT;
1265771Swnj 	impnoops(sc);
1276500Ssam 	splx(s);
1285640Sroot }
1295640Sroot 
1305640Sroot struct sockproto impproto = { PF_IMPLINK };
1315645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1325645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1337199Ssam #ifdef IMPLEADERS
1347170Ssam int	impprintfs = 0;
1357199Ssam #endif
1365640Sroot 
1375640Sroot /*
1385640Sroot  * ARPAnet 1822 input routine.
1395640Sroot  * Called from hardware input interrupt routine to handle 1822
1405640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1415640Sroot  * passed to higher level protocol processors on the basis
1425640Sroot  * of link number.  Other type messages (control) are handled here.
1435640Sroot  */
1445771Swnj impinput(unit, m)
1455640Sroot 	int unit;
1465771Swnj 	register struct mbuf *m;
1475640Sroot {
1485640Sroot 	register struct imp_leader *ip;
1495640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1505640Sroot 	register struct host *hp;
1515640Sroot 	register struct ifqueue *inq;
1525771Swnj 	struct control_leader *cp;
1535640Sroot 	struct in_addr addr;
1545924Sroot 	struct mbuf *next;
1556336Ssam 	struct sockaddr_in *sin;
1565640Sroot 
1576257Sroot 	/* verify leader length. */
1585771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1595771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1605771Swnj 		return;
1615771Swnj 	cp = mtod(m, struct control_leader *);
1625771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1635771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1645771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1655771Swnj 			return;
1665640Sroot 	ip = mtod(m, struct imp_leader *);
1677199Ssam #ifdef IMPLEADERS
1687170Ssam 	if (impprintfs)
1697170Ssam 		printleader("impinput", ip);
1707199Ssam #endif
1715640Sroot 
1726257Sroot 	/* check leader type */
1735771Swnj 	if (ip->il_format != IMP_NFF) {
1745771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1755640Sroot 		goto drop;
1765771Swnj 	}
1775640Sroot 
1786588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1795859Sroot #ifdef notdef
1805771Swnj 		addr.s_net = ip->il_network;
1815859Sroot #else
1826588Ssam 		addr.s_net = sc->imp_if.if_net;
1835859Sroot #endif
1845771Swnj 		addr.s_imp = ip->il_imp;
1855771Swnj 		addr.s_host = ip->il_host;
1865640Sroot 	}
1875640Sroot 	switch (ip->il_mtype) {
1885640Sroot 
1895640Sroot 	case IMPTYPE_DATA:
1905640Sroot 		break;
1915640Sroot 
1925640Sroot 	/*
1935640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1945640Sroot 	 */
1955640Sroot 	case IMPTYPE_BADLEADER:
1965647Ssam 		/*
1975647Ssam 		 * According to 1822 document, this message
1985647Ssam 		 * will be generated in response to the
1995647Ssam 		 * first noop sent to the IMP after
2005647Ssam 		 * the host resets the IMP interface.
2015647Ssam 		 */
2025771Swnj 		if (sc->imp_state != IMPS_INIT) {
2035924Sroot 			impmsg(sc, "leader error");
2046582Ssam 			hostreset(sc->imp_if.if_net);
2055647Ssam 			impnoops(sc);
2065647Ssam 		}
2076582Ssam 		goto drop;
2085640Sroot 
2095640Sroot 	/*
2105640Sroot 	 * IMP going down.  Print message, and if not immediate,
2115640Sroot 	 * set off a timer to insure things will be reset at the
2125640Sroot 	 * appropriate time.
2135640Sroot 	 */
2145640Sroot 	case IMPTYPE_DOWN:
2156599Ssam 		if (sc->imp_state < IMPS_INIT)
2166598Ssam 			goto drop;
2175640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2185640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2196160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2205640Sroot 		}
2216160Ssam 		impmsg(sc, "going down %s",
2226160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2236582Ssam 		goto drop;
2245640Sroot 
2255640Sroot 	/*
2265640Sroot 	 * A NOP usually seen during the initialization sequence.
2275640Sroot 	 * Compare the local address with that in the message.
2285640Sroot 	 * Reset the local address notion if it doesn't match.
2295640Sroot 	 */
2306336Ssam 	case IMPTYPE_NOOP:
2315647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2325647Ssam 			sc->imp_state = IMPS_INIT;
2335647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2345647Ssam 		}
2356500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2366271Sroot 			goto drop;
2376500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2386500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2396500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2406500Ssam 			sc->imp_if.if_host[0] =
2416500Ssam 				sin->sin_addr.s_host = ip->il_host;
2426500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2436500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2446500Ssam 				ntohs(ip->il_imp));
2456500Ssam 		}
2465771Swnj 		sc->imp_state = IMPS_UP;
2476336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2487201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2496271Sroot 		goto drop;
2505640Sroot 
2515640Sroot 	/*
2526582Ssam 	 * RFNM or INCOMPLETE message, send next
2536582Ssam 	 * message on the q.  We could pass incomplete's
2546582Ssam 	 * up to the next level, but this currently isn't
2556582Ssam 	 * needed.
2565640Sroot 	 */
2575640Sroot 	case IMPTYPE_RFNM:
2585640Sroot 	case IMPTYPE_INCOMPLETE:
2596588Ssam 		if (hp = hostlookup(addr)) {
2606588Ssam 			if (hp->h_rfnm == 0)
2616588Ssam 				hp->h_flags &= ~HF_INUSE;
2626588Ssam 			else if (next = hostdeque(hp))
2636588Ssam 				(void) impsnd(&sc->imp_if, next);
2646588Ssam 		}
2656271Sroot 		goto drop;
2665640Sroot 
2675640Sroot 	/*
2685640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2695640Sroot 	 * awaiting transmission and release the host structure.
2705640Sroot 	 */
2715640Sroot 	case IMPTYPE_HOSTDEAD:
272*11230Ssam 	case IMPTYPE_HOSTUNREACH:
2738705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
2748705Sroot 		    hostlookup(addr));
2755859Sroot 		goto rawlinkin;
2765640Sroot 
2775640Sroot 	/*
2785640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2795640Sroot 	 * noops to the IMP to clear the interface.
2805640Sroot 	 */
281*11230Ssam 	case IMPTYPE_BADDATA:
2825924Sroot 		impmsg(sc, "data error");
2836588Ssam 		if (hp = hostlookup(addr))
2845640Sroot 			hp->h_rfnm = 0;
2855640Sroot 		impnoops(sc);
2866582Ssam 		goto drop;
2875640Sroot 
2885640Sroot 	/*
2895647Ssam 	 * Interface reset.
2905640Sroot 	 */
2915640Sroot 	case IMPTYPE_RESET:
2925924Sroot 		impmsg(sc, "interface reset");
2935647Ssam 		impnoops(sc);
2946582Ssam 		goto drop;
2955640Sroot 
2965640Sroot 	default:
2975640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
2986582Ssam 		goto drop;
2995640Sroot 	}
3005640Sroot 
3015640Sroot 	/*
3026257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3036257Sroot 	 * protocol routine (running at software interrupt).
3046257Sroot 	 * If this isn't a raw interface, advance pointer
3056257Sroot 	 * into mbuf past leader.
3065640Sroot 	 */
3075640Sroot 	switch (ip->il_link) {
3085640Sroot 
3095640Sroot #ifdef INET
3105640Sroot 	case IMPLINK_IP:
3115640Sroot 		m->m_len -= sizeof(struct imp_leader);
3125640Sroot 		m->m_off += sizeof(struct imp_leader);
3136261Swnj 		schednetisr(NETISR_IP);
3145640Sroot 		inq = &ipintrq;
3155640Sroot 		break;
3165640Sroot #endif
3175640Sroot 
3185640Sroot 	default:
3195868Sroot 	rawlinkin:
3205640Sroot 		impproto.sp_protocol = ip->il_link;
3216336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3226336Ssam 		impdst.sin_addr = sin->sin_addr;;
3235645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3245645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3255645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3266527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3276527Ssam 		  (struct sockaddr *)&impdst);
3285640Sroot 		return;
3295640Sroot 	}
3306208Swnj 	if (IF_QFULL(inq)) {
3316208Swnj 		IF_DROP(inq);
3326208Swnj 		goto drop;
3336208Swnj 	}
3345640Sroot 	IF_ENQUEUE(inq, m);
3355640Sroot 	return;
3365640Sroot 
3375640Sroot drop:
3385640Sroot 	m_freem(m);
3395640Sroot }
3405640Sroot 
3415647Ssam /*
3425647Ssam  * Bring the IMP down after notification.
3435647Ssam  */
3445647Ssam impdown(sc)
3455647Ssam 	struct imp_softc *sc;
3465647Ssam {
347*11230Ssam 	int s = splimp();
3486208Swnj 
3495647Ssam 	sc->imp_state = IMPS_DOWN;
3505924Sroot 	impmsg(sc, "marked down");
3516588Ssam 	hostreset(sc->imp_if.if_net);
3526582Ssam 	if_down(&sc->imp_if);
353*11230Ssam 	splx(s);
3545647Ssam }
3555647Ssam 
3565640Sroot /*VARARGS*/
3575924Sroot impmsg(sc, fmt, a1, a2)
3585640Sroot 	struct imp_softc *sc;
3595640Sroot 	char *fmt;
3606160Ssam 	u_int a1;
3615640Sroot {
3626208Swnj 
3635640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3645640Sroot 	printf(fmt, a1, a2);
3655640Sroot 	printf("\n");
3665640Sroot }
3675640Sroot 
3685640Sroot /*
3696582Ssam  * Process an IMP "error" message, passing this
3706582Ssam  * up to the higher level protocol.
3716582Ssam  */
3726582Ssam impnotify(what, cp, hp)
3736582Ssam 	int what;
3746582Ssam 	struct control_leader *cp;
3756582Ssam 	struct host *hp;
3766582Ssam {
3776582Ssam 	struct in_addr in;
3786582Ssam 
3796582Ssam #ifdef notdef
3806582Ssam 	in.s_net = cp->dl_network;
3816582Ssam #else
3826588Ssam 	in.s_net = 10;			/* XXX */
3836582Ssam #endif
3846582Ssam 	in.s_host = cp->dl_host;
3856582Ssam 	in.s_imp = cp->dl_imp;
3866582Ssam 	if (cp->dl_link != IMPLINK_IP)
3876582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3886582Ssam 	else
3896582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3906588Ssam 	if (hp) {
3916588Ssam 		hp->h_flags |= (1 << what);
3926582Ssam 		hostfree(hp);
3936588Ssam 	}
3946582Ssam }
3956582Ssam 
3966582Ssam /*
3975640Sroot  * ARPAnet 1822 output routine.
3985640Sroot  * Called from higher level protocol routines to set up messages for
3995640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4005640Sroot  * enqueue the message for this IMP's hardware driver.
4015640Sroot  */
4026336Ssam impoutput(ifp, m0, dst)
4035640Sroot 	register struct ifnet *ifp;
4045640Sroot 	struct mbuf *m0;
4056336Ssam 	struct sockaddr *dst;
4065640Sroot {
4075640Sroot 	register struct imp_leader *imp;
4085640Sroot 	register struct mbuf *m = m0;
4098782Sroot 	int dhost, dimp, dlink, len, dnet;
4106504Ssam 	int error = 0;
4115640Sroot 
4125640Sroot 	/*
4135640Sroot 	 * Don't even try if the IMP is unavailable.
4145640Sroot 	 */
4156504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4166504Ssam 		error = ENETDOWN;
4175647Ssam 		goto drop;
4186504Ssam 	}
4195640Sroot 
4206336Ssam 	switch (dst->sa_family) {
4215640Sroot 
4225640Sroot #ifdef INET
4236336Ssam 	case AF_INET: {
4246336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4256336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4265640Sroot 
4276336Ssam 		dhost = sin->sin_addr.s_host;
4286336Ssam 		dimp = sin->sin_addr.s_impno;
4295640Sroot 		dlink = IMPLINK_IP;
4306244Sroot 		dnet = 0;
4316160Ssam 		len = ntohs((u_short)ip->ip_len);
4325640Sroot 		break;
4335640Sroot 	}
4345640Sroot #endif
4356336Ssam 	case AF_IMPLINK:
4365640Sroot 		goto leaderexists;
4375640Sroot 
4385640Sroot 	default:
4396336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4406336Ssam 			dst->sa_family);
4416504Ssam 		error = EAFNOSUPPORT;
4425647Ssam 		goto drop;
4435640Sroot 	}
4445640Sroot 
4455640Sroot 	/*
4465640Sroot 	 * Add IMP leader.  If there's not enough space in the
4475640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4485640Sroot 	 * drop this sucker.
4495640Sroot 	 */
4505640Sroot 	if (m->m_off > MMAXOFF ||
4515640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4529645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4536504Ssam 		if (m == 0) {
4546504Ssam 			error = ENOBUFS;
4555647Ssam 			goto drop;
4566504Ssam 		}
4575640Sroot 		m->m_next = m0;
4585640Sroot 		m->m_len = sizeof(struct imp_leader);
4595640Sroot 	} else {
4605640Sroot 		m->m_off -= sizeof(struct imp_leader);
4615640Sroot 		m->m_len += sizeof(struct imp_leader);
4625640Sroot 	}
4635640Sroot 	imp = mtod(m, struct imp_leader *);
4645640Sroot 	imp->il_format = IMP_NFF;
4655859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4666244Sroot 	imp->il_network = dnet;
4675640Sroot 	imp->il_host = dhost;
4686244Sroot 	imp->il_imp = htons((u_short)dimp);
4696160Ssam 	imp->il_length =
4706160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4715640Sroot 	imp->il_link = dlink;
4725859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4735640Sroot 
4745640Sroot leaderexists:
4755640Sroot 	return (impsnd(ifp, m));
4765647Ssam drop:
4775647Ssam 	m_freem(m0);
4786504Ssam 	return (error);
4795640Sroot }
4805640Sroot 
4815640Sroot /*
4825640Sroot  * Put a message on an interface's output queue.
4835640Sroot  * Perform RFNM counting: no more than 8 message may be
4845640Sroot  * in flight to any one host.
4855640Sroot  */
4865640Sroot impsnd(ifp, m)
4875640Sroot 	struct ifnet *ifp;
4885640Sroot 	struct mbuf *m;
4895640Sroot {
4905640Sroot 	register struct imp_leader *ip;
4915640Sroot 	register struct host *hp;
4925640Sroot 	struct impcb *icp;
4936588Ssam 	int s, error;
4945640Sroot 
4955640Sroot 	ip = mtod(m, struct imp_leader *);
4965640Sroot 
4975640Sroot 	/*
4985640Sroot 	 * Do RFNM counting for data messages
4995640Sroot 	 * (no more than 8 outstanding to any host)
5005640Sroot 	 */
5016588Ssam 	s = splimp();
5025640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5035640Sroot 		struct in_addr addr;
5045640Sroot 
5055859Sroot #ifdef notdef
5065771Swnj                 addr.s_net = ip->il_network;
5075859Sroot #else
5086588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5095859Sroot #endif
5105640Sroot                 addr.s_host = ip->il_host;
5115640Sroot                 addr.s_imp = ip->il_imp;
5125771Swnj 		if ((hp = hostlookup(addr)) == 0)
5135771Swnj 			hp = hostenter(addr);
5146588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5156607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5166588Ssam 			hp->h_timer = HOSTTIMER;
5176588Ssam 			hp->h_flags &= ~HF_INUSE;
5186588Ssam 			goto bad;
5196588Ssam 		}
5205640Sroot 
5215640Sroot 		/*
5225647Ssam 		 * If IMP would block, queue until RFNM
5235640Sroot 		 */
5245640Sroot 		if (hp) {
5255640Sroot 			if (hp->h_rfnm < 8) {
5265640Sroot 				hp->h_rfnm++;
5275640Sroot 				goto enque;
5285640Sroot 			}
5296095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5306095Swnj 				HOST_ENQUE(hp, m);
5316095Swnj 				goto start;
5326095Swnj 			}
5335640Sroot 		}
5346588Ssam 		error = ENOBUFS;
5356588Ssam 		goto bad;
5365640Sroot 	}
5375640Sroot enque:
5386208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5396208Swnj 		IF_DROP(&ifp->if_snd);
5406588Ssam 		error = ENOBUFS;
5416588Ssam bad:
5426208Swnj 		m_freem(m);
5436588Ssam 		splx(s);
5446588Ssam 		return (error);
5456208Swnj 	}
5465640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5476095Swnj start:
5485640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5495640Sroot 	if (icp->ic_oactive == 0)
5505640Sroot 		(*icp->ic_start)(ifp->if_unit);
5516630Ssam 	splx(s);
5526504Ssam 	return (0);
5535640Sroot }
5545640Sroot 
5555640Sroot /*
5565640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5575640Sroot  * Part of host-IMP initialization procedure.
5585640Sroot  * (Should return success/failure, but noone knows
5595640Sroot  * what to do with this, so why bother?)
5606818Ssam  * This routine is always called at splimp, so we don't
5616818Ssam  * protect the call to IF_PREPEND.
5625640Sroot  */
5635640Sroot impnoops(sc)
5645640Sroot 	register struct imp_softc *sc;
5655640Sroot {
5665640Sroot 	register i;
5675640Sroot 	register struct mbuf *m;
5685771Swnj 	register struct control_leader *cp;
5695640Sroot 
5705640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5715771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5729645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
5735640Sroot 			return;
5745771Swnj 		m->m_len = sizeof(struct control_leader);
5755771Swnj 		cp = mtod(m, struct control_leader *);
5765771Swnj 		cp->dl_format = IMP_NFF;
5775771Swnj                 cp->dl_link = i;
5785771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5795640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5805640Sroot 	}
5815640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5825640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5835640Sroot }
5847170Ssam 
5857170Ssam #ifdef IMPLEADERS
5867170Ssam printleader(routine, ip)
5877170Ssam 	char *routine;
5887170Ssam 	register struct imp_leader *ip;
5897170Ssam {
5907170Ssam 	printf("%s: ", routine);
5917170Ssam 	printbyte((char *)ip, 12);
5927170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5937170Ssam 		ip->il_flags);
5947170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
5957170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
5967170Ssam 	else
5977170Ssam 		printf("%x,", ip->il_mtype);
5987170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
5997170Ssam 		ntohs(ip->il_imp));
6007170Ssam 	if (ip->il_link == IMPLINK_IP)
6017170Ssam 		printf("ip,");
6027170Ssam 	else
6037170Ssam 		printf("%x,", ip->il_link);
6047170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6057170Ssam }
6067170Ssam 
6077170Ssam printbyte(cp, n)
6087170Ssam 	register char *cp;
6097170Ssam 	int n;
6107170Ssam {
6117170Ssam 	register i, j, c;
6127170Ssam 
6137170Ssam 	for (i=0; i<n; i++) {
6147170Ssam 		c = *cp++;
6157170Ssam 		for (j=0; j<2; j++)
6167170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6177170Ssam 		putchar(' ');
6187170Ssam 	}
6197170Ssam 	putchar('\n');
6207170Ssam }
6215640Sroot #endif
6227170Ssam #endif
623