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