xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 18132)
1*18132Skarels /*	if_imp.c	6.3	85/02/28	*/
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 
1317068Sbloom #include "param.h"
1417068Sbloom #include "systm.h"
1517068Sbloom #include "mbuf.h"
1617068Sbloom #include "buf.h"
1717068Sbloom #include "protosw.h"
1817068Sbloom #include "socket.h"
1917068Sbloom #include "vmmac.h"
2017068Sbloom #include "time.h"
2117068Sbloom #include "kernel.h"
2217068Sbloom #include "errno.h"
2317068Sbloom #include "ioctl.h"
248705Sroot 
258705Sroot #include "../vax/cpu.h"
268533Sroot #include "../vax/mtpr.h"
278782Sroot #include "../vaxuba/ubareg.h"
288782Sroot #include "../vaxuba/ubavar.h"
298705Sroot 
308705Sroot #include "../net/if.h"
318705Sroot #include "../net/route.h"
3210899Ssam 
338705Sroot #include "../net/netisr.h"
348408Swnj #include "../netinet/in.h"
358408Swnj #include "../netinet/in_systm.h"
368705Sroot #include "../netinet/ip.h"
378705Sroot #include "../netinet/ip_var.h"
387199Ssam /* define IMPLEADERS here to get leader printing code */
3917068Sbloom #include "if_imp.h"
4017068Sbloom #include "if_imphost.h"
415640Sroot 
425640Sroot /*
435640Sroot  * IMP software status per interface.
445640Sroot  * (partially shared with the hardware specific module)
455640Sroot  *
465640Sroot  * Each interface is referenced by a network interface structure,
475640Sroot  * imp_if, which the routing code uses to locate the interface.
485640Sroot  * This structure contains the output queue for the interface, its
495640Sroot  * address, ...  IMP specific structures used in connecting the
505640Sroot  * IMP software modules to the hardware specific interface routines
515771Swnj  * are stored here.  The common structures are made visible to the
525771Swnj  * interface driver by passing a pointer to the hardware routine
535771Swnj  * at "attach" time.
545640Sroot  *
555640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
565640Sroot  */
575640Sroot struct imp_softc {
585640Sroot 	struct	ifnet imp_if;		/* network visible interface */
595640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
605640Sroot 	u_char	imp_state;		/* current state of IMP */
615640Sroot 	char	imp_dropcnt;		/* used during initialization */
625640Sroot } imp_softc[NIMP];
635640Sroot 
645640Sroot /*
655640Sroot  * Messages from IMP regarding why
665640Sroot  * it's going down.
675640Sroot  */
685924Sroot static char *impmessage[] = {
695640Sroot 	"in 30 seconds",
705640Sroot 	"for hardware PM",
715640Sroot 	"to reload software",
725640Sroot 	"for emergency reset"
735640Sroot };
745640Sroot 
75*18132Skarels #define HOSTDEADTIMER	10		/* How long to wait when down */
76*18132Skarels 
7713067Ssam 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;
925640Sroot 
935640Sroot 	/* UNIT COULD BE AMBIGUOUS */
945640Sroot 	ifp->if_unit = ui->ui_unit;
955640Sroot 	ifp->if_name = "imp";
966271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
979001Sroot 	ifp->if_reset = reset;
985771Swnj 	ifp->if_init = impinit;
9913067Ssam 	ifp->if_ioctl = impioctl;
1005771Swnj 	ifp->if_output = impoutput;
1015771Swnj 	/* reset is handled at the hardware level */
1025640Sroot 	if_attach(ifp);
1035640Sroot 	return ((int)&sc->imp_if);
1045640Sroot }
1055640Sroot 
1065640Sroot /*
1075640Sroot  * IMP initialization routine: call hardware module to
1085640Sroot  * setup UNIBUS resources, init state and get ready for
1095640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1105640Sroot  */
1115640Sroot impinit(unit)
1125640Sroot 	int unit;
1135640Sroot {
1146500Ssam 	int s = splimp();
1155640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
11613067Ssam 	struct sockaddr_in *sin;
1175640Sroot 
11813094Ssam 	sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
11913067Ssam 	if (in_netof(sin->sin_addr) == 0)
12013067Ssam 		return;
1215771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1225771Swnj 		sc->imp_state = IMPS_DOWN;
1236336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1246500Ssam 		splx(s);
1255771Swnj 		return;
1265771Swnj 	}
12713094Ssam 	sc->imp_if.if_flags |= IFF_RUNNING;
1285640Sroot 	sc->imp_state = IMPS_INIT;
1295771Swnj 	impnoops(sc);
1306500Ssam 	splx(s);
1315640Sroot }
1325640Sroot 
1335640Sroot struct sockproto impproto = { PF_IMPLINK };
1345645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1355645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1367199Ssam #ifdef IMPLEADERS
1377170Ssam int	impprintfs = 0;
1387199Ssam #endif
1395640Sroot 
1405640Sroot /*
1415640Sroot  * ARPAnet 1822 input routine.
1425640Sroot  * Called from hardware input interrupt routine to handle 1822
1435640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1445640Sroot  * passed to higher level protocol processors on the basis
1455640Sroot  * of link number.  Other type messages (control) are handled here.
1465640Sroot  */
1475771Swnj impinput(unit, m)
1485640Sroot 	int unit;
1495771Swnj 	register struct mbuf *m;
1505640Sroot {
1515640Sroot 	register struct imp_leader *ip;
1525640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1535640Sroot 	register struct host *hp;
1545640Sroot 	register struct ifqueue *inq;
1555771Swnj 	struct control_leader *cp;
1565640Sroot 	struct in_addr addr;
1575924Sroot 	struct mbuf *next;
1586336Ssam 	struct sockaddr_in *sin;
1595640Sroot 
1606257Sroot 	/* verify leader length. */
1615771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1625771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1635771Swnj 		return;
1645771Swnj 	cp = mtod(m, struct control_leader *);
1655771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1665771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1675771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1685771Swnj 			return;
1695640Sroot 	ip = mtod(m, struct imp_leader *);
1707199Ssam #ifdef IMPLEADERS
1717170Ssam 	if (impprintfs)
1727170Ssam 		printleader("impinput", ip);
1737199Ssam #endif
1745640Sroot 
1756257Sroot 	/* check leader type */
1765771Swnj 	if (ip->il_format != IMP_NFF) {
1775771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1785640Sroot 		goto drop;
1795771Swnj 	}
1805640Sroot 
1816588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
182*18132Skarels 		/* If not data packet, build IP addr from leader (BRL) */
183*18132Skarels 		imp_leader_to_addr( &addr, ip, &sc->imp_if );
1845640Sroot 	}
1855640Sroot 	switch (ip->il_mtype) {
1865640Sroot 
1875640Sroot 	case IMPTYPE_DATA:
1885640Sroot 		break;
1895640Sroot 
1905640Sroot 	/*
1915640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1925640Sroot 	 */
1935640Sroot 	case IMPTYPE_BADLEADER:
1945647Ssam 		/*
1955647Ssam 		 * According to 1822 document, this message
1965647Ssam 		 * will be generated in response to the
1975647Ssam 		 * first noop sent to the IMP after
1985647Ssam 		 * the host resets the IMP interface.
1995647Ssam 		 */
2005771Swnj 		if (sc->imp_state != IMPS_INIT) {
2015924Sroot 			impmsg(sc, "leader error");
2026582Ssam 			hostreset(sc->imp_if.if_net);
2035647Ssam 			impnoops(sc);
2045647Ssam 		}
2056582Ssam 		goto drop;
2065640Sroot 
2075640Sroot 	/*
2085640Sroot 	 * IMP going down.  Print message, and if not immediate,
2095640Sroot 	 * set off a timer to insure things will be reset at the
2105640Sroot 	 * appropriate time.
2115640Sroot 	 */
2125640Sroot 	case IMPTYPE_DOWN:
2136599Ssam 		if (sc->imp_state < IMPS_INIT)
2146598Ssam 			goto drop;
2155640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2165640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2176160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2185640Sroot 		}
2196160Ssam 		impmsg(sc, "going down %s",
2206160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2216582Ssam 		goto drop;
2225640Sroot 
2235640Sroot 	/*
2245640Sroot 	 * A NOP usually seen during the initialization sequence.
2255640Sroot 	 * Compare the local address with that in the message.
2265640Sroot 	 * Reset the local address notion if it doesn't match.
2275640Sroot 	 */
2286336Ssam 	case IMPTYPE_NOOP:
2295647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2305647Ssam 			sc->imp_state = IMPS_INIT;
2315647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2325647Ssam 		}
2336500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2346271Sroot 			goto drop;
2356500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
236*18132Skarels 		if( ip->il_imp != 0 )  {	/* BRL */
237*18132Skarels 			struct in_addr leader_addr;
238*18132Skarels 			imp_leader_to_addr( &leader_addr, ip, &sc->imp_if );
239*18132Skarels 			if( sin->sin_addr.s_addr != leader_addr.s_addr )  {
240*18132Skarels 				impmsg(sc, "address x%x (%d/%d)",
241*18132Skarels 					htonl( leader_addr.s_addr ),
242*18132Skarels 					(u_int)ip->il_host,
243*18132Skarels 					htons(ip->il_imp) );
244*18132Skarels 				sin->sin_addr.s_addr = leader_addr.s_addr;
245*18132Skarels 				sc->imp_if.if_host[0] = in_lnaof( leader_addr.s_addr );
246*18132Skarels 			}
2476500Ssam 		}
2485771Swnj 		sc->imp_state = IMPS_UP;
2496336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2507201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2516271Sroot 		goto drop;
2525640Sroot 
2535640Sroot 	/*
2546582Ssam 	 * RFNM or INCOMPLETE message, send next
2556582Ssam 	 * message on the q.  We could pass incomplete's
2566582Ssam 	 * up to the next level, but this currently isn't
2576582Ssam 	 * needed.
2585640Sroot 	 */
2595640Sroot 	case IMPTYPE_RFNM:
2605640Sroot 	case IMPTYPE_INCOMPLETE:
2616588Ssam 		if (hp = hostlookup(addr)) {
2626588Ssam 			if (hp->h_rfnm == 0)
2636588Ssam 				hp->h_flags &= ~HF_INUSE;
2646588Ssam 			else if (next = hostdeque(hp))
2656588Ssam 				(void) impsnd(&sc->imp_if, next);
2666588Ssam 		}
2676271Sroot 		goto drop;
2685640Sroot 
2695640Sroot 	/*
2705640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2715640Sroot 	 * awaiting transmission and release the host structure.
2725640Sroot 	 */
2735640Sroot 	case IMPTYPE_HOSTDEAD:
27411230Ssam 	case IMPTYPE_HOSTUNREACH:
2758705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
276*18132Skarels 		    hostlookup(addr), sc->imp_if);
2775859Sroot 		goto rawlinkin;
2785640Sroot 
2795640Sroot 	/*
2805640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2815640Sroot 	 * noops to the IMP to clear the interface.
2825640Sroot 	 */
28311230Ssam 	case IMPTYPE_BADDATA:
2845924Sroot 		impmsg(sc, "data error");
2856588Ssam 		if (hp = hostlookup(addr))
2865640Sroot 			hp->h_rfnm = 0;
2875640Sroot 		impnoops(sc);
2886582Ssam 		goto drop;
2895640Sroot 
2905640Sroot 	/*
2915647Ssam 	 * Interface reset.
2925640Sroot 	 */
2935640Sroot 	case IMPTYPE_RESET:
2945924Sroot 		impmsg(sc, "interface reset");
295*18132Skarels 		hostreset(sc->imp_if.if_net);	/* clear RFNM counts */
2965647Ssam 		impnoops(sc);
2976582Ssam 		goto drop;
2985640Sroot 
2995640Sroot 	default:
3005640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3016582Ssam 		goto drop;
3025640Sroot 	}
3035640Sroot 
3045640Sroot 	/*
3056257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3066257Sroot 	 * protocol routine (running at software interrupt).
3076257Sroot 	 * If this isn't a raw interface, advance pointer
3086257Sroot 	 * into mbuf past leader.
3095640Sroot 	 */
3105640Sroot 	switch (ip->il_link) {
3115640Sroot 
3125640Sroot #ifdef INET
3135640Sroot 	case IMPLINK_IP:
3145640Sroot 		m->m_len -= sizeof(struct imp_leader);
3155640Sroot 		m->m_off += sizeof(struct imp_leader);
3166261Swnj 		schednetisr(NETISR_IP);
3175640Sroot 		inq = &ipintrq;
3185640Sroot 		break;
3195640Sroot #endif
3205640Sroot 
3215640Sroot 	default:
3225868Sroot 	rawlinkin:
3235640Sroot 		impproto.sp_protocol = ip->il_link;
3246336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
325*18132Skarels 		impdst.sin_addr = sin->sin_addr;
326*18132Skarels 		imp_leader_to_addr( &impsrc.sin_addr, ip, &sc->imp_if );
3276527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3286527Ssam 		  (struct sockaddr *)&impdst);
3295640Sroot 		return;
3305640Sroot 	}
3316208Swnj 	if (IF_QFULL(inq)) {
3326208Swnj 		IF_DROP(inq);
3336208Swnj 		goto drop;
3346208Swnj 	}
3355640Sroot 	IF_ENQUEUE(inq, m);
3365640Sroot 	return;
3375640Sroot 
3385640Sroot drop:
3395640Sroot 	m_freem(m);
3405640Sroot }
3415640Sroot 
3425647Ssam /*
3435647Ssam  * Bring the IMP down after notification.
3445647Ssam  */
3455647Ssam impdown(sc)
3465647Ssam 	struct imp_softc *sc;
3475647Ssam {
34811230Ssam 	int s = splimp();
3496208Swnj 
3505647Ssam 	sc->imp_state = IMPS_DOWN;
3515924Sroot 	impmsg(sc, "marked down");
3526588Ssam 	hostreset(sc->imp_if.if_net);
3536582Ssam 	if_down(&sc->imp_if);
35411230Ssam 	splx(s);
3555647Ssam }
3565647Ssam 
3575640Sroot /*VARARGS*/
358*18132Skarels impmsg(sc, fmt, a1, a2, a3)
3595640Sroot 	struct imp_softc *sc;
3605640Sroot 	char *fmt;
3616160Ssam 	u_int a1;
3625640Sroot {
3636208Swnj 
3645640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
365*18132Skarels 	printf(fmt, a1, a2, a3);
3665640Sroot 	printf("\n");
3675640Sroot }
3685640Sroot 
3695640Sroot /*
3706582Ssam  * Process an IMP "error" message, passing this
3716582Ssam  * up to the higher level protocol.
3726582Ssam  */
373*18132Skarels impnotify(what, cp, hp, ifp)
3746582Ssam 	int what;
3756582Ssam 	struct control_leader *cp;
3766582Ssam 	struct host *hp;
377*18132Skarels 	struct ifnet *ifp;		/* BRL */
3786582Ssam {
3796582Ssam 	struct in_addr in;
3806582Ssam 
381*18132Skarels 	imp_leader_to_addr( &in, (struct imp_leader *) cp, ifp );  /* BRL */
382*18132Skarels 
3836582Ssam 	if (cp->dl_link != IMPLINK_IP)
3846582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3856582Ssam 	else
3866582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3876588Ssam 	if (hp) {
3886588Ssam 		hp->h_flags |= (1 << what);
3896582Ssam 		hostfree(hp);
390*18132Skarels 		hp->h_timer = HOSTDEADTIMER;
3916588Ssam 	}
3926582Ssam }
3936582Ssam 
3946582Ssam /*
3955640Sroot  * ARPAnet 1822 output routine.
3965640Sroot  * Called from higher level protocol routines to set up messages for
3975640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3985640Sroot  * enqueue the message for this IMP's hardware driver.
3995640Sroot  */
4006336Ssam impoutput(ifp, m0, dst)
4015640Sroot 	register struct ifnet *ifp;
4025640Sroot 	struct mbuf *m0;
4036336Ssam 	struct sockaddr *dst;
4045640Sroot {
4055640Sroot 	register struct imp_leader *imp;
4065640Sroot 	register struct mbuf *m = m0;
4078782Sroot 	int dhost, dimp, dlink, len, dnet;
4086504Ssam 	int error = 0;
4095640Sroot 
4105640Sroot 	/*
4115640Sroot 	 * Don't even try if the IMP is unavailable.
4125640Sroot 	 */
4136504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4146504Ssam 		error = ENETDOWN;
4155647Ssam 		goto drop;
4166504Ssam 	}
4175640Sroot 
4186336Ssam 	switch (dst->sa_family) {
4195640Sroot 
4205640Sroot #ifdef INET
4216336Ssam 	case AF_INET: {
4226336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4236336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4245640Sroot 
4256336Ssam 		dhost = sin->sin_addr.s_host;
4266336Ssam 		dimp = sin->sin_addr.s_impno;
4275640Sroot 		dlink = IMPLINK_IP;
4286244Sroot 		dnet = 0;
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;
464*18132Skarels 	imp_addr_to_leader(imp,
465*18132Skarels 		((struct sockaddr_in *) dst)->sin_addr.s_addr ); /* BRL */
466*18132Skarels 	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 
501*18132Skarels 		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;
506*18132Skarels 			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) {
515*18132Skarels #ifndef NORFNM					/* BRL */
516*18132Skarels 			if (hp->h_rfnm < 8)
517*18132Skarels #endif
518*18132Skarels 			{
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;
5645771Swnj 	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 {
58613067Ssam 	struct ifreq *ifr = (struct ifreq *)data;
58713067Ssam 	struct sockaddr_in *sin;
58813067Ssam 	int s = splimp(), error = 0;
58913067Ssam 
59013067Ssam 	switch (cmd) {
59113067Ssam 
59213067Ssam 	case SIOCSIFADDR:
59313067Ssam 		if (ifp->if_flags & IFF_RUNNING)
59413067Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
59513067Ssam 		sin = (struct sockaddr_in *)&ifr->ifr_addr;
59613067Ssam 		ifp->if_net = in_netof(sin->sin_addr);
59713067Ssam 		sin = (struct sockaddr_in *)&ifp->if_addr;
59813067Ssam 		sin->sin_family = AF_INET;
59913067Ssam 		/* host number filled in already, or filled in later */
60013067Ssam 		sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
60113067Ssam 		if (ifp->if_flags & IFF_RUNNING)
60213067Ssam 			if_rtinit(ifp, RTF_UP);
60313067Ssam 		else
60413067Ssam 			impinit(ifp->if_unit);
60513067Ssam 		break;
60613067Ssam 
60713067Ssam 	default:
60813067Ssam 		error = EINVAL;
60913067Ssam 	}
61013067Ssam 	splx(s);
61113067Ssam 	return (error);
61213067Ssam }
61313067Ssam 
6147170Ssam #ifdef IMPLEADERS
6157170Ssam printleader(routine, ip)
6167170Ssam 	char *routine;
6177170Ssam 	register struct imp_leader *ip;
6187170Ssam {
6197170Ssam 	printf("%s: ", routine);
6207170Ssam 	printbyte((char *)ip, 12);
6217170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6227170Ssam 		ip->il_flags);
6237170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6247170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6257170Ssam 	else
6267170Ssam 		printf("%x,", ip->il_mtype);
6277170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6287170Ssam 		ntohs(ip->il_imp));
6297170Ssam 	if (ip->il_link == IMPLINK_IP)
6307170Ssam 		printf("ip,");
6317170Ssam 	else
6327170Ssam 		printf("%x,", ip->il_link);
6337170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6347170Ssam }
6357170Ssam 
6367170Ssam printbyte(cp, n)
6377170Ssam 	register char *cp;
6387170Ssam 	int n;
6397170Ssam {
6407170Ssam 	register i, j, c;
6417170Ssam 
6427170Ssam 	for (i=0; i<n; i++) {
6437170Ssam 		c = *cp++;
6447170Ssam 		for (j=0; j<2; j++)
6457170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6467170Ssam 		putchar(' ');
6477170Ssam 	}
6487170Ssam 	putchar('\n');
6497170Ssam }
6505640Sroot #endif
651*18132Skarels 
652*18132Skarels /*
653*18132Skarels  * Routine to convert from IMP Leader to InterNet Address.
654*18132Skarels  *
655*18132Skarels  * This procedure is necessary because IMPs may be assigned Class A, B, or C
656*18132Skarels  * network numbers, but only have 8 bits in the leader to reflect the
657*18132Skarels  * IMP "network number".  The strategy is to take the network number from
658*18132Skarels  * the ifnet structure, and blend in the host-on-imp and imp-on-net numbers
659*18132Skarels  * from the leader.
660*18132Skarels  *
661*18132Skarels  * There is no support for "Logical Hosts".
662*18132Skarels  *
663*18132Skarels  * Class A:	Net.Host.0.Imp
664*18132Skarels  * Class B:	Net.net.Host.Imp
665*18132Skarels  * Class C:	Net.net.net.(Host4|Imp4)
666*18132Skarels  */
667*18132Skarels imp_leader_to_addr( ap, ip, ifp )
668*18132Skarels register struct in_addr *ap;
669*18132Skarels register struct imp_leader *ip;
670*18132Skarels register struct ifnet *ifp;
671*18132Skarels {
672*18132Skarels 	register long final;
673*18132Skarels 	register struct sockaddr_in *sin;
674*18132Skarels 	int imp = htons(ip->il_imp);
675*18132Skarels 
676*18132Skarels 	sin = (struct sockaddr_in *) (&ifp->if_addr);
677*18132Skarels 	final = htonl( sin->sin_addr.s_addr );		/* host order */
678*18132Skarels 
679*18132Skarels 	if( IN_CLASSA( final ) )  {
680*18132Skarels 		final &= IN_CLASSA_NET;
681*18132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<16);
682*18132Skarels 	} else if( IN_CLASSB( final ) )  {
683*18132Skarels 		final &= IN_CLASSB_NET;
684*18132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<8);
685*18132Skarels 	} else {
686*18132Skarels 		final &= IN_CLASSC_NET;
687*18132Skarels 		final |= (imp & 0x0F) | ((ip->il_host & 0x0F)<<4);
688*18132Skarels 	}
689*18132Skarels 	ap->s_addr = htonl( final );
690*18132Skarels }
691*18132Skarels 
692*18132Skarels /*
693*18132Skarels  * Function to take InterNet address and fill in IMP leader fields.
694*18132Skarels  */
695*18132Skarels imp_addr_to_leader( imp, a )
696*18132Skarels register struct imp_leader *imp;
697*18132Skarels long a;
698*18132Skarels {
699*18132Skarels 	register long addr = htonl( a );		/* host order */
700*18132Skarels 
701*18132Skarels 	imp->il_network = 0;	/* !! */
702*18132Skarels 
703*18132Skarels 	if( IN_CLASSA( addr ) )  {
704*18132Skarels 		imp->il_host = ((addr>>16) & 0xFF);
705*18132Skarels 		imp->il_imp = addr & 0xFF;
706*18132Skarels 	} else if ( IN_CLASSB( addr ) )  {
707*18132Skarels 		imp->il_host = ((addr>>8) & 0xFF);
708*18132Skarels 		imp->il_imp = addr & 0xFF;
709*18132Skarels 	} else {
710*18132Skarels 		imp->il_host = ((addr>>4) & 0xF);
711*18132Skarels 		imp->il_imp = addr & 0xF;
712*18132Skarels 	}
713*18132Skarels 	imp->il_imp = htons(imp->il_imp);	/* network order! */
714*18132Skarels }
7157170Ssam #endif
716