xref: /csrg-svn/sys/netiso/tp_emit.c (revision 36399)
1*36399Ssklower /***********************************************************
2*36399Ssklower 		Copyright IBM Corporation 1987
3*36399Ssklower 
4*36399Ssklower                       All Rights Reserved
5*36399Ssklower 
6*36399Ssklower Permission to use, copy, modify, and distribute this software and its
7*36399Ssklower documentation for any purpose and without fee is hereby granted,
8*36399Ssklower provided that the above copyright notice appear in all copies and that
9*36399Ssklower both that copyright notice and this permission notice appear in
10*36399Ssklower supporting documentation, and that the name of IBM not be
11*36399Ssklower used in advertising or publicity pertaining to distribution of the
12*36399Ssklower software without specific, written prior permission.
13*36399Ssklower 
14*36399Ssklower IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15*36399Ssklower ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16*36399Ssklower IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17*36399Ssklower ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18*36399Ssklower WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19*36399Ssklower ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*36399Ssklower SOFTWARE.
21*36399Ssklower 
22*36399Ssklower ******************************************************************/
23*36399Ssklower 
24*36399Ssklower /*
25*36399Ssklower  * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison
26*36399Ssklower  */
27*36399Ssklower /*
28*36399Ssklower  * ARGO TP
29*36399Ssklower  *
30*36399Ssklower  * $Header: tp_emit.c,v 5.5 88/11/18 17:27:20 nhall Exp $
31*36399Ssklower  * $Source: /usr/argo/sys/netiso/RCS/tp_emit.c,v $
32*36399Ssklower  *
33*36399Ssklower  * This file contains tp_emit() and tp_error_emit(), which
34*36399Ssklower  * form TPDUs and hand them to ip.
35*36399Ssklower  * They take data in the form of mbuf chain, allocate mbufs as
36*36399Ssklower  * necessary for headers, and set the fields as appropriate from
37*36399Ssklower  * information found in the tpcb and net-level pcb.
38*36399Ssklower  *
39*36399Ssklower  * The worst thing about this code is adding the variable-length
40*36399Ssklower  * options on a machine that requires alignment for any memory access
41*36399Ssklower  * that isn't of size 1.  See the macro ADDOPTION() below.
42*36399Ssklower  *
43*36399Ssklower  * We don't do any concatenation. (There's a kludge to test the
44*36399Ssklower  * basic mechanism of separation under the 'w' tpdebug option, that's all.)
45*36399Ssklower  */
46*36399Ssklower 
47*36399Ssklower #ifndef lint
48*36399Ssklower static char *rcsid = "$Header: tp_emit.c,v 5.5 88/11/18 17:27:20 nhall Exp $";
49*36399Ssklower #endif lint
50*36399Ssklower 
51*36399Ssklower 
52*36399Ssklower #include "argoxtwentyfive.h"
53*36399Ssklower #include "param.h"
54*36399Ssklower #include "mbuf.h"
55*36399Ssklower #include "socket.h"
56*36399Ssklower #include "socketvar.h"
57*36399Ssklower #include "protosw.h"
58*36399Ssklower #include "errno.h"
59*36399Ssklower #include "types.h"
60*36399Ssklower #include "time.h"
61*36399Ssklower #include "../netiso/iso.h"
62*36399Ssklower #include "../netiso/argo_debug.h"
63*36399Ssklower #include "../netiso/tp_timer.h"
64*36399Ssklower #include "../netiso/tp_param.h"
65*36399Ssklower #include "../netiso/tp_stat.h"
66*36399Ssklower #include "../netiso/tp_pcb.h"
67*36399Ssklower #include "../netiso/tp_tpdu.h"
68*36399Ssklower #include "../netiso/tp_trace.h"
69*36399Ssklower #include "../netiso/tp_meas.h"
70*36399Ssklower #include "../netiso/tp_seq.h"
71*36399Ssklower #include "../netiso/iso_errno.h"
72*36399Ssklower 
73*36399Ssklower void iso_gen_csum();
74*36399Ssklower 
75*36399Ssklower 
76*36399Ssklower /* Here is a mighty kludge.  The token ring misorders packets if you
77*36399Ssklower  * fire them at it too fast, and TP sans checksum is "too fast", so
78*36399Ssklower  * we have introduced a delay when checksumming isn't used.
79*36399Ssklower  */
80*36399Ssklower char tp_delay = 0x00; /* delay to keep token ring from blowing it */
81*36399Ssklower 
82*36399Ssklower /*
83*36399Ssklower  * NAME:	tp_emit()
84*36399Ssklower  *
85*36399Ssklower  * CALLED FROM: tp.trans and from tp_sbsend()
86*36399Ssklower  *
87*36399Ssklower  * FUNCTION and ARGUMENTS:
88*36399Ssklower  * 	Emits one tpdu of the type (dutype), of the format appropriate
89*36399Ssklower  * 	to the connection described by the pcb (tpcb), with sequence
90*36399Ssklower  * 	number (seq) (where appropriate), end-of-tsdu bit (eot) where
91*36399Ssklower  * 	appropriate, and with the data in the mbuf chain (data).
92*36399Ssklower  * 	For DR and ER tpdus, the argument (eot) is
93*36399Ssklower  * 	the reason for issuing the tpdu rather than an end-of-tsdu indicator.
94*36399Ssklower  *
95*36399Ssklower  * RETURNS:
96*36399Ssklower  * 	0  OK
97*36399Ssklower  * 	ENOBUFS
98*36399Ssklower  * 	E* returned from net layer output rtn
99*36399Ssklower  *
100*36399Ssklower  * SIDE EFFECTS:
101*36399Ssklower  *
102*36399Ssklower  * NOTES:
103*36399Ssklower  *
104*36399Ssklower  * 	WE ASSUME that the tp header + all options will fit in ONE mbuf.
105*36399Ssklower  *	If mbufs are 256 this will most likely be true, but if they are 128 it's
106*36399Ssklower  *	possible that they won't.
107*36399Ssklower  *	If you used every option on the CR + max. user data you'd overrun
108*36399Ssklower  *	112 but unless you used > 115 bytes for the security
109*36399Ssklower  *	parameter, it would fit in a 256-byte mbuf (240 bytes for the header)
110*36399Ssklower  *	We don't support the security parameter, so this isn't a problem.
111*36399Ssklower  *	If security is added, we ought to remove this assumption.
112*36399Ssklower  *
113*36399Ssklower  *  We do not implement the flow control confirmation "element of procedure".
114*36399Ssklower  *  A) it should not affect interoperability,
115*36399Ssklower  *  B) it should not be necessary - the protocol will eventually
116*36399Ssklower  *   	straighten things out w/o FCC, as long as we don't have severely
117*36399Ssklower  *		mismatched keepalive and inactivity timers, and
118*36399Ssklower  *	C) it appears not to be REQUIRED, and
119*36399Ssklower  *  D) it's incredibly grotesque, and no doubt will lengthen a few
120*36399Ssklower  *   	critical paths.
121*36399Ssklower  *  HOWEVER, we're thinking about putting it in anyway, for
122*36399Ssklower  *  completeness, just like we did with ack subsequencing.
123*36399Ssklower  */
124*36399Ssklower 
125*36399Ssklower int
126*36399Ssklower tp_emit(dutype,	tpcb, seq, eot, data)
127*36399Ssklower 	int dutype;
128*36399Ssklower 	struct tp_pcb *tpcb;
129*36399Ssklower 	SeqNum	seq;
130*36399Ssklower 	u_int 	eot;
131*36399Ssklower 	struct mbuf *data;
132*36399Ssklower {
133*36399Ssklower 	register struct tpdu *hdr;
134*36399Ssklower 	register struct mbuf *m;
135*36399Ssklower 	int csum_offset=0;
136*36399Ssklower 	int datalen = 0;
137*36399Ssklower 	int error = 0;
138*36399Ssklower 
139*36399Ssklower 	/* NOTE:
140*36399Ssklower 	 * here we treat tpdu_li as if it DID include the li field, up until
141*36399Ssklower 	 * the end, at which time we subtract 1
142*36399Ssklower 	 * THis is because if we subtract 1 right away, we end up adding
143*36399Ssklower 	 * one every time we add an option.
144*36399Ssklower 	 */
145*36399Ssklower 	IFDEBUG(D_EMIT)
146*36399Ssklower 		printf(
147*36399Ssklower 	"tp_emit dutype 0x%x, tpcb 0x%x, eot 0x%x, seq 0x%x, data 0x%x mfree 0x%x",
148*36399Ssklower 		dutype, tpcb, eot, seq, data, mfree);
149*36399Ssklower 	ENDDEBUG
150*36399Ssklower 
151*36399Ssklower 	MGET(m, M_DONTWAIT, TPMT_TPHDR);
152*36399Ssklower 	if (m == NULL) {
153*36399Ssklower 		if(data != (struct mbuf *)0)
154*36399Ssklower 			m_freem(data);
155*36399Ssklower 		error = ENOBUFS;
156*36399Ssklower 		goto done;
157*36399Ssklower 	}
158*36399Ssklower 	m->m_len = sizeof(struct tpdu);
159*36399Ssklower 	m->m_act = MNULL;
160*36399Ssklower 
161*36399Ssklower 	hdr = mtod(m, struct tpdu *);
162*36399Ssklower 	bzero(hdr, sizeof(struct tpdu));
163*36399Ssklower 
164*36399Ssklower 	{
165*36399Ssklower 		int 	tp_headersize();
166*36399Ssklower 
167*36399Ssklower 		hdr->tpdu_type = dutype;
168*36399Ssklower 		hdr->tpdu_li = tp_headersize(dutype, tpcb);
169*36399Ssklower 		/*
170*36399Ssklower 		 * class 0 doesn't use this for DT
171*36399Ssklower 		 * it'll just get overwritten below
172*36399Ssklower 		 */
173*36399Ssklower 		hdr->tpdu_dref = htons(tpcb->tp_fref);
174*36399Ssklower 		if( tpcb->tp_use_checksum ||
175*36399Ssklower 			(dutype == CR_TPDU_type && (tpcb->tp_class & TP_CLASS_4) )) {
176*36399Ssklower 			csum_offset =  hdr->tpdu_li + 2; /* DOESN'T include csum */
177*36399Ssklower 			ADDOPTION(TPP_checksum, hdr, 2, eot /* dummy arg */);
178*36399Ssklower 			IFDEBUG(D_CHKSUM)
179*36399Ssklower 				printf(
180*36399Ssklower 					"tp_emit: csum_offset 0x%x, hdr->tpdu_li 0x%x\n",
181*36399Ssklower 						csum_offset, hdr->tpdu_li);
182*36399Ssklower 			ENDDEBUG
183*36399Ssklower 		}
184*36399Ssklower 		/*
185*36399Ssklower 		 * VARIABLE PARTS...
186*36399Ssklower 		 */
187*36399Ssklower 		switch( dutype ) {
188*36399Ssklower 
189*36399Ssklower 		case CR_TPDU_type:
190*36399Ssklower 			hdr->tpdu_CRdref_0 = htons(0);	/* must be zero */
191*36399Ssklower 
192*36399Ssklower 		case CC_TPDU_type:
193*36399Ssklower 				{
194*36399Ssklower 					u_char x;
195*36399Ssklower 
196*36399Ssklower 				hdr->tpdu_CCsref =  htons(tpcb->tp_lref); /* same as CRsref */
197*36399Ssklower 
198*36399Ssklower 				if( tpcb->tp_class > TP_CLASS_1 ) {
199*36399Ssklower 					LOCAL_CREDIT( tpcb );
200*36399Ssklower 					tpcb->tp_sent_uwe = tpcb->tp_lcredit -1;
201*36399Ssklower 					tpcb->tp_sent_rcvnxt = 1;
202*36399Ssklower 					tpcb->tp_sent_lcdt = tpcb->tp_lcredit;
203*36399Ssklower 					hdr->tpdu_cdt = tpcb->tp_lcredit;
204*36399Ssklower 				} else {
205*36399Ssklower 					hdr->tpdu_cdt = 0;
206*36399Ssklower 				}
207*36399Ssklower 				hdr->tpdu_CCclass = tp_mask_to_num(tpcb->tp_class);
208*36399Ssklower 				hdr->tpdu_CCoptions =
209*36399Ssklower 					(tpcb->tp_xtd_format? TPO_XTD_FMT:0) |
210*36399Ssklower 					(tpcb->tp_use_efc? TPO_USE_EFC:0);
211*36399Ssklower 
212*36399Ssklower 				IFPERF(tpcb)
213*36399Ssklower 					u_char perf_meas = tpcb->tp_perf_on;
214*36399Ssklower 					ADDOPTION(TPP_perf_meas, hdr, sizeof(perf_meas), perf_meas);
215*36399Ssklower 				ENDPERF
216*36399Ssklower 
217*36399Ssklower 				if( dutype == CR_TPDU_type ) {
218*36399Ssklower 					IncStat(ts_CR_sent);
219*36399Ssklower 
220*36399Ssklower 					ASSERT( tpcb->tp_lsuffixlen > 0 );
221*36399Ssklower 					ASSERT( tpcb->tp_fsuffixlen > 0 );
222*36399Ssklower 
223*36399Ssklower 					ADDOPTION(TPP_calling_sufx, hdr,
224*36399Ssklower 						tpcb->tp_lsuffixlen, tpcb->tp_lsuffix);
225*36399Ssklower 					ADDOPTION(TPP_called_sufx, hdr,
226*36399Ssklower 						tpcb->tp_fsuffixlen, tpcb->tp_fsuffix);
227*36399Ssklower 				} else {
228*36399Ssklower 					IncStat(ts_CC_sent);
229*36399Ssklower 				}
230*36399Ssklower 
231*36399Ssklower 				ADDOPTION(TPP_tpdu_size, hdr,
232*36399Ssklower 					sizeof(tpcb->tp_tpdusize), tpcb->tp_tpdusize);
233*36399Ssklower 
234*36399Ssklower 				if (tpcb->tp_class != TP_CLASS_0) {
235*36399Ssklower 					short millisec = 500*(tpcb->tp_sendack_ticks);
236*36399Ssklower 
237*36399Ssklower 					millisec = htons(millisec);
238*36399Ssklower 					ADDOPTION(TPP_acktime, hdr, sizeof(short), millisec);
239*36399Ssklower 
240*36399Ssklower 					x = (tpcb->tp_use_nxpd? TPAO_USE_NXPD: 0)
241*36399Ssklower 					 |	(tpcb->tp_use_rcc?  TPAO_USE_RCC : 0)
242*36399Ssklower 					 |  (tpcb->tp_use_checksum?0: TPAO_NO_CSUM)
243*36399Ssklower 					 |	(tpcb->tp_xpd_service? TPAO_USE_TXPD: 0);
244*36399Ssklower 					ADDOPTION(TPP_addl_opt, hdr, 1, x);
245*36399Ssklower 
246*36399Ssklower 				}
247*36399Ssklower 
248*36399Ssklower 				if( (dutype == CR_TPDU_type) && (tpcb->tp_class != TP_CLASS_0)){
249*36399Ssklower 
250*36399Ssklower 					ASSERT( 1 == sizeof(tpcb->tp_vers) );
251*36399Ssklower 					ADDOPTION(TPP_vers, hdr, 1, tpcb->tp_vers);
252*36399Ssklower 
253*36399Ssklower 					/* for each alt protocol class x,
254*36399Ssklower 					 * 	x = x<<4;
255*36399Ssklower 					 *  option = concat(option, x);
256*36399Ssklower 					 * Well, for now we only have TP0 for an
257*36399Ssklower 					 * alternative so... this is easy.
258*36399Ssklower 					 *
259*36399Ssklower 					 * HOWEVER... There should be NO alt protocol
260*36399Ssklower 					 * class over CLNS.  Need to see if the route suggests
261*36399Ssklower 					 * CONS, and iff so add alt class.
262*36399Ssklower 					 */
263*36399Ssklower 					x = 0;
264*36399Ssklower 					ADDOPTION(TPP_alt_class, hdr, 1, x);
265*36399Ssklower 				}
266*36399Ssklower 
267*36399Ssklower 				if( hdr->tpdu_li > MLEN)
268*36399Ssklower 					panic("tp_emit CR/CC");
269*36399Ssklower 			}
270*36399Ssklower 			break;
271*36399Ssklower 
272*36399Ssklower 		case DR_TPDU_type:
273*36399Ssklower 			if( hdr->tpdu_DRdref == 0 ) {
274*36399Ssklower 				/* don't issue the DR */
275*36399Ssklower 				goto done;
276*36399Ssklower 			}
277*36399Ssklower 			hdr->tpdu_cdt = 0;
278*36399Ssklower 			hdr->tpdu_DRsref = htons(tpcb->tp_lref);
279*36399Ssklower 			hdr->tpdu_DRreason = (u_char)eot; /* WHICH BYTE OF THIS??? */
280*36399Ssklower 
281*36399Ssklower 			/* forget the add'l information variable part */
282*36399Ssklower 			IncStat(ts_DR_sent);
283*36399Ssklower 			break;
284*36399Ssklower 
285*36399Ssklower 		case DC_TPDU_type: /* not used in class 0 */
286*36399Ssklower 			ASSERT( tpcb->tp_class != TP_CLASS_0);
287*36399Ssklower 			hdr->tpdu_DCsref =  htons(tpcb->tp_lref);
288*36399Ssklower 			hdr->tpdu_cdt = 0;
289*36399Ssklower 			data = (struct mbuf *)0;
290*36399Ssklower 			IncStat(ts_DC_sent);
291*36399Ssklower 			break;
292*36399Ssklower 
293*36399Ssklower 		case XAK_TPDU_type: /* xak not used in class 0 */
294*36399Ssklower 			ASSERT( tpcb->tp_class != TP_CLASS_0); /* fall through */
295*36399Ssklower 			hdr->tpdu_cdt = 0;
296*36399Ssklower 
297*36399Ssklower 			IFTRACE(D_XPD)
298*36399Ssklower 				tptraceTPCB(TPPTXack, seq, 0, 0, 0, 0);
299*36399Ssklower 			ENDTRACE
300*36399Ssklower 			data = (struct mbuf *)0;
301*36399Ssklower 			if (tpcb->tp_xtd_format) {
302*36399Ssklower #ifdef BYTE_ORDER
303*36399Ssklower 				hdr->tpdu_XAKseqX = htonl(seq);
304*36399Ssklower #else
305*36399Ssklower 				hdr->tpdu_XAKseqX = seq;
306*36399Ssklower #endif BYTE_ORDER
307*36399Ssklower 			} else {
308*36399Ssklower 				hdr->tpdu_XAKseq = seq;
309*36399Ssklower 			}
310*36399Ssklower 			IncStat(ts_XAK_sent);
311*36399Ssklower 			IncPStat(tpcb, tps_XAK_sent);
312*36399Ssklower 			break;
313*36399Ssklower 
314*36399Ssklower 		case XPD_TPDU_type: /* xpd not used in class 0 */
315*36399Ssklower 			ASSERT( tpcb->tp_class != TP_CLASS_0); /* fall through */
316*36399Ssklower 			hdr->tpdu_cdt = 0;
317*36399Ssklower 			if (tpcb->tp_xtd_format) {
318*36399Ssklower #ifdef BYTE_ORDER
319*36399Ssklower 				union seq_type seqeotX;
320*36399Ssklower 
321*36399Ssklower 				seqeotX.s_seq = seq;
322*36399Ssklower 				seqeotX.s_eot = 1;
323*36399Ssklower 				hdr->tpdu_seqeotX = htonl(seqeotX.s_seqeot);
324*36399Ssklower #else
325*36399Ssklower 				hdr->tpdu_XPDseqX = seq;
326*36399Ssklower 				hdr->tpdu_XPDeotX = 1; /* always 1 for XPD tpdu */
327*36399Ssklower #endif BYTE_ORDER
328*36399Ssklower 			} else {
329*36399Ssklower 				hdr->tpdu_XPDseq = seq;
330*36399Ssklower 				hdr->tpdu_XPDeot = 1; /* always 1 for XPD tpdu */
331*36399Ssklower 			}
332*36399Ssklower 			IncStat(ts_XPD_sent);
333*36399Ssklower 			IncPStat(tpcb, tps_XPD_sent);
334*36399Ssklower 
335*36399Ssklower 			/* kludge to test the input size checking */
336*36399Ssklower 			IFDEBUG(D_SIZE_CHECK)
337*36399Ssklower 				if(data->m_len <= 16 && data->m_off < (MLEN-18) ) {
338*36399Ssklower 					printf("Sending too much data on XPD: 18 bytes\n");
339*36399Ssklower 					data->m_len = 18;
340*36399Ssklower 				}
341*36399Ssklower 			ENDDEBUG
342*36399Ssklower 			break;
343*36399Ssklower 
344*36399Ssklower 		case DT_TPDU_type:
345*36399Ssklower 			hdr->tpdu_cdt = 0;
346*36399Ssklower 			IFTRACE(D_DATA)
347*36399Ssklower 				tptraceTPCB(TPPTmisc, "emit DT: eot seq tpdu_li", eot, seq,
348*36399Ssklower 					hdr->tpdu_li, 0);
349*36399Ssklower 			ENDTRACE
350*36399Ssklower 			if (tpcb->tp_xtd_format) {
351*36399Ssklower #ifdef BYTE_ORDER
352*36399Ssklower 				union seq_type seqeotX;
353*36399Ssklower 
354*36399Ssklower 				seqeotX.s_seq = seq;
355*36399Ssklower 				seqeotX.s_eot = eot;
356*36399Ssklower 				hdr->tpdu_seqeotX = htonl(seqeotX.s_seqeot);
357*36399Ssklower #else
358*36399Ssklower 				hdr->tpdu_DTseqX = seq;
359*36399Ssklower 				hdr->tpdu_DTeotX = eot;
360*36399Ssklower #endif BYTE_ORDER
361*36399Ssklower 			} else if (tpcb->tp_class == TP_CLASS_0) {
362*36399Ssklower 				IFDEBUG(D_EMIT)
363*36399Ssklower 					printf("DT tpdu: class 0 m 0x%x hdr 0x%x\n", m, hdr);
364*36399Ssklower 					dump_buf( hdr, hdr->tpdu_li + 1 );
365*36399Ssklower 				ENDDEBUG
366*36399Ssklower 				((struct tp0du *)hdr)->tp0du_eot = eot;
367*36399Ssklower 				((struct tp0du *)hdr)->tp0du_mbz = 0;
368*36399Ssklower 				IFDEBUG(D_EMIT)
369*36399Ssklower 					printf("DT 2 tpdu: class 0 m 0x%x hdr 0x%x\n", m, hdr);
370*36399Ssklower 					dump_buf( hdr, hdr->tpdu_li + 1 );
371*36399Ssklower 				ENDDEBUG
372*36399Ssklower 			} else {
373*36399Ssklower 				hdr->tpdu_DTseq = seq;
374*36399Ssklower 				hdr->tpdu_DTeot = eot;
375*36399Ssklower 			}
376*36399Ssklower 			if(eot) {
377*36399Ssklower 				IncStat(ts_EOT_sent);
378*36399Ssklower 			}
379*36399Ssklower 			IncStat(ts_DT_sent);
380*36399Ssklower 			IncPStat(tpcb, tps_DT_sent);
381*36399Ssklower 			break;
382*36399Ssklower 
383*36399Ssklower 		case AK_TPDU_type:/* ak not used in class 0 */
384*36399Ssklower 			ASSERT( tpcb->tp_class != TP_CLASS_0);
385*36399Ssklower 			data = (struct mbuf *)0;
386*36399Ssklower 			{ 	SeqNum olduwe = tpcb->tp_sent_uwe;
387*36399Ssklower 
388*36399Ssklower 				tpcb->tp_sent_uwe =
389*36399Ssklower 					SEQ(tpcb,tpcb->tp_rcvnxt + tpcb->tp_lcredit -1);
390*36399Ssklower 				LOCAL_CREDIT( tpcb );
391*36399Ssklower 				tpcb->tp_sent_lcdt = tpcb->tp_lcredit;
392*36399Ssklower 
393*36399Ssklower 				IFDEBUG(D_RENEG)
394*36399Ssklower 					/* occasionally fake a reneging so
395*36399Ssklower 						you can test subsequencing */
396*36399Ssklower 					if( olduwe & 0x1 ) {
397*36399Ssklower 						tpcb->tp_reneged = 1;
398*36399Ssklower 						IncStat(ts_ldebug);
399*36399Ssklower 					}
400*36399Ssklower 				ENDDEBUG
401*36399Ssklower 				/* Are we about to reneg on credit?
402*36399Ssklower 				 * When might we do so?
403*36399Ssklower 				 *	a) when using optimistic credit (which we no longer do).
404*36399Ssklower 				 *  b) when drain() gets implemented (not in the plans).
405*36399Ssklower 				 *  c) when D_RENEG is on.
406*36399Ssklower 				 *  d) when DEC BIT response (PRC_QUENCH2) is implemented.
407*36399Ssklower 				 *	(not- when we do this, we'll need to implement flow control
408*36399Ssklower 				 *	confirmation)
409*36399Ssklower 				 */
410*36399Ssklower 				if( SEQ_LT(tpcb, tpcb->tp_sent_uwe, olduwe) ) {
411*36399Ssklower 					tpcb->tp_reneged = 1;
412*36399Ssklower 					IncStat(ts_lcdt_reduced);
413*36399Ssklower 					IFTRACE(D_CREDIT)
414*36399Ssklower 						tptraceTPCB(TPPTmisc,
415*36399Ssklower 							"RENEG: olduwe newuwe lcredit rcvnxt",
416*36399Ssklower 							olduwe,
417*36399Ssklower 							tpcb->tp_sent_uwe, tpcb->tp_lcredit,
418*36399Ssklower 							tpcb->tp_rcvnxt);
419*36399Ssklower 					ENDTRACE
420*36399Ssklower 				}
421*36399Ssklower 
422*36399Ssklower 				IFPERF(tpcb)
423*36399Ssklower 					/* new lwe is less than old uwe means we're
424*36399Ssklower 					 * acking before we received a whole window full
425*36399Ssklower 					 */
426*36399Ssklower 					if( SEQ_LT( tpcb, tpcb->tp_rcvnxt, olduwe) ) {
427*36399Ssklower 						/* tmp1 = number of pkts fewer than the full window */
428*36399Ssklower 						register int tmp1 =
429*36399Ssklower 							(int) SEQ_SUB( tpcb, olduwe, tpcb->tp_rcvnxt);
430*36399Ssklower 
431*36399Ssklower 						if(tmp1 > TP_PM_MAX)
432*36399Ssklower 							tmp1 = TP_PM_MAX;
433*36399Ssklower 						IncPStat( tpcb,  tps_ack_early[tmp1] );
434*36399Ssklower 
435*36399Ssklower 						/* tmp1 = amt of new cdt we're advertising */
436*36399Ssklower 						tmp1 = SEQ_SUB( tpcb, seq, tpcb->tp_sent_rcvnxt);
437*36399Ssklower 						if(tmp1 > TP_PM_MAX )
438*36399Ssklower 							tmp1 = TP_PM_MAX;
439*36399Ssklower 
440*36399Ssklower 						IncPStat( tpcb,
441*36399Ssklower 								tps_cdt_acked [ tmp1 ]
442*36399Ssklower 								[ ((tpcb->tp_lcredit > TP_PM_MAX)?
443*36399Ssklower 									TP_PM_MAX:tpcb->tp_lcredit) ] );
444*36399Ssklower 
445*36399Ssklower 					}
446*36399Ssklower 				ENDPERF
447*36399Ssklower 			}
448*36399Ssklower 			IFTRACE(D_ACKSEND)
449*36399Ssklower 				tptraceTPCB(TPPTack, seq, tpcb->tp_lcredit, tpcb->tp_sent_uwe,
450*36399Ssklower 					tpcb->tp_r_subseq, 0);
451*36399Ssklower 			ENDTRACE
452*36399Ssklower 			if (tpcb->tp_xtd_format) {
453*36399Ssklower #ifdef BYTE_ORDER
454*36399Ssklower 				hdr->tpdu_cdt = 0;
455*36399Ssklower 				hdr->tpdu_AKseqX = htonl(seq);
456*36399Ssklower 				hdr->tpdu_AKcdtX = htons(tpcb->tp_lcredit);
457*36399Ssklower #else
458*36399Ssklower 				hdr->tpdu_cdt = 0;
459*36399Ssklower 				hdr->tpdu_AKseqX = seq;
460*36399Ssklower 				hdr->tpdu_AKcdtX = tpcb->tp_lcredit;
461*36399Ssklower #endif BYTE_ORDER
462*36399Ssklower 			} else {
463*36399Ssklower 				hdr->tpdu_AKseq = seq;
464*36399Ssklower 				hdr->tpdu_AKcdt = tpcb->tp_lcredit;
465*36399Ssklower 			}
466*36399Ssklower 			if ((tpcb->tp_class == TP_CLASS_4) && tpcb->tp_reneged ) {
467*36399Ssklower 				/*
468*36399Ssklower 				 * Ack subsequence parameter req'd if WE reneged on
469*36399Ssklower 				 * credit offered.  (ISO 8073, 12.2.3.8.2, p. 74)
470*36399Ssklower 				 */
471*36399Ssklower 				IFDEBUG(D_RENEG)
472*36399Ssklower 					printf("Adding subseq 0x%x\n", tpcb->tp_s_subseq);
473*36399Ssklower 				ENDDEBUG
474*36399Ssklower 				tpcb->tp_s_subseq++;
475*36399Ssklower 				/*
476*36399Ssklower 				 * add tmp subseq and do a htons on it.
477*36399Ssklower 				 */
478*36399Ssklower 				ADDOPTION(TPP_subseq, hdr,
479*36399Ssklower 					sizeof(tpcb->tp_s_subseq), tpcb->tp_s_subseq);
480*36399Ssklower 			} else
481*36399Ssklower 				tpcb->tp_s_subseq = 0;
482*36399Ssklower 
483*36399Ssklower 			if ( tpcb->tp_sendfcc || eot ) /* overloaded to mean SEND FCC */ {
484*36399Ssklower 				/*
485*36399Ssklower 				 * Rules for sending FCC ("should" send when) :
486*36399Ssklower 				 * %a) received an ack from peer with NO NEWS whatsoever,
487*36399Ssklower 				 *  	and it did not contain an FCC
488*36399Ssklower 				 * 	b) received an ack from peer that opens its closed window.
489*36399Ssklower 				 * 	c) received an ack from peer after it reneged on its
490*36399Ssklower 				 *		offered credit, AND this ack raises UWE but LWE is same
491*36399Ssklower 				 *		and below UWE at time of reneging (reduction)
492*36399Ssklower 				 * Now, ISO 8073 12.2.3.8.3 says
493*36399Ssklower 				 * that a retransmitted AK shall not contain the FCC
494*36399Ssklower 				 * parameter.  Now, how the hell you tell the difference
495*36399Ssklower 				 * between a retransmitted ack and an ack that's sent in
496*36399Ssklower 				 * response to a received ack, I don't know, because without
497*36399Ssklower 				 * any local activity, and w/o any received DTs, they
498*36399Ssklower 				 * will contain exactly the same credit/seq# information.
499*36399Ssklower 				 * Anyway, given that the "retransmission of acks"
500*36399Ssklower 				 * procedure (ISO 8073 12.2.3.8.3) is optional, and we
501*36399Ssklower 				 * don't do it (although the peer can't tell that), we
502*36399Ssklower 				 * ignore this last rule.
503*36399Ssklower 				 *
504*36399Ssklower 				 * We send FCC for reasons a) and b) only.
505*36399Ssklower 				 * To add reason c) would require a ridiculous amount of state.
506*36399Ssklower 				 *
507*36399Ssklower 				 */
508*36399Ssklower 				u_short 	bogus[4]; /* lwe(32), subseq(16), cdt(16) */
509*36399Ssklower 				SeqNum		lwe;
510*36399Ssklower 				u_short		subseq, fcredit;
511*36399Ssklower 
512*36399Ssklower 				tpcb->tp_sendfcc = 0;
513*36399Ssklower 
514*36399Ssklower 				lwe = (SeqNum) htonl(tpcb->tp_snduna);
515*36399Ssklower 				subseq = htons(tpcb->tp_r_subseq);
516*36399Ssklower 				fcredit = htons(tpcb->tp_fcredit);
517*36399Ssklower 
518*36399Ssklower 				bcopy((caddr_t) &lwe, &bogus[0], sizeof(SeqNum));
519*36399Ssklower 				bcopy((caddr_t) &subseq, &bogus[2], sizeof(u_short));
520*36399Ssklower 				bcopy((caddr_t) &fcredit, &bogus[3], sizeof(u_short));
521*36399Ssklower 
522*36399Ssklower 				IFTRACE(D_ACKSEND)
523*36399Ssklower 					tptraceTPCB(TPPTmisc,
524*36399Ssklower 						"emit w/FCC: snduna r_subseq fcredit",
525*36399Ssklower 						tpcb->tp_snduna, tpcb->tp_r_subseq,
526*36399Ssklower 						tpcb->tp_fcredit, 0);
527*36399Ssklower 				ENDTRACE
528*36399Ssklower 
529*36399Ssklower 				IFDEBUG(D_ACKSEND)
530*36399Ssklower 					printf("Calling ADDOPTION 0x%x, 0x%x, 0x%x,0x%x\n",
531*36399Ssklower 						TPP_flow_cntl_conf,
532*36399Ssklower 						hdr, sizeof(bogus), bogus[0]);
533*36399Ssklower 				ENDDEBUG
534*36399Ssklower 				ADDOPTION(TPP_flow_cntl_conf, hdr, sizeof(bogus), bogus[0]);
535*36399Ssklower 				IFDEBUG(D_ACKSEND)
536*36399Ssklower 					printf("after ADDOPTION hdr 0x%x hdr->tpdu_li 0x%x\n",
537*36399Ssklower 						hdr, hdr->tpdu_li);
538*36399Ssklower 					printf(
539*36399Ssklower 					"after ADDOPTION csum_offset 0x%x, hdr->tpdu_li 0x%x\n",
540*36399Ssklower 							csum_offset, hdr->tpdu_li);
541*36399Ssklower 				ENDDEBUG
542*36399Ssklower 
543*36399Ssklower 			}
544*36399Ssklower 			tpcb->tp_reneged = 0;
545*36399Ssklower 			tpcb->tp_sent_rcvnxt = seq;
546*36399Ssklower 			tp_ctimeout(tpcb->tp_refp, TM_sendack,
547*36399Ssklower 				(int)tpcb->tp_keepalive_ticks);
548*36399Ssklower 			IncStat(ts_AK_sent);
549*36399Ssklower 			IncPStat(tpcb, tps_AK_sent);
550*36399Ssklower 			IFDEBUG(D_ACKSEND)
551*36399Ssklower 				printf(
552*36399Ssklower 				"2 after rADDOPTION csum_offset 0x%x, hdr->tpdu_li 0x%x\n",
553*36399Ssklower 						csum_offset, hdr->tpdu_li);
554*36399Ssklower 			ENDDEBUG
555*36399Ssklower 			break;
556*36399Ssklower 
557*36399Ssklower 		case ER_TPDU_type:
558*36399Ssklower 			hdr->tpdu_ERreason = eot;
559*36399Ssklower 			hdr->tpdu_cdt = 0;
560*36399Ssklower 			/* no user data */
561*36399Ssklower 			data = (struct mbuf *)0;
562*36399Ssklower 			IncStat(ts_ER_sent);
563*36399Ssklower 			break;
564*36399Ssklower 		}
565*36399Ssklower 
566*36399Ssklower 	}
567*36399Ssklower 	ASSERT( ((int)hdr->tpdu_li > 0) && ((int)hdr->tpdu_li < MLEN) );
568*36399Ssklower 
569*36399Ssklower 	m->m_next = data;
570*36399Ssklower 
571*36399Ssklower 	ASSERT( hdr->tpdu_li < MMAXOFF ); /* leave this in */
572*36399Ssklower 	ASSERT( hdr->tpdu_li != 0 ); /* leave this in */
573*36399Ssklower 
574*36399Ssklower 	m->m_len = hdr->tpdu_li ;
575*36399Ssklower 	hdr->tpdu_li --; /* doesn't include the li field */
576*36399Ssklower 
577*36399Ssklower 	datalen = m_datalen( m ); /* total len */
578*36399Ssklower 
579*36399Ssklower 	ASSERT( datalen <= tpcb->tp_l_tpdusize ); /* may become a problem
580*36399Ssklower 				when CLNP is used; leave in here for the time being */
581*36399Ssklower 		IFDEBUG(D_ACKSEND)
582*36399Ssklower 			printf(
583*36399Ssklower 			"4 after rADDOPTION csum_offset 0x%x, hdr->tpdu_li 0x%x\n",
584*36399Ssklower 					csum_offset, hdr->tpdu_li);
585*36399Ssklower 		ENDDEBUG
586*36399Ssklower 	if( datalen > tpcb->tp_l_tpdusize ) {
587*36399Ssklower 		printf("data len 0x%x tpcb->tp_l_tpdusize 0x%x\n",
588*36399Ssklower 			datalen, tpcb->tp_l_tpdusize);
589*36399Ssklower 	}
590*36399Ssklower 	IFDEBUG(D_EMIT)
591*36399Ssklower 		printf(
592*36399Ssklower 		"tp_emit before gen_csum m_len 0x%x, csum_offset 0x%x, datalen 0x%x\n",
593*36399Ssklower 		m->m_len, csum_offset, datalen);
594*36399Ssklower 	ENDDEBUG
595*36399Ssklower 	if( tpcb->tp_use_checksum ||
596*36399Ssklower 		(dutype == CR_TPDU_type && (tpcb->tp_class & TP_CLASS_4)) ) {
597*36399Ssklower 		iso_gen_csum(m, csum_offset, datalen);
598*36399Ssklower 	}
599*36399Ssklower 
600*36399Ssklower 	IFDEBUG(D_EMIT)
601*36399Ssklower 	printf("tp_emit before tpxxx_output tpcb 0x%x, dutype 0x%x, datalen 0x%x\n",
602*36399Ssklower 		tpcb, dutype, datalen);
603*36399Ssklower 		dump_buf( m, datalen+12);
604*36399Ssklower 	ENDDEBUG
605*36399Ssklower 
606*36399Ssklower 	IFPERF(tpcb)
607*36399Ssklower 		if( dutype == DT_TPDU_type ) {
608*36399Ssklower 			PStat(tpcb, Nb_to_ll) += (datalen - m->m_len);
609*36399Ssklower 			tpmeas( tpcb->tp_lref, TPtime_to_ll,  0,
610*36399Ssklower 				seq, PStat(tpcb, Nb_to_ll), (datalen - m->m_len));
611*36399Ssklower 		}
612*36399Ssklower 	ENDPERF
613*36399Ssklower 
614*36399Ssklower 	IFTRACE(D_EMIT)
615*36399Ssklower 		tptraceTPCB(TPPTtpduout, dutype, hdr, hdr->tpdu_li+1, datalen, 0);
616*36399Ssklower 	ENDTRACE
617*36399Ssklower 	IFDEBUG(D_EMIT)
618*36399Ssklower 		printf("OUTPUT: tpcb 0x%x, isop 0x%x, so 0x%x\n",
619*36399Ssklower 			tpcb,  tpcb->tp_npcb,  tpcb->tp_sock);
620*36399Ssklower 	ENDDEBUG
621*36399Ssklower 
622*36399Ssklower 	{ extern char tp_delay;
623*36399Ssklower 
624*36399Ssklower 		if( tp_delay )
625*36399Ssklower 			if( tpcb->tp_use_checksum == 0 ) {
626*36399Ssklower 				register u_int i  = tp_delay;
627*36399Ssklower 				for (; i!= 0; i--)
628*36399Ssklower 					(void) iso_check_csum(m, datalen);
629*36399Ssklower 			}
630*36399Ssklower 	}
631*36399Ssklower 	ASSERT( m->m_len > 0 );
632*36399Ssklower 	error = (tpcb->tp_nlproto->nlp_output)(tpcb->tp_npcb, m, datalen,
633*36399Ssklower 		!tpcb->tp_use_checksum);
634*36399Ssklower 	IFDEBUG(D_EMIT)
635*36399Ssklower 		printf("OUTPUT: returned 0x%x\n", error);
636*36399Ssklower 	ENDDEBUG
637*36399Ssklower 	IFTRACE(D_EMIT)
638*36399Ssklower 		tptraceTPCB(TPPTmisc,
639*36399Ssklower 			"tp_emit nlproto->output netservice returns datalen",
640*36399Ssklower 			tpcb->tp_nlproto->nlp_output, tpcb->tp_netservice, error, datalen);
641*36399Ssklower 	ENDTRACE
642*36399Ssklower done:
643*36399Ssklower 	if( error == E_CO_QFULL ) {
644*36399Ssklower 		tp_quench( tpcb );
645*36399Ssklower 		return 0;
646*36399Ssklower 	}
647*36399Ssklower 	return error;
648*36399Ssklower }
649*36399Ssklower 
650*36399Ssklower /*
651*36399Ssklower  * NAME:		tp_error_emit()
652*36399Ssklower  * CALLED FROM:	tp_input() when a DR or ER is to be issued in
653*36399Ssklower  * 		response to an input error.
654*36399Ssklower  * FUNCTION and ARGUMENTS:
655*36399Ssklower  * 		The error type is the first argument.
656*36399Ssklower  * 		The argument (sref) is the source reference on the bad incoming tpdu,
657*36399Ssklower  * 		and is used for a destination reference on the outgoing packet.
658*36399Ssklower  * 		(faddr) and (laddr) are the foreign and local addresses for this
659*36399Ssklower  *		connection.
660*36399Ssklower  * 		(erdata) is a ptr to the errant incoming tpdu, and is copied into the
661*36399Ssklower  * 		outgoing ER, if an ER is to be issued.
662*36399Ssklower  * 		(erlen)  is the number of octets of the errant tpdu that we should
663*36399Ssklower  * 		try to copy.
664*36399Ssklower  * 		(tpcb) is the pcb that describes the connection for which the bad tpdu
665*36399Ssklower  * 		arrived.
666*36399Ssklower  * RETURN VALUES:
667*36399Ssklower  * 		0 OK
668*36399Ssklower  *  	ENOBUFS
669*36399Ssklower  *  	E* from net layer datagram output routine
670*36399Ssklower  * SIDE EFFECTS:
671*36399Ssklower  *
672*36399Ssklower  * NOTES:
673*36399Ssklower  */
674*36399Ssklower 
675*36399Ssklower int
676*36399Ssklower tp_error_emit(error, sref, faddr, laddr, erdata, erlen, tpcb, cons_channel,
677*36399Ssklower 	dgout_routine)
678*36399Ssklower 	int				error;
679*36399Ssklower 	u_long			sref;
680*36399Ssklower 	struct sockaddr_iso *faddr, *laddr;
681*36399Ssklower 	struct mbuf 	*erdata;
682*36399Ssklower 	int 			erlen;
683*36399Ssklower 	struct tp_pcb 	*tpcb;
684*36399Ssklower 	int 			cons_channel;
685*36399Ssklower 	int				(*dgout_routine)();
686*36399Ssklower {
687*36399Ssklower 	int						dutype;
688*36399Ssklower 	int 					datalen = 0;
689*36399Ssklower 	register struct tpdu	*hdr;
690*36399Ssklower 	register struct mbuf	*m;
691*36399Ssklower 	int						csum_offset;
692*36399Ssklower 
693*36399Ssklower 	IFTRACE(D_ERROR_EMIT)
694*36399Ssklower 		tptrace(TPPTmisc, "tp_error_emit error sref tpcb erlen",
695*36399Ssklower 			error, sref, tpcb, erlen);
696*36399Ssklower 	ENDTRACE
697*36399Ssklower 	IFDEBUG(D_ERROR_EMIT)
698*36399Ssklower 		printf(
699*36399Ssklower 		"tp_error_emit error 0x%x sref 0x%x tpcb 0x%x erlen 0x%x chan 0x%x\n",
700*36399Ssklower 			error, sref, tpcb, erlen, cons_channel);
701*36399Ssklower 	ENDDEBUG
702*36399Ssklower 
703*36399Ssklower 	MGET(m, M_DONTWAIT, TPMT_TPHDR);
704*36399Ssklower 	if (m == NULL) {
705*36399Ssklower 		return ENOBUFS;
706*36399Ssklower 	}
707*36399Ssklower 	m->m_len = sizeof(struct tpdu);
708*36399Ssklower 	m->m_act = MNULL;
709*36399Ssklower 
710*36399Ssklower 	hdr = mtod(m, struct tpdu *);
711*36399Ssklower 
712*36399Ssklower 	IFDEBUG(D_ERROR_EMIT)
713*36399Ssklower 		printf("[error 0x%x] [error&0xff  0x%x] [(char)error 0x%x]\n",
714*36399Ssklower 			error, error&0xff, (char)error);
715*36399Ssklower 	ENDDEBUG
716*36399Ssklower 
717*36399Ssklower 	error &= 0xff;
718*36399Ssklower 
719*36399Ssklower 	if( error & 0x40 ) {
720*36399Ssklower 		error &= ~0x40;
721*36399Ssklower 		dutype = ER_TPDU_type;
722*36399Ssklower 	} else
723*36399Ssklower 		dutype = DR_TPDU_type;
724*36399Ssklower 
725*36399Ssklower 	hdr->tpdu_type = dutype;
726*36399Ssklower 	hdr->tpdu_cdt = 0;
727*36399Ssklower 
728*36399Ssklower 	switch( dutype ) {
729*36399Ssklower 
730*36399Ssklower 	case DR_TPDU_type:
731*36399Ssklower 		IncStat(ts_DR_sent);
732*36399Ssklower 		hdr->tpdu_li = 7;
733*36399Ssklower 		hdr->tpdu_DRdref = htons(sref);
734*36399Ssklower 		hdr->tpdu_DRsref = htons(0);
735*36399Ssklower 		hdr->tpdu_DRreason = (char)error;
736*36399Ssklower 		IFDEBUG(D_ERROR_EMIT)
737*36399Ssklower 			printf("DR case:\n");
738*36399Ssklower 			dump_buf( hdr, 7);
739*36399Ssklower 		ENDDEBUG
740*36399Ssklower 		/* forget the add'l information variable part */
741*36399Ssklower 		break;
742*36399Ssklower 
743*36399Ssklower 	case ER_TPDU_type:
744*36399Ssklower 		IncStat(ts_ER_sent);
745*36399Ssklower 		hdr->tpdu_li = 5;
746*36399Ssklower 		hdr->tpdu_ERreason = (char)error;
747*36399Ssklower 		break;
748*36399Ssklower 
749*36399Ssklower 	default:
750*36399Ssklower 		ASSERT(0);
751*36399Ssklower 		printf("TP PANIC: bad dutype 0x%x\n", dutype);
752*36399Ssklower 	}
753*36399Ssklower 
754*36399Ssklower 	if(tpcb)
755*36399Ssklower 		if( tpcb->tp_use_checksum ) {
756*36399Ssklower 			ADDOPTION(TPP_checksum, hdr, 2, csum_offset /* dummy argument */);
757*36399Ssklower 			csum_offset =  hdr->tpdu_li - 2;
758*36399Ssklower 		}
759*36399Ssklower 
760*36399Ssklower 	ASSERT( hdr->tpdu_li < MMAXOFF );
761*36399Ssklower 
762*36399Ssklower 	if (dutype == ER_TPDU_type) {
763*36399Ssklower 		/* copy the errant tpdu into another 'variable part' */
764*36399Ssklower 		register caddr_t P;
765*36399Ssklower 
766*36399Ssklower 		IFTRACE(D_ERROR_EMIT)
767*36399Ssklower 			tptrace(TPPTmisc, "error_emit ER len tpduli", erlen, hdr->tpdu_li,
768*36399Ssklower 				0,0);
769*36399Ssklower 		ENDTRACE
770*36399Ssklower 		IFDEBUG(D_ERROR_EMIT)
771*36399Ssklower 			printf("error_emit ER len 0x%x tpduli 0x%x\n", erlen, hdr->tpdu_li);
772*36399Ssklower 		ENDDEBUG
773*36399Ssklower 
774*36399Ssklower 		/* copy at most as many octets for which you have room */
775*36399Ssklower 		if (erlen + hdr->tpdu_li + 2 > TP_MAX_HEADER_LEN)
776*36399Ssklower 			erlen = TP_MAX_HEADER_LEN - hdr->tpdu_li - 2;
777*36399Ssklower 
778*36399Ssklower 		/* add the "invalid tpdu" parameter : required in class 0 */
779*36399Ssklower 		P = (caddr_t)hdr + (int)(hdr->tpdu_li);
780*36399Ssklower 		vbptr(P)->tpv_code =  TPP_invalid_tpdu; /* parameter code */
781*36399Ssklower 		vbptr(P)->tpv_len = erlen;	/* parameter length */
782*36399Ssklower 		m->m_len = hdr->tpdu_li + 2; /* 1 for code, 1 for length */
783*36399Ssklower 
784*36399Ssklower 		/* tp_input very likely handed us an mbuf chain w/ nothing in
785*36399Ssklower 		 * the first mbuf and the data following the empty mbuf
786*36399Ssklower 		 */
787*36399Ssklower 		if(erdata->m_len == 0) {
788*36399Ssklower 			erdata = m_free(erdata); /* returns the next mbuf on the chain */
789*36399Ssklower 		}
790*36399Ssklower 		m->m_next = m_copy(erdata, 0, erlen); /* copy only up to the
791*36399Ssklower 					bad octet (or max that will fit in a header */
792*36399Ssklower 		hdr->tpdu_li += erlen + 2;
793*36399Ssklower 		m_freem(erdata);
794*36399Ssklower 	} else {
795*36399Ssklower 		IFDEBUG(D_ERROR_EMIT)
796*36399Ssklower 			printf("error_emit DR error tpduli 0x%x\n", error, hdr->tpdu_li);
797*36399Ssklower 			dump_buf( (char *)hdr, hdr->tpdu_li );
798*36399Ssklower 		ENDDEBUG
799*36399Ssklower 		m->m_len = hdr->tpdu_li ;
800*36399Ssklower 		m_freem(erdata);
801*36399Ssklower 	}
802*36399Ssklower 
803*36399Ssklower 	hdr->tpdu_li --;
804*36399Ssklower 	IFTRACE(D_ERROR_EMIT)
805*36399Ssklower 		tptrace(TPPTtpduout, 2, hdr, hdr->tpdu_li+1, 0, 0);
806*36399Ssklower 	ENDTRACE
807*36399Ssklower 
808*36399Ssklower 	datalen = m_datalen( m);
809*36399Ssklower 
810*36399Ssklower 	if(tpcb) {
811*36399Ssklower 		if( tpcb->tp_use_checksum ) {
812*36399Ssklower 			IFTRACE(D_ERROR_EMIT)
813*36399Ssklower 				tptrace(TPPTmisc, "before gen csum datalen", datalen,0,0,0);
814*36399Ssklower 			ENDTRACE
815*36399Ssklower 			IFDEBUG(D_ERROR_EMIT)
816*36399Ssklower 				printf("before gen csum datalen 0x%x, csum_offset 0x%x\n",
817*36399Ssklower 					datalen, csum_offset);
818*36399Ssklower 			ENDDEBUG
819*36399Ssklower 
820*36399Ssklower 			iso_gen_csum(m, csum_offset, datalen);
821*36399Ssklower 		}
822*36399Ssklower 
823*36399Ssklower 		IFDEBUG(D_ERROR_EMIT)
824*36399Ssklower 			printf("OUTPUT: tpcb 0x%x, isop 0x%x, so 0x%x\n",
825*36399Ssklower 				tpcb,  tpcb->tp_npcb,  tpcb->tp_sock);
826*36399Ssklower 		ENDDEBUG
827*36399Ssklower 		/* Problem: if packet comes in on ISO but sock is listening
828*36399Ssklower 		 * in INET, this assertion will fail.
829*36399Ssklower 		 * Have to believe the argument, not the nlp_proto.
830*36399Ssklower 		ASSERT( tpcb->tp_nlproto->nlp_dgoutput == dgout_routine );
831*36399Ssklower 		 */
832*36399Ssklower 
833*36399Ssklower 		IFDEBUG(D_ERROR_EMIT)
834*36399Ssklower 			printf("tp_error_emit 1 sending DG: Laddr\n");
835*36399Ssklower 			dump_isoaddr( laddr );
836*36399Ssklower 			printf("Faddr\n");
837*36399Ssklower 			dump_isoaddr( faddr );
838*36399Ssklower 		ENDDEBUG
839*36399Ssklower 		return (tpcb->tp_nlproto->nlp_dgoutput)(
840*36399Ssklower 			&laddr->siso_addr,
841*36399Ssklower 			&faddr->siso_addr,
842*36399Ssklower 			m, datalen,
843*36399Ssklower 					/* no route */	(caddr_t)0, !tpcb->tp_use_checksum);
844*36399Ssklower 	} else  {
845*36399Ssklower 		if( cons_channel ) {
846*36399Ssklower #if NARGOXTWENTYFIVE > 0
847*36399Ssklower #include "../netiso/cons.h"
848*36399Ssklower 			/* This is unfortunate...
849*36399Ssklower 				cons_send_on_vc(cons_channel, m, datalen);
850*36399Ssklower 			*/
851*36399Ssklower 			cons_netcmd( CONN_CLOSE, (struct isopcb *)0,
852*36399Ssklower 				cons_channel, CONS_NOT_DGM);
853*36399Ssklower 			IFDEBUG(D_ERROR_EMIT)
854*36399Ssklower 				printf("OUTPUT: dutype 0x%x channel 0x%x\n",
855*36399Ssklower 					dutype, cons_channel);
856*36399Ssklower 			ENDDEBUG
857*36399Ssklower #else NARGOXTWENTYFIVE
858*36399Ssklower 			printf("TP panic! cons channel 0x%x but not cons configured\n",
859*36399Ssklower 				cons_channel);
860*36399Ssklower #endif NARGOXTWENTYFIVE > 0
861*36399Ssklower 		} else {
862*36399Ssklower #ifndef nodef
863*36399Ssklower 			IFDEBUG(D_ERROR_EMIT)
864*36399Ssklower 				printf("tp_error_emit sending DG: Laddr\n");
865*36399Ssklower 				dump_isoaddr( laddr );
866*36399Ssklower 				printf("Faddr\n");
867*36399Ssklower 				dump_isoaddr( laddr );
868*36399Ssklower 			ENDDEBUG
869*36399Ssklower 			return (*dgout_routine)( &laddr->siso_addr, &faddr->siso_addr,
870*36399Ssklower 				m, datalen, /* no route */
871*36399Ssklower 				(caddr_t)0, /* nochecksum==false */0);
872*36399Ssklower #else nodef
873*36399Ssklower 			IFDEBUG(D_ERROR_EMIT)
874*36399Ssklower 				printf("tp_error_emit DROPPING \n", m);
875*36399Ssklower 			ENDDEBUG
876*36399Ssklower 			IncStat(ts_send_drop);
877*36399Ssklower 			m_freem(m);
878*36399Ssklower 			return 0;
879*36399Ssklower #endif nodef
880*36399Ssklower 		}
881*36399Ssklower 	}
882*36399Ssklower }
883