1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include "slirp.h"
40#include "ip_icmp.h"
41
42#define TCPREXMTTHRESH 3
43
44#define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
45
46
47#define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
48#define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
49
50
51
52
53
54
55
56
57
58
59
60#define TCP_REASS(tp, ti, m, so, flags) { \
61 if ((ti)->ti_seq == (tp)->rcv_nxt && \
62 tcpfrag_list_empty(tp) && \
63 (tp)->t_state == TCPS_ESTABLISHED) { \
64 tp->t_flags |= TF_DELACK; \
65 (tp)->rcv_nxt += (ti)->ti_len; \
66 flags = (ti)->ti_flags & TH_FIN; \
67 if (so->so_emu) { \
68 if (tcp_emu((so),(m))) sbappend(so, (m)); \
69 } else \
70 sbappend((so), (m)); \
71 } else { \
72 (flags) = tcp_reass((tp), (ti), (m)); \
73 tp->t_flags |= TF_ACKNOW; \
74 } \
75}
76
77static void tcp_dooptions(struct tcpcb *tp, uint8_t *cp, int cnt,
78 struct tcpiphdr *ti);
79static void tcp_xmit_timer(register struct tcpcb *tp, int rtt);
80
81static int
82tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti,
83 struct mbuf *m)
84{
85 register struct tcpiphdr *q;
86 struct socket *so = tp->t_socket;
87 int flags;
88
89
90
91
92
93 if (ti == NULL)
94 goto present;
95
96
97
98
99 for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp);
100 q = tcpiphdr_next(q))
101 if (SEQ_GT(q->ti_seq, ti->ti_seq))
102 break;
103
104
105
106
107
108
109 if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) {
110 register int i;
111 q = tcpiphdr_prev(q);
112
113 i = q->ti_seq + q->ti_len - ti->ti_seq;
114 if (i > 0) {
115 if (i >= ti->ti_len) {
116 m_free(m);
117
118
119
120
121
122
123 goto present;
124 }
125 m_adj(m, i);
126 ti->ti_len -= i;
127 ti->ti_seq += i;
128 }
129 q = tcpiphdr_next(q);
130 }
131 ti->ti_mbuf = m;
132
133
134
135
136
137 while (!tcpfrag_list_end(q, tp)) {
138 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
139 if (i <= 0)
140 break;
141 if (i < q->ti_len) {
142 q->ti_seq += i;
143 q->ti_len -= i;
144 m_adj(q->ti_mbuf, i);
145 break;
146 }
147 q = tcpiphdr_next(q);
148 m = tcpiphdr_prev(q)->ti_mbuf;
149 remque(tcpiphdr2qlink(tcpiphdr_prev(q)));
150 m_free(m);
151 }
152
153
154
155
156 insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q)));
157
158present:
159
160
161
162
163 if (!TCPS_HAVEESTABLISHED(tp->t_state))
164 return (0);
165 ti = tcpfrag_list_first(tp);
166 if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt)
167 return (0);
168 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
169 return (0);
170 do {
171 tp->rcv_nxt += ti->ti_len;
172 flags = ti->ti_flags & TH_FIN;
173 remque(tcpiphdr2qlink(ti));
174 m = ti->ti_mbuf;
175 ti = tcpiphdr_next(ti);
176 if (so->so_state & SS_FCANTSENDMORE)
177 m_free(m);
178 else {
179 if (so->so_emu) {
180 if (tcp_emu(so,m)) sbappend(so, m);
181 } else
182 sbappend(so, m);
183 }
184 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
185 return (flags);
186}
187
188
189
190
191
192void
193tcp_input(struct mbuf *m, int iphlen, struct socket *inso, unsigned short af)
194{
195 struct ip save_ip, *ip;
196 struct ip6 save_ip6, *ip6;
197 register struct tcpiphdr *ti;
198 char *optp = NULL;
199 int optlen = 0;
200 int len, tlen, off;
201 register struct tcpcb *tp = NULL;
202 register int tiflags;
203 struct socket *so = NULL;
204 int todrop, acked, ourfinisacked, needoutput = 0;
205 int iss = 0;
206 uint32_t tiwin;
207 int ret;
208 struct sockaddr_storage lhost, fhost;
209 struct sockaddr_in *lhost4, *fhost4;
210 struct sockaddr_in6 *lhost6, *fhost6;
211 struct gfwd_list *ex_ptr;
212 Slirp *slirp;
213
214 DEBUG_CALL("tcp_input");
215 DEBUG_ARG("m = %p iphlen = %2d inso = %p",
216 m, iphlen, inso);
217
218
219
220
221 if (m == NULL) {
222 so = inso;
223 slirp = so->slirp;
224
225
226 tp = sototcpcb(so);
227 m = so->so_m;
228 so->so_m = NULL;
229 ti = so->so_ti;
230 tiwin = ti->ti_win;
231 tiflags = ti->ti_flags;
232
233 goto cont_conn;
234 }
235 slirp = m->slirp;
236
237 ip = mtod(m, struct ip *);
238 ip6 = mtod(m, struct ip6 *);
239
240 switch (af) {
241 case AF_INET:
242 if (iphlen > sizeof(struct ip)) {
243 ip_stripoptions(m, (struct mbuf *)0);
244 iphlen = sizeof(struct ip);
245 }
246
247
248
249
250
251
252
253 save_ip = *ip;
254 save_ip.ip_len += iphlen;
255
256
257
258
259
260 m->m_data -= sizeof(struct tcpiphdr) - sizeof(struct ip)
261 - sizeof(struct tcphdr);
262 m->m_len += sizeof(struct tcpiphdr) - sizeof(struct ip)
263 - sizeof(struct tcphdr);
264 ti = mtod(m, struct tcpiphdr *);
265
266
267
268
269 tlen = ip->ip_len;
270 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
271 memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
272 memset(&ti->ti, 0, sizeof(ti->ti));
273 ti->ti_x0 = 0;
274 ti->ti_src = save_ip.ip_src;
275 ti->ti_dst = save_ip.ip_dst;
276 ti->ti_pr = save_ip.ip_p;
277 ti->ti_len = htons((uint16_t)tlen);
278 break;
279
280 case AF_INET6:
281
282
283
284
285 save_ip6 = *ip6;
286
287
288
289
290 m->m_data -= sizeof(struct tcpiphdr) - (sizeof(struct ip6)
291 + sizeof(struct tcphdr));
292 m->m_len += sizeof(struct tcpiphdr) - (sizeof(struct ip6)
293 + sizeof(struct tcphdr));
294 ti = mtod(m, struct tcpiphdr *);
295
296 tlen = ip6->ip_pl;
297 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
298 memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
299 memset(&ti->ti, 0, sizeof(ti->ti));
300 ti->ti_x0 = 0;
301 ti->ti_src6 = save_ip6.ip_src;
302 ti->ti_dst6 = save_ip6.ip_dst;
303 ti->ti_nh6 = save_ip6.ip_nh;
304 ti->ti_len = htons((uint16_t)tlen);
305 break;
306
307 default:
308 g_assert_not_reached();
309 }
310
311 len = ((sizeof(struct tcpiphdr) - sizeof(struct tcphdr)) + tlen);
312 if (cksum(m, len)) {
313 goto drop;
314 }
315
316
317
318
319
320 off = ti->ti_off << 2;
321 if (off < sizeof (struct tcphdr) || off > tlen) {
322 goto drop;
323 }
324 tlen -= off;
325 ti->ti_len = tlen;
326 if (off > sizeof (struct tcphdr)) {
327 optlen = off - sizeof (struct tcphdr);
328 optp = mtod(m, char *) + sizeof (struct tcpiphdr);
329 }
330 tiflags = ti->ti_flags;
331
332
333
334
335 NTOHL(ti->ti_seq);
336 NTOHL(ti->ti_ack);
337 NTOHS(ti->ti_win);
338 NTOHS(ti->ti_urp);
339
340
341
342
343 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
344 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
345
346
347
348
349findso:
350 lhost.ss_family = af;
351 fhost.ss_family = af;
352 switch (af) {
353 case AF_INET:
354 lhost4 = (struct sockaddr_in *) &lhost;
355 lhost4->sin_addr = ti->ti_src;
356 lhost4->sin_port = ti->ti_sport;
357 fhost4 = (struct sockaddr_in *) &fhost;
358 fhost4->sin_addr = ti->ti_dst;
359 fhost4->sin_port = ti->ti_dport;
360 break;
361 case AF_INET6:
362 lhost6 = (struct sockaddr_in6 *) &lhost;
363 lhost6->sin6_addr = ti->ti_src6;
364 lhost6->sin6_port = ti->ti_sport;
365 fhost6 = (struct sockaddr_in6 *) &fhost;
366 fhost6->sin6_addr = ti->ti_dst6;
367 fhost6->sin6_port = ti->ti_dport;
368 break;
369 default:
370 g_assert_not_reached();
371 }
372
373 so = solookup(&slirp->tcp_last_so, &slirp->tcb, &lhost, &fhost);
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388 if (so == NULL) {
389
390 if (slirp->restricted) {
391
392
393
394
395 for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
396 if (ex_ptr->ex_fport == ti->ti_dport &&
397 ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
398 break;
399 }
400 }
401 if (!ex_ptr) {
402 goto dropwithreset;
403 }
404 }
405
406 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
407 goto dropwithreset;
408
409 so = socreate(slirp);
410 if (tcp_attach(so) < 0) {
411 g_free(so);
412 goto dropwithreset;
413 }
414
415 sbreserve(&so->so_snd, TCP_SNDSPACE);
416 sbreserve(&so->so_rcv, TCP_RCVSPACE);
417
418 so->lhost.ss = lhost;
419 so->fhost.ss = fhost;
420
421 so->so_iptos = tcp_tos(so);
422 if (so->so_iptos == 0) {
423 switch (af) {
424 case AF_INET:
425 so->so_iptos = ((struct ip *)ti)->ip_tos;
426 break;
427 case AF_INET6:
428 break;
429 default:
430 g_assert_not_reached();
431 }
432 }
433
434 tp = sototcpcb(so);
435 tp->t_state = TCPS_LISTEN;
436 }
437
438
439
440
441
442
443 if (so->so_state & SS_ISFCONNECTING)
444 goto drop;
445
446 tp = sototcpcb(so);
447
448
449 if (tp == NULL)
450 goto dropwithreset;
451 if (tp->t_state == TCPS_CLOSED)
452 goto drop;
453
454 tiwin = ti->ti_win;
455
456
457
458
459
460 tp->t_idle = 0;
461 if (slirp_do_keepalive)
462 tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
463 else
464 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
465
466
467
468
469
470 if (optp && tp->t_state != TCPS_LISTEN)
471 tcp_dooptions(tp, (uint8_t *)optp, optlen, ti);
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491 if (tp->t_state == TCPS_ESTABLISHED &&
492 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
493 ti->ti_seq == tp->rcv_nxt &&
494 tiwin && tiwin == tp->snd_wnd &&
495 tp->snd_nxt == tp->snd_max) {
496 if (ti->ti_len == 0) {
497 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
498 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
499 tp->snd_cwnd >= tp->snd_wnd) {
500
501
502
503 if (tp->t_rtt &&
504 SEQ_GT(ti->ti_ack, tp->t_rtseq))
505 tcp_xmit_timer(tp, tp->t_rtt);
506 acked = ti->ti_ack - tp->snd_una;
507 sodrop(so, acked);
508 tp->snd_una = ti->ti_ack;
509 m_free(m);
510
511
512
513
514
515
516
517
518
519
520 if (tp->snd_una == tp->snd_max)
521 tp->t_timer[TCPT_REXMT] = 0;
522 else if (tp->t_timer[TCPT_PERSIST] == 0)
523 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
524
525
526
527
528
529
530 if (so->so_snd.sb_cc)
531 (void) tcp_output(tp);
532
533 return;
534 }
535 } else if (ti->ti_ack == tp->snd_una &&
536 tcpfrag_list_empty(tp) &&
537 ti->ti_len <= sbspace(&so->so_rcv)) {
538
539
540
541
542
543 tp->rcv_nxt += ti->ti_len;
544
545
546
547 if (so->so_emu) {
548 if (tcp_emu(so,m)) sbappend(so, m);
549 } else
550 sbappend(so, m);
551
552
553
554
555
556
557
558
559
560 tp->t_flags |= TF_ACKNOW;
561 tcp_output(tp);
562 return;
563 }
564 }
565
566
567
568
569
570
571 { int win;
572 win = sbspace(&so->so_rcv);
573 if (win < 0)
574 win = 0;
575 tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
576 }
577
578 switch (tp->t_state) {
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593 case TCPS_LISTEN: {
594
595 if (tiflags & TH_RST)
596 goto drop;
597 if (tiflags & TH_ACK)
598 goto dropwithreset;
599 if ((tiflags & TH_SYN) == 0)
600 goto drop;
601
602
603
604
605
606
607
608
609
610
611
612 if (af == AF_INET &&
613 (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
614 slirp->vnetwork_addr.s_addr) {
615 if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr &&
616 so->so_faddr.s_addr != slirp->vnameserver_addr.s_addr) {
617
618 for (ex_ptr = slirp->guestfwd_list; ex_ptr;
619 ex_ptr = ex_ptr->ex_next) {
620 if(ex_ptr->ex_fport == so->so_fport &&
621 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
622 so->so_state |= SS_CTL;
623 break;
624 }
625 }
626 if (so->so_state & SS_CTL) {
627 goto cont_input;
628 }
629 }
630
631 }
632
633 if (so->so_emu & EMU_NOCONNECT) {
634 so->so_emu &= ~EMU_NOCONNECT;
635 goto cont_input;
636 }
637
638 if ((tcp_fconnect(so, so->so_ffamily) == -1) &&
639 (errno != EAGAIN) &&
640 (errno != EINPROGRESS) && (errno != EWOULDBLOCK)
641 ) {
642 uint8_t code;
643 DEBUG_MISC(" tcp fconnect errno = %d-%s", errno, strerror(errno));
644 if(errno == ECONNREFUSED) {
645
646 tcp_respond(tp, ti, m, ti->ti_seq + 1, (tcp_seq) 0,
647 TH_RST | TH_ACK, af);
648 } else {
649 switch (af) {
650 case AF_INET:
651 code = ICMP_UNREACH_NET;
652 if (errno == EHOSTUNREACH) {
653 code = ICMP_UNREACH_HOST;
654 }
655 break;
656 case AF_INET6:
657 code = ICMP6_UNREACH_NO_ROUTE;
658 if (errno == EHOSTUNREACH) {
659 code = ICMP6_UNREACH_ADDRESS;
660 }
661 break;
662 default:
663 g_assert_not_reached();
664 }
665 HTONL(ti->ti_seq);
666 HTONL(ti->ti_ack);
667 HTONS(ti->ti_win);
668 HTONS(ti->ti_urp);
669 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
670 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
671 switch (af) {
672 case AF_INET:
673 m->m_data += sizeof(struct tcpiphdr) - sizeof(struct ip)
674 - sizeof(struct tcphdr);
675 m->m_len -= sizeof(struct tcpiphdr) - sizeof(struct ip)
676 - sizeof(struct tcphdr);
677 *ip = save_ip;
678 icmp_send_error(m, ICMP_UNREACH, code, 0, strerror(errno));
679 break;
680 case AF_INET6:
681 m->m_data += sizeof(struct tcpiphdr) - (sizeof(struct ip6)
682 + sizeof(struct tcphdr));
683 m->m_len -= sizeof(struct tcpiphdr) - (sizeof(struct ip6)
684 + sizeof(struct tcphdr));
685 *ip6 = save_ip6;
686 icmp6_send_error(m, ICMP6_UNREACH, code);
687 break;
688 default:
689 g_assert_not_reached();
690 }
691 }
692 tcp_close(tp);
693 m_free(m);
694 } else {
695
696
697
698
699
700
701 so->so_m = m;
702 so->so_ti = ti;
703 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
704 tp->t_state = TCPS_SYN_RECEIVED;
705
706
707
708
709 tp->irs = ti->ti_seq;
710 tcp_rcvseqinit(tp);
711 tcp_template(tp);
712 }
713 return;
714
715 cont_conn:
716
717
718
719 if (so->so_state & SS_NOFDREF) {
720 tp = tcp_close(tp);
721 goto dropwithreset;
722 }
723 cont_input:
724 tcp_template(tp);
725
726 if (optp)
727 tcp_dooptions(tp, (uint8_t *)optp, optlen, ti);
728
729 if (iss)
730 tp->iss = iss;
731 else
732 tp->iss = slirp->tcp_iss;
733 slirp->tcp_iss += TCP_ISSINCR/2;
734 tp->irs = ti->ti_seq;
735 tcp_sendseqinit(tp);
736 tcp_rcvseqinit(tp);
737 tp->t_flags |= TF_ACKNOW;
738 tp->t_state = TCPS_SYN_RECEIVED;
739 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
740 goto trimthenstep6;
741 }
742
743
744
745
746
747
748
749
750
751
752
753
754
755 case TCPS_SYN_SENT:
756 if ((tiflags & TH_ACK) &&
757 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
758 SEQ_GT(ti->ti_ack, tp->snd_max)))
759 goto dropwithreset;
760
761 if (tiflags & TH_RST) {
762 if (tiflags & TH_ACK) {
763 tcp_drop(tp, 0);
764 }
765 goto drop;
766 }
767
768 if ((tiflags & TH_SYN) == 0)
769 goto drop;
770 if (tiflags & TH_ACK) {
771 tp->snd_una = ti->ti_ack;
772 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
773 tp->snd_nxt = tp->snd_una;
774 }
775
776 tp->t_timer[TCPT_REXMT] = 0;
777 tp->irs = ti->ti_seq;
778 tcp_rcvseqinit(tp);
779 tp->t_flags |= TF_ACKNOW;
780 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
781 soisfconnected(so);
782 tp->t_state = TCPS_ESTABLISHED;
783
784 (void) tcp_reass(tp, (struct tcpiphdr *)0,
785 (struct mbuf *)0);
786
787
788
789
790 if (tp->t_rtt)
791 tcp_xmit_timer(tp, tp->t_rtt);
792 } else
793 tp->t_state = TCPS_SYN_RECEIVED;
794
795trimthenstep6:
796
797
798
799
800
801 ti->ti_seq++;
802 if (ti->ti_len > tp->rcv_wnd) {
803 todrop = ti->ti_len - tp->rcv_wnd;
804 m_adj(m, -todrop);
805 ti->ti_len = tp->rcv_wnd;
806 tiflags &= ~TH_FIN;
807 }
808 tp->snd_wl1 = ti->ti_seq - 1;
809 tp->rcv_up = ti->ti_seq;
810 goto step6;
811 }
812
813
814
815
816
817
818 todrop = tp->rcv_nxt - ti->ti_seq;
819 if (todrop > 0) {
820 if (tiflags & TH_SYN) {
821 tiflags &= ~TH_SYN;
822 ti->ti_seq++;
823 if (ti->ti_urp > 1)
824 ti->ti_urp--;
825 else
826 tiflags &= ~TH_URG;
827 todrop--;
828 }
829
830
831
832 if (todrop > ti->ti_len
833 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
834
835
836
837
838
839 tiflags &= ~TH_FIN;
840
841
842
843
844
845 tp->t_flags |= TF_ACKNOW;
846 todrop = ti->ti_len;
847 }
848 m_adj(m, todrop);
849 ti->ti_seq += todrop;
850 ti->ti_len -= todrop;
851 if (ti->ti_urp > todrop)
852 ti->ti_urp -= todrop;
853 else {
854 tiflags &= ~TH_URG;
855 ti->ti_urp = 0;
856 }
857 }
858
859
860
861
862 if ((so->so_state & SS_NOFDREF) &&
863 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
864 tp = tcp_close(tp);
865 goto dropwithreset;
866 }
867
868
869
870
871
872 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
873 if (todrop > 0) {
874 if (todrop >= ti->ti_len) {
875
876
877
878
879
880
881 if (tiflags & TH_SYN &&
882 tp->t_state == TCPS_TIME_WAIT &&
883 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
884 iss = tp->rcv_nxt + TCP_ISSINCR;
885 tp = tcp_close(tp);
886 goto findso;
887 }
888
889
890
891
892
893
894
895 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
896 tp->t_flags |= TF_ACKNOW;
897 } else {
898 goto dropafterack;
899 }
900 }
901 m_adj(m, -todrop);
902 ti->ti_len -= todrop;
903 tiflags &= ~(TH_PUSH|TH_FIN);
904 }
905
906
907
908
909
910
911
912
913
914
915
916 if (tiflags&TH_RST) switch (tp->t_state) {
917
918 case TCPS_SYN_RECEIVED:
919 case TCPS_ESTABLISHED:
920 case TCPS_FIN_WAIT_1:
921 case TCPS_FIN_WAIT_2:
922 case TCPS_CLOSE_WAIT:
923 tp->t_state = TCPS_CLOSED;
924 tcp_close(tp);
925 goto drop;
926
927 case TCPS_CLOSING:
928 case TCPS_LAST_ACK:
929 case TCPS_TIME_WAIT:
930 tcp_close(tp);
931 goto drop;
932 }
933
934
935
936
937
938 if (tiflags & TH_SYN) {
939 tp = tcp_drop(tp,0);
940 goto dropwithreset;
941 }
942
943
944
945
946 if ((tiflags & TH_ACK) == 0) goto drop;
947
948
949
950
951 switch (tp->t_state) {
952
953
954
955
956
957 case TCPS_SYN_RECEIVED:
958
959 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
960 SEQ_GT(ti->ti_ack, tp->snd_max))
961 goto dropwithreset;
962 tp->t_state = TCPS_ESTABLISHED;
963
964
965
966
967
968
969
970 tp->snd_una=ti->ti_ack;
971 if (so->so_state & SS_CTL) {
972
973 ret = tcp_ctl(so);
974 if (ret == 1) {
975 soisfconnected(so);
976 so->so_state &= ~SS_CTL;
977 } else if (ret == 2) {
978 so->so_state &= SS_PERSISTENT_MASK;
979 so->so_state |= SS_NOFDREF;
980 } else {
981 needoutput = 1;
982 tp->t_state = TCPS_FIN_WAIT_1;
983 }
984 } else {
985 soisfconnected(so);
986 }
987
988 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
989 tp->snd_wl1 = ti->ti_seq - 1;
990
991 goto synrx_to_est;
992
993
994
995
996
997
998
999
1000
1001
1002 case TCPS_ESTABLISHED:
1003 case TCPS_FIN_WAIT_1:
1004 case TCPS_FIN_WAIT_2:
1005 case TCPS_CLOSE_WAIT:
1006 case TCPS_CLOSING:
1007 case TCPS_LAST_ACK:
1008 case TCPS_TIME_WAIT:
1009
1010 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1011 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1012 DEBUG_MISC(" dup ack m = %p so = %p", m, so);
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 if (tp->t_timer[TCPT_REXMT] == 0 ||
1038 ti->ti_ack != tp->snd_una)
1039 tp->t_dupacks = 0;
1040 else if (++tp->t_dupacks == TCPREXMTTHRESH) {
1041 tcp_seq onxt = tp->snd_nxt;
1042 unsigned win =
1043 MIN(tp->snd_wnd, tp->snd_cwnd) /
1044 2 / tp->t_maxseg;
1045
1046 if (win < 2)
1047 win = 2;
1048 tp->snd_ssthresh = win * tp->t_maxseg;
1049 tp->t_timer[TCPT_REXMT] = 0;
1050 tp->t_rtt = 0;
1051 tp->snd_nxt = ti->ti_ack;
1052 tp->snd_cwnd = tp->t_maxseg;
1053 (void) tcp_output(tp);
1054 tp->snd_cwnd = tp->snd_ssthresh +
1055 tp->t_maxseg * tp->t_dupacks;
1056 if (SEQ_GT(onxt, tp->snd_nxt))
1057 tp->snd_nxt = onxt;
1058 goto drop;
1059 } else if (tp->t_dupacks > TCPREXMTTHRESH) {
1060 tp->snd_cwnd += tp->t_maxseg;
1061 (void) tcp_output(tp);
1062 goto drop;
1063 }
1064 } else
1065 tp->t_dupacks = 0;
1066 break;
1067 }
1068 synrx_to_est:
1069
1070
1071
1072
1073 if (tp->t_dupacks > TCPREXMTTHRESH &&
1074 tp->snd_cwnd > tp->snd_ssthresh)
1075 tp->snd_cwnd = tp->snd_ssthresh;
1076 tp->t_dupacks = 0;
1077 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1078 goto dropafterack;
1079 }
1080 acked = ti->ti_ack - tp->snd_una;
1081
1082
1083
1084
1085
1086
1087
1088
1089 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1090 tcp_xmit_timer(tp,tp->t_rtt);
1091
1092
1093
1094
1095
1096
1097
1098 if (ti->ti_ack == tp->snd_max) {
1099 tp->t_timer[TCPT_REXMT] = 0;
1100 needoutput = 1;
1101 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1102 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1103
1104
1105
1106
1107
1108
1109
1110 {
1111 register unsigned cw = tp->snd_cwnd;
1112 register unsigned incr = tp->t_maxseg;
1113
1114 if (cw > tp->snd_ssthresh)
1115 incr = incr * incr / cw;
1116 tp->snd_cwnd = MIN(cw + incr, TCP_MAXWIN << tp->snd_scale);
1117 }
1118 if (acked > so->so_snd.sb_cc) {
1119 tp->snd_wnd -= so->so_snd.sb_cc;
1120 sodrop(so, (int)so->so_snd.sb_cc);
1121 ourfinisacked = 1;
1122 } else {
1123 sodrop(so, acked);
1124 tp->snd_wnd -= acked;
1125 ourfinisacked = 0;
1126 }
1127 tp->snd_una = ti->ti_ack;
1128 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1129 tp->snd_nxt = tp->snd_una;
1130
1131 switch (tp->t_state) {
1132
1133
1134
1135
1136
1137
1138 case TCPS_FIN_WAIT_1:
1139 if (ourfinisacked) {
1140
1141
1142
1143
1144
1145
1146
1147 if (so->so_state & SS_FCANTRCVMORE) {
1148 tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE;
1149 }
1150 tp->t_state = TCPS_FIN_WAIT_2;
1151 }
1152 break;
1153
1154
1155
1156
1157
1158
1159
1160 case TCPS_CLOSING:
1161 if (ourfinisacked) {
1162 tp->t_state = TCPS_TIME_WAIT;
1163 tcp_canceltimers(tp);
1164 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1165 }
1166 break;
1167
1168
1169
1170
1171
1172
1173
1174 case TCPS_LAST_ACK:
1175 if (ourfinisacked) {
1176 tcp_close(tp);
1177 goto drop;
1178 }
1179 break;
1180
1181
1182
1183
1184
1185
1186 case TCPS_TIME_WAIT:
1187 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1188 goto dropafterack;
1189 }
1190 }
1191
1192step6:
1193
1194
1195
1196
1197 if ((tiflags & TH_ACK) &&
1198 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1199 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1200 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1201 tp->snd_wnd = tiwin;
1202 tp->snd_wl1 = ti->ti_seq;
1203 tp->snd_wl2 = ti->ti_ack;
1204 if (tp->snd_wnd > tp->max_sndwnd)
1205 tp->max_sndwnd = tp->snd_wnd;
1206 needoutput = 1;
1207 }
1208
1209
1210
1211
1212 if ((tiflags & TH_URG) && ti->ti_urp &&
1213 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1214
1215
1216
1217
1218
1219
1220 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
1221 ti->ti_urp = 0;
1222 tiflags &= ~TH_URG;
1223 goto dodata;
1224 }
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1240 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1241 so->so_urgc = so->so_rcv.sb_cc +
1242 (tp->rcv_up - tp->rcv_nxt);
1243 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1244
1245 }
1246 } else
1247
1248
1249
1250
1251
1252 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1253 tp->rcv_up = tp->rcv_nxt;
1254dodata:
1255
1256
1257
1258
1259
1260
1261 if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
1262 ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
1263 tp->t_flags |= TF_ACKNOW;
1264 }
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1275 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1276 TCP_REASS(tp, ti, m, so, tiflags);
1277 } else {
1278 m_free(m);
1279 tiflags &= ~TH_FIN;
1280 }
1281
1282
1283
1284
1285
1286 if (tiflags & TH_FIN) {
1287 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297 sofwdrain(so);
1298
1299 tp->t_flags |= TF_ACKNOW;
1300 tp->rcv_nxt++;
1301 }
1302 switch (tp->t_state) {
1303
1304
1305
1306
1307
1308 case TCPS_SYN_RECEIVED:
1309 case TCPS_ESTABLISHED:
1310 if(so->so_emu == EMU_CTL)
1311 tp->t_state = TCPS_LAST_ACK;
1312 else
1313 tp->t_state = TCPS_CLOSE_WAIT;
1314 break;
1315
1316
1317
1318
1319
1320 case TCPS_FIN_WAIT_1:
1321 tp->t_state = TCPS_CLOSING;
1322 break;
1323
1324
1325
1326
1327
1328
1329 case TCPS_FIN_WAIT_2:
1330 tp->t_state = TCPS_TIME_WAIT;
1331 tcp_canceltimers(tp);
1332 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1333 break;
1334
1335
1336
1337
1338 case TCPS_TIME_WAIT:
1339 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1340 break;
1341 }
1342 }
1343
1344
1345
1346
1347 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
1348 (void) tcp_output(tp);
1349 }
1350 return;
1351
1352dropafterack:
1353
1354
1355
1356
1357 if (tiflags & TH_RST)
1358 goto drop;
1359 m_free(m);
1360 tp->t_flags |= TF_ACKNOW;
1361 (void) tcp_output(tp);
1362 return;
1363
1364dropwithreset:
1365
1366 if (tiflags & TH_ACK)
1367 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST, af);
1368 else {
1369 if (tiflags & TH_SYN) ti->ti_len++;
1370 tcp_respond(tp, ti, m, ti->ti_seq + ti->ti_len, (tcp_seq) 0,
1371 TH_RST | TH_ACK, af);
1372 }
1373
1374 return;
1375
1376drop:
1377
1378
1379
1380 m_free(m);
1381}
1382
1383static void
1384tcp_dooptions(struct tcpcb *tp, uint8_t *cp, int cnt, struct tcpiphdr *ti)
1385{
1386 uint16_t mss;
1387 int opt, optlen;
1388
1389 DEBUG_CALL("tcp_dooptions");
1390 DEBUG_ARG("tp = %p cnt=%i", tp, cnt);
1391
1392 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1393 opt = cp[0];
1394 if (opt == TCPOPT_EOL)
1395 break;
1396 if (opt == TCPOPT_NOP)
1397 optlen = 1;
1398 else {
1399 optlen = cp[1];
1400 if (optlen <= 0)
1401 break;
1402 }
1403 switch (opt) {
1404
1405 default:
1406 continue;
1407
1408 case TCPOPT_MAXSEG:
1409 if (optlen != TCPOLEN_MAXSEG)
1410 continue;
1411 if (!(ti->ti_flags & TH_SYN))
1412 continue;
1413 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1414 NTOHS(mss);
1415 (void) tcp_mss(tp, mss);
1416 break;
1417 }
1418 }
1419}
1420
1421
1422
1423
1424
1425
1426static void
1427tcp_xmit_timer(register struct tcpcb *tp, int rtt)
1428{
1429 register short delta;
1430
1431 DEBUG_CALL("tcp_xmit_timer");
1432 DEBUG_ARG("tp = %p", tp);
1433 DEBUG_ARG("rtt = %d", rtt);
1434
1435 if (tp->t_srtt != 0) {
1436
1437
1438
1439
1440
1441
1442
1443 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1444 if ((tp->t_srtt += delta) <= 0)
1445 tp->t_srtt = 1;
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456 if (delta < 0)
1457 delta = -delta;
1458 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1459 if ((tp->t_rttvar += delta) <= 0)
1460 tp->t_rttvar = 1;
1461 } else {
1462
1463
1464
1465
1466
1467 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1468 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1469 }
1470 tp->t_rtt = 0;
1471 tp->t_rxtshift = 0;
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1485 (short)tp->t_rttmin, TCPTV_REXMTMAX);
1486
1487
1488
1489
1490
1491
1492
1493
1494 tp->t_softerror = 0;
1495}
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513int
1514tcp_mss(struct tcpcb *tp, unsigned offer)
1515{
1516 struct socket *so = tp->t_socket;
1517 int mss;
1518
1519 DEBUG_CALL("tcp_mss");
1520 DEBUG_ARG("tp = %p", tp);
1521 DEBUG_ARG("offer = %d", offer);
1522
1523 switch (so->so_ffamily) {
1524 case AF_INET:
1525 mss = MIN(IF_MTU, IF_MRU) - sizeof(struct tcphdr)
1526 - sizeof(struct ip);
1527 break;
1528 case AF_INET6:
1529 mss = MIN(IF_MTU, IF_MRU) - sizeof(struct tcphdr)
1530 - sizeof(struct ip6);
1531 break;
1532 default:
1533 g_assert_not_reached();
1534 }
1535
1536 if (offer)
1537 mss = MIN(mss, offer);
1538 mss = MAX(mss, 32);
1539 if (mss < tp->t_maxseg || offer != 0)
1540 tp->t_maxseg = mss;
1541
1542 tp->snd_cwnd = mss;
1543
1544 sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ?
1545 (mss - (TCP_SNDSPACE % mss)) :
1546 0));
1547 sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ?
1548 (mss - (TCP_RCVSPACE % mss)) :
1549 0));
1550
1551 DEBUG_MISC(" returning mss = %d", mss);
1552
1553 return mss;
1554}
1555