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