listing
[unix-history] / usr / src / sys / netinet / tcp_usrreq.c
index feeb1f9..abad774 100644 (file)
-/* tcp_usrreq.c 1.7 81/10/22 */
+/* tcp_usrreq.c 1.35 81/11/25 */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
-#include "../bbnnet/net.h"
-#include "../bbnnet/mbuf.h"
-#include "../bbnnet/tcp.h"
-#include "../bbnnet/ip.h"
-#include "../bbnnet/imp.h"
-#include "../bbnnet/ucb.h"
-#define TCPFSTAB
-#ifdef TCPDEBUG
-#define TCPSTATES
-#endif
-#include "../bbnnet/fsm.h"
-
-tcp_timeo()
+#include "../h/mbuf.h"
+#include "../h/socket.h"
+#include "../h/socketvar.h"
+#include "../h/protosw.h"
+#include "../net/inet.h"
+#include "../net/inet_pcb.h"
+#include "../net/inet_systm.h"
+#include "../net/if.h"
+#include "../net/imp.h"
+#include "../net/ip.h"
+#include "../net/ip_var.h"
+#include "../net/tcp.h"
+#include "../net/tcp_fsm.h"
+#include "../net/tcp_var.h"
+#include "/usr/include/errno.h"
+
+struct tcpcb *tcp_newtcpcb();
+/*
+ * 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 tcb *tp;
+       register struct inpcb *inp = sotoinpcb(so);
+       register struct tcpcb *tp;
        int s = splnet();
        int s = splnet();
-COUNT(TCP_TIMEO);
+       int error = 0;
+       struct tcpiphdr ti;
+COUNT(TCP_USRREQ);
 
        /*
 
        /*
-        * Search through tcb's and update active timers.
+        * Make sure attached.  If not,
+        * only PRU_ATTACH is valid.
         */
         */
-       for (tp = netcb.n_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);
-               tp->t_xmt++;
+       if (inp == 0 && req != PRU_ATTACH)
+               splx(s);
+               return (EINVAL);
        }
        }
