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