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