summaryrefslogtreecommitdiff
path: root/sys/netccitt/pk_output.c
blob: 5619407181d14e89e56ef42c1fc18e41a58fe778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*	$OpenBSD: pk_output.c,v 1.7 2003/12/10 07:22:43 itojun Exp $	*/
/*	$NetBSD: pk_output.c,v 1.7 1996/02/13 22:05:30 christos Exp $	*/

/*
 * Copyright(c) University of British Columbia, 1984
 * Copyright(C) Computer Science Department IV,
 * 		 University of Erlangen-Nuremberg, Germany, 1992
 * Copyright(c) 1991, 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by the
 * Laboratory for Computation Vision and the Computer Science Department
 * of the the University of British Columbia and the Computer Science
 * Department(IV) of the University of Erlangen-Nuremberg, Germany.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)pk_output.c	8.1(Berkeley) 6/10/93
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/errno.h>

#include <net/if.h>

#include <netccitt/x25.h>
#include <netccitt/pk.h>
#include <netccitt/pk_var.h>
#include <netccitt/pk_extern.h>

struct mbuf_cache pk_output_cache = {0}, pk_input_cache;

void
pk_output(lcp)
	struct pklcd *lcp;
{
	struct x25_packet *xp;
	struct mbuf *m;
	struct pkcb *pkp = lcp->lcd_pkp;

	if (lcp == 0 || pkp == 0) {
		printf("pk_output: zero arg\n");
		return;
	}
	while ((m = nextpk(lcp)) != NULL) {
		xp = mtod(m, struct x25_packet *);

		switch (pk_decode(xp) + lcp->lcd_state) {
			/*
			 * All the work is already done - just set the state
			 * and pass to peer.
			 */
		case PK_CALL + READY:
			lcp->lcd_state = SENT_CALL;
			lcp->lcd_timer = pk_t21;
			break;

			/*
			 *  Just set the state to allow packet to flow and send the
			 *  confirmation.
			 */
		case PK_CALL_ACCEPTED + RECEIVED_CALL:
			lcp->lcd_state = DATA_TRANSFER;
			break;

			/*
			 * Just set the state. Keep the LCD around till the
			 * clear confirmation is returned.
			 */
		case PK_CLEAR + RECEIVED_CALL:
		case PK_CLEAR + SENT_CALL:
		case PK_CLEAR + DATA_TRANSFER:
			lcp->lcd_state = SENT_CLEAR;
			lcp->lcd_retry = 0;
			/* fall through */

		case PK_CLEAR + SENT_CLEAR:
			lcp->lcd_timer = pk_t23;
			lcp->lcd_retry++;
			break;

		case PK_CLEAR_CONF + RECEIVED_CLEAR:
		case PK_CLEAR_CONF + SENT_CLEAR:
		case PK_CLEAR_CONF + READY:
			lcp->lcd_state = READY;
			break;

		case PK_DATA + DATA_TRANSFER:
			SPS(xp, lcp->lcd_ssn);
			lcp->lcd_input_window =
				(lcp->lcd_rsn + 1) % MODULUS;
			SPR(xp, lcp->lcd_input_window);
			lcp->lcd_last_transmitted_pr = lcp->lcd_input_window;
			lcp->lcd_ssn = (lcp->lcd_ssn + 1) % MODULUS;
			if (lcp->lcd_ssn == ((lcp->lcd_output_window + lcp->lcd_windowsize) % MODULUS))
				lcp->lcd_window_condition = TRUE;
			break;

		case PK_INTERRUPT + DATA_TRANSFER:
#ifdef ancient_history
			xp->packet_data = 0;
#endif
			lcp->lcd_intrconf_pending = TRUE;
			break;

		case PK_INTERRUPT_CONF + DATA_TRANSFER:
			break;

		case PK_RR + DATA_TRANSFER:
		case PK_RNR + DATA_TRANSFER:
			lcp->lcd_input_window =
				(lcp->lcd_rsn + 1) % MODULUS;
			SPR(xp, lcp->lcd_input_window);
			lcp->lcd_last_transmitted_pr = lcp->lcd_input_window;
			break;

		case PK_RESET + DATA_TRANSFER:
			lcp->lcd_reset_condition = TRUE;
			break;

		case PK_RESET_CONF + DATA_TRANSFER:
			lcp->lcd_reset_condition = FALSE;
			break;

			/*
			 * A restart should be only generated internally.
			 * Therefore all logic for restart is in the
			 * pk_restart routine.
			 */
		case PK_RESTART + READY:
			lcp->lcd_timer = pk_t20;
			break;

			/*
			 * Restarts are all  handled internally.  Therefore
			 * all the logic for the incoming restart packet is
			 * handled in  the pk_input routine.
			 */
		case PK_RESTART_CONF + READY:
			break;

		default:
			m_freem(m);
			return;
		}

		/* Trace the packet. */
		pk_trace(pkp->pk_xcp, m, "P-Out");

		/* Pass the packet on down to the link layer */
		if (pk_input_cache.mbc_size || pk_input_cache.mbc_oldsize) {
			m->m_flags |= 0x08;
			mbuf_cache(&pk_input_cache, m);
		}
		(*pkp->pk_lloutput) (m, pkp->pk_llnext, pkp->pk_rt);
	}
}

/*
 * This procedure returns the next packet to send or null. A packet is
 * composed of one or more mbufs.
 */

struct mbuf *
nextpk(lcp)
	struct pklcd   *lcp;
{
	struct mbuf *m, *n;
	struct socket  *so = lcp->lcd_so;
	struct sockbuf *sb = (so ? &so->so_snd : &lcp->lcd_sb);

	if (lcp->lcd_template) {
		m = lcp->lcd_template;
		lcp->lcd_template = NULL;
	} else {
		if (lcp->lcd_rnr_condition || lcp->lcd_window_condition ||
		    lcp->lcd_reset_condition)
			return (NULL);

		if ((m = sb->sb_mb) == 0)
			return (NULL);

		sb->sb_mb = m->m_nextpkt;
		SB_EMPTY_FIXUP(sb);
		m->m_nextpkt = 0;
		for (n = m; n; n = n->m_next)
			sbfree(sb, n);
	}
	return (m);
}