-       netcb.n_iss += ISSINCR;         /* increment iss */
-       timeout(tcp_timeo, 0, hz);      /* reschedule every second */
-       splx(s);
-}
-
-tcp_usrreq(input, timertype, tp, mp)
-       int input, timertype;
-       register struct tcb *tp;
-       struct mbuf *mp;
-{
-       int s = splnet();
-       register int nstate;
-#ifdef TCPDEBUG
-       struct tcp_debug tdb;
+       if (inp) {
+               tp = intotcpcb(inp);
+#ifdef KPROF
+               tcp_acounts[tp->t_state][req]++;
 #endif
 #endif
-COUNT(TCP_USRREQ);
-
-       nstate = tp->t_state;
-       tp->tc_flags &= ~TC_NET_KEEP;
-       acounts[nstate][input]++;
-#ifdef TCPDEBUG
-       if ((tp->t_ucb->uc_flags & UDEBUG) || tcpconsdebug) {
-               tdb.td_tod = time;
-               tdb.td_tcb = tp;
-               tdb.td_old = nstate;
-               tdb.td_inp = input;
-               tdb.td_tim = timertype;
-       } else
-               tdb.td_tod = 0;
-#endif
-       switch (tcp_fstab[nstate][input]) {
-
-       default:
-               printf("tcp: bad state: tcb=%x state=%d input=%d\n",
-                   tp, tp->t_state, input);
-               nstate = EFAILEC;
-               break;
-
-       case LIS_CLS:                           /* 1 */
-               t_open(tp, PASSIVE);
-               nstate = LISTEN;
-               break;
-
-       case SYS_CLS:                           /* 2 */
-               t_open(tp, ACTIVE);
-               send_ctl(tp);
-               nstate = SYN_SENT;
-               break;
-
-       case CLS_OPN:                           /* 10 */
-               t_close(tp, UCLOSED);
-               nstate = CLOSED;
-               break;
-
-       case CL2_CLW:                           /* 10 */
-               tp->tc_flags |= TC_SND_FIN;
-               send_ctl(tp);
-               tp->tc_flags |= TC_USR_CLOSED;
-               nstate = CLOSING2;
-               break;
-
-       case TIMERS:                            /* 14,17,34,35,36,37,38 */
-               nstate = tcp_timers(tp, timertype);
-               break;
+       }
+       switch (req) {
 
 
-       case CLS_RWT:                           /* 20 */
-               present_data(tp);
-               if (rcv_empty(tp)) {
-                       t_close(tp, UCLOSED);
-                       nstate = CLOSED;
+       case PRU_ATTACH:
+               if (inp) {
+                       error = EISCONN;
+                       break;
+               }
+               error = in_pcballoc(so, &tcb, 2048, 2048, (struct sockaddr_in *)addr);
+               if (error)
+                       break;
+               inp = (struct inpcb *)so->so_pcb;
+               if (so->so_options & SO_ACCEPTCONN) {
+                       tp = tcp_newtcpcb(inp);
+                       if (tp == 0) {
+                               in_pcbfree(inp);
+                               error = ENOBUFS;
+                               break;
+                       }
+                       tp->t_state = TCPS_LISTEN;
                } else
                } else
-                       nstate = RCV_WAIT;
+                       tp->t_state = TCPS_CLOSED;
                break;
 
                break;
 
-       case FW1_SYR:                           /* 24,25 */
-               tp->tc_flags |= TC_SND_FIN;
-               send_ctl(tp);
-               tp->tc_flags |= TC_USR_CLOSED;
-               nstate = FIN_W1;
+       case PRU_DETACH:
                break;
 
                break;
 
-       case SSS_SND:                           /* 40,41 */
-               nstate = sss_snd(tp, mp);
+       case PRU_CONNECT:
+               error = in_pcbsetpeer(inp, (struct sockaddr_in *)addr);
+               if (error)
+                       break;
+               tp = tcp_newtcpcb(inp);
+               if (tp == 0) {
+                       inp->inp_faddr.s_addr = 0;
+                       error = ENOBUFS;
+                       break;
+               }
+               tp->t_inpcb = inp;
+               inp->inp_ppcb = (caddr_t)tp;
+               soisconnecting(so);
+               tp->t_state = TCPS_SYN_SENT;
+               tcp_output(tp);
                break;
 
                break;
 
-       case SSS_RCV:                           /* 42 */
-               send_ctl(tp);           /* send new window */
-               present_data(tp);
+       case PRU_ACCEPT:
+               soisconnected(so);
                break;
 
                break;
 
-       case CLS_NSY:                           /* 44 */
-               t_close(tp, UABORT);
-               nstate = CLOSED;
+       case PRU_DISCONNECT:
+               if (tp->t_state < TCPS_ESTABLISHED)
+                       tcp_close(tp);
+               else {
+                       soisdisconnecting(so);
+                       tcp_output(tp);
+               }
                break;
 
                break;
 
-       case CLS_SYN:                           /* 45 */
-               tp->tc_flags |= TC_SND_RST;
-               send_null(tp);
-               t_close(tp, UABORT);
-               nstate = CLOSED;
+       case PRU_SHUTDOWN:
+               socantsndmore(so);
+               switch (tp->t_state) {
+
+               case TCPS_LISTEN:
+               case TCPS_SYN_SENT:
+                       tp->t_state = TCPS_CLOSED;
+                       break;
+
+               case TCPS_SYN_RCVD:
+               case TCPS_ESTAB:
+                       tp->t_state = TCPS_FIN_WAIT_1;
+                       tcp_output(tp);
+                       break;
+
+               case TCPS_CLOSE_WAIT:
+                       tp->t_state = TCPS_LAST_ACK;
+                       tcp_output(tp);
+                       break;
+               }
                break;
 
                break;
 
-       case CLS_ACT:                           /* 47 */
-               t_close(tp, UNETDWN);
-               nstate = CLOSED;
+       case PRU_RCVD:
+               if (tp->t_state < TCPS_ESTABLISHED) {
+                       error = ENOTCONN;
+                       break;
+               }
+               tcp_output(tp);
                break;
 
                break;
 
-       case NOP:
+       case PRU_SEND:
+               if (tp->t_state < TCPS_ESTABLISHED) {
+                       error = ENOTCONN;
+                       break;
+               }
+               if (tp->t_state > TCPS_CLOSE_WAIT) {
+                       error = EISDISCONN;
+                       m_freem(m);
+                       break;
+               }
+               sbappend(&so->so_snd, m);
+               if (tp->t_options & TO_EOL)
+                       tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
+               if (tp->t_options & TO_URG)
+                       tp->snd_urp = tp->snd_una + so->so_snd.sb_cc + 1;
+               tcp_output(tp);
                break;
 
                break;
 
-       case CLS_ERR:
-               to_user(tp->t_ucb, UCLSERR);
+       case PRU_ABORT:
+               tcp_drop(tp, ECONNABORTED);
                break;
                break;
-       }
-#ifdef TCPDEBUG
-       if (tdb.td_tod) {
-               tdb.td_new = nstate;
-               tcp_debug[tdbx++ % TDBSIZE] = tdb;
-               if (tcpconsdebug)
-                       tcp_prt(&tdb);
-       }
-#endif
-       /* YECH */
-       switch (nstate) {
 
 
-       case CLOSED:
-       case SAME:
+       case PRU_CONTROL:
+               error = EOPNOTSUPP;
                break;
 
                break;
 
-       case EFAILEC:
-               if (mp)
-                       m_freem(dtom(mp));
+       case PRU_SLOWTIMO:
+               tcp_timers(tp, (int)addr);
                break;
 
        default:
                break;
 
        default:
-               tp->t_state = nstate;
-               break;
+               panic("tcp_usrreq");
        }
        splx(s);
        }
        splx(s);
+       return (error);
 }
 }
