/
tcp_subr.c
1687 lines (1575 loc) · 41.4 KB
/
tcp_subr.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: tcp_subr.c,v 1.81 1999/09/23 04:02:27 enami Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
* Facility, NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
*/
#include "opt_inet.h"
#include "opt_ipsec.h"
#include "opt_tcp_compat_42.h"
#include "rnd.h"
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/pool.h>
#if NRND > 0
#include <sys/rnd.h>
#endif
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#ifdef INET6
#ifndef INET
#include <netinet/in.h>
#endif
#include <netinet/ip6.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_var.h>
#endif
#include <netinet/tcp.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#ifdef IPSEC
#include <netinet6/ipsec.h>
#endif /*IPSEC*/
#ifdef INET6
struct in6pcb tcb6;
#endif
/* patchable/settable parameters for tcp */
int tcp_mssdflt = TCP_MSS;
int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
int tcp_do_rfc1323 = 1; /* window scaling / timestamps (obsolete) */
int tcp_do_sack = 1; /* selective acknowledgement */
int tcp_do_win_scale = 1; /* RFC1323 window scaling */
int tcp_do_timestamps = 1; /* RFC1323 timestamps */
int tcp_do_newreno = 0; /* Use the New Reno algorithms */
int tcp_ack_on_push = 0; /* set to enable immediate ACK-on-PUSH */
int tcp_init_win = 1;
int tcp_mss_ifmtu = 0;
#ifdef TCP_COMPAT_42
int tcp_compat_42 = 1;
#else
int tcp_compat_42 = 0;
#endif
#ifndef TCBHASHSIZE
#define TCBHASHSIZE 128
#endif
int tcbhashsize = TCBHASHSIZE;
int tcp_freeq __P((struct tcpcb *));
struct pool tcpcb_pool;
/*
* Tcp initialization
*/
void
tcp_init()
{
int hlen;
pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
0, NULL, NULL, M_PCB);
in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
#ifdef INET6
tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
#endif
LIST_INIT(&tcp_delacks);
hlen = sizeof(struct ip) + sizeof(struct tcphdr);
#ifdef INET6
if (sizeof(struct ip) < sizeof(struct ip6_hdr))
hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
#endif
if (max_protohdr < hlen)
max_protohdr = hlen;
if (max_linkhdr + hlen > MHLEN)
panic("tcp_init");
/* Initialize the compressed state engine. */
syn_cache_init();
}
/*
* Create template to be used to send tcp packets on a connection.
* Call after host entry created, allocates an mbuf and fills
* in a skeletal tcp/ip header, minimizing the amount of work
* necessary when the connection is used.
*/
struct mbuf *
tcp_template(tp)
struct tcpcb *tp;
{
register struct inpcb *inp = tp->t_inpcb;
#ifdef INET6
register struct in6pcb *in6p = tp->t_in6pcb;
#endif
register struct tcphdr *n;
register struct mbuf *m;
int hlen;
switch (tp->t_family) {
case AF_INET:
hlen = sizeof(struct ip);
if (inp)
break;
#ifdef INET6
if (in6p) {
/* mapped addr case */
if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
&& IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
break;
}
#endif
return NULL; /*EINVAL*/
#ifdef INET6
case AF_INET6:
hlen = sizeof(struct ip6_hdr);
if (in6p) {
/* more sainty check? */
break;
}
return NULL; /*EINVAL*/
#endif
default:
hlen = 0; /*pacify gcc*/
return NULL; /*EAFNOSUPPORT*/
}
if ((m = tp->t_template) == 0) {
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (m) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_free(m);
m = NULL;
}
}
if (m == NULL)
return NULL;
m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
}
bzero(mtod(m, caddr_t), m->m_len);
switch (tp->t_family) {
case AF_INET:
{
struct ipovly *ipov;
mtod(m, struct ip *)->ip_v = 4;
ipov = mtod(m, struct ipovly *);
ipov->ih_pr = IPPROTO_TCP;
ipov->ih_len = htons(sizeof(struct tcphdr));
if (inp) {
ipov->ih_src = inp->inp_laddr;
ipov->ih_dst = inp->inp_faddr;
}
#ifdef INET6
else if (in6p) {
/* mapped addr case */
bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
sizeof(ipov->ih_src));
bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
sizeof(ipov->ih_dst));
}
#endif
break;
}
#ifdef INET6
case AF_INET6:
{
struct ip6_hdr *ip6;
mtod(m, struct ip *)->ip_v = 6;
ip6 = mtod(m, struct ip6_hdr *);
ip6->ip6_nxt = IPPROTO_TCP;
ip6->ip6_plen = htons(sizeof(struct tcphdr));
ip6->ip6_src = in6p->in6p_laddr;
ip6->ip6_dst = in6p->in6p_faddr;
ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
if (ip6_auto_flowlabel) {
ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
ip6->ip6_flow |=
(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
}
ip6->ip6_vfc = IPV6_VERSION;
break;
}
#endif
}
n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
if (inp) {
n->th_sport = inp->inp_lport;
n->th_dport = inp->inp_fport;
}
#ifdef INET6
else if (in6p) {
n->th_sport = in6p->in6p_lport;
n->th_dport = in6p->in6p_fport;
}
#endif
n->th_seq = 0;
n->th_ack = 0;
n->th_x2 = 0;
n->th_off = 5;
n->th_flags = 0;
n->th_win = 0;
n->th_sum = 0;
n->th_urp = 0;
return (m);
}
/*
* Send a single message to the TCP at address specified by
* the given TCP/IP header. If m == 0, then we make a copy
* of the tcpiphdr at ti and send directly to the addressed host.
* This is used to force keep alive messages out using the TCP
* template for a connection tp->t_template. If flags are given
* then we send a message back to the TCP which originated the
* segment ti, and discard the mbuf containing it and any other
* attached mbufs.
*
* In any case the ack and sequence number of the transmitted
* segment are as specified by the parameters.
*/
int
tcp_respond(tp, template, m, th0, ack, seq, flags)
struct tcpcb *tp;
struct mbuf *template;
register struct mbuf *m;
struct tcphdr *th0;
tcp_seq ack, seq;
int flags;
{
#ifndef INET6
struct route iproute;
#else
struct route_in6 iproute; /* sizeof(route_in6) > sizeof(route) */
#endif
struct route *ro;
struct rtentry *rt;
int error, tlen, win = 0;
int hlen;
struct ip *ip;
#ifdef INET6
struct ip6_hdr *ip6;
#endif
int family; /* family on packet, not inpcb/in6pcb! */
struct tcphdr *th;
if (tp != NULL && (flags & TH_RST) == 0) {
if (tp->t_inpcb)
win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
#ifdef INET6
else if (tp->t_in6pcb)
win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
#endif
}
ip = NULL;
#ifdef INET6
ip6 = NULL;
#endif
if (m == 0) {
if (!template)
return EINVAL;
/* get family information from template */
switch (mtod(template, struct ip *)->ip_v) {
case 4:
family = AF_INET;
hlen = sizeof(struct ip);
break;
#ifdef INET6
case 6:
family = AF_INET6;
hlen = sizeof(struct ip6_hdr);
break;
#endif
default:
return EAFNOSUPPORT;
}
MGETHDR(m, M_DONTWAIT, MT_HEADER);
if (m) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_free(m);
m = NULL;
}
}
if (m == NULL)
return (ENOBUFS);
if (tcp_compat_42)
tlen = 1;
else
tlen = 0;
m->m_data += max_linkhdr;
bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
template->m_len);
switch (family) {
case AF_INET:
ip = mtod(m, struct ip *);
th = (struct tcphdr *)(ip + 1);
break;
#ifdef INET6
case AF_INET6:
ip6 = mtod(m, struct ip6_hdr *);
th = (struct tcphdr *)(ip6 + 1);
break;
#endif
default: /*pacify gcc*/
ip = NULL;
#ifdef INET6
ip6 = NULL;
#endif
th = NULL;
break;
}
flags = TH_ACK;
} else {
/* get family information from m */
switch (mtod(m, struct ip *)->ip_v) {
case 4:
family = AF_INET;
hlen = sizeof(struct ip);
break;
#ifdef INET6
case 6:
family = AF_INET6;
hlen = sizeof(struct ip6_hdr);
break;
#endif
default:
if (m)
m_freem(m);
return EAFNOSUPPORT;
}
/* template pointer almost has no meaning */
m_freem(m->m_next);
m->m_next = 0;
m->m_len = hlen + sizeof(struct tcphdr);
if ((m->m_flags & M_PKTHDR) == 0) {
printf("non PKTHDR to tcp_respond\n");
m_freem(m);
return EINVAL;
}
tlen = 0;
#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
switch (family) {
case AF_INET:
ip = mtod(m, struct ip *);
th = (struct tcphdr *)(ip + 1);
xchg(ip->ip_dst, ip->ip_src, struct in_addr);
ip->ip_p = IPPROTO_TCP;
break;
#ifdef INET6
case AF_INET6:
ip6 = mtod(m, struct ip6_hdr *);
th = (struct tcphdr *)(ip6 + 1);
xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
ip6->ip6_nxt = IPPROTO_TCP;
break;
#endif
}
*th = *th0;
xchg(th->th_dport, th->th_sport, u_int16_t);
#undef xchg
}
th->th_seq = htonl(seq);
th->th_ack = htonl(ack);
th->th_x2 = 0;
if ((flags & TH_SYN) == 0) {
if (tp)
th->th_win = htons((u_int16_t) (win >> tp->rcv_scale));
else
th->th_win = htons((u_int16_t)win);
th->th_off = sizeof (struct tcphdr) >> 2;
tlen += sizeof (struct tcphdr);
} else
tlen += th->th_off << 2;
m->m_len = hlen + tlen;
m->m_pkthdr.len = hlen + tlen;
m->m_pkthdr.rcvif = (struct ifnet *) 0;
th->th_flags = flags;
th->th_urp = 0;
switch (family) {
case AF_INET:
{
struct ipovly *ipov = (struct ipovly *)ip;
bzero(ipov->ih_x1, sizeof ipov->ih_x1);
ipov->ih_len = htons((u_int16_t)tlen);
th->th_sum = 0;
th->th_sum = in_cksum(m, hlen + tlen);
ip->ip_len = hlen + tlen; /*will be flipped on output*/
ip->ip_ttl = ip_defttl;
break;
}
#ifdef INET6
case AF_INET6:
{
th->th_sum = 0;
th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
tlen);
ip6->ip6_plen = ntohs(tlen);
ip6->ip6_hlim = ip6_defhlim;
ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
if (ip6_auto_flowlabel) {
ip6->ip6_flow |=
(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
}
break;
}
#endif
}
#ifdef IPSEC
m->m_pkthdr.rcvif = NULL;
#endif /*IPSEC*/
/*
* If we're doing Path MTU discovery, we need to set DF unless
* the route's MTU is locked. If we lack a route, we need to
* look it up now.
*
* ip_output() could do this for us, but it's convenient to just
* do it here unconditionally.
*/
if (tp != NULL && tp->t_inpcb != NULL) {
ro = &tp->t_inpcb->inp_route;
#ifdef IPSEC
m->m_pkthdr.rcvif = (struct ifnet *)tp->t_inpcb->inp_socket;
#endif
#ifdef DIAGNOSTIC
if (family != AF_INET)
panic("tcp_respond: address family mismatch");
if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
panic("tcp_respond: ip_dst %x != inp_faddr %x",
ntohl(ip->ip_dst.s_addr),
ntohl(tp->t_inpcb->inp_faddr.s_addr));
}
#endif
}
#ifdef INET6
else if (tp != NULL && tp->t_in6pcb != NULL) {
ro = (struct route *)&tp->t_in6pcb->in6p_route;
#ifdef IPSEC
m->m_pkthdr.rcvif = (struct ifnet *)tp->t_in6pcb->in6p_socket;
#endif
#ifdef DIAGNOSTIC
if (family == AF_INET) {
if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
panic("tcp_respond: not mapped addr");
if (bcmp(&ip->ip_dst,
&tp->t_in6pcb->in6p_faddr.s6_addr32[3],
sizeof(ip->ip_dst)) != 0) {
panic("tcp_respond: ip_dst != in6p_faddr");
}
} else if (family == AF_INET6) {
if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
panic("tcp_respond: ip6_dst != in6p_faddr");
} else
panic("tcp_respond: address family mismatch");
#endif
}
#endif
else {
ro = (struct route *)&iproute;
bzero(ro, sizeof(iproute));
}
if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) {
if (ro->ro_rt != NULL) {
RTFREE(ro->ro_rt);
ro->ro_rt = NULL;
}
switch (family) {
case AF_INET:
{
struct sockaddr_in *dst;
dst = satosin(&ro->ro_dst);
dst->sin_family = AF_INET;
dst->sin_len = sizeof(*dst);
dst->sin_addr = ip->ip_dst;
break;
}
#ifdef INET6
case AF_INET6:
{
struct sockaddr_in6 *dst;
dst = satosin6(&ro->ro_dst);
bzero(dst, sizeof(*dst));
dst->sin6_family = AF_INET6;
dst->sin6_len = sizeof(*dst);
dst->sin6_addr = ip6->ip6_dst;
break;
}
#endif
}
rtalloc(ro);
if ((rt = ro->ro_rt) == NULL) {
m_freem(m);
switch (family) {
case AF_INET:
ipstat.ips_noroute++;
break;
#ifdef INET6
case AF_INET6:
ip6stat.ip6s_noroute++;
break;
#endif
}
return (EHOSTUNREACH);
}
}
switch (family) {
case AF_INET:
if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
ip->ip_off |= IP_DF;
error = ip_output(m, NULL, ro, 0, NULL);
break;
#ifdef INET6
case AF_INET6:
error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL);
break;
#endif
default:
error = EAFNOSUPPORT;
break;
}
if (ro == (struct route *)&iproute) {
RTFREE(ro->ro_rt);
ro->ro_rt = NULL;
}
return (error);
}
/*
* Create a new TCP control block, making an
* empty reassembly queue and hooking it to the argument
* protocol control block.
*/
struct tcpcb *
tcp_newtcpcb(family, aux)
int family; /* selects inpcb, or in6pcb */
void *aux;
{
register struct tcpcb *tp;
switch (family) {
case PF_INET:
break;
#ifdef INET6
case PF_INET6:
break;
#endif
default:
return NULL;
}
tp = pool_get(&tcpcb_pool, PR_NOWAIT);
if (tp == NULL)
return (NULL);
bzero((caddr_t)tp, sizeof(struct tcpcb));
LIST_INIT(&tp->segq);
LIST_INIT(&tp->timeq);
tp->t_family = family; /* may be overridden later on */
tp->t_peermss = tcp_mssdflt;
tp->t_ourmss = tcp_mssdflt;
tp->t_segsz = tcp_mssdflt;
LIST_INIT(&tp->t_sc);
tp->t_flags = 0;
if (tcp_do_rfc1323 && tcp_do_win_scale)
tp->t_flags |= TF_REQ_SCALE;
if (tcp_do_rfc1323 && tcp_do_timestamps)
tp->t_flags |= TF_REQ_TSTMP;
if (tcp_do_sack == 2)
tp->t_flags |= TF_WILL_SACK;
else if (tcp_do_sack == 1)
tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
tp->t_flags |= TF_CANT_TXSACK;
switch (family) {
case PF_INET:
tp->t_inpcb = (struct inpcb *)aux;
break;
#ifdef INET6
case PF_INET6:
tp->t_in6pcb = (struct in6pcb *)aux;
break;
#endif
}
/*
* Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
* rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
* reasonable initial retransmit time.
*/
tp->t_srtt = TCPTV_SRTTBASE;
tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
tp->t_rttmin = TCPTV_MIN;
TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
TCPTV_MIN, TCPTV_REXMTMAX);
tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
if (family == AF_INET) {
struct inpcb *inp = (struct inpcb *)aux;
inp->inp_ip.ip_ttl = ip_defttl;
inp->inp_ppcb = (caddr_t)tp;
}
#ifdef INET6
else if (family == AF_INET6) {
struct in6pcb *in6p = (struct in6pcb *)aux;
in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
in6p->in6p_ppcb = (caddr_t)tp;
}
#endif
return (tp);
}
/*
* Drop a TCP connection, reporting
* the specified error. If connection is synchronized,
* then send a RST to peer.
*/
struct tcpcb *
tcp_drop(tp, errno)
register struct tcpcb *tp;
int errno;
{
struct socket *so;
if (tp->t_inpcb)
so = tp->t_inpcb->inp_socket;
#ifdef INET6
else if (tp->t_in6pcb)
so = tp->t_in6pcb->in6p_socket;
#endif
else
return NULL;
if (TCPS_HAVERCVDSYN(tp->t_state)) {
tp->t_state = TCPS_CLOSED;
(void) tcp_output(tp);
tcpstat.tcps_drops++;
} else
tcpstat.tcps_conndrops++;
if (errno == ETIMEDOUT && tp->t_softerror)
errno = tp->t_softerror;
so->so_error = errno;
return (tcp_close(tp));
}
/*
* Close a TCP control block:
* discard all space held by the tcp
* discard internet protocol block
* wake up any sleepers
*/
struct tcpcb *
tcp_close(tp)
register struct tcpcb *tp;
{
struct inpcb *inp;
#ifdef INET6
struct in6pcb *in6p;
#endif
struct socket *so;
#ifdef RTV_RTT
register struct rtentry *rt;
#endif
struct route *ro;
inp = tp->t_inpcb;
#ifdef INET6
in6p = tp->t_in6pcb;
#endif
so = NULL;
ro = NULL;
if (inp) {
so = inp->inp_socket;
ro = &inp->inp_route;
}
#ifdef INET6
else if (in6p) {
so = in6p->in6p_socket;
ro = (struct route *)&in6p->in6p_route;
}
#endif
#ifdef RTV_RTT
/*
* If we sent enough data to get some meaningful characteristics,
* save them in the routing entry. 'Enough' is arbitrarily
* defined as the sendpipesize (default 4K) * 16. This would
* give us 16 rtt samples assuming we only get one sample per
* window (the usual case on a long haul net). 16 samples is
* enough for the srtt filter to converge to within 5% of the correct
* value; fewer samples and we could save a very bogus rtt.
*
* Don't update the default route's characteristics and don't
* update anything that the user "locked".
*/
if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
ro && (rt = ro->ro_rt) &&
!in_nullhost(satosin(rt_key(rt))->sin_addr)) {
register u_long i = 0;
if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
i = tp->t_srtt *
((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
if (rt->rt_rmx.rmx_rtt && i)
/*
* filter this update to half the old & half
* the new values, converting scale.
* See route.h and tcp_var.h for a
* description of the scaling constants.
*/
rt->rt_rmx.rmx_rtt =
(rt->rt_rmx.rmx_rtt + i) / 2;
else
rt->rt_rmx.rmx_rtt = i;
}
if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
i = tp->t_rttvar *
((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
if (rt->rt_rmx.rmx_rttvar && i)
rt->rt_rmx.rmx_rttvar =
(rt->rt_rmx.rmx_rttvar + i) / 2;
else
rt->rt_rmx.rmx_rttvar = i;
}
/*
* update the pipelimit (ssthresh) if it has been updated
* already or if a pipesize was specified & the threshhold
* got below half the pipesize. I.e., wait for bad news
* before we start updating, then update on both good
* and bad news.
*/
if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
(i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
i < (rt->rt_rmx.rmx_sendpipe / 2)) {
/*
* convert the limit from user data bytes to
* packets then to packet data bytes.
*/
i = (i + tp->t_segsz / 2) / tp->t_segsz;
if (i < 2)
i = 2;
i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
if (rt->rt_rmx.rmx_ssthresh)
rt->rt_rmx.rmx_ssthresh =
(rt->rt_rmx.rmx_ssthresh + i) / 2;
else
rt->rt_rmx.rmx_ssthresh = i;
}
}
#endif /* RTV_RTT */
/* free the reassembly queue, if any */
TCP_REASS_LOCK(tp);
(void) tcp_freeq(tp);
TCP_REASS_UNLOCK(tp);
TCP_CLEAR_DELACK(tp);
syn_cache_cleanup(tp);
if (tp->t_template) {
m_free(tp->t_template);
tp->t_template = NULL;
}
pool_put(&tcpcb_pool, tp);
if (inp) {
inp->inp_ppcb = 0;
soisdisconnected(so);
in_pcbdetach(inp);
}
#ifdef INET6
else if (in6p) {
in6p->in6p_ppcb = 0;
soisdisconnected(so);
in6_pcbdetach(in6p);
}
#endif
tcpstat.tcps_closed++;
return ((struct tcpcb *)0);
}
int
tcp_freeq(tp)
struct tcpcb *tp;
{
register struct ipqent *qe;
int rv = 0;
#ifdef TCPREASS_DEBUG
int i = 0;
#endif
TCP_REASS_LOCK_CHECK(tp);
while ((qe = tp->segq.lh_first) != NULL) {
#ifdef TCPREASS_DEBUG
printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
#endif
LIST_REMOVE(qe, ipqe_q);
LIST_REMOVE(qe, ipqe_timeq);
m_freem(qe->ipqe_m);
pool_put(&ipqent_pool, qe);
rv = 1;
}
return (rv);
}
/*
* Protocol drain routine. Called when memory is in short supply.
*/
void
tcp_drain()
{
register struct inpcb *inp;
register struct tcpcb *tp;
/*
* Free the sequence queue of all TCP connections.
*/
inp = tcbtable.inpt_queue.cqh_first;
if (inp) /* XXX */
for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
inp = inp->inp_queue.cqe_next) {
if ((tp = intotcpcb(inp)) != NULL) {
/*
* We may be called from a device's interrupt
* context. If the tcpcb is already busy,
* just bail out now.
*/
if (tcp_reass_lock_try(tp) == 0)
continue;
if (tcp_freeq(tp))
tcpstat.tcps_connsdrained++;
TCP_REASS_UNLOCK(tp);
}
}
}
/*
* Notify a tcp user of an asynchronous error;
* store error as soft error, but wake up user
* (for now, won't do anything until can select for soft error).
*/
void
tcp_notify(inp, error)
struct inpcb *inp;
int error;
{
register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
register struct socket *so = inp->inp_socket;
/*
* Ignore some errors if we are hooked up.
* If connection hasn't completed, has retransmitted several times,
* and receives a second error, give up now. This is better
* than waiting a long time to establish a connection that
* can never complete.
*/
if (tp->t_state == TCPS_ESTABLISHED &&