xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 13067)
1*13067Ssam /*	if_imp.c	4.50	83/06/13	*/
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.
10*13067Ssam  *
11*13067Ssam  * NB: only handles IMPS on class A networks.
125640Sroot  */
139800Ssam #include "../machine/pte.h"
149800Ssam 
155640Sroot #include "../h/param.h"
165640Sroot #include "../h/systm.h"
175640Sroot #include "../h/mbuf.h"
185640Sroot #include "../h/buf.h"
195640Sroot #include "../h/protosw.h"
205640Sroot #include "../h/socket.h"
218705Sroot #include "../h/vmmac.h"
2210874Ssam #include "../h/time.h"
2310874Ssam #include "../h/kernel.h"
2410899Ssam #include "../h/errno.h"
25*13067Ssam #include "../h/ioctl.h"
268705Sroot 
278705Sroot #include "../vax/cpu.h"
288533Sroot #include "../vax/mtpr.h"
298782Sroot #include "../vaxuba/ubareg.h"
308782Sroot #include "../vaxuba/ubavar.h"
318705Sroot 
328705Sroot #include "../net/if.h"
338705Sroot #include "../net/route.h"
3410899Ssam 
358705Sroot #include "../net/netisr.h"
368408Swnj #include "../netinet/in.h"
378408Swnj #include "../netinet/in_systm.h"
388705Sroot #include "../netinet/ip.h"
398705Sroot #include "../netinet/ip_var.h"
407199Ssam /* define IMPLEADERS here to get leader printing code */
418408Swnj #include "../netimp/if_imp.h"
428408Swnj #include "../netimp/if_imphost.h"
435640Sroot 
445640Sroot /*
455640Sroot  * IMP software status per interface.
465640Sroot  * (partially shared with the hardware specific module)
475640Sroot  *
485640Sroot  * Each interface is referenced by a network interface structure,
495640Sroot  * imp_if, which the routing code uses to locate the interface.
505640Sroot  * This structure contains the output queue for the interface, its
515640Sroot  * address, ...  IMP specific structures used in connecting the
525640Sroot  * IMP software modules to the hardware specific interface routines
535771Swnj  * are stored here.  The common structures are made visible to the
545771Swnj  * interface driver by passing a pointer to the hardware routine
555771Swnj  * at "attach" time.
565640Sroot  *
575640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
585640Sroot  */
595640Sroot struct imp_softc {
605640Sroot 	struct	ifnet imp_if;		/* network visible interface */
615640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
625640Sroot 	u_char	imp_state;		/* current state of IMP */
635640Sroot 	char	imp_dropcnt;		/* used during initialization */
645640Sroot } imp_softc[NIMP];
655640Sroot 
665640Sroot /*
675640Sroot  * Messages from IMP regarding why
685640Sroot  * it's going down.
695640Sroot  */
705924Sroot static char *impmessage[] = {
715640Sroot 	"in 30 seconds",
725640Sroot 	"for hardware PM",
735640Sroot 	"to reload software",
745640Sroot 	"for emergency reset"
755640Sroot };
765640Sroot 
77*13067Ssam int	impdown(), impinit(), impioctl(), impoutput();
785771Swnj 
795640Sroot /*
805640Sroot  * IMP attach routine.  Called from hardware device attach routine
815640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
825640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
835640Sroot  * structures.  This is then used by the device's attach routine
845640Sroot  * set up its back pointers.
855640Sroot  */
868815Sroot impattach(ui, reset)
875640Sroot 	struct uba_device *ui;
888815Sroot 	int (*reset)();
895640Sroot {
905640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
915640Sroot 	register struct ifnet *ifp = &sc->imp_if;
926336Ssam 	struct sockaddr_in *sin;
935640Sroot 
945640Sroot 	/* UNIT COULD BE AMBIGUOUS */
955640Sroot 	ifp->if_unit = ui->ui_unit;
965640Sroot 	ifp->if_name = "imp";
976271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
989001Sroot 	ifp->if_reset = reset;
995771Swnj 	ifp->if_init = impinit;
100*13067Ssam 	ifp->if_ioctl = impioctl;
1015771Swnj 	ifp->if_output = impoutput;
1025771Swnj 	/* reset is handled at the hardware level */
1035640Sroot 	if_attach(ifp);
1045640Sroot 	return ((int)&sc->imp_if);
1055640Sroot }
1065640Sroot 
1075640Sroot /*
1085640Sroot  * IMP initialization routine: call hardware module to
1095640Sroot  * setup UNIBUS resources, init state and get ready for
1105640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1115640Sroot  */
1125640Sroot impinit(unit)
1135640Sroot 	int unit;
1145640Sroot {
1156500Ssam 	int s = splimp();
1165640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
117*13067Ssam 	struct sockaddr_in *sin;
1185640Sroot 
119*13067Ssam 	sin = (struct sockaddr_in *)&sc->sc_if;
120*13067Ssam 	if (in_netof(sin->sin_addr) == 0)
121*13067Ssam 		return;
1225771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1235771Swnj 		sc->imp_state = IMPS_DOWN;
1246336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1256500Ssam 		splx(s);
1265771Swnj 		return;
1275771Swnj 	}
128*13067Ssam 	sc->sc_if.if_flags |= IFF_RUNNING;
1295640Sroot 	sc->imp_state = IMPS_INIT;
1305771Swnj 	impnoops(sc);
1316500Ssam 	splx(s);
1325640Sroot }
1335640Sroot 
1345640Sroot struct sockproto impproto = { PF_IMPLINK };
1355645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1365645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1377199Ssam #ifdef IMPLEADERS
1387170Ssam int	impprintfs = 0;
1397199Ssam #endif
1405640Sroot 
1415640Sroot /*
1425640Sroot  * ARPAnet 1822 input routine.
1435640Sroot  * Called from hardware input interrupt routine to handle 1822
1445640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1455640Sroot  * passed to higher level protocol processors on the basis
1465640Sroot  * of link number.  Other type messages (control) are handled here.
1475640Sroot  */
1485771Swnj impinput(unit, m)
1495640Sroot 	int unit;
1505771Swnj 	register struct mbuf *m;
1515640Sroot {
1525640Sroot 	register struct imp_leader *ip;
1535640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1545640Sroot 	register struct host *hp;
1555640Sroot 	register struct ifqueue *inq;
1565771Swnj 	struct control_leader *cp;
1575640Sroot 	struct in_addr addr;
1585924Sroot 	struct mbuf *next;
1596336Ssam 	struct sockaddr_in *sin;
1605640Sroot 
1616257Sroot 	/* verify leader length. */
1625771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1635771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1645771Swnj 		return;
1655771Swnj 	cp = mtod(m, struct control_leader *);
1665771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1675771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1685771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1695771Swnj 			return;
1705640Sroot 	ip = mtod(m, struct imp_leader *);
1717199Ssam #ifdef IMPLEADERS
1727170Ssam 	if (impprintfs)
1737170Ssam 		printleader("impinput", ip);
1747199Ssam #endif
1755640Sroot 
1766257Sroot 	/* check leader type */
1775771Swnj 	if (ip->il_format != IMP_NFF) {
1785771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1795640Sroot 		goto drop;
1805771Swnj 	}
1815640Sroot 
1826588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1835859Sroot #ifdef notdef
1845771Swnj 		addr.s_net = ip->il_network;
1855859Sroot #else
1866588Ssam 		addr.s_net = sc->imp_if.if_net;
1875859Sroot #endif
1885771Swnj 		addr.s_imp = ip->il_imp;
1895771Swnj 		addr.s_host = ip->il_host;
1905640Sroot 	}
1915640Sroot 	switch (ip->il_mtype) {
1925640Sroot 
1935640Sroot 	case IMPTYPE_DATA:
1945640Sroot 		break;
1955640Sroot 
1965640Sroot 	/*
1975640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1985640Sroot 	 */
1995640Sroot 	case IMPTYPE_BADLEADER:
2005647Ssam 		/*
2015647Ssam 		 * According to 1822 document, this message
2025647Ssam 		 * will be generated in response to the
2035647Ssam 		 * first noop sent to the IMP after
2045647Ssam 		 * the host resets the IMP interface.
2055647Ssam 		 */
2065771Swnj 		if (sc->imp_state != IMPS_INIT) {
2075924Sroot 			impmsg(sc, "leader error");
2086582Ssam 			hostreset(sc->imp_if.if_net);
2095647Ssam 			impnoops(sc);
2105647Ssam 		}
2116582Ssam 		goto drop;
2125640Sroot 
2135640Sroot 	/*
2145640Sroot 	 * IMP going down.  Print message, and if not immediate,
2155640Sroot 	 * set off a timer to insure things will be reset at the
2165640Sroot 	 * appropriate time.
2175640Sroot 	 */
2185640Sroot 	case IMPTYPE_DOWN:
2196599Ssam 		if (sc->imp_state < IMPS_INIT)
2206598Ssam 			goto drop;
2215640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2225640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2236160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2245640Sroot 		}
2256160Ssam 		impmsg(sc, "going down %s",
2266160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2276582Ssam 		goto drop;
2285640Sroot 
2295640Sroot 	/*
2305640Sroot 	 * A NOP usually seen during the initialization sequence.
2315640Sroot 	 * Compare the local address with that in the message.
2325640Sroot 	 * Reset the local address notion if it doesn't match.
2335640Sroot 	 */
2346336Ssam 	case IMPTYPE_NOOP:
2355647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2365647Ssam 			sc->imp_state = IMPS_INIT;
2375647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2385647Ssam 		}
2396500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2406271Sroot 			goto drop;
2416500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2426500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2436500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2446500Ssam 			sc->imp_if.if_host[0] =
2456500Ssam 				sin->sin_addr.s_host = ip->il_host;
2466500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2476500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2486500Ssam 				ntohs(ip->il_imp));
2496500Ssam 		}
2505771Swnj 		sc->imp_state = IMPS_UP;
2516336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2527201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2536271Sroot 		goto drop;
2545640Sroot 
2555640Sroot 	/*
2566582Ssam 	 * RFNM or INCOMPLETE message, send next
2576582Ssam 	 * message on the q.  We could pass incomplete's
2586582Ssam 	 * up to the next level, but this currently isn't
2596582Ssam 	 * needed.
2605640Sroot 	 */
2615640Sroot 	case IMPTYPE_RFNM:
2625640Sroot 	case IMPTYPE_INCOMPLETE:
2636588Ssam 		if (hp = hostlookup(addr)) {
2646588Ssam 			if (hp->h_rfnm == 0)
2656588Ssam 				hp->h_flags &= ~HF_INUSE;
2666588Ssam 			else if (next = hostdeque(hp))
2676588Ssam 				(void) impsnd(&sc->imp_if, next);
2686588Ssam 		}
2696271Sroot 		goto drop;
2705640Sroot 
2715640Sroot 	/*
2725640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2735640Sroot 	 * awaiting transmission and release the host structure.
2745640Sroot 	 */
2755640Sroot 	case IMPTYPE_HOSTDEAD:
27611230Ssam 	case IMPTYPE_HOSTUNREACH:
2778705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
2788705Sroot 		    hostlookup(addr));
2795859Sroot 		goto rawlinkin;
2805640Sroot 
2815640Sroot 	/*
2825640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2835640Sroot 	 * noops to the IMP to clear the interface.
2845640Sroot 	 */
28511230Ssam 	case IMPTYPE_BADDATA:
2865924Sroot 		impmsg(sc, "data error");
2876588Ssam 		if (hp = hostlookup(addr))
2885640Sroot 			hp->h_rfnm = 0;
2895640Sroot 		impnoops(sc);
2906582Ssam 		goto drop;
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 {
35111230Ssam 	int s = splimp();
3526208Swnj 
3535647Ssam 	sc->imp_state = IMPS_DOWN;
3545924Sroot 	impmsg(sc, "marked down");
3556588Ssam 	hostreset(sc->imp_if.if_net);
3566582Ssam 	if_down(&sc->imp_if);
35711230Ssam 	splx(s);
3585647Ssam }
3595647Ssam 
3605640Sroot /*VARARGS*/
3615924Sroot impmsg(sc, fmt, a1, a2)
3625640Sroot 	struct imp_softc *sc;
3635640Sroot 	char *fmt;
3646160Ssam 	u_int a1;
3655640Sroot {
3666208Swnj 
3675640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3685640Sroot 	printf(fmt, a1, a2);
3695640Sroot 	printf("\n");
3705640Sroot }
3715640Sroot 
3725640Sroot /*
3736582Ssam  * Process an IMP "error" message, passing this
3746582Ssam  * up to the higher level protocol.
3756582Ssam  */
3766582Ssam impnotify(what, cp, hp)
3776582Ssam 	int what;
3786582Ssam 	struct control_leader *cp;
3796582Ssam 	struct host *hp;
3806582Ssam {
3816582Ssam 	struct in_addr in;
3826582Ssam 
3836582Ssam #ifdef notdef
3846582Ssam 	in.s_net = cp->dl_network;
3856582Ssam #else
3866588Ssam 	in.s_net = 10;			/* XXX */
3876582Ssam #endif
3886582Ssam 	in.s_host = cp->dl_host;
3896582Ssam 	in.s_imp = cp->dl_imp;
3906582Ssam 	if (cp->dl_link != IMPLINK_IP)
3916582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3926582Ssam 	else
3936582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3946588Ssam 	if (hp) {
3956588Ssam 		hp->h_flags |= (1 << what);
3966582Ssam 		hostfree(hp);
3976588Ssam 	}
3986582Ssam }
3996582Ssam 
4006582Ssam /*
4015640Sroot  * ARPAnet 1822 output routine.
4025640Sroot  * Called from higher level protocol routines to set up messages for
4035640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4045640Sroot  * enqueue the message for this IMP's hardware driver.
4055640Sroot  */
4066336Ssam impoutput(ifp, m0, dst)
4075640Sroot 	register struct ifnet *ifp;
4085640Sroot 	struct mbuf *m0;
4096336Ssam 	struct sockaddr *dst;
4105640Sroot {
4115640Sroot 	register struct imp_leader *imp;
4125640Sroot 	register struct mbuf *m = m0;
4138782Sroot 	int dhost, dimp, dlink, len, dnet;
4146504Ssam 	int error = 0;
4155640Sroot 
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) {
4569645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4576504Ssam 		if (m == 0) {
4586504Ssam 			error = ENOBUFS;
4595647Ssam 			goto drop;
4606504Ssam 		}
4615640Sroot 		m->m_next = m0;
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 
4995640Sroot 	ip = mtod(m, struct imp_leader *);
5005640Sroot 
5015640Sroot 	/*
5025640Sroot 	 * Do RFNM counting for data messages
5035640Sroot 	 * (no more than 8 outstanding to any host)
5045640Sroot 	 */
5056588Ssam 	s = splimp();
5065640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5075640Sroot 		struct in_addr addr;
5085640Sroot 
5095859Sroot #ifdef notdef
5105771Swnj                 addr.s_net = ip->il_network;
5115859Sroot #else
5126588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5135859Sroot #endif
5145640Sroot                 addr.s_host = ip->il_host;
5155640Sroot                 addr.s_imp = ip->il_imp;
5165771Swnj 		if ((hp = hostlookup(addr)) == 0)
5175771Swnj 			hp = hostenter(addr);
5186588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5196607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5206588Ssam 			hp->h_timer = HOSTTIMER;
5216588Ssam 			hp->h_flags &= ~HF_INUSE;
5226588Ssam 			goto bad;
5236588Ssam 		}
5245640Sroot 
5255640Sroot 		/*
5265647Ssam 		 * If IMP would block, queue until RFNM
5275640Sroot 		 */
5285640Sroot 		if (hp) {
5295640Sroot 			if (hp->h_rfnm < 8) {
5305640Sroot 				hp->h_rfnm++;
5315640Sroot 				goto enque;
5325640Sroot 			}
5336095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5346095Swnj 				HOST_ENQUE(hp, m);
5356095Swnj 				goto start;
5366095Swnj 			}
5375640Sroot 		}
5386588Ssam 		error = ENOBUFS;
5396588Ssam 		goto bad;
5405640Sroot 	}
5415640Sroot enque:
5426208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5436208Swnj 		IF_DROP(&ifp->if_snd);
5446588Ssam 		error = ENOBUFS;
5456588Ssam bad:
5466208Swnj 		m_freem(m);
5476588Ssam 		splx(s);
5486588Ssam 		return (error);
5496208Swnj 	}
5505640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5516095Swnj start:
5525640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5535640Sroot 	if (icp->ic_oactive == 0)
5545640Sroot 		(*icp->ic_start)(ifp->if_unit);
5556630Ssam 	splx(s);
5566504Ssam 	return (0);
5575640Sroot }
5585640Sroot 
5595640Sroot /*
5605640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5615640Sroot  * Part of host-IMP initialization procedure.
5625640Sroot  * (Should return success/failure, but noone knows
5635640Sroot  * what to do with this, so why bother?)
5646818Ssam  * This routine is always called at splimp, so we don't
5656818Ssam  * protect the call to IF_PREPEND.
5665640Sroot  */
5675640Sroot impnoops(sc)
5685640Sroot 	register struct imp_softc *sc;
5695640Sroot {
5705640Sroot 	register i;
5715640Sroot 	register struct mbuf *m;
5725771Swnj 	register struct control_leader *cp;
5735640Sroot 
5745640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5755771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5769645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
5775640Sroot 			return;
5785771Swnj 		m->m_len = sizeof(struct control_leader);
5795771Swnj 		cp = mtod(m, struct control_leader *);
5805771Swnj 		cp->dl_format = IMP_NFF;
5815771Swnj                 cp->dl_link = i;
5825771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5835640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5845640Sroot 	}
5855640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5865640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5875640Sroot }
5887170Ssam 
589*13067Ssam /*
590*13067Ssam  * Process an ioctl request.
591*13067Ssam  */
592*13067Ssam impioctl(ifp, cmd, data)
593*13067Ssam 	register struct ifnet *ifp;
594*13067Ssam 	int cmd;
595*13067Ssam 	caddr_t data;
596*13067Ssam {
597*13067Ssam 	struct ifreq *ifr = (struct ifreq *)data;
598*13067Ssam 	struct sockaddr_in *sin;
599*13067Ssam 	int s = splimp(), error = 0;
600*13067Ssam 
601*13067Ssam 	switch (cmd) {
602*13067Ssam 
603*13067Ssam 	case SIOCSIFADDR:
604*13067Ssam 		if (ifp->if_flags & IFF_RUNNING)
605*13067Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
606*13067Ssam 		sin = (struct sockaddr_in *)&ifr->ifr_addr;
607*13067Ssam 		ifp->if_net = in_netof(sin->sin_addr);
608*13067Ssam 		sin = (struct sockaddr_in *)&ifp->if_addr;
609*13067Ssam 		sin->sin_family = AF_INET;
610*13067Ssam 		/* host number filled in already, or filled in later */
611*13067Ssam 		sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
612*13067Ssam 		if (ifp->if_flags & IFF_RUNNING)
613*13067Ssam 			if_rtinit(ifp, RTF_UP);
614*13067Ssam 		else
615*13067Ssam 			impinit(ifp->if_unit);
616*13067Ssam 		break;
617*13067Ssam 
618*13067Ssam 	default:
619*13067Ssam 		error = EINVAL;
620*13067Ssam 	}
621*13067Ssam 	splx(s);
622*13067Ssam 	return (error);
623*13067Ssam }
624*13067Ssam 
6257170Ssam #ifdef IMPLEADERS
6267170Ssam printleader(routine, ip)
6277170Ssam 	char *routine;
6287170Ssam 	register struct imp_leader *ip;
6297170Ssam {
6307170Ssam 	printf("%s: ", routine);
6317170Ssam 	printbyte((char *)ip, 12);
6327170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6337170Ssam 		ip->il_flags);
6347170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6357170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6367170Ssam 	else
6377170Ssam 		printf("%x,", ip->il_mtype);
6387170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6397170Ssam 		ntohs(ip->il_imp));
6407170Ssam 	if (ip->il_link == IMPLINK_IP)
6417170Ssam 		printf("ip,");
6427170Ssam 	else
6437170Ssam 		printf("%x,", ip->il_link);
6447170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6457170Ssam }
6467170Ssam 
6477170Ssam printbyte(cp, n)
6487170Ssam 	register char *cp;
6497170Ssam 	int n;
6507170Ssam {
6517170Ssam 	register i, j, c;
6527170Ssam 
6537170Ssam 	for (i=0; i<n; i++) {
6547170Ssam 		c = *cp++;
6557170Ssam 		for (j=0; j<2; j++)
6567170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6577170Ssam 		putchar(' ');
6587170Ssam 	}
6597170Ssam 	putchar('\n');
6607170Ssam }
6615640Sroot #endif
6627170Ssam #endif
663