1 /* if_imp.c 4.20 82/03/28 */ 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 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 COUNT(IMPATTACH); 84 /* UNIT COULD BE AMBIGUOUS */ 85 ifp->if_unit = ui->ui_unit; 86 ifp->if_name = "imp"; 87 ifp->if_mtu = IMPMTU - sizeof(struct imp_leader); 88 ifp->if_net = ui->ui_flags; 89 /* the host and imp fields will be filled in by the imp */ 90 sin = (struct sockaddr_in *)&ifp->if_addr; 91 sin->sin_family = AF_INET; 92 sin->sin_addr = if_makeaddr(ifp->if_net, 0); 93 ifp->if_init = impinit; 94 ifp->if_output = impoutput; 95 /* reset is handled at the hardware level */ 96 if_attach(ifp); 97 return ((int)&sc->imp_if); 98 } 99 100 /* 101 * IMP initialization routine: call hardware module to 102 * setup UNIBUS resources, init state and get ready for 103 * NOOPs the IMP should send us, and that we want to drop. 104 */ 105 impinit(unit) 106 int unit; 107 { 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 return; 114 } 115 sc->imp_state = IMPS_INIT; 116 sc->imp_dropcnt = IMP_DROPCNT; 117 impnoops(sc); 118 } 119 120 struct sockproto impproto = { PF_IMPLINK }; 121 struct sockaddr_in impdst = { AF_IMPLINK }; 122 struct sockaddr_in impsrc = { AF_IMPLINK }; 123 124 /* 125 * ARPAnet 1822 input routine. 126 * Called from hardware input interrupt routine to handle 1822 127 * IMP-host messages. Type 0 messages (non-control) are 128 * passed to higher level protocol processors on the basis 129 * of link number. Other type messages (control) are handled here. 130 */ 131 impinput(unit, m) 132 int unit; 133 register struct mbuf *m; 134 { 135 register struct imp_leader *ip; 136 register struct imp_softc *sc = &imp_softc[unit]; 137 register struct host *hp; 138 register struct ifqueue *inq; 139 struct control_leader *cp; 140 struct in_addr addr; 141 struct mbuf *next; 142 struct sockaddr_in *sin; 143 144 COUNT(IMPINPUT); 145 /* verify leader length. */ 146 if (m->m_len < sizeof(struct control_leader) && 147 (m = m_pullup(m, sizeof(struct control_leader))) == 0) 148 return; 149 cp = mtod(m, struct control_leader *); 150 if (cp->dl_mtype == IMPTYPE_DATA) 151 if (m->m_len < sizeof(struct imp_leader) && 152 (m = m_pullup(m, sizeof(struct imp_leader))) == 0) 153 return; 154 ip = mtod(m, struct imp_leader *); 155 156 /* check leader type */ 157 if (ip->il_format != IMP_NFF) { 158 sc->imp_if.if_collisions++; /* XXX */ 159 goto drop; 160 } 161 162 /* 163 * Certain messages require a host structure. 164 * Do this in one shot here. 165 */ 166 switch (ip->il_mtype) { 167 168 case IMPTYPE_RFNM: 169 case IMPTYPE_INCOMPLETE: 170 case IMPTYPE_HOSTDEAD: 171 case IMPTYPE_HOSTUNREACH: 172 case IMPTYPE_BADDATA: 173 #ifdef notdef 174 addr.s_net = ip->il_network; 175 #else 176 addr.s_net = 0; 177 #endif 178 addr.s_imp = ip->il_imp; 179 addr.s_host = ip->il_host; 180 hp = hostlookup(addr); 181 break; 182 } 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); /* XXX */ 202 impnoops(sc); 203 } 204 goto rawlinkin; 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 ((ip->il_link & IMP_DMASK) == 0) { 213 sc->imp_state = IMPS_GOINGDOWN; 214 timeout(impdown, (caddr_t)sc, 30 * hz); 215 } 216 impmsg(sc, "going down %s", 217 (u_int)impmessage[ip->il_link&IMP_DMASK]); 218 goto rawlinkin; 219 220 /* 221 * A NOP usually seen during the initialization sequence. 222 * Compare the local address with that in the message. 223 * Reset the local address notion if it doesn't match. 224 */ 225 case IMPTYPE_NOOP: 226 if (sc->imp_state == IMPS_DOWN) { 227 sc->imp_state = IMPS_INIT; 228 sc->imp_dropcnt = IMP_DROPCNT; 229 } 230 if (sc->imp_state != IMPS_INIT || --sc->imp_dropcnt > 0) 231 goto drop; 232 sc->imp_state = IMPS_UP; 233 sc->imp_if.if_flags |= IFF_UP; 234 sin = (struct sockaddr_in *)&sc->imp_if.if_addr; 235 sc->imp_if.if_host[0] = sin->sin_addr.s_host = ip->il_host; 236 sin->sin_addr.s_imp = ip->il_imp; 237 impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host, 238 ntohs(ip->il_imp)); 239 /* restart output in case something was q'd */ 240 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 241 goto drop; 242 243 /* 244 * RFNM or INCOMPLETE message, record in 245 * host table and prime output routine. 246 * 247 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES. 248 */ 249 case IMPTYPE_RFNM: 250 case IMPTYPE_INCOMPLETE: 251 if (hp && hp->h_rfnm) 252 if (next = hostdeque(hp)) 253 (void) impsnd(&sc->imp_if, next); 254 goto drop; 255 256 /* 257 * Host or IMP can't be reached. Flush any packets 258 * awaiting transmission and release the host structure. 259 * 260 * TODO: NOTIFY THE PROTOCOL 261 */ 262 case IMPTYPE_HOSTDEAD: 263 impmsg(sc, "host dead"); /* XXX */ 264 goto common; /* XXX */ 265 266 /* SHOULD SIGNAL ROUTING DAEMON */ 267 case IMPTYPE_HOSTUNREACH: 268 impmsg(sc, "host unreachable"); /* XXX */ 269 common: 270 if (hp) 271 hostfree(hp); /* won't work right */ 272 goto rawlinkin; 273 274 /* 275 * Error in data. Clear RFNM status for this host and send 276 * noops to the IMP to clear the interface. 277 */ 278 case IMPTYPE_BADDATA: 279 impmsg(sc, "data error"); 280 if (hp) 281 hp->h_rfnm = 0; 282 impnoops(sc); 283 goto rawlinkin; 284 285 /* 286 * Interface reset. 287 */ 288 case IMPTYPE_RESET: 289 impmsg(sc, "interface reset"); 290 impnoops(sc); 291 goto rawlinkin; 292 293 default: 294 sc->imp_if.if_collisions++; /* XXX */ 295 goto rawlinkin; 296 } 297 298 /* 299 * Data for a protocol. Dispatch to the appropriate 300 * protocol routine (running at software interrupt). 301 * If this isn't a raw interface, advance pointer 302 * into mbuf past leader. 303 */ 304 switch (ip->il_link) { 305 306 #ifdef INET 307 case IMPLINK_IP: 308 m->m_len -= sizeof(struct imp_leader); 309 m->m_off += sizeof(struct imp_leader); 310 schednetisr(NETISR_IP); 311 inq = &ipintrq; 312 break; 313 #endif 314 315 default: 316 rawlinkin: 317 impproto.sp_protocol = ip->il_link; 318 sin = (struct sockaddr_in *)&sc->imp_if.if_addr; 319 impdst.sin_addr = sin->sin_addr;; 320 impsrc.sin_addr.s_net = ip->il_network; 321 impsrc.sin_addr.s_host = ip->il_host; 322 impsrc.sin_addr.s_imp = ip->il_imp; 323 raw_input(m, &impproto, (struct sockaddr *)&impdst, 324 (struct sockaddr *)&impsrc); 325 return; 326 } 327 if (IF_QFULL(inq)) { 328 IF_DROP(inq); 329 goto drop; 330 } 331 IF_ENQUEUE(inq, m); 332 return; 333 334 drop: 335 m_freem(m); 336 } 337 338 /* 339 * Bring the IMP down after notification. 340 */ 341 impdown(sc) 342 struct imp_softc *sc; 343 { 344 345 sc->imp_state = IMPS_DOWN; 346 sc->imp_if.if_flags &= ~IFF_UP; 347 impmsg(sc, "marked down"); 348 /* notify protocols with messages waiting? */ 349 } 350 351 /*VARARGS*/ 352 impmsg(sc, fmt, a1, a2) 353 struct imp_softc *sc; 354 char *fmt; 355 u_int a1; 356 { 357 358 printf("imp%d: ", sc->imp_if.if_unit); 359 printf(fmt, a1, a2); 360 printf("\n"); 361 } 362 363 /* 364 * ARPAnet 1822 output routine. 365 * Called from higher level protocol routines to set up messages for 366 * transmission to the imp. Sets up the header and calls impsnd to 367 * enqueue the message for this IMP's hardware driver. 368 */ 369 impoutput(ifp, m0, dst) 370 register struct ifnet *ifp; 371 struct mbuf *m0; 372 struct sockaddr *dst; 373 { 374 register struct imp_leader *imp; 375 register struct mbuf *m = m0; 376 int x, dhost, dimp, dlink, len, dnet; 377 378 COUNT(IMPOUTPUT); 379 /* 380 * Don't even try if the IMP is unavailable. 381 */ 382 x = imp_softc[ifp->if_unit].imp_state; 383 if (x == IMPS_DOWN || x == IMPS_GOINGDOWN) 384 goto drop; 385 386 switch (dst->sa_family) { 387 388 #ifdef INET 389 case AF_INET: { 390 struct ip *ip = mtod(m0, struct ip *); 391 struct sockaddr_in *sin = (struct sockaddr_in *)dst; 392 393 dhost = sin->sin_addr.s_host; 394 dimp = sin->sin_addr.s_impno; 395 dlink = IMPLINK_IP; 396 dnet = 0; 397 len = ntohs((u_short)ip->ip_len); 398 break; 399 } 400 #endif 401 case AF_IMPLINK: 402 goto leaderexists; 403 404 default: 405 printf("imp%d: can't handle af%d\n", ifp->if_unit, 406 dst->sa_family); 407 goto drop; 408 } 409 410 /* 411 * Add IMP leader. If there's not enough space in the 412 * first mbuf, allocate another. If that should fail, we 413 * drop this sucker. 414 */ 415 if (m->m_off > MMAXOFF || 416 MMINOFF + sizeof(struct imp_leader) > m->m_off) { 417 m = m_get(M_DONTWAIT); 418 if (m == 0) 419 goto drop; 420 m->m_next = m0; 421 m->m_off = MMINOFF; 422 m->m_len = sizeof(struct imp_leader); 423 } else { 424 m->m_off -= sizeof(struct imp_leader); 425 m->m_len += sizeof(struct imp_leader); 426 } 427 imp = mtod(m, struct imp_leader *); 428 imp->il_format = IMP_NFF; 429 imp->il_mtype = IMPTYPE_DATA; 430 imp->il_network = dnet; 431 imp->il_host = dhost; 432 imp->il_imp = htons((u_short)dimp); 433 imp->il_length = 434 htons((u_short)(len + sizeof(struct imp_leader)) << 3); 435 imp->il_link = dlink; 436 imp->il_flags = imp->il_htype = imp->il_subtype = 0; 437 438 leaderexists: 439 /* 440 * Hand message to impsnd to perform RFNM counting 441 * and eventual transmission. 442 */ 443 return (impsnd(ifp, m)); 444 drop: 445 m_freem(m0); 446 return (0); 447 } 448 449 /* 450 * Put a message on an interface's output queue. 451 * Perform RFNM counting: no more than 8 message may be 452 * in flight to any one host. 453 */ 454 impsnd(ifp, m) 455 struct ifnet *ifp; 456 struct mbuf *m; 457 { 458 register struct imp_leader *ip; 459 register struct host *hp; 460 struct impcb *icp; 461 int x; 462 463 COUNT(IMPSND); 464 ip = mtod(m, struct imp_leader *); 465 466 /* 467 * Do RFNM counting for data messages 468 * (no more than 8 outstanding to any host) 469 */ 470 x = splimp(); 471 if (ip->il_mtype == IMPTYPE_DATA) { 472 struct in_addr addr; 473 474 #ifdef notdef 475 addr.s_net = ip->il_network; 476 #else 477 addr.s_net = 0; 478 #endif 479 addr.s_host = ip->il_host; 480 addr.s_imp = ip->il_imp; 481 if ((hp = hostlookup(addr)) == 0) 482 hp = hostenter(addr); 483 484 /* 485 * If IMP would block, queue until RFNM 486 */ 487 if (hp) { 488 if (hp->h_rfnm < 8) { 489 hp->h_rfnm++; 490 goto enque; 491 } 492 if (hp->h_qcnt < 8) { /* high water mark */ 493 HOST_ENQUE(hp, m); 494 goto start; 495 } 496 } 497 m_freem(m); 498 splx(x); 499 return (0); 500 } 501 enque: 502 if (IF_QFULL(&ifp->if_snd)) { 503 IF_DROP(&ifp->if_snd); 504 m_freem(m); 505 splx(x); 506 return (0); 507 } 508 IF_ENQUEUE(&ifp->if_snd, m); 509 start: 510 splx(x); 511 icp = &imp_softc[ifp->if_unit].imp_cb; 512 if (icp->ic_oactive == 0) 513 (*icp->ic_start)(ifp->if_unit); 514 return (1); 515 } 516 517 /* 518 * Put three 1822 NOOPs at the head of the output queue. 519 * Part of host-IMP initialization procedure. 520 * (Should return success/failure, but noone knows 521 * what to do with this, so why bother?) 522 */ 523 impnoops(sc) 524 register struct imp_softc *sc; 525 { 526 register i; 527 register struct mbuf *m; 528 register struct control_leader *cp; 529 int x; 530 531 COUNT(IMPNOOPS); 532 sc->imp_state = IMPS_INIT; 533 sc->imp_dropcnt = IMP_DROPCNT; 534 for (i = 0; i < IMP_DROPCNT + 1; i++ ) { 535 if ((m = m_getclr(M_DONTWAIT)) == 0) 536 return; 537 m->m_off = MMINOFF; 538 m->m_len = sizeof(struct control_leader); 539 cp = mtod(m, struct control_leader *); 540 cp->dl_format = IMP_NFF; 541 cp->dl_link = i; 542 cp->dl_mtype = IMPTYPE_NOOP; 543 x = splimp(); 544 IF_PREPEND(&sc->imp_if.if_snd, m); 545 splx(x); 546 } 547 if (sc->imp_cb.ic_oactive == 0) 548 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 549 } 550 551 #ifdef IMPLEADERS 552 printleader(routine, ip) 553 char *routine; 554 register struct imp_leader *ip; 555 { 556 printf("%s: ", routine); 557 printbyte((char *)ip, 12); 558 printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network, 559 ip->il_flags); 560 if (ip->il_mtype <= IMPTYPE_READY) 561 printf("%s,", impleaders[ip->il_mtype]); 562 else 563 printf("%x,", ip->il_mtype); 564 printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host, 565 ntohs(ip->il_imp)); 566 if (ip->il_link == IMPLINK_IP) 567 printf("ip,"); 568 else 569 printf("%x,", ip->il_link); 570 printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3); 571 } 572 573 printbyte(cp, n) 574 register char *cp; 575 int n; 576 { 577 register i, j, c; 578 579 for (i=0; i<n; i++) { 580 c = *cp++; 581 for (j=0; j<2; j++) 582 putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]); 583 putchar(' '); 584 } 585 putchar('\n'); 586 } 587 #endif 588 #endif 589