typed mbufs
[unix-history] / usr / src / sys / netinet / tcp_subr.c
index 3938b7e..0995957 100644 (file)
@@ -1,4 +1,4 @@
-/* tcp_subr.c 4.2 81/11/25 */
+/*     tcp_subr.c      4.36    82/12/14        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -6,18 +6,21 @@
 #include "../h/socket.h"
 #include "../h/socketvar.h"
 #include "../h/protosw.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 "../netinet/in.h"
+#include "../net/route.h"
+#include "../netinet/in_pcb.h"
+#include "../netinet/in_systm.h"
 #include "../net/if.h"
 #include "../net/if.h"
-#include "../net/imp.h"
-#include "../net/ip.h"
-#include "../net/ip_var.h"
-#include "../net/tcp.h"
-#define TCPFSTAB
-#include "../net/tcp_fsm.h"
-#include "../net/tcp_var.h"
-#include "/usr/include/errno.h"
+#include "../netinet/ip.h"
+#include "../netinet/ip_var.h"
+#include "../netinet/ip_icmp.h"
+#include "../netinet/tcp.h"
+#include "../netinet/tcp_fsm.h"
+#include "../netinet/tcp_seq.h"
+#include "../netinet/tcp_timer.h"
+#include "../netinet/tcp_var.h"
+#include "../netinet/tcpip.h"
+#include <errno.h>
 
 /*
  * Tcp initialization
 
 /*
  * Tcp initialization
@@ -27,6 +30,8 @@ tcp_init()
 
        tcp_iss = 1;            /* wrong */
        tcb.inp_next = tcb.inp_prev = &tcb;
 
        tcp_iss = 1;            /* wrong */
        tcb.inp_next = tcb.inp_prev = &tcb;
+       tcp_alpha = TCP_ALPHA;
+       tcp_beta = TCP_BETA;
 }
 
 /*
 }
 
 /*
@@ -43,8 +48,7 @@ tcp_template(tp)
        register struct mbuf *m;
        register struct tcpiphdr *n;
 
        register struct mbuf *m;
        register struct tcpiphdr *n;
 
-COUNT(TCP_TEMPLATE);
-       m = m_get(1);
+       m = m_get(M_WAIT, MT_HEADER);
        if (m == 0)
                return (0);
        m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
        if (m == 0)
                return (0);
        m->m_off = MMAXOFF - sizeof (struct tcpiphdr);
@@ -59,7 +63,7 @@ COUNT(TCP_TEMPLATE);
        n->ti_sport = inp->inp_lport;
        n->ti_dport = inp->inp_fport;
        n->ti_seq = 0;
        n->ti_sport = inp->inp_lport;
        n->ti_dport = inp->inp_fport;
        n->ti_seq = 0;
-       n->ti_ackno = 0;
+       n->ti_ack = 0;
        n->ti_x2 = 0;
        n->ti_off = 5;
        n->ti_flags = 0;
        n->ti_x2 = 0;
        n->ti_off = 5;
        n->ti_flags = 0;
@@ -70,124 +74,176 @@ COUNT(TCP_TEMPLATE);
 }
 
 /*
 }
 
 /*
- * Reflect a control message back to sender of tcp segment ti,
- * with ack, seq and flags fields as specified by parameters.
+ * Send a single message to the TCP at address specified by
+ * the given TCP/IP header.  If flags==0, then we make a copy
+ * of the tcpiphdr at ti and send directly to the addressed host.
+ * This is used to force keep alive messages out using the TCP
+ * template for a connection tp->t_template.  If flags are given
+ * then we send a message back to the TCP which originated the
+ * segment ti, and discard the mbuf containing it and any other
+ * attached mbufs.
+ *
+ * In any case the ack and sequence number of the transmitted
+ * segment are as specified by the parameters.
  */
  */
-tcp_reflect(ti, ack, seq, flags)
+tcp_respond(tp, ti, ack, seq, flags)
+       struct tcpcb *tp;
        register struct tcpiphdr *ti;
        register struct tcpiphdr *ti;
