xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 21114)
1*21114Skarels /*	if_imp.c	6.5	85/05/27	*/
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"
3618412Skarels #include "../netinet/in_var.h"
378705Sroot #include "../netinet/ip.h"
388705Sroot #include "../netinet/ip_var.h"
397199Ssam /* define IMPLEADERS here to get leader printing code */
4017068Sbloom #include "if_imp.h"
4117068Sbloom #include "if_imphost.h"
425640Sroot 
435640Sroot /*
445640Sroot  * IMP software status per interface.
455640Sroot  * (partially shared with the hardware specific module)
465640Sroot  *
475640Sroot  * Each interface is referenced by a network interface structure,
485640Sroot  * imp_if, which the routing code uses to locate the interface.
495640Sroot  * This structure contains the output queue for the interface, its
505640Sroot  * address, ...  IMP specific structures used in connecting the
515640Sroot  * IMP software modules to the hardware specific interface routines
525771Swnj  * are stored here.  The common structures are made visible to the
535771Swnj  * interface driver by passing a pointer to the hardware routine
545771Swnj  * at "attach" time.
555640Sroot  *
565640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
575640Sroot  */
585640Sroot struct imp_softc {
595640Sroot 	struct	ifnet imp_if;		/* network visible interface */
605640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
615640Sroot 	u_char	imp_state;		/* current state of IMP */
625640Sroot 	char	imp_dropcnt;		/* used during initialization */
635640Sroot } imp_softc[NIMP];
645640Sroot 
655640Sroot /*
665640Sroot  * Messages from IMP regarding why
675640Sroot  * it's going down.
685640Sroot  */
695924Sroot static char *impmessage[] = {
705640Sroot 	"in 30 seconds",
715640Sroot 	"for hardware PM",
725640Sroot 	"to reload software",
735640Sroot 	"for emergency reset"
745640Sroot };
755640Sroot 
7618132Skarels #define HOSTDEADTIMER	10		/* How long to wait when down */
7718132Skarels 
7813067Ssam int	impdown(), impinit(), impioctl(), impoutput();
795771Swnj 
805640Sroot /*
815640Sroot  * IMP attach routine.  Called from hardware device attach routine
825640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
835640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
845640Sroot  * structures.  This is then used by the device's attach routine
855640Sroot  * set up its back pointers.
865640Sroot  */
878815Sroot impattach(ui, reset)
885640Sroot 	struct uba_device *ui;
898815Sroot 	int (*reset)();
905640Sroot {
915640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
925640Sroot 	register struct ifnet *ifp = &sc->imp_if;
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;
10013067Ssam 	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];
1175640Sroot 
11818412Skarels 	if (sc->imp_if.if_addrlist == 0)
11913067Ssam 		return;
1205771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1215771Swnj 		sc->imp_state = IMPS_DOWN;
1226336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1236500Ssam 		splx(s);
1245771Swnj 		return;
1255771Swnj 	}
1265640Sroot 	sc->imp_state = IMPS_INIT;
1275771Swnj 	impnoops(sc);
1286500Ssam 	splx(s);
1295640Sroot }
1305640Sroot 
1315640Sroot struct sockproto impproto = { PF_IMPLINK };
1325645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1335645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1347199Ssam #ifdef IMPLEADERS
1357170Ssam int	impprintfs = 0;
1367199Ssam #endif
1375640Sroot 
1385640Sroot /*
1395640Sroot  * ARPAnet 1822 input routine.
1405640Sroot  * Called from hardware input interrupt routine to handle 1822
1415640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1425640Sroot  * passed to higher level protocol processors on the basis
1435640Sroot  * of link number.  Other type messages (control) are handled here.
1445640Sroot  */
1455771Swnj impinput(unit, m)
1465640Sroot 	int unit;
1475771Swnj 	register struct mbuf *m;
1485640Sroot {
1495640Sroot 	register struct imp_leader *ip;
1505640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1515640Sroot 	register struct host *hp;
1525640Sroot 	register struct ifqueue *inq;
1535771Swnj 	struct control_leader *cp;
1545640Sroot 	struct in_addr addr;
1555924Sroot 	struct mbuf *next;
1566336Ssam 	struct sockaddr_in *sin;
1575640Sroot 
1586257Sroot 	/* verify leader length. */
1595771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1605771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1615771Swnj 		return;
1625771Swnj 	cp = mtod(m, struct control_leader *);
1635771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1645771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1655771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1665771Swnj 			return;
1675640Sroot 	ip = mtod(m, struct imp_leader *);
1687199Ssam #ifdef IMPLEADERS
1697170Ssam 	if (impprintfs)
1707170Ssam 		printleader("impinput", ip);
1717199Ssam #endif
1725640Sroot 
1736257Sroot 	/* check leader type */
1745771Swnj 	if (ip->il_format != IMP_NFF) {
1755771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1765640Sroot 		goto drop;
1775771Swnj 	}
1785640Sroot 
1796588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
18018132Skarels 		/* If not data packet, build IP addr from leader (BRL) */
18118412Skarels 		imp_leader_to_addr(&addr, ip, &sc->imp_if);
1825640Sroot 	}
1835640Sroot 	switch (ip->il_mtype) {
1845640Sroot 
1855640Sroot 	case IMPTYPE_DATA:
1865640Sroot 		break;
1875640Sroot 
1885640Sroot 	/*
1895640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1905640Sroot 	 */
1915640Sroot 	case IMPTYPE_BADLEADER:
1925647Ssam 		/*
1935647Ssam 		 * According to 1822 document, this message
1945647Ssam 		 * will be generated in response to the
1955647Ssam 		 * first noop sent to the IMP after
1965647Ssam 		 * the host resets the IMP interface.
1975647Ssam 		 */
1985771Swnj 		if (sc->imp_state != IMPS_INIT) {
1995924Sroot 			impmsg(sc, "leader error");
20018412Skarels 			hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
2015647Ssam 			impnoops(sc);
2025647Ssam 		}
2036582Ssam 		goto drop;
2045640Sroot 
2055640Sroot 	/*
2065640Sroot 	 * IMP going down.  Print message, and if not immediate,
2075640Sroot 	 * set off a timer to insure things will be reset at the
2085640Sroot 	 * appropriate time.
2095640Sroot 	 */
2105640Sroot 	case IMPTYPE_DOWN:
2116599Ssam 		if (sc->imp_state < IMPS_INIT)
2126598Ssam 			goto drop;
2135640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2145640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2156160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2165640Sroot 		}
2176160Ssam 		impmsg(sc, "going down %s",
2186160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2196582Ssam 		goto drop;
2205640Sroot 
2215640Sroot 	/*
2225640Sroot 	 * A NOP usually seen during the initialization sequence.
2235640Sroot 	 * Compare the local address with that in the message.
2245640Sroot 	 * Reset the local address notion if it doesn't match.
2255640Sroot 	 */
2266336Ssam 	case IMPTYPE_NOOP:
2275647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2285647Ssam 			sc->imp_state = IMPS_INIT;
2295647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2305647Ssam 		}
2316500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2326271Sroot 			goto drop;
23318412Skarels 		sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
23418412Skarels 		if (ip->il_imp != 0) {	/* BRL */
23518132Skarels 			struct in_addr leader_addr;
23618412Skarels 			imp_leader_to_addr(&leader_addr, ip, &sc->imp_if);
23718412Skarels 			if (sin->sin_addr.s_addr != leader_addr.s_addr) {
23818412Skarels 				impmsg(sc, "address reset to x%x (%d/%d)",
23918412Skarels 					htonl(leader_addr.s_addr),
24018132Skarels 					(u_int)ip->il_host,
24118412Skarels 					htons(ip->il_imp));
24218132Skarels 				sin->sin_addr.s_addr = leader_addr.s_addr;
24318132Skarels 			}
2446500Ssam 		}
2455771Swnj 		sc->imp_state = IMPS_UP;
2466336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2476271Sroot 		goto drop;
2485640Sroot 
2495640Sroot 	/*
2506582Ssam 	 * RFNM or INCOMPLETE message, send next
2516582Ssam 	 * message on the q.  We could pass incomplete's
2526582Ssam 	 * up to the next level, but this currently isn't
2536582Ssam 	 * needed.
2545640Sroot 	 */
2555640Sroot 	case IMPTYPE_RFNM:
2565640Sroot 	case IMPTYPE_INCOMPLETE:
2576588Ssam 		if (hp = hostlookup(addr)) {
2586588Ssam 			if (hp->h_rfnm == 0)
2596588Ssam 				hp->h_flags &= ~HF_INUSE;
2606588Ssam 			else if (next = hostdeque(hp))
2616588Ssam 				(void) impsnd(&sc->imp_if, next);
2626588Ssam 		}
2636271Sroot 		goto drop;
2645640Sroot 
2655640Sroot 	/*
2665640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2675640Sroot 	 * awaiting transmission and release the host structure.
2685640Sroot 	 */
2695640Sroot 	case IMPTYPE_HOSTDEAD:
27011230Ssam 	case IMPTYPE_HOSTUNREACH:
2718705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
27218412Skarels 		    hostlookup(addr), &sc->imp_if);
2735859Sroot 		goto rawlinkin;
2745640Sroot 
2755640Sroot 	/*
2765640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2775640Sroot 	 * noops to the IMP to clear the interface.
2785640Sroot 	 */
27911230Ssam 	case IMPTYPE_BADDATA:
2805924Sroot 		impmsg(sc, "data error");
2816588Ssam 		if (hp = hostlookup(addr))
2825640Sroot 			hp->h_rfnm = 0;
2835640Sroot 		impnoops(sc);
2846582Ssam 		goto drop;
2855640Sroot 
2865640Sroot 	/*
2875647Ssam 	 * Interface reset.
2885640Sroot 	 */
2895640Sroot 	case IMPTYPE_RESET:
2905924Sroot 		impmsg(sc, "interface reset");
29118412Skarels 		/* clear RFNM counts */
29218412Skarels 		hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
2935647Ssam 		impnoops(sc);
2946582Ssam 		goto drop;
2955640Sroot 
2965640Sroot 	default:
2975640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
2986582Ssam 		goto drop;
2995640Sroot 	}
3005640Sroot 
3015640Sroot 	/*
3026257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3036257Sroot 	 * protocol routine (running at software interrupt).
3046257Sroot 	 * If this isn't a raw interface, advance pointer
3056257Sroot 	 * into mbuf past leader.
3065640Sroot 	 */
3075640Sroot 	switch (ip->il_link) {
3085640Sroot 
3095640Sroot #ifdef INET
3105640Sroot 	case IMPLINK_IP:
3115640Sroot 		m->m_len -= sizeof(struct imp_leader);
3125640Sroot 		m->m_off += sizeof(struct imp_leader);
3136261Swnj 		schednetisr(NETISR_IP);
3145640Sroot 		inq = &ipintrq;
3155640Sroot 		break;
3165640Sroot #endif
3175640Sroot 
3185640Sroot 	default:
3195868Sroot 	rawlinkin:
3205640Sroot 		impproto.sp_protocol = ip->il_link;
32118412Skarels 		sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
32218132Skarels 		impdst.sin_addr = sin->sin_addr;
32318412Skarels 		imp_leader_to_addr(&impsrc.sin_addr, ip, &sc->imp_if);
3246527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3256527Ssam 		  (struct sockaddr *)&impdst);
3265640Sroot 		return;
3275640Sroot 	}
3286208Swnj 	if (IF_QFULL(inq)) {
3296208Swnj 		IF_DROP(inq);
3306208Swnj 		goto drop;
3316208Swnj 	}
3325640Sroot 	IF_ENQUEUE(inq, m);
3335640Sroot 	return;
3345640Sroot 
3355640Sroot drop:
3365640Sroot 	m_freem(m);
3375640Sroot }
3385640Sroot 
3395647Ssam /*
3405647Ssam  * Bring the IMP down after notification.
3415647Ssam  */
3425647Ssam impdown(sc)
3435647Ssam 	struct imp_softc *sc;
3445647Ssam {
34511230Ssam 	int s = splimp();
3466208Swnj 
3475647Ssam 	sc->imp_state = IMPS_DOWN;
3485924Sroot 	impmsg(sc, "marked down");
34918412Skarels 	hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
3506582Ssam 	if_down(&sc->imp_if);
35111230Ssam 	splx(s);
3525647Ssam }
3535647Ssam 
3545640Sroot /*VARARGS*/
35518132Skarels impmsg(sc, fmt, a1, a2, a3)
3565640Sroot 	struct imp_softc *sc;
3575640Sroot 	char *fmt;
3586160Ssam 	u_int a1;
3595640Sroot {
3606208Swnj 
3615640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
36218132Skarels 	printf(fmt, a1, a2, a3);
3635640Sroot 	printf("\n");
3645640Sroot }
3655640Sroot 
3665640Sroot /*
3676582Ssam  * Process an IMP "error" message, passing this
3686582Ssam  * up to the higher level protocol.
3696582Ssam  */
37018132Skarels impnotify(what, cp, hp, ifp)
3716582Ssam 	int what;
3726582Ssam 	struct control_leader *cp;
3736582Ssam 	struct host *hp;
37418132Skarels 	struct ifnet *ifp;		/* BRL */
3756582Ssam {
3766582Ssam 	struct in_addr in;
3776582Ssam 
37818412Skarels 	imp_leader_to_addr(&in, (struct imp_leader *)cp, ifp);  /* BRL */
37918132Skarels 
3806582Ssam 	if (cp->dl_link != IMPLINK_IP)
3816582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3826582Ssam 	else
383*21114Skarels 		pfctlinput(what, (caddr_t)&in);
3846588Ssam 	if (hp) {
3856588Ssam 		hp->h_flags |= (1 << what);
3866582Ssam 		hostfree(hp);
38718132Skarels 		hp->h_timer = HOSTDEADTIMER;
3886588Ssam 	}
3896582Ssam }
3906582Ssam 
3916582Ssam /*
3925640Sroot  * ARPAnet 1822 output routine.
3935640Sroot  * Called from higher level protocol routines to set up messages for
3945640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3955640Sroot  * enqueue the message for this IMP's hardware driver.
3965640Sroot  */
3976336Ssam impoutput(ifp, m0, dst)
3985640Sroot 	register struct ifnet *ifp;
3995640Sroot 	struct mbuf *m0;
4006336Ssam 	struct sockaddr *dst;
4015640Sroot {
4025640Sroot 	register struct imp_leader *imp;
4035640Sroot 	register struct mbuf *m = m0;
40418412Skarels 	int dlink, len;
4056504Ssam 	int error = 0;
4065640Sroot 
4075640Sroot 	/*
4085640Sroot 	 * Don't even try if the IMP is unavailable.
4095640Sroot 	 */
4106504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4116504Ssam 		error = ENETDOWN;
4125647Ssam 		goto drop;
4136504Ssam 	}
4145640Sroot 
4156336Ssam 	switch (dst->sa_family) {
4165640Sroot 
4175640Sroot #ifdef INET
4186336Ssam 	case AF_INET: {
4196336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4206336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4215640Sroot 
4225640Sroot 		dlink = IMPLINK_IP;
4236160Ssam 		len = ntohs((u_short)ip->ip_len);
4245640Sroot 		break;
4255640Sroot 	}
4265640Sroot #endif
4276336Ssam 	case AF_IMPLINK:
4285640Sroot 		goto leaderexists;
4295640Sroot 
4305640Sroot 	default:
4316336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4326336Ssam 			dst->sa_family);
4336504Ssam 		error = EAFNOSUPPORT;
4345647Ssam 		goto drop;
4355640Sroot 	}
4365640Sroot 
4375640Sroot 	/*
4385640Sroot 	 * Add IMP leader.  If there's not enough space in the
4395640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4405640Sroot 	 * drop this sucker.
4415640Sroot 	 */
4425640Sroot 	if (m->m_off > MMAXOFF ||
4435640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4449645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4456504Ssam 		if (m == 0) {
4466504Ssam 			error = ENOBUFS;
4475647Ssam 			goto drop;
4486504Ssam 		}
4495640Sroot 		m->m_next = m0;
4505640Sroot 		m->m_len = sizeof(struct imp_leader);
4515640Sroot 	} else {
4525640Sroot 		m->m_off -= sizeof(struct imp_leader);
4535640Sroot 		m->m_len += sizeof(struct imp_leader);
4545640Sroot 	}
4555640Sroot 	imp = mtod(m, struct imp_leader *);
4565640Sroot 	imp->il_format = IMP_NFF;
4575859Sroot 	imp->il_mtype = IMPTYPE_DATA;
45818132Skarels 	imp_addr_to_leader(imp,
45918412Skarels 		((struct sockaddr_in *)dst)->sin_addr.s_addr); /* BRL */
46018412Skarels 	imp->il_length = htons((u_short)len << 3);		/* BRL */
4615640Sroot 	imp->il_link = dlink;
4625859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4635640Sroot 
4645640Sroot leaderexists:
4655640Sroot 	return (impsnd(ifp, m));
4665647Ssam drop:
4675647Ssam 	m_freem(m0);
4686504Ssam 	return (error);
4695640Sroot }
4705640Sroot 
4715640Sroot /*
4725640Sroot  * Put a message on an interface's output queue.
4735640Sroot  * Perform RFNM counting: no more than 8 message may be
4745640Sroot  * in flight to any one host.
4755640Sroot  */
4765640Sroot impsnd(ifp, m)
4775640Sroot 	struct ifnet *ifp;
4785640Sroot 	struct mbuf *m;
4795640Sroot {
4805640Sroot 	register struct imp_leader *ip;
4815640Sroot 	register struct host *hp;
4825640Sroot 	struct impcb *icp;
4836588Ssam 	int s, error;
4845640Sroot 
4855640Sroot 	ip = mtod(m, struct imp_leader *);
4865640Sroot 
4875640Sroot 	/*
4885640Sroot 	 * Do RFNM counting for data messages
4895640Sroot 	 * (no more than 8 outstanding to any host)
4905640Sroot 	 */
4916588Ssam 	s = splimp();
4925640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
4935640Sroot 		struct in_addr addr;
4945640Sroot 
49518412Skarels 		imp_leader_to_addr(&addr, ip, ifp);	/* BRL */
4965771Swnj 		if ((hp = hostlookup(addr)) == 0)
4975771Swnj 			hp = hostenter(addr);
4986588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
4996607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
50018132Skarels 			hp->h_timer = HOSTDEADTIMER;
5016588Ssam 			hp->h_flags &= ~HF_INUSE;
5026588Ssam 			goto bad;
5036588Ssam 		}
5045640Sroot 
5055640Sroot 		/*
5065647Ssam 		 * If IMP would block, queue until RFNM
5075640Sroot 		 */
5085640Sroot 		if (hp) {
50918132Skarels #ifndef NORFNM					/* BRL */
51018132Skarels 			if (hp->h_rfnm < 8)
51118132Skarels #endif
51218132Skarels 			{
5135640Sroot 				hp->h_rfnm++;
5145640Sroot 				goto enque;
5155640Sroot 			}
5166095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5176095Swnj 				HOST_ENQUE(hp, m);
5186095Swnj 				goto start;
5196095Swnj 			}
5205640Sroot 		}
5216588Ssam 		error = ENOBUFS;
5226588Ssam 		goto bad;
5235640Sroot 	}
5245640Sroot enque:
5256208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5266208Swnj 		IF_DROP(&ifp->if_snd);
5276588Ssam 		error = ENOBUFS;
5286588Ssam bad:
5296208Swnj 		m_freem(m);
5306588Ssam 		splx(s);
5316588Ssam 		return (error);
5326208Swnj 	}
5335640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5346095Swnj start:
5355640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5365640Sroot 	if (icp->ic_oactive == 0)
5375640Sroot 		(*icp->ic_start)(ifp->if_unit);
5386630Ssam 	splx(s);
5396504Ssam 	return (0);
5405640Sroot }
5415640Sroot 
5425640Sroot /*
5435640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5445640Sroot  * Part of host-IMP initialization procedure.
5455640Sroot  * (Should return success/failure, but noone knows
5465640Sroot  * what to do with this, so why bother?)
5476818Ssam  * This routine is always called at splimp, so we don't
5486818Ssam  * protect the call to IF_PREPEND.
5495640Sroot  */
5505640Sroot impnoops(sc)
5515640Sroot 	register struct imp_softc *sc;
5525640Sroot {
5535640Sroot 	register i;
5545640Sroot 	register struct mbuf *m;
5555771Swnj 	register struct control_leader *cp;
5565640Sroot 
5575640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
55818412Skarels 	for (i = 0; i < IMP_DROPCNT + 1; i++) {
5599645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
5605640Sroot 			return;
5615771Swnj 		m->m_len = sizeof(struct control_leader);
5625771Swnj 		cp = mtod(m, struct control_leader *);
5635771Swnj 		cp->dl_format = IMP_NFF;
5645771Swnj                 cp->dl_link = i;
5655771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5665640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5675640Sroot 	}
5685640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5695640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5705640Sroot }
5717170Ssam 
57213067Ssam /*
57313067Ssam  * Process an ioctl request.
57413067Ssam  */
57513067Ssam impioctl(ifp, cmd, data)
57613067Ssam 	register struct ifnet *ifp;
57713067Ssam 	int cmd;
57813067Ssam 	caddr_t data;
57913067Ssam {
58018412Skarels 	struct ifaddr *ifa = (struct ifaddr *) data;
58113067Ssam 	int s = splimp(), error = 0;
58213067Ssam 
58313067Ssam 	switch (cmd) {
58413067Ssam 
58513067Ssam 	case SIOCSIFADDR:
58618412Skarels 		if (ifa->ifa_addr.sa_family != AF_INET) {
58718412Skarels 			error = EINVAL;
58818412Skarels 			break;
58918412Skarels 		}
59018412Skarels 		if ((ifp->if_flags & IFF_RUNNING) == 0)
59113067Ssam 			impinit(ifp->if_unit);
59213067Ssam 		break;
59313067Ssam 
59413067Ssam 	default:
59513067Ssam 		error = EINVAL;
59613067Ssam 	}
59713067Ssam 	splx(s);
59813067Ssam 	return (error);
59913067Ssam }
60013067Ssam 
6017170Ssam #ifdef IMPLEADERS
6027170Ssam printleader(routine, ip)
6037170Ssam 	char *routine;
6047170Ssam 	register struct imp_leader *ip;
6057170Ssam {
6067170Ssam 	printf("%s: ", routine);
6077170Ssam 	printbyte((char *)ip, 12);
6087170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6097170Ssam 		ip->il_flags);
6107170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6117170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6127170Ssam 	else
6137170Ssam 		printf("%x,", ip->il_mtype);
6147170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6157170Ssam 		ntohs(ip->il_imp));
6167170Ssam 	if (ip->il_link == IMPLINK_IP)
6177170Ssam 		printf("ip,");
6187170Ssam 	else
6197170Ssam 		printf("%x,", ip->il_link);
6207170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6217170Ssam }
6227170Ssam 
6237170Ssam printbyte(cp, n)
6247170Ssam 	register char *cp;
6257170Ssam 	int n;
6267170Ssam {
6277170Ssam 	register i, j, c;
6287170Ssam 
6297170Ssam 	for (i=0; i<n; i++) {
6307170Ssam 		c = *cp++;
6317170Ssam 		for (j=0; j<2; j++)
6327170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6337170Ssam 		putchar(' ');
6347170Ssam 	}
6357170Ssam 	putchar('\n');
6367170Ssam }
6375640Sroot #endif
63818132Skarels 
63918132Skarels /*
64018132Skarels  * Routine to convert from IMP Leader to InterNet Address.
64118132Skarels  *
64218132Skarels  * This procedure is necessary because IMPs may be assigned Class A, B, or C
64318132Skarels  * network numbers, but only have 8 bits in the leader to reflect the
64418132Skarels  * IMP "network number".  The strategy is to take the network number from
64518132Skarels  * the ifnet structure, and blend in the host-on-imp and imp-on-net numbers
64618132Skarels  * from the leader.
64718132Skarels  *
64818132Skarels  * There is no support for "Logical Hosts".
64918132Skarels  *
65018132Skarels  * Class A:	Net.Host.0.Imp
65118132Skarels  * Class B:	Net.net.Host.Imp
65218132Skarels  * Class C:	Net.net.net.(Host4|Imp4)
65318132Skarels  */
65418412Skarels imp_leader_to_addr(ap, ip, ifp)
65518412Skarels 	struct in_addr *ap;
65618412Skarels 	register struct imp_leader *ip;
65718412Skarels 	struct ifnet *ifp;
65818132Skarels {
65918132Skarels 	register long final;
66018412Skarels 	struct in_ifaddr *ia;
66118132Skarels 	register struct sockaddr_in *sin;
66218132Skarels 	int imp = htons(ip->il_imp);
66318132Skarels 
66418412Skarels 	sin = (struct sockaddr_in *)(&ifp->if_addrlist->ifa_addr);
66518412Skarels 	final = htonl(sin->sin_addr.s_addr);
66618132Skarels 
66718412Skarels 	if (IN_CLASSA(final)) {
66818132Skarels 		final &= IN_CLASSA_NET;
66918132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<16);
67018412Skarels 	} else if (IN_CLASSB(final)) {
67118132Skarels 		final &= IN_CLASSB_NET;
67218132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<8);
67318132Skarels 	} else {
67418132Skarels 		final &= IN_CLASSC_NET;
67518132Skarels 		final |= (imp & 0x0F) | ((ip->il_host & 0x0F)<<4);
67618132Skarels 	}
67718412Skarels 	ap->s_addr = htonl(final);
67818132Skarels }
67918132Skarels 
68018132Skarels /*
68118132Skarels  * Function to take InterNet address and fill in IMP leader fields.
68218132Skarels  */
68318412Skarels imp_addr_to_leader(imp, a)
68418412Skarels 	register struct imp_leader *imp;
68518412Skarels 	long a;
68618132Skarels {
68718412Skarels 	register long addr = htonl(a);		/* host order */
68818132Skarels 
68918132Skarels 	imp->il_network = 0;	/* !! */
69018132Skarels 
69118412Skarels 	if (IN_CLASSA(addr)) {
69218132Skarels 		imp->il_host = ((addr>>16) & 0xFF);
69318132Skarels 		imp->il_imp = addr & 0xFF;
69418412Skarels 	} else if (IN_CLASSB(addr)) {
69518132Skarels 		imp->il_host = ((addr>>8) & 0xFF);
69618132Skarels 		imp->il_imp = addr & 0xFF;
69718132Skarels 	} else {
69818132Skarels 		imp->il_host = ((addr>>4) & 0xF);
69918132Skarels 		imp->il_imp = addr & 0xF;
70018132Skarels 	}
70118132Skarels 	imp->il_imp = htons(imp->il_imp);	/* network order! */
70218132Skarels }
7037170Ssam #endif
704