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