-       tcpseq_t ack, seq;
+       tcp_seq ack, seq;
        int flags;
 {
        int flags;
 {
+       struct mbuf *m;
+       int win = 0, tlen;
+       struct route *ro = 0;
 
 
-       m_freem(m->m_next);
-       m->m_next = 0;
-       m->m_len = sizeof(struct tcpiphdr);
-#define xchg(a,b) j=a; a=b; b=j
-       xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr);
-       xchg(ti->ti_dport, ti->ti_sport);
+       if (tp) {
+               win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
+               ro = &tp->t_inpcb->inp_route;
+       }
+       if (flags == 0) {
+               m = m_get(M_DONTWAIT, MT_HEADER);
+               if (m == 0)
+                       return;
+               m->m_len = sizeof (struct tcpiphdr) + 1;
+               *mtod(m, struct tcpiphdr *) = *ti;
+               ti = mtod(m, struct tcpiphdr *);
+               flags = TH_ACK;
+               tlen = 1;
+       } else {
+               m = dtom(ti);
+               m_freem(m->m_next);
+               m->m_next = 0;
+               m->m_off = (int)ti - (int)m;
+               m->m_len = sizeof (struct tcpiphdr);
+#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
+               xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long);
+               xchg(ti->ti_dport, ti->ti_sport, u_short);
 #undef xchg
 #undef xchg
-       ti->ti_ack = htonl(ack);
+               tlen = 0;
+       }
+       ti->ti_next = ti->ti_prev = 0;
+       ti->ti_x1 = 0;
+       ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
        ti->ti_seq = htonl(seq);
        ti->ti_seq = htonl(seq);
+       ti->ti_ack = htonl(ack);
+       ti->ti_x2 = 0;
+       ti->ti_off = sizeof (struct tcphdr) >> 2;
        ti->ti_flags = flags;
        ti->ti_flags = flags;
-
-       ti->ti_len = htons(sizeof (struct tcphdr));
-       ti->ti_off = 5;
-       ti->ti_sum = inet_cksum(m, sizeof(struct tcpiphdr));
-       ((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr);
-       ((struct ip *)ti)->ip_ttl = MAXTTL;
-       ip_output(m);
+       ti->ti_win = htons((u_short)win);
+       ti->ti_urp = 0;
+       ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen);
+       ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen;
+       ((struct ip *)ti)->ip_ttl = TCP_TTL;
+       (void) ip_output(m, (struct mbuf *)0, ro, 0);
 }
 
 }
 
+/*
+ * Create a new TCP control block, making an
+ * empty reassembly queue and hooking it to the argument
+ * protocol control block.
+ */
 struct tcpcb *
 tcp_newtcpcb(inp)
        struct inpcb *inp;
 {
 struct tcpcb *
 tcp_newtcpcb(inp)
        struct inpcb *inp;
 {
-       struct mbuf *m = m_getclr(0);
+       struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB);
        register struct tcpcb *tp;
        register struct tcpcb *tp;
-COUNT(TCP_NEWTCPCB);
 
        if (m == 0)
                return (0);
        tp = mtod(m, struct tcpcb *);
 
        if (m == 0)
                return (0);
        tp = mtod(m, struct tcpcb *);
-
-       /*
-        * Make empty reassembly queue.
-        */
        tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
        tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp;
-
-       /*
-        * Initialize sequence numbers and round trip retransmit timer.
-        */
-       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_off = tp->iss + 1;
-       tcp_iss += (ISSINCR >> 1) + 1;
-
-       /*
-        * Hook to inpcb.
-        */
+       tp->t_maxseg = 576;             /* satisfy the rest of the world */
+       tp->t_flags = 0;                /* sends options! */
        tp->t_inpcb = inp;
        inp->inp_ppcb = (caddr_t)tp;
        return (tp);
 }
 
        tp->t_inpcb = inp;
        inp->inp_ppcb = (caddr_t)tp;
        return (tp);
 }
 
