No empty .Rs/.Re
[netbsd-mini2440.git] / sys / netiso / tp_driver.c
blobfaa4bed06034572c7678b2d3f4d3d5e276cb4e0e
1 /* $NetBSD: tp_driver.c,v 1.19 2005/12/11 12:25:12 christos Exp $ */
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: tp_driver.c,v 1.19 2005/12/11 12:25:12 christos Exp $");
6 #include "tp_states.h"
8 static const struct act_ent {
9 int a_newstate;
10 int a_action;
11 } statetable[] = {{
12 0, 0
14 #include "tp_states.init"
17 /* @(#)tp.trans 8.1 (Berkeley) 6/10/93 */
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/socket.h>
22 #include <sys/socketvar.h>
23 #include <sys/protosw.h>
24 #include <sys/mbuf.h>
25 #include <sys/time.h>
26 #include <sys/errno.h>
28 #include <netiso/tp_param.h>
29 #include <netiso/tp_var.h>
30 #include <netiso/tp_stat.h>
31 #include <netiso/tp_pcb.h>
32 #include <netiso/tp_tpdu.h>
33 #include <netiso/argo_debug.h>
34 #include <netiso/tp_trace.h>
35 #include <netiso/iso_errno.h>
36 #include <netiso/tp_seq.h>
37 #include <netiso/cons.h>
39 #define DRIVERTRACE TPPTdriver
40 #define sbwakeup(sb, dir) sowakeup(p->tp_sock, sb, dir);
41 #define MCPY(d, w) (d ? m_copym(d, 0, (int)M_COPYALL, w): 0)
43 static int trick_hc = 1;
45 #include "tp_events.h"
46 static int _Xebec_action (int, struct tp_event *, struct tp_pcb *);
47 static int _Xebec_index (struct tp_event *, struct tp_pcb *);
49 static int
50 _Xebec_action(int a, struct tp_event *e, struct tp_pcb *p)
52 int error;
53 struct mbuf *data = NULL;
54 int doack;
55 struct socket *so = p->tp_sock;
56 struct sockbuf *sb;
57 int timo;
59 switch (a) {
60 case -1:
61 return tp_protocol_error(e, p);
62 case 0x1:
63 (void) tp_emit(DC_TPDU_type, p, 0, 0, NULL);
64 break;
65 case 0x2:
66 #ifdef TP_DEBUG
67 if (e->ev_number != AK_TPDU)
68 printf("TPDU 0x%x in REFWAIT!!!!\n", e->ev_number);
69 #endif /* TP_DEBUG */
70 break;
71 case 0x3:
72 /* oh, man is this grotesque or what? */
73 (void) tp_goodack(p, e->ev_union.EV_AK_TPDU.e_cdt, e->ev_union.EV_AK_TPDU.e_seq, e->ev_union.EV_AK_TPDU.e_subseq);
75 * but it's necessary because this pseudo-ack may
76 * happen before the CC arrives, but we HAVE to
77 * adjust the snduna as a result of the ack, WHENEVER
78 * it arrives
80 break;
81 case 0x4:
82 tp_detach(p);
83 break;
84 case 0x5:
85 p->tp_refstate = REF_OPEN; /* has timers ??? */
86 break;
87 case 0x6:
88 #ifdef TPPT
89 if (tp_traceflags[D_CONN])
90 tptrace(TPPTmisc, "CR datalen data",
91 e->ev_union.EV_CR_TPDU.e_datalen,
92 e->ev_union.EV_CR_TPDU.e_data, 0, 0);
93 #endif
94 #ifdef ARGO_DEBUG
95 if (argo_debug[D_CONN]) {
96 printf("CR datalen 0x%x data %p",
97 e->ev_union.EV_CR_TPDU.e_datalen,
98 e->ev_union.EV_CR_TPDU.e_data);
100 #endif
101 p->tp_refstate = REF_OPEN; /* has timers */
102 p->tp_fcredit = e->ev_union.EV_CR_TPDU.e_cdt;
104 if (e->ev_union.EV_CR_TPDU.e_datalen > 0) {
105 /* n/a for class 0 */
106 ASSERT(p->tp_Xrcv.sb_cc == 0);
107 sbappendrecord(&p->tp_Xrcv,
108 e->ev_union.EV_CR_TPDU.e_data);
109 e->ev_union.EV_CR_TPDU.e_data = NULL;
111 break;
112 case 0x7:
113 IncStat(ts_tp0_conn);
114 #ifdef TPPT
115 if (tp_traceflags[D_CONN])
116 tptrace(TPPTmisc, "Confiming", p, 0, 0, 0);
117 #endif
118 #ifdef ARGO_DEBUG
119 if (argo_debug[D_CONN]) {
120 printf("Confirming connection: p");
122 #endif
123 soisconnected(p->tp_sock);
124 (void) tp_emit(CC_TPDU_type, p, 0, 0, NULL);
125 p->tp_fcredit = 1;
126 break;
127 case 0x8:
128 IncStat(ts_tp4_conn); /* even though not quite open */
129 #ifdef TPPT
130 if (tp_traceflags[D_CONN])
131 tptrace(TPPTmisc, "Confiming", p, 0, 0, 0);
132 #endif
133 #ifdef ARGO_DEBUG
134 if (argo_debug[D_CONN]) {
135 printf("Confirming connection: p");
137 #endif
138 tp_getoptions(p);
139 soisconnecting(p->tp_sock);
140 if ((p->tp_rx_strat & TPRX_FASTSTART) && (p->tp_fcredit > 0))
141 p->tp_cong_win = p->tp_fcredit * p->tp_l_tpdusize;
142 p->tp_retrans = p->tp_Nretrans;
143 tp_ctimeout(p, TM_retrans, (int) p->tp_cc_ticks);
144 break;
145 case 0x9:
146 #ifdef ARGO_DEBUG
147 if (argo_debug[D_CONN]) {
148 printf("event: CR_TPDU emit CC failed done ");
150 #endif
151 soisdisconnected(p->tp_sock);
152 tp_recycle_tsuffix(p);
153 tp_freeref(p->tp_lref);
154 tp_detach(p);
155 break;
156 case 0xa:
157 #ifdef TPPT
158 if (tp_traceflags[D_CONN])
159 tptrace(TPPTmisc, "T_CONN_req flags ucddata",
160 (int) p->tp_flags, p->tp_ucddata, 0, 0);
161 #endif
162 data = MCPY(p->tp_ucddata, M_WAIT);
163 if (data) {
164 #ifdef ARGO_DEBUG
165 if (argo_debug[D_CONN]) {
166 printf("T_CONN_req.trans m_copy cc %p\n",
167 p->tp_ucddata);
168 dump_mbuf(data, "sosnd @ T_CONN_req");
170 #endif
172 if ((error = tp_emit(CR_TPDU_type, p, 0, 0, data)) != 0)
173 return error; /* driver WON'T change state;
174 * will return error */
176 p->tp_refstate = REF_OPEN; /* has timers */
177 if (p->tp_class != TP_CLASS_0) {
178 p->tp_retrans = p->tp_Nretrans;
179 tp_ctimeout(p, TM_retrans, (int) p->tp_cr_ticks);
181 break;
182 case 0xb:
183 sbflush(&p->tp_Xrcv); /* purge non-delivered data
184 * data */
185 if (e->ev_union.EV_DR_TPDU.e_datalen > 0) {
186 sbappendrecord(&p->tp_Xrcv, e->ev_union.EV_DR_TPDU.e_data);
187 e->ev_union.EV_DR_TPDU.e_data = NULL;
189 if (p->tp_state == TP_OPEN)
190 tp_indicate(T_DISCONNECT, p, 0);
191 else {
192 int so_error = ECONNREFUSED;
193 if (e->ev_union.EV_DR_TPDU.e_reason != (E_TP_NO_SESSION ^ TP_ERROR_MASK) &&
194 e->ev_union.EV_DR_TPDU.e_reason != (E_TP_NO_CR_ON_NC ^ TP_ERROR_MASK) &&
195 e->ev_union.EV_DR_TPDU.e_reason != (E_TP_REF_OVERFLOW ^ TP_ERROR_MASK))
196 so_error = ECONNABORTED;
197 tp_indicate(T_DISCONNECT, p, so_error);
199 tp_soisdisconnected(p);
200 if (p->tp_class != TP_CLASS_0) {
201 if (p->tp_state == TP_OPEN) {
202 tp_euntimeout(p, TM_data_retrans);/* all */
203 tp_cuntimeout(p, TM_retrans);
204 tp_cuntimeout(p, TM_inact);
205 tp_cuntimeout(p, TM_sendack);
206 p->tp_flags &= ~TPF_DELACK;
208 tp_cuntimeout(p, TM_retrans);
209 if (e->ev_union.EV_DR_TPDU.e_sref != 0)
210 (void) tp_emit(DC_TPDU_type, p, 0, 0, NULL);
212 break;
213 case 0xc:
214 if (e->ev_union.EV_DR_TPDU.e_sref != 0)
215 (void) tp_emit(DC_TPDU_type, p, 0, 0, NULL);
217 * reference timer already set - reset it to be safe
218 * (???)
220 tp_euntimeout(p, TM_reference); /* all */
221 tp_etimeout(p, TM_reference, (int) p->tp_refer_ticks);
222 break;
223 case 0xd:
224 tp_cuntimeout(p, TM_retrans);
225 tp_indicate(ER_TPDU, p, e->ev_union.EV_ER_TPDU.e_reason);
226 tp_soisdisconnected(p);
227 break;
228 case 0xe:
229 tp_cuntimeout(p, TM_retrans);
230 tp_soisdisconnected(p);
231 break;
232 case 0xf:
233 tp_indicate(ER_TPDU, p, e->ev_union.EV_ER_TPDU.e_reason);
234 tp_cuntimeout(p, TM_retrans);
235 tp_soisdisconnected(p);
236 break;
237 case 0x10:
238 tp_cuntimeout(p, TM_retrans);
239 tp_soisdisconnected(p);
240 break;
241 case 0x11:
242 /* don't ask me why we have to do this - spec
243 * says so */
244 (void) tp_emit(DR_TPDU_type, p, 0, E_TP_NO_SESSION, NULL);
245 /* don't bother with retransmissions of the DR */
246 break;
247 case 0x12:
248 tp_soisdisconnecting(p->tp_sock);
249 tp_indicate(ER_TPDU, p, e->ev_union.EV_ER_TPDU.e_reason);
250 tp_soisdisconnected(p);
251 tp_netcmd(p, CONN_CLOSE);
252 break;
253 case 0x13:
254 if (p->tp_state == TP_OPEN) {
255 tp_euntimeout(p, TM_data_retrans); /* all */
256 tp_cuntimeout(p, TM_inact);
257 tp_cuntimeout(p, TM_sendack);
259 tp_soisdisconnecting(p->tp_sock);
260 tp_indicate(ER_TPDU, p, e->ev_union.EV_ER_TPDU.e_reason);
261 p->tp_retrans = p->tp_Nretrans;
262 tp_ctimeout(p, TM_retrans, (int) p->tp_dr_ticks);
263 (void) tp_emit(DR_TPDU_type, p, 0, E_TP_PROTO_ERR, NULL);
264 break;
265 case 0x14:
266 tp_cuntimeout(p, TM_retrans);
267 IncStat(ts_tp0_conn);
268 p->tp_fcredit = 1;
269 soisconnected(p->tp_sock);
270 break;
271 case 0x15:
272 #ifdef ARGO_DEBUG
273 if (argo_debug[D_CONN]) {
274 printf("trans: CC_TPDU in CRSENT state flags 0x%x\n",
275 (int) p->tp_flags);
277 #endif
278 IncStat(ts_tp4_conn);
279 p->tp_fref = e->ev_union.EV_CC_TPDU.e_sref;
280 p->tp_fcredit = e->ev_union.EV_CC_TPDU.e_cdt;
281 if ((p->tp_rx_strat & TPRX_FASTSTART) &&
282 (e->ev_union.EV_CC_TPDU.e_cdt > 0))
283 p->tp_cong_win = e->ev_union.EV_CC_TPDU.e_cdt * p->tp_l_tpdusize;
284 tp_getoptions(p);
285 tp_cuntimeout(p, TM_retrans);
286 if (p->tp_ucddata) {
287 #ifdef ARGO_DEBUG
288 if (argo_debug[D_CONN]) {
289 printf("dropping user connect data cc 0x%x\n",
290 p->tp_ucddata->m_len);
292 #endif
293 m_freem(p->tp_ucddata);
294 p->tp_ucddata = 0;
296 soisconnected(p->tp_sock);
297 if (e->ev_union.EV_CC_TPDU.e_datalen > 0) {
298 ASSERT(p->tp_Xrcv.sb_cc == 0); /* should be empty */
299 sbappendrecord(&p->tp_Xrcv,
300 e->ev_union.EV_CC_TPDU.e_data);
301 e->ev_union.EV_CC_TPDU.e_data = NULL;
303 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
304 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
305 break;
306 case 0x16:
307 IncStat(ts_retrans_cr);
308 p->tp_cong_win = 1 * p->tp_l_tpdusize;
309 data = MCPY(p->tp_ucddata, M_NOWAIT);
310 if (p->tp_ucddata) {
311 #ifdef ARGO_DEBUG
312 if (argo_debug[D_CONN]) {
313 printf("TM_retrans.trans m_copy cc %p\n",
314 data);
315 dump_mbuf(p->tp_ucddata, "sosnd @ TM_retrans");
317 #endif
318 if (data == NULL)
319 return ENOBUFS;
321 p->tp_retrans--;
322 if ((error = tp_emit(CR_TPDU_type, p, 0, 0, data)) != 0) {
323 p->tp_sock->so_error = error;
325 tp_ctimeout(p, TM_retrans, (int) p->tp_cr_ticks);
326 break;
327 case 0x17:
328 IncStat(ts_conn_gaveup);
329 p->tp_sock->so_error = ETIMEDOUT;
330 tp_indicate(T_DISCONNECT, p, ETIMEDOUT);
331 tp_soisdisconnected(p);
332 break;
333 case 0x18:
334 data = MCPY(p->tp_ucddata, M_WAIT);
336 if ((error = tp_emit(CC_TPDU_type, p, 0, 0, data)) != 0) {
337 p->tp_sock->so_error = error;
339 p->tp_retrans = p->tp_Nretrans;
340 tp_ctimeout(p, TM_retrans, (int) p->tp_cc_ticks);
341 break;
342 case 0x19:
344 * Get rid of any confirm or connect data, so that if we
345 * crash or close, it isn't thought of as disconnect data.
347 if (p->tp_ucddata) {
348 m_freem(p->tp_ucddata);
349 p->tp_ucddata = 0;
351 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
352 tp_cuntimeout(p, TM_retrans);
353 soisconnected(p->tp_sock);
354 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
357 * see also next 2 transitions, if you make any
358 * changes
361 doack = tp_stash(p, e);
362 #ifdef ARGO_DEBUG
363 if (argo_debug[D_DATA]) {
364 printf("tp_stash returns %d\n", doack);
366 #endif
368 if (doack) {
369 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
370 tp_ctimeout(p, TM_sendack, (int) p->tp_keepalive_ticks);
371 } else
372 tp_ctimeout(p, TM_sendack, (int) p->tp_sendack_ticks);
374 #ifdef ARGO_DEBUG
375 if (argo_debug[D_DATA]) {
376 printf("after stash calling sbwakeup\n");
378 #endif
379 break;
380 case 0x1a:
381 tp0_stash(p, e);
382 sbwakeup(&p->tp_sock->so_rcv, POLL_IN);
384 #ifdef ARGO_DEBUG
385 if (argo_debug[D_DATA]) {
386 printf("after stash calling sbwakeup\n");
388 #endif
389 break;
390 case 0x1b:
391 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
392 sbwakeup(&p->tp_sock->so_rcv, POLL_IN);
394 doack = tp_stash(p, e);
395 #ifdef ARGO_DEBUG
396 if (argo_debug[D_DATA]) {
397 printf("tp_stash returns %d\n", doack);
399 #endif
401 if (doack)
402 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
403 else
404 tp_ctimeout_MIN(p, TM_sendack, (int) p->tp_sendack_ticks);
406 #ifdef ARGO_DEBUG
407 if (argo_debug[D_DATA]) {
408 printf("after stash calling sbwakeup\n");
410 #endif
411 break;
412 case 0x1c:
413 #ifdef TPPT
414 if (tp_traceflags[D_DATA])
415 tptrace(TPPTmisc, "NIW seq rcvnxt lcredit ",
416 e->ev_union.EV_DT_TPDU.e_seq,
417 p->tp_rcvnxt, p->tp_lcredit, 0);
418 #endif
419 IncStat(ts_dt_niw);
420 m_freem(e->ev_union.EV_DT_TPDU.e_data);
421 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
422 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
423 break;
424 case 0x1d:
425 if (p->tp_ucddata) {
426 m_freem(p->tp_ucddata);
427 p->tp_ucddata = 0;
429 (void) tp_goodack(p, e->ev_union.EV_AK_TPDU.e_cdt, e->ev_union.EV_AK_TPDU.e_seq, e->ev_union.EV_AK_TPDU.e_subseq);
430 tp_cuntimeout(p, TM_retrans);
432 soisconnected(p->tp_sock);
433 #ifdef TPPT
434 if (tp_traceflags[D_CONN]) {
435 struct socket *so = p->tp_sock;
436 tptrace(TPPTmisc,
437 "called sosiconn: so so_state rcv.sb_sel rcv.sb_flags",
438 so, so->so_state, so->so_rcv.sb_sel,
439 so->so_rcv.sb_flags);
440 tptrace(TPPTmisc,
441 "called sosiconn 2: so_qlen so_error so_rcv.sb_cc so_head",
442 so->so_qlen, so->so_error, so->so_rcv.sb_cc,
443 so->so_head);
445 #endif
447 tp_ctimeout(p, TM_sendack, (int) p->tp_keepalive_ticks);
448 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
449 break;
450 case 0x1e:
451 if (p->tp_state == TP_AKWAIT) {
452 if (p->tp_ucddata) {
453 m_freem(p->tp_ucddata);
454 p->tp_ucddata = 0;
456 tp_cuntimeout(p, TM_retrans);
457 soisconnected(p->tp_sock);
458 tp_ctimeout(p, TM_sendack, (int) p->tp_keepalive_ticks);
459 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
461 #ifdef TPPT
462 if (tp_traceflags[D_XPD]) {
463 tptrace(TPPTmisc, "XPD tpdu accepted Xrcvnxt, "
464 "e_seq datalen m_len\n", p->tp_Xrcvnxt,
465 e->ev_union.EV_XPD_TPDU.e_seq,
466 e->ev_union.EV_XPD_TPDU.e_datalen,
467 e->ev_union.EV_XPD_TPDU.e_data->m_len);
469 #endif
471 p->tp_sock->so_state |= SS_RCVATMARK;
472 e->ev_union.EV_XPD_TPDU.e_data->m_flags |= M_EOR;
473 sbinsertoob(&p->tp_Xrcv, e->ev_union.EV_XPD_TPDU.e_data);
474 #ifdef ARGO_DEBUG
475 if (argo_debug[D_XPD]) {
476 dump_mbuf(e->ev_union.EV_XPD_TPDU.e_data, "XPD TPDU: tp_Xrcv");
478 #endif
479 tp_indicate(T_XDATA, p, 0);
480 sbwakeup(&p->tp_Xrcv, POLL_IN);
482 (void) tp_emit(XAK_TPDU_type, p, p->tp_Xrcvnxt, 0, NULL);
483 SEQ_INC(p, p->tp_Xrcvnxt);
484 break;
485 case 0x1f:
486 if (p->tp_Xrcv.sb_cc == 0) {
487 /* kludge for select(): */
488 /* p->tp_sock->so_state &= ~SS_OOBAVAIL; */
490 break;
491 case 0x20:
492 #ifdef TPPT
493 if (tp_traceflags[D_XPD])
494 tptrace(TPPTmisc,
495 "XPD tpdu niw (Xrcvnxt, e_seq) or not cdt (cc)\n",
496 p->tp_Xrcvnxt, e->ev_union.EV_XPD_TPDU.e_seq,
497 p->tp_Xrcv.sb_cc, 0);
498 #endif
499 if (p->tp_Xrcvnxt != e->ev_union.EV_XPD_TPDU.e_seq)
500 IncStat(ts_xpd_niw);
501 if (p->tp_Xrcv.sb_cc) {
502 /* might as well kick 'em again */
503 tp_indicate(T_XDATA, p, 0);
504 IncStat(ts_xpd_dup);
506 m_freem(e->ev_union.EV_XPD_TPDU.e_data);
507 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
509 * don't send an xack because the xak gives "last one
510 * received", not "next one i expect" (dumb)
512 break;
513 case 0x21:
514 /* detach from parent socket so it can finish closing */
515 if (so->so_head) {
516 if (!soqremque(so, 0) && !soqremque(so, 1))
517 panic("tp: T_DETACH");
518 so->so_head = 0;
520 tp_soisdisconnecting(p->tp_sock);
521 tp_netcmd(p, CONN_CLOSE);
522 tp_soisdisconnected(p);
523 break;
524 case 0x22:
525 /* detach from parent socket so it can finish closing */
526 if (so->so_head) {
527 if (!soqremque(so, 0) && !soqremque(so, 1))
528 panic("tp: T_DETACH");
529 so->so_head = 0;
531 if (p->tp_state != TP_CLOSING) {
532 tp_soisdisconnecting(p->tp_sock);
533 data = MCPY(p->tp_ucddata, M_NOWAIT);
534 (void) tp_emit(DR_TPDU_type, p, 0, E_TP_NORMAL_DISC, data);
535 p->tp_retrans = p->tp_Nretrans;
536 tp_ctimeout(p, TM_retrans, (int) p->tp_dr_ticks);
538 break;
539 case 0x23:
540 tp_soisdisconnecting(p->tp_sock);
541 tp_netcmd(p, CONN_CLOSE);
542 tp_soisdisconnected(p);
543 break;
544 case 0x24:
545 data = MCPY(p->tp_ucddata, M_WAIT);
547 if (p->tp_state == TP_OPEN) {
548 tp_euntimeout(p, TM_data_retrans); /* all */
549 tp_cuntimeout(p, TM_inact);
550 tp_cuntimeout(p, TM_sendack);
551 p->tp_flags &= ~TPF_DELACK;
553 if (data) {
554 #ifdef ARGO_DEBUG
555 if (argo_debug[D_CONN]) {
556 printf("T_DISC_req.trans tp_ucddata %p\n",
557 p->tp_ucddata);
558 dump_mbuf(data, "ucddata @ T_DISC_req");
560 #endif
562 tp_soisdisconnecting(p->tp_sock);
563 p->tp_retrans = p->tp_Nretrans;
564 tp_ctimeout(p, TM_retrans, (int) p->tp_dr_ticks);
566 if (trick_hc)
567 return tp_emit(DR_TPDU_type, p, 0,
568 e->ev_union.EV_REQ_TPDU.e_reason,
569 data);
570 break;
571 case 0x25:
572 data = MCPY(p->tp_ucddata, M_WAIT);
574 IncStat(ts_retrans_cc);
575 p->tp_retrans--;
576 p->tp_cong_win = 1 * p->tp_l_tpdusize;
578 if ((error = tp_emit(CC_TPDU_type, p, 0, 0, data)) != 0)
579 p->tp_sock->so_error = error;
580 tp_ctimeout(p, TM_retrans, (int) p->tp_cc_ticks);
581 break;
582 case 0x26:
583 IncStat(ts_conn_gaveup);
584 tp_soisdisconnecting(p->tp_sock);
585 p->tp_sock->so_error = ETIMEDOUT;
586 tp_indicate(T_DISCONNECT, p, ETIMEDOUT);
587 (void) tp_emit(DR_TPDU_type, p, 0, E_TP_CONGEST, NULL);
588 p->tp_retrans = p->tp_Nretrans;
589 tp_ctimeout(p, TM_retrans, (int) p->tp_dr_ticks);
590 break;
591 case 0x27:
592 tp_euntimeout(p, TM_data_retrans); /* all */
593 tp_cuntimeout(p, TM_inact);
594 tp_cuntimeout(p, TM_sendack);
596 IncStat(ts_conn_gaveup);
597 tp_soisdisconnecting(p->tp_sock);
598 p->tp_sock->so_error = ETIMEDOUT;
599 tp_indicate(T_DISCONNECT, p, ETIMEDOUT);
600 (void) tp_emit(DR_TPDU_type, p, 0, E_TP_CONGEST_2, NULL);
601 p->tp_retrans = p->tp_Nretrans;
602 tp_ctimeout(p, TM_retrans, (int) p->tp_dr_ticks);
603 break;
604 case 0x28:
605 p->tp_cong_win = 1 * p->tp_l_tpdusize;
606 /* resume XPD */
607 if (p->tp_Xsnd.sb_mb) {
608 struct mbuf *m = m_copy(p->tp_Xsnd.sb_mb, 0,
609 (int) p->tp_Xsnd.sb_cc);
610 int shift;
612 #ifdef TPPT
613 if (tp_traceflags[D_XPD]) {
614 tptrace(TPPTmisc,
615 "XPD retrans: Xuna Xsndnxt sndnxt snduna",
616 p->tp_Xuna, p->tp_Xsndnxt, p->tp_sndnxt,
617 p->tp_snduna);
619 #endif
620 #ifdef ARGO_DEBUG
621 if (argo_debug[D_XPD]) {
622 dump_mbuf(m, "XPD retrans emitting M");
624 #endif
625 IncStat(ts_retrans_xpd);
626 p->tp_retrans--;
627 shift = max(p->tp_Nretrans - p->tp_retrans, 6);
628 (void) tp_emit(XPD_TPDU_type, p, p->tp_Xuna, 1, m);
629 tp_ctimeout(p, TM_retrans, ((int) p->tp_dt_ticks) << shift);
631 break;
632 case 0x29:
633 p->tp_rxtshift++;
634 (void) tp_data_retrans(p);
635 break;
636 case 0x2a:
637 p->tp_retrans--;
638 (void) tp_emit(DR_TPDU_type, p, 0, E_TP_DR_NO_REAS, NULL);
639 IncStat(ts_retrans_dr);
640 tp_ctimeout(p, TM_retrans, (int) p->tp_dr_ticks);
641 break;
642 case 0x2b:
643 p->tp_sock->so_error = ETIMEDOUT;
644 p->tp_refstate = REF_FROZEN;
645 tp_recycle_tsuffix(p);
646 tp_etimeout(p, TM_reference, (int) p->tp_refer_ticks);
647 break;
648 case 0x2c:
649 tp_freeref(p->tp_lref);
650 tp_detach(p);
651 break;
652 case 0x2d:
653 if (p->tp_class != TP_CLASS_0) {
654 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
655 if (e->ev_number == CC_TPDU)
656 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
659 * ignore it if class 0 - state tables are blank for
660 * this
662 break;
663 case 0x2e:
664 #ifdef TPPT
665 if (tp_traceflags[D_DATA])
666 tptrace(TPPTmisc,
667 "T_DATA_req sndnxt snduna fcredit, tpcb",
668 p->tp_sndnxt, p->tp_snduna, p->tp_fcredit, p);
669 #endif
671 tp_send(p);
672 break;
673 case 0x2f:
674 error = 0;
676 /* resume XPD */
677 if (p->tp_Xsnd.sb_mb) {
678 struct mbuf *m = m_copy(p->tp_Xsnd.sb_mb, 0, (int) p->tp_Xsnd.sb_cc);
680 * m_copy doesn't preserve the m_xlink field,
681 * but at this pt. that doesn't matter
684 #ifdef TPPT
685 if (tp_traceflags[D_XPD])
686 tptrace(TPPTmisc,
687 "XPD req: Xuna Xsndnxt sndnxt snduna",
688 p->tp_Xuna, p->tp_Xsndnxt, p->tp_sndnxt,
689 p->tp_snduna);
690 #endif
691 #ifdef ARGO_DEBUG
692 if (argo_debug[D_XPD]) {
693 printf("T_XPD_req: sb_cc 0x%lx\n", p->tp_Xsnd.sb_cc);
694 dump_mbuf(m, "XPD req emitting M");
696 #endif
697 error =
698 tp_emit(XPD_TPDU_type, p, p->tp_Xuna, 1, m);
699 p->tp_retrans = p->tp_Nretrans;
701 tp_ctimeout(p, TM_retrans, (int) p->tp_rxtcur);
702 SEQ_INC(p, p->tp_Xsndnxt);
704 if (trick_hc)
705 return error;
706 break;
707 case 0x30:
708 sb = &p->tp_sock->so_snd;
710 #ifdef ARGO_DEBUG
711 if (argo_debug[D_ACKRECV]) {
712 printf("GOOD ACK seq 0x%x cdt 0x%x\n", e->ev_union.EV_AK_TPDU.e_seq, e->ev_union.EV_AK_TPDU.e_cdt);
714 #endif
715 if (p->tp_class != TP_CLASS_0) {
716 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
718 sbwakeup(sb, POLL_OUT);
719 #ifdef ARGO_DEBUG
720 if (argo_debug[D_ACKRECV]) {
721 printf("GOOD ACK new sndnxt 0x%x\n", p->tp_sndnxt);
723 #endif
724 break;
725 case 0x31:
726 #ifdef TPPT
727 if (tp_traceflags[D_ACKRECV])
728 tptrace(TPPTmisc,
729 "BOGUS ACK fcc_present, tp_r_subseq e_subseq",
730 e->ev_union.EV_AK_TPDU.e_fcc_present,
731 p->tp_r_subseq,
732 e->ev_union.EV_AK_TPDU.e_subseq, 0);
733 #endif
734 if (p->tp_class != TP_CLASS_0) {
736 if (!e->ev_union.EV_AK_TPDU.e_fcc_present) {
737 /* send ACK with FCC */
738 IncStat(ts_ackreason[_ACK_FCC_]);
739 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt,
740 1, NULL);
742 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
744 break;
745 case 0x32:
746 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
747 tp_cuntimeout(p, TM_retrans);
749 sbwakeup(&p->tp_sock->so_snd, POLL_OUT);
751 /* resume normal data */
752 tp_send(p);
753 break;
754 case 0x33:
755 #ifdef TPPT
756 if (tp_traceflags[D_ACKRECV])
757 tptrace(TPPTmisc, "BOGUS XACK eventtype ",
758 e->ev_number, 0, 0, 0);
759 #endif
760 if (p->tp_class != TP_CLASS_0) {
761 tp_ctimeout(p, TM_inact, (int) p->tp_inact_ticks);
763 break;
764 case 0x34:
765 #ifdef TPPT
766 if (tp_traceflags[D_TIMER])
767 tptrace(TPPTsendack, -1, p->tp_lcredit, p->tp_sent_uwe,
768 p->tp_sent_lcdt, 0);
769 #endif
770 IncPStat(p, tps_n_TMsendack);
771 (void) tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
772 if (p->tp_fcredit == 0) {
773 if (p->tp_rxtshift < TP_MAXRXTSHIFT)
774 p->tp_rxtshift++;
775 timo = (p->tp_dt_ticks) << p->tp_rxtshift;
776 } else
777 timo = p->tp_sendack_ticks;
778 tp_ctimeout(p, TM_sendack, timo);
779 break;
780 case 0x35:
781 if (sbspace(&p->tp_sock->so_rcv) > 0)
782 tp0_openflow(p);
783 break;
784 case 0x36:
785 if (trick_hc) {
786 SeqNum ack_thresh;
788 * If the upper window edge has advanced a reasonable
789 * amount beyond what was known, send an ACK.
790 * A reasonable amount is 2 packets, unless the max window
791 * is only 1 or 2 packets, in which case we
792 * should send an ack for any advance in the upper window edge.
794 LOCAL_CREDIT(p);
795 ack_thresh = SEQ_SUB(p, p->tp_lcredit + p->tp_rcvnxt,
796 (p->tp_maxlcredit > 2 ? 2 : 1));
797 if (SEQ_GT(p, ack_thresh, p->tp_sent_uwe)) {
798 IncStat(ts_ackreason[_ACK_USRRCV_]);
799 p->tp_flags &= ~TPF_DELACK;
800 return tp_emit(AK_TPDU_type, p, p->tp_rcvnxt, 0, NULL);
803 break;
804 case 0x37:
805 if (trick_hc)
806 return ECONNABORTED;
807 break;
808 case 0x38:
809 ASSERT(p->tp_state != TP_LISTENING);
810 tp_indicate(T_DISCONNECT, p, ECONNRESET);
811 tp_soisdisconnected(p);
812 break;
814 return 0;
817 static int
818 _Xebec_index(struct tp_event *e, struct tp_pcb *p)
820 switch ((e->ev_number << 4) + (p->tp_state)) {
821 case 0x12:
822 if (p->tp_retrans > 0)
823 return 0x1e;
824 else
825 return 0x1f;
826 case 0x13:
827 if (p->tp_retrans > 0)
828 return 0x2f;
829 else
830 return 0x30;
831 case 0x14:
832 if (p->tp_retrans > 0)
833 return 0x32;
834 else
835 return 0x31;
836 case 0x15:
837 if (p->tp_retrans > 0)
838 return 0x34;
839 else
840 return 0x35;
841 case 0x54:
842 if (p->tp_rxtshift < TP_NRETRANS)
843 return 0x33;
844 else
845 return 0x31;
846 case 0x64:
847 if (p->tp_class == TP_CLASS_0)
848 return 0x1a;
849 else
850 return 0x1b;
851 case 0x77:
852 if (p->tp_class == TP_CLASS_0)
853 return 0xd;
854 else
855 return 0xe;
856 case 0x86:
857 if (e->ev_union.EV_DR_TPDU.e_sref != 0)
858 return 0x2;
859 else
860 return 0x3;
861 case 0xa2:
862 if (p->tp_class == TP_CLASS_0)
863 return 0x1c;
864 else
865 return 0x1d;
866 case 0xb2:
867 if (p->tp_class == TP_CLASS_0)
868 return 0x5;
869 else
870 return 0x0;
871 case 0xb4:
872 if (tp_goodack(p, e->ev_union.EV_AK_TPDU.e_cdt, e->ev_union.EV_AK_TPDU.e_seq, e->ev_union.EV_AK_TPDU.e_subseq))
873 return 0x3a;
874 else
875 return 0x3b;
876 case 0xc3:
877 if (IN_RWINDOW(p, e->ev_union.EV_DT_TPDU.e_seq,
878 p->tp_rcvnxt, SEQ(p, p->tp_rcvnxt + p->tp_lcredit)))
879 return 0x21;
880 else
881 return 0x24;
882 case 0xc4:
883 if (p->tp_class == TP_CLASS_0)
884 return 0x22;
885 else if (IN_RWINDOW(p, e->ev_union.EV_DT_TPDU.e_seq,
886 p->tp_rcvnxt, SEQ(p, p->tp_rcvnxt + p->tp_lcredit)))
887 return 0x23;
888 else
889 return 0x25;
890 case 0xd3:
891 if (p->tp_Xrcvnxt == e->ev_union.EV_XPD_TPDU.e_seq)
892 return 0x27;
893 else
894 return 0x2a;
895 case 0xd4:
896 if (p->tp_Xrcvnxt == e->ev_union.EV_XPD_TPDU.e_seq)
897 return 0x27;
898 else
899 return 0x29;
900 case 0xe4:
901 if (tp_goodXack(p, e->ev_union.EV_XAK_TPDU.e_seq))
902 return 0x3c;
903 else
904 return 0x3d;
905 case 0x102:
906 if (p->tp_class == TP_CLASS_0)
907 return 0x2d;
908 else
909 return 0x2e;
910 case 0x104:
911 if (p->tp_class == TP_CLASS_0)
912 return 0x2d;
913 else
914 return 0x2e;
915 case 0x144:
916 if (p->tp_class == TP_CLASS_0)
917 return 0x3f;
918 else
919 return 0x40;
920 case 0x162:
921 if (p->tp_class == TP_CLASS_0)
922 return 0x2b;
923 else
924 return 0x2c;
925 case 0x172:
926 if (p->tp_class != TP_CLASS_4)
927 return 0x42;
928 else
929 return 0x46;
930 case 0x174:
931 if (p->tp_class != TP_CLASS_4)
932 return 0x42;
933 else
934 return 0x47;
935 case 0x177:
936 if (p->tp_class != TP_CLASS_4)
937 return 0x42;
938 else
939 return 0x43;
940 case 0x188:
941 if (p->tp_class == TP_CLASS_0)
942 return 0xf;
943 else if (tp_emit(CC_TPDU_type, p, 0, 0, MCPY(p->tp_ucddata, M_NOWAIT)) == 0)
944 return 0x10;
945 else
946 return 0x11;
947 default:
948 return 0;
949 } /* end switch */
950 } /* _Xebec_index() */
951 static const int inx[26][9] =
953 {0, 0, 0, 0, 0, 0, 0, 0, 0,},
954 {0x0, 0x0, 0x0, 0x0, 0x31, 0x0, 0x0, 0x0, 0x0,},
955 {0x0, 0x0, -1, -1, -1, -1, 0x0, 0x0, 0x0,},
956 {0x0, 0x0, 0x0, 0x0, 0x3e, 0x0, 0x0, 0x0, 0x0,},
957 {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,},
958 {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x36, 0x0, 0x0,},
959 {0x0, 0x0, 0x0, 0x0, -1, 0x0, 0x0, 0x0, 0x0,},
960 {0x0, 0x7, 0x15, 0x1b, -1, 0x17, 0x3, 0xa, 0x0,},
961 {0x0, 0x19, 0x6, 0x20, 0x37, 0x8, 0x3, -1, 0x0,},
962 {0x0, 0x14, 0x13, 0x13, 0x13, 0x16, -1, 0xa, 0x0,},
963 {0x0, 0x7, 0x6, 0x1, 0x9, 0x18, 0x3, 0xa, 0x0,},
964 {0x0, 0x19, -1, 0x1, 0x37, 0x8, 0x3, 0xa, 0x0,},
965 {0x0, 0x7, -1, 0x26, -1, 0x8, 0x3, 0xa, 0x0,},
966 {0x0, 0x7, 0x6, -1, -1, 0x8, 0x3, 0xa, 0x0,},
967 {0x0, 0x7, 0x6, -1, -1, 0x8, 0x3, 0xa, 0x0,},
968 {0x0, 0x7, 0x6, 0x1, -1, 0x8, 0x3, 0xa, 0x0,},
969 {0x0, 0x12, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,},
970 {0x0, 0x0, -1, 0x2e, -1, 0x0, 0x4, 0x0, 0x2e,},
971 {0x0, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,},
972 {0x0, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x0,},
973 {0x0, 0x0, 0x0, 0x0, 0x39, 0x0, 0x0, 0x0, 0x0,},
974 {0x0, 0x0, 0x0, 0x0, -1, 0x0, 0x41, 0x0, 0x0,},
975 {0x0, 0x0, 0x0, 0x0, 0x28, 0x0, 0x41, 0x0, 0x0,},
976 {0x0, 0xc, -1, 0x2c, 0x0, 0x2c, 0x4, 0xc, 0x2c,},
977 {0x0, 0x49, -1, 0x45, -1, 0x44, 0x48, -1, 0x0,},
978 {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, -1,},
981 tp_driver(struct tp_pcb *p, struct tp_event *e)
983 int index, error = 0;
984 const struct act_ent *a;
985 static const struct act_ent erroraction = {0, -1};
987 index = inx[1 + e->ev_number][p->tp_state];
988 if (index < 0)
989 index = _Xebec_index(e, p);
990 if (index == 0) {
991 a = &erroraction;
992 } else
993 a = &statetable[index];
995 if (a->a_action)
996 error = _Xebec_action(a->a_action, e, p);
997 #ifdef TPPT
998 if (tp_traceflag[D_DRIVER])
999 tptrace(DRIVERTRACE, a->a_newstate, p->tp_state,
1000 e->ev_number, a->a_action, 0);
1001 #endif
1002 if (error == 0)
1003 p->tp_state = a->a_newstate;
1004 return error;