1*7170Ssam /*	if_imp.c	4.34	82/06/14	*/
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*7170Ssam #define	IMPLEADERS
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*7170Ssam int	impprintfs = 0;
1285640Sroot 
1295640Sroot /*
1305640Sroot  * ARPAnet 1822 input routine.
1315640Sroot  * Called from hardware input interrupt routine to handle 1822
1325640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1335640Sroot  * passed to higher level protocol processors on the basis
1345640Sroot  * of link number.  Other type messages (control) are handled here.
1355640Sroot  */
1365771Swnj impinput(unit, m)
1375640Sroot 	int unit;
1385771Swnj 	register struct mbuf *m;
1395640Sroot {
1405640Sroot 	register struct imp_leader *ip;
1415640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1425640Sroot 	register struct host *hp;
1435640Sroot 	register struct ifqueue *inq;
1445771Swnj 	struct control_leader *cp;
1455640Sroot 	struct in_addr addr;
1465924Sroot 	struct mbuf *next;
1476336Ssam 	struct sockaddr_in *sin;
1485640Sroot 
1495932Sroot COUNT(IMPINPUT);
1506257Sroot 	/* verify leader length. */
1515771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1525771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1535771Swnj 		return;
1545771Swnj 	cp = mtod(m, struct control_leader *);
1555771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1565771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1575771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1585771Swnj 			return;
1595640Sroot 	ip = mtod(m, struct imp_leader *);
160*7170Ssam 	if (impprintfs)
161*7170Ssam 		printleader("impinput", ip);
1625640Sroot 
1636257Sroot 	/* check leader type */
1645771Swnj 	if (ip->il_format != IMP_NFF) {
1655771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1665640Sroot 		goto drop;
1675771Swnj 	}
1685640Sroot 
1696588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1705859Sroot #ifdef notdef
1715771Swnj 		addr.s_net = ip->il_network;
1725859Sroot #else
1736588Ssam 		addr.s_net = sc->imp_if.if_net;
1745859Sroot #endif
1755771Swnj 		addr.s_imp = ip->il_imp;
1765771Swnj 		addr.s_host = ip->il_host;
1775640Sroot 	}
1785640Sroot 	switch (ip->il_mtype) {
1795640Sroot 
1805640Sroot 	case IMPTYPE_DATA:
1815640Sroot 		break;
1825640Sroot 
1835640Sroot 	/*
1845640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1855640Sroot 	 */
1865640Sroot 	case IMPTYPE_BADLEADER:
1875647Ssam 		/*
1885647Ssam 		 * According to 1822 document, this message
1895647Ssam 		 * will be generated in response to the
1905647Ssam 		 * first noop sent to the IMP after
1915647Ssam 		 * the host resets the IMP interface.
1925647Ssam 		 */
1935771Swnj 		if (sc->imp_state != IMPS_INIT) {
1945924Sroot 			impmsg(sc, "leader error");
1956582Ssam 			hostreset(sc->imp_if.if_net);
1965647Ssam 			impnoops(sc);
1975647Ssam 		}
1986582Ssam 		goto drop;
1995640Sroot 
2005640Sroot 	/*
2015640Sroot 	 * IMP going down.  Print message, and if not immediate,
2025640Sroot 	 * set off a timer to insure things will be reset at the
2035640Sroot 	 * appropriate time.
2045640Sroot 	 */
2055640Sroot 	case IMPTYPE_DOWN:
2066599Ssam 		if (sc->imp_state < IMPS_INIT)
2076598Ssam 			goto drop;
2085640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2095640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2106160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2115640Sroot 		}
2126160Ssam 		impmsg(sc, "going down %s",
2136160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2146582Ssam 		goto drop;
2155640Sroot 
2165640Sroot 	/*
2175640Sroot 	 * A NOP usually seen during the initialization sequence.
2185640Sroot 	 * Compare the local address with that in the message.
2195640Sroot 	 * Reset the local address notion if it doesn't match.
2205640Sroot 	 */
2216336Ssam 	case IMPTYPE_NOOP:
2225647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2235647Ssam 			sc->imp_state = IMPS_INIT;
2245647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2255647Ssam 		}
2266500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2276271Sroot 			goto drop;
2286500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2296500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2306500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2316500Ssam 			sc->imp_if.if_host[0] =
2326500Ssam 				sin->sin_addr.s_host = ip->il_host;
2336500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2346500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2356500Ssam 				ntohs(ip->il_imp));
2366500Ssam 		}
2375771Swnj 		sc->imp_state = IMPS_UP;
2386336Ssam 		sc->imp_if.if_flags |= IFF_UP;
239*7170Ssam #ifdef notdef
2405771Swnj 		/* restart output in case something was q'd */
241*7170Ssam 		if (sc->imp_cb.ic_oactive == 0)
242*7170Ssam 			(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
243*7170Ssam #endif
2446271Sroot 		goto drop;
2455640Sroot 
2465640Sroot 	/*
2476582Ssam 	 * RFNM or INCOMPLETE message, send next
2486582Ssam 	 * message on the q.  We could pass incomplete's
2496582Ssam 	 * up to the next level, but this currently isn't
2506582Ssam 	 * needed.
2515640Sroot 	 */
2525640Sroot 	case IMPTYPE_RFNM:
2535640Sroot 	case IMPTYPE_INCOMPLETE:
2546588Ssam 		if (hp = hostlookup(addr)) {
2556588Ssam 			if (hp->h_rfnm == 0)
2566588Ssam 				hp->h_flags &= ~HF_INUSE;
2576588Ssam 			else if (next = hostdeque(hp))
2586588Ssam 				(void) impsnd(&sc->imp_if, next);
2596588Ssam 		}
2606271Sroot 		goto drop;
2615640Sroot 
2625640Sroot 	/*
2635640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2645640Sroot 	 * awaiting transmission and release the host structure.
2655640Sroot 	 */
2665640Sroot 	case IMPTYPE_HOSTDEAD:
2676588Ssam 	case IMPTYPE_HOSTUNREACH: {
2686588Ssam 		int s = splnet();
2696588Ssam 		impnotify(ip->il_mtype, ip, hostlookup(addr));
2706588Ssam 		splx(s);
2715859Sroot 		goto rawlinkin;
2726588Ssam 	}
2735640Sroot 
2745640Sroot 	/*
2755640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2765640Sroot 	 * noops to the IMP to clear the interface.
2775640Sroot 	 */
2786588Ssam 	case IMPTYPE_BADDATA: {
2796588Ssam 		int s;
2806588Ssam 
2815924Sroot 		impmsg(sc, "data error");
2826588Ssam 		s = splnet();
2836588Ssam 		if (hp = hostlookup(addr))
2845640Sroot 			hp->h_rfnm = 0;
2856588Ssam 		splx(s);
2865640Sroot 		impnoops(sc);
2876582Ssam 		goto drop;
2886588Ssam 	}
2895640Sroot 
2905640Sroot 	/*
2915647Ssam 	 * Interface reset.
2925640Sroot 	 */
2935640Sroot 	case IMPTYPE_RESET:
2945924Sroot 		impmsg(sc, "interface reset");
2955647Ssam 		impnoops(sc);
2966582Ssam 		goto drop;
2975640Sroot 
2985640Sroot 	default:
2995640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3006582Ssam 		goto drop;
3015640Sroot 	}
3025640Sroot 
3035640Sroot 	/*
3046257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3056257Sroot 	 * protocol routine (running at software interrupt).
3066257Sroot 	 * If this isn't a raw interface, advance pointer
3076257Sroot 	 * into mbuf past leader.
3085640Sroot 	 */
3095640Sroot 	switch (ip->il_link) {
3105640Sroot 
3115640Sroot #ifdef INET
3125640Sroot 	case IMPLINK_IP:
3135640Sroot 		m->m_len -= sizeof(struct imp_leader);
3145640Sroot 		m->m_off += sizeof(struct imp_leader);
3156261Swnj 		schednetisr(NETISR_IP);
3165640Sroot 		inq = &ipintrq;
3175640Sroot 		break;
3185640Sroot #endif
3195640Sroot 
3205640Sroot 	default:
3215868Sroot 	rawlinkin:
3225640Sroot 		impproto.sp_protocol = ip->il_link;
3236336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3246336Ssam 		impdst.sin_addr = sin->sin_addr;;
3255645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3265645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3275645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3286527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3296527Ssam 		  (struct sockaddr *)&impdst);
3305640Sroot 		return;
3315640Sroot 	}
3326208Swnj 	if (IF_QFULL(inq)) {
3336208Swnj 		IF_DROP(inq);
3346208Swnj 		goto drop;
3356208Swnj 	}
3365640Sroot 	IF_ENQUEUE(inq, m);
3375640Sroot 	return;
3385640Sroot 
3395640Sroot drop:
3405640Sroot 	m_freem(m);
3415640Sroot }
3425640Sroot 
3435647Ssam /*
3445647Ssam  * Bring the IMP down after notification.
3455647Ssam  */
3465647Ssam impdown(sc)
3475647Ssam 	struct imp_softc *sc;
3485647Ssam {
3496208Swnj 
3506588Ssam COUNT(IMPDOWN);
3515647Ssam 	sc->imp_state = IMPS_DOWN;
3525924Sroot 	impmsg(sc, "marked down");
3536588Ssam 	hostreset(sc->imp_if.if_net);
3546582Ssam 	if_down(&sc->imp_if);
3555647Ssam }
3565647Ssam 
3575640Sroot /*VARARGS*/
3585924Sroot impmsg(sc, fmt, a1, a2)
3595640Sroot 	struct imp_softc *sc;
3605640Sroot 	char *fmt;
3616160Ssam 	u_int a1;
3625640Sroot {
3636208Swnj 
3646588Ssam COUNT(IMPMSG);
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 
3816588Ssam COUNT(IMPNOTIFY);
3826582Ssam #ifdef notdef
3836582Ssam 	in.s_net = cp->dl_network;
3846582Ssam #else
3856588Ssam 	in.s_net = 10;			/* XXX */
3866582Ssam #endif
3876582Ssam 	in.s_host = cp->dl_host;
3886582Ssam 	in.s_imp = cp->dl_imp;
3896582Ssam 	if (cp->dl_link != IMPLINK_IP)
3906582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3916582Ssam 	else
3926582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3936588Ssam 	if (hp) {
3946588Ssam 		hp->h_flags |= (1 << what);
3956582Ssam 		hostfree(hp);
3966588Ssam 	}
3976582Ssam }
3986582Ssam 
3996582Ssam /*
4005640Sroot  * ARPAnet 1822 output routine.
4015640Sroot  * Called from higher level protocol routines to set up messages for
4025640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4035640Sroot  * enqueue the message for this IMP's hardware driver.
4045640Sroot  */
4056336Ssam impoutput(ifp, m0, dst)
4065640Sroot 	register struct ifnet *ifp;
4075640Sroot 	struct mbuf *m0;
4086336Ssam 	struct sockaddr *dst;
4095640Sroot {
4105640Sroot 	register struct imp_leader *imp;
4115640Sroot 	register struct mbuf *m = m0;
4126244Sroot 	int x, dhost, dimp, dlink, len, dnet;
4136504Ssam 	int error = 0;
4145640Sroot 
4155771Swnj COUNT(IMPOUTPUT);
4165640Sroot 	/*
4175640Sroot 	 * Don't even try if the IMP is unavailable.
4185640Sroot 	 */
4196504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4206504Ssam 		error = ENETDOWN;
4215647Ssam 		goto drop;
4226504Ssam 	}
4235640Sroot 
4246336Ssam 	switch (dst->sa_family) {
4255640Sroot 
4265640Sroot #ifdef INET
4276336Ssam 	case AF_INET: {
4286336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4296336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4305640Sroot 
4316336Ssam 		dhost = sin->sin_addr.s_host;
4326336Ssam 		dimp = sin->sin_addr.s_impno;
4335640Sroot 		dlink = IMPLINK_IP;
4346244Sroot 		dnet = 0;
4356160Ssam 		len = ntohs((u_short)ip->ip_len);
4365640Sroot 		break;
4375640Sroot 	}
4385640Sroot #endif
4396336Ssam 	case AF_IMPLINK:
4405640Sroot 		goto leaderexists;
4415640Sroot 
4425640Sroot 	default:
4436336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4446336Ssam 			dst->sa_family);
4456504Ssam 		error = EAFNOSUPPORT;
4465647Ssam 		goto drop;
4475640Sroot 	}
4485640Sroot 
4495640Sroot 	/*
4505640Sroot 	 * Add IMP leader.  If there's not enough space in the
4515640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4525640Sroot 	 * drop this sucker.
4535640Sroot 	 */
4545640Sroot 	if (m->m_off > MMAXOFF ||
4555640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4565640Sroot 		m = m_get(M_DONTWAIT);
4576504Ssam 		if (m == 0) {
4586504Ssam 			error = ENOBUFS;
4595647Ssam 			goto drop;
4606504Ssam 		}
4615640Sroot 		m->m_next = m0;
4625640Sroot 		m->m_off = MMINOFF;
4635640Sroot 		m->m_len = sizeof(struct imp_leader);
4645640Sroot 	} else {
4655640Sroot 		m->m_off -= sizeof(struct imp_leader);
4665640Sroot 		m->m_len += sizeof(struct imp_leader);
4675640Sroot 	}
4685640Sroot 	imp = mtod(m, struct imp_leader *);
4695640Sroot 	imp->il_format = IMP_NFF;
4705859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4716244Sroot 	imp->il_network = dnet;
4725640Sroot 	imp->il_host = dhost;
4736244Sroot 	imp->il_imp = htons((u_short)dimp);
4746160Ssam 	imp->il_length =
4756160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4765640Sroot 	imp->il_link = dlink;
4775859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4785640Sroot 
4795640Sroot leaderexists:
4805640Sroot 	return (impsnd(ifp, m));
4815647Ssam drop:
4825647Ssam 	m_freem(m0);
4836504Ssam 	return (error);
4845640Sroot }
4855640Sroot 
4865640Sroot /*
4875640Sroot  * Put a message on an interface's output queue.
4885640Sroot  * Perform RFNM counting: no more than 8 message may be
4895640Sroot  * in flight to any one host.
4905640Sroot  */
4915640Sroot impsnd(ifp, m)
4925640Sroot 	struct ifnet *ifp;
4935640Sroot 	struct mbuf *m;
4945640Sroot {
4955640Sroot 	register struct imp_leader *ip;
4965640Sroot 	register struct host *hp;
4975640Sroot 	struct impcb *icp;
4986588Ssam 	int s, error;
4995640Sroot 
5005771Swnj COUNT(IMPSND);
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 	int x;
5765640Sroot 
5775771Swnj COUNT(IMPNOOPS);
5785640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5795771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5805640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5815640Sroot 			return;
5825640Sroot 		m->m_off = MMINOFF;
5835771Swnj 		m->m_len = sizeof(struct control_leader);
5845771Swnj 		cp = mtod(m, struct control_leader *);
5855771Swnj 		cp->dl_format = IMP_NFF;
5865771Swnj                 cp->dl_link = i;
5875771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5885640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5895640Sroot 	}
5905640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5915640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5925640Sroot }
593*7170Ssam 
594*7170Ssam #ifdef IMPLEADERS
595*7170Ssam printleader(routine, ip)
596*7170Ssam 	char *routine;
597*7170Ssam 	register struct imp_leader *ip;
598*7170Ssam {
599*7170Ssam 	printf("%s: ", routine);
600*7170Ssam 	printbyte((char *)ip, 12);
601*7170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
602*7170Ssam 		ip->il_flags);
603*7170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
604*7170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
605*7170Ssam 	else
606*7170Ssam 		printf("%x,", ip->il_mtype);
607*7170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
608*7170Ssam 		ntohs(ip->il_imp));
609*7170Ssam 	if (ip->il_link == IMPLINK_IP)
610*7170Ssam 		printf("ip,");
611*7170Ssam 	else
612*7170Ssam 		printf("%x,", ip->il_link);
613*7170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
614*7170Ssam }
615*7170Ssam 
616*7170Ssam printbyte(cp, n)
617*7170Ssam 	register char *cp;
618*7170Ssam 	int n;
619*7170Ssam {
620*7170Ssam 	register i, j, c;
621*7170Ssam 
622*7170Ssam 	for (i=0; i<n; i++) {
623*7170Ssam 		c = *cp++;
624*7170Ssam 		for (j=0; j<2; j++)
625*7170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
626*7170Ssam 		putchar(' ');
627*7170Ssam 	}
628*7170Ssam 	putchar('\n');
629*7170Ssam }
6305640Sroot #endif
631*7170Ssam #endif
632