1 /* 2 * Copyright (c) University of British Columbia, 1984 3 * Copyright (c) 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Laboratory for Computation Vision and the Computer Science Department 8 * of the University of British Columbia. 9 * 10 * %sccs.include.redist.c% 11 * 12 * @(#)pk_subr.c 7.18 (Berkeley) 11/18/91 13 */ 14 15 #include "param.h" 16 #include "systm.h" 17 #include "mbuf.h" 18 #include "socket.h" 19 #include "protosw.h" 20 #include "socketvar.h" 21 #include "errno.h" 22 #include "time.h" 23 #include "kernel.h" 24 25 #include "../net/if.h" 26 27 #include "x25.h" 28 #include "pk.h" 29 #include "pk_var.h" 30 #include "x25err.h" 31 32 int pk_sendspace = 1024 * 2 + 8; 33 int pk_recvspace = 1024 * 2 + 8; 34 35 struct pklcd_q pklcd_q = {&pklcd_q, &pklcd_q}; 36 37 /* 38 * Attach X.25 protocol to socket, allocate logical channel descripter 39 * and buffer space, and enter LISTEN state if we are to accept 40 * IN-COMMING CALL packets. 41 * 42 */ 43 44 struct pklcd * 45 pk_attach (so) 46 struct socket *so; 47 { 48 register struct pklcd *lcp; 49 register int error = ENOBUFS; 50 int pk_output(); 51 52 MALLOC(lcp, struct pklcd *, sizeof (*lcp), M_PCB, M_NOWAIT); 53 if (lcp) { 54 bzero ((caddr_t)lcp, sizeof (*lcp)); 55 insque (&lcp -> lcd_q, &pklcd_q); 56 lcp -> lcd_state = READY; 57 lcp -> lcd_send = pk_output; 58 if (so) { 59 error = soreserve (so, pk_sendspace, pk_recvspace); 60 lcp -> lcd_so = so; 61 if (so -> so_options & SO_ACCEPTCONN) 62 lcp -> lcd_state = LISTEN; 63 } else 64 sbreserve (&lcp -> lcd_sb, pk_sendspace); 65 } 66 if (so) { 67 so -> so_pcb = (caddr_t) lcp; 68 so -> so_error = error; 69 } 70 return (lcp); 71 } 72 73 /* 74 * Disconnect X.25 protocol from socket. 75 */ 76 77 pk_disconnect (lcp) 78 register struct pklcd *lcp; 79 { 80 register struct socket *so = lcp -> lcd_so; 81 register struct pklcd *l, *p; 82 83 switch (lcp -> lcd_state) { 84 case LISTEN: 85 for (p = 0, l = pk_listenhead; l && l != lcp; p = l, l = l -> lcd_listen); 86 if (p == 0) { 87 if (l != 0) 88 pk_listenhead = l -> lcd_listen; 89 } 90 else 91 if (l != 0) 92 p -> lcd_listen = l -> lcd_listen; 93 pk_close (lcp); 94 break; 95 96 case READY: 97 pk_acct (lcp); 98 pk_close (lcp); 99 break; 100 101 case SENT_CLEAR: 102 case RECEIVED_CLEAR: 103 break; 104 105 default: 106 pk_acct (lcp); 107 if (so) { 108 soisdisconnecting (so); 109 sbflush (&so -> so_rcv); 110 } 111 pk_clear (lcp, 241, 0); /* Normal Disconnect */ 112 113 } 114 } 115 116 /* 117 * Close an X.25 Logical Channel. Discard all space held by the 118 * connection and internal descriptors. Wake up any sleepers. 119 */ 120 121 pk_close (lcp) 122 struct pklcd *lcp; 123 { 124 register struct socket *so = lcp -> lcd_so; 125 126 pk_freelcd (lcp); 127 128 if (so == NULL) 129 return; 130 131 so -> so_pcb = 0; 132 soisdisconnected (so); 133 /* sofree (so); /* gak!!! you can't do that here */ 134 } 135 136 /* 137 * Create a template to be used to send X.25 packets on a logical 138 * channel. It allocates an mbuf and fills in a skeletal packet 139 * depending on its type. This packet is passed to pk_output where 140 * the remainer of the packet is filled in. 141 */ 142 143 struct mbuf * 144 pk_template (lcn, type) 145 int lcn, type; 146 { 147 register struct mbuf *m; 148 register struct x25_packet *xp; 149 150 MGETHDR (m, M_DONTWAIT, MT_HEADER); 151 if (m == 0) 152 panic ("pk_template"); 153 m -> m_act = 0; 154 155 /* 156 * Efficiency hack: leave a four byte gap at the beginning 157 * of the packet level header with the hope that this will 158 * be enough room for the link level to insert its header. 159 */ 160 m -> m_data += max_linkhdr; 161 m -> m_pkthdr.len = m -> m_len = PKHEADERLN; 162 163 xp = mtod (m, struct x25_packet *); 164 *(long *)xp = 0; /* ugly, but fast */ 165 /* xp -> q_bit = 0;*/ 166 xp -> fmt_identifier = 1; 167 /* xp -> lc_group_number = 0;*/ 168 169 SET_LCN(xp, lcn); 170 xp -> packet_type = type; 171 172 return (m); 173 } 174 175 /* 176 * This routine restarts all the virtual circuits. Actually, 177 * the virtual circuits are not "restarted" as such. Instead, 178 * any active switched circuit is simply returned to READY 179 * state. 180 */ 181 182 pk_restart (pkp, restart_cause) 183 register struct pkcb *pkp; 184 int restart_cause; 185 { 186 register struct mbuf *m; 187 register struct pklcd *lcp; 188 register int i; 189 190 /* Restart all logical channels. */ 191 if (pkp -> pk_chan == 0) 192 return; 193 for (i = 1; i <= pkp -> pk_maxlcn; ++i) 194 if ((lcp = pkp -> pk_chan[i]) != NULL) { 195 if (lcp -> lcd_so) { 196 lcp -> lcd_so -> so_error = ENETRESET; 197 pk_close (lcp); 198 } else { 199 pk_flush (lcp); 200 lcp -> lcd_state = READY; 201 if (lcp -> lcd_upper) 202 lcp -> lcd_upper (lcp, 0); 203 } 204 } 205 206 if (restart_cause < 0) 207 return; 208 209 pkp -> pk_state = DTE_SENT_RESTART; 210 lcp = pkp -> pk_chan[0]; 211 m = lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESTART); 212 m -> m_pkthdr.len = m -> m_len += 2; 213 mtod (m, struct x25_packet *) -> packet_data = 0; /* DTE only */ 214 mtod (m, octet *)[4] = restart_cause; 215 pk_output (lcp); 216 } 217 218 219 /* 220 * This procedure frees up the Logical Channel Descripter. 221 */ 222 223 pk_freelcd (lcp) 224 register struct pklcd *lcp; 225 { 226 if (lcp == NULL) 227 return; 228 229 if (lcp -> lcd_lcn > 0) 230 lcp -> lcd_pkp -> pk_chan[lcp -> lcd_lcn] = NULL; 231 232 pk_flush (lcp); 233 remque (&lcp -> lcd_q); 234 free ((caddr_t)lcp, M_PCB); 235 } 236 237 238 /* 239 * Bind a address and protocol value to a socket. The important 240 * part is the protocol value - the first four characters of the 241 * Call User Data field. 242 */ 243 244 pk_bind (lcp, nam) 245 struct pklcd *lcp; 246 struct mbuf *nam; 247 { 248 register struct pkcb *pkp; 249 register struct pklcd *pp; 250 register struct sockaddr_x25 *sa; 251 252 if (nam == NULL) 253 return (EADDRNOTAVAIL); 254 if (lcp -> lcd_ceaddr) /* XXX */ 255 return (EADDRINUSE); 256 if (pk_checksockaddr (nam)) 257 return (EINVAL); 258 sa = mtod (nam, struct sockaddr_x25 *); 259 260 /* 261 * If the user wishes to accept calls only from a particular 262 * net (net != 0), make sure the net is known 263 */ 264 265 if (sa -> x25_net) 266 for (pkp = pkcbhead; ; pkp = pkp -> pk_next) { 267 if (pkp == 0) 268 return (ENETUNREACH); 269 if (pkp -> pk_xcp -> xc_addr.x25_net == sa -> x25_net) 270 break; 271 } 272 273 /* 274 * For ISO's sake permit default listeners, but only one such . . . 275 */ 276 for (pp = pk_listenhead; pp; pp = pp -> lcd_listen) { 277 register struct sockaddr_x25 *sa2 = pp -> lcd_ceaddr; 278 if ((sa2 -> x25_udlen == sa -> x25_udlen) && 279 (sa2 -> x25_udlen == 0 || 280 (bcmp (sa2 -> x25_udata, sa -> x25_udata, 281 min (sa2 -> x25_udlen, sa -> x25_udlen)) == 0))) 282 return (EADDRINUSE); 283 } 284 lcp -> lcd_laddr = *sa; 285 lcp -> lcd_ceaddr = &lcp -> lcd_laddr; 286 return (0); 287 } 288 289 /* 290 * Include a bound control block in the list of listeners. 291 */ 292 pk_listen (lcp) 293 register struct pklcd *lcp; 294 { 295 register struct pklcd **pp; 296 297 if (lcp -> lcd_ceaddr == 0) 298 return (EDESTADDRREQ); 299 300 lcp -> lcd_state = LISTEN; 301 /* 302 * Add default listener at end, any others at start. 303 */ 304 if (lcp -> lcd_ceaddr -> x25_udlen == 0) { 305 for (pp = &pk_listenhead; *pp; ) 306 pp = &((*pp) -> lcd_listen); 307 *pp = lcp; 308 } else { 309 lcp -> lcd_listen = pk_listenhead; 310 pk_listenhead = lcp; 311 } 312 return (0); 313 } 314 /* 315 * Include a listening control block for the benefit of other protocols. 316 */ 317 pk_protolisten (spi, spilen, callee) 318 int (*callee) (); 319 { 320 register struct pklcd *lcp = pk_attach ((struct socket *)0); 321 register struct mbuf *nam; 322 register struct sockaddr_x25 *sa; 323 int error = ENOBUFS; 324 325 if (lcp) { 326 if (nam = m_getclr (MT_SONAME, M_DONTWAIT)) { 327 sa = mtod (nam, struct sockaddr_x25 *); 328 sa -> x25_family = AF_CCITT; 329 sa -> x25_len = nam -> m_len = sizeof (*sa); 330 sa -> x25_udlen = spilen; 331 sa -> x25_udata[0] = spi; 332 lcp -> lcd_upper = callee; 333 lcp -> lcd_flags = X25_MBS_HOLD; 334 if ((error = pk_bind (lcp, nam)) == 0) 335 error = pk_listen (lcp); 336 (void) m_free (nam); 337 } 338 if (error) 339 pk_freelcd (lcp); 340 } 341 return error; /* Hopefully Zero !*/ 342 } 343 344 /* 345 * Associate a logical channel descriptor with a network. 346 * Fill in the default network specific parameters and then 347 * set any parameters explicitly specified by the user or 348 * by the remote DTE. 349 */ 350 351 pk_assoc (pkp, lcp, sa) 352 register struct pkcb *pkp; 353 register struct pklcd *lcp; 354 register struct sockaddr_x25 *sa; 355 { 356 357 lcp -> lcd_pkp = pkp; 358 lcp -> lcd_packetsize = pkp -> pk_xcp -> xc_psize; 359 lcp -> lcd_windowsize = pkp -> pk_xcp -> xc_pwsize; 360 lcp -> lcd_rsn = MODULUS - 1; 361 pkp -> pk_chan[lcp -> lcd_lcn] = lcp; 362 363 if (sa -> x25_opts.op_psize) 364 lcp -> lcd_packetsize = sa -> x25_opts.op_psize; 365 else 366 sa -> x25_opts.op_psize = lcp -> lcd_packetsize; 367 if (sa -> x25_opts.op_wsize) 368 lcp -> lcd_windowsize = sa -> x25_opts.op_wsize; 369 else 370 sa -> x25_opts.op_wsize = lcp -> lcd_windowsize; 371 sa -> x25_net = pkp -> pk_xcp -> xc_addr.x25_net; 372 lcp -> lcd_flags |= sa -> x25_opts.op_flags; 373 lcp -> lcd_stime = time.tv_sec; 374 } 375 376 pk_connect (lcp, sa) 377 register struct pklcd *lcp; 378 register struct sockaddr_x25 *sa; 379 { 380 register struct pkcb *pkp; 381 382 if (sa -> x25_addr[0] == '\0') 383 return (EDESTADDRREQ); 384 if (lcp -> lcd_pkp == 0) 385 for (pkp = pkcbhead; ; pkp = pkp -> pk_next) { 386 if (pkp == 0) 387 return (ENETUNREACH); 388 /* 389 * use first net configured (last in list 390 * headed by pkcbhead) if net is zero 391 * 392 * This is clearly bogus for many llc2's sharing 393 * the same xcp; we will replace this with a 394 * routing lookup. 395 */ 396 if (sa -> x25_net == 0 && pkp -> pk_next == 0) 397 break; 398 if (sa -> x25_net == pkp -> pk_xcp -> xc_addr.x25_net) 399 break; 400 } 401 402 if (pkp -> pk_state != DTE_READY) 403 return (ENETDOWN); 404 if ((lcp -> lcd_lcn = pk_getlcn (pkp)) == 0) 405 return (EMFILE); 406 lcp -> lcd_faddr = *sa; 407 lcp -> lcd_ceaddr = & lcp -> lcd_faddr; 408 pk_assoc (pkp, lcp, lcp -> lcd_ceaddr); 409 if (lcp -> lcd_so) 410 soisconnecting (lcp -> lcd_so); 411 lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL); 412 pk_callrequest (lcp, lcp -> lcd_ceaddr, pkp -> pk_xcp); 413 return (*pkp -> pk_ia -> ia_start) (lcp); 414 } 415 416 struct bcdinfo { 417 octet *cp; 418 unsigned posn; 419 }; 420 /* 421 * Build the rest of the CALL REQUEST packet. Fill in calling 422 * address, facilities fields and the user data field. 423 */ 424 425 pk_callrequest (lcp, sa, xcp) 426 struct pklcd *lcp; 427 register struct sockaddr_x25 *sa; 428 register struct x25config *xcp; 429 { 430 register struct x25_calladdr *a; 431 register struct mbuf *m = lcp -> lcd_template; 432 register struct x25_packet *xp = mtod (m, struct x25_packet *); 433 struct bcdinfo b; 434 435 if (lcp -> lcd_flags & X25_DBIT) 436 xp -> d_bit = 1; 437 a = (struct x25_calladdr *) &xp -> packet_data; 438 b.cp = (octet *) a -> address_field; 439 b.posn = 0; 440 a -> called_addrlen = to_bcd (&b, sa, xcp); 441 a -> calling_addrlen = to_bcd (&b, &xcp -> xc_addr, xcp); 442 if (b.posn & 0x01) 443 *b.cp++ &= 0xf0; 444 m -> m_pkthdr.len = m -> m_len += b.cp - (octet *) a; 445 446 if (lcp -> lcd_facilities) { 447 m -> m_pkthdr.len += 448 (m -> m_next = lcp -> lcd_facilities) -> m_pkthdr.len; 449 lcp -> lcd_facilities = 0; 450 } else 451 pk_build_facilities (m, sa, (int)xcp -> xc_type); 452 453 m_copyback (m, m -> m_pkthdr.len, sa -> x25_udlen, sa -> x25_udata); 454 } 455 456 pk_build_facilities (m, sa, type) 457 register struct mbuf *m; 458 struct sockaddr_x25 *sa; 459 { 460 register octet *cp; 461 register octet *fcp; 462 register int revcharge; 463 464 cp = mtod (m, octet *) + m -> m_len; 465 fcp = cp + 1; 466 revcharge = sa -> x25_opts.op_flags & X25_REVERSE_CHARGE ? 1 : 0; 467 /* 468 * This is specific to Datapac X.25(1976) DTEs. International 469 * calls must have the "hi priority" bit on. 470 */ 471 if (type == X25_1976 && sa -> x25_opts.op_psize == X25_PS128) 472 revcharge |= 02; 473 if (revcharge) { 474 *fcp++ = FACILITIES_REVERSE_CHARGE; 475 *fcp++ = revcharge; 476 } 477 switch (type) { 478 case X25_1980: 479 case X25_1984: 480 *fcp++ = FACILITIES_PACKETSIZE; 481 *fcp++ = sa -> x25_opts.op_psize; 482 *fcp++ = sa -> x25_opts.op_psize; 483 484 *fcp++ = FACILITIES_WINDOWSIZE; 485 *fcp++ = sa -> x25_opts.op_wsize; 486 *fcp++ = sa -> x25_opts.op_wsize; 487 } 488 *cp = fcp - cp - 1; 489 m -> m_pkthdr.len = (m -> m_len += *cp + 1); 490 } 491 492 to_bcd (b, sa, xcp) 493 register struct bcdinfo *b; 494 struct sockaddr_x25 *sa; 495 register struct x25config *xcp; 496 { 497 register char *x = sa -> x25_addr; 498 unsigned start = b -> posn; 499 /* 500 * The nodnic and prepnd0 stuff looks tedious, 501 * but it does allow full X.121 addresses to be used, 502 * which is handy for routing info (& OSI type 37 addresses). 503 */ 504 if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp -> xc_prepnd0)) { 505 char dnicname[sizeof(long) * NBBY/3 + 2]; 506 register char *p = dnicname; 507 508 sprintf (p, "%d", xcp -> xc_addr.x25_net & 0x7fff); 509 for (; *p; p++) /* *p == 0 means dnic matched */ 510 if ((*p ^ *x++) & 0x0f) 511 break; 512 if (*p || xcp -> xc_nodnic == 0) 513 x = sa -> x25_addr; 514 if (*p && xcp -> xc_prepnd0) { 515 if ((b -> posn)++ & 0x01) 516 *(b -> cp)++; 517 else 518 *(b -> cp) = 0; 519 } 520 } 521 while (*x) 522 if ((b -> posn)++ & 0x01) 523 *(b -> cp)++ |= *x++ & 0x0F; 524 else 525 *(b -> cp) = *x++ << 4; 526 return ((b -> posn) - start); 527 } 528 529 /* 530 * This routine gets the first available logical channel number. The 531 * search is from the highest number to lowest number (DTE). 532 */ 533 534 pk_getlcn (pkp) 535 register struct pkcb *pkp; 536 { 537 register int i; 538 539 if (pkp -> pk_chan == 0) 540 return (0); 541 for (i = pkp -> pk_maxlcn; i > 0; --i) 542 if (pkp -> pk_chan[i] == NULL) 543 break; 544 return (i); 545 546 } 547 548 /* 549 * This procedure sends a CLEAR request packet. The lc state is 550 * set to "SENT_CLEAR". 551 */ 552 553 pk_clear (lcp, diagnostic, abortive) 554 register struct pklcd *lcp; 555 { 556 register struct mbuf *m = pk_template (lcp -> lcd_lcn, X25_CLEAR); 557 558 m -> m_len += 2; 559 m -> m_pkthdr.len += 2; 560 mtod (m, struct x25_packet *) -> packet_data = 0; 561 mtod (m, octet *)[4] = diagnostic; 562 if (lcp -> lcd_facilities) { 563 m -> m_next = lcp -> lcd_facilities; 564 m -> m_pkthdr.len += m -> m_next -> m_len; 565 lcp -> lcd_facilities = 0; 566 } 567 if (abortive) 568 lcp -> lcd_template = m; 569 else { 570 struct socket *so = lcp -> lcd_so; 571 struct sockbuf *sb = so ? & so -> so_snd : & lcp -> lcd_sb; 572 sbappendrecord (sb, m); 573 } 574 pk_output (lcp); 575 576 } 577 578 /* 579 * This procedure generates RNR's or RR's to inhibit or enable 580 * inward data flow, if the current state changes (blocked ==> open or 581 * vice versa), or if forced to generate one. One forces RNR's to ack data. 582 */ 583 pk_flowcontrol (lcp, inhibit, forced) 584 register struct pklcd *lcp; 585 { 586 inhibit = (inhibit != 0); 587 if (lcp == 0 || lcp -> lcd_state != DATA_TRANSFER || 588 (forced == 0 && lcp -> lcd_rxrnr_condition == inhibit)) 589 return; 590 lcp -> lcd_rxrnr_condition = inhibit; 591 lcp -> lcd_template = 592 pk_template (lcp -> lcd_lcn, inhibit ? X25_RNR : X25_RR); 593 pk_output (lcp); 594 } 595 596 /* 597 * This procedure sends a RESET request packet. It re-intializes 598 * virtual circuit. 599 */ 600 601 static 602 pk_reset (lcp, diagnostic) 603 register struct pklcd *lcp; 604 { 605 register struct mbuf *m; 606 register struct socket *so = lcp -> lcd_so; 607 608 if (lcp -> lcd_state != DATA_TRANSFER) 609 return; 610 611 if (so) 612 so -> so_error = ECONNRESET; 613 lcp -> lcd_reset_condition = TRUE; 614 615 /* Reset all the control variables for the channel. */ 616 pk_flush (lcp); 617 lcp -> lcd_window_condition = lcp -> lcd_rnr_condition = 618 lcp -> lcd_intrconf_pending = FALSE; 619 lcp -> lcd_rsn = MODULUS - 1; 620 lcp -> lcd_ssn = 0; 621 lcp -> lcd_output_window = lcp -> lcd_input_window = 622 lcp -> lcd_last_transmitted_pr = 0; 623 m = lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET); 624 m -> m_pkthdr.len = m -> m_len += 2; 625 mtod (m, struct x25_packet *) -> packet_data = 0; 626 mtod (m, octet *)[4] = diagnostic; 627 pk_output (lcp); 628 629 } 630 631 /* 632 * This procedure frees all data queued for output or delivery on a 633 * virtual circuit. 634 */ 635 636 pk_flush (lcp) 637 register struct pklcd *lcp; 638 { 639 register struct socket *so; 640 641 if (lcp -> lcd_template) 642 m_freem (lcp -> lcd_template); 643 644 if (lcp -> lcd_cps) { 645 m_freem (lcp -> lcd_cps); 646 lcp -> lcd_cps = 0; 647 } 648 if (lcp -> lcd_facilities) { 649 m_freem (lcp -> lcd_facilities); 650 lcp -> lcd_facilities = 0; 651 } 652 if (so = lcp -> lcd_so) { 653 sbflush (&so -> so_rcv); 654 sbflush (&so -> so_snd); 655 } else 656 sbflush (&lcp -> lcd_sb); 657 } 658 659 /* 660 * This procedure handles all local protocol procedure errors. 661 */ 662 663 pk_procerror (error, lcp, errstr, diagnostic) 664 register struct pklcd *lcp; 665 char *errstr; 666 { 667 668 pk_message (lcp -> lcd_lcn, lcp -> lcd_pkp -> pk_xcp, errstr); 669 670 switch (error) { 671 case CLEAR: 672 if (lcp -> lcd_so) { 673 lcp -> lcd_so -> so_error = ECONNABORTED; 674 soisdisconnecting (lcp -> lcd_so); 675 } 676 pk_clear (lcp, diagnostic, 1); 677 break; 678 679 case RESET: 680 pk_reset (lcp, diagnostic); 681 } 682 } 683 684 /* 685 * This procedure is called during the DATA TRANSFER state to check 686 * and process the P(R) values received in the DATA, RR OR RNR 687 * packets. 688 */ 689 690 pk_ack (lcp, pr) 691 struct pklcd *lcp; 692 unsigned pr; 693 { 694 register struct socket *so = lcp -> lcd_so; 695 696 if (lcp -> lcd_output_window == pr) 697 return (PACKET_OK); 698 if (lcp -> lcd_output_window < lcp -> lcd_ssn) { 699 if (pr < lcp -> lcd_output_window || pr > lcp -> lcd_ssn) { 700 pk_procerror (RESET, lcp, 701 "p(r) flow control error", 2); 702 return (ERROR_PACKET); 703 } 704 } 705 else { 706 if (pr < lcp -> lcd_output_window && pr > lcp -> lcd_ssn) { 707 pk_procerror (RESET, lcp, 708 "p(r) flow control error #2", 2); 709 return (ERROR_PACKET); 710 } 711 } 712 713 lcp -> lcd_output_window = pr; /* Rotate window. */ 714 if (lcp -> lcd_window_condition == TRUE) 715 lcp -> lcd_window_condition = FALSE; 716 717 if (so && ((so -> so_snd.sb_flags & SB_WAIT) || so -> so_snd.sb_sel)) 718 sowwakeup (so); 719 720 return (PACKET_OK); 721 } 722 723 /* 724 * This procedure decodes the X.25 level 3 packet returning a 725 * code to be used in switchs or arrays. 726 */ 727 728 pk_decode (xp) 729 register struct x25_packet *xp; 730 { 731 register int type; 732 733 if (xp -> fmt_identifier != 1) 734 return (INVALID_PACKET); 735 #ifdef ancient_history 736 /* 737 * Make sure that the logical channel group number is 0. 738 * This restriction may be removed at some later date. 739 */ 740 if (xp -> lc_group_number != 0) 741 return (INVALID_PACKET); 742 #endif 743 /* 744 * Test for data packet first. 745 */ 746 if (!(xp -> packet_type & DATA_PACKET_DESIGNATOR)) 747 return (DATA); 748 749 /* 750 * Test if flow control packet (RR or RNR). 751 */ 752 if (!(xp -> packet_type & RR_OR_RNR_PACKET_DESIGNATOR)) 753 switch (xp -> packet_type & 0x1f) { 754 case X25_RR: 755 return (RR); 756 case X25_RNR: 757 return (RNR); 758 case X25_REJECT: 759 return (REJECT); 760 } 761 762 /* 763 * Determine the rest of the packet types. 764 */ 765 switch (xp -> packet_type) { 766 case X25_CALL: 767 type = CALL; 768 break; 769 770 case X25_CALL_ACCEPTED: 771 type = CALL_ACCEPTED; 772 break; 773 774 case X25_CLEAR: 775 type = CLEAR; 776 break; 777 778 case X25_CLEAR_CONFIRM: 779 type = CLEAR_CONF; 780 break; 781 782 case X25_INTERRUPT: 783 type = INTERRUPT; 784 break; 785 786 case X25_INTERRUPT_CONFIRM: 787 type = INTERRUPT_CONF; 788 break; 789 790 case X25_RESET: 791 type = RESET; 792 break; 793 794 case X25_RESET_CONFIRM: 795 type = RESET_CONF; 796 break; 797 798 case X25_RESTART: 799 type = RESTART; 800 break; 801 802 case X25_RESTART_CONFIRM: 803 type = RESTART_CONF; 804 break; 805 806 case X25_DIAGNOSTIC: 807 type = DIAG_TYPE; 808 break; 809 810 default: 811 type = INVALID_PACKET; 812 } 813 return (type); 814 } 815 816 /* 817 * A restart packet has been received. Print out the reason 818 * for the restart. 819 */ 820 821 pk_restartcause (pkp, xp) 822 struct pkcb *pkp; 823 register struct x25_packet *xp; 824 { 825 register struct x25config *xcp = pkp -> pk_xcp; 826 register int lcn = LCN(xp); 827 828 switch (xp -> packet_data) { 829 case X25_RESTART_LOCAL_PROCEDURE_ERROR: 830 pk_message (lcn, xcp, "restart: local procedure error"); 831 break; 832 833 case X25_RESTART_NETWORK_CONGESTION: 834 pk_message (lcn, xcp, "restart: network congestion"); 835 break; 836 837 case X25_RESTART_NETWORK_OPERATIONAL: 838 pk_message (lcn, xcp, "restart: network operational"); 839 break; 840 841 default: 842 pk_message (lcn, xcp, "restart: unknown cause"); 843 } 844 } 845 846 #define MAXRESETCAUSE 7 847 848 int Reset_cause[] = { 849 EXRESET, EXROUT, 0, EXRRPE, 0, EXRLPE, 0, EXRNCG 850 }; 851 852 /* 853 * A reset packet has arrived. Return the cause to the user. 854 */ 855 856 pk_resetcause (pkp, xp) 857 struct pkcb *pkp; 858 register struct x25_packet *xp; 859 { 860 register struct pklcd *lcp = 861 pkp -> pk_chan[LCN(xp)]; 862 register int code = xp -> packet_data; 863 864 if (code > MAXRESETCAUSE) 865 code = 7; /* EXRNCG */ 866 867 pk_message(LCN(xp), lcp -> lcd_pkp, "reset code 0x%x, diagnostic 0x%x", 868 xp -> packet_data, 4[(u_char *)xp]); 869 870 if (lcp -> lcd_so) 871 lcp -> lcd_so -> so_error = Reset_cause[code]; 872 } 873 874 #define MAXCLEARCAUSE 25 875 876 int Clear_cause[] = { 877 EXCLEAR, EXCBUSY, 0, EXCINV, 0, EXCNCG, 0, 878 0, 0, EXCOUT, 0, EXCAB, 0, EXCNOB, 0, 0, 0, EXCRPE, 879 0, EXCLPE, 0, 0, 0, 0, 0, EXCRRC 880 }; 881 882 /* 883 * A clear packet has arrived. Return the cause to the user. 884 */ 885 886 pk_clearcause (pkp, xp) 887 struct pkcb *pkp; 888 register struct x25_packet *xp; 889 { 890 register struct pklcd *lcp = 891 pkp -> pk_chan[LCN(xp)]; 892 register int code = xp -> packet_data; 893 894 if (code > MAXCLEARCAUSE) 895 code = 5; /* EXRNCG */ 896 if (lcp -> lcd_so) 897 lcp -> lcd_so -> so_error = Clear_cause[code]; 898 } 899 900 char * 901 format_ntn (xcp) 902 register struct x25config *xcp; 903 { 904 905 return (xcp -> xc_addr.x25_addr); 906 } 907 908 /* VARARGS1 */ 909 pk_message (lcn, xcp, fmt, a1, a2, a3, a4, a5, a6) 910 struct x25config *xcp; 911 char *fmt; 912 { 913 914 if (lcn) 915 if (pkcbhead -> pk_next) 916 printf ("X.25(%s): lcn %d: ", format_ntn (xcp), lcn); 917 else 918 printf ("X.25: lcn %d: ", lcn); 919 else 920 if (pkcbhead -> pk_next) 921 printf ("X.25(%s): ", format_ntn (xcp)); 922 else 923 printf ("X.25: "); 924 925 printf (fmt, a1, a2, a3, a4, a5, a6); 926 printf ("\n"); 927 } 928 929 pk_fragment (lcp, m0, qbit, mbit, wait) 930 struct mbuf *m0; 931 register struct pklcd *lcp; 932 { 933 register struct mbuf *m = m0; 934 register struct x25_packet *xp; 935 register struct sockbuf *sb; 936 struct mbuf *head = 0, *next, **mp = &head, *m_split (); 937 int totlen, psize = 1 << (lcp -> lcd_packetsize); 938 939 if (m == 0) 940 return 0; 941 if (m -> m_flags & M_PKTHDR == 0) 942 panic ("pk_fragment"); 943 totlen = m -> m_pkthdr.len; 944 m -> m_act = 0; 945 sb = lcp -> lcd_so ? &lcp -> lcd_so -> so_snd : & lcp -> lcd_sb; 946 do { 947 if (totlen > psize) { 948 if ((next = m_split (m, psize, wait)) == 0) 949 goto abort; 950 totlen -= psize; 951 } else 952 next = 0; 953 M_PREPEND(m, PKHEADERLN, wait); 954 if (m == 0) 955 goto abort; 956 *mp = m; 957 mp = & m -> m_act; 958 *mp = 0; 959 xp = mtod (m, struct x25_packet *); 960 0[(char *)xp] = 0; 961 if (qbit) 962 xp -> q_bit = 1; 963 if (lcp -> lcd_flags & X25_DBIT) 964 xp -> d_bit = 1; 965 xp -> fmt_identifier = 1; 966 xp -> packet_type = X25_DATA; 967 SET_LCN(xp, lcp -> lcd_lcn); 968 if (next || (mbit && (totlen == psize || 969 (lcp -> lcd_flags & X25_DBIT)))) 970 MBIT(xp) = 1; 971 } while (m = next); 972 for (m = head; m; m = next) { 973 next = m -> m_act; 974 m -> m_act = 0; 975 sbappendrecord (sb, m); 976 } 977 return 0; 978 abort: 979 if (wait) 980 panic ("pk_fragment null mbuf after wait"); 981 if (next) 982 m_freem (next); 983 for (m = head; m; m = next) { 984 next = m -> m_act; 985 m_freem (m); 986 } 987 return ENOBUFS; 988 } 989