more lint
[unix-history] / usr / src / sys / netinet / tcp_usrreq.c
index 97a13f5..d91552b 100644 (file)
-/* tcp_usrreq.c 1.14 81/10/29 */
+/* tcp_usrreq.c 1.29 81/11/16 */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 #include "../h/mbuf.h"
 #include "../h/socket.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
 #include "../h/mbuf.h"
 #include "../h/socket.h"
-#include "../inet/inet.h"
-#include "../inet/inet_systm.h"
-#include "../inet/imp.h"
-#include "../inet/ip.h"
-#include "../inet/tcp.h"
+#include "../h/socketvar.h"
+#include "../h/protosw.h"
+#include "../net/inet.h"
+#include "../net/inet_host.h"
+#include "../net/inet_pcb.h"
+#include "../net/inet_systm.h"
+#include "../net/imp.h"
+#include "../net/ip.h"
+#include "../net/ip_var.h"
+#include "../net/tcp.h"
 #define TCPFSTAB
 #ifdef TCPDEBUG
 #define TCPSTATES
 #endif
 #define TCPFSTAB
 #ifdef TCPDEBUG
 #define TCPSTATES
 #endif
-#include "../inet/tcp_fsm.h"
+#include "../net/tcp_fsm.h"
+#include "../net/tcp_var.h"
+#include "/usr/include/errno.h"
+
+/*
+ * Tcp initialization
+ */
+tcp_init()
+{
+
+       tcp_iss = 1;            /* wrong */
+       tcb.inp_next = tcb.inp_prev = &tcb;
+}
 
 
-tcp_timeo()
+/*
+ * Tcp finite state machine entries for timer and user generated
+ * requests.  These routines raise the ipl to that of the network
+ * to prevent reentry.  In particluar, this requires that the software
+ * clock interrupt have lower priority than the network so that
+ * we can enter the network from timeout routines without improperly
+ * nesting the interrupt stack.
+ */
+
+/*
+ * Tcp protocol timeout routine called every 500 ms.
+ * Updates the timers in all active tcb's and
+ * causes finite state machine actions if timers expire.
+ */
+tcp_slowtimo()
 {
 {
-       register struct tcb *tp;
+       register struct inpcb *ip;
+       register struct tcpcb *tp;
        int s = splnet();
        int s = splnet();
+       register short *tmp;
+       register int i;
 COUNT(TCP_TIMEO);
 
        /*
         * Search through tcb's and update active timers.
         */
 COUNT(TCP_TIMEO);
 
        /*
         * Search through tcb's and update active timers.
         */
-       for (tp = tcb_head; tp != NULL; tp = tp->t_tcb_next) {
-               if (tp->t_init != 0 && --tp->t_init == 0)
-                       tcp_usrreq(ISTIMER, TINIT, tp, 0);
-               if (tp->t_rexmt != 0 && --tp->t_rexmt == 0)
-                       tcp_usrreq(ISTIMER, TREXMT, tp, 0);
-               if (tp->t_rexmttl != 0 && --tp->t_rexmttl == 0)
-                       tcp_usrreq(ISTIMER, TREXMTTL, tp, 0);
-               if (tp->t_persist != 0 && --tp->t_persist == 0)
-                       tcp_usrreq(ISTIMER, TPERSIST, tp, 0);
-               if (tp->t_finack != 0 && --tp->t_finack == 0)
-                       tcp_usrreq(ISTIMER, TFINACK, tp, 0);
+       for (ip = tcb.inp_next; ip != &tcb; ip = ip->inp_next) {
+               tp = intotcpcb(ip);
+               tmp = &tp->t_init;
+               for (i = 0; i < TNTIMERS; i++) {
+                       if (*tmp && --*tmp == 0)
+                               (void) tcp_usrreq(tp->t_inpcb->inp_socket,
+                                   PRU_SLOWTIMO, (struct mbuf *)0,
+                                   (caddr_t)i);
+                       tmp++;
+               }
                tp->t_xmt++;
        }
                tp->t_xmt++;
        }
-       tcp_iss += ISSINCR;             /* increment iss */
-       timeout(tcp_timeo, 0, hz);      /* reschedule every second */
+       tcp_iss += ISSINCR/2;           /* increment iss */
        splx(s);
 }
 
        splx(s);
 }
 