-
-t_open(tp, mode)                /* set up a tcb for a connection */
-       register struct tcb *tp;
-       int mode;
-{
-       register struct ucb *up;
-COUNT(T_OPEN);
-
-       /* enqueue the tcb */
-
-       if (netcb.n_tcb_head == NULL) {
-               netcb.n_tcb_head = tp;
-               netcb.n_tcb_tail = tp;
-       } else {
-               tp->t_tcb_next = netcb.n_tcb_head;
-               netcb.n_tcb_head->t_tcb_prev = tp;
-               netcb.n_tcb_head = tp;
-       }
-
-       /* initialize non-zero tcb fields */
-
-       tp->t_rcv_next = (struct th *)tp;
-       tp->t_rcv_prev = (struct th *)tp;
-       tp->t_xmtime = T_REXMT;
-       tp->snd_end = tp->seq_fin = tp->snd_nxt = tp->snd_hi =
-                     tp->snd_una = tp->iss = netcb.n_iss;
-       tp->snd_off = tp->iss + 1;
-       netcb.n_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;
-{
-       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;
-
-       /* delete tcb */
-
-       if (tp->t_tcb_prev == NULL)
-               netcb.n_tcb_head = tp->t_tcb_next;
-       else
-               tp->t_tcb_prev->t_tcb_next = tp->t_tcb_next;
-       if (tp->t_tcb_next == NULL)
-               netcb.n_tcb_tail = tp->t_tcb_prev;
-       else
-               tp->t_tcb_next->t_tcb_prev = tp->t_tcb_prev;
-
-       /* free all data on receive and send buffers */
-
-       for (t = tp->t_rcv_next; t != (struct th *)tp; t = t->t_next)
-               m_freem(dtom(t));
-
-       if (up->uc_rbuf != NULL) {
-               m_freem(up->uc_rbuf);
-               up->uc_rbuf = NULL;
-       }
-       if (up->uc_sbuf != NULL) {
-               m_freem(up->uc_sbuf);
-               up->uc_sbuf = NULL;
-       }
-       for (m = tp->t_rcv_unack; m != NULL; m = m->m_act) {
-               m_freem(m);
-               tp->t_rcv_unack = NULL;
-       }
-       m_free(dtom(tp));
-       up->uc_tcb = NULL;
-
-       /* lower buffer allocation and decrement host entry */
-
-       netcb.n_lowat -= up->uc_snd + up->uc_rcv + 2;
-       netcb.n_hiwat = 2 * netcb.n_lowat;
-       if (up->uc_host != NULL) {
-               h_free(up->uc_host);
-               up->uc_host = NULL;
-       }
-
-       /* if user has initiated close (via close call), delete ucb
-          entry, otherwise just wakeup so user can issue close call */
-
-       if (tp->tc_flags&TC_USR_ABORT)
-               up->uc_proc = NULL;
-       else
-               to_user(up, state);
-}
-
-sss_snd(tp, m0)
-       register struct tcb *tp;
-       struct mbuf *m0;
-{
-       register struct mbuf *m, *n;
-       register struct ucb *up = tp->t_ucb;
-       register off;
-       seq_t last;
-
-       last = tp->snd_off;
-
-       /* count number of mbufs in send data */
-
-       for (m = n = m0; m != NULL; m = m->m_next) {
-               up->uc_ssize++;
-               last += m->m_len;
-       }
-
-       /* find end of send buffer and append data */
-
-       if ((m = up->uc_sbuf) != NULL) {                /* something in send buffer */
-               while (m->m_next != NULL) {             /* find the end */
-                       m = m->m_next;
-                       last += m->m_len;
-               }
-               last += m->m_len;
-
-               /* if there's room in old buffer for new data, consolidate */
-
-               off = m->m_off + m->m_len;
-               while (n != NULL && (MSIZE - 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;
-
-       } else          /* nothing in send buffer */
-               up->uc_sbuf = n;
-
-       if (up->uc_flags & UEOL) {              /* set EOL */
-               tp->snd_end = last;
-       }
-
-       if (up->uc_flags & UURG) {              /* urgent data */
-               tp->snd_urp = last+1;
-               tp->tc_flags |= TC_SND_URG;
-       }
-
-       send(tp);
-
-       return (SAME);
-}
-
-tcp_timers(tp, timertype)
-       register struct tcb *tp;
-       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 TIME_WAIT:
-                       /*
-                        * We can be sure our ACK of foreign FIN was rcvd,
-                        * and can close if no data left for user.
-                        */
-                       if (rcv_empty(tp)) {
-                               t_close(tp, UCLOSED);           /* 14 */
-                               return (CLOSED);
-                       }
-                       return (RCV_WAIT);                      /* 17 */
-
-               case CLOSING1:
-                       tp->tc_flags |= TC_WAITED_2_ML;
-                       return (SAME);
-
-               default:
-                       return (SAME);
-               }
-
-       case TREXMT:            /* retransmission timer */
-               if (tp->t_rexmt_val > tp->snd_una) {            /* 34 */
-                       /*
-                        * Set up for a retransmission, increase rexmt time
-                        * in case of multiple retransmissions.
-                        */
-                       tp->snd_nxt = tp->snd_una;
-                       tp->tc_flags |= TC_REXMT;
-                       tp->t_xmtime = tp->t_xmtime << 1;
-                       if (tp->t_xmtime > T_REMAX)
-                               tp->t_xmtime = T_REMAX;
-                       send(tp);
-               }
-               return (SAME);
-
-       case TREXMTTL:          /* retransmit too long */
-               if (tp->t_rtl_val > tp->snd_una)                /* 36 */
-                       to_user(tp->t_ucb, URXTIMO);
-               /*
-                * If user has already closed, abort the connection.
-                */
-               if (tp->tc_flags & TC_USR_CLOSED) {
-                       t_close(tp, URXTIMO);
-                       return (CLOSED);
-               }
-               return (SAME);
-
-       case TPERSIST:          /* persist timer */
-               /*
-                * Force a byte send through closed window.
-                */
-               tp->tc_flags |= TC_FORCE_ONE;
-               send(tp);
-               return (SAME);
-       }
-       panic("tcp_timers");
-}
-
-/* THIS ROUTINE IS A CROCK */
-to_user(up, state)
-       register struct ucb *up;
-       register short state;
-{
-COUNT(TO_USER);
-
-       up->uc_state |= state;
-       netwakeup(up);
-       if (state == UURGENT)
-               psignal(up->uc_proc, SIGURG);
-}
-
-#ifdef TCPDEBUG
-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)");
-       printf("\n");
-}
-#endif