This links.
[akaros.git] / kern / src / net / tcp.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 #include <vfs.h>
17 #include <kfs.h>
18 #include <slab.h>
19 #include <kmalloc.h>
20 #include <kref.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <error.h>
25 #include <cpio.h>
26 #include <pmap.h>
27 #include <smp.h>
28 #include <ip.h>
29
30 enum
31 {
32         QMAX            = 64*1024-1,
33         IP_TCPPROTO     = 6,
34
35         TCP4_IPLEN      = 8,
36         TCP4_PHDRSIZE   = 12,
37         TCP4_HDRSIZE    = 20,
38         TCP4_TCBPHDRSZ  = 40,
39         TCP4_PKT        = TCP4_IPLEN+TCP4_PHDRSIZE,
40
41         TCP6_IPLEN      = 0,
42         TCP6_PHDRSIZE   = 40,
43         TCP6_HDRSIZE    = 20,
44         TCP6_TCBPHDRSZ  = 60,
45         TCP6_PKT        = TCP6_IPLEN+TCP6_PHDRSIZE,
46
47         TcptimerOFF     = 0,
48         TcptimerON      = 1,
49         TcptimerDONE    = 2,
50         MAX_TIME        = (1<<20),      /* Forever */
51         TCP_ACK         = 50,           /* Timed ack sequence in ms */
52         MAXBACKMS       = 9*60*1000,    /* longest backoff time (ms) before hangup */
53
54         URG             = 0x20,         /* Data marked urgent */
55         ACK             = 0x10,         /* Acknowledge is valid */
56         PSH             = 0x08,         /* Whole data pipe is pushed */
57         RST             = 0x04,         /* Reset connection */
58         SYN             = 0x02,         /* Pkt. is synchronise */
59         FIN             = 0x01,         /* Start close down */
60
61         EOLOPT          = 0,
62         NOOPOPT         = 1,
63         MSSOPT          = 2,
64         MSS_LENGTH      = 4,            /* Mean segment size */
65         WSOPT           = 3,
66         WS_LENGTH       = 3,            /* Bits to scale window size by */
67         MSL2            = 10,
68         MSPTICK         = 50,           /* Milliseconds per timer tick */
69         DEF_MSS         = 1460,         /* Default mean segment */
70         DEF_MSS6        = 1280,         /* Default mean segment (min) for v6 */
71         DEF_RTT         = 500,          /* Default round trip */
72         DEF_KAT         = 120000,       /* Default time (ms) between keep alives */
73         TCP_LISTEN      = 0,            /* Listen connection */
74         TCP_CONNECT     = 1,            /* Outgoing connection */
75         SYNACK_RXTIMER  = 250,          /* ms between SYNACK retransmits */
76
77         TCPREXMTTHRESH  = 3,            /* dupack threshhold for rxt */
78
79         FORCE           = 1,
80         CLONE           = 2,
81         RETRAN          = 4,
82         ACTIVE          = 8,
83         SYNACK          = 16,
84
85         LOGAGAIN        = 3,
86         LOGDGAIN        = 2,
87
88         Closed          = 0,            /* Connection states */
89         Listen,
90         Syn_sent,
91         Syn_received,
92         Established,
93         Finwait1,
94         Finwait2,
95         Close_wait,
96         Closing,
97         Last_ack,
98         Time_wait,
99
100         Maxlimbo        = 1000,         /* maximum procs waiting for response to SYN ACK */
101         NLHT            = 256,          /* hash table size, must be a power of 2 */
102         LHTMASK         = NLHT-1,
103
104         HaveWS          = 1<<8,
105 };
106
107 /* Must correspond to the enumeration above */
108 char *tcpstates[] =
109 {
110         "Closed",       "Listen",       "Syn_sent", "Syn_received",
111         "Established",  "Finwait1",     "Finwait2", "Close_wait",
112         "Closing",      "Last_ack",     "Time_wait"
113 };
114
115 typedef struct Tcptimer Tcptimer;
116 struct Tcptimer
117 {
118         Tcptimer        *next;
119         Tcptimer        *prev;
120         Tcptimer        *readynext;
121         int     state;
122         int     start;
123         int     count;
124         void    (*func)(void*);
125         void    *arg;
126 };
127
128 /*
129  *  v4 and v6 pseudo headers used for
130  *  checksuming tcp
131  */
132 typedef struct Tcp4hdr Tcp4hdr;
133 struct Tcp4hdr
134 {
135         uint8_t vihl;           /* Version and header length */
136         uint8_t tos;            /* Type of service */
137         uint8_t length[2];      /* packet length */
138         uint8_t id[2];          /* Identification */
139         uint8_t frag[2];        /* Fragment information */
140         uint8_t Unused;
141         uint8_t proto;
142         uint8_t tcplen[2];
143         uint8_t tcpsrc[4];
144         uint8_t tcpdst[4];
145         uint8_t tcpsport[2];
146         uint8_t tcpdport[2];
147         uint8_t tcpseq[4];
148         uint8_t tcpack[4];
149         uint8_t tcpflag[2];
150         uint8_t tcpwin[2];
151         uint8_t tcpcksum[2];
152         uint8_t tcpurg[2];
153         /* Options segment */
154         uint8_t tcpopt[1];
155 };
156
157 typedef struct Tcp6hdr Tcp6hdr;
158 struct Tcp6hdr
159 {
160         uint8_t vcf[4];
161         uint8_t ploadlen[2];
162         uint8_t proto;
163         uint8_t ttl;
164         uint8_t tcpsrc[IPaddrlen];
165         uint8_t tcpdst[IPaddrlen];
166         uint8_t tcpsport[2];
167         uint8_t tcpdport[2];
168         uint8_t tcpseq[4];
169         uint8_t tcpack[4];
170         uint8_t tcpflag[2];
171         uint8_t tcpwin[2];
172         uint8_t tcpcksum[2];
173         uint8_t tcpurg[2];
174         /* Options segment */
175         uint8_t tcpopt[1];
176 };
177
178 /*
179  *  this represents the control info
180  *  for a single packet.  It is derived from
181  *  a packet in ntohtcp{4,6}() and stuck into
182  *  a packet in htontcp{4,6}().
183  */
184 typedef struct Tcp Tcp;
185 struct  Tcp
186 {
187         uint16_t        source;
188         uint16_t        dest;
189         uint32_t        seq;
190         uint32_t        ack;
191         uint8_t flags;
192         uint16_t        ws;     /* window scale option (if not zero) */
193         uint32_t        wnd;
194         uint16_t        urg;
195         uint16_t        mss;    /* max segment size option (if not zero) */
196         uint16_t        len;    /* size of data */
197 };
198
199 /*
200  *  this header is malloc'd to thread together fragments
201  *  waiting to be coalesced
202  */
203 typedef struct Reseq Reseq;
204 struct Reseq
205 {
206         Reseq   *next;
207         Tcp     seg;
208         struct block    *bp;
209         uint16_t        length;
210 };
211
212 /*
213  *  the qlock in the Conv locks this structure
214  */
215 typedef struct Tcpctl Tcpctl;
216 struct Tcpctl
217 {
218         uint8_t state;                  /* Connection state */
219         uint8_t type;                   /* Listening or active connection */
220         uint8_t code;                   /* Icmp code */
221         struct {
222                 uint32_t        una;            /* Unacked data pointer */
223                 uint32_t        nxt;            /* Next sequence expected */
224                 uint32_t        ptr;            /* Data pointer */
225                 uint32_t        wnd;            /* Tcp send window */
226                 uint32_t        urg;            /* Urgent data pointer */
227                 uint32_t        wl2;
228                 int     scale;          /* how much to right shift window in xmitted packets */
229                 /* to implement tahoe and reno TCP */
230                 uint32_t        dupacks;        /* number of duplicate acks rcvd */
231                 int     recovery;       /* loss recovery flag */
232                 uint32_t        rxt;            /* right window marker for recovery */
233         } snd;
234         struct {
235                 uint32_t        nxt;            /* Receive pointer to next uint8_t slot */
236                 uint32_t        wnd;            /* Receive window incoming */
237                 uint32_t        urg;            /* Urgent pointer */
238                 int     blocked;
239                 int     una;            /* unacked data segs */
240                 int     scale;          /* how much to left shift window in rcved packets */
241         } rcv;
242         uint32_t        iss;                    /* Initial sequence number */
243         int     sawwsopt;               /* true if we saw a wsopt on the incoming SYN */
244         uint32_t        cwind;                  /* Congestion window */
245         int     scale;                  /* desired snd.scale */
246         uint16_t        ssthresh;               /* Slow start threshold */
247         int     resent;                 /* Bytes just resent */
248         int     irs;                    /* Initial received squence */
249         uint16_t        mss;                    /* Mean segment size */
250         int     rerecv;                 /* Overlap of data rerecevived */
251         uint32_t        window;                 /* Recevive window */
252         uint8_t backoff;                /* Exponential backoff counter */
253         int     backedoff;              /* ms we've backed off for rexmits */
254         uint8_t flags;                  /* State flags */
255         Reseq   *reseq;                 /* Resequencing queue */
256         Tcptimer        timer;                  /* Activity timer */
257         Tcptimer        acktimer;               /* Acknowledge timer */
258         Tcptimer        rtt_timer;              /* Round trip timer */
259         Tcptimer        katimer;                /* keep alive timer */
260         uint32_t        rttseq;                 /* Round trip sequence */
261         int     srtt;                   /* Shortened round trip */
262         int     mdev;                   /* Mean deviation of round trip */
263         int     kacounter;              /* count down for keep alive */
264         unsigned int    sndsyntime;             /* time syn sent */
265         uint32_t        time;                   /* time Finwait2 or Syn_received was sent */
266         int     nochecksum;             /* non-zero means don't send checksums */
267         int     flgcnt;                 /* number of flags in the sequence (FIN,SEQ) */
268
269         union {
270                 Tcp4hdr tcp4hdr;
271                 Tcp6hdr tcp6hdr;
272         } protohdr;             /* prototype header */
273 };
274
275 /*
276  *  New calls are put in limbo rather than having a conversation structure
277  *  allocated.  Thus, a SYN attack results in lots of limbo'd calls but not
278  *  any real Conv structures mucking things up.  Calls in limbo rexmit their
279  *  SYN ACK every SYNACK_RXTIMER ms up to 4 times, i.e., they disappear after 1 second.
280  *
281  *  In particular they aren't on a listener's queue so that they don't figure
282  *  in the input queue limit.
283  *
284  *  If 1/2 of a T3 was attacking SYN packets, we'ld have a permanent queue
285  *  of 70000 limbo'd calls.  Not great for a linear list but doable.  Therefore
286  *  there is no hashing of this list.
287  */
288 typedef struct Limbo Limbo;
289 struct Limbo
290 {
291         Limbo   *next;
292
293         uint8_t laddr[IPaddrlen];
294         uint8_t raddr[IPaddrlen];
295         uint16_t        lport;
296         uint16_t        rport;
297         uint32_t        irs;            /* initial received sequence */
298         uint32_t        iss;            /* initial sent sequence */
299         uint16_t        mss;            /* mss from the other end */
300         uint16_t        rcvscale;       /* how much to scale rcvd windows */
301         uint16_t        sndscale;       /* how much to scale sent windows */
302         uint32_t        lastsend;       /* last time we sent a synack */
303         uint8_t version;        /* v4 or v6 */
304         uint8_t rexmits;        /* number of retransmissions */
305 };
306
307 int     tcp_irtt = DEF_RTT;     /* Initial guess at round trip time */
308 uint16_t        tcp_mss = DEF_MSS;      /* Maximum segment size to be sent */
309
310 enum {
311         /* MIB stats */
312         MaxConn,
313         ActiveOpens,
314         PassiveOpens,
315         EstabResets,
316         CurrEstab,
317         InSegs,
318         OutSegs,
319         RetransSegs,
320         RetransTimeouts,
321         InErrs,
322         OutRsts,
323
324         /* non-MIB stats */
325         CsumErrs,
326         HlenErrs,
327         LenErrs,
328         OutOfOrder,
329
330         Nstats
331 };
332
333 static char *statnames[] =
334 {
335 [MaxConn]       "MaxConn",
336 [ActiveOpens]   "ActiveOpens",
337 [PassiveOpens]  "PassiveOpens",
338 [EstabResets]   "EstabResets",
339 [CurrEstab]     "CurrEstab",
340 [InSegs]        "InSegs",
341 [OutSegs]       "OutSegs",
342 [RetransSegs]   "RetransSegs",
343 [RetransTimeouts]       "RetransTimeouts",
344 [InErrs]        "InErrs",
345 [OutRsts]       "OutRsts",
346 [CsumErrs]      "CsumErrs",
347 [HlenErrs]      "HlenErrs",
348 [LenErrs]       "LenErrs",
349 [OutOfOrder]    "OutOfOrder",
350 };
351
352 typedef struct Tcppriv Tcppriv;
353 struct tcppriv
354 {
355         /* List of active timers */
356         qlock_t         tl;
357         Tcptimer *timers;
358
359         /* hash table for matching conversations */
360         struct Ipht     ht;
361
362         /* calls in limbo waiting for an ACK to our SYN ACK */
363         int     nlimbo;
364         Limbo   *lht[NLHT];
365
366         /* for keeping track of tcpackproc */
367         qlock_t apl;
368         int     ackprocstarted;
369
370         uint32_t        stats[Nstats];
371 };
372
373 /*
374  *  Setting tcpporthogdefense to non-zero enables Dong Lin's
375  *  solution to hijacked systems staking out port's as a form
376  *  of DoS attack.
377  *
378  *  To avoid stateless Conv hogs, we pick a sequence number at random.  If
379  *  it that number gets acked by the other end, we shut down the connection.
380  *  Look for tcpporthogedefense in the code.
381  */
382 int tcpporthogdefense = 0;
383
384 int     addreseq(Tcpctl*, struct tcppriv*, Tcp*, struct block*, uint16_t);
385 void    getreseq(Tcpctl*, Tcp*, struct block**, uint16_t*);
386 void    localclose(struct conv*, char *unused_char_p_t);
387 void    procsyn(struct conv*, Tcp*);
388 void    tcpiput(struct Proto*, struct Ipifc*, struct block*);
389 void    tcpoutput(struct conv*);
390 int     tcptrim(Tcpctl*, Tcp*, struct block**, uint16_t*);
391 void    tcpstart(struct conv*, int);
392 void    tcptimeout(void*);
393 void    tcpsndsyn(struct conv*, Tcpctl*);
394 void    tcprcvwin(struct conv*);
395 void    tcpacktimer(void*);
396 void    tcpkeepalive(void*);
397 void    tcpsetkacounter(Tcpctl*);
398 void    tcprxmit(struct conv*);
399 void    tcpsettimer(Tcpctl*);
400 void    tcpsynackrtt(struct conv*);
401 void    tcpsetscale(struct conv*, Tcpctl*, uint16_t, uint16_t);
402
403 static void limborexmit(struct Proto*);
404 static void limbo(struct conv*, uint8_t *unused_uint8_p_t, uint8_t*, Tcp*, int);
405
406 void
407 tcpsetstate(struct conv *s, uint8_t newstate)
408 {
409         Tcpctl *tcb;
410         uint8_t oldstate;
411         struct tcppriv *tpriv;
412
413         tpriv = s->p->priv;
414
415         tcb = (Tcpctl*)s->ptcl;
416
417         oldstate = tcb->state;
418         if(oldstate == newstate)
419                 return;
420
421         if(oldstate == Established)
422                 tpriv->stats[CurrEstab]--;
423         if(newstate == Established)
424                 tpriv->stats[CurrEstab]++;
425
426         /**
427         print( "%d/%d %s->%s CurrEstab=%d\n", s->lport, s->rport,
428                 tcpstates[oldstate], tcpstates[newstate], tpriv->tstats.tcpCurrEstab );
429         **/
430
431         switch(newstate) {
432         case Closed:
433                 qclose(s->rq);
434                 qclose(s->wq);
435                 qclose(s->eq);
436                 break;
437
438         case Close_wait:                /* Remote closes */
439                 qhangup(s->rq, NULL);
440                 break;
441         }
442
443         tcb->state = newstate;
444
445         if(oldstate == Syn_sent && newstate != Closed)
446                 Fsconnected(s, NULL);
447 }
448
449 static char*
450 tcpconnect(struct conv *c, char **argv, int argc)
451 {
452         char *e;
453
454         e = Fsstdconnect(c, argv, argc);
455         if(e != NULL)
456                 return e;
457         tcpstart(c, TCP_CONNECT);
458
459         return NULL;
460 }
461
462 static int
463 tcpstate(struct conv *c, char *state, int n)
464 {
465         Tcpctl *s;
466
467         s = (Tcpctl*)(c->ptcl);
468
469         return snprintf(state, n,
470                 "%s qin %d qout %d srtt %d mdev %d cwin %lud swin %lud>>%d rwin %lud>>%d timer.start %d timer.count %d rerecv %d katimer.start %d katimer.count %d\n",
471                 tcpstates[s->state],
472                 c->rq ? qlen(c->rq) : 0,
473                 c->wq ? qlen(c->wq) : 0,
474                 s->srtt, s->mdev,
475                 s->cwind, s->snd.wnd, s->rcv.scale, s->rcv.wnd, s->snd.scale,
476                 s->timer.start, s->timer.count, s->rerecv,
477                 s->katimer.start, s->katimer.count);
478 }
479
480 static int
481 tcpinuse(struct conv *c)
482 {
483         Tcpctl *s;
484
485         s = (Tcpctl*)(c->ptcl);
486         return s->state != Closed;
487 }
488
489 static char*
490 tcpannounce(struct conv *c, char **argv, int argc)
491 {
492         char *e;
493
494         e = Fsstdannounce(c, argv, argc);
495         if(e != NULL)
496                 return e;
497         tcpstart(c, TCP_LISTEN);
498         Fsconnected(c, NULL);
499
500         return NULL;
501 }
502
503 /*
504  *  tcpclose is always called with the q locked
505  */
506 static void
507 tcpclose(struct conv *c)
508 {
509         Tcpctl *tcb;
510
511         tcb = (Tcpctl*)c->ptcl;
512
513         qhangup(c->rq, NULL);
514         qhangup(c->wq, NULL);
515         qhangup(c->eq, NULL);
516         qflush(c->rq);
517
518         switch(tcb->state) {
519         case Listen:
520                 /*
521                  *  reset any incoming calls to this listener
522                  */
523                 Fsconnected(c, "Hangup");
524
525                 localclose(c, NULL);
526                 break;
527         case Closed:
528         case Syn_sent:
529                 localclose(c, NULL);
530                 break;
531         case Syn_received:
532         case Established:
533                 tcb->flgcnt++;
534                 tcb->snd.nxt++;
535                 tcpsetstate(c, Finwait1);
536                 tcpoutput(c);
537                 break;
538         case Close_wait:
539                 tcb->flgcnt++;
540                 tcb->snd.nxt++;
541                 tcpsetstate(c, Last_ack);
542                 tcpoutput(c);
543                 break;
544         }
545 }
546
547 void
548 tcpkick(void *x)
549 {
550         ERRSTACK(2);
551         struct conv *s = x;
552         Tcpctl *tcb;
553
554         tcb = (Tcpctl*)s->ptcl;
555
556         if(waserror()){
557                 qunlock(&s->qlock);
558                 nexterror();
559         }
560         qlock(&s->qlock);
561
562         switch(tcb->state) {
563         case Syn_sent:
564         case Syn_received:
565         case Established:
566         case Close_wait:
567                 /*
568                  * Push data
569                  */
570                 tcprcvwin(s);
571                 tcpoutput(s);
572                 break;
573         default:
574                 localclose(s, "Hangup");
575                 break;
576         }
577
578         qunlock(&s->qlock);
579         poperror();
580 }
581
582 void
583 tcprcvwin(struct conv *s)                               /* Call with tcb locked */
584 {
585         int w;
586         Tcpctl *tcb;
587
588         tcb = (Tcpctl*)s->ptcl;
589         w = tcb->window - qlen(s->rq);
590         if(w < 0)
591                 w = 0;
592         tcb->rcv.wnd = w;
593         if(w == 0)
594                 tcb->rcv.blocked = 1;
595 }
596
597 void
598 tcpacktimer(void *v)
599 {
600         ERRSTACK(2);
601         Tcpctl *tcb;
602         struct conv *s;
603
604         s = v;
605         tcb = (Tcpctl*)s->ptcl;
606
607         if(waserror()){
608                 qunlock(&s->qlock);
609                 nexterror();
610         }
611         qlock(&s->qlock);
612         if(tcb->state != Closed){
613                 tcb->flags |= FORCE;
614                 tcprcvwin(s);
615                 tcpoutput(s);
616         }
617         qunlock(&s->qlock);
618         poperror();
619 }
620
621 static void
622 tcpcreate(struct conv *c)
623 {
624         c->rq = qopen(QMAX, Qcoalesce, tcpacktimer, c);
625         c->wq = qopen((3*QMAX)/2, Qkick, tcpkick, c);
626 }
627
628 static void
629 timerstate(struct tcppriv *priv, Tcptimer *t, int newstate)
630 {
631         if(newstate != TcptimerON){
632                 if(t->state == TcptimerON){
633                         // unchain
634                         if(priv->timers == t){
635                                 priv->timers = t->next;
636                                 if(t->prev != NULL)
637                                         panic("timerstate1");
638                         }
639                         if(t->next)
640                                 t->next->prev = t->prev;
641                         if(t->prev)
642                                 t->prev->next = t->next;
643                         t->next = t->prev = NULL;
644                 }
645         } else {
646                 if(t->state != TcptimerON){
647                         // chain
648                         if(t->prev != NULL || t->next != NULL)
649                                 panic("timerstate2");
650                         t->prev = NULL;
651                         t->next = priv->timers;
652                         if(t->next)
653                                 t->next->prev = t;
654                         priv->timers = t;
655                 }
656         }
657         t->state = newstate;
658 }
659
660 void
661 tcpackproc(void *a)
662 {
663         ERRSTACK(2);
664         Tcptimer *t, *tp, *timeo;
665         struct Proto *tcp;
666         struct tcppriv *priv;
667         int loop;
668
669         tcp = a;
670         priv = tcp->priv;
671
672         for(;;) {
673                 udelay_sched(MSPTICK * 1000);
674
675                 qlock(&priv->tl);
676                 timeo = NULL;
677                 loop = 0;
678                 for(t = priv->timers; t != NULL; t = tp) {
679                         if(loop++ > 10000)
680                                 panic("tcpackproc1");
681                         tp = t->next;
682                         if(t->state == TcptimerON) {
683                                 t->count--;
684                                 if(t->count == 0) {
685                                         timerstate(priv, t, TcptimerDONE);
686                                         t->readynext = timeo;
687                                         timeo = t;
688                                 }
689                         }
690                 }
691                 qunlock(&priv->tl);
692
693                 loop = 0;
694                 for(t = timeo; t != NULL; t = t->readynext) {
695                         if(loop++ > 10000)
696                                 panic("tcpackproc2");
697                         if(t->state == TcptimerDONE && t->func != NULL && !waserror()){
698                                 (*t->func)(t->arg);
699                                 poperror();
700                         }
701                 }
702
703                 limborexmit(tcp);
704         }
705 }
706
707 void
708 tcpgo(struct tcppriv *priv, Tcptimer *t)
709 {
710         if(t == NULL || t->start == 0)
711                 return;
712
713         qlock(&priv->tl);
714         t->count = t->start;
715         timerstate(priv, t, TcptimerON);
716         qunlock(&priv->tl);
717 }
718
719 void
720 tcphalt(struct tcppriv *priv, Tcptimer *t)
721 {
722         if(t == NULL)
723                 return;
724
725         qlock(&priv->tl);
726         timerstate(priv, t, TcptimerOFF);
727         qunlock(&priv->tl);
728 }
729
730 int
731 backoff(int n)
732 {
733         return 1 << n;
734 }
735
736 void
737 localclose(struct conv *s, char *reason)        /* called with tcb locked */
738 {
739         Tcpctl *tcb;
740         Reseq *rp,*rp1;
741         struct tcppriv *tpriv;
742
743         tpriv = s->p->priv;
744         tcb = (Tcpctl*)s->ptcl;
745
746         iphtrem(&tpriv->ht, s);
747
748         tcphalt(tpriv, &tcb->timer);
749         tcphalt(tpriv, &tcb->rtt_timer);
750         tcphalt(tpriv, &tcb->acktimer);
751         tcphalt(tpriv, &tcb->katimer);
752
753         /* Flush reassembly queue; nothing more can arrive */
754         for(rp = tcb->reseq; rp != NULL; rp = rp1) {
755                 rp1 = rp->next;
756                 freeblist(rp->bp);
757                 kfree(rp);
758         }
759         tcb->reseq = NULL;
760
761         if(tcb->state == Syn_sent)
762                 Fsconnected(s, reason);
763         if(s->state == Announced)
764                 rendez_wakeup(&s->listenr);
765
766         qhangup(s->rq, reason);
767         qhangup(s->wq, reason);
768
769         tcpsetstate(s, Closed);
770 }
771
772 /* mtu (- TCP + IP hdr len) of 1st hop */
773 int
774 tcpmtu(struct Proto *tcp, uint8_t *addr, int version, int *scale)
775 {
776         struct Ipifc *ifc;
777         int mtu;
778
779         ifc = findipifc(tcp->f, addr, 0);
780         switch(version){
781         default:
782         case V4:
783                 mtu = DEF_MSS;
784                 if(ifc != NULL)
785                         mtu = ifc->maxtu - ifc->m->hsize - (TCP4_PKT + TCP4_HDRSIZE);
786                 break;
787         case V6:
788                 mtu = DEF_MSS6;
789                 if(ifc != NULL)
790                         mtu = ifc->maxtu - ifc->m->hsize - (TCP6_PKT + TCP6_HDRSIZE);
791                 break;
792         }
793         if(ifc != NULL){
794                 if(ifc->mbps > 100)
795                         *scale = HaveWS | 3;
796                 else if(ifc->mbps > 10)
797                         *scale = HaveWS | 1;
798                 else
799                         *scale = HaveWS | 0;
800         } else
801                 *scale = HaveWS | 0;
802
803         return mtu;
804 }
805
806 void
807 inittcpctl(struct conv *s, int mode)
808 {
809         Tcpctl *tcb;
810         Tcp4hdr* h4;
811         Tcp6hdr* h6;
812         int mss;
813
814         tcb = (Tcpctl*)s->ptcl;
815
816         memset(tcb, 0, sizeof(Tcpctl));
817
818         tcb->ssthresh = 65535;
819         tcb->srtt = tcp_irtt<<LOGAGAIN;
820         tcb->mdev = 0;
821
822         /* setup timers */
823         tcb->timer.start = tcp_irtt / MSPTICK;
824         tcb->timer.func = tcptimeout;
825         tcb->timer.arg = s;
826         tcb->rtt_timer.start = MAX_TIME;
827         tcb->acktimer.start = TCP_ACK / MSPTICK;
828         tcb->acktimer.func = tcpacktimer;
829         tcb->acktimer.arg = s;
830         tcb->katimer.start = DEF_KAT / MSPTICK;
831         tcb->katimer.func = tcpkeepalive;
832         tcb->katimer.arg = s;
833
834         mss = DEF_MSS;
835
836         /* create a prototype(pseudo) header */
837         if(mode != TCP_LISTEN){
838                 if(ipcmp(s->laddr, IPnoaddr) == 0)
839                         findlocalip(s->p->f, s->laddr, s->raddr);
840
841                 switch(s->ipversion){
842                 case V4:
843                         h4 = &tcb->protohdr.tcp4hdr;
844                         memset(h4, 0, sizeof(*h4));
845                         h4->proto = IP_TCPPROTO;
846                         hnputs(h4->tcpsport, s->lport);
847                         hnputs(h4->tcpdport, s->rport);
848                         v6tov4(h4->tcpsrc, s->laddr);
849                         v6tov4(h4->tcpdst, s->raddr);
850                         break;
851                 case V6:
852                         h6 = &tcb->protohdr.tcp6hdr;
853                         memset(h6, 0, sizeof(*h6));
854                         h6->proto = IP_TCPPROTO;
855                         hnputs(h6->tcpsport, s->lport);
856                         hnputs(h6->tcpdport, s->rport);
857                         ipmove(h6->tcpsrc, s->laddr);
858                         ipmove(h6->tcpdst, s->raddr);
859                         mss = DEF_MSS6;
860                         break;
861                 default:
862                         panic("inittcpctl: version %d", s->ipversion);
863                 }
864         }
865
866         tcb->mss = tcb->cwind = mss;
867
868         /* default is no window scaling */
869         tcb->window = QMAX;
870         tcb->rcv.wnd = QMAX;
871         tcb->rcv.scale = 0;
872         tcb->snd.scale = 0;
873         qsetlimit(s->rq, QMAX);
874 }
875
876 /*
877  *  called with s qlocked
878  */
879 void
880 tcpstart(struct conv *s, int mode)
881 {
882         Tcpctl *tcb;
883         struct tcppriv *tpriv;
884         char kpname[KNAMELEN];
885
886         tpriv = s->p->priv;
887
888         if(tpriv->ackprocstarted == 0){
889                 qlock(&tpriv->apl);
890                 if(tpriv->ackprocstarted == 0){
891                         snprintf(kpname, sizeof(kpname), "#I%dtcpack", s->p->f->dev);
892                         ktask(kpname, tcpackproc, s->p);
893                         tpriv->ackprocstarted = 1;
894                 }
895                 qunlock(&tpriv->apl);
896         }
897
898         tcb = (Tcpctl*)s->ptcl;
899
900         inittcpctl(s, mode);
901
902         iphtadd(&tpriv->ht, s);
903         switch(mode) {
904         case TCP_LISTEN:
905                 tpriv->stats[PassiveOpens]++;
906                 tcb->flags |= CLONE;
907                 tcpsetstate(s, Listen);
908                 break;
909
910         case TCP_CONNECT:
911                 tpriv->stats[ActiveOpens]++;
912                 tcb->flags |= ACTIVE;
913                 tcpsndsyn(s, tcb);
914                 tcpsetstate(s, Syn_sent);
915                 tcpoutput(s);
916                 break;
917         }
918 }
919
920 static char*
921 tcpflag(uint16_t flag)
922 {
923         static char buf[128];
924
925         snprintf(buf, sizeof(buf), "%d", flag>>10);     /* Head len */
926         if(flag & URG)
927                 snprintf(buf, sizeof(buf), "%s%s", buf, " URG");
928         if(flag & ACK)
929                 snprintf(buf, sizeof(buf), "%s%s", buf, " ACK");
930         if(flag & PSH)
931                 snprintf(buf, sizeof(buf), "%s%s", buf, " PSH");
932         if(flag & RST)
933                 snprintf(buf, sizeof(buf), "%s%s", buf, " RST");
934         if(flag & SYN)
935                 snprintf(buf, sizeof(buf), "%s%s", buf, " SYN");
936         if(flag & FIN)
937                 snprintf(buf, sizeof(buf), "%s%s", buf, " FIN");
938
939         return buf;
940 }
941
942 struct block *
943 htontcp6(Tcp *tcph, struct block *data, Tcp6hdr *ph, Tcpctl *tcb)
944 {
945         int dlen;
946         Tcp6hdr *h;
947         uint16_t csum;
948         uint16_t hdrlen, optpad = 0;
949         uint8_t *opt;
950
951         hdrlen = TCP6_HDRSIZE;
952         if(tcph->flags & SYN){
953                 if(tcph->mss)
954                         hdrlen += MSS_LENGTH;
955                 if(tcph->ws)
956                         hdrlen += WS_LENGTH;
957                 optpad = hdrlen & 3;
958                 if(optpad)
959                         optpad = 4 - optpad;
960                 hdrlen += optpad;
961         }
962
963         if(data) {
964                 dlen = blocklen(data);
965                 data = padblock(data, hdrlen + TCP6_PKT);
966                 if(data == NULL)
967                         return NULL;
968         }
969         else {
970                 dlen = 0;
971                 data = allocb(hdrlen + TCP6_PKT + 64);  /* the 64 pad is to meet mintu's */
972                 if(data == NULL)
973                         return NULL;
974                 data->wp += hdrlen + TCP6_PKT;
975         }
976
977         /* copy in pseudo ip header plus port numbers */
978         h = (Tcp6hdr *)(data->rp);
979         memmove(h, ph, TCP6_TCBPHDRSZ);
980
981         /* compose pseudo tcp header, do cksum calculation */
982         hnputl(h->vcf, hdrlen + dlen);
983         h->ploadlen[0] = h->ploadlen[1] = h->proto = 0;
984         h->ttl = ph->proto;
985
986         /* copy in variable bits */
987         hnputl(h->tcpseq, tcph->seq);
988         hnputl(h->tcpack, tcph->ack);
989         hnputs(h->tcpflag, (hdrlen<<10) | tcph->flags);
990         hnputs(h->tcpwin, tcph->wnd>>(tcb != NULL ? tcb->snd.scale : 0));
991         hnputs(h->tcpurg, tcph->urg);
992
993         if(tcph->flags & SYN){
994                 opt = h->tcpopt;
995                 if(tcph->mss != 0){
996                         *opt++ = MSSOPT;
997                         *opt++ = MSS_LENGTH;
998                         hnputs(opt, tcph->mss);
999                         opt += 2;
1000                 }
1001                 if(tcph->ws != 0){
1002                         *opt++ = WSOPT;
1003                         *opt++ = WS_LENGTH;
1004                         *opt++ = tcph->ws;
1005                 }
1006                 while(optpad-- > 0)
1007                         *opt++ = NOOPOPT;
1008         }
1009
1010         if(tcb != NULL && tcb->nochecksum){
1011                 h->tcpcksum[0] = h->tcpcksum[1] = 0;
1012         } else {
1013                 csum = ptclcsum(data, TCP6_IPLEN, hdrlen+dlen+TCP6_PHDRSIZE);
1014                 hnputs(h->tcpcksum, csum);
1015         }
1016
1017         /* move from pseudo header back to normal ip header */
1018         memset(h->vcf, 0, 4);
1019         h->vcf[0] = IP_VER6;
1020         hnputs(h->ploadlen, hdrlen+dlen);
1021         h->proto = ph->proto;
1022
1023         return data;
1024 }
1025
1026 struct block *
1027 htontcp4(Tcp *tcph, struct block *data, Tcp4hdr *ph, Tcpctl *tcb)
1028 {
1029         int dlen;
1030         Tcp4hdr *h;
1031         uint16_t csum;
1032         uint16_t hdrlen, optpad = 0;
1033         uint8_t *opt;
1034
1035         hdrlen = TCP4_HDRSIZE;
1036         if(tcph->flags & SYN){
1037                 if(tcph->mss)
1038                         hdrlen += MSS_LENGTH;
1039                 if(tcph->ws)
1040                         hdrlen += WS_LENGTH;
1041                 optpad = hdrlen & 3;
1042                 if(optpad)
1043                         optpad = 4 - optpad;
1044                 hdrlen += optpad;
1045         }
1046
1047         if(data) {
1048                 dlen = blocklen(data);
1049                 data = padblock(data, hdrlen + TCP4_PKT);
1050                 if(data == NULL)
1051                         return NULL;
1052         }
1053         else {
1054                 dlen = 0;
1055                 data = allocb(hdrlen + TCP4_PKT + 64);  /* the 64 pad is to meet mintu's */
1056                 if(data == NULL)
1057                         return NULL;
1058                 data->wp += hdrlen + TCP4_PKT;
1059         }
1060
1061         /* copy in pseudo ip header plus port numbers */
1062         h = (Tcp4hdr *)(data->rp);
1063         memmove(h, ph, TCP4_TCBPHDRSZ);
1064
1065         /* copy in variable bits */
1066         hnputs(h->tcplen, hdrlen + dlen);
1067         hnputl(h->tcpseq, tcph->seq);
1068         hnputl(h->tcpack, tcph->ack);
1069         hnputs(h->tcpflag, (hdrlen<<10) | tcph->flags);
1070         hnputs(h->tcpwin, tcph->wnd>>(tcb != NULL ? tcb->snd.scale : 0));
1071         hnputs(h->tcpurg, tcph->urg);
1072
1073         if(tcph->flags & SYN){
1074                 opt = h->tcpopt;
1075                 if(tcph->mss != 0){
1076                         *opt++ = MSSOPT;
1077                         *opt++ = MSS_LENGTH;
1078                         hnputs(opt, tcph->mss);
1079                         opt += 2;
1080                 }
1081                 if(tcph->ws != 0){
1082                         *opt++ = WSOPT;
1083                         *opt++ = WS_LENGTH;
1084                         *opt++ = tcph->ws;
1085                 }
1086                 while(optpad-- > 0)
1087                         *opt++ = NOOPOPT;
1088         }
1089
1090         if(tcb != NULL && tcb->nochecksum){
1091                 h->tcpcksum[0] = h->tcpcksum[1] = 0;
1092         } else {
1093                 csum = ptclcsum(data, TCP4_IPLEN, hdrlen+dlen+TCP4_PHDRSIZE);
1094                 hnputs(h->tcpcksum, csum);
1095         }
1096
1097         return data;
1098 }
1099
1100 int
1101 ntohtcp6(Tcp *tcph, struct block **bpp)
1102 {
1103         Tcp6hdr *h;
1104         uint8_t *optr;
1105         uint16_t hdrlen;
1106         uint16_t optlen;
1107         int n;
1108
1109         *bpp = pullupblock(*bpp, TCP6_PKT+TCP6_HDRSIZE);
1110         if(*bpp == NULL)
1111                 return -1;
1112
1113         h = (Tcp6hdr *)((*bpp)->rp);
1114         tcph->source = nhgets(h->tcpsport);
1115         tcph->dest = nhgets(h->tcpdport);
1116         tcph->seq = nhgetl(h->tcpseq);
1117         tcph->ack = nhgetl(h->tcpack);
1118         hdrlen = (h->tcpflag[0]>>2) & ~3;
1119         if(hdrlen < TCP6_HDRSIZE) {
1120                 freeblist(*bpp);
1121                 return -1;
1122         }
1123
1124         tcph->flags = h->tcpflag[1];
1125         tcph->wnd = nhgets(h->tcpwin);
1126         tcph->urg = nhgets(h->tcpurg);
1127         tcph->mss = 0;
1128         tcph->ws = 0;
1129         tcph->len = nhgets(h->ploadlen) - hdrlen;
1130
1131         *bpp = pullupblock(*bpp, hdrlen+TCP6_PKT);
1132         if(*bpp == NULL)
1133                 return -1;
1134
1135         optr = h->tcpopt;
1136         n = hdrlen - TCP6_HDRSIZE;
1137         while(n > 0 && *optr != EOLOPT) {
1138                 if(*optr == NOOPOPT) {
1139                         n--;
1140                         optr++;
1141                         continue;
1142                 }
1143                 optlen = optr[1];
1144                 if(optlen < 2 || optlen > n)
1145                         break;
1146                 switch(*optr) {
1147                 case MSSOPT:
1148                         if(optlen == MSS_LENGTH)
1149                                 tcph->mss = nhgets(optr+2);
1150                         break;
1151                 case WSOPT:
1152                         if(optlen == WS_LENGTH && *(optr+2) <= 14)
1153                                 tcph->ws = HaveWS | *(optr+2);
1154                         break;
1155                 }
1156                 n -= optlen;
1157                 optr += optlen;
1158         }
1159         return hdrlen;
1160 }
1161
1162 int
1163 ntohtcp4(Tcp *tcph, struct block **bpp)
1164 {
1165         Tcp4hdr *h;
1166         uint8_t *optr;
1167         uint16_t hdrlen;
1168         uint16_t optlen;
1169         int n;
1170
1171         *bpp = pullupblock(*bpp, TCP4_PKT+TCP4_HDRSIZE);
1172         if(*bpp == NULL)
1173                 return -1;
1174
1175         h = (Tcp4hdr *)((*bpp)->rp);
1176         tcph->source = nhgets(h->tcpsport);
1177         tcph->dest = nhgets(h->tcpdport);
1178         tcph->seq = nhgetl(h->tcpseq);
1179         tcph->ack = nhgetl(h->tcpack);
1180
1181         hdrlen = (h->tcpflag[0]>>2) & ~3;
1182         if(hdrlen < TCP4_HDRSIZE) {
1183                 freeblist(*bpp);
1184                 return -1;
1185         }
1186
1187         tcph->flags = h->tcpflag[1];
1188         tcph->wnd = nhgets(h->tcpwin);
1189         tcph->urg = nhgets(h->tcpurg);
1190         tcph->mss = 0;
1191         tcph->ws = 0;
1192         tcph->len = nhgets(h->length) - (hdrlen + TCP4_PKT);
1193
1194         *bpp = pullupblock(*bpp, hdrlen+TCP4_PKT);
1195         if(*bpp == NULL)
1196                 return -1;
1197
1198         optr = h->tcpopt;
1199         n = hdrlen - TCP4_HDRSIZE;
1200         while(n > 0 && *optr != EOLOPT) {
1201                 if(*optr == NOOPOPT) {
1202                         n--;
1203                         optr++;
1204                         continue;
1205                 }
1206                 optlen = optr[1];
1207                 if(optlen < 2 || optlen > n)
1208                         break;
1209                 switch(*optr) {
1210                 case MSSOPT:
1211                         if(optlen == MSS_LENGTH)
1212                                 tcph->mss = nhgets(optr+2);
1213                         break;
1214                 case WSOPT:
1215                         if(optlen == WS_LENGTH && *(optr+2) <= 14)
1216                                 tcph->ws = HaveWS | *(optr+2);
1217                         break;
1218                 }
1219                 n -= optlen;
1220                 optr += optlen;
1221         }
1222         return hdrlen;
1223 }
1224
1225 /*
1226  *  For outgiing calls, generate an initial sequence
1227  *  number and put a SYN on the send queue
1228  */
1229 void
1230 tcpsndsyn(struct conv *s, Tcpctl *tcb)
1231 {
1232         tcb->iss = (nrand(1<<16)<<16)|nrand(1<<16);
1233         tcb->rttseq = tcb->iss;
1234         tcb->snd.wl2 = tcb->iss;
1235         tcb->snd.una = tcb->iss;
1236         tcb->snd.ptr = tcb->rttseq;
1237         tcb->snd.nxt = tcb->rttseq;
1238         tcb->flgcnt++;
1239         tcb->flags |= FORCE;
1240         tcb->sndsyntime = NOW;
1241
1242         /* set desired mss and scale */
1243         tcb->mss = tcpmtu(s->p, s->laddr, s->ipversion, &tcb->scale);
1244 }
1245
1246 void
1247 sndrst(struct Proto *tcp, uint8_t *source, uint8_t *dest,
1248        uint16_t length, Tcp *seg, uint8_t version, char *reason)
1249 {
1250         struct block *hbp;
1251         uint8_t rflags;
1252         struct tcppriv *tpriv;
1253         Tcp4hdr ph4;
1254         Tcp6hdr ph6;
1255
1256         netlog(tcp->f, Logtcp, "sndrst: %s", reason);
1257
1258         tpriv = tcp->priv;
1259
1260         if(seg->flags & RST)
1261                 return;
1262
1263         /* make pseudo header */
1264         switch(version) {
1265         case V4:
1266                 memset(&ph4, 0, sizeof(ph4));
1267                 ph4.vihl = IP_VER4;
1268                 v6tov4(ph4.tcpsrc, dest);
1269                 v6tov4(ph4.tcpdst, source);
1270                 ph4.proto = IP_TCPPROTO;
1271                 hnputs(ph4.tcplen, TCP4_HDRSIZE);
1272                 hnputs(ph4.tcpsport, seg->dest);
1273                 hnputs(ph4.tcpdport, seg->source);
1274                 break;
1275         case V6:
1276                 memset(&ph6, 0, sizeof(ph6));
1277                 ph6.vcf[0] = IP_VER6;
1278                 ipmove(ph6.tcpsrc, dest);
1279                 ipmove(ph6.tcpdst, source);
1280                 ph6.proto = IP_TCPPROTO;
1281                 hnputs(ph6.ploadlen, TCP6_HDRSIZE);
1282                 hnputs(ph6.tcpsport, seg->dest);
1283                 hnputs(ph6.tcpdport, seg->source);
1284                 break;
1285         default:
1286                 panic("sndrst: version %d", version);
1287         }
1288
1289         tpriv->stats[OutRsts]++;
1290         rflags = RST;
1291
1292         /* convince the other end that this reset is in band */
1293         if(seg->flags & ACK) {
1294                 seg->seq = seg->ack;
1295                 seg->ack = 0;
1296         }
1297         else {
1298                 rflags |= ACK;
1299                 seg->ack = seg->seq;
1300                 seg->seq = 0;
1301                 if(seg->flags & SYN)
1302                         seg->ack++;
1303                 seg->ack += length;
1304                 if(seg->flags & FIN)
1305                         seg->ack++;
1306         }
1307         seg->flags = rflags;
1308         seg->wnd = 0;
1309         seg->urg = 0;
1310         seg->mss = 0;
1311         seg->ws = 0;
1312         switch(version) {
1313         case V4:
1314                 hbp = htontcp4(seg, NULL, &ph4, NULL);
1315                 if(hbp == NULL)
1316                         return;
1317                 ipoput4(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1318                 break;
1319         case V6:
1320                 hbp = htontcp6(seg, NULL, &ph6, NULL);
1321                 if(hbp == NULL)
1322                         return;
1323                 ipoput6(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1324                 break;
1325         default:
1326                 panic("sndrst2: version %d", version);
1327         }
1328 }
1329
1330 /*
1331  *  send a reset to the remote side and close the conversation
1332  *  called with s qlocked
1333  */
1334 char*
1335 tcphangup(struct conv *s)
1336 {
1337         ERRSTACK(2);
1338         Tcp seg;
1339         Tcpctl *tcb;
1340         struct block *hbp;
1341
1342         tcb = (Tcpctl*)s->ptcl;
1343         if(waserror())
1344                 return commonerror();
1345         if(s->raddr != 0) {
1346                 if(!waserror()){
1347                         seg.flags = RST | ACK;
1348                         seg.ack = tcb->rcv.nxt;
1349                         tcb->rcv.una = 0;
1350                         seg.seq = tcb->snd.ptr;
1351                         seg.wnd = 0;
1352                         seg.urg = 0;
1353                         seg.mss = 0;
1354                         seg.ws = 0;
1355                         switch(s->ipversion) {
1356                         case V4:
1357                                 tcb->protohdr.tcp4hdr.vihl = IP_VER4;
1358                                 hbp = htontcp4(&seg, NULL, &tcb->protohdr.tcp4hdr, tcb);
1359                                 ipoput4(s->p->f, hbp, 0, s->ttl, s->tos, s);
1360                                 break;
1361                         case V6:
1362                                 tcb->protohdr.tcp6hdr.vcf[0] = IP_VER6;
1363                                 hbp = htontcp6(&seg, NULL, &tcb->protohdr.tcp6hdr, tcb);
1364                                 ipoput6(s->p->f, hbp, 0, s->ttl, s->tos, s);
1365                                 break;
1366                         default:
1367                                 panic("tcphangup: version %d", s->ipversion);
1368                         }
1369                         poperror();
1370                 }
1371         }
1372         localclose(s, NULL);
1373         poperror();
1374         return NULL;
1375 }
1376
1377 /*
1378  *  (re)send a SYN ACK
1379  */
1380 int
1381 sndsynack(struct Proto *tcp, Limbo *lp)
1382 {
1383         struct block *hbp;
1384         Tcp4hdr ph4;
1385         Tcp6hdr ph6;
1386         Tcp seg;
1387         int scale;
1388
1389         /* make pseudo header */
1390         switch(lp->version) {
1391         case V4:
1392                 memset(&ph4, 0, sizeof(ph4));
1393                 ph4.vihl = IP_VER4;
1394                 v6tov4(ph4.tcpsrc, lp->laddr);
1395                 v6tov4(ph4.tcpdst, lp->raddr);
1396                 ph4.proto = IP_TCPPROTO;
1397                 hnputs(ph4.tcplen, TCP4_HDRSIZE);
1398                 hnputs(ph4.tcpsport, lp->lport);
1399                 hnputs(ph4.tcpdport, lp->rport);
1400                 break;
1401         case V6:
1402                 memset(&ph6, 0, sizeof(ph6));
1403                 ph6.vcf[0] = IP_VER6;
1404                 ipmove(ph6.tcpsrc, lp->laddr);
1405                 ipmove(ph6.tcpdst, lp->raddr);
1406                 ph6.proto = IP_TCPPROTO;
1407                 hnputs(ph6.ploadlen, TCP6_HDRSIZE);
1408                 hnputs(ph6.tcpsport, lp->lport);
1409                 hnputs(ph6.tcpdport, lp->rport);
1410                 break;
1411         default:
1412                 panic("sndrst: version %d", lp->version);
1413         }
1414
1415         seg.seq = lp->iss;
1416         seg.ack = lp->irs+1;
1417         seg.flags = SYN|ACK;
1418         seg.urg = 0;
1419         seg.mss = tcpmtu(tcp, lp->laddr, lp->version, &scale);
1420         seg.wnd = QMAX;
1421
1422         /* if the other side set scale, we should too */
1423         if(lp->rcvscale){
1424                 seg.ws = scale;
1425                 lp->sndscale = scale;
1426         } else {
1427                 seg.ws = 0;
1428                 lp->sndscale = 0;
1429         }
1430
1431         switch(lp->version) {
1432         case V4:
1433                 hbp = htontcp4(&seg, NULL, &ph4, NULL);
1434                 if(hbp == NULL)
1435                         return -1;
1436                 ipoput4(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1437                 break;
1438         case V6:
1439                 hbp = htontcp6(&seg, NULL, &ph6, NULL);
1440                 if(hbp == NULL)
1441                         return -1;
1442                 ipoput6(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1443                 break;
1444         default:
1445                 panic("sndsnack: version %d", lp->version);
1446         }
1447         lp->lastsend = NOW;
1448         return 0;
1449 }
1450
1451 #define hashipa(a, p) ( ( (a)[IPaddrlen-2] + (a)[IPaddrlen-1] + p )&LHTMASK )
1452
1453 /*
1454  *  put a call into limbo and respond with a SYN ACK
1455  *
1456  *  called with proto locked
1457  */
1458 static void
1459 limbo(struct conv *s, uint8_t *source, uint8_t *dest, Tcp *seg, int version)
1460 {
1461         Limbo *lp, **l;
1462         struct tcppriv *tpriv;
1463         int h;
1464
1465         tpriv = s->p->priv;
1466         h = hashipa(source, seg->source);
1467
1468         for(l = &tpriv->lht[h]; *l != NULL; l = &lp->next){
1469                 lp = *l;
1470                 if(lp->lport != seg->dest || lp->rport != seg->source || lp->version != version)
1471                         continue;
1472                 if(ipcmp(lp->raddr, source) != 0)
1473                         continue;
1474                 if(ipcmp(lp->laddr, dest) != 0)
1475                         continue;
1476
1477                 /* each new SYN restarts the retransmits */
1478                 lp->irs = seg->seq;
1479                 break;
1480         }
1481         lp = *l;
1482         if(lp == NULL){
1483                 if(tpriv->nlimbo >= Maxlimbo && tpriv->lht[h]){
1484                         lp = tpriv->lht[h];
1485                         tpriv->lht[h] = lp->next;
1486                         lp->next = NULL;
1487                 } else {
1488                         lp = kzmalloc(sizeof(*lp), 0);
1489                         if(lp == NULL)
1490                                 return;
1491                         tpriv->nlimbo++;
1492                 }
1493                 *l = lp;
1494                 lp->version = version;
1495                 ipmove(lp->laddr, dest);
1496                 ipmove(lp->raddr, source);
1497                 lp->lport = seg->dest;
1498                 lp->rport = seg->source;
1499                 lp->mss = seg->mss;
1500                 lp->rcvscale = seg->ws;
1501                 lp->irs = seg->seq;
1502                 lp->iss = (nrand(1<<16)<<16)|nrand(1<<16);
1503         }
1504
1505         if(sndsynack(s->p, lp) < 0){
1506                 *l = lp->next;
1507                 tpriv->nlimbo--;
1508                 kfree(lp);
1509         }
1510 }
1511
1512 /*
1513  *  resend SYN ACK's once every SYNACK_RXTIMER ms.
1514  */
1515 static void
1516 limborexmit(struct Proto *tcp)
1517 {
1518         struct tcppriv *tpriv;
1519         Limbo **l, *lp;
1520         int h;
1521         int seen;
1522         uint32_t now;
1523
1524         tpriv = tcp->priv;
1525
1526         if(!canqlock(&tcp->qlock))
1527                 return;
1528         seen = 0;
1529         now = NOW;
1530         for(h = 0; h < NLHT && seen < tpriv->nlimbo; h++){
1531                 for(l = &tpriv->lht[h]; *l != NULL && seen < tpriv->nlimbo; ){
1532                         lp = *l;
1533                         seen++;
1534                         if(now - lp->lastsend < (lp->rexmits+1)*SYNACK_RXTIMER)
1535                                 continue;
1536
1537                         /* time it out after 1 second */
1538                         if(++(lp->rexmits) > 5){
1539                                 tpriv->nlimbo--;
1540                                 *l = lp->next;
1541                                 kfree(lp);
1542                                 continue;
1543                         }
1544
1545                         /* if we're being attacked, don't bother resending SYN ACK's */
1546                         if(tpriv->nlimbo > 100)
1547                                 continue;
1548
1549                         if(sndsynack(tcp, lp) < 0){
1550                                 tpriv->nlimbo--;
1551                                 *l = lp->next;
1552                                 kfree(lp);
1553                                 continue;
1554                         }
1555
1556                         l = &lp->next;
1557                 }
1558         }
1559         qunlock(&tcp->qlock);
1560 }
1561
1562 /*
1563  *  lookup call in limbo.  if found, throw it out.
1564  *
1565  *  called with proto locked
1566  */
1567 static void
1568 limborst(struct conv *s, Tcp *segp, uint8_t *src, uint8_t *dst, uint8_t version)
1569 {
1570         Limbo *lp, **l;
1571         int h;
1572         struct tcppriv *tpriv;
1573
1574         tpriv = s->p->priv;
1575
1576         /* find a call in limbo */
1577         h = hashipa(src, segp->source);
1578         for(l = &tpriv->lht[h]; *l != NULL; l = &lp->next){
1579                 lp = *l;
1580                 if(lp->lport != segp->dest || lp->rport != segp->source || lp->version != version)
1581                         continue;
1582                 if(ipcmp(lp->laddr, dst) != 0)
1583                         continue;
1584                 if(ipcmp(lp->raddr, src) != 0)
1585                         continue;
1586
1587                 /* RST can only follow the SYN */
1588                 if(segp->seq == lp->irs+1){
1589                         tpriv->nlimbo--;
1590                         *l = lp->next;
1591                         kfree(lp);
1592                 }
1593                 break;
1594         }
1595 }
1596
1597 /*
1598  *  come here when we finally get an ACK to our SYN-ACK.
1599  *  lookup call in limbo.  if found, create a new conversation
1600  *
1601  *  called with proto locked
1602  */
1603 static struct conv*
1604 tcpincoming(struct conv *s, Tcp *segp, uint8_t *src, uint8_t *dst, uint8_t version)
1605 {
1606         struct conv *new;
1607         Tcpctl *tcb;
1608         struct tcppriv *tpriv;
1609         Tcp4hdr *h4;
1610         Tcp6hdr *h6;
1611         Limbo *lp, **l;
1612         int h;
1613
1614         /* unless it's just an ack, it can't be someone coming out of limbo */
1615         if((segp->flags & SYN) || (segp->flags & ACK) == 0)
1616                 return NULL;
1617
1618         tpriv = s->p->priv;
1619
1620         /* find a call in limbo */
1621         h = hashipa(src, segp->source);
1622         for(l = &tpriv->lht[h]; (lp = *l) != NULL; l = &lp->next){
1623                 netlog(s->p->f, Logtcp, "tcpincoming s %I,%ux/%I,%ux d %I,%ux/%I,%ux v %d/%d",
1624                         src, segp->source, lp->raddr, lp->rport,
1625                         dst, segp->dest, lp->laddr, lp->lport,
1626                         version, lp->version
1627                 );
1628
1629                 if(lp->lport != segp->dest || lp->rport != segp->source || lp->version != version)
1630                         continue;
1631                 if(ipcmp(lp->laddr, dst) != 0)
1632                         continue;
1633                 if(ipcmp(lp->raddr, src) != 0)
1634                         continue;
1635
1636                 /* we're assuming no data with the initial SYN */
1637                 if(segp->seq != lp->irs+1 || segp->ack != lp->iss+1){
1638                         netlog(s->p->f, Logtcp, "tcpincoming s %lux/%lux a %lux %lux",
1639                                 segp->seq, lp->irs+1, segp->ack, lp->iss+1);
1640                         lp = NULL;
1641                 } else {
1642                         tpriv->nlimbo--;
1643                         *l = lp->next;
1644                 }
1645                 break;
1646         }
1647         if(lp == NULL)
1648                 return NULL;
1649
1650         new = Fsnewcall(s, src, segp->source, dst, segp->dest, version);
1651         if(new == NULL)
1652                 return NULL;
1653
1654         memmove(new->ptcl, s->ptcl, sizeof(Tcpctl));
1655         tcb = (Tcpctl*)new->ptcl;
1656         tcb->flags &= ~CLONE;
1657         tcb->timer.arg = new;
1658         tcb->timer.state = TcptimerOFF;
1659         tcb->acktimer.arg = new;
1660         tcb->acktimer.state = TcptimerOFF;
1661         tcb->katimer.arg = new;
1662         tcb->katimer.state = TcptimerOFF;
1663         tcb->rtt_timer.arg = new;
1664         tcb->rtt_timer.state = TcptimerOFF;
1665
1666         tcb->irs = lp->irs;
1667         tcb->rcv.nxt = tcb->irs+1;
1668         tcb->rcv.urg = tcb->rcv.nxt;
1669
1670         tcb->iss = lp->iss;
1671         tcb->rttseq = tcb->iss;
1672         tcb->snd.wl2 = tcb->iss;
1673         tcb->snd.una = tcb->iss+1;
1674         tcb->snd.ptr = tcb->iss+1;
1675         tcb->snd.nxt = tcb->iss+1;
1676         tcb->flgcnt = 0;
1677         tcb->flags |= SYNACK;
1678
1679         /* our sending max segment size cannot be bigger than what he asked for */
1680         if(lp->mss != 0 && lp->mss < tcb->mss)
1681                 tcb->mss = lp->mss;
1682
1683         /* window scaling */
1684         tcpsetscale(new, tcb, lp->rcvscale, lp->sndscale);
1685
1686         /* the congestion window always starts out as a single segment */
1687         tcb->snd.wnd = segp->wnd;
1688         tcb->cwind = tcb->mss;
1689
1690         /* set initial round trip time */
1691         tcb->sndsyntime = lp->lastsend+lp->rexmits*SYNACK_RXTIMER;
1692         tcpsynackrtt(new);
1693
1694         kfree(lp);
1695
1696         /* set up proto header */
1697         switch(version){
1698         case V4:
1699                 h4 = &tcb->protohdr.tcp4hdr;
1700                 memset(h4, 0, sizeof(*h4));
1701                 h4->proto = IP_TCPPROTO;
1702                 hnputs(h4->tcpsport, new->lport);
1703                 hnputs(h4->tcpdport, new->rport);
1704                 v6tov4(h4->tcpsrc, dst);
1705                 v6tov4(h4->tcpdst, src);
1706                 break;
1707         case V6:
1708                 h6 = &tcb->protohdr.tcp6hdr;
1709                 memset(h6, 0, sizeof(*h6));
1710                 h6->proto = IP_TCPPROTO;
1711                 hnputs(h6->tcpsport, new->lport);
1712                 hnputs(h6->tcpdport, new->rport);
1713                 ipmove(h6->tcpsrc, dst);
1714                 ipmove(h6->tcpdst, src);
1715                 break;
1716         default:
1717                 panic("tcpincoming: version %d", new->ipversion);
1718         }
1719
1720         tcpsetstate(new, Established);
1721
1722         iphtadd(&tpriv->ht, new);
1723
1724         return new;
1725 }
1726
1727 int
1728 seq_within(uint32_t x, uint32_t low, uint32_t high)
1729 {
1730         if(low <= high){
1731                 if(low <= x && x <= high)
1732                         return 1;
1733         }
1734         else {
1735                 if(x >= low || x <= high)
1736                         return 1;
1737         }
1738         return 0;
1739 }
1740
1741 int
1742 seq_lt(uint32_t x, uint32_t y)
1743 {
1744         return (int)(x-y) < 0;
1745 }
1746
1747 int
1748 seq_le(uint32_t x, uint32_t y)
1749 {
1750         return (int)(x-y) <= 0;
1751 }
1752
1753 int
1754 seq_gt(uint32_t x, uint32_t y)
1755 {
1756         return (int)(x-y) > 0;
1757 }
1758
1759 int
1760 seq_ge(uint32_t x, uint32_t y)
1761 {
1762         return (int)(x-y) >= 0;
1763 }
1764
1765 /*
1766  *  use the time between the first SYN and it's ack as the
1767  *  initial round trip time
1768  */
1769 void
1770 tcpsynackrtt(struct conv *s)
1771 {
1772         Tcpctl *tcb;
1773         int delta;
1774         struct tcppriv *tpriv;
1775
1776         tcb = (Tcpctl*)s->ptcl;
1777         tpriv = s->p->priv;
1778
1779         delta = NOW - tcb->sndsyntime;
1780         tcb->srtt = delta<<LOGAGAIN;
1781         tcb->mdev = delta<<LOGDGAIN;
1782
1783         /* halt round trip timer */
1784         tcphalt(tpriv, &tcb->rtt_timer);
1785 }
1786
1787 void
1788 update(struct conv *s, Tcp *seg)
1789 {
1790         int rtt, delta;
1791         Tcpctl *tcb;
1792         uint32_t acked;
1793         uint32_t expand;
1794         struct tcppriv *tpriv;
1795
1796         tpriv = s->p->priv;
1797         tcb = (Tcpctl*)s->ptcl;
1798
1799         /* if everything has been acked, force output(?) */
1800         if(seq_gt(seg->ack, tcb->snd.nxt)) {
1801                 tcb->flags |= FORCE;
1802                 return;
1803         }
1804
1805         /* added by Dong Lin for fast retransmission */
1806         if(seg->ack == tcb->snd.una
1807         && tcb->snd.una != tcb->snd.nxt
1808         && seg->len == 0
1809         && seg->wnd == tcb->snd.wnd) {
1810
1811                 /* this is a pure ack w/o window update */
1812                 netlog(s->p->f, Logtcprxmt, "dupack %lud ack %lud sndwnd %d advwin %d\n",
1813                         tcb->snd.dupacks, seg->ack, tcb->snd.wnd, seg->wnd);
1814
1815                 if(++tcb->snd.dupacks == TCPREXMTTHRESH) {
1816                         /*
1817                          *  tahoe tcp rxt the packet, half sshthresh,
1818                          *  and set cwnd to one packet
1819                          */
1820                         tcb->snd.recovery = 1;
1821                         tcb->snd.rxt = tcb->snd.nxt;
1822                         netlog(s->p->f, Logtcprxmt, "fast rxt %lud, nxt %lud\n", tcb->snd.una, tcb->snd.nxt);
1823                         tcprxmit(s);
1824                 } else {
1825                         /* do reno tcp here. */
1826                 }
1827         }
1828
1829         /*
1830          *  update window
1831          */
1832         if(seq_gt(seg->ack, tcb->snd.wl2)
1833         || (tcb->snd.wl2 == seg->ack && seg->wnd > tcb->snd.wnd)){
1834                 tcb->snd.wnd = seg->wnd;
1835                 tcb->snd.wl2 = seg->ack;
1836         }
1837
1838         if(!seq_gt(seg->ack, tcb->snd.una)){
1839                 /*
1840                  *  don't let us hangup if sending into a closed window and
1841                  *  we're still getting acks
1842                  */
1843                 if((tcb->flags&RETRAN) && tcb->snd.wnd == 0){
1844                         tcb->backedoff = MAXBACKMS/4;
1845                 }
1846                 return;
1847         }
1848
1849         /*
1850          *  any positive ack turns off fast rxt,
1851          *  (should we do new-reno on partial acks?)
1852          */
1853         if(!tcb->snd.recovery || seq_ge(seg->ack, tcb->snd.rxt)) {
1854                 tcb->snd.dupacks = 0;
1855                 tcb->snd.recovery = 0;
1856         } else
1857                 netlog(s->p->f, Logtcp, "rxt next %lud, cwin %ud\n", seg->ack, tcb->cwind);
1858
1859         /* Compute the new send window size */
1860         acked = seg->ack - tcb->snd.una;
1861
1862         /* avoid slow start and timers for SYN acks */
1863         if((tcb->flags & SYNACK) == 0) {
1864                 tcb->flags |= SYNACK;
1865                 acked--;
1866                 tcb->flgcnt--;
1867                 goto done;
1868         }
1869
1870         /* slow start as long as we're not recovering from lost packets */
1871         if(tcb->cwind < tcb->snd.wnd && !tcb->snd.recovery) {
1872                 if(tcb->cwind < tcb->ssthresh) {
1873                         expand = tcb->mss;
1874                         if(acked < expand)
1875                                 expand = acked;
1876                 }
1877                 else
1878                         expand = ((int)tcb->mss * tcb->mss) / tcb->cwind;
1879
1880                 if(tcb->cwind + expand < tcb->cwind)
1881                         expand = tcb->snd.wnd - tcb->cwind;
1882                 if(tcb->cwind + expand > tcb->snd.wnd)
1883                         expand = tcb->snd.wnd - tcb->cwind;
1884                 tcb->cwind += expand;
1885         }
1886
1887         /* Adjust the timers according to the round trip time */
1888         if(tcb->rtt_timer.state == TcptimerON && seq_ge(seg->ack, tcb->rttseq)) {
1889                 tcphalt(tpriv, &tcb->rtt_timer);
1890                 if((tcb->flags&RETRAN) == 0) {
1891                         tcb->backoff = 0;
1892                         tcb->backedoff = 0;
1893                         rtt = tcb->rtt_timer.start - tcb->rtt_timer.count;
1894                         if(rtt == 0)
1895                                 rtt = 1;        /* otherwise all close systems will rexmit in 0 time */
1896                         rtt *= MSPTICK;
1897                         if(tcb->srtt == 0) {
1898                                 tcb->srtt = rtt << LOGAGAIN;
1899                                 tcb->mdev = rtt << LOGDGAIN;
1900                         } else {
1901                                 delta = rtt - (tcb->srtt>>LOGAGAIN);
1902                                 tcb->srtt += delta;
1903                                 if(tcb->srtt <= 0)
1904                                         tcb->srtt = 1;
1905
1906                                 delta = abs(delta) - (tcb->mdev>>LOGDGAIN);
1907                                 tcb->mdev += delta;
1908                                 if(tcb->mdev <= 0)
1909                                         tcb->mdev = 1;
1910                         }
1911                         tcpsettimer(tcb);
1912                 }
1913         }
1914
1915 done:
1916         if(qdiscard(s->wq, acked) < acked)
1917                 tcb->flgcnt--;
1918
1919         tcb->snd.una = seg->ack;
1920         if(seq_gt(seg->ack, tcb->snd.urg))
1921                 tcb->snd.urg = seg->ack;
1922
1923         if(tcb->snd.una != tcb->snd.nxt)
1924                 tcpgo(tpriv, &tcb->timer);
1925         else
1926                 tcphalt(tpriv, &tcb->timer);
1927
1928         if(seq_lt(tcb->snd.ptr, tcb->snd.una))
1929                 tcb->snd.ptr = tcb->snd.una;
1930
1931         tcb->flags &= ~RETRAN;
1932         tcb->backoff = 0;
1933         tcb->backedoff = 0;
1934 }
1935
1936 void
1937 tcpiput(struct Proto *tcp, struct Ipifc*unused, struct block *bp)
1938 {
1939         ERRSTACK(2);
1940         Tcp seg;
1941         Tcp4hdr *h4;
1942         Tcp6hdr *h6;
1943         int hdrlen;
1944         Tcpctl *tcb;
1945         uint16_t length;
1946         uint8_t source[IPaddrlen], dest[IPaddrlen];
1947         struct conv *s;
1948         struct Fs *f;
1949         struct tcppriv *tpriv;
1950         uint8_t version;
1951
1952         f = tcp->f;
1953         tpriv = tcp->priv;
1954
1955         tpriv->stats[InSegs]++;
1956
1957         h4 = (Tcp4hdr*)(bp->rp);
1958         h6 = (Tcp6hdr*)(bp->rp);
1959
1960         if((h4->vihl&0xF0)==IP_VER4) {
1961                 version = V4;
1962                 length = nhgets(h4->length);
1963                 v4tov6(dest, h4->tcpdst);
1964                 v4tov6(source, h4->tcpsrc);
1965
1966                 h4->Unused = 0;
1967                 hnputs(h4->tcplen, length-TCP4_PKT);
1968                 if(!(bp->flag & Btcpck) && (h4->tcpcksum[0] || h4->tcpcksum[1]) &&
1969                         ptclcsum(bp, TCP4_IPLEN, length-TCP4_IPLEN)) {
1970                         tpriv->stats[CsumErrs]++;
1971                         tpriv->stats[InErrs]++;
1972                         netlog(f, Logtcp, "bad tcp proto cksum\n");
1973                         freeblist(bp);
1974                         return;
1975                 }
1976
1977                 hdrlen = ntohtcp4(&seg, &bp);
1978                 if(hdrlen < 0){
1979                         tpriv->stats[HlenErrs]++;
1980                         tpriv->stats[InErrs]++;
1981                         netlog(f, Logtcp, "bad tcp hdr len\n");
1982                         return;
1983                 }
1984
1985                 /* trim the packet to the size claimed by the datagram */
1986                 length -= hdrlen+TCP4_PKT;
1987                 bp = trimblock(bp, hdrlen+TCP4_PKT, length);
1988                 if(bp == NULL){
1989                         tpriv->stats[LenErrs]++;
1990                         tpriv->stats[InErrs]++;
1991                         netlog(f, Logtcp, "tcp len < 0 after trim\n");
1992                         return;
1993                 }
1994         }
1995         else {
1996                 int ttl = h6->ttl;
1997                 int proto = h6->proto;
1998
1999                 version = V6;
2000                 length = nhgets(h6->ploadlen);
2001                 ipmove(dest, h6->tcpdst);
2002                 ipmove(source, h6->tcpsrc);
2003
2004                 h6->ploadlen[0] = h6->ploadlen[1] = h6->proto = 0;
2005                 h6->ttl = proto;
2006                 hnputl(h6->vcf, length);
2007                 if((h6->tcpcksum[0] || h6->tcpcksum[1]) &&
2008                         ptclcsum(bp, TCP6_IPLEN, length+TCP6_PHDRSIZE)) {
2009                         tpriv->stats[CsumErrs]++;
2010                         tpriv->stats[InErrs]++;
2011                         netlog(f, Logtcp, "bad tcp proto cksum\n");
2012                         freeblist(bp);
2013                         return;
2014                 }
2015                 h6->ttl = ttl;
2016                 h6->proto = proto;
2017                 hnputs(h6->ploadlen, length);
2018
2019                 hdrlen = ntohtcp6(&seg, &bp);
2020                 if(hdrlen < 0){
2021                         tpriv->stats[HlenErrs]++;
2022                         tpriv->stats[InErrs]++;
2023                         netlog(f, Logtcp, "bad tcp hdr len\n");
2024                         return;
2025                 }
2026
2027                 /* trim the packet to the size claimed by the datagram */
2028                 length -= hdrlen;
2029                 bp = trimblock(bp, hdrlen+TCP6_PKT, length);
2030                 if(bp == NULL){
2031                         tpriv->stats[LenErrs]++;
2032                         tpriv->stats[InErrs]++;
2033                         netlog(f, Logtcp, "tcp len < 0 after trim\n");
2034                         return;
2035                 }
2036         }
2037
2038         /* lock protocol while searching for a conversation */
2039         qlock(&tcp->qlock);
2040
2041         /* Look for a matching conversation */
2042         s = iphtlook(&tpriv->ht, source, seg.source, dest, seg.dest);
2043         if(s == NULL){
2044                 netlog(f, Logtcp, "iphtlook failed");
2045 reset:
2046                 qunlock(&tcp->qlock);
2047                 sndrst(tcp, source, dest, length, &seg, version, "no conversation");
2048                 freeblist(bp);
2049                 return;
2050         }
2051
2052         /* if it's a listener, look for the right flags and get a new conv */
2053         tcb = (Tcpctl*)s->ptcl;
2054         if(tcb->state == Listen){
2055                 if(seg.flags & RST){
2056                         limborst(s, &seg, source, dest, version);
2057                         qunlock(&tcp->qlock);
2058                         freeblist(bp);
2059                         return;
2060                 }
2061
2062                 /* if this is a new SYN, put the call into limbo */
2063                 if((seg.flags & SYN) && (seg.flags & ACK) == 0){
2064                         limbo(s, source, dest, &seg, version);
2065                         qunlock(&tcp->qlock);
2066                         freeblist(bp);
2067                         return;
2068                 }
2069
2070                 /*
2071                  *  if there's a matching call in limbo, tcpincoming will
2072                  *  return it in state Syn_received
2073                  */
2074                 s = tcpincoming(s, &seg, source, dest, version);
2075                 if(s == NULL)
2076                         goto reset;
2077         }
2078
2079         /* The rest of the input state machine is run with the control block
2080          * locked and implements the state machine directly out of the RFC.
2081          * Out-of-band data is ignored - it was always a bad idea.
2082          */
2083         tcb = (Tcpctl*)s->ptcl;
2084         if(waserror()){
2085                 qunlock(&s->qlock);
2086                 nexterror();
2087         }
2088         qlock(&s->qlock);
2089         qunlock(&tcp->qlock);
2090
2091         /* fix up window */
2092         seg.wnd <<= tcb->rcv.scale;
2093
2094         /* every input packet in puts off the keep alive time out */
2095         tcpsetkacounter(tcb);
2096
2097         switch(tcb->state) {
2098         case Closed:
2099                 sndrst(tcp, source, dest, length, &seg, version, "sending to Closed");
2100                 goto raise;
2101         case Syn_sent:
2102                 if(seg.flags & ACK) {
2103                         if(!seq_within(seg.ack, tcb->iss+1, tcb->snd.nxt)) {
2104                                 sndrst(tcp, source, dest, length, &seg, version,
2105                                          "bad seq in Syn_sent");
2106                                 goto raise;
2107                         }
2108                 }
2109                 if(seg.flags & RST) {
2110                         if(seg.flags & ACK)
2111                                 localclose(s, Econrefused);
2112                         goto raise;
2113                 }
2114
2115                 if(seg.flags & SYN) {
2116                         procsyn(s, &seg);
2117                         if(seg.flags & ACK){
2118                                 update(s, &seg);
2119                                 tcpsynackrtt(s);
2120                                 tcpsetstate(s, Established);
2121                                 tcpsetscale(s, tcb, seg.ws, tcb->scale);
2122                         }
2123                         else {
2124                                 tcb->time = NOW;
2125                                 tcpsetstate(s, Syn_received);   /* DLP - shouldn't this be a reset? */
2126                         }
2127
2128                         if(length != 0 || (seg.flags & FIN))
2129                                 break;
2130
2131                         freeblist(bp);
2132                         goto output;
2133                 }
2134                 else
2135                         freeblist(bp);
2136
2137                 qunlock(&s->qlock);
2138                 poperror();
2139                 return;
2140         case Syn_received:
2141                 /* doesn't matter if it's the correct ack, we're just trying to set timing */
2142                 if(seg.flags & ACK)
2143                         tcpsynackrtt(s);
2144                 break;
2145         }
2146
2147         /*
2148          *  One DOS attack is to open connections to us and then forget about them,
2149          *  thereby tying up a conv at no long term cost to the attacker.
2150          *  This is an attempt to defeat these stateless DOS attacks.  See
2151          *  corresponding code in tcpsendka().
2152          */
2153         if(tcb->state != Syn_received && (seg.flags & RST) == 0){
2154                 if(tcpporthogdefense
2155                 && seq_within(seg.ack, tcb->snd.una-(1<<31), tcb->snd.una-(1<<29))){
2156                         printd("stateless hog %I.%d->%I.%d f %ux %lux - %lux - %lux\n",
2157                                 source, seg.source, dest, seg.dest, seg.flags,
2158                                 tcb->snd.una-(1<<31), seg.ack, tcb->snd.una-(1<<29));
2159                         localclose(s, "stateless hog");
2160                 }
2161         }
2162
2163         /* Cut the data to fit the receive window */
2164         if(tcptrim(tcb, &seg, &bp, &length) == -1) {
2165                 netlog(f, Logtcp, "tcp len < 0, %lud %d\n", seg.seq, length);
2166                 update(s, &seg);
2167                 if(qlen(s->wq)+tcb->flgcnt == 0 && tcb->state == Closing) {
2168                         tcphalt(tpriv, &tcb->rtt_timer);
2169                         tcphalt(tpriv, &tcb->acktimer);
2170                         tcphalt(tpriv, &tcb->katimer);
2171                         tcpsetstate(s, Time_wait);
2172                         tcb->timer.start = MSL2*(1000 / MSPTICK);
2173                         tcpgo(tpriv, &tcb->timer);
2174                 }
2175                 if(!(seg.flags & RST)) {
2176                         tcb->flags |= FORCE;
2177                         goto output;
2178                 }
2179                 qunlock(&s->qlock);
2180                 poperror();
2181                 return;
2182         }
2183
2184         /* Cannot accept so answer with a rst */
2185         if(length && tcb->state == Closed) {
2186                 sndrst(tcp, source, dest, length, &seg, version, "sending to Closed");
2187                 goto raise;
2188         }
2189
2190         /* The segment is beyond the current receive pointer so
2191          * queue the data in the resequence queue
2192          */
2193         if(seg.seq != tcb->rcv.nxt)
2194         if(length != 0 || (seg.flags & (SYN|FIN))) {
2195                 update(s, &seg);
2196                 if(addreseq(tcb, tpriv, &seg, bp, length) < 0)
2197                         printd("reseq %I.%d -> %I.%d\n", s->raddr, s->rport, s->laddr, s->lport);
2198                 tcb->flags |= FORCE;
2199                 goto output;
2200         }
2201
2202         /*
2203          *  keep looping till we've processed this packet plus any
2204          *  adjacent packets in the resequence queue
2205          */
2206         for(;;) {
2207                 if(seg.flags & RST) {
2208                         if(tcb->state == Established) {
2209                                 tpriv->stats[EstabResets]++;
2210                                 if(tcb->rcv.nxt != seg.seq)
2211                                         printd("out of order RST rcvd: %I.%d -> %I.%d, rcv.nxt %lux seq %lux\n", s->raddr, s->rport, s->laddr, s->lport, tcb->rcv.nxt, seg.seq);
2212                         }
2213                         localclose(s, Econrefused);
2214                         goto raise;
2215                 }
2216
2217                 if((seg.flags&ACK) == 0)
2218                         goto raise;
2219
2220                 switch(tcb->state) {
2221                 case Syn_received:
2222                         if(!seq_within(seg.ack, tcb->snd.una+1, tcb->snd.nxt)){
2223                                 sndrst(tcp, source, dest, length, &seg, version,
2224                                         "bad seq in Syn_received");
2225                                 goto raise;
2226                         }
2227                         update(s, &seg);
2228                         tcpsetstate(s, Established);
2229                 case Established:
2230                 case Close_wait:
2231                         update(s, &seg);
2232                         break;
2233                 case Finwait1:
2234                         update(s, &seg);
2235                         if(qlen(s->wq)+tcb->flgcnt == 0){
2236                                 tcphalt(tpriv, &tcb->rtt_timer);
2237                                 tcphalt(tpriv, &tcb->acktimer);
2238                                 tcpsetkacounter(tcb);
2239                                 tcb->time = NOW;
2240                                 tcpsetstate(s, Finwait2);
2241                                 tcb->katimer.start = MSL2 * (1000 / MSPTICK);
2242                                 tcpgo(tpriv, &tcb->katimer);
2243                         }
2244                         break;
2245                 case Finwait2:
2246                         update(s, &seg);
2247                         break;
2248                 case Closing:
2249                         update(s, &seg);
2250                         if(qlen(s->wq)+tcb->flgcnt == 0) {
2251                                 tcphalt(tpriv, &tcb->rtt_timer);
2252                                 tcphalt(tpriv, &tcb->acktimer);
2253                                 tcphalt(tpriv, &tcb->katimer);
2254                                 tcpsetstate(s, Time_wait);
2255                                 tcb->timer.start = MSL2*(1000 / MSPTICK);
2256                                 tcpgo(tpriv, &tcb->timer);
2257                         }
2258                         break;
2259                 case Last_ack:
2260                         update(s, &seg);
2261                         if(qlen(s->wq)+tcb->flgcnt == 0) {
2262                                 localclose(s, NULL);
2263                                 goto raise;
2264                         }
2265                 case Time_wait:
2266                         tcb->flags |= FORCE;
2267                         if(tcb->timer.state != TcptimerON)
2268                                 tcpgo(tpriv, &tcb->timer);
2269                 }
2270
2271                 if((seg.flags&URG) && seg.urg) {
2272                         if(seq_gt(seg.urg + seg.seq, tcb->rcv.urg)) {
2273                                 tcb->rcv.urg = seg.urg + seg.seq;
2274                                 pullblock(&bp, seg.urg);
2275                         }
2276                 }
2277                 else
2278                 if(seq_gt(tcb->rcv.nxt, tcb->rcv.urg))
2279                         tcb->rcv.urg = tcb->rcv.nxt;
2280
2281                 if(length == 0) {
2282                         if(bp != NULL)
2283                                 freeblist(bp);
2284                 }
2285                 else {
2286                         switch(tcb->state){
2287                         default:
2288                                 /* Ignore segment text */
2289                                 if(bp != NULL)
2290                                         freeblist(bp);
2291                                 break;
2292
2293                         case Syn_received:
2294                         case Established:
2295                         case Finwait1:
2296                                 /* If we still have some data place on
2297                                  * receive queue
2298                                  */
2299                                 if(bp) {
2300                                         bp = packblock(bp);
2301                                         if(bp == NULL)
2302                                                 panic("tcp packblock");
2303                                         qpassnolim(s->rq, bp);
2304                                         bp = NULL;
2305
2306                                         /*
2307                                          *  Force an ack every 2 data messages.  This is
2308                                          *  a hack for rob to make his home system run
2309                                          *  faster.
2310                                          *
2311                                          *  this also keeps the standard TCP congestion
2312                                          *  control working since it needs an ack every
2313                                          *  2 max segs worth.  This is not quite that,
2314                                          *  but under a real stream is equivalent since
2315                                          *  every packet has a max seg in it.
2316                                          */
2317                                         if(++(tcb->rcv.una) >= 2)
2318                                                 tcb->flags |= FORCE;
2319                                 }
2320                                 tcb->rcv.nxt += length;
2321
2322                                 /*
2323                                  *  update our rcv window
2324                                  */
2325                                 tcprcvwin(s);
2326
2327                                 /*
2328                                  *  turn on the acktimer if there's something
2329                                  *  to ack
2330                                  */
2331                                 if(tcb->acktimer.state != TcptimerON)
2332                                         tcpgo(tpriv, &tcb->acktimer);
2333
2334                                 break;
2335                         case Finwait2:
2336                                 /* no process to read the data, send a reset */
2337                                 if(bp != NULL)
2338                                         freeblist(bp);
2339                                 sndrst(tcp, source, dest, length, &seg, version,
2340                                         "send to Finwait2");
2341                                 qunlock(&s->qlock);
2342                                 poperror();
2343                                 return;
2344                         }
2345                 }
2346
2347                 if(seg.flags & FIN) {
2348                         tcb->flags |= FORCE;
2349
2350                         switch(tcb->state) {
2351                         case Syn_received:
2352                         case Established:
2353                                 tcb->rcv.nxt++;
2354                                 tcpsetstate(s, Close_wait);
2355                                 break;
2356                         case Finwait1:
2357                                 tcb->rcv.nxt++;
2358                                 if(qlen(s->wq)+tcb->flgcnt == 0) {
2359                                         tcphalt(tpriv, &tcb->rtt_timer);
2360                                         tcphalt(tpriv, &tcb->acktimer);
2361                                         tcphalt(tpriv, &tcb->katimer);
2362                                         tcpsetstate(s, Time_wait);
2363                                         tcb->timer.start = MSL2*(1000/MSPTICK);
2364                                         tcpgo(tpriv, &tcb->timer);
2365                                 }
2366                                 else
2367                                         tcpsetstate(s, Closing);
2368                                 break;
2369                         case Finwait2:
2370                                 tcb->rcv.nxt++;
2371                                 tcphalt(tpriv, &tcb->rtt_timer);
2372                                 tcphalt(tpriv, &tcb->acktimer);
2373                                 tcphalt(tpriv, &tcb->katimer);
2374                                 tcpsetstate(s, Time_wait);
2375                                 tcb->timer.start = MSL2 * (1000/MSPTICK);
2376                                 tcpgo(tpriv, &tcb->timer);
2377                                 break;
2378                         case Close_wait:
2379                         case Closing:
2380                         case Last_ack:
2381                                 break;
2382                         case Time_wait:
2383                                 tcpgo(tpriv, &tcb->timer);
2384                                 break;
2385                         }
2386                 }
2387
2388                 /*
2389                  *  get next adjacent segment from the resequence queue.
2390                  *  dump/trim any overlapping segments
2391                  */
2392                 for(;;) {
2393                         if(tcb->reseq == NULL)
2394                                 goto output;
2395
2396                         if(seq_ge(tcb->rcv.nxt, tcb->reseq->seg.seq) == 0)
2397                                 goto output;
2398
2399                         getreseq(tcb, &seg, &bp, &length);
2400
2401                         if(tcptrim(tcb, &seg, &bp, &length) == 0)
2402                                 break;
2403                 }
2404         }
2405 output:
2406         tcpoutput(s);
2407         qunlock(&s->qlock);
2408         poperror();
2409         return;
2410 raise:
2411         qunlock(&s->qlock);
2412         poperror();
2413         freeblist(bp);
2414         tcpkick(s);
2415 }
2416
2417 /*
2418  *  always enters and exits with the s locked.  We drop
2419  *  the lock to ipoput the packet so some care has to be
2420  *  taken by callers.
2421  */
2422 void
2423 tcpoutput(struct conv *s)
2424 {
2425         Tcp seg;
2426         int msgs;
2427         Tcpctl *tcb;
2428         struct block *hbp, *bp;
2429         int sndcnt, n;
2430         uint32_t ssize, dsize, usable, sent;
2431         struct Fs *f;
2432         struct tcppriv *tpriv;
2433         uint8_t version;
2434
2435         f = s->p->f;
2436         tpriv = s->p->priv;
2437         version = s->ipversion;
2438
2439         for(msgs = 0; msgs < 100; msgs++) {
2440                 tcb = (Tcpctl*)s->ptcl;
2441
2442                 switch(tcb->state) {
2443                 case Listen:
2444                 case Closed:
2445                 case Finwait2:
2446                         return;
2447                 }
2448
2449                 /* force an ack when a window has opened up */
2450                 if(tcb->rcv.blocked && tcb->rcv.wnd > 0){
2451                         tcb->rcv.blocked = 0;
2452                         tcb->flags |= FORCE;
2453                 }
2454
2455                 sndcnt = qlen(s->wq)+tcb->flgcnt;
2456                 sent = tcb->snd.ptr - tcb->snd.una;
2457
2458                 /* Don't send anything else until our SYN has been acked */
2459                 if(tcb->snd.ptr != tcb->iss && (tcb->flags & SYNACK) == 0)
2460                         break;
2461
2462                 /* Compute usable segment based on offered window and limit
2463                  * window probes to one
2464                  */
2465                 if(tcb->snd.wnd == 0){
2466                         if(sent != 0) {
2467                                 if((tcb->flags&FORCE) == 0)
2468                                         break;
2469 //                              tcb->snd.ptr = tcb->snd.una;
2470                         }
2471                         usable = 1;
2472                 }
2473                 else {
2474                         usable = tcb->cwind;
2475                         if(tcb->snd.wnd < usable)
2476                                 usable = tcb->snd.wnd;
2477                         usable -= sent;
2478                 }
2479                 ssize = sndcnt-sent;
2480                 if(ssize && usable < 2)
2481                         netlog(s->p->f, Logtcp, "throttled snd.wnd %lud cwind %lud\n",
2482                                 tcb->snd.wnd, tcb->cwind);
2483                 if(usable < ssize)
2484                         ssize = usable;
2485                 if(tcb->mss < ssize)
2486                         ssize = tcb->mss;
2487                 dsize = ssize;
2488                 seg.urg = 0;
2489
2490                 if(ssize == 0)
2491                 if((tcb->flags&FORCE) == 0)
2492                         break;
2493
2494                 tcb->flags &= ~FORCE;
2495                 tcprcvwin(s);
2496
2497                 /* By default we will generate an ack */
2498                 tcphalt(tpriv, &tcb->acktimer);
2499                 tcb->rcv.una = 0;
2500                 seg.source = s->lport;
2501                 seg.dest = s->rport;
2502                 seg.flags = ACK;
2503                 seg.mss = 0;
2504                 seg.ws = 0;
2505                 switch(tcb->state){
2506                 case Syn_sent:
2507                         seg.flags = 0;
2508                         if(tcb->snd.ptr == tcb->iss){
2509                                 seg.flags |= SYN;
2510                                 dsize--;
2511                                 seg.mss = tcb->mss;
2512                                 seg.ws = tcb->scale;
2513                         }
2514                         break;
2515                 case Syn_received:
2516                         /*
2517                          *  don't send any data with a SYN/ACK packet
2518                          *  because Linux rejects the packet in its
2519                          *  attempt to solve the SYN attack problem
2520                          */
2521                         if(tcb->snd.ptr == tcb->iss){
2522                                 seg.flags |= SYN;
2523                                 dsize = 0;
2524                                 ssize = 1;
2525                                 seg.mss = tcb->mss;
2526                                 seg.ws = tcb->scale;
2527                         }
2528                         break;
2529                 }
2530                 seg.seq = tcb->snd.ptr;
2531                 seg.ack = tcb->rcv.nxt;
2532                 seg.wnd = tcb->rcv.wnd;
2533
2534                 /* Pull out data to send */
2535                 bp = NULL;
2536                 if(dsize != 0) {
2537                         bp = qcopy(s->wq, dsize, sent);
2538                         if(BLEN(bp) != dsize) {
2539                                 seg.flags |= FIN;
2540                                 dsize--;
2541                         }
2542                 }
2543
2544                 if(sent+dsize == sndcnt)
2545                         seg.flags |= PSH;
2546
2547                 /* keep track of balance of resent data */
2548                 if(seq_lt(tcb->snd.ptr, tcb->snd.nxt)) {
2549                         n = tcb->snd.nxt - tcb->snd.ptr;
2550                         if(ssize < n)
2551                                 n = ssize;
2552                         tcb->resent += n;
2553                         netlog(f, Logtcp, "rexmit: %I.%d -> %I.%d ptr %lux nxt %lux\n",
2554                                 s->raddr, s->rport, s->laddr, s->lport, tcb->snd.ptr, tcb->snd.nxt);
2555                         tpriv->stats[RetransSegs]++;
2556                 }
2557
2558                 tcb->snd.ptr += ssize;
2559
2560                 /* Pull up the send pointer so we can accept acks
2561                  * for this window
2562                  */
2563                 if(seq_gt(tcb->snd.ptr,tcb->snd.nxt))
2564                         tcb->snd.nxt = tcb->snd.ptr;
2565
2566                 /* Build header, link data and compute cksum */
2567                 switch(version){
2568                 case V4:
2569                         tcb->protohdr.tcp4hdr.vihl = IP_VER4;
2570                         hbp = htontcp4(&seg, bp, &tcb->protohdr.tcp4hdr, tcb);
2571                         if(hbp == NULL) {
2572                                 freeblist(bp);
2573                                 return;
2574                         }
2575                         break;
2576                 case V6:
2577                         tcb->protohdr.tcp6hdr.vcf[0] = IP_VER6;
2578                         hbp = htontcp6(&seg, bp, &tcb->protohdr.tcp6hdr, tcb);
2579                         if(hbp == NULL) {
2580                                 freeblist(bp);
2581                                 return;
2582                         }
2583                         break;
2584                 default:
2585                         hbp = NULL;     /* to suppress a warning */
2586                         panic("tcpoutput: version %d", version);
2587                 }
2588
2589                 /* Start the transmission timers if there is new data and we
2590                  * expect acknowledges
2591                  */
2592                 if(ssize != 0){
2593                         if(tcb->timer.state != TcptimerON)
2594                                 tcpgo(tpriv, &tcb->timer);
2595
2596                         /*  If round trip timer isn't running, start it.
2597                          *  measure the longest packet only in case the
2598                          *  transmission time dominates RTT
2599                          */
2600                         if(tcb->rtt_timer.state != TcptimerON)
2601                         if(ssize == tcb->mss) {
2602                                 tcpgo(tpriv, &tcb->rtt_timer);
2603                                 tcb->rttseq = tcb->snd.ptr;
2604                         }
2605                 }
2606
2607                 tpriv->stats[OutSegs]++;
2608
2609                 /* put off the next keep alive */
2610                 tcpgo(tpriv, &tcb->katimer);
2611
2612                 switch(version){
2613                 case V4:
2614                         if(ipoput4(f, hbp, 0, s->ttl, s->tos, s) < 0){
2615                                 /* a negative return means no route */
2616                                 localclose(s, "no route");
2617                         }
2618                         break;
2619                 case V6:
2620                         if(ipoput6(f, hbp, 0, s->ttl, s->tos, s) < 0){
2621                                 /* a negative return means no route */
2622                                 localclose(s, "no route");
2623                         }
2624                         break;
2625                 default:
2626                         panic("tcpoutput2: version %d", version);
2627                 }
2628                 if((msgs%4) == 1){
2629                         qunlock(&s->qlock);
2630                         kthread_yield();
2631                         qlock(&s->qlock);
2632                 }
2633         }
2634 }
2635
2636 /*
2637  *  the BSD convention (hack?) for keep alives.  resend last uint8_t acked.
2638  */
2639 void
2640 tcpsendka(struct conv *s)
2641 {
2642         Tcp seg;
2643         Tcpctl *tcb;
2644         struct block *hbp,*dbp;
2645
2646         tcb = (Tcpctl*)s->ptcl;
2647
2648         dbp = NULL;
2649         seg.urg = 0;
2650         seg.source = s->lport;
2651         seg.dest = s->rport;
2652         seg.flags = ACK|PSH;
2653         seg.mss = 0;
2654         seg.ws = 0;
2655         if(tcpporthogdefense)
2656                 seg.seq = tcb->snd.una-(1<<30)-nrand(1<<20);
2657         else
2658                 seg.seq = tcb->snd.una-1;
2659         seg.ack = tcb->rcv.nxt;
2660         tcb->rcv.una = 0;
2661         seg.wnd = tcb->rcv.wnd;
2662         if(tcb->state == Finwait2){
2663                 seg.flags |= FIN;
2664         } else {
2665                 dbp = allocb(1);
2666                 dbp->wp++;
2667         }
2668
2669         if(isv4(s->raddr)) {
2670                 /* Build header, link data and compute cksum */
2671                 tcb->protohdr.tcp4hdr.vihl = IP_VER4;
2672                 hbp = htontcp4(&seg, dbp, &tcb->protohdr.tcp4hdr, tcb);
2673                 if(hbp == NULL) {
2674                         freeblist(dbp);
2675                         return;
2676                 }
2677                 ipoput4(s->p->f, hbp, 0, s->ttl, s->tos, s);
2678         }
2679         else {
2680                 /* Build header, link data and compute cksum */
2681                 tcb->protohdr.tcp6hdr.vcf[0] = IP_VER6;
2682                 hbp = htontcp6(&seg, dbp, &tcb->protohdr.tcp6hdr, tcb);
2683                 if(hbp == NULL) {
2684                         freeblist(dbp);
2685                         return;
2686                 }
2687                 ipoput6(s->p->f, hbp, 0, s->ttl, s->tos, s);
2688         }
2689 }
2690
2691 /*
2692  *  set connection to time out after 12 minutes
2693  */
2694 void
2695 tcpsetkacounter(Tcpctl *tcb)
2696 {
2697         tcb->kacounter = (12 * 60 * 1000) / (tcb->katimer.start*MSPTICK);
2698         if(tcb->kacounter < 3)
2699                 tcb->kacounter = 3;
2700 }
2701
2702 /*
2703  *  if we've timed out, close the connection
2704  *  otherwise, send a keepalive and restart the timer
2705  */
2706 void
2707 tcpkeepalive(void *v)
2708 {
2709         ERRSTACK(2);
2710         Tcpctl *tcb;
2711         struct conv *s;
2712
2713         s = v;
2714         tcb = (Tcpctl*)s->ptcl;
2715         if(waserror()){
2716                 qunlock(&s->qlock);
2717                 nexterror();
2718         }
2719         qlock(&s->qlock);
2720         if(tcb->state != Closed){
2721                 if(--(tcb->kacounter) <= 0) {
2722                         localclose(s, Etimedout);
2723                 } else {
2724                         tcpsendka(s);
2725                         tcpgo(s->p->priv, &tcb->katimer);
2726                 }
2727         }
2728         qunlock(&s->qlock);
2729         poperror();
2730 }
2731
2732 /*
2733  *  start keepalive timer
2734  */
2735 char*
2736 tcpstartka(struct conv *s, char **f, int n)
2737 {
2738         Tcpctl *tcb;
2739         int x;
2740
2741         tcb = (Tcpctl*)s->ptcl;
2742         if(tcb->state != Established)
2743                 return "connection must be in Establised state";
2744         if(n > 1){
2745                 x = atoi(f[1]);
2746                 if(x >= MSPTICK)
2747                         tcb->katimer.start = x/MSPTICK;
2748         }
2749         tcpsetkacounter(tcb);
2750         tcpgo(s->p->priv, &tcb->katimer);
2751
2752         return NULL;
2753 }
2754
2755 /*
2756  *  turn checksums on/off
2757  */
2758 char*
2759 tcpsetchecksum(struct conv *s, char **f, int unused)
2760 {
2761         Tcpctl *tcb;
2762
2763         tcb = (Tcpctl*)s->ptcl;
2764         tcb->nochecksum = !atoi(f[1]);
2765
2766         return NULL;
2767 }
2768
2769 void
2770 tcprxmit(struct conv *s)
2771 {
2772         Tcpctl *tcb;
2773
2774         tcb = (Tcpctl*)s->ptcl;
2775
2776         tcb->flags |= RETRAN|FORCE;
2777         tcb->snd.ptr = tcb->snd.una;
2778
2779         /*
2780          *  We should be halving the slow start threshhold (down to one
2781          *  mss) but leaving it at mss seems to work well enough
2782          */
2783         tcb->ssthresh = tcb->mss;
2784
2785         /*
2786          *  pull window down to a single packet
2787          */
2788         tcb->cwind = tcb->mss;
2789         tcpoutput(s);
2790 }
2791
2792 void
2793 tcptimeout(void *arg)
2794 {
2795         ERRSTACK(2);
2796         struct conv *s;
2797         Tcpctl *tcb;
2798         int maxback;
2799         struct tcppriv *tpriv;
2800
2801         s = (struct conv*)arg;
2802         tpriv = s->p->priv;
2803         tcb = (Tcpctl*)s->ptcl;
2804
2805         if(waserror()){
2806                 qunlock(&s->qlock);
2807                 nexterror();
2808         }
2809         qlock(&s->qlock);
2810         switch(tcb->state){
2811         default:
2812                 tcb->backoff++;
2813                 if(tcb->state == Syn_sent)
2814                         maxback = MAXBACKMS/2;
2815                 else
2816                         maxback = MAXBACKMS;
2817                 tcb->backedoff += tcb->timer.start * MSPTICK;
2818                 if(tcb->backedoff >= maxback) {
2819                         localclose(s, Etimedout);
2820                         break;
2821                 }
2822                 netlog(s->p->f, Logtcprxmt, "timeout rexmit 0x%lux %d/%d\n", tcb->snd.una, tcb->timer.start, NOW);
2823                 tcpsettimer(tcb);
2824                 tcprxmit(s);
2825                 tpriv->stats[RetransTimeouts]++;
2826                 tcb->snd.dupacks = 0;
2827                 break;
2828         case Time_wait:
2829                 localclose(s, NULL);
2830                 break;
2831         case Closed:
2832                 break;
2833         }
2834         qunlock(&s->qlock);
2835         poperror();
2836 }
2837
2838 int
2839 inwindow(Tcpctl *tcb, int seq)
2840 {
2841         return seq_within(seq, tcb->rcv.nxt, tcb->rcv.nxt+tcb->rcv.wnd-1);
2842 }
2843
2844 /*
2845  *  set up state for a received SYN (or SYN ACK) packet
2846  */
2847 void
2848 procsyn(struct conv *s, Tcp *seg)
2849 {
2850         Tcpctl *tcb;
2851
2852         tcb = (Tcpctl*)s->ptcl;
2853         tcb->flags |= FORCE;
2854
2855         tcb->rcv.nxt = seg->seq + 1;
2856         tcb->rcv.urg = tcb->rcv.nxt;
2857         tcb->irs = seg->seq;
2858
2859         /* our sending max segment size cannot be bigger than what he asked for */
2860         if(seg->mss != 0 && seg->mss < tcb->mss)
2861                 tcb->mss = seg->mss;
2862
2863         /* the congestion window always starts out as a single segment */
2864         tcb->snd.wnd = seg->wnd;
2865         tcb->cwind = tcb->mss;
2866 }
2867
2868 int
2869 addreseq(Tcpctl *tcb, struct tcppriv *tpriv, Tcp *seg,
2870          struct block *bp, uint16_t length)
2871 {
2872         Reseq *rp, *rp1;
2873         int i, rqlen, qmax;
2874
2875         rp = kzmalloc(sizeof(Reseq), 0);
2876         if(rp == NULL){
2877                 freeblist(bp);  /* bp always consumed by add_reseq */
2878                 return 0;
2879         }
2880
2881         rp->seg = *seg;
2882         rp->bp = bp;
2883         rp->length = length;
2884
2885         /* Place on reassembly list sorting by starting seq number */
2886         rp1 = tcb->reseq;
2887         if(rp1 == NULL || seq_lt(seg->seq, rp1->seg.seq)) {
2888                 rp->next = rp1;
2889                 tcb->reseq = rp;
2890                 if(rp->next != NULL)
2891                         tpriv->stats[OutOfOrder]++;
2892                 return 0;
2893         }
2894
2895         rqlen = 0;
2896         for(i = 0;; i++) {
2897                 rqlen += rp1->length;
2898                 if(rp1->next == NULL || seq_lt(seg->seq, rp1->next->seg.seq)) {
2899                         rp->next = rp1->next;
2900                         rp1->next = rp;
2901                         if(rp->next != NULL)
2902                                 tpriv->stats[OutOfOrder]++;
2903                         break;
2904                 }
2905                 rp1 = rp1->next;
2906         }
2907         qmax = QMAX<<tcb->rcv.scale;
2908         if(rqlen > qmax){
2909                 printd("resequence queue > window: %d > %d\n", rqlen, qmax);
2910                 i = 0;
2911                 for(rp1 = tcb->reseq; rp1 != NULL; rp1 = rp1->next){
2912                         printd("%#lux %#lux %#ux\n", rp1->seg.seq,
2913                                 rp1->seg.ack, rp1->seg.flags);
2914                         if(i++ > 10){
2915                                 printd("...\n");
2916                                 break;
2917                         }
2918                 }
2919
2920                 // delete entire reassembly queue; wait for retransmit.
2921                 // - should we be smarter and only delete the tail?
2922                 for(rp = tcb->reseq; rp != NULL; rp = rp1){
2923                         rp1 = rp->next;
2924                         freeblist(rp->bp);
2925                         kfree(rp);
2926                 }
2927                 tcb->reseq = NULL;
2928
2929                 return -1;
2930         }
2931         return 0;
2932 }
2933
2934 void
2935 getreseq(Tcpctl *tcb, Tcp *seg, struct block **bp, uint16_t *length)
2936 {
2937         Reseq *rp;
2938
2939         rp = tcb->reseq;
2940         if(rp == NULL)
2941                 return;
2942
2943         tcb->reseq = rp->next;
2944
2945         *seg = rp->seg;
2946         *bp = rp->bp;
2947         *length = rp->length;
2948
2949         kfree(rp);
2950 }
2951
2952 int
2953 tcptrim(Tcpctl *tcb, Tcp *seg, struct block **bp, uint16_t *length)
2954 {
2955         uint16_t len;
2956         uint8_t accept;
2957         int dupcnt, excess;
2958
2959         accept = 0;
2960         len = *length;
2961         if(seg->flags & SYN)
2962                 len++;
2963         if(seg->flags & FIN)
2964                 len++;
2965
2966         if(tcb->rcv.wnd == 0) {
2967                 if(len == 0 && seg->seq == tcb->rcv.nxt)
2968                         return 0;
2969         }
2970         else {
2971                 /* Some part of the segment should be in the window */
2972                 if(inwindow(tcb,seg->seq))
2973                         accept++;
2974                 else
2975                 if(len != 0) {
2976                         if(inwindow(tcb, seg->seq+len-1) ||
2977                         seq_within(tcb->rcv.nxt, seg->seq,seg->seq+len-1))
2978                                 accept++;
2979                 }
2980         }
2981         if(!accept) {
2982                 freeblist(*bp);
2983                 return -1;
2984         }
2985         dupcnt = tcb->rcv.nxt - seg->seq;
2986         if(dupcnt > 0){
2987                 tcb->rerecv += dupcnt;
2988                 if(seg->flags & SYN){
2989                         seg->flags &= ~SYN;
2990                         seg->seq++;
2991
2992                         if(seg->urg > 1)
2993                                 seg->urg--;
2994                         else
2995                                 seg->flags &= ~URG;
2996                         dupcnt--;
2997                 }
2998                 if(dupcnt > 0){
2999                         pullblock(bp, (uint16_t)dupcnt);
3000                         seg->seq += dupcnt;
3001                         *length -= dupcnt;
3002
3003                         if(seg->urg > dupcnt)
3004                                 seg->urg -= dupcnt;
3005                         else {
3006                                 seg->flags &= ~URG;
3007                                 seg->urg = 0;
3008                         }
3009                 }
3010         }
3011         excess = seg->seq + *length - (tcb->rcv.nxt + tcb->rcv.wnd);
3012         if(excess > 0) {
3013                 tcb->rerecv += excess;
3014                 *length -= excess;
3015                 *bp = trimblock(*bp, 0, *length);
3016                 if(*bp == NULL)
3017                         panic("presotto is a boofhead");
3018                 seg->flags &= ~FIN;
3019         }
3020         return 0;
3021 }
3022
3023 void
3024 tcpadvise(struct Proto *tcp, struct block *bp, char *msg)
3025 {
3026         Tcp4hdr *h4;
3027         Tcp6hdr *h6;
3028         Tcpctl *tcb;
3029         uint8_t source[IPaddrlen];
3030         uint8_t dest[IPaddrlen];
3031         uint16_t psource, pdest;
3032         struct conv *s, **p;
3033
3034         h4 = (Tcp4hdr*)(bp->rp);
3035         h6 = (Tcp6hdr*)(bp->rp);
3036
3037         if((h4->vihl&0xF0)==IP_VER4) {
3038                 v4tov6(dest, h4->tcpdst);
3039                 v4tov6(source, h4->tcpsrc);
3040                 psource = nhgets(h4->tcpsport);
3041                 pdest = nhgets(h4->tcpdport);
3042         }
3043         else {
3044                 ipmove(dest, h6->tcpdst);
3045                 ipmove(source, h6->tcpsrc);
3046                 psource = nhgets(h6->tcpsport);
3047                 pdest = nhgets(h6->tcpdport);
3048         }
3049
3050         /* Look for a connection */
3051         qlock(&tcp->qlock);
3052         for(p = tcp->conv; *p; p++) {
3053                 s = *p;
3054                 tcb = (Tcpctl*)s->ptcl;
3055                 if(s->rport == pdest)
3056                 if(s->lport == psource)
3057                 if(tcb->state != Closed)
3058                 if(ipcmp(s->raddr, dest) == 0)
3059                 if(ipcmp(s->laddr, source) == 0){
3060                         qlock(&s->qlock);
3061                         qunlock(&tcp->qlock);
3062                         switch(tcb->state){
3063                         case Syn_sent:
3064                                 localclose(s, msg);
3065                                 break;
3066                         }
3067                         qunlock(&s->qlock);
3068                         freeblist(bp);
3069                         return;
3070                 }
3071         }
3072         qunlock(&tcp->qlock);
3073         freeblist(bp);
3074 }
3075
3076 static char*
3077 tcpporthogdefensectl(char *val)
3078 {
3079         if(strcmp(val, "on") == 0)
3080                 tcpporthogdefense = 1;
3081         else if(strcmp(val, "off") == 0)
3082                 tcpporthogdefense = 0;
3083         else
3084                 return "unknown value for tcpporthogdefense";
3085         return NULL;
3086 }
3087
3088 /* called with c qlocked */
3089 char*
3090 tcpctl(struct conv* c, char** f, int n)
3091 {
3092         if(n == 1 && strcmp(f[0], "hangup") == 0)
3093                 return tcphangup(c);
3094         if(n >= 1 && strcmp(f[0], "keepalive") == 0)
3095                 return tcpstartka(c, f, n);
3096         if(n >= 1 && strcmp(f[0], "checksum") == 0)
3097                 return tcpsetchecksum(c, f, n);
3098         if(n >= 1 && strcmp(f[0], "tcpporthogdefense") == 0)
3099                 return tcpporthogdefensectl(f[1]);
3100         return "unknown control request";
3101 }
3102
3103 int
3104 tcpstats(struct Proto *tcp, char *buf, int len)
3105 {
3106         struct tcppriv *priv;
3107         char *p, *e;
3108         int i;
3109
3110         priv = tcp->priv;
3111         p = buf;
3112         e = p+len;
3113         for(i = 0; i < Nstats; i++)
3114                 p = seprintf(p, e, "%s: %lud\n", statnames[i], priv->stats[i]);
3115         return p - buf;
3116 }
3117
3118 /*
3119  *  garbage collect any stale conversations:
3120  *      - SYN received but no SYN-ACK after 5 seconds (could be the SYN attack)
3121  *      - Finwait2 after 5 minutes
3122  *
3123  *  this is called whenever we run out of channels.  Both checks are
3124  *  of questionable validity so we try to use them only when we're
3125  *  up against the wall.
3126  */
3127 int
3128 tcpgc(struct Proto *tcp)
3129 {
3130         struct conv *c, **pp, **ep;
3131         int n;
3132         Tcpctl *tcb;
3133
3134
3135         n = 0;
3136         ep = &tcp->conv[tcp->nc];
3137         for(pp = tcp->conv; pp < ep; pp++) {
3138                 c = *pp;
3139                 if(c == NULL)
3140                         break;
3141                 if(!canqlock(&c->qlock))
3142                         continue;
3143                 tcb = (Tcpctl*)c->ptcl;
3144                 switch(tcb->state){
3145                 case Syn_received:
3146                         if(NOW - tcb->time > 5000){
3147                                 localclose(c, "timed out");
3148                                 n++;
3149                         }
3150                         break;
3151                 case Finwait2:
3152                         if(NOW - tcb->time > 5*60*1000){
3153                                 localclose(c, "timed out");
3154                                 n++;
3155                         }
3156                         break;
3157                 }
3158                 qunlock(&c->qlock);
3159         }
3160         return n;
3161 }
3162
3163 void
3164 tcpsettimer(Tcpctl *tcb)
3165 {
3166         int x;
3167
3168         /* round trip dependency */
3169         x = backoff(tcb->backoff) *
3170                 (tcb->mdev + (tcb->srtt>>LOGAGAIN) + MSPTICK) / MSPTICK;
3171
3172         /* bounded twixt 1/2 and 64 seconds */
3173         if(x < 500/MSPTICK)
3174                 x = 500/MSPTICK;
3175         else if(x > (64000/MSPTICK))
3176                 x = 64000/MSPTICK;
3177         tcb->timer.start = x;
3178 }
3179
3180 void
3181 tcpinit(struct Fs *fs)
3182 {
3183         struct Proto *tcp;
3184         struct tcppriv *tpriv;
3185
3186         tcp = kzmalloc(sizeof(struct Proto), 0);
3187         tpriv = tcp->priv = kzmalloc(sizeof(struct tcppriv), 0);
3188         tcp->name = "tcp";
3189         tcp->connect = tcpconnect;
3190         tcp->announce = tcpannounce;
3191         tcp->ctl = tcpctl;
3192         tcp->state = tcpstate;
3193         tcp->create = tcpcreate;
3194         tcp->close = tcpclose;
3195         tcp->rcv = tcpiput;
3196         tcp->advise = tcpadvise;
3197         tcp->stats = tcpstats;
3198         tcp->inuse = tcpinuse;
3199         tcp->gc = tcpgc;
3200         tcp->ipproto = IP_TCPPROTO;
3201         tcp->nc = scalednconv();
3202         tcp->ptclsize = sizeof(Tcpctl);
3203         tpriv->stats[MaxConn] = tcp->nc;
3204
3205         Fsproto(fs, tcp);
3206 }
3207
3208 void
3209 tcpsetscale(struct conv *s, Tcpctl *tcb, uint16_t rcvscale, uint16_t sndscale)
3210 {
3211         if(rcvscale){
3212                 tcb->rcv.scale = rcvscale & 0xff;
3213                 tcb->snd.scale = sndscale & 0xff;
3214                 tcb->window = QMAX<<tcb->snd.scale;
3215                 qsetlimit(s->rq, tcb->window);
3216         } else {
3217                 tcb->rcv.scale = 0;
3218                 tcb->snd.scale = 0;
3219                 tcb->window = QMAX;
3220                 qsetlimit(s->rq, tcb->window);
3221         }
3222 }