-tcp_usrreq(input, timertype, tp, mp)
-       int input, timertype;
-       register struct tcb *tp;
-       struct mbuf *mp;
+/*
+ * Cancel all timers for tcp tp.
+ */
+tcp_tcancel(tp)
+       struct tcpcb *tp;
 {
 {
+       register short *tmp = &tp->t_init;
+       register int i;
+
+       for (i = 0; i < TNTIMERS; i++)
+               *tmp++ = 0;
+}
+
+/*
+ * Process a TCP user request for tcp tb.  If this is a send request
+ * then m is the mbuf chain of send data.  If this is a timer expiration
+ * (called from the software clock routine), then timertype tells which timer.
+ */
+tcp_usrreq(so, req, m, addr)
+       struct socket *so;
+       int req;
+       struct mbuf *m;
+       caddr_t addr;
+{
+       register struct inpcb *inp = sotoinpcb(so);
+       register struct tcpcb *tp;
        int s = splnet();
        register int nstate;
 #ifdef TCPDEBUG
        struct tcp_debug tdb;
 #endif
        int s = splnet();
        register int nstate;
 #ifdef TCPDEBUG
        struct tcp_debug tdb;
 #endif
+       int error = 0;
 COUNT(TCP_USRREQ);
 
 COUNT(TCP_USRREQ);
 
-       nstate = tp->t_state;
-       tp->tc_flags &= ~TC_NET_KEEP;
-       acounts[nstate][input]++;
+       /*
+        * Make sure attached.  If not,
+        * only PRU_ATTACH is valid.
+        */
 #ifdef TCPDEBUG
 #ifdef TCPDEBUG
-       if ((tp->t_ucb->uc_flags & UDEBUG) || tcpconsdebug) {
-               tdb_setup(tp, (struct th *)0, input, &tdb);
-               tdb.td_tim = timertype;
-       } else
-               tdb.td_tod = 0;
+       tdb.td_tod = 0;
 #endif
 #endif
-       switch (tcp_fstab[nstate][input]) {
+       if (inp == 0) {
+               if (req != PRU_ATTACH) {
+                       splx(s);
+                       return (EINVAL);
+               }
+       } else {
+               tp = intotcpcb(inp);
+               nstate = tp->t_state;
+#ifdef KPROF
+               tcp_acounts[nstate][req]++;
+#endif
+#ifdef TCPDEBUG
+               if (((tp->t_socket->so_options & SO_DEBUG) || tcpconsdebug)) {
+                       tdb_setup(tp, (struct tcpiphdr *)0, req, &tdb);
+                       tdb.td_tim = timertype;
+               }
+#endif
+               tp->tc_flags &= ~TC_NET_KEEP;
+       }
 
 
-       default:
-               printf("tcp: bad state: tcb=%x state=%d input=%d\n",
-                   tp, tp->t_state, input);
-               nstate = EFAILEC;
+       switch (req) {
+
+       case PRU_ATTACH:
+               if (tp) {
+                       error = EISCONN;
+                       break;
+               }
+               tcp_attach(so);
+               tp = sototcpcb(so);
+               if (so->so_options & SO_ACCEPTCONN) {
+                       inp->inp_lhost = in_hostalloc(&n_lhost);        /*XXX*/
+                       inp->inp_lport = in_pcbgenport(&tcb);
+                       nstate = LISTEN;
+               } else
+                       nstate = CLOSED;
                break;
 
                break;
 
-       case LIS_CLS:                           /* 1 */
-               t_open(tp, PASSIVE);
-               nstate = LISTEN;
+       case PRU_DETACH:
+               tcp_detach(tp);
                break;
 
                break;
 
-       case SYS_CLS:                           /* 2 */
-               t_open(tp, ACTIVE);
-               send_ctl(tp);
+       case PRU_CONNECT:
+               if (tp->t_state != 0 && tp->t_state != CLOSED)
+                       goto bad;
+               inp->inp_fhost = in_hosteval((struct inaddr *)addr, &error);
+               if (inp->inp_fhost == 0)
+                       break;
+               (void) tcp_sndctl(tp);
                nstate = SYN_SENT;
                nstate = SYN_SENT;
+               soisconnecting(so);
                break;
 
                break;
 
-       case CLS_OPN:                           /* 10 */
-               t_close(tp, UCLOSED);
-               nstate = CLOSED;
+       case PRU_DISCONNECT:
+               if ((tp->tc_flags & TC_FIN_RCVD) == 0)
+                       goto abort;
+               if (nstate < ESTAB)
+                       tcp_disconnect(tp);
+               else {
+                       tp->tc_flags |= TC_SND_FIN;
+                       (void) tcp_sndctl(tp);
+                       tp->tc_flags |= TC_USR_CLOSED;
+                       soisdisconnecting(so);
+               }
                break;
 
                break;
 
-       case CL2_CLW:                           /* 10 */
-               tp->tc_flags |= TC_SND_FIN;
-               send_ctl(tp);
-               tp->tc_flags |= TC_USR_CLOSED;
-               nstate = CLOSING2;
+       case PRU_FLUSH:
+               error = EOPNOTSUPP;
                break;
 
                break;
 
-       case TIMERS:                            /* 14,17,34,35,36,37,38 */
-               nstate = tcp_timers(tp, timertype);
-               break;
+       case PRU_SHUTDOWN:
+               switch (nstate) {
 
 
-       case CLS_RWT:                           /* 20 */
-               present_data(tp);
-               if (rcv_empty(tp)) {
-                       t_close(tp, UCLOSED);
+               case LISTEN:
+               case SYN_SENT:
                        nstate = CLOSED;
                        nstate = CLOSED;
-               } else
-                       nstate = RCV_WAIT;
-               break;
+                       break;
+
+               case SYN_RCVD:
+               case L_SYN_RCVD:
+               case ESTAB:     
+               case CLOSE_WAIT:
+                       tp->tc_flags |= TC_SND_FIN;
+                       (void) tcp_sndctl(tp);
+                       tp->tc_flags |= TC_USR_CLOSED;
+                       nstate = nstate != CLOSE_WAIT ? FIN_W1 : LAST_ACK;
+                       break;
+                       
+               case FIN_W1:
+               case FIN_W2:
+               case TIME_WAIT:
+               case CLOSING:
+               case LAST_ACK:
+               case RCV_WAIT:
+                       break;
 
 
-       case FW1_SYR:                           /* 24,25 */
-               tp->tc_flags |= TC_SND_FIN;
-               send_ctl(tp);
-               tp->tc_flags |= TC_USR_CLOSED;
-               nstate = FIN_W1;
+               default:
+                       goto bad;
+               }
                break;
 
                break;
 
-       case SSS_SND:                           /* 40,41 */
-               nstate = sss_send(tp, mp);
+       case PRU_RCVD:
+               if (nstate < ESTAB || nstate == CLOSED)
+                       goto bad;
+               tcp_sndwin(tp);
+               if ((tp->tc_flags&TC_FIN_RCVD) &&
+                   (tp->tc_flags&TC_USR_CLOSED) == 0 &&
+                   rcv_empty(tp))
+                       error = ESHUTDOWN;
+               if (nstate == RCV_WAIT && rcv_empty(tp))
+                       nstate = CLOSED;
                break;
 
                break;
 
-       case SSS_RCV:                           /* 42 */
-               send_ctl(tp);           /* send new window */
-               present_data(tp);
-               break;
+       case PRU_SEND:
+               switch (nstate) {
 
 
-       case CLS_NSY:                           /* 44 */
-               t_close(tp, UABORT);
-               nstate = CLOSED;
+               case ESTAB:
+               case CLOSE_WAIT:
+                       tcp_usrsend(tp, m);
+                       break;
+               
+               default:
+                       if (nstate < ESTAB)
+                               goto bad;
+                       m_freem(m);
+                       error = ENOTCONN;
+                       break;
+               }
                break;
 
                break;
 
-       case CLS_SYN:                           /* 45 */
-               tp->tc_flags |= TC_SND_RST;
-               send_null(tp);
-               t_close(tp, UABORT);
+abort:
+       case PRU_ABORT:
+               tcp_abort(tp);
                nstate = CLOSED;
                break;
 
                nstate = CLOSED;
                break;
 
-       case CLS_ACT:                           /* 47 */
-               t_close(tp, UNETDWN);
-               nstate = CLOSED;
+       case PRU_CONTROL:
+               error = EOPNOTSUPP;
                break;
 
                break;
 
-       case NOP:
+       case PRU_SLOWTIMO:
+               switch (nstate) {
+
+               case 0:
+               case CLOSED:
+               case LISTEN:
+                       goto bad;
+
+               default:
+                       nstate = tcp_timers(tp, (int)addr);
+               }
                break;
 
                break;
 
-       case CLS_ERR:
-               to_user(tp->t_ucb, UCLSERR);
+       default:
+               panic("tcp_usrreq");
+       bad:
+               printf("tcp: bad state: tcb=%x state=%d input=%d\n",
+                   tp, tp->t_state, req);
+               nstate = EFAILEC;
                break;
        }
 #ifdef TCPDEBUG
        if (tdb.td_tod)
                tdb_stuff(&tdb, nstate);
 #endif
                break;
        }
 #ifdef TCPDEBUG
        if (tdb.td_tod)
                tdb_stuff(&tdb, nstate);
 #endif
-       /* YECH */
        switch (nstate) {
 
        case CLOSED:
        switch (nstate) {
 
        case CLOSED:
@@ -160,8 +286,8 @@ COUNT(TCP_USRREQ);
                break;
 
        case EFAILEC:
                break;
 
        case EFAILEC:
-               if (mp)
-                       m_freem(dtom(mp));
+               if (m)
+                       m_freem(dtom(m));
                break;
 
        default:
                break;
 
        default:
@@ -169,179 +295,107 @@ COUNT(TCP_USRREQ);
                break;
        }
        splx(s);
                break;
        }
        splx(s);
+       return (error);
 }
 
 }
 
