1 /* if_imp.c 4.32 82/05/14 */ 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 #include "../net/if_imp.h" 27 #include "../net/if_imphost.h" 28 #include "../net/ip.h" 29 #include "../net/ip_var.h" 30 #include "../net/route.h" 31 #include <errno.h> 32 33 /* 34 * IMP software status per interface. 35 * (partially shared with the hardware specific module) 36 * 37 * Each interface is referenced by a network interface structure, 38 * imp_if, which the routing code uses to locate the interface. 39 * This structure contains the output queue for the interface, its 40 * address, ... IMP specific structures used in connecting the 41 * IMP software modules to the hardware specific interface routines 42 * are stored here. The common structures are made visible to the 43 * interface driver by passing a pointer to the hardware routine 44 * at "attach" time. 45 * 46 * NOTE: imp_if and imp_cb are assumed adjacent in hardware code. 47 */ 48 struct imp_softc { 49 struct ifnet imp_if; /* network visible interface */ 50 struct impcb imp_cb; /* hooks to hardware module */ 51 u_char imp_state; /* current state of IMP */ 52 char imp_dropcnt; /* used during initialization */ 53 } imp_softc[NIMP]; 54 55 /* 56 * Messages from IMP regarding why 57 * it's going down. 58 */ 59 static char *impmessage[] = { 60 "in 30 seconds", 61 "for hardware PM", 62 "to reload software", 63 "for emergency reset" 64 }; 65 66 int impdown(), impinit(), impoutput(); 67 68 /* 69 * IMP attach routine. Called from hardware device attach routine 70 * at configuration time with a pointer to the UNIBUS device structure. 71 * Sets up local state and returns pointer to base of ifnet+impcb 72 * structures. This is then used by the device's attach routine 73 * set up its back pointers. 74 */ 75 impattach(ui) 76 struct uba_device *ui; 77 { 78 struct imp_softc *sc = &imp_softc[ui->ui_unit]; 79 register struct ifnet *ifp = &sc->imp_if; 80 struct sockaddr_in *sin; 81 82 COUNT(IMPATTACH); 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 COUNT(IMPINIT); 111 if ((*sc->imp_cb.ic_init)(unit) == 0) { 112 sc->imp_state = IMPS_DOWN; 113 sc->imp_if.if_flags &= ~IFF_UP; 114 splx(s); 115 return; 116 } 117 sc->imp_state = IMPS_INIT; 118 impnoops(sc); 119 if_rtinit(&sc->imp_if, RTF_DIRECT|RTF_UP); 120 splx(s); 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 if (ip->il_mtype != IMPTYPE_DATA) { 166 #ifdef notdef 167 addr.s_net = ip->il_network; 168 #else 169 addr.s_net = sc->imp_if.if_net; 170 #endif 171 addr.s_imp = ip->il_imp; 172 addr.s_host = ip->il_host; 173 } 174 switch (ip->il_mtype) { 175 176 case IMPTYPE_DATA: 177 break; 178 179 /* 180 * IMP leader error. Reset the IMP and discard the packet. 181 */ 182 case IMPTYPE_BADLEADER: 183 /* 184 * According to 1822 document, this message 185 * will be generated in response to the 186 * first noop sent to the IMP after 187 * the host resets the IMP interface. 188 */ 189 if (sc->imp_state != IMPS_INIT) { 190 impmsg(sc, "leader error"); 191 hostreset(sc->imp_if.if_net); 192 impnoops(sc); 193 } 194 goto drop; 195 196 /* 197 * IMP going down. Print message, and if not immediate, 198 * set off a timer to insure things will be reset at the 199 * appropriate time. 200 */ 201 case IMPTYPE_DOWN: 202 if (sc->imp_state < IMPS_INIT) 203 goto drop; 204 if ((ip->il_link & IMP_DMASK) == 0) { 205 sc->imp_state = IMPS_GOINGDOWN; 206 timeout(impdown, (caddr_t)sc, 30 * hz); 207 } 208 impmsg(sc, "going down %s", 209 (u_int)impmessage[ip->il_link&IMP_DMASK]); 210 goto drop; 211 212 /* 213 * A NOP usually seen during the initialization sequence. 214 * Compare the local address with that in the message. 215 * Reset the local address notion if it doesn't match. 216 */ 217 case IMPTYPE_NOOP: 218 if (sc->imp_state == IMPS_DOWN) { 219 sc->imp_state = IMPS_INIT; 220 sc->imp_dropcnt = IMP_DROPCNT; 221 } 222 if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0) 223 goto drop; 224 sin = (struct sockaddr_in *)&sc->imp_if.if_addr; 225 if (sin->sin_addr.s_host != ip->il_host || 226 sin->sin_addr.s_imp != ip->il_imp) { 227 sc->imp_if.if_host[0] = 228 sin->sin_addr.s_host = ip->il_host; 229 sin->sin_addr.s_imp = ip->il_imp; 230 impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host, 231 ntohs(ip->il_imp)); 232 } 233 sc->imp_state = IMPS_UP; 234 sc->imp_if.if_flags |= IFF_UP; 235 /* restart output in case something was q'd */ 236 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 237 goto drop; 238 239 /* 240 * RFNM or INCOMPLETE message, send next 241 * message on the q. We could pass incomplete's 242 * up to the next level, but this currently isn't 243 * needed. 244 */ 245 case IMPTYPE_RFNM: 246 case IMPTYPE_INCOMPLETE: 247 if (hp = hostlookup(addr)) { 248 if (hp->h_rfnm == 0) 249 hp->h_flags &= ~HF_INUSE; 250 else if (next = hostdeque(hp)) 251 (void) impsnd(&sc->imp_if, next); 252 } 253 goto drop; 254 255 /* 256 * Host or IMP can't be reached. Flush any packets 257 * awaiting transmission and release the host structure. 258 */ 259 case IMPTYPE_HOSTDEAD: 260 case IMPTYPE_HOSTUNREACH: { 261 int s = splnet(); 262 impnotify(ip->il_mtype, ip, hostlookup(addr)); 263 splx(s); 264 goto rawlinkin; 265 } 266 267 /* 268 * Error in data. Clear RFNM status for this host and send 269 * noops to the IMP to clear the interface. 270 */ 271 case IMPTYPE_BADDATA: { 272 int s; 273 274 impmsg(sc, "data error"); 275 s = splnet(); 276 if (hp = hostlookup(addr)) 277 hp->h_rfnm = 0; 278 splx(s); 279 impnoops(sc); 280 goto drop; 281 } 282 283 /* 284 * Interface reset. 285 */ 286 case IMPTYPE_RESET: 287 impmsg(sc, "interface reset"); 288 impnoops(sc); 289 goto drop; 290 291 default: 292 sc->imp_if.if_collisions++; /* XXX */ 293 goto drop; 294 } 295 296 /* 297 * Data for a protocol. Dispatch to the appropriate 298 * protocol routine (running at software interrupt). 299 * If this isn't a raw interface, advance pointer 300 * into mbuf past leader. 301 */ 302 switch (ip->il_link) { 303 304 #ifdef INET 305 case IMPLINK_IP: 306 m->m_len -= sizeof(struct imp_leader); 307 m->m_off += sizeof(struct imp_leader); 308 schednetisr(NETISR_IP); 309 inq = &ipintrq; 310 break; 311 #endif 312 313 default: 314 rawlinkin: 315 impproto.sp_protocol = ip->il_link; 316 sin = (struct sockaddr_in *)&sc->imp_if.if_addr; 317 impdst.sin_addr = sin->sin_addr;; 318 impsrc.sin_addr.s_net = ip->il_network; 319 impsrc.sin_addr.s_host = ip->il_host; 320 impsrc.sin_addr.s_imp = ip->il_imp; 321 raw_input(m, &impproto, (struct sockaddr *)&impsrc, 322 (struct sockaddr *)&impdst); 323 return; 324 } 325 if (IF_QFULL(inq)) { 326 IF_DROP(inq); 327 goto drop; 328 } 329 IF_ENQUEUE(inq, m); 330 return; 331 332 drop: 333 m_freem(m); 334 } 335 336 /* 337 * Bring the IMP down after notification. 338 */ 339 impdown(sc) 340 struct imp_softc *sc; 341 { 342 343 COUNT(IMPDOWN); 344 sc->imp_state = IMPS_DOWN; 345 impmsg(sc, "marked down"); 346 hostreset(sc->imp_if.if_net); 347 if_down(&sc->imp_if); 348 } 349 350 /*VARARGS*/ 351 impmsg(sc, fmt, a1, a2) 352 struct imp_softc *sc; 353 char *fmt; 354 u_int a1; 355 { 356 357 COUNT(IMPMSG); 358 printf("imp%d: ", sc->imp_if.if_unit); 359 printf(fmt, a1, a2); 360 printf("\n"); 361 } 362 363 /* 364 * Process an IMP "error" message, passing this 365 * up to the higher level protocol. 366 */ 367 impnotify(what, cp, hp) 368 int what; 369 struct control_leader *cp; 370 struct host *hp; 371 { 372 struct in_addr in; 373 374 COUNT(IMPNOTIFY); 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 COUNT(IMPOUTPUT); 409 /* 410 * Don't even try if the IMP is unavailable. 411 */ 412 if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) { 413 error = ENETDOWN; 414 goto drop; 415 } 416 417 switch (dst->sa_family) { 418 419 #ifdef INET 420 case AF_INET: { 421 struct ip *ip = mtod(m0, struct ip *); 422 struct sockaddr_in *sin = (struct sockaddr_in *)dst; 423 424 dhost = sin->sin_addr.s_host; 425 dimp = sin->sin_addr.s_impno; 426 dlink = IMPLINK_IP; 427 dnet = 0; 428 len = ntohs((u_short)ip->ip_len); 429 break; 430 } 431 #endif 432 case AF_IMPLINK: 433 goto leaderexists; 434 435 default: 436 printf("imp%d: can't handle af%d\n", ifp->if_unit, 437 dst->sa_family); 438 error = EAFNOSUPPORT; 439 goto drop; 440 } 441 442 /* 443 * Add IMP leader. If there's not enough space in the 444 * first mbuf, allocate another. If that should fail, we 445 * drop this sucker. 446 */ 447 if (m->m_off > MMAXOFF || 448 MMINOFF + sizeof(struct imp_leader) > m->m_off) { 449 m = m_get(M_DONTWAIT); 450 if (m == 0) { 451 error = ENOBUFS; 452 goto drop; 453 } 454 m->m_next = m0; 455 m->m_off = MMINOFF; 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->il_network = dnet; 465 imp->il_host = dhost; 466 imp->il_imp = htons((u_short)dimp); 467 imp->il_length = 468 htons((u_short)(len + sizeof(struct imp_leader)) << 3); 469 imp->il_link = dlink; 470 imp->il_flags = imp->il_htype = imp->il_subtype = 0; 471 472 leaderexists: 473 return (impsnd(ifp, m)); 474 drop: 475 m_freem(m0); 476 return (error); 477 } 478 479 /* 480 * Put a message on an interface's output queue. 481 * Perform RFNM counting: no more than 8 message may be 482 * in flight to any one host. 483 */ 484 impsnd(ifp, m) 485 struct ifnet *ifp; 486 struct mbuf *m; 487 { 488 register struct imp_leader *ip; 489 register struct host *hp; 490 struct impcb *icp; 491 int s, error; 492 493 COUNT(IMPSND); 494 ip = mtod(m, struct imp_leader *); 495 496 /* 497 * Do RFNM counting for data messages 498 * (no more than 8 outstanding to any host) 499 */ 500 s = splimp(); 501 if (ip->il_mtype == IMPTYPE_DATA) { 502 struct in_addr addr; 503 504 #ifdef notdef 505 addr.s_net = ip->il_network; 506 #else 507 addr.s_net = ifp->if_net; /* XXX */ 508 #endif 509 addr.s_host = ip->il_host; 510 addr.s_imp = ip->il_imp; 511 if ((hp = hostlookup(addr)) == 0) 512 hp = hostenter(addr); 513 if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) { 514 error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH; 515 hp->h_timer = HOSTTIMER; 516 hp->h_flags &= ~HF_INUSE; 517 goto bad; 518 } 519 520 /* 521 * If IMP would block, queue until RFNM 522 */ 523 if (hp) { 524 if (hp->h_rfnm < 8) { 525 hp->h_rfnm++; 526 goto enque; 527 } 528 if (hp->h_qcnt < 8) { /* high water mark */ 529 HOST_ENQUE(hp, m); 530 goto start; 531 } 532 } 533 error = ENOBUFS; 534 goto bad; 535 } 536 enque: 537 if (IF_QFULL(&ifp->if_snd)) { 538 IF_DROP(&ifp->if_snd); 539 error = ENOBUFS; 540 bad: 541 m_freem(m); 542 splx(s); 543 return (error); 544 } 545 IF_ENQUEUE(&ifp->if_snd, m); 546 start: 547 icp = &imp_softc[ifp->if_unit].imp_cb; 548 if (icp->ic_oactive == 0) 549 (*icp->ic_start)(ifp->if_unit); 550 splx(s); 551 return (0); 552 } 553 554 /* 555 * Put three 1822 NOOPs at the head of the output queue. 556 * Part of host-IMP initialization procedure. 557 * (Should return success/failure, but noone knows 558 * what to do with this, so why bother?) 559 * This routine is always called at splimp, so we don't 560 * protect the call to IF_PREPEND. 561 */ 562 impnoops(sc) 563 register struct imp_softc *sc; 564 { 565 register i; 566 register struct mbuf *m; 567 register struct control_leader *cp; 568 int x; 569 570 COUNT(IMPNOOPS); 571 sc->imp_dropcnt = IMP_DROPCNT; 572 for (i = 0; i < IMP_DROPCNT + 1; i++ ) { 573 if ((m = m_getclr(M_DONTWAIT)) == 0) 574 return; 575 m->m_off = MMINOFF; 576 m->m_len = sizeof(struct control_leader); 577 cp = mtod(m, struct control_leader *); 578 cp->dl_format = IMP_NFF; 579 cp->dl_link = i; 580 cp->dl_mtype = IMPTYPE_NOOP; 581 IF_PREPEND(&sc->imp_if.if_snd, m); 582 } 583 if (sc->imp_cb.ic_oactive == 0) 584 (*sc->imp_cb.ic_start)(sc->imp_if.if_unit); 585 } 586 #endif 587