xref: /csrg-svn/sys/netccitt/pk_input.c (revision 57032)
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*57032Ssklower  *	@(#)pk_input.c	7.18 (Berkeley) 12/08/92
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>
27*57032Ssklower #include <net/if_dl.h>
28*57032Ssklower #include <net/if_llc.h>
29*57032Ssklower #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 --
29749930Ssklower  *  since there may be multiple pkcb's for 802.2 class 2
29849930Ssklower  *  for a given interface, we must be informed which one it is;
29949930Ssklower  *  so we overwrite the pkthdr.rcvif; it can be recovered if necessary.
30049930Ssklower  *
30141591Ssklower  */
30241591Ssklower 
30357024Ssklower #define RESTART_DTE_ORIGINATED(xp) (((xp) -> packet_cause == X25_RESTART_DTE_ORIGINATED) || \
30457024Ssklower 			    ((xp) -> packet_cause >= X25_RESTART_DTE_ORIGINATED2))
30557024Ssklower 
30649930Ssklower pk_input (m)
30741591Ssklower register struct mbuf *m;
30841591Ssklower {
30941591Ssklower 	register struct x25_packet *xp;
31041591Ssklower 	register struct pklcd *lcp;
31141591Ssklower 	register struct socket *so = 0;
31241591Ssklower 	register struct pkcb *pkp;
31341591Ssklower 	int  ptype, lcn, lcdstate = LISTEN;
31441591Ssklower 
31549593Ssklower 	if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize)
31649593Ssklower 		mbuf_cache(&pk_input_cache, m);
31749930Ssklower 	if ((m->m_flags & M_PKTHDR) == 0)
31849930Ssklower 		panic("pkintr");
31957024Ssklower 
32049930Ssklower 	if ((pkp = (struct pkcb *)m->m_pkthdr.rcvif) == 0)
32149930Ssklower 		return;
32241591Ssklower 	xp = mtod (m, struct x25_packet *);
32341591Ssklower 	ptype = pk_decode (xp);
32445573Ssklower 	lcn = LCN(xp);
32541591Ssklower 	lcp = pkp -> pk_chan[lcn];
32641591Ssklower 
32741591Ssklower 	/*
32841591Ssklower 	 *  If the DTE is in Restart  state, then it will ignore data,
32941591Ssklower 	 *  interrupt, call setup and clearing, flow control and reset
33041591Ssklower 	 *  packets.
33141591Ssklower 	 */
33241591Ssklower 	if (lcn < 0 || lcn > pkp -> pk_maxlcn) {
33341591Ssklower 		pk_message (lcn, pkp -> pk_xcp, "illegal lcn");
33441591Ssklower 		m_freem (m);
33541591Ssklower 		return;
33641591Ssklower 	}
33741591Ssklower 
33845895Ssklower 	pk_trace (pkp -> pk_xcp, m, "P-In");
33941591Ssklower 
34041591Ssklower 	if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) {
34141591Ssklower 		m_freem (m);
34241591Ssklower 		return;
34341591Ssklower 	}
34441591Ssklower 	if (lcp) {
34541591Ssklower 		so = lcp -> lcd_so;
34641591Ssklower 		lcdstate = lcp -> lcd_state;
34741591Ssklower 	} else {
34841591Ssklower 		if (ptype == CLEAR) {	/* idle line probe (Datapac specific) */
34941591Ssklower 			/* send response on lcd 0's output queue */
35049930Ssklower 			lcp = pkp -> pk_chan[0];
35141591Ssklower 			lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM);
35241591Ssklower 			pk_output (lcp);
35341591Ssklower 			m_freem (m);
35441591Ssklower 			return;
35541591Ssklower 		}
35641591Ssklower 		if (ptype != CALL)
35741591Ssklower 			ptype = INVALID_PACKET;
35841591Ssklower 	}
35941591Ssklower 
36041591Ssklower 	if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) {
36145297Ssklower 		pk_message (0, pkp -> pk_xcp, "illegal ptype (%d, %s) on lcn 0",
36245297Ssklower 			ptype, pk_name[ptype / MAXSTATES]);
36345297Ssklower 		if (pk_bad_packet)
36445297Ssklower 			m_freem (pk_bad_packet);
36545297Ssklower 		pk_bad_packet = m;
36641591Ssklower 		return;
36741591Ssklower 	}
36841591Ssklower 
36941591Ssklower 	switch (ptype + lcdstate) {
37041591Ssklower 	/*
37141591Ssklower 	 *  Incoming Call packet received.
37241591Ssklower 	 */
37341591Ssklower 	case CALL + LISTEN:
37449593Ssklower 		pk_incoming_call (pkp, m);
37541591Ssklower 		break;
37641591Ssklower 
37741591Ssklower 	/*
37841591Ssklower 	 *  Call collision: Just throw this "incoming call" away since
37941591Ssklower 	 *  the DCE will ignore it anyway.
38041591Ssklower 	 */
38141591Ssklower 	case CALL + SENT_CALL:
38245573Ssklower 		pk_message ((int)lcn, pkp -> pk_xcp,
38341591Ssklower 			"incoming call collision");
38441591Ssklower 		break;
38541591Ssklower 
38641591Ssklower 	/*
38741591Ssklower 	 *  Call confirmation packet received. This usually means our
38841591Ssklower 	 *  previous connect request is now complete.
38941591Ssklower 	 */
39041591Ssklower 	case CALL_ACCEPTED + SENT_CALL:
39149252Ssklower 		MCHTYPE(m, MT_CONTROL);
39249593Ssklower 		pk_call_accepted (lcp, m);
39341591Ssklower 		break;
39441591Ssklower 
39541591Ssklower 	/*
39641591Ssklower 	 *  This condition can only happen if the previous state was
39741591Ssklower 	 *  SENT_CALL. Just ignore the packet, eventually a clear
39841591Ssklower 	 *  confirmation should arrive.
39941591Ssklower 	 */
40041591Ssklower 	case CALL_ACCEPTED + SENT_CLEAR:
40141591Ssklower 		break;
40241591Ssklower 
40341591Ssklower 	/*
40441591Ssklower 	 *  Clear packet received. This requires a complete tear down
40541591Ssklower 	 *  of the virtual circuit.  Free buffers and control blocks.
40641591Ssklower 	 *  and send a clear confirmation.
40741591Ssklower 	 */
40841591Ssklower 	case CLEAR + READY:
40941591Ssklower 	case CLEAR + RECEIVED_CALL:
41041591Ssklower 	case CLEAR + SENT_CALL:
41141591Ssklower 	case CLEAR + DATA_TRANSFER:
41241591Ssklower 		lcp -> lcd_state = RECEIVED_CLEAR;
41341591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM);
41441591Ssklower 		pk_output (lcp);
41541591Ssklower 		pk_clearcause (pkp, xp);
41649252Ssklower 		if (lcp -> lcd_upper) {
41749252Ssklower 			MCHTYPE(m, MT_CONTROL);
41849252Ssklower 			lcp -> lcd_upper (lcp, m);
41949252Ssklower 		}
42041591Ssklower 		pk_close (lcp);
42149252Ssklower 		lcp = 0;
42241591Ssklower 		break;
42341591Ssklower 
42441591Ssklower 	/*
42541591Ssklower 	 *  Clear collision: Treat this clear packet as a confirmation.
42641591Ssklower 	 */
42741591Ssklower 	case CLEAR + SENT_CLEAR:
42841591Ssklower 		pk_close (lcp);
42941591Ssklower 		break;
43041591Ssklower 
43141591Ssklower 	/*
43241591Ssklower 	 *  Clear confirmation received. This usually means the virtual
43341591Ssklower 	 *  circuit is now completely removed.
43441591Ssklower 	 */
43541591Ssklower 	case CLEAR_CONF + SENT_CLEAR:
43641591Ssklower 		pk_close (lcp);
43741591Ssklower 		break;
43841591Ssklower 
43941591Ssklower 	/*
44041591Ssklower 	 *  A clear confirmation on an unassigned logical channel - just
44141591Ssklower 	 *  ignore it. Note: All other packets on an unassigned channel
44241591Ssklower 	 *  results in a clear.
44341591Ssklower 	 */
44441591Ssklower 	case CLEAR_CONF + READY:
44549930Ssklower 	case CLEAR_CONF + LISTEN:
44641591Ssklower 		break;
44741591Ssklower 
44841591Ssklower 	/*
44941591Ssklower 	 *  Data packet received. Pass on to next level. Move the Q and M
45041591Ssklower 	 *  bits into the data portion for the next level.
45141591Ssklower 	 */
45241591Ssklower 	case DATA + DATA_TRANSFER:
45341591Ssklower 		if (lcp -> lcd_reset_condition) {
45441591Ssklower 			ptype = DELETE_PACKET;
45541591Ssklower 			break;
45641591Ssklower 		}
45741591Ssklower 
45841591Ssklower 		/*
45941591Ssklower 		 *  Process the P(S) flow control information in this Data packet.
46041591Ssklower 		 *  Check that the packets arrive in the correct sequence and that
46141591Ssklower 		 *  they are within the "lcd_input_window". Input window rotation is
46241591Ssklower 		 *  initiated by the receive interface.
46341591Ssklower 		 */
46441591Ssklower 
46541591Ssklower 		if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) ||
46641591Ssklower 			PS(xp) == ((lcp -> lcd_input_window + lcp->lcd_windowsize) % MODULUS)) {
46741591Ssklower 			m_freem (m);
46845895Ssklower 			pk_procerror (RESET, lcp, "p(s) flow control error", 1);
46941591Ssklower 			break;
47041591Ssklower 		}
47141591Ssklower 		lcp -> lcd_rsn = PS(xp);
47241591Ssklower 
47341591Ssklower 		if (pk_ack (lcp, PR(xp)) != PACKET_OK) {
47441591Ssklower 			m_freem (m);
47541591Ssklower 			break;
47641591Ssklower 		}
47745895Ssklower 		m -> m_data += PKHEADERLN;
47845895Ssklower 		m -> m_len -= PKHEADERLN;
47945895Ssklower 		m -> m_pkthdr.len -= PKHEADERLN;
48045895Ssklower 
48149930Ssklower 		lcp -> lcd_rxcnt++;
48245895Ssklower 		if (lcp -> lcd_flags & X25_MBS_HOLD) {
48345895Ssklower 			register struct mbuf *n = lcp -> lcd_cps;
48445895Ssklower 			int mbit = MBIT(xp);
48545895Ssklower 			octet q_and_d_bits;
48645895Ssklower 
48745895Ssklower 			if (n) {
48845895Ssklower 				n -> m_pkthdr.len += m -> m_pkthdr.len;
48945895Ssklower 				while (n -> m_next)
49045895Ssklower 					n = n -> m_next;
49145895Ssklower 				n -> m_next = m;
49245895Ssklower 				m = lcp -> lcd_cps;
49345895Ssklower 
49445895Ssklower 				if (lcp -> lcd_cpsmax &&
49545895Ssklower 				    n -> m_pkthdr.len > lcp -> lcd_cpsmax) {
49645895Ssklower 					pk_procerror (RESET, lcp,
49745895Ssklower 						"C.P.S. overflow", 128);
49845895Ssklower 					return;
49945895Ssklower 				}
50045895Ssklower 				q_and_d_bits = 0xc0 & *(octet *)xp;
50145895Ssklower 				xp = (struct x25_packet *)
50245895Ssklower 					(mtod(m, octet *) - PKHEADERLN);
50345895Ssklower 				*(octet *)xp |= q_and_d_bits;
50445895Ssklower 			}
50545895Ssklower 			if (mbit) {
50645895Ssklower 				lcp -> lcd_cps = m;
50747268Ssklower 				pk_flowcontrol(lcp, 0, 1);
50845895Ssklower 				return;
50945895Ssklower 			}
51045895Ssklower 			lcp -> lcd_cps = 0;
51145895Ssklower 		}
51245297Ssklower 		if (so == 0)
51345297Ssklower 			break;
51441591Ssklower 		if (lcp -> lcd_flags & X25_MQBIT) {
51557024Ssklower 			octet t = (X25GBITS(xp -> bits, q_bit)) ? t = 0x80 : 0;
51641591Ssklower 
51745573Ssklower 			if (MBIT(xp))
51845573Ssklower 				t |= 0x40;
51943361Ssklower 			m -> m_data -= 1;
52041591Ssklower 			m -> m_len += 1;
52145895Ssklower 			m -> m_pkthdr.len += 1;
52245573Ssklower 			*mtod(m, octet *) = t;
52341591Ssklower 		}
52441591Ssklower 
52541591Ssklower 		/*
52641591Ssklower 		 * Discard Q-BIT packets if the application
52741591Ssklower 		 * doesn't want to be informed of M and Q bit status
52841591Ssklower 		 */
52957024Ssklower 		if (X25GBITS(xp -> bits, q_bit)
53057024Ssklower 		    && (lcp -> lcd_flags & X25_MQBIT) == 0) {
53141591Ssklower 			m_freem (m);
53241591Ssklower 			/*
53341591Ssklower 			 * NB.  This is dangerous: sending a RR here can
53441591Ssklower 			 * cause sequence number errors if a previous data
53541591Ssklower 			 * packet has not yet been passed up to the application
53641591Ssklower 			 * (RR's are normally generated via PRU_RCVD).
53741591Ssklower 			 */
53847268Ssklower 			pk_flowcontrol(lcp, 0, 1);
53941591Ssklower 		} else {
54041591Ssklower 			sbappendrecord (&so -> so_rcv, m);
54141591Ssklower 			sorwakeup (so);
54241591Ssklower 		}
54341591Ssklower 		break;
54441591Ssklower 
54541591Ssklower 	/*
54641591Ssklower 	 *  Interrupt packet received.
54741591Ssklower 	 */
54841591Ssklower 	case INTERRUPT + DATA_TRANSFER:
54941591Ssklower 		if (lcp -> lcd_reset_condition)
55041591Ssklower 			break;
55141591Ssklower 		lcp -> lcd_intrdata = xp -> packet_data;
55241591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM);
55341591Ssklower 		pk_output (lcp);
55445895Ssklower 		m -> m_data += PKHEADERLN;
55545895Ssklower 		m -> m_len -= PKHEADERLN;
55645895Ssklower 		m -> m_pkthdr.len -= PKHEADERLN;
55745297Ssklower 		MCHTYPE(m, MT_OOBDATA);
55845895Ssklower 		if (so) {
55945895Ssklower 			if (so -> so_options & SO_OOBINLINE)
56045895Ssklower 				sbinsertoob (&so -> so_rcv, m);
56145895Ssklower 			else
56245895Ssklower 				m_freem (m);
56345297Ssklower 			sohasoutofband (so);
56445895Ssklower 		}
56541591Ssklower 		break;
56641591Ssklower 
56741591Ssklower 	/*
56841591Ssklower 	 *  Interrupt confirmation packet received.
56941591Ssklower 	 */
57041591Ssklower 	case INTERRUPT_CONF + DATA_TRANSFER:
57141591Ssklower 		if (lcp -> lcd_reset_condition)
57241591Ssklower 			break;
57341591Ssklower 		if (lcp -> lcd_intrconf_pending == TRUE)
57441591Ssklower 			lcp -> lcd_intrconf_pending = FALSE;
57541591Ssklower 		else
57645895Ssklower 			pk_procerror (RESET, lcp, "unexpected packet", 43);
57741591Ssklower 		break;
57841591Ssklower 
57941591Ssklower 	/*
58041591Ssklower 	 *  Receiver ready received. Rotate the output window and output
58141591Ssklower 	 *  any data packets waiting transmission.
58241591Ssklower 	 */
58341591Ssklower 	case RR + DATA_TRANSFER:
58445297Ssklower 		if (lcp -> lcd_reset_condition ||
58545297Ssklower 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
58645297Ssklower 			ptype = DELETE_PACKET;
58741591Ssklower 			break;
58845297Ssklower 		}
58941591Ssklower 		if (lcp -> lcd_rnr_condition == TRUE)
59041591Ssklower 			lcp -> lcd_rnr_condition = FALSE;
59141591Ssklower 		pk_output (lcp);
59241591Ssklower 		break;
59341591Ssklower 
59441591Ssklower 	/*
59541591Ssklower 	 *  Receiver Not Ready received. Packets up to the P(R) can be
59641591Ssklower 	 *  be sent. Condition is cleared with a RR.
59741591Ssklower 	 */
59841591Ssklower 	case RNR + DATA_TRANSFER:
59945297Ssklower 		if (lcp -> lcd_reset_condition ||
60045297Ssklower 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
60145297Ssklower 			ptype = DELETE_PACKET;
60241591Ssklower 			break;
60345297Ssklower 		}
60441591Ssklower 		lcp -> lcd_rnr_condition = TRUE;
60541591Ssklower 		break;
60641591Ssklower 
60741591Ssklower 	/*
60841591Ssklower 	 *  Reset packet received. Set state to FLOW_OPEN.  The Input and
60941591Ssklower 	 *  Output window edges ar set to zero. Both the send and receive
61041591Ssklower 	 *  numbers are reset. A confirmation is returned.
61141591Ssklower 	 */
61241591Ssklower 	case RESET + DATA_TRANSFER:
61341591Ssklower 		if (lcp -> lcd_reset_condition)
61441591Ssklower 			/* Reset collision. Just ignore packet. */
61541591Ssklower 			break;
61641591Ssklower 
61741591Ssklower 		pk_resetcause (pkp, xp);
61841591Ssklower 		lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
61941591Ssklower 			lcp -> lcd_intrconf_pending = FALSE;
62041591Ssklower 		lcp -> lcd_output_window = lcp -> lcd_input_window =
62141591Ssklower 			lcp -> lcd_last_transmitted_pr = 0;
62241591Ssklower 		lcp -> lcd_ssn = 0;
62341591Ssklower 		lcp -> lcd_rsn = MODULUS - 1;
62441591Ssklower 
62541591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM);
62641591Ssklower 		pk_output (lcp);
62745297Ssklower 
62845895Ssklower 		pk_flush(lcp);
62945297Ssklower 		if (so == 0)
63045297Ssklower 			break;
63145297Ssklower 		wakeup ((caddr_t) & so -> so_timeo);
63245297Ssklower 		sorwakeup (so);
63345297Ssklower 		sowwakeup (so);
63441591Ssklower 		break;
63541591Ssklower 
63641591Ssklower 	/*
63741591Ssklower 	 *  Reset confirmation received.
63841591Ssklower 	 */
63941591Ssklower 	case RESET_CONF + DATA_TRANSFER:
64041591Ssklower 		if (lcp -> lcd_reset_condition) {
64141591Ssklower 			lcp -> lcd_reset_condition = FALSE;
64241591Ssklower 			pk_output (lcp);
64341591Ssklower 		}
64441591Ssklower 		else
64545895Ssklower 			pk_procerror (RESET, lcp, "unexpected packet", 32);
64641591Ssklower 		break;
64741591Ssklower 
64841591Ssklower 	case DATA + SENT_CLEAR:
64941591Ssklower 		ptype = DELETE_PACKET;
65041591Ssklower 	case RR + SENT_CLEAR:
65141591Ssklower 	case RNR + SENT_CLEAR:
65241591Ssklower 	case INTERRUPT + SENT_CLEAR:
65341591Ssklower 	case INTERRUPT_CONF + SENT_CLEAR:
65441591Ssklower 	case RESET + SENT_CLEAR:
65541591Ssklower 	case RESET_CONF + SENT_CLEAR:
65645297Ssklower 		/* Just ignore p if we have sent a CLEAR already.
65741591Ssklower 		   */
65841591Ssklower 		break;
65941591Ssklower 
66041591Ssklower 	/*
66141591Ssklower 	 *  Restart sets all the permanent virtual circuits to the "Data
66241591Ssklower 	 *  Transfer" stae and  all the switched virtual circuits to the
66341591Ssklower 	 *  "Ready" state.
66441591Ssklower 	 */
66541591Ssklower 	case RESTART + READY:
66641591Ssklower 		switch (pkp -> pk_state) {
66741591Ssklower 		case DTE_SENT_RESTART:
66857024Ssklower 			/*
66957024Ssklower 			 * Restart collision.
67057024Ssklower 			 * If case the restart cause is "DTE originated" we
67157024Ssklower 			 * have a DTE-DTE situation and are trying to resolve
67257024Ssklower 			 * who is going to play DTE/DCE [ISO 8208:4.2-4.5]
67357024Ssklower 			 */
67457024Ssklower 			if (RESTART_DTE_ORIGINATED(xp)) {
67557024Ssklower 				pk_restart (pkp, X25_RESTART_DTE_ORIGINATED);
67657024Ssklower 				pk_message (0, pkp -> pk_xcp,
67757024Ssklower 					    "RESTART collision");
67857024Ssklower 				if ((pkp -> pk_restartcolls++) > MAXRESTARTCOLLISIONS) {
67957024Ssklower 					pk_message (0, pkp -> pk_xcp,
68057024Ssklower 						    "excessive RESTART collisions");
68157024Ssklower 					pkp -> pk_restartcolls = 0;
68257024Ssklower 				}
68357024Ssklower 				break;
68457024Ssklower 			}
68541591Ssklower 			pkp -> pk_state = DTE_READY;
68657024Ssklower 			pkp -> pk_dxerole |= DTE_PLAYDTE;
68757024Ssklower 			pkp -> pk_dxerole &= ~DTE_PLAYDCE;
68841591Ssklower 			pk_message (0, pkp -> pk_xcp,
68941591Ssklower 				"Packet level operational");
69057024Ssklower 			pk_message (0, pkp -> pk_xcp,
69157024Ssklower 				    "Assuming DTE role");
69257024Ssklower 			if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
69357024Ssklower 				pk_callcomplete(pkp);
69441591Ssklower 			break;
69541591Ssklower 
69641591Ssklower 		default:
69741591Ssklower 			pk_restart (pkp, -1);
69841591Ssklower 			pk_restartcause (pkp, xp);
69941591Ssklower 			pkp -> pk_chan[0] -> lcd_template = pk_template (0,
70041591Ssklower 				X25_RESTART_CONFIRM);
70141591Ssklower 			pk_output (pkp -> pk_chan[0]);
70257024Ssklower 			pkp -> pk_state = DTE_READY;
70357024Ssklower 			pkp -> pk_dxerole |= RESTART_DTE_ORIGINATED(xp) ? DTE_PLAYDCE :
70457024Ssklower 				DTE_PLAYDTE;
70557024Ssklower 			if (pkp -> pk_dxerole & DTE_PLAYDTE) {
70657024Ssklower 				pkp -> pk_dxerole &= ~DTE_PLAYDCE;
70757024Ssklower 				pk_message (0, pkp -> pk_xcp,
70857024Ssklower 					    "Assuming DTE role");
70957024Ssklower 			} else {
71057024Ssklower 				pkp -> pk_dxerole &= ~DTE_PLAYDTE;
71157024Ssklower 				pk_message (0, pkp -> pk_xcp,
71257024Ssklower 					 "Assuming DCE role");
71357024Ssklower 			}
71457024Ssklower 			if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
71557024Ssklower 				pk_callcomplete(pkp);
71641591Ssklower 		}
71741591Ssklower 		break;
71841591Ssklower 
71941591Ssklower 	/*
72041591Ssklower 	 *  Restart confirmation received. All logical channels are set
72141591Ssklower 	 *  to READY.
72241591Ssklower 	 */
72341591Ssklower 	case RESTART_CONF + READY:
72441591Ssklower 		switch (pkp -> pk_state) {
72541591Ssklower 		case DTE_SENT_RESTART:
72641591Ssklower 			pkp -> pk_state = DTE_READY;
72757024Ssklower 			pkp -> pk_dxerole |= DTE_PLAYDTE;
72857024Ssklower 			pkp -> pk_dxerole &= ~DTE_PLAYDCE;
72941591Ssklower 			pk_message (0, pkp -> pk_xcp,
73057024Ssklower 				    "Packet level operational");
73157024Ssklower 			pk_message (0, pkp-> pk_xcp,
73257024Ssklower 				    "Assuming DTE role");
73357024Ssklower 			if (pkp -> pk_dxerole & DTE_CONNECTPENDING)
73457024Ssklower 				pk_callcomplete(pkp);
73541591Ssklower 			break;
73641591Ssklower 
73741591Ssklower 		default:
73841591Ssklower 			/* Restart local procedure error. */
73941591Ssklower 			pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
74041591Ssklower 			pkp -> pk_state = DTE_SENT_RESTART;
74157024Ssklower 			pkp -> pk_dxerole &= ~(DTE_PLAYDTE | DTE_PLAYDCE);
74241591Ssklower 		}
74341591Ssklower 		break;
74441591Ssklower 
74541591Ssklower 	default:
74641591Ssklower 		if (lcp) {
74745895Ssklower 			pk_procerror (CLEAR, lcp, "unknown packet error", 33);
74841591Ssklower 			pk_message (lcn, pkp -> pk_xcp,
74941591Ssklower 				"\"%s\" unexpected in \"%s\" state",
75041591Ssklower 				pk_name[ptype/MAXSTATES], pk_state[lcdstate]);
75145895Ssklower 		} else
75245573Ssklower 			pk_message (lcn, pkp -> pk_xcp,
75341591Ssklower 				"packet arrived on unassigned lcn");
75441591Ssklower 		break;
75541591Ssklower 	}
75649252Ssklower 	if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) {
75745895Ssklower 		if (ptype != DATA && ptype != INTERRUPT)
75845895Ssklower 			MCHTYPE(m, MT_CONTROL);
75945297Ssklower 		lcp -> lcd_upper (lcp, m);
76045895Ssklower 	} else if (ptype != DATA && ptype != INTERRUPT)
76141591Ssklower 		m_freem (m);
76241591Ssklower }
76341591Ssklower 
76449930Ssklower static
76549930Ssklower prune_dnic(from, to, dnicname, xcp)
76649930Ssklower char *from, *to, *dnicname;
76749930Ssklower register struct x25config *xcp;
76849930Ssklower {
76949930Ssklower 	register char *cp1 = from, *cp2 = from;
77049930Ssklower 	if (xcp->xc_prepnd0 && *cp1 == '0') {
77149930Ssklower 		from = ++cp1;
77249930Ssklower 		goto copyrest;
77349930Ssklower 	}
77449930Ssklower 	if (xcp->xc_nodnic) {
77549930Ssklower 		for (cp1 = dnicname; *cp2 = *cp1++;)
77649930Ssklower 			cp2++;
77749930Ssklower 		cp1 = from;
77849930Ssklower 	}
77949930Ssklower copyrest:
78049930Ssklower 	for (cp1 = dnicname; *cp2 = *cp1++;)
78149930Ssklower 		cp2++;
78249930Ssklower }
78349930Ssklower /* static */
78449930Ssklower pk_simple_bsd (from, to, lower, len)
78549930Ssklower register octet *from, *to;
78649930Ssklower register len, lower;
78749930Ssklower {
78849930Ssklower 	register int c;
78949930Ssklower 	while (--len >= 0) {
79049930Ssklower 		c = *from;
79149930Ssklower 		if (lower & 0x01)
79249930Ssklower 			*from++;
79349930Ssklower 		else
79449930Ssklower 			c >>= 4;
79549930Ssklower 		c &= 0x0f; c |= 0x30; *to++ = c; lower++;
79649930Ssklower 	}
79749930Ssklower 	*to = 0;
79849930Ssklower }
79941591Ssklower 
80049930Ssklower /*static octet * */
80149930Ssklower pk_from_bcd (a, iscalling, sa, xcp)
80249930Ssklower register struct x25_calladdr *a;
80349930Ssklower register struct sockaddr_x25 *sa;
80449930Ssklower register struct x25config *xcp;
80549930Ssklower {
80649930Ssklower 	octet buf[MAXADDRLN+1];
80749930Ssklower 	octet *cp;
80849930Ssklower 	unsigned count;
80949930Ssklower 
81049930Ssklower 	bzero ((caddr_t)sa, sizeof (*sa));
81149930Ssklower 	sa -> x25_len = sizeof (*sa);
81249930Ssklower 	sa -> x25_family = AF_CCITT;
81349930Ssklower 	if (iscalling) {
81457024Ssklower 		cp = a -> address_field + (X25GBITS(a -> addrlens, called_addrlen) / 2);
81557024Ssklower 		count = X25GBITS(a -> addrlens, calling_addrlen);
81657024Ssklower 		pk_simple_bsd (cp, buf, X25GBITS(a -> addrlens, called_addrlen), count);
81749930Ssklower 	} else {
81857024Ssklower 		count = X25GBITS(a -> addrlens, called_addrlen);
81949930Ssklower 		pk_simple_bsd (a -> address_field, buf, 0, count);
82049930Ssklower 	}
82149930Ssklower 	if (xcp -> xc_addr.x25_net && (xcp -> xc_nodnic || xcp ->xc_prepnd0)) {
82249930Ssklower 		octet dnicname[sizeof(long) * NBBY/3 + 2];
82349930Ssklower 
82457024Ssklower 		sprintf ((char *) dnicname, "%d", xcp -> xc_addr.x25_net);
82557024Ssklower 		prune_dnic ((char *)buf, sa -> x25_addr, dnicname, xcp);
82649930Ssklower 	} else
82749930Ssklower 		bcopy ((caddr_t)buf, (caddr_t)sa -> x25_addr, count + 1);
82849930Ssklower }
82949930Ssklower 
83049930Ssklower static
83149930Ssklower save_extra(m0, fp, so)
83249930Ssklower struct mbuf *m0;
83349930Ssklower octet *fp;
83449930Ssklower struct socket *so;
83549930Ssklower {
83649930Ssklower 	register struct mbuf *m;
83749930Ssklower 	struct cmsghdr cmsghdr;
83852449Ssklower 	if (m = m_copy (m, 0, (int)M_COPYALL)) {
83949930Ssklower 		int off = fp - mtod (m0, octet *);
84049930Ssklower 		int len = m->m_pkthdr.len - off + sizeof (cmsghdr);
84149930Ssklower 		cmsghdr.cmsg_len = len;
84249930Ssklower 		cmsghdr.cmsg_level = AF_CCITT;
84349930Ssklower 		cmsghdr.cmsg_type = PK_FACILITIES;
84449930Ssklower 		m_adj (m, off);
84549930Ssklower 		M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT);
84649930Ssklower 		if (m == 0)
84749930Ssklower 			return;
84849930Ssklower 		bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr));
84949930Ssklower 		MCHTYPE(m, MT_CONTROL);
85049930Ssklower 		sbappendrecord(&so -> so_rcv, m);
85149930Ssklower 	}
85249930Ssklower }
85349930Ssklower 
85441591Ssklower /*
85541591Ssklower  * This routine handles incoming call packets. It matches the protocol
85641591Ssklower  * field on the Call User Data field (usually the first four bytes) with
85741591Ssklower  * sockets awaiting connections.
85841591Ssklower  */
85941591Ssklower 
86049593Ssklower pk_incoming_call (pkp, m0)
86145895Ssklower struct mbuf *m0;
86241591Ssklower struct pkcb *pkp;
86341591Ssklower {
86442277Ssklower 	register struct pklcd *lcp = 0, *l;
86541591Ssklower 	register struct sockaddr_x25 *sa;
86641591Ssklower 	register struct x25_calladdr *a;
86742277Ssklower 	register struct socket *so = 0;
86849930Ssklower 	struct	x25_packet *xp = mtod(m0, struct x25_packet *);
86949930Ssklower 	struct	mbuf *m;
87049930Ssklower 	struct	x25config *xcp = pkp -> pk_xcp;
87145895Ssklower 	int len = m0->m_pkthdr.len;
87249930Ssklower 	unsigned udlen;
87349930Ssklower 	char *errstr = "server unavailable";
87445895Ssklower 	octet *u, *facp;
87545573Ssklower 	int lcn = LCN(xp);
87641591Ssklower 
87749930Ssklower 	/* First, copy the data from the incoming call packet to a X25 address
87849930Ssklower 	   descriptor. It is to be regretted that you have
87949930Ssklower 	   to parse the facilities into a sockaddr to determine
88049930Ssklower 	   if reverse charging is being requested */
88149930Ssklower 	if ((m = m_get (M_DONTWAIT, MT_SONAME)) == 0)
88241591Ssklower 		return;
88341591Ssklower 	sa = mtod (m, struct sockaddr_x25 *);
88449930Ssklower 	a = (struct x25_calladdr *) &xp -> packet_data;
88549930Ssklower 	facp = u = (octet *) (a -> address_field +
88657024Ssklower 		((X25GBITS(a -> addrlens, called_addrlen) + X25GBITS(a -> addrlens, calling_addrlen) + 1) / 2));
88749930Ssklower 	u += *u + 1;
88849930Ssklower 	udlen = min (16, ((octet *)xp) + len - u);
88949930Ssklower 	if (udlen < 0)
89049930Ssklower 		udlen = 0;
89149930Ssklower 	pk_from_bcd (a, 1, sa, pkp -> pk_xcp); /* get calling address */
89250426Ssklower 	pk_parse_facilities (facp, sa);
89349930Ssklower 	bcopy ((caddr_t)u, sa -> x25_udata, udlen);
89449930Ssklower 	sa -> x25_udlen = udlen;
89541591Ssklower 
89641591Ssklower 	/*
89750426Ssklower 	 * Now, loop through the listen sockets looking for a match on the
89850426Ssklower 	 * PID. That is the first few octets of the user data field.
89950426Ssklower 	 * This is the closest thing to a port number for X.25 packets.
90050426Ssklower 	 * It does provide a way of multiplexing services at the user level.
90141591Ssklower 	 */
90241591Ssklower 
90341591Ssklower 	for (l = pk_listenhead; l; l = l -> lcd_listen) {
90441591Ssklower 		struct sockaddr_x25 *sxp = l -> lcd_ceaddr;
90541591Ssklower 
90649930Ssklower 		if (bcmp (sxp -> x25_udata, u, sxp->x25_udlen))
90741591Ssklower 			continue;
90845165Ssklower 		if (sxp -> x25_net &&
90949930Ssklower 		    sxp -> x25_net != xcp -> xc_addr.x25_net)
91041591Ssklower 			continue;
91141591Ssklower 		/*
91249930Ssklower 		 * don't accept incoming calls with the D-Bit on
91349930Ssklower 		 * unless the server agrees
91449930Ssklower 		 */
91557024Ssklower 		if (X25GBITS(xp -> bits, d_bit) && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
91649930Ssklower 			errstr = "incoming D-Bit mismatch";
91749930Ssklower 			break;
91849930Ssklower 		}
91949930Ssklower 		/*
92041591Ssklower 		 * don't accept incoming collect calls unless
92141591Ssklower 		 * the server sets the reverse charging option.
92241591Ssklower 		 */
92341591Ssklower 		if ((sxp -> x25_opts.op_flags & (X25_OLDSOCKADDR|X25_REVERSE_CHARGE)) == 0 &&
92441591Ssklower 			sa -> x25_opts.op_flags & X25_REVERSE_CHARGE) {
92541591Ssklower 			errstr = "incoming collect call refused";
92641591Ssklower 			break;
92741591Ssklower 		}
92842277Ssklower 		if (l -> lcd_so) {
92945165Ssklower 			if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
93042277Ssklower 				    lcp = (struct pklcd *) so -> so_pcb;
93142277Ssklower 		} else
93242277Ssklower 			lcp = pk_attach((struct socket *) 0);
93342277Ssklower 		if (lcp == 0) {
93441591Ssklower 			/*
93541591Ssklower 			 * Insufficient space or too many unaccepted
93641591Ssklower 			 * connections.  Just throw the call away.
93741591Ssklower 			 */
93841591Ssklower 			errstr = "server malfunction";
93941591Ssklower 			break;
94041591Ssklower 		}
94143361Ssklower 		lcp -> lcd_upper = l -> lcd_upper;
94243361Ssklower 		lcp -> lcd_upnext = l -> lcd_upnext;
94341591Ssklower 		lcp -> lcd_lcn = lcn;
94441591Ssklower 		lcp -> lcd_state = RECEIVED_CALL;
94550020Ssklower 		sa -> x25_opts.op_flags |= (sxp -> x25_opts.op_flags &
94650020Ssklower 			~X25_REVERSE_CHARGE) | l -> lcd_flags;
94741591Ssklower 		pk_assoc (pkp, lcp, sa);
94849930Ssklower 		lcp -> lcd_faddr = *sa;
94949930Ssklower 		lcp -> lcd_laddr.x25_udlen = sxp -> x25_udlen;
95049930Ssklower 		lcp -> lcd_craddr = &lcp->lcd_faddr;
95141591Ssklower 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
95245573Ssklower 		if (lcp -> lcd_flags & X25_DBIT) {
95357024Ssklower 			if (X25GBITS(xp -> bits, d_bit))
95457024Ssklower 				X25SBITS(mtod(lcp -> lcd_template,
95557024Ssklower 					struct x25_packet *) -> bits, d_bit, 1);
95645573Ssklower 			else
95745573Ssklower 				lcp -> lcd_flags &= ~X25_DBIT;
95845573Ssklower 		}
95943361Ssklower 		if (so) {
96043361Ssklower 			pk_output (lcp);
96142277Ssklower 			soisconnected (so);
96245895Ssklower 			if (so -> so_options & SO_OOBINLINE)
96345895Ssklower 				save_extra(m0, facp, so);
96445895Ssklower 		} else if (lcp -> lcd_upper) {
96549930Ssklower 			(*lcp -> lcd_upper) (lcp, m0);
96645895Ssklower 		}
96749930Ssklower 		(void) m_free (m);
96841591Ssklower 		return;
96941591Ssklower 	}
97041591Ssklower 
97141591Ssklower 	/*
97241591Ssklower 	 * If the call fails for whatever reason, we still need to build a
97341591Ssklower 	 * skeleton LCD in order to be able to properly  receive the CLEAR
97441591Ssklower 	 * CONFIRMATION.
97541591Ssklower 	 */
97641591Ssklower #ifdef WATERLOO		/* be explicit */
97741591Ssklower 	if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0)
97841591Ssklower 		pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s",
97941591Ssklower 			sa->x25_addr, sa->x25_udata[3] & 0xff, errstr);
98041591Ssklower 	else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0)
98141591Ssklower 		pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s",
98241591Ssklower 			sa->x25_addr, errstr);
98341591Ssklower 	else
98441591Ssklower #endif
98541591Ssklower 	pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s",
98641591Ssklower 		sa -> x25_addr, sa -> x25_udata[0] & 0xff,
98741591Ssklower 		sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff,
98841591Ssklower 		sa -> x25_udata[3] & 0xff, errstr);
98945297Ssklower 	if ((lcp = pk_attach((struct socket *)0)) == 0) {
99045297Ssklower 		(void) m_free (m);
99141591Ssklower 		return;
99241591Ssklower 	}
99341591Ssklower 	lcp -> lcd_lcn = lcn;
99441591Ssklower 	lcp -> lcd_state = RECEIVED_CALL;
99541591Ssklower 	pk_assoc (pkp, lcp, sa);
99645297Ssklower 	(void) m_free (m);
99745895Ssklower 	pk_clear (lcp, 0, 1);
99841591Ssklower }
99941591Ssklower 
100049593Ssklower pk_call_accepted (lcp, m)
100141591Ssklower struct pklcd *lcp;
100249252Ssklower struct mbuf *m;
100341591Ssklower {
100441591Ssklower 	register struct x25_calladdr *ap;
100541591Ssklower 	register octet *fcp;
100649252Ssklower 	struct x25_packet *xp = mtod (m, struct x25_packet *);
100749252Ssklower 	int len = m -> m_len;
100841591Ssklower 
100941591Ssklower 	lcp -> lcd_state = DATA_TRANSFER;
101045297Ssklower 	if (lcp -> lcd_so)
101145297Ssklower 		soisconnected (lcp -> lcd_so);
101257024Ssklower 	if ((lcp -> lcd_flags & X25_DBIT) && (X25GBITS(xp -> bits, d_bit) == 0))
101345573Ssklower 		lcp -> lcd_flags &= ~X25_DBIT;
101441591Ssklower 	if (len > 3) {
101541591Ssklower 		ap = (struct x25_calladdr *) &xp -> packet_data;
101657024Ssklower 		fcp = (octet *) ap -> address_field + (X25GBITS(ap -> addrlens, calling_addrlen) +
101757024Ssklower 			X25GBITS(ap -> addrlens, called_addrlen) + 1) / 2;
101841591Ssklower 		if (fcp + *fcp <= ((octet *)xp) + len)
101949593Ssklower 			pk_parse_facilities (fcp, lcp -> lcd_ceaddr);
102041591Ssklower 	}
102141591Ssklower 	pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
102249252Ssklower 	if (lcp -> lcd_so == 0 && lcp -> lcd_upper)
102349252Ssklower 		lcp -> lcd_upper(lcp, m);
102441591Ssklower }
102541591Ssklower 
102649593Ssklower pk_parse_facilities (fcp, sa)
102741591Ssklower register octet *fcp;
102841591Ssklower register struct sockaddr_x25 *sa;
102941591Ssklower {
103041591Ssklower 	register octet *maxfcp;
103141591Ssklower 
103241591Ssklower 	maxfcp = fcp + *fcp;
103341591Ssklower 	fcp++;
103441591Ssklower 	while (fcp < maxfcp) {
103541591Ssklower 		/*
103641591Ssklower 		 * Ignore national DCE or DTE facilities
103741591Ssklower 		 */
103841591Ssklower 		if (*fcp == 0 || *fcp == 0xff)
103941591Ssklower 			break;
104041591Ssklower 		switch (*fcp) {
104141591Ssklower 		case FACILITIES_WINDOWSIZE:
104241591Ssklower 			sa -> x25_opts.op_wsize = fcp[1];
104341591Ssklower 			fcp += 3;
104441591Ssklower 			break;
104541591Ssklower 
104641591Ssklower 		case FACILITIES_PACKETSIZE:
104741591Ssklower 			sa -> x25_opts.op_psize = fcp[1];
104841591Ssklower 			fcp += 3;
104941591Ssklower 			break;
105041591Ssklower 
105141591Ssklower 		case FACILITIES_THROUGHPUT:
105241591Ssklower 			sa -> x25_opts.op_speed = fcp[1];
105341591Ssklower 			fcp += 2;
105441591Ssklower 			break;
105541591Ssklower 
105641591Ssklower 		case FACILITIES_REVERSE_CHARGE:
105741591Ssklower 			if (fcp[1] & 01)
105841591Ssklower 				sa -> x25_opts.op_flags |= X25_REVERSE_CHARGE;
105941591Ssklower 			/*
106041591Ssklower 			 * Datapac specific: for a X.25(1976) DTE, bit 2
106141591Ssklower 			 * indicates a "hi priority" (eg. international) call.
106241591Ssklower 			 */
106341591Ssklower 			if (fcp[1] & 02 && sa -> x25_opts.op_psize == 0)
106441591Ssklower 				sa -> x25_opts.op_psize = X25_PS128;
106541591Ssklower 			fcp += 2;
106641591Ssklower 			break;
106741591Ssklower 
106841591Ssklower 		default:
106941591Ssklower /*printf("unknown facility %x, class=%d\n", *fcp, (*fcp & 0xc0) >> 6);*/
107041591Ssklower 			switch ((*fcp & 0xc0) >> 6) {
107141591Ssklower 			case 0:			/* class A */
107241591Ssklower 				fcp += 2;
107341591Ssklower 				break;
107441591Ssklower 
107541591Ssklower 			case 1:
107641591Ssklower 				fcp += 3;
107741591Ssklower 				break;
107841591Ssklower 
107941591Ssklower 			case 2:
108041591Ssklower 				fcp += 4;
108141591Ssklower 				break;
108241591Ssklower 
108341591Ssklower 			case 3:
108441591Ssklower 				fcp++;
108541591Ssklower 				fcp += *fcp;
108641591Ssklower 			}
108741591Ssklower 		}
108841591Ssklower 	}
108941591Ssklower }
1090