-t_open(tp, mode)                /* set up a tcb for a connection */
-       register struct tcb *tp;
-       int mode;
+tcp_attach(so)
+       struct socket *so;
 {
 {
-       register struct ucb *up;
-COUNT(T_OPEN);
-
-       /* enqueue the tcb */
-
-       if (tcb_head == NULL) {
-               tcb_head = tp;
-               tcb_tail = tp;
-       } else {
-               tp->t_tcb_next = tcb_head;
-               tcb_head->t_tcb_prev = tp;
-               tcb_head = tp;
-       }
+       register struct tcpcb *tp = sototcpcb(so);
+COUNT(TCP_ATTACH);
 
 
-       /* initialize non-zero tcb fields */
+       /*
+        * Make empty reassembly queue.
+        */
+       tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
 
 
-       tp->t_rcv_next = (struct th *)tp;
-       tp->t_rcv_prev = (struct th *)tp;
+       /*
+        * Initialize sequence numbers and round trip retransmit timer.
+        */
        tp->t_xmtime = T_REXMT;
        tp->t_xmtime = T_REXMT;
-       tp->snd_end = tp->seq_fin = tp->snd_nxt = tp->snd_hi =
-                     tp->snd_una = tp->iss = tcp_iss;
+       tp->snd_end = tp->seq_fin = tp->snd_nxt = tp->snd_hi = tp->snd_una =
+           tp->iss = tcp_iss;
        tp->snd_off = tp->iss + 1;
        tcp_iss += (ISSINCR >> 1) + 1;
        tp->snd_off = tp->iss + 1;
        tcp_iss += (ISSINCR >> 1) + 1;
-
-       /* set timeout for open */
-
-       up = tp->t_ucb;
-       tp->t_init = (up->uc_timeo != 0 ? up->uc_timeo :
-                                       (mode == ACTIVE ? T_INIT : 0));
-       up->uc_timeo = 0;       /* overlays uc_ssize */
 }
 
 }
 
