lint
[unix-history] / usr / src / sys / netinet / tcp_input.c
index 8b26a8a..cf96e45 100644 (file)
@@ -1,4 +1,4 @@
-/*     tcp_input.c     1.52    82/02/03        */
+/*     tcp_input.c     1.60    82/03/13        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -21,7 +21,9 @@
 #include "../net/tcp_debug.h"
 #include "../errno.h"
 
 #include "../net/tcp_debug.h"
 #include "../errno.h"
 
+#ifdef notdef
 int    tcpprintfs = 0;
 int    tcpprintfs = 0;
+#endif
 int    tcpcksum = 1;
 struct sockaddr_in tcp_in = { AF_INET };
 struct tcpiphdr tcp_saveti;
 int    tcpcksum = 1;
 struct sockaddr_in tcp_in = { AF_INET };
 struct tcpiphdr tcp_saveti;
@@ -45,6 +47,7 @@ tcp_input(m0)
        struct socket *so;
        int todrop, acked;
        short ostate;
        struct socket *so;
        int todrop, acked;
        short ostate;
+       struct in_addr laddr;
 
 COUNT(TCP_INPUT);
        /*
 
 COUNT(TCP_INPUT);
        /*
@@ -73,7 +76,7 @@ COUNT(TCP_INPUT);
                ti->ti_x1 = 0;
                ti->ti_len = (u_short)tlen;
 #if vax
                ti->ti_x1 = 0;
                ti->ti_len = (u_short)tlen;
 #if vax
-               ti->ti_len = htons(ti->ti_len);
+               ti->ti_len = htons((u_short)ti->ti_len);
 #endif
                if (ti->ti_sum = in_cksum(m, len)) {
                        tcpstat.tcps_badsum++;
 #endif
                if (ti->ti_sum = in_cksum(m, len)) {
                        tcpstat.tcps_badsum++;
@@ -104,13 +107,21 @@ COUNT(TCP_INPUT);
                om->m_off = MMINOFF;
                om->m_len = off - sizeof (struct tcphdr);
                { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
                om->m_off = MMINOFF;
                om->m_len = off - sizeof (struct tcphdr);
                { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
-                 bcopy(op, mtod(om, caddr_t), om->m_len);
+                 bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len);
                  m->m_len -= om->m_len;
                  m->m_len -= om->m_len;
-                 bcopy(op+om->m_len, op, m->m_len-sizeof (struct tcpiphdr));
+                 bcopy(op+om->m_len, op,
+                  (unsigned)(m->m_len-sizeof (struct tcpiphdr)));
                }
        }
        tiflags = ti->ti_flags;
 
                }
        }
        tiflags = ti->ti_flags;
 
+       /*
+        * drop IP header
+        */
+       off += sizeof (struct ip);
+       m->m_off += off;
+       m->m_len -= off;
+
 #if vax
        /*
         * Convert TCP protocol specific fields to host format.
 #if vax
        /*
         * Convert TCP protocol specific fields to host format.
@@ -122,10 +133,12 @@ COUNT(TCP_INPUT);
 #endif
 
        /*
 #endif
 
        /*
-        * Locate pcb for segment.
+        * Locate pcb for segment.  On match, update the local
+        * address stored in the block to reflect anchoring.
         */
        inp = in_pcblookup
         */
        inp = in_pcblookup
-               (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport);
+               (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport,
+               INPLOOKUP_WILDCARD);
 
        /*
         * If the state is CLOSED (i.e., TCB does not exist) then
 
        /*
         * If the state is CLOSED (i.e., TCB does not exist) then
@@ -188,13 +201,20 @@ COUNT(TCP_INPUT);
                        goto drop;
                tcp_in.sin_addr = ti->ti_src;
                tcp_in.sin_port = ti->ti_sport;
                        goto drop;
                tcp_in.sin_addr = ti->ti_src;
                tcp_in.sin_port = ti->ti_sport;
-               if (in_pcbconnect(inp, (struct sockaddr *)&tcp_in))
+               laddr = inp->inp_laddr;
+               if (inp->inp_laddr.s_addr == 0)
+                       inp->inp_laddr = ti->ti_dst;
+               if (in_pcbconnect(inp, (struct sockaddr_in *)&tcp_in)) {
+                       inp->inp_laddr = laddr;
                        goto drop;
                        goto drop;
+               }
                tp->t_template = tcp_template(tp);
                if (tp->t_template == 0) {
                        in_pcbdisconnect(inp);
                tp->t_template = tcp_template(tp);
                if (tp->t_template == 0) {
                        in_pcbdisconnect(inp);
+                       inp->inp_laddr = laddr;
                        goto drop;
                }
                        goto drop;
                }
+               in_setsockaddr(inp);
                tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
                tp->irs = ti->ti_seq;
                tcp_sendseqinit(tp);
                tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
                tp->irs = ti->ti_seq;
                tcp_sendseqinit(tp);
@@ -324,6 +344,15 @@ trimthenstep6:
                }
        }
 
                }
        }
 
+       /*
+        * If a segment is received on a connection after the
+        * user processes are gone, then RST the other end.
+        */
+       if (so->so_state & SS_USERGONE) {
+               tcp_close(tp);
+               goto dropwithreset;
+       }
+
        /*
         * If the RST bit is set examine the state:
         *    SYN_RECEIVED STATE:
        /*
         * If the RST bit is set examine the state:
         *    SYN_RECEIVED STATE:
@@ -344,6 +373,9 @@ trimthenstep6:
                        inp->inp_ppcb = 0;
                        tp = tcp_newtcpcb(inp);
                        tp->t_state = TCPS_LISTEN;
                        inp->inp_ppcb = 0;
                        tp = tcp_newtcpcb(inp);
                        tp->t_state = TCPS_LISTEN;
+                       inp->inp_faddr.s_addr = 0;
+                       inp->inp_fport = 0;
+                       inp->inp_laddr.s_addr = 0;      /* not quite right */
                        goto drop;
                }
                tcp_drop(tp, ECONNREFUSED);
                        goto drop;
                }
                tcp_drop(tp, ECONNREFUSED);
