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