1 /* if_imp.c 4.7 82/02/16 */ 2 3 #include "imp.h" 4 #if NIMP > 0 5 /* 6 * ARPAnet IMP interface driver. 7 * 8 * The IMP-host protocol is handled here, leaving 9 * hardware specifics to the lower level interface driver. 10 * 11 * TODO: 12 * rethink coupling between this module and device driver 13 * pass more error indications up to protocol modules 14 */ 15 #include "../h/param.h" 16 #include "../h/systm.h" 17 #include "../h/mbuf.h" 18 #include "../h/pte.h" 19 #include "../h/buf.h" 20 #include "../h/protosw.h" 21 #include "../h/socket.h" 22 #include "../h/ubareg.h" 23 #include "../h/ubavar.h" 24 #include "../h/cpu.h" 25 #include "../h/mtpr.h" 26 #include "../h/vmmac.h" 27 #include "../net/in.h" 28 #include "../net/in_systm.h" 29 #include "../net/if.h" 30 #include "../net/if_imp.h" 31 #include "../net/if_imphost.h" 32 #include "../net/ip.h" 33 #include "../net/ip_var.h" 34 35 /* 36 * IMP software status per interface. 37 * (partially shared with the hardware specific module) 38 * 39 * Each interface is referenced by a network interface structure, 40 * imp_if, which the routing code uses to locate the interface. 41 * This structure contains the output queue for the interface, its 42 * address, ... IMP specific structures used in connecting the 43 * IMP software modules to the hardware specific interface routines 44 * are stored here. The common structures are made visible to the 45 * interface driver by passing a pointer to the hardware routine 46 * at "attach" time. 47 * 48 * NOTE: imp_if and imp_cb are assumed adjacent in hardware code. 49 */ 50 struct imp_softc { 51 struct ifnet imp_if; /* network visible interface */ 52 struct impcb imp_cb; /* hooks to hardware module */ 53 u_char imp_state; /* current state of IMP */ 54 char imp_dropcnt; /* used during initialization */ 55 } imp_softc[NIMP]; 56 57 /* 58 * Messages from IMP regarding why 59 * it's going down. 60 */ 61 static char *impmsg[] = { 62 "in 30 seconds", 63 "for hardware PM", 64 "to reload software", 65 "for emergency reset" 66 }; 67 68 int impdown(), impinit(), impoutput(); 69 70 /* 71 * IMP attach routine. Called from hardware device attach routine 72 * at configuration time with a pointer to the UNIBUS device structure. 73 * Sets up local state and returns pointer to base of ifnet+impcb 74 * structures. This is then used by the device's attach routine 75 * set up its back pointers. 76 */ 77 impattach(ui) 78 struct uba_device *ui; 79 { 80 struct imp_softc *sc = &imp_softc[ui->ui_unit]; 81 register struct ifnet *ifp = &sc->imp_if; 82 83 COUNT(IMPATTACH); 84 /* UNIT COULD BE AMBIGUOUS */ 85 ifp->if_unit = ui->ui_unit; 86 ifp->if_name = "imp"; 87 ifp->if_mtu = IMP_MTU; 88 ifp->if_net = ui->ui_flags; 89 /* this should be found by talking to the imp */ 90 ifp->if_addr.s_addr = 0x4e00000a;; 91 ifp->if_init = impinit; 92 ifp->if_output = impoutput; 93 /* reset is handled at the hardware level */ 94 if_attach(ifp); 95 /* kludge to hand pointers back to hardware attach routine */ 96 return ((int)&sc->imp_if); 97 } 98 99 /* 100 * IMP initialization routine: call hardware module to 101 * setup UNIBUS resources, init state and get ready for 102 * NOOPs the IMP should send us, and that we want to drop. 103 */ 104 impinit(unit) 105 int unit; 106 { 107 register struct imp_softc *sc = &imp_softc[unit]; 108 109 if ((*sc->imp_cb.ic_init)(unit) == 0) { 110 sc->imp_state = IMPS_DOWN; 111 return; 112 } 113 sc->imp_state = IMPS_INIT; 114 sc->imp_dropcnt = IMP_DROPCNT; 115 impnoops(sc); 116 } 117 118 struct sockproto impproto = { PF_IMPLINK }; 119 struct sockaddr_in impdst = { AF_IMPLINK }; 120 struct sockaddr_in impsrc = { AF_IMPLINK }; 121 122 /* 123 * ARPAnet 1822 input routine. 124 * Called from hardware input interrupt routine to handle 1822 125 * IMP-host messages. Type 0 messages (non-control) are 126 * passed to higher level protocol processors on the basis 127 * of link number. Other type messages (control) are handled here. 128 */ 129 impinput(unit, m) 130 int unit; 131 register struct mbuf *m; 132 { 133 int s; 134 register struct imp_leader *ip; 135 register struct imp_softc *sc = &imp_softc[unit]; 136 register struct host *hp; 137 register struct ifqueue *inq; 138 struct control_leader *cp; 139 struct in_addr addr; 140 141 COUNT(IMP_INPUT); 142 /* 143 * Verify leader length. Be careful with control 144 * message which don't get a length included. 145 * We should generate a "bad leader" message 146 * to the IMP about messages too short. 147 */ 148 if (m->m_len < sizeof(struct control_leader) && 149 (m = m_pullup(m, sizeof(struct control_leader))) == 0) 150 return; 151 cp = mtod(m, struct control_leader *); 152 if (cp->dl_mtype == IMPTYPE_DATA) 153 if (m->m_len < sizeof(struct imp_leader) && 154 (m = m_pullup(m, sizeof(struct imp_leader))) == 0) 155 return; 156 ip = mtod(m, struct imp_leader *); 157 158 /* 159 * Check leader type -- should notify IMP 160 * in case of failure... 161 */ 162 if (ip->il_format != IMP_NFF) { 163 sc->imp_if.if_collisions++; /* XXX */ 164 goto drop; 165 } 166 167 /* 168 * Certain messages require a host structure. 169 * Do this in one shot here. 170 */ 171 switch (ip->il_mtype) { 172 173 case IMPTYPE_RFNM: 174 case IMPTYPE_INCOMPLETE: 175 case IMPTYPE_HOSTDEAD: 176 case IMPTYPE_HOSTUNREACH: 177 case IMPTYPE_BADDATA: 178 #ifdef notdef 179 addr.s_net = ip->il_network; 180 #else 181 addr.s_net = 0; 182 #endif 183 addr.s_imp = ip->il_imp; 184 addr.s_host = ip->il_host; 185 hp = hostlookup(addr); 186 break; 187 } 188 189 switch (ip->il_mtype) { 190 191 /* 192 * Data for a protocol. Dispatch to the appropriate 193 * protocol routine (running at software interrupt). 194 * If this isn't a raw interface, advance pointer 195 * into mbuf past leader (done below). 196 */ 197 case IMPTYPE_DATA: 198 ip->il_length = 199 (ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader); 200 break; 201 202 /* 203 * IMP leader error. Reset the IMP and discard the packet. 204 */ 205 case IMPTYPE_BADLEADER: 206 /* 207 * According to 1822 document, this message 208 * will be generated in response to the 209 * first noop sent to the IMP after 210 * the host resets the IMP interface. 211 */ 212 if (sc->imp_state != IMPS_INIT) { 213 imperr(sc, "leader error"); 214 hostreset(sc->imp_if.if_net); /* XXX */ 215 impnoops(sc); 216 } 217 goto drop; 218 219 /* 220 * IMP going down. Print message, and if not immediate, 221 * set off a timer to insure things will be reset at the 222 * appropriate time. 223 */ 224 case IMPTYPE_DOWN: 225 if ((ip->il_link & IMP_DMASK) == 0) { 226 sc->imp_state = IMPS_GOINGDOWN; 227 timeout(impdown, sc, 30 * hz); 228 } 229 imperr(sc, "going down %s", impmsg[ip->il_link & IMP_DMASK]); 230 goto drop; 231 232 /* 233 * A NOP usually seen during the initialization sequence. 234 * Compare the local address with that in the message. 235 * Reset the local address notion if it doesn't match. 236 */ 237 case IMPTYPE_NOOP: { 238 register struct in_addr *sin; 239 240 if (sc->imp_state == IMPS_DOWN) { 241 sc->imp_state = IMPS_INIT; 242 sc->imp_dropcnt = IMP_DROPCNT; 243 } 244 if (sc->imp_state != IMPS_INIT) 245 goto drop; 246 if (--sc->imp_dropcnt > 0) 247 goto drop; 248 sc->imp_state = IMPS_UP; 249 sin = &sc->imp_if.if_addr; 250 sc->imp_if.if_host[0] = sin->s_host = ip->il_host; 251 sin->s_imp = ip->il_imp; 252 imperr(sc, "reset (host %d/imp %d)", ip->il_host, 253 ntohs(ip->il_imp)); 254 /* restart output in case something was q'd */ 255 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 256 goto drop; 257 } 258 259 /* 260 * RFNM or INCOMPLETE message, record in 261 * host table and prime output routine. 262 * 263 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES. 264 */ 265 case IMPTYPE_RFNM: 266 case IMPTYPE_INCOMPLETE: 267 if (hp && hp->h_rfnm) { 268 register struct mbuf *n; 269 270 hp->h_rfnm--; 271 /* poke holding queue */ 272 if (n = hp->h_q) { 273 if (n->m_act == n) 274 hp->h_q = 0; 275 else { 276 n = n->m_act; 277 hp->h_q->m_act = n->m_act; 278 } 279 (void) impsnd(sc, n); 280 goto rawlinkin; 281 } 282 if (hp->h_rfnm == 0) 283 hostfree(hp); 284 } 285 goto rawlinkin; 286 287 /* 288 * Host or IMP can't be reached. Flush any packets 289 * awaiting transmission and release the host structure. 290 * 291 * TODO: NOTIFY THE PROTOCOL 292 */ 293 case IMPTYPE_HOSTDEAD: 294 imperr(sc, "host dead"); /* XXX */ 295 goto common; /* XXX */ 296 297 /* SHOULD SIGNAL ROUTING DAEMON */ 298 case IMPTYPE_HOSTUNREACH: 299 imperr(sc, "host unreachable"); /* XXX */ 300 common: 301 if (hp) 302 hostfree(hp); /* won't work right */ 303 goto rawlinkin; 304 305 /* 306 * Error in data. Clear RFNM status for this host and send 307 * noops to the IMP to clear the interface. 308 */ 309 case IMPTYPE_BADDATA: 310 imperr(sc, "data error"); 311 if (hp) 312 hp->h_rfnm = 0; 313 impnoops(sc); 314 goto rawlinkin; 315 316 /* 317 * Interface reset. 318 */ 319 case IMPTYPE_RESET: 320 imperr(sc, "interface reset"); 321 impnoops(sc); 322 goto drop; 323 324 default: 325 sc->imp_if.if_collisions++; /* XXX */ 326 goto drop; 327 } 328 329 /* 330 * Queue on protocol's input queue. 331 */ 332 switch (ip->il_link) { 333 334 #ifdef INET 335 case IMPLINK_IP: 336 m->m_len -= sizeof(struct imp_leader); 337 m->m_off += sizeof(struct imp_leader); 338 setipintr(); 339 inq = &ipintrq; 340 break; 341 #endif 342 343 default: 344 rawlinkin: 345 impproto.sp_protocol = ip->il_link; 346 impdst.sin_addr = sc->imp_if.if_addr; 347 impsrc.sin_addr.s_net = ip->il_network; 348 impsrc.sin_addr.s_host = ip->il_host; 349 impsrc.sin_addr.s_imp = ip->il_imp; 350 raw_input(m, &impproto, &impdst, &impsrc); 351 return; 352 } 353 IF_ENQUEUE(inq, m); 354 return; 355 356 drop: 357 m_freem(m); 358 } 359 360 /* 361 * Bring the IMP down after notification. 362 */ 363 impdown(sc) 364 struct imp_softc *sc; 365 { 366 sc->imp_state = IMPS_DOWN; 367 imperr(sc, "marked down"); 368 /* notify protocols with messages waiting? */ 369 } 370 371 /*VARARGS*/ 372 imperr(sc, fmt, a1, a2) 373 struct imp_softc *sc; 374 char *fmt; 375 { 376 printf("imp%d: ", sc->imp_if.if_unit); 377 printf(fmt, a1, a2); 378 printf("\n"); 379 } 380 381 /* 382 * ARPAnet 1822 output routine. 383 * Called from higher level protocol routines to set up messages for 384 * transmission to the imp. Sets up the header and calls impsnd to 385 * enqueue the message for this IMP's hardware driver. 386 */ 387 impoutput(ifp, m0, pf) 388 register struct ifnet *ifp; 389 struct mbuf *m0; 390 { 391 register struct imp_leader *imp; 392 register struct mbuf *m = m0; 393 int x, dhost, dimp, dlink, len, dnet; 394 395 COUNT(IMPOUTPUT); 396 #ifdef notdef 397 /* 398 * Don't even try if the IMP is unavailable. 399 */ 400 x = imp_softc[ifp->if_unit].imp_state; 401 if (x == IMPS_DOWN || x == IMPS_GOINGDOWN) 402 goto drop; 403 #endif 404 405 switch (pf) { 406 407 #ifdef INET 408 case PF_INET: { 409 register struct ip *ip = mtod(m0, struct ip *); 410 411 dnet = ip->ip_dst.s_net; 412 dhost = ip->ip_dst.s_host; 413 dimp = ip->ip_dst.s_imp; 414 dlink = IMPLINK_IP; 415 len = ntohs(ip->ip_len); 416 break; 417 } 418 #endif 419 case PF_IMPLINK: 420 goto leaderexists; 421 422 default: 423 printf("imp%d: can't encapsulate pf%d\n", ifp->if_unit, pf); 424 goto drop; 425 } 426 427 /* 428 * Add IMP leader. If there's not enough space in the 429 * first mbuf, allocate another. If that should fail, we 430 * drop this sucker. 431 */ 432 if (m->m_off > MMAXOFF || 433 MMINOFF + sizeof(struct imp_leader) > m->m_off) { 434 m = m_get(M_DONTWAIT); 435 if (m == 0) 436 goto drop; 437 m->m_next = m0; 438 m->m_off = MMINOFF; 439 m->m_len = sizeof(struct imp_leader); 440 } else { 441 m->m_off -= sizeof(struct imp_leader); 442 m->m_len += sizeof(struct imp_leader); 443 } 444 imp = mtod(m, struct imp_leader *); 445 imp->il_format = IMP_NFF; 446 imp->il_mtype = IMPTYPE_DATA; 447 #ifdef notdef 448 imp->il_network = dnet; 449 #else 450 imp->il_network = 0; 451 #endif 452 imp->il_host = dhost; 453 imp->il_imp = dimp; 454 imp->il_length = htons((len + sizeof(struct imp_leader)) << 3); 455 imp->il_link = dlink; 456 imp->il_flags = imp->il_htype = imp->il_subtype = 0; 457 458 leaderexists: 459 /* 460 * Hand message to impsnd to perform RFNM counting 461 * and eventual transmission. 462 */ 463 return (impsnd(ifp, m)); 464 drop: 465 m_freem(m0); 466 return (0); 467 } 468 469 /* 470 * Put a message on an interface's output queue. 471 * Perform RFNM counting: no more than 8 message may be 472 * in flight to any one host. 473 */ 474 impsnd(ifp, m) 475 struct ifnet *ifp; 476 struct mbuf *m; 477 { 478 register struct imp_leader *ip; 479 register struct host *hp; 480 struct impcb *icp; 481 int x; 482 483 COUNT(IMPSND); 484 ip = mtod(m, struct imp_leader *); 485 486 /* 487 * Do RFNM counting for data messages 488 * (no more than 8 outstanding to any host) 489 */ 490 if (ip->il_mtype == IMPTYPE_DATA) { 491 struct in_addr addr; 492 493 #ifdef notdef 494 addr.s_net = ip->il_network; 495 #else 496 addr.s_net = 0; 497 #endif 498 addr.s_host = ip->il_host; 499 addr.s_imp = ip->il_imp; 500 x = splimp(); 501 if ((hp = hostlookup(addr)) == 0) 502 hp = hostenter(addr); 503 504 /* 505 * If IMP would block, queue until RFNM 506 */ 507 if (hp) { 508 register struct mbuf *n; 509 int cnt; 510 511 if (hp->h_rfnm < 8) { 512 hp->h_rfnm++; 513 splx(x); 514 goto enque; 515 } 516 /* 517 * Keeping the count in the host structure 518 * causes the packing scheme to lose too much. 519 */ 520 cnt = 0; 521 if (n = hp->h_q) 522 do { 523 n = n->m_act; 524 cnt++; 525 } while (n != hp->h_q); 526 if (cnt >= 8) 527 goto drop; 528 529 /* 530 * Q is kept as circular list with h_q 531 * (head) pointing to the last entry. 532 */ 533 if ((n = hp->h_q) == 0) 534 hp->h_q = m->m_act = m; 535 else { 536 m->m_act = n->m_act; 537 hp->h_q = n->m_act = m; 538 } 539 splx(x); 540 goto start; 541 } 542 drop: 543 m_freem(m); 544 splx(x); 545 return (0); 546 } 547 enque: 548 x = splimp(); 549 IF_ENQUEUE(&ifp->if_snd, m); 550 splx(x); 551 552 start: 553 icp = &imp_softc[ifp->if_unit].imp_cb; 554 if (icp->ic_oactive == 0) 555 (*icp->ic_start)(ifp->if_unit); 556 return (1); 557 } 558 559 /* 560 * Put three 1822 NOOPs at the head of the output queue. 561 * Part of host-IMP initialization procedure. 562 * (Should return success/failure, but noone knows 563 * what to do with this, so why bother?) 564 */ 565 impnoops(sc) 566 register struct imp_softc *sc; 567 { 568 register i; 569 register struct mbuf *m; 570 register struct control_leader *cp; 571 int x; 572 573 COUNT(IMPNOOPS); 574 sc->imp_state = IMPS_INIT; 575 sc->imp_dropcnt = IMP_DROPCNT; 576 for (i = 0; i < IMP_DROPCNT + 1; i++ ) { 577 if ((m = m_getclr(M_DONTWAIT)) == 0) 578 return; 579 m->m_off = MMINOFF; 580 m->m_len = sizeof(struct control_leader); 581 cp = mtod(m, struct control_leader *); 582 cp->dl_format = IMP_NFF; 583 cp->dl_link = i; 584 cp->dl_mtype = IMPTYPE_NOOP; 585 x = splimp(); 586 IF_PREPEND(&sc->imp_if.if_snd, m); 587 splx(x); 588 } 589 if (sc->imp_cb.ic_oactive == 0) 590 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 591 } 592 593 #ifdef IMPLEADERS 594 printleader(routine, ip) 595 char *routine; 596 register struct imp_leader *ip; 597 { 598 printf("%s: ", routine); 599 printbyte((char *)ip, 12); 600 printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network, 601 ip->il_flags); 602 if (ip->il_mtype <= IMPTYPE_READY) 603 printf("%s,", impleaders[ip->il_mtype]); 604 else 605 printf("%x,", ip->il_mtype); 606 printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host, 607 ip->il_impno); 608 if (ip->il_link == IMPLINK_IP) 609 printf("ip,"); 610 else 611 printf("%x,", ip->il_link); 612 printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3); 613 } 614 615 printbyte(cp, n) 616 register char *cp; 617 int n; 618 { 619 register i, j, c; 620 621 for (i=0; i<n; i++) { 622 c = *cp++; 623 for (j=0; j<2; j++) 624 putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]); 625 putchar(' '); 626 } 627 putchar('\n'); 628 } 629 #endif 630 #endif 631