xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 10899)
1*10899Ssam /*	if_imp.c	4.48	83/02/10	*/
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"
22*10899Ssam #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"
31*10899Ssam 
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:
2726588Ssam 	case IMPTYPE_HOSTUNREACH: {
2736588Ssam 		int s = splnet();
2748705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
2758705Sroot 		    hostlookup(addr));
2766588Ssam 		splx(s);
2775859Sroot 		goto rawlinkin;
2786588Ssam 	}
2795640Sroot 
2805640Sroot 	/*
2815640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2825640Sroot 	 * noops to the IMP to clear the interface.
2835640Sroot 	 */
2846588Ssam 	case IMPTYPE_BADDATA: {
2856588Ssam 		int s;
2866588Ssam 
2875924Sroot 		impmsg(sc, "data error");
2886588Ssam 		s = splnet();
2896588Ssam 		if (hp = hostlookup(addr))
2905640Sroot 			hp->h_rfnm = 0;
2916588Ssam 		splx(s);
2925640Sroot 		impnoops(sc);
2936582Ssam 		goto drop;
2946588Ssam 	}
2955640Sroot 
2965640Sroot 	/*
2975647Ssam 	 * Interface reset.
2985640Sroot 	 */
2995640Sroot 	case IMPTYPE_RESET:
3005924Sroot 		impmsg(sc, "interface reset");
3015647Ssam 		impnoops(sc);
3026582Ssam 		goto drop;
3035640Sroot 
3045640Sroot 	default:
3055640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3066582Ssam 		goto drop;
3075640Sroot 	}
3085640Sroot 
3095640Sroot 	/*
3106257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3116257Sroot 	 * protocol routine (running at software interrupt).
3126257Sroot 	 * If this isn't a raw interface, advance pointer
3136257Sroot 	 * into mbuf past leader.
3145640Sroot 	 */
3155640Sroot 	switch (ip->il_link) {
3165640Sroot 
3175640Sroot #ifdef INET
3185640Sroot 	case IMPLINK_IP:
3195640Sroot 		m->m_len -= sizeof(struct imp_leader);
3205640Sroot 		m->m_off += sizeof(struct imp_leader);
3216261Swnj 		schednetisr(NETISR_IP);
3225640Sroot 		inq = &ipintrq;
3235640Sroot 		break;
3245640Sroot #endif
3255640Sroot 
3265640Sroot 	default:
3275868Sroot 	rawlinkin:
3285640Sroot 		impproto.sp_protocol = ip->il_link;
3296336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3306336Ssam 		impdst.sin_addr = sin->sin_addr;;
3315645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3325645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3335645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3346527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3356527Ssam 		  (struct sockaddr *)&impdst);
3365640Sroot 		return;
3375640Sroot 	}
3386208Swnj 	if (IF_QFULL(inq)) {
3396208Swnj 		IF_DROP(inq);
3406208Swnj 		goto drop;
3416208Swnj 	}
3425640Sroot 	IF_ENQUEUE(inq, m);
3435640Sroot 	return;
3445640Sroot 
3455640Sroot drop:
3465640Sroot 	m_freem(m);
3475640Sroot }
3485640Sroot 
3495647Ssam /*
3505647Ssam  * Bring the IMP down after notification.
3515647Ssam  */
3525647Ssam impdown(sc)
3535647Ssam 	struct imp_softc *sc;
3545647Ssam {
3556208Swnj 
3565647Ssam 	sc->imp_state = IMPS_DOWN;
3575924Sroot 	impmsg(sc, "marked down");
3586588Ssam 	hostreset(sc->imp_if.if_net);
3596582Ssam 	if_down(&sc->imp_if);
3605647Ssam }
3615647Ssam 
3625640Sroot /*VARARGS*/
3635924Sroot impmsg(sc, fmt, a1, a2)
3645640Sroot 	struct imp_softc *sc;
3655640Sroot 	char *fmt;
3666160Ssam 	u_int a1;
3675640Sroot {
3686208Swnj 
3695640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3705640Sroot 	printf(fmt, a1, a2);
3715640Sroot 	printf("\n");
3725640Sroot }
3735640Sroot 
3745640Sroot /*
3756582Ssam  * Process an IMP "error" message, passing this
3766582Ssam  * up to the higher level protocol.
3776582Ssam  */
3786582Ssam impnotify(what, cp, hp)
3796582Ssam 	int what;
3806582Ssam 	struct control_leader *cp;
3816582Ssam 	struct host *hp;
3826582Ssam {
3836582Ssam 	struct in_addr in;
3846582Ssam 
3856582Ssam #ifdef notdef
3866582Ssam 	in.s_net = cp->dl_network;
3876582Ssam #else
3886588Ssam 	in.s_net = 10;			/* XXX */
3896582Ssam #endif
3906582Ssam 	in.s_host = cp->dl_host;
3916582Ssam 	in.s_imp = cp->dl_imp;
3926582Ssam 	if (cp->dl_link != IMPLINK_IP)
3936582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3946582Ssam 	else
3956582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3966588Ssam 	if (hp) {
3976588Ssam 		hp->h_flags |= (1 << what);
3986582Ssam 		hostfree(hp);
3996588Ssam 	}
4006582Ssam }
4016582Ssam 
4026582Ssam /*
4035640Sroot  * ARPAnet 1822 output routine.
4045640Sroot  * Called from higher level protocol routines to set up messages for
4055640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4065640Sroot  * enqueue the message for this IMP's hardware driver.
4075640Sroot  */
4086336Ssam impoutput(ifp, m0, dst)
4095640Sroot 	register struct ifnet *ifp;
4105640Sroot 	struct mbuf *m0;
4116336Ssam 	struct sockaddr *dst;
4125640Sroot {
4135640Sroot 	register struct imp_leader *imp;
4145640Sroot 	register struct mbuf *m = m0;
4158782Sroot 	int dhost, dimp, dlink, len, dnet;
4166504Ssam 	int error = 0;
4175640Sroot 
4185640Sroot 	/*
4195640Sroot 	 * Don't even try if the IMP is unavailable.
4205640Sroot 	 */
4216504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4226504Ssam 		error = ENETDOWN;
4235647Ssam 		goto drop;
4246504Ssam 	}
4255640Sroot 
4266336Ssam 	switch (dst->sa_family) {
4275640Sroot 
4285640Sroot #ifdef INET
4296336Ssam 	case AF_INET: {
4306336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4316336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4325640Sroot 
4336336Ssam 		dhost = sin->sin_addr.s_host;
4346336Ssam 		dimp = sin->sin_addr.s_impno;
4355640Sroot 		dlink = IMPLINK_IP;
4366244Sroot 		dnet = 0;
4376160Ssam 		len = ntohs((u_short)ip->ip_len);
4385640Sroot 		break;
4395640Sroot 	}
4405640Sroot #endif
4416336Ssam 	case AF_IMPLINK:
4425640Sroot 		goto leaderexists;
4435640Sroot 
4445640Sroot 	default:
4456336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4466336Ssam 			dst->sa_family);
4476504Ssam 		error = EAFNOSUPPORT;
4485647Ssam 		goto drop;
4495640Sroot 	}
4505640Sroot 
4515640Sroot 	/*
4525640Sroot 	 * Add IMP leader.  If there's not enough space in the
4535640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4545640Sroot 	 * drop this sucker.
4555640Sroot 	 */
4565640Sroot 	if (m->m_off > MMAXOFF ||
4575640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4589645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4596504Ssam 		if (m == 0) {
4606504Ssam 			error = ENOBUFS;
4615647Ssam 			goto drop;
4626504Ssam 		}
4635640Sroot 		m->m_next = m0;
4645640Sroot 		m->m_len = sizeof(struct imp_leader);
4655640Sroot 	} else {
4665640Sroot 		m->m_off -= sizeof(struct imp_leader);
4675640Sroot 		m->m_len += sizeof(struct imp_leader);
4685640Sroot 	}
4695640Sroot 	imp = mtod(m, struct imp_leader *);
4705640Sroot 	imp->il_format = IMP_NFF;
4715859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4726244Sroot 	imp->il_network = dnet;
4735640Sroot 	imp->il_host = dhost;
4746244Sroot 	imp->il_imp = htons((u_short)dimp);
4756160Ssam 	imp->il_length =
4766160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4775640Sroot 	imp->il_link = dlink;
4785859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4795640Sroot 
4805640Sroot leaderexists:
4815640Sroot 	return (impsnd(ifp, m));
4825647Ssam drop:
4835647Ssam 	m_freem(m0);
4846504Ssam 	return (error);
4855640Sroot }
4865640Sroot 
4875640Sroot /*
4885640Sroot  * Put a message on an interface's output queue.
4895640Sroot  * Perform RFNM counting: no more than 8 message may be
4905640Sroot  * in flight to any one host.
4915640Sroot  */
4925640Sroot impsnd(ifp, m)
4935640Sroot 	struct ifnet *ifp;
4945640Sroot 	struct mbuf *m;
4955640Sroot {
4965640Sroot 	register struct imp_leader *ip;
4975640Sroot 	register struct host *hp;
4985640Sroot 	struct impcb *icp;
4996588Ssam 	int s, error;
5005640Sroot 
5015640Sroot 	ip = mtod(m, struct imp_leader *);
5025640Sroot 
5035640Sroot 	/*
5045640Sroot 	 * Do RFNM counting for data messages
5055640Sroot 	 * (no more than 8 outstanding to any host)
5065640Sroot 	 */
5076588Ssam 	s = splimp();
5085640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5095640Sroot 		struct in_addr addr;
5105640Sroot 
5115859Sroot #ifdef notdef
5125771Swnj                 addr.s_net = ip->il_network;
5135859Sroot #else
5146588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5155859Sroot #endif
5165640Sroot                 addr.s_host = ip->il_host;
5175640Sroot                 addr.s_imp = ip->il_imp;
5185771Swnj 		if ((hp = hostlookup(addr)) == 0)
5195771Swnj 			hp = hostenter(addr);
5206588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5216607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5226588Ssam 			hp->h_timer = HOSTTIMER;
5236588Ssam 			hp->h_flags &= ~HF_INUSE;
5246588Ssam 			goto bad;
5256588Ssam 		}
5265640Sroot 
5275640Sroot 		/*
5285647Ssam 		 * If IMP would block, queue until RFNM
5295640Sroot 		 */
5305640Sroot 		if (hp) {
5315640Sroot 			if (hp->h_rfnm < 8) {
5325640Sroot 				hp->h_rfnm++;
5335640Sroot 				goto enque;
5345640Sroot 			}
5356095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5366095Swnj 				HOST_ENQUE(hp, m);
5376095Swnj 				goto start;
5386095Swnj 			}
5395640Sroot 		}
5406588Ssam 		error = ENOBUFS;
5416588Ssam 		goto bad;
5425640Sroot 	}
5435640Sroot enque:
5446208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5456208Swnj 		IF_DROP(&ifp->if_snd);
5466588Ssam 		error = ENOBUFS;
5476588Ssam bad:
5486208Swnj 		m_freem(m);
5496588Ssam 		splx(s);
5506588Ssam 		return (error);
5516208Swnj 	}
5525640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5536095Swnj start:
5545640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5555640Sroot 	if (icp->ic_oactive == 0)
5565640Sroot 		(*icp->ic_start)(ifp->if_unit);
5576630Ssam 	splx(s);
5586504Ssam 	return (0);
5595640Sroot }
5605640Sroot 
5615640Sroot /*
5625640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5635640Sroot  * Part of host-IMP initialization procedure.
5645640Sroot  * (Should return success/failure, but noone knows
5655640Sroot  * what to do with this, so why bother?)
5666818Ssam  * This routine is always called at splimp, so we don't
5676818Ssam  * protect the call to IF_PREPEND.
5685640Sroot  */
5695640Sroot impnoops(sc)
5705640Sroot 	register struct imp_softc *sc;
5715640Sroot {
5725640Sroot 	register i;
5735640Sroot 	register struct mbuf *m;
5745771Swnj 	register struct control_leader *cp;
5755640Sroot 
5765640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5775771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5789645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
5795640Sroot 			return;
5805771Swnj 		m->m_len = sizeof(struct control_leader);
5815771Swnj 		cp = mtod(m, struct control_leader *);
5825771Swnj 		cp->dl_format = IMP_NFF;
5835771Swnj                 cp->dl_link = i;
5845771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5855640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5865640Sroot 	}
5875640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5885640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5895640Sroot }
5907170Ssam 
5917170Ssam #ifdef IMPLEADERS
5927170Ssam printleader(routine, ip)
5937170Ssam 	char *routine;
5947170Ssam 	register struct imp_leader *ip;
5957170Ssam {
5967170Ssam 	printf("%s: ", routine);
5977170Ssam 	printbyte((char *)ip, 12);
5987170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5997170Ssam 		ip->il_flags);
6007170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6017170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6027170Ssam 	else
6037170Ssam 		printf("%x,", ip->il_mtype);
6047170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6057170Ssam 		ntohs(ip->il_imp));
6067170Ssam 	if (ip->il_link == IMPLINK_IP)
6077170Ssam 		printf("ip,");
6087170Ssam 	else
6097170Ssam 		printf("%x,", ip->il_link);
6107170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6117170Ssam }
6127170Ssam 
6137170Ssam printbyte(cp, n)
6147170Ssam 	register char *cp;
6157170Ssam 	int n;
6167170Ssam {
6177170Ssam 	register i, j, c;
6187170Ssam 
6197170Ssam 	for (i=0; i<n; i++) {
6207170Ssam 		c = *cp++;
6217170Ssam 		for (j=0; j<2; j++)
6227170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6237170Ssam 		putchar(' ');
6247170Ssam 	}
6257170Ssam 	putchar('\n');
6267170Ssam }
6275640Sroot #endif
6287170Ssam #endif
629