xref: /csrg-svn/sys/netccitt/pk_input.c (revision 57955)
141709Ssklower /*
241709Ssklower  * Copyright (c) University of British Columbia, 1984
357024Ssklower  * Copyright (C) Computer Science Department IV,
457024Ssklower  * 		 University of Erlangen-Nuremberg, Germany, 1992
557024Ssklower  * Copyright (c) 1991, 1992  The Regents of the University of California.
641709Ssklower  * All rights reserved.
741709Ssklower  *
857024Ssklower  * This code is derived from software contributed to Berkeley by the
957024Ssklower  * Laboratory for Computation Vision and the Computer Science Department
1057024Ssklower  * of the the University of British Columbia and the Computer Science
1157024Ssklower  * Department (IV) of the University of Erlangen-Nuremberg, Germany.
1241709Ssklower  *
1341709Ssklower  * %sccs.include.redist.c%
1441709Ssklower  *
15*57955Ssklower  *	@(#)pk_input.c	7.19 (Berkeley) 02/12/93
1641709Ssklower  */
1741591Ssklower 
1856530Sbostic #include <sys/param.h>
1956530Sbostic #include <sys/systm.h>
2056530Sbostic #include <sys/mbuf.h>
2156530Sbostic #include <sys/socket.h>
2256530Sbostic #include <sys/protosw.h>
2356530Sbostic #include <sys/socketvar.h>
2456530Sbostic #include <sys/errno.h>
2541591Ssklower 
2656530Sbostic #include <net/if.h>
2757032Ssklower #include <net/if_dl.h>
2857032Ssklower #include <net/if_llc.h>
2957032Ssklower #include <net/route.h>
3041591Ssklower 
3157024Ssklower #include <netccitt/dll.h>
3256530Sbostic #include <netccitt/x25.h>
3356530Sbostic #include <netccitt/pk.h>
3456530Sbostic #include <netccitt/pk_var.h>
3557024Ssklower #include <netccitt/llc_var.h>
3641591Ssklower 
3757024Ssklower struct pkcb_q pkcb_q = {&pkcb_q, &pkcb_q};
3857024Ssklower 
3957024Ssklower /*
4057024Ssklower  * ccittintr() is the generic interrupt handler for HDLC, LLC2, and X.25. This
4157024Ssklower  * allows to have kernel running X.25 but no HDLC or LLC2 or both (in case we
4257024Ssklower  * employ boards that do all the stuff themselves, e.g. ADAX X.25 or TPS ISDN.)
4357024Ssklower  */
4457024Ssklower void
4557024Ssklower ccittintr()
4657024Ssklower {
4757024Ssklower 	extern struct ifqueue pkintrq;
4857024Ssklower 	extern struct ifqueue hdintrq;
4957024Ssklower 	extern struct ifqueue llcintrq;
5057024Ssklower 
5157024Ssklower #ifdef HDLC
5257024Ssklower 	if (hdintrq.ifq_len)
5357024Ssklower 		hdintr ();
5457024Ssklower #endif
5557024Ssklower #ifdef LLC
5657024Ssklower 	if (llcintrq.ifq_len)
5757024Ssklower 		llcintr ();
5857024Ssklower #endif
5957024Ssklower 	if (pkintrq.ifq_len)
6057024Ssklower 		pkintr ();
6157024Ssklower }
6257024Ssklower 
6349930Ssklower struct pkcb *
6449930Ssklower pk_newlink (ia, llnext)
6549930Ssklower struct x25_ifaddr *ia;
6649930Ssklower caddr_t llnext;
6749930Ssklower {
6849930Ssklower 	register struct x25config *xcp = &ia->ia_xc;
6949930Ssklower 	register struct pkcb *pkp;
7049930Ssklower 	register struct pklcd *lcp;
7149930Ssklower 	register struct protosw *pp;
7249930Ssklower 	unsigned size;
7349930Ssklower 
7449930Ssklower 	pp = pffindproto (AF_CCITT, (int)xcp -> xc_lproto, 0);
7549930Ssklower 	if (pp == 0 || pp -> pr_output == 0) {
7649930Ssklower 		pk_message (0, xcp, "link level protosw error");
7749930Ssklower 		return ((struct pkcb *)0);
7849930Ssklower 	}
7949930Ssklower 	/*
8049930Ssklower 	 * Allocate a network control block structure
8149930Ssklower 	 */
8249930Ssklower 	size = sizeof (struct pkcb);
8349930Ssklower 	pkp = (struct pkcb *)malloc(size, M_PCB, M_WAITOK);
8449930Ssklower 	if (pkp == 0)
8549930Ssklower 		return ((struct pkcb *)0);
8649930Ssklower 	bzero ((caddr_t)pkp, size);
8749930Ssklower 	pkp -> pk_lloutput = pp -> pr_output;
8857024Ssklower 	pkp -> pk_llctlinput = (caddr_t (*)())pp -> pr_ctlinput;
8949930Ssklower 	pkp -> pk_xcp = xcp;
9049930Ssklower 	pkp -> pk_ia = ia;
9149930Ssklower 	pkp -> pk_state = DTE_WAITING;
9249930Ssklower 	pkp -> pk_llnext = llnext;
9357024Ssklower 	insque(pkp, &pkcb_q);
9449930Ssklower 
9549930Ssklower 	/*
9649930Ssklower 	 * set defaults
9749930Ssklower 	 */
9849930Ssklower 
9949930Ssklower 	if (xcp -> xc_pwsize == 0)
10049930Ssklower 		xcp -> xc_pwsize = DEFAULT_WINDOW_SIZE;
10149930Ssklower 	if (xcp -> xc_psize == 0)
10249930Ssklower 		xcp -> xc_psize = X25_PS128;
10349930Ssklower 	/*
10449930Ssklower 	 * Allocate logical channel descriptor vector
10549930Ssklower 	 */
10649930Ssklower 
10749930Ssklower 	(void)pk_resize(pkp);
10849930Ssklower 	return (pkp);
10949930Ssklower }
11049930Ssklower 
11157024Ssklower 
11257024Ssklower pk_dellink (pkp)
11357024Ssklower register struct pkcb *pkp;
11457024Ssklower {
11557024Ssklower 	register int i;
11657024Ssklower 	register struct protosw *pp;
11757024Ssklower 
11857024Ssklower 	/*
11957024Ssklower 	 * Essentially we have the choice to
12057024Ssklower 	 * (a) go ahead and let the route be deleted and
12157024Ssklower 	 *     leave the pkcb associated with that route
12257024Ssklower 	 *     as it is, i.e. the connections stay open
12357024Ssklower 	 * (b) do a pk_disconnect() on all channels associated
12457024Ssklower 	 *     with the route via the pkcb and then proceed.
12557024Ssklower 	 *
12657024Ssklower 	 * For the time being we stick with (b)
12757024Ssklower 	 */
12857024Ssklower 
12957024Ssklower 	for(i = 1; i < pkp->pk_maxlcn; ++i)
13057024Ssklower 		if (pkp->pk_chan[i])
13157024Ssklower 			pk_disconnect(pkp->pk_chan[i]);
13257024Ssklower 
13357024Ssklower 	/*
13457024Ssklower 	 * Free the pkcb
13557024Ssklower 	 */
13657024Ssklower 
13757024Ssklower 	/*
13857024Ssklower 	 * First find the protoswitch to get hold of the link level
13957024Ssklower 	 * protocol to be notified that the packet level entity is
14057024Ssklower 	 * dissolving ...
14157024Ssklower 	 */
14257024Ssklower 	pp = pffindproto (AF_CCITT, (int)pkp ->pk_xcp -> xc_lproto, 0);
14357024Ssklower 	if (pp == 0 || pp -> pr_output == 0) {
14457024Ssklower 		pk_message (0, pkp -> pk_xcp, "link level protosw error");
14557024Ssklower 		return(EPROTONOSUPPORT);
14657024Ssklower 	}
14757024Ssklower 
14857024Ssklower 	pkp -> pk_refcount--;
14957024Ssklower 	if (!pkp -> pk_refcount) {
15057024Ssklower 		struct dll_ctlinfo ctlinfo;
15157024Ssklower 
15257024Ssklower 		remque(pkp);
15357024Ssklower 		if (pkp -> pk_rt -> rt_llinfo == (caddr_t) pkp)
15457024Ssklower 			pkp -> pk_rt -> rt_llinfo = (caddr_t) NULL;
15557024Ssklower 
15657024Ssklower 		/*
15757024Ssklower 		 * Tell the link level that the pkcb is dissolving
15857024Ssklower 		 */
15957024Ssklower 		if (pp -> pr_ctlinput && pkp -> pk_llnext) {
16057024Ssklower 			ctlinfo.dlcti_pcb = pkp -> pk_llnext;
16157024Ssklower 			ctlinfo.dlcti_rt = pkp -> pk_rt;
16257024Ssklower 			(pp -> pr_ctlinput)(PRC_DISCONNECT_REQUEST,
16357024Ssklower 					    pkp -> pk_xcp, &ctlinfo);
16457024Ssklower 		}
16557024Ssklower 		free((caddr_t) pkp -> pk_chan, M_IFADDR);
16657024Ssklower 		free((caddr_t) pkp, M_PCB);
16757024Ssklower 	}
16857024Ssklower 
16957024Ssklower 	return (0);
17057024Ssklower }
17157024Ssklower 
17257024Ssklower 
17349930Ssklower pk_resize (pkp)
17449930Ssklower register struct pkcb *pkp;
17549930Ssklower {
17649930Ssklower 	struct pklcd *dev_lcp = 0;
17749930Ssklower 	struct x25config *xcp = pkp -> pk_xcp;
17849930Ssklower 	if (pkp -> pk_chan &&
17949930Ssklower 	    (pkp -> pk_maxlcn != xcp -> xc_maxlcn)) {
18049930Ssklower 		pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
18149930Ssklower 		dev_lcp = pkp -> pk_chan[0];
18249930Ssklower 		free ((caddr_t)pkp -> pk_chan, M_IFADDR);
18349930Ssklower 		pkp -> pk_chan = 0;
18449930Ssklower 	}
18549930Ssklower 	if (pkp -> pk_chan == 0) {
18649930Ssklower 		unsigned size;
18749930Ssklower 		pkp -> pk_maxlcn = xcp -> xc_maxlcn;
18849930Ssklower 		size = (pkp -> pk_maxlcn + 1) * sizeof (struct pklcd *);
18949930Ssklower 		pkp -> pk_chan =
19049930Ssklower 			(struct pklcd **) malloc (size, M_IFADDR, M_WAITOK);
19149930Ssklower 		if (pkp -> pk_chan) {
19249930Ssklower 			bzero ((caddr_t)pkp -> pk_chan, size);
19349930Ssklower 			/*
19449930Ssklower 			 * Allocate a logical channel descriptor for lcn 0
19549930Ssklower 			 */
19649930Ssklower 			if (dev_lcp == 0 &&
19749930Ssklower 			    (dev_lcp = pk_attach ((struct socket *)0)) == 0)
19849930Ssklower 				return (ENOBUFS);
19949930Ssklower 			dev_lcp -> lcd_state = READY;
20049930Ssklower 			dev_lcp -> lcd_pkp = pkp;
20149930Ssklower 			pkp -> pk_chan[0] = dev_lcp;
20249930Ssklower 		} else {
20349930Ssklower 			if (dev_lcp)
20449930Ssklower 				pk_close (dev_lcp);
20549930Ssklower 			return (ENOBUFS);
20649930Ssklower 		}
20749930Ssklower 	}
20849930Ssklower 	return 0;
20949930Ssklower }
21049930Ssklower 
21141591Ssklower /*
21241591Ssklower  *  This procedure is called by the link level whenever the link
21341591Ssklower  *  becomes operational, is reset, or when the link goes down.
21441591Ssklower  */
21557024Ssklower /*VARARGS*/
21657024Ssklower caddr_t
21757024Ssklower pk_ctlinput (code, src, addr)
21857024Ssklower 	struct sockaddr *src;
21957024Ssklower 	caddr_t addr;
22041591Ssklower {
22157024Ssklower 	register struct pkcb *pkp = (struct pkcb *)addr;
22241591Ssklower 
22341591Ssklower 	switch (code) {
22441591Ssklower 	case PRC_LINKUP:
22541591Ssklower 		if (pkp -> pk_state == DTE_WAITING)
22641591Ssklower 			pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
22741591Ssklower 		break;
22841591Ssklower 
22941591Ssklower 	case PRC_LINKDOWN:
23041591Ssklower 		pk_restart (pkp, -1);	/* Clear all active circuits */
23141591Ssklower 		pkp -> pk_state = DTE_WAITING;
23241591Ssklower 		break;
23341591Ssklower 
23441591Ssklower 	case PRC_LINKRESET:
23541591Ssklower 		pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
23641591Ssklower 		break;
23757024Ssklower 
23857024Ssklower 	case PRC_CONNECT_INDICATION: {
23957024Ssklower 		struct rtentry *llrt;
24041591Ssklower 
24157024Ssklower 		if ((llrt = rtalloc1(src, 0)) == 0)
24257024Ssklower 			return 0;
24357024Ssklower 		else llrt->rt_refcnt--;
24457024Ssklower 
24557024Ssklower 		pkp = (((struct npaidbentry *)llrt->rt_llinfo)->np_rt) ?
24657024Ssklower 			(struct pkcb *)(((struct npaidbentry *)llrt->rt_llinfo)->np_rt->rt_llinfo) : (struct pkcb *) 0;
24757024Ssklower 		if (pkp == (struct pkcb *) 0)
24857024Ssklower 			return 0;
24957024Ssklower 		pkp->pk_llnext = addr;
25057024Ssklower 
25157024Ssklower 		return ((caddr_t) pkp);
25241591Ssklower 	}
25357024Ssklower 	case PRC_DISCONNECT_INDICATION:
25457024Ssklower 		pk_restart (pkp, -1) ;  /* Clear all active circuits */
25557024Ssklower 		pkp->pk_state = DTE_WAITING;
25657024Ssklower 		pkp->pk_llnext = (caddr_t) 0;
25757024Ssklower 	}
25841591Ssklower 	return (0);
25941591Ssklower }
26045297Ssklower struct ifqueue pkintrq;
26145297Ssklower /*
26245297Ssklower  * This routine is called if there are semi-smart devices that do HDLC
26345297Ssklower  * in hardware and want to queue the packet and call level 3 directly
26445297Ssklower  */
26545297Ssklower pkintr ()
26645297Ssklower {
26745297Ssklower 	register struct mbuf *m;
26845297Ssklower 	register struct ifaddr *ifa;
26945297Ssklower 	register struct ifnet *ifp;
27045297Ssklower 	register int s;
27141591Ssklower 
27245297Ssklower 	for (;;) {
27345297Ssklower 		s = splimp ();
27445297Ssklower 		IF_DEQUEUE (&pkintrq, m);
27545297Ssklower 		splx (s);
27645297Ssklower 		if (m == 0)
27745297Ssklower 			break;
27845297Ssklower 		if (m->m_len < PKHEADERLN) {
27945297Ssklower 			printf ("pkintr: packet too short (len=%d)\n",
28045297Ssklower 				m->m_len);
28145297Ssklower 			m_freem (m);
28245297Ssklower 			continue;
28345297Ssklower 		}
28449930Ssklower 		pk_input(m);
28545297Ssklower 	}
28645297Ssklower }
28745297Ssklower struct mbuf *pk_bad_packet;
28849593Ssklower struct mbuf_cache pk_input_cache = {0 };
28941591Ssklower /*
29041591Ssklower  *  X.25 PACKET INPUT
29141591Ssklower  *
29241591Ssklower  *  This procedure is called by a link level procedure whenever
29341591Ssklower  *  an information frame is received. It decodes the packet and
29441591Ssklower  *  demultiplexes based on the logical channel number.
29541591Ssklower  *
29649930Ssklower  *  We change the original conventions of the UBC code here --
297*57955Ssklower  *  since there may be multiple pkcb's for a given interface
298*57955Ssklower  *  of type 802.2 class 2, we retrieve which one it is from
299*57955Ssklower  *  m_pkthdr.rcvif (which has been overwritten by lower layers);
300*57955Ssklower  *  That field is then restored for the benefit of upper layers which
301*57955Ssklower  *  may make use of it, such as CLNP.
30241591Ssklower  */
30341591Ssklower 
30457024Ssklower #define RESTART_DTE_ORIGINATED(xp) (((xp) -> packet_cause == X25_RESTART_DTE_ORIGINATED) || \
30557024Ssklower 			    ((xp) -> packet_cause >= X25_RESTART_DTE_ORIGINATED2))
30657024Ssklower 
30749930Ssklower pk_input (m)
30841591Ssklower register struct mbuf *m;
30941591Ssklower {
31041591Ssklower 	register struct x25_packet *xp;
31141591Ssklower 	register struct pklcd *lcp;
31241591Ssklower 	register struct socket *so = 0;
31341591Ssklower 	register struct pkcb *pkp;
31441591Ssklower 	int  ptype, lcn, lcdstate = LISTEN;
31541591Ssklower 
31649593Ssklower 	if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize)
31749593Ssklower 		mbuf_cache(&pk_input_cache, m);
31849930Ssklower 	if ((m->m_flags & M_PKTHDR) == 0)
31949930Ssklower 		panic("pkintr");
32057024Ssklower 
32149930Ssklower 	if ((pkp = (struct pkcb *)m->m_pkthdr.rcvif) == 0)
32249930Ssklower 		return;
32341591Ssklower 	xp = mtod (m, struct x25_packet *);
32441591Ssklower 	ptype = pk_decode (xp);
32545573Ssklower 	lcn = LCN(xp);
32641591Ssklower 	lcp = pkp -> pk_chan[lcn];
32741591Ssklower 
32841591Ssklower 	/*
32941591Ssklower 	 *  If the DTE is in Restart  state, then it will ignore data,
33041591Ssklower 	 *  interrupt, call setup and clearing, flow control and reset
33141591Ssklower 	 *  packets.
33241591Ssklower 	 */
33341591Ssklower 	if (lcn < 0 || lcn > pkp -> pk_maxlcn) {
33441591Ssklower 		pk_message (lcn, pkp -> pk_xcp, "illegal lcn");
33541591Ssklower 		m_freem (m);
33641591Ssklower 		return;
33741591Ssklower 	}
33841591Ssklower 
33945895Ssklower 	pk_trace (pkp -> pk_xcp, m, "P-In");
34041591Ssklower 
34141591Ssklower 	if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) {
34241591Ssklower 		m_freem (m);
34341591Ssklower 		return;
34441591Ssklower 	}
34541591Ssklower 	if (lcp) {
34641591Ssklower 		so = lcp -> lcd_so;
34741591Ssklower 		lcdstate = lcp -> lcd_state;
34841591Ssklower 	} else {
34941591Ssklower 		if (ptype == CLEAR) {	/* idle line probe (Datapac specific) */
35041591Ssklower 			/* send response on lcd 0's output queue */
35149930Ssklower 			lcp = pkp -> pk_chan[0];
35241591Ssklower 			lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM);
35341591Ssklower 			pk_output (lcp);
35441591Ssklower 			m_freem (m);
35541591Ssklower 			return;
35641591Ssklower 		}
35741591Ssklower 		if (ptype != CALL)
35841591Ssklower 			ptype = INVALID_PACKET;
35941591Ssklower 	}
36041591Ssklower 
36141591Ssklower 	if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) {
36245297Ssklower 		pk_message (0, pkp -> pk_xcp, "illegal ptype (%d, %s) on lcn 0",
36345297Ssklower 			ptype, pk_name[ptype / MAXSTATES]);
36445297Ssklower 		if (pk_bad_packet)
36545297Ssklower 			m_freem (pk_bad_packet);
36645297Ssklower 		pk_bad_packet = m;
36741591Ssklower 		return;
36841591Ssklower 	}
36941591Ssklower 
370*57955Ssklower 	m -> m_pkthdr.rcvif = pkp -> pk_ia -> ia_ifp;
371*57955Ssklower 
37241591Ssklower 	switch (ptype + lcdstate) {
37341591Ssklower 	/*
37441591Ssklower 	 *  Incoming Call packet received.
37541591Ssklower 	 */
37641591Ssklower 	case CALL + LISTEN:
37749593Ssklower 		pk_incoming_call (pkp, m);
37841591Ssklower 		break;
37941591Ssklower 
38041591Ssklower 	/*
38141591Ssklower 	 *  Call collision: Just throw this "incoming call" away since
38241591Ssklower 	 *  the DCE will ignore it anyway.
38341591Ssklower 	 */
38441591Ssklower 	case CALL + SENT_CALL:
38545573Ssklower 		pk_message ((int)lcn, pkp -> pk_xcp,
38641591Ssklower 			"incoming call collision");
38741591Ssklower 		break;
38841591Ssklower 
38941591Ssklower 	/*
39041591Ssklower 	 *  Call confirmation packet received. This usually means our
39141591Ssklower 	 *  previous connect request is now complete.
39241591Ssklower 	 */
39341591Ssklower 	case CALL_ACCEPTED + SENT_CALL:
39449252Ssklower 		MCHTYPE(m, MT_CONTROL);
39549593Ssklower 		pk_call_accepted (lcp, m);
39641591Ssklower 		break;
39741591Ssklower 
39841591Ssklower 	/*
39941591Ssklower 	 *  This condition can only happen if the previous state was
40041591Ssklower 	 *  SENT_CALL. Just ignore the packet, eventually a clear
40141591Ssklower 	 *  confirmation should arrive.
40241591Ssklower 	 */
40341591Ssklower 	case CALL_ACCEPTED + SENT_CLEAR:
40441591Ssklower 		break;
40541591Ssklower 
40641591Ssklower 	/*
40741591Ssklower 	 *  Clear packet received. This requires a complete tear down
40841591Ssklower 	 *  of the virtual circuit.  Free buffers and control blocks.
40941591Ssklower 	 *  and send a clear confirmation.
41041591Ssklower 	 */
41141591Ssklower 	case CLEAR + READY:
41241591Ssklower 	case CLEAR + RECEIVED_CALL:
41341591Ssklower 	case CLEAR + SENT_CALL:
41441591Ssklower 	case CLEAR + DATA_TRANSFER:
41541591Ssklower 		lcp -> lcd_state = RECEIVED_CLEAR;
41641591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM);
41741591Ssklower 		pk_output (lcp);
41841591Ssklower 		pk_clearcause (pkp, xp);
41949252Ssklower 		if (lcp -> lcd_upper) {
42049252Ssklower 			MCHTYPE(m, MT_CONTROL);
42149252Ssklower 			lcp -> lcd_upper (lcp, m);
42249252Ssklower 		}
42341591Ssklower 		pk_close (lcp);
42449252Ssklower 		lcp = 0;
42541591Ssklower 		break;
42641591Ssklower 
42741591Ssklower 	/*
42841591Ssklower 	 *  Clear collision: Treat this clear packet as a confirmation.
42941591Ssklower 	 */
43041591Ssklower 	case CLEAR + SENT_CLEAR:
43141591Ssklower 		pk_close (lcp);
43241591Ssklower 		break;
43341591Ssklower 
43441591Ssklower 	/*
43541591Ssklower 	 *  Clear confirmation received. This usually means the virtual
43641591Ssklower 	 *  circuit is now completely removed.
43741591Ssklower 	 */
43841591Ssklower 	case CLEAR_CONF + SENT_CLEAR:
43941591Ssklower 		pk_close (lcp);
44041591Ssklower 		break;
44141591Ssklower 
44241591Ssklower 	/*
44341591Ssklower 	 *  A clear confirmation on an unassigned logical channel - just
44441591Ssklower 	 *  ignore it. Note: All other packets on an unassigned channel
44541591Ssklower 	 *  results in a clear.
44641591Ssklower 	 */
44741591Ssklower 	case CLEAR_CONF + READY:
44849930Ssklower 	case CLEAR_CONF + LISTEN:
44941591Ssklower 		break;
45041591Ssklower 
45141591Ssklower 	/*
45241591Ssklower 	 *  Data packet received. Pass on to next level. Move the Q and M
45341591Ssklower 	 *  bits into the data portion for the next level.
45441591Ssklower 	 */
45541591Ssklower 	case DATA + DATA_TRANSFER:
45641591Ssklower 		if (lcp -> lcd_reset_condition) {
45741591Ssklower 			ptype = DELETE_PACKET;
45841591Ssklower 			break;
45941591Ssklower 		}
46041591Ssklower 
46141591Ssklower 		/*
46241591Ssklower 		 *  Process the P(S) flow control information in this Data packet.
46341591Ssklower 		 *  Check that the packets arrive in the correct sequence and that
46441591Ssklower 		 *  they are within the "lcd_input_window". Input window rotation is
46541591Ssklower 		 *  initiated by the receive interface.
46641591Ssklower 		 */
46741591Ssklower 
46841591Ssklower 		if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) ||
46941591Ssklower 			PS(xp) == ((lcp -> lcd_input_window + lcp->lcd_windowsize) % MODULUS)) {
47041591Ssklower 			m_freem (m);
47145895Ssklower 			pk_procerror (RESET, lcp, "p(s) flow control error", 1);
47241591Ssklower 			break;
47341591Ssklower 		}
47441591Ssklower 		lcp -> lcd_rsn = PS(xp);
47541591Ssklower 
47641591Ssklower 		if (pk_ack (lcp, PR(xp)) != PACKET_OK) {
47741591Ssklower 			m_freem (m);
47841591Ssklower 			break;
47941591Ssklower 		}
48045895Ssklower 		m -> m_data += PKHEADERLN;
48145895Ssklower 		m -> m_len -= PKHEADERLN;
48245895Ssklower 		m -> m_pkthdr.len -= PKHEADERLN;
48345895Ssklower 
48449930Ssklower 		lcp -> lcd_rxcnt++;
48545895Ssklower 		if (lcp -> lcd_flags & X25_MBS_HOLD) {
48645895Ssklower 			register struct mbuf *n = lcp -> lcd_cps;
48745895Ssklower 			int mbit = MBIT(xp);
48845895Ssklower 			octet q_and_d_bits;
48945895Ssklower 
49045895Ssklower 			if (n) {
49145895Ssklower 				n -> m_pkthdr.len += m -> m_pkthdr.len;
49245895Ssklower 				while (n -> m_next)
49345895Ssklower 					n = n -> m_next;
49445895Ssklower 				n -> m_next = m;
49545895Ssklower 				m = lcp -> lcd_cps;
49645895Ssklower 
49745895Ssklower 				if (lcp -> lcd_cpsmax &&
49845895Ssklower 				    n -> m_pkthdr.len > lcp -> lcd_cpsmax) {
49945895Ssklower 					pk_procerror (RESET, lcp,
50045895Ssklower 						"C.P.S. overflow", 128);
50145895Ssklower 					return;
50245895Ssklower 				}
50345895Ssklower 				q_and_d_bits = 0xc0 & *(octet *)xp;
50445895Ssklower 				xp = (struct x25_packet *)
50545895Ssklower 					(mtod(m, octet *) - PKHEADERLN);
50645895Ssklower 				*(octet *)xp |= q_and_d_bits;
50745895Ssklower 			}
50845895Ssklower 			if (mbit) {
50945895Ssklower 				lcp -> lcd_cps = m;
51047268Ssklower 				pk_flowcontrol(lcp, 0, 1);
51145895Ssklower 				return;
51245895Ssklower 			}
51345895Ssklower 			lcp -> lcd_cps = 0;
51445895Ssklower 		}
51545297Ssklower 		if (so == 0)
51645297Ssklower 			break;
51741591Ssklower 		if (lcp -> lcd_flags & X25_MQBIT) {
51857024Ssklower 			octet t = (X25GBITS(xp -> bits, q_bit)) ? t = 0x80 : 0;
51941591Ssklower 
52045573Ssklower 			if (MBIT(xp))
52145573Ssklower 				t |= 0x40;
52243361Ssklower 			m -> m_data -= 1;
52341591Ssklower 			m -> m_len += 1;
52445895Ssklower 			m -> m_pkthdr.len += 1;
52545573Ssklower 			*mtod(m, octet *) = t;
52641591Ssklower 		}
52741591Ssklower 
52841591Ssklower 		/*
52941591Ssklower 		 * Discard Q-BIT packets if the application
53041591Ssklower 		 * doesn't want to be informed of M and Q bit status
53141591Ssklower 		 */
53257024Ssklower 		if (X25GBITS(xp -> bits, q_bit)
53357024Ssklower 		    && (lcp -> lcd_flags & X25_MQBIT) == 0) {
53441591Ssklower 			m_freem (m);
53541591Ssklower 			/*
53641591Ssklower 			 * NB.  This is dangerous: sending a RR here can
53741591Ssklower 			 * cause sequence number errors if a previous data
53841591Ssklower 			 * packet has not yet been passed up to the application
53941591Ssklower 			 * (RR's are normally generated via PRU_RCVD).
54041591Ssklower 			 */
54147268Ssklower 			pk_flowcontrol(lcp, 0, 1);
54241591Ssklower 		} else {
54341591Ssklower 			sbappendrecord (&so -> so_rcv, m);
54441591Ssklower 			sorwakeup (so);
54541591Ssklower 		}
54641591Ssklower 		break;
54741591Ssklower 
54841591Ssklower 	/*
54941591Ssklower 	 *  Interrupt packet received.
55041591Ssklower 	 */
55141591Ssklower 	case INTERRUPT + DATA_TRANSFER:
55241591Ssklower 		if (lcp -> lcd_reset_condition)
55341591Ssklower 			break;
55441591Ssklower 		lcp -> lcd_intrdata = xp -> packet_data;
55541591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM);
55641591Ssklower 		pk_output (lcp);
55745895Ssklower 		m -> m_data += PKHEADERLN;
55845895Ssklower 		m -> m_len -= PKHEADERLN;
55945895Ssklower 		m -> m_pkthdr.len -= PKHEADERLN;
56045297Ssklower 		MCHTYPE(m, MT_OOBDATA);
56145895Ssklower 		if (so) {
56245895Ssklower 			if (so -> so_options & SO_OOBINLINE)
56345895Ssklower 				sbinsertoob (&so -> so_rcv, m);
56445895Ssklower 			else
56545895Ssklower 				m_freem (m);
56645297Ssklower 			sohasoutofband (so);
56745895Ssklower 		}
56841591Ssklower 		break;
56941591Ssklower 
57041591Ssklower 	/*
57141591Ssklower 	 *  Interrupt confirmation packet received.
57241591Ssklower 	 */
57341591Ssklower 	case INTERRUPT_CONF + DATA_TRANSFER:
57441591Ssklower 		if (lcp -> lcd_reset_condition)
57541591Ssklower 			break;
57641591Ssklower 		if (lcp -> lcd_intrconf_pending == TRUE)
57741591Ssklower 			lcp -> lcd_intrconf_pending = FALSE;
57841591Ssklower 		else
57945895Ssklower 			pk_procerror (RESET, lcp, "unexpected packet", 43);
58041591Ssklower 		break;
58141591Ssklower 
58241591Ssklower 	/*
58341591Ssklower 	 *  Receiver ready received. Rotate the output window and output
58441591Ssklower 	 *  any data packets waiting transmission.
58541591Ssklower 	 */
58641591Ssklower 	case RR + DATA_TRANSFER:
58745297Ssklower 		if (lcp -> lcd_reset_condition ||
58845297Ssklower 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
58945297Ssklower 			ptype = DELETE_PACKET;
59041591Ssklower 			break;
59145297Ssklower 		}
59241591Ssklower 		if (lcp -> lcd_rnr_condition == TRUE)
59341591Ssklower 			lcp -> lcd_rnr_condition = FALSE;
59441591Ssklower 		pk_output (lcp);
59541591Ssklower 		break;
59641591Ssklower 
59741591Ssklower 	/*
59841591Ssklower 	 *  Receiver Not Ready received. Packets up to the P(R) can be
59941591Ssklower 	 *  be sent. Condition is cleared with a RR.
60041591Ssklower 	 */
60141591Ssklower 	case RNR + DATA_TRANSFER:
60245297Ssklower 		if (lcp -> lcd_reset_condition ||
60345297Ssklower 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
60445297Ssklower 			ptype = DELETE_PACKET;
60541591Ssklower 			break;
60645297Ssklower 		}
60741591Ssklower 		lcp -> lcd_rnr_condition = TRUE;
60841591Ssklower 		break;
60941591Ssklower 
61041591Ssklower 	/*
61141591Ssklower 	 *  Reset packet received. Set state to FLOW_OPEN.  The Input and
61241591Ssklower 	 *  Output window edges ar set to zero. Both the send and receive
61341591Ssklower 	 *  numbers are reset. A confirmation is returned.
61441591Ssklower 	 */
61541591Ssklower 	case RESET + DATA_TRANSFER:
61641591Ssklower 		if (lcp -> lcd_reset_condition)
61741591Ssklower 			/* Reset collision. Just ignore packet. */
61841591Ssklower 			break;
61941591Ssklower 
62041591Ssklower 		pk_resetcause (pkp, xp);
62141591Ssklower 		lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
62241591Ssklower 			lcp -> lcd_intrconf_pending = FALSE;
62341591Ssklower 		lcp -> lcd_output_window = lcp -> lcd_input_window =
62441591Ssklower 			lcp -> lcd_last_transmitted_pr = 0;
62541591Ssklower 		lcp -> lcd_ssn = 0;
62641591Ssklower 		lcp -> lcd_rsn = MODULUS - 1;
62741591Ssklower 
62841591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM);
62941591Ssklower 		pk_output (lcp);
63045297Ssklower 
63145895Ssklower 		pk_flush(lcp);
63245297Ssklower 		if (so == 0)
63345297Ssklower 			break;
63445297Ssklower 		wakeup ((caddr_t) & so -> so_timeo);
63545297Ssklower 		sorwakeup (so);
63645297Ssklower 		sowwakeup (so);
63741591Ssklower 		break;
63841591Ssklower 
63941591Ssklower 	/*
64041591Ssklower 	 *  Reset confirmation received.
64141591Ssklower 	 */
64241591Ssklower 	case RESET_CONF + DATA_TRANSFER:
64341591Ssklower 		if (lcp -> lcd_reset_condition) {
64441591Ssklower 			lcp -> lcd_reset_condition = FALSE;
64541591Ssklower 			pk_output (lcp);
64641591Ssklower 		}
64741591Ssklower 		else
64845895Ssklower 			pk_procerror (RESET, lcp, "unexpected packet", 32);
64941591Ssklower 		break;
65041591Ssklower 
65141591Ssklower 	case DATA + SENT_CLEAR:
65241591Ssklower 		ptype = DELETE_PACKET;
65341591Ssklower 	case RR + SENT_CLEAR:
65441591Ssklower 	case RNR + SENT_CLEAR:
65541591Ssklower 	case INTERRUPT + SENT_CLEAR:
65641591Ssklower 	case INTERRUPT_CONF + SENT_CLEAR:
65741591Ssklower 	case RESET + SENT_CLEAR:
65841591Ssklower 	case RESET_CONF + SENT_CLEAR:
65945297Ssklower 		/* Just ignore p if we have sent a CLEAR already.
66041591Ssklower 		   */
66141591Ssklower 		break;
66241591Ssklower 
66341591Ssklower 	/*
66441591Ssklower 	 *  Restart sets all the permanent virtual circuits to the "Data
66541591Ssklower 	 *  Transfer" stae and  all the switched virtual circuits to the
66641591Ssklower 	 *  "Ready" state.
66741591Ssklower 	 */
66841591Ssklower 	case RESTART + READY:
66941591Ssklower 		switch (pkp -> pk_state) {
67041591Ssklower 		case DTE_SENT_RESTART:
67157024Ssklower 			/*
67257024Ssklower 			 * Restart collision.
67357024Ssklower 			 * If case the restart cause is "DTE originated" we
67457024Ssklower 			 * have a DTE-DTE situation and are trying to resolve
67557024Ssklower 			 * who is going to play DTE/DCE [ISO 8208:4.2-4.5]
67657024Ssklower 			 */
67757024Ssklower 			if (RESTART_DTE_ORIGINATED(xp)) {
67857024Ssklower 				pk_restart (pkp, X25_RESTART_DTE_ORIGINATED);
67957024Ssklower 				pk_message (0, pkp -> pk_xcp,
68057024Ssklower 					    "RESTART collision");
68157024Ssklower 				if ((pkp -> pk_restartcolls++) > MAXRESTARTCOLLISIONS) {
68257024Ssklower 					pk_message (0, pkp -> pk_xcp,
68357024Ssklower 						    "excessive RESTART collisions");
68457024Ssklower 					pkp -> pk_restartcolls = 0;
68557024Ssklower 				}
68657024Ssklower 				break;
68757024Ssklower 			}
68841591Ssklower 			pkp -> pk_state = DTE_READY;
68957024Ssklower 			pkp -> pk_dxerole |= DTE_PLAYDTE;
69057024Ssklower 			pkp -> pk_dxerole &= ~DTE_PLAYDCE;
69141591Ssklower 			pk_message (0, pkp -> pk_xcp,
69241591Ssklower 				"Packet level operational");
69357024Ssklower 			pk_message (0, pkp -> pk_xcp,
69457024Ssklower 				    "Assuming DTE role");
69557024Ssklower 			if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
69657024Ssklower 				pk_callcomplete(pkp);
69741591Ssklower 			break;
69841591Ssklower 
69941591Ssklower 		default:
70041591Ssklower 			pk_restart (pkp, -1);
70141591Ssklower 			pk_restartcause (pkp, xp);
70241591Ssklower 			pkp -> pk_chan[0] -> lcd_template = pk_template (0,
70341591Ssklower 				X25_RESTART_CONFIRM);
70441591Ssklower 			pk_output (pkp -> pk_chan[0]);
70557024Ssklower 			pkp -> pk_state = DTE_READY;
70657024Ssklower 			pkp -> pk_dxerole |= RESTART_DTE_ORIGINATED(xp) ? DTE_PLAYDCE :
70757024Ssklower 				DTE_PLAYDTE;
70857024Ssklower 			if (pkp -> pk_dxerole & DTE_PLAYDTE) {
70957024Ssklower 				pkp -> pk_dxerole &= ~DTE_PLAYDCE;
71057024Ssklower 				pk_message (0, pkp -> pk_xcp,
71157024Ssklower 					    "Assuming DTE role");
71257024Ssklower 			} else {
71357024Ssklower 				pkp -> pk_dxerole &= ~DTE_PLAYDTE;
71457024Ssklower 				pk_message (0, pkp -> pk_xcp,
71557024Ssklower 					 "Assuming DCE role");
71657024Ssklower 			}
71757024Ssklower 			if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
71857024Ssklower 				pk_callcomplete(pkp);
71941591Ssklower 		}
72041591Ssklower 		break;
72141591Ssklower 
72241591Ssklower 	/*
72341591Ssklower 	 *  Restart confirmation received. All logical channels are set
72441591Ssklower 	 *  to READY.
72541591Ssklower 	 */
72641591Ssklower 	case RESTART_CONF + READY:
72741591Ssklower 		switch (pkp -> pk_state) {
72841591Ssklower 		case DTE_SENT_RESTART:
72941591Ssklower 			pkp -> pk_state = DTE_READY;
73057024Ssklower 			pkp -> pk_dxerole |= DTE_PLAYDTE;
73157024Ssklower 			pkp -> pk_dxerole &= ~DTE_PLAYDCE;
73241591Ssklower 			pk_message (0, pkp -> pk_xcp,
73357024Ssklower 				    "Packet level operational");
73457024Ssklower 			pk_message (0, pkp-> pk_xcp,
73557024Ssklower 				    "Assuming DTE role");
73657024Ssklower 			if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
73757024Ssklower 				pk_callcomplete(pkp);
73841591Ssklower 			break;
73941591Ssklower 
74041591Ssklower 		default:
74141591Ssklower 			/* Restart local procedure error. */
74241591Ssklower 			pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
74341591Ssklower 			pkp -> pk_state = DTE_SENT_RESTART;
74457024Ssklower 			pkp -> pk_dxerole &= ~(DTE_PLAYDTE | DTE_PLAYDCE);
74541591Ssklower 		}
74641591Ssklower 		break;
74741591Ssklower 
74841591Ssklower 	default:
74941591Ssklower 		if (lcp) {
75045895Ssklower 			pk_procerror (CLEAR, lcp, "unknown packet error", 33);
75141591Ssklower 			pk_message (lcn, pkp -> pk_xcp,
75241591Ssklower 				"\"%s\" unexpected in \"%s\" state",
75341591Ssklower 				pk_name[ptype/MAXSTATES], pk_state[lcdstate]);
75445895Ssklower 		} else
75545573Ssklower 			pk_message (lcn, pkp -> pk_xcp,
75641591Ssklower 				"packet arrived on unassigned lcn");
75741591Ssklower 		break;
75841591Ssklower 	}
75949252Ssklower 	if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) {
76045895Ssklower 		if (ptype != DATA && ptype != INTERRUPT)
76145895Ssklower 			MCHTYPE(m, MT_CONTROL);
76245297Ssklower 		lcp -> lcd_upper (lcp, m);
76345895Ssklower 	} else if (ptype != DATA && ptype != INTERRUPT)
76441591Ssklower 		m_freem (m);
76541591Ssklower }
76641591Ssklower 
76749930Ssklower static
76849930Ssklower prune_dnic(from, to, dnicname, xcp)
76949930Ssklower char *from, *to, *dnicname;
77049930Ssklower register struct x25config *xcp;
77149930Ssklower {
77249930Ssklower 	register char *cp1 = from, *cp2 = from;
77349930Ssklower 	if (xcp->xc_prepnd0 && *cp1 == '0') {
77449930Ssklower 		from = ++cp1;
77549930Ssklower 		goto copyrest;
77649930Ssklower 	}
77749930Ssklower 	if (xcp->xc_nodnic) {
77849930Ssklower 		for (cp1 = dnicname; *cp2 = *cp1++;)
77949930Ssklower 			cp2++;
78049930Ssklower 		cp1 = from;
78149930Ssklower 	}
78249930Ssklower copyrest:
78349930Ssklower 	for (cp1 = dnicname; *cp2 = *cp1++;)
78449930Ssklower 		cp2++;
78549930Ssklower }
78649930Ssklower /* static */
78749930Ssklower pk_simple_bsd (from, to, lower, len)
78849930Ssklower register octet *from, *to;
78949930Ssklower register len, lower;
79049930Ssklower {
79149930Ssklower 	register int c;
79249930Ssklower 	while (--len >= 0) {
79349930Ssklower 		c = *from;
79449930Ssklower 		if (lower & 0x01)
79549930Ssklower 			*from++;
79649930Ssklower 		else
79749930Ssklower 			c >>= 4;
79849930Ssklower 		c &= 0x0f; c |= 0x30; *to++ = c; lower++;
79949930Ssklower 	}
80049930Ssklower 	*to = 0;
80149930Ssklower }
80241591Ssklower 
80349930Ssklower /*static octet * */
80449930Ssklower pk_from_bcd (a, iscalling, sa, xcp)
80549930Ssklower register struct x25_calladdr *a;
80649930Ssklower register struct sockaddr_x25 *sa;
80749930Ssklower register struct x25config *xcp;
80849930Ssklower {
80949930Ssklower 	octet buf[MAXADDRLN+1];
81049930Ssklower 	octet *cp;
81149930Ssklower 	unsigned count;
81249930Ssklower 
81349930Ssklower 	bzero ((caddr_t)sa, sizeof (*sa));
81449930Ssklower 	sa -> x25_len = sizeof (*sa);
81549930Ssklower 	sa -> x25_family = AF_CCITT;
81649930Ssklower 	if (iscalling) {
81757024Ssklower 		cp = a -> address_field + (X25GBITS(a -> addrlens, called_addrlen) / 2);
81857024Ssklower 		count = X25GBITS(a -> addrlens, calling_addrlen);
81957024Ssklower 		pk_simple_bsd (cp, buf, X25GBITS(a -> addrlens, called_addrlen), count);
82049930Ssklower 	} else {
82157024Ssklower 		count = X25GBITS(a -> addrlens, called_addrlen);
82249930Ssklower 		pk_simple_bsd (a -> address_field, buf, 0, count);
82349930Ssklower 	}
82449930Ssklower 	if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp ->xc_prepnd0)) {
82549930Ssklower 		octet dnicname[sizeof(long) * NBBY/3 + 2];
82649930Ssklower 
82757024Ssklower 		sprintf ((char *) dnicname, "%d", xcp -> xc_addr.x25_net);
82857024Ssklower 		prune_dnic ((char *)buf, sa -> x25_addr, dnicname, xcp);
82949930Ssklower 	} else
83049930Ssklower 		bcopy ((caddr_t)buf, (caddr_t)sa -> x25_addr, count + 1);
83149930Ssklower }
83249930Ssklower 
83349930Ssklower static
83449930Ssklower save_extra(m0, fp, so)
83549930Ssklower struct mbuf *m0;
83649930Ssklower octet *fp;
83749930Ssklower struct socket *so;
83849930Ssklower {
83949930Ssklower 	register struct mbuf *m;
84049930Ssklower 	struct cmsghdr cmsghdr;
84152449Ssklower 	if (m = m_copy (m, 0, (int)M_COPYALL)) {
84249930Ssklower 		int off = fp - mtod (m0, octet *);
84349930Ssklower 		int len = m->m_pkthdr.len - off + sizeof (cmsghdr);
84449930Ssklower 		cmsghdr.cmsg_len = len;
84549930Ssklower 		cmsghdr.cmsg_level = AF_CCITT;
84649930Ssklower 		cmsghdr.cmsg_type = PK_FACILITIES;
84749930Ssklower 		m_adj (m, off);
84849930Ssklower 		M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT);
84949930Ssklower 		if (m == 0)
85049930Ssklower 			return;
85149930Ssklower 		bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr));
85249930Ssklower 		MCHTYPE(m, MT_CONTROL);
85349930Ssklower 		sbappendrecord(&so -> so_rcv, m);
85449930Ssklower 	}
85549930Ssklower }
85649930Ssklower 
85741591Ssklower /*
85841591Ssklower  * This routine handles incoming call packets. It matches the protocol
85941591Ssklower  * field on the Call User Data field (usually the first four bytes) with
86041591Ssklower  * sockets awaiting connections.
86141591Ssklower  */
86241591Ssklower 
86349593Ssklower pk_incoming_call (pkp, m0)
86445895Ssklower struct mbuf *m0;
86541591Ssklower struct pkcb *pkp;
86641591Ssklower {
86742277Ssklower 	register struct pklcd *lcp = 0, *l;
86841591Ssklower 	register struct sockaddr_x25 *sa;
86941591Ssklower 	register struct x25_calladdr *a;
87042277Ssklower 	register struct socket *so = 0;
87149930Ssklower 	struct	x25_packet *xp = mtod(m0, struct x25_packet *);
87249930Ssklower 	struct	mbuf *m;
87349930Ssklower 	struct	x25config *xcp = pkp -> pk_xcp;
87445895Ssklower 	int len = m0->m_pkthdr.len;
87549930Ssklower 	unsigned udlen;
87649930Ssklower 	char *errstr = "server unavailable";
87745895Ssklower 	octet *u, *facp;
87845573Ssklower 	int lcn = LCN(xp);
87941591Ssklower 
88049930Ssklower 	/* First, copy the data from the incoming call packet to a X25 address
88149930Ssklower 	   descriptor. It is to be regretted that you have
88249930Ssklower 	   to parse the facilities into a sockaddr to determine
88349930Ssklower 	   if reverse charging is being requested */
88449930Ssklower 	if ((m = m_get (M_DONTWAIT, MT_SONAME)) == 0)
88541591Ssklower 		return;
88641591Ssklower 	sa = mtod (m, struct sockaddr_x25 *);
88749930Ssklower 	a = (struct x25_calladdr *) &xp -> packet_data;
88849930Ssklower 	facp = u = (octet *) (a -> address_field +
88957024Ssklower 		((X25GBITS(a -> addrlens, called_addrlen) + X25GBITS(a -> addrlens, calling_addrlen) + 1) / 2));
89049930Ssklower 	u += *u + 1;
89149930Ssklower 	udlen = min (16, ((octet *)xp) + len - u);
89249930Ssklower 	if (udlen < 0)
89349930Ssklower 		udlen = 0;
89449930Ssklower 	pk_from_bcd (a, 1, sa, pkp -> pk_xcp); /* get calling address */
89550426Ssklower 	pk_parse_facilities (facp, sa);
89649930Ssklower 	bcopy ((caddr_t)u, sa -> x25_udata, udlen);
89749930Ssklower 	sa -> x25_udlen = udlen;
89841591Ssklower 
89941591Ssklower 	/*
90050426Ssklower 	 * Now, loop through the listen sockets looking for a match on the
90150426Ssklower 	 * PID. That is the first few octets of the user data field.
90250426Ssklower 	 * This is the closest thing to a port number for X.25 packets.
90350426Ssklower 	 * It does provide a way of multiplexing services at the user level.
90441591Ssklower 	 */
90541591Ssklower 
90641591Ssklower 	for (l = pk_listenhead; l; l = l -> lcd_listen) {
90741591Ssklower 		struct sockaddr_x25 *sxp = l -> lcd_ceaddr;
90841591Ssklower 
90949930Ssklower 		if (bcmp (sxp -> x25_udata, u, sxp->x25_udlen))
91041591Ssklower 			continue;
91145165Ssklower 		if (sxp -> x25_net &&
91249930Ssklower 		    sxp -> x25_net != xcp -> xc_addr.x25_net)
91341591Ssklower 			continue;
91441591Ssklower 		/*
91549930Ssklower 		 * don't accept incoming calls with the D-Bit on
91649930Ssklower 		 * unless the server agrees
91749930Ssklower 		 */
91857024Ssklower 		if (X25GBITS(xp -> bits, d_bit) && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
91949930Ssklower 			errstr = "incoming D-Bit mismatch";
92049930Ssklower 			break;
92149930Ssklower 		}
92249930Ssklower 		/*
92341591Ssklower 		 * don't accept incoming collect calls unless
92441591Ssklower 		 * the server sets the reverse charging option.
92541591Ssklower 		 */
92641591Ssklower 		if ((sxp -> x25_opts.op_flags & (X25_OLDSOCKADDR|X25_REVERSE_CHARGE)) == 0 &&
92741591Ssklower 			sa -> x25_opts.op_flags & X25_REVERSE_CHARGE) {
92841591Ssklower 			errstr = "incoming collect call refused";
92941591Ssklower 			break;
93041591Ssklower 		}
93142277Ssklower 		if (l -> lcd_so) {
93245165Ssklower 			if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
93342277Ssklower 				    lcp = (struct pklcd *) so -> so_pcb;
93442277Ssklower 		} else
93542277Ssklower 			lcp = pk_attach((struct socket *) 0);
93642277Ssklower 		if (lcp == 0) {
93741591Ssklower 			/*
93841591Ssklower 			 * Insufficient space or too many unaccepted
93941591Ssklower 			 * connections.  Just throw the call away.
94041591Ssklower 			 */
94141591Ssklower 			errstr = "server malfunction";
94241591Ssklower 			break;
94341591Ssklower 		}
94443361Ssklower 		lcp -> lcd_upper = l -> lcd_upper;
94543361Ssklower 		lcp -> lcd_upnext = l -> lcd_upnext;
94641591Ssklower 		lcp -> lcd_lcn = lcn;
94741591Ssklower 		lcp -> lcd_state = RECEIVED_CALL;
94850020Ssklower 		sa -> x25_opts.op_flags |= (sxp -> x25_opts.op_flags &
94950020Ssklower 			~X25_REVERSE_CHARGE) | l -> lcd_flags;
95041591Ssklower 		pk_assoc (pkp, lcp, sa);
95149930Ssklower 		lcp -> lcd_faddr = *sa;
95249930Ssklower 		lcp -> lcd_laddr.x25_udlen = sxp -> x25_udlen;
95349930Ssklower 		lcp -> lcd_craddr = &lcp->lcd_faddr;
95441591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
95545573Ssklower 		if (lcp -> lcd_flags & X25_DBIT) {
95657024Ssklower 			if (X25GBITS(xp -> bits, d_bit))
95757024Ssklower 				X25SBITS(mtod(lcp -> lcd_template,
95857024Ssklower 					struct x25_packet *) -> bits, d_bit, 1);
95945573Ssklower 			else
96045573Ssklower 				lcp -> lcd_flags &= ~X25_DBIT;
96145573Ssklower 		}
96243361Ssklower 		if (so) {
96343361Ssklower 			pk_output (lcp);
96442277Ssklower 			soisconnected (so);
96545895Ssklower 			if (so -> so_options & SO_OOBINLINE)
96645895Ssklower 				save_extra(m0, facp, so);
96745895Ssklower 		} else if (lcp -> lcd_upper) {
96849930Ssklower 			(*lcp -> lcd_upper) (lcp, m0);
96945895Ssklower 		}
97049930Ssklower 		(void) m_free (m);
97141591Ssklower 		return;
97241591Ssklower 	}
97341591Ssklower 
97441591Ssklower 	/*
97541591Ssklower 	 * If the call fails for whatever reason, we still need to build a
97641591Ssklower 	 * skeleton LCD in order to be able to properly  receive the CLEAR
97741591Ssklower 	 * CONFIRMATION.
97841591Ssklower 	 */
97941591Ssklower #ifdef WATERLOO		/* be explicit */
98041591Ssklower 	if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0)
98141591Ssklower 		pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s",
98241591Ssklower 			sa->x25_addr, sa->x25_udata[3] & 0xff, errstr);
98341591Ssklower 	else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0)
98441591Ssklower 		pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s",
98541591Ssklower 			sa->x25_addr, errstr);
98641591Ssklower 	else
98741591Ssklower #endif
98841591Ssklower 	pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s",
98941591Ssklower 		sa -> x25_addr, sa -> x25_udata[0] & 0xff,
99041591Ssklower 		sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff,
99141591Ssklower 		sa -> x25_udata[3] & 0xff, errstr);
99245297Ssklower 	if ((lcp = pk_attach((struct socket *)0)) == 0) {
99345297Ssklower 		(void) m_free (m);
99441591Ssklower 		return;
99541591Ssklower 	}
99641591Ssklower 	lcp -> lcd_lcn = lcn;
99741591Ssklower 	lcp -> lcd_state = RECEIVED_CALL;
99841591Ssklower 	pk_assoc (pkp, lcp, sa);
99945297Ssklower 	(void) m_free (m);
100045895Ssklower 	pk_clear (lcp, 0, 1);
100141591Ssklower }
100241591Ssklower 
100349593Ssklower pk_call_accepted (lcp, m)
100441591Ssklower struct pklcd *lcp;
100549252Ssklower struct mbuf *m;
100641591Ssklower {
100741591Ssklower 	register struct x25_calladdr *ap;
100841591Ssklower 	register octet *fcp;
100949252Ssklower 	struct x25_packet *xp = mtod (m, struct x25_packet *);
101049252Ssklower 	int len = m -> m_len;
101141591Ssklower 
101241591Ssklower 	lcp -> lcd_state = DATA_TRANSFER;
101345297Ssklower 	if (lcp -> lcd_so)
101445297Ssklower 		soisconnected (lcp -> lcd_so);
101557024Ssklower 	if ((lcp -> lcd_flags & X25_DBIT) && (X25GBITS(xp -> bits, d_bit) == 0))
101645573Ssklower 		lcp -> lcd_flags &= ~X25_DBIT;
101741591Ssklower 	if (len > 3) {
101841591Ssklower 		ap = (struct x25_calladdr *) &xp -> packet_data;
101957024Ssklower 		fcp = (octet *) ap -> address_field + (X25GBITS(ap -> addrlens, calling_addrlen) +
102057024Ssklower 			X25GBITS(ap -> addrlens, called_addrlen) + 1) / 2;
102141591Ssklower 		if (fcp + *fcp <= ((octet *)xp) + len)
102249593Ssklower 			pk_parse_facilities (fcp, lcp -> lcd_ceaddr);
102341591Ssklower 	}
102441591Ssklower 	pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
102549252Ssklower 	if (lcp -> lcd_so == 0 && lcp -> lcd_upper)
102649252Ssklower 		lcp -> lcd_upper(lcp, m);
102741591Ssklower }
102841591Ssklower 
102949593Ssklower pk_parse_facilities (fcp, sa)
103041591Ssklower register octet *fcp;
103141591Ssklower register struct sockaddr_x25 *sa;
103241591Ssklower {
103341591Ssklower 	register octet *maxfcp;
103441591Ssklower 
103541591Ssklower 	maxfcp = fcp + *fcp;
103641591Ssklower 	fcp++;
103741591Ssklower 	while (fcp < maxfcp) {
103841591Ssklower 		/*
103941591Ssklower 		 * Ignore national DCE or DTE facilities
104041591Ssklower 		 */
104141591Ssklower 		if (*fcp == 0 || *fcp == 0xff)
104241591Ssklower 			break;
104341591Ssklower 		switch (*fcp) {
104441591Ssklower 		case FACILITIES_WINDOWSIZE:
104541591Ssklower 			sa -> x25_opts.op_wsize = fcp[1];
104641591Ssklower 			fcp += 3;
104741591Ssklower 			break;
104841591Ssklower 
104941591Ssklower 		case FACILITIES_PACKETSIZE:
105041591Ssklower 			sa -> x25_opts.op_psize = fcp[1];
105141591Ssklower 			fcp += 3;
105241591Ssklower 			break;
105341591Ssklower 
105441591Ssklower 		case FACILITIES_THROUGHPUT:
105541591Ssklower 			sa -> x25_opts.op_speed = fcp[1];
105641591Ssklower 			fcp += 2;
105741591Ssklower 			break;
105841591Ssklower 
105941591Ssklower 		case FACILITIES_REVERSE_CHARGE:
106041591Ssklower 			if (fcp[1] & 01)
106141591Ssklower 				sa -> x25_opts.op_flags |= X25_REVERSE_CHARGE;
106241591Ssklower 			/*
106341591Ssklower 			 * Datapac specific: for a X.25(1976) DTE, bit 2
106441591Ssklower 			 * indicates a "hi priority" (eg. international) call.
106541591Ssklower 			 */
106641591Ssklower 			if (fcp[1] & 02 && sa -> x25_opts.op_psize == 0)
106741591Ssklower 				sa -> x25_opts.op_psize = X25_PS128;
106841591Ssklower 			fcp += 2;
106941591Ssklower 			break;
107041591Ssklower 
107141591Ssklower 		default:
107241591Ssklower /*printf("unknown facility %x, class=%d\n", *fcp, (*fcp & 0xc0) >> 6);*/
107341591Ssklower 			switch ((*fcp & 0xc0) >> 6) {
107441591Ssklower 			case 0:			/* class A */
107541591Ssklower 				fcp += 2;
107641591Ssklower 				break;
107741591Ssklower 
107841591Ssklower 			case 1:
107941591Ssklower 				fcp += 3;
108041591Ssklower 				break;
108141591Ssklower 
108241591Ssklower 			case 2:
108341591Ssklower 				fcp += 4;
108441591Ssklower 				break;
108541591Ssklower 
108641591Ssklower 			case 3:
108741591Ssklower 				fcp++;
108841591Ssklower 				fcp += *fcp;
108941591Ssklower 			}
109041591Ssklower 		}
109141591Ssklower 	}
109241591Ssklower }
1093