+/*
+ * Drop a TCP connection, reporting
+ * the specified error.  If connection is synchronized,
+ * then send a RST to peer.
+ */
 tcp_drop(tp, errno)
        struct tcpcb *tp;
        int errno;
 {
        struct socket *so = tp->t_inpcb->inp_socket;
 
 tcp_drop(tp, errno)
        struct tcpcb *tp;
        int errno;
 {
        struct socket *so = tp->t_inpcb->inp_socket;
 
-COUNT(TCP_DROP);
-       if (TCPS_HAVERCVDSYN(tp->t_state) &&
-           TCPS_OURFINISACKED(tp->t_state) == 0) {
+       if (TCPS_HAVERCVDSYN(tp->t_state)) {
                tp->t_state = TCPS_CLOSED;
                tp->t_state = TCPS_CLOSED;
-               tcp_output(tp);
+               (void) tcp_output(tp);
        }
        so->so_error = errno;
        }
        so->so_error = errno;
-       socantrcvmore(so);
-       socantsndmore(so);
        tcp_close(tp);
 }
 
        tcp_close(tp);
 }
 
+tcp_abort(inp)
+       struct inpcb *inp;
+{
+
+       tcp_close((struct tcpcb *)inp->inp_ppcb);
+}
+
+/*
+ * Close a TCP control block:
+ *     discard all space held by the tcp
+ *     discard internet protocol block
+ *     wake up any sleepers
+ */
 tcp_close(tp)
        register struct tcpcb *tp;
 {
        register struct tcpiphdr *t;
 tcp_close(tp)
        register struct tcpcb *tp;
 {
        register struct tcpiphdr *t;
+       struct inpcb *inp = tp->t_inpcb;
+       struct socket *so = inp->inp_socket;
 
 
-COUNT(TCP_CLOSE);
-       tcp_canceltimers(tp);
        t = tp->seg_next;
        for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
                m_freem(dtom(t));
        t = tp->seg_next;
        for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next)
                m_freem(dtom(t));
-       if (tp->t_template) {
+       if (tp->t_template)
                (void) m_free(dtom(tp->t_template));
                (void) m_free(dtom(tp->t_template));
-               tp->t_template = 0;
-       }
-       in_pcbfree(tp->t_inpcb);
+       if (tp->t_tcpopt)
+               (void) m_free(dtom(tp->t_tcpopt));
+       if (tp->t_ipopt)
+               (void) m_free(dtom(tp->t_ipopt));
        (void) m_free(dtom(tp));
        (void) m_free(dtom(tp));
+       inp->inp_ppcb = 0;
+       soisdisconnected(so);
+       in_pcbdetach(inp);
 }
 
 }
 
-/*ARGSUSED*/
-tcp_sense(m)
-       struct mbuf *m;
+tcp_drain()
 {
 
 {
 
-COUNT(TCP_SENSE);
-       return (EOPNOTSUPP);
 }
 
 }
 
-tcp_drain()
+tcp_ctlinput(cmd, arg)
+       int cmd;
+       caddr_t arg;
 {
 {
-       register struct inpcb *ip;
+       struct in_addr *sin;
+       extern u_char inetctlerrmap[];
 
 
-COUNT(TCP_DRAIN);
-}
+       if (cmd < 0 || cmd > PRC_NCMDS)
+               return;
+       switch (cmd) {
 
 
-tcp_ctlinput(m)
-       struct mbuf *m;
-{
+       case PRC_ROUTEDEAD:
+               break;
 
 
-COUNT(TCP_CTLINPUT);
-       m_freem(m);
+       case PRC_QUENCH:
+               break;
+
+       /* these are handled by ip */
+       case PRC_IFDOWN:
+       case PRC_HOSTDEAD:
+       case PRC_HOSTUNREACH:
+               break;
+
+       default:
+               sin = &((struct icmp *)arg)->icmp_ip.ip_dst;
+               in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort);
+       }
 }
 }