This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: RedBoot sequencenumber generation


>>>>> Roland =?iso-8859-15?q?Ca=DFebohm?= writes:

> Hello Mark,
> thanks for helping.

> In most cases it works, but if the initial sequencenumber, which
> is now a pseudo random number, is anyhow in the window of the 
> TIME_WAIT connections of the host I get the old problem.
> The SYN packet don't get a responce and will be retransmitted
> on and on with the same sequencenumber.

Retransmitting the same seqnum with the SYN is okay. Interestingly,
the RFCs state that while in the TIME_WAIT state, the server refuses
connection requests using the same tcp socket pair. In the tcpdump
you sent, the second connection uses a different socket pair (port
7800 vs 7801). Some BSD stacks are more stringent and refuse to reuse
the local port number while in TIME_WAIT. It looks like this is the
problem you are running into.

However, there is hope. Most BSD implementations allow a new
connection in TIME_WAIT iff the new seqnum is greater than the
final seqnum of the connection in TIME_WAIT. So, here's the same
patch, but with an extra manipulation of initial_seqnum when an
active close FIN is received from the server.

--Mark


Index: redboot/current/src/net/tcp.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/src/net/tcp.c,v
retrieving revision 1.11
diff -u -p -5 -r1.11 tcp.c
--- redboot/current/src/net/tcp.c	21 Dec 2003 13:17:52 -0000	1.11
+++ redboot/current/src/net/tcp.c	16 Mar 2004 17:02:20 -0000
@@ -58,10 +58,12 @@
 #include <cyg/hal/hal_if.h>
 
 #define MAX_TCP_SEGMENT (ETH_MAX_PKTLEN - (sizeof(eth_header_t) + sizeof(ip_header_t)))
 #define MAX_TCP_DATA    (MAX_TCP_SEGMENT - sizeof(tcp_header_t))
 
+/* setting this to 1 is technically wrong, but BSD does it too... */
+static int initial_seqnum = 1;
 
 /* sequence number comparison macros */
 #define SEQ_LT(a,b) ((int)((a)-(b)) < 0)
 #define SEQ_LE(a,b) ((int)((a)-(b)) <= 0)
 #define SEQ_GT(a,b) ((int)((a)-(b)) > 0)
@@ -479,10 +481,11 @@ __tcp_handler(pktbuf_t *pkt, ip_route_t 
                       return;
                   }
                   s->state = _ESTABLISHED;
                   s->ack = ntohl(tcp->seqnum) + 1;
                   s->seq = ntohl(tcp->acknum);
+                  initial_seqnum += 64000;
 		  __timer_cancel(&s->timer);
                   send_ack(s);
 		break;
 
 	      case _LISTEN:
@@ -523,10 +526,11 @@ __tcp_handler(pktbuf_t *pkt, ip_route_t 
 		} else if ((tcp->flags & TCP_FLAG_ACK) &&
 			   ntohl(tcp->acknum) == (s->seq + 1)) {
 		    /* we've established the connection */
 		    s->state = _ESTABLISHED;
 		    s->seq++;
+                    initial_seqnum += 64000;
 
 		    BSPLOG(bsp_log("ACK received - connection established\n"));
 		}
 		break;
 
@@ -543,10 +547,12 @@ __tcp_handler(pktbuf_t *pkt, ip_route_t 
 
 		    BSPLOG(bsp_log("FIN received - going to _CLOSE_WAIT\n"));
 
 		    s->ack++;
 		    s->state = _CLOSE_WAIT;
+
+                    initial_seqnum = ntohl(tcp->seqnum) + 64000;
 		}
 		/*
 		 * Send an ack if neccessary.
 		 */
 		if (s->ack != ack || pkt->pkt_bytes > (tcp->hdr_len << 2))
@@ -643,10 +649,13 @@ void
 __tcp_poll(void)
 {
     __enet_poll();
     MS_TICKS_DELAY();
     __timer_poll();
+
+    /* rfc793 says this should be incremented approx. once per 4ms */
+    initial_seqnum += 1000 / 4;
 }
 
 
 int
 __tcp_listen(tcp_socket_t *s, word port)
@@ -890,11 +899,11 @@ __tcp_open(tcp_socket_t *s, struct socka
     s->his_port = host->sin_port;
     s->pkt.buf = (word *)s->pktbuf;
     s->pkt.bufsize = ETH_MAX_PKTLEN;
     s->pkt.ip_hdr  = (ip_header_t *)s->pkt.buf;
     s->pkt.tcp_hdr = (tcp_header_t *)(s->pkt.ip_hdr + 1);
-    s->seq = (port << 16) | 0xDE77;
+    s->seq = initial_seqnum;
     s->ack = 0;
     if (__arp_lookup((ip_addr_t *)&host->sin_addr, &s->his_addr) < 0) {
         diag_printf("%s: Can't find address of server\n", __FUNCTION__);
         return -1;
     }

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]