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