@@ -426,19 +458,35 @@ trimthenstep6:
                if (SEQ_GT(ti->ti_ack, tp->snd_max))
                        goto dropafterack;
                acked = ti->ti_ack - tp->snd_una;
                if (SEQ_GT(ti->ti_ack, tp->snd_max))
                        goto dropafterack;
                acked = ti->ti_ack - tp->snd_una;
+
+               /*
+                * If transmit timer is running and timed sequence
+                * number was acked, update smoothed round trip time.
+                */
+               if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
+                       if (tp->t_srtt == 0)
+                               tp->t_srtt = tp->t_rtt;
+                       else
+                               tp->t_srtt =
+                                   tcp_alpha * tp->t_srtt +
+                                   (1 - tcp_alpha) * tp->t_rtt;
+/* printf("rtt %d srtt*100 now %d\n", tp->t_rtt, (int)(tp->t_srtt*100)); */
+                       tp->t_rtt = 0;
+               }
+
                if (ti->ti_ack == tp->snd_max)
                        tp->t_timer[TCPT_REXMT] = 0;
                else {
                        TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
                            tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
                if (ti->ti_ack == tp->snd_max)
                        tp->t_timer[TCPT_REXMT] = 0;
                else {
                        TCPT_RANGESET(tp->t_timer[TCPT_REXMT],
                            tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX);
-                       tp->t_rtt = 0;
+                       tp->t_rtt = 1;
                        tp->t_rxtshift = 0;
                }
                if (acked > so->so_snd.sb_cc) {
                        sbdrop(&so->so_snd, so->so_snd.sb_cc);
                        tp->snd_wnd -= so->so_snd.sb_cc;
                } else {
                        tp->t_rxtshift = 0;
                }
                if (acked > so->so_snd.sb_cc) {
                        sbdrop(&so->so_snd, so->so_snd.sb_cc);
                        tp->snd_wnd -= so->so_snd.sb_cc;
                } else {
-                       sbdrop(&so->so_snd.sb_cc, acked);
+                       sbdrop(&so->so_snd, acked);
                        tp->snd_wnd -= acked;
                        acked = 0;
                }
                        tp->snd_wnd -= acked;
                        acked = 0;
                }
@@ -448,20 +496,6 @@ trimthenstep6:
                if (SEQ_LT(tp->snd_nxt, tp->snd_una))
                        tp->snd_nxt = tp->snd_una;
 
                if (SEQ_LT(tp->snd_nxt, tp->snd_una))
                        tp->snd_nxt = tp->snd_una;
 
