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