xref: /original-bsd/sys/netccitt/pk_input.c (revision de3f5c4e)
1 /*
2  * Copyright (c) University of British Columbia, 1984
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Laboratory for Computation Vision and the Computer Science Department
8  * of the University of British Columbia.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)pk_input.c	7.11 (Berkeley) 05/09/91
13  */
14 
15 #include "param.h"
16 #include "systm.h"
17 #include "mbuf.h"
18 #include "socket.h"
19 #include "protosw.h"
20 #include "socketvar.h"
21 #include "errno.h"
22 
23 #include "../net/if.h"
24 
25 #include "x25.h"
26 #include "pk.h"
27 #include "pk_var.h"
28 
29 /*
30  *  This procedure is called by the link level whenever the link
31  *  becomes operational, is reset, or when the link goes down.
32  */
33 
34 pk_ctlinput (code, xcp)
35 register struct x25config *xcp;
36 {
37 
38 	register struct pkcb *pkp;
39 
40 	for (pkp = pkcbhead; pkp; pkp = pkp -> pk_next)
41 		if (pkp -> pk_xcp == xcp)
42 			break;
43 
44 	if (pkp == 0)
45 		return (EINVAL);
46 
47 	switch (code) {
48 	case PRC_LINKUP:
49 		if (pkp -> pk_state == DTE_WAITING)
50 			pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
51 		break;
52 
53 	case PRC_LINKDOWN:
54 		pk_restart (pkp, -1);	/* Clear all active circuits */
55 		pkp -> pk_state = DTE_WAITING;
56 		break;
57 
58 	case PRC_LINKRESET:
59 		pk_restart (pkp, X25_RESTART_NETWORK_CONGESTION);
60 		break;
61 
62 	}
63 	return (0);
64 }
65 struct ifqueue pkintrq;
66 /*
67  * This routine is called if there are semi-smart devices that do HDLC
68  * in hardware and want to queue the packet and call level 3 directly
69  */
70 pkintr ()
71 {
72 	register struct mbuf *m;
73 	register struct ifaddr *ifa;
74 	register struct ifnet *ifp;
75 	register int s;
76 
77 	for (;;) {
78 		s = splimp ();
79 		IF_DEQUEUE (&pkintrq, m);
80 		splx (s);
81 		if (m == 0)
82 			break;
83 		if (m->m_len < PKHEADERLN) {
84 			printf ("pkintr: packet too short (len=%d)\n",
85 				m->m_len);
86 			m_freem (m);
87 			continue;
88 		}
89 		if ((m->m_flags & M_PKTHDR) == 0)
90 			panic("pkintr");
91 		ifp = m->m_pkthdr.rcvif;
92 		/*
93 		 * look up the appropriate control block
94 		 */
95 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
96 			if (ifa->ifa_addr->sa_family == AF_CCITT)
97 				break;
98 		if (ifa == 0)
99 			continue;
100 		pk_input(m, ((struct x25_ifaddr *)ifa)->ia_xcp);
101 	}
102 }
103 struct mbuf *pk_bad_packet;
104 struct mbuf_cache pk_input_cache = {0 };
105 /*
106  *  X.25 PACKET INPUT
107  *
108  *  This procedure is called by a link level procedure whenever
109  *  an information frame is received. It decodes the packet and
110  *  demultiplexes based on the logical channel number.
111  *
112  */
113 
114 pk_input (m, xcp)
115 register struct mbuf *m;
116 struct x25config *xcp;
117 {
118 	register struct x25_packet *xp;
119 	register struct pklcd *lcp;
120 	register struct socket *so = 0;
121 	register struct pkcb *pkp;
122 	int  ptype, lcn, lcdstate = LISTEN;
123 	static struct x25config *lastxcp;
124 	static struct pkcb *lastpkp;
125 
126 	if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize)
127 		mbuf_cache(&pk_input_cache, m);
128 	if (xcp == lastxcp)
129 		pkp = lastpkp;
130 	else {
131 		for (pkp = pkcbhead; ; pkp = pkp -> pk_next) {
132 			if (pkp == 0) {
133 				pk_message (0, xcp, "pk_input: unknown network");
134 				m_freem (m);
135 				return;
136 			}
137 			if (pkp -> pk_xcp == xcp)
138 				break;
139 		}
140 		lastxcp = xcp;
141 		lastpkp = pkp;
142 	}
143 
144 	xp = mtod (m, struct x25_packet *);
145 	ptype = pk_decode (xp);
146 	lcn = LCN(xp);
147 	lcp = pkp -> pk_chan[lcn];
148 
149 	/*
150 	 *  If the DTE is in Restart  state, then it will ignore data,
151 	 *  interrupt, call setup and clearing, flow control and reset
152 	 *  packets.
153 	 */
154 	if (lcn < 0 || lcn > pkp -> pk_maxlcn) {
155 		pk_message (lcn, pkp -> pk_xcp, "illegal lcn");
156 		m_freem (m);
157 		return;
158 	}
159 
160 	pk_trace (pkp -> pk_xcp, m, "P-In");
161 
162 	if (pkp -> pk_state != DTE_READY && ptype != RESTART && ptype != RESTART_CONF) {
163 		m_freem (m);
164 		return;
165 	}
166 	if (lcp) {
167 		so = lcp -> lcd_so;
168 		lcdstate = lcp -> lcd_state;
169 	} else {
170 		if (ptype == CLEAR) {	/* idle line probe (Datapac specific) */
171 			/* send response on lcd 0's output queue */
172 			lcp -> lcd_template = pk_template (lcn, X25_CLEAR_CONFIRM);
173 			pk_output (lcp);
174 			m_freem (m);
175 			return;
176 		}
177 		if (ptype != CALL)
178 			ptype = INVALID_PACKET;
179 	}
180 
181 	if (lcn == 0 && ptype != RESTART && ptype != RESTART_CONF) {
182 		pk_message (0, pkp -> pk_xcp, "illegal ptype (%d, %s) on lcn 0",
183 			ptype, pk_name[ptype / MAXSTATES]);
184 		if (pk_bad_packet)
185 			m_freem (pk_bad_packet);
186 		pk_bad_packet = m;
187 		return;
188 	}
189 
190 	switch (ptype + lcdstate) {
191 	/*
192 	 *  Incoming Call packet received.
193 	 */
194 	case CALL + LISTEN:
195 		pk_incoming_call (pkp, m);
196 		break;
197 
198 	/*
199 	 *  Call collision: Just throw this "incoming call" away since
200 	 *  the DCE will ignore it anyway.
201 	 */
202 	case CALL + SENT_CALL:
203 		pk_message ((int)lcn, pkp -> pk_xcp,
204 			"incoming call collision");
205 		break;
206 
207 	/*
208 	 *  Call confirmation packet received. This usually means our
209 	 *  previous connect request is now complete.
210 	 */
211 	case CALL_ACCEPTED + SENT_CALL:
212 		MCHTYPE(m, MT_CONTROL);
213 		pk_call_accepted (lcp, m);
214 		break;
215 
216 	/*
217 	 *  This condition can only happen if the previous state was
218 	 *  SENT_CALL. Just ignore the packet, eventually a clear
219 	 *  confirmation should arrive.
220 	 */
221 	case CALL_ACCEPTED + SENT_CLEAR:
222 		break;
223 
224 	/*
225 	 *  Clear packet received. This requires a complete tear down
226 	 *  of the virtual circuit.  Free buffers and control blocks.
227 	 *  and send a clear confirmation.
228 	 */
229 	case CLEAR + READY:
230 	case CLEAR + RECEIVED_CALL:
231 	case CLEAR + SENT_CALL:
232 	case CLEAR + DATA_TRANSFER:
233 		lcp -> lcd_state = RECEIVED_CLEAR;
234 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CLEAR_CONFIRM);
235 		pk_output (lcp);
236 		pk_clearcause (pkp, xp);
237 		if (lcp -> lcd_upper) {
238 			MCHTYPE(m, MT_CONTROL);
239 			lcp -> lcd_upper (lcp, m);
240 		}
241 		pk_close (lcp);
242 		lcp = 0;
243 		break;
244 
245 	/*
246 	 *  Clear collision: Treat this clear packet as a confirmation.
247 	 */
248 	case CLEAR + SENT_CLEAR:
249 		pk_close (lcp);
250 		break;
251 
252 	/*
253 	 *  Clear confirmation received. This usually means the virtual
254 	 *  circuit is now completely removed.
255 	 */
256 	case CLEAR_CONF + SENT_CLEAR:
257 		pk_close (lcp);
258 		break;
259 
260 	/*
261 	 *  A clear confirmation on an unassigned logical channel - just
262 	 *  ignore it. Note: All other packets on an unassigned channel
263 	 *  results in a clear.
264 	 */
265 	case CLEAR_CONF + READY:
266 		break;
267 
268 	/*
269 	 *  Data packet received. Pass on to next level. Move the Q and M
270 	 *  bits into the data portion for the next level.
271 	 */
272 	case DATA + DATA_TRANSFER:
273 		if (lcp -> lcd_reset_condition) {
274 			ptype = DELETE_PACKET;
275 			break;
276 		}
277 
278 		/*
279 		 *  Process the P(S) flow control information in this Data packet.
280 		 *  Check that the packets arrive in the correct sequence and that
281 		 *  they are within the "lcd_input_window". Input window rotation is
282 		 *  initiated by the receive interface.
283 		 */
284 
285 		if (PS(xp) != ((lcp -> lcd_rsn + 1) % MODULUS) ||
286 			PS(xp) == ((lcp -> lcd_input_window + lcp->lcd_windowsize) % MODULUS)) {
287 			m_freem (m);
288 			pk_procerror (RESET, lcp, "p(s) flow control error", 1);
289 			break;
290 		}
291 		lcp -> lcd_rsn = PS(xp);
292 
293 		if (pk_ack (lcp, PR(xp)) != PACKET_OK) {
294 			m_freem (m);
295 			break;
296 		}
297 		m -> m_data += PKHEADERLN;
298 		m -> m_len -= PKHEADERLN;
299 		m -> m_pkthdr.len -= PKHEADERLN;
300 
301 		if (lcp -> lcd_flags & X25_MBS_HOLD) {
302 			register struct mbuf *n = lcp -> lcd_cps;
303 			int mbit = MBIT(xp);
304 			octet q_and_d_bits;
305 
306 			if (n) {
307 				n -> m_pkthdr.len += m -> m_pkthdr.len;
308 				while (n -> m_next)
309 					n = n -> m_next;
310 				n -> m_next = m;
311 				m = lcp -> lcd_cps;
312 
313 				if (lcp -> lcd_cpsmax &&
314 				    n -> m_pkthdr.len > lcp -> lcd_cpsmax) {
315 					pk_procerror (RESET, lcp,
316 						"C.P.S. overflow", 128);
317 					return;
318 				}
319 				q_and_d_bits = 0xc0 & *(octet *)xp;
320 				xp = (struct x25_packet *)
321 					(mtod(m, octet *) - PKHEADERLN);
322 				*(octet *)xp |= q_and_d_bits;
323 			}
324 			if (mbit) {
325 				lcp -> lcd_cps = m;
326 				lcp -> lcd_rxcnt++;
327 				pk_flowcontrol(lcp, 0, 1);
328 				return;
329 			}
330 			lcp -> lcd_cps = 0;
331 		}
332 		if (so == 0)
333 			break;
334 		if (lcp -> lcd_flags & X25_MQBIT) {
335 			octet t = (xp -> q_bit) ? t = 0x80 : 0;
336 
337 			if (MBIT(xp))
338 				t |= 0x40;
339 			m -> m_data -= 1;
340 			m -> m_len += 1;
341 			m -> m_pkthdr.len += 1;
342 			*mtod(m, octet *) = t;
343 		}
344 
345 		/*
346 		 * Discard Q-BIT packets if the application
347 		 * doesn't want to be informed of M and Q bit status
348 		 */
349 		if (xp -> q_bit && (lcp -> lcd_flags & X25_MQBIT) == 0) {
350 			m_freem (m);
351 			lcp -> lcd_rxcnt++;
352 			/*
353 			 * NB.  This is dangerous: sending a RR here can
354 			 * cause sequence number errors if a previous data
355 			 * packet has not yet been passed up to the application
356 			 * (RR's are normally generated via PRU_RCVD).
357 			 */
358 			pk_flowcontrol(lcp, 0, 1);
359 		} else {
360 			sbappendrecord (&so -> so_rcv, m);
361 			sorwakeup (so);
362 		}
363 		break;
364 
365 	/*
366 	 *  Interrupt packet received.
367 	 */
368 	case INTERRUPT + DATA_TRANSFER:
369 		if (lcp -> lcd_reset_condition)
370 			break;
371 		lcp -> lcd_intrdata = xp -> packet_data;
372 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM);
373 		pk_output (lcp);
374 		m -> m_data += PKHEADERLN;
375 		m -> m_len -= PKHEADERLN;
376 		m -> m_pkthdr.len -= PKHEADERLN;
377 		MCHTYPE(m, MT_OOBDATA);
378 		if (so) {
379 			if (so -> so_options & SO_OOBINLINE)
380 				sbinsertoob (&so -> so_rcv, m);
381 			else
382 				m_freem (m);
383 			sohasoutofband (so);
384 		}
385 		break;
386 
387 	/*
388 	 *  Interrupt confirmation packet received.
389 	 */
390 	case INTERRUPT_CONF + DATA_TRANSFER:
391 		if (lcp -> lcd_reset_condition)
392 			break;
393 		if (lcp -> lcd_intrconf_pending == TRUE)
394 			lcp -> lcd_intrconf_pending = FALSE;
395 		else
396 			pk_procerror (RESET, lcp, "unexpected packet", 43);
397 		break;
398 
399 	/*
400 	 *  Receiver ready received. Rotate the output window and output
401 	 *  any data packets waiting transmission.
402 	 */
403 	case RR + DATA_TRANSFER:
404 		if (lcp -> lcd_reset_condition ||
405 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
406 			ptype = DELETE_PACKET;
407 			break;
408 		}
409 		if (lcp -> lcd_rnr_condition == TRUE)
410 			lcp -> lcd_rnr_condition = FALSE;
411 		pk_output (lcp);
412 		break;
413 
414 	/*
415 	 *  Receiver Not Ready received. Packets up to the P(R) can be
416 	 *  be sent. Condition is cleared with a RR.
417 	 */
418 	case RNR + DATA_TRANSFER:
419 		if (lcp -> lcd_reset_condition ||
420 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
421 			ptype = DELETE_PACKET;
422 			break;
423 		}
424 		lcp -> lcd_rnr_condition = TRUE;
425 		break;
426 
427 	/*
428 	 *  Reset packet received. Set state to FLOW_OPEN.  The Input and
429 	 *  Output window edges ar set to zero. Both the send and receive
430 	 *  numbers are reset. A confirmation is returned.
431 	 */
432 	case RESET + DATA_TRANSFER:
433 		if (lcp -> lcd_reset_condition)
434 			/* Reset collision. Just ignore packet. */
435 			break;
436 
437 		pk_resetcause (pkp, xp);
438 		lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
439 			lcp -> lcd_intrconf_pending = FALSE;
440 		lcp -> lcd_output_window = lcp -> lcd_input_window =
441 			lcp -> lcd_last_transmitted_pr = 0;
442 		lcp -> lcd_ssn = 0;
443 		lcp -> lcd_rsn = MODULUS - 1;
444 
445 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM);
446 		pk_output (lcp);
447 
448 		pk_flush(lcp);
449 		if (so == 0)
450 			break;
451 		wakeup ((caddr_t) & so -> so_timeo);
452 		sorwakeup (so);
453 		sowwakeup (so);
454 		break;
455 
456 	/*
457 	 *  Reset confirmation received.
458 	 */
459 	case RESET_CONF + DATA_TRANSFER:
460 		if (lcp -> lcd_reset_condition) {
461 			lcp -> lcd_reset_condition = FALSE;
462 			pk_output (lcp);
463 		}
464 		else
465 			pk_procerror (RESET, lcp, "unexpected packet", 32);
466 		break;
467 
468 	case DATA + SENT_CLEAR:
469 		ptype = DELETE_PACKET;
470 	case RR + SENT_CLEAR:
471 	case RNR + SENT_CLEAR:
472 	case INTERRUPT + SENT_CLEAR:
473 	case INTERRUPT_CONF + SENT_CLEAR:
474 	case RESET + SENT_CLEAR:
475 	case RESET_CONF + SENT_CLEAR:
476 		/* Just ignore p if we have sent a CLEAR already.
477 		   */
478 		break;
479 
480 	/*
481 	 *  Restart sets all the permanent virtual circuits to the "Data
482 	 *  Transfer" stae and  all the switched virtual circuits to the
483 	 *  "Ready" state.
484 	 */
485 	case RESTART + READY:
486 		switch (pkp -> pk_state) {
487 		case DTE_SENT_RESTART:
488 			/* Restart collision. */
489 			pkp -> pk_state = DTE_READY;
490 			pk_message (0, pkp -> pk_xcp,
491 				"Packet level operational");
492 			break;
493 
494 		default:
495 			pk_restart (pkp, -1);
496 			pk_restartcause (pkp, xp);
497 			pkp -> pk_chan[0] -> lcd_template = pk_template (0,
498 				X25_RESTART_CONFIRM);
499 			pk_output (pkp -> pk_chan[0]);
500 		}
501 		break;
502 
503 	/*
504 	 *  Restart confirmation received. All logical channels are set
505 	 *  to READY.
506 	 */
507 	case RESTART_CONF + READY:
508 		switch (pkp -> pk_state) {
509 		case DTE_SENT_RESTART:
510 			pkp -> pk_state = DTE_READY;
511 			pk_message (0, pkp -> pk_xcp,
512 				"Packet level operational");
513 			break;
514 
515 		default:
516 			/* Restart local procedure error. */
517 			pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
518 			pkp -> pk_state = DTE_SENT_RESTART;
519 		}
520 		break;
521 
522 	default:
523 		if (lcp) {
524 			pk_procerror (CLEAR, lcp, "unknown packet error", 33);
525 			pk_message (lcn, pkp -> pk_xcp,
526 				"\"%s\" unexpected in \"%s\" state",
527 				pk_name[ptype/MAXSTATES], pk_state[lcdstate]);
528 		} else
529 			pk_message (lcn, pkp -> pk_xcp,
530 				"packet arrived on unassigned lcn");
531 		break;
532 	}
533 	if (so == 0 && lcp && lcp -> lcd_upper && lcdstate == DATA_TRANSFER) {
534 		if (ptype != DATA && ptype != INTERRUPT)
535 			MCHTYPE(m, MT_CONTROL);
536 		lcp -> lcd_upper (lcp, m);
537 	} else if (ptype != DATA && ptype != INTERRUPT)
538 		m_freem (m);
539 }
540 
541 
542 /*
543  * This routine handles incoming call packets. It matches the protocol
544  * field on the Call User Data field (usually the first four bytes) with
545  * sockets awaiting connections.
546  */
547 
548 pk_incoming_call (pkp, m0)
549 struct mbuf *m0;
550 struct pkcb *pkp;
551 {
552 	register struct pklcd *lcp = 0, *l;
553 	register struct sockaddr_x25 *sa;
554 	register struct x25_calladdr *a;
555 	register struct socket *so = 0;
556 	struct x25_packet *xp = mtod(m0, struct x25_packet *);
557 	struct mbuf *m;
558 	int len = m0->m_pkthdr.len;
559 	register int l1, l2;
560 	char *e, *errstr = "server unavailable";
561 	octet *u, *facp;
562 	int lcn = LCN(xp);
563 
564 	/* First, copy the data from the incoming call packet to a X25_socket
565 	   descriptor. */
566 
567 	a = (struct x25_calladdr *) &xp -> packet_data;
568 	l1 = a -> calling_addrlen;
569 	l2 = a -> called_addrlen;
570 	if ((m = m_getclr (M_DONTWAIT, MT_SONAME)) == 0)
571 		return;
572 	sa = mtod (m, struct sockaddr_x25 *);
573 	u = (octet *) (a -> address_field + l2 / 2);
574 	e = sa -> x25_addr;
575 	if (l2 & 0x01) {
576 		*e++ = *u++ & 0x0f;
577 		l1--;
578 	}
579 	from_bcd (e, &u, l1);
580 	if (l1 & 0x01)
581 		u++;
582 
583 	facp = u;
584 	pk_parse_facilities (u, sa);
585 	u += *u + 1;
586 	sa -> x25_udlen = min (16, ((octet *)xp) + len - u);
587 	if (sa -> x25_udlen < 0)
588 		sa -> x25_udlen = 0;
589 	bcopy ((caddr_t)u, sa -> x25_udata, (unsigned)sa -> x25_udlen);
590 
591 	/*
592 	 * Now, loop through the  listen sockets looking for a match on the
593 	 * PID. That is  the first  four octets  of the user data field.  This
594 	 * is the closest thing to a port number for X.25 packets. What it
595 	 * does provide is away of  multiplexing  services at the user level.
596 	 */
597 
598 	for (l = pk_listenhead; l; l = l -> lcd_listen) {
599 		struct sockaddr_x25 *sxp = l -> lcd_ceaddr;
600 
601 		if (bcmp (sxp -> x25_udata, sa -> x25_udata, sxp->x25_udlen))
602 			continue;
603 		if (sxp -> x25_net &&
604 		    sxp -> x25_net != pkp->pk_xc.xc_addr.x25_net)
605 			continue;
606 		/*
607 		 * don't accept incoming collect calls unless
608 		 * the server sets the reverse charging option.
609 		 */
610 		if ((sxp -> x25_opts.op_flags & (X25_OLDSOCKADDR|X25_REVERSE_CHARGE)) == 0 &&
611 			sa -> x25_opts.op_flags & X25_REVERSE_CHARGE) {
612 			errstr = "incoming collect call refused";
613 			break;
614 		}
615 		/*
616 		 * don't accept incoming calls with the D-Bit on
617 		 * unless the server agrees
618 		 */
619 		if (xp -> d_bit && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
620 			errstr = "incoming D-Bit mismatch";
621 			break;
622 		}
623 		if (l -> lcd_so) {
624 			if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
625 				    lcp = (struct pklcd *) so -> so_pcb;
626 		} else
627 			lcp = pk_attach((struct socket *) 0);
628 		if (lcp == 0) {
629 			/*
630 			 * Insufficient space or too many unaccepted
631 			 * connections.  Just throw the call away.
632 			 */
633 			errstr = "server malfunction";
634 			break;
635 		}
636 		lcp -> lcd_upper = l -> lcd_upper;
637 		lcp -> lcd_upnext = l -> lcd_upnext;
638 		lcp -> lcd_lcn = lcn;
639 		lcp -> lcd_state = RECEIVED_CALL;
640 		lcp -> lcd_craddr = sa;
641 		sa -> x25_opts.op_flags |= sxp -> x25_opts.op_flags &
642 			~X25_REVERSE_CHARGE;
643 		pk_assoc (pkp, lcp, sa);
644 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
645 		if (lcp -> lcd_flags & X25_DBIT) {
646 			if (xp -> d_bit)
647 				mtod(lcp -> lcd_template,
648 					struct x25_packet *) -> d_bit = 1;
649 			else
650 				lcp -> lcd_flags &= ~X25_DBIT;
651 		}
652 		if (so) {
653 			pk_output (lcp);
654 			soisconnected (so);
655 			if (so -> so_options & SO_OOBINLINE)
656 				save_extra(m0, facp, so);
657 		} else if (lcp -> lcd_upper) {
658 			m -> m_next = m0;
659 			(*lcp -> lcd_upper) (lcp, m);
660 			(void) m_free (m);  /* only m; m0 freed by caller */
661 		}
662 		return;
663 	}
664 
665 	/*
666 	 * If the call fails for whatever reason, we still need to build a
667 	 * skeleton LCD in order to be able to properly  receive the CLEAR
668 	 * CONFIRMATION.
669 	 */
670 #ifdef WATERLOO		/* be explicit */
671 	if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0)
672 		pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s",
673 			sa->x25_addr, sa->x25_udata[3] & 0xff, errstr);
674 	else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0)
675 		pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s",
676 			sa->x25_addr, errstr);
677 	else
678 #endif
679 	pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s",
680 		sa -> x25_addr, sa -> x25_udata[0] & 0xff,
681 		sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff,
682 		sa -> x25_udata[3] & 0xff, errstr);
683 	if ((lcp = pk_attach((struct socket *)0)) == 0) {
684 		(void) m_free (m);
685 		return;
686 	}
687 	lcp -> lcd_lcn = lcn;
688 	lcp -> lcd_state = RECEIVED_CALL;
689 	pk_assoc (pkp, lcp, sa);
690 	(void) m_free (m);
691 	pk_clear (lcp, 0, 1);
692 }
693 
694 static
695 save_extra(m0, fp, so)
696 struct mbuf *m0;
697 octet *fp;
698 struct socket *so;
699 {
700 	register struct mbuf *m;
701 	struct cmsghdr cmsghdr;
702 	if (m = m_copym (m, 0, (int)M_COPYALL)) {
703 		int off = fp - mtod (m0, octet *);
704 		int len = m->m_pkthdr.len - off + sizeof (cmsghdr);
705 		cmsghdr.cmsg_len = len;
706 		cmsghdr.cmsg_level = AF_CCITT;
707 		cmsghdr.cmsg_type = PK_FACILITIES;
708 		m_adj (m, off);
709 		M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT);
710 		if (m == 0)
711 			return;
712 		bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr));
713 		MCHTYPE(m, MT_CONTROL);
714 		sbappendrecord(&so -> so_rcv, m);
715 	}
716 }
717 
718 pk_call_accepted (lcp, m)
719 struct pklcd *lcp;
720 struct mbuf *m;
721 {
722 	register struct x25_calladdr *ap;
723 	register octet *fcp;
724 	struct x25_packet *xp = mtod (m, struct x25_packet *);
725 	int len = m -> m_len;
726 
727 	lcp -> lcd_state = DATA_TRANSFER;
728 	if (lcp -> lcd_so)
729 		soisconnected (lcp -> lcd_so);
730 	if ((lcp -> lcd_flags & X25_DBIT) && (xp -> d_bit == 0))
731 		lcp -> lcd_flags &= ~X25_DBIT;
732 	if (len > 3) {
733 		ap = (struct x25_calladdr *) &xp -> packet_data;
734 		fcp = (octet *) ap -> address_field + (ap -> calling_addrlen +
735 			ap -> called_addrlen + 1) / 2;
736 		if (fcp + *fcp <= ((octet *)xp) + len)
737 			pk_parse_facilities (fcp, lcp -> lcd_ceaddr);
738 	}
739 	pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
740 	if (lcp -> lcd_so == 0 && lcp -> lcd_upper)
741 		lcp -> lcd_upper(lcp, m);
742 }
743 
744 pk_parse_facilities (fcp, sa)
745 register octet *fcp;
746 register struct sockaddr_x25 *sa;
747 {
748 	register octet *maxfcp;
749 
750 	maxfcp = fcp + *fcp;
751 	fcp++;
752 	while (fcp < maxfcp) {
753 		/*
754 		 * Ignore national DCE or DTE facilities
755 		 */
756 		if (*fcp == 0 || *fcp == 0xff)
757 			break;
758 		switch (*fcp) {
759 		case FACILITIES_WINDOWSIZE:
760 			sa -> x25_opts.op_wsize = fcp[1];
761 			fcp += 3;
762 			break;
763 
764 		case FACILITIES_PACKETSIZE:
765 			sa -> x25_opts.op_psize = fcp[1];
766 			fcp += 3;
767 			break;
768 
769 		case FACILITIES_THROUGHPUT:
770 			sa -> x25_opts.op_speed = fcp[1];
771 			fcp += 2;
772 			break;
773 
774 		case FACILITIES_REVERSE_CHARGE:
775 			if (fcp[1] & 01)
776 				sa -> x25_opts.op_flags |= X25_REVERSE_CHARGE;
777 			/*
778 			 * Datapac specific: for a X.25(1976) DTE, bit 2
779 			 * indicates a "hi priority" (eg. international) call.
780 			 */
781 			if (fcp[1] & 02 && sa -> x25_opts.op_psize == 0)
782 				sa -> x25_opts.op_psize = X25_PS128;
783 			fcp += 2;
784 			break;
785 
786 		default:
787 /*printf("unknown facility %x, class=%d\n", *fcp, (*fcp & 0xc0) >> 6);*/
788 			switch ((*fcp & 0xc0) >> 6) {
789 			case 0:			/* class A */
790 				fcp += 2;
791 				break;
792 
793 			case 1:
794 				fcp += 3;
795 				break;
796 
797 			case 2:
798 				fcp += 4;
799 				break;
800 
801 			case 3:
802 				fcp++;
803 				fcp += *fcp;
804 			}
805 		}
806 	}
807 }
808 
809 from_bcd (a, x, len)
810 register char *a;
811 register octet **x;
812 register int len;
813 {
814 	register int posn = 0;
815 
816 	while (--len >= 0) {
817 		if (posn++ & 0x01)
818 			*a = *(*x)++ & 0x0f;
819 		else
820 			*a = (**x >> 4) & 0x0F;
821 		*a++ |= 0x30;
822 	}
823 }
824