141709Ssklower /* 241709Ssklower * Copyright (c) University of British Columbia, 1984 349252Ssklower * Copyright (c) 1991 The Regents of the University of California. 441709Ssklower * All rights reserved. 541709Ssklower * 641709Ssklower * This code is derived from software contributed to Berkeley by 741709Ssklower * the Laboratory for Computation Vision and the Computer Science Department 841709Ssklower * of the University of British Columbia. 941709Ssklower * 1041709Ssklower * %sccs.include.redist.c% 1141709Ssklower * 12*56530Sbostic * @(#)pk_input.c 7.16 (Berkeley) 10/11/92 1341709Ssklower */ 1441591Ssklower 15*56530Sbostic #include <sys/param.h> 16*56530Sbostic #include <sys/systm.h> 17*56530Sbostic #include <sys/mbuf.h> 18*56530Sbostic #include <sys/socket.h> 19*56530Sbostic #include <sys/protosw.h> 20*56530Sbostic #include <sys/socketvar.h> 21*56530Sbostic #include <sys/errno.h> 2241591Ssklower 23*56530Sbostic #include <net/if.h> 2441591Ssklower 25*56530Sbostic #include <netccitt/x25.h> 26*56530Sbostic #include <netccitt/pk.h> 27*56530Sbostic #include <netccitt/pk_var.h> 2841591Ssklower 2949930Ssklower struct pkcb * 3049930Ssklower pk_newlink (ia, llnext) 3149930Ssklower struct x25_ifaddr *ia; 3249930Ssklower caddr_t llnext; 3349930Ssklower { 3449930Ssklower register struct x25config *xcp = &ia->ia_xc; 3549930Ssklower register struct pkcb *pkp; 3649930Ssklower register struct pklcd *lcp; 3749930Ssklower register struct protosw *pp; 3849930Ssklower unsigned size; 3949930Ssklower 4049930Ssklower pp = pffindproto (AF_CCITT, (int)xcp -> xc_lproto, 0); 4149930Ssklower if (pp == 0 || pp -> pr_output == 0) { 4249930Ssklower pk_message (0, xcp, "link level protosw error"); 4349930Ssklower return ((struct pkcb *)0); 4449930Ssklower } 4549930Ssklower /* 4649930Ssklower * Allocate a network control block structure 4749930Ssklower */ 4849930Ssklower size = sizeof (struct pkcb); 4949930Ssklower pkp = (struct pkcb *)malloc(size, M_PCB, M_WAITOK); 5049930Ssklower if (pkp == 0) 5149930Ssklower return ((struct pkcb *)0); 5249930Ssklower bzero ((caddr_t)pkp, size); 5349930Ssklower pkp -> pk_lloutput = pp -> pr_output; 5449930Ssklower pkp -> pk_xcp = xcp; 5549930Ssklower pkp -> pk_ia = ia; 5649930Ssklower pkp -> pk_state = DTE_WAITING; 5749930Ssklower pkp -> pk_next = pkcbhead; 5849930Ssklower pkp -> pk_llnext = llnext; 5949930Ssklower pkcbhead = pkp; 6049930Ssklower 6149930Ssklower /* 6249930Ssklower * set defaults 6349930Ssklower */ 6449930Ssklower 6549930Ssklower if (xcp -> xc_pwsize == 0) 6649930Ssklower xcp -> xc_pwsize = DEFAULT_WINDOW_SIZE; 6749930Ssklower if (xcp -> xc_psize == 0) 6849930Ssklower xcp -> xc_psize = X25_PS128; 6949930Ssklower /* 7049930Ssklower * Allocate logical channel descriptor vector 7149930Ssklower */ 7249930Ssklower 7349930Ssklower (void)pk_resize(pkp); 7449930Ssklower return (pkp); 7549930Ssklower } 7649930Ssklower 7749930Ssklower pk_resize (pkp) 7849930Ssklower register struct pkcb *pkp; 7949930Ssklower { 8049930Ssklower struct pklcd *dev_lcp = 0; 8149930Ssklower struct x25config *xcp = pkp -> pk_xcp; 8249930Ssklower if (pkp -> pk_chan && 8349930Ssklower (pkp -> pk_maxlcn != xcp -> xc_maxlcn)) { 8449930Ssklower pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION); 8549930Ssklower dev_lcp = pkp -> pk_chan[0]; 8649930Ssklower free ((caddr_t)pkp -> pk_chan, M_IFADDR); 8749930Ssklower pkp -> pk_chan = 0; 8849930Ssklower } 8949930Ssklower if (pkp -> pk_chan == 0) { 9049930Ssklower unsigned size; 9149930Ssklower pkp -> pk_maxlcn = xcp -> xc_maxlcn; 9249930Ssklower size = (pkp -> pk_maxlcn + 1) * sizeof (struct pklcd *); 9349930Ssklower pkp -> pk_chan = 9449930Ssklower (struct pklcd **) malloc (size, M_IFADDR, M_WAITOK); 9549930Ssklower if (pkp -> pk_chan) { 9649930Ssklower bzero ((caddr_t)pkp -> pk_chan, size); 9749930Ssklower /* 9849930Ssklower * Allocate a logical channel descriptor for lcn 0 9949930Ssklower */ 10049930Ssklower if (dev_lcp == 0 && 10149930Ssklower (dev_lcp = pk_attach ((struct socket *)0)) == 0) 10249930Ssklower return (ENOBUFS); 10349930Ssklower dev_lcp -> lcd_state = READY; 10449930Ssklower dev_lcp -> lcd_pkp = pkp; 10549930Ssklower pkp -> pk_chan[0] = dev_lcp; 10649930Ssklower } else { 10749930Ssklower if (dev_lcp) 10849930Ssklower pk_close (dev_lcp); 10949930Ssklower return (ENOBUFS); 11049930Ssklower } 11149930Ssklower } 11249930Ssklower return 0; 11349930Ssklower } 11449930Ssklower 11541591Ssklower /* 11641591Ssklower * This procedure is called by the link level whenever the link 11741591Ssklower * becomes operational, is reset, or when the link goes down. 11841591Ssklower */ 11941591Ssklower 12049930Ssklower pk_ctlinput (code, pkp) 12149930Ssklower register struct pkcb *pkp; 12241591Ssklower { 12341591Ssklower 12445297Ssklower 12541591Ssklower switch (code) { 12641591Ssklower case PRC_LINKUP: 12741591Ssklower if (pkp -> pk_state == DTE_WAITING) 12841591Ssklower pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION); 12941591Ssklower break; 13041591Ssklower 13141591Ssklower case PRC_LINKDOWN: 13241591Ssklower pk_restart (pkp, -1); /* Clear all active circuits */ 13341591Ssklower pkp -> pk_state = DTE_WAITING; 13441591Ssklower break; 13541591Ssklower 13641591Ssklower case PRC_LINKRESET: 13741591Ssklower pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION); 13841591Ssklower break; 13941591Ssklower 14041591Ssklower } 14141591Ssklower return (0); 14241591Ssklower } 14345297Ssklower struct ifqueue pkintrq; 14445297Ssklower /* 14545297Ssklower * This routine is called if there are semi-smart devices that do HDLC 14645297Ssklower * in hardware and want to queue the packet and call level 3 directly 14745297Ssklower */ 14845297Ssklower pkintr () 14945297Ssklower { 15045297Ssklower register struct mbuf *m; 15145297Ssklower register struct ifaddr *ifa; 15245297Ssklower register struct ifnet *ifp; 15345297Ssklower register int s; 15441591Ssklower 15545297Ssklower for (;;) { 15645297Ssklower s = splimp (); 15745297Ssklower IF_DEQUEUE (&pkintrq, m); 15845297Ssklower splx (s); 15945297Ssklower if (m == 0) 16045297Ssklower break; 16145297Ssklower if (m->m_len < PKHEADERLN) { 16245297Ssklower printf ("pkintr: packet too short (len=%d)\n", 16345297Ssklower m->m_len); 16445297Ssklower m_freem (m); 16545297Ssklower continue; 16645297Ssklower } 16749930Ssklower pk_input(m); 16845297Ssklower } 16945297Ssklower } 17045297Ssklower struct mbuf *pk_bad_packet; 17149593Ssklower struct mbuf_cache pk_input_cache = {0 }; 17241591Ssklower /* 17341591Ssklower * X.25 PACKET INPUT 17441591Ssklower * 17541591Ssklower * This procedure is called by a link level procedure whenever 17641591Ssklower * an information frame is received. It decodes the packet and 17741591Ssklower * demultiplexes based on the logical channel number. 17841591Ssklower * 17949930Ssklower * We change the original conventions of the UBC code here -- 18049930Ssklower * since there may be multiple pkcb's for 802.2 class 2 18149930Ssklower * for a given interface, we must be informed which one it is; 18249930Ssklower * so we overwrite the pkthdr.rcvif; it can be recovered if necessary. 18349930Ssklower * 18441591Ssklower */ 18541591Ssklower 18649930Ssklower pk_input (m) 18741591Ssklower register struct mbuf *m; 18841591Ssklower { 18941591Ssklower register struct x25_packet *xp; 19041591Ssklower register struct pklcd *lcp; 19141591Ssklower register struct socket *so = 0; 19241591Ssklower register struct pkcb *pkp; 19341591Ssklower int ptype, lcn, lcdstate = LISTEN; 19441591Ssklower 19549593Ssklower if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize) 19649593Ssklower mbuf_cache(&pk_input_cache, m); 19749930Ssklower if ((m->m_flags & M_PKTHDR) == 0) 19849930Ssklower panic("pkintr"); 19949930Ssklower if ((pkp = (struct pkcb *)m->m_pkthdr.rcvif) == 0) 20049930Ssklower return; 20141591Ssklower xp = mtod (m, struct x25_packet *); 20241591Ssklower ptype = pk_decode (xp); 20345573Ssklower lcn = LCN(xp); 20441591Ssklower lcp = pkp -> pk_chan[lcn]; 20541591Ssklower 20641591Ssklower /* 20741591Ssklower * If the DTE is in Restart state, then it will ignore data, 20841591Ssklower * interrupt, call setup and clearing, flow control and reset 20941591Ssklower * packets. 21041591Ssklower */ 21141591Ssklower if (lcn < 0 || lcn > pkp -> pk_maxlcn) { 21241591Ssklower pk_message (lcn, pkp -> pk_xcp, "illegal lcn"); 21341591Ssklower m_freem (m); 21441591Ssklower return; 21541591Ssklower } 21641591Ssklower 21745895Ssklower pk_trace (pkp -> pk_xcp, m, "P-In"); 21841591Ssklower 21941591Ssklower if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) { 22041591Ssklower m_freem (m); 22141591Ssklower return; 22241591Ssklower } 22341591Ssklower if (lcp) { 22441591Ssklower so = lcp -> lcd_so; 22541591Ssklower lcdstate = lcp -> lcd_state; 22641591Ssklower } else { 22741591Ssklower if (ptype == CLEAR) { /* idle line probe (Datapac specific) */ 22841591Ssklower /* send response on lcd 0's output queue */ 22949930Ssklower lcp = pkp -> pk_chan[0]; 23041591Ssklower lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM); 23141591Ssklower pk_output (lcp); 23241591Ssklower m_freem (m); 23341591Ssklower return; 23441591Ssklower } 23541591Ssklower if (ptype != CALL) 23641591Ssklower ptype = INVALID_PACKET; 23741591Ssklower } 23841591Ssklower 23941591Ssklower if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) { 24045297Ssklower pk_message (0, pkp -> pk_xcp, "illegal ptype (%d, %s) on lcn 0", 24145297Ssklower ptype, pk_name[ptype / MAXSTATES]); 24245297Ssklower if (pk_bad_packet) 24345297Ssklower m_freem (pk_bad_packet); 24445297Ssklower pk_bad_packet = m; 24541591Ssklower return; 24641591Ssklower } 24741591Ssklower 24841591Ssklower switch (ptype + lcdstate) { 24941591Ssklower /* 25041591Ssklower * Incoming Call packet received. 25141591Ssklower */ 25241591Ssklower case CALL + LISTEN: 25349593Ssklower pk_incoming_call (pkp, m); 25441591Ssklower break; 25541591Ssklower 25641591Ssklower /* 25741591Ssklower * Call collision: Just throw this "incoming call" away since 25841591Ssklower * the DCE will ignore it anyway. 25941591Ssklower */ 26041591Ssklower case CALL + SENT_CALL: 26145573Ssklower pk_message ((int)lcn, pkp -> pk_xcp, 26241591Ssklower "incoming call collision"); 26341591Ssklower break; 26441591Ssklower 26541591Ssklower /* 26641591Ssklower * Call confirmation packet received. This usually means our 26741591Ssklower * previous connect request is now complete. 26841591Ssklower */ 26941591Ssklower case CALL_ACCEPTED + SENT_CALL: 27049252Ssklower MCHTYPE(m, MT_CONTROL); 27149593Ssklower pk_call_accepted (lcp, m); 27241591Ssklower break; 27341591Ssklower 27441591Ssklower /* 27541591Ssklower * This condition can only happen if the previous state was 27641591Ssklower * SENT_CALL. Just ignore the packet, eventually a clear 27741591Ssklower * confirmation should arrive. 27841591Ssklower */ 27941591Ssklower case CALL_ACCEPTED + SENT_CLEAR: 28041591Ssklower break; 28141591Ssklower 28241591Ssklower /* 28341591Ssklower * Clear packet received. This requires a complete tear down 28441591Ssklower * of the virtual circuit. Free buffers and control blocks. 28541591Ssklower * and send a clear confirmation. 28641591Ssklower */ 28741591Ssklower case CLEAR + READY: 28841591Ssklower case CLEAR + RECEIVED_CALL: 28941591Ssklower case CLEAR + SENT_CALL: 29041591Ssklower case CLEAR + DATA_TRANSFER: 29141591Ssklower lcp -> lcd_state = RECEIVED_CLEAR; 29241591Ssklower lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM); 29341591Ssklower pk_output (lcp); 29441591Ssklower pk_clearcause (pkp, xp); 29549252Ssklower if (lcp -> lcd_upper) { 29649252Ssklower MCHTYPE(m, MT_CONTROL); 29749252Ssklower lcp -> lcd_upper (lcp, m); 29849252Ssklower } 29941591Ssklower pk_close (lcp); 30049252Ssklower lcp = 0; 30141591Ssklower break; 30241591Ssklower 30341591Ssklower /* 30441591Ssklower * Clear collision: Treat this clear packet as a confirmation. 30541591Ssklower */ 30641591Ssklower case CLEAR + SENT_CLEAR: 30741591Ssklower pk_close (lcp); 30841591Ssklower break; 30941591Ssklower 31041591Ssklower /* 31141591Ssklower * Clear confirmation received. This usually means the virtual 31241591Ssklower * circuit is now completely removed. 31341591Ssklower */ 31441591Ssklower case CLEAR_CONF + SENT_CLEAR: 31541591Ssklower pk_close (lcp); 31641591Ssklower break; 31741591Ssklower 31841591Ssklower /* 31941591Ssklower * A clear confirmation on an unassigned logical channel - just 32041591Ssklower * ignore it. Note: All other packets on an unassigned channel 32141591Ssklower * results in a clear. 32241591Ssklower */ 32341591Ssklower case CLEAR_CONF + READY: 32449930Ssklower case CLEAR_CONF + LISTEN: 32541591Ssklower break; 32641591Ssklower 32741591Ssklower /* 32841591Ssklower * Data packet received. Pass on to next level. Move the Q and M 32941591Ssklower * bits into the data portion for the next level. 33041591Ssklower */ 33141591Ssklower case DATA + DATA_TRANSFER: 33241591Ssklower if (lcp -> lcd_reset_condition) { 33341591Ssklower ptype = DELETE_PACKET; 33441591Ssklower break; 33541591Ssklower } 33641591Ssklower 33741591Ssklower /* 33841591Ssklower * Process the P(S) flow control information in this Data packet. 33941591Ssklower * Check that the packets arrive in the correct sequence and that 34041591Ssklower * they are within the "lcd_input_window". Input window rotation is 34141591Ssklower * initiated by the receive interface. 34241591Ssklower */ 34341591Ssklower 34441591Ssklower if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) || 34541591Ssklower PS(xp) == ((lcp -> lcd_input_window + lcp->lcd_windowsize) % MODULUS)) { 34641591Ssklower m_freem (m); 34745895Ssklower pk_procerror (RESET, lcp, "p(s) flow control error", 1); 34841591Ssklower break; 34941591Ssklower } 35041591Ssklower lcp -> lcd_rsn = PS(xp); 35141591Ssklower 35241591Ssklower if (pk_ack (lcp, PR(xp)) != PACKET_OK) { 35341591Ssklower m_freem (m); 35441591Ssklower break; 35541591Ssklower } 35645895Ssklower m -> m_data += PKHEADERLN; 35745895Ssklower m -> m_len -= PKHEADERLN; 35845895Ssklower m -> m_pkthdr.len -= PKHEADERLN; 35945895Ssklower 36049930Ssklower lcp -> lcd_rxcnt++; 36145895Ssklower if (lcp -> lcd_flags & X25_MBS_HOLD) { 36245895Ssklower register struct mbuf *n = lcp -> lcd_cps; 36345895Ssklower int mbit = MBIT(xp); 36445895Ssklower octet q_and_d_bits; 36545895Ssklower 36645895Ssklower if (n) { 36745895Ssklower n -> m_pkthdr.len += m -> m_pkthdr.len; 36845895Ssklower while (n -> m_next) 36945895Ssklower n = n -> m_next; 37045895Ssklower n -> m_next = m; 37145895Ssklower m = lcp -> lcd_cps; 37245895Ssklower 37345895Ssklower if (lcp -> lcd_cpsmax && 37445895Ssklower n -> m_pkthdr.len > lcp -> lcd_cpsmax) { 37545895Ssklower pk_procerror (RESET, lcp, 37645895Ssklower "C.P.S. overflow", 128); 37745895Ssklower return; 37845895Ssklower } 37945895Ssklower q_and_d_bits = 0xc0 & *(octet *)xp; 38045895Ssklower xp = (struct x25_packet *) 38145895Ssklower (mtod(m, octet *) - PKHEADERLN); 38245895Ssklower *(octet *)xp |= q_and_d_bits; 38345895Ssklower } 38445895Ssklower if (mbit) { 38545895Ssklower lcp -> lcd_cps = m; 38647268Ssklower pk_flowcontrol(lcp, 0, 1); 38745895Ssklower return; 38845895Ssklower } 38945895Ssklower lcp -> lcd_cps = 0; 39045895Ssklower } 39145297Ssklower if (so == 0) 39245297Ssklower break; 39341591Ssklower if (lcp -> lcd_flags & X25_MQBIT) { 39445573Ssklower octet t = (xp -> q_bit) ? t = 0x80 : 0; 39541591Ssklower 39645573Ssklower if (MBIT(xp)) 39745573Ssklower t |= 0x40; 39843361Ssklower m -> m_data -= 1; 39941591Ssklower m -> m_len += 1; 40045895Ssklower m -> m_pkthdr.len += 1; 40145573Ssklower *mtod(m, octet *) = t; 40241591Ssklower } 40341591Ssklower 40441591Ssklower /* 40541591Ssklower * Discard Q-BIT packets if the application 40641591Ssklower * doesn't want to be informed of M and Q bit status 40741591Ssklower */ 40841591Ssklower if (xp -> q_bit && (lcp -> lcd_flags & X25_MQBIT) == 0) { 40941591Ssklower m_freem (m); 41041591Ssklower /* 41141591Ssklower * NB. This is dangerous: sending a RR here can 41241591Ssklower * cause sequence number errors if a previous data 41341591Ssklower * packet has not yet been passed up to the application 41441591Ssklower * (RR's are normally generated via PRU_RCVD). 41541591Ssklower */ 41647268Ssklower pk_flowcontrol(lcp, 0, 1); 41741591Ssklower } else { 41841591Ssklower sbappendrecord (&so -> so_rcv, m); 41941591Ssklower sorwakeup (so); 42041591Ssklower } 42141591Ssklower break; 42241591Ssklower 42341591Ssklower /* 42441591Ssklower * Interrupt packet received. 42541591Ssklower */ 42641591Ssklower case INTERRUPT + DATA_TRANSFER: 42741591Ssklower if (lcp -> lcd_reset_condition) 42841591Ssklower break; 42941591Ssklower lcp -> lcd_intrdata = xp -> packet_data; 43041591Ssklower lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM); 43141591Ssklower pk_output (lcp); 43245895Ssklower m -> m_data += PKHEADERLN; 43345895Ssklower m -> m_len -= PKHEADERLN; 43445895Ssklower m -> m_pkthdr.len -= PKHEADERLN; 43545297Ssklower MCHTYPE(m, MT_OOBDATA); 43645895Ssklower if (so) { 43745895Ssklower if (so -> so_options & SO_OOBINLINE) 43845895Ssklower sbinsertoob (&so -> so_rcv, m); 43945895Ssklower else 44045895Ssklower m_freem (m); 44145297Ssklower sohasoutofband (so); 44245895Ssklower } 44341591Ssklower break; 44441591Ssklower 44541591Ssklower /* 44641591Ssklower * Interrupt confirmation packet received. 44741591Ssklower */ 44841591Ssklower case INTERRUPT_CONF + DATA_TRANSFER: 44941591Ssklower if (lcp -> lcd_reset_condition) 45041591Ssklower break; 45141591Ssklower if (lcp -> lcd_intrconf_pending == TRUE) 45241591Ssklower lcp -> lcd_intrconf_pending = FALSE; 45341591Ssklower else 45445895Ssklower pk_procerror (RESET, lcp, "unexpected packet", 43); 45541591Ssklower break; 45641591Ssklower 45741591Ssklower /* 45841591Ssklower * Receiver ready received. Rotate the output window and output 45941591Ssklower * any data packets waiting transmission. 46041591Ssklower */ 46141591Ssklower case RR + DATA_TRANSFER: 46245297Ssklower if (lcp -> lcd_reset_condition || 46345297Ssklower pk_ack (lcp, PR(xp)) != PACKET_OK) { 46445297Ssklower ptype = DELETE_PACKET; 46541591Ssklower break; 46645297Ssklower } 46741591Ssklower if (lcp -> lcd_rnr_condition == TRUE) 46841591Ssklower lcp -> lcd_rnr_condition = FALSE; 46941591Ssklower pk_output (lcp); 47041591Ssklower break; 47141591Ssklower 47241591Ssklower /* 47341591Ssklower * Receiver Not Ready received. Packets up to the P(R) can be 47441591Ssklower * be sent. Condition is cleared with a RR. 47541591Ssklower */ 47641591Ssklower case RNR + DATA_TRANSFER: 47745297Ssklower if (lcp -> lcd_reset_condition || 47845297Ssklower pk_ack (lcp, PR(xp)) != PACKET_OK) { 47945297Ssklower ptype = DELETE_PACKET; 48041591Ssklower break; 48145297Ssklower } 48241591Ssklower lcp -> lcd_rnr_condition = TRUE; 48341591Ssklower break; 48441591Ssklower 48541591Ssklower /* 48641591Ssklower * Reset packet received. Set state to FLOW_OPEN. The Input and 48741591Ssklower * Output window edges ar set to zero. Both the send and receive 48841591Ssklower * numbers are reset. A confirmation is returned. 48941591Ssklower */ 49041591Ssklower case RESET + DATA_TRANSFER: 49141591Ssklower if (lcp -> lcd_reset_condition) 49241591Ssklower /* Reset collision. Just ignore packet. */ 49341591Ssklower break; 49441591Ssklower 49541591Ssklower pk_resetcause (pkp, xp); 49641591Ssklower lcp -> lcd_window_condition = lcp -> lcd_rnr_condition = 49741591Ssklower lcp -> lcd_intrconf_pending = FALSE; 49841591Ssklower lcp -> lcd_output_window = lcp -> lcd_input_window = 49941591Ssklower lcp -> lcd_last_transmitted_pr = 0; 50041591Ssklower lcp -> lcd_ssn = 0; 50141591Ssklower lcp -> lcd_rsn = MODULUS - 1; 50241591Ssklower 50341591Ssklower lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM); 50441591Ssklower pk_output (lcp); 50545297Ssklower 50645895Ssklower pk_flush(lcp); 50745297Ssklower if (so == 0) 50845297Ssklower break; 50945297Ssklower wakeup ((caddr_t) & so -> so_timeo); 51045297Ssklower sorwakeup (so); 51145297Ssklower sowwakeup (so); 51241591Ssklower break; 51341591Ssklower 51441591Ssklower /* 51541591Ssklower * Reset confirmation received. 51641591Ssklower */ 51741591Ssklower case RESET_CONF + DATA_TRANSFER: 51841591Ssklower if (lcp -> lcd_reset_condition) { 51941591Ssklower lcp -> lcd_reset_condition = FALSE; 52041591Ssklower pk_output (lcp); 52141591Ssklower } 52241591Ssklower else 52345895Ssklower pk_procerror (RESET, lcp, "unexpected packet", 32); 52441591Ssklower break; 52541591Ssklower 52641591Ssklower case DATA + SENT_CLEAR: 52741591Ssklower ptype = DELETE_PACKET; 52841591Ssklower case RR + SENT_CLEAR: 52941591Ssklower case RNR + SENT_CLEAR: 53041591Ssklower case INTERRUPT + SENT_CLEAR: 53141591Ssklower case INTERRUPT_CONF + SENT_CLEAR: 53241591Ssklower case RESET + SENT_CLEAR: 53341591Ssklower case RESET_CONF + SENT_CLEAR: 53445297Ssklower /* Just ignore p if we have sent a CLEAR already. 53541591Ssklower */ 53641591Ssklower break; 53741591Ssklower 53841591Ssklower /* 53941591Ssklower * Restart sets all the permanent virtual circuits to the "Data 54041591Ssklower * Transfer" stae and all the switched virtual circuits to the 54141591Ssklower * "Ready" state. 54241591Ssklower */ 54341591Ssklower case RESTART + READY: 54441591Ssklower switch (pkp -> pk_state) { 54541591Ssklower case DTE_SENT_RESTART: 54641591Ssklower /* Restart collision. */ 54741591Ssklower pkp -> pk_state = DTE_READY; 54841591Ssklower pk_message (0, pkp -> pk_xcp, 54941591Ssklower "Packet level operational"); 55041591Ssklower break; 55141591Ssklower 55241591Ssklower default: 55341591Ssklower pk_restart (pkp, -1); 55441591Ssklower pk_restartcause (pkp, xp); 55541591Ssklower pkp -> pk_chan[0] -> lcd_template = pk_template (0, 55641591Ssklower X25_RESTART_CONFIRM); 55741591Ssklower pk_output (pkp -> pk_chan[0]); 55841591Ssklower } 55941591Ssklower break; 56041591Ssklower 56141591Ssklower /* 56241591Ssklower * Restart confirmation received. All logical channels are set 56341591Ssklower * to READY. 56441591Ssklower */ 56541591Ssklower case RESTART_CONF + READY: 56641591Ssklower switch (pkp -> pk_state) { 56741591Ssklower case DTE_SENT_RESTART: 56841591Ssklower pkp -> pk_state = DTE_READY; 56941591Ssklower pk_message (0, pkp -> pk_xcp, 57041591Ssklower "Packet level operational"); 57141591Ssklower break; 57241591Ssklower 57341591Ssklower default: 57441591Ssklower /* Restart local procedure error. */ 57541591Ssklower pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR); 57641591Ssklower pkp -> pk_state = DTE_SENT_RESTART; 57741591Ssklower } 57841591Ssklower break; 57941591Ssklower 58041591Ssklower default: 58141591Ssklower if (lcp) { 58245895Ssklower pk_procerror (CLEAR, lcp, "unknown packet error", 33); 58341591Ssklower pk_message (lcn, pkp -> pk_xcp, 58441591Ssklower "\"%s\" unexpected in \"%s\" state", 58541591Ssklower pk_name[ptype/MAXSTATES], pk_state[lcdstate]); 58645895Ssklower } else 58745573Ssklower pk_message (lcn, pkp -> pk_xcp, 58841591Ssklower "packet arrived on unassigned lcn"); 58941591Ssklower break; 59041591Ssklower } 59149252Ssklower if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) { 59245895Ssklower if (ptype != DATA && ptype != INTERRUPT) 59345895Ssklower MCHTYPE(m, MT_CONTROL); 59445297Ssklower lcp -> lcd_upper (lcp, m); 59545895Ssklower } else if (ptype != DATA && ptype != INTERRUPT) 59641591Ssklower m_freem (m); 59741591Ssklower } 59841591Ssklower 59949930Ssklower static 60049930Ssklower prune_dnic(from, to, dnicname, xcp) 60149930Ssklower char *from, *to, *dnicname; 60249930Ssklower register struct x25config *xcp; 60349930Ssklower { 60449930Ssklower register char *cp1 = from, *cp2 = from; 60549930Ssklower if (xcp->xc_prepnd0 && *cp1 == '0') { 60649930Ssklower from = ++cp1; 60749930Ssklower goto copyrest; 60849930Ssklower } 60949930Ssklower if (xcp->xc_nodnic) { 61049930Ssklower for (cp1 = dnicname; *cp2 = *cp1++;) 61149930Ssklower cp2++; 61249930Ssklower cp1 = from; 61349930Ssklower } 61449930Ssklower copyrest: 61549930Ssklower for (cp1 = dnicname; *cp2 = *cp1++;) 61649930Ssklower cp2++; 61749930Ssklower } 61849930Ssklower /* static */ 61949930Ssklower pk_simple_bsd (from, to, lower, len) 62049930Ssklower register octet *from, *to; 62149930Ssklower register len, lower; 62249930Ssklower { 62349930Ssklower register int c; 62449930Ssklower while (--len >= 0) { 62549930Ssklower c = *from; 62649930Ssklower if (lower & 0x01) 62749930Ssklower *from++; 62849930Ssklower else 62949930Ssklower c >>= 4; 63049930Ssklower c &= 0x0f; c |= 0x30; *to++ = c; lower++; 63149930Ssklower } 63249930Ssklower *to = 0; 63349930Ssklower } 63441591Ssklower 63549930Ssklower /*static octet * */ 63649930Ssklower pk_from_bcd (a, iscalling, sa, xcp) 63749930Ssklower register struct x25_calladdr *a; 63849930Ssklower register struct sockaddr_x25 *sa; 63949930Ssklower register struct x25config *xcp; 64049930Ssklower { 64149930Ssklower octet buf[MAXADDRLN+1]; 64249930Ssklower octet *cp; 64349930Ssklower unsigned count; 64449930Ssklower 64549930Ssklower bzero ((caddr_t)sa, sizeof (*sa)); 64649930Ssklower sa -> x25_len = sizeof (*sa); 64749930Ssklower sa -> x25_family = AF_CCITT; 64849930Ssklower if (iscalling) { 64949930Ssklower cp = a -> address_field + (a -> called_addrlen / 2); 65049930Ssklower count = a -> calling_addrlen; 65149930Ssklower pk_simple_bsd (cp, buf, a -> called_addrlen, count); 65249930Ssklower } else { 65349930Ssklower count = a -> called_addrlen; 65449930Ssklower pk_simple_bsd (a -> address_field, buf, 0, count); 65549930Ssklower } 65649930Ssklower if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp ->xc_prepnd0)) { 65749930Ssklower octet dnicname[sizeof(long) * NBBY/3 + 2]; 65849930Ssklower 65952449Ssklower sprintf ((char *)dnicname, "%d", xcp -> xc_addr.x25_net); 66049930Ssklower prune_dnic (buf, sa -> x25_addr, dnicname, xcp); 66149930Ssklower } else 66249930Ssklower bcopy ((caddr_t)buf, (caddr_t)sa -> x25_addr, count + 1); 66349930Ssklower } 66449930Ssklower 66549930Ssklower static 66649930Ssklower save_extra(m0, fp, so) 66749930Ssklower struct mbuf *m0; 66849930Ssklower octet *fp; 66949930Ssklower struct socket *so; 67049930Ssklower { 67149930Ssklower register struct mbuf *m; 67249930Ssklower struct cmsghdr cmsghdr; 67352449Ssklower if (m = m_copy (m, 0, (int)M_COPYALL)) { 67449930Ssklower int off = fp - mtod (m0, octet *); 67549930Ssklower int len = m->m_pkthdr.len - off + sizeof (cmsghdr); 67649930Ssklower cmsghdr.cmsg_len = len; 67749930Ssklower cmsghdr.cmsg_level = AF_CCITT; 67849930Ssklower cmsghdr.cmsg_type = PK_FACILITIES; 67949930Ssklower m_adj (m, off); 68049930Ssklower M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT); 68149930Ssklower if (m == 0) 68249930Ssklower return; 68349930Ssklower bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr)); 68449930Ssklower MCHTYPE(m, MT_CONTROL); 68549930Ssklower sbappendrecord(&so -> so_rcv, m); 68649930Ssklower } 68749930Ssklower } 68849930Ssklower 68941591Ssklower /* 69041591Ssklower * This routine handles incoming call packets. It matches the protocol 69141591Ssklower * field on the Call User Data field (usually the first four bytes) with 69241591Ssklower * sockets awaiting connections. 69341591Ssklower */ 69441591Ssklower 69549593Ssklower pk_incoming_call (pkp, m0) 69645895Ssklower struct mbuf *m0; 69741591Ssklower struct pkcb *pkp; 69841591Ssklower { 69942277Ssklower register struct pklcd *lcp = 0, *l; 70041591Ssklower register struct sockaddr_x25 *sa; 70141591Ssklower register struct x25_calladdr *a; 70242277Ssklower register struct socket *so = 0; 70349930Ssklower struct x25_packet *xp = mtod(m0, struct x25_packet *); 70449930Ssklower struct mbuf *m; 70549930Ssklower struct x25config *xcp = pkp -> pk_xcp; 70645895Ssklower int len = m0->m_pkthdr.len; 70749930Ssklower unsigned udlen; 70849930Ssklower char *errstr = "server unavailable"; 70945895Ssklower octet *u, *facp; 71045573Ssklower int lcn = LCN(xp); 71141591Ssklower 71249930Ssklower /* First, copy the data from the incoming call packet to a X25 address 71349930Ssklower descriptor. It is to be regretted that you have 71449930Ssklower to parse the facilities into a sockaddr to determine 71549930Ssklower if reverse charging is being requested */ 71649930Ssklower if ((m = m_get (M_DONTWAIT, MT_SONAME)) == 0) 71741591Ssklower return; 71841591Ssklower sa = mtod (m, struct sockaddr_x25 *); 71949930Ssklower a = (struct x25_calladdr *) &xp -> packet_data; 72049930Ssklower facp = u = (octet *) (a -> address_field + 72149930Ssklower ((a -> called_addrlen + a -> calling_addrlen + 1) / 2)); 72249930Ssklower u += *u + 1; 72349930Ssklower udlen = min (16, ((octet *)xp) + len - u); 72449930Ssklower if (udlen < 0) 72549930Ssklower udlen = 0; 72649930Ssklower pk_from_bcd (a, 1, sa, pkp -> pk_xcp); /* get calling address */ 72750426Ssklower pk_parse_facilities (facp, sa); 72849930Ssklower bcopy ((caddr_t)u, sa -> x25_udata, udlen); 72949930Ssklower sa -> x25_udlen = udlen; 73041591Ssklower 73141591Ssklower /* 73250426Ssklower * Now, loop through the listen sockets looking for a match on the 73350426Ssklower * PID. That is the first few octets of the user data field. 73450426Ssklower * This is the closest thing to a port number for X.25 packets. 73550426Ssklower * It does provide a way of multiplexing services at the user level. 73641591Ssklower */ 73741591Ssklower 73841591Ssklower for (l = pk_listenhead; l; l = l -> lcd_listen) { 73941591Ssklower struct sockaddr_x25 *sxp = l -> lcd_ceaddr; 74041591Ssklower 74149930Ssklower if (bcmp (sxp -> x25_udata, u, sxp->x25_udlen)) 74241591Ssklower continue; 74345165Ssklower if (sxp -> x25_net && 74449930Ssklower sxp -> x25_net != xcp -> xc_addr.x25_net) 74541591Ssklower continue; 74641591Ssklower /* 74749930Ssklower * don't accept incoming calls with the D-Bit on 74849930Ssklower * unless the server agrees 74949930Ssklower */ 75049930Ssklower if (xp -> d_bit && !(sxp -> x25_opts.op_flags & X25_DBIT)) { 75149930Ssklower errstr = "incoming D-Bit mismatch"; 75249930Ssklower break; 75349930Ssklower } 75449930Ssklower /* 75541591Ssklower * don't accept incoming collect calls unless 75641591Ssklower * the server sets the reverse charging option. 75741591Ssklower */ 75841591Ssklower if ((sxp -> x25_opts.op_flags & (X25_OLDSOCKADDR|X25_REVERSE_CHARGE)) == 0 && 75941591Ssklower sa -> x25_opts.op_flags & X25_REVERSE_CHARGE) { 76041591Ssklower errstr = "incoming collect call refused"; 76141591Ssklower break; 76241591Ssklower } 76342277Ssklower if (l -> lcd_so) { 76445165Ssklower if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED)) 76542277Ssklower lcp = (struct pklcd *) so -> so_pcb; 76642277Ssklower } else 76742277Ssklower lcp = pk_attach((struct socket *) 0); 76842277Ssklower if (lcp == 0) { 76941591Ssklower /* 77041591Ssklower * Insufficient space or too many unaccepted 77141591Ssklower * connections. Just throw the call away. 77241591Ssklower */ 77341591Ssklower errstr = "server malfunction"; 77441591Ssklower break; 77541591Ssklower } 77643361Ssklower lcp -> lcd_upper = l -> lcd_upper; 77743361Ssklower lcp -> lcd_upnext = l -> lcd_upnext; 77841591Ssklower lcp -> lcd_lcn = lcn; 77941591Ssklower lcp -> lcd_state = RECEIVED_CALL; 78050020Ssklower sa -> x25_opts.op_flags |= (sxp -> x25_opts.op_flags & 78150020Ssklower ~X25_REVERSE_CHARGE) | l -> lcd_flags; 78241591Ssklower pk_assoc (pkp, lcp, sa); 78349930Ssklower lcp -> lcd_faddr = *sa; 78449930Ssklower lcp -> lcd_laddr.x25_udlen = sxp -> x25_udlen; 78549930Ssklower lcp -> lcd_craddr = &lcp->lcd_faddr; 78641591Ssklower lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED); 78745573Ssklower if (lcp -> lcd_flags & X25_DBIT) { 78845573Ssklower if (xp -> d_bit) 78945895Ssklower mtod(lcp -> lcd_template, 79045895Ssklower struct x25_packet *) -> d_bit = 1; 79145573Ssklower else 79245573Ssklower lcp -> lcd_flags &= ~X25_DBIT; 79345573Ssklower } 79443361Ssklower if (so) { 79543361Ssklower pk_output (lcp); 79642277Ssklower soisconnected (so); 79745895Ssklower if (so -> so_options & SO_OOBINLINE) 79845895Ssklower save_extra(m0, facp, so); 79945895Ssklower } else if (lcp -> lcd_upper) { 80049930Ssklower (*lcp -> lcd_upper) (lcp, m0); 80145895Ssklower } 80249930Ssklower (void) m_free (m); 80341591Ssklower return; 80441591Ssklower } 80541591Ssklower 80641591Ssklower /* 80741591Ssklower * If the call fails for whatever reason, we still need to build a 80841591Ssklower * skeleton LCD in order to be able to properly receive the CLEAR 80941591Ssklower * CONFIRMATION. 81041591Ssklower */ 81141591Ssklower #ifdef WATERLOO /* be explicit */ 81241591Ssklower if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0) 81341591Ssklower pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s", 81441591Ssklower sa->x25_addr, sa->x25_udata[3] & 0xff, errstr); 81541591Ssklower else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0) 81641591Ssklower pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s", 81741591Ssklower sa->x25_addr, errstr); 81841591Ssklower else 81941591Ssklower #endif 82041591Ssklower pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s", 82141591Ssklower sa -> x25_addr, sa -> x25_udata[0] & 0xff, 82241591Ssklower sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff, 82341591Ssklower sa -> x25_udata[3] & 0xff, errstr); 82445297Ssklower if ((lcp = pk_attach((struct socket *)0)) == 0) { 82545297Ssklower (void) m_free (m); 82641591Ssklower return; 82741591Ssklower } 82841591Ssklower lcp -> lcd_lcn = lcn; 82941591Ssklower lcp -> lcd_state = RECEIVED_CALL; 83041591Ssklower pk_assoc (pkp, lcp, sa); 83145297Ssklower (void) m_free (m); 83245895Ssklower pk_clear (lcp, 0, 1); 83341591Ssklower } 83441591Ssklower 83549593Ssklower pk_call_accepted (lcp, m) 83641591Ssklower struct pklcd *lcp; 83749252Ssklower struct mbuf *m; 83841591Ssklower { 83941591Ssklower register struct x25_calladdr *ap; 84041591Ssklower register octet *fcp; 84149252Ssklower struct x25_packet *xp = mtod (m, struct x25_packet *); 84249252Ssklower int len = m -> m_len; 84341591Ssklower 84441591Ssklower lcp -> lcd_state = DATA_TRANSFER; 84545297Ssklower if (lcp -> lcd_so) 84645297Ssklower soisconnected (lcp -> lcd_so); 84745573Ssklower if ((lcp -> lcd_flags & X25_DBIT) && (xp -> d_bit == 0)) 84845573Ssklower lcp -> lcd_flags &= ~X25_DBIT; 84941591Ssklower if (len > 3) { 85041591Ssklower ap = (struct x25_calladdr *) &xp -> packet_data; 85141591Ssklower fcp = (octet *) ap -> address_field + (ap -> calling_addrlen + 85241591Ssklower ap -> called_addrlen + 1) / 2; 85341591Ssklower if (fcp + *fcp <= ((octet *)xp) + len) 85449593Ssklower pk_parse_facilities (fcp, lcp -> lcd_ceaddr); 85541591Ssklower } 85641591Ssklower pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr); 85749252Ssklower if (lcp -> lcd_so == 0 && lcp -> lcd_upper) 85849252Ssklower lcp -> lcd_upper(lcp, m); 85941591Ssklower } 86041591Ssklower 86149593Ssklower pk_parse_facilities (fcp, sa) 86241591Ssklower register octet *fcp; 86341591Ssklower register struct sockaddr_x25 *sa; 86441591Ssklower { 86541591Ssklower register octet *maxfcp; 86641591Ssklower 86741591Ssklower maxfcp = fcp + *fcp; 86841591Ssklower fcp++; 86941591Ssklower while (fcp < maxfcp) { 87041591Ssklower /* 87141591Ssklower * Ignore national DCE or DTE facilities 87241591Ssklower */ 87341591Ssklower if (*fcp == 0 || *fcp == 0xff) 87441591Ssklower break; 87541591Ssklower switch (*fcp) { 87641591Ssklower case FACILITIES_WINDOWSIZE: 87741591Ssklower sa -> x25_opts.op_wsize = fcp[1]; 87841591Ssklower fcp += 3; 87941591Ssklower break; 88041591Ssklower 88141591Ssklower case FACILITIES_PACKETSIZE: 88241591Ssklower sa -> x25_opts.op_psize = fcp[1]; 88341591Ssklower fcp += 3; 88441591Ssklower break; 88541591Ssklower 88641591Ssklower case FACILITIES_THROUGHPUT: 88741591Ssklower sa -> x25_opts.op_speed = fcp[1]; 88841591Ssklower fcp += 2; 88941591Ssklower break; 89041591Ssklower 89141591Ssklower case FACILITIES_REVERSE_CHARGE: 89241591Ssklower if (fcp[1] & 01) 89341591Ssklower sa -> x25_opts.op_flags |= X25_REVERSE_CHARGE; 89441591Ssklower /* 89541591Ssklower * Datapac specific: for a X.25(1976) DTE, bit 2 89641591Ssklower * indicates a "hi priority" (eg. international) call. 89741591Ssklower */ 89841591Ssklower if (fcp[1] & 02 && sa -> x25_opts.op_psize == 0) 89941591Ssklower sa -> x25_opts.op_psize = X25_PS128; 90041591Ssklower fcp += 2; 90141591Ssklower break; 90241591Ssklower 90341591Ssklower default: 90441591Ssklower /*printf("unknown facility %x, class=%d\n", *fcp, (*fcp & 0xc0) >> 6);*/ 90541591Ssklower switch ((*fcp & 0xc0) >> 6) { 90641591Ssklower case 0: /* class A */ 90741591Ssklower fcp += 2; 90841591Ssklower break; 90941591Ssklower 91041591Ssklower case 1: 91141591Ssklower fcp += 3; 91241591Ssklower break; 91341591Ssklower 91441591Ssklower case 2: 91541591Ssklower fcp += 4; 91641591Ssklower break; 91741591Ssklower 91841591Ssklower case 3: 91941591Ssklower fcp++; 92041591Ssklower fcp += *fcp; 92141591Ssklower } 92241591Ssklower } 92341591Ssklower } 92441591Ssklower } 925