1 /* 2 * Copyright (c) University of British Columbia, 1984 3 * Copyright (C) Computer Science Department IV, 4 * University of Erlangen-Nuremberg, Germany, 1992 5 * Copyright (c) 1991, 1992 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by the 9 * Laboratory for Computation Vision and the Computer Science Department 10 * of the the University of British Columbia and the Computer Science 11 * Department (IV) of the University of Erlangen-Nuremberg, Germany. 12 * 13 * %sccs.include.redist.c% 14 * 15 * @(#)pk_input.c 7.17 (Berkeley) 12/08/92 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/mbuf.h> 21 #include <sys/socket.h> 22 #include <sys/protosw.h> 23 #include <sys/socketvar.h> 24 #include <sys/errno.h> 25 26 #include <net/if.h> 27 28 #include <netccitt/dll.h> 29 #include <netccitt/x25.h> 30 #include <netccitt/pk.h> 31 #include <netccitt/pk_var.h> 32 #include <netccitt/llc_var.h> 33 34 struct pkcb_q pkcb_q = {&pkcb_q, &pkcb_q}; 35 36 /* 37 * ccittintr() is the generic interrupt handler for HDLC, LLC2, and X.25. This 38 * allows to have kernel running X.25 but no HDLC or LLC2 or both (in case we 39 * employ boards that do all the stuff themselves, e.g. ADAX X.25 or TPS ISDN.) 40 */ 41 void 42 ccittintr() 43 { 44 extern struct ifqueue pkintrq; 45 extern struct ifqueue hdintrq; 46 extern struct ifqueue llcintrq; 47 48 #ifdef HDLC 49 if (hdintrq.ifq_len) 50 hdintr (); 51 #endif 52 #ifdef LLC 53 if (llcintrq.ifq_len) 54 llcintr (); 55 #endif 56 if (pkintrq.ifq_len) 57 pkintr (); 58 } 59 60 struct pkcb * 61 pk_newlink (ia, llnext) 62 struct x25_ifaddr *ia; 63 caddr_t llnext; 64 { 65 register struct x25config *xcp = &ia->ia_xc; 66 register struct pkcb *pkp; 67 register struct pklcd *lcp; 68 register struct protosw *pp; 69 unsigned size; 70 71 pp = pffindproto (AF_CCITT, (int)xcp -> xc_lproto, 0); 72 if (pp == 0 || pp -> pr_output == 0) { 73 pk_message (0, xcp, "link level protosw error"); 74 return ((struct pkcb *)0); 75 } 76 /* 77 * Allocate a network control block structure 78 */ 79 size = sizeof (struct pkcb); 80 pkp = (struct pkcb *)malloc(size, M_PCB, M_WAITOK); 81 if (pkp == 0) 82 return ((struct pkcb *)0); 83 bzero ((caddr_t)pkp, size); 84 pkp -> pk_lloutput = pp -> pr_output; 85 pkp -> pk_llctlinput = (caddr_t (*)())pp -> pr_ctlinput; 86 pkp -> pk_xcp = xcp; 87 pkp -> pk_ia = ia; 88 pkp -> pk_state = DTE_WAITING; 89 pkp -> pk_llnext = llnext; 90 insque(pkp, &pkcb_q); 91 92 /* 93 * set defaults 94 */ 95 96 if (xcp -> xc_pwsize == 0) 97 xcp -> xc_pwsize = DEFAULT_WINDOW_SIZE; 98 if (xcp -> xc_psize == 0) 99 xcp -> xc_psize = X25_PS128; 100 /* 101 * Allocate logical channel descriptor vector 102 */ 103 104 (void)pk_resize(pkp); 105 return (pkp); 106 } 107 108 109 pk_dellink (pkp) 110 register struct pkcb *pkp; 111 { 112 register int i; 113 register struct protosw *pp; 114 115 /* 116 * Essentially we have the choice to 117 * (a) go ahead and let the route be deleted and 118 * leave the pkcb associated with that route 119 * as it is, i.e. the connections stay open 120 * (b) do a pk_disconnect() on all channels associated 121 * with the route via the pkcb and then proceed. 122 * 123 * For the time being we stick with (b) 124 */ 125 126 for(i = 1; i < pkp->pk_maxlcn; ++i) 127 if (pkp->pk_chan[i]) 128 pk_disconnect(pkp->pk_chan[i]); 129 130 /* 131 * Free the pkcb 132 */ 133 134 /* 135 * First find the protoswitch to get hold of the link level 136 * protocol to be notified that the packet level entity is 137 * dissolving ... 138 */ 139 pp = pffindproto (AF_CCITT, (int)pkp ->pk_xcp -> xc_lproto, 0); 140 if (pp == 0 || pp -> pr_output == 0) { 141 pk_message (0, pkp -> pk_xcp, "link level protosw error"); 142 return(EPROTONOSUPPORT); 143 } 144 145 pkp -> pk_refcount--; 146 if (!pkp -> pk_refcount) { 147 struct dll_ctlinfo ctlinfo; 148 149 remque(pkp); 150 if (pkp -> pk_rt -> rt_llinfo == (caddr_t) pkp) 151 pkp -> pk_rt -> rt_llinfo = (caddr_t) NULL; 152 153 /* 154 * Tell the link level that the pkcb is dissolving 155 */ 156 if (pp -> pr_ctlinput && pkp -> pk_llnext) { 157 ctlinfo.dlcti_pcb = pkp -> pk_llnext; 158 ctlinfo.dlcti_rt = pkp -> pk_rt; 159 (pp -> pr_ctlinput)(PRC_DISCONNECT_REQUEST, 160 pkp -> pk_xcp, &ctlinfo); 161 } 162 free((caddr_t) pkp -> pk_chan, M_IFADDR); 163 free((caddr_t) pkp, M_PCB); 164 } 165 166 return (0); 167 } 168 169 170 pk_resize (pkp) 171 register struct pkcb *pkp; 172 { 173 struct pklcd *dev_lcp = 0; 174 struct x25config *xcp = pkp -> pk_xcp; 175 if (pkp -> pk_chan && 176 (pkp -> pk_maxlcn != xcp -> xc_maxlcn)) { 177 pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION); 178 dev_lcp = pkp -> pk_chan[0]; 179 free ((caddr_t)pkp -> pk_chan, M_IFADDR); 180 pkp -> pk_chan = 0; 181 } 182 if (pkp -> pk_chan == 0) { 183 unsigned size; 184 pkp -> pk_maxlcn = xcp -> xc_maxlcn; 185 size = (pkp -> pk_maxlcn + 1) * sizeof (struct pklcd *); 186 pkp -> pk_chan = 187 (struct pklcd **) malloc (size, M_IFADDR, M_WAITOK); 188 if (pkp -> pk_chan) { 189 bzero ((caddr_t)pkp -> pk_chan, size); 190 /* 191 * Allocate a logical channel descriptor for lcn 0 192 */ 193 if (dev_lcp == 0 && 194 (dev_lcp = pk_attach ((struct socket *)0)) == 0) 195 return (ENOBUFS); 196 dev_lcp -> lcd_state = READY; 197 dev_lcp -> lcd_pkp = pkp; 198 pkp -> pk_chan[0] = dev_lcp; 199 } else { 200 if (dev_lcp) 201 pk_close (dev_lcp); 202 return (ENOBUFS); 203 } 204 } 205 return 0; 206 } 207 208 /* 209 * This procedure is called by the link level whenever the link 210 * becomes operational, is reset, or when the link goes down. 211 */ 212 /*VARARGS*/ 213 caddr_t 214 pk_ctlinput (code, src, addr) 215 struct sockaddr *src; 216 caddr_t addr; 217 { 218 register struct pkcb *pkp = (struct pkcb *)addr; 219 220 switch (code) { 221 case PRC_LINKUP: 222 if (pkp -> pk_state == DTE_WAITING) 223 pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION); 224 break; 225 226 case PRC_LINKDOWN: 227 pk_restart (pkp, -1); /* Clear all active circuits */ 228 pkp -> pk_state = DTE_WAITING; 229 break; 230 231 case PRC_LINKRESET: 232 pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION); 233 break; 234 235 case PRC_CONNECT_INDICATION: { 236 struct rtentry *llrt; 237 238 if ((llrt = rtalloc1(src, 0)) == 0) 239 return 0; 240 else llrt->rt_refcnt--; 241 242 pkp = (((struct npaidbentry *)llrt->rt_llinfo)->np_rt) ? 243 (struct pkcb *)(((struct npaidbentry *)llrt->rt_llinfo)->np_rt->rt_llinfo) : (struct pkcb *) 0; 244 if (pkp == (struct pkcb *) 0) 245 return 0; 246 pkp->pk_llnext = addr; 247 248 return ((caddr_t) pkp); 249 } 250 case PRC_DISCONNECT_INDICATION: 251 pk_restart (pkp, -1) ; /* Clear all active circuits */ 252 pkp->pk_state = DTE_WAITING; 253 pkp->pk_llnext = (caddr_t) 0; 254 } 255 return (0); 256 } 257 struct ifqueue pkintrq; 258 /* 259 * This routine is called if there are semi-smart devices that do HDLC 260 * in hardware and want to queue the packet and call level 3 directly 261 */ 262 pkintr () 263 { 264 register struct mbuf *m; 265 register struct ifaddr *ifa; 266 register struct ifnet *ifp; 267 register int s; 268 269 for (;;) { 270 s = splimp (); 271 IF_DEQUEUE (&pkintrq, m); 272 splx (s); 273 if (m == 0) 274 break; 275 if (m->m_len < PKHEADERLN) { 276 printf ("pkintr: packet too short (len=%d)\n", 277 m->m_len); 278 m_freem (m); 279 continue; 280 } 281 pk_input(m); 282 } 283 } 284 struct mbuf *pk_bad_packet; 285 struct mbuf_cache pk_input_cache = {0 }; 286 /* 287 * X.25 PACKET INPUT 288 * 289 * This procedure is called by a link level procedure whenever 290 * an information frame is received. It decodes the packet and 291 * demultiplexes based on the logical channel number. 292 * 293 * We change the original conventions of the UBC code here -- 294 * since there may be multiple pkcb's for 802.2 class 2 295 * for a given interface, we must be informed which one it is; 296 * so we overwrite the pkthdr.rcvif; it can be recovered if necessary. 297 * 298 */ 299 300 #define RESTART_DTE_ORIGINATED(xp) (((xp) -> packet_cause == X25_RESTART_DTE_ORIGINATED) || \ 301 ((xp) -> packet_cause >= X25_RESTART_DTE_ORIGINATED2)) 302 303 pk_input (m) 304 register struct mbuf *m; 305 { 306 register struct x25_packet *xp; 307 register struct pklcd *lcp; 308 register struct socket *so = 0; 309 register struct pkcb *pkp; 310 int ptype, lcn, lcdstate = LISTEN; 311 312 if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize) 313 mbuf_cache(&pk_input_cache, m); 314 if ((m->m_flags & M_PKTHDR) == 0) 315 panic("pkintr"); 316 317 if ((pkp = (struct pkcb *)m->m_pkthdr.rcvif) == 0) 318 return; 319 xp = mtod (m, struct x25_packet *); 320 ptype = pk_decode (xp); 321 lcn = LCN(xp); 322 lcp = pkp -> pk_chan[lcn]; 323 324 /* 325 * If the DTE is in Restart state, then it will ignore data, 326 * interrupt, call setup and clearing, flow control and reset 327 * packets. 328 */ 329 if (lcn < 0 || lcn > pkp -> pk_maxlcn) { 330 pk_message (lcn, pkp -> pk_xcp, "illegal lcn"); 331 m_freem (m); 332 return; 333 } 334 335 pk_trace (pkp -> pk_xcp, m, "P-In"); 336 337 if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) { 338 m_freem (m); 339 return; 340 } 341 if (lcp) { 342 so = lcp -> lcd_so; 343 lcdstate = lcp -> lcd_state; 344 } else { 345 if (ptype == CLEAR) { /* idle line probe (Datapac specific) */ 346 /* send response on lcd 0's output queue */ 347 lcp = pkp -> pk_chan[0]; 348 lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM); 349 pk_output (lcp); 350 m_freem (m); 351 return; 352 } 353 if (ptype != CALL) 354 ptype = INVALID_PACKET; 355 } 356 357 if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) { 358 pk_message (0, pkp -> pk_xcp, "illegal ptype (%d, %s) on lcn 0", 359 ptype, pk_name[ptype / MAXSTATES]); 360 if (pk_bad_packet) 361 m_freem (pk_bad_packet); 362 pk_bad_packet = m; 363 return; 364 } 365 366 switch (ptype + lcdstate) { 367 /* 368 * Incoming Call packet received. 369 */ 370 case CALL + LISTEN: 371 pk_incoming_call (pkp, m); 372 break; 373 374 /* 375 * Call collision: Just throw this "incoming call" away since 376 * the DCE will ignore it anyway. 377 */ 378 case CALL + SENT_CALL: 379 pk_message ((int)lcn, pkp -> pk_xcp, 380 "incoming call collision"); 381 break; 382 383 /* 384 * Call confirmation packet received. This usually means our 385 * previous connect request is now complete. 386 */ 387 case CALL_ACCEPTED + SENT_CALL: 388 MCHTYPE(m, MT_CONTROL); 389 pk_call_accepted (lcp, m); 390 break; 391 392 /* 393 * This condition can only happen if the previous state was 394 * SENT_CALL. Just ignore the packet, eventually a clear 395 * confirmation should arrive. 396 */ 397 case CALL_ACCEPTED + SENT_CLEAR: 398 break; 399 400 /* 401 * Clear packet received. This requires a complete tear down 402 * of the virtual circuit. Free buffers and control blocks. 403 * and send a clear confirmation. 404 */ 405 case CLEAR + READY: 406 case CLEAR + RECEIVED_CALL: 407 case CLEAR + SENT_CALL: 408 case CLEAR + DATA_TRANSFER: 409 lcp -> lcd_state = RECEIVED_CLEAR; 410 lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM); 411 pk_output (lcp); 412 pk_clearcause (pkp, xp); 413 if (lcp -> lcd_upper) { 414 MCHTYPE(m, MT_CONTROL); 415 lcp -> lcd_upper (lcp, m); 416 } 417 pk_close (lcp); 418 lcp = 0; 419 break; 420 421 /* 422 * Clear collision: Treat this clear packet as a confirmation. 423 */ 424 case CLEAR + SENT_CLEAR: 425 pk_close (lcp); 426 break; 427 428 /* 429 * Clear confirmation received. This usually means the virtual 430 * circuit is now completely removed. 431 */ 432 case CLEAR_CONF + SENT_CLEAR: 433 pk_close (lcp); 434 break; 435 436 /* 437 * A clear confirmation on an unassigned logical channel - just 438 * ignore it. Note: All other packets on an unassigned channel 439 * results in a clear. 440 */ 441 case CLEAR_CONF + READY: 442 case CLEAR_CONF + LISTEN: 443 break; 444 445 /* 446 * Data packet received. Pass on to next level. Move the Q and M 447 * bits into the data portion for the next level. 448 */ 449 case DATA + DATA_TRANSFER: 450 if (lcp -> lcd_reset_condition) { 451 ptype = DELETE_PACKET; 452 break; 453 } 454 455 /* 456 * Process the P(S) flow control information in this Data packet. 457 * Check that the packets arrive in the correct sequence and that 458 * they are within the "lcd_input_window". Input window rotation is 459 * initiated by the receive interface. 460 */ 461 462 if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) || 463 PS(xp) == ((lcp -> lcd_input_window + lcp->lcd_windowsize) % MODULUS)) { 464 m_freem (m); 465 pk_procerror (RESET, lcp, "p(s) flow control error", 1); 466 break; 467 } 468 lcp -> lcd_rsn = PS(xp); 469 470 if (pk_ack (lcp, PR(xp)) != PACKET_OK) { 471 m_freem (m); 472 break; 473 } 474 m -> m_data += PKHEADERLN; 475 m -> m_len -= PKHEADERLN; 476 m -> m_pkthdr.len -= PKHEADERLN; 477 478 lcp -> lcd_rxcnt++; 479 if (lcp -> lcd_flags & X25_MBS_HOLD) { 480 register struct mbuf *n = lcp -> lcd_cps; 481 int mbit = MBIT(xp); 482 octet q_and_d_bits; 483 484 if (n) { 485 n -> m_pkthdr.len += m -> m_pkthdr.len; 486 while (n -> m_next) 487 n = n -> m_next; 488 n -> m_next = m; 489 m = lcp -> lcd_cps; 490 491 if (lcp -> lcd_cpsmax && 492 n -> m_pkthdr.len > lcp -> lcd_cpsmax) { 493 pk_procerror (RESET, lcp, 494 "C.P.S. overflow", 128); 495 return; 496 } 497 q_and_d_bits = 0xc0 & *(octet *)xp; 498 xp = (struct x25_packet *) 499 (mtod(m, octet *) - PKHEADERLN); 500 *(octet *)xp |= q_and_d_bits; 501 } 502 if (mbit) { 503 lcp -> lcd_cps = m; 504 pk_flowcontrol(lcp, 0, 1); 505 return; 506 } 507 lcp -> lcd_cps = 0; 508 } 509 if (so == 0) 510 break; 511 if (lcp -> lcd_flags & X25_MQBIT) { 512 octet t = (X25GBITS(xp -> bits, q_bit)) ? t = 0x80 : 0; 513 514 if (MBIT(xp)) 515 t |= 0x40; 516 m -> m_data -= 1; 517 m -> m_len += 1; 518 m -> m_pkthdr.len += 1; 519 *mtod(m, octet *) = t; 520 } 521 522 /* 523 * Discard Q-BIT packets if the application 524 * doesn't want to be informed of M and Q bit status 525 */ 526 if (X25GBITS(xp -> bits, q_bit) 527 && (lcp -> lcd_flags & X25_MQBIT) == 0) { 528 m_freem (m); 529 /* 530 * NB. This is dangerous: sending a RR here can 531 * cause sequence number errors if a previous data 532 * packet has not yet been passed up to the application 533 * (RR's are normally generated via PRU_RCVD). 534 */ 535 pk_flowcontrol(lcp, 0, 1); 536 } else { 537 sbappendrecord (&so -> so_rcv, m); 538 sorwakeup (so); 539 } 540 break; 541 542 /* 543 * Interrupt packet received. 544 */ 545 case INTERRUPT + DATA_TRANSFER: 546 if (lcp -> lcd_reset_condition) 547 break; 548 lcp -> lcd_intrdata = xp -> packet_data; 549 lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM); 550 pk_output (lcp); 551 m -> m_data += PKHEADERLN; 552 m -> m_len -= PKHEADERLN; 553 m -> m_pkthdr.len -= PKHEADERLN; 554 MCHTYPE(m, MT_OOBDATA); 555 if (so) { 556 if (so -> so_options & SO_OOBINLINE) 557 sbinsertoob (&so -> so_rcv, m); 558 else 559 m_freem (m); 560 sohasoutofband (so); 561 } 562 break; 563 564 /* 565 * Interrupt confirmation packet received. 566 */ 567 case INTERRUPT_CONF + DATA_TRANSFER: 568 if (lcp -> lcd_reset_condition) 569 break; 570 if (lcp -> lcd_intrconf_pending == TRUE) 571 lcp -> lcd_intrconf_pending = FALSE; 572 else 573 pk_procerror (RESET, lcp, "unexpected packet", 43); 574 break; 575 576 /* 577 * Receiver ready received. Rotate the output window and output 578 * any data packets waiting transmission. 579 */ 580 case RR + DATA_TRANSFER: 581 if (lcp -> lcd_reset_condition || 582 pk_ack (lcp, PR(xp)) != PACKET_OK) { 583 ptype = DELETE_PACKET; 584 break; 585 } 586 if (lcp -> lcd_rnr_condition == TRUE) 587 lcp -> lcd_rnr_condition = FALSE; 588 pk_output (lcp); 589 break; 590 591 /* 592 * Receiver Not Ready received. Packets up to the P(R) can be 593 * be sent. Condition is cleared with a RR. 594 */ 595 case RNR + DATA_TRANSFER: 596 if (lcp -> lcd_reset_condition || 597 pk_ack (lcp, PR(xp)) != PACKET_OK) { 598 ptype = DELETE_PACKET; 599 break; 600 } 601 lcp -> lcd_rnr_condition = TRUE; 602 break; 603 604 /* 605 * Reset packet received. Set state to FLOW_OPEN. The Input and 606 * Output window edges ar set to zero. Both the send and receive 607 * numbers are reset. A confirmation is returned. 608 */ 609 case RESET + DATA_TRANSFER: 610 if (lcp -> lcd_reset_condition) 611 /* Reset collision. Just ignore packet. */ 612 break; 613 614 pk_resetcause (pkp, xp); 615 lcp -> lcd_window_condition = lcp -> lcd_rnr_condition = 616 lcp -> lcd_intrconf_pending = FALSE; 617 lcp -> lcd_output_window = lcp -> lcd_input_window = 618 lcp -> lcd_last_transmitted_pr = 0; 619 lcp -> lcd_ssn = 0; 620 lcp -> lcd_rsn = MODULUS - 1; 621 622 lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM); 623 pk_output (lcp); 624 625 pk_flush(lcp); 626 if (so == 0) 627 break; 628 wakeup ((caddr_t) & so -> so_timeo); 629 sorwakeup (so); 630 sowwakeup (so); 631 break; 632 633 /* 634 * Reset confirmation received. 635 */ 636 case RESET_CONF + DATA_TRANSFER: 637 if (lcp -> lcd_reset_condition) { 638 lcp -> lcd_reset_condition = FALSE; 639 pk_output (lcp); 640 } 641 else 642 pk_procerror (RESET, lcp, "unexpected packet", 32); 643 break; 644 645 case DATA + SENT_CLEAR: 646 ptype = DELETE_PACKET; 647 case RR + SENT_CLEAR: 648 case RNR + SENT_CLEAR: 649 case INTERRUPT + SENT_CLEAR: 650 case INTERRUPT_CONF + SENT_CLEAR: 651 case RESET + SENT_CLEAR: 652 case RESET_CONF + SENT_CLEAR: 653 /* Just ignore p if we have sent a CLEAR already. 654 */ 655 break; 656 657 /* 658 * Restart sets all the permanent virtual circuits to the "Data 659 * Transfer" stae and all the switched virtual circuits to the 660 * "Ready" state. 661 */ 662 case RESTART + READY: 663 switch (pkp -> pk_state) { 664 case DTE_SENT_RESTART: 665 /* 666 * Restart collision. 667 * If case the restart cause is "DTE originated" we 668 * have a DTE-DTE situation and are trying to resolve 669 * who is going to play DTE/DCE [ISO 8208:4.2-4.5] 670 */ 671 if (RESTART_DTE_ORIGINATED(xp)) { 672 pk_restart (pkp, X25_RESTART_DTE_ORIGINATED); 673 pk_message (0, pkp -> pk_xcp, 674 "RESTART collision"); 675 if ((pkp -> pk_restartcolls++) > MAXRESTARTCOLLISIONS) { 676 pk_message (0, pkp -> pk_xcp, 677 "excessive RESTART collisions"); 678 pkp -> pk_restartcolls = 0; 679 } 680 break; 681 } 682 pkp -> pk_state = DTE_READY; 683 pkp -> pk_dxerole |= DTE_PLAYDTE; 684 pkp -> pk_dxerole &= ~DTE_PLAYDCE; 685 pk_message (0, pkp -> pk_xcp, 686 "Packet level operational"); 687 pk_message (0, pkp -> pk_xcp, 688 "Assuming DTE role"); 689 if (pkp -> pk_dxerole & DTE_CONNECTPENDING) 690 pk_callcomplete(pkp); 691 break; 692 693 default: 694 pk_restart (pkp, -1); 695 pk_restartcause (pkp, xp); 696 pkp -> pk_chan[0] -> lcd_template = pk_template (0, 697 X25_RESTART_CONFIRM); 698 pk_output (pkp -> pk_chan[0]); 699 pkp -> pk_state = DTE_READY; 700 pkp -> pk_dxerole |= RESTART_DTE_ORIGINATED(xp) ? DTE_PLAYDCE : 701 DTE_PLAYDTE; 702 if (pkp -> pk_dxerole & DTE_PLAYDTE) { 703 pkp -> pk_dxerole &= ~DTE_PLAYDCE; 704 pk_message (0, pkp -> pk_xcp, 705 "Assuming DTE role"); 706 } else { 707 pkp -> pk_dxerole &= ~DTE_PLAYDTE; 708 pk_message (0, pkp -> pk_xcp, 709 "Assuming DCE role"); 710 } 711 if (pkp -> pk_dxerole & DTE_CONNECTPENDING) 712 pk_callcomplete(pkp); 713 } 714 break; 715 716 /* 717 * Restart confirmation received. All logical channels are set 718 * to READY. 719 */ 720 case RESTART_CONF + READY: 721 switch (pkp -> pk_state) { 722 case DTE_SENT_RESTART: 723 pkp -> pk_state = DTE_READY; 724 pkp -> pk_dxerole |= DTE_PLAYDTE; 725 pkp -> pk_dxerole &= ~DTE_PLAYDCE; 726 pk_message (0, pkp -> pk_xcp, 727 "Packet level operational"); 728 pk_message (0, pkp-> pk_xcp, 729 "Assuming DTE role"); 730 if (pkp -> pk_dxerole & DTE_CONNECTPENDING) 731 pk_callcomplete(pkp); 732 break; 733 734 default: 735 /* Restart local procedure error. */ 736 pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR); 737 pkp -> pk_state = DTE_SENT_RESTART; 738 pkp -> pk_dxerole &= ~(DTE_PLAYDTE | DTE_PLAYDCE); 739 } 740 break; 741 742 default: 743 if (lcp) { 744 pk_procerror (CLEAR, lcp, "unknown packet error", 33); 745 pk_message (lcn, pkp -> pk_xcp, 746 "\"%s\" unexpected in \"%s\" state", 747 pk_name[ptype/MAXSTATES], pk_state[lcdstate]); 748 } else 749 pk_message (lcn, pkp -> pk_xcp, 750 "packet arrived on unassigned lcn"); 751 break; 752 } 753 if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) { 754 if (ptype != DATA && ptype != INTERRUPT) 755 MCHTYPE(m, MT_CONTROL); 756 lcp -> lcd_upper (lcp, m); 757 } else if (ptype != DATA && ptype != INTERRUPT) 758 m_freem (m); 759 } 760 761 static 762 prune_dnic(from, to, dnicname, xcp) 763 char *from, *to, *dnicname; 764 register struct x25config *xcp; 765 { 766 register char *cp1 = from, *cp2 = from; 767 if (xcp->xc_prepnd0 && *cp1 == '0') { 768 from = ++cp1; 769 goto copyrest; 770 } 771 if (xcp->xc_nodnic) { 772 for (cp1 = dnicname; *cp2 = *cp1++;) 773 cp2++; 774 cp1 = from; 775 } 776 copyrest: 777 for (cp1 = dnicname; *cp2 = *cp1++;) 778 cp2++; 779 } 780 /* static */ 781 pk_simple_bsd (from, to, lower, len) 782 register octet *from, *to; 783 register len, lower; 784 { 785 register int c; 786 while (--len >= 0) { 787 c = *from; 788 if (lower & 0x01) 789 *from++; 790 else 791 c >>= 4; 792 c &= 0x0f; c |= 0x30; *to++ = c; lower++; 793 } 794 *to = 0; 795 } 796 797 /*static octet * */ 798 pk_from_bcd (a, iscalling, sa, xcp) 799 register struct x25_calladdr *a; 800 register struct sockaddr_x25 *sa; 801 register struct x25config *xcp; 802 { 803 octet buf[MAXADDRLN+1]; 804 octet *cp; 805 unsigned count; 806 807 bzero ((caddr_t)sa, sizeof (*sa)); 808 sa -> x25_len = sizeof (*sa); 809 sa -> x25_family = AF_CCITT; 810 if (iscalling) { 811 cp = a -> address_field + (X25GBITS(a -> addrlens, called_addrlen) / 2); 812 count = X25GBITS(a -> addrlens, calling_addrlen); 813 pk_simple_bsd (cp, buf, X25GBITS(a -> addrlens, called_addrlen), count); 814 } else { 815 count = X25GBITS(a -> addrlens, called_addrlen); 816 pk_simple_bsd (a -> address_field, buf, 0, count); 817 } 818 if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp ->xc_prepnd0)) { 819 octet dnicname[sizeof(long) * NBBY/3 + 2]; 820 821 sprintf ((char *) dnicname, "%d", xcp -> xc_addr.x25_net); 822 prune_dnic ((char *)buf, sa -> x25_addr, dnicname, xcp); 823 } else 824 bcopy ((caddr_t)buf, (caddr_t)sa -> x25_addr, count + 1); 825 } 826 827 static 828 save_extra(m0, fp, so) 829 struct mbuf *m0; 830 octet *fp; 831 struct socket *so; 832 { 833 register struct mbuf *m; 834 struct cmsghdr cmsghdr; 835 if (m = m_copy (m, 0, (int)M_COPYALL)) { 836 int off = fp - mtod (m0, octet *); 837 int len = m->m_pkthdr.len - off + sizeof (cmsghdr); 838 cmsghdr.cmsg_len = len; 839 cmsghdr.cmsg_level = AF_CCITT; 840 cmsghdr.cmsg_type = PK_FACILITIES; 841 m_adj (m, off); 842 M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT); 843 if (m == 0) 844 return; 845 bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr)); 846 MCHTYPE(m, MT_CONTROL); 847 sbappendrecord(&so -> so_rcv, m); 848 } 849 } 850 851 /* 852 * This routine handles incoming call packets. It matches the protocol 853 * field on the Call User Data field (usually the first four bytes) with 854 * sockets awaiting connections. 855 */ 856 857 pk_incoming_call (pkp, m0) 858 struct mbuf *m0; 859 struct pkcb *pkp; 860 { 861 register struct pklcd *lcp = 0, *l; 862 register struct sockaddr_x25 *sa; 863 register struct x25_calladdr *a; 864 register struct socket *so = 0; 865 struct x25_packet *xp = mtod(m0, struct x25_packet *); 866 struct mbuf *m; 867 struct x25config *xcp = pkp -> pk_xcp; 868 int len = m0->m_pkthdr.len; 869 unsigned udlen; 870 char *errstr = "server unavailable"; 871 octet *u, *facp; 872 int lcn = LCN(xp); 873 874 /* First, copy the data from the incoming call packet to a X25 address 875 descriptor. It is to be regretted that you have 876 to parse the facilities into a sockaddr to determine 877 if reverse charging is being requested */ 878 if ((m = m_get (M_DONTWAIT, MT_SONAME)) == 0) 879 return; 880 sa = mtod (m, struct sockaddr_x25 *); 881 a = (struct x25_calladdr *) &xp -> packet_data; 882 facp = u = (octet *) (a -> address_field + 883 ((X25GBITS(a -> addrlens, called_addrlen) + X25GBITS(a -> addrlens, calling_addrlen) + 1) / 2)); 884 u += *u + 1; 885 udlen = min (16, ((octet *)xp) + len - u); 886 if (udlen < 0) 887 udlen = 0; 888 pk_from_bcd (a, 1, sa, pkp -> pk_xcp); /* get calling address */ 889 pk_parse_facilities (facp, sa); 890 bcopy ((caddr_t)u, sa -> x25_udata, udlen); 891 sa -> x25_udlen = udlen; 892 893 /* 894 * Now, loop through the listen sockets looking for a match on the 895 * PID. That is the first few octets of the user data field. 896 * This is the closest thing to a port number for X.25 packets. 897 * It does provide a way of multiplexing services at the user level. 898 */ 899 900 for (l = pk_listenhead; l; l = l -> lcd_listen) { 901 struct sockaddr_x25 *sxp = l -> lcd_ceaddr; 902 903 if (bcmp (sxp -> x25_udata, u, sxp->x25_udlen)) 904 continue; 905 if (sxp -> x25_net && 906 sxp -> x25_net != xcp -> xc_addr.x25_net) 907 continue; 908 /* 909 * don't accept incoming calls with the D-Bit on 910 * unless the server agrees 911 */ 912 if (X25GBITS(xp -> bits, d_bit) && !(sxp -> x25_opts.op_flags & X25_DBIT)) { 913 errstr = "incoming D-Bit mismatch"; 914 break; 915 } 916 /* 917 * don't accept incoming collect calls unless 918 * the server sets the reverse charging option. 919 */ 920 if ((sxp -> x25_opts.op_flags & (X25_OLDSOCKADDR|X25_REVERSE_CHARGE)) == 0 && 921 sa -> x25_opts.op_flags & X25_REVERSE_CHARGE) { 922 errstr = "incoming collect call refused"; 923 break; 924 } 925 if (l -> lcd_so) { 926 if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED)) 927 lcp = (struct pklcd *) so -> so_pcb; 928 } else 929 lcp = pk_attach((struct socket *) 0); 930 if (lcp == 0) { 931 /* 932 * Insufficient space or too many unaccepted 933 * connections. Just throw the call away. 934 */ 935 errstr = "server malfunction"; 936 break; 937 } 938 lcp -> lcd_upper = l -> lcd_upper; 939 lcp -> lcd_upnext = l -> lcd_upnext; 940 lcp -> lcd_lcn = lcn; 941 lcp -> lcd_state = RECEIVED_CALL; 942 sa -> x25_opts.op_flags |= (sxp -> x25_opts.op_flags & 943 ~X25_REVERSE_CHARGE) | l -> lcd_flags; 944 pk_assoc (pkp, lcp, sa); 945 lcp -> lcd_faddr = *sa; 946 lcp -> lcd_laddr.x25_udlen = sxp -> x25_udlen; 947 lcp -> lcd_craddr = &lcp->lcd_faddr; 948 lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED); 949 if (lcp -> lcd_flags & X25_DBIT) { 950 if (X25GBITS(xp -> bits, d_bit)) 951 X25SBITS(mtod(lcp -> lcd_template, 952 struct x25_packet *) -> bits, d_bit, 1); 953 else 954 lcp -> lcd_flags &= ~X25_DBIT; 955 } 956 if (so) { 957 pk_output (lcp); 958 soisconnected (so); 959 if (so -> so_options & SO_OOBINLINE) 960 save_extra(m0, facp, so); 961 } else if (lcp -> lcd_upper) { 962 (*lcp -> lcd_upper) (lcp, m0); 963 } 964 (void) m_free (m); 965 return; 966 } 967 968 /* 969 * If the call fails for whatever reason, we still need to build a 970 * skeleton LCD in order to be able to properly receive the CLEAR 971 * CONFIRMATION. 972 */ 973 #ifdef WATERLOO /* be explicit */ 974 if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0) 975 pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s", 976 sa->x25_addr, sa->x25_udata[3] & 0xff, errstr); 977 else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0) 978 pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s", 979 sa->x25_addr, errstr); 980 else 981 #endif 982 pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s", 983 sa -> x25_addr, sa -> x25_udata[0] & 0xff, 984 sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff, 985 sa -> x25_udata[3] & 0xff, errstr); 986 if ((lcp = pk_attach((struct socket *)0)) == 0) { 987 (void) m_free (m); 988 return; 989 } 990 lcp -> lcd_lcn = lcn; 991 lcp -> lcd_state = RECEIVED_CALL; 992 pk_assoc (pkp, lcp, sa); 993 (void) m_free (m); 994 pk_clear (lcp, 0, 1); 995 } 996 997 pk_call_accepted (lcp, m) 998 struct pklcd *lcp; 999 struct mbuf *m; 1000 { 1001 register struct x25_calladdr *ap; 1002 register octet *fcp; 1003 struct x25_packet *xp = mtod (m, struct x25_packet *); 1004 int len = m -> m_len; 1005 1006 lcp -> lcd_state = DATA_TRANSFER; 1007 if (lcp -> lcd_so) 1008 soisconnected (lcp -> lcd_so); 1009 if ((lcp -> lcd_flags & X25_DBIT) && (X25GBITS(xp -> bits, d_bit) == 0)) 1010 lcp -> lcd_flags &= ~X25_DBIT; 1011 if (len > 3) { 1012 ap = (struct x25_calladdr *) &xp -> packet_data; 1013 fcp = (octet *) ap -> address_field + (X25GBITS(ap -> addrlens, calling_addrlen) + 1014 X25GBITS(ap -> addrlens, called_addrlen) + 1) / 2; 1015 if (fcp + *fcp <= ((octet *)xp) + len) 1016 pk_parse_facilities (fcp, lcp -> lcd_ceaddr); 1017 } 1018 pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr); 1019 if (lcp -> lcd_so == 0 && lcp -> lcd_upper) 1020 lcp -> lcd_upper(lcp, m); 1021 } 1022 1023 pk_parse_facilities (fcp, sa) 1024 register octet *fcp; 1025 register struct sockaddr_x25 *sa; 1026 { 1027 register octet *maxfcp; 1028 1029 maxfcp = fcp + *fcp; 1030 fcp++; 1031 while (fcp < maxfcp) { 1032 /* 1033 * Ignore national DCE or DTE facilities 1034 */ 1035 if (*fcp == 0 || *fcp == 0xff) 1036 break; 1037 switch (*fcp) { 1038 case FACILITIES_WINDOWSIZE: 1039 sa -> x25_opts.op_wsize = fcp[1]; 1040 fcp += 3; 1041 break; 1042 1043 case FACILITIES_PACKETSIZE: 1044 sa -> x25_opts.op_psize = fcp[1]; 1045 fcp += 3; 1046 break; 1047 1048 case FACILITIES_THROUGHPUT: 1049 sa -> x25_opts.op_speed = fcp[1]; 1050 fcp += 2; 1051 break; 1052 1053 case FACILITIES_REVERSE_CHARGE: 1054 if (fcp[1] & 01) 1055 sa -> x25_opts.op_flags |= X25_REVERSE_CHARGE; 1056 /* 1057 * Datapac specific: for a X.25(1976) DTE, bit 2 1058 * indicates a "hi priority" (eg. international) call. 1059 */ 1060 if (fcp[1] & 02 && sa -> x25_opts.op_psize == 0) 1061 sa -> x25_opts.op_psize = X25_PS128; 1062 fcp += 2; 1063 break; 1064 1065 default: 1066 /*printf("unknown facility %x, class=%d\n", *fcp, (*fcp & 0xc0) >> 6);*/ 1067 switch ((*fcp & 0xc0) >> 6) { 1068 case 0: /* class A */ 1069 fcp += 2; 1070 break; 1071 1072 case 1: 1073 fcp += 3; 1074 break; 1075 1076 case 2: 1077 fcp += 4; 1078 break; 1079 1080 case 3: 1081 fcp++; 1082 fcp += *fcp; 1083 } 1084 } 1085 } 1086 } 1087