-t_close(tp, state)
-       register struct tcb *tp;
-       short state;
+tcp_detach(tp)
+       struct tcpcb *tp;
 {
 {
-       register struct ucb *up;
-       register struct th *t;
-       register struct mbuf *m;
-COUNT(T_CLOSE);
-
-       up = tp->t_ucb;
-
-       tp->t_init = tp->t_rexmt = tp->t_rexmttl = tp->t_persist = 
-           tp->t_finack = 0;
+COUNT(TCP_DETACH);
 
 
-       /* delete tcb */
-
-       if (tp->t_tcb_prev == NULL)
-               tcb_head = tp->t_tcb_next;
-       else
-               tp->t_tcb_prev->t_tcb_next = tp->t_tcb_next;
-       if (tp->t_tcb_next == NULL)
-               tcb_tail = tp->t_tcb_prev;
-       else
-               tp->t_tcb_next->t_tcb_prev = tp->t_tcb_prev;
+       in_pcbfree(tp->t_inpcb);
+       (void) m_free(dtom(tp));
+}
 
 
-       /* free all data on receive and send buffers */
+tcp_disconnect(tp)
+       register struct tcpcb *tp;
+{
+       register struct tcpiphdr *t;
 
 
-       for (t = tp->t_rcv_next; t != (struct th *)tp; t = t->t_next)
+       tcp_tcancel(tp);
+       t = tp->seg_next;
+       for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
                m_freem(dtom(t));
                m_freem(dtom(t));
-
-       if (up->uc_rbuf != NULL) {
-               m_freem(up->uc_rbuf);
-               up->uc_rbuf = NULL;
-       }
-       up->uc_rcc = 0;
-       if (up->uc_sbuf != NULL) {
-               m_freem(up->uc_sbuf);
-               up->uc_sbuf = NULL;
-       }
-       up->uc_ssize = 0;
-       for (m = tp->t_rcv_unack; m != NULL; m = m->m_act) {
-               m_freem(m);
-               tp->t_rcv_unack = NULL;
-       }
-       if (up->uc_template) {
-               m_free(dtom(up->uc_template));
-               up->uc_template = 0;
-       }
-       m = dtom(tp);
-       m->m_off = 0;
-       m_free(m);
-       up->uc_tcb = NULL;
-
-       /* lower buffer allocation and decrement host entry */
-
-       mbstat.m_lowat -= up->uc_snd + (up->uc_rhiwat/MSIZE) + 2;
-       mbstat.m_hiwat = 2 * mbstat.m_lowat;
-       if (up->uc_host != NULL) {
-               h_free(up->uc_host);
-               up->uc_host = NULL;
+       tcp_drainunack(tp);
+       if (tp->t_template) {
+               (void) m_free(dtom(tp->t_template));
+               tp->t_template = 0;
        }
        }
+       in_pcbfree(tp->t_inpcb);
+}
 
 
-       /* if user has initiated close (via close call), delete ucb
-          entry, otherwise just wakeup so user can issue close call */
+tcp_abort(tp)
+       register struct tcpcb *tp;
+{
+
+       switch (tp->t_state) {
 
 
-       if (tp->tc_flags&TC_USR_ABORT)
-               up->uc_proc = NULL;
-       else
-               to_user(up, state);
+       case SYN_RCVD:
+       case ESTAB:
+       case FIN_W1:
+       case FIN_W2:
+       case CLOSE_WAIT:
+               tp->tc_flags |= TC_SND_RST;
+               tcp_sndnull(tp);
+       }
+       soisdisconnected(tp->t_inpcb->inp_socket);
 }
 
 }
 
