1 /* if_imp.c 4.37 82/06/20 */ 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 /* UNIT COULD BE AMBIGUOUS */ 84 ifp->if_unit = ui->ui_unit; 85 ifp->if_name = "imp"; 86 ifp->if_mtu = IMPMTU - sizeof(struct imp_leader); 87 ifp->if_net = ui->ui_flags; 88 /* the host and imp fields will be filled in by the imp */ 89 sin = (struct sockaddr_in *)&ifp->if_addr; 90 sin->sin_family = AF_INET; 91 sin->sin_addr = if_makeaddr(ifp->if_net, 0); 92 ifp->if_init = impinit; 93 ifp->if_output = impoutput; 94 /* reset is handled at the hardware level */ 95 if_attach(ifp); 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 int s = splimp(); 108 register struct imp_softc *sc = &imp_softc[unit]; 109 110 if ((*sc->imp_cb.ic_init)(unit) == 0) { 111 sc->imp_state = IMPS_DOWN; 112 sc->imp_if.if_flags &= ~IFF_UP; 113 splx(s); 114 return; 115 } 116 sc->imp_state = IMPS_INIT; 117 impnoops(sc); 118 splx(s); 119 } 120 121 struct sockproto impproto = { PF_IMPLINK }; 122 struct sockaddr_in impdst = { AF_IMPLINK }; 123 struct sockaddr_in impsrc = { AF_IMPLINK }; 124 #ifdef IMPLEADERS 125 int impprintfs = 0; 126 #endif 127 128 /* 129 * ARPAnet 1822 input routine. 130 * Called from hardware input interrupt routine to handle 1822 131 * IMP-host messages. Type 0 messages (non-control) are 132 * passed to higher level protocol processors on the basis 133 * of link number. Other type messages (control) are handled here. 134 */ 135 impinput(unit, m) 136 int unit; 137 register struct mbuf *m; 138 { 139 register struct imp_leader *ip; 140 register struct imp_softc *sc = &imp_softc[unit]; 141 register struct host *hp; 142 register struct ifqueue *inq; 143 struct control_leader *cp; 144 struct in_addr addr; 145 struct mbuf *next; 146 struct sockaddr_in *sin; 147 148 /* verify leader length. */ 149 if (m->m_len < sizeof(struct control_leader) && 150 (m = m_pullup(m, sizeof(struct control_leader))) == 0) 151 return; 152 cp = mtod(m, struct control_leader *); 153 if (cp->dl_mtype == IMPTYPE_DATA) 154 if (m->m_len < sizeof(struct imp_leader) && 155 (m = m_pullup(m, sizeof(struct imp_leader))) == 0) 156 return; 157 ip = mtod(m, struct imp_leader *); 158 #ifdef IMPLEADERS 159 if (impprintfs) 160 printleader("impinput", ip); 161 #endif 162 163 /* check leader type */ 164 if (ip->il_format != IMP_NFF) { 165 sc->imp_if.if_collisions++; /* XXX */ 166 goto drop; 167 } 168 169 if (ip->il_mtype != IMPTYPE_DATA) { 170 #ifdef notdef 171 addr.s_net = ip->il_network; 172 #else 173 addr.s_net = sc->imp_if.if_net; 174 #endif 175 addr.s_imp = ip->il_imp; 176 addr.s_host = ip->il_host; 177 } 178 switch (ip->il_mtype) { 179 180 case IMPTYPE_DATA: 181 break; 182 183 /* 184 * IMP leader error. Reset the IMP and discard the packet. 185 */ 186 case IMPTYPE_BADLEADER: 187 /* 188 * According to 1822 document, this message 189 * will be generated in response to the 190 * first noop sent to the IMP after 191 * the host resets the IMP interface. 192 */ 193 if (sc->imp_state != IMPS_INIT) { 194 impmsg(sc, "leader error"); 195 hostreset(sc->imp_if.if_net); 196 impnoops(sc); 197 } 198 goto drop; 199 200 /* 201 * IMP going down. Print message, and if not immediate, 202 * set off a timer to insure things will be reset at the 203 * appropriate time. 204 */ 205 case IMPTYPE_DOWN: 206 if (sc->imp_state < IMPS_INIT) 207 goto drop; 208 if ((ip->il_link & IMP_DMASK) == 0) { 209 sc->imp_state = IMPS_GOINGDOWN; 210 timeout(impdown, (caddr_t)sc, 30 * hz); 211 } 212 impmsg(sc, "going down %s", 213 (u_int)impmessage[ip->il_link&IMP_DMASK]); 214 goto drop; 215 216 /* 217 * A NOP usually seen during the initialization sequence. 218 * Compare the local address with that in the message. 219 * Reset the local address notion if it doesn't match. 220 */ 221 case IMPTYPE_NOOP: 222 if (sc->imp_state == IMPS_DOWN) { 223 sc->imp_state = IMPS_INIT; 224 sc->imp_dropcnt = IMP_DROPCNT; 225 } 226 if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0) 227 goto drop; 228 sin = (struct sockaddr_in *)&sc->imp_if.if_addr; 229 if (sin->sin_addr.s_host != ip->il_host || 230 sin->sin_addr.s_imp != ip->il_imp) { 231 sc->imp_if.if_host[0] = 232 sin->sin_addr.s_host = ip->il_host; 233 sin->sin_addr.s_imp = ip->il_imp; 234 impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host, 235 ntohs(ip->il_imp)); 236 } 237 sc->imp_state = IMPS_UP; 238 sc->imp_if.if_flags |= IFF_UP; 239 if_rtinit(&sc->imp_if, RTF_UP); 240 goto drop; 241 242 /* 243 * RFNM or INCOMPLETE message, send next 244 * message on the q. We could pass incomplete's 245 * up to the next level, but this currently isn't 246 * needed. 247 */ 248 case IMPTYPE_RFNM: 249 case IMPTYPE_INCOMPLETE: 250 if (hp = hostlookup(addr)) { 251 if (hp->h_rfnm == 0) 252 hp->h_flags &= ~HF_INUSE; 253 else if (next = hostdeque(hp)) 254 (void) impsnd(&sc->imp_if, next); 255 } 256 goto drop; 257 258 /* 259 * Host or IMP can't be reached. Flush any packets 260 * awaiting transmission and release the host structure. 261 */ 262 case IMPTYPE_HOSTDEAD: 263 case IMPTYPE_HOSTUNREACH: { 264 int s = splnet(); 265 impnotify(ip->il_mtype, ip, hostlookup(addr)); 266 splx(s); 267 goto rawlinkin; 268 } 269 270 /* 271 * Error in data. Clear RFNM status for this host and send 272 * noops to the IMP to clear the interface. 273 */ 274 case IMPTYPE_BADDATA: { 275 int s; 276 277 impmsg(sc, "data error"); 278 s = splnet(); 279 if (hp = hostlookup(addr)) 280 hp->h_rfnm = 0; 281 splx(s); 282 impnoops(sc); 283 goto drop; 284 } 285 286 /* 287 * Interface reset. 288 */ 289 case IMPTYPE_RESET: 290 impmsg(sc, "interface reset"); 291 impnoops(sc); 292 goto drop; 293 294 default: 295 sc->imp_if.if_collisions++; /* XXX */ 296 goto drop; 297 } 298 299 /* 300 * Data for a protocol. Dispatch to the appropriate 301 * protocol routine (running at software interrupt). 302 * If this isn't a raw interface, advance pointer 303 * into mbuf past leader. 304 */ 305 switch (ip->il_link) { 306 307 #ifdef INET 308 case IMPLINK_IP: 309 m->m_len -= sizeof(struct imp_leader); 310 m->m_off += sizeof(struct imp_leader); 311 schednetisr(NETISR_IP); 312 inq = &ipintrq; 313 break; 314 #endif 315 316 default: 317 rawlinkin: 318 impproto.sp_protocol = ip->il_link; 319 sin = (struct sockaddr_in *)&sc->imp_if.if_addr; 320 impdst.sin_addr = sin->sin_addr;; 321 impsrc.sin_addr.s_net = ip->il_network; 322 impsrc.sin_addr.s_host = ip->il_host; 323 impsrc.sin_addr.s_imp = ip->il_imp; 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 346 sc->imp_state = IMPS_DOWN; 347 impmsg(sc, "marked down"); 348 hostreset(sc->imp_if.if_net); 349 if_down(&sc->imp_if); 350 } 351 352 /*VARARGS*/ 353 impmsg(sc, fmt, a1, a2) 354 struct imp_softc *sc; 355 char *fmt; 356 u_int a1; 357 { 358 359 printf("imp%d: ", sc->imp_if.if_unit); 360 printf(fmt, a1, a2); 361 printf("\n"); 362 } 363 364 /* 365 * Process an IMP "error" message, passing this 366 * up to the higher level protocol. 367 */ 368 impnotify(what, cp, hp) 369 int what; 370 struct control_leader *cp; 371 struct host *hp; 372 { 373 struct in_addr in; 374 375 #ifdef notdef 376 in.s_net = cp->dl_network; 377 #else 378 in.s_net = 10; /* XXX */ 379 #endif 380 in.s_host = cp->dl_host; 381 in.s_imp = cp->dl_imp; 382 if (cp->dl_link != IMPLINK_IP) 383 raw_ctlinput(what, (caddr_t)&in); 384 else 385 ip_ctlinput(what, (caddr_t)&in); 386 if (hp) { 387 hp->h_flags |= (1 << what); 388 hostfree(hp); 389 } 390 } 391 392 /* 393 * ARPAnet 1822 output routine. 394 * Called from higher level protocol routines to set up messages for 395 * transmission to the imp. Sets up the header and calls impsnd to 396 * enqueue the message for this IMP's hardware driver. 397 */ 398 impoutput(ifp, m0, dst) 399 register struct ifnet *ifp; 400 struct mbuf *m0; 401 struct sockaddr *dst; 402 { 403 register struct imp_leader *imp; 404 register struct mbuf *m = m0; 405 int x, dhost, dimp, dlink, len, dnet; 406 int error = 0; 407 408 /* 409 * Don't even try if the IMP is unavailable. 410 */ 411 if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) { 412 error = ENETDOWN; 413 goto drop; 414 } 415 416 switch (dst->sa_family) { 417 418 #ifdef INET 419 case AF_INET: { 420 struct ip *ip = mtod(m0, struct ip *); 421 struct sockaddr_in *sin = (struct sockaddr_in *)dst; 422 423 dhost = sin->sin_addr.s_host; 424 dimp = sin->sin_addr.s_impno; 425 dlink = IMPLINK_IP; 426 dnet = 0; 427 len = ntohs((u_short)ip->ip_len); 428 break; 429 } 430 #endif 431 case AF_IMPLINK: 432 goto leaderexists; 433 434 default: 435 printf("imp%d: can't handle af%d\n", ifp->if_unit, 436 dst->sa_family); 437 error = EAFNOSUPPORT; 438 goto drop; 439 } 440 441 /* 442 * Add IMP leader. If there's not enough space in the 443 * first mbuf, allocate another. If that should fail, we 444 * drop this sucker. 445 */ 446 if (m->m_off > MMAXOFF || 447 MMINOFF + sizeof(struct imp_leader) > m->m_off) { 448 m = m_get(M_DONTWAIT); 449 if (m == 0) { 450 error = ENOBUFS; 451 goto drop; 452 } 453 m->m_next = m0; 454 m->m_off = MMINOFF; 455 m->m_len = sizeof(struct imp_leader); 456 } else { 457 m->m_off -= sizeof(struct imp_leader); 458 m->m_len += sizeof(struct imp_leader); 459 } 460 imp = mtod(m, struct imp_leader *); 461 imp->il_format = IMP_NFF; 462 imp->il_mtype = IMPTYPE_DATA; 463 imp->il_network = dnet; 464 imp->il_host = dhost; 465 imp->il_imp = htons((u_short)dimp); 466 imp->il_length = 467 htons((u_short)(len + sizeof(struct imp_leader)) << 3); 468 imp->il_link = dlink; 469 imp->il_flags = imp->il_htype = imp->il_subtype = 0; 470 471 leaderexists: 472 return (impsnd(ifp, m)); 473 drop: 474 m_freem(m0); 475 return (error); 476 } 477 478 /* 479 * Put a message on an interface's output queue. 480 * Perform RFNM counting: no more than 8 message may be 481 * in flight to any one host. 482 */ 483 impsnd(ifp, m) 484 struct ifnet *ifp; 485 struct mbuf *m; 486 { 487 register struct imp_leader *ip; 488 register struct host *hp; 489 struct impcb *icp; 490 int s, error; 491 492 ip = mtod(m, struct imp_leader *); 493 494 /* 495 * Do RFNM counting for data messages 496 * (no more than 8 outstanding to any host) 497 */ 498 s = splimp(); 499 if (ip->il_mtype == IMPTYPE_DATA) { 500 struct in_addr addr; 501 502 #ifdef notdef 503 addr.s_net = ip->il_network; 504 #else 505 addr.s_net = ifp->if_net; /* XXX */ 506 #endif 507 addr.s_host = ip->il_host; 508 addr.s_imp = ip->il_imp; 509 if ((hp = hostlookup(addr)) == 0) 510 hp = hostenter(addr); 511 if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) { 512 error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH; 513 hp->h_timer = HOSTTIMER; 514 hp->h_flags &= ~HF_INUSE; 515 goto bad; 516 } 517 518 /* 519 * If IMP would block, queue until RFNM 520 */ 521 if (hp) { 522 if (hp->h_rfnm < 8) { 523 hp->h_rfnm++; 524 goto enque; 525 } 526 if (hp->h_qcnt < 8) { /* high water mark */ 527 HOST_ENQUE(hp, m); 528 goto start; 529 } 530 } 531 error = ENOBUFS; 532 goto bad; 533 } 534 enque: 535 if (IF_QFULL(&ifp->if_snd)) { 536 IF_DROP(&ifp->if_snd); 537 error = ENOBUFS; 538 bad: 539 m_freem(m); 540 splx(s); 541 return (error); 542 } 543 IF_ENQUEUE(&ifp->if_snd, m); 544 start: 545 icp = &imp_softc[ifp->if_unit].imp_cb; 546 if (icp->ic_oactive == 0) 547 (*icp->ic_start)(ifp->if_unit); 548 splx(s); 549 return (0); 550 } 551 552 /* 553 * Put three 1822 NOOPs at the head of the output queue. 554 * Part of host-IMP initialization procedure. 555 * (Should return success/failure, but noone knows 556 * what to do with this, so why bother?) 557 * This routine is always called at splimp, so we don't 558 * protect the call to IF_PREPEND. 559 */ 560 impnoops(sc) 561 register struct imp_softc *sc; 562 { 563 register i; 564 register struct mbuf *m; 565 register struct control_leader *cp; 566 int x; 567 568 sc->imp_dropcnt = IMP_DROPCNT; 569 for (i = 0; i < IMP_DROPCNT + 1; i++ ) { 570 if ((m = m_getclr(M_DONTWAIT)) == 0) 571 return; 572 m->m_off = MMINOFF; 573 m->m_len = sizeof(struct control_leader); 574 cp = mtod(m, struct control_leader *); 575 cp->dl_format = IMP_NFF; 576 cp->dl_link = i; 577 cp->dl_mtype = IMPTYPE_NOOP; 578 IF_PREPEND(&sc->imp_if.if_snd, m); 579 } 580 if (sc->imp_cb.ic_oactive == 0) 581 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 582 } 583 584 #ifdef IMPLEADERS 585 printleader(routine, ip) 586 char *routine; 587 register struct imp_leader *ip; 588 { 589 printf("%s: ", routine); 590 printbyte((char *)ip, 12); 591 printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network, 592 ip->il_flags); 593 if (ip->il_mtype <= IMPTYPE_READY) 594 printf("%s,", impleaders[ip->il_mtype]); 595 else 596 printf("%x,", ip->il_mtype); 597 printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host, 598 ntohs(ip->il_imp)); 599 if (ip->il_link == IMPLINK_IP) 600 printf("ip,"); 601 else 602 printf("%x,", ip->il_link); 603 printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3); 604 } 605 606 printbyte(cp, n) 607 register char *cp; 608 int n; 609 { 610 register i, j, c; 611 612 for (i=0; i<n; i++) { 613 c = *cp++; 614 for (j=0; j<2; j++) 615 putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]); 616 putchar(' '); 617 } 618 putchar('\n'); 619 } 620 #endif 621 #endif 622