-               /*
-                * If transmit timer is running and timed sequence
-                * number was acked, update smoothed round trip time.
-                */
-               if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) {
-                       if (tp->t_srtt == 0)
-                               tp->t_srtt = tp->t_rtt;
-                       else
-                               tp->t_srtt =
-                                   tcp_alpha * tp->t_srtt +
-                                   (1 - tcp_alpha) * tp->t_rtt;
-                       tp->t_rtt = 0;
-               }
-
                switch (tp->t_state) {
 
                /*
                switch (tp->t_state) {
 
                /*
@@ -470,8 +504,15 @@ trimthenstep6:
                 * then enter FIN_WAIT_2.
                 */
                case TCPS_FIN_WAIT_1:
                 * then enter FIN_WAIT_2.
                 */
                case TCPS_FIN_WAIT_1:
-                       if (ourfinisacked)
+                       if (ourfinisacked) {
+                               /*
+                                * If we can't receive any more
+                                * data, then closing user can proceed.
+                                */
+                               if (so->so_state & SS_CANTRCVMORE)
+                                       soisdisconnected(so);
                                tp->t_state = TCPS_FIN_WAIT_2;
                                tp->t_state = TCPS_FIN_WAIT_2;
+                       }
                        break;
 
                /*
                        break;
 
                /*
@@ -519,9 +560,6 @@ step6:
        if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
            (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
             tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
        if (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq &&
            (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
             tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd)) {
-/*
-printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, tp->snd_wl2, ti->ti_ack, ti->ti_win, tp->snd_wnd);
-*/
                tp->snd_wnd = ti->ti_win;
                tp->snd_wl1 = ti->ti_seq;
                tp->snd_wl2 = ti->ti_ack;
                tp->snd_wnd = ti->ti_win;
                tp->snd_wl1 = ti->ti_seq;
                tp->snd_wl2 = ti->ti_ack;
@@ -573,9 +611,6 @@ printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, t
         */
        if ((ti->ti_len || (tiflags&TH_FIN)) &&
            TCPS_HAVERCVDFIN(tp->t_state) == 0) {
         */
        if ((ti->ti_len || (tiflags&TH_FIN)) &&
            TCPS_HAVERCVDFIN(tp->t_state) == 0) {
-               off += sizeof (struct ip);              /* drop IP header */
-               m->m_off += off;
-               m->m_len -= off;
                tiflags = tcp_reass(tp, ti);
                if (tcpnodelack == 0)
                        tp->t_flags |= TF_DELACK;
                tiflags = tcp_reass(tp, ti);
                if (tcpnodelack == 0)
                        tp->t_flags |= TF_DELACK;
@@ -641,7 +676,7 @@ printf("wl1 %x seq %x wl2 %x ack %x win %x wnd %x\n", tp->snd_wl1, ti->ti_seq, t
        /*
         * Return any desired output.
         */
        /*
         * Return any desired output.
         */
-       tcp_output(tp);
+       (void) tcp_output(tp);
        return;
 
 dropafterack:
        return;
 
 dropafterack:
@@ -656,7 +691,7 @@ dropafterack:
 
 dropwithreset:
        if (om)
 
 dropwithreset:
        if (om)
-               m_free(om);
+               (void) m_free(om);
        /*
         * Generate a RST, dropping incoming segment.
         * Make ACK acceptable to originator of segment.
        /*
         * Generate a RST, dropping incoming segment.
         * Make ACK acceptable to originator of segment.
@@ -707,7 +742,7 @@ tcp_dooptions(tp, om)
                                continue;
                        tp->t_maxseg = *(u_short *)(cp + 2);
 #if vax
                                continue;
                        tp->t_maxseg = *(u_short *)(cp + 2);
 #if vax
-                       tp->t_maxseg = ntohs(tp->t_maxseg);
+                       tp->t_maxseg = ntohs((u_short)tp->t_maxseg);
 #endif
                        break;
                        
 #endif
                        break;
                        
@@ -764,7 +799,7 @@ printf("take oob ack %x and cancel rexmt\n", cp[2]);
 #endif TCPTRUEOOB
                }
        }
 #endif TCPTRUEOOB
                }
        }
-       m_free(om);
+       (void) m_free(om);
 }
 
 /*
 }
 
 /*
@@ -778,7 +813,7 @@ tcp_pulloutofband(so, ti)
        struct tcpiphdr *ti;
 {
        register struct mbuf *m;
        struct tcpiphdr *ti;
 {
        register struct mbuf *m;
-       int cnt = sizeof (struct tcpiphdr) + ti->ti_urp - 1;
+       int cnt = ti->ti_urp - 1;
        
        m = dtom(ti);
        while (cnt >= 0) {
        
        m = dtom(ti);
        while (cnt >= 0) {
@@ -788,7 +823,7 @@ tcp_pulloutofband(so, ti)
 
                        tp->t_iobc = *cp;
                        tp->t_oobflags |= TCPOOB_HAVEDATA;
 
                        tp->t_iobc = *cp;
                        tp->t_oobflags |= TCPOOB_HAVEDATA;
-                       bcopy(cp+1, cp, m->m_len - cnt - 1);
+                       bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
                        m->m_len--;
                        return;
                }
                        m->m_len--;
                        return;
                }
@@ -894,7 +929,7 @@ present:
                m = dtom(ti);
                ti = (struct tcpiphdr *)ti->ti_next;
                if (so->so_state & SS_CANTRCVMORE)
                m = dtom(ti);
                ti = (struct tcpiphdr *)ti->ti_next;
                if (so->so_state & SS_CANTRCVMORE)
-                       (void) m_freem(m);
+                       m_freem(m);
                else
                        sbappend(&so->so_rcv, m);
        } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
                else
                        sbappend(&so->so_rcv, m);
        } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);