net: Clarify transport checksum offload
[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-2017 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 <net/ip.h>
42 #include <net/tcp.h>
43
44 /* Must correspond to the enumeration in tcp.h */
45 static char *tcpstates[] = {
46         "Closed", "Listen", "Syn_sent",
47         "Established", "Finwait1", "Finwait2", "Close_wait",
48         "Closing", "Last_ack", "Time_wait"
49 };
50
51 static int tcp_irtt = DEF_RTT;                  /* Initial guess at round trip time */
52 static uint16_t tcp_mss = DEF_MSS;              /* Maximum segment size to be sent */
53
54 /* Must correspond to the enumeration in tcp.h */
55 static char *statnames[] = {
56         [MaxConn] "MaxConn",
57         [ActiveOpens] "ActiveOpens",
58         [PassiveOpens] "PassiveOpens",
59         [EstabResets] "EstabResets",
60         [CurrEstab] "CurrEstab",
61         [InSegs] "InSegs",
62         [OutSegs] "OutSegs",
63         [RetransSegs] "RetransSegs",
64         [RetransTimeouts] "RetransTimeouts",
65         [InErrs] "InErrs",
66         [OutRsts] "OutRsts",
67         [CsumErrs] "CsumErrs",
68         [HlenErrs] "HlenErrs",
69         [LenErrs] "LenErrs",
70         [OutOfOrder] "OutOfOrder",
71 };
72
73 /*
74  *  Setting tcpporthogdefense to non-zero enables Dong Lin's
75  *  solution to hijacked systems staking out port's as a form
76  *  of DoS attack.
77  *
78  *  To avoid stateless Conv hogs, we pick a sequence number at random.  If
79  *  it that number gets acked by the other end, we shut down the connection.
80  *  Look for tcpporthogedefense in the code.
81  */
82 static int tcpporthogdefense = 0;
83
84 static int addreseq(Tcpctl *, struct tcppriv *, Tcp *, struct block *,
85                     uint16_t);
86 static void getreseq(Tcpctl *, Tcp *, struct block **, uint16_t *);
87 static void localclose(struct conv *, char *unused_char_p_t);
88 static void procsyn(struct conv *, Tcp *);
89 static void tcpiput(struct Proto *, struct Ipifc *, struct block *);
90 static void tcpoutput(struct conv *);
91 static int tcptrim(Tcpctl *, Tcp *, struct block **, uint16_t *);
92 static void tcpstart(struct conv *, int);
93 static void tcptimeout(void *);
94 static void tcpsndsyn(struct conv *, Tcpctl *);
95 static void tcprcvwin(struct conv *);
96 static void tcpacktimer(void *);
97 static void tcpkeepalive(void *);
98 static void tcpsetkacounter(Tcpctl *);
99 static void tcprxmit(struct conv *);
100 static void tcpsettimer(Tcpctl *);
101 static void tcpsynackrtt(struct conv *);
102 static void tcpsetscale(struct conv *, Tcpctl *, uint16_t, uint16_t);
103 static void tcp_loss_event(struct conv *s, Tcpctl *tcb);
104 static uint16_t derive_payload_mss(Tcpctl *tcb);
105 static void set_in_flight(Tcpctl *tcb);
106
107 static void limborexmit(struct Proto *);
108 static void limbo(struct conv *, uint8_t *unused_uint8_p_t, uint8_t *, Tcp *,
109                                   int);
110
111 static void tcpsetstate(struct conv *s, uint8_t newstate)
112 {
113         Tcpctl *tcb;
114         uint8_t oldstate;
115         struct tcppriv *tpriv;
116
117         tpriv = s->p->priv;
118
119         tcb = (Tcpctl *) s->ptcl;
120
121         oldstate = tcb->state;
122         if (oldstate == newstate)
123                 return;
124
125         if (oldstate == Established)
126                 tpriv->stats[CurrEstab]--;
127         if (newstate == Established)
128                 tpriv->stats[CurrEstab]++;
129
130         /**
131         print( "%d/%d %s->%s CurrEstab=%d\n", s->lport, s->rport,
132                 tcpstates[oldstate], tcpstates[newstate], tpriv->tstats.tcpCurrEstab );
133         **/
134
135         switch (newstate) {
136                 case Closed:
137                         qclose(s->rq);
138                         qclose(s->wq);
139                         qclose(s->eq);
140                         break;
141
142                 case Close_wait:        /* Remote closes */
143                         qhangup(s->rq, NULL);
144                         break;
145         }
146
147         tcb->state = newstate;
148
149         if (oldstate == Syn_sent && newstate != Closed)
150                 Fsconnected(s, NULL);
151 }
152
153 static void tcpconnect(struct conv *c, char **argv, int argc)
154 {
155         Fsstdconnect(c, argv, argc);
156         tcpstart(c, TCP_CONNECT);
157 }
158
159 static int tcpstate(struct conv *c, char *state, int n)
160 {
161         Tcpctl *s;
162
163         s = (Tcpctl *) (c->ptcl);
164
165         return snprintf(state, n,
166                                         "%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",
167                                         tcpstates[s->state],
168                                         c->rq ? qlen(c->rq) : 0,
169                                         c->wq ? qlen(c->wq) : 0,
170                                         s->srtt, s->mdev,
171                                         s->cwind, s->snd.wnd, s->rcv.scale, s->rcv.wnd,
172                                         s->snd.scale, s->timer.start, s->timer.count, s->rerecv,
173                                         s->katimer.start, s->katimer.count);
174 }
175
176 static int tcpinuse(struct conv *c)
177 {
178         Tcpctl *s;
179
180         s = (Tcpctl *) (c->ptcl);
181         return s->state != Closed;
182 }
183
184 static void tcpannounce(struct conv *c, char **argv, int argc)
185 {
186         Fsstdannounce(c, argv, argc);
187         tcpstart(c, TCP_LISTEN);
188         Fsconnected(c, NULL);
189 }
190
191 static void tcpbypass(struct conv *cv, char **argv, int argc)
192 {
193         struct tcppriv *tpriv = cv->p->priv;
194
195         Fsstdbypass(cv, argv, argc);
196         iphtadd(&tpriv->ht, cv);
197 }
198
199 static void tcpshutdown(struct conv *c, int how)
200 {
201         Tcpctl *tcb = (Tcpctl*)c->ptcl;
202
203         /* Do nothing for the read side */
204         if (how == SHUT_RD)
205                 return;
206         /* Sends a FIN.  If we're in another state (like Listen), we'll run into
207          * issues, since we'll never send the FIN.  We'll be shutdown on our end,
208          * but we'll never tell the distant end.  Might just be an app issue. */
209         switch (tcb->state) {
210         case Established:
211                 tcb->flgcnt++;
212                 tcpsetstate(c, Finwait1);
213                 tcpoutput(c);
214                 break;
215         }
216 }
217
218 /*
219  *  tcpclose is always called with the q locked
220  */
221 static void tcpclose(struct conv *c)
222 {
223         Tcpctl *tcb;
224
225         tcb = (Tcpctl *) c->ptcl;
226
227         qhangup(c->rq, NULL);
228         qhangup(c->wq, NULL);
229         qhangup(c->eq, NULL);
230         qflush(c->rq);
231
232         switch (tcb->state) {
233                 case Listen:
234                         /*
235                          *  reset any incoming calls to this listener
236                          */
237                         Fsconnected(c, "Hangup");
238
239                         localclose(c, NULL);
240                         break;
241                 case Closed:
242                 case Syn_sent:
243                         localclose(c, NULL);
244                         break;
245                 case Established:
246                         tcb->flgcnt++;
247                         tcpsetstate(c, Finwait1);
248                         tcpoutput(c);
249                         break;
250                 case Close_wait:
251                         tcb->flgcnt++;
252                         tcpsetstate(c, Last_ack);
253                         tcpoutput(c);
254                         break;
255         }
256 }
257
258 static void tcpkick(void *x)
259 {
260         ERRSTACK(1);
261         struct conv *s = x;
262         Tcpctl *tcb;
263
264         tcb = (Tcpctl *) s->ptcl;
265
266         qlock(&s->qlock);
267         if (waserror()) {
268                 qunlock(&s->qlock);
269                 nexterror();
270         }
271
272         switch (tcb->state) {
273                 case Syn_sent:
274                 case Established:
275                 case Close_wait:
276                         /*
277                          * Push data
278                          */
279                         tcprcvwin(s);
280                         tcpoutput(s);
281                         break;
282                 default:
283                         localclose(s, "Hangup");
284                         break;
285         }
286
287         qunlock(&s->qlock);
288         poperror();
289 }
290
291 static void tcprcvwin(struct conv *s)
292 {
293         /* Call with tcb locked */
294         int w;
295         Tcpctl *tcb;
296
297         tcb = (Tcpctl *) s->ptcl;
298         w = tcb->window - qlen(s->rq);
299         if (w < 0)
300                 w = 0;
301
302         /* RFC 813: Avoid SWS.  We'll always reduce the window (because the qio
303          * increased - that's legit), and we'll always advertise the window
304          * increases (corresponding to qio drains) when those are greater than MSS.
305          * But we don't advertise increases less than MSS.
306          *
307          * Note we don't shrink the window at all - that'll result in tcptrim()
308          * dropping packets that were sent before the sender gets our update. */
309         if ((w < tcb->rcv.wnd) || (w >= tcb->mss))
310                 tcb->rcv.wnd = w;
311         /* We've delayed sending an update to rcv.wnd, and we might never get
312          * another ACK to drive the TCP stack after the qio is drained.  We could
313          * replace this stuff with qio kicks or callbacks, but that might be
314          * trickier with the MSS limitation.  (and 'edge' isn't empty or not). */
315         if (w < tcb->mss)
316                 tcb->rcv.blocked = 1;
317 }
318
319 static void tcpacktimer(void *v)
320 {
321         ERRSTACK(1);
322         Tcpctl *tcb;
323         struct conv *s;
324
325         s = v;
326         tcb = (Tcpctl *) s->ptcl;
327
328         qlock(&s->qlock);
329         if (waserror()) {
330                 qunlock(&s->qlock);
331                 nexterror();
332         }
333         if (tcb->state != Closed) {
334                 tcb->flags |= FORCE;
335                 tcprcvwin(s);
336                 tcpoutput(s);
337         }
338         qunlock(&s->qlock);
339         poperror();
340 }
341
342 static void tcpcreate(struct conv *c)
343 {
344         /* We don't use qio limits.  Instead, TCP manages flow control on its own.
345          * We only use qpassnolim().  Note for qio that 0 doesn't mean no limit. */
346         c->rq = qopen(0, Qcoalesce, 0, 0);
347         c->wq = qopen(8 * QMAX, Qkick, tcpkick, c);
348 }
349
350 static void timerstate(struct tcppriv *priv, Tcptimer *t, int newstate)
351 {
352         if (newstate != TcptimerON) {
353                 if (t->state == TcptimerON) {
354                         // unchain
355                         if (priv->timers == t) {
356                                 priv->timers = t->next;
357                                 if (t->prev != NULL)
358                                         panic("timerstate1");
359                         }
360                         if (t->next)
361                                 t->next->prev = t->prev;
362                         if (t->prev)
363                                 t->prev->next = t->next;
364                         t->next = t->prev = NULL;
365                 }
366         } else {
367                 if (t->state != TcptimerON) {
368                         // chain
369                         if (t->prev != NULL || t->next != NULL)
370                                 panic("timerstate2");
371                         t->prev = NULL;
372                         t->next = priv->timers;
373                         if (t->next)
374                                 t->next->prev = t;
375                         priv->timers = t;
376                 }
377         }
378         t->state = newstate;
379 }
380
381 static void tcpackproc(void *a)
382 {
383         ERRSTACK(1);
384         Tcptimer *t, *tp, *timeo;
385         struct Proto *tcp;
386         struct tcppriv *priv;
387         int loop;
388
389         tcp = a;
390         priv = tcp->priv;
391
392         for (;;) {
393                 kthread_usleep(MSPTICK * 1000);
394
395                 qlock(&priv->tl);
396                 timeo = NULL;
397                 loop = 0;
398                 for (t = priv->timers; t != NULL; t = tp) {
399                         if (loop++ > 10000)
400                                 panic("tcpackproc1");
401                         tp = t->next;
402                         if (t->state == TcptimerON) {
403                                 t->count--;
404                                 if (t->count == 0) {
405                                         timerstate(priv, t, TcptimerDONE);
406                                         t->readynext = timeo;
407                                         timeo = t;
408                                 }
409                         }
410                 }
411                 qunlock(&priv->tl);
412
413                 loop = 0;
414                 for (t = timeo; t != NULL; t = t->readynext) {
415                         if (loop++ > 10000)
416                                 panic("tcpackproc2");
417                         if (t->state == TcptimerDONE && t->func != NULL) {
418                                 /* discard error style */
419                                 if (!waserror())
420                                         (*t->func) (t->arg);
421                                 poperror();
422                         }
423                 }
424
425                 limborexmit(tcp);
426         }
427 }
428
429 static void tcpgo(struct tcppriv *priv, Tcptimer *t)
430 {
431         if (t == NULL || t->start == 0)
432                 return;
433
434         qlock(&priv->tl);
435         t->count = t->start;
436         timerstate(priv, t, TcptimerON);
437         qunlock(&priv->tl);
438 }
439
440 static void tcphalt(struct tcppriv *priv, Tcptimer *t)
441 {
442         if (t == NULL)
443                 return;
444
445         qlock(&priv->tl);
446         timerstate(priv, t, TcptimerOFF);
447         qunlock(&priv->tl);
448 }
449
450 static int backoff(int n)
451 {
452         return 1 << n;
453 }
454
455 static void localclose(struct conv *s, char *reason)
456 {
457         /* called with tcb locked */
458         Tcpctl *tcb;
459         Reseq *rp, *rp1;
460         struct tcppriv *tpriv;
461
462         tpriv = s->p->priv;
463         tcb = (Tcpctl *) s->ptcl;
464
465         iphtrem(&tpriv->ht, s);
466
467         tcphalt(tpriv, &tcb->timer);
468         tcphalt(tpriv, &tcb->rtt_timer);
469         tcphalt(tpriv, &tcb->acktimer);
470         tcphalt(tpriv, &tcb->katimer);
471
472         /* Flush reassembly queue; nothing more can arrive */
473         for (rp = tcb->reseq; rp != NULL; rp = rp1) {
474                 rp1 = rp->next;
475                 freeblist(rp->bp);
476                 kfree(rp);
477         }
478         tcb->reseq = NULL;
479
480         if (tcb->state == Syn_sent)
481                 Fsconnected(s, reason);
482
483         qhangup(s->rq, reason);
484         qhangup(s->wq, reason);
485
486         tcpsetstate(s, Closed);
487
488         /* listener will check the rq state */
489         if (s->state == Announced)
490                 rendez_wakeup(&s->listenr);
491 }
492
493 /* mtu (- TCP + IP hdr len) of 1st hop */
494 static int tcpmtu(struct Ipifc *ifc, int version, int *scale)
495 {
496         int mtu;
497
498         switch (version) {
499                 default:
500                 case V4:
501                         mtu = DEF_MSS;
502                         if (ifc != NULL)
503                                 mtu = ifc->maxtu - ifc->m->hsize - (TCP4_PKT + TCP4_HDRSIZE);
504                         break;
505                 case V6:
506                         mtu = DEF_MSS6;
507                         if (ifc != NULL)
508                                 mtu = ifc->maxtu - ifc->m->hsize - (TCP6_PKT + TCP6_HDRSIZE);
509                         break;
510         }
511         *scale = HaveWS | 7;
512
513         return mtu;
514 }
515
516 static void tcb_check_tso(Tcpctl *tcb)
517 {
518         /* This can happen if the netdev isn't up yet. */
519         if (!tcb->ifc)
520                 return;
521         if (tcb->ifc->feat & NETF_TSO)
522                 tcb->flags |= TSO;
523         else
524                 tcb->flags &= ~TSO;
525 }
526
527 static void inittcpctl(struct conv *s, int mode)
528 {
529         Tcpctl *tcb;
530         Tcp4hdr *h4;
531         Tcp6hdr *h6;
532         int mss;
533
534         tcb = (Tcpctl *) s->ptcl;
535
536         memset(tcb, 0, sizeof(Tcpctl));
537
538         tcb->ssthresh = UINT32_MAX;
539         tcb->srtt = tcp_irtt;
540         tcb->mdev = 0;
541
542         /* setup timers */
543         tcb->timer.start = tcp_irtt / MSPTICK;
544         tcb->timer.func = tcptimeout;
545         tcb->timer.arg = s;
546         tcb->rtt_timer.start = MAX_TIME;
547         tcb->acktimer.start = TCP_ACK / MSPTICK;
548         tcb->acktimer.func = tcpacktimer;
549         tcb->acktimer.arg = s;
550         tcb->katimer.start = DEF_KAT / MSPTICK;
551         tcb->katimer.func = tcpkeepalive;
552         tcb->katimer.arg = s;
553
554         mss = DEF_MSS;
555
556         /* create a prototype(pseudo) header */
557         if (mode != TCP_LISTEN) {
558                 if (ipcmp(s->laddr, IPnoaddr) == 0)
559                         findlocalip(s->p->f, s->laddr, s->raddr);
560
561                 switch (s->ipversion) {
562                         case V4:
563                                 h4 = &tcb->protohdr.tcp4hdr;
564                                 memset(h4, 0, sizeof(*h4));
565                                 h4->proto = IP_TCPPROTO;
566                                 hnputs(h4->tcpsport, s->lport);
567                                 hnputs(h4->tcpdport, s->rport);
568                                 v6tov4(h4->tcpsrc, s->laddr);
569                                 v6tov4(h4->tcpdst, s->raddr);
570                                 break;
571                         case V6:
572                                 h6 = &tcb->protohdr.tcp6hdr;
573                                 memset(h6, 0, sizeof(*h6));
574                                 h6->proto = IP_TCPPROTO;
575                                 hnputs(h6->tcpsport, s->lport);
576                                 hnputs(h6->tcpdport, s->rport);
577                                 ipmove(h6->tcpsrc, s->laddr);
578                                 ipmove(h6->tcpdst, s->raddr);
579                                 mss = DEF_MSS6;
580                                 break;
581                         default:
582                                 panic("inittcpctl: version %d", s->ipversion);
583                 }
584         }
585
586         tcb->ifc = findipifc(s->p->f, s->laddr, 0);
587         tcb->mss = mss;
588         tcb->typical_mss = mss;
589         tcb->cwind = tcb->typical_mss * CWIND_SCALE;
590
591         /* default is no window scaling */
592         tcb->window = QMAX;
593         tcb->rcv.wnd = QMAX;
594         tcb->rcv.scale = 0;
595         tcb->snd.scale = 0;
596         tcb_check_tso(tcb);
597 }
598
599 /*
600  *  called with s qlocked
601  */
602 static void tcpstart(struct conv *s, int mode)
603 {
604         Tcpctl *tcb;
605         struct tcppriv *tpriv;
606         char *kpname;
607
608         tpriv = s->p->priv;
609
610         if (tpriv->ackprocstarted == 0) {
611                 qlock(&tpriv->apl);
612                 if (tpriv->ackprocstarted == 0) {
613                         /* tcpackproc needs to free this if it ever exits */
614                         kpname = kmalloc(KNAMELEN, MEM_WAIT);
615                         snprintf(kpname, KNAMELEN, "#I%dtcpack", s->p->f->dev);
616                         ktask(kpname, tcpackproc, s->p);
617                         tpriv->ackprocstarted = 1;
618                 }
619                 qunlock(&tpriv->apl);
620         }
621
622         tcb = (Tcpctl *) s->ptcl;
623
624         inittcpctl(s, mode);
625
626         iphtadd(&tpriv->ht, s);
627         switch (mode) {
628                 case TCP_LISTEN:
629                         tpriv->stats[PassiveOpens]++;
630                         tcb->flags |= CLONE;
631                         tcpsetstate(s, Listen);
632                         break;
633
634                 case TCP_CONNECT:
635                         tpriv->stats[ActiveOpens]++;
636                         tcb->flags |= ACTIVE;
637                         tcpsndsyn(s, tcb);
638                         tcpsetstate(s, Syn_sent);
639                         tcpoutput(s);
640                         break;
641         }
642 }
643
644 static char *tcpflag(uint16_t flag)
645 {
646         static char buf[128];
647
648         snprintf(buf, sizeof(buf), "%d", flag >> 10);   /* Head len */
649         if (flag & URG)
650                 snprintf(buf, sizeof(buf), "%s%s", buf, " URG");
651         if (flag & ACK)
652                 snprintf(buf, sizeof(buf), "%s%s", buf, " ACK");
653         if (flag & PSH)
654                 snprintf(buf, sizeof(buf), "%s%s", buf, " PSH");
655         if (flag & RST)
656                 snprintf(buf, sizeof(buf), "%s%s", buf, " RST");
657         if (flag & SYN)
658                 snprintf(buf, sizeof(buf), "%s%s", buf, " SYN");
659         if (flag & FIN)
660                 snprintf(buf, sizeof(buf), "%s%s", buf, " FIN");
661
662         return buf;
663 }
664
665 /* Helper, determine if we should send a TCP timestamp.  ts_val was the
666  * timestamp from our distant end.  We'll also send a TS on SYN (no ACK). */
667 static bool tcp_seg_has_ts(Tcp *tcph)
668 {
669         return tcph->ts_val || ((tcph->flags & SYN) && !(tcph->flags & ACK));
670 }
671
672 /* Given a TCP header/segment and default header size (e.g. TCP4_HDRSIZE),
673  * return the actual hdr_len and opt_pad */
674 static void compute_hdrlen_optpad(Tcp *tcph, uint16_t default_hdrlen,
675                                   uint16_t *ret_hdrlen, uint16_t *ret_optpad,
676                                   Tcpctl *tcb)
677 {
678         uint16_t hdrlen = default_hdrlen;
679         uint16_t optpad = 0;
680
681         if (tcph->flags & SYN) {
682                 if (tcph->mss)
683                         hdrlen += MSS_LENGTH;
684                 if (tcph->ws)
685                         hdrlen += WS_LENGTH;
686                 if (tcph->sack_ok)
687                         hdrlen += SACK_OK_LENGTH;
688         }
689         if (tcp_seg_has_ts(tcph)) {
690                 hdrlen += TS_LENGTH;
691                 /* SYNs have other opts, don't do the PREPAD NOOP optimization. */
692                 if (!(tcph->flags & SYN))
693                         hdrlen += TS_SEND_PREPAD;
694         }
695         if (tcb && tcb->rcv.nr_sacks)
696                 hdrlen += 2 + tcb->rcv.nr_sacks * 8;
697         optpad = hdrlen & 3;
698         if (optpad)
699                 optpad = 4 - optpad;
700         hdrlen += optpad;
701         *ret_hdrlen = hdrlen;
702         *ret_optpad = optpad;
703 }
704
705 /* Writes the TCP options for tcph to opt. */
706 static void write_opts(Tcp *tcph, uint8_t *opt, uint16_t optpad, Tcpctl *tcb)
707 {
708         if (tcph->flags & SYN) {
709                 if (tcph->mss != 0) {
710                         *opt++ = MSSOPT;
711                         *opt++ = MSS_LENGTH;
712                         hnputs(opt, tcph->mss);
713                         opt += 2;
714                 }
715                 if (tcph->ws != 0) {
716                         *opt++ = WSOPT;
717                         *opt++ = WS_LENGTH;
718                         *opt++ = tcph->ws;
719                 }
720                 if (tcph->sack_ok) {
721                         *opt++ = SACK_OK_OPT;
722                         *opt++ = SACK_OK_LENGTH;
723                 }
724         }
725         if (tcp_seg_has_ts(tcph)) {
726                 if (!(tcph->flags & SYN)) {
727                         *opt++ = NOOPOPT;
728                         *opt++ = NOOPOPT;
729                 }
730                 *opt++ = TS_OPT;
731                 *opt++ = TS_LENGTH;
732                 /* Setting TSval, our time */
733                 hnputl(opt, milliseconds());
734                 opt += 4;
735                 /* Setting TSecr, the time we last saw from them, stored in ts_val */
736                 hnputl(opt, tcph->ts_val);
737                 opt += 4;
738         }
739         if (tcb && tcb->rcv.nr_sacks) {
740                 *opt++ = SACK_OPT;
741                 *opt++ = 2 + tcb->rcv.nr_sacks * 8;
742                 for (int i = 0; i < tcb->rcv.nr_sacks; i++) {
743                         hnputl(opt, tcb->rcv.sacks[i].left);
744                         opt += 4;
745                         hnputl(opt, tcb->rcv.sacks[i].right);
746                         opt += 4;
747                 }
748         }
749         while (optpad-- > 0)
750                 *opt++ = NOOPOPT;
751 }
752
753 /* Given a data block (or NULL) returns a block with enough header room that we
754  * can send out.  block->wp is set to the beginning of the payload.  Returns
755  * NULL on some sort of error. */
756 static struct block *alloc_or_pad_block(struct block *data,
757                                         uint16_t total_hdr_size)
758 {
759         if (data) {
760                 data = padblock(data, total_hdr_size);
761                 if (data == NULL)
762                         return NULL;
763         } else {
764                 /* the 64 pad is to meet mintu's */
765                 data = block_alloc(total_hdr_size + 64, MEM_WAIT);
766                 if (data == NULL)
767                         return NULL;
768                 data->wp += total_hdr_size;
769         }
770         return data;
771 }
772
773 static struct block *htontcp6(Tcp *tcph, struct block *data, Tcp6hdr *ph,
774                               Tcpctl *tcb)
775 {
776         int dlen = blocklen(data);
777         Tcp6hdr *h;
778         uint16_t csum;
779         uint16_t hdrlen, optpad;
780
781         compute_hdrlen_optpad(tcph, TCP6_HDRSIZE, &hdrlen, &optpad, tcb);
782
783         data = alloc_or_pad_block(data, hdrlen + TCP6_PKT);
784         if (data == NULL)
785                 return NULL;
786         /* relative to the block start (bp->rp).  Note TCP structs include IP. */
787         data->network_offset = 0;
788         data->transport_offset = offsetof(Tcp6hdr, tcpsport);
789
790         /* copy in pseudo ip header plus port numbers */
791         h = (Tcp6hdr *) (data->rp);
792         memmove(h, ph, TCP6_TCBPHDRSZ);
793
794         /* compose pseudo tcp header, do cksum calculation */
795         hnputl(h->vcf, hdrlen + dlen);
796         h->ploadlen[0] = h->ploadlen[1] = h->proto = 0;
797         h->ttl = ph->proto;
798
799         /* copy in variable bits */
800         hnputl(h->tcpseq, tcph->seq);
801         hnputl(h->tcpack, tcph->ack);
802         hnputs(h->tcpflag, (hdrlen << 10) | tcph->flags);
803         hnputs(h->tcpwin, tcph->wnd >> (tcb != NULL ? tcb->snd.scale : 0));
804         hnputs(h->tcpurg, tcph->urg);
805
806         write_opts(tcph, h->tcpopt, optpad, tcb);
807
808         if (tcb != NULL && tcb->nochecksum) {
809                 h->tcpcksum[0] = h->tcpcksum[1] = 0;
810         } else {
811                 csum = ptclcsum(data, TCP6_IPLEN, hdrlen + dlen + TCP6_PHDRSIZE);
812                 hnputs(h->tcpcksum, csum);
813         }
814
815         /* move from pseudo header back to normal ip header */
816         memset(h->vcf, 0, 4);
817         h->vcf[0] = IP_VER6;
818         hnputs(h->ploadlen, hdrlen + dlen);
819         h->proto = ph->proto;
820
821         return data;
822 }
823
824 static struct block *htontcp4(Tcp *tcph, struct block *data, Tcp4hdr *ph,
825                               Tcpctl *tcb)
826 {
827         int dlen = blocklen(data);
828         Tcp4hdr *h;
829         uint16_t csum;
830         uint16_t hdrlen, optpad;
831
832         compute_hdrlen_optpad(tcph, TCP4_HDRSIZE, &hdrlen, &optpad, tcb);
833
834         data = alloc_or_pad_block(data, hdrlen + TCP4_PKT);
835         if (data == NULL)
836                 return NULL;
837         /* relative to the block start (bp->rp).  Note TCP structs include IP. */
838         data->network_offset = 0;
839         data->transport_offset = offsetof(Tcp4hdr, tcpsport);
840
841         /* copy in pseudo ip header plus port numbers */
842         h = (Tcp4hdr *) (data->rp);
843         memmove(h, ph, TCP4_TCBPHDRSZ);
844
845         /* copy in variable bits */
846         hnputs(h->tcplen, hdrlen + dlen);
847         hnputl(h->tcpseq, tcph->seq);
848         hnputl(h->tcpack, tcph->ack);
849         hnputs(h->tcpflag, (hdrlen << 10) | tcph->flags);
850         hnputs(h->tcpwin, tcph->wnd >> (tcb != NULL ? tcb->snd.scale : 0));
851         hnputs(h->tcpurg, tcph->urg);
852
853         write_opts(tcph, h->tcpopt, optpad, tcb);
854
855         if (tcb != NULL && tcb->nochecksum) {
856                 h->tcpcksum[0] = h->tcpcksum[1] = 0;
857         } else {
858                 assert(data->transport_offset == TCP4_IPLEN + TCP4_PHDRSIZE);
859                 csum = ~ptclcsum(data, TCP4_IPLEN, TCP4_PHDRSIZE);
860                 hnputs(h->tcpcksum, csum);
861                 data->tx_csum_offset = ph->tcpcksum - ph->tcpsport;
862                 data->flag |= Btcpck;
863         }
864
865         return data;
866 }
867
868 static void parse_inbound_sacks(Tcp *tcph, uint8_t *opt, uint16_t optlen)
869 {
870         uint8_t nr_sacks;
871         uint32_t left, right;
872
873         nr_sacks = (optlen - 2) / 8;
874         if (nr_sacks > MAX_NR_SACKS_PER_PACKET)
875                 return;
876         opt += 2;
877         for (int i = 0; i < nr_sacks; i++, opt += 8) {
878                 left = nhgetl(opt);
879                 right = nhgetl(opt + 4);
880                 if (seq_ge(left, right)) {
881                         /* bad / malicious SACK.  Skip it, and adjust. */
882                         nr_sacks--;
883                         i--;    /* stay on this array element next loop */
884                         continue;
885                 }
886                 tcph->sacks[i].left = left;
887                 tcph->sacks[i].right = right;
888         }
889         tcph->nr_sacks = nr_sacks;
890 }
891
892 static void parse_inbound_opts(Tcp *tcph, uint8_t *opt, uint16_t optsize)
893 {
894         uint16_t optlen;
895
896         while (optsize > 0 && *opt != EOLOPT) {
897                 if (*opt == NOOPOPT) {
898                         optsize--;
899                         opt++;
900                         continue;
901                 }
902                 optlen = opt[1];
903                 if (optlen < 2 || optlen > optsize)
904                         break;
905                 switch (*opt) {
906                         case MSSOPT:
907                                 if (optlen == MSS_LENGTH)
908                                         tcph->mss = nhgets(opt + 2);
909                                 break;
910                         case WSOPT:
911                                 if (optlen == WS_LENGTH && *(opt + 2) <= MAX_WS_VALUE)
912                                         tcph->ws = HaveWS | *(opt + 2);
913                                 break;
914                         case SACK_OK_OPT:
915                                 if (optlen == SACK_OK_LENGTH)
916                                         tcph->sack_ok = TRUE;
917                                 break;
918                         case SACK_OPT:
919                                 parse_inbound_sacks(tcph, opt, optlen);
920                                 break;
921                         case TS_OPT:
922                                 if (optlen == TS_LENGTH) {
923                                         tcph->ts_val = nhgetl(opt + 2);
924                                         tcph->ts_ecr = nhgetl(opt + 6);
925                                 }
926                                 break;
927                 }
928                 optsize -= optlen;
929                 opt += optlen;
930         }
931 }
932
933 /* Helper, clears the opts.  We'll later set them with e.g. parse_inbound_opts,
934  * set them manually, or something else. */
935 static void clear_tcph_opts(Tcp *tcph)
936 {
937         tcph->mss = 0;
938         tcph->ws = 0;
939         tcph->sack_ok = FALSE;
940         tcph->nr_sacks = 0;
941         tcph->ts_val = 0;
942         tcph->ts_ecr = 0;
943 }
944
945 static int ntohtcp6(Tcp *tcph, struct block **bpp)
946 {
947         Tcp6hdr *h;
948         uint16_t hdrlen;
949
950         *bpp = pullupblock(*bpp, TCP6_PKT + TCP6_HDRSIZE);
951         if (*bpp == NULL)
952                 return -1;
953
954         h = (Tcp6hdr *) ((*bpp)->rp);
955         tcph->source = nhgets(h->tcpsport);
956         tcph->dest = nhgets(h->tcpdport);
957         tcph->seq = nhgetl(h->tcpseq);
958         tcph->ack = nhgetl(h->tcpack);
959         hdrlen = (h->tcpflag[0] >> 2) & ~3;
960         if (hdrlen < TCP6_HDRSIZE) {
961                 freeblist(*bpp);
962                 return -1;
963         }
964
965         tcph->flags = h->tcpflag[1];
966         tcph->wnd = nhgets(h->tcpwin);
967         tcph->urg = nhgets(h->tcpurg);
968         clear_tcph_opts(tcph);
969         tcph->len = nhgets(h->ploadlen) - hdrlen;
970
971         *bpp = pullupblock(*bpp, hdrlen + TCP6_PKT);
972         if (*bpp == NULL)
973                 return -1;
974         parse_inbound_opts(tcph, h->tcpopt, hdrlen - TCP6_HDRSIZE);
975         return hdrlen;
976 }
977
978 static int ntohtcp4(Tcp *tcph, struct block **bpp)
979 {
980         Tcp4hdr *h;
981         uint16_t hdrlen;
982
983         *bpp = pullupblock(*bpp, TCP4_PKT + TCP4_HDRSIZE);
984         if (*bpp == NULL)
985                 return -1;
986
987         h = (Tcp4hdr *) ((*bpp)->rp);
988         tcph->source = nhgets(h->tcpsport);
989         tcph->dest = nhgets(h->tcpdport);
990         tcph->seq = nhgetl(h->tcpseq);
991         tcph->ack = nhgetl(h->tcpack);
992
993         hdrlen = (h->tcpflag[0] >> 2) & ~3;
994         if (hdrlen < TCP4_HDRSIZE) {
995                 freeblist(*bpp);
996                 return -1;
997         }
998
999         tcph->flags = h->tcpflag[1];
1000         tcph->wnd = nhgets(h->tcpwin);
1001         tcph->urg = nhgets(h->tcpurg);
1002         clear_tcph_opts(tcph);
1003         tcph->len = nhgets(h->length) - (hdrlen + TCP4_PKT);
1004
1005         *bpp = pullupblock(*bpp, hdrlen + TCP4_PKT);
1006         if (*bpp == NULL)
1007                 return -1;
1008         parse_inbound_opts(tcph, h->tcpopt, hdrlen - TCP4_HDRSIZE);
1009         return hdrlen;
1010 }
1011
1012 /*
1013  *  For outgoing calls, generate an initial sequence
1014  *  number and put a SYN on the send queue
1015  */
1016 static void tcpsndsyn(struct conv *s, Tcpctl *tcb)
1017 {
1018         urandom_read(&tcb->iss, sizeof(tcb->iss));
1019         tcb->rttseq = tcb->iss;
1020         tcb->snd.wl2 = tcb->iss;
1021         tcb->snd.una = tcb->iss;
1022         tcb->snd.rtx = tcb->rttseq;
1023         tcb->snd.nxt = tcb->rttseq;
1024         tcb->flgcnt++;
1025         tcb->flags |= FORCE;
1026         tcb->sndsyntime = NOW;
1027
1028         /* set desired mss and scale */
1029         tcb->mss = tcpmtu(tcb->ifc, s->ipversion, &tcb->scale);
1030 }
1031
1032 static void sndrst(struct Proto *tcp, uint8_t *source, uint8_t *dest,
1033                    uint16_t length, Tcp *seg, uint8_t version, char *reason)
1034 {
1035         struct block *hbp;
1036         uint8_t rflags;
1037         struct tcppriv *tpriv;
1038         Tcp4hdr ph4;
1039         Tcp6hdr ph6;
1040
1041         netlog(tcp->f, Logtcpreset, "sndrst: %s\n", reason);
1042
1043         tpriv = tcp->priv;
1044
1045         if (seg->flags & RST)
1046                 return;
1047
1048         /* make pseudo header */
1049         switch (version) {
1050                 case V4:
1051                         memset(&ph4, 0, sizeof(ph4));
1052                         ph4.vihl = IP_VER4;
1053                         v6tov4(ph4.tcpsrc, dest);
1054                         v6tov4(ph4.tcpdst, source);
1055                         ph4.proto = IP_TCPPROTO;
1056                         hnputs(ph4.tcplen, TCP4_HDRSIZE);
1057                         hnputs(ph4.tcpsport, seg->dest);
1058                         hnputs(ph4.tcpdport, seg->source);
1059                         break;
1060                 case V6:
1061                         memset(&ph6, 0, sizeof(ph6));
1062                         ph6.vcf[0] = IP_VER6;
1063                         ipmove(ph6.tcpsrc, dest);
1064                         ipmove(ph6.tcpdst, source);
1065                         ph6.proto = IP_TCPPROTO;
1066                         hnputs(ph6.ploadlen, TCP6_HDRSIZE);
1067                         hnputs(ph6.tcpsport, seg->dest);
1068                         hnputs(ph6.tcpdport, seg->source);
1069                         break;
1070                 default:
1071                         panic("sndrst: version %d", version);
1072         }
1073
1074         tpriv->stats[OutRsts]++;
1075         rflags = RST;
1076
1077         /* convince the other end that this reset is in band */
1078         if (seg->flags & ACK) {
1079                 seg->seq = seg->ack;
1080                 seg->ack = 0;
1081         } else {
1082                 rflags |= ACK;
1083                 seg->ack = seg->seq;
1084                 seg->seq = 0;
1085                 if (seg->flags & SYN)
1086                         seg->ack++;
1087                 seg->ack += length;
1088                 if (seg->flags & FIN)
1089                         seg->ack++;
1090         }
1091         seg->flags = rflags;
1092         seg->wnd = 0;
1093         seg->urg = 0;
1094         seg->mss = 0;
1095         seg->ws = 0;
1096         seg->sack_ok = FALSE;
1097         seg->nr_sacks = 0;
1098         /* seg->ts_val is already set with their timestamp */
1099         switch (version) {
1100                 case V4:
1101                         hbp = htontcp4(seg, NULL, &ph4, NULL);
1102                         if (hbp == NULL)
1103                                 return;
1104                         ipoput4(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1105                         break;
1106                 case V6:
1107                         hbp = htontcp6(seg, NULL, &ph6, NULL);
1108                         if (hbp == NULL)
1109                                 return;
1110                         ipoput6(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1111                         break;
1112                 default:
1113                         panic("sndrst2: version %d", version);
1114         }
1115 }
1116
1117 /*
1118  *  send a reset to the remote side and close the conversation
1119  *  called with s qlocked
1120  */
1121 static void tcphangup(struct conv *s)
1122 {
1123         ERRSTACK(1);
1124         Tcp seg;
1125         Tcpctl *tcb;
1126         struct block *hbp;
1127
1128         tcb = (Tcpctl *) s->ptcl;
1129         if (ipcmp(s->raddr, IPnoaddr)) {
1130                 /* discard error style, poperror regardless */
1131                 if (!waserror()) {
1132                         seg.flags = RST | ACK;
1133                         seg.ack = tcb->rcv.nxt;
1134                         tcb->last_ack_sent = seg.ack;
1135                         tcb->rcv.una = 0;
1136                         seg.seq = tcb->snd.nxt;
1137                         seg.wnd = 0;
1138                         seg.urg = 0;
1139                         seg.mss = 0;
1140                         seg.ws = 0;
1141                         seg.sack_ok = FALSE;
1142                         seg.nr_sacks = 0;
1143                         seg.ts_val = tcb->ts_recent;
1144                         switch (s->ipversion) {
1145                                 case V4:
1146                                         tcb->protohdr.tcp4hdr.vihl = IP_VER4;
1147                                         hbp = htontcp4(&seg, NULL, &tcb->protohdr.tcp4hdr, tcb);
1148                                         ipoput4(s->p->f, hbp, 0, s->ttl, s->tos, s);
1149                                         break;
1150                                 case V6:
1151                                         tcb->protohdr.tcp6hdr.vcf[0] = IP_VER6;
1152                                         hbp = htontcp6(&seg, NULL, &tcb->protohdr.tcp6hdr, tcb);
1153                                         ipoput6(s->p->f, hbp, 0, s->ttl, s->tos, s);
1154                                         break;
1155                                 default:
1156                                         panic("tcphangup: version %d", s->ipversion);
1157                         }
1158                 }
1159                 poperror();
1160         }
1161         localclose(s, NULL);
1162 }
1163
1164 /*
1165  *  (re)send a SYN ACK
1166  */
1167 static int sndsynack(struct Proto *tcp, Limbo *lp)
1168 {
1169         struct block *hbp;
1170         Tcp4hdr ph4;
1171         Tcp6hdr ph6;
1172         Tcp seg;
1173         int scale;
1174         uint8_t flag = 0;
1175
1176         /* make pseudo header */
1177         switch (lp->version) {
1178                 case V4:
1179                         memset(&ph4, 0, sizeof(ph4));
1180                         ph4.vihl = IP_VER4;
1181                         v6tov4(ph4.tcpsrc, lp->laddr);
1182                         v6tov4(ph4.tcpdst, lp->raddr);
1183                         ph4.proto = IP_TCPPROTO;
1184                         hnputs(ph4.tcplen, TCP4_HDRSIZE);
1185                         hnputs(ph4.tcpsport, lp->lport);
1186                         hnputs(ph4.tcpdport, lp->rport);
1187                         break;
1188                 case V6:
1189                         memset(&ph6, 0, sizeof(ph6));
1190                         ph6.vcf[0] = IP_VER6;
1191                         ipmove(ph6.tcpsrc, lp->laddr);
1192                         ipmove(ph6.tcpdst, lp->raddr);
1193                         ph6.proto = IP_TCPPROTO;
1194                         hnputs(ph6.ploadlen, TCP6_HDRSIZE);
1195                         hnputs(ph6.tcpsport, lp->lport);
1196                         hnputs(ph6.tcpdport, lp->rport);
1197                         break;
1198                 default:
1199                         panic("sndrst: version %d", lp->version);
1200         }
1201         lp->ifc = findipifc(tcp->f, lp->laddr, 0);
1202
1203         seg.seq = lp->iss;
1204         seg.ack = lp->irs + 1;
1205         seg.flags = SYN | ACK;
1206         seg.urg = 0;
1207         seg.mss = tcpmtu(lp->ifc, lp->version, &scale);
1208         seg.wnd = QMAX;
1209         seg.ts_val = lp->ts_val;
1210         seg.nr_sacks = 0;
1211
1212         /* if the other side set scale, we should too */
1213         if (lp->rcvscale) {
1214                 seg.ws = scale;
1215                 lp->sndscale = scale;
1216         } else {
1217                 seg.ws = 0;
1218                 lp->sndscale = 0;
1219         }
1220         if (SACK_SUPPORTED)
1221                 seg.sack_ok = lp->sack_ok;
1222         else
1223                 seg.sack_ok = FALSE;
1224
1225         switch (lp->version) {
1226                 case V4:
1227                         hbp = htontcp4(&seg, NULL, &ph4, NULL);
1228                         if (hbp == NULL)
1229                                 return -1;
1230                         ipoput4(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1231                         break;
1232                 case V6:
1233                         hbp = htontcp6(&seg, NULL, &ph6, NULL);
1234                         if (hbp == NULL)
1235                                 return -1;
1236                         ipoput6(tcp->f, hbp, 0, MAXTTL, DFLTTOS, NULL);
1237                         break;
1238                 default:
1239                         panic("sndsnack: version %d", lp->version);
1240         }
1241         lp->lastsend = NOW;
1242         return 0;
1243 }
1244
1245 #define hashipa(a, p) ( ( (a)[IPaddrlen-2] + (a)[IPaddrlen-1] + p )&LHTMASK )
1246
1247 /*
1248  *  put a call into limbo and respond with a SYN ACK
1249  *
1250  *  called with proto locked
1251  */
1252 static void limbo(struct conv *s, uint8_t *source, uint8_t *dest, Tcp *seg,
1253                   int version)
1254 {
1255         Limbo *lp, **l;
1256         struct tcppriv *tpriv;
1257         int h;
1258
1259         tpriv = s->p->priv;
1260         h = hashipa(source, seg->source);
1261
1262         for (l = &tpriv->lht[h]; *l != NULL; l = &lp->next) {
1263                 lp = *l;
1264                 if (lp->lport != seg->dest || lp->rport != seg->source
1265                         || lp->version != version)
1266                         continue;
1267                 if (ipcmp(lp->raddr, source) != 0)
1268                         continue;
1269                 if (ipcmp(lp->laddr, dest) != 0)
1270                         continue;
1271
1272                 /* each new SYN restarts the retransmits */
1273                 lp->irs = seg->seq;
1274                 break;
1275         }
1276         lp = *l;
1277         if (lp == NULL) {
1278                 if (tpriv->nlimbo >= Maxlimbo && tpriv->lht[h]) {
1279                         lp = tpriv->lht[h];
1280                         tpriv->lht[h] = lp->next;
1281                         lp->next = NULL;
1282                 } else {
1283                         lp = kzmalloc(sizeof(*lp), 0);
1284                         if (lp == NULL)
1285                                 return;
1286                         tpriv->nlimbo++;
1287                 }
1288                 *l = lp;
1289                 lp->version = version;
1290                 ipmove(lp->laddr, dest);
1291                 ipmove(lp->raddr, source);
1292                 lp->lport = seg->dest;
1293                 lp->rport = seg->source;
1294                 lp->mss = seg->mss;
1295                 lp->rcvscale = seg->ws;
1296                 lp->sack_ok = seg->sack_ok;
1297                 lp->irs = seg->seq;
1298                 lp->ts_val = seg->ts_val;
1299                 urandom_read(&lp->iss, sizeof(lp->iss));
1300         }
1301
1302         if (sndsynack(s->p, lp) < 0) {
1303                 *l = lp->next;
1304                 tpriv->nlimbo--;
1305                 kfree(lp);
1306         }
1307 }
1308
1309 /*
1310  *  resend SYN ACK's once every SYNACK_RXTIMER ms.
1311  */
1312 static void limborexmit(struct Proto *tcp)
1313 {
1314         struct tcppriv *tpriv;
1315         Limbo **l, *lp;
1316         int h;
1317         int seen;
1318         uint64_t now;
1319
1320         tpriv = tcp->priv;
1321
1322         if (!canqlock(&tcp->qlock))
1323                 return;
1324         seen = 0;
1325         now = NOW;
1326         for (h = 0; h < NLHT && seen < tpriv->nlimbo; h++) {
1327                 for (l = &tpriv->lht[h]; *l != NULL && seen < tpriv->nlimbo;) {
1328                         lp = *l;
1329                         seen++;
1330                         if (now - lp->lastsend < (lp->rexmits + 1) * SYNACK_RXTIMER)
1331                                 continue;
1332
1333                         /* time it out after 1 second */
1334                         if (++(lp->rexmits) > 5) {
1335                                 tpriv->nlimbo--;
1336                                 *l = lp->next;
1337                                 kfree(lp);
1338                                 continue;
1339                         }
1340
1341                         /* if we're being attacked, don't bother resending SYN ACK's */
1342                         if (tpriv->nlimbo > 100)
1343                                 continue;
1344
1345                         if (sndsynack(tcp, lp) < 0) {
1346                                 tpriv->nlimbo--;
1347                                 *l = lp->next;
1348                                 kfree(lp);
1349                                 continue;
1350                         }
1351
1352                         l = &lp->next;
1353                 }
1354         }
1355         qunlock(&tcp->qlock);
1356 }
1357
1358 /*
1359  *  lookup call in limbo.  if found, throw it out.
1360  *
1361  *  called with proto locked
1362  */
1363 static void limborst(struct conv *s, Tcp *segp, uint8_t *src, uint8_t *dst,
1364                      uint8_t version)
1365 {
1366         Limbo *lp, **l;
1367         int h;
1368         struct tcppriv *tpriv;
1369
1370         tpriv = s->p->priv;
1371
1372         /* find a call in limbo */
1373         h = hashipa(src, segp->source);
1374         for (l = &tpriv->lht[h]; *l != NULL; l = &lp->next) {
1375                 lp = *l;
1376                 if (lp->lport != segp->dest || lp->rport != segp->source
1377                         || lp->version != version)
1378                         continue;
1379                 if (ipcmp(lp->laddr, dst) != 0)
1380                         continue;
1381                 if (ipcmp(lp->raddr, src) != 0)
1382                         continue;
1383
1384                 /* RST can only follow the SYN */
1385                 if (segp->seq == lp->irs + 1) {
1386                         tpriv->nlimbo--;
1387                         *l = lp->next;
1388                         kfree(lp);
1389                 }
1390                 break;
1391         }
1392 }
1393
1394 /* The advertised MSS (e.g. 1460) includes any per-packet TCP options, such as
1395  * TCP timestamps.  A given packet will contain mss bytes, but only typical_mss
1396  * bytes of *data*.  If we know we'll use those options, we should adjust our
1397  * typical_mss, which will affect the cwnd. */
1398 static void adjust_typical_mss_for_opts(Tcp *tcph, Tcpctl *tcb)
1399 {
1400         uint16_t opt_size = 0;
1401
1402         if (tcph->ts_val)
1403                 opt_size += TS_LENGTH + TS_SEND_PREPAD;
1404         opt_size = ROUNDUP(opt_size, 4);
1405         tcb->typical_mss -= opt_size;
1406 }
1407
1408 /*
1409  *  come here when we finally get an ACK to our SYN-ACK.
1410  *  lookup call in limbo.  if found, create a new conversation
1411  *
1412  *  called with proto locked
1413  */
1414 static struct conv *tcpincoming(struct conv *s, Tcp *segp, uint8_t *src,
1415                                                                 uint8_t *dst, uint8_t version)
1416 {
1417         struct conv *new;
1418         Tcpctl *tcb;
1419         struct tcppriv *tpriv;
1420         Tcp4hdr *h4;
1421         Tcp6hdr *h6;
1422         Limbo *lp, **l;
1423         int h;
1424
1425         /* unless it's just an ack, it can't be someone coming out of limbo */
1426         if ((segp->flags & SYN) || (segp->flags & ACK) == 0)
1427                 return NULL;
1428
1429         tpriv = s->p->priv;
1430
1431         /* find a call in limbo */
1432         h = hashipa(src, segp->source);
1433         for (l = &tpriv->lht[h]; (lp = *l) != NULL; l = &lp->next) {
1434                 netlog(s->p->f, Logtcp,
1435                            "tcpincoming s %I!%d/%I!%d d %I!%d/%I!%d v %d/%d\n", src,
1436                            segp->source, lp->raddr, lp->rport, dst, segp->dest, lp->laddr,
1437                            lp->lport, version, lp->version);
1438
1439                 if (lp->lport != segp->dest || lp->rport != segp->source
1440                         || lp->version != version)
1441                         continue;
1442                 if (ipcmp(lp->laddr, dst) != 0)
1443                         continue;
1444                 if (ipcmp(lp->raddr, src) != 0)
1445                         continue;
1446
1447                 /* we're assuming no data with the initial SYN */
1448                 if (segp->seq != lp->irs + 1 || segp->ack != lp->iss + 1) {
1449                         netlog(s->p->f, Logtcp, "tcpincoming s 0x%lx/0x%lx a 0x%lx 0x%lx\n",
1450                                    segp->seq, lp->irs + 1, segp->ack, lp->iss + 1);
1451                         lp = NULL;
1452                 } else {
1453                         tpriv->nlimbo--;
1454                         *l = lp->next;
1455                 }
1456                 break;
1457         }
1458         if (lp == NULL)
1459                 return NULL;
1460
1461         new = Fsnewcall(s, src, segp->source, dst, segp->dest, version);
1462         if (new == NULL)
1463                 return NULL;
1464
1465         memmove(new->ptcl, s->ptcl, sizeof(Tcpctl));
1466         tcb = (Tcpctl *) new->ptcl;
1467         tcb->flags &= ~CLONE;
1468         tcb->timer.arg = new;
1469         tcb->timer.state = TcptimerOFF;
1470         tcb->acktimer.arg = new;
1471         tcb->acktimer.state = TcptimerOFF;
1472         tcb->katimer.arg = new;
1473         tcb->katimer.state = TcptimerOFF;
1474         tcb->rtt_timer.arg = new;
1475         tcb->rtt_timer.state = TcptimerOFF;
1476
1477         tcb->irs = lp->irs;
1478         tcb->rcv.nxt = tcb->irs + 1;
1479         tcb->rcv.urg = tcb->rcv.nxt;
1480
1481         tcb->iss = lp->iss;
1482         tcb->rttseq = tcb->iss;
1483         tcb->snd.wl2 = tcb->iss;
1484         tcb->snd.una = tcb->iss + 1;
1485         tcb->snd.rtx = tcb->iss + 1;
1486         tcb->snd.nxt = tcb->iss + 1;
1487         tcb->flgcnt = 0;
1488         tcb->flags |= SYNACK;
1489
1490         /* our sending max segment size cannot be bigger than what he asked for */
1491         if (lp->mss != 0 && lp->mss < tcb->mss) {
1492                 tcb->mss = lp->mss;
1493                 tcb->typical_mss = tcb->mss;
1494         }
1495         adjust_typical_mss_for_opts(segp, tcb);
1496
1497         /* Here's where we record the previously-decided header options.  They were
1498          * actually decided on when we agreed to them in the SYNACK we sent.  We
1499          * didn't create an actual TCB until now, so we can copy those decisions out
1500          * of the limbo tracker and into the TCB. */
1501         tcb->ifc = lp->ifc;
1502         tcb->sack_ok = lp->sack_ok;
1503         /* window scaling */
1504         tcpsetscale(new, tcb, lp->rcvscale, lp->sndscale);
1505         tcb_check_tso(tcb);
1506
1507         tcb->snd.wnd = segp->wnd;
1508         tcb->cwind = tcb->typical_mss * CWIND_SCALE;
1509
1510         /* set initial round trip time */
1511         tcb->sndsyntime = lp->lastsend + lp->rexmits * SYNACK_RXTIMER;
1512         tcpsynackrtt(new);
1513
1514         kfree(lp);
1515
1516         /* set up proto header */
1517         switch (version) {
1518                 case V4:
1519                         h4 = &tcb->protohdr.tcp4hdr;
1520                         memset(h4, 0, sizeof(*h4));
1521                         h4->proto = IP_TCPPROTO;
1522                         hnputs(h4->tcpsport, new->lport);
1523                         hnputs(h4->tcpdport, new->rport);
1524                         v6tov4(h4->tcpsrc, dst);
1525                         v6tov4(h4->tcpdst, src);
1526                         break;
1527                 case V6:
1528                         h6 = &tcb->protohdr.tcp6hdr;
1529                         memset(h6, 0, sizeof(*h6));
1530                         h6->proto = IP_TCPPROTO;
1531                         hnputs(h6->tcpsport, new->lport);
1532                         hnputs(h6->tcpdport, new->rport);
1533                         ipmove(h6->tcpsrc, dst);
1534                         ipmove(h6->tcpdst, src);
1535                         break;
1536                 default:
1537                         panic("tcpincoming: version %d", new->ipversion);
1538         }
1539
1540         tcpsetstate(new, Established);
1541
1542         iphtadd(&tpriv->ht, new);
1543
1544         return new;
1545 }
1546
1547 /*
1548  *  use the time between the first SYN and it's ack as the
1549  *  initial round trip time
1550  */
1551 static void tcpsynackrtt(struct conv *s)
1552 {
1553         Tcpctl *tcb;
1554         uint64_t delta;
1555         struct tcppriv *tpriv;
1556
1557         tcb = (Tcpctl *) s->ptcl;
1558         tpriv = s->p->priv;
1559
1560         delta = NOW - tcb->sndsyntime;
1561         tcb->srtt = delta;
1562         tcb->mdev = delta / 2;
1563
1564         /* halt round trip timer */
1565         tcphalt(tpriv, &tcb->rtt_timer);
1566 }
1567
1568 /* For LFNs (long/fat), our default tx queue doesn't hold enough data, and TCP
1569  * blocks on the application - even if the app already has the data ready to go.
1570  * We need to hold the sent, unacked data (1x cwnd), plus all the data we might
1571  * send next RTT (1x cwnd).  Note this is called after cwnd was expanded. */
1572 static void adjust_tx_qio_limit(struct conv *s)
1573 {
1574         Tcpctl *tcb = (Tcpctl *) s->ptcl;
1575         size_t ideal_limit = tcb->cwind * 2;
1576
1577         /* This is called for every ACK, and it's not entirely free to update the
1578          * limit (locks, CVs, taps).  Updating in chunks of mss seems reasonable.
1579          * During SS, we'll update this on most ACKs (given each ACK increased the
1580          * cwind by > MSS).
1581          *
1582          * We also don't want a lot of tiny blocks from the user, but the way qio
1583          * works, you can put in as much as you want (Maxatomic) and then get
1584          * flow-controlled. */
1585         if (qgetlimit(s->wq) + tcb->typical_mss < ideal_limit)
1586                 qsetlimit(s->wq, ideal_limit);
1587         /* TODO: we could shrink the qio limit too, if we had a better idea what the
1588          * actual threshold was.  We want the limit to be the 'stable' cwnd * 2. */
1589 }
1590
1591 /* Attempts to merge later sacks into sack 'into' (index in the array) */
1592 static void merge_sacks_into(Tcpctl *tcb, int into)
1593 {
1594         struct sack_block *into_sack = &tcb->snd.sacks[into];
1595         struct sack_block *tcb_sack;
1596         int shift = 0;
1597
1598         for (int i = into + 1; i < tcb->snd.nr_sacks; i++) {
1599                 tcb_sack = &tcb->snd.sacks[i];
1600                 if (seq_lt(into_sack->right, tcb_sack->left))
1601                         break;
1602                 if (seq_gt(tcb_sack->right, into_sack->right))
1603                         into_sack->right = tcb_sack->right;
1604                 shift++;
1605         }
1606         if (shift) {
1607                 memmove(tcb->snd.sacks + into + 1,
1608                         tcb->snd.sacks + into + 1 + shift,
1609                         sizeof(struct sack_block) * (tcb->snd.nr_sacks - into - 1
1610                                                              - shift));
1611                 tcb->snd.nr_sacks -= shift;
1612         }
1613 }
1614
1615 /* If we update a sack, it means they received a packet (possibly out of order),
1616  * but they have not received earlier packets.  Otherwise, they would do a full
1617  * ACK.
1618  *
1619  * The trick is in knowing whether the reception growing this sack is due to a
1620  * retrans or due to packets from before our last loss event.  The rightmost
1621  * sack tends to grow a lot with packets we sent before the loss.  However,
1622  * intermediate sacks that grow are signs of a loss, since they only grow as a
1623  * result of retrans.
1624  *
1625  * This is only true for the first time through a retrans.  After we've gone
1626  * through a full retrans blast, the sack that hinted at the retrans loss (and
1627  * there could be multiple of them!) will continue to grow.  We could come up
1628  * with some tracking for this, but instead we'll just do a one-time deal.  You
1629  * can recover from one detected sack retrans loss.  After that, you'll have to
1630  * use the RTO.
1631  *
1632  * This won't catch some things, like a sack that grew and merged with the
1633  * rightmost sack.  This also won't work if you have a single sack.  We can't
1634  * tell where the retrans ends and the sending begins. */
1635 static bool sack_hints_at_loss(Tcpctl *tcb, struct sack_block *tcb_sack)
1636 {
1637         if (tcb->snd.recovery != SACK_RETRANS_RECOVERY)
1638                 return FALSE;
1639         return &tcb->snd.sacks[tcb->snd.nr_sacks - 1] != tcb_sack;
1640 }
1641
1642 static bool sack_contains(struct sack_block *tcb_sack, uint32_t seq)
1643 {
1644         return seq_le(tcb_sack->left, seq) && seq_lt(seq, tcb_sack->right);
1645 }
1646
1647 /* Debugging helper! */
1648 static void sack_asserter(Tcpctl *tcb, char *str)
1649 {
1650         struct sack_block *tcb_sack;
1651
1652         for (int i = 0; i < tcb->snd.nr_sacks; i++) {
1653                 tcb_sack = &tcb->snd.sacks[i];
1654                 /* Checking invariants: snd.rtx is never inside a sack, sacks are always
1655                  * mutually exclusive. */
1656                 if (sack_contains(tcb_sack, tcb->snd.rtx) ||
1657                     ((i + 1 < tcb->snd.nr_sacks) && seq_ge(tcb_sack->right,
1658                                                                (tcb_sack + 1)->left))) {
1659                         printk("SACK ASSERT ERROR at %s\n", str);
1660                         printk("rtx %u una %u nxt %u, sack [%u, %u)\n",
1661                                tcb->snd.rtx, tcb->snd.una, tcb->snd.nxt, tcb_sack->left,
1662                                    tcb_sack->right);
1663                         for (int i = 0; i < tcb->snd.nr_sacks; i++)
1664                                 printk("\t %d: [%u, %u)\n", i, tcb->snd.sacks[i].left,
1665                                        tcb->snd.sacks[i].right);
1666                         backtrace();
1667                         panic("");
1668                 }
1669         }
1670 }
1671
1672 /* Updates bookkeeping whenever a sack is added or updated */
1673 static void sack_has_changed(struct conv *s, Tcpctl *tcb,
1674                              struct sack_block *tcb_sack)
1675 {
1676         /* Due to the change, snd.rtx might be in the middle of this sack.  Advance
1677          * it to the right edge. */
1678         if (sack_contains(tcb_sack, tcb->snd.rtx))
1679                 tcb->snd.rtx = tcb_sack->right;
1680
1681         /* This is a sack for something we retransed and we think it means there was
1682          * another loss.  Instead of waiting for the RTO, we can take action. */
1683         if (sack_hints_at_loss(tcb, tcb_sack)) {
1684                 if (++tcb->snd.sack_loss_hint == TCPREXMTTHRESH) {
1685                         netlog(s->p->f, Logtcprxmt,
1686                                "%I.%d -> %I.%d: sack rxmit loss: snd.rtx %u, sack [%u,%u), una %u, recovery_pt %u\n",
1687                                s->laddr, s->lport, s->raddr, s->rport,
1688                                tcb->snd.rtx, tcb_sack->left, tcb_sack->right, tcb->snd.una,
1689                                tcb->snd.recovery_pt);
1690                         /* Redo retrans, but keep the sacks and recovery point */
1691                         tcp_loss_event(s, tcb);
1692                         tcb->snd.rtx = tcb->snd.una;
1693                         tcb->snd.sack_loss_hint = 0;
1694                         /* Act like an RTO.  We just detected it earlier.  This prevents us
1695                          * from getting another sack hint loss this recovery period and from
1696                          * advancing the opportunistic right edge. */
1697                         tcb->snd.recovery = RTO_RETRANS_RECOVERY;
1698                         /* We didn't actually time out yet and we expect to keep getting
1699                          * sacks, so we don't want to flush or worry about in_flight.  If we
1700                          * messed something up, the RTO will still fire. */
1701                         set_in_flight(tcb);
1702                 }
1703         }
1704 }
1705
1706 /* Advances tcb_sack's right edge, if new_right is farther, and updates the
1707  * bookkeeping due to the change. */
1708 static void update_right_edge(struct conv *s, Tcpctl *tcb,
1709                               struct sack_block *tcb_sack, uint32_t new_right)
1710 {
1711         if (seq_le(new_right, tcb_sack->right))
1712                 return;
1713         tcb_sack->right = new_right;
1714         merge_sacks_into(tcb, tcb_sack - tcb->snd.sacks);
1715         sack_has_changed(s, tcb, tcb_sack);
1716 }
1717
1718 static void update_or_insert_sack(struct conv *s, Tcpctl *tcb,
1719                                   struct sack_block *seg_sack)
1720 {
1721         struct sack_block *tcb_sack;
1722
1723         for (int i = 0; i < tcb->snd.nr_sacks; i++) {
1724                 tcb_sack = &tcb->snd.sacks[i];
1725                 if (seq_lt(tcb_sack->left, seg_sack->left)) {
1726                         /* This includes adjacent (which I've seen!) and overlap. */
1727                         if (seq_le(seg_sack->left, tcb_sack->right)) {
1728                                 update_right_edge(s, tcb, tcb_sack, seg_sack->right);
1729                                 return;
1730                         }
1731                         continue;
1732                 }
1733                 /* Update existing sack */
1734                 if (tcb_sack->left == seg_sack->left) {
1735                         update_right_edge(s, tcb, tcb_sack, seg_sack->right);
1736                         return;
1737                 }
1738                 /* Found our slot */
1739                 if (seq_gt(tcb_sack->left, seg_sack->left)) {
1740                         if (tcb->snd.nr_sacks == MAX_NR_SND_SACKS) {
1741                                 /* Out of room, but it is possible this sack overlaps later
1742                                  * sacks, including the max sack's right edge. */
1743                                 if (seq_ge(seg_sack->right, tcb_sack->left)) {
1744                                         /* Take over the sack */
1745                                         tcb_sack->left = seg_sack->left;
1746                                         update_right_edge(s, tcb, tcb_sack, seg_sack->right);
1747                                 }
1748                                 return;
1749                         }
1750                         /* O/W, it's our slot and we have room (at least one spot). */
1751                         memmove(&tcb->snd.sacks[i + 1], &tcb->snd.sacks[i],
1752                                 sizeof(struct sack_block) * (tcb->snd.nr_sacks - i));
1753                         tcb_sack->left = seg_sack->left;
1754                         tcb_sack->right = seg_sack->right;
1755                         tcb->snd.nr_sacks++;
1756                         merge_sacks_into(tcb, i);
1757                         sack_has_changed(s, tcb, tcb_sack);
1758                         return;
1759                 }
1760         }
1761         if (tcb->snd.nr_sacks == MAX_NR_SND_SACKS) {
1762                 /* We didn't find space in the sack array. */
1763                 tcb_sack = &tcb->snd.sacks[MAX_NR_SND_SACKS - 1];
1764                 /* Need to always maintain the rightmost sack, discarding the prev */
1765                 if (seq_gt(seg_sack->right, tcb_sack->right)) {
1766                         tcb_sack->left = seg_sack->left;
1767                         tcb_sack->right = seg_sack->right;
1768                         sack_has_changed(s, tcb, tcb_sack);
1769                 }
1770                 return;
1771         }
1772         tcb_sack = &tcb->snd.sacks[tcb->snd.nr_sacks];
1773         tcb->snd.nr_sacks++;
1774         tcb_sack->left = seg_sack->left;
1775         tcb_sack->right = seg_sack->right;
1776         sack_has_changed(s, tcb, tcb_sack);
1777 }
1778
1779 /* Given the packet seg, track the sacks in TCB.  There are a few things: if seg
1780  * acks new data, some sacks might no longer be needed.  Some sacks might grow,
1781  * we might add new sacks, either of which can cause a merger.
1782  *
1783  * The important thing is that we always have the max sack entry: it must be
1784  * inserted for sure and findable.  We need that for our measurement of what
1785  * packets are in the network.
1786  *
1787  * Note that we keep sacks that are below snd.rtx (and above
1788  * seg.ack/tcb->snd.una) as best we can - we don't prune them.  We'll need those
1789  * for the in_flight estimate.
1790  *
1791  * When we run out of room, we'll have to throw away a sack.  Anything we throw
1792  * away below snd.rtx will be counted as 'in flight', even though it isn't.  If
1793  * we throw away something greater than snd.rtx, we'll also retrans it.  For
1794  * simplicity, we throw-away / replace the rightmost sack, since we're always
1795  * maintaining a highest sack. */
1796 static void update_sacks(struct conv *s, Tcpctl *tcb, Tcp *seg)
1797 {
1798         int prune = 0;
1799         struct sack_block *tcb_sack;
1800
1801         for (int i = 0; i < tcb->snd.nr_sacks; i++) {
1802                 tcb_sack = &tcb->snd.sacks[i];
1803                 /* For the equality case, if they acked up to, but not including an old
1804                  * sack, they must have reneged it.  Otherwise they would have acked
1805                  * beyond the sack. */
1806                 if (seq_lt(seg->ack, tcb_sack->left))
1807                         break;
1808                 prune++;
1809         }
1810         if (prune) {
1811                 memmove(tcb->snd.sacks, tcb->snd.sacks + prune,
1812                         sizeof(struct sack_block) * (tcb->snd.nr_sacks - prune));
1813                 tcb->snd.nr_sacks -= prune;
1814         }
1815         for (int i = 0; i < seg->nr_sacks; i++) {
1816                 /* old sacks */
1817                 if (seq_lt(seg->sacks[i].left, seg->ack))
1818                         continue;
1819                 /* buggy sack: out of range */
1820                 if (seq_gt(seg->sacks[i].right, tcb->snd.nxt))
1821                         continue;
1822                 update_or_insert_sack(s, tcb, &seg->sacks[i]);
1823         }
1824 }
1825
1826 /* This is a little bit of an under estimate, since we assume a packet is lost
1827  * once we have any sacks above it.  Overall, it's at most 2 * MSS of an
1828  * overestimate.
1829  *
1830  * If we have no sacks (either reneged or never used) we'll assume all packets
1831  * above snd.rtx are lost.  This will be the case for sackless fast rxmit
1832  * (Dong's stuff) or for a timeout.  In the former case, this is probably not
1833  * true, and in_flight should be higher, but we have no knowledge without the
1834  * sacks. */
1835 static void set_in_flight(Tcpctl *tcb)
1836 {
1837         struct sack_block *tcb_sack;
1838         uint32_t in_flight = 0;
1839         uint32_t from;
1840
1841         if (!tcb->snd.nr_sacks) {
1842                 tcb->snd.in_flight = tcb->snd.rtx - tcb->snd.una;
1843                 return;
1844         }
1845
1846         /* Everything to the right of the unsacked */
1847         tcb_sack = &tcb->snd.sacks[tcb->snd.nr_sacks - 1];
1848         in_flight += tcb->snd.nxt - tcb_sack->right;
1849
1850         /* Everything retransed (from una to snd.rtx, minus sacked regions.  Note
1851          * we only retrans at most the last sack's left edge.  snd.rtx will be
1852          * advanced to the right edge of some sack (possibly the last one). */
1853         from = tcb->snd.una;
1854         for (int i = 0; i < tcb->snd.nr_sacks; i++) {
1855                 tcb_sack = &tcb->snd.sacks[i];
1856                 if (seq_ge(tcb_sack->left, tcb->snd.rtx))
1857                         break;
1858                 assert(seq_ge(tcb->snd.rtx, tcb_sack->right));
1859                 in_flight += tcb_sack->left - from;
1860                 from = tcb_sack->right;
1861         }
1862         in_flight += tcb->snd.rtx - from;
1863
1864         tcb->snd.in_flight = in_flight;
1865 }
1866
1867 static void reset_recovery(struct conv *s, Tcpctl *tcb)
1868 {
1869         netlog(s->p->f, Logtcprxmt,
1870                "%I.%d -> %I.%d: recovery complete, una %u, rtx %u, nxt %u, recovery %u\n",
1871                s->laddr, s->lport, s->raddr, s->rport,
1872                tcb->snd.una, tcb->snd.rtx, tcb->snd.nxt, tcb->snd.recovery_pt);
1873         tcb->snd.recovery = 0;
1874         tcb->snd.recovery_pt = 0;
1875         tcb->snd.loss_hint = 0;
1876         tcb->snd.flush_sacks = FALSE;
1877         tcb->snd.sack_loss_hint = 0;
1878 }
1879
1880 static bool is_dup_ack(Tcpctl *tcb, Tcp *seg)
1881 {
1882         /* this is a pure ack w/o window update */
1883         return (seg->ack == tcb->snd.una) &&
1884                (tcb->snd.una != tcb->snd.nxt) &&
1885                (seg->len == 0) &&
1886                (seg->wnd == tcb->snd.wnd);
1887 }
1888
1889 /* If we have sacks, we'll ignore dupacks and look at the sacks ahead of una
1890  * (which are managed by the TCB).  The tcb will not have old sacks (below
1891  * ack/snd.rtx).  Receivers often send sacks below their ack point when we are
1892  * coming out of a loss, and we don't want those to count.
1893  *
1894  * Note the tcb could have sacks (in the future), but the receiver stopped using
1895  * them (reneged).  We'll catch that with the RTO.  If we try to catch it here,
1896  * we could get in a state where we never allow them to renege. */
1897 static bool is_potential_loss(Tcpctl *tcb, Tcp *seg)
1898 {
1899         if (seg->nr_sacks > 0)
1900                 return tcb->snd.nr_sacks > 0;
1901         else
1902                 return is_dup_ack(tcb, seg);
1903 }
1904
1905 /* When we use timestamps for RTTM, RFC 7323 suggests scaling by
1906  * expected_samples (per cwnd).  They say:
1907  *
1908  * ExpectedSamples = ceiling(FlightSize / (SMSS * 2))
1909  *
1910  * However, SMMS * 2 is really "number of bytes expected to be acked in a
1911  * packet.".  We'll use 'acked' to approximate that.  When the receiver uses
1912  * LRO, they'll send back large ACKs, which decreases the number of samples.
1913  *
1914  * If it turns out that all the divides are bad, we can just go back to not
1915  * using expected_samples at all. */
1916 static int expected_samples_ts(Tcpctl *tcb, uint32_t acked)
1917 {
1918         assert(acked);
1919         return MAX(DIV_ROUND_UP(tcb->snd.nxt - tcb->snd.una, acked), 1);
1920 }
1921
1922 /* Updates the RTT, given the currently sampled RTT and the number samples per
1923  * cwnd.  For non-TS RTTM, that'll be 1. */
1924 static void update_rtt(Tcpctl *tcb, int rtt_sample, int expected_samples)
1925 {
1926         int delta;
1927
1928         tcb->backoff = 0;
1929         tcb->backedoff = 0;
1930         if (tcb->srtt == 0) {
1931                 tcb->srtt = rtt_sample;
1932                 tcb->mdev = rtt_sample / 2;
1933         } else {
1934                 delta = rtt_sample - tcb->srtt;
1935                 tcb->srtt += (delta >> RTTM_ALPHA_SHIFT) / expected_samples;
1936                 if (tcb->srtt <= 0)
1937                         tcb->srtt = 1;
1938                 tcb->mdev += ((abs(delta) - tcb->mdev) >> RTTM_BRAVO_SHIFT) /
1939                              expected_samples;
1940                 if (tcb->mdev <= 0)
1941                         tcb->mdev = 1;
1942         }
1943         tcpsettimer(tcb);
1944 }
1945
1946 static void update(struct conv *s, Tcp *seg)
1947 {
1948         int rtt;
1949         Tcpctl *tcb;
1950         uint32_t acked, expand;
1951         struct tcppriv *tpriv;
1952
1953         tpriv = s->p->priv;
1954         tcb = (Tcpctl *) s->ptcl;
1955
1956         if (!seq_within(seg->ack, tcb->snd.una, tcb->snd.nxt))
1957                 return;
1958
1959         acked = seg->ack - tcb->snd.una;
1960         tcb->snd.una = seg->ack;
1961         if (seq_gt(seg->ack, tcb->snd.rtx))
1962                 tcb->snd.rtx = seg->ack;
1963
1964         update_sacks(s, tcb, seg);
1965         set_in_flight(tcb);
1966
1967         /* We treat either a dupack or forward SACKs as a hint that there is a loss.
1968          * The RFCs suggest three dupacks before treating it as a loss (alternative
1969          * is reordered packets).  We'll treat three SACKs the same way. */
1970         if (is_potential_loss(tcb, seg) && !tcb->snd.recovery) {
1971                 tcb->snd.loss_hint++;
1972                 if (tcb->snd.loss_hint == TCPREXMTTHRESH) {
1973                         netlog(s->p->f, Logtcprxmt,
1974                                "%I.%d -> %I.%d: loss hint thresh, nr sacks %u, nxt %u, una %u, cwnd %u\n",
1975                                s->laddr, s->lport, s->raddr, s->rport,
1976                                tcb->snd.nr_sacks, tcb->snd.nxt, tcb->snd.una, tcb->cwind);
1977                         tcp_loss_event(s, tcb);
1978                         tcb->snd.recovery_pt = tcb->snd.nxt;
1979                         if (tcb->snd.nr_sacks) {
1980                                 tcb->snd.recovery = SACK_RETRANS_RECOVERY;
1981                                 tcb->snd.flush_sacks = FALSE;
1982                                 tcb->snd.sack_loss_hint = 0;
1983                         } else {
1984                                 tcb->snd.recovery = FAST_RETRANS_RECOVERY;
1985                         }
1986                         tcprxmit(s);
1987                 }
1988         }
1989
1990         /*
1991          *  update window
1992          */
1993         if (seq_gt(seg->ack, tcb->snd.wl2)
1994                 || (tcb->snd.wl2 == seg->ack && seg->wnd > tcb->snd.wnd)) {
1995                 tcb->snd.wnd = seg->wnd;
1996                 tcb->snd.wl2 = seg->ack;
1997         }
1998
1999         if (!acked) {
2000                 /*
2001                  *  don't let us hangup if sending into a closed window and
2002                  *  we're still getting acks
2003                  */
2004                 if (tcb->snd.recovery && (tcb->snd.wnd == 0))
2005                         tcb->backedoff = MAXBACKMS / 4;
2006                 return;
2007         }
2008         /* At this point, they have acked something new. (positive ack, ack > una).
2009          *
2010          * If we hadn't reached the threshold for recovery yet, the positive ACK
2011          * will reset our loss_hint count. */
2012         if (!tcb->snd.recovery)
2013                 tcb->snd.loss_hint = 0;
2014         else if (seq_ge(seg->ack, tcb->snd.recovery_pt))
2015                 reset_recovery(s, tcb);
2016
2017         /* avoid slow start and timers for SYN acks */
2018         if ((tcb->flags & SYNACK) == 0) {
2019                 tcb->flags |= SYNACK;
2020                 acked--;
2021                 tcb->flgcnt--;
2022                 goto done;
2023         }
2024
2025         /* slow start as long as we're not recovering from lost packets */
2026         if (tcb->cwind < tcb->snd.wnd && !tcb->snd.recovery) {
2027                 if (tcb->cwind < tcb->ssthresh) {
2028                         /* We increase the cwind by every byte we receive.  We want to
2029                          * increase the cwind by one MSS for every MSS that gets ACKed.
2030                          * Note that multiple MSSs can be ACKed in a single ACK.  If we had
2031                          * a remainder of acked / MSS, we'd add just that remainder - not 0
2032                          * or 1 MSS. */
2033                         expand = acked;
2034                 } else {
2035                         /* Every RTT, which consists of CWND bytes, we're supposed to expand
2036                          * by MSS bytes.  The classic algorithm was
2037                          *              expand = (tcb->mss * tcb->mss) / tcb->cwind;
2038                          * which assumes the ACK was for MSS bytes.  Instead, for every
2039                          * 'acked' bytes, we increase the window by acked / CWND (in units
2040                          * of MSS). */
2041                         expand = MAX(acked, tcb->typical_mss) * tcb->typical_mss
2042                                  / tcb->cwind;
2043                 }
2044
2045                 if (tcb->cwind + expand < tcb->cwind)
2046                         expand = tcb->snd.wnd - tcb->cwind;
2047                 if (tcb->cwind + expand > tcb->snd.wnd)
2048                         expand = tcb->snd.wnd - tcb->cwind;
2049                 tcb->cwind += expand;
2050         }
2051         adjust_tx_qio_limit(s);
2052
2053         if (tcb->ts_recent) {
2054                 update_rtt(tcb, abs(milliseconds() - seg->ts_ecr),
2055                            expected_samples_ts(tcb, acked));
2056         } else if (tcb->rtt_timer.state == TcptimerON &&
2057                    seq_ge(seg->ack, tcb->rttseq)) {
2058                 /* Adjust the timers according to the round trip time */
2059                 tcphalt(tpriv, &tcb->rtt_timer);
2060                 if (!tcb->snd.recovery) {
2061                         rtt = tcb->rtt_timer.start - tcb->rtt_timer.count;
2062                         if (rtt == 0)
2063                                 rtt = 1;        /* o/w all close systems will rexmit in 0 time */
2064                         rtt *= MSPTICK;
2065                         update_rtt(tcb, rtt, 1);
2066                 }
2067         }
2068
2069 done:
2070         if (qdiscard(s->wq, acked) < acked) {
2071                 tcb->flgcnt--;
2072                 /* This happened due to another bug where acked was very large
2073                  * (negative), which was interpreted as "hey, one less flag, since they
2074                  * acked one of our flags (like a SYN).  If flgcnt goes negative,
2075                  * get_xmit_segment() will attempt to send out large packets. */
2076                 assert(tcb->flgcnt >= 0);
2077         }
2078
2079         if (seq_gt(seg->ack, tcb->snd.urg))
2080                 tcb->snd.urg = seg->ack;
2081
2082         if (tcb->snd.una != tcb->snd.nxt)
2083                 tcpgo(tpriv, &tcb->timer);
2084         else
2085                 tcphalt(tpriv, &tcb->timer);
2086
2087         tcb->backoff = 0;
2088         tcb->backedoff = 0;
2089 }
2090
2091 static void update_tcb_ts(Tcpctl *tcb, Tcp *seg)
2092 {
2093         /* Get timestamp info from the tcp header.  Even though the timestamps
2094          * aren't sequence numbers, we still need to protect for wraparound.  Though
2095          * if the values were 0, assume that means we need an update.  We could have
2096          * an initial ts_val that appears negative (signed). */
2097         if (!tcb->ts_recent || !tcb->last_ack_sent ||
2098             (seq_ge(seg->ts_val, tcb->ts_recent) &&
2099              seq_le(seg->seq, tcb->last_ack_sent)))
2100                 tcb->ts_recent = seg->ts_val;
2101 }
2102
2103 /* Overlap happens when one sack's left edge is inside another sack. */
2104 static bool sacks_overlap(struct sack_block *x, struct sack_block *y)
2105 {
2106         return (seq_le(x->left, y->left) && seq_le(y->left, x->right)) ||
2107                (seq_le(y->left, x->left) && seq_le(x->left, y->right));
2108 }
2109
2110 static void make_sack_first(Tcpctl *tcb, struct sack_block *tcb_sack)
2111 {
2112         struct sack_block temp;
2113
2114         if (tcb_sack == &tcb->rcv.sacks[0])
2115                 return;
2116         temp = tcb->rcv.sacks[0];
2117         tcb->rcv.sacks[0] = *tcb_sack;
2118         *tcb_sack = temp;
2119 }
2120
2121 /* Track sack in our tcb for a block of data we received.  This handles all the
2122  * stuff: making sure sack is first (since it's the most recent sack change),
2123  * updating or merging sacks, and dropping excess sacks (we only need to
2124  * maintain 3).  Unlike on the snd side, our tcb sacks are *not* sorted. */
2125 static void track_rcv_sack(Tcpctl *tcb, uint32_t left, uint32_t right)
2126 {
2127         struct sack_block *tcb_sack;
2128         struct sack_block sack[1];
2129
2130         if (!tcb->sack_ok)
2131                 return;
2132         assert(seq_lt(left, right));
2133         sack->left = left;
2134         sack->right = right;
2135         /* We can reuse an existing sack if we're merging or overlapping. */
2136         for (int i = 0; i < tcb->rcv.nr_sacks; i++) {
2137                 tcb_sack = &tcb->rcv.sacks[i];
2138                 if (sacks_overlap(tcb_sack, sack)) {
2139                         tcb_sack->left = seq_min(tcb_sack->left, sack->left);
2140                         tcb_sack->right = seq_max(tcb_sack->right, sack->right);
2141                         make_sack_first(tcb, tcb_sack);
2142                         return;
2143                 }
2144         }
2145         /* We can discard the last sack (right shift) - we should have sent it at
2146          * least once by now.  If not, oh well. */
2147         memmove(tcb->rcv.sacks + 1, tcb->rcv.sacks, sizeof(struct sack_block) *
2148                 MIN(MAX_NR_RCV_SACKS - 1, tcb->rcv.nr_sacks));
2149         tcb->rcv.sacks[0] = *sack;
2150         if (tcb->rcv.nr_sacks < MAX_NR_RCV_SACKS)
2151                 tcb->rcv.nr_sacks++;
2152 }
2153
2154 /* Once we receive everything and move rcv.nxt past a sack, we don't need to
2155  * track it.  I've seen Linux report sacks in the past, but we probably
2156  * shouldn't. */
2157 static void drop_old_rcv_sacks(Tcpctl *tcb)
2158 {
2159         struct sack_block *tcb_sack;
2160
2161         for (int i = 0; i < tcb->rcv.nr_sacks; i++) {
2162                 tcb_sack = &tcb->rcv.sacks[i];
2163                 /* Moving up to or past the left is enough to drop it. */
2164                 if (seq_ge(tcb->rcv.nxt, tcb_sack->left)) {
2165                         memmove(tcb->rcv.sacks + i, tcb->rcv.sacks + i + 1,
2166                                 sizeof(struct sack_block) * (tcb->rcv.nr_sacks - i - 1));
2167                         tcb->rcv.nr_sacks--;
2168                         i--;
2169                 }
2170         }
2171 }
2172
2173 static void tcpiput(struct Proto *tcp, struct Ipifc *unused, struct block *bp)
2174 {
2175         ERRSTACK(1);
2176         Tcp seg;
2177         Tcp4hdr *h4;
2178         Tcp6hdr *h6;
2179         int hdrlen;
2180         Tcpctl *tcb;
2181         uint16_t length;
2182         uint8_t source[IPaddrlen], dest[IPaddrlen];
2183         struct conv *s;
2184         struct Fs *f;
2185         struct tcppriv *tpriv;
2186         uint8_t version;
2187
2188         f = tcp->f;
2189         tpriv = tcp->priv;
2190
2191         tpriv->stats[InSegs]++;
2192
2193         h4 = (Tcp4hdr *) (bp->rp);
2194         h6 = (Tcp6hdr *) (bp->rp);
2195
2196         if ((h4->vihl & 0xF0) == IP_VER4) {
2197                 uint8_t ttl;
2198
2199                 version = V4;
2200                 length = nhgets(h4->length);
2201                 v4tov6(dest, h4->tcpdst);
2202                 v4tov6(source, h4->tcpsrc);
2203
2204                 /* ttl isn't part of the xsum pseudo header, but bypass needs it. */
2205                 ttl = h4->Unused;
2206                 h4->Unused = 0;
2207                 hnputs(h4->tcplen, length - TCP4_PKT);
2208                 if (!(bp->flag & Btcpck) && (h4->tcpcksum[0] || h4->tcpcksum[1]) &&
2209                         ptclcsum(bp, TCP4_IPLEN, length - TCP4_IPLEN)) {
2210                         tpriv->stats[CsumErrs]++;
2211                         tpriv->stats[InErrs]++;
2212                         netlog(f, Logtcp, "bad tcp proto cksum\n");
2213                         freeblist(bp);
2214                         return;
2215                 }
2216                 h4->Unused = ttl;
2217
2218                 hdrlen = ntohtcp4(&seg, &bp);
2219                 if (hdrlen < 0) {
2220                         tpriv->stats[HlenErrs]++;
2221                         tpriv->stats[InErrs]++;
2222                         netlog(f, Logtcp, "bad tcp hdr len\n");
2223                         return;
2224                 }
2225
2226                 s = iphtlook(&tpriv->ht, source, seg.source, dest, seg.dest);
2227                 if (s && s->state == Bypass) {
2228                         bypass_or_drop(s, bp);
2229                         return;
2230                 }
2231
2232                 /* trim the packet to the size claimed by the datagram */
2233                 length -= hdrlen + TCP4_PKT;
2234                 bp = trimblock(bp, hdrlen + TCP4_PKT, length);
2235                 if (bp == NULL) {
2236                         tpriv->stats[LenErrs]++;
2237                         tpriv->stats[InErrs]++;
2238                         netlog(f, Logtcp, "tcp len < 0 after trim\n");
2239                         return;
2240                 }
2241         } else {
2242                 int ttl = h6->ttl;
2243                 int proto = h6->proto;
2244
2245                 version = V6;
2246                 length = nhgets(h6->ploadlen);
2247                 ipmove(dest, h6->tcpdst);
2248                 ipmove(source, h6->tcpsrc);
2249
2250                 h6->ploadlen[0] = h6->ploadlen[1] = h6->proto = 0;
2251                 h6->ttl = proto;
2252                 hnputl(h6->vcf, length);
2253                 if ((h6->tcpcksum[0] || h6->tcpcksum[1]) &&
2254                         ptclcsum(bp, TCP6_IPLEN, length + TCP6_PHDRSIZE)) {
2255                         tpriv->stats[CsumErrs]++;
2256                         tpriv->stats[InErrs]++;
2257                         netlog(f, Logtcp, "bad tcp proto cksum\n");
2258                         freeblist(bp);
2259                         return;
2260                 }
2261                 h6->ttl = ttl;
2262                 h6->proto = proto;
2263                 hnputs(h6->ploadlen, length);
2264
2265                 hdrlen = ntohtcp6(&seg, &bp);
2266                 if (hdrlen < 0) {
2267                         tpriv->stats[HlenErrs]++;
2268                         tpriv->stats[InErrs]++;
2269                         netlog(f, Logtcp, "bad tcp hdr len\n");
2270                         return;
2271                 }
2272
2273                 s = iphtlook(&tpriv->ht, source, seg.source, dest, seg.dest);
2274                 if (s && s->state == Bypass) {
2275                         bypass_or_drop(s, bp);
2276                         return;
2277                 }
2278
2279                 /* trim the packet to the size claimed by the datagram */
2280                 length -= hdrlen;
2281                 bp = trimblock(bp, hdrlen + TCP6_PKT, length);
2282                 if (bp == NULL) {
2283                         tpriv->stats[LenErrs]++;
2284                         tpriv->stats[InErrs]++;
2285                         netlog(f, Logtcp, "tcp len < 0 after trim\n");
2286                         return;
2287                 }
2288         }
2289
2290         /* s, the conv matching the n-tuple, was set above */
2291         if (s == NULL) {
2292                 netlog(f, Logtcpreset, "iphtlook failed: src %I:%u, dst %I:%u\n",
2293                        source, seg.source, dest, seg.dest);
2294 reset:
2295                 sndrst(tcp, source, dest, length, &seg, version, "no conversation");
2296                 freeblist(bp);
2297                 return;
2298         }
2299
2300         /* lock protocol for unstate Plan 9 invariants.  funcs like limbo or
2301          * incoming might rely on it. */
2302         qlock(&tcp->qlock);
2303
2304         /* if it's a listener, look for the right flags and get a new conv */
2305         tcb = (Tcpctl *) s->ptcl;
2306         if (tcb->state == Listen) {
2307                 if (seg.flags & RST) {
2308                         limborst(s, &seg, source, dest, version);
2309                         qunlock(&tcp->qlock);
2310                         freeblist(bp);
2311                         return;
2312                 }
2313
2314                 /* if this is a new SYN, put the call into limbo */
2315                 if ((seg.flags & SYN) && (seg.flags & ACK) == 0) {
2316                         limbo(s, source, dest, &seg, version);
2317                         qunlock(&tcp->qlock);
2318                         freeblist(bp);
2319                         return;
2320                 }
2321
2322                 /* if there's a matching call in limbo, tcpincoming will return it */
2323                 s = tcpincoming(s, &seg, source, dest, version);
2324                 if (s == NULL) {
2325                         qunlock(&tcp->qlock);
2326                         goto reset;
2327                 }
2328         }
2329
2330         /* The rest of the input state machine is run with the control block
2331          * locked and implements the state machine directly out of the RFC.
2332          * Out-of-band data is ignored - it was always a bad idea.
2333          */
2334         tcb = (Tcpctl *) s->ptcl;
2335         if (waserror()) {
2336                 qunlock(&s->qlock);
2337                 nexterror();
2338         }
2339         qlock(&s->qlock);
2340         qunlock(&tcp->qlock);
2341
2342         update_tcb_ts(tcb, &seg);
2343         /* fix up window */
2344         seg.wnd <<= tcb->rcv.scale;
2345
2346         /* every input packet in puts off the keep alive time out */
2347         tcpsetkacounter(tcb);
2348
2349         switch (tcb->state) {
2350                 case Closed:
2351                         sndrst(tcp, source, dest, length, &seg, version,
2352                                    "sending to Closed");
2353                         goto raise;
2354                 case Syn_sent:
2355                         if (seg.flags & ACK) {
2356                                 if (!seq_within(seg.ack, tcb->iss + 1, tcb->snd.nxt)) {
2357                                         sndrst(tcp, source, dest, length, &seg, version,
2358                                                    "bad seq in Syn_sent");
2359                                         goto raise;
2360                                 }
2361                         }
2362                         if (seg.flags & RST) {
2363                                 if (seg.flags & ACK)
2364                                         localclose(s, "connection refused");
2365                                 goto raise;
2366                         }
2367
2368                         if (seg.flags & SYN) {
2369                                 procsyn(s, &seg);
2370                                 if (seg.flags & ACK) {
2371                                         update(s, &seg);
2372                                         tcpsynackrtt(s);
2373                                         tcpsetstate(s, Established);
2374                                         /* Here's where we get the results of header option
2375                                          * negotiations for connections we started. (SYNACK has the
2376                                          * response) */
2377                                         tcpsetscale(s, tcb, seg.ws, tcb->scale);
2378                                         tcb->sack_ok = seg.sack_ok;
2379                                 } else {
2380                                         sndrst(tcp, source, dest, length, &seg, version,
2381                                                    "Got SYN with no ACK");
2382                                         goto raise;
2383                                 }
2384
2385                                 if (length != 0 || (seg.flags & FIN))
2386                                         break;
2387
2388                                 freeblist(bp);
2389                                 goto output;
2390                         } else
2391                                 freeblist(bp);
2392
2393                         qunlock(&s->qlock);
2394                         poperror();
2395                         return;
2396         }
2397
2398         /*
2399          *  One DOS attack is to open connections to us and then forget about them,
2400          *  thereby tying up a conv at no long term cost to the attacker.
2401          *  This is an attempt to defeat these stateless DOS attacks.  See
2402          *  corresponding code in tcpsendka().
2403          */
2404         if ((seg.flags & RST) == 0) {
2405                 if (tcpporthogdefense
2406                         && seq_within(seg.ack, tcb->snd.una - (1 << 31),
2407                                                   tcb->snd.una - (1 << 29))) {
2408                         printd("stateless hog %I.%d->%I.%d f 0x%x 0x%lx - 0x%lx - 0x%lx\n",
2409                                    source, seg.source, dest, seg.dest, seg.flags,
2410                                    tcb->snd.una - (1 << 31), seg.ack, tcb->snd.una - (1 << 29));
2411                         localclose(s, "stateless hog");
2412                 }
2413         }
2414
2415         /* Cut the data to fit the receive window */
2416         if (tcptrim(tcb, &seg, &bp, &length) == -1) {
2417                 netlog(f, Logtcp, "%I.%d -> %I.%d: tcp len < 0, %lu %d\n",
2418                        s->raddr, s->rport, s->laddr, s->lport, seg.seq, length);
2419                 update(s, &seg);
2420                 if (qlen(s->wq) + tcb->flgcnt == 0 && tcb->state == Closing) {
2421                         tcphalt(tpriv, &tcb->rtt_timer);
2422                         tcphalt(tpriv, &tcb->acktimer);
2423                         tcphalt(tpriv, &tcb->katimer);
2424                         tcpsetstate(s, Time_wait);
2425                         tcb->timer.start = MSL2 * (1000 / MSPTICK);
2426                         tcpgo(tpriv, &tcb->timer);
2427                 }
2428                 if (!(seg.flags & RST)) {
2429                         tcb->flags |= FORCE;
2430                         goto output;
2431                 }
2432                 qunlock(&s->qlock);
2433                 poperror();
2434                 return;
2435         }
2436
2437         /* Cannot accept so answer with a rst */
2438         if (length && tcb->state == Closed) {
2439                 sndrst(tcp, source, dest, length, &seg, version, "sending to Closed");
2440                 goto raise;
2441         }
2442
2443         /* The segment is beyond the current receive pointer so
2444          * queue the data in the resequence queue
2445          */
2446         if (seg.seq != tcb->rcv.nxt)
2447                 if (length != 0 || (seg.flags & (SYN | FIN))) {
2448                         update(s, &seg);
2449                         if (addreseq(tcb, tpriv, &seg, bp, length) < 0)
2450                                 printd("reseq %I.%d -> %I.%d\n", s->raddr, s->rport, s->laddr,
2451                                            s->lport);
2452                         tcb->flags |= FORCE;
2453                         goto output;
2454                 }
2455
2456         /*
2457          *  keep looping till we've processed this packet plus any
2458          *  adjacent packets in the resequence queue
2459          */
2460         for (;;) {
2461                 if (seg.flags & RST) {
2462                         if (tcb->state == Established) {
2463                                 tpriv->stats[EstabResets]++;
2464                                 if (tcb->rcv.nxt != seg.seq)
2465                                         printd
2466                                                 ("out of order RST rcvd: %I.%d -> %I.%d, rcv.nxt 0x%lx seq 0x%lx\n",
2467                                                  s->raddr, s->rport, s->laddr, s->lport, tcb->rcv.nxt,
2468                                                  seg.seq);
2469                         }
2470                         localclose(s, "connection refused");
2471                         goto raise;
2472                 }
2473
2474                 if ((seg.flags & ACK) == 0)
2475                         goto raise;
2476
2477                 switch (tcb->state) {
2478                         case Established:
2479                         case Close_wait:
2480                                 update(s, &seg);
2481                                 break;
2482                         case Finwait1:
2483                                 update(s, &seg);
2484                                 if (qlen(s->wq) + tcb->flgcnt == 0) {
2485                                         tcphalt(tpriv, &tcb->rtt_timer);
2486                                         tcphalt(tpriv, &tcb->acktimer);
2487                                         tcpsetkacounter(tcb);
2488                                         tcb->time = NOW;
2489                                         tcpsetstate(s, Finwait2);
2490                                         tcb->katimer.start = MSL2 * (1000 / MSPTICK);
2491                                         tcpgo(tpriv, &tcb->katimer);
2492                                 }
2493                                 break;
2494                         case Finwait2:
2495                                 update(s, &seg);
2496                                 break;
2497                         case Closing:
2498                                 update(s, &seg);
2499                                 if (qlen(s->wq) + tcb->flgcnt == 0) {
2500                                         tcphalt(tpriv, &tcb->rtt_timer);
2501                                         tcphalt(tpriv, &tcb->acktimer);
2502                                         tcphalt(tpriv, &tcb->katimer);
2503                                         tcpsetstate(s, Time_wait);
2504                                         tcb->timer.start = MSL2 * (1000 / MSPTICK);
2505                                         tcpgo(tpriv, &tcb->timer);
2506                                 }
2507                                 break;
2508                         case Last_ack:
2509                                 update(s, &seg);
2510                                 if (qlen(s->wq) + tcb->flgcnt == 0) {
2511                                         localclose(s, NULL);
2512                                         goto raise;
2513                                 }
2514                         case Time_wait:
2515                                 tcb->flags |= FORCE;
2516                                 if (tcb->timer.state != TcptimerON)
2517                                         tcpgo(tpriv, &tcb->timer);
2518                 }
2519
2520                 if ((seg.flags & URG) && seg.urg) {
2521                         if (seq_gt(seg.urg + seg.seq, tcb->rcv.urg)) {
2522                                 tcb->rcv.urg = seg.urg + seg.seq;
2523                                 pullblock(&bp, seg.urg);
2524                         }
2525                 } else if (seq_gt(tcb->rcv.nxt, tcb->rcv.urg))
2526                         tcb->rcv.urg = tcb->rcv.nxt;
2527
2528                 if (length == 0) {
2529                         if (bp != NULL)
2530                                 freeblist(bp);
2531                 } else {
2532                         switch (tcb->state) {
2533                                 default:
2534                                         /* Ignore segment text */
2535                                         if (bp != NULL)
2536                                                 freeblist(bp);
2537                                         break;
2538
2539                                 case Established:
2540                                 case Finwait1:
2541                                         /* If we still have some data place on
2542                                          * receive queue
2543                                          */
2544                                         if (bp) {
2545                                                 bp = packblock(bp);
2546                                                 if (bp == NULL)
2547                                                         panic("tcp packblock");
2548                                                 qpassnolim(s->rq, bp);
2549                                                 bp = NULL;
2550
2551                                                 /*
2552                                                  *  Force an ack every 2 data messages.  This is
2553                                                  *  a hack for rob to make his home system run
2554                                                  *  faster.
2555                                                  *
2556                                                  *  this also keeps the standard TCP congestion
2557                                                  *  control working since it needs an ack every
2558                                                  *  2 max segs worth.  This is not quite that,
2559                                                  *  but under a real stream is equivalent since
2560                                                  *  every packet has a max seg in it.
2561                                                  */
2562                                                 if (++(tcb->rcv.una) >= 2)
2563                                                         tcb->flags |= FORCE;
2564                                         }
2565                                         tcb->rcv.nxt += length;
2566                                         drop_old_rcv_sacks(tcb);
2567
2568                                         /*
2569                                          *  update our rcv window
2570                                          */
2571                                         tcprcvwin(s);
2572
2573                                         /*
2574                                          *  turn on the acktimer if there's something
2575                                          *  to ack
2576                                          */
2577                                         if (tcb->acktimer.state != TcptimerON)
2578                                                 tcpgo(tpriv, &tcb->acktimer);
2579
2580                                         break;
2581                                 case Finwait2:
2582                                         /* no process to read the data, send a reset */
2583                                         if (bp != NULL)
2584                                                 freeblist(bp);
2585                                         sndrst(tcp, source, dest, length, &seg, version,
2586                                                    "send to Finwait2");
2587                                         qunlock(&s->qlock);
2588                                         poperror();
2589                                         return;
2590                         }
2591                 }
2592
2593                 if (seg.flags & FIN) {
2594                         tcb->flags |= FORCE;
2595
2596                         switch (tcb->state) {
2597                                 case Established:
2598                                         tcb->rcv.nxt++;
2599                                         tcpsetstate(s, Close_wait);
2600                                         break;
2601                                 case Finwait1:
2602                                         tcb->rcv.nxt++;
2603                                         if (qlen(s->wq) + tcb->flgcnt == 0) {
2604                                                 tcphalt(tpriv, &tcb->rtt_timer);
2605                                                 tcphalt(tpriv, &tcb->acktimer);
2606                                                 tcphalt(tpriv, &tcb->katimer);
2607                                                 tcpsetstate(s, Time_wait);
2608                                                 tcb->timer.start = MSL2 * (1000 / MSPTICK);
2609                                                 tcpgo(tpriv, &tcb->timer);
2610                                         } else
2611                                                 tcpsetstate(s, Closing);
2612                                         break;
2613                                 case Finwait2:
2614                                         tcb->rcv.nxt++;
2615                                         tcphalt(tpriv, &tcb->rtt_timer);
2616                                         tcphalt(tpriv, &tcb->acktimer);
2617                                         tcphalt(tpriv, &tcb->katimer);
2618                                         tcpsetstate(s, Time_wait);
2619                                         tcb->timer.start = MSL2 * (1000 / MSPTICK);
2620                                         tcpgo(tpriv, &tcb->timer);
2621                                         break;
2622                                 case Close_wait:
2623                                 case Closing:
2624                                 case Last_ack:
2625                                         break;
2626                                 case Time_wait:
2627                                         tcpgo(tpriv, &tcb->timer);
2628                                         break;
2629                         }
2630                 }
2631
2632                 /*
2633                  *  get next adjacent segment from the resequence queue.
2634                  *  dump/trim any overlapping segments
2635                  */
2636                 for (;;) {
2637                         if (tcb->reseq == NULL)
2638                                 goto output;
2639
2640                         if (seq_ge(tcb->rcv.nxt, tcb->reseq->seg.seq) == 0)
2641                                 goto output;
2642
2643                         getreseq(tcb, &seg, &bp, &length);
2644
2645                         if (tcptrim(tcb, &seg, &bp, &length) == 0)
2646                                 break;
2647                 }
2648         }
2649 output:
2650         tcpoutput(s);
2651         qunlock(&s->qlock);
2652         poperror();
2653         return;
2654 raise:
2655         qunlock(&s->qlock);
2656         poperror();
2657         freeblist(bp);
2658         tcpkick(s);
2659 }
2660
2661 /* The advertised mss = data + TCP headers */
2662 static uint16_t derive_payload_mss(Tcpctl *tcb)
2663 {
2664         uint16_t payload_mss = tcb->mss;
2665         uint16_t opt_size = 0;
2666
2667         if (tcb->ts_recent) {
2668                 opt_size += TS_LENGTH;
2669                 /* Note that when we're a SYN, we overestimate slightly.  This is safe,
2670                  * and not really a problem. */
2671                 opt_size += TS_SEND_PREPAD;
2672         }
2673         if (tcb->rcv.nr_sacks)
2674                 opt_size += 2 + tcb->rcv.nr_sacks * 8;
2675         opt_size = ROUNDUP(opt_size, 4);
2676         payload_mss -= opt_size;
2677         return payload_mss;
2678 }
2679
2680 /* Decreases the xmit amt, given the MSS / TSO. */
2681 static uint32_t throttle_for_mss(Tcpctl *tcb, uint32_t ssize,
2682                                  uint16_t payload_mss, bool retrans)
2683 {
2684         if (ssize > payload_mss) {
2685                 if ((tcb->flags & TSO) == 0) {
2686                         ssize = payload_mss;
2687                 } else {
2688                         /* Don't send too much.  32K is arbitrary.. */
2689                         if (ssize > 32 * 1024)
2690                                 ssize = 32 * 1024;
2691                         if (!retrans) {
2692                                 /* Clamp xmit to an integral MSS to avoid ragged tail segments
2693                                  * causing poor link utilization. */
2694                                 ssize = ROUNDDOWN(ssize, payload_mss);
2695                         }
2696                 }
2697         }
2698         return ssize;
2699 }
2700
2701 /* Reduces ssize for a variety of reasons.  Returns FALSE if we should abort
2702  * sending the packet.  o/w returns TRUE and modifies ssize by reference. */
2703 static bool throttle_ssize(struct conv *s, Tcpctl *tcb, uint32_t *ssize_p,
2704                            uint16_t payload_mss, bool retrans)
2705 {
2706         struct Fs *f = s->p->f;
2707         uint32_t usable;
2708         uint32_t ssize = *ssize_p;
2709
2710         /* Compute usable segment based on offered window and limit
2711          * window probes to one */
2712         if (tcb->snd.wnd == 0) {
2713                 if (tcb->snd.in_flight != 0) {
2714                         if ((tcb->flags & FORCE) == 0)
2715                                 return FALSE;
2716                 }
2717                 usable = 1;
2718         } else {
2719                 usable = tcb->cwind;
2720                 if (tcb->snd.wnd < usable)
2721                         usable = tcb->snd.wnd;
2722                 if (usable > tcb->snd.in_flight)
2723                         usable -= tcb->snd.in_flight;
2724                 else
2725                         usable = 0;
2726                 /* Avoid Silly Window Syndrome.  This is a little different thant RFC
2727                  * 813.  I took their additional enhancement of "< MSS" as an AND, not
2728                  * an OR.  25% of a large snd.wnd is pretty large, and our main goal is
2729                  * to avoid packets smaller than MSS.  I still use the 25% threshold,
2730                  * because it is important that there is *some* data in_flight.  If
2731                  * usable < MSS because snd.wnd is very small (but not 0), we might
2732                  * never get an ACK and would need to set up a timer.
2733                  *
2734                  * Also, I'm using 'ssize' as a proxy for a PSH point.  If there's just
2735                  * a small blob in the qio (or retrans!), then we might as well just
2736                  * send it. */
2737                 if ((usable < tcb->typical_mss) && (usable < tcb->snd.wnd >> 2)
2738                     && (usable < ssize)) {
2739                         return FALSE;
2740                 }
2741         }
2742         if (ssize && usable < 2)
2743                 netlog(s->p->f, Logtcpverbose,
2744                        "%I.%d -> %I.%d: throttled snd.wnd %lu cwind %lu\n",
2745                        s->laddr, s->lport, s->raddr, s->rport,
2746                        tcb->snd.wnd, tcb->cwind);
2747         if (usable < ssize)
2748                 ssize = usable;
2749
2750         ssize = throttle_for_mss(tcb, ssize, payload_mss, retrans);
2751
2752         *ssize_p = ssize;
2753         return TRUE;
2754 }
2755
2756 /* Helper, picks the next segment to send, which is possibly a retransmission.
2757  * Returns TRUE if we have a segment, FALSE o/w.  Returns ssize, from_seq, and
2758  * sent by reference.
2759  *
2760  * from_seq is the seq number we are transmitting from.
2761  *
2762  * sent includes all seq from una to from_seq *including* any previously sent
2763  * flags (part of tcb->flgcnt), for instance an unacknowledged SYN (which counts
2764  * as a seq number).  Those flags are in the e.g. snd.nxt - snd.una range, and
2765  * they get dropped after qdiscard.
2766  *
2767  * ssize is the amount of data we are sending, starting from from_seq, and it
2768  * will include any *new* flags, which haven't been accounted for yet.
2769  *
2770  * tcb->flgcnt consists of the flags both in ssize and in sent.
2771  *
2772  * Note that we could be in recovery and not sack_retrans a segment. */
2773 static bool get_xmit_segment(struct conv *s, Tcpctl *tcb, uint16_t payload_mss,
2774                              uint32_t *from_seq_p, uint32_t *sent_p,
2775                              uint32_t *ssize_p)
2776 {
2777         struct Fs *f = s->p->f;
2778         struct tcppriv *tpriv = s->p->priv;
2779         uint32_t ssize, sent, from_seq;
2780         bool sack_retrans = FALSE;
2781         struct sack_block *tcb_sack = 0;
2782
2783         for (int i = 0; i < tcb->snd.nr_sacks; i++) {
2784                 tcb_sack = &tcb->snd.sacks[i];
2785                 if (seq_lt(tcb->snd.rtx, tcb_sack->left)) {
2786                         /* So ssize is supposed to include any *new* flags to flgcnt, which
2787                          * at this point would be a FIN.
2788                          *
2789                          * It might be possible that flgcnt is incremented so we send a FIN,
2790                          * even for an intermediate sack retrans.  Perhaps the user closed
2791                          * the conv.
2792                          *
2793                          * However, the way the "flgcnt for FIN" works is that it inflates
2794                          * the desired amount we'd like to send (qlen + flgcnt).
2795                          * Eventually, we reach the end of the queue and fail to extract all
2796                          * of dsize.  At that point, we put on the FIN, and that's where the
2797                          * extra 'byte' comes from.
2798                          *
2799                          * For sack retrans, since we're extracting from parts of the qio
2800                          * that aren't the right-most edge, we don't need to consider flgcnt
2801                          * when setting ssize. */
2802                         from_seq = tcb->snd.rtx;
2803                         sent = from_seq - tcb->snd.una;
2804                         ssize = tcb_sack->left - from_seq;
2805                         sack_retrans = TRUE;
2806                         break;
2807                 }
2808         }
2809         /* SACK holes have first dibs, but we can still opportunisitically send new
2810          * data.
2811          *
2812          * During other types of recovery, we'll just send from the retrans point.
2813          * If we're in an RTO while we still have sacks, we could be resending data
2814          * that wasn't lost.  Consider a sack that is still growing (usually the
2815          * right-most), but we haven't received the ACK yet.  rxt may be included in
2816          * that area.  Given we had two losses or otherwise timed out, I'm not too
2817          * concerned.
2818          *
2819          * Note that Fast and RTO can send data beyond nxt.  If we change that,
2820          * change the accounting below. */
2821         if (!sack_retrans) {
2822                 switch (tcb->snd.recovery) {
2823                 default:
2824                 case SACK_RETRANS_RECOVERY:
2825                         from_seq = tcb->snd.nxt;
2826                         break;
2827                 case FAST_RETRANS_RECOVERY:
2828                 case RTO_RETRANS_RECOVERY:
2829                         from_seq = tcb->snd.rtx;
2830                         break;
2831                 }
2832                 sent = from_seq - tcb->snd.una;
2833                 /* qlen + flgcnt is every seq we want to have sent, including unack'd
2834                  * data, unacked flags, and new flags. */
2835                 ssize = qlen(s->wq) + tcb->flgcnt - sent;
2836         }
2837
2838         if (!throttle_ssize(s, tcb, &ssize, payload_mss, sack_retrans))
2839                 return FALSE;
2840
2841         /* This counts flags, which is a little hokey, but it's okay since in_flight
2842          * gets reset on each ACK */
2843         tcb->snd.in_flight += ssize;
2844         /* Log and track rxmit.  This covers both SACK (retrans) and fast rxmit. */
2845         if (ssize && seq_lt(tcb->snd.rtx, tcb->snd.nxt)) {
2846                 netlog(f, Logtcpverbose,
2847                        "%I.%d -> %I.%d: rxmit: rtx %u amt %u, nxt %u\n",
2848                        s->laddr, s->lport, s->raddr, s->rport,
2849                        tcb->snd.rtx, MIN(tcb->snd.nxt - tcb->snd.rtx, ssize),
2850                        tcb->snd.nxt);
2851                 tpriv->stats[RetransSegs]++;
2852         }
2853         if (sack_retrans) {
2854                 /* If we'll send up to the left edge, advance snd.rtx to the right.
2855                  *
2856                  * This includes the largest sack.  It might get removed later, in which
2857                  * case we'll underestimate the amount in-flight.  The alternative is to
2858                  * not count the rightmost sack, but when it gets removed, we'll retrans
2859                  * it anyway.  No matter what, we'd count it. */
2860                 tcb->snd.rtx += ssize;
2861                 if (tcb->snd.rtx == tcb_sack->left)
2862                         tcb->snd.rtx = tcb_sack->right;
2863                 /* RFC 6675 says we MAY rearm the RTO timer on each retrans, since we
2864                  * might not be getting ACKs for a while. */
2865                 tcpsettimer(tcb);
2866         } else {
2867                 switch (tcb->snd.recovery) {
2868                 default:
2869                         /* under normal op, we drag rtx along with nxt.  this prevents us
2870                          * from sending sacks too early (up above), since rtx doesn't get
2871                          * reset to una until we have a loss (e.g. 3 dupacks/sacks). */
2872                         tcb->snd.nxt += ssize;
2873                         tcb->snd.rtx = tcb->snd.nxt;
2874                         break;
2875                 case SACK_RETRANS_RECOVERY:
2876                         /* We explicitly do not want to increase rtx here.  We might still
2877                          * need it to fill in a sack gap below nxt if we get new, higher
2878                          * sacks. */
2879                         tcb->snd.nxt += ssize;
2880                         break;
2881                 case FAST_RETRANS_RECOVERY:
2882                 case RTO_RETRANS_RECOVERY:
2883                         tcb->snd.rtx += ssize;
2884                         /* Fast and RTO can send new data, advancing nxt. */
2885                         if (seq_gt(tcb->snd.rtx, tcb->snd.nxt))
2886                                 tcb->snd.nxt = tcb->snd.rtx;
2887                         break;
2888                 }
2889         }
2890         *from_seq_p = from_seq;
2891         *sent_p = sent;
2892         *ssize_p = ssize;
2893
2894         return TRUE;
2895 }
2896
2897 /*
2898  *  always enters and exits with the s locked.  We drop
2899  *  the lock to ipoput the packet so some care has to be
2900  *  taken by callers.
2901  */
2902 static void tcpoutput(struct conv *s)
2903 {
2904         Tcp seg;
2905         int msgs;
2906         int next_yield = 1;
2907         Tcpctl *tcb;
2908         struct block *hbp, *bp;
2909         uint32_t ssize, dsize, sent, from_seq;
2910         struct Fs *f;
2911         struct tcppriv *tpriv;
2912         uint8_t version;
2913         uint16_t payload_mss;
2914
2915         f = s->p->f;
2916         tpriv = s->p->priv;
2917         version = s->ipversion;
2918
2919         for (msgs = 0; msgs < 100; msgs++) {
2920                 tcb = (Tcpctl *) s->ptcl;
2921
2922                 switch (tcb->state) {
2923                         case Listen:
2924                         case Closed:
2925                         case Finwait2:
2926                                 return;
2927                 }
2928
2929                 /* force an ack when a window has opened up */
2930                 if (tcb->rcv.blocked && tcb->rcv.wnd >= tcb->mss) {
2931                         tcb->rcv.blocked = 0;
2932                         tcb->flags |= FORCE;
2933                 }
2934
2935                 /* Don't send anything else until our SYN has been acked */
2936                 if (tcb->snd.nxt != tcb->iss && (tcb->flags & SYNACK) == 0)
2937                         break;
2938
2939                 /* payload_mss is the actual amount of data in the packet, which is the
2940                  * advertised (mss - header opts).  This varies from packet to packet,
2941                  * based on the options that might be present (e.g. always timestamps,
2942                  * sometimes SACKs) */
2943                 payload_mss = derive_payload_mss(tcb);
2944
2945                 if (!get_xmit_segment(s, tcb, payload_mss, &from_seq, &sent, &ssize))
2946                         break;
2947
2948                 dsize = ssize;
2949                 seg.urg = 0;
2950
2951                 if (ssize == 0)
2952                         if ((tcb->flags & FORCE) == 0)
2953                                 break;
2954
2955                 tcb->flags &= ~FORCE;
2956                 tcprcvwin(s);
2957
2958                 /* By default we will generate an ack, so we can normally turn off the
2959                  * timer.  If we're blocked, we'll want the timer so we can send a
2960                  * window update. */
2961                 if (!tcb->rcv.blocked)
2962                         tcphalt(tpriv, &tcb->acktimer);
2963                 tcb->rcv.una = 0;
2964                 seg.source = s->lport;
2965                 seg.dest = s->rport;
2966                 seg.flags = ACK;
2967                 seg.mss = 0;
2968                 seg.ws = 0;
2969                 seg.sack_ok = FALSE;
2970                 seg.nr_sacks = 0;
2971                 /* When outputting, Syn_sent means "send the Syn", for connections we
2972                  * initiate.  SYNACKs are sent from sndsynack directly. */
2973                 if (tcb->state == Syn_sent) {
2974                         seg.flags = 0;
2975                         seg.sack_ok = SACK_SUPPORTED;   /* here's where we advertise SACK */
2976                         if (tcb->snd.nxt - ssize == tcb->iss) {
2977                                 seg.flags |= SYN;
2978                                 dsize--;
2979                                 seg.mss = tcb->mss;
2980                                 seg.ws = tcb->scale;
2981                         } else {
2982                                 /* TODO: Not sure why we'd get here. */
2983                                 warn("TCP: weird Syn_sent state, tell someone you saw this");
2984                         }
2985                 }
2986                 seg.seq = from_seq;
2987                 seg.ack = tcb->rcv.nxt;
2988                 tcb->last_ack_sent = seg.ack;
2989                 seg.wnd = tcb->rcv.wnd;
2990                 seg.ts_val = tcb->ts_recent;
2991
2992                 /* Pull out data to send */
2993                 bp = NULL;
2994                 if (dsize != 0) {
2995                         bp = qcopy(s->wq, dsize, sent);
2996                         if (BLEN(bp) != dsize) {
2997                                 /* Here's where the flgcnt kicked in.  Note dsize is
2998                                  * decremented, but ssize isn't.  Not that we use ssize for much
2999                                  * anymore.  Decrementing dsize prevents us from sending a PSH
3000                                  * with the FIN. */
3001                                 seg.flags |= FIN;
3002                                 dsize--;
3003                         }
3004                         if (BLEN(bp) > payload_mss) {
3005                                 bp->flag |= Btso;
3006                                 bp->mss = payload_mss;
3007                         }
3008                 }
3009
3010                 if (sent + dsize == qlen(s->wq) + tcb->flgcnt)
3011                         seg.flags |= PSH;
3012
3013                 /* Build header, link data and compute cksum */
3014                 switch (version) {
3015                         case V4:
3016                                 tcb->protohdr.tcp4hdr.vihl = IP_VER4;
3017                                 hbp = htontcp4(&seg, bp, &tcb->protohdr.tcp4hdr, tcb);
3018                                 if (hbp == NULL) {
3019                                         freeblist(bp);
3020                                         return;
3021                                 }
3022                                 break;
3023                         case V6:
3024                                 tcb->protohdr.tcp6hdr.vcf[0] = IP_VER6;
3025                                 hbp = htontcp6(&seg, bp, &tcb->protohdr.tcp6hdr, tcb);
3026                                 if (hbp == NULL) {
3027                                         freeblist(bp);
3028                                         return;
3029                                 }
3030                                 break;
3031                         default:
3032                                 hbp = NULL;     /* to suppress a warning */
3033                                 panic("tcpoutput: version %d", version);
3034                 }
3035
3036                 /* Start the transmission timers if there is new data and we
3037                  * expect acknowledges
3038                  */
3039                 if (ssize != 0) {
3040                         if (tcb->timer.state != TcptimerON)
3041                                 tcpgo(tpriv, &tcb->timer);
3042
3043                         if (!tcb->ts_recent && (tcb->rtt_timer.state != TcptimerON)) {
3044                                 /* If round trip timer isn't running, start it. */
3045                                 tcpgo(tpriv, &tcb->rtt_timer);
3046                                 tcb->rttseq = from_seq + ssize;
3047                         }
3048                 }
3049
3050                 tpriv->stats[OutSegs]++;
3051
3052                 /* put off the next keep alive */
3053                 tcpgo(tpriv, &tcb->katimer);
3054
3055                 switch (version) {
3056                         case V4:
3057                                 if (ipoput4(f, hbp, 0, s->ttl, s->tos, s) < 0) {
3058                                         /* a negative return means no route */
3059                                         localclose(s, "no route");
3060                                 }
3061                                 break;
3062                         case V6:
3063                                 if (ipoput6(f, hbp, 0, s->ttl, s->tos, s) < 0) {
3064                                         /* a negative return means no route */
3065                                         localclose(s, "no route");
3066                                 }
3067                                 break;
3068                         default:
3069                                 panic("tcpoutput2: version %d", version);
3070                 }
3071                 if (ssize) {
3072                         /* The outer loop thinks we sent one packet.  If we used TSO, we
3073                          * might have sent several.  Minus one for the loop increment. */
3074                         msgs += DIV_ROUND_UP(ssize, payload_mss) - 1;
3075                 }
3076                 /* Old Plan 9 tidbit - yield every four messages.  We want to break out
3077                  * and unlock so we can process inbound ACKs which might do things like
3078                  * say "slow down". */
3079                 if (msgs >= next_yield) {
3080                         next_yield = msgs + 4;
3081                         qunlock(&s->qlock);
3082                         kthread_yield();
3083                         qlock(&s->qlock);
3084                 }
3085         }
3086 }
3087
3088 /*
3089  *  the BSD convention (hack?) for keep alives.  resend last uint8_t acked.
3090  */
3091 static void tcpsendka(struct conv *s)
3092 {
3093         Tcp seg;
3094         Tcpctl *tcb;
3095         struct block *hbp, *dbp;
3096
3097         tcb = (Tcpctl *) s->ptcl;
3098
3099         dbp = NULL;
3100         seg.urg = 0;
3101         seg.source = s->lport;
3102         seg.dest = s->rport;
3103         seg.flags = ACK | PSH;
3104         seg.mss = 0;
3105         seg.ws = 0;
3106         seg.sack_ok = FALSE;
3107         seg.nr_sacks = 0;
3108         if (tcpporthogdefense)
3109                 urandom_read(&seg.seq, sizeof(seg.seq));
3110         else
3111                 seg.seq = tcb->snd.una - 1;
3112         seg.ack = tcb->rcv.nxt;
3113         tcb->last_ack_sent = seg.ack;
3114         tcb->rcv.una = 0;
3115         seg.wnd = tcb->rcv.wnd;
3116         seg.ts_val = tcb->ts_recent;
3117         if (tcb->state == Finwait2) {
3118                 seg.flags |= FIN;
3119         } else {
3120                 dbp = block_alloc(1, MEM_WAIT);
3121                 dbp->wp++;
3122         }
3123
3124         if (isv4(s->raddr)) {
3125                 /* Build header, link data and compute cksum */
3126                 tcb->protohdr.tcp4hdr.vihl = IP_VER4;
3127                 hbp = htontcp4(&seg, dbp, &tcb->protohdr.tcp4hdr, tcb);
3128                 if (hbp == NULL) {
3129                         freeblist(dbp);
3130                         return;
3131                 }
3132                 ipoput4(s->p->f, hbp, 0, s->ttl, s->tos, s);
3133         } else {
3134                 /* Build header, link data and compute cksum */
3135                 tcb->protohdr.tcp6hdr.vcf[0] = IP_VER6;
3136                 hbp = htontcp6(&seg, dbp, &tcb->protohdr.tcp6hdr, tcb);
3137                 if (hbp == NULL) {
3138                         freeblist(dbp);
3139                         return;
3140                 }
3141                 ipoput6(s->p->f, hbp, 0, s->ttl, s->tos, s);
3142         }
3143 }
3144
3145 /*
3146  *  set connection to time out after 12 minutes
3147  */
3148 static void tcpsetkacounter(Tcpctl *tcb)
3149 {
3150         tcb->kacounter = (12 * 60 * 1000) / (tcb->katimer.start * MSPTICK);
3151         if (tcb->kacounter < 3)
3152                 tcb->kacounter = 3;
3153 }
3154
3155 /*
3156  *  if we've timed out, close the connection
3157  *  otherwise, send a keepalive and restart the timer
3158  */
3159 static void tcpkeepalive(void *v)
3160 {
3161         ERRSTACK(1);
3162         Tcpctl *tcb;
3163         struct conv *s;
3164
3165         s = v;
3166         tcb = (Tcpctl *) s->ptcl;
3167         qlock(&s->qlock);
3168         if (waserror()) {
3169                 qunlock(&s->qlock);
3170                 nexterror();
3171         }
3172         if (tcb->state != Closed) {
3173                 if (--(tcb->kacounter) <= 0) {
3174                         localclose(s, "connection timed out");
3175                 } else {
3176                         tcpsendka(s);
3177                         tcpgo(s->p->priv, &tcb->katimer);
3178                 }
3179         }
3180         qunlock(&s->qlock);
3181         poperror();
3182 }
3183
3184 /*
3185  *  start keepalive timer
3186  */
3187 static void tcpstartka(struct conv *s, char **f, int n)
3188 {
3189         Tcpctl *tcb;
3190         int x;
3191
3192         tcb = (Tcpctl *) s->ptcl;
3193         if (tcb->state != Established)
3194                 error(ENOTCONN, "connection must be in Establised state");
3195         if (n > 1) {
3196                 x = atoi(f[1]);
3197                 if (x >= MSPTICK)
3198                         tcb->katimer.start = x / MSPTICK;
3199         }
3200         tcpsetkacounter(tcb);
3201         tcpgo(s->p->priv, &tcb->katimer);
3202 }
3203
3204 /*
3205  *  turn checksums on/off
3206  */
3207 static void tcpsetchecksum(struct conv *s, char **f, int unused)
3208 {
3209         Tcpctl *tcb;
3210
3211         tcb = (Tcpctl *) s->ptcl;
3212         tcb->nochecksum = !atoi(f[1]);
3213 }
3214
3215 static void tcp_loss_event(struct conv *s, Tcpctl *tcb)
3216 {
3217         uint32_t old_cwnd = tcb->cwind;
3218
3219         /* Reno */
3220         tcb->ssthresh = tcb->cwind / 2;
3221         tcb->cwind = tcb->ssthresh;
3222         netlog(s->p->f, Logtcprxmt,
3223                "%I.%d -> %I.%d: loss event, cwnd was %d, now %d\n",
3224                s->laddr, s->lport, s->raddr, s->rport,
3225                old_cwnd, tcb->cwind);
3226 }
3227
3228 /* Called when we need to retrans the entire outstanding window (everything
3229  * previously sent, but unacknowledged). */
3230 static void tcprxmit(struct conv *s)
3231 {
3232         Tcpctl *tcb;
3233
3234         tcb = (Tcpctl *) s->ptcl;
3235
3236         tcb->flags |= FORCE;
3237         tcb->snd.rtx = tcb->snd.una;
3238         set_in_flight(tcb);
3239
3240         tcpoutput(s);
3241 }
3242
3243 /* The original RFC said to drop sacks on a timeout, since the receiver could
3244  * renege.  Later RFCs say we can keep them around, so long as we are careful.
3245  *
3246  * We'll go with a "flush if we have two timeouts" plan.  This doesn't have to
3247  * be perfect - there might be cases where we accidentally flush the sacks too
3248  * often.  Perhaps we never get dup_acks to start fast/sack rxmit.  The main
3249  * thing is that after multiple timeouts we flush the sacks, since the receiver
3250  * might renege.
3251  *
3252  * We also have an Akaros-specific problem.  We use the sacks to determine
3253  * in_flight.  Specifically, the (snd.nxt - upper right edge) is tracked as in
3254  * flight.  Usually the receiver will keep sacking that right edge all the way
3255  * up to snd.nxt, but they might not, and the gap might be quite large.  After a
3256  * timeout, that data is definitely not in flight.  If that block's size is
3257  * greater than cwnd, we'll never transmit.  This should be rare, and in that
3258  * case we can just dump the sacks.  The typical_mss fudge factor is so we can
3259  * send a reasonably-sized packet. */
3260 static void timeout_handle_sacks(Tcpctl *tcb)
3261 {
3262         struct sack_block *last_sack;
3263
3264         if (tcb->snd.nr_sacks) {
3265                 last_sack = &tcb->snd.sacks[tcb->snd.nr_sacks - 1];
3266                 if (tcb->snd.flush_sacks || (tcb->snd.nxt - last_sack->right >=
3267                                              tcb->cwind - tcb->typical_mss)) {
3268                         tcb->snd.nr_sacks = 0;
3269                         tcb->snd.flush_sacks = FALSE;
3270                 } else {
3271                         tcb->snd.flush_sacks = TRUE;
3272                 }
3273         }
3274 }
3275
3276 static void tcptimeout(void *arg)
3277 {
3278         ERRSTACK(1);
3279         struct conv *s;
3280         Tcpctl *tcb;
3281         int maxback;
3282         struct tcppriv *tpriv;
3283
3284         s = (struct conv *)arg;
3285         tpriv = s->p->priv;
3286         tcb = (Tcpctl *) s->ptcl;
3287
3288         qlock(&s->qlock);
3289         if (waserror()) {
3290                 qunlock(&s->qlock);
3291                 nexterror();
3292         }
3293         switch (tcb->state) {
3294                 default:
3295                         tcb->backoff++;
3296                         if (tcb->state == Syn_sent)
3297                                 maxback = MAXBACKMS / 2;
3298                         else
3299                                 maxback = MAXBACKMS;
3300                         tcb->backedoff += tcb->timer.start * MSPTICK;
3301                         if (tcb->backedoff >= maxback) {
3302                                 localclose(s, "connection timed out");
3303                                 break;
3304                         }
3305                         netlog(s->p->f, Logtcprxmt,
3306                                "%I.%d -> %I.%d: timeout rxmit una %u, rtx %u, nxt %u, in_flight %u, timer.start %u\n",
3307                                s->laddr, s->lport, s->raddr, s->rport,
3308                                tcb->snd.una, tcb->snd.rtx, tcb->snd.nxt, tcb->snd.in_flight,
3309                                tcb->timer.start);
3310                         tcpsettimer(tcb);
3311                         tcp_loss_event(s, tcb);
3312                         /* Advance the recovery point.  Any dupacks/sacks below this won't
3313                          * trigger a new loss, since we won't reset_recovery() until we ack
3314                          * past recovery_pt. */
3315                         tcb->snd.recovery = RTO_RETRANS_RECOVERY;
3316                         tcb->snd.recovery_pt = tcb->snd.nxt;
3317                         timeout_handle_sacks(tcb);
3318                         tcprxmit(s);
3319                         tpriv->stats[RetransTimeouts]++;
3320                         break;
3321                 case Time_wait:
3322                         localclose(s, NULL);
3323                         break;
3324                 case Closed:
3325                         break;
3326         }
3327         qunlock(&s->qlock);
3328         poperror();
3329 }
3330
3331 static int inwindow(Tcpctl *tcb, int seq)
3332 {
3333         return seq_within(seq, tcb->rcv.nxt, tcb->rcv.nxt + tcb->rcv.wnd - 1);
3334 }
3335
3336 /*
3337  *  set up state for a received SYN (or SYN ACK) packet
3338  */
3339 static void procsyn(struct conv *s, Tcp *seg)
3340 {
3341         Tcpctl *tcb;
3342
3343         tcb = (Tcpctl *) s->ptcl;
3344         tcb->flags |= FORCE;
3345
3346         tcb->rcv.nxt = seg->seq + 1;
3347         tcb->rcv.urg = tcb->rcv.nxt;
3348         tcb->irs = seg->seq;
3349
3350         /* our sending max segment size cannot be bigger than what he asked for */
3351         if (seg->mss != 0 && seg->mss < tcb->mss) {
3352                 tcb->mss = seg->mss;
3353                 tcb->typical_mss = tcb->mss;
3354         }
3355         adjust_typical_mss_for_opts(seg, tcb);
3356
3357         tcb->snd.wnd = seg->wnd;
3358         tcb->cwind = tcb->typical_mss * CWIND_SCALE;
3359 }
3360
3361 static int addreseq(Tcpctl *tcb, struct tcppriv *tpriv, Tcp *seg,
3362                     struct block *bp, uint16_t length)
3363 {
3364         Reseq *rp, *rp1;
3365         int i, rqlen, qmax;
3366
3367         rp = kzmalloc(sizeof(Reseq), 0);
3368         if (rp == NULL) {
3369                 freeblist(bp);  /* bp always consumed by add_reseq */
3370                 return 0;
3371         }
3372
3373         rp->seg = *seg;
3374         rp->bp = bp;
3375         rp->length = length;
3376
3377         track_rcv_sack(tcb, seg->seq, seg->seq + length);
3378         /* Place on reassembly list sorting by starting seq number */
3379         rp1 = tcb->reseq;
3380         if (rp1 == NULL || seq_lt(seg->seq, rp1->seg.seq)) {
3381                 rp->next = rp1;
3382                 tcb->reseq = rp;
3383                 if (rp->next != NULL)
3384                         tpriv->stats[OutOfOrder]++;
3385                 return 0;
3386         }
3387
3388         rqlen = 0;
3389         for (i = 0;; i++) {
3390                 rqlen += rp1->length;
3391                 if (rp1->next == NULL || seq_lt(seg->seq, rp1->next->seg.seq)) {
3392                         rp->next = rp1->next;
3393                         rp1->next = rp;
3394                         if (rp->next != NULL)
3395                                 tpriv->stats[OutOfOrder]++;
3396                         break;
3397                 }
3398                 rp1 = rp1->next;
3399         }
3400         qmax = QMAX << tcb->rcv.scale;
3401         /* Here's where we're reneging on previously reported sacks. */
3402         if (rqlen > qmax) {
3403                 printd("resequence queue > window: %d > %d\n", rqlen, qmax);
3404                 i = 0;
3405                 for (rp1 = tcb->reseq; rp1 != NULL; rp1 = rp1->next) {
3406                         printd("0x%#lx 0x%#lx 0x%#x\n", rp1->seg.seq,
3407                                    rp1->seg.ack, rp1->seg.flags);
3408                         if (i++ > 10) {
3409                                 printd("...\n");
3410                                 break;
3411                         }
3412                 }
3413
3414                 // delete entire reassembly queue; wait for retransmit.
3415                 // - should we be smarter and only delete the tail?
3416                 for (rp = tcb->reseq; rp != NULL; rp = rp1) {
3417                         rp1 = rp->next;
3418                         freeblist(rp->bp);
3419                         kfree(rp);
3420                 }
3421                 tcb->reseq = NULL;
3422                 tcb->rcv.nr_sacks = 0;
3423
3424                 return -1;
3425         }
3426         return 0;
3427 }
3428
3429 static void getreseq(Tcpctl *tcb, Tcp *seg, struct block **bp, uint16_t *length)
3430 {
3431         Reseq *rp;
3432
3433         rp = tcb->reseq;
3434         if (rp == NULL)
3435                 return;
3436
3437         tcb->reseq = rp->next;
3438
3439         *seg = rp->seg;
3440         *bp = rp->bp;
3441         *length = rp->length;
3442
3443         kfree(rp);
3444 }
3445
3446 static int tcptrim(Tcpctl *tcb, Tcp *seg, struct block **bp, uint16_t *length)
3447 {
3448         uint16_t len;
3449         uint8_t accept;
3450         int dupcnt, excess;
3451
3452         accept = 0;
3453         len = *length;
3454         if (seg->flags & SYN)
3455                 len++;
3456         if (seg->flags & FIN)
3457                 len++;
3458
3459         if (tcb->rcv.wnd == 0) {
3460                 if (len == 0 && seg->seq == tcb->rcv.nxt)
3461                         return 0;
3462         } else {
3463                 /* Some part of the segment should be in the window */
3464                 if (inwindow(tcb, seg->seq))
3465                         accept++;
3466                 else if (len != 0) {
3467                         if (inwindow(tcb, seg->seq + len - 1) ||
3468                                 seq_within(tcb->rcv.nxt, seg->seq, seg->seq + len - 1))
3469                                 accept++;
3470                 }
3471         }
3472         if (!accept) {
3473                 freeblist(*bp);
3474                 return -1;
3475         }
3476         dupcnt = tcb->rcv.nxt - seg->seq;
3477         if (dupcnt > 0) {
3478                 tcb->rerecv += dupcnt;
3479                 if (seg->flags & SYN) {
3480                         seg->flags &= ~SYN;
3481                         seg->seq++;
3482
3483                         if (seg->urg > 1)
3484                                 seg->urg--;
3485                         else
3486                                 seg->flags &= ~URG;
3487                         dupcnt--;
3488                 }
3489                 if (dupcnt > 0) {
3490                         pullblock(bp, (uint16_t) dupcnt);
3491                         seg->seq += dupcnt;
3492                         *length -= dupcnt;
3493
3494                         if (seg->urg > dupcnt)
3495                                 seg->urg -= dupcnt;
3496                         else {
3497                                 seg->flags &= ~URG;
3498                                 seg->urg = 0;
3499                         }
3500                 }
3501         }
3502         excess = seg->seq + *length - (tcb->rcv.nxt + tcb->rcv.wnd);
3503         if (excess > 0) {
3504                 tcb->rerecv += excess;
3505                 *length -= excess;
3506                 *bp = trimblock(*bp, 0, *length);
3507                 if (*bp == NULL)
3508                         panic("presotto is a boofhead");
3509                 seg->flags &= ~FIN;
3510         }
3511         return 0;
3512 }
3513
3514 static void tcpadvise(struct Proto *tcp, struct block *bp, char *msg)
3515 {
3516         Tcp4hdr *h4;
3517         Tcp6hdr *h6;
3518         Tcpctl *tcb;
3519         uint8_t source[IPaddrlen];
3520         uint8_t dest[IPaddrlen];
3521         uint16_t psource, pdest;
3522         struct conv *s, **p;
3523
3524         h4 = (Tcp4hdr *) (bp->rp);
3525         h6 = (Tcp6hdr *) (bp->rp);
3526
3527         if ((h4->vihl & 0xF0) == IP_VER4) {
3528                 v4tov6(dest, h4->tcpdst);
3529                 v4tov6(source, h4->tcpsrc);
3530                 psource = nhgets(h4->tcpsport);
3531                 pdest = nhgets(h4->tcpdport);
3532         } else {
3533                 ipmove(dest, h6->tcpdst);
3534                 ipmove(source, h6->tcpsrc);
3535                 psource = nhgets(h6->tcpsport);
3536                 pdest = nhgets(h6->tcpdport);
3537         }
3538
3539         /* Look for a connection */
3540         for (p = tcp->conv; *p; p++) {
3541                 s = *p;
3542                 tcb = (Tcpctl *) s->ptcl;
3543                 if (s->rport == pdest)
3544                         if (s->lport == psource)
3545                                 if (tcb->state != Closed)
3546                                         if (ipcmp(s->raddr, dest) == 0)
3547                                                 if (ipcmp(s->laddr, source) == 0) {
3548                                                         qlock(&s->qlock);
3549                                                         switch (tcb->state) {
3550                                                                 case Syn_sent:
3551                                                                         localclose(s, msg);
3552                                                                         break;
3553                                                         }
3554                                                         qunlock(&s->qlock);
3555                                                         freeblist(bp);
3556                                                         return;
3557                                                 }
3558         }
3559         freeblist(bp);
3560 }
3561
3562 static void tcpporthogdefensectl(char *val)
3563 {
3564         if (strcmp(val, "on") == 0)
3565                 tcpporthogdefense = 1;
3566         else if (strcmp(val, "off") == 0)
3567                 tcpporthogdefense = 0;
3568         else
3569                 error(EINVAL, "unknown value for tcpporthogdefense");
3570 }
3571
3572 /* called with c qlocked */
3573 static void tcpctl(struct conv *c, char **f, int n)
3574 {
3575         if (n == 1 && strcmp(f[0], "hangup") == 0)
3576                 tcphangup(c);
3577         else if (n >= 1 && strcmp(f[0], "keepalive") == 0)
3578                 tcpstartka(c, f, n);
3579         else if (n >= 1 && strcmp(f[0], "checksum") == 0)
3580                 tcpsetchecksum(c, f, n);
3581         else if (n >= 1 && strcmp(f[0], "tcpporthogdefense") == 0)
3582                 tcpporthogdefensectl(f[1]);
3583         else
3584                 error(EINVAL, "unknown command to %s", __func__);
3585 }
3586
3587 static int tcpstats(struct Proto *tcp, char *buf, int len)
3588 {
3589         struct tcppriv *priv;
3590         char *p, *e;
3591         int i;
3592
3593         priv = tcp->priv;
3594         p = buf;
3595         e = p + len;
3596         for (i = 0; i < Nstats; i++)
3597                 p = seprintf(p, e, "%s: %u\n", statnames[i], priv->stats[i]);
3598         return p - buf;
3599 }
3600
3601 /*
3602  *  garbage collect any stale conversations:
3603  *      - SYN received but no SYN-ACK after 5 seconds (could be the SYN attack)
3604  *      - Finwait2 after 5 minutes
3605  *
3606  *  this is called whenever we run out of channels.  Both checks are
3607  *  of questionable validity so we try to use them only when we're
3608  *  up against the wall.
3609  */
3610 static int tcpgc(struct Proto *tcp)
3611 {
3612         struct conv *c, **pp, **ep;
3613         int n;
3614         Tcpctl *tcb;
3615
3616         n = 0;
3617         ep = &tcp->conv[tcp->nc];
3618         for (pp = tcp->conv; pp < ep; pp++) {
3619                 c = *pp;
3620                 if (c == NULL)
3621                         break;
3622                 if (!canqlock(&c->qlock))
3623                         continue;
3624                 tcb = (Tcpctl *) c->ptcl;
3625                 if (tcb->state == Finwait2) {
3626                         if (NOW - tcb->time > 5 * 60 * 1000) {
3627                                 localclose(c, "timed out");
3628                                 n++;
3629                         }