-sss_send(tp, m0)
-       register struct tcb *tp;
+/*
+/*###366 [cc] warning: struct/union or struct/union pointer required %%%*/
+/*###366 [cc] member of structure or union required %%%*/
+/*###366 [cc] tp_inpcb undefined %%%*/
+ * Send data queue headed by m0 into the protocol.
+ */
+tcp_usrsend(tp, m0)
+       register struct tcpcb *tp;
        struct mbuf *m0;
 {
        struct mbuf *m0;
 {
-       register struct mbuf *m, *n;
-       register struct ucb *up = tp->t_ucb;
-       register off;
-       seq_t last;
-COUNT(SSS_SEND);
-
-       last = tp->snd_off;
-       for (m = n = m0; m != NULL; m = m->m_next) {
-               up->uc_ssize++;
-               if (m->m_off > MMAXOFF)
-                       up->uc_ssize += NMBPG;
-               last += m->m_len;
-       }
-       if ((m = up->uc_sbuf) == NULL)
-               up->uc_sbuf = n;
-       else {
-               while (m->m_next != NULL) {
-                       m = m->m_next;
-                       last += m->m_len;
-               }
-               if (m->m_off <= MMAXOFF) {
-                       last += m->m_len;
-                       off = m->m_off + m->m_len;
-                       while (n && n->m_off <= MMAXOFF &&
-                           (MMAXOFF - off) >= n->m_len) {
-                               bcopy((caddr_t)((int)n + n->m_off),
-                                     (caddr_t)((int)m + off), n->m_len);
-                               m->m_len += n->m_len;
-                               off += n->m_len;
-                               up->uc_ssize--;
-                               n = m_free(n);
-                       }
-               }
-               m->m_next = n;
-       }
-       if (up->uc_flags & UEOL)
-               tp->snd_end = last;
-       if (up->uc_flags & UURG) {
-               tp->snd_urp = last+1;
+       register struct socket *so = tp->t_inpcb->inp_socket;
+COUNT(TCP_USRSEND);
+
+       sbappend(&so->so_snd, m0);
+       if (tp->t_options & TO_EOL)
+               tp->snd_end = tp->snd_off + so->so_snd.sb_cc;
+       if (tp->t_options & TO_URG) {
+               tp->snd_urp = tp->snd_off + so->so_snd.sb_cc + 1;
                tp->tc_flags |= TC_SND_URG;
        }
                tp->tc_flags |= TC_SND_URG;
        }
-       send(tp);
-       return (SAME);
+       (void) tcp_send(tp);
 }
 
 }
 
+/*
+ * TCP timer went off processing.
+ */
 tcp_timers(tp, timertype)
 tcp_timers(tp, timertype)
-       register struct tcb *tp;
+       register struct tcpcb *tp;
        int timertype;
 {
 
 COUNT(TCP_TIMERS);
        switch (timertype) {
 
        int timertype;
 {
 
 COUNT(TCP_TIMERS);
        switch (timertype) {
 
-       case TINIT:             /* initialization timer */
-               if ((tp->tc_flags&TC_SYN_ACKED) == 0) {         /* 35 */
-                       t_close(tp, UINTIMO);
-                       return (CLOSED);
-               }
-               return (SAME);
-
        case TFINACK:           /* fin-ack timer */
                switch (tp->t_state) {
 
        case TFINACK:           /* fin-ack timer */
                switch (tp->t_state) {
 
@@ -351,12 +405,12 @@ COUNT(TCP_TIMERS);
                         * and can close if no data left for user.
                         */
                        if (rcv_empty(tp)) {
                         * and can close if no data left for user.
                         */
                        if (rcv_empty(tp)) {
-                               t_close(tp, UCLOSED);           /* 14 */
+                               tcp_disconnect(tp);
                                return (CLOSED);
                        }
                        return (RCV_WAIT);                      /* 17 */
 
                                return (CLOSED);
                        }
                        return (RCV_WAIT);                      /* 17 */
 
-               case CLOSING1:
+               case CLOSING:
                        tp->tc_flags |= TC_WAITED_2_ML;
                        return (SAME);
 
                        tp->tc_flags |= TC_WAITED_2_ML;
                        return (SAME);
 
@@ -367,7 +421,7 @@ COUNT(TCP_TIMERS);
        case TREXMT:            /* retransmission timer */
                if (tp->t_rexmt_val > tp->snd_una) {            /* 34 */
                        /*
        case TREXMT:            /* retransmission timer */
                if (tp->t_rexmt_val > tp->snd_una) {            /* 34 */
                        /*
-                        * Set up for a retransmission, increase rexmt time
+                        * Set so for a retransmission, increase rexmt time
                         * in case of multiple retransmissions.
                         */
                        tp->snd_nxt = tp->snd_una;
                         * in case of multiple retransmissions.
                         */
                        tp->snd_nxt = tp->snd_una;
@@ -375,18 +429,18 @@ COUNT(TCP_TIMERS);
                        tp->t_xmtime = tp->t_xmtime << 1;
                        if (tp->t_xmtime > T_REMAX)
                                tp->t_xmtime = T_REMAX;
                        tp->t_xmtime = tp->t_xmtime << 1;
                        if (tp->t_xmtime > T_REMAX)
                                tp->t_xmtime = T_REMAX;
-                       send(tp);
+                       (void) tcp_send(tp);
                }
                return (SAME);
 
        case TREXMTTL:          /* retransmit too long */
                if (tp->t_rtl_val > tp->snd_una)                /* 36 */
                }
                return (SAME);
 
        case TREXMTTL:          /* retransmit too long */
                if (tp->t_rtl_val > tp->snd_una)                /* 36 */
-                       to_user(tp->t_ucb, URXTIMO);
+                       tcp_error(tp, EIO);             /* URXTIMO !?! */
                /*
                 * If user has already closed, abort the connection.
                 */
                if (tp->tc_flags & TC_USR_CLOSED) {
                /*
                 * If user has already closed, abort the connection.
                 */
                if (tp->tc_flags & TC_USR_CLOSED) {
-                       t_close(tp, URXTIMO);
+                       tcp_abort(tp);
                        return (CLOSED);
                }
                return (SAME);
                        return (CLOSED);
                }
                return (SAME);
@@ -396,44 +450,108 @@ COUNT(TCP_TIMERS);
                 * Force a byte send through closed window.
                 */
                tp->tc_flags |= TC_FORCE_ONE;
                 * Force a byte send through closed window.
                 */
                tp->tc_flags |= TC_FORCE_ONE;
-               send(tp);
+               (void) tcp_send(tp);
                return (SAME);
        }
        panic("tcp_timers");
                return (SAME);
        }
        panic("tcp_timers");
+       /*NOTREACHED*/
+}
+
+/*ARGSUSED*/
+tcp_sense(m)
+       struct mbuf *m;
+{
+
+       return (EOPNOTSUPP);
 }
 
 }
 
-/* THIS ROUTINE IS A CROCK */
-to_user(up, state)
-       register struct ucb *up;
-       register short state;
+tcp_error(tp, errno)
+       struct tcpcb *tp;
+       int errno;
 {
 {
-COUNT(TO_USER);
+       struct socket *so = tp->t_inpcb->inp_socket;
+COUNT(TCP_ERROR);
 
 
-       up->uc_state |= state;
-       netwakeup(up);
-       if (state == UURGENT)
-               psignal(up->uc_proc, SIGURG);
+       so->so_error = errno;
+       sorwakeup(so);
+       sowwakeup(so);
 }
 
 #ifdef TCPDEBUG
 }
 
 #ifdef TCPDEBUG
+/*
+ * TCP debugging utility subroutines.
+ * THE NAMES OF THE FIELDS USED BY THESE ROUTINES ARE STUPID.
+ */
+tdb_setup(tp, n, input, tdp)
+       struct tcpcb *tp;
+       register struct tcpiphdr *n;
+       int input;
+       register struct tcp_debug *tdp;
+{
+
+COUNT(TDB_SETUP);
+       tdp->td_tod = time;
+       tdp->td_tcb = tp;
+       tdp->td_old = tp->t_state;
+       tdp->td_inp = input;
+       tdp->td_tim = 0;
+       tdp->td_new = -1;
+       if (n) {
+               tdp->td_sno = n->ti_seq;
+               tdp->td_ano = n->ti_ackno;
+               tdp->td_wno = n->t_win;
+               tdp->td_lno = n->ti_len;
+               tdp->td_flg = n->ti_flags;
+       } else
+               tdp->td_sno = tdp->td_ano = tdp->td_wno = tdp->td_lno =
+                   tdp->td_flg = 0;
+}
+
+tdb_stuff(tdp, nstate)
+       struct tcp_debug *tdp;
+       int nstate;
+{
+COUNT(TDB_STUFF);
+
+       tdp->td_new = nstate;
+       tcp_debug[tdbx++ % TDBSIZE] = *tdp;
+       if (tcpconsdebug & 2)
+               tcp_prt(tdp);
+}
+
 tcp_prt(tdp)
        register struct tcp_debug *tdp;
 {
 COUNT(TCP_PRT);
 
 tcp_prt(tdp)
        register struct tcp_debug *tdp;
 {
 COUNT(TCP_PRT);
 
-       printf("TCP(%x) %s x %s",
-           tdp->td_tcb, tcpstates[tdp->td_old], tcpinputs[tdp->td_inp]);
-       if (tdp->td_inp == ISTIMER)
-               printf("(%s)", tcptimers[tdp->td_tim]);
-       printf(" --> %s",
-           tcpstates[(tdp->td_new > 0) ? tdp->td_new : tdp->td_old]);
-       /* GROSS... DEPENDS ON SIGN EXTENSION OF CHARACTERS */
-       if (tdp->td_new < 0)
-               printf(" (FAILED)");
-       if (tdp->td_sno) {
-               printf(" sno %x ano %x win %d len %d flags %x",
-                   tdp->td_sno, tdp->td_ano, tdp->td_wno, tdp->td_lno, tdp->td_flg);
+       printf("%x ", ((int)tdp->td_tcb)&0xffffff);
+       if (tdp->td_inp == INSEND) {
+               printf("SEND #%x", tdp->td_sno);
+               tdp->td_lno = ntohs(tdp->td_lno);
+               tdp->td_wno = ntohs(tdp->td_wno);
+       } else {
+               if (tdp->td_inp == INRECV)
+                       printf("RCV #%x ", tdp->td_sno);
+               printf("%s.%s",
+                   tcpstates[tdp->td_old], tcpinputs[tdp->td_inp]);
+               if (tdp->td_inp == ISTIMER)
+                       printf("(%s)", tcptimers[tdp->td_tim]);
+               printf(" -> %s",
+                   tcpstates[(tdp->td_new > 0) ? tdp->td_new : tdp->td_old]);
+               if (tdp->td_new == -1)
+                       printf(" (FAILED)");
        }
        }
+       /* GROSS... DEPENDS ON SIGN EXTENSION OF CHARACTERS */
+       if (tdp->td_lno)
+               printf(" len=%d", tdp->td_lno);
+       if (tdp->td_wno)
+               printf(" win=%d", tdp->td_wno);
+       if (tdp->td_flg & TH_FIN) printf(" FIN");
+       if (tdp->td_flg & TH_SYN) printf(" SYN");
+       if (tdp->td_flg & TH_RST) printf(" RST");
+       if (tdp->td_flg & TH_EOL) printf(" EOL");
+       if (tdp->td_flg & TH_ACK)  printf(" ACK %x", tdp->td_ano);
+       if (tdp->td_flg & TH_URG) printf(" URG");
        printf("\n");
 }
 #endif
        printf("\n");
 }
 #endif