1*5647Ssam /* if_imp.c 4.3 82/02/01 */ 25640Sroot 35640Sroot #include "imp.h" 45640Sroot #if NIMP > 0 55640Sroot /* 65640Sroot * ARPAnet IMP interface driver. 75640Sroot * 85640Sroot * The IMP-host protocol is handled here, leaving 95640Sroot * hardware specifics to the lower level interface driver. 105640Sroot */ 115640Sroot #include "../h/param.h" 125640Sroot #include "../h/systm.h" 135640Sroot #include "../h/mbuf.h" 145640Sroot #include "../h/pte.h" 155640Sroot #include "../h/buf.h" 165640Sroot #include "../h/protosw.h" 175640Sroot #include "../h/socket.h" 185640Sroot #include "../h/ubareg.h" 195640Sroot #include "../h/ubavar.h" 205640Sroot #include "../h/cpu.h" 215640Sroot #include "../h/mtpr.h" 225640Sroot #include "../h/vmmac.h" 235640Sroot #include "../net/in.h" 245640Sroot #include "../net/in_systm.h" 255640Sroot #include "../net/if.h" 265640Sroot #include "../net/if_imp.h" 275640Sroot #include "../net/host.h" 285640Sroot #include "../net/ip.h" 295640Sroot #include "../net/ip_var.h" 305640Sroot 315640Sroot /* 325640Sroot * IMP software status per interface. 335640Sroot * (partially shared with the hardware specific module) 345640Sroot * 355640Sroot * Each interface is referenced by a network interface structure, 365640Sroot * imp_if, which the routing code uses to locate the interface. 375640Sroot * This structure contains the output queue for the interface, its 385640Sroot * address, ... IMP specific structures used in connecting the 395640Sroot * IMP software modules to the hardware specific interface routines 405640Sroot * are also stored here. These structures are visible in the interface 415640Sroot * driver through back pointers set up in the hardware's attach routine. 425640Sroot * 435640Sroot * NOTE: imp_if and imp_cb are assumed adjacent in hardware code. 445640Sroot */ 455640Sroot struct imp_softc { 465640Sroot struct ifnet imp_if; /* network visible interface */ 475640Sroot struct impcb imp_cb; /* hooks to hardware module */ 485640Sroot u_char imp_state; /* current state of IMP */ 495640Sroot char imp_dropcnt; /* used during initialization */ 505640Sroot short imp_timer; /* going down timer */ 515640Sroot } imp_softc[NIMP]; 525640Sroot 535640Sroot /* 545640Sroot * Messages from IMP regarding why 555640Sroot * it's going down. 565640Sroot */ 575640Sroot static char *impmsg[] = { 585640Sroot "in 30 seconds", 595640Sroot "for hardware PM", 605640Sroot "to reload software", 615640Sroot "for emergency reset" 625640Sroot }; 635640Sroot 645640Sroot /* 655640Sroot * IMP attach routine. Called from hardware device attach routine 665640Sroot * at configuration time with a pointer to the UNIBUS device structure. 675640Sroot * Sets up local state and returns pointer to base of ifnet+impcb 685640Sroot * structures. This is then used by the device's attach routine 695640Sroot * set up its back pointers. 705640Sroot */ 715640Sroot impattach(ui) 725640Sroot struct uba_device *ui; 735640Sroot { 745640Sroot struct imp_softc *sc = &imp_softc[ui->ui_unit]; 755640Sroot register struct ifnet *ifp = &sc->imp_if; 765640Sroot 775640Sroot COUNT(IMPATTACH); 785640Sroot /* UNIT COULD BE AMBIGUOUS */ 795640Sroot ifp->if_unit = ui->ui_unit; 805640Sroot ifp->if_name = "imp"; 815640Sroot ifp->if_mtu = IMP_MTU; 825640Sroot ifp->if_net = ui->ui_flags; 835640Sroot /* ifp->if_host = ... */ 845640Sroot /* ifp->if_addr = if_makeaddr(ifp->if_net, ifp->if_host); */ 855640Sroot if_attach(ifp); 865640Sroot /* kludge to hand pointers back to hardware attach routine */ 875640Sroot return ((int)&sc->imp_if); 885640Sroot } 895640Sroot 905640Sroot /* 915640Sroot * IMP initialization routine: call hardware module to 925640Sroot * setup UNIBUS resources, init state and get ready for 935640Sroot * NOOPs the IMP should send us, and that we want to drop. 945640Sroot */ 955640Sroot impinit(unit) 965640Sroot int unit; 975640Sroot { 985640Sroot register struct imp_softc *sc = &imp_softc[unit]; 995640Sroot 1005640Sroot (*sc->imp_cb.ic_init)(unit); 1015640Sroot sc->imp_state = IMPS_INIT; 1025640Sroot sc->imp_dropcnt = IMP_DROPCNT; 1035640Sroot } 1045640Sroot 1055640Sroot struct sockproto impproto = { PF_IMPLINK }; 1065645Ssam struct sockaddr_in impdst = { AF_IMPLINK }; 1075645Ssam struct sockaddr_in impsrc = { AF_IMPLINK }; 1085640Sroot 1095640Sroot /* 1105640Sroot * ARPAnet 1822 input routine. 1115640Sroot * Called from hardware input interrupt routine to handle 1822 1125640Sroot * IMP-host messages. Type 0 messages (non-control) are 1135640Sroot * passed to higher level protocol processors on the basis 1145640Sroot * of link number. Other type messages (control) are handled here. 1155640Sroot */ 1165640Sroot impinput(unit, m0) 1175640Sroot int unit; 1185640Sroot struct mbuf *m0; 1195640Sroot { 1205640Sroot int s; 1215640Sroot register struct mbuf *m; 1225640Sroot register struct imp_leader *ip; 1235640Sroot register struct imp_softc *sc = &imp_softc[unit]; 1245640Sroot register struct host *hp; 1255640Sroot register struct ifqueue *inq; 1265640Sroot struct in_addr addr; 1275640Sroot 1285640Sroot COUNT(IMP_INPUT); 1295640Sroot m = m0; 130*5647Ssam 131*5647Ssam /* 132*5647Ssam * We should generate a "bad leader" message 133*5647Ssam * to the IMP about messages too short. 134*5647Ssam */ 1355640Sroot if (m->m_len < sizeof(struct imp_leader) && 1365640Sroot m_pullup(m, sizeof(struct imp_leader)) == 0) 1375640Sroot goto drop; 1385640Sroot ip = mtod(m, struct imp_leader *); 1395640Sroot 140*5647Ssam /* 141*5647Ssam * Check leader type -- should notify IMP 142*5647Ssam * in case of failure... 143*5647Ssam */ 1445640Sroot if (ip->il_format != IMP_NFF) 1455640Sroot goto drop; 1465640Sroot 1475640Sroot /* 1485640Sroot * Certain messages require a host structure. 1495640Sroot * Do this in one shot here. 1505640Sroot */ 1515640Sroot switch (ip->il_mtype) { 1525640Sroot 1535640Sroot case IMPTYPE_RFNM: 1545640Sroot case IMPTYPE_INCOMPLETE: 1555640Sroot case IMPTYPE_HOSTDEAD: 1565640Sroot case IMPTYPE_HOSTUNREACH: 1575640Sroot case IMPTYPE_BADDATA: 158*5647Ssam addr.s_host = ntohs(ip->il_host); /* XXX */ 1595640Sroot hp = h_lookup(addr); 1605640Sroot break; 1615640Sroot } 1625640Sroot 1635640Sroot switch (ip->il_mtype) { 1645640Sroot 1655640Sroot /* 1665640Sroot * Data for a protocol. Dispatch to the appropriate 1675640Sroot * protocol routine (running at software interrupt). 1685640Sroot * If this isn't a raw interface, advance pointer 169*5647Ssam * into mbuf past leader (done below). 1705640Sroot */ 1715640Sroot case IMPTYPE_DATA: 1725640Sroot ip->il_length = ntohs(ip->il_length) >> 3; 1735640Sroot break; 1745640Sroot 1755640Sroot /* 1765640Sroot * IMP leader error. Reset the IMP and discard the packet. 1775640Sroot */ 1785640Sroot case IMPTYPE_BADLEADER: 179*5647Ssam /* 180*5647Ssam * According to 1822 document, this message 181*5647Ssam * will be generated in response to the 182*5647Ssam * first noop sent to the IMP after 183*5647Ssam * the host resets the IMP interface. 184*5647Ssam */ 185*5647Ssam if (sc->imp_state != IMPS_RESET) { 186*5647Ssam imperr(sc, "leader error"); 187*5647Ssam h_reset(sc->imp_if.if_net); /* XXX */ 188*5647Ssam impnoops(sc); 189*5647Ssam } 1905640Sroot goto drop; 1915640Sroot 1925640Sroot /* 1935640Sroot * IMP going down. Print message, and if not immediate, 1945640Sroot * set off a timer to insure things will be reset at the 1955640Sroot * appropriate time. 1965640Sroot */ 1975640Sroot case IMPTYPE_DOWN: 1985640Sroot if ((ip->il_link & IMP_DMASK) == 0) { 1995640Sroot sc->imp_state = IMPS_GOINGDOWN; 200*5647Ssam timeout(impdown, sc, 30 * 60 * HZ); 2015640Sroot } 2025640Sroot imperr(sc, "going down %s", impmsg[ip->il_link & IMP_DMASK]); 2035640Sroot goto drop; 2045640Sroot 2055640Sroot /* 2065640Sroot * A NOP usually seen during the initialization sequence. 2075640Sroot * Compare the local address with that in the message. 2085640Sroot * Reset the local address notion if it doesn't match. 2095640Sroot */ 2105640Sroot case IMPTYPE_NOOP: 211*5647Ssam if (sc->imp_state == IMPS_DOWN) { 212*5647Ssam sc->imp_state = IMPS_INIT; 213*5647Ssam sc->imp_dropcnt = IMP_DROPCNT; 214*5647Ssam } 2155640Sroot if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt == 0) { 2165640Sroot sc->imp_state = IMPS_UP; 2175640Sroot /* restart output in case something was q'd */ 2185640Sroot (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 2195640Sroot } 2205640Sroot if (ip->il_host != sc->imp_if.if_addr.s_host || 2215640Sroot ip->il_impno != sc->imp_if.if_addr.s_imp) { 2225640Sroot sc->imp_if.if_addr.s_host = ip->il_host; 2235640Sroot sc->imp_if.if_addr.s_imp = ip->il_imp; 2245640Sroot imperr(sc, "imp%d: address set to %d/%d\n", 2255640Sroot ip->il_host, ip->il_impno); 2265640Sroot } 2275640Sroot goto drop; 2285640Sroot 2295640Sroot /* 2305640Sroot * RFNM or INCOMPLETE message, record in 2315640Sroot * host table and prime output routine. 2325640Sroot * 233*5647Ssam * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES. 2345640Sroot */ 2355640Sroot case IMPTYPE_RFNM: 2365640Sroot case IMPTYPE_INCOMPLETE: 2375640Sroot if (hp && hp->h_rfnm) { 2385640Sroot register struct mbuf *n; 2395640Sroot 2405640Sroot hp->h_rfnm--; 2415640Sroot /* poke holding queue */ 2425640Sroot if (n = hp->h_q) { 2435640Sroot if (n->m_act == n) 2445640Sroot hp->h_q = 0; 2455640Sroot else { 2465640Sroot n = n->m_act; 2475640Sroot hp->h_q->m_act = n->m_act; 2485640Sroot } 2495640Sroot (void) impsnd(n, sc); 2505640Sroot } 2515640Sroot } 2525640Sroot break; 2535640Sroot 2545640Sroot /* 2555640Sroot * Host or IMP can't be reached. Flush any packets 2565640Sroot * awaiting transmission and release the host structure. 2575640Sroot * 2585640Sroot * HOW DO WE NOTIFY THE PROTOCOL? 2595640Sroot * HOW DO WE AGE THE HOST STRUCTURE TO SAVE STATUS? 2605640Sroot */ 2615640Sroot case IMPTYPE_HOSTDEAD: 2625640Sroot case IMPTYPE_HOSTUNREACH: 2635640Sroot if (hp) 2645640Sroot h_free(hp); /* won't work right */ 2655640Sroot break; 2665640Sroot 2675640Sroot /* 2685640Sroot * Error in data. Clear RFNM status for this host and send 2695640Sroot * noops to the IMP to clear the interface. 2705640Sroot */ 2715640Sroot case IMPTYPE_BADDATA: 2725640Sroot imperr(sc, "data error"); 2735640Sroot if (hp) 2745640Sroot hp->h_rfnm = 0; 2755640Sroot impnoops(sc); 2765640Sroot break; 2775640Sroot 2785640Sroot /* 279*5647Ssam * Interface reset. 2805640Sroot */ 2815640Sroot case IMPTYPE_RESET: 282*5647Ssam imperr(sc, "interface reset"); 283*5647Ssam sc->imp_state = IMPS_RESET; 284*5647Ssam impnoops(sc); 2855640Sroot goto drop; 2865640Sroot 2875640Sroot default: 2885640Sroot sc->imp_if.if_collisions++; /* XXX */ 2895640Sroot goto drop; 2905640Sroot } 2915640Sroot 2925640Sroot /* 2935640Sroot * Queue on protocol's input queue. 2945640Sroot */ 2955640Sroot switch (ip->il_link) { 2965640Sroot 2975640Sroot #ifdef INET 2985640Sroot case IMPLINK_IP: 2995640Sroot m->m_len -= sizeof(struct imp_leader); 3005640Sroot m->m_off += sizeof(struct imp_leader); 3015640Sroot setipintr(); 3025640Sroot inq = &ipintrq; 3035640Sroot break; 3045640Sroot #endif 3055640Sroot 3065640Sroot default: 3075640Sroot impproto.sp_protocol = ip->il_link; 3085645Ssam impdst.sin_addr = sc->imp_if.if_addr; 3095645Ssam impsrc.sin_addr.s_net = ip->il_network; 3105645Ssam impsrc.sin_addr.s_host = ip->il_host; 3115645Ssam impsrc.sin_addr.s_imp = ip->il_imp; 3125645Ssam raw_input(m, impproto, impdst, impsrc); 3135640Sroot return; 3145640Sroot } 3155640Sroot IF_ENQUEUE(inq, m); 3165640Sroot return; 3175640Sroot 3185640Sroot drop: 3195640Sroot m_freem(m); 3205640Sroot } 3215640Sroot 322*5647Ssam /* 323*5647Ssam * Bring the IMP down after notification. 324*5647Ssam */ 325*5647Ssam impdown(sc) 326*5647Ssam struct imp_softc *sc; 327*5647Ssam { 328*5647Ssam sc->imp_state = IMPS_DOWN; 329*5647Ssam /* notify protocols with messages waiting? */ 330*5647Ssam } 331*5647Ssam 3325640Sroot /*VARARGS*/ 3335640Sroot imperr(sc, fmt, a1, a2) 3345640Sroot struct imp_softc *sc; 3355640Sroot char *fmt; 3365640Sroot { 3375640Sroot printf("imp%d: ", sc->imp_if.if_unit); 3385640Sroot printf(fmt, a1, a2); 3395640Sroot printf("\n"); 3405640Sroot } 3415640Sroot 3425640Sroot /* 3435640Sroot * ARPAnet 1822 output routine. 3445640Sroot * Called from higher level protocol routines to set up messages for 3455640Sroot * transmission to the imp. Sets up the header and calls impsnd to 3465640Sroot * enqueue the message for this IMP's hardware driver. 3475640Sroot */ 3485640Sroot impoutput(ifp, m0, pf) 3495640Sroot register struct ifnet *ifp; 3505640Sroot struct mbuf *m0; 3515640Sroot { 3525640Sroot register struct imp_leader *imp; 3535640Sroot register struct mbuf *m = m0; 3545640Sroot int x, dhost, dimp, dlink, len; 3555640Sroot 3565640Sroot /* 3575640Sroot * Don't even try if the IMP is unavailable. 3585640Sroot */ 359*5647Ssam x = imp_softc[ifp->if_unit].imp_state; 360*5647Ssam if (x == IMPS_DOWN || x == IMPS_GOINGDOWN) 361*5647Ssam goto drop; 3625640Sroot 3635640Sroot switch (pf) { 3645640Sroot 3655640Sroot #ifdef INET 3665640Sroot case PF_INET: { 3675640Sroot register struct ip *ip = mtod(m0, struct ip *); 3685640Sroot 3695640Sroot dhost = ip->ip_dst.s_host; 3705640Sroot dimp = ip->ip_dst.s_imp; 3715640Sroot dlink = IMPLINK_IP; 3725640Sroot len = ntohs(ip->ip_len); 3735640Sroot break; 3745640Sroot } 3755640Sroot #endif 3765640Sroot case PF_IMPLINK: 3775640Sroot goto leaderexists; 3785640Sroot 3795640Sroot default: 3805640Sroot printf("imp%d: can't encapsulate pf%d\n", ifp->if_unit, pf); 381*5647Ssam goto drop; 3825640Sroot } 3835640Sroot 3845640Sroot /* 3855640Sroot * Add IMP leader. If there's not enough space in the 3865640Sroot * first mbuf, allocate another. If that should fail, we 3875640Sroot * drop this sucker. 3885640Sroot */ 3895640Sroot if (m->m_off > MMAXOFF || 3905640Sroot MMINOFF + sizeof(struct imp_leader) > m->m_off) { 3915640Sroot m = m_get(M_DONTWAIT); 392*5647Ssam if (m == 0) 393*5647Ssam goto drop; 3945640Sroot m->m_next = m0; 3955640Sroot m->m_off = MMINOFF; 3965640Sroot m->m_len = sizeof(struct imp_leader); 3975640Sroot } else { 3985640Sroot m->m_off -= sizeof(struct imp_leader); 3995640Sroot m->m_len += sizeof(struct imp_leader); 4005640Sroot } 4015640Sroot imp = mtod(m, struct imp_leader *); 4025640Sroot imp->il_format = IMP_NFF; 4035640Sroot imp->il_host = dhost; 4045640Sroot imp->il_impno = dimp; 4055640Sroot imp->il_length = (len + sizeof(struct imp_leader)) << 3; 4065640Sroot imp->il_link = dlink; 4075640Sroot 4085640Sroot leaderexists: 4095640Sroot /* 4105640Sroot * Hand message to impsnd to perform RFNM counting 4115640Sroot * and eventual transmission. 4125640Sroot */ 4135640Sroot return (impsnd(ifp, m)); 414*5647Ssam drop: 415*5647Ssam m_freem(m0); 416*5647Ssam return (0); 4175640Sroot } 4185640Sroot 4195640Sroot /* 4205640Sroot * Put a message on an interface's output queue. 4215640Sroot * Perform RFNM counting: no more than 8 message may be 4225640Sroot * in flight to any one host. 4235640Sroot */ 4245640Sroot impsnd(ifp, m) 4255640Sroot struct ifnet *ifp; 4265640Sroot struct mbuf *m; 4275640Sroot { 4285640Sroot register struct imp_leader *ip; 4295640Sroot register struct host *hp; 4305640Sroot struct impcb *icp; 4315640Sroot int x; 4325640Sroot 4335640Sroot ip = mtod(m, struct imp_leader *); 4345640Sroot 4355640Sroot /* 4365640Sroot * Do RFNM counting for data messages 4375640Sroot * (no more than 8 outstanding to any host) 4385640Sroot */ 4395640Sroot if (ip->il_mtype == IMPTYPE_DATA) { 4405640Sroot struct in_addr addr; 4415640Sroot 4425640Sroot addr.s_net = ifp->if_net; 4435640Sroot addr.s_host = ip->il_host; 4445640Sroot addr.s_imp = ip->il_imp; 4455640Sroot hp = h_enter(addr); 4465640Sroot 4475640Sroot /* 448*5647Ssam * If IMP would block, queue until RFNM 4495640Sroot */ 4505640Sroot if (hp) { 4515640Sroot register struct mbuf *n; 4525640Sroot int cnt; 4535640Sroot 4545640Sroot if (hp->h_rfnm < 8) { 4555640Sroot hp->h_rfnm++; 4565640Sroot goto enque; 4575640Sroot } 4585640Sroot /* 4595640Sroot * Keeping the count in the host structure 4605640Sroot * causes the packing scheme to lose too much. 4615640Sroot */ 4625640Sroot cnt = 0, n = hp->h_q; 4635640Sroot for (; n != (struct mbuf *)hp; n = n->m_act) 4645640Sroot cnt++; 4655640Sroot if (cnt >= 8) 4665640Sroot goto drop; 467*5647Ssam 468*5647Ssam /* 469*5647Ssam * Q is kept as circulare list with h_q 470*5647Ssam * (head) pointing to the last entry. 471*5647Ssam */ 4725640Sroot if ((n = hp->h_q) == 0) 4735640Sroot hp->h_q = m->m_act = m; 4745640Sroot else { 4755640Sroot m->m_act = n->m_act; 4765640Sroot hp->h_q = n->m_act = m; 4775640Sroot } 4785640Sroot goto start; 4795640Sroot } 4805640Sroot drop: 4815640Sroot m_freem(m); 4825640Sroot return (0); 4835640Sroot } 4845640Sroot enque: 4855640Sroot x = splimp(); 4865640Sroot IF_ENQUEUE(&ifp->if_snd, m); 4875640Sroot splx(x); 4885640Sroot 4895640Sroot start: 4905640Sroot icp = &imp_softc[ifp->if_unit].imp_cb; 4915640Sroot if (icp->ic_oactive == 0) 4925640Sroot (*icp->ic_start)(ifp->if_unit); 4935640Sroot return (1); 4945640Sroot } 4955640Sroot 4965640Sroot /* 4975640Sroot * Put three 1822 NOOPs at the head of the output queue. 4985640Sroot * Part of host-IMP initialization procedure. 4995640Sroot * (Should return success/failure, but noone knows 5005640Sroot * what to do with this, so why bother?) 5015640Sroot */ 5025640Sroot impnoops(sc) 5035640Sroot register struct imp_softc *sc; 5045640Sroot { 5055640Sroot register i; 5065640Sroot register struct mbuf *m; 5075640Sroot register struct imp_leader *ip; 5085640Sroot int x; 5095640Sroot 5105640Sroot sc->imp_state = IMPS_INIT; 5115640Sroot sc->imp_dropcnt = IMP_DROPCNT; 5125640Sroot for (i = 0; i < IMP_DROPCNT; i++ ) { 5135640Sroot if ((m = m_getclr(M_DONTWAIT)) == 0) 5145640Sroot return; 5155640Sroot m->m_off = MMINOFF; 5165640Sroot m->m_len = sizeof(struct imp_leader); 5175640Sroot ip = mtod(m, struct imp_leader *); 5185640Sroot ip->il_format = IMP_NFF; 5195640Sroot ip->il_link = i; 5205640Sroot ip->il_mtype = IMPTYPE_NOOP; 5215640Sroot x = splimp(); 5225640Sroot IF_PREPEND(&sc->imp_if.if_snd, m); 5235640Sroot splx(x); 5245640Sroot } 5255640Sroot if (sc->imp_cb.ic_oactive == 0) 5265640Sroot (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 5275640Sroot } 5285640Sroot #endif 529