forked from illumos/illumos-gate
/
tcp_input.c
5912 lines (5490 loc) · 172 KB
/
tcp_input.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
* Copyright 2019 Joyent, Inc.
* Copyright (c) 2014, 2016 by Delphix. All rights reserved.
*/
/* This file contains all TCP input processing functions. */
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/strsubr.h>
#include <sys/stropts.h>
#include <sys/strlog.h>
#define _SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/suntpi.h>
#include <sys/xti_inet.h>
#include <sys/squeue_impl.h>
#include <sys/squeue.h>
#include <sys/tsol/tnet.h>
#include <inet/common.h>
#include <inet/ip.h>
#include <inet/tcp.h>
#include <inet/tcp_impl.h>
#include <inet/tcp_cluster.h>
#include <inet/proto_set.h>
#include <inet/ipsec_impl.h>
/*
* RFC7323-recommended phrasing of TSTAMP option, for easier parsing
*/
#ifdef _BIG_ENDIAN
#define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
(TCPOPT_TSTAMP << 8) | 10)
#else
#define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
(TCPOPT_NOP << 8) | TCPOPT_NOP)
#endif
/*
* PAWS needs a timer for 24 days. This is the number of ticks in 24 days
*/
#define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz))
/*
* Since tcp_listener is not cleared atomically with tcp_detached
* being cleared we need this extra bit to tell a detached connection
* apart from one that is in the process of being accepted.
*/
#define TCP_IS_DETACHED_NONEAGER(tcp) \
(TCP_IS_DETACHED(tcp) && \
(!(tcp)->tcp_hard_binding))
/*
* Steps to do when a tcp_t moves to TIME-WAIT state.
*
* This connection is done, we don't need to account for it. Decrement
* the listener connection counter if needed.
*
* Decrement the connection counter of the stack. Note that this counter
* is per CPU. So the total number of connections in a stack is the sum of all
* of them. Since there is no lock for handling all of them exclusively, the
* resulting sum is only an approximation.
*
* Unconditionally clear the exclusive binding bit so this TIME-WAIT
* connection won't interfere with new ones.
*
* Start the TIME-WAIT timer. If upper layer has not closed the connection,
* the timer is handled within the context of this tcp_t. When the timer
* fires, tcp_clean_death() is called. If upper layer closes the connection
* during this period, tcp_time_wait_append() will be called to add this
* tcp_t to the global TIME-WAIT list. Note that this means that the
* actual wait time in TIME-WAIT state will be longer than the
* tcps_time_wait_interval since the period before upper layer closes the
* connection is not accounted for when tcp_time_wait_append() is called.
*
* If upper layer has closed the connection, call tcp_time_wait_append()
* directly.
*
*/
#define SET_TIME_WAIT(tcps, tcp, connp) \
{ \
(tcp)->tcp_state = TCPS_TIME_WAIT; \
if ((tcp)->tcp_listen_cnt != NULL) \
TCP_DECR_LISTEN_CNT(tcp); \
atomic_dec_64( \
(uint64_t *)&(tcps)->tcps_sc[CPU->cpu_seqid]->tcp_sc_conn_cnt); \
(connp)->conn_exclbind = 0; \
if (!TCP_IS_DETACHED(tcp)) { \
TCP_TIMER_RESTART(tcp, (tcps)->tcps_time_wait_interval); \
} else { \
tcp_time_wait_append(tcp); \
TCP_DBGSTAT(tcps, tcp_rput_time_wait); \
} \
}
/*
* If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more
* than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent
* data, TCP will not respond with an ACK. RFC 793 requires that
* TCP responds with an ACK for such a bogus ACK. By not following
* the RFC, we prevent TCP from getting into an ACK storm if somehow
* an attacker successfully spoofs an acceptable segment to our
* peer; or when our peer is "confused."
*/
static uint32_t tcp_drop_ack_unsent_cnt = 10;
/*
* To protect TCP against attacker using a small window and requesting
* large amount of data (DoS attack by conuming memory), TCP checks the
* window advertised in the last ACK of the 3-way handshake. TCP uses
* the tcp_mss (the size of one packet) value for comparion. The window
* should be larger than tcp_mss. But while a sane TCP should advertise
* a receive window larger than or equal to 4*MSS to avoid stop and go
* tarrfic, not all TCP stacks do that. This is especially true when
* tcp_mss is a big value.
*
* To work around this issue, an additional fixed value for comparison
* is also used. If the advertised window is smaller than both tcp_mss
* and tcp_init_wnd_chk, the ACK is considered as invalid. So for large
* tcp_mss value (say, 8K), a window larger than tcp_init_wnd_chk but
* smaller than 8K is considered to be OK.
*/
static uint32_t tcp_init_wnd_chk = 4096;
/* Process ICMP source quench message or not. */
static boolean_t tcp_icmp_source_quench = B_FALSE;
static boolean_t tcp_outbound_squeue_switch = B_FALSE;
static mblk_t *tcp_conn_create_v4(conn_t *, conn_t *, mblk_t *,
ip_recv_attr_t *);
static mblk_t *tcp_conn_create_v6(conn_t *, conn_t *, mblk_t *,
ip_recv_attr_t *);
static boolean_t tcp_drop_q0(tcp_t *);
static void tcp_icmp_error_ipv6(tcp_t *, mblk_t *, ip_recv_attr_t *);
static mblk_t *tcp_input_add_ancillary(tcp_t *, mblk_t *, ip_pkt_t *,
ip_recv_attr_t *);
static void tcp_input_listener(void *, mblk_t *, void *, ip_recv_attr_t *);
static void tcp_process_options(tcp_t *, tcpha_t *);
static mblk_t *tcp_reass(tcp_t *, mblk_t *, uint32_t);
static void tcp_reass_elim_overlap(tcp_t *, mblk_t *);
static void tcp_rsrv_input(void *, mblk_t *, void *, ip_recv_attr_t *);
static void tcp_set_rto(tcp_t *, hrtime_t);
static void tcp_setcred_data(mblk_t *, ip_recv_attr_t *);
/*
* CC wrapper hook functions
*/
static void
cc_ack_received(tcp_t *tcp, uint32_t seg_ack, int32_t bytes_acked,
uint16_t type)
{
uint32_t old_cwnd = tcp->tcp_cwnd;
tcp->tcp_ccv.bytes_this_ack = bytes_acked;
if (tcp->tcp_cwnd <= tcp->tcp_swnd)
tcp->tcp_ccv.flags |= CCF_CWND_LIMITED;
else
tcp->tcp_ccv.flags &= ~CCF_CWND_LIMITED;
if (type == CC_ACK) {
if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) {
if (tcp->tcp_ccv.flags & CCF_RTO)
tcp->tcp_ccv.flags &= ~CCF_RTO;
tcp->tcp_ccv.t_bytes_acked +=
min(tcp->tcp_ccv.bytes_this_ack,
tcp->tcp_tcps->tcps_abc_l_var * tcp->tcp_mss);
if (tcp->tcp_ccv.t_bytes_acked >= tcp->tcp_cwnd) {
tcp->tcp_ccv.t_bytes_acked -= tcp->tcp_cwnd;
tcp->tcp_ccv.flags |= CCF_ABC_SENTAWND;
}
} else {
tcp->tcp_ccv.flags &= ~CCF_ABC_SENTAWND;
tcp->tcp_ccv.t_bytes_acked = 0;
}
}
if (CC_ALGO(tcp)->ack_received != NULL) {
/*
* The FreeBSD code where this originated had a comment "Find
* a way to live without this" in several places where curack
* got set. If they eventually dump curack from the cc
* variables, we'll need to adapt our code.
*/
tcp->tcp_ccv.curack = seg_ack;
CC_ALGO(tcp)->ack_received(&tcp->tcp_ccv, type);
}
DTRACE_PROBE3(cwnd__cc__ack__received, tcp_t *, tcp, uint32_t, old_cwnd,
uint32_t, tcp->tcp_cwnd);
}
void
cc_cong_signal(tcp_t *tcp, uint32_t seg_ack, uint32_t type)
{
uint32_t old_cwnd = tcp->tcp_cwnd;
uint32_t old_cwnd_ssthresh = tcp->tcp_cwnd_ssthresh;
switch (type) {
case CC_NDUPACK:
if (!IN_FASTRECOVERY(tcp->tcp_ccv.flags)) {
tcp->tcp_rexmit_max = tcp->tcp_snxt;
if (tcp->tcp_ecn_ok) {
tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
tcp->tcp_cwr = B_TRUE;
tcp->tcp_ecn_cwr_sent = B_FALSE;
}
}
break;
case CC_ECN:
if (!IN_CONGRECOVERY(tcp->tcp_ccv.flags)) {
tcp->tcp_rexmit_max = tcp->tcp_snxt;
if (tcp->tcp_ecn_ok) {
tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
tcp->tcp_cwr = B_TRUE;
tcp->tcp_ecn_cwr_sent = B_FALSE;
}
}
break;
case CC_RTO:
tcp->tcp_ccv.flags |= CCF_RTO;
tcp->tcp_dupack_cnt = 0;
tcp->tcp_ccv.t_bytes_acked = 0;
/*
* Give up on fast recovery and congestion recovery if we were
* attempting either.
*/
EXIT_RECOVERY(tcp->tcp_ccv.flags);
if (CC_ALGO(tcp)->cong_signal == NULL) {
/*
* RFC5681 Section 3.1
* ssthresh = max (FlightSize / 2, 2*SMSS) eq (4)
*/
tcp->tcp_cwnd_ssthresh = max(
(tcp->tcp_snxt - tcp->tcp_suna) / 2 / tcp->tcp_mss,
2) * tcp->tcp_mss;
tcp->tcp_cwnd = tcp->tcp_mss;
}
if (tcp->tcp_ecn_ok) {
tcp->tcp_cwr = B_TRUE;
tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
tcp->tcp_ecn_cwr_sent = B_FALSE;
}
break;
}
if (CC_ALGO(tcp)->cong_signal != NULL) {
tcp->tcp_ccv.curack = seg_ack;
CC_ALGO(tcp)->cong_signal(&tcp->tcp_ccv, type);
}
DTRACE_PROBE6(cwnd__cc__cong__signal, tcp_t *, tcp, uint32_t, old_cwnd,
uint32_t, tcp->tcp_cwnd, uint32_t, old_cwnd_ssthresh,
uint32_t, tcp->tcp_cwnd_ssthresh, uint32_t, type);
}
static void
cc_post_recovery(tcp_t *tcp, uint32_t seg_ack)
{
uint32_t old_cwnd = tcp->tcp_cwnd;
if (CC_ALGO(tcp)->post_recovery != NULL) {
tcp->tcp_ccv.curack = seg_ack;
CC_ALGO(tcp)->post_recovery(&tcp->tcp_ccv);
}
tcp->tcp_ccv.t_bytes_acked = 0;
DTRACE_PROBE3(cwnd__cc__post__recovery, tcp_t *, tcp,
uint32_t, old_cwnd, uint32_t, tcp->tcp_cwnd);
}
/*
* Set the MSS associated with a particular tcp based on its current value,
* and a new one passed in. Observe minimums and maximums, and reset other
* state variables that we want to view as multiples of MSS.
*
* The value of MSS could be either increased or descreased.
*/
void
tcp_mss_set(tcp_t *tcp, uint32_t mss)
{
uint32_t mss_max;
tcp_stack_t *tcps = tcp->tcp_tcps;
conn_t *connp = tcp->tcp_connp;
if (connp->conn_ipversion == IPV4_VERSION)
mss_max = tcps->tcps_mss_max_ipv4;
else
mss_max = tcps->tcps_mss_max_ipv6;
if (mss < tcps->tcps_mss_min)
mss = tcps->tcps_mss_min;
if (mss > mss_max)
mss = mss_max;
/*
* Unless naglim has been set by our client to
* a non-mss value, force naglim to track mss.
* This can help to aggregate small writes.
*/
if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim)
tcp->tcp_naglim = mss;
/*
* TCP should be able to buffer at least 4 MSS data for obvious
* performance reason.
*/
if ((mss << 2) > connp->conn_sndbuf)
connp->conn_sndbuf = mss << 2;
/*
* Set the send lowater to at least twice of MSS.
*/
if ((mss << 1) > connp->conn_sndlowat)
connp->conn_sndlowat = mss << 1;
/*
* Update tcp_cwnd according to the new value of MSS. Keep the
* previous ratio to preserve the transmit rate.
*/
tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss;
tcp->tcp_cwnd_cnt = 0;
tcp->tcp_mss = mss;
(void) tcp_maxpsz_set(tcp, B_TRUE);
}
/*
* Extract option values from a tcp header. We put any found values into the
* tcpopt struct and return a bitmask saying which options were found.
*/
int
tcp_parse_options(tcpha_t *tcpha, tcp_opt_t *tcpopt)
{
uchar_t *endp;
int len;
uint32_t mss;
uchar_t *up = (uchar_t *)tcpha;
int found = 0;
int32_t sack_len;
tcp_seq sack_begin, sack_end;
tcp_t *tcp;
endp = up + TCP_HDR_LENGTH(tcpha);
up += TCP_MIN_HEADER_LENGTH;
/*
* If timestamp option is aligned as recommended in RFC 7323 Appendix
* A, and is the only option, return quickly.
*/
if (TCP_HDR_LENGTH(tcpha) == (uint32_t)TCP_MIN_HEADER_LENGTH +
TCPOPT_REAL_TS_LEN &&
OK_32PTR(up) &&
*(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) {
tcpopt->tcp_opt_ts_val = ABE32_TO_U32((up+4));
tcpopt->tcp_opt_ts_ecr = ABE32_TO_U32((up+8));
return (TCP_OPT_TSTAMP_PRESENT);
}
while (up < endp) {
len = endp - up;
switch (*up) {
case TCPOPT_EOL:
break;
case TCPOPT_NOP:
up++;
continue;
case TCPOPT_MAXSEG:
if (len < TCPOPT_MAXSEG_LEN ||
up[1] != TCPOPT_MAXSEG_LEN)
break;
mss = BE16_TO_U16(up+2);
/* Caller must handle tcp_mss_min and tcp_mss_max_* */
tcpopt->tcp_opt_mss = mss;
found |= TCP_OPT_MSS_PRESENT;
up += TCPOPT_MAXSEG_LEN;
continue;
case TCPOPT_WSCALE:
if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN)
break;
if (up[2] > TCP_MAX_WINSHIFT)
tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT;
else
tcpopt->tcp_opt_wscale = up[2];
found |= TCP_OPT_WSCALE_PRESENT;
up += TCPOPT_WS_LEN;
continue;
case TCPOPT_SACK_PERMITTED:
if (len < TCPOPT_SACK_OK_LEN ||
up[1] != TCPOPT_SACK_OK_LEN)
break;
found |= TCP_OPT_SACK_OK_PRESENT;
up += TCPOPT_SACK_OK_LEN;
continue;
case TCPOPT_SACK:
if (len <= 2 || up[1] <= 2 || len < up[1])
break;
/* If TCP is not interested in SACK blks... */
if ((tcp = tcpopt->tcp) == NULL) {
up += up[1];
continue;
}
sack_len = up[1] - TCPOPT_HEADER_LEN;
up += TCPOPT_HEADER_LEN;
/*
* If the list is empty, allocate one and assume
* nothing is sack'ed.
*/
if (tcp->tcp_notsack_list == NULL) {
tcp_notsack_update(&(tcp->tcp_notsack_list),
tcp->tcp_suna, tcp->tcp_snxt,
&(tcp->tcp_num_notsack_blk),
&(tcp->tcp_cnt_notsack_list));
/*
* Make sure tcp_notsack_list is not NULL.
* This happens when kmem_alloc(KM_NOSLEEP)
* returns NULL.
*/
if (tcp->tcp_notsack_list == NULL) {
up += sack_len;
continue;
}
tcp->tcp_fack = tcp->tcp_suna;
}
while (sack_len > 0) {
if (up + 8 > endp) {
up = endp;
break;
}
sack_begin = BE32_TO_U32(up);
up += 4;
sack_end = BE32_TO_U32(up);
up += 4;
sack_len -= 8;
/*
* Bounds checking. Make sure the SACK
* info is within tcp_suna and tcp_snxt.
* If this SACK blk is out of bound, ignore
* it but continue to parse the following
* blks.
*/
if (SEQ_LEQ(sack_end, sack_begin) ||
SEQ_LT(sack_begin, tcp->tcp_suna) ||
SEQ_GT(sack_end, tcp->tcp_snxt)) {
continue;
}
tcp_notsack_insert(&(tcp->tcp_notsack_list),
sack_begin, sack_end,
&(tcp->tcp_num_notsack_blk),
&(tcp->tcp_cnt_notsack_list));
if (SEQ_GT(sack_end, tcp->tcp_fack)) {
tcp->tcp_fack = sack_end;
}
}
found |= TCP_OPT_SACK_PRESENT;
continue;
case TCPOPT_TSTAMP:
if (len < TCPOPT_TSTAMP_LEN ||
up[1] != TCPOPT_TSTAMP_LEN)
break;
tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2);
tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6);
found |= TCP_OPT_TSTAMP_PRESENT;
up += TCPOPT_TSTAMP_LEN;
continue;
default:
if (len <= 1 || len < (int)up[1] || up[1] == 0)
break;
up += up[1];
continue;
}
break;
}
return (found);
}
/*
* Process all TCP option in SYN segment. Note that this function should
* be called after tcp_set_destination() is called so that the necessary info
* from IRE is already set in the tcp structure.
*
* This function sets up the correct tcp_mss value according to the
* MSS option value and our header size. It also sets up the window scale
* and timestamp values, and initialize SACK info blocks. But it does not
* change receive window size after setting the tcp_mss value. The caller
* should do the appropriate change.
*/
static void
tcp_process_options(tcp_t *tcp, tcpha_t *tcpha)
{
int options;
tcp_opt_t tcpopt;
uint32_t mss_max;
char *tmp_tcph;
tcp_stack_t *tcps = tcp->tcp_tcps;
conn_t *connp = tcp->tcp_connp;
tcpopt.tcp = NULL;
options = tcp_parse_options(tcpha, &tcpopt);
/*
* Process MSS option. Note that MSS option value does not account
* for IP or TCP options. This means that it is equal to MTU - minimum
* IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
* IPv6.
*/
if (!(options & TCP_OPT_MSS_PRESENT)) {
if (connp->conn_ipversion == IPV4_VERSION)
tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4;
else
tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6;
} else {
if (connp->conn_ipversion == IPV4_VERSION)
mss_max = tcps->tcps_mss_max_ipv4;
else
mss_max = tcps->tcps_mss_max_ipv6;
if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min)
tcpopt.tcp_opt_mss = tcps->tcps_mss_min;
else if (tcpopt.tcp_opt_mss > mss_max)
tcpopt.tcp_opt_mss = mss_max;
}
/* Process Window Scale option. */
if (options & TCP_OPT_WSCALE_PRESENT) {
tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale;
tcp->tcp_snd_ws_ok = B_TRUE;
} else {
tcp->tcp_snd_ws = B_FALSE;
tcp->tcp_snd_ws_ok = B_FALSE;
tcp->tcp_rcv_ws = B_FALSE;
}
/* Process Timestamp option. */
if ((options & TCP_OPT_TSTAMP_PRESENT) &&
(tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) {
tmp_tcph = (char *)tcp->tcp_tcpha;
tcp->tcp_snd_ts_ok = B_TRUE;
tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
tcp->tcp_last_rcv_lbolt = ddi_get_lbolt64();
ASSERT(OK_32PTR(tmp_tcph));
ASSERT(connp->conn_ht_ulp_len == TCP_MIN_HEADER_LENGTH);
/* Fill in our template header with basic timestamp option. */
tmp_tcph += connp->conn_ht_ulp_len;
tmp_tcph[0] = TCPOPT_NOP;
tmp_tcph[1] = TCPOPT_NOP;
tmp_tcph[2] = TCPOPT_TSTAMP;
tmp_tcph[3] = TCPOPT_TSTAMP_LEN;
connp->conn_ht_iphc_len += TCPOPT_REAL_TS_LEN;
connp->conn_ht_ulp_len += TCPOPT_REAL_TS_LEN;
tcp->tcp_tcpha->tha_offset_and_reserved += (3 << 4);
} else {
tcp->tcp_snd_ts_ok = B_FALSE;
}
/*
* Process SACK options. If SACK is enabled for this connection,
* then allocate the SACK info structure. Note the following ways
* when tcp_snd_sack_ok is set to true.
*
* For active connection: in tcp_set_destination() called in
* tcp_connect().
*
* For passive connection: in tcp_set_destination() called in
* tcp_input_listener().
*
* That's the reason why the extra TCP_IS_DETACHED() check is there.
* That check makes sure that if we did not send a SACK OK option,
* we will not enable SACK for this connection even though the other
* side sends us SACK OK option. For active connection, the SACK
* info structure has already been allocated. So we need to free
* it if SACK is disabled.
*/
if ((options & TCP_OPT_SACK_OK_PRESENT) &&
(tcp->tcp_snd_sack_ok ||
(tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) {
ASSERT(tcp->tcp_num_sack_blk == 0);
ASSERT(tcp->tcp_notsack_list == NULL);
tcp->tcp_snd_sack_ok = B_TRUE;
if (tcp->tcp_snd_ts_ok) {
tcp->tcp_max_sack_blk = 3;
} else {
tcp->tcp_max_sack_blk = 4;
}
} else if (tcp->tcp_snd_sack_ok) {
/*
* Resetting tcp_snd_sack_ok to B_FALSE so that
* no SACK info will be used for this
* connection. This assumes that SACK usage
* permission is negotiated. This may need
* to be changed once this is clarified.
*/
ASSERT(tcp->tcp_num_sack_blk == 0);
ASSERT(tcp->tcp_notsack_list == NULL);
tcp->tcp_snd_sack_ok = B_FALSE;
}
/*
* Now we know the exact TCP/IP header length, subtract
* that from tcp_mss to get our side's MSS.
*/
tcp->tcp_mss -= connp->conn_ht_iphc_len;
/*
* Here we assume that the other side's header size will be equal to
* our header size. We calculate the real MSS accordingly. Need to
* take into additional stuffs IPsec puts in.
*
* Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
*/
tcpopt.tcp_opt_mss -= connp->conn_ht_iphc_len +
tcp->tcp_ipsec_overhead -
((connp->conn_ipversion == IPV4_VERSION ?
IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH);
/*
* Set MSS to the smaller one of both ends of the connection.
* We should not have called tcp_mss_set() before, but our
* side of the MSS should have been set to a proper value
* by tcp_set_destination(). tcp_mss_set() will also set up the
* STREAM head parameters properly.
*
* If we have a larger-than-16-bit window but the other side
* didn't want to do window scale, tcp_rwnd_set() will take
* care of that.
*/
tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss));
/*
* Initialize tcp_cwnd value. After tcp_mss_set(), tcp_mss has been
* updated properly.
*/
TCP_SET_INIT_CWND(tcp, tcp->tcp_mss, tcps->tcps_slow_start_initial);
if (tcp->tcp_cc_algo->conn_init != NULL)
tcp->tcp_cc_algo->conn_init(&tcp->tcp_ccv);
}
/*
* Add a new piece to the tcp reassembly queue. If the gap at the beginning
* is filled, return as much as we can. The message passed in may be
* multi-part, chained using b_cont. "start" is the starting sequence
* number for this piece.
*/
static mblk_t *
tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start)
{
uint32_t end, bytes;
mblk_t *mp1;
mblk_t *mp2;
mblk_t *next_mp;
uint32_t u1;
tcp_stack_t *tcps = tcp->tcp_tcps;
/* Walk through all the new pieces. */
do {
ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
(uintptr_t)INT_MAX);
end = start + (int)(mp->b_wptr - mp->b_rptr);
next_mp = mp->b_cont;
if (start == end) {
/* Empty. Blast it. */
freeb(mp);
continue;
}
bytes = end - start;
mp->b_cont = NULL;
TCP_REASS_SET_SEQ(mp, start);
TCP_REASS_SET_END(mp, end);
mp1 = tcp->tcp_reass_tail;
if (mp1 == NULL || SEQ_GEQ(start, TCP_REASS_END(mp1))) {
if (mp1 != NULL) {
/*
* New stuff is beyond the tail; link it on the
* end.
*/
mp1->b_cont = mp;
} else {
tcp->tcp_reass_head = mp;
}
tcp->tcp_reass_tail = mp;
TCPS_BUMP_MIB(tcps, tcpInDataUnorderSegs);
TCPS_UPDATE_MIB(tcps, tcpInDataUnorderBytes, bytes);
tcp->tcp_cs.tcp_in_data_unorder_segs++;
tcp->tcp_cs.tcp_in_data_unorder_bytes += bytes;
continue;
}
mp1 = tcp->tcp_reass_head;
u1 = TCP_REASS_SEQ(mp1);
/* New stuff at the front? */
if (SEQ_LT(start, u1)) {
/* Yes... Check for overlap. */
mp->b_cont = mp1;
tcp->tcp_reass_head = mp;
tcp_reass_elim_overlap(tcp, mp);
continue;
}
/*
* The new piece fits somewhere between the head and tail.
* We find our slot, where mp1 precedes us and mp2 trails.
*/
for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) {
u1 = TCP_REASS_SEQ(mp2);
if (SEQ_LEQ(start, u1))
break;
}
/* Link ourselves in */
mp->b_cont = mp2;
mp1->b_cont = mp;
/* Trim overlap with following mblk(s) first */
tcp_reass_elim_overlap(tcp, mp);
/* Trim overlap with preceding mblk */
tcp_reass_elim_overlap(tcp, mp1);
} while (start = end, mp = next_mp);
mp1 = tcp->tcp_reass_head;
/* Anything ready to go? */
if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt)
return (NULL);
/* Eat what we can off the queue */
for (;;) {
mp = mp1->b_cont;
end = TCP_REASS_END(mp1);
TCP_REASS_SET_SEQ(mp1, 0);
TCP_REASS_SET_END(mp1, 0);
if (!mp) {
tcp->tcp_reass_tail = NULL;
break;
}
if (end != TCP_REASS_SEQ(mp)) {
mp1->b_cont = NULL;
break;
}
mp1 = mp;
}
mp1 = tcp->tcp_reass_head;
tcp->tcp_reass_head = mp;
return (mp1);
}
/* Eliminate any overlap that mp may have over later mblks */
static void
tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp)
{
uint32_t end;
mblk_t *mp1;
uint32_t u1;
tcp_stack_t *tcps = tcp->tcp_tcps;
end = TCP_REASS_END(mp);
while ((mp1 = mp->b_cont) != NULL) {
u1 = TCP_REASS_SEQ(mp1);
if (!SEQ_GT(end, u1))
break;
if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) {
mp->b_wptr -= end - u1;
TCP_REASS_SET_END(mp, u1);
TCPS_BUMP_MIB(tcps, tcpInDataPartDupSegs);
TCPS_UPDATE_MIB(tcps, tcpInDataPartDupBytes,
end - u1);
break;
}
mp->b_cont = mp1->b_cont;
TCP_REASS_SET_SEQ(mp1, 0);
TCP_REASS_SET_END(mp1, 0);
freeb(mp1);
TCPS_BUMP_MIB(tcps, tcpInDataDupSegs);
TCPS_UPDATE_MIB(tcps, tcpInDataDupBytes, end - u1);
}
if (!mp1)
tcp->tcp_reass_tail = mp;
}
/*
* This function does PAWS protection check, per RFC 7323 section 5. Requires
* that timestamp options are already processed into tcpoptp. Returns B_TRUE if
* the segment passes the PAWS test, else returns B_FALSE.
*/
boolean_t
tcp_paws_check(tcp_t *tcp, const tcp_opt_t *tcpoptp)
{
if (TSTMP_LT(tcpoptp->tcp_opt_ts_val,
tcp->tcp_ts_recent)) {
if (LBOLT_FASTPATH64 <
(tcp->tcp_last_rcv_lbolt + PAWS_TIMEOUT)) {
/* This segment is not acceptable. */
return (B_FALSE);
} else {
/*
* Connection has been idle for
* too long. Reset the timestamp
*/
tcp->tcp_ts_recent =
tcpoptp->tcp_opt_ts_val;
}
}
return (B_TRUE);
}
/*
* Defense for the SYN attack -
* 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest
* one from the list of droppable eagers. This list is a subset of q0.
* see comments before the definition of MAKE_DROPPABLE().
* 2. Don't drop a SYN request before its first timeout. This gives every
* request at least til the first timeout to complete its 3-way handshake.
* 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many
* requests currently on the queue that has timed out. This will be used
* as an indicator of whether an attack is under way, so that appropriate
* actions can be taken. (It's incremented in tcp_timer() and decremented
* either when eager goes into ESTABLISHED, or gets freed up.)
* 4. The current threshold is - # of timeout > q0len/4 => SYN alert on
* # of timeout drops back to <= q0len/32 => SYN alert off
*/
static boolean_t
tcp_drop_q0(tcp_t *tcp)
{
tcp_t *eager;
mblk_t *mp;
tcp_stack_t *tcps = tcp->tcp_tcps;
ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock));
ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0);
/* Pick oldest eager from the list of droppable eagers */
eager = tcp->tcp_eager_prev_drop_q0;
/* If list is empty. return B_FALSE */
if (eager == tcp) {
return (B_FALSE);
}
/* If allocated, the mp will be freed in tcp_clean_death_wrapper() */
if ((mp = allocb(0, BPRI_HI)) == NULL)
return (B_FALSE);
/*
* Take this eager out from the list of droppable eagers since we are
* going to drop it.
*/
MAKE_UNDROPPABLE(eager);
if (tcp->tcp_connp->conn_debug) {
(void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE,
"tcp_drop_q0: listen half-open queue (max=%d) overflow"
" (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0,
tcp->tcp_conn_req_cnt_q0,
tcp_display(tcp, NULL, DISP_PORT_ONLY));
}
TCPS_BUMP_MIB(tcps, tcpHalfOpenDrop);
/* Put a reference on the conn as we are enqueueing it in the sqeue */
CONN_INC_REF(eager->tcp_connp);
SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp,
tcp_clean_death_wrapper, eager->tcp_connp, NULL,
SQ_FILL, SQTAG_TCP_DROP_Q0);
return (B_TRUE);
}
/*
* Handle a SYN on an AF_INET6 socket; can be either IPv4 or IPv6
*/
static mblk_t *
tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp,
ip_recv_attr_t *ira)
{
tcp_t *ltcp = lconnp->conn_tcp;
tcp_t *tcp = connp->conn_tcp;
mblk_t *tpi_mp;
ipha_t *ipha;
ip6_t *ip6h;
sin6_t sin6;
uint_t ifindex = ira->ira_ruifindex;
tcp_stack_t *tcps = tcp->tcp_tcps;
if (ira->ira_flags & IRAF_IS_IPV4) {
ipha = (ipha_t *)mp->b_rptr;
connp->conn_ipversion = IPV4_VERSION;
IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_laddr_v6);
IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_faddr_v6);
connp->conn_saddr_v6 = connp->conn_laddr_v6;
sin6 = sin6_null;
sin6.sin6_addr = connp->conn_faddr_v6;
sin6.sin6_port = connp->conn_fport;
sin6.sin6_family = AF_INET6;
sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
IPCL_ZONEID(lconnp), tcps->tcps_netstack);
if (connp->conn_recv_ancillary.crb_recvdstaddr) {
sin6_t sin6d;
sin6d = sin6_null;
sin6d.sin6_addr = connp->conn_laddr_v6;
sin6d.sin6_port = connp->conn_lport;
sin6d.sin6_family = AF_INET;
tpi_mp = mi_tpi_extconn_ind(NULL,
(char *)&sin6d, sizeof (sin6_t),
(char *)&tcp,
(t_scalar_t)sizeof (intptr_t),
(char *)&sin6d, sizeof (sin6_t),
(t_scalar_t)ltcp->tcp_conn_req_seqnum);
} else {
tpi_mp = mi_tpi_conn_ind(NULL,
(char *)&sin6, sizeof (sin6_t),
(char *)&tcp, (t_scalar_t)sizeof (intptr_t),
(t_scalar_t)ltcp->tcp_conn_req_seqnum);
}
} else {
ip6h = (ip6_t *)mp->b_rptr;
connp->conn_ipversion = IPV6_VERSION;
connp->conn_laddr_v6 = ip6h->ip6_dst;
connp->conn_faddr_v6 = ip6h->ip6_src;
connp->conn_saddr_v6 = connp->conn_laddr_v6;
sin6 = sin6_null;
sin6.sin6_addr = connp->conn_faddr_v6;
sin6.sin6_port = connp->conn_fport;
sin6.sin6_family = AF_INET6;
sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
sin6.__sin6_src_id = ip_srcid_find_addr(&connp->conn_laddr_v6,
IPCL_ZONEID(lconnp), tcps->tcps_netstack);
if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) {
/* Pass up the scope_id of remote addr */
sin6.sin6_scope_id = ifindex;
} else {
sin6.sin6_scope_id = 0;
}
if (connp->conn_recv_ancillary.crb_recvdstaddr) {
sin6_t sin6d;
sin6d = sin6_null;
sin6.sin6_addr = connp->conn_laddr_v6;
sin6d.sin6_port = connp->conn_lport;
sin6d.sin6_family = AF_INET6;
if (IN6_IS_ADDR_LINKSCOPE(&connp->conn_laddr_v6))
sin6d.sin6_scope_id = ifindex;
tpi_mp = mi_tpi_extconn_ind(NULL,
(char *)&sin6d, sizeof (sin6_t),
(char *)&tcp, (t_scalar_t)sizeof (intptr_t),
(char *)&sin6d, sizeof (sin6_t),
(t_scalar_t)ltcp->tcp_conn_req_seqnum);
} else {
tpi_mp = mi_tpi_conn_ind(NULL,
(char *)&sin6, sizeof (sin6_t),
(char *)&tcp, (t_scalar_t)sizeof (intptr_t),