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