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