xref: /csrg-svn/sys/deprecated/netimp/if_imp.c (revision 25409)
123166Smckusick /*
223166Smckusick  * Copyright (c) 1982 Regents of the University of California.
323166Smckusick  * All rights reserved.  The Berkeley software License Agreement
423166Smckusick  * specifies the terms and conditions for redistribution.
523166Smckusick  *
6*25409Skarels  *	@(#)if_imp.c	6.8 (Berkeley) 11/06/85
723166Smckusick  */
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 
7124777Skarels struct ifqueue impintrq;
7224777Skarels 
735640Sroot /*
745640Sroot  * Messages from IMP regarding why
755640Sroot  * it's going down.
765640Sroot  */
775924Sroot static char *impmessage[] = {
785640Sroot 	"in 30 seconds",
795640Sroot 	"for hardware PM",
805640Sroot 	"to reload software",
815640Sroot 	"for emergency reset"
825640Sroot };
835640Sroot 
8418132Skarels #define HOSTDEADTIMER	10		/* How long to wait when down */
8518132Skarels 
8613067Ssam int	impdown(), impinit(), impioctl(), impoutput();
875771Swnj 
885640Sroot /*
895640Sroot  * IMP attach routine.  Called from hardware device attach routine
905640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
915640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
925640Sroot  * structures.  This is then used by the device's attach routine
935640Sroot  * set up its back pointers.
945640Sroot  */
958815Sroot impattach(ui, reset)
965640Sroot 	struct uba_device *ui;
978815Sroot 	int (*reset)();
985640Sroot {
9924777Skarels 	struct imp_softc *sc;
10024777Skarels 	register struct ifnet *ifp;
1015640Sroot 
10224777Skarels 	if (ui->ui_unit >= NIMP) {
10324777Skarels 		printf("imp%d: not configured\n", ui->ui_unit);
10424777Skarels 		return (0);
10524777Skarels 	}
10624777Skarels 	sc = &imp_softc[ui->ui_unit];
10724777Skarels 	ifp = &sc->imp_if;
1085640Sroot 	/* UNIT COULD BE AMBIGUOUS */
1095640Sroot 	ifp->if_unit = ui->ui_unit;
1105640Sroot 	ifp->if_name = "imp";
1116271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
1129001Sroot 	ifp->if_reset = reset;
1135771Swnj 	ifp->if_init = impinit;
11413067Ssam 	ifp->if_ioctl = impioctl;
1155771Swnj 	ifp->if_output = impoutput;
1165771Swnj 	/* reset is handled at the hardware level */
1175640Sroot 	if_attach(ifp);
11824777Skarels 	return ((int)ifp);
1195640Sroot }
1205640Sroot 
1215640Sroot /*
1225640Sroot  * IMP initialization routine: call hardware module to
1235640Sroot  * setup UNIBUS resources, init state and get ready for
1245640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1255640Sroot  */
1265640Sroot impinit(unit)
1275640Sroot 	int unit;
1285640Sroot {
1296500Ssam 	int s = splimp();
1305640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1315640Sroot 
13218412Skarels 	if (sc->imp_if.if_addrlist == 0)
13313067Ssam 		return;
1345771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1355771Swnj 		sc->imp_state = IMPS_DOWN;
1366336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1376500Ssam 		splx(s);
1385771Swnj 		return;
1395771Swnj 	}
1405640Sroot 	sc->imp_state = IMPS_INIT;
1415771Swnj 	impnoops(sc);
1426500Ssam 	splx(s);
1435640Sroot }
1445640Sroot 
1457199Ssam #ifdef IMPLEADERS
1467170Ssam int	impprintfs = 0;
1477199Ssam #endif
1485640Sroot 
1495640Sroot /*
1505640Sroot  * ARPAnet 1822 input routine.
1515640Sroot  * Called from hardware input interrupt routine to handle 1822
1525640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1535640Sroot  * passed to higher level protocol processors on the basis
1545640Sroot  * of link number.  Other type messages (control) are handled here.
1555640Sroot  */
1565771Swnj impinput(unit, m)
1575640Sroot 	int unit;
1585771Swnj 	register struct mbuf *m;
1595640Sroot {
1605640Sroot 	register struct imp_leader *ip;
1615640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
16224777Skarels 	struct ifnet *ifp;
1635640Sroot 	register struct host *hp;
1645640Sroot 	register struct ifqueue *inq;
1655771Swnj 	struct control_leader *cp;
1665640Sroot 	struct in_addr addr;
1675924Sroot 	struct mbuf *next;
1686336Ssam 	struct sockaddr_in *sin;
1695640Sroot 
17024777Skarels 	/*
17124777Skarels 	 * Pull the interface pointer out of the mbuf
17224777Skarels 	 * and save for later; adjust mbuf to look at rest of data.
17324777Skarels 	 */
17424777Skarels 	ifp = *(mtod(m, struct ifnet **));
17524777Skarels 	IF_ADJ(m);
1766257Sroot 	/* verify leader length. */
1775771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1785771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1795771Swnj 		return;
1805771Swnj 	cp = mtod(m, struct control_leader *);
1815771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1825771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1835771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1845771Swnj 			return;
1855640Sroot 	ip = mtod(m, struct imp_leader *);
1867199Ssam #ifdef IMPLEADERS
1877170Ssam 	if (impprintfs)
1887170Ssam 		printleader("impinput", ip);
1897199Ssam #endif
1905640Sroot 
1916257Sroot 	/* check leader type */
1925771Swnj 	if (ip->il_format != IMP_NFF) {
1935771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1945640Sroot 		goto drop;
1955771Swnj 	}
1965640Sroot 
1976588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
19818132Skarels 		/* If not data packet, build IP addr from leader (BRL) */
19918412Skarels 		imp_leader_to_addr(&addr, ip, &sc->imp_if);
2005640Sroot 	}
2015640Sroot 	switch (ip->il_mtype) {
2025640Sroot 
2035640Sroot 	case IMPTYPE_DATA:
2045640Sroot 		break;
2055640Sroot 
2065640Sroot 	/*
2075640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
2085640Sroot 	 */
2095640Sroot 	case IMPTYPE_BADLEADER:
2105647Ssam 		/*
2115647Ssam 		 * According to 1822 document, this message
2125647Ssam 		 * will be generated in response to the
2135647Ssam 		 * first noop sent to the IMP after
2145647Ssam 		 * the host resets the IMP interface.
2155647Ssam 		 */
2165771Swnj 		if (sc->imp_state != IMPS_INIT) {
2175924Sroot 			impmsg(sc, "leader error");
21818412Skarels 			hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
2195647Ssam 			impnoops(sc);
2205647Ssam 		}
2216582Ssam 		goto drop;
2225640Sroot 
2235640Sroot 	/*
2245640Sroot 	 * IMP going down.  Print message, and if not immediate,
2255640Sroot 	 * set off a timer to insure things will be reset at the
2265640Sroot 	 * appropriate time.
2275640Sroot 	 */
2285640Sroot 	case IMPTYPE_DOWN:
2296599Ssam 		if (sc->imp_state < IMPS_INIT)
2306598Ssam 			goto drop;
2315640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2325640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2336160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2345640Sroot 		}
2356160Ssam 		impmsg(sc, "going down %s",
2366160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2376582Ssam 		goto drop;
2385640Sroot 
2395640Sroot 	/*
2405640Sroot 	 * A NOP usually seen during the initialization sequence.
2415640Sroot 	 * Compare the local address with that in the message.
2425640Sroot 	 * Reset the local address notion if it doesn't match.
2435640Sroot 	 */
2446336Ssam 	case IMPTYPE_NOOP:
2455647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2465647Ssam 			sc->imp_state = IMPS_INIT;
2475647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2485647Ssam 		}
2496500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2506271Sroot 			goto drop;
25118412Skarels 		sin = (struct sockaddr_in *)&sc->imp_if.if_addrlist->ifa_addr;
25218412Skarels 		if (ip->il_imp != 0) {	/* BRL */
25318132Skarels 			struct in_addr leader_addr;
25418412Skarels 			imp_leader_to_addr(&leader_addr, ip, &sc->imp_if);
25518412Skarels 			if (sin->sin_addr.s_addr != leader_addr.s_addr) {
25618412Skarels 				impmsg(sc, "address reset to x%x (%d/%d)",
25718412Skarels 					htonl(leader_addr.s_addr),
25818132Skarels 					(u_int)ip->il_host,
25918412Skarels 					htons(ip->il_imp));
26018132Skarels 				sin->sin_addr.s_addr = leader_addr.s_addr;
26118132Skarels 			}
2626500Ssam 		}
2635771Swnj 		sc->imp_state = IMPS_UP;
2646336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2656271Sroot 		goto drop;
2665640Sroot 
2675640Sroot 	/*
2686582Ssam 	 * RFNM or INCOMPLETE message, send next
2696582Ssam 	 * message on the q.  We could pass incomplete's
2706582Ssam 	 * up to the next level, but this currently isn't
2716582Ssam 	 * needed.
2725640Sroot 	 */
2735640Sroot 	case IMPTYPE_RFNM:
2745640Sroot 	case IMPTYPE_INCOMPLETE:
2756588Ssam 		if (hp = hostlookup(addr)) {
27624777Skarels 			hp->h_timer = HOSTTIMER;
2776588Ssam 			if (hp->h_rfnm == 0)
2786588Ssam 				hp->h_flags &= ~HF_INUSE;
2796588Ssam 			else if (next = hostdeque(hp))
2806588Ssam 				(void) impsnd(&sc->imp_if, next);
2816588Ssam 		}
2826271Sroot 		goto drop;
2835640Sroot 
2845640Sroot 	/*
2855640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2865640Sroot 	 * awaiting transmission and release the host structure.
28724777Skarels 	 * Enqueue for notifying protocols at software interrupt time.
2885640Sroot 	 */
2895640Sroot 	case IMPTYPE_HOSTDEAD:
29011230Ssam 	case IMPTYPE_HOSTUNREACH:
29124777Skarels 		if (hp = hostlookup(addr)) {
29224777Skarels 			hp->h_flags |= (1 << (int)ip->il_mtype);
29324777Skarels 			hostfree(hp);
29424777Skarels 			hp->h_timer = HOSTDEADTIMER;
29524777Skarels 		}
2965859Sroot 		goto rawlinkin;
2975640Sroot 
2985640Sroot 	/*
2995640Sroot 	 * Error in data.  Clear RFNM status for this host and send
3005640Sroot 	 * noops to the IMP to clear the interface.
3015640Sroot 	 */
30211230Ssam 	case IMPTYPE_BADDATA:
3035924Sroot 		impmsg(sc, "data error");
3046588Ssam 		if (hp = hostlookup(addr))
3055640Sroot 			hp->h_rfnm = 0;
3065640Sroot 		impnoops(sc);
3076582Ssam 		goto drop;
3085640Sroot 
3095640Sroot 	/*
3105647Ssam 	 * Interface reset.
3115640Sroot 	 */
3125640Sroot 	case IMPTYPE_RESET:
3135924Sroot 		impmsg(sc, "interface reset");
31418412Skarels 		/* clear RFNM counts */
31518412Skarels 		hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
3165647Ssam 		impnoops(sc);
3176582Ssam 		goto drop;
3185640Sroot 
3195640Sroot 	default:
3205640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3216582Ssam 		goto drop;
3225640Sroot 	}
3235640Sroot 
3245640Sroot 	/*
3256257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3266257Sroot 	 * protocol routine (running at software interrupt).
3276257Sroot 	 * If this isn't a raw interface, advance pointer
3286257Sroot 	 * into mbuf past leader.
3295640Sroot 	 */
3305640Sroot 	switch (ip->il_link) {
3315640Sroot 
3325640Sroot 	case IMPLINK_IP:
3335640Sroot 		m->m_len -= sizeof(struct imp_leader);
3345640Sroot 		m->m_off += sizeof(struct imp_leader);
3356261Swnj 		schednetisr(NETISR_IP);
3365640Sroot 		inq = &ipintrq;
3375640Sroot 		break;
3385640Sroot 
3395640Sroot 	default:
3405868Sroot 	rawlinkin:
34124777Skarels 		schednetisr(NETISR_IMP);
34224777Skarels 		inq = &impintrq;
34324777Skarels 		break;
3445640Sroot 	}
34524777Skarels 	/*
34624777Skarels 	 * Re-insert interface pointer in the mbuf chain
34724777Skarels 	 * for the next protocol up.
34824777Skarels 	 */
34924777Skarels 	m->m_off -= sizeof(struct ifnet *);
35024777Skarels 	m->m_len += sizeof(struct ifnet *);
35124777Skarels 	*(mtod(m, struct ifnet **)) = ifp;
3526208Swnj 	if (IF_QFULL(inq)) {
3536208Swnj 		IF_DROP(inq);
3546208Swnj 		goto drop;
3556208Swnj 	}
3565640Sroot 	IF_ENQUEUE(inq, m);
3575640Sroot 	return;
3585640Sroot 
3595640Sroot drop:
3605640Sroot 	m_freem(m);
3615640Sroot }
3625640Sroot 
3635647Ssam /*
3645647Ssam  * Bring the IMP down after notification.
3655647Ssam  */
3665647Ssam impdown(sc)
3675647Ssam 	struct imp_softc *sc;
3685647Ssam {
36911230Ssam 	int s = splimp();
3706208Swnj 
3715647Ssam 	sc->imp_state = IMPS_DOWN;
3725924Sroot 	impmsg(sc, "marked down");
37318412Skarels 	hostreset(((struct in_ifaddr *)&sc->imp_if.if_addrlist)->ia_net);
3746582Ssam 	if_down(&sc->imp_if);
37511230Ssam 	splx(s);
3765647Ssam }
3775647Ssam 
3785640Sroot /*VARARGS*/
37918132Skarels impmsg(sc, fmt, a1, a2, a3)
3805640Sroot 	struct imp_softc *sc;
3815640Sroot 	char *fmt;
3826160Ssam 	u_int a1;
3835640Sroot {
3846208Swnj 
3855640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
38618132Skarels 	printf(fmt, a1, a2, a3);
3875640Sroot 	printf("\n");
3885640Sroot }
3895640Sroot 
39024777Skarels struct sockproto impproto = { PF_IMPLINK };
39124777Skarels struct sockaddr_in impdst = { AF_IMPLINK };
39224777Skarels struct sockaddr_in impsrc = { AF_IMPLINK };
39324777Skarels 
3945640Sroot /*
39524777Skarels  * Pick up the IMP "error" messages enqueued earlier,
39624777Skarels  * passing these up to the higher level protocol
39724777Skarels  * and the raw interface.
3986582Ssam  */
39924777Skarels impintr()
4006582Ssam {
40124777Skarels 	register struct mbuf *m;
40224777Skarels 	register struct control_leader *cp;
40324777Skarels 	struct ifnet *ifp;
40424777Skarels 	int s;
4056582Ssam 
40624777Skarels 	for (;;) {
40724777Skarels 		s = splimp();
40824777Skarels 		IF_DEQUEUEIF(&impintrq, m, ifp);
40924777Skarels 		splx(s);
41024777Skarels 		if (m == 0)
41124777Skarels 			return;
41218132Skarels 
41324777Skarels 		cp = mtod(m, struct control_leader *);
41424777Skarels 		imp_leader_to_addr(&impsrc.sin_addr, (struct imp_leader *)cp,
41524777Skarels 		    ifp);
41624777Skarels 		impproto.sp_protocol = cp->dl_link;
417*25409Skarels 		impdst.sin_addr = IA_SIN(ifp->if_addrlist)->sin_addr;
41824777Skarels 
41924777Skarels 		switch (cp->dl_link) {
42024777Skarels 
42124777Skarels 		case IMPLINK_IP:
42224777Skarels 			pfctlinput(cp->dl_mtype, (caddr_t)&impsrc);
42324777Skarels 			break;
42424777Skarels 		default:
42524777Skarels 			raw_ctlinput(cp->dl_mtype, (caddr_t)&impsrc);
42624777Skarels 			break;
42724777Skarels 		}
42824777Skarels 
42924777Skarels 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
43024777Skarels 		  (struct sockaddr *)&impdst);
4316588Ssam 	}
4326582Ssam }
4336582Ssam 
4346582Ssam /*
4355640Sroot  * ARPAnet 1822 output routine.
4365640Sroot  * Called from higher level protocol routines to set up messages for
4375640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4385640Sroot  * enqueue the message for this IMP's hardware driver.
4395640Sroot  */
4406336Ssam impoutput(ifp, m0, dst)
4415640Sroot 	register struct ifnet *ifp;
4425640Sroot 	struct mbuf *m0;
4436336Ssam 	struct sockaddr *dst;
4445640Sroot {
4455640Sroot 	register struct imp_leader *imp;
4465640Sroot 	register struct mbuf *m = m0;
44718412Skarels 	int dlink, len;
4486504Ssam 	int error = 0;
4495640Sroot 
4505640Sroot 	/*
4515640Sroot 	 * Don't even try if the IMP is unavailable.
4525640Sroot 	 */
4536504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4546504Ssam 		error = ENETDOWN;
4555647Ssam 		goto drop;
4566504Ssam 	}
4575640Sroot 
4586336Ssam 	switch (dst->sa_family) {
4595640Sroot 
4606336Ssam 	case AF_INET: {
46124777Skarels 		struct ip *ip = mtod(m, struct ip *);
4625640Sroot 
4635640Sroot 		dlink = IMPLINK_IP;
4646160Ssam 		len = ntohs((u_short)ip->ip_len);
4655640Sroot 		break;
4665640Sroot 	}
46724777Skarels 
4686336Ssam 	case AF_IMPLINK:
46924777Skarels 		len = 0;
47024777Skarels 		do
47124777Skarels 			len += m->m_len;
47224777Skarels 		while (m = m->m_next);
47324777Skarels 		m = m0;
4745640Sroot 		goto leaderexists;
4755640Sroot 
4765640Sroot 	default:
4776336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4786336Ssam 			dst->sa_family);
4796504Ssam 		error = EAFNOSUPPORT;
4805647Ssam 		goto drop;
4815640Sroot 	}
4825640Sroot 
4835640Sroot 	/*
4845640Sroot 	 * Add IMP leader.  If there's not enough space in the
4855640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4865640Sroot 	 * drop this sucker.
4875640Sroot 	 */
4885640Sroot 	if (m->m_off > MMAXOFF ||
4895640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4909645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4916504Ssam 		if (m == 0) {
4926504Ssam 			error = ENOBUFS;
4935647Ssam 			goto drop;
4946504Ssam 		}
4955640Sroot 		m->m_next = m0;
4965640Sroot 		m->m_len = sizeof(struct imp_leader);
4975640Sroot 	} else {
4985640Sroot 		m->m_off -= sizeof(struct imp_leader);
4995640Sroot 		m->m_len += sizeof(struct imp_leader);
5005640Sroot 	}
5015640Sroot 	imp = mtod(m, struct imp_leader *);
5025640Sroot 	imp->il_format = IMP_NFF;
5035859Sroot 	imp->il_mtype = IMPTYPE_DATA;
50418132Skarels 	imp_addr_to_leader(imp,
50518412Skarels 		((struct sockaddr_in *)dst)->sin_addr.s_addr); /* BRL */
50618412Skarels 	imp->il_length = htons((u_short)len << 3);		/* BRL */
5075640Sroot 	imp->il_link = dlink;
5085859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
5095640Sroot 
5105640Sroot leaderexists:
5115640Sroot 	return (impsnd(ifp, m));
5125647Ssam drop:
5135647Ssam 	m_freem(m0);
5146504Ssam 	return (error);
5155640Sroot }
5165640Sroot 
5175640Sroot /*
5185640Sroot  * Put a message on an interface's output queue.
5195640Sroot  * Perform RFNM counting: no more than 8 message may be
5205640Sroot  * in flight to any one host.
5215640Sroot  */
5225640Sroot impsnd(ifp, m)
5235640Sroot 	struct ifnet *ifp;
5245640Sroot 	struct mbuf *m;
5255640Sroot {
5265640Sroot 	register struct imp_leader *ip;
5275640Sroot 	register struct host *hp;
5285640Sroot 	struct impcb *icp;
5296588Ssam 	int s, error;
5305640Sroot 
5315640Sroot 	ip = mtod(m, struct imp_leader *);
5325640Sroot 
5335640Sroot 	/*
5345640Sroot 	 * Do RFNM counting for data messages
5355640Sroot 	 * (no more than 8 outstanding to any host)
5365640Sroot 	 */
5376588Ssam 	s = splimp();
5385640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5395640Sroot 		struct in_addr addr;
5405640Sroot 
54118412Skarels 		imp_leader_to_addr(&addr, ip, ifp);	/* BRL */
5425771Swnj 		if ((hp = hostlookup(addr)) == 0)
5435771Swnj 			hp = hostenter(addr);
5446588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5456607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5466588Ssam 			hp->h_flags &= ~HF_INUSE;
5476588Ssam 			goto bad;
5486588Ssam 		}
5495640Sroot 
5505640Sroot 		/*
5515647Ssam 		 * If IMP would block, queue until RFNM
5525640Sroot 		 */
5535640Sroot 		if (hp) {
55424777Skarels #ifndef NORFNM
55518132Skarels 			if (hp->h_rfnm < 8)
55618132Skarels #endif
55718132Skarels 			{
55824777Skarels 				hp->h_timer = HOSTTIMER;
5595640Sroot 				hp->h_rfnm++;
5605640Sroot 				goto enque;
5615640Sroot 			}
5626095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5636095Swnj 				HOST_ENQUE(hp, m);
5646095Swnj 				goto start;
5656095Swnj 			}
5665640Sroot 		}
5676588Ssam 		error = ENOBUFS;
5686588Ssam 		goto bad;
5695640Sroot 	}
5705640Sroot enque:
5716208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5726208Swnj 		IF_DROP(&ifp->if_snd);
5736588Ssam 		error = ENOBUFS;
57424777Skarels 		if (ip->il_mtype == IMPTYPE_DATA)
57524777Skarels 			hp->h_rfnm--;
5766588Ssam bad:
5776208Swnj 		m_freem(m);
5786588Ssam 		splx(s);
5796588Ssam 		return (error);
5806208Swnj 	}
5815640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5826095Swnj start:
5835640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5845640Sroot 	if (icp->ic_oactive == 0)
5855640Sroot 		(*icp->ic_start)(ifp->if_unit);
5866630Ssam 	splx(s);
5876504Ssam 	return (0);
5885640Sroot }
5895640Sroot 
5905640Sroot /*
5915640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5925640Sroot  * Part of host-IMP initialization procedure.
5935640Sroot  * (Should return success/failure, but noone knows
5945640Sroot  * what to do with this, so why bother?)
5956818Ssam  * This routine is always called at splimp, so we don't
5966818Ssam  * protect the call to IF_PREPEND.
5975640Sroot  */
5985640Sroot impnoops(sc)
5995640Sroot 	register struct imp_softc *sc;
6005640Sroot {
6015640Sroot 	register i;
6025640Sroot 	register struct mbuf *m;
6035771Swnj 	register struct control_leader *cp;
6045640Sroot 
6055640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
60618412Skarels 	for (i = 0; i < IMP_DROPCNT + 1; i++) {
6079645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
6085640Sroot 			return;
6095771Swnj 		m->m_len = sizeof(struct control_leader);
6105771Swnj 		cp = mtod(m, struct control_leader *);
6115771Swnj 		cp->dl_format = IMP_NFF;
6125771Swnj                 cp->dl_link = i;
6135771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
6145640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
6155640Sroot 	}
6165640Sroot 	if (sc->imp_cb.ic_oactive == 0)
6175640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
6185640Sroot }
6197170Ssam 
62013067Ssam /*
62113067Ssam  * Process an ioctl request.
62213067Ssam  */
62313067Ssam impioctl(ifp, cmd, data)
62413067Ssam 	register struct ifnet *ifp;
62513067Ssam 	int cmd;
62613067Ssam 	caddr_t data;
62713067Ssam {
62818412Skarels 	struct ifaddr *ifa = (struct ifaddr *) data;
62913067Ssam 	int s = splimp(), error = 0;
63013067Ssam 
63113067Ssam 	switch (cmd) {
63213067Ssam 
63313067Ssam 	case SIOCSIFADDR:
63418412Skarels 		if (ifa->ifa_addr.sa_family != AF_INET) {
63518412Skarels 			error = EINVAL;
63618412Skarels 			break;
63718412Skarels 		}
63818412Skarels 		if ((ifp->if_flags & IFF_RUNNING) == 0)
63913067Ssam 			impinit(ifp->if_unit);
64013067Ssam 		break;
64113067Ssam 
64213067Ssam 	default:
64313067Ssam 		error = EINVAL;
64413067Ssam 	}
64513067Ssam 	splx(s);
64613067Ssam 	return (error);
64713067Ssam }
64813067Ssam 
6497170Ssam #ifdef IMPLEADERS
6507170Ssam printleader(routine, ip)
6517170Ssam 	char *routine;
6527170Ssam 	register struct imp_leader *ip;
6537170Ssam {
6547170Ssam 	printf("%s: ", routine);
6557170Ssam 	printbyte((char *)ip, 12);
6567170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6577170Ssam 		ip->il_flags);
6587170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
6597170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
6607170Ssam 	else
6617170Ssam 		printf("%x,", ip->il_mtype);
6627170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6637170Ssam 		ntohs(ip->il_imp));
6647170Ssam 	if (ip->il_link == IMPLINK_IP)
6657170Ssam 		printf("ip,");
6667170Ssam 	else
6677170Ssam 		printf("%x,", ip->il_link);
6687170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6697170Ssam }
6707170Ssam 
6717170Ssam printbyte(cp, n)
6727170Ssam 	register char *cp;
6737170Ssam 	int n;
6747170Ssam {
6757170Ssam 	register i, j, c;
6767170Ssam 
6777170Ssam 	for (i=0; i<n; i++) {
6787170Ssam 		c = *cp++;
6797170Ssam 		for (j=0; j<2; j++)
68024777Skarels 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf], 0);
68124777Skarels 		putchar(' ', 0);
6827170Ssam 	}
68324777Skarels 	putchar('\n', 0);
6847170Ssam }
6855640Sroot #endif
68618132Skarels 
68718132Skarels /*
68818132Skarels  * Routine to convert from IMP Leader to InterNet Address.
68918132Skarels  *
69018132Skarels  * This procedure is necessary because IMPs may be assigned Class A, B, or C
69118132Skarels  * network numbers, but only have 8 bits in the leader to reflect the
69218132Skarels  * IMP "network number".  The strategy is to take the network number from
69318132Skarels  * the ifnet structure, and blend in the host-on-imp and imp-on-net numbers
69418132Skarels  * from the leader.
69518132Skarels  *
69618132Skarels  * There is no support for "Logical Hosts".
69718132Skarels  *
69818132Skarels  * Class A:	Net.Host.0.Imp
69918132Skarels  * Class B:	Net.net.Host.Imp
70018132Skarels  * Class C:	Net.net.net.(Host4|Imp4)
70118132Skarels  */
70218412Skarels imp_leader_to_addr(ap, ip, ifp)
70318412Skarels 	struct in_addr *ap;
70418412Skarels 	register struct imp_leader *ip;
70518412Skarels 	struct ifnet *ifp;
70618132Skarels {
70718132Skarels 	register long final;
70818132Skarels 	register struct sockaddr_in *sin;
70918132Skarels 	int imp = htons(ip->il_imp);
71018132Skarels 
71118412Skarels 	sin = (struct sockaddr_in *)(&ifp->if_addrlist->ifa_addr);
71218412Skarels 	final = htonl(sin->sin_addr.s_addr);
71318132Skarels 
71418412Skarels 	if (IN_CLASSA(final)) {
71518132Skarels 		final &= IN_CLASSA_NET;
71618132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<16);
71718412Skarels 	} else if (IN_CLASSB(final)) {
71818132Skarels 		final &= IN_CLASSB_NET;
71918132Skarels 		final |= (imp & 0xFF) | ((ip->il_host & 0xFF)<<8);
72018132Skarels 	} else {
72118132Skarels 		final &= IN_CLASSC_NET;
72218132Skarels 		final |= (imp & 0x0F) | ((ip->il_host & 0x0F)<<4);
72318132Skarels 	}
72418412Skarels 	ap->s_addr = htonl(final);
72518132Skarels }
72618132Skarels 
72718132Skarels /*
72818132Skarels  * Function to take InterNet address and fill in IMP leader fields.
72918132Skarels  */
73018412Skarels imp_addr_to_leader(imp, a)
73118412Skarels 	register struct imp_leader *imp;
73218412Skarels 	long a;
73318132Skarels {
73418412Skarels 	register long addr = htonl(a);		/* host order */
73518132Skarels 
73618132Skarels 	imp->il_network = 0;	/* !! */
73718132Skarels 
73818412Skarels 	if (IN_CLASSA(addr)) {
73918132Skarels 		imp->il_host = ((addr>>16) & 0xFF);
74018132Skarels 		imp->il_imp = addr & 0xFF;
74118412Skarels 	} else if (IN_CLASSB(addr)) {
74218132Skarels 		imp->il_host = ((addr>>8) & 0xFF);
74318132Skarels 		imp->il_imp = addr & 0xFF;
74418132Skarels 	} else {
74518132Skarels 		imp->il_host = ((addr>>4) & 0xF);
74618132Skarels 		imp->il_imp = addr & 0xF;
74718132Skarels 	}
74818132Skarels 	imp->il_imp = htons(imp->il_imp);	/* network order! */
74918132Skarels }
7507170Ssam #endif
751