xref: /csrg-svn/sys/netccitt/pk_subr.c (revision 49595)
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_subr.c	7.13 (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 #include "time.h"
23 #include "kernel.h"
24 
25 #include "../net/if.h"
26 
27 #include "x25.h"
28 #include "pk.h"
29 #include "pk_var.h"
30 #include "x25err.h"
31 
32 int     pk_sendspace = 1024 * 2 + 8;
33 int     pk_recvspace = 1024 * 2 + 8;
34 
35 struct pklcd_q pklcd_q = {&pklcd_q, &pklcd_q};
36 
37 /*
38  *  Attach X.25 protocol to socket, allocate logical channel descripter
39  *  and buffer space, and enter LISTEN state if we are to accept
40  *  IN-COMMING CALL packets.
41  *
42  */
43 
44 struct pklcd *
45 pk_attach (so)
46 struct socket *so;
47 {
48 	register struct pklcd *lcp;
49 	register int error = ENOBUFS;
50 	int pk_output();
51 
52 	MALLOC(lcp, struct pklcd *, sizeof (*lcp), M_PCB, M_NOWAIT);
53 	if (lcp) {
54 		bzero ((caddr_t)lcp, sizeof (*lcp));
55 		insque (&lcp -> lcd_q, &pklcd_q);
56 		lcp -> lcd_state = READY;
57 		lcp -> lcd_send = pk_output;
58 		if (so) {
59 			error = soreserve (so, pk_sendspace, pk_recvspace);
60 			lcp -> lcd_so = so;
61 			if (so -> so_options & SO_ACCEPTCONN)
62 				lcp -> lcd_state = LISTEN;
63 		} else
64 			sbreserve (&lcp -> lcd_sb, pk_sendspace);
65 	}
66 	if (so) {
67 		so -> so_pcb = (caddr_t) lcp;
68 		so -> so_error = error;
69 	}
70 	return (lcp);
71 }
72 
73 /*
74  *  Disconnect X.25 protocol from socket.
75  */
76 
77 pk_disconnect (lcp)
78 register struct pklcd *lcp;
79 {
80 	register struct socket *so = lcp -> lcd_so;
81 	register struct pklcd *l, *p;
82 
83 	switch (lcp -> lcd_state) {
84 	case LISTEN:
85 		for (p = 0, l = pk_listenhead; l && l != lcp; p = l, l = l -> lcd_listen);
86 		if (p == 0) {
87 			if (l != 0)
88 				pk_listenhead = l -> lcd_listen;
89 		}
90 		else
91 		if (l != 0)
92 			p -> lcd_listen = l -> lcd_listen;
93 		pk_close (lcp);
94 		break;
95 
96 	case READY:
97 		pk_acct (lcp);
98 		pk_close (lcp);
99 		break;
100 
101 	case SENT_CLEAR:
102 	case RECEIVED_CLEAR:
103 		break;
104 
105 	default:
106 		pk_acct (lcp);
107 		if (so) {
108 			soisdisconnecting (so);
109 			sbflush (&so -> so_rcv);
110 		}
111 		pk_clear (lcp, 241, 0); /* Normal Disconnect */
112 
113 	}
114 }
115 
116 /*
117  *  Close an X.25 Logical Channel. Discard all space held by the
118  *  connection and internal descriptors. Wake up any sleepers.
119  */
120 
121 pk_close (lcp)
122 struct pklcd *lcp;
123 {
124 	register struct socket *so = lcp -> lcd_so;
125 
126 	pk_freelcd (lcp);
127 
128 	if (so == NULL)
129 		return;
130 
131 	so -> so_pcb = 0;
132 	soisdisconnected (so);
133 	/* sofree (so);	/* gak!!! you can't do that here */
134 }
135 
136 /*
137  *  Create a template to be used to send X.25 packets on a logical
138  *  channel. It allocates an mbuf and fills in a skeletal packet
139  *  depending on its type. This packet is passed to pk_output where
140  *  the remainer of the packet is filled in.
141 */
142 
143 struct mbuf *
144 pk_template (lcn, type)
145 int lcn, type;
146 {
147 	register struct mbuf *m;
148 	register struct x25_packet *xp;
149 
150 	MGETHDR (m, M_DONTWAIT, MT_HEADER);
151 	if (m == 0)
152 		panic ("pk_template");
153 	m -> m_act = 0;
154 
155 	/*
156 	 * Efficiency hack: leave a four byte gap at the beginning
157 	 * of the packet level header with the hope that this will
158 	 * be enough room for the link level to insert its header.
159 	 */
160 	m -> m_data += max_linkhdr;
161 	m -> m_pkthdr.len = m -> m_len = PKHEADERLN;
162 
163 	xp = mtod (m, struct x25_packet *);
164 	*(long *)xp = 0;		/* ugly, but fast */
165 /*	xp -> q_bit = 0;*/
166 	xp -> fmt_identifier = 1;
167 /*	xp -> lc_group_number = 0;*/
168 
169 	SET_LCN(xp, lcn);
170 	xp -> packet_type = type;
171 
172 	return (m);
173 }
174 
175 /*
176  *  This routine restarts all the virtual circuits. Actually,
177  *  the virtual circuits are not "restarted" as such. Instead,
178  *  any active switched circuit is simply returned to READY
179  *  state.
180  */
181 
182 pk_restart (pkp, restart_cause)
183 register struct pkcb *pkp;
184 int restart_cause;
185 {
186 	register struct mbuf *m;
187 	register struct pklcd *lcp;
188 	register int i;
189 
190 	/* Restart all logical channels. */
191 	if (pkp -> pk_chan == 0)
192 		return;
193 	for (i = 1; i <= pkp -> pk_maxlcn; ++i)
194 		if ((lcp = pkp -> pk_chan[i]) != NULL) {
195 			if (lcp -> lcd_so) {
196 				lcp -> lcd_so -> so_error = ENETRESET;
197 				pk_close (lcp);
198 			} else {
199 				pk_flush (lcp);
200 				lcp -> lcd_state = READY;
201 				if (lcp -> lcd_upper)
202 					lcp -> lcd_upper (lcp, 0);
203 			}
204 		}
205 
206 	if (restart_cause < 0)
207 		return;
208 
209 	pkp -> pk_state = DTE_SENT_RESTART;
210 	lcp = pkp -> pk_chan[0];
211 	m = lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESTART);
212 	m -> m_pkthdr.len = m -> m_len += 2;
213 	mtod (m, struct x25_packet *) -> packet_data = 0;	/* DTE only */
214 	mtod (m, octet *)[4]  = restart_cause;
215 	pk_output (lcp);
216 }
217 
218 
219 /*
220  *  This procedure frees up the Logical Channel Descripter.
221  */
222 
223 pk_freelcd (lcp)
224 register struct pklcd *lcp;
225 {
226 	if (lcp == NULL)
227 		return;
228 
229 	if (lcp -> lcd_lcn > 0)
230 		lcp -> lcd_pkp -> pk_chan[lcp -> lcd_lcn] = NULL;
231 
232 	pk_flush (lcp);
233 	remque (&lcp -> lcd_q);
234 	free ((caddr_t)lcp, M_PCB);
235 }
236 
237 
238 /*
239  *  Bind a address and protocol value to a socket.  The important
240  *  part is the protocol value - the first four characters of the
241  *  Call User Data field.
242  */
243 
244 pk_bind (lcp, nam)
245 struct pklcd *lcp;
246 struct mbuf *nam;
247 {
248 	register struct pkcb *pkp;
249 	register struct pklcd *pp;
250 	register struct sockaddr_x25 *sa;
251 
252 	if (nam == NULL)
253 		return (EADDRNOTAVAIL);
254 	if (lcp -> lcd_ceaddr)				/* XXX */
255 		return (EADDRINUSE);
256 	if (pk_checksockaddr (nam))
257 		return (EINVAL);
258 	sa = mtod (nam, struct sockaddr_x25 *);
259 
260 	/*
261 	 * If the user wishes to accept calls only from a particular
262 	 * net (net != 0), make sure the net is known
263 	 */
264 
265 	if (sa -> x25_net)
266 		for (pkp = pkcbhead; ; pkp = pkp -> pk_next) {
267 			if (pkp == 0)
268 				return (ENETUNREACH);
269 			if (pkp -> pk_xcp -> xc_addr.x25_net == sa -> x25_net)
270 				break;
271 		}
272 
273 	/*
274 	 * For ISO's sake permit default listeners, but only one such . . .
275 	 */
276 	for (pp = pk_listenhead; pp; pp = pp -> lcd_listen) {
277 		register struct sockaddr_x25 *sa2 = pp -> lcd_ceaddr;
278 		if ((sa2 -> x25_udlen == sa -> x25_udlen) &&
279 		    (sa2 -> x25_udlen == 0 ||
280 		     (bcmp (sa2 -> x25_udata, sa -> x25_udata,
281 			    min (sa2 -> x25_udlen, sa -> x25_udlen)) == 0)))
282 				return (EADDRINUSE);
283 	}
284 	lcp -> lcd_laddr = *sa;
285 	lcp -> lcd_ceaddr = &lcp -> lcd_laddr;
286 	return (0);
287 }
288 
289 /*
290  * Include a bound control block in the list of listeners.
291  */
292 pk_listen (lcp)
293 register struct pklcd *lcp;
294 {
295 	register struct pklcd **pp;
296 
297 	if (lcp -> lcd_ceaddr == 0)
298 		return (EDESTADDRREQ);
299 
300 	lcp -> lcd_state = LISTEN;
301 	/*
302 	 * Add default listener at end, any others at start.
303 	 */
304 	if (lcp -> lcd_ceaddr -> x25_udlen == 0) {
305 		for (pp = &pk_listenhead; *pp; )
306 			pp = &((*pp) -> lcd_listen);
307 		*pp = lcp;
308 	} else {
309 		lcp -> lcd_listen = pk_listenhead;
310 		pk_listenhead = lcp;
311 	}
312 	return (0);
313 }
314 /*
315  * Include a listening control block for the benefit of other protocols.
316  */
317 pk_protolisten (spi, spilen, callee)
318 int (*callee) ();
319 {
320 	register struct pklcd *lcp = pk_attach ((struct socket *)0);
321 	register struct mbuf *nam;
322 	register struct sockaddr_x25 *sa;
323 	int error = ENOBUFS;
324 
325 	if (lcp) {
326 		if (nam = m_getclr (MT_SONAME, M_DONTWAIT)) {
327 			sa = mtod (nam, struct sockaddr_x25 *);
328 			sa -> x25_family = AF_CCITT;
329 			sa -> x25_len = nam -> m_len = sizeof (*sa);
330 			sa -> x25_udlen = spilen;
331 			sa -> x25_udata[0] = spi;
332 			lcp -> lcd_upper = callee;
333 			lcp -> lcd_flags = X25_MBS_HOLD;
334 			error = pk_bind (lcp, nam) || pk_listen (lcp);
335 			(void) m_free (nam);
336 		}
337 		if (error)
338 			pk_freelcd (lcp);
339 	}
340 	return error; /* Hopefully Zero !*/
341 }
342 
343 /*
344  * Associate a logical channel descriptor with a network.
345  * Fill in the default network specific parameters and then
346  * set any parameters explicitly specified by the user or
347  * by the remote DTE.
348  */
349 
350 pk_assoc (pkp, lcp, sa)
351 register struct pkcb *pkp;
352 register struct pklcd *lcp;
353 register struct sockaddr_x25 *sa;
354 {
355 
356 	lcp -> lcd_pkp = pkp;
357 	lcp -> lcd_packetsize = pkp -> pk_xcp -> xc_psize;
358 	lcp -> lcd_windowsize = pkp -> pk_xcp -> xc_pwsize;
359 	lcp -> lcd_rsn = MODULUS - 1;
360 	pkp -> pk_chan[lcp -> lcd_lcn] = lcp;
361 
362 	if (sa -> x25_opts.op_psize)
363 		lcp -> lcd_packetsize = sa -> x25_opts.op_psize;
364 	else
365 		sa -> x25_opts.op_psize = lcp -> lcd_packetsize;
366 	if (sa -> x25_opts.op_wsize)
367 		lcp -> lcd_windowsize = sa -> x25_opts.op_wsize;
368 	else
369 		sa -> x25_opts.op_wsize = lcp -> lcd_windowsize;
370 	sa -> x25_net = pkp -> pk_xcp -> xc_addr.x25_net;
371 	lcp -> lcd_flags = sa -> x25_opts.op_flags;
372 	lcp -> lcd_stime = time.tv_sec;
373 }
374 
375 pk_connect (lcp, sa)
376 register struct pklcd *lcp;
377 register struct sockaddr_x25 *sa;
378 {
379 	register struct pkcb *pkp;
380 
381 	if (sa -> x25_addr[0] == '\0')
382 		return (EDESTADDRREQ);
383 	if (lcp -> lcd_pkp == 0)
384 	    for (pkp = pkcbhead; ; pkp = pkp -> pk_next) {
385 		if (pkp == 0)
386 			return (ENETUNREACH);
387 		/*
388 		 * use first net configured (last in list
389 		 * headed by pkcbhead) if net is zero
390 		 */
391 		if (sa -> x25_net == 0 && pkp -> pk_next == 0)
392 			break;
393 		if (sa -> x25_net == pkp -> pk_xcp -> xc_addr.x25_net)
394 			break;
395 	}
396 
397 	if (pkp -> pk_state != DTE_READY)
398 		return (ENETDOWN);
399 	if ((lcp -> lcd_lcn = pk_getlcn (pkp)) == 0)
400 		return (EMFILE);
401 	lcp -> lcd_faddr = *sa;
402 	lcp -> lcd_ceaddr = & lcp -> lcd_faddr;
403 	pk_assoc (pkp, lcp, lcp -> lcd_ceaddr);
404 	if (lcp -> lcd_so)
405 		soisconnecting (lcp -> lcd_so);
406 	lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_CALL);
407 	pk_callrequest (lcp, lcp -> lcd_ceaddr, pkp -> pk_xcp);
408 	return (*pkp -> pk_start) (lcp);
409 }
410 
411 /*
412  *  Build the rest of the CALL REQUEST packet. Fill in calling
413  *  address, facilities fields and the user data field.
414  */
415 
416 pk_callrequest (lcp, sa, xcp)
417 struct pklcd *lcp;
418 register struct sockaddr_x25 *sa;
419 register struct x25config *xcp;
420 {
421 	register struct x25_calladdr *a;
422 	register struct mbuf *m = lcp -> lcd_template;
423 	register struct x25_packet *xp = mtod (m, struct x25_packet *);
424 	unsigned posn = 0;
425 	octet *cp;
426 
427 	if (lcp -> lcd_flags & X25_DBIT)
428 		xp -> d_bit = 1;
429 	a = (struct x25_calladdr *) &xp -> packet_data;
430 	a -> calling_addrlen = strlen (xcp -> xc_addr.x25_addr);
431 	a -> called_addrlen = strlen (sa -> x25_addr);
432 	cp = (octet *) a -> address_field;
433 	to_bcd (&cp, (int)a -> called_addrlen, sa -> x25_addr, &posn);
434 	to_bcd (&cp, (int)a -> calling_addrlen, xcp -> xc_addr.x25_addr, &posn);
435 	if (posn & 0x01)
436 		*cp++ &= 0xf0;
437 	m -> m_pkthdr.len = m -> m_len += cp - (octet *) a;
438 
439 	if (lcp -> lcd_facilities) {
440 		m -> m_pkthdr.len +=
441 			(m -> m_next = lcp -> lcd_facilities) -> m_len;
442 		lcp -> lcd_facilities = 0;
443 	} else
444 		build_facilities (m, sa, (int)xcp -> xc_type);
445 
446 	m_copyback (m, m -> m_pkthdr.len, sa -> x25_udlen, sa -> x25_udata);
447 #ifdef ANDREW
448 	printf ("call: ");
449 	for (cp = mtod (m, octet *), posn = 0; posn < m -> m_len; ++posn)
450 		printf ("%x ", *cp++);
451 	printf ("\n");
452 #endif
453 }
454 
455 static
456 build_facilities (m, sa, type)
457 register struct mbuf *m;
458 struct sockaddr_x25 *sa;
459 {
460 	register octet *cp;
461 	register octet *fcp;
462 	register int revcharge;
463 
464 	cp = mtod (m, octet *) + m -> m_len;
465 	fcp = cp + 1;
466 	revcharge = sa -> x25_opts.op_flags & X25_REVERSE_CHARGE ? 1 : 0;
467 	/*
468 	 * This is specific to Datapac X.25(1976) DTEs.  International
469 	 * calls must have the "hi priority" bit on.
470 	 */
471 	if (type == X25_1976 && sa -> x25_opts.op_psize == X25_PS128)
472 		revcharge |= 02;
473 	if (revcharge) {
474 		*fcp++ = FACILITIES_REVERSE_CHARGE;
475 		*fcp++ = revcharge;
476 	}
477 	switch (type) {
478 	case X25_1980:
479 	case X25_1984:
480 		*fcp++ = FACILITIES_PACKETSIZE;
481 		*fcp++ = sa -> x25_opts.op_psize;
482 		*fcp++ = sa -> x25_opts.op_psize;
483 
484 		*fcp++ = FACILITIES_WINDOWSIZE;
485 		*fcp++ = sa -> x25_opts.op_wsize;
486 		*fcp++ = sa -> x25_opts.op_wsize;
487 	}
488 	*cp = fcp - cp - 1;
489 	m -> m_pkthdr.len = (m -> m_len += *cp + 1);
490 }
491 
492 to_bcd (a, len, x, posn)
493 register octet **a;
494 register char *x;
495 register int len;
496 register unsigned *posn;
497 {
498 	while (--len >= 0)
499 		if ((*posn)++ & 0x01)
500 			*(*a)++ |= *x++ & 0x0F;
501 		else
502 			**a = *x++ << 4;
503 }
504 
505 /*
506  *  This routine gets the  first available logical channel number.  The
507  *  search is from the highest number to lowest number (DTE).
508  */
509 
510 pk_getlcn (pkp)
511 register struct pkcb *pkp;
512 {
513 	register int i;
514 
515 	if (pkp -> pk_chan == 0)
516 		return (0);
517 	for (i = pkp -> pk_maxlcn; i > 0; --i)
518 		if (pkp -> pk_chan[i] == NULL)
519 			break;
520 	return (i);
521 
522 }
523 
524 /*
525  *  This procedure sends a CLEAR request packet. The lc state is
526  *  set to "SENT_CLEAR".
527  */
528 
529 pk_clear (lcp, diagnostic, abortive)
530 register struct pklcd *lcp;
531 {
532 	register struct mbuf *m = pk_template (lcp -> lcd_lcn, X25_CLEAR);
533 
534 	m -> m_len += 2;
535 	mtod (m, struct x25_packet *) -> packet_data = 0;
536 	mtod (m, octet *)[4] = diagnostic;
537 	if (lcp -> lcd_facilities) {
538 		m -> m_next = lcp -> lcd_facilities;
539 		m -> m_pkthdr.len += m -> m_next -> m_len;
540 		lcp -> lcd_facilities = 0;
541 	}
542 	if (abortive)
543 		lcp -> lcd_template = m;
544 	else {
545 		struct socket *so = lcp -> lcd_so;
546 		struct sockbuf *sb = so ? & so -> so_snd : & lcp -> lcd_sb;
547 		sbappendrecord (sb, m);
548 	}
549 	pk_output (lcp);
550 
551 }
552 
553 /*
554  * This procedure generates RNR's or RR's to inhibit or enable
555  * inward data flow, if the current state changes (blocked ==> open or
556  * vice versa), or if forced to generate one.  One forces RNR's to ack data.
557  */
558 pk_flowcontrol (lcp, inhibit, forced)
559 register struct pklcd *lcp;
560 {
561 	inhibit = (inhibit != 0);
562 	if (lcp == 0 || lcp -> lcd_state != DATA_TRANSFER ||
563 	    (forced == 0 && lcp -> lcd_rxrnr_condition == inhibit))
564 		return;
565 	lcp -> lcd_rxrnr_condition = inhibit;
566 	lcp -> lcd_template = pk_template (lcp -> lcd_lcn, inhibit ? RNR : RR);
567 	pk_output (lcp);
568 }
569 
570 /*
571  *  This procedure sends a RESET request packet. It re-intializes
572  *  virtual circuit.
573  */
574 
575 static
576 pk_reset (lcp, diagnostic)
577 register struct pklcd *lcp;
578 {
579 	register struct mbuf *m;
580 	register struct socket *so = lcp -> lcd_so;
581 
582 	if (lcp -> lcd_state != DATA_TRANSFER)
583 		return;
584 
585 	if (so)
586 		so -> so_error = ECONNRESET;
587 	lcp -> lcd_reset_condition = TRUE;
588 
589 	/* Reset all the control variables for the channel. */
590 	pk_flush (lcp);
591 	lcp -> lcd_window_condition = lcp -> lcd_rnr_condition =
592 		lcp -> lcd_intrconf_pending = FALSE;
593 	lcp -> lcd_rsn = MODULUS - 1;
594 	lcp -> lcd_ssn = 0;
595 	lcp -> lcd_output_window = lcp -> lcd_input_window =
596 		lcp -> lcd_last_transmitted_pr = 0;
597 	m = lcp -> lcd_template = pk_template (lcp -> lcd_lcn, X25_RESET);
598 	m -> m_pkthdr.len = m -> m_len += 2;
599 	mtod (m, struct x25_packet *) -> packet_data = 0;
600 	mtod (m, octet *)[4] = diagnostic;
601 	pk_output (lcp);
602 
603 }
604 
605 /*
606  * This procedure frees all data queued for output or delivery on a
607  *  virtual circuit.
608  */
609 
610 pk_flush (lcp)
611 register struct pklcd *lcp;
612 {
613 	register struct socket *so;
614 
615 	if (lcp -> lcd_template)
616 		m_freem (lcp -> lcd_template);
617 
618 	if (lcp -> lcd_cps) {
619 		m_freem (lcp -> lcd_cps);
620 		lcp -> lcd_cps = 0;
621 	}
622 	if (lcp -> lcd_facilities) {
623 		m_freem (lcp -> lcd_facilities);
624 		lcp -> lcd_facilities = 0;
625 	}
626 	if (so = lcp -> lcd_so) {
627 		sbflush (&so -> so_rcv);
628 		sbflush (&so -> so_snd);
629 	} else
630 		sbflush (&lcp -> lcd_sb);
631 }
632 
633 /*
634  *  This procedure handles all local protocol procedure errors.
635  */
636 
637 pk_procerror (error, lcp, errstr, diagnostic)
638 register struct pklcd *lcp;
639 char *errstr;
640 {
641 
642 	pk_message (lcp -> lcd_lcn, lcp -> lcd_pkp -> pk_xcp, errstr);
643 
644 	switch (error) {
645 	case CLEAR:
646 		if (lcp -> lcd_so) {
647 			lcp -> lcd_so -> so_error = ECONNABORTED;
648 			soisdisconnecting (lcp -> lcd_so);
649 		}
650 		pk_clear (lcp, diagnostic, 1);
651 		break;
652 
653 	case RESET:
654 		pk_reset (lcp, diagnostic);
655 	}
656 }
657 
658 /*
659  *  This procedure is called during the DATA TRANSFER state to check
660  *  and  process  the P(R) values  received  in the DATA,  RR OR RNR
661  *  packets.
662  */
663 
664 pk_ack (lcp, pr)
665 struct pklcd *lcp;
666 unsigned pr;
667 {
668 	register struct socket *so = lcp -> lcd_so;
669 
670 	if (lcp -> lcd_output_window == pr)
671 		return (PACKET_OK);
672 	if (lcp -> lcd_output_window < lcp -> lcd_ssn) {
673 		if (pr < lcp -> lcd_output_window || pr > lcp -> lcd_ssn) {
674 			pk_procerror (RESET, lcp,
675 				"p(r) flow control error", 2);
676 			return (ERROR_PACKET);
677 		}
678 	}
679 	else {
680 		if (pr < lcp -> lcd_output_window && pr > lcp -> lcd_ssn) {
681 			pk_procerror (RESET, lcp,
682 				"p(r) flow control error #2", 2);
683 			return (ERROR_PACKET);
684 		}
685 	}
686 
687 	lcp -> lcd_output_window = pr;		/* Rotate window. */
688 	if (lcp -> lcd_window_condition == TRUE)
689 		lcp -> lcd_window_condition = FALSE;
690 
691 	if (so && ((so -> so_snd.sb_flags & SB_WAIT) || so -> so_snd.sb_sel))
692 		sowwakeup (so);
693 
694 	return (PACKET_OK);
695 }
696 
697 /*
698  *  This procedure decodes the X.25 level 3 packet returning a
699  *  code to be used in switchs or arrays.
700  */
701 
702 pk_decode (xp)
703 register struct x25_packet *xp;
704 {
705 	register int type;
706 
707 	if (xp -> fmt_identifier != 1)
708 		return (INVALID_PACKET);
709 #ifdef ancient_history
710 	/*
711 	 *  Make sure that the logical channel group number is 0.
712 	 *  This restriction may be removed at some later date.
713 	 */
714 	if (xp -> lc_group_number != 0)
715 		return (INVALID_PACKET);
716 #endif
717 	/*
718 	 *  Test for data packet first.
719 	 */
720 	if (!(xp -> packet_type & DATA_PACKET_DESIGNATOR))
721 		return (DATA);
722 
723 	/*
724 	 *  Test if flow control packet (RR or RNR).
725 	 */
726 	if (!(xp -> packet_type & RR_OR_RNR_PACKET_DESIGNATOR))
727 		switch (xp -> packet_type & 0x1f) {
728 		case X25_RR:
729 			return (RR);
730 		case X25_RNR:
731 			return (RNR);
732 		case X25_REJECT:
733 			return (REJECT);
734 		}
735 
736 	/*
737 	 *  Determine the rest of the packet types.
738 	 */
739 	switch (xp -> packet_type) {
740 	case X25_CALL:
741 		type = CALL;
742 		break;
743 
744 	case X25_CALL_ACCEPTED:
745 		type = CALL_ACCEPTED;
746 		break;
747 
748 	case X25_CLEAR:
749 		type = CLEAR;
750 		break;
751 
752 	case X25_CLEAR_CONFIRM:
753 		type = CLEAR_CONF;
754 		break;
755 
756 	case X25_INTERRUPT:
757 		type = INTERRUPT;
758 		break;
759 
760 	case X25_INTERRUPT_CONFIRM:
761 		type = INTERRUPT_CONF;
762 		break;
763 
764 	case X25_RESET:
765 		type = RESET;
766 		break;
767 
768 	case X25_RESET_CONFIRM:
769 		type = RESET_CONF;
770 		break;
771 
772 	case X25_RESTART:
773 		type = RESTART;
774 		break;
775 
776 	case X25_RESTART_CONFIRM:
777 		type = RESTART_CONF;
778 		break;
779 
780 	case X25_DIAGNOSTIC:
781 		type = DIAG_TYPE;
782 		break;
783 
784 	default:
785 		type = INVALID_PACKET;
786 	}
787 	return (type);
788 }
789 
790 /*
791  *  A restart packet has been received. Print out the reason
792  *  for the restart.
793  */
794 
795 pk_restartcause (pkp, xp)
796 struct pkcb *pkp;
797 register struct x25_packet *xp;
798 {
799 	register struct x25config *xcp = pkp -> pk_xcp;
800 	register int lcn = LCN(xp);
801 
802 	switch (xp -> packet_data) {
803 	case X25_RESTART_LOCAL_PROCEDURE_ERROR:
804 		pk_message (lcn, xcp, "restart: local procedure error");
805 		break;
806 
807 	case X25_RESTART_NETWORK_CONGESTION:
808 		pk_message (lcn, xcp, "restart: network congestion");
809 		break;
810 
811 	case X25_RESTART_NETWORK_OPERATIONAL:
812 		pk_message (lcn, xcp, "restart: network operational");
813 		break;
814 
815 	default:
816 		pk_message (lcn, xcp, "restart: unknown cause");
817 	}
818 }
819 
820 #define MAXRESETCAUSE	7
821 
822 int     Reset_cause[] = {
823 	EXRESET, EXROUT, 0, EXRRPE, 0, EXRLPE, 0, EXRNCG
824 };
825 
826 /*
827  *  A reset packet has arrived. Return the cause to the user.
828  */
829 
830 pk_resetcause (pkp, xp)
831 struct pkcb *pkp;
832 register struct x25_packet *xp;
833 {
834 	register struct pklcd *lcp =
835 				pkp -> pk_chan[LCN(xp)];
836 	register int code = xp -> packet_data;
837 
838 	if (code > MAXRESETCAUSE)
839 		code = 7;	/* EXRNCG */
840 
841 	pk_message(LCN(xp), lcp -> lcd_pkp, "reset code 0x%x, diagnostic 0x%x",
842 			xp -> packet_data, 4[(u_char *)xp]);
843 
844 	lcp -> lcd_so -> so_error = Reset_cause[code];
845 }
846 
847 #define MAXCLEARCAUSE	25
848 
849 int     Clear_cause[] = {
850 	EXCLEAR, EXCBUSY, 0, EXCINV, 0, EXCNCG, 0,
851 	0, 0, EXCOUT, 0, EXCAB, 0, EXCNOB, 0, 0, 0, EXCRPE,
852 	0, EXCLPE, 0, 0, 0, 0, 0, EXCRRC
853 };
854 
855 /*
856  *  A clear packet has arrived. Return the cause to the user.
857  */
858 
859 pk_clearcause (pkp, xp)
860 struct pkcb *pkp;
861 register struct x25_packet *xp;
862 {
863 	register struct pklcd *lcp =
864 		pkp -> pk_chan[LCN(xp)];
865 	register int code = xp -> packet_data;
866 
867 	if (code > MAXCLEARCAUSE)
868 		code = 5;	/* EXRNCG */
869 	if (lcp -> lcd_so)
870 		lcp -> lcd_so -> so_error = Clear_cause[code];
871 }
872 
873 char *
874 format_ntn (xcp)
875 register struct x25config *xcp;
876 {
877 
878 	return (xcp -> xc_addr.x25_addr);
879 }
880 
881 /* VARARGS1 */
882 pk_message (lcn, xcp, fmt, a1, a2, a3, a4, a5, a6)
883 struct x25config *xcp;
884 char *fmt;
885 {
886 
887 	if (lcn)
888 		if (pkcbhead -> pk_next)
889 			printf ("X.25(%s): lcn %d: ", format_ntn (xcp), lcn);
890 		else
891 			printf ("X.25: lcn %d: ", lcn);
892 	else
893 		if (pkcbhead -> pk_next)
894 			printf ("X.25(%s): ", format_ntn (xcp));
895 		else
896 			printf ("X.25: ");
897 
898 	printf (fmt, a1, a2, a3, a4, a5, a6);
899 	printf ("\n");
900 }
901 
902 pk_ifattach (ia, lloutput, llnext)
903 register struct x25_ifaddr *ia;
904 int (*lloutput) ();
905 caddr_t llnext;
906 {
907 	/* this is here because you can't include both pk_var and hd_var */
908 	/* this will probably be replace by a streams gluing mechanism */
909 	ia -> ia_pkcb.pk_lloutput = lloutput;
910 	ia -> ia_pkcb.pk_llnext = llnext;
911 }
912 
913 pk_fragment (lcp, m0, qbit, mbit, wait)
914 struct mbuf *m0;
915 register struct pklcd *lcp;
916 {
917 	register struct mbuf *m = m0;
918 	register struct x25_packet *xp;
919 	register struct sockbuf *sb;
920 	struct mbuf *head = 0, *next, **mp = &head, *m_split ();
921 	int totlen, psize = 1 << (lcp -> lcd_packetsize);
922 
923 	if (m == 0)
924 		return 0;
925 	if (m -> m_flags & M_PKTHDR == 0)
926 		panic ("pk_fragment");
927 	totlen = m -> m_pkthdr.len;
928 	m -> m_act = 0;
929 	sb = lcp -> lcd_so ? &lcp -> lcd_so -> so_snd : & lcp -> lcd_sb;
930 	do {
931 		if (totlen > psize) {
932 			if ((next = m_split (m, psize, wait)) == 0)
933 				goto abort;
934 			totlen -= psize;
935 		} else
936 			next = 0;
937 		M_PREPEND(m, PKHEADERLN, wait);
938 		if (m == 0)
939 			goto abort;
940 		*mp = m;
941 		mp = & m -> m_act;
942 		*mp = 0;
943 		xp = mtod (m, struct x25_packet *);
944 		0[(char *)xp] = 0;
945 		if (qbit)
946 			xp -> q_bit = 1;
947 		if (lcp -> lcd_flags & X25_DBIT)
948 			xp -> d_bit = 1;
949 		xp -> fmt_identifier = 1;
950 		xp -> packet_type = X25_DATA;
951 		SET_LCN(xp, lcp -> lcd_lcn);
952 		if (next || (mbit && (totlen == psize ||
953 				      (lcp -> lcd_flags & X25_DBIT))))
954 			MBIT(xp) = 1;
955 	} while (m = next);
956 	for (m = head; m; m = next) {
957 		next = m -> m_act;
958 		m -> m_act = 0;
959 		sbappendrecord (sb, m);
960 	}
961 	return 0;
962 abort:
963 	if (wait)
964 		panic ("pk_fragment null mbuf after wait");
965 	if (next)
966 		m_freem (next);
967 	for (m = head; m; m = next) {
968 		next = m -> m_act;
969 		m_freem (m);
970 	}
971 	return ENOBUFS;
972 }
973 
974 struct mbuf *
975 m_split (m0, len0, wait)
976 register struct mbuf *m0;
977 int len0;
978 {
979 	register struct mbuf *m, *n;
980 	unsigned len = len0;
981 
982 	for (m = m0; m && len > m -> m_len; m = m -> m_next)
983 		len -= m -> m_len;
984 	if (m == 0)
985 		return (0);
986 	if (m0 -> m_flags & M_PKTHDR) {
987 		MGETHDR(n, wait, m0 -> m_type);
988 		if (n == 0)
989 			return (0);
990 		n -> m_pkthdr.rcvif = m0 -> m_pkthdr.rcvif;
991 		n -> m_pkthdr.len = m0 -> m_pkthdr.len - len0;
992 		m0 -> m_pkthdr.len = len0;
993 		if (m -> m_flags & M_EXT)
994 			goto extpacket;
995 		if (len > MHLEN) {
996 			/* m can't be the lead packet */
997 			MH_ALIGN(n, 0);
998 			n -> m_next = m_split (m, len, wait);
999 			if (n -> m_next == 0) {
1000 				(void) m_free (n);
1001 				return (0);
1002 			} else
1003 				return (n);
1004 		} else
1005 			MH_ALIGN(n, len);
1006 	} else if (len == m -> m_len) {
1007 		n = m -> m_next;
1008 		m -> m_next = 0;
1009 		return (n);
1010 	}
1011 extpacket:
1012 	len = m -> m_len - len;		/* remainder to be copied */
1013 	m -> m_len -= len;		/* now equals original len */
1014 	if (m -> m_flags & M_EXT) {
1015 		n -> m_flags |= M_EXT;
1016 		n -> m_ext = m -> m_ext;
1017 		mclrefcnt[mtocl (m -> m_ext.ext_buf)]++;
1018 		n -> m_data = m -> m_data + m -> m_len;
1019 	} else {
1020 		MGET(n, wait, m -> m_type);
1021 		if (n == 0) {
1022 			m -> m_len += len;
1023 			return (0);
1024 		}
1025 		M_ALIGN(n, len);
1026 		bcopy (mtod (m, caddr_t), mtod (n, caddr_t), len);
1027 	}
1028 	n -> m_len = len;
1029 	n -> m_next = m -> m_next;
1030 	m -> m_next = 0;
1031 	return (n);
1032 }
1033