xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 23166)
1*23166Smckusick /*
2*23166Smckusick  * Copyright (c) 1982 Regents of the University of California.
3*23166Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*23166Smckusick  * specifies the terms and conditions for redistribution.
5*23166Smckusick  *
6*23166Smckusick  *	@(#)if_imp.c	6.6 (Berkeley) 06/08/85
7*23166Smckusick  */
85640Sroot 
95640Sroot #include "imp.h"
105640Sroot #if NIMP > 0
115640Sroot /*
126582Ssam  * ARPANET IMP interface driver.
135640Sroot  *
145640Sroot  * The IMP-host protocol is handled here, leaving
155640Sroot  * hardware specifics to the lower level interface driver.
165640Sroot  */
179800Ssam #include "../machine/pte.h"
189800Ssam 
1917068Sbloom #include "param.h"
2017068Sbloom #include "systm.h"
2117068Sbloom #include "mbuf.h"
2217068Sbloom #include "buf.h"
2317068Sbloom #include "protosw.h"
2417068Sbloom #include "socket.h"
2517068Sbloom #include "vmmac.h"
2617068Sbloom #include "time.h"
2717068Sbloom #include "kernel.h"
2817068Sbloom #include "errno.h"
2917068Sbloom #include "ioctl.h"
308705Sroot 
318705Sroot #include "../vax/cpu.h"
328533Sroot #include "../vax/mtpr.h"
338782Sroot #include "../vaxuba/ubareg.h"
348782Sroot #include "../vaxuba/ubavar.h"
358705Sroot 
368705Sroot #include "../net/if.h"
378705Sroot #include "../net/route.h"
3810899Ssam 
398705Sroot #include "../net/netisr.h"
408408Swnj #include "../netinet/in.h"
418408Swnj #include "../netinet/in_systm.h"
4218412Skarels #include "../netinet/in_var.h"
438705Sroot #include "../netinet/ip.h"
448705Sroot #include "../netinet/ip_var.h"
457199Ssam /* define IMPLEADERS here to get leader printing code */
4617068Sbloom #include "if_imp.h"
4717068Sbloom #include "if_imphost.h"
485640Sroot 
495640Sroot /*
505640Sroot  * IMP software status per interface.
515640Sroot  * (partially shared with the hardware specific module)
525640Sroot  *
535640Sroot  * Each interface is referenced by a network interface structure,
545640Sroot  * imp_if, which the routing code uses to locate the interface.
555640Sroot  * This structure contains the output queue for the interface, its
565640Sroot  * address, ...  IMP specific structures used in connecting the
575640Sroot  * IMP software modules to the hardware specific interface routines
585771Swnj  * are stored here.  The common structures are made visible to the
595771Swnj  * interface driver by passing a pointer to the hardware routine
605771Swnj  * at "attach" time.
615640Sroot  *
625640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
635640Sroot  */
645640Sroot struct imp_softc {
655640Sroot 	struct	ifnet imp_if;		/* network visible interface */
665640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
675640Sroot 	u_char	imp_state;		/* current state of IMP */
685640Sroot 	char	imp_dropcnt;		/* used during initialization */
695640Sroot } imp_softc[NIMP];
705640Sroot 
715640Sroot /*
725640Sroot  * Messages from IMP regarding why
735640Sroot  * it's going down.
745640Sroot  */
755924Sroot static char *impmessage[] = {
765640Sroot 	"in 30 seconds",
775640Sroot 	"for hardware PM",
785640Sroot 	"to reload software",
795640Sroot 	"for emergency reset"
805640Sroot };
815640Sroot 
8218132Skarels #define HOSTDEADTIMER	10		/* How long to wait when down */
8318132Skarels 
8413067Ssam int	impdown(), impinit(), impioctl(), impoutput();
855771Swnj 
865640Sroot /*
875640Sroot  * IMP attach routine.  Called from hardware device attach routine
885640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
895640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
905640Sroot  * structures.  This is then used by the device's attach routine
915640Sroot  * set up its back pointers.
925640Sroot  */
938815Sroot impattach(ui, reset)
945640Sroot 	struct uba_device *ui;
958815Sroot 	int (*reset)();
965640Sroot {
975640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
985640Sroot 	register struct ifnet *ifp = &sc->imp_if;
995640Sroot 
1005640Sroot 	/* UNIT COULD BE AMBIGUOUS */
1015640Sroot 	ifp->if_unit = ui->ui_unit;
1025640Sroot 	ifp->if_name = "imp";
1036271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
1049001Sroot 	ifp->if_reset = reset;
1055771Swnj 	ifp->if_init = impinit;
10613067Ssam 	ifp->if_ioctl = impioctl;
1075771Swnj 	ifp->if_output = impoutput;
1085771Swnj 	/* reset is handled at the hardware level */
1095640Sroot 	if_attach(ifp);
1105640Sroot 	return ((int)&sc->imp_if);
1115640Sroot }
1125640Sroot 
1135640Sroot /*
1145640Sroot  * IMP initialization routine: call hardware module to
1155640Sroot  * setup UNIBUS resources, init state and get ready for
1165640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1175640Sroot  */
1185640Sroot impinit(unit)
1195640Sroot 	int unit;
1205640Sroot {
1216500Ssam 	int s = splimp();
1225640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1235640Sroot 
12418412Skarels 	if (sc->imp_if.if_addrlist == 0)
12513067Ssam 		return;
1265771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1275771Swnj 		sc->imp_state = IMPS_DOWN;
1286336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1296500Ssam 		splx(s);
1305771Swnj 		return;
1315771Swnj 	}
1325640Sroot 	sc->imp_state = IMPS_INIT;
1335771Swnj 	impnoops(sc);
1346500Ssam 	splx(s);
1355640Sroot }
1365640Sroot 
1375640Sroot struct sockproto impproto = { PF_IMPLINK };
1385645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1395645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1407199Ssam #ifdef IMPLEADERS
1417170Ssam int	impprintfs = 0;
1427199Ssam #endif
1435640Sroot 
1445640Sroot /*
1455640Sroot  * ARPAnet 1822 input routine.
1465640Sroot  * Called from hardware input interrupt routine to handle 1822
1475640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1485640Sroot  * passed to higher level protocol processors on the basis
1495640Sroot  * of link number.  Other type messages (control) are handled here.
1505640Sroot  */
1515771Swnj impinput(unit, m)
1525640Sroot 	int unit;
1535771Swnj 	register struct mbuf *m;
1545640Sroot {
1555640Sroot 	register struct imp_leader *ip;
1565640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1575640Sroot 	register struct host *hp;
1585640Sroot 	register struct ifqueue *inq;
1595771Swnj 	struct control_leader *cp;
1605640Sroot 	struct in_addr addr;
1615924Sroot 	struct mbuf *next;
1626336Ssam 	struct sockaddr_in *sin;
1635640Sroot 
1646257Sroot 	/* verify leader length. */
1655771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1665771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1675771Swnj 		return;
1685771Swnj 	cp = mtod(m, struct control_leader *);
1695771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1705771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1715771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1725771Swnj 			return;
1735640Sroot 	ip = mtod(m, struct imp_leader *);
1747199Ssam #ifdef IMPLEADERS
1757170Ssam 	if (impprintfs)
1767170Ssam 		printleader("impinput", ip);
1777199Ssam #endif
1785640Sroot 
1796257Sroot 	/* check leader type */
1805771Swnj 	if (ip->il_format != IMP_NFF) {
1815771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1825640Sroot 		goto drop;
1835771Swnj 	}
1845640Sroot 
1856588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
18618132Skarels 		/* If not data packet, build IP addr from leader (BRL) */
18718412Skarels 		imp_leader_to_addr(&addr, ip, &sc->imp_if);
1885640Sroot 	}
1895640Sroot 	switch (ip->il_mtype) {
1905640Sroot 
1915640Sroot 	case IMPTYPE_DATA:
1925640Sroot 		break;
1935640Sroot 
1945640Sroot 	/*
1955640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1965640Sroot 	 */
1975640Sroot 	case IMPTYPE_BADLEADER:
1985647Ssam 		/*
1995647Ssam 		 * According to 1822 document, this message
2005647Ssam 		 * will be generated in response to the
2015647Ssam 		 * first noop sent to the IMP after
2025647Ssam 		 * the host resets the IMP interface.
2035647Ssam 		 */
2045771Swnj 		if (sc->imp_state != IMPS_INIT) {
2055924Sroot 			impmsg(sc, "leader error");
20618412Skarels 			hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
2075647Ssam 			impnoops(sc);
2085647Ssam 		}
2096582Ssam 		goto drop;
2105640Sroot 
2115640Sroot 	/*
2125640Sroot 	 * IMP going down.  Print message, and if not immediate,
2135640Sroot 	 * set off a timer to insure things will be reset at the
2145640Sroot 	 * appropriate time.
2155640Sroot 	 */
2165640Sroot 	case IMPTYPE_DOWN:
2176599Ssam 		if (sc->imp_state < IMPS_INIT)
2186598Ssam 			goto drop;
2195640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2205640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2216160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2225640Sroot 		}
2236160Ssam 		impmsg(sc, "going down %s",
2246160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2256582Ssam 		goto drop;
2265640Sroot 
2275640Sroot 	/*
2285640Sroot 	 * A NOP usually seen during the initialization sequence.
2295640Sroot 	 * Compare the local address with that in the message.
2305640Sroot 	 * Reset the local address notion if it doesn't match.
2315640Sroot 	 */
2326336Ssam 	case IMPTYPE_NOOP:
2335647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2345647Ssam 			sc->imp_state = IMPS_INIT;
2355647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2365647Ssam 		}
2376500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2386271Sroot 			goto drop;
23918412Skarels 		sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
24018412Skarels 		if (ip->il_imp != 0) {	/* BRL */
24118132Skarels 			struct in_addr leader_addr;
24218412Skarels 			imp_leader_to_addr(&leader_addr, ip, &sc->imp_if);
24318412Skarels 			if (sin->sin_addr.s_addr != leader_addr.s_addr) {
24418412Skarels 				impmsg(sc, "address reset to x%x (%d/%d)",
24518412Skarels 					htonl(leader_addr.s_addr),
24618132Skarels 					(u_int)ip->il_host,
24718412Skarels 					htons(ip->il_imp));
24818132Skarels 				sin->sin_addr.s_addr = leader_addr.s_addr;
24918132Skarels 			}
2506500Ssam 		}
2515771Swnj 		sc->imp_state = IMPS_UP;
2526336Ssam 		sc->imp_if.if_flags |= IFF_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,
27818412Skarels 		    hostlookup(addr), &sc->imp_if);
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");
29718412Skarels 		/* clear RFNM counts */
29818412Skarels 		hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
2995647Ssam 		impnoops(sc);
3006582Ssam 		goto drop;
3015640Sroot 
3025640Sroot 	default:
3035640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3046582Ssam 		goto drop;
3055640Sroot 	}
3065640Sroot 
3075640Sroot 	/*
3086257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3096257Sroot 	 * protocol routine (running at software interrupt).
3106257Sroot 	 * If this isn't a raw interface, advance pointer
3116257Sroot 	 * into mbuf past leader.
3125640Sroot 	 */
3135640Sroot 	switch (ip->il_link) {
3145640Sroot 
3155640Sroot #ifdef INET
3165640Sroot 	case IMPLINK_IP:
3175640Sroot 		m->m_len -= sizeof(struct imp_leader);
3185640Sroot 		m->m_off += sizeof(struct imp_leader);
3196261Swnj 		schednetisr(NETISR_IP);
3205640Sroot 		inq = &ipintrq;
3215640Sroot 		break;
3225640Sroot #endif
3235640Sroot 
3245640Sroot 	default:
3255868Sroot 	rawlinkin:
3265640Sroot 		impproto.sp_protocol = ip->il_link;
32718412Skarels 		sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
32818132Skarels 		impdst.sin_addr = sin->sin_addr;
32918412Skarels 		imp_leader_to_addr(&impsrc.sin_addr, ip, &sc->imp_if);
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");
35518412Skarels 	hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
3566582Ssam 	if_down(&sc->imp_if);
35711230Ssam 	splx(s);
3585647Ssam }
3595647Ssam 
3605640Sroot /*VARARGS*/
36118132Skarels impmsg(sc, fmt, a1, a2, a3)
3625640Sroot 	struct imp_softc *sc;
3635640Sroot 	char *fmt;
3646160Ssam 	u_int a1;
3655640Sroot {
3666208Swnj 
3675640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
36818132Skarels 	printf(fmt, a1, a2, a3);
3695640Sroot 	printf("\n");
3705640Sroot }
3715640Sroot 
3725640Sroot /*
3736582Ssam  * Process an IMP "error" message, passing this
3746582Ssam  * up to the higher level protocol.
3756582Ssam  */
37618132Skarels impnotify(what, cp, hp, ifp)
3776582Ssam 	int what;
3786582Ssam 	struct control_leader *cp;
3796582Ssam 	struct host *hp;
38018132Skarels 	struct ifnet *ifp;		/* BRL */
3816582Ssam {
3826582Ssam 	struct in_addr in;
3836582Ssam 
38418412Skarels 	imp_leader_to_addr(&in, (struct imp_leader *)cp, ifp);  /* BRL */
38518132Skarels 
3866582Ssam 	if (cp->dl_link != IMPLINK_IP)
3876582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3886582Ssam 	else
38921114Skarels 		pfctlinput(what, (caddr_t)&in);
3906588Ssam 	if (hp) {
3916588Ssam 		hp->h_flags |= (1 << what);
3926582Ssam 		hostfree(hp);
39318132Skarels 		hp->h_timer = HOSTDEADTIMER;
3946588Ssam 	}
3956582Ssam }
3966582Ssam 
3976582Ssam /*
3985640Sroot  * ARPAnet 1822 output routine.
3995640Sroot  * Called from higher level protocol routines to set up messages for
4005640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4015640Sroot  * enqueue the message for this IMP's hardware driver.
4025640Sroot  */
4036336Ssam impoutput(ifp, m0, dst)
4045640Sroot 	register struct ifnet *ifp;
4055640Sroot 	struct mbuf *m0;
4066336Ssam 	struct sockaddr *dst;
4075640Sroot {
4085640Sroot 	register struct imp_leader *imp;
4095640Sroot 	register struct mbuf *m = m0;
41018412Skarels 	int dlink, len;
4116504Ssam 	int error = 0;
4125640Sroot 
4135640Sroot 	/*
4145640Sroot 	 * Don't even try if the IMP is unavailable.
4155640Sroot 	 */
4166504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4176504Ssam 		error = ENETDOWN;
4185647Ssam 		goto drop;
4196504Ssam 	}
4205640Sroot 
4216336Ssam 	switch (dst->sa_family) {
4225640Sroot 
4235640Sroot #ifdef INET
4246336Ssam 	case AF_INET: {
4256336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4266336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4275640Sroot 
4285640Sroot 		dlink = IMPLINK_IP;
4296160Ssam 		len = ntohs((u_short)ip->ip_len);
4305640Sroot 		break;
4315640Sroot 	}
4325640Sroot #endif
4336336Ssam 	case AF_IMPLINK:
4345640Sroot 		goto leaderexists;
4355640Sroot 
4365640Sroot 	default:
4376336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4386336Ssam 			dst->sa_family);
4396504Ssam 		error = EAFNOSUPPORT;
4405647Ssam 		goto drop;
4415640Sroot 	}
4425640Sroot 
4435640Sroot 	/*
4445640Sroot 	 * Add IMP leader.  If there's not enough space in the
4455640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4465640Sroot 	 * drop this sucker.
4475640Sroot 	 */
4485640Sroot 	if (m->m_off > MMAXOFF ||
4495640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4509645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4516504Ssam 		if (m == 0) {
4526504Ssam 			error = ENOBUFS;
4535647Ssam 			goto drop;
4546504Ssam 		}
4555640Sroot 		m->m_next = m0;
4565640Sroot 		m->m_len = sizeof(struct imp_leader);
4575640Sroot 	} else {
4585640Sroot 		m->m_off -= sizeof(struct imp_leader);
4595640Sroot 		m->m_len += sizeof(struct imp_leader);
4605640Sroot 	}
4615640Sroot 	imp = mtod(m, struct imp_leader *);
4625640Sroot 	imp->il_format = IMP_NFF;
4635859Sroot 	imp->il_mtype = IMPTYPE_DATA;
46418132Skarels 	imp_addr_to_leader(imp,
46518412Skarels 		((struct sockaddr_in *)dst)->sin_addr.s_addr); /* BRL */
46618412Skarels 	imp->il_length = htons((u_short)len << 3);		/* BRL */
4675640Sroot 	imp->il_link = dlink;
4685859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4695640Sroot 
4705640Sroot leaderexists:
4715640Sroot 	return (impsnd(ifp, m));
4725647Ssam drop:
4735647Ssam 	m_freem(m0);
4746504Ssam 	return (error);
4755640Sroot }
4765640Sroot 
4775640Sroot /*
4785640Sroot  * Put a message on an interface's output queue.
4795640Sroot  * Perform RFNM counting: no more than 8 message may be
4805640Sroot  * in flight to any one host.
4815640Sroot  */
4825640Sroot impsnd(ifp, m)
4835640Sroot 	struct ifnet *ifp;
4845640Sroot 	struct mbuf *m;
4855640Sroot {
4865640Sroot 	register struct imp_leader *ip;
4875640Sroot 	register struct host *hp;
4885640Sroot 	struct impcb *icp;
4896588Ssam 	int s, error;
4905640Sroot 
4915640Sroot 	ip = mtod(m, struct imp_leader *);
4925640Sroot 
4935640Sroot 	/*
4945640Sroot 	 * Do RFNM counting for data messages
4955640Sroot 	 * (no more than 8 outstanding to any host)
4965640Sroot 	 */
4976588Ssam 	s = splimp();
4985640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
4995640Sroot 		struct in_addr addr;
5005640Sroot 
50118412Skarels 		imp_leader_to_addr(&addr, ip, ifp);	/* BRL */
5025771Swnj 		if ((hp = hostlookup(addr)) == 0)
5035771Swnj 			hp = hostenter(addr);
5046588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5056607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
50618132Skarels 			hp->h_timer = HOSTDEADTIMER;
5076588Ssam 			hp->h_flags &= ~HF_INUSE;
5086588Ssam 			goto bad;
5096588Ssam 		}
5105640Sroot 
5115640Sroot 		/*
5125647Ssam 		 * If IMP would block, queue until RFNM
5135640Sroot 		 */
5145640Sroot 		if (hp) {
51518132Skarels #ifndef NORFNM					/* BRL */
51618132Skarels 			if (hp->h_rfnm < 8)
51718132Skarels #endif
51818132Skarels 			{
5195640Sroot 				hp->h_rfnm++;
5205640Sroot 				goto enque;
5215640Sroot 			}
5226095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5236095Swnj 				HOST_ENQUE(hp, m);
5246095Swnj 				goto start;
5256095Swnj 			}
5265640Sroot 		}
5276588Ssam 		error = ENOBUFS;
5286588Ssam 		goto bad;
5295640Sroot 	}
5305640Sroot enque:
5316208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5326208Swnj 		IF_DROP(&ifp->if_snd);
5336588Ssam 		error = ENOBUFS;
5346588Ssam bad:
5356208Swnj 		m_freem(m);
5366588Ssam 		splx(s);
5376588Ssam 		return (error);
5386208Swnj 	}
5395640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5406095Swnj start:
5415640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5425640Sroot 	if (icp->ic_oactive == 0)
5435640Sroot 		(*icp->ic_start)(ifp->if_unit);
5446630Ssam 	splx(s);
5456504Ssam 	return (0);
5465640Sroot }
5475640Sroot 
5485640Sroot /*
5495640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5505640Sroot  * Part of host-IMP initialization procedure.
5515640Sroot  * (Should return success/failure, but noone knows
5525640Sroot  * what to do with this, so why bother?)
5536818Ssam  * This routine is always called at splimp, so we don't
5546818Ssam  * protect the call to IF_PREPEND.
5555640Sroot  */
5565640Sroot impnoops(sc)
5575640Sroot 	register struct imp_softc *sc;
5585640Sroot {
5595640Sroot 	register i;
5605640Sroot 	register struct mbuf *m;
5615771Swnj 	register struct control_leader *cp;
5625640Sroot 
5635640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
56418412Skarels 	for (i = 0; i < IMP_DROPCNT + 1; i++) {
5659645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
5665640Sroot 			return;
5675771Swnj 		m->m_len = sizeof(struct control_leader);
5685771Swnj 		cp = mtod(m, struct control_leader *);
5695771Swnj 		cp->dl_format = IMP_NFF;
5705771Swnj                 cp->dl_link = i;
5715771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5725640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5735640Sroot 	}
5745640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5755640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5765640Sroot }
5777170Ssam 
57813067Ssam /*
57913067Ssam  * Process an ioctl request.
58013067Ssam  */
58113067Ssam impioctl(ifp, cmd, data)
58213067Ssam 	register struct ifnet *ifp;
58313067Ssam 	int cmd;
58413067Ssam 	caddr_t data;
58513067Ssam {
58618412Skarels 	struct ifaddr *ifa = (struct ifaddr *) data;
58713067Ssam 	int s = splimp(), error = 0;
58813067Ssam 
58913067Ssam 	switch (cmd) {
59013067Ssam 
59113067Ssam 	case SIOCSIFADDR:
59218412Skarels 		if (ifa->ifa_addr.sa_family != AF_INET) {
59318412Skarels 			error = EINVAL;
59418412Skarels 			break;
59518412Skarels 		}
59618412Skarels 		if ((ifp->if_flags & IFF_RUNNING) == 0)
59713067Ssam 			impinit(ifp->if_unit);
59813067Ssam 		break;
59913067Ssam 
60013067Ssam 	default:
60113067Ssam 		error = EINVAL;
60213067Ssam 	}
60313067Ssam 	splx(s);
60413067Ssam 	return (error);
60513067Ssam }
60613067Ssam 
6077170Ssam #ifdef IMPLEADERS
6087170Ssam printleader(routine, ip)
6097170Ssam 	char *routine;
6107170Ssam 	register struct imp_leader *ip;
6117170Ssam {
6127170Ssam 	printf("%s: ", routine);
6137170Ssam 	printbyte((char *)ip, 12);
6147170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6157170Ssam 		ip->il_flags);
6167170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6177170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6187170Ssam 	else
6197170Ssam 		printf("%x,", ip->il_mtype);
6207170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6217170Ssam 		ntohs(ip->il_imp));
6227170Ssam 	if (ip->il_link == IMPLINK_IP)
6237170Ssam 		printf("ip,");
6247170Ssam 	else
6257170Ssam 		printf("%x,", ip->il_link);
6267170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6277170Ssam }
6287170Ssam 
6297170Ssam printbyte(cp, n)
6307170Ssam 	register char *cp;
6317170Ssam 	int n;
6327170Ssam {
6337170Ssam 	register i, j, c;
6347170Ssam 
6357170Ssam 	for (i=0; i<n; i++) {
6367170Ssam 		c = *cp++;
6377170Ssam 		for (j=0; j<2; j++)
6387170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6397170Ssam 		putchar(' ');
6407170Ssam 	}
6417170Ssam 	putchar('\n');
6427170Ssam }
6435640Sroot #endif
64418132Skarels 
64518132Skarels /*
64618132Skarels  * Routine to convert from IMP Leader to InterNet Address.
64718132Skarels  *
64818132Skarels  * This procedure is necessary because IMPs may be assigned Class A, B, or C
64918132Skarels  * network numbers, but only have 8 bits in the leader to reflect the
65018132Skarels  * IMP "network number".  The strategy is to take the network number from
65118132Skarels  * the ifnet structure, and blend in the host-on-imp and imp-on-net numbers
65218132Skarels  * from the leader.
65318132Skarels  *
65418132Skarels  * There is no support for "Logical Hosts".
65518132Skarels  *
65618132Skarels  * Class A:	Net.Host.0.Imp
65718132Skarels  * Class B:	Net.net.Host.Imp
65818132Skarels  * Class C:	Net.net.net.(Host4|Imp4)
65918132Skarels  */
66018412Skarels imp_leader_to_addr(ap, ip, ifp)
66118412Skarels 	struct in_addr *ap;
66218412Skarels 	register struct imp_leader *ip;
66318412Skarels 	struct ifnet *ifp;
66418132Skarels {
66518132Skarels 	register long final;
66618412Skarels 	struct in_ifaddr *ia;
66718132Skarels 	register struct sockaddr_in *sin;
66818132Skarels 	int imp = htons(ip->il_imp);
66918132Skarels 
67018412Skarels 	sin = (struct sockaddr_in *)(&ifp->if_addrlist->ifa_addr);
67118412Skarels 	final = htonl(sin->sin_addr.s_addr);
67218132Skarels 
67318412Skarels 	if (IN_CLASSA(final)) {
67418132Skarels 		final &= IN_CLASSA_NET;
67518132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<16);
67618412Skarels 	} else if (IN_CLASSB(final)) {
67718132Skarels 		final &= IN_CLASSB_NET;
67818132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<8);
67918132Skarels 	} else {
68018132Skarels 		final &= IN_CLASSC_NET;
68118132Skarels 		final |= (imp & 0x0F) | ((ip->il_host & 0x0F)<<4);
68218132Skarels 	}
68318412Skarels 	ap->s_addr = htonl(final);
68418132Skarels }
68518132Skarels 
68618132Skarels /*
68718132Skarels  * Function to take InterNet address and fill in IMP leader fields.
68818132Skarels  */
68918412Skarels imp_addr_to_leader(imp, a)
69018412Skarels 	register struct imp_leader *imp;
69118412Skarels 	long a;
69218132Skarels {
69318412Skarels 	register long addr = htonl(a);		/* host order */
69418132Skarels 
69518132Skarels 	imp->il_network = 0;	/* !! */
69618132Skarels 
69718412Skarels 	if (IN_CLASSA(addr)) {
69818132Skarels 		imp->il_host = ((addr>>16) & 0xFF);
69918132Skarels 		imp->il_imp = addr & 0xFF;
70018412Skarels 	} else if (IN_CLASSB(addr)) {
70118132Skarels 		imp->il_host = ((addr>>8) & 0xFF);
70218132Skarels 		imp->il_imp = addr & 0xFF;
70318132Skarels 	} else {
70418132Skarels 		imp->il_host = ((addr>>4) & 0xF);
70518132Skarels 		imp->il_imp = addr & 0xF;
70618132Skarels 	}
70718132Skarels 	imp->il_imp = htons(imp->il_imp);	/* network order! */
70818132Skarels }
7097170Ssam #endif
710