xref: /csrg-svn/sys/netccitt/pk_input.c (revision 45895)
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.8 (Berkeley) 01/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 /*
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 				return;
318 			}
319 			lcp -> lcd_cps = 0;
320 		}
321 		if (so == 0)
322 			break;
323 		if (lcp -> lcd_flags & X25_MQBIT) {
324 			octet t = (xp -> q_bit) ? t = 0x80 : 0;
325 
326 			if (MBIT(xp))
327 				t |= 0x40;
328 			m -> m_data -= 1;
329 			m -> m_len += 1;
330 			m -> m_pkthdr.len += 1;
331 			*mtod(m, octet *) = t;
332 		}
333 
334 		/*
335 		 * Discard Q-BIT packets if the application
336 		 * doesn't want to be informed of M and Q bit status
337 		 */
338 		if (xp -> q_bit && (lcp -> lcd_flags & X25_MQBIT) == 0) {
339 			m_freem (m);
340 			lcp -> lcd_rxcnt++;
341 			/*
342 			 * NB.  This is dangerous: sending a RR here can
343 			 * cause sequence number errors if a previous data
344 			 * packet has not yet been passed up to the application
345 			 * (RR's are normally generated via PRU_RCVD).
346 			 */
347 			lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RR);
348 			pk_output (lcp);
349 		} else {
350 			sbappendrecord (&so -> so_rcv, m);
351 			sorwakeup (so);
352 		}
353 		break;
354 
355 	/*
356 	 *  Interrupt packet received.
357 	 */
358 	case INTERRUPT + DATA_TRANSFER:
359 		if (lcp -> lcd_reset_condition)
360 			break;
361 		lcp -> lcd_intrdata = xp -> packet_data;
362 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_INTERRUPT_CONFIRM);
363 		pk_output (lcp);
364 		m -> m_data += PKHEADERLN;
365 		m -> m_len -= PKHEADERLN;
366 		m -> m_pkthdr.len -= PKHEADERLN;
367 		MCHTYPE(m, MT_OOBDATA);
368 		if (so) {
369 			if (so -> so_options & SO_OOBINLINE)
370 				sbinsertoob (&so -> so_rcv, m);
371 			else
372 				m_freem (m);
373 			sohasoutofband (so);
374 		}
375 		break;
376 
377 	/*
378 	 *  Interrupt confirmation packet received.
379 	 */
380 	case INTERRUPT_CONF + DATA_TRANSFER:
381 		if (lcp -> lcd_reset_condition)
382 			break;
383 		if (lcp -> lcd_intrconf_pending == TRUE)
384 			lcp -> lcd_intrconf_pending = FALSE;
385 		else
386 			pk_procerror (RESET, lcp, "unexpected packet", 43);
387 		break;
388 
389 	/*
390 	 *  Receiver ready received. Rotate the output window and output
391 	 *  any data packets waiting transmission.
392 	 */
393 	case RR + DATA_TRANSFER:
394 		if (lcp -> lcd_reset_condition ||
395 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
396 			ptype = DELETE_PACKET;
397 			break;
398 		}
399 		if (lcp -> lcd_rnr_condition == TRUE)
400 			lcp -> lcd_rnr_condition = FALSE;
401 		pk_output (lcp);
402 		break;
403 
404 	/*
405 	 *  Receiver Not Ready received. Packets up to the P(R) can be
406 	 *  be sent. Condition is cleared with a RR.
407 	 */
408 	case RNR + DATA_TRANSFER:
409 		if (lcp -> lcd_reset_condition ||
410 		    pk_ack (lcp, PR(xp)) != PACKET_OK) {
411 			ptype = DELETE_PACKET;
412 			break;
413 		}
414 		lcp -> lcd_rnr_condition = TRUE;
415 		break;
416 
417 	/*
418 	 *  Reset packet received. Set state to FLOW_OPEN.  The Input and
419 	 *  Output window edges ar set to zero. Both the send and receive
420 	 *  numbers are reset. A confirmation is returned.
421 	 */
422 	case RESET + DATA_TRANSFER:
423 		if (lcp -> lcd_reset_condition)
424 			/* Reset collision. Just ignore packet. */
425 			break;
426 
427 		pk_resetcause (pkp, xp);
428 		lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
429 			lcp -> lcd_intrconf_pending = FALSE;
430 		lcp -> lcd_output_window = lcp -> lcd_input_window =
431 			lcp -> lcd_last_transmitted_pr = 0;
432 		lcp -> lcd_ssn = 0;
433 		lcp -> lcd_rsn = MODULUS - 1;
434 
435 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET_CONFIRM);
436 		pk_output (lcp);
437 
438 		pk_flush(lcp);
439 		if (so == 0)
440 			break;
441 		wakeup ((caddr_t) & so -> so_timeo);
442 		sorwakeup (so);
443 		sowwakeup (so);
444 		break;
445 
446 	/*
447 	 *  Reset confirmation received.
448 	 */
449 	case RESET_CONF + DATA_TRANSFER:
450 		if (lcp -> lcd_reset_condition) {
451 			lcp -> lcd_reset_condition = FALSE;
452 			pk_output (lcp);
453 		}
454 		else
455 			pk_procerror (RESET, lcp, "unexpected packet", 32);
456 		break;
457 
458 	case DATA + SENT_CLEAR:
459 		ptype = DELETE_PACKET;
460 	case RR + SENT_CLEAR:
461 	case RNR + SENT_CLEAR:
462 	case INTERRUPT + SENT_CLEAR:
463 	case INTERRUPT_CONF + SENT_CLEAR:
464 	case RESET + SENT_CLEAR:
465 	case RESET_CONF + SENT_CLEAR:
466 		/* Just ignore p if we have sent a CLEAR already.
467 		   */
468 		break;
469 
470 	/*
471 	 *  Restart sets all the permanent virtual circuits to the "Data
472 	 *  Transfer" stae and  all the switched virtual circuits to the
473 	 *  "Ready" state.
474 	 */
475 	case RESTART + READY:
476 		switch (pkp -> pk_state) {
477 		case DTE_SENT_RESTART:
478 			/* Restart collision. */
479 			pkp -> pk_state = DTE_READY;
480 			pk_message (0, pkp -> pk_xcp,
481 				"Packet level operational");
482 			break;
483 
484 		default:
485 			pk_restart (pkp, -1);
486 			pk_restartcause (pkp, xp);
487 			pkp -> pk_chan[0] -> lcd_template = pk_template (0,
488 				X25_RESTART_CONFIRM);
489 			pk_output (pkp -> pk_chan[0]);
490 		}
491 		break;
492 
493 	/*
494 	 *  Restart confirmation received. All logical channels are set
495 	 *  to READY.
496 	 */
497 	case RESTART_CONF + READY:
498 		switch (pkp -> pk_state) {
499 		case DTE_SENT_RESTART:
500 			pkp -> pk_state = DTE_READY;
501 			pk_message (0, pkp -> pk_xcp,
502 				"Packet level operational");
503 			break;
504 
505 		default:
506 			/* Restart local procedure error. */
507 			pk_restart (pkp, X25_RESTART_LOCAL_PROCEDURE_ERROR);
508 			pkp -> pk_state = DTE_SENT_RESTART;
509 		}
510 		break;
511 
512 	default:
513 		if (lcp) {
514 			pk_procerror (CLEAR, lcp, "unknown packet error", 33);
515 			pk_message (lcn, pkp -> pk_xcp,
516 				"\"%s\" unexpected in \"%s\" state",
517 				pk_name[ptype/MAXSTATES], pk_state[lcdstate]);
518 		} else
519 			pk_message (lcn, pkp -> pk_xcp,
520 				"packet arrived on unassigned lcn");
521 		break;
522 	}
523 	if (so == 0 && lcp -> lcd_upper &&
524 	    (lcdstate == SENT_CALL || lcdstate == DATA_TRANSFER)) {
525 		if (ptype != DATA && ptype != INTERRUPT)
526 			MCHTYPE(m, MT_CONTROL);
527 		lcp -> lcd_upper (lcp, m);
528 	} else if (ptype != DATA && ptype != INTERRUPT)
529 		m_freem (m);
530 }
531 
532 
533 /*
534  * This routine handles incoming call packets. It matches the protocol
535  * field on the Call User Data field (usually the first four bytes) with
536  * sockets awaiting connections.
537  */
538 
539 static
540 incoming_call (pkp, m0)
541 struct mbuf *m0;
542 struct pkcb *pkp;
543 {
544 	register struct pklcd *lcp = 0, *l;
545 	register struct sockaddr_x25 *sa;
546 	register struct x25_calladdr *a;
547 	register struct socket *so = 0;
548 	struct x25_packet *xp = mtod(m0, struct x25_packet *);
549 	struct mbuf *m;
550 	int len = m0->m_pkthdr.len;
551 	register int l1, l2;
552 	char *e, *errstr = "server unavailable";
553 	octet *u, *facp;
554 	int lcn = LCN(xp);
555 
556 	/* First, copy the data from the incoming call packet to a X25_socket
557 	   descriptor. */
558 
559 	a = (struct x25_calladdr *) &xp -> packet_data;
560 	l1 = a -> calling_addrlen;
561 	l2 = a -> called_addrlen;
562 	if ((m = m_getclr (M_DONTWAIT, MT_SONAME)) == 0)
563 		return;
564 	sa = mtod (m, struct sockaddr_x25 *);
565 	u = (octet *) (a -> address_field + l2 / 2);
566 	e = sa -> x25_addr;
567 	if (l2 & 0x01) {
568 		*e++ = *u++ & 0x0f;
569 		l1--;
570 	}
571 	from_bcd (e, &u, l1);
572 	if (l1 & 0x01)
573 		u++;
574 
575 	facp = u;
576 	parse_facilities (u, sa);
577 	u += *u + 1;
578 	sa -> x25_udlen = min (16, ((octet *)xp) + len - u);
579 	if (sa -> x25_udlen < 0)
580 		sa -> x25_udlen = 0;
581 	bcopy ((caddr_t)u, sa -> x25_udata, (unsigned)sa -> x25_udlen);
582 
583 	/*
584 	 * Now, loop through the  listen sockets looking for a match on the
585 	 * PID. That is  the first  four octets  of the user data field.  This
586 	 * is the closest thing to a port number for X.25 packets. What it
587 	 * does provide is away of  multiplexing  services at the user level.
588 	 */
589 
590 	for (l = pk_listenhead; l; l = l -> lcd_listen) {
591 		struct sockaddr_x25 *sxp = l -> lcd_ceaddr;
592 
593 		if (bcmp (sxp -> x25_udata, sa -> x25_udata, sxp->x25_udlen))
594 			continue;
595 		if (sxp -> x25_net &&
596 		    sxp -> x25_net != pkp->pk_xc.xc_addr.x25_net)
597 			continue;
598 		/*
599 		 * don't accept incoming collect calls unless
600 		 * the server sets the reverse charging option.
601 		 */
602 		if ((sxp -> x25_opts.op_flags & (X25_OLDSOCKADDR|X25_REVERSE_CHARGE)) == 0 &&
603 			sa -> x25_opts.op_flags & X25_REVERSE_CHARGE) {
604 			errstr = "incoming collect call refused";
605 			break;
606 		}
607 		/*
608 		 * don't accept incoming calls with the D-Bit on
609 		 * unless the server agrees
610 		 */
611 		if (xp -> d_bit && !(sxp -> x25_opts.op_flags & X25_DBIT)) {
612 			errstr = "incoming D-Bit mismatch";
613 			break;
614 		}
615 		if (l -> lcd_so) {
616 			if (so = sonewconn (l -> lcd_so, SS_ISCONNECTED))
617 				    lcp = (struct pklcd *) so -> so_pcb;
618 		} else
619 			lcp = pk_attach((struct socket *) 0);
620 		if (lcp == 0) {
621 			/*
622 			 * Insufficient space or too many unaccepted
623 			 * connections.  Just throw the call away.
624 			 */
625 			errstr = "server malfunction";
626 			break;
627 		}
628 		lcp -> lcd_upper = l -> lcd_upper;
629 		lcp -> lcd_upnext = l -> lcd_upnext;
630 		lcp -> lcd_lcn = lcn;
631 		lcp -> lcd_state = RECEIVED_CALL;
632 		lcp -> lcd_craddr = sa;
633 		sa -> x25_opts.op_flags |= sxp -> x25_opts.op_flags &
634 			~X25_REVERSE_CHARGE;
635 		pk_assoc (pkp, lcp, sa);
636 		lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL_ACCEPTED);
637 		if (lcp -> lcd_flags & X25_DBIT) {
638 			if (xp -> d_bit)
639 				mtod(lcp -> lcd_template,
640 					struct x25_packet *) -> d_bit = 1;
641 			else
642 				lcp -> lcd_flags &= ~X25_DBIT;
643 		}
644 		if (so) {
645 			pk_output (lcp);
646 			soisconnected (so);
647 			if (so -> so_options & SO_OOBINLINE)
648 				save_extra(m0, facp, so);
649 		} else if (lcp -> lcd_upper) {
650 			m -> m_next = m0;
651 			(*lcp -> lcd_upper) (lcp, m);
652 			(void) m_free (m);  /* only m; m0 freed by caller */
653 		}
654 		return;
655 	}
656 
657 	/*
658 	 * If the call fails for whatever reason, we still need to build a
659 	 * skeleton LCD in order to be able to properly  receive the CLEAR
660 	 * CONFIRMATION.
661 	 */
662 #ifdef WATERLOO		/* be explicit */
663 	if (l == 0 && bcmp(sa->x25_udata, "ean", 3) == 0)
664 		pk_message (lcn, pkp -> pk_xcp, "host=%s ean%c: %s",
665 			sa->x25_addr, sa->x25_udata[3] & 0xff, errstr);
666 	else if (l == 0 && bcmp(sa->x25_udata, "\1\0\0\0", 4) == 0)
667 		pk_message (lcn, pkp -> pk_xcp, "host=%s x29d: %s",
668 			sa->x25_addr, errstr);
669 	else
670 #endif
671 	pk_message (lcn, pkp -> pk_xcp, "host=%s pid=%x %x %x %x: %s",
672 		sa -> x25_addr, sa -> x25_udata[0] & 0xff,
673 		sa -> x25_udata[1] & 0xff, sa -> x25_udata[2] & 0xff,
674 		sa -> x25_udata[3] & 0xff, errstr);
675 	if ((lcp = pk_attach((struct socket *)0)) == 0) {
676 		(void) m_free (m);
677 		return;
678 	}
679 	lcp -> lcd_lcn = lcn;
680 	lcp -> lcd_state = RECEIVED_CALL;
681 	pk_assoc (pkp, lcp, sa);
682 	(void) m_free (m);
683 	pk_clear (lcp, 0, 1);
684 }
685 
686 static
687 save_extra(m0, fp, so)
688 struct mbuf *m0;
689 octet *fp;
690 struct socket *so;
691 {
692 	register struct mbuf *m;
693 	struct cmsghdr cmsghdr;
694 	if (m = m_copym (m, 0, (int)M_COPYALL)) {
695 		int off = fp - mtod (m0, octet *);
696 		int len = m->m_pkthdr.len - off + sizeof (cmsghdr);
697 		cmsghdr.cmsg_len = len;
698 		cmsghdr.cmsg_level = AF_CCITT;
699 		cmsghdr.cmsg_type = PK_FACILITIES;
700 		m_adj (m, off);
701 		M_PREPEND (m, sizeof(cmsghdr), M_DONTWAIT);
702 		if (m == 0)
703 			return;
704 		bcopy ((caddr_t)&cmsghdr, mtod (m, caddr_t), sizeof (cmsghdr));
705 		MCHTYPE(m, MT_CONTROL);
706 		sbappendrecord(&so -> so_rcv, m);
707 	}
708 }
709 
710 static
711 call_accepted (lcp, xp, len)
712 struct pklcd *lcp;
713 struct x25_packet *xp;
714 {
715 	register struct x25_calladdr *ap;
716 	register octet *fcp;
717 
718 	lcp -> lcd_state = DATA_TRANSFER;
719 	if (lcp -> lcd_so)
720 		soisconnected (lcp -> lcd_so);
721 	if ((lcp -> lcd_flags & X25_DBIT) && (xp -> d_bit == 0))
722 		lcp -> lcd_flags &= ~X25_DBIT;
723 	if (len > 3) {
724 		ap = (struct x25_calladdr *) &xp -> packet_data;
725 		fcp = (octet *) ap -> address_field + (ap -> calling_addrlen +
726 			ap -> called_addrlen + 1) / 2;
727 		if (fcp + *fcp <= ((octet *)xp) + len)
728 			parse_facilities (fcp, lcp -> lcd_ceaddr);
729 	}
730 	pk_assoc (lcp -> lcd_pkp, lcp, lcp -> lcd_ceaddr);
731 }
732 
733 static
734 parse_facilities (fcp, sa)
735 register octet *fcp;
736 register struct sockaddr_x25 *sa;
737 {
738 	register octet *maxfcp;
739 
740 	maxfcp = fcp + *fcp;
741 	fcp++;
742 	while (fcp < maxfcp) {
743 		/*
744 		 * Ignore national DCE or DTE facilities
745 		 */
746 		if (*fcp == 0 || *fcp == 0xff)
747 			break;
748 		switch (*fcp) {
749 		case FACILITIES_WINDOWSIZE:
750 			sa -> x25_opts.op_wsize = fcp[1];
751 			fcp += 3;
752 			break;
753 
754 		case FACILITIES_PACKETSIZE:
755 			sa -> x25_opts.op_psize = fcp[1];
756 			fcp += 3;
757 			break;
758 
759 		case FACILITIES_THROUGHPUT:
760 			sa -> x25_opts.op_speed = fcp[1];
761 			fcp += 2;
762 			break;
763 
764 		case FACILITIES_REVERSE_CHARGE:
765 			if (fcp[1] & 01)
766 				sa -> x25_opts.op_flags |= X25_REVERSE_CHARGE;
767 			/*
768 			 * Datapac specific: for a X.25(1976) DTE, bit 2
769 			 * indicates a "hi priority" (eg. international) call.
770 			 */
771 			if (fcp[1] & 02 && sa -> x25_opts.op_psize == 0)
772 				sa -> x25_opts.op_psize = X25_PS128;
773 			fcp += 2;
774 			break;
775 
776 		default:
777 /*printf("unknown facility %x, class=%d\n", *fcp, (*fcp & 0xc0) >> 6);*/
778 			switch ((*fcp & 0xc0) >> 6) {
779 			case 0:			/* class A */
780 				fcp += 2;
781 				break;
782 
783 			case 1:
784 				fcp += 3;
785 				break;
786 
787 			case 2:
788 				fcp += 4;
789 				break;
790 
791 			case 3:
792 				fcp++;
793 				fcp += *fcp;
794 			}
795 		}
796 	}
797 }
798 
799 from_bcd (a, x, len)
800 register char *a;
801 register octet **x;
802 register int len;
803 {
804 	register int posn = 0;
805 
806 	while (--len >= 0) {
807 		if (posn++ & 0x01)
808 			*a = *(*x)++ & 0x0f;
809 		else
810 			*a = (**x >> 4) & 0x0F;
811 		*a++ |= 0x30;
812 	}
813 }
814