]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_input.c
b8fbb62d4acb1e57dcf043b4e93794d263dce447
[apple/xnu.git] / bsd / netinet / tcp_input.c
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.16 2001/08/22 00:59:12 silby Exp $
62 */
63 /*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/proc.h> /* for proc0 declaration */
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/syslog.h>
81 #include <sys/mcache.h>
82 #if !CONFIG_EMBEDDED
83 #include <sys/kasl.h>
84 #endif
85 #include <sys/kauth.h>
86 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
87
88 #include <machine/endian.h>
89
90 #include <net/if.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/ntstat.h>
94 #include <net/content_filter.h>
95 #include <net/dlil.h>
96 #include <net/multi_layer_pkt_log.h>
97
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/ip.h>
101 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
102 #include <netinet/in_var.h>
103 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
104 #include <netinet/in_pcb.h>
105 #include <netinet/ip_var.h>
106 #include <mach/sdt.h>
107 #if INET6
108 #include <netinet/ip6.h>
109 #include <netinet/icmp6.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/in6_pcb.h>
113 #endif
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_cache.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcp_cc.h>
121 #include <dev/random/randomdev.h>
122 #include <kern/zalloc.h>
123 #if INET6
124 #include <netinet6/tcp6_var.h>
125 #endif
126 #include <netinet/tcpip.h>
127 #if TCPDEBUG
128 #include <netinet/tcp_debug.h>
129 u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
130 struct tcphdr tcp_savetcp;
131 #endif /* TCPDEBUG */
132 #include <netinet/tcp_log.h>
133
134 #if IPSEC
135 #include <netinet6/ipsec.h>
136 #if INET6
137 #include <netinet6/ipsec6.h>
138 #endif
139 #include <netkey/key.h>
140 #endif /*IPSEC*/
141
142 #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
143 #include <security/mac_framework.h>
144 #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
145
146 #include <sys/kdebug.h>
147 #include <netinet/lro_ext.h>
148 #if MPTCP
149 #include <netinet/mptcp_var.h>
150 #include <netinet/mptcp.h>
151 #include <netinet/mptcp_opt.h>
152 #endif /* MPTCP */
153
154 #include <corecrypto/ccaes.h>
155
156 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
157 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
158 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
159 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
160
161 #define TCP_RTT_HISTORY_EXPIRE_TIME (60 * TCP_RETRANSHZ)
162 #define TCP_RECV_THROTTLE_WIN (5 * TCP_RETRANSHZ)
163 #define TCP_STRETCHACK_ENABLE_PKTCNT 2000
164
165 struct tcpstat tcpstat;
166
167 static int log_in_vain = 0;
168 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain,
169 CTLFLAG_RW | CTLFLAG_LOCKED, &log_in_vain, 0,
170 "Log all incoming TCP connections");
171
172 static int blackhole = 0;
173 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole,
174 CTLFLAG_RW | CTLFLAG_LOCKED, &blackhole, 0,
175 "Do not send RST when dropping refused connections");
176
177 SYSCTL_SKMEM_TCP_INT(OID_AUTO, delayed_ack,
178 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_delack_enabled, 3,
179 "Delay ACK to try and piggyback it onto a data packet");
180
181 SYSCTL_SKMEM_TCP_INT(OID_AUTO, tcp_lq_overflow,
182 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_lq_overflow, 1,
183 "Listen Queue Overflow");
184
185 SYSCTL_SKMEM_TCP_INT(OID_AUTO, recvbg, CTLFLAG_RW | CTLFLAG_LOCKED,
186 int, tcp_recv_bg, 0, "Receive background");
187
188 #if TCP_DROP_SYNFIN
189 SYSCTL_SKMEM_TCP_INT(OID_AUTO, drop_synfin,
190 CTLFLAG_RW | CTLFLAG_LOCKED, static int, drop_synfin, 1,
191 "Drop TCP packets with SYN+FIN set");
192 #endif
193
194 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
195 "TCP Segment Reassembly Queue");
196
197 static int tcp_reass_overflows = 0;
198 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows,
199 CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_reass_overflows, 0,
200 "Global number of TCP Segment Reassembly Queue Overflows");
201
202
203 SYSCTL_SKMEM_TCP_INT(OID_AUTO, slowlink_wsize, CTLFLAG_RW | CTLFLAG_LOCKED,
204 __private_extern__ int, slowlink_wsize, 8192,
205 "Maximum advertised window size for slowlink");
206
207 SYSCTL_SKMEM_TCP_INT(OID_AUTO, maxseg_unacked,
208 CTLFLAG_RW | CTLFLAG_LOCKED, int, maxseg_unacked, 8,
209 "Maximum number of outstanding segments left unacked");
210
211 SYSCTL_SKMEM_TCP_INT(OID_AUTO, rfc3465, CTLFLAG_RW | CTLFLAG_LOCKED,
212 int, tcp_do_rfc3465, 1, "");
213
214 SYSCTL_SKMEM_TCP_INT(OID_AUTO, rfc3465_lim2,
215 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_do_rfc3465_lim2, 1,
216 "Appropriate bytes counting w/ L=2*SMSS");
217
218 int rtt_samples_per_slot = 20;
219
220 int tcp_acc_iaj_high_thresh = ACC_IAJ_HIGH_THRESH;
221 u_int32_t tcp_autorcvbuf_inc_shift = 3;
222 SYSCTL_SKMEM_TCP_INT(OID_AUTO, recv_allowed_iaj,
223 CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_allowed_iaj, ALLOWED_IAJ,
224 "Allowed inter-packet arrival jiter");
225 #if (DEVELOPMENT || DEBUG)
226 SYSCTL_INT(_net_inet_tcp, OID_AUTO, acc_iaj_high_thresh,
227 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_acc_iaj_high_thresh, 0,
228 "Used in calculating maximum accumulated IAJ");
229
230 SYSCTL_INT(_net_inet_tcp, OID_AUTO, autorcvbufincshift,
231 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_autorcvbuf_inc_shift, 0,
232 "Shift for increment in receive socket buffer size");
233 #endif /* (DEVELOPMENT || DEBUG) */
234
235 SYSCTL_SKMEM_TCP_INT(OID_AUTO, doautorcvbuf,
236 CTLFLAG_RW | CTLFLAG_LOCKED, u_int32_t, tcp_do_autorcvbuf, 1,
237 "Enable automatic socket buffer tuning");
238
239 SYSCTL_SKMEM_TCP_INT(OID_AUTO, autorcvbufmax,
240 CTLFLAG_RW | CTLFLAG_LOCKED, u_int32_t, tcp_autorcvbuf_max, 512 * 1024,
241 "Maximum receive socket buffer size");
242
243 #if CONFIG_EMBEDDED
244 int sw_lro = 1;
245 #else
246 int sw_lro = 0;
247 #endif /* !CONFIG_EMBEDDED */
248 SYSCTL_INT(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_LOCKED,
249 &sw_lro, 0, "Used to coalesce TCP packets");
250
251 int lrodebug = 0;
252 SYSCTL_INT(_net_inet_tcp, OID_AUTO, lrodbg,
253 CTLFLAG_RW | CTLFLAG_LOCKED, &lrodebug, 0,
254 "Used to debug SW LRO");
255
256 int lro_start = 4;
257 SYSCTL_INT(_net_inet_tcp, OID_AUTO, lro_startcnt,
258 CTLFLAG_RW | CTLFLAG_LOCKED, &lro_start, 0,
259 "Segments for starting LRO computed as power of 2");
260
261 int limited_txmt = 1;
262 int early_rexmt = 1;
263 int sack_ackadv = 1;
264 int tcp_dsack_enable = 1;
265
266 #if (DEVELOPMENT || DEBUG)
267 SYSCTL_INT(_net_inet_tcp, OID_AUTO, limited_transmit,
268 CTLFLAG_RW | CTLFLAG_LOCKED, &limited_txmt, 0,
269 "Enable limited transmit");
270
271 SYSCTL_INT(_net_inet_tcp, OID_AUTO, early_rexmt,
272 CTLFLAG_RW | CTLFLAG_LOCKED, &early_rexmt, 0,
273 "Enable Early Retransmit");
274
275 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_ackadv,
276 CTLFLAG_RW | CTLFLAG_LOCKED, &sack_ackadv, 0,
277 "Use SACK with cumulative ack advancement as a dupack");
278
279 SYSCTL_INT(_net_inet_tcp, OID_AUTO, dsack_enable,
280 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_dsack_enable, 0,
281 "use DSACK TCP option to report duplicate segments");
282
283 #endif /* (DEVELOPMENT || DEBUG) */
284 int tcp_disable_access_to_stats = 1;
285 SYSCTL_INT(_net_inet_tcp, OID_AUTO, disable_access_to_stats,
286 CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_disable_access_to_stats, 0,
287 "Disable access to tcpstat");
288
289 SYSCTL_SKMEM_TCP_INT(OID_AUTO, challengeack_limit,
290 CTLFLAG_RW | CTLFLAG_LOCKED, uint32_t, tcp_challengeack_limit, 10,
291 "Maximum number of challenge ACKs per connection per second");
292
293 SYSCTL_SKMEM_TCP_INT(OID_AUTO, do_rfc5961,
294 CTLFLAG_RW | CTLFLAG_LOCKED, static int, tcp_do_rfc5961, 1,
295 "Enable/Disable full RFC 5961 compliance");
296
297 extern int tcp_acc_iaj_high;
298 extern int tcp_acc_iaj_react_limit;
299
300 int tcprexmtthresh = 3;
301
302 u_int32_t tcp_now;
303 struct timeval tcp_uptime; /* uptime when tcp_now was last updated */
304 lck_spin_t *tcp_uptime_lock; /* Used to sychronize updates to tcp_now */
305
306 struct inpcbhead tcb;
307 #define tcb6 tcb /* for KAME src sync over BSD*'s */
308 struct inpcbinfo tcbinfo;
309
310 static void tcp_dooptions(struct tcpcb *, u_char *, int, struct tcphdr *,
311 struct tcpopt *);
312 static void tcp_finalize_options(struct tcpcb *, struct tcpopt *, unsigned int);
313 static void tcp_pulloutofband(struct socket *,
314 struct tcphdr *, struct mbuf *, int);
315 static void tcp_xmit_timer(struct tcpcb *, int, u_int32_t, tcp_seq);
316 static inline unsigned int tcp_maxmtu(struct rtentry *);
317 static inline int tcp_stretch_ack_enable(struct tcpcb *tp, int thflags);
318 static inline void tcp_adaptive_rwtimo_check(struct tcpcb *, int);
319
320 #if TRAFFIC_MGT
321 static inline void update_iaj_state(struct tcpcb *tp, uint32_t tlen,
322 int reset_size);
323 void compute_iaj(struct tcpcb *tp, int nlropkts, int lro_delay_factor);
324 static void compute_iaj_meat(struct tcpcb *tp, uint32_t cur_iaj);
325 #endif /* TRAFFIC_MGT */
326
327 #if INET6
328 static inline unsigned int tcp_maxmtu6(struct rtentry *);
329 #endif
330
331 unsigned int get_maxmtu(struct rtentry *);
332
333 static void tcp_sbrcv_grow(struct tcpcb *tp, struct sockbuf *sb,
334 struct tcpopt *to, u_int32_t tlen, u_int32_t rcvbuf_max);
335 void tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sb);
336 static void tcp_sbsnd_trim(struct sockbuf *sbsnd);
337 static inline void tcp_sbrcv_tstmp_check(struct tcpcb *tp);
338 static inline void tcp_sbrcv_reserve(struct tcpcb *tp, struct sockbuf *sb,
339 u_int32_t newsize, u_int32_t idealsize, u_int32_t rcvbuf_max);
340 static void tcp_bad_rexmt_restore_state(struct tcpcb *tp, struct tcphdr *th);
341 static void tcp_compute_rtt(struct tcpcb *tp, struct tcpopt *to,
342 struct tcphdr *th);
343 static void tcp_early_rexmt_check(struct tcpcb *tp, struct tcphdr *th);
344 static void tcp_bad_rexmt_check(struct tcpcb *tp, struct tcphdr *th,
345 struct tcpopt *to);
346 /*
347 * Constants used for resizing receive socket buffer
348 * when timestamps are not supported
349 */
350 #define TCPTV_RCVNOTS_QUANTUM 100
351 #define TCP_RCVNOTS_BYTELEVEL 204800
352
353 /*
354 * Constants used for limiting early retransmits
355 * to 10 per minute.
356 */
357 #define TCP_EARLY_REXMT_WIN (60 * TCP_RETRANSHZ) /* 60 seconds */
358 #define TCP_EARLY_REXMT_LIMIT 10
359
360 extern void ipfwsyslog( int level, const char *format, ...);
361 extern int fw_verbose;
362
363 #if IPFIREWALL
364 extern void ipfw_stealth_stats_incr_tcp(void);
365
366 #define log_in_vain_log( a ) { \
367 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
368 ipfwsyslog a ; \
369 } else if ( (log_in_vain == 4 ) && (fw_verbose == 2)) { \
370 ipfw_stealth_stats_incr_tcp(); \
371 } \
372 else log a ; \
373 }
374 #else
375 #define log_in_vain_log( a ) { log a; }
376 #endif
377
378 int tcp_rcvunackwin = TCPTV_UNACKWIN;
379 int tcp_maxrcvidle = TCPTV_MAXRCVIDLE;
380 SYSCTL_SKMEM_TCP_INT(OID_AUTO, rcvsspktcnt, CTLFLAG_RW | CTLFLAG_LOCKED,
381 int, tcp_rcvsspktcnt, TCP_RCV_SS_PKTCOUNT, "packets to be seen before receiver stretches acks");
382
383 #define DELAY_ACK(tp, th) \
384 (CC_ALGO(tp)->delay_ack != NULL && CC_ALGO(tp)->delay_ack(tp, th))
385
386 static int tcp_dropdropablreq(struct socket *head);
387 static void tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th);
388 static void update_base_rtt(struct tcpcb *tp, uint32_t rtt);
389 void tcp_set_background_cc(struct socket *so);
390 void tcp_set_foreground_cc(struct socket *so);
391 static void tcp_set_new_cc(struct socket *so, uint16_t cc_index);
392 static void tcp_bwmeas_check(struct tcpcb *tp);
393
394 #if TRAFFIC_MGT
395 void
396 reset_acc_iaj(struct tcpcb *tp)
397 {
398 tp->acc_iaj = 0;
399 CLEAR_IAJ_STATE(tp);
400 }
401
402 static inline void
403 update_iaj_state(struct tcpcb *tp, uint32_t size, int rst_size)
404 {
405 if (rst_size > 0) {
406 tp->iaj_size = 0;
407 }
408 if (tp->iaj_size == 0 || size >= tp->iaj_size) {
409 tp->iaj_size = size;
410 tp->iaj_rcv_ts = tcp_now;
411 tp->iaj_small_pkt = 0;
412 }
413 }
414
415 /* For every 32 bit unsigned integer(v), this function will find the
416 * largest integer n such that (n*n <= v). This takes at most 16 iterations
417 * irrespective of the value of v and does not involve multiplications.
418 */
419 static inline int
420 isqrt(unsigned int val)
421 {
422 unsigned int sqrt_cache[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
423 unsigned int temp, g = 0, b = 0x8000, bshft = 15;
424 if (val <= 100) {
425 for (g = 0; g <= 10; ++g) {
426 if (sqrt_cache[g] > val) {
427 g--;
428 break;
429 } else if (sqrt_cache[g] == val) {
430 break;
431 }
432 }
433 } else {
434 do {
435 temp = (((g << 1) + b) << (bshft--));
436 if (val >= temp) {
437 g += b;
438 val -= temp;
439 }
440 b >>= 1;
441 } while (b > 0 && val > 0);
442 }
443 return g;
444 }
445
446 /*
447 * With LRO, roughly estimate the inter arrival time between
448 * each sub coalesced packet as an average. Count the delay
449 * cur_iaj to be the delay between the last packet received
450 * and the first packet of the LRO stream. Due to round off errors
451 * cur_iaj may be the same as lro_delay_factor. Averaging has
452 * round off errors too. lro_delay_factor may be close to 0
453 * in steady state leading to lower values fed to compute_iaj_meat.
454 */
455 void
456 compute_iaj(struct tcpcb *tp, int nlropkts, int lro_delay_factor)
457 {
458 uint32_t cur_iaj = tcp_now - tp->iaj_rcv_ts;
459 uint32_t timediff = 0;
460
461 if (cur_iaj >= lro_delay_factor) {
462 cur_iaj = cur_iaj - lro_delay_factor;
463 }
464
465 compute_iaj_meat(tp, cur_iaj);
466
467 if (nlropkts <= 1) {
468 return;
469 }
470
471 nlropkts--;
472
473 timediff = lro_delay_factor / nlropkts;
474
475 while (nlropkts > 0) {
476 compute_iaj_meat(tp, timediff);
477 nlropkts--;
478 }
479 }
480
481 static
482 void
483 compute_iaj_meat(struct tcpcb *tp, uint32_t cur_iaj)
484 {
485 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
486 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
487 */
488 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
489 #define IAJ_DIV_SHIFT 4
490 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
491
492 uint32_t allowed_iaj, acc_iaj = 0;
493
494 uint32_t mean, temp;
495 int32_t cur_iaj_dev;
496
497 cur_iaj_dev = (cur_iaj - tp->avg_iaj);
498
499 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
500 * may have a constant jitter more than that. We detect this by
501 * using standard deviation.
502 */
503 allowed_iaj = tp->avg_iaj + tp->std_dev_iaj;
504 if (allowed_iaj < tcp_allowed_iaj) {
505 allowed_iaj = tcp_allowed_iaj;
506 }
507
508 /* Initially when the connection starts, the senders congestion
509 * window is small. During this period we avoid throttling a
510 * connection because we do not have a good starting point for
511 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
512 * the first few packets.
513 */
514 if (tp->iaj_pktcnt > IAJ_IGNORE_PKTCNT) {
515 if (cur_iaj <= allowed_iaj) {
516 if (tp->acc_iaj >= 2) {
517 acc_iaj = tp->acc_iaj - 2;
518 } else {
519 acc_iaj = 0;
520 }
521 } else {
522 acc_iaj = tp->acc_iaj + (cur_iaj - allowed_iaj);
523 }
524
525 if (acc_iaj > MAX_ACC_IAJ) {
526 acc_iaj = MAX_ACC_IAJ;
527 }
528 tp->acc_iaj = acc_iaj;
529 }
530
531 /* Compute weighted average where the history has a weight of
532 * 15 out of 16 and the current value has a weight of 1 out of 16.
533 * This will make the short-term measurements have more weight.
534 *
535 * The addition of 8 will help to round-up the value
536 * instead of round-down
537 */
538 tp->avg_iaj = (((tp->avg_iaj << IAJ_DIV_SHIFT) - tp->avg_iaj)
539 + cur_iaj + IAJ_ROUNDUP_CONST) >> IAJ_DIV_SHIFT;
540
541 /* Compute Root-mean-square of deviation where mean is a weighted
542 * average as described above.
543 */
544 temp = tp->std_dev_iaj * tp->std_dev_iaj;
545 mean = (((temp << IAJ_DIV_SHIFT) - temp)
546 + (cur_iaj_dev * cur_iaj_dev)
547 + IAJ_ROUNDUP_CONST) >> IAJ_DIV_SHIFT;
548
549 tp->std_dev_iaj = isqrt(mean);
550
551 DTRACE_TCP3(iaj, struct tcpcb *, tp, uint32_t, cur_iaj,
552 uint32_t, allowed_iaj);
553
554 return;
555 }
556 #endif /* TRAFFIC_MGT */
557
558 /*
559 * Perform rate limit check per connection per second
560 * tp->t_challengeack_last is the last_time diff was greater than 1sec
561 * tp->t_challengeack_count is the number of ACKs sent (within 1sec)
562 * Return TRUE if we shouldn't send the ACK due to rate limitation
563 * Return FALSE if it is still ok to send challenge ACK
564 */
565 static boolean_t
566 tcp_is_ack_ratelimited(struct tcpcb *tp)
567 {
568 boolean_t ret = TRUE;
569 uint32_t now = tcp_now;
570 int32_t diff = 0;
571
572 diff = timer_diff(now, 0, tp->t_challengeack_last, 0);
573 /* If it is first time or diff > 1000ms,
574 * update the challengeack_last and reset the
575 * current count of ACKs
576 */
577 if (tp->t_challengeack_last == 0 || diff >= 1000) {
578 tp->t_challengeack_last = now;
579 tp->t_challengeack_count = 0;
580 ret = FALSE;
581 } else if (tp->t_challengeack_count < tcp_challengeack_limit) {
582 ret = FALSE;
583 }
584
585 /* Careful about wrap-around */
586 if (ret == FALSE && (tp->t_challengeack_count + 1 > 0)) {
587 tp->t_challengeack_count++;
588 }
589
590 return ret;
591 }
592
593 /* Check if enough amount of data has been acknowledged since
594 * bw measurement was started
595 */
596 static void
597 tcp_bwmeas_check(struct tcpcb *tp)
598 {
599 int32_t bw_meas_bytes;
600 uint32_t bw, bytes, elapsed_time;
601
602 if (SEQ_LEQ(tp->snd_una, tp->t_bwmeas->bw_start)) {
603 return;
604 }
605
606 bw_meas_bytes = tp->snd_una - tp->t_bwmeas->bw_start;
607 if ((tp->t_flagsext & TF_BWMEAS_INPROGRESS) &&
608 bw_meas_bytes >= (int32_t)(tp->t_bwmeas->bw_size)) {
609 bytes = bw_meas_bytes;
610 elapsed_time = tcp_now - tp->t_bwmeas->bw_ts;
611 if (elapsed_time > 0) {
612 bw = bytes / elapsed_time;
613 if (bw > 0) {
614 if (tp->t_bwmeas->bw_sndbw > 0) {
615 tp->t_bwmeas->bw_sndbw =
616 (((tp->t_bwmeas->bw_sndbw << 3)
617 - tp->t_bwmeas->bw_sndbw)
618 + bw) >> 3;
619 } else {
620 tp->t_bwmeas->bw_sndbw = bw;
621 }
622
623 /* Store the maximum value */
624 if (tp->t_bwmeas->bw_sndbw_max == 0) {
625 tp->t_bwmeas->bw_sndbw_max =
626 tp->t_bwmeas->bw_sndbw;
627 } else {
628 tp->t_bwmeas->bw_sndbw_max =
629 max(tp->t_bwmeas->bw_sndbw,
630 tp->t_bwmeas->bw_sndbw_max);
631 }
632 }
633 }
634 tp->t_flagsext &= ~(TF_BWMEAS_INPROGRESS);
635 }
636 }
637
638 static int
639 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m,
640 struct ifnet *ifp, int *dowakeup)
641 {
642 struct tseg_qent *q;
643 struct tseg_qent *p = NULL;
644 struct tseg_qent *nq;
645 struct tseg_qent *te = NULL;
646 struct inpcb *inp = tp->t_inpcb;
647 struct socket *so = inp->inp_socket;
648 int flags = 0;
649 struct mbuf *oodata = NULL;
650 int copy_oodata = 0;
651 u_int16_t qlimit;
652 boolean_t cell = IFNET_IS_CELLULAR(ifp);
653 boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
654 boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
655 boolean_t dsack_set = FALSE;
656
657 /*
658 * Call with th==0 after become established to
659 * force pre-ESTABLISHED data up to user socket.
660 */
661 if (th == NULL) {
662 goto present;
663 }
664
665 /*
666 * If the reassembly queue already has entries or if we are going
667 * to add a new one, then the connection has reached a loss state.
668 * Reset the stretch-ack algorithm at this point.
669 */
670 tcp_reset_stretch_ack(tp);
671
672 #if TRAFFIC_MGT
673 if (tp->acc_iaj > 0) {
674 reset_acc_iaj(tp);
675 }
676 #endif /* TRAFFIC_MGT */
677
678 /*
679 * Limit the number of segments in the reassembly queue to prevent
680 * holding on to too many segments (and thus running out of mbufs).
681 * Make sure to let the missing segment through which caused this
682 * queue. Always keep one global queue entry spare to be able to
683 * process the missing segment.
684 */
685 qlimit = min(max(100, so->so_rcv.sb_hiwat >> 10),
686 (TCP_AUTORCVBUF_MAX(ifp) >> 10));
687 if (th->th_seq != tp->rcv_nxt &&
688 (tp->t_reassqlen + 1) >= qlimit) {
689 tcp_reass_overflows++;
690 tcpstat.tcps_rcvmemdrop++;
691 m_freem(m);
692 *tlenp = 0;
693 return 0;
694 }
695
696 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
697 te = (struct tseg_qent *) zalloc(tcp_reass_zone);
698 if (te == NULL) {
699 tcpstat.tcps_rcvmemdrop++;
700 m_freem(m);
701 return 0;
702 }
703 tp->t_reassqlen++;
704
705 /*
706 * Find a segment which begins after this one does.
707 */
708 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
709 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) {
710 break;
711 }
712 p = q;
713 }
714
715 /*
716 * If there is a preceding segment, it may provide some of
717 * our data already. If so, drop the data from the incoming
718 * segment. If it provides all of our data, drop us.
719 */
720 if (p != NULL) {
721 int i;
722 /* conversion to int (in i) handles seq wraparound */
723 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
724 if (i > 0) {
725 if (TCP_DSACK_ENABLED(tp) && i > 1) {
726 /*
727 * Note duplicate data sequnce numbers
728 * to report in DSACK option
729 */
730 tp->t_dsack_lseq = th->th_seq;
731 tp->t_dsack_rseq = th->th_seq +
732 min(i, *tlenp);
733
734 /*
735 * Report only the first part of partial/
736 * non-contiguous duplicate sequence space
737 */
738 dsack_set = TRUE;
739 }
740 if (i >= *tlenp) {
741 tcpstat.tcps_rcvduppack++;
742 tcpstat.tcps_rcvdupbyte += *tlenp;
743 if (nstat_collect) {
744 nstat_route_rx(inp->inp_route.ro_rt,
745 1, *tlenp,
746 NSTAT_RX_FLAG_DUPLICATE);
747 INP_ADD_STAT(inp, cell, wifi, wired,
748 rxpackets, 1);
749 INP_ADD_STAT(inp, cell, wifi, wired,
750 rxbytes, *tlenp);
751 tp->t_stat.rxduplicatebytes += *tlenp;
752 inp_set_activity_bitmap(inp);
753 }
754 m_freem(m);
755 zfree(tcp_reass_zone, te);
756 te = NULL;
757 tp->t_reassqlen--;
758 /*
759 * Try to present any queued data
760 * at the left window edge to the user.
761 * This is needed after the 3-WHS
762 * completes.
763 */
764 goto present;
765 }
766 m_adj(m, i);
767 *tlenp -= i;
768 th->th_seq += i;
769 }
770 }
771 tp->t_rcvoopack++;
772 tcpstat.tcps_rcvoopack++;
773 tcpstat.tcps_rcvoobyte += *tlenp;
774 if (nstat_collect) {
775 nstat_route_rx(inp->inp_route.ro_rt, 1, *tlenp,
776 NSTAT_RX_FLAG_OUT_OF_ORDER);
777 INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1);
778 INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, *tlenp);
779 tp->t_stat.rxoutoforderbytes += *tlenp;
780 inp_set_activity_bitmap(inp);
781 }
782
783 /*
784 * While we overlap succeeding segments trim them or,
785 * if they are completely covered, dequeue them.
786 */
787 while (q) {
788 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
789 if (i <= 0) {
790 break;
791 }
792
793 /*
794 * Report only the first part of partial/non-contiguous
795 * duplicate segment in dsack option. The variable
796 * dsack_set will be true if a previous entry has some of
797 * the duplicate sequence space.
798 */
799 if (TCP_DSACK_ENABLED(tp) && i > 1 && !dsack_set) {
800 if (tp->t_dsack_lseq == 0) {
801 tp->t_dsack_lseq = q->tqe_th->th_seq;
802 tp->t_dsack_rseq =
803 tp->t_dsack_lseq + min(i, q->tqe_len);
804 } else {
805 /*
806 * this segment overlaps data in multple
807 * entries in the reassembly queue, move
808 * the right sequence number further.
809 */
810 tp->t_dsack_rseq =
811 tp->t_dsack_rseq + min(i, q->tqe_len);
812 }
813 }
814 if (i < q->tqe_len) {
815 q->tqe_th->th_seq += i;
816 q->tqe_len -= i;
817 m_adj(q->tqe_m, i);
818 break;
819 }
820
821 nq = LIST_NEXT(q, tqe_q);
822 LIST_REMOVE(q, tqe_q);
823 m_freem(q->tqe_m);
824 zfree(tcp_reass_zone, q);
825 tp->t_reassqlen--;
826 q = nq;
827 }
828
829 /* Insert the new segment queue entry into place. */
830 te->tqe_m = m;
831 te->tqe_th = th;
832 te->tqe_len = *tlenp;
833
834 if (p == NULL) {
835 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
836 } else {
837 LIST_INSERT_AFTER(p, te, tqe_q);
838 }
839
840 /*
841 * New out-of-order data exists, and is pointed to by
842 * queue entry te. Set copy_oodata to 1 so out-of-order data
843 * can be copied off to sockbuf after in-order data
844 * is copied off.
845 */
846 if (!(so->so_state & SS_CANTRCVMORE)) {
847 copy_oodata = 1;
848 }
849
850 present:
851 /*
852 * Present data to user, advancing rcv_nxt through
853 * completed sequence space.
854 */
855 if (!TCPS_HAVEESTABLISHED(tp->t_state)) {
856 return 0;
857 }
858 q = LIST_FIRST(&tp->t_segq);
859 if (!q || q->tqe_th->th_seq != tp->rcv_nxt) {
860 /* Stop using LRO once out of order packets arrive */
861 if (tp->t_flagsext & TF_LRO_OFFLOADED) {
862 tcp_lro_remove_state(inp->inp_laddr, inp->inp_faddr,
863 th->th_dport, th->th_sport);
864 tp->t_flagsext &= ~TF_LRO_OFFLOADED;
865 }
866
867 /*
868 * continue processing if out-of-order data
869 * can be delivered
870 */
871 if (q && (so->so_flags & SOF_ENABLE_MSGS)) {
872 goto msg_unordered_delivery;
873 }
874
875 return 0;
876 }
877
878 /*
879 * If there is already another thread doing reassembly for this
880 * connection, it is better to let it finish the job --
881 * (radar 16316196)
882 */
883 if (tp->t_flagsext & TF_REASS_INPROG) {
884 return 0;
885 }
886
887 tp->t_flagsext |= TF_REASS_INPROG;
888 /* lost packet was recovered, so ooo data can be returned */
889 tcpstat.tcps_recovered_pkts++;
890
891 do {
892 tp->rcv_nxt += q->tqe_len;
893 flags = q->tqe_th->th_flags & TH_FIN;
894 LIST_REMOVE(q, tqe_q);
895 if (so->so_state & SS_CANTRCVMORE) {
896 m_freem(q->tqe_m);
897 } else {
898 /*
899 * The mbuf may be freed after it has been added to the
900 * receive socket buffer so we reinitialize th to point
901 * to a safe copy of the TCP header
902 */
903 struct tcphdr saved_tcphdr = {};
904
905 so_recv_data_stat(so, q->tqe_m, 0); /* XXXX */
906 if (so->so_flags & SOF_ENABLE_MSGS) {
907 /*
908 * Append the inorder data as a message to the
909 * receive socket buffer. Also check to see if
910 * the data we are about to deliver is the same
911 * data that we wanted to pass up to the user
912 * out of order. If so, reset copy_oodata --
913 * the received data filled a gap, and
914 * is now in order!
915 */
916 if (q == te) {
917 copy_oodata = 0;
918 }
919 }
920 memcpy(&saved_tcphdr, th, sizeof(struct tcphdr));
921 if (sbappendstream_rcvdemux(so, q->tqe_m,
922 q->tqe_th->th_seq - (tp->irs + 1), 0)) {
923 *dowakeup = 1;
924 }
925 th = &saved_tcphdr;
926
927 if (tp->t_flagsext & TF_LRO_OFFLOADED) {
928 tcp_update_lro_seq(tp->rcv_nxt,
929 inp->inp_laddr, inp->inp_faddr,
930 th->th_dport, th->th_sport);
931 }
932 }
933 zfree(tcp_reass_zone, q);
934 tp->t_reassqlen--;
935 q = LIST_FIRST(&tp->t_segq);
936 } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
937 tp->t_flagsext &= ~TF_REASS_INPROG;
938
939 #if INET6
940 if ((inp->inp_vflag & INP_IPV6) != 0) {
941 KERNEL_DEBUG(DBG_LAYER_BEG,
942 ((inp->inp_fport << 16) | inp->inp_lport),
943 (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
944 (inp->in6p_faddr.s6_addr16[0] & 0xffff)),
945 0, 0, 0);
946 } else
947 #endif
948 {
949 KERNEL_DEBUG(DBG_LAYER_BEG,
950 ((inp->inp_fport << 16) | inp->inp_lport),
951 (((inp->inp_laddr.s_addr & 0xffff) << 16) |
952 (inp->inp_faddr.s_addr & 0xffff)),
953 0, 0, 0);
954 }
955
956 msg_unordered_delivery:
957 /* Deliver out-of-order data as a message */
958 if (te && (so->so_flags & SOF_ENABLE_MSGS) && copy_oodata && te->tqe_len) {
959 /*
960 * make a copy of the mbuf to be delivered up to
961 * the user, and add it to the sockbuf
962 */
963 oodata = m_copym(te->tqe_m, 0, M_COPYALL, M_DONTWAIT);
964 if (oodata != NULL) {
965 if (sbappendmsgstream_rcv(&so->so_rcv, oodata,
966 te->tqe_th->th_seq - (tp->irs + 1), 1)) {
967 *dowakeup = 1;
968 tcpstat.tcps_msg_unopkts++;
969 } else {
970 tcpstat.tcps_msg_unoappendfail++;
971 }
972 }
973 }
974
975 return flags;
976 }
977
978 /*
979 * Reduce congestion window -- used when ECN is seen or when a tail loss
980 * probe recovers the last packet.
981 */
982 static void
983 tcp_reduce_congestion_window(
984 struct tcpcb *tp)
985 {
986 /*
987 * If the current tcp cc module has
988 * defined a hook for tasks to run
989 * before entering FR, call it
990 */
991 if (CC_ALGO(tp)->pre_fr != NULL) {
992 CC_ALGO(tp)->pre_fr(tp);
993 }
994 ENTER_FASTRECOVERY(tp);
995 if (tp->t_flags & TF_SENTFIN) {
996 tp->snd_recover = tp->snd_max - 1;
997 } else {
998 tp->snd_recover = tp->snd_max;
999 }
1000 tp->t_timer[TCPT_REXMT] = 0;
1001 tp->t_timer[TCPT_PTO] = 0;
1002 tp->t_rtttime = 0;
1003 if (tp->t_flagsext & TF_CWND_NONVALIDATED) {
1004 tcp_cc_adjust_nonvalidated_cwnd(tp);
1005 } else {
1006 tp->snd_cwnd = tp->snd_ssthresh +
1007 tp->t_maxseg * tcprexmtthresh;
1008 }
1009 }
1010
1011 /*
1012 * This function is called upon reception of data on a socket. It's purpose is
1013 * to handle the adaptive keepalive timers that monitor whether the connection
1014 * is making progress. First the adaptive read-timer, second the TFO probe-timer.
1015 *
1016 * The application wants to get an event if there is a stall during read.
1017 * Set the initial keepalive timeout to be equal to twice RTO.
1018 *
1019 * If the outgoing interface is in marginal conditions, we need to
1020 * enable read probes for that too.
1021 */
1022 static inline void
1023 tcp_adaptive_rwtimo_check(struct tcpcb *tp, int tlen)
1024 {
1025 struct ifnet *outifp = tp->t_inpcb->inp_last_outifp;
1026
1027 if ((tp->t_adaptive_rtimo > 0 ||
1028 (outifp != NULL &&
1029 (outifp->if_eflags & IFEF_PROBE_CONNECTIVITY)))
1030 && tlen > 0 &&
1031 tp->t_state == TCPS_ESTABLISHED) {
1032 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
1033 (TCP_REXMTVAL(tp) << 1));
1034 tp->t_flagsext |= TF_DETECT_READSTALL;
1035 tp->t_rtimo_probes = 0;
1036 }
1037 }
1038
1039 inline void
1040 tcp_keepalive_reset(struct tcpcb *tp)
1041 {
1042 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
1043 TCP_CONN_KEEPIDLE(tp));
1044 tp->t_flagsext &= ~(TF_DETECT_READSTALL);
1045 tp->t_rtimo_probes = 0;
1046 }
1047
1048 /*
1049 * TCP input routine, follows pages 65-76 of the
1050 * protocol specification dated September, 1981 very closely.
1051 */
1052 #if INET6
1053 int
1054 tcp6_input(struct mbuf **mp, int *offp, int proto)
1055 {
1056 #pragma unused(proto)
1057 struct mbuf *m = *mp;
1058 uint32_t ia6_flags;
1059 struct ifnet *ifp = m->m_pkthdr.rcvif;
1060
1061 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), return IPPROTO_DONE);
1062
1063 /* Expect 32-bit aligned data pointer on strict-align platforms */
1064 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
1065
1066 /*
1067 * draft-itojun-ipv6-tcp-to-anycast
1068 * better place to put this in?
1069 */
1070 if (ip6_getdstifaddr_info(m, NULL, &ia6_flags) == 0) {
1071 if (ia6_flags & IN6_IFF_ANYCAST) {
1072 struct ip6_hdr *ip6;
1073
1074 ip6 = mtod(m, struct ip6_hdr *);
1075 icmp6_error(m, ICMP6_DST_UNREACH,
1076 ICMP6_DST_UNREACH_ADDR,
1077 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
1078
1079 IF_TCP_STATINC(ifp, icmp6unreach);
1080
1081 return IPPROTO_DONE;
1082 }
1083 }
1084
1085 tcp_input(m, *offp);
1086 return IPPROTO_DONE;
1087 }
1088 #endif
1089
1090 /* Depending on the usage of mbuf space in the system, this function
1091 * will return true or false. This is used to determine if a socket
1092 * buffer can take more memory from the system for auto-tuning or not.
1093 */
1094 u_int8_t
1095 tcp_cansbgrow(struct sockbuf *sb)
1096 {
1097 /* Calculate the host level space limit in terms of MSIZE buffers.
1098 * We can use a maximum of half of the available mbuf space for
1099 * socket buffers.
1100 */
1101 u_int32_t mblim = ((nmbclusters >> 1) << (MCLSHIFT - MSIZESHIFT));
1102
1103 /* Calculate per sb limit in terms of bytes. We optimize this limit
1104 * for upto 16 socket buffers.
1105 */
1106
1107 u_int32_t sbspacelim = ((nmbclusters >> 4) << MCLSHIFT);
1108
1109 if ((total_sbmb_cnt < mblim) &&
1110 (sb->sb_hiwat < sbspacelim)) {
1111 return 1;
1112 } else {
1113 OSIncrementAtomic64(&sbmb_limreached);
1114 }
1115 return 0;
1116 }
1117
1118 static void
1119 tcp_sbrcv_reserve(struct tcpcb *tp, struct sockbuf *sbrcv,
1120 u_int32_t newsize, u_int32_t idealsize, u_int32_t rcvbuf_max)
1121 {
1122 /* newsize should not exceed max */
1123 newsize = min(newsize, rcvbuf_max);
1124
1125 /* The receive window scale negotiated at the
1126 * beginning of the connection will also set a
1127 * limit on the socket buffer size
1128 */
1129 newsize = min(newsize, TCP_MAXWIN << tp->rcv_scale);
1130
1131 /* Set new socket buffer size */
1132 if (newsize > sbrcv->sb_hiwat &&
1133 (sbreserve(sbrcv, newsize) == 1)) {
1134 sbrcv->sb_idealsize = min(max(sbrcv->sb_idealsize,
1135 (idealsize != 0) ? idealsize : newsize), rcvbuf_max);
1136
1137 /* Again check the limit set by the advertised
1138 * window scale
1139 */
1140 sbrcv->sb_idealsize = min(sbrcv->sb_idealsize,
1141 TCP_MAXWIN << tp->rcv_scale);
1142 }
1143 }
1144
1145 /*
1146 * This function is used to grow a receive socket buffer. It
1147 * will take into account system-level memory usage and the
1148 * bandwidth available on the link to make a decision.
1149 */
1150 static void
1151 tcp_sbrcv_grow(struct tcpcb *tp, struct sockbuf *sbrcv,
1152 struct tcpopt *to, u_int32_t pktlen, u_int32_t rcvbuf_max)
1153 {
1154 struct socket *so = sbrcv->sb_so;
1155
1156 /*
1157 * Do not grow the receive socket buffer if
1158 * - auto resizing is disabled, globally or on this socket
1159 * - the high water mark already reached the maximum
1160 * - the stream is in background and receive side is being
1161 * throttled
1162 * - if there are segments in reassembly queue indicating loss,
1163 * do not need to increase recv window during recovery as more
1164 * data is not going to be sent. A duplicate ack sent during
1165 * recovery should not change the receive window
1166 */
1167 if (tcp_do_autorcvbuf == 0 ||
1168 (sbrcv->sb_flags & SB_AUTOSIZE) == 0 ||
1169 tcp_cansbgrow(sbrcv) == 0 ||
1170 sbrcv->sb_hiwat >= rcvbuf_max ||
1171 (tp->t_flagsext & TF_RECV_THROTTLE) ||
1172 (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) ||
1173 !LIST_EMPTY(&tp->t_segq)) {
1174 /* Can not resize the socket buffer, just return */
1175 goto out;
1176 }
1177
1178 if (TSTMP_GT(tcp_now,
1179 tp->rfbuf_ts + TCPTV_RCVBUFIDLE)) {
1180 /* If there has been an idle period in the
1181 * connection, just restart the measurement
1182 */
1183 goto out;
1184 }
1185
1186 if (!TSTMP_SUPPORTED(tp)) {
1187 /*
1188 * Timestamp option is not supported on this connection.
1189 * If the connection reached a state to indicate that
1190 * the receive socket buffer needs to grow, increase
1191 * the high water mark.
1192 */
1193 if (TSTMP_GEQ(tcp_now,
1194 tp->rfbuf_ts + TCPTV_RCVNOTS_QUANTUM)) {
1195 if (tp->rfbuf_cnt + pktlen >= TCP_RCVNOTS_BYTELEVEL) {
1196 tcp_sbrcv_reserve(tp, sbrcv,
1197 tcp_autorcvbuf_max, 0,
1198 tcp_autorcvbuf_max);
1199 }
1200 goto out;
1201 } else {
1202 tp->rfbuf_cnt += pktlen;
1203 return;
1204 }
1205 } else if (to->to_tsecr != 0) {
1206 /*
1207 * If the timestamp shows that one RTT has
1208 * completed, we can stop counting the
1209 * bytes. Here we consider increasing
1210 * the socket buffer if the bandwidth measured in
1211 * last rtt, is more than half of sb_hiwat, this will
1212 * help to scale the buffer according to the bandwidth
1213 * on the link.
1214 */
1215 if (TSTMP_GEQ(to->to_tsecr, tp->rfbuf_ts)) {
1216 if (tp->rfbuf_cnt + pktlen > (sbrcv->sb_hiwat -
1217 (sbrcv->sb_hiwat >> 1))) {
1218 tp->rfbuf_cnt += pktlen;
1219 int32_t rcvbuf_inc, min_incr;
1220 /*
1221 * Increment the receive window by a
1222 * multiple of maximum sized segments.
1223 * This will prevent a connection from
1224 * sending smaller segments on wire if it
1225 * is limited by the receive window.
1226 *
1227 * Set the ideal size based on current
1228 * bandwidth measurements. We set the
1229 * ideal size on receive socket buffer to
1230 * be twice the bandwidth delay product.
1231 */
1232 rcvbuf_inc = (tp->rfbuf_cnt << 1)
1233 - sbrcv->sb_hiwat;
1234
1235 /*
1236 * Make the increment equal to 8 segments
1237 * at least
1238 */
1239 min_incr = tp->t_maxseg << tcp_autorcvbuf_inc_shift;
1240 if (rcvbuf_inc < min_incr) {
1241 rcvbuf_inc = min_incr;
1242 }
1243
1244 rcvbuf_inc =
1245 (rcvbuf_inc / tp->t_maxseg) * tp->t_maxseg;
1246 tcp_sbrcv_reserve(tp, sbrcv,
1247 sbrcv->sb_hiwat + rcvbuf_inc,
1248 (tp->rfbuf_cnt << 1), rcvbuf_max);
1249 }
1250 /* Measure instantaneous receive bandwidth */
1251 if (tp->t_bwmeas != NULL && tp->rfbuf_cnt > 0 &&
1252 TSTMP_GT(tcp_now, tp->rfbuf_ts)) {
1253 u_int32_t rcv_bw;
1254 rcv_bw = tp->rfbuf_cnt /
1255 (int)(tcp_now - tp->rfbuf_ts);
1256 if (tp->t_bwmeas->bw_rcvbw_max == 0) {
1257 tp->t_bwmeas->bw_rcvbw_max = rcv_bw;
1258 } else {
1259 tp->t_bwmeas->bw_rcvbw_max = max(
1260 tp->t_bwmeas->bw_rcvbw_max, rcv_bw);
1261 }
1262 }
1263 goto out;
1264 } else {
1265 tp->rfbuf_cnt += pktlen;
1266 return;
1267 }
1268 }
1269 out:
1270 /* Restart the measurement */
1271 tp->rfbuf_ts = 0;
1272 tp->rfbuf_cnt = 0;
1273 return;
1274 }
1275
1276 /* This function will trim the excess space added to the socket buffer
1277 * to help a slow-reading app. The ideal-size of a socket buffer depends
1278 * on the link bandwidth or it is set by an application and we aim to
1279 * reach that size.
1280 */
1281 void
1282 tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sbrcv)
1283 {
1284 if (tcp_do_autorcvbuf == 1 && sbrcv->sb_idealsize > 0 &&
1285 sbrcv->sb_hiwat > sbrcv->sb_idealsize) {
1286 int32_t trim;
1287 /* compute the difference between ideal and current sizes */
1288 u_int32_t diff = sbrcv->sb_hiwat - sbrcv->sb_idealsize;
1289
1290 /* Compute the maximum advertised window for
1291 * this connection.
1292 */
1293 u_int32_t advwin = tp->rcv_adv - tp->rcv_nxt;
1294
1295 /* How much can we trim the receive socket buffer?
1296 * 1. it can not be trimmed beyond the max rcv win advertised
1297 * 2. if possible, leave 1/16 of bandwidth*delay to
1298 * avoid closing the win completely
1299 */
1300 u_int32_t leave = max(advwin, (sbrcv->sb_idealsize >> 4));
1301
1302 /* Sometimes leave can be zero, in that case leave at least
1303 * a few segments worth of space.
1304 */
1305 if (leave == 0) {
1306 leave = tp->t_maxseg << tcp_autorcvbuf_inc_shift;
1307 }
1308
1309 trim = sbrcv->sb_hiwat - (sbrcv->sb_cc + leave);
1310 trim = imin(trim, (int32_t)diff);
1311
1312 if (trim > 0) {
1313 sbreserve(sbrcv, (sbrcv->sb_hiwat - trim));
1314 }
1315 }
1316 }
1317
1318 /* We may need to trim the send socket buffer size for two reasons:
1319 * 1. if the rtt seen on the connection is climbing up, we do not
1320 * want to fill the buffers any more.
1321 * 2. if the congestion win on the socket backed off, there is no need
1322 * to hold more mbufs for that connection than what the cwnd will allow.
1323 */
1324 void
1325 tcp_sbsnd_trim(struct sockbuf *sbsnd)
1326 {
1327 if (tcp_do_autosendbuf == 1 &&
1328 ((sbsnd->sb_flags & (SB_AUTOSIZE | SB_TRIM)) ==
1329 (SB_AUTOSIZE | SB_TRIM)) &&
1330 (sbsnd->sb_idealsize > 0) &&
1331 (sbsnd->sb_hiwat > sbsnd->sb_idealsize)) {
1332 u_int32_t trim = 0;
1333 if (sbsnd->sb_cc <= sbsnd->sb_idealsize) {
1334 trim = sbsnd->sb_hiwat - sbsnd->sb_idealsize;
1335 } else {
1336 trim = sbsnd->sb_hiwat - sbsnd->sb_cc;
1337 }
1338 sbreserve(sbsnd, (sbsnd->sb_hiwat - trim));
1339 }
1340 if (sbsnd->sb_hiwat <= sbsnd->sb_idealsize) {
1341 sbsnd->sb_flags &= ~(SB_TRIM);
1342 }
1343 }
1344
1345 /*
1346 * If timestamp option was not negotiated on this connection
1347 * and this connection is on the receiving side of a stream
1348 * then we can not measure the delay on the link accurately.
1349 * Instead of enabling automatic receive socket buffer
1350 * resizing, just give more space to the receive socket buffer.
1351 */
1352 static inline void
1353 tcp_sbrcv_tstmp_check(struct tcpcb *tp)
1354 {
1355 struct socket *so = tp->t_inpcb->inp_socket;
1356 u_int32_t newsize = 2 * tcp_recvspace;
1357 struct sockbuf *sbrcv = &so->so_rcv;
1358
1359 if ((tp->t_flags & (TF_REQ_TSTMP | TF_RCVD_TSTMP)) !=
1360 (TF_REQ_TSTMP | TF_RCVD_TSTMP) &&
1361 (sbrcv->sb_flags & SB_AUTOSIZE) != 0) {
1362 tcp_sbrcv_reserve(tp, sbrcv, newsize, 0, newsize);
1363 }
1364 }
1365
1366 /* A receiver will evaluate the flow of packets on a connection
1367 * to see if it can reduce ack traffic. The receiver will start
1368 * stretching acks if all of the following conditions are met:
1369 * 1. tcp_delack_enabled is set to 3
1370 * 2. If the bytes received in the last 100ms is greater than a threshold
1371 * defined by maxseg_unacked
1372 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1373 * 4. If the connection has seen enough packets to let the slow-start
1374 * finish after connection establishment or after some packet loss.
1375 *
1376 * The receiver will stop stretching acks if there is congestion/reordering
1377 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1378 * timer fires while stretching acks, it means that the packet flow has gone
1379 * below the threshold defined by maxseg_unacked and the receiver will stop
1380 * stretching acks. The receiver gets no indication when slow-start is completed
1381 * or when the connection reaches an idle state. That is why we use
1382 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1383 * state.
1384 */
1385 static inline int
1386 tcp_stretch_ack_enable(struct tcpcb *tp, int thflags)
1387 {
1388 if (tp->rcv_by_unackwin >= (maxseg_unacked * tp->t_maxseg) &&
1389 TSTMP_GEQ(tp->rcv_unackwin, tcp_now)) {
1390 tp->t_flags |= TF_STREAMING_ON;
1391 } else {
1392 tp->t_flags &= ~TF_STREAMING_ON;
1393 }
1394
1395 /* If there has been an idle time, reset streaming detection */
1396 if (TSTMP_GT(tcp_now, tp->rcv_unackwin + tcp_maxrcvidle)) {
1397 tp->t_flags &= ~TF_STREAMING_ON;
1398 }
1399
1400 /*
1401 * If there are flags other than TH_ACK set, reset streaming
1402 * detection
1403 */
1404 if (thflags & ~TH_ACK) {
1405 tp->t_flags &= ~TF_STREAMING_ON;
1406 }
1407
1408 if (tp->t_flagsext & TF_DISABLE_STRETCHACK) {
1409 if (tp->rcv_nostrack_pkts >= TCP_STRETCHACK_ENABLE_PKTCNT) {
1410 tp->t_flagsext &= ~TF_DISABLE_STRETCHACK;
1411 tp->rcv_nostrack_pkts = 0;
1412 tp->rcv_nostrack_ts = 0;
1413 } else {
1414 tp->rcv_nostrack_pkts++;
1415 }
1416 }
1417
1418 if (!(tp->t_flagsext & (TF_NOSTRETCHACK | TF_DISABLE_STRETCHACK)) &&
1419 (tp->t_flags & TF_STREAMING_ON) &&
1420 (!(tp->t_flagsext & TF_RCVUNACK_WAITSS) ||
1421 (tp->rcv_waitforss >= tcp_rcvsspktcnt))) {
1422 return 1;
1423 }
1424
1425 return 0;
1426 }
1427
1428 /*
1429 * Reset the state related to stretch-ack algorithm. This will make
1430 * the receiver generate an ack every other packet. The receiver
1431 * will start re-evaluating the rate at which packets come to decide
1432 * if it can benefit by lowering the ack traffic.
1433 */
1434 void
1435 tcp_reset_stretch_ack(struct tcpcb *tp)
1436 {
1437 tp->t_flags &= ~(TF_STRETCHACK | TF_STREAMING_ON);
1438 tp->rcv_by_unackwin = 0;
1439 tp->rcv_by_unackhalfwin = 0;
1440 tp->rcv_unackwin = tcp_now + tcp_rcvunackwin;
1441
1442 /*
1443 * When there is packet loss or packet re-ordering or CWR due to
1444 * ECN, the sender's congestion window is reduced. In these states,
1445 * generate an ack for every other packet for some time to allow
1446 * the sender's congestion window to grow.
1447 */
1448 tp->t_flagsext |= TF_RCVUNACK_WAITSS;
1449 tp->rcv_waitforss = 0;
1450 }
1451
1452 /*
1453 * The last packet was a retransmission, check if this ack
1454 * indicates that the retransmission was spurious.
1455 *
1456 * If the connection supports timestamps, we could use it to
1457 * detect if the last retransmit was not needed. Otherwise,
1458 * we check if the ACK arrived within RTT/2 window, then it
1459 * was a mistake to do the retransmit in the first place.
1460 *
1461 * This function will return 1 if it is a spurious retransmit,
1462 * 0 otherwise.
1463 */
1464 int
1465 tcp_detect_bad_rexmt(struct tcpcb *tp, struct tcphdr *th,
1466 struct tcpopt *to, u_int32_t rxtime)
1467 {
1468 int32_t tdiff, bad_rexmt_win;
1469 bad_rexmt_win = (tp->t_srtt >> (TCP_RTT_SHIFT + 1));
1470
1471 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1472 if (TCP_ECN_ENABLED(tp) && (th->th_flags & TH_ECE)) {
1473 return 0;
1474 }
1475 if (TSTMP_SUPPORTED(tp)) {
1476 if (rxtime > 0 && (to->to_flags & TOF_TS)
1477 && to->to_tsecr != 0
1478 && TSTMP_LT(to->to_tsecr, rxtime)) {
1479 return 1;
1480 }
1481 } else {
1482 if ((tp->t_rxtshift == 1
1483 || (tp->t_flagsext & TF_SENT_TLPROBE))
1484 && rxtime > 0) {
1485 tdiff = (int32_t)(tcp_now - rxtime);
1486 if (tdiff < bad_rexmt_win) {
1487 return 1;
1488 }
1489 }
1490 }
1491 return 0;
1492 }
1493
1494
1495 /*
1496 * Restore congestion window state if a spurious timeout
1497 * was detected.
1498 */
1499 static void
1500 tcp_bad_rexmt_restore_state(struct tcpcb *tp, struct tcphdr *th)
1501 {
1502 if (TSTMP_SUPPORTED(tp)) {
1503 u_int32_t fsize, acked;
1504 fsize = tp->snd_max - th->th_ack;
1505 acked = BYTES_ACKED(th, tp);
1506
1507 /*
1508 * Implement bad retransmit recovery as
1509 * described in RFC 4015.
1510 */
1511 tp->snd_ssthresh = tp->snd_ssthresh_prev;
1512
1513 /* Initialize cwnd to the initial window */
1514 if (CC_ALGO(tp)->cwnd_init != NULL) {
1515 CC_ALGO(tp)->cwnd_init(tp);
1516 }
1517
1518 tp->snd_cwnd = fsize + min(acked, tp->snd_cwnd);
1519 } else {
1520 tp->snd_cwnd = tp->snd_cwnd_prev;
1521 tp->snd_ssthresh = tp->snd_ssthresh_prev;
1522 if (tp->t_flags & TF_WASFRECOVERY) {
1523 ENTER_FASTRECOVERY(tp);
1524 }
1525
1526 /* Do not use the loss flight size in this case */
1527 tp->t_lossflightsize = 0;
1528 }
1529 tp->snd_cwnd = max(tp->snd_cwnd, TCP_CC_CWND_INIT_BYTES);
1530 tp->snd_recover = tp->snd_recover_prev;
1531 tp->snd_nxt = tp->snd_max;
1532
1533 /* Fix send socket buffer to reflect the change in cwnd */
1534 tcp_bad_rexmt_fix_sndbuf(tp);
1535
1536 /*
1537 * This RTT might reflect the extra delay induced
1538 * by the network. Skip using this sample for RTO
1539 * calculation and mark the connection so we can
1540 * recompute RTT when the next eligible sample is
1541 * found.
1542 */
1543 tp->t_flagsext |= TF_RECOMPUTE_RTT;
1544 tp->t_badrexmt_time = tcp_now;
1545 tp->t_rtttime = 0;
1546 }
1547
1548 /*
1549 * If the previous packet was sent in retransmission timer, and it was
1550 * not needed, then restore the congestion window to the state before that
1551 * transmission.
1552 *
1553 * If the last packet was sent in tail loss probe timeout, check if that
1554 * recovered the last packet. If so, that will indicate a real loss and
1555 * the congestion window needs to be lowered.
1556 */
1557 static void
1558 tcp_bad_rexmt_check(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
1559 {
1560 if (tp->t_rxtshift > 0 &&
1561 tcp_detect_bad_rexmt(tp, th, to, tp->t_rxtstart)) {
1562 ++tcpstat.tcps_sndrexmitbad;
1563 tcp_bad_rexmt_restore_state(tp, th);
1564 tcp_ccdbg_trace(tp, th, TCP_CC_BAD_REXMT_RECOVERY);
1565 } else if ((tp->t_flagsext & TF_SENT_TLPROBE)
1566 && tp->t_tlphighrxt > 0
1567 && SEQ_GEQ(th->th_ack, tp->t_tlphighrxt)
1568 && !tcp_detect_bad_rexmt(tp, th, to, tp->t_tlpstart)) {
1569 /*
1570 * check DSACK information also to make sure that
1571 * the TLP was indeed needed
1572 */
1573 if (tcp_rxtseg_dsack_for_tlp(tp)) {
1574 /*
1575 * received a DSACK to indicate that TLP was
1576 * not needed
1577 */
1578 tcp_rxtseg_clean(tp);
1579 goto out;
1580 }
1581
1582 /*
1583 * The tail loss probe recovered the last packet and
1584 * we need to adjust the congestion window to take
1585 * this loss into account.
1586 */
1587 ++tcpstat.tcps_tlp_recoverlastpkt;
1588 if (!IN_FASTRECOVERY(tp)) {
1589 tcp_reduce_congestion_window(tp);
1590 EXIT_FASTRECOVERY(tp);
1591 }
1592 tcp_ccdbg_trace(tp, th, TCP_CC_TLP_RECOVER_LASTPACKET);
1593 } else if (tcp_rxtseg_detect_bad_rexmt(tp, th->th_ack)) {
1594 /*
1595 * All of the retransmitted segments were duplicated, this
1596 * can be an indication of bad fast retransmit.
1597 */
1598 tcpstat.tcps_dsack_badrexmt++;
1599 tcp_bad_rexmt_restore_state(tp, th);
1600 tcp_ccdbg_trace(tp, th, TCP_CC_DSACK_BAD_REXMT);
1601 tcp_rxtseg_clean(tp);
1602 }
1603 out:
1604 tp->t_flagsext &= ~(TF_SENT_TLPROBE);
1605 tp->t_tlphighrxt = 0;
1606 tp->t_tlpstart = 0;
1607
1608 /*
1609 * check if the latest ack was for a segment sent during PMTU
1610 * blackhole detection. If the timestamp on the ack is before
1611 * PMTU blackhole detection, then revert the size of the max
1612 * segment to previous size.
1613 */
1614 if (tp->t_rxtshift > 0 && (tp->t_flags & TF_BLACKHOLE) &&
1615 tp->t_pmtud_start_ts > 0 && TSTMP_SUPPORTED(tp)) {
1616 if ((to->to_flags & TOF_TS) && to->to_tsecr != 0
1617 && TSTMP_LT(to->to_tsecr, tp->t_pmtud_start_ts)) {
1618 tcp_pmtud_revert_segment_size(tp);
1619 }
1620 }
1621 if (tp->t_pmtud_start_ts > 0) {
1622 tp->t_pmtud_start_ts = 0;
1623 }
1624 }
1625
1626 /*
1627 * Check if early retransmit can be attempted according to RFC 5827.
1628 *
1629 * If packet reordering is detected on a connection, fast recovery will
1630 * be delayed until it is clear that the packet was lost and not reordered.
1631 * But reordering detection is done only when SACK is enabled.
1632 *
1633 * On connections that do not support SACK, there is a limit on the number
1634 * of early retransmits that can be done per minute. This limit is needed
1635 * to make sure that too many packets are not retransmitted when there is
1636 * packet reordering.
1637 */
1638 static void
1639 tcp_early_rexmt_check(struct tcpcb *tp, struct tcphdr *th)
1640 {
1641 u_int32_t obytes, snd_off;
1642 int32_t snd_len;
1643 struct socket *so = tp->t_inpcb->inp_socket;
1644
1645 if (early_rexmt && (SACK_ENABLED(tp) ||
1646 tp->t_early_rexmt_count < TCP_EARLY_REXMT_LIMIT) &&
1647 SEQ_GT(tp->snd_max, tp->snd_una) &&
1648 (tp->t_dupacks == 1 ||
1649 (SACK_ENABLED(tp) &&
1650 !TAILQ_EMPTY(&tp->snd_holes)))) {
1651 /*
1652 * If there are only a few outstanding
1653 * segments on the connection, we might need
1654 * to lower the retransmit threshold. This
1655 * will allow us to do Early Retransmit as
1656 * described in RFC 5827.
1657 */
1658 if (SACK_ENABLED(tp) &&
1659 !TAILQ_EMPTY(&tp->snd_holes)) {
1660 obytes = (tp->snd_max - tp->snd_fack) +
1661 tp->sackhint.sack_bytes_rexmit;
1662 } else {
1663 obytes = (tp->snd_max - tp->snd_una);
1664 }
1665
1666 /*
1667 * In order to lower retransmit threshold the
1668 * following two conditions must be met.
1669 * 1. the amount of outstanding data is less
1670 * than 4*SMSS bytes
1671 * 2. there is no unsent data ready for
1672 * transmission or the advertised window
1673 * will limit sending new segments.
1674 */
1675 snd_off = tp->snd_max - tp->snd_una;
1676 snd_len = min(so->so_snd.sb_cc, tp->snd_wnd) - snd_off;
1677 if (obytes < (tp->t_maxseg << 2) &&
1678 snd_len <= 0) {
1679 u_int32_t osegs;
1680
1681 osegs = obytes / tp->t_maxseg;
1682 if ((osegs * tp->t_maxseg) < obytes) {
1683 osegs++;
1684 }
1685
1686 /*
1687 * Since the connection might have already
1688 * received some dupacks, we add them to
1689 * to the outstanding segments count to get
1690 * the correct retransmit threshold.
1691 *
1692 * By checking for early retransmit after
1693 * receiving some duplicate acks when SACK
1694 * is supported, the connection will
1695 * enter fast recovery even if multiple
1696 * segments are lost in the same window.
1697 */
1698 osegs += tp->t_dupacks;
1699 if (osegs < 4) {
1700 tp->t_rexmtthresh =
1701 ((osegs - 1) > 1) ? (osegs - 1) : 1;
1702 tp->t_rexmtthresh =
1703 min(tp->t_rexmtthresh, tcprexmtthresh);
1704 tp->t_rexmtthresh =
1705 max(tp->t_rexmtthresh, tp->t_dupacks);
1706
1707 if (tp->t_early_rexmt_count == 0) {
1708 tp->t_early_rexmt_win = tcp_now;
1709 }
1710
1711 if (tp->t_flagsext & TF_SENT_TLPROBE) {
1712 tcpstat.tcps_tlp_recovery++;
1713 tcp_ccdbg_trace(tp, th,
1714 TCP_CC_TLP_RECOVERY);
1715 } else {
1716 tcpstat.tcps_early_rexmt++;
1717 tp->t_early_rexmt_count++;
1718 tcp_ccdbg_trace(tp, th,
1719 TCP_CC_EARLY_RETRANSMIT);
1720 }
1721 }
1722 }
1723 }
1724
1725 /*
1726 * If we ever sent a TLP probe, the acknowledgement will trigger
1727 * early retransmit because the value of snd_fack will be close
1728 * to snd_max. This will take care of adjustments to the
1729 * congestion window. So we can reset TF_SENT_PROBE flag.
1730 */
1731 tp->t_flagsext &= ~(TF_SENT_TLPROBE);
1732 tp->t_tlphighrxt = 0;
1733 tp->t_tlpstart = 0;
1734 }
1735
1736 static boolean_t
1737 tcp_tfo_syn(struct tcpcb *tp, struct tcpopt *to)
1738 {
1739 u_char out[CCAES_BLOCK_SIZE];
1740 unsigned char len;
1741
1742 if (!(to->to_flags & (TOF_TFO | TOF_TFOREQ)) ||
1743 !(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
1744 return FALSE;
1745 }
1746
1747 if ((to->to_flags & TOF_TFOREQ)) {
1748 tp->t_tfo_flags |= TFO_F_OFFER_COOKIE;
1749
1750 tp->t_tfo_stats |= TFO_S_COOKIEREQ_RECV;
1751 tcpstat.tcps_tfo_cookie_req_rcv++;
1752 return FALSE;
1753 }
1754
1755 /* Ok, then it must be an offered cookie. We need to check that ... */
1756 tcp_tfo_gen_cookie(tp->t_inpcb, out, sizeof(out));
1757
1758 len = *to->to_tfo - TCPOLEN_FASTOPEN_REQ;
1759 to->to_tfo++;
1760 if (memcmp(out, to->to_tfo, len)) {
1761 /* Cookies are different! Let's return and offer a new cookie */
1762 tp->t_tfo_flags |= TFO_F_OFFER_COOKIE;
1763
1764 tp->t_tfo_stats |= TFO_S_COOKIE_INVALID;
1765 tcpstat.tcps_tfo_cookie_invalid++;
1766 return FALSE;
1767 }
1768
1769 if (OSIncrementAtomic(&tcp_tfo_halfcnt) >= tcp_tfo_backlog) {
1770 /* Need to decrement again as we just increased it... */
1771 OSDecrementAtomic(&tcp_tfo_halfcnt);
1772 return FALSE;
1773 }
1774
1775 tp->t_tfo_flags |= TFO_F_COOKIE_VALID;
1776
1777 tp->t_tfo_stats |= TFO_S_SYNDATA_RCV;
1778 tcpstat.tcps_tfo_syn_data_rcv++;
1779
1780 return TRUE;
1781 }
1782
1783 static void
1784 tcp_tfo_synack(struct tcpcb *tp, struct tcpopt *to)
1785 {
1786 if (to->to_flags & TOF_TFO) {
1787 unsigned char len = *to->to_tfo - TCPOLEN_FASTOPEN_REQ;
1788
1789 /*
1790 * If this happens, things have gone terribly wrong. len should
1791 * have been checked in tcp_dooptions.
1792 */
1793 VERIFY(len <= TFO_COOKIE_LEN_MAX);
1794
1795 to->to_tfo++;
1796
1797 tcp_cache_set_cookie(tp, to->to_tfo, len);
1798 tcp_heuristic_tfo_success(tp);
1799
1800 tp->t_tfo_stats |= TFO_S_COOKIE_RCV;
1801 tcpstat.tcps_tfo_cookie_rcv++;
1802 if (tp->t_tfo_flags & TFO_F_COOKIE_SENT) {
1803 tcpstat.tcps_tfo_cookie_wrong++;
1804 tp->t_tfo_stats |= TFO_S_COOKIE_WRONG;
1805 }
1806 } else {
1807 /*
1808 * Thus, no cookie in the response, but we either asked for one
1809 * or sent SYN+DATA. Now, we need to check whether we had to
1810 * rexmit the SYN. If that's the case, it's better to start
1811 * backing of TFO-cookie requests.
1812 */
1813 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
1814 tp->t_tfo_flags & TFO_F_SYN_LOSS) {
1815 tp->t_tfo_stats |= TFO_S_SYN_LOSS;
1816 tcpstat.tcps_tfo_syn_loss++;
1817
1818 tcp_heuristic_tfo_loss(tp);
1819 } else {
1820 if (tp->t_tfo_flags & TFO_F_COOKIE_REQ) {
1821 tp->t_tfo_stats |= TFO_S_NO_COOKIE_RCV;
1822 tcpstat.tcps_tfo_no_cookie_rcv++;
1823 }
1824
1825 tcp_heuristic_tfo_success(tp);
1826 }
1827 }
1828 }
1829
1830 static void
1831 tcp_tfo_rcv_probe(struct tcpcb *tp, int tlen)
1832 {
1833 if (tlen != 0) {
1834 return;
1835 }
1836
1837 tp->t_tfo_probe_state = TFO_PROBE_PROBING;
1838
1839 /*
1840 * We send the probe out rather quickly (after one RTO). It does not
1841 * really hurt that much, it's only one additional segment on the wire.
1842 */
1843 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, (TCP_REXMTVAL(tp)));
1844 }
1845
1846 static void
1847 tcp_tfo_rcv_data(struct tcpcb *tp)
1848 {
1849 /* Transition from PROBING to NONE as data has been received */
1850 if (tp->t_tfo_probe_state >= TFO_PROBE_PROBING) {
1851 tp->t_tfo_probe_state = TFO_PROBE_NONE;
1852 }
1853 }
1854
1855 static void
1856 tcp_tfo_rcv_ack(struct tcpcb *tp, struct tcphdr *th)
1857 {
1858 if (tp->t_tfo_probe_state == TFO_PROBE_PROBING &&
1859 tp->t_tfo_probes > 0) {
1860 if (th->th_seq == tp->rcv_nxt) {
1861 /* No hole, so stop probing */
1862 tp->t_tfo_probe_state = TFO_PROBE_NONE;
1863 } else if (SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1864 /* There is a hole! Wait a bit for data... */
1865 tp->t_tfo_probe_state = TFO_PROBE_WAIT_DATA;
1866 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
1867 TCP_REXMTVAL(tp));
1868 }
1869 }
1870 }
1871
1872 /*
1873 * Update snd_wnd information.
1874 */
1875 static inline bool
1876 tcp_update_window(struct tcpcb *tp, int thflags, struct tcphdr * th,
1877 u_int32_t tiwin, int tlen)
1878 {
1879 /* Don't look at the window if there is no ACK flag */
1880 if ((thflags & TH_ACK) &&
1881 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
1882 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
1883 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
1884 /* keep track of pure window updates */
1885 if (tlen == 0 &&
1886 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) {
1887 tcpstat.tcps_rcvwinupd++;
1888 }
1889 tp->snd_wnd = tiwin;
1890 tp->snd_wl1 = th->th_seq;
1891 tp->snd_wl2 = th->th_ack;
1892 if (tp->snd_wnd > tp->max_sndwnd) {
1893 tp->max_sndwnd = tp->snd_wnd;
1894 }
1895
1896 if (tp->t_inpcb->inp_socket->so_flags & SOF_MP_SUBFLOW) {
1897 mptcp_update_window_wakeup(tp);
1898 }
1899 return true;
1900 }
1901 return false;
1902 }
1903
1904 static void
1905 tcp_handle_wakeup(struct socket *so, int read_wakeup, int write_wakeup)
1906 {
1907 if (read_wakeup != 0) {
1908 sorwakeup(so);
1909 }
1910 if (write_wakeup != 0) {
1911 sowwakeup(so);
1912 }
1913 }
1914
1915 void
1916 tcp_input(struct mbuf *m, int off0)
1917 {
1918 struct tcphdr *th;
1919 struct ip *ip = NULL;
1920 struct inpcb *inp;
1921 u_char *optp = NULL;
1922 int optlen = 0;
1923 int tlen, off;
1924 int drop_hdrlen;
1925 struct tcpcb *tp = 0;
1926 int thflags;
1927 struct socket *so = 0;
1928 int todrop, acked, ourfinisacked, needoutput = 0;
1929 int read_wakeup = 0;
1930 int write_wakeup = 0;
1931 struct in_addr laddr;
1932 #if INET6
1933 struct in6_addr laddr6;
1934 #endif
1935 int dropsocket = 0;
1936 int iss = 0, nosock = 0;
1937 u_int32_t tiwin, sack_bytes_acked = 0;
1938 struct tcpopt to; /* options in this segment */
1939 #if TCPDEBUG
1940 short ostate = 0;
1941 #endif
1942 #if IPFIREWALL
1943 struct sockaddr_in *next_hop = NULL;
1944 struct m_tag *fwd_tag;
1945 #endif /* IPFIREWALL */
1946 u_char ip_ecn = IPTOS_ECN_NOTECT;
1947 unsigned int ifscope;
1948 uint8_t isconnected, isdisconnected;
1949 struct ifnet *ifp = m->m_pkthdr.rcvif;
1950 int pktf_sw_lro_pkt = (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) ? 1 : 0;
1951 int nlropkts = (pktf_sw_lro_pkt == 1) ? m->m_pkthdr.lro_npkts : 1;
1952 int turnoff_lro = 0, win;
1953 #if MPTCP
1954 struct mptcb *mp_tp = NULL;
1955 #endif /* MPTCP */
1956 boolean_t cell = IFNET_IS_CELLULAR(ifp);
1957 boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
1958 boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
1959 boolean_t recvd_dsack = FALSE;
1960 struct tcp_respond_args tra;
1961 int prev_t_state;
1962 boolean_t check_cfil = cfil_filter_present();
1963 bool findpcb_iterated = false;
1964 /*
1965 * The mbuf may be freed after it has been added to the receive socket
1966 * buffer or the reassembly queue, so we reinitialize th to point to a
1967 * safe copy of the TCP header
1968 */
1969 struct tcphdr saved_tcphdr = {};
1970 /*
1971 * Save copy of the IPv4/IPv6 header.
1972 * Note: use array of uint32_t to silence compiler warning when casting
1973 * to a struct ip6_hdr pointer.
1974 */
1975 #define MAX_IPWORDS ((sizeof(struct ip) + MAX_IPOPTLEN) / sizeof(uint32_t))
1976 uint32_t saved_hdr[MAX_IPWORDS];
1977
1978 #define TCP_INC_VAR(stat, npkts) do { \
1979 stat += npkts; \
1980 } while (0)
1981
1982 TCP_INC_VAR(tcpstat.tcps_rcvtotal, nlropkts);
1983 #if IPFIREWALL
1984 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1985 if (!SLIST_EMPTY(&m->m_pkthdr.tags)) {
1986 fwd_tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
1987 KERNEL_TAG_TYPE_IPFORWARD, NULL);
1988 } else {
1989 fwd_tag = NULL;
1990 }
1991 if (fwd_tag != NULL) {
1992 struct ip_fwd_tag *ipfwd_tag =
1993 (struct ip_fwd_tag *)(fwd_tag + 1);
1994
1995 next_hop = ipfwd_tag->next_hop;
1996 m_tag_delete(m, fwd_tag);
1997 }
1998 #endif /* IPFIREWALL */
1999
2000 #if INET6
2001 struct ip6_hdr *ip6 = NULL;
2002 int isipv6;
2003 #endif /* INET6 */
2004 int rstreason; /* For badport_bandlim accounting purposes */
2005 struct proc *proc0 = current_proc();
2006
2007 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
2008
2009 #if INET6
2010 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
2011 #endif
2012 bzero((char *)&to, sizeof(to));
2013
2014 #if INET6
2015 if (isipv6) {
2016 /*
2017 * Expect 32-bit aligned data pointer on
2018 * strict-align platforms
2019 */
2020 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
2021
2022 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
2023 ip6 = mtod(m, struct ip6_hdr *);
2024 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
2025 th = (struct tcphdr *)(void *)((caddr_t)ip6 + off0);
2026
2027 if (tcp_input_checksum(AF_INET6, m, th, off0, tlen)) {
2028 TCP_LOG_DROP_PKT(ip6, th, ifp, "IPv6 bad tcp checksum");
2029 goto dropnosock;
2030 }
2031
2032 KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport),
2033 (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])),
2034 th->th_seq, th->th_ack, th->th_win);
2035 /*
2036 * Be proactive about unspecified IPv6 address in source.
2037 * As we use all-zero to indicate unbounded/unconnected pcb,
2038 * unspecified IPv6 address can be used to confuse us.
2039 *
2040 * Note that packets with unspecified IPv6 destination is
2041 * already dropped in ip6_input.
2042 */
2043 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
2044 /* XXX stat */
2045 IF_TCP_STATINC(ifp, unspecv6);
2046 TCP_LOG_DROP_PKT(ip6, th, ifp, "src IPv6 address unspecified");
2047 goto dropnosock;
2048 }
2049 DTRACE_TCP5(receive, struct mbuf *, m, struct inpcb *, NULL,
2050 struct ip6_hdr *, ip6, struct tcpcb *, NULL,
2051 struct tcphdr *, th);
2052
2053 ip_ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
2054 } else
2055 #endif /* INET6 */
2056 {
2057 /*
2058 * Get IP and TCP header together in first mbuf.
2059 * Note: IP leaves IP header in first mbuf.
2060 */
2061 if (off0 > sizeof(struct ip)) {
2062 ip_stripoptions(m);
2063 off0 = sizeof(struct ip);
2064 }
2065 if (m->m_len < sizeof(struct tcpiphdr)) {
2066 if ((m = m_pullup(m, sizeof(struct tcpiphdr))) == 0) {
2067 tcpstat.tcps_rcvshort++;
2068 return;
2069 }
2070 }
2071
2072 /* Expect 32-bit aligned data pointer on strict-align platforms */
2073 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
2074
2075 ip = mtod(m, struct ip *);
2076 th = (struct tcphdr *)(void *)((caddr_t)ip + off0);
2077 tlen = ip->ip_len;
2078
2079 if (tcp_input_checksum(AF_INET, m, th, off0, tlen)) {
2080 TCP_LOG_DROP_PKT(ip, th, ifp, "IPv4 bad tcp checksum");
2081 goto dropnosock;
2082 }
2083
2084 #if INET6
2085 /* Re-initialization for later version check */
2086 ip->ip_v = IPVERSION;
2087 #endif
2088 ip_ecn = (ip->ip_tos & IPTOS_ECN_MASK);
2089
2090 DTRACE_TCP5(receive, struct mbuf *, m, struct inpcb *, NULL,
2091 struct ip *, ip, struct tcpcb *, NULL, struct tcphdr *, th);
2092
2093 KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport),
2094 (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)),
2095 th->th_seq, th->th_ack, th->th_win);
2096 }
2097
2098 #define TCP_LOG_HDR (isipv6 ? (void *)ip6 : (void *)ip)
2099
2100 /*
2101 * Check that TCP offset makes sense,
2102 * pull out TCP options and adjust length.
2103 */
2104 off = th->th_off << 2;
2105 if (off < sizeof(struct tcphdr) || off > tlen) {
2106 tcpstat.tcps_rcvbadoff++;
2107 IF_TCP_STATINC(ifp, badformat);
2108 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "bad tcp offset");
2109 goto dropnosock;
2110 }
2111 tlen -= off; /* tlen is used instead of ti->ti_len */
2112 if (off > sizeof(struct tcphdr)) {
2113 #if INET6
2114 if (isipv6) {
2115 IP6_EXTHDR_CHECK(m, off0, off, return );
2116 ip6 = mtod(m, struct ip6_hdr *);
2117 th = (struct tcphdr *)(void *)((caddr_t)ip6 + off0);
2118 } else
2119 #endif /* INET6 */
2120 {
2121 if (m->m_len < sizeof(struct ip) + off) {
2122 if ((m = m_pullup(m, sizeof(struct ip) + off)) == 0) {
2123 tcpstat.tcps_rcvshort++;
2124 return;
2125 }
2126 ip = mtod(m, struct ip *);
2127 th = (struct tcphdr *)(void *)((caddr_t)ip + off0);
2128 }
2129 }
2130 optlen = off - sizeof(struct tcphdr);
2131 optp = (u_char *)(th + 1);
2132 /*
2133 * Do quick retrieval of timestamp options ("options
2134 * prediction?"). If timestamp is the only option and it's
2135 * formatted as recommended in RFC 1323 appendix A, we
2136 * quickly get the values now and not bother calling
2137 * tcp_dooptions(), etc.
2138 */
2139 if ((optlen == TCPOLEN_TSTAMP_APPA ||
2140 (optlen > TCPOLEN_TSTAMP_APPA &&
2141 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
2142 *(u_int32_t *)(void *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
2143 (th->th_flags & TH_SYN) == 0) {
2144 to.to_flags |= TOF_TS;
2145 to.to_tsval = ntohl(*(u_int32_t *)(void *)(optp + 4));
2146 to.to_tsecr = ntohl(*(u_int32_t *)(void *)(optp + 8));
2147 optp = NULL; /* we've parsed the options */
2148 }
2149 }
2150 thflags = th->th_flags;
2151
2152 #if TCP_DROP_SYNFIN
2153 /*
2154 * If the drop_synfin option is enabled, drop all packets with
2155 * both the SYN and FIN bits set. This prevents e.g. nmap from
2156 * identifying the TCP/IP stack.
2157 *
2158 * This is a violation of the TCP specification.
2159 */
2160 if (drop_synfin && (thflags & (TH_SYN | TH_FIN)) == (TH_SYN | TH_FIN)) {
2161 IF_TCP_STATINC(ifp, synfin);
2162 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "drop SYN FIN");
2163 goto dropnosock;
2164 }
2165 #endif
2166
2167 /*
2168 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
2169 * until after ip6_savecontrol() is called and before other functions
2170 * which don't want those proto headers.
2171 * Because ip6_savecontrol() is going to parse the mbuf to
2172 * search for data to be passed up to user-land, it wants mbuf
2173 * parameters to be unchanged.
2174 */
2175 drop_hdrlen = off0 + off;
2176
2177 /* Since this is an entry point for input processing of tcp packets, we
2178 * can update the tcp clock here.
2179 */
2180 calculate_tcp_clock();
2181
2182 /*
2183 * Record the interface where this segment arrived on; this does not
2184 * affect normal data output (for non-detached TCP) as it provides a
2185 * hint about which route and interface to use for sending in the
2186 * absence of a PCB, when scoped routing (and thus source interface
2187 * selection) are enabled.
2188 */
2189 if ((m->m_pkthdr.pkt_flags & PKTF_LOOP) || m->m_pkthdr.rcvif == NULL) {
2190 ifscope = IFSCOPE_NONE;
2191 } else {
2192 ifscope = m->m_pkthdr.rcvif->if_index;
2193 }
2194
2195 /*
2196 * Convert TCP protocol specific fields to host format.
2197 */
2198
2199 #if BYTE_ORDER != BIG_ENDIAN
2200 NTOHL(th->th_seq);
2201 NTOHL(th->th_ack);
2202 NTOHS(th->th_win);
2203 NTOHS(th->th_urp);
2204 #endif
2205
2206 /*
2207 * Locate pcb for segment.
2208 */
2209 findpcb:
2210
2211 isconnected = FALSE;
2212 isdisconnected = FALSE;
2213
2214 #if IPFIREWALL_FORWARD
2215 if (next_hop != NULL
2216 #if INET6
2217 && isipv6 == 0 /* IPv6 support is not yet */
2218 #endif /* INET6 */
2219 ) {
2220 /*
2221 * Diverted. Pretend to be the destination.
2222 * already got one like this?
2223 */
2224 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
2225 ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
2226 if (!inp) {
2227 /*
2228 * No, then it's new. Try find the ambushing socket
2229 */
2230 if (!next_hop->sin_port) {
2231 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
2232 th->th_sport, next_hop->sin_addr,
2233 th->th_dport, 1, m->m_pkthdr.rcvif);
2234 } else {
2235 inp = in_pcblookup_hash(&tcbinfo,
2236 ip->ip_src, th->th_sport,
2237 next_hop->sin_addr,
2238 ntohs(next_hop->sin_port), 1,
2239 m->m_pkthdr.rcvif);
2240 }
2241 }
2242 } else
2243 #endif /* IPFIREWALL_FORWARD */
2244 {
2245 #if INET6
2246 if (isipv6) {
2247 inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
2248 &ip6->ip6_dst, th->th_dport, 1,
2249 m->m_pkthdr.rcvif);
2250 } else
2251 #endif /* INET6 */
2252 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
2253 ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
2254 }
2255
2256 /*
2257 * Use the interface scope information from the PCB for outbound
2258 * segments. If the PCB isn't present and if scoped routing is
2259 * enabled, tcp_respond will use the scope of the interface where
2260 * the segment arrived on.
2261 */
2262 if (inp != NULL && (inp->inp_flags & INP_BOUND_IF)) {
2263 ifscope = inp->inp_boundifp->if_index;
2264 }
2265
2266 /*
2267 * If the state is CLOSED (i.e., TCB does not exist) then
2268 * all data in the incoming segment is discarded.
2269 * If the TCB exists but is in CLOSED state, it is embryonic,
2270 * but should either do a listen or a connect soon.
2271 */
2272 if (inp == NULL) {
2273 if (log_in_vain) {
2274 #if INET6
2275 char dbuf[MAX_IPv6_STR_LEN], sbuf[MAX_IPv6_STR_LEN];
2276 #else /* INET6 */
2277 char dbuf[MAX_IPv4_STR_LEN], sbuf[MAX_IPv4_STR_LEN];
2278 #endif /* INET6 */
2279
2280 #if INET6
2281 if (isipv6) {
2282 inet_ntop(AF_INET6, &ip6->ip6_dst, dbuf, sizeof(dbuf));
2283 inet_ntop(AF_INET6, &ip6->ip6_src, sbuf, sizeof(sbuf));
2284 } else
2285 #endif
2286 {
2287 inet_ntop(AF_INET, &ip->ip_dst, dbuf, sizeof(dbuf));
2288 inet_ntop(AF_INET, &ip->ip_src, sbuf, sizeof(sbuf));
2289 }
2290 switch (log_in_vain) {
2291 case 1:
2292 if (thflags & TH_SYN) {
2293 log(LOG_INFO,
2294 "Connection attempt to TCP %s:%d from %s:%d\n",
2295 dbuf, ntohs(th->th_dport),
2296 sbuf,
2297 ntohs(th->th_sport));
2298 }
2299 break;
2300 case 2:
2301 log(LOG_INFO,
2302 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
2303 dbuf, ntohs(th->th_dport), sbuf,
2304 ntohs(th->th_sport), thflags);
2305 break;
2306 case 3:
2307 case 4:
2308 if ((thflags & TH_SYN) && !(thflags & TH_ACK) &&
2309 !(m->m_flags & (M_BCAST | M_MCAST)) &&
2310 #if INET6
2311 ((isipv6 && !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) ||
2312 (!isipv6 && ip->ip_dst.s_addr != ip->ip_src.s_addr))
2313 #else
2314 ip->ip_dst.s_addr != ip->ip_src.s_addr
2315 #endif
2316 ) {
2317 log_in_vain_log((LOG_INFO,
2318 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
2319 dbuf, ntohs(th->th_dport),
2320 sbuf,
2321 ntohs(th->th_sport)));
2322 }
2323 break;
2324 default:
2325 break;
2326 }
2327 }
2328 if (blackhole) {
2329 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type != IFT_LOOP) {
2330 switch (blackhole) {
2331 case 1:
2332 if (thflags & TH_SYN) {
2333 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "blackhole 1 syn for closed port");
2334 goto dropnosock;
2335 }
2336 break;
2337 case 2:
2338 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "blackhole 2 closed port");
2339 goto dropnosock;
2340 default:
2341 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "blackhole closed port");
2342 goto dropnosock;
2343 }
2344 }
2345 }
2346 rstreason = BANDLIM_RST_CLOSEDPORT;
2347 IF_TCP_STATINC(ifp, noconnnolist);
2348 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "closed port");
2349 goto dropwithresetnosock;
2350 }
2351 so = inp->inp_socket;
2352 if (so == NULL) {
2353 /* This case shouldn't happen as the socket shouldn't be null
2354 * if inp_state isn't set to INPCB_STATE_DEAD
2355 * But just in case, we pretend we didn't find the socket if we hit this case
2356 * as this isn't cause for a panic (the socket might be leaked however)...
2357 */
2358 inp = NULL;
2359 #if TEMPDEBUG
2360 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp);
2361 #endif
2362 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "inp_socket NULL");
2363 goto dropnosock;
2364 }
2365
2366 socket_lock(so, 1);
2367 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
2368 socket_unlock(so, 1);
2369 inp = NULL; // pretend we didn't find it
2370 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "inp state WNT_STOPUSING");
2371 goto dropnosock;
2372 }
2373
2374 if (!isipv6 && inp->inp_faddr.s_addr != INADDR_ANY) {
2375 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr ||
2376 inp->inp_laddr.s_addr != ip->ip_dst.s_addr ||
2377 inp->inp_fport != th->th_sport ||
2378 inp->inp_lport != th->th_dport) {
2379 os_log_error(OS_LOG_DEFAULT, "%s 5-tuple does not match: %u:%u %u:%u\n",
2380 __func__,
2381 ntohs(inp->inp_fport), ntohs(th->th_sport),
2382 ntohs(inp->inp_lport), ntohs(th->th_dport));
2383 if (findpcb_iterated) {
2384 goto drop;
2385 }
2386 findpcb_iterated = true;
2387 socket_unlock(so, 1);
2388 inp = NULL;
2389 goto findpcb;
2390 }
2391 } else if (isipv6 && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
2392 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src) ||
2393 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst) ||
2394 inp->inp_fport != th->th_sport ||
2395 inp->inp_lport != th->th_dport) {
2396 os_log_error(OS_LOG_DEFAULT, "%s 5-tuple does not match: %u:%u %u:%u\n",
2397 __func__,
2398 ntohs(inp->inp_fport), ntohs(th->th_sport),
2399 ntohs(inp->inp_lport), ntohs(th->th_dport));
2400 if (findpcb_iterated) {
2401 goto drop;
2402 }
2403 findpcb_iterated = true;
2404 socket_unlock(so, 1);
2405 inp = NULL;
2406 goto findpcb;
2407 }
2408 }
2409
2410 tp = intotcpcb(inp);
2411 if (tp == NULL) {
2412 rstreason = BANDLIM_RST_CLOSEDPORT;
2413 IF_TCP_STATINC(ifp, noconnlist);
2414 TCP_LOG_DROP_PKT(TCP_LOG_HDR, th, ifp, "tp is NULL");
2415 goto dropwithreset;
2416 }
2417
2418 TCP_LOG_TH_FLAGS(TCP_LOG_HDR, th, tp, false, ifp);
2419
2420 if (tp->t_state == TCPS_CLOSED) {
2421 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "tp state TCPS_CLOSED");
2422 goto drop;
2423 }
2424
2425 #if NECP
2426 if (so->so_state & SS_ISCONNECTED) {
2427 // Connected TCP sockets have a fully-bound local and remote,
2428 // so the policy check doesn't need to override addresses
2429 if (!necp_socket_is_allowed_to_send_recv(inp, ifp, NULL, NULL, NULL)) {
2430 TCP_LOG_DROP_NECP(TCP_LOG_HDR, th, intotcpcb(inp), false);
2431 IF_TCP_STATINC(ifp, badformat);
2432 goto drop;
2433 }
2434 } else {
2435 /*
2436 * If the proc_uuid_policy table has been updated since the last use
2437 * of the listening socket (i.e., the proc_uuid_policy_table_gencount
2438 * has been updated), the flags in the socket may be out of date.
2439 * If INP2_WANT_APP_POLICY is stale, inbound packets may
2440 * be dropped by NECP if the socket should now match a per-app
2441 * exception policy.
2442 * In order to avoid this refresh the proc_uuid_policy state to
2443 * potentially recalculate the socket's flags before checking
2444 * with NECP.
2445 */
2446 (void) inp_update_policy(inp);
2447 #if INET6
2448 if (isipv6) {
2449 if (!necp_socket_is_allowed_to_send_recv_v6(inp,
2450 th->th_dport, th->th_sport, &ip6->ip6_dst,
2451 &ip6->ip6_src, ifp, NULL, NULL, NULL)) {
2452 TCP_LOG_DROP_NECP(TCP_LOG_HDR, th, intotcpcb(inp), false);
2453 IF_TCP_STATINC(ifp, badformat);
2454 goto drop;
2455 }
2456 } else
2457 #endif
2458 {
2459 if (!necp_socket_is_allowed_to_send_recv_v4(inp,
2460 th->th_dport, th->th_sport, &ip->ip_dst, &ip->ip_src,
2461 ifp, NULL, NULL, NULL)) {
2462 TCP_LOG_DROP_NECP(TCP_LOG_HDR, th, intotcpcb(inp), false);
2463 IF_TCP_STATINC(ifp, badformat);
2464 goto drop;
2465 }
2466 }
2467 }
2468 #endif /* NECP */
2469
2470 prev_t_state = tp->t_state;
2471
2472 /* If none of the FIN|SYN|RST|ACK flag is set, drop */
2473 if (tcp_do_rfc5961 && (thflags & TH_ACCEPT) == 0) {
2474 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "rfc5961 TH_ACCEPT == 0");
2475 goto drop;
2476 }
2477
2478 /* Unscale the window into a 32-bit value. */
2479 if ((thflags & TH_SYN) == 0) {
2480 tiwin = th->th_win << tp->snd_scale;
2481 } else {
2482 tiwin = th->th_win;
2483 }
2484
2485
2486 #if CONFIG_MACF_NET
2487 if (mac_inpcb_check_deliver(inp, m, AF_INET, SOCK_STREAM)) {
2488 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "mac_inpcb_check_deliver failed");
2489 goto drop;
2490 }
2491 #endif
2492
2493 /* Avoid processing packets while closing a listen socket */
2494 if (tp->t_state == TCPS_LISTEN &&
2495 (so->so_options & SO_ACCEPTCONN) == 0) {
2496 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "closing a listening socket");
2497 goto drop;
2498 }
2499
2500 if (so->so_options & (SO_DEBUG | SO_ACCEPTCONN)) {
2501 #if TCPDEBUG
2502 if (so->so_options & SO_DEBUG) {
2503 ostate = tp->t_state;
2504 #if INET6
2505 if (isipv6) {
2506 bcopy((char *)ip6, (char *)tcp_saveipgen,
2507 sizeof(*ip6));
2508 } else
2509 #endif /* INET6 */
2510 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
2511 tcp_savetcp = *th;
2512 }
2513 #endif
2514 if (so->so_options & SO_ACCEPTCONN) {
2515 struct tcpcb *tp0 = tp;
2516 struct socket *so2;
2517 struct socket *oso;
2518 struct sockaddr_storage from;
2519 struct sockaddr_storage to2;
2520 #if INET6
2521 struct inpcb *oinp = sotoinpcb(so);
2522 #endif /* INET6 */
2523 struct ifnet *head_ifscope;
2524 unsigned int head_nocell, head_recvanyif,
2525 head_noexpensive, head_awdl_unrestricted,
2526 head_intcoproc_allowed, head_external_port,
2527 head_noconstrained;
2528
2529 /* Get listener's bound-to-interface, if any */
2530 head_ifscope = (inp->inp_flags & INP_BOUND_IF) ?
2531 inp->inp_boundifp : NULL;
2532 /* Get listener's no-cellular information, if any */
2533 head_nocell = INP_NO_CELLULAR(inp);
2534 /* Get listener's recv-any-interface, if any */
2535 head_recvanyif = (inp->inp_flags & INP_RECV_ANYIF);
2536 /* Get listener's no-expensive information, if any */
2537 head_noexpensive = INP_NO_EXPENSIVE(inp);
2538 head_noconstrained = INP_NO_CONSTRAINED(inp);
2539 head_awdl_unrestricted = INP_AWDL_UNRESTRICTED(inp);
2540 head_intcoproc_allowed = INP_INTCOPROC_ALLOWED(inp);
2541 head_external_port = (inp->inp_flags2 & INP2_EXTERNAL_PORT);
2542
2543 /*
2544 * If the state is LISTEN then ignore segment if it contains an RST.
2545 * If the segment contains an ACK then it is bad and send a RST.
2546 * If it does not contain a SYN then it is not interesting; drop it.
2547 * If it is from this socket, drop it, it must be forged.
2548 */
2549 if ((thflags & (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) {
2550 IF_TCP_STATINC(ifp, listbadsyn);
2551
2552 if (thflags & TH_RST) {
2553 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN with RST");
2554 goto drop;
2555 }
2556 if (thflags & TH_ACK) {
2557 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN with ACK");
2558 tp = NULL;
2559 tcpstat.tcps_badsyn++;
2560 rstreason = BANDLIM_RST_OPENPORT;
2561 goto dropwithreset;
2562 }
2563
2564 /* We come here if there is no SYN set */
2565 tcpstat.tcps_badsyn++;
2566 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "bad SYN");
2567 goto drop;
2568 }
2569 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_START, 0, 0, 0, 0, 0);
2570 if (th->th_dport == th->th_sport) {
2571 #if INET6
2572 if (isipv6) {
2573 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
2574 &ip6->ip6_src)) {
2575 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "bad tuple same port");
2576 goto drop;
2577 }
2578 } else
2579 #endif /* INET6 */
2580 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) {
2581 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "bad tuple same IPv4 address");
2582 goto drop;
2583 }
2584 }
2585 /*
2586 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2587 * in_broadcast() should never return true on a received
2588 * packet with M_BCAST not set.
2589 *
2590 * Packets with a multicast source address should also
2591 * be discarded.
2592 */
2593 if (m->m_flags & (M_BCAST | M_MCAST)) {
2594 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "mbuf M_BCAST | M_MCAST");
2595 goto drop;
2596 }
2597 #if INET6
2598 if (isipv6) {
2599 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2600 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
2601 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "IN6_IS_ADDR_MULTICAST");
2602 goto drop;
2603 }
2604 } else
2605 #endif
2606 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2607 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2608 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2609 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
2610 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "multicast or broadcast address");
2611 goto drop;
2612 }
2613
2614
2615 #if INET6
2616 /*
2617 * If deprecated address is forbidden,
2618 * we do not accept SYN to deprecated interface
2619 * address to prevent any new inbound connection from
2620 * getting established.
2621 * When we do not accept SYN, we send a TCP RST,
2622 * with deprecated source address (instead of dropping
2623 * it). We compromise it as it is much better for peer
2624 * to send a RST, and RST will be the final packet
2625 * for the exchange.
2626 *
2627 * If we do not forbid deprecated addresses, we accept
2628 * the SYN packet. RFC 4862 forbids dropping SYN in
2629 * this case.
2630 */
2631 if (isipv6 && !ip6_use_deprecated) {
2632 uint32_t ia6_flags;
2633
2634 if (ip6_getdstifaddr_info(m, NULL,
2635 &ia6_flags) == 0) {
2636 if (ia6_flags & IN6_IFF_DEPRECATED) {
2637 tp = NULL;
2638 rstreason = BANDLIM_RST_OPENPORT;
2639 IF_TCP_STATINC(ifp, deprecate6);
2640 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "deprecated IPv6 address");
2641 goto dropwithreset;
2642 }
2643 }
2644 }
2645 #endif
2646 if (so->so_filt || check_cfil) {
2647 #if INET6
2648 if (isipv6) {
2649 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&from;
2650
2651 sin6->sin6_len = sizeof(*sin6);
2652 sin6->sin6_family = AF_INET6;
2653 sin6->sin6_port = th->th_sport;
2654 sin6->sin6_flowinfo = 0;
2655 sin6->sin6_addr = ip6->ip6_src;
2656 sin6->sin6_scope_id = 0;
2657
2658 sin6 = (struct sockaddr_in6*)&to2;
2659
2660 sin6->sin6_len = sizeof(struct sockaddr_in6);
2661 sin6->sin6_family = AF_INET6;
2662 sin6->sin6_port = th->th_dport;
2663 sin6->sin6_flowinfo = 0;
2664 sin6->sin6_addr = ip6->ip6_dst;
2665 sin6->sin6_scope_id = 0;
2666 } else
2667 #endif
2668 {
2669 struct sockaddr_in *sin = (struct sockaddr_in*)&from;
2670
2671 sin->sin_len = sizeof(*sin);
2672 sin->sin_family = AF_INET;
2673 sin->sin_port = th->th_sport;
2674 sin->sin_addr = ip->ip_src;
2675
2676 sin = (struct sockaddr_in*)&to2;
2677
2678 sin->sin_len = sizeof(struct sockaddr_in);
2679 sin->sin_family = AF_INET;
2680 sin->sin_port = th->th_dport;
2681 sin->sin_addr = ip->ip_dst;
2682 }
2683 }
2684
2685 if (so->so_filt) {
2686 so2 = sonewconn(so, 0, (struct sockaddr*)&from);
2687 } else {
2688 so2 = sonewconn(so, 0, NULL);
2689 }
2690 if (so2 == 0) {
2691 tcpstat.tcps_listendrop++;
2692 if (tcp_dropdropablreq(so)) {
2693 if (so->so_filt) {
2694 so2 = sonewconn(so, 0, (struct sockaddr*)&from);
2695 } else {
2696 so2 = sonewconn(so, 0, NULL);
2697 }
2698 }
2699 if (!so2) {
2700 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, " listen drop");
2701 goto drop;
2702 }
2703 }
2704
2705 /* Point "inp" and "tp" in tandem to new socket */
2706 inp = (struct inpcb *)so2->so_pcb;
2707 tp = intotcpcb(inp);
2708
2709 oso = so;
2710 socket_unlock(so, 0); /* Unlock but keep a reference on listener for now */
2711
2712 so = so2;
2713 socket_lock(so, 1);
2714 /*
2715 * Mark socket as temporary until we're
2716 * committed to keeping it. The code at
2717 * ``drop'' and ``dropwithreset'' check the
2718 * flag dropsocket to see if the temporary
2719 * socket created here should be discarded.
2720 * We mark the socket as discardable until
2721 * we're committed to it below in TCPS_LISTEN.
2722 * There are some error conditions in which we
2723 * have to drop the temporary socket.
2724 */
2725 dropsocket++;
2726 /*
2727 * Inherit INP_BOUND_IF from listener; testing if
2728 * head_ifscope is non-NULL is sufficient, since it
2729 * can only be set to a non-zero value earlier if
2730 * the listener has such a flag set.
2731 */
2732 if (head_ifscope != NULL) {
2733 inp->inp_flags |= INP_BOUND_IF;
2734 inp->inp_boundifp = head_ifscope;
2735 } else {
2736 inp->inp_flags &= ~INP_BOUND_IF;
2737 }
2738 /*
2739 * Inherit restrictions from listener.
2740 */
2741 if (head_nocell) {
2742 inp_set_nocellular(inp);
2743 }
2744 if (head_noexpensive) {
2745 inp_set_noexpensive(inp);
2746 }
2747 if (head_noconstrained) {
2748 inp_set_noconstrained(inp);
2749 }
2750 if (head_awdl_unrestricted) {
2751 inp_set_awdl_unrestricted(inp);
2752 }
2753 if (head_intcoproc_allowed) {
2754 inp_set_intcoproc_allowed(inp);
2755 }
2756 /*
2757 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2758 */
2759 if (head_recvanyif) {
2760 inp->inp_flags |= INP_RECV_ANYIF;
2761 } else {
2762 inp->inp_flags &= ~INP_RECV_ANYIF;
2763 }
2764
2765 if (head_external_port) {
2766 inp->inp_flags2 |= INP2_EXTERNAL_PORT;
2767 }
2768 #if INET6
2769 if (isipv6) {
2770 inp->in6p_laddr = ip6->ip6_dst;
2771 } else {
2772 inp->inp_vflag &= ~INP_IPV6;
2773 inp->inp_vflag |= INP_IPV4;
2774 #endif /* INET6 */
2775 inp->inp_laddr = ip->ip_dst;
2776 #if INET6
2777 }
2778 #endif /* INET6 */
2779 inp->inp_lport = th->th_dport;
2780 if (in_pcbinshash(inp, 0) != 0) {
2781 /*
2782 * Undo the assignments above if we failed to
2783 * put the PCB on the hash lists.
2784 */
2785 #if INET6
2786 if (isipv6) {
2787 inp->in6p_laddr = in6addr_any;
2788 } else
2789 #endif /* INET6 */
2790 inp->inp_laddr.s_addr = INADDR_ANY;
2791 inp->inp_lport = 0;
2792 socket_lock(oso, 0); /* release ref on parent */
2793 socket_unlock(oso, 1);
2794 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, " in_pcbinshash failed");
2795 goto drop;
2796 }
2797 socket_lock(oso, 0);
2798 #if INET6
2799 if (isipv6) {
2800 /*
2801 * Inherit socket options from the listening
2802 * socket.
2803 * Note that in6p_inputopts are not (even
2804 * should not be) copied, since it stores
2805 * previously received options and is used to
2806 * detect if each new option is different than
2807 * the previous one and hence should be passed
2808 * to a user.
2809 * If we copied in6p_inputopts, a user would
2810 * not be able to receive options just after
2811 * calling the accept system call.
2812 */
2813 inp->inp_flags |=
2814 oinp->inp_flags & INP_CONTROLOPTS;
2815 if (oinp->in6p_outputopts) {
2816 inp->in6p_outputopts =
2817 ip6_copypktopts(oinp->in6p_outputopts,
2818 M_NOWAIT);
2819 }
2820 } else
2821 #endif /* INET6 */
2822 {
2823 inp->inp_options = ip_srcroute();
2824 inp->inp_ip_tos = oinp->inp_ip_tos;
2825 }
2826 #if IPSEC
2827 /* copy old policy into new socket's */
2828 if (sotoinpcb(oso)->inp_sp) {
2829 int error = 0;
2830 /* Is it a security hole here to silently fail to copy the policy? */
2831 if (inp->inp_sp != NULL) {
2832 error = ipsec_init_policy(so, &inp->inp_sp);
2833 }
2834 if (error != 0 || ipsec_copy_policy(sotoinpcb(oso)->inp_sp, inp->inp_sp)) {
2835 printf("tcp_input: could not copy policy\n");
2836 }
2837 }
2838 #endif
2839 /* inherit states from the listener */
2840 DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp,
2841 struct tcpcb *, tp, int32_t, TCPS_LISTEN);
2842 tp->t_state = TCPS_LISTEN;
2843 tp->t_flags |= tp0->t_flags & (TF_NOPUSH | TF_NOOPT | TF_NODELAY);
2844 tp->t_flagsext |= (tp0->t_flagsext & (TF_RXTFINDROP | TF_NOTIMEWAIT | TF_FASTOPEN));
2845 tp->t_keepinit = tp0->t_keepinit;
2846 tp->t_keepcnt = tp0->t_keepcnt;
2847 tp->t_keepintvl = tp0->t_keepintvl;
2848 tp->t_adaptive_wtimo = tp0->t_adaptive_wtimo;
2849 tp->t_adaptive_rtimo = tp0->t_adaptive_rtimo;
2850 tp->t_inpcb->inp_ip_ttl = tp0->t_inpcb->inp_ip_ttl;
2851 if ((so->so_flags & SOF_NOTSENT_LOWAT) != 0) {
2852 tp->t_notsent_lowat = tp0->t_notsent_lowat;
2853 }
2854 tp->t_inpcb->inp_flags2 |=
2855 tp0->t_inpcb->inp_flags2 & INP2_KEEPALIVE_OFFLOAD;
2856
2857 /* now drop the reference on the listener */
2858 socket_unlock(oso, 1);
2859
2860 tcp_set_max_rwinscale(tp, so, ifp);
2861
2862 #if CONTENT_FILTER
2863 if (check_cfil) {
2864 int error = cfil_sock_attach(so2, (struct sockaddr*)&to2, (struct sockaddr*)&from,
2865 CFS_CONNECTION_DIR_IN);
2866 if (error != 0) {
2867 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, " cfil_sock_attach failed");
2868 goto drop;
2869 }
2870 }
2871 #endif /* CONTENT_FILTER */
2872
2873 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_END, 0, 0, 0, 0, 0);
2874 }
2875 }
2876 socket_lock_assert_owned(so);
2877
2878 if (net_mpklog_enabled && (m->m_pkthdr.rcvif->if_xflags & IFXF_MPK_LOG)) {
2879 MPKL_TCP_INPUT(tcp_mpkl_log_object,
2880 ntohs(tp->t_inpcb->inp_lport), ntohs(tp->t_inpcb->inp_fport),
2881 th->th_seq, th->th_ack, tlen, thflags,
2882 so->last_pid, so->so_log_seqn++);
2883 }
2884
2885 if (tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
2886 /*
2887 * Evaluate the rate of arrival of packets to see if the
2888 * receiver can reduce the ack traffic. The algorithm to
2889 * stretch acks will be enabled if the connection meets
2890 * certain criteria defined in tcp_stretch_ack_enable function.
2891 */
2892 if ((tp->t_flagsext & TF_RCVUNACK_WAITSS) != 0) {
2893 TCP_INC_VAR(tp->rcv_waitforss, nlropkts);
2894 }
2895 if (tcp_stretch_ack_enable(tp, thflags)) {
2896 tp->t_flags |= TF_STRETCHACK;
2897 tp->t_flagsext &= ~(TF_RCVUNACK_WAITSS);
2898 tp->rcv_waitforss = 0;
2899 } else {
2900 tp->t_flags &= ~(TF_STRETCHACK);
2901 }
2902 if (TSTMP_GT(tp->rcv_unackwin - (tcp_rcvunackwin >> 1), tcp_now)) {
2903 tp->rcv_by_unackhalfwin += (tlen + off);
2904 tp->rcv_by_unackwin += (tlen + off);
2905 } else {
2906 tp->rcv_unackwin = tcp_now + tcp_rcvunackwin;
2907 tp->rcv_by_unackwin = tp->rcv_by_unackhalfwin + tlen + off;
2908 tp->rcv_by_unackhalfwin = tlen + off;
2909 }
2910 }
2911
2912 /*
2913 * Keep track of how many bytes were received in the LRO packet
2914 */
2915 if ((pktf_sw_lro_pkt) && (nlropkts > 2)) {
2916 tp->t_lropktlen += tlen;
2917 }
2918 /*
2919 * Explicit Congestion Notification - Flag that we need to send ECT if
2920 * + The IP Congestion experienced flag was set.
2921 * + Socket is in established state
2922 * + We negotiated ECN in the TCP setup
2923 * + This isn't a pure ack (tlen > 0)
2924 * + The data is in the valid window
2925 *
2926 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2927 */
2928 if (ip_ecn == IPTOS_ECN_CE && tp->t_state == TCPS_ESTABLISHED &&
2929 TCP_ECN_ENABLED(tp) && tlen > 0 &&
2930 SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
2931 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2932 tp->t_ecn_recv_ce++;
2933 tcpstat.tcps_ecn_recv_ce++;
2934 INP_INC_IFNET_STAT(inp, ecn_recv_ce);
2935 /* Mark this connection as it received CE from network */
2936 tp->ecn_flags |= TE_RECV_ECN_CE;
2937 tp->ecn_flags |= TE_SENDECE;
2938 }
2939
2940 /*
2941 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2942 * bother doing extensive checks for state and whatnot.
2943 */
2944 if (thflags & TH_CWR) {
2945 tp->ecn_flags &= ~TE_SENDECE;
2946 tp->t_ecn_recv_cwr++;
2947 }
2948
2949 /*
2950 * If we received an explicit notification of congestion in
2951 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2952 * the ack-strteching state. We need to handle ECN notification if
2953 * an ECN setup SYN was sent even once.
2954 */
2955 if (tp->t_state == TCPS_ESTABLISHED
2956 && (tp->ecn_flags & TE_SETUPSENT)
2957 && (ip_ecn == IPTOS_ECN_CE || (thflags & TH_CWR))) {
2958 tcp_reset_stretch_ack(tp);
2959 CLEAR_IAJ_STATE(tp);
2960 }
2961
2962 if (ip_ecn == IPTOS_ECN_CE && tp->t_state == TCPS_ESTABLISHED &&
2963 !TCP_ECN_ENABLED(tp) && !(tp->ecn_flags & TE_CEHEURI_SET)) {
2964 tcpstat.tcps_ecn_fallback_ce++;
2965 tcp_heuristic_ecn_aggressive(tp);
2966 tp->ecn_flags |= TE_CEHEURI_SET;
2967 }
2968
2969 if (tp->t_state == TCPS_ESTABLISHED && TCP_ECN_ENABLED(tp) &&
2970 ip_ecn == IPTOS_ECN_CE && !(tp->ecn_flags & TE_CEHEURI_SET)) {
2971 if (inp->inp_stat->rxpackets < ECN_MIN_CE_PROBES) {
2972 tp->t_ecn_recv_ce_pkt++;
2973 } else if (tp->t_ecn_recv_ce_pkt > ECN_MAX_CE_RATIO) {
2974 tcpstat.tcps_ecn_fallback_ce++;
2975 tcp_heuristic_ecn_aggressive(tp);
2976 tp->ecn_flags |= TE_CEHEURI_SET;
2977 INP_INC_IFNET_STAT(inp, ecn_fallback_ce);
2978 } else {
2979 /* We tracked the first ECN_MIN_CE_PROBES segments, we
2980 * now know that the path is good.
2981 */
2982 tp->ecn_flags |= TE_CEHEURI_SET;
2983 }
2984 }
2985
2986 /*
2987 * Try to determine if we are receiving a packet after a long time.
2988 * Use our own approximation of idletime to roughly measure remote
2989 * end's idle time. Since slowstart is used after an idle period
2990 * we want to avoid doing LRO if the remote end is not up to date
2991 * on initial window support and starts with 1 or 2 packets as its IW.
2992 */
2993 if (sw_lro && (tp->t_flagsext & TF_LRO_OFFLOADED) &&
2994 ((tcp_now - tp->t_rcvtime) >= (TCP_IDLETIMEOUT(tp)))) {
2995 turnoff_lro = 1;
2996 }
2997
2998 /* Update rcvtime as a new segment was received on the connection */
2999 tp->t_rcvtime = tcp_now;
3000
3001 /*
3002 * Segment received on connection.
3003 * Reset idle time and keep-alive timer.
3004 */
3005 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
3006 tcp_keepalive_reset(tp);
3007
3008 if (tp->t_mpsub) {
3009 mptcp_reset_keepalive(tp);
3010 }
3011 }
3012
3013 /*
3014 * Process options if not in LISTEN state,
3015 * else do it below (after getting remote address).
3016 */
3017 if (tp->t_state != TCPS_LISTEN && optp) {
3018 tcp_dooptions(tp, optp, optlen, th, &to);
3019 }
3020 #if MPTCP
3021 if (tp->t_state != TCPS_LISTEN && (so->so_flags & SOF_MP_SUBFLOW) &&
3022 mptcp_input_preproc(tp, m, th, drop_hdrlen) != 0) {
3023 tp->t_flags |= TF_ACKNOW;
3024 (void) tcp_output(tp);
3025 tcp_check_timer_state(tp);
3026 socket_unlock(so, 1);
3027 KERNEL_DEBUG(DBG_FNC_TCP_INPUT |
3028 DBG_FUNC_END, 0, 0, 0, 0, 0);
3029 return;
3030 }
3031 #endif /* MPTCP */
3032 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
3033 if (!(thflags & TH_ACK) ||
3034 (SEQ_GT(th->th_ack, tp->iss) &&
3035 SEQ_LEQ(th->th_ack, tp->snd_max))) {
3036 tcp_finalize_options(tp, &to, ifscope);
3037 }
3038 }
3039
3040 #if TRAFFIC_MGT
3041 /*
3042 * Compute inter-packet arrival jitter. According to RFC 3550,
3043 * inter-packet arrival jitter is defined as the difference in
3044 * packet spacing at the receiver compared to the sender for a
3045 * pair of packets. When two packets of maximum segment size come
3046 * one after the other with consecutive sequence numbers, we
3047 * consider them as packets sent together at the sender and use
3048 * them as a pair to compute inter-packet arrival jitter. This
3049 * metric indicates the delay induced by the network components due
3050 * to queuing in edge/access routers.
3051 */
3052 if (tp->t_state == TCPS_ESTABLISHED &&
3053 (thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK | TH_ECE | TH_PUSH)) == TH_ACK &&
3054 ((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) == 0) &&
3055 ((to.to_flags & TOF_TS) == 0 ||
3056 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
3057 th->th_seq == tp->rcv_nxt && LIST_EMPTY(&tp->t_segq)) {
3058 int seg_size = tlen;
3059 if (tp->iaj_pktcnt <= IAJ_IGNORE_PKTCNT) {
3060 TCP_INC_VAR(tp->iaj_pktcnt, nlropkts);
3061 }
3062
3063 if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) {
3064 seg_size = m->m_pkthdr.lro_pktlen;
3065 }
3066 if (tp->iaj_size == 0 || seg_size > tp->iaj_size ||
3067 (seg_size == tp->iaj_size && tp->iaj_rcv_ts == 0)) {
3068 /*
3069 * State related to inter-arrival jitter is
3070 * uninitialized or we are trying to find a good
3071 * first packet to start computing the metric
3072 */
3073 update_iaj_state(tp, seg_size, 0);
3074 } else {
3075 if (seg_size == tp->iaj_size) {
3076 /*
3077 * Compute inter-arrival jitter taking
3078 * this packet as the second packet
3079 */
3080 if (pktf_sw_lro_pkt) {
3081 compute_iaj(tp, nlropkts,
3082 m->m_pkthdr.lro_elapsed);
3083 } else {
3084 compute_iaj(tp, 1, 0);
3085 }
3086 }
3087 if (seg_size < tp->iaj_size) {
3088 /*
3089 * There is a smaller packet in the stream.
3090 * Some times the maximum size supported
3091 * on a path can change if there is a new
3092 * link with smaller MTU. The receiver will
3093 * not know about this change. If there
3094 * are too many packets smaller than
3095 * iaj_size, we try to learn the iaj_size
3096 * again.
3097 */
3098 TCP_INC_VAR(tp->iaj_small_pkt, nlropkts);
3099 if (tp->iaj_small_pkt > RESET_IAJ_SIZE_THRESH) {
3100 update_iaj_state(tp, seg_size, 1);
3101 } else {
3102 CLEAR_IAJ_STATE(tp);
3103 }
3104 } else {
3105 update_iaj_state(tp, seg_size, 0);
3106 }
3107 }
3108 } else {
3109 CLEAR_IAJ_STATE(tp);
3110 }
3111 #endif /* TRAFFIC_MGT */
3112
3113 /*
3114 * Header prediction: check for the two common cases
3115 * of a uni-directional data xfer. If the packet has
3116 * no control flags, is in-sequence, the window didn't
3117 * change and we're not retransmitting, it's a
3118 * candidate. If the length is zero and the ack moved
3119 * forward, we're the sender side of the xfer. Just
3120 * free the data acked & wake any higher level process
3121 * that was blocked waiting for space. If the length
3122 * is non-zero and the ack didn't move, we're the
3123 * receiver side. If we're getting packets in-order
3124 * (the reassembly queue is empty), add the data to
3125 * the socket buffer and note that we need a delayed ack.
3126 * Make sure that the hidden state-flags are also off.
3127 * Since we check for TCPS_ESTABLISHED above, it can only
3128 * be TH_NEEDSYN.
3129 */
3130 if (tp->t_state == TCPS_ESTABLISHED &&
3131 (thflags & (TH_SYN | TH_FIN | TH_RST | TH_URG | TH_ACK | TH_ECE | TH_CWR)) == TH_ACK &&
3132 ((tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) == 0) &&
3133 ((to.to_flags & TOF_TS) == 0 ||
3134 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
3135 th->th_seq == tp->rcv_nxt &&
3136 tiwin && tiwin == tp->snd_wnd &&
3137 tp->snd_nxt == tp->snd_max) {
3138 /*
3139 * If last ACK falls within this segment's sequence numbers,
3140 * record the timestamp.
3141 * NOTE that the test is modified according to the latest
3142 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3143 */
3144 if ((to.to_flags & TOF_TS) != 0 &&
3145 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
3146 tp->ts_recent_age = tcp_now;
3147 tp->ts_recent = to.to_tsval;
3148 }
3149
3150 if (tlen == 0) {
3151 if (SEQ_GT(th->th_ack, tp->snd_una) &&
3152 SEQ_LEQ(th->th_ack, tp->snd_max) &&
3153 tp->snd_cwnd >= tp->snd_ssthresh &&
3154 (!IN_FASTRECOVERY(tp) &&
3155 ((!(SACK_ENABLED(tp)) &&
3156 tp->t_dupacks < tp->t_rexmtthresh) ||
3157 (SACK_ENABLED(tp) && to.to_nsacks == 0 &&
3158 TAILQ_EMPTY(&tp->snd_holes))))) {
3159 /*
3160 * this is a pure ack for outstanding data.
3161 */
3162 ++tcpstat.tcps_predack;
3163
3164 tcp_bad_rexmt_check(tp, th, &to);
3165
3166 /* Recalculate the RTT */
3167 tcp_compute_rtt(tp, &to, th);
3168
3169 VERIFY(SEQ_GEQ(th->th_ack, tp->snd_una));
3170 acked = BYTES_ACKED(th, tp);
3171 tcpstat.tcps_rcvackpack++;
3172 tcpstat.tcps_rcvackbyte += acked;
3173
3174 /*
3175 * Handle an ack that is in sequence during
3176 * congestion avoidance phase. The
3177 * calculations in this function
3178 * assume that snd_una is not updated yet.
3179 */
3180 if (CC_ALGO(tp)->congestion_avd != NULL) {
3181 CC_ALGO(tp)->congestion_avd(tp, th);
3182 }
3183 tcp_ccdbg_trace(tp, th, TCP_CC_INSEQ_ACK_RCVD);
3184 sbdrop(&so->so_snd, acked);
3185 if (so->so_flags & SOF_ENABLE_MSGS) {
3186 VERIFY(acked <= so->so_msg_state->msg_serial_bytes);
3187 so->so_msg_state->msg_serial_bytes -= acked;
3188 }
3189 tcp_sbsnd_trim(&so->so_snd);
3190
3191 if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
3192 SEQ_LEQ(th->th_ack, tp->snd_recover)) {
3193 tp->snd_recover = th->th_ack - 1;
3194 }
3195 tp->snd_una = th->th_ack;
3196
3197 TCP_RESET_REXMT_STATE(tp);
3198
3199 /*
3200 * pull snd_wl2 up to prevent seq wrap relative
3201 * to th_ack.
3202 */
3203 tp->snd_wl2 = th->th_ack;
3204
3205 if (tp->t_dupacks > 0) {
3206 tp->t_dupacks = 0;
3207 tp->t_rexmtthresh = tcprexmtthresh;
3208 }
3209
3210 /*
3211 * If all outstanding data are acked, stop
3212 * retransmit timer, otherwise restart timer
3213 * using current (possibly backed-off) value.
3214 * If process is waiting for space,
3215 * wakeup/selwakeup/signal. If data
3216 * are ready to send, let tcp_output
3217 * decide between more output or persist.
3218 */
3219 if (tp->snd_una == tp->snd_max) {
3220 tp->t_timer[TCPT_REXMT] = 0;
3221 tp->t_timer[TCPT_PTO] = 0;
3222 } else if (tp->t_timer[TCPT_PERSIST] == 0) {
3223 tp->t_timer[TCPT_REXMT] =
3224 OFFSET_FROM_START(tp,
3225 tp->t_rxtcur);
3226 }
3227 if (!SLIST_EMPTY(&tp->t_rxt_segments) &&
3228 !TCP_DSACK_SEQ_IN_WINDOW(tp,
3229 tp->t_dsack_lastuna, tp->snd_una)) {
3230 tcp_rxtseg_clean(tp);
3231 }
3232
3233 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
3234 tp->t_bwmeas != NULL) {
3235 tcp_bwmeas_check(tp);
3236 }
3237
3238 write_wakeup = 1;
3239 if (!SLIST_EMPTY(&tp->t_notify_ack)) {
3240 tcp_notify_acknowledgement(tp, so);
3241 }
3242
3243 if ((so->so_snd.sb_cc) || (tp->t_flags & TF_ACKNOW)) {
3244 (void) tcp_output(tp);
3245 }
3246
3247 tcp_tfo_rcv_ack(tp, th);
3248
3249 m_freem(m);
3250
3251 tcp_check_timer_state(tp);
3252
3253 tcp_handle_wakeup(so, read_wakeup, write_wakeup);
3254
3255 socket_unlock(so, 1);
3256 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
3257 return;
3258 }
3259 } else if (th->th_ack == tp->snd_una &&
3260 LIST_EMPTY(&tp->t_segq) &&
3261 tlen <= tcp_sbspace(tp)) {
3262 /*
3263 * this is a pure, in-sequence data packet
3264 * with nothing on the reassembly queue and
3265 * we have enough buffer space to take it.
3266 */
3267
3268 /*
3269 * If this is a connection in steady state, start
3270 * coalescing packets belonging to this flow.
3271 */
3272 if (turnoff_lro) {
3273 tcp_lro_remove_state(tp->t_inpcb->inp_laddr,
3274 tp->t_inpcb->inp_faddr,
3275 tp->t_inpcb->inp_lport,
3276 tp->t_inpcb->inp_fport);
3277 tp->t_flagsext &= ~TF_LRO_OFFLOADED;
3278 tp->t_idleat = tp->rcv_nxt;
3279 } else if (sw_lro && !pktf_sw_lro_pkt && !isipv6 &&
3280 (so->so_flags & SOF_USELRO) &&
3281 !IFNET_IS_CELLULAR(m->m_pkthdr.rcvif) &&
3282 (m->m_pkthdr.rcvif->if_type != IFT_LOOP) &&
3283 ((th->th_seq - tp->irs) >
3284 (tp->t_maxseg << lro_start)) &&
3285 ((tp->t_idleat == 0) || ((th->th_seq -
3286 tp->t_idleat) > (tp->t_maxseg << lro_start)))) {
3287 tp->t_flagsext |= TF_LRO_OFFLOADED;
3288 tcp_start_coalescing(ip, th, tlen);
3289 tp->t_idleat = 0;
3290 }
3291
3292 /* Clean receiver SACK report if present */
3293 if (SACK_ENABLED(tp) && tp->rcv_numsacks) {
3294 tcp_clean_sackreport(tp);
3295 }
3296 ++tcpstat.tcps_preddat;
3297 tp->rcv_nxt += tlen;
3298 /*
3299 * Pull snd_wl1 up to prevent seq wrap relative to
3300 * th_seq.
3301 */
3302 tp->snd_wl1 = th->th_seq;
3303 /*
3304 * Pull rcv_up up to prevent seq wrap relative to
3305 * rcv_nxt.
3306 */
3307 tp->rcv_up = tp->rcv_nxt;
3308 TCP_INC_VAR(tcpstat.tcps_rcvpack, nlropkts);
3309 tcpstat.tcps_rcvbyte += tlen;
3310 if (nstat_collect) {
3311 if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) {
3312 INP_ADD_STAT(inp, cell, wifi, wired,
3313 rxpackets, m->m_pkthdr.lro_npkts);
3314 } else {
3315 INP_ADD_STAT(inp, cell, wifi, wired,
3316 rxpackets, 1);
3317 }
3318 INP_ADD_STAT(inp, cell, wifi, wired, rxbytes,
3319 tlen);
3320 inp_set_activity_bitmap(inp);
3321 }
3322
3323 /*
3324 * Calculate the RTT on the receiver only if the
3325 * connection is in streaming mode and the last
3326 * packet was not an end-of-write
3327 */
3328 if (tp->t_flags & TF_STREAMING_ON) {
3329 tcp_compute_rtt(tp, &to, th);
3330 }
3331
3332 tcp_sbrcv_grow(tp, &so->so_rcv, &to, tlen,
3333 TCP_AUTORCVBUF_MAX(ifp));
3334
3335 /*
3336 * Add data to socket buffer.
3337 */
3338 so_recv_data_stat(so, m, 0);
3339 m_adj(m, drop_hdrlen); /* delayed header drop */
3340
3341 /*
3342 * If message delivery (SOF_ENABLE_MSGS) is enabled on
3343 * this socket, deliver the packet received as an
3344 * in-order message with sequence number attached to it.
3345 */
3346 if (isipv6) {
3347 memcpy(&saved_hdr, ip6, sizeof(struct ip6_hdr));
3348 ip6 = (struct ip6_hdr *)&saved_hdr[0];
3349 } else {
3350 memcpy(&saved_hdr, ip, ip->ip_hl << 2);
3351 ip = (struct ip *)&saved_hdr[0];
3352 }
3353 memcpy(&saved_tcphdr, th, sizeof(struct tcphdr));
3354 if (sbappendstream_rcvdemux(so, m,
3355 th->th_seq - (tp->irs + 1), 0)) {
3356 mptcp_handle_input(so);
3357 read_wakeup = 1;
3358 }
3359 th = &saved_tcphdr;
3360
3361 #if INET6
3362 if (isipv6) {
3363 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
3364 (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])),
3365 th->th_seq, th->th_ack, th->th_win);
3366 } else
3367 #endif
3368 {
3369 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
3370 (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)),
3371 th->th_seq, th->th_ack, th->th_win);
3372 }
3373 TCP_INC_VAR(tp->t_unacksegs, nlropkts);
3374 if (DELAY_ACK(tp, th)) {
3375 if ((tp->t_flags & TF_DELACK) == 0) {
3376 tp->t_flags |= TF_DELACK;
3377 tp->t_timer[TCPT_DELACK] = OFFSET_FROM_START(tp, tcp_delack);
3378 }
3379 } else {
3380 tp->t_flags |= TF_ACKNOW;
3381 tcp_output(tp);
3382 }
3383
3384 tcp_adaptive_rwtimo_check(tp, tlen);
3385
3386 if (tlen > 0) {
3387 tcp_tfo_rcv_data(tp);
3388 }
3389
3390 tcp_check_timer_state(tp);
3391
3392 tcp_handle_wakeup(so, read_wakeup, write_wakeup);
3393
3394 socket_unlock(so, 1);
3395 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
3396 return;
3397 }
3398 }
3399
3400 /*
3401 * Calculate amount of space in receive window,
3402 * and then do TCP input processing.
3403 * Receive window is amount of space in rcv queue,
3404 * but not less than advertised window.
3405 */
3406 socket_lock_assert_owned(so);
3407 win = tcp_sbspace(tp);
3408 if (win < 0) {
3409 win = 0;
3410 } else { /* clip rcv window to 4K for modems */
3411 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) {
3412 win = min(win, slowlink_wsize);
3413 }
3414 }
3415 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
3416 #if MPTCP
3417 /*
3418 * Ensure that the subflow receive window isn't greater
3419 * than the connection level receive window.
3420 */
3421 if ((tp->t_mpflags & TMPF_MPTCP_TRUE) &&
3422 (mp_tp = tptomptp(tp))) {
3423 socket_lock_assert_owned(mptetoso(mp_tp->mpt_mpte));
3424
3425 if (tp->rcv_wnd > (int)(mp_tp->mpt_rcvadv - (uint32_t)mp_tp->mpt_rcvnxt)) {
3426 tp->rcv_wnd = mp_tp->mpt_rcvadv - (uint32_t)mp_tp->mpt_rcvnxt;
3427 tcpstat.tcps_mp_reducedwin++;
3428 }
3429 }
3430 #endif /* MPTCP */
3431
3432 switch (tp->t_state) {
3433 /*
3434 * Initialize tp->rcv_nxt, and tp->irs, select an initial
3435 * tp->iss, and send a segment:
3436 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3437 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3438 * Fill in remote peer address fields if not previously specified.
3439 * Enter SYN_RECEIVED state, and process any other fields of this
3440 * segment in this state.
3441 */
3442 case TCPS_LISTEN: {
3443 struct sockaddr_in *sin;
3444 #if INET6
3445 struct sockaddr_in6 *sin6;
3446 #endif
3447
3448 socket_lock_assert_owned(so);
3449
3450 /* Clear the logging flags inherited from the listening socket */
3451 tp->t_log_flags = 0;
3452 tp->t_flagsext &= ~TF_LOGGED_CONN_SUMMARY;
3453
3454 #if INET6
3455 if (isipv6) {
3456 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
3457 M_SONAME, M_NOWAIT);
3458 if (sin6 == NULL) {
3459 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "LISTEN malloc M_SONAME failed");
3460 goto drop;
3461 }
3462 bzero(sin6, sizeof(*sin6));
3463 sin6->sin6_family = AF_INET6;
3464 sin6->sin6_len = sizeof(*sin6);
3465 sin6->sin6_addr = ip6->ip6_src;
3466 sin6->sin6_port = th->th_sport;
3467 laddr6 = inp->in6p_laddr;
3468 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
3469 inp->in6p_laddr = ip6->ip6_dst;
3470 }
3471 if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
3472 proc0)) {
3473 inp->in6p_laddr = laddr6;
3474 FREE(sin6, M_SONAME);
3475 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, " LISTEN in6_pcbconnect failed");
3476 goto drop;
3477 }
3478 FREE(sin6, M_SONAME);
3479 } else
3480 #endif
3481 {
3482 socket_lock_assert_owned(so);
3483 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
3484 M_NOWAIT);
3485 if (sin == NULL) {
3486 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "LISTEN malloc M_SONAME failed");
3487 goto drop;
3488 }
3489 sin->sin_family = AF_INET;
3490 sin->sin_len = sizeof(*sin);
3491 sin->sin_addr = ip->ip_src;
3492 sin->sin_port = th->th_sport;
3493 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
3494 laddr = inp->inp_laddr;
3495 if (inp->inp_laddr.s_addr == INADDR_ANY) {
3496 inp->inp_laddr = ip->ip_dst;
3497 }
3498 if (in_pcbconnect(inp, (struct sockaddr *)sin, proc0,
3499 IFSCOPE_NONE, NULL)) {
3500 inp->inp_laddr = laddr;
3501 FREE(sin, M_SONAME);
3502 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, " LISTEN in_pcbconnect failed");
3503 goto drop;
3504 }
3505 FREE(sin, M_SONAME);
3506 }
3507
3508 tcp_dooptions(tp, optp, optlen, th, &to);
3509 tcp_finalize_options(tp, &to, ifscope);
3510
3511 if (tfo_enabled(tp) && tcp_tfo_syn(tp, &to)) {
3512 isconnected = TRUE;
3513 }
3514
3515 if (iss) {
3516 tp->iss = iss;
3517 } else {
3518 tp->iss = tcp_new_isn(tp);
3519 }
3520 tp->irs = th->th_seq;
3521 tcp_sendseqinit(tp);
3522 tcp_rcvseqinit(tp);
3523 tp->snd_recover = tp->snd_una;
3524 /*
3525 * Initialization of the tcpcb for transaction;
3526 * set SND.WND = SEG.WND,
3527 * initialize CCsend and CCrecv.
3528 */
3529 tp->snd_wnd = tiwin; /* initial send-window */
3530 tp->max_sndwnd = tp->snd_wnd;
3531 tp->t_flags |= TF_ACKNOW;
3532 tp->t_unacksegs = 0;
3533 DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp,
3534 struct tcpcb *, tp, int32_t, TCPS_SYN_RECEIVED);
3535 tp->t_state = TCPS_SYN_RECEIVED;
3536 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
3537 TCP_CONN_KEEPINIT(tp));
3538 tp->t_connect_time = tcp_now;
3539 dropsocket = 0; /* committed to socket */
3540
3541 if (inp->inp_flowhash == 0) {
3542 inp->inp_flowhash = inp_calc_flowhash(inp);
3543 }
3544 #if INET6
3545 /* update flowinfo - RFC 6437 */
3546 if (inp->inp_flow == 0 &&
3547 inp->in6p_flags & IN6P_AUTOFLOWLABEL) {
3548 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
3549 inp->inp_flow |=
3550 (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK);
3551 }
3552 #endif /* INET6 */
3553
3554 /* reset the incomp processing flag */
3555 so->so_flags &= ~(SOF_INCOMP_INPROGRESS);
3556 tcpstat.tcps_accepts++;
3557 if ((thflags & (TH_ECE | TH_CWR)) == (TH_ECE | TH_CWR)) {
3558 /* ECN-setup SYN */
3559 tp->ecn_flags |= (TE_SETUPRECEIVED | TE_SENDIPECT);
3560 }
3561
3562 /*
3563 * The address and connection state are finalized
3564 */
3565 TCP_LOG_CONNECT(tp, false, 0);
3566
3567 goto trimthenstep6;
3568 }
3569
3570 /*
3571 * If the state is SYN_RECEIVED and the seg contains an ACK,
3572 * but not for our SYN/ACK, send a RST.
3573 */
3574 case TCPS_SYN_RECEIVED:
3575 if ((thflags & TH_ACK) &&
3576 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
3577 SEQ_GT(th->th_ack, tp->snd_max))) {
3578 rstreason = BANDLIM_RST_OPENPORT;
3579 IF_TCP_STATINC(ifp, ooopacket);
3580 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN_RECEIVED bad ACK");
3581 goto dropwithreset;
3582 }
3583
3584 /*
3585 * In SYN_RECEIVED state, if we recv some SYNS with
3586 * window scale and others without, window scaling should
3587 * be disabled. Otherwise the window advertised will be
3588 * lower if we assume scaling and the other end does not.
3589 */
3590 if ((thflags & TH_SYN) &&
3591 (tp->irs == th->th_seq) &&
3592 !(to.to_flags & TOF_SCALE)) {
3593 tp->t_flags &= ~TF_RCVD_SCALE;
3594 }
3595 break;
3596
3597 /*
3598 * If the state is SYN_SENT:
3599 * if seg contains an ACK, but not for our SYN, drop the input.
3600 * if seg contains a RST, then drop the connection.
3601 * if seg does not contain SYN, then drop it.
3602 * Otherwise this is an acceptable SYN segment
3603 * initialize tp->rcv_nxt and tp->irs
3604 * if seg contains ack then advance tp->snd_una
3605 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
3606 * arrange for segment to be acked (eventually)
3607 * continue processing rest of data/controls, beginning with URG
3608 */
3609 case TCPS_SYN_SENT:
3610 if ((thflags & TH_ACK) &&
3611 (SEQ_LEQ(th->th_ack, tp->iss) ||
3612 SEQ_GT(th->th_ack, tp->snd_max))) {
3613 rstreason = BANDLIM_UNLIMITED;
3614 IF_TCP_STATINC(ifp, ooopacket);
3615 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN_SENT bad ACK");
3616 goto dropwithreset;
3617 }
3618 if (thflags & TH_RST) {
3619 if ((thflags & TH_ACK) != 0) {
3620 if (tfo_enabled(tp) &&
3621 !(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE)) {
3622 tcp_heuristic_tfo_rst(tp);
3623 }
3624 if ((tp->ecn_flags & (TE_SETUPSENT | TE_RCVD_SYN_RST)) == TE_SETUPSENT) {
3625 /*
3626 * On local connections, send
3627 * non-ECN syn one time before
3628 * dropping the connection
3629 */
3630 if (tp->t_flags & TF_LOCAL) {
3631 tp->ecn_flags |= TE_RCVD_SYN_RST;
3632 goto drop;
3633 } else {
3634 tcp_heuristic_ecn_synrst(tp);
3635 }
3636 }
3637 soevent(so,
3638 (SO_FILT_HINT_LOCKED |
3639 SO_FILT_HINT_CONNRESET));
3640 tp = tcp_drop(tp, ECONNREFUSED);
3641 postevent(so, 0, EV_RESET);
3642 }
3643 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN_SENT got RST");
3644 goto drop;
3645 }
3646 if ((thflags & TH_SYN) == 0) {
3647 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN_SENT no SYN");
3648 goto drop;
3649 }
3650 tp->snd_wnd = th->th_win; /* initial send window */
3651 tp->max_sndwnd = tp->snd_wnd;
3652
3653 tp->irs = th->th_seq;
3654 tcp_rcvseqinit(tp);
3655 if (thflags & TH_ACK) {
3656 tcpstat.tcps_connects++;
3657
3658 if ((thflags & (TH_ECE | TH_CWR)) == (TH_ECE)) {
3659 /* ECN-setup SYN-ACK */
3660 tp->ecn_flags |= TE_SETUPRECEIVED;
3661 if (TCP_ECN_ENABLED(tp)) {
3662 tcp_heuristic_ecn_success(tp);
3663 tcpstat.tcps_ecn_client_success++;
3664 }
3665 } else {
3666 if (tp->ecn_flags & TE_SETUPSENT &&
3667 tp->t_rxtshift == 0) {
3668 tcp_heuristic_ecn_success(tp);
3669 tcpstat.tcps_ecn_not_supported++;
3670 }
3671 if (tp->ecn_flags & TE_SETUPSENT &&
3672 tp->t_rxtshift > 0) {
3673 tcp_heuristic_ecn_loss(tp);
3674 }
3675
3676 /* non-ECN-setup SYN-ACK */
3677 tp->ecn_flags &= ~TE_SENDIPECT;
3678 }
3679
3680 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
3681 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
3682 mac_socketpeer_label_associate_mbuf(m, so);
3683 /* XXXMAC: SOCK_UNLOCK(so); */
3684 #endif
3685 /* Do window scaling on this connection? */
3686 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
3687 tp->snd_scale = tp->requested_s_scale;
3688 tp->rcv_scale = tp->request_r_scale;
3689 }
3690
3691 tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN << tp->rcv_scale);
3692 tp->snd_una++; /* SYN is acked */
3693 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
3694 tp->snd_nxt = tp->snd_una;
3695 }
3696
3697 /*
3698 * We have sent more in the SYN than what is being
3699 * acked. (e.g., TFO)
3700 * We should restart the sending from what the receiver
3701 * has acknowledged immediately.
3702 */
3703 if (SEQ_GT(tp->snd_nxt, th->th_ack)) {
3704 /*
3705 * rdar://problem/33214601
3706 * There is a middlebox that acks all but one
3707 * byte and still drops the data.
3708 */
3709 if (!(tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) &&
3710 (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) &&
3711 tp->snd_max == th->th_ack + 1 &&
3712 tp->snd_max > tp->snd_una + 1) {
3713 tcp_heuristic_tfo_middlebox(tp);
3714
3715 so->so_error = ENODATA;
3716 soevent(so,
3717 (SO_FILT_HINT_LOCKED | SO_FILT_HINT_MP_SUB_ERROR));
3718
3719 tp->t_tfo_stats |= TFO_S_ONE_BYTE_PROXY;
3720 }
3721
3722 tp->snd_max = tp->snd_nxt = th->th_ack;
3723 }
3724
3725 /*
3726 * If there's data, delay ACK; if there's also a FIN
3727 * ACKNOW will be turned on later.
3728 */
3729 TCP_INC_VAR(tp->t_unacksegs, nlropkts);
3730 if (DELAY_ACK(tp, th) && tlen != 0) {
3731 if ((tp->t_flags & TF_DELACK) == 0) {
3732 tp->t_flags |= TF_DELACK;
3733 tp->t_timer[TCPT_DELACK] = OFFSET_FROM_START(tp, tcp_delack);
3734 }
3735 } else {
3736 tp->t_flags |= TF_ACKNOW;
3737 }
3738 /*
3739 * Received <SYN,ACK> in SYN_SENT[*] state.
3740 * Transitions:
3741 * SYN_SENT --> ESTABLISHED
3742 * SYN_SENT* --> FIN_WAIT_1
3743 */
3744 tp->t_starttime = tcp_now;
3745 tcp_sbrcv_tstmp_check(tp);
3746 if (tp->t_flags & TF_NEEDFIN) {
3747 DTRACE_TCP4(state__change, void, NULL,
3748 struct inpcb *, inp,
3749 struct tcpcb *, tp, int32_t,
3750 TCPS_FIN_WAIT_1);
3751 tp->t_state = TCPS_FIN_WAIT_1;
3752 tp->t_flags &= ~TF_NEEDFIN;
3753 thflags &= ~TH_SYN;
3754
3755 TCP_LOG_CONNECTION_SUMMARY(tp);
3756 } else {
3757 DTRACE_TCP4(state__change, void, NULL,
3758 struct inpcb *, inp, struct tcpcb *,
3759 tp, int32_t, TCPS_ESTABLISHED);
3760 tp->t_state = TCPS_ESTABLISHED;
3761 tp->t_timer[TCPT_KEEP] =
3762 OFFSET_FROM_START(tp,
3763 TCP_CONN_KEEPIDLE(tp));
3764 if (nstat_collect) {
3765 nstat_route_connect_success(
3766 inp->inp_route.ro_rt);
3767 }
3768 /*
3769 * The SYN is acknowledged but una is not
3770 * updated yet. So pass the value of
3771 * ack to compute sndbytes correctly
3772 */
3773 inp_count_sndbytes(inp, th->th_ack);
3774 }
3775 #if MPTCP
3776 /*
3777 * Do not send the connect notification for additional
3778 * subflows until ACK for 3-way handshake arrives.
3779 */
3780 if ((!(tp->t_mpflags & TMPF_MPTCP_TRUE)) &&
3781 (tp->t_mpflags & TMPF_SENT_JOIN)) {
3782 isconnected = FALSE;
3783 } else
3784 #endif /* MPTCP */
3785 isconnected = TRUE;
3786
3787 if ((tp->t_tfo_flags & (TFO_F_COOKIE_REQ | TFO_F_COOKIE_SENT)) ||
3788 (tp->t_tfo_stats & TFO_S_SYN_DATA_SENT)) {
3789 tcp_tfo_synack(tp, &to);
3790
3791 if ((tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) &&
3792 SEQ_LT(tp->snd_una, th->th_ack)) {
3793 tp->t_tfo_stats |= TFO_S_SYN_DATA_ACKED;
3794 tcpstat.tcps_tfo_syn_data_acked++;
3795 #if MPTCP
3796 if (so->so_flags & SOF_MP_SUBFLOW) {
3797 so->so_flags1 |= SOF1_TFO_REWIND;
3798 }
3799 #endif
3800 tcp_tfo_rcv_probe(tp, tlen);
3801 }
3802 }
3803 } else {
3804 /*
3805 * Received initial SYN in SYN-SENT[*] state => simul-
3806 * taneous open. If segment contains CC option and there is
3807 * a cached CC, apply TAO test; if it succeeds, connection is
3808 * half-synchronized. Otherwise, do 3-way handshake:
3809 * SYN-SENT -> SYN-RECEIVED
3810 * SYN-SENT* -> SYN-RECEIVED*
3811 */
3812 tp->t_flags |= TF_ACKNOW;
3813 tp->t_timer[TCPT_REXMT] = 0;
3814 DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp,
3815 struct tcpcb *, tp, int32_t, TCPS_SYN_RECEIVED);
3816 tp->t_state = TCPS_SYN_RECEIVED;
3817
3818 /*
3819 * During simultaneous open, TFO should not be used.
3820 * So, we disable it here, to prevent that data gets
3821 * sent on the SYN/ACK.
3822 */
3823 tcp_disable_tfo(tp);
3824 }
3825
3826 trimthenstep6:
3827 /*
3828 * Advance th->th_seq to correspond to first data byte.
3829 * If data, trim to stay within window,
3830 * dropping FIN if necessary.
3831 */
3832 th->th_seq++;
3833 if (tlen > tp->rcv_wnd) {
3834 todrop = tlen - tp->rcv_wnd;
3835 m_adj(m, -todrop);
3836 tlen = tp->rcv_wnd;
3837 thflags &= ~TH_FIN;
3838 tcpstat.tcps_rcvpackafterwin++;
3839 tcpstat.tcps_rcvbyteafterwin += todrop;
3840 }
3841 tp->snd_wl1 = th->th_seq - 1;
3842 tp->rcv_up = th->th_seq;
3843 /*
3844 * Client side of transaction: already sent SYN and data.
3845 * If the remote host used T/TCP to validate the SYN,
3846 * our data will be ACK'd; if so, enter normal data segment
3847 * processing in the middle of step 5, ack processing.
3848 * Otherwise, goto step 6.
3849 */
3850 if (thflags & TH_ACK) {
3851 goto process_ACK;
3852 }
3853 goto step6;
3854 /*
3855 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3856 * do normal processing.
3857 *
3858 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3859 */
3860 case TCPS_LAST_ACK:
3861 case TCPS_CLOSING:
3862 case TCPS_TIME_WAIT:
3863 break; /* continue normal processing */
3864
3865 /* Received a SYN while connection is already established.
3866 * This is a "half open connection and other anomalies" described
3867 * in RFC793 page 34, send an ACK so the remote reset the connection
3868 * or recovers by adjusting its sequence numbering. Sending an ACK is
3869 * in accordance with RFC 5961 Section 4.2
3870 */
3871 case TCPS_ESTABLISHED:
3872 if (thflags & TH_SYN) {
3873 /* Drop the packet silently if we have reached the limit */
3874 if (tcp_do_rfc5961 && tcp_is_ack_ratelimited(tp)) {
3875 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "ESTABLISHED rfc5961 rate limited");
3876 goto drop;
3877 } else {
3878 /* Send challenge ACK */
3879 tcpstat.tcps_synchallenge++;
3880 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "ESTABLISHED rfc5961 challenge ACK");
3881 goto dropafterack;
3882 }
3883 }
3884 break;
3885 }
3886
3887 /*
3888 * States other than LISTEN or SYN_SENT.
3889 * First check the RST flag and sequence number since reset segments
3890 * are exempt from the timestamp and connection count tests. This
3891 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3892 * below which allowed reset segments in half the sequence space
3893 * to fall though and be processed (which gives forged reset
3894 * segments with a random sequence number a 50 percent chance of
3895 * killing a connection).
3896 * Then check timestamp, if present.
3897 * Then check the connection count, if present.
3898 * Then check that at least some bytes of segment are within
3899 * receive window. If segment begins before rcv_nxt,
3900 * drop leading data (and SYN); if nothing left, just ack.
3901 *
3902 *
3903 * If the RST bit is set, check the sequence number to see
3904 * if this is a valid reset segment.
3905 * RFC 793 page 37:
3906 * In all states except SYN-SENT, all reset (RST) segments
3907 * are validated by checking their SEQ-fields. A reset is
3908 * valid if its sequence number is in the window.
3909 * Note: this does not take into account delayed ACKs, so
3910 * we should test against last_ack_sent instead of rcv_nxt.
3911 * The sequence number in the reset segment is normally an
3912 * echo of our outgoing acknowlegement numbers, but some hosts
3913 * send a reset with the sequence number at the rightmost edge
3914 * of our receive window, and we have to handle this case.
3915 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3916 * that brute force RST attacks are possible. To combat this,
3917 * we use a much stricter check while in the ESTABLISHED state,
3918 * only accepting RSTs where the sequence number is equal to
3919 * last_ack_sent. In all other states (the states in which a
3920 * RST is more likely), the more permissive check is used.
3921 * RFC 5961 Section 3.2: if the RST bit is set, sequence # is
3922 * within the receive window and last_ack_sent == seq,
3923 * then reset the connection. Otherwise if the seq doesn't
3924 * match last_ack_sent, TCP must send challenge ACK. Perform
3925 * rate limitation when sending the challenge ACK.
3926 * If we have multiple segments in flight, the intial reset
3927 * segment sequence numbers will be to the left of last_ack_sent,
3928 * but they will eventually catch up.
3929 * In any case, it never made sense to trim reset segments to
3930 * fit the receive window since RFC 1122 says:
3931 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3932 *
3933 * A TCP SHOULD allow a received RST segment to include data.
3934 *
3935 * DISCUSSION
3936 * It has been suggested that a RST segment could contain
3937 * ASCII text that encoded and explained the cause of the
3938 * RST. No standard has yet been established for such
3939 * data.
3940 *
3941 * If the reset segment passes the sequence number test examine
3942 * the state:
3943 * SYN_RECEIVED STATE:
3944 * If passive open, return to LISTEN state.
3945 * If active open, inform user that connection was refused.
3946 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3947 * Inform user that connection was reset, and close tcb.
3948 * CLOSING, LAST_ACK STATES:
3949 * Close the tcb.
3950 * TIME_WAIT STATE:
3951 * Drop the segment - see Stevens, vol. 2, p. 964 and
3952 * RFC 1337.
3953 *
3954 * Radar 4803931: Allows for the case where we ACKed the FIN but
3955 * there is already a RST in flight from the peer.
3956 * In that case, accept the RST for non-established
3957 * state if it's one off from last_ack_sent.
3958 *
3959 */
3960 if (thflags & TH_RST) {
3961 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
3962 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
3963 (tp->rcv_wnd == 0 &&
3964 ((tp->last_ack_sent == th->th_seq) ||
3965 ((tp->last_ack_sent - 1) == th->th_seq)))) {
3966 if (tcp_do_rfc5961 == 0 || tp->last_ack_sent == th->th_seq) {
3967 switch (tp->t_state) {
3968 case TCPS_SYN_RECEIVED:
3969 IF_TCP_STATINC(ifp, rstinsynrcv);
3970 so->so_error = ECONNREFUSED;
3971 goto close;
3972
3973 case TCPS_ESTABLISHED:
3974 if (tcp_do_rfc5961 == 0 && tp->last_ack_sent != th->th_seq) {
3975 tcpstat.tcps_badrst++;
3976 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "ESTABLISHED rfc5961 bad RST");
3977 goto drop;
3978 }
3979 if (TCP_ECN_ENABLED(tp) &&
3980 tp->snd_una == tp->iss + 1 &&
3981 SEQ_GT(tp->snd_max, tp->snd_una)) {
3982 /*
3983 * If the first data packet on an
3984 * ECN connection, receives a RST
3985 * increment the heuristic
3986 */
3987 tcp_heuristic_ecn_droprst(tp);
3988 }
3989 case TCPS_FIN_WAIT_1:
3990 case TCPS_CLOSE_WAIT:
3991 /*
3992 * Drop through ...
3993 */
3994 case TCPS_FIN_WAIT_2:
3995 so->so_error = ECONNRESET;
3996 close:
3997 postevent(so, 0, EV_RESET);
3998 soevent(so,
3999 (SO_FILT_HINT_LOCKED |
4000 SO_FILT_HINT_CONNRESET));
4001
4002 tcpstat.tcps_drops++;
4003 tp = tcp_close(tp);
4004 break;
4005
4006 case TCPS_CLOSING:
4007 case TCPS_LAST_ACK:
4008 tp = tcp_close(tp);
4009 break;
4010
4011 case TCPS_TIME_WAIT:
4012 break;
4013 }
4014 } else if (tcp_do_rfc5961) {
4015 tcpstat.tcps_badrst++;
4016 /* Drop if we have reached the ACK limit */
4017 if (tcp_is_ack_ratelimited(tp)) {
4018 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "ESTABLISHED rfc5961 rate limited");
4019 goto drop;
4020 } else {
4021 /* Send challenge ACK */
4022 tcpstat.tcps_rstchallenge++;
4023 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "ESTABLISHED rfc5961 challenge ACK");
4024 goto dropafterack;
4025 }
4026 }
4027 }
4028 goto drop;
4029 }
4030
4031 /*
4032 * RFC 1323 PAWS: If we have a timestamp reply on this segment
4033 * and it's less than ts_recent, drop it.
4034 */
4035 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
4036 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
4037 /* Check to see if ts_recent is over 24 days old. */
4038 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
4039 /*
4040 * Invalidate ts_recent. If this segment updates
4041 * ts_recent, the age will be reset later and ts_recent
4042 * will get a valid value. If it does not, setting
4043 * ts_recent to zero will at least satisfy the
4044 * requirement that zero be placed in the timestamp
4045 * echo reply when ts_recent isn't valid. The
4046 * age isn't reset until we get a valid ts_recent
4047 * because we don't want out-of-order segments to be
4048 * dropped when ts_recent is old.
4049 */
4050 tp->ts_recent = 0;
4051 } else {
4052 tcpstat.tcps_rcvduppack++;
4053 tcpstat.tcps_rcvdupbyte += tlen;
4054 tp->t_pawsdrop++;
4055 tcpstat.tcps_pawsdrop++;
4056
4057 /*
4058 * PAWS-drop when ECN is being used? That indicates
4059 * that ECT-marked packets take a different path, with
4060 * different congestion-characteristics.
4061 *
4062 * Only fallback when we did send less than 2GB as PAWS
4063 * really has no reason to kick in earlier.
4064 */
4065 if (TCP_ECN_ENABLED(tp) &&
4066 inp->inp_stat->rxbytes < 2147483648) {
4067 INP_INC_IFNET_STAT(inp, ecn_fallback_reorder);
4068 tcpstat.tcps_ecn_fallback_reorder++;
4069 tcp_heuristic_ecn_aggressive(tp);
4070 }
4071
4072 if (nstat_collect) {
4073 nstat_route_rx(tp->t_inpcb->inp_route.ro_rt,
4074 1, tlen, NSTAT_RX_FLAG_DUPLICATE);
4075 INP_ADD_STAT(inp, cell, wifi, wired,
4076 rxpackets, 1);
4077 INP_ADD_STAT(inp, cell, wifi, wired,
4078 rxbytes, tlen);
4079 tp->t_stat.rxduplicatebytes += tlen;
4080 inp_set_activity_bitmap(inp);
4081 }
4082 if (tlen > 0) {
4083 goto dropafterack;
4084 }
4085 goto drop;
4086 }
4087 }
4088
4089 /*
4090 * In the SYN-RECEIVED state, validate that the packet belongs to
4091 * this connection before trimming the data to fit the receive
4092 * window. Check the sequence number versus IRS since we know
4093 * the sequence numbers haven't wrapped. This is a partial fix
4094 * for the "LAND" DoS attack.
4095 */
4096 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
4097 rstreason = BANDLIM_RST_OPENPORT;
4098 IF_TCP_STATINC(ifp, dospacket);
4099 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SYN_RECEIVED bad SEQ");
4100 goto dropwithreset;
4101 }
4102
4103 /*
4104 * Check if there is old data at the beginning of the window
4105 * i.e. the sequence number is before rcv_nxt
4106 */
4107 todrop = tp->rcv_nxt - th->th_seq;
4108 if (todrop > 0) {
4109 boolean_t is_syn_set = FALSE;
4110
4111 if (thflags & TH_SYN) {
4112 is_syn_set = TRUE;
4113 thflags &= ~TH_SYN;
4114 th->th_seq++;
4115 if (th->th_urp > 1) {
4116 th->th_urp--;
4117 } else {
4118 thflags &= ~TH_URG;
4119 }
4120 todrop--;
4121 }
4122 /*
4123 * Following if statement from Stevens, vol. 2, p. 960.
4124 * The amount of duplicate data is greater than or equal
4125 * to the size of the segment - entire segment is duplicate
4126 */
4127 if (todrop > tlen
4128 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
4129 /*
4130 * Any valid FIN must be to the left of the window.
4131 * At this point the FIN must be a duplicate or out
4132 * of sequence; drop it.
4133 */
4134 thflags &= ~TH_FIN;
4135
4136 /*
4137 * Send an ACK to resynchronize and drop any data.
4138 * But keep on processing for RST or ACK.
4139 *
4140 * If the SYN bit was originally set, then only send
4141 * an ACK if we are not rate-limiting this connection.
4142 */
4143 if (tcp_do_rfc5961 && is_syn_set) {
4144 if (!tcp_is_ack_ratelimited(tp)) {
4145 tcpstat.tcps_synchallenge++;
4146 tp->t_flags |= TF_ACKNOW;
4147 }
4148 } else {
4149 tp->t_flags |= TF_ACKNOW;
4150 }
4151
4152 if (todrop == 1) {
4153 /* This could be a keepalive */
4154 soevent(so, SO_FILT_HINT_LOCKED |
4155 SO_FILT_HINT_KEEPALIVE);
4156 }
4157 todrop = tlen;
4158 tcpstat.tcps_rcvduppack++;
4159 tcpstat.tcps_rcvdupbyte += todrop;
4160 } else {
4161 tcpstat.tcps_rcvpartduppack++;
4162 tcpstat.tcps_rcvpartdupbyte += todrop;
4163 }
4164
4165 if (TCP_DSACK_ENABLED(tp) && todrop > 1) {
4166 /*
4167 * Note the duplicate data sequence space so that
4168 * it can be reported in DSACK option.
4169 */
4170 tp->t_dsack_lseq = th->th_seq;
4171 tp->t_dsack_rseq = th->th_seq + todrop;
4172 tp->t_flags |= TF_ACKNOW;
4173 }
4174 if (nstat_collect) {
4175 nstat_route_rx(tp->t_inpcb->inp_route.ro_rt, 1,
4176 todrop, NSTAT_RX_FLAG_DUPLICATE);
4177 INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1);
4178 INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, todrop);
4179 tp->t_stat.rxduplicatebytes += todrop;
4180 inp_set_activity_bitmap(inp);
4181 }
4182 drop_hdrlen += todrop; /* drop from the top afterwards */
4183 th->th_seq += todrop;
4184 tlen -= todrop;
4185 if (th->th_urp > todrop) {
4186 th->th_urp -= todrop;
4187 } else {
4188 thflags &= ~TH_URG;
4189 th->th_urp = 0;
4190 }
4191 }
4192
4193 /*
4194 * If new data are received on a connection after the user
4195 * processes are gone, then RST the other end.
4196 * Send also a RST when we received a data segment after we've
4197 * sent our FIN when the socket is defunct.
4198 * Note that an MPTCP subflow socket would have SS_NOFDREF set
4199 * by default. So, if it's an MPTCP-subflow we rather check the
4200 * MPTCP-level's socket state for SS_NOFDREF.
4201 */
4202 if (tlen) {
4203 boolean_t close_it = FALSE;
4204
4205 if (!(so->so_flags & SOF_MP_SUBFLOW) && (so->so_state & SS_NOFDREF) &&
4206 tp->t_state > TCPS_CLOSE_WAIT) {
4207 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SS_NOFDREF");
4208 close_it = TRUE;
4209 }
4210
4211 if ((so->so_flags & SOF_MP_SUBFLOW) && (mptetoso(tptomptp(tp)->mpt_mpte)->so_state & SS_NOFDREF) &&
4212 tp->t_state > TCPS_CLOSE_WAIT) {
4213 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SOF_MP_SUBFLOW SS_NOFDREF");
4214 close_it = TRUE;
4215 }
4216
4217 if ((so->so_flags & SOF_DEFUNCT) && tp->t_state > TCPS_FIN_WAIT_1) {
4218 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "SOF_DEFUNCT");
4219 close_it = TRUE;
4220 }
4221
4222 if (close_it) {
4223 tp = tcp_close(tp);
4224 tcpstat.tcps_rcvafterclose++;
4225 rstreason = BANDLIM_UNLIMITED;
4226 IF_TCP_STATINC(ifp, cleanup);
4227 goto dropwithreset;
4228 }
4229 }
4230
4231 /*
4232 * If segment ends after window, drop trailing data
4233 * (and PUSH and FIN); if nothing left, just ACK.
4234 */
4235 todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
4236 if (todrop > 0) {
4237 tcpstat.tcps_rcvpackafterwin++;
4238 if (todrop >= tlen) {
4239 tcpstat.tcps_rcvbyteafterwin += tlen;
4240 /*
4241 * If a new connection request is received
4242 * while in TIME_WAIT, drop the old connection
4243 * and start over if the sequence numbers
4244 * are above the previous ones.
4245 */
4246 if (thflags & TH_SYN &&
4247 tp->t_state == TCPS_TIME_WAIT &&
4248 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
4249 iss = tcp_new_isn(tp);
4250 tp = tcp_close(tp);
4251 socket_unlock(so, 1);
4252 goto findpcb;
4253 }
4254 /*
4255 * If window is closed can only take segments at
4256 * window edge, and have to drop data and PUSH from
4257 * incoming segments. Continue processing, but
4258 * remember to ack. Otherwise, drop segment
4259 * and ack.
4260 */
4261 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
4262 tp->t_flags |= TF_ACKNOW;
4263 tcpstat.tcps_rcvwinprobe++;
4264 } else {
4265 goto dropafterack;
4266 }
4267 } else {
4268 tcpstat.tcps_rcvbyteafterwin += todrop;
4269 }
4270 m_adj(m, -todrop);
4271 tlen -= todrop;
4272 thflags &= ~(TH_PUSH | TH_FIN);
4273 }
4274
4275 /*
4276 * If last ACK falls within this segment's sequence numbers,
4277 * record its timestamp.
4278 * NOTE:
4279 * 1) That the test incorporates suggestions from the latest
4280 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
4281 * 2) That updating only on newer timestamps interferes with
4282 * our earlier PAWS tests, so this check should be solely
4283 * predicated on the sequence space of this segment.
4284 * 3) That we modify the segment boundary check to be
4285 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
4286 * instead of RFC1323's
4287 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
4288 * This modified check allows us to overcome RFC1323's
4289 * limitations as described in Stevens TCP/IP Illustrated
4290 * Vol. 2 p.869. In such cases, we can still calculate the
4291 * RTT correctly when RCV.NXT == Last.ACK.Sent.
4292 */
4293 if ((to.to_flags & TOF_TS) != 0 &&
4294 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
4295 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
4296 ((thflags & (TH_SYN | TH_FIN)) != 0))) {
4297 tp->ts_recent_age = tcp_now;
4298 tp->ts_recent = to.to_tsval;
4299 }
4300
4301 /*
4302 * Stevens: If a SYN is in the window, then this is an
4303 * error and we send an RST and drop the connection.
4304 *
4305 * RFC 5961 Section 4.2
4306 * Send challenge ACK for any SYN in synchronized state
4307 * Perform rate limitation in doing so.
4308 */
4309 if (thflags & TH_SYN) {
4310 if (tcp_do_rfc5961) {
4311 tcpstat.tcps_badsyn++;
4312 /* Drop if we have reached ACK limit */
4313 if (tcp_is_ack_ratelimited(tp)) {
4314 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "rfc5961 bad SYN rate limited");
4315 goto drop;
4316 } else {
4317 /* Send challenge ACK */
4318 tcpstat.tcps_synchallenge++;
4319 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "rfc5961 bad SYN challenge ack");
4320 goto dropafterack;
4321 }
4322 } else {
4323 tp = tcp_drop(tp, ECONNRESET);
4324 rstreason = BANDLIM_UNLIMITED;
4325 postevent(so, 0, EV_RESET);
4326 IF_TCP_STATINC(ifp, synwindow);
4327 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "bad SYN");
4328 goto dropwithreset;
4329 }
4330 }
4331
4332 /*
4333 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
4334 * flag is on (half-synchronized state), then queue data for
4335 * later processing; else drop segment and return.
4336 */
4337 if ((thflags & TH_ACK) == 0) {
4338 if (tp->t_state == TCPS_SYN_RECEIVED ||
4339 (tp->t_flags & TF_NEEDSYN)) {
4340 if ((tfo_enabled(tp))) {
4341 /*
4342 * So, we received a valid segment while in
4343 * SYN-RECEIVED (TF_NEEDSYN is actually never
4344 * set, so this is dead code).
4345 * As this cannot be an RST (see that if a bit
4346 * higher), and it does not have the ACK-flag
4347 * set, we want to retransmit the SYN/ACK.
4348 * Thus, we have to reset snd_nxt to snd_una to
4349 * trigger the going back to sending of the
4350 * SYN/ACK. This is more consistent with the
4351 * behavior of tcp_output(), which expects
4352 * to send the segment that is pointed to by
4353 * snd_nxt.
4354 */
4355 tp->snd_nxt = tp->snd_una;
4356
4357 /*
4358 * We need to make absolutely sure that we are
4359 * going to reply upon a duplicate SYN-segment.
4360 */
4361 if (th->th_flags & TH_SYN) {
4362 needoutput = 1;
4363 }
4364 }
4365
4366 goto step6;
4367 } else if (tp->t_flags & TF_ACKNOW) {
4368 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "bad ACK");
4369 goto dropafterack;
4370 } else {
4371 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "bad ACK");
4372 goto drop;
4373 }
4374 }
4375
4376 /*
4377 * Ack processing.
4378 */
4379
4380 switch (tp->t_state) {
4381 /*
4382 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
4383 * ESTABLISHED state and continue processing.
4384 * The ACK was checked above.
4385 */
4386 case TCPS_SYN_RECEIVED:
4387
4388 tcpstat.tcps_connects++;
4389
4390 /* Do window scaling? */
4391 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
4392 tp->snd_scale = tp->requested_s_scale;
4393 tp->rcv_scale = tp->request_r_scale;
4394 tp->snd_wnd = th->th_win << tp->snd_scale;
4395 tp->max_sndwnd = tp->snd_wnd;
4396 tiwin = tp->snd_wnd;
4397 }
4398 /*
4399 * Make transitions:
4400 * SYN-RECEIVED -> ESTABLISHED
4401 * SYN-RECEIVED* -> FIN-WAIT-1
4402 */
4403 tp->t_starttime = tcp_now;
4404 tcp_sbrcv_tstmp_check(tp);
4405 if (tp->t_flags & TF_NEEDFIN) {
4406 DTRACE_TCP4(state__change, void, NULL,
4407 struct inpcb *, inp,
4408 struct tcpcb *, tp, int32_t, TCPS_FIN_WAIT_1);
4409 tp->t_state = TCPS_FIN_WAIT_1;
4410 tp->t_flags &= ~TF_NEEDFIN;
4411
4412 TCP_LOG_CONNECTION_SUMMARY(tp);
4413 } else {
4414 DTRACE_TCP4(state__change, void, NULL,
4415 struct inpcb *, inp,
4416 struct tcpcb *, tp, int32_t, TCPS_ESTABLISHED);
4417 tp->t_state = TCPS_ESTABLISHED;
4418 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
4419 TCP_CONN_KEEPIDLE(tp));
4420 if (nstat_collect) {
4421 nstat_route_connect_success(
4422 tp->t_inpcb->inp_route.ro_rt);
4423 }
4424 /*
4425 * The SYN is acknowledged but una is not updated
4426 * yet. So pass the value of ack to compute
4427 * sndbytes correctly
4428 */
4429 inp_count_sndbytes(inp, th->th_ack);
4430 }
4431 /*
4432 * If segment contains data or ACK, will call tcp_reass()
4433 * later; if not, do so now to pass queued data to user.
4434 */
4435 if (tlen == 0 && (thflags & TH_FIN) == 0) {
4436 if (isipv6) {
4437 memcpy(&saved_hdr, ip6, sizeof(struct ip6_hdr));
4438 ip6 = (struct ip6_hdr *)&saved_hdr[0];
4439 } else {
4440 memcpy(&saved_hdr, ip, ip->ip_hl << 2);
4441 ip = (struct ip *)&saved_hdr[0];
4442 }
4443 memcpy(&saved_tcphdr, th, sizeof(struct tcphdr));
4444 (void) tcp_reass(tp, (struct tcphdr *)0, &tlen,
4445 NULL, ifp, &read_wakeup);
4446 th = &saved_tcphdr;
4447 }
4448 tp->snd_wl1 = th->th_seq - 1;
4449
4450 #if MPTCP
4451 /*
4452 * Do not send the connect notification for additional subflows
4453 * until ACK for 3-way handshake arrives.
4454 */
4455 if ((!(tp->t_mpflags & TMPF_MPTCP_TRUE)) &&
4456 (tp->t_mpflags & TMPF_SENT_JOIN)) {
4457 isconnected = FALSE;
4458 } else
4459 #endif /* MPTCP */
4460 isconnected = TRUE;
4461 if ((tp->t_tfo_flags & TFO_F_COOKIE_VALID)) {
4462 /* Done this when receiving the SYN */
4463 isconnected = FALSE;
4464
4465 OSDecrementAtomic(&tcp_tfo_halfcnt);
4466
4467 /* Panic if something has gone terribly wrong. */
4468 VERIFY(tcp_tfo_halfcnt >= 0);
4469
4470 tp->t_tfo_flags &= ~TFO_F_COOKIE_VALID;
4471 }
4472
4473 /*
4474 * In case there is data in the send-queue (e.g., TFO is being
4475 * used, or connectx+data has been done), then if we would
4476 * "FALLTHROUGH", we would handle this ACK as if data has been
4477 * acknowledged. But, we have to prevent this. And this
4478 * can be prevented by increasing snd_una by 1, so that the
4479 * SYN is not considered as data (snd_una++ is actually also
4480 * done in SYN_SENT-state as part of the regular TCP stack).
4481 *
4482 * In case there is data on this ack as well, the data will be
4483 * handled by the label "dodata" right after step6.
4484 */
4485 if (so->so_snd.sb_cc) {
4486 tp->snd_una++; /* SYN is acked */
4487 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
4488 tp->snd_nxt = tp->snd_una;
4489 }
4490
4491 /*
4492 * No duplicate-ACK handling is needed. So, we
4493 * directly advance to processing the ACK (aka,
4494 * updating the RTT estimation,...)
4495 *
4496 * But, we first need to handle eventual SACKs,
4497 * because TFO will start sending data with the
4498 * SYN/ACK, so it might be that the client
4499 * includes a SACK with its ACK.
4500 */
4501 if (SACK_ENABLED(tp) &&
4502 (to.to_nsacks > 0 ||
4503 !TAILQ_EMPTY(&tp->snd_holes))) {
4504 tcp_sack_doack(tp, &to, th,
4505 &sack_bytes_acked);
4506 }
4507
4508 goto process_ACK;
4509 }
4510
4511 /* FALLTHROUGH */
4512
4513 /*
4514 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4515 * ACKs. If the ack is in the range
4516 * tp->snd_una < th->th_ack <= tp->snd_max
4517 * then advance tp->snd_una to th->th_ack and drop
4518 * data from the retransmission queue. If this ACK reflects
4519 * more up to date window information we update our window information.
4520 */
4521 case TCPS_ESTABLISHED:
4522 case TCPS_FIN_WAIT_1:
4523 case TCPS_FIN_WAIT_2:
4524 case TCPS_CLOSE_WAIT:
4525 case TCPS_CLOSING:
4526 case TCPS_LAST_ACK:
4527 case TCPS_TIME_WAIT:
4528 if (SEQ_GT(th->th_ack, tp->snd_max)) {
4529 tcpstat.tcps_rcvacktoomuch++;
4530 if (tcp_do_rfc5961 && tcp_is_ack_ratelimited(tp)) {
4531 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "rfc5961 rcvacktoomuch");
4532 goto drop;
4533 } else {
4534 goto dropafterack;
4535 }
4536 }
4537 if (tcp_do_rfc5961 && SEQ_LT(th->th_ack, tp->snd_una - tp->max_sndwnd)) {
4538 if (tcp_is_ack_ratelimited(tp)) {
4539 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "rfc5961 bad ACK");
4540 goto drop;
4541 } else {
4542 goto dropafterack;
4543 }
4544 }
4545 if (SACK_ENABLED(tp) && to.to_nsacks > 0) {
4546 recvd_dsack = tcp_sack_process_dsack(tp, &to, th);
4547 /*
4548 * If DSACK is received and this packet has no
4549 * other SACK information, it can be dropped.
4550 * We do not want to treat it as a duplicate ack.
4551 */
4552 if (recvd_dsack &&
4553 SEQ_LEQ(th->th_ack, tp->snd_una) &&
4554 to.to_nsacks == 0) {
4555 tcp_bad_rexmt_check(tp, th, &to);
4556 goto drop;
4557 }
4558 }
4559
4560 if (SACK_ENABLED(tp) &&
4561 (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes))) {
4562 tcp_sack_doack(tp, &to, th, &sack_bytes_acked);
4563 }
4564
4565 #if MPTCP
4566 if (tp->t_mpuna && SEQ_GEQ(th->th_ack, tp->t_mpuna)) {
4567 if (tp->t_mpflags & TMPF_PREESTABLISHED) {
4568 /* MP TCP establishment succeeded */
4569 tp->t_mpuna = 0;
4570 if (tp->t_mpflags & TMPF_JOINED_FLOW) {
4571 if (tp->t_mpflags & TMPF_SENT_JOIN) {
4572 tp->t_mpflags &=
4573 ~TMPF_PREESTABLISHED;
4574 tp->t_mpflags |=
4575 TMPF_MPTCP_TRUE;
4576
4577 tp->t_timer[TCPT_JACK_RXMT] = 0;
4578 tp->t_mprxtshift = 0;
4579 isconnected = TRUE;
4580 } else {
4581 isconnected = FALSE;
4582 }
4583 } else {
4584 isconnected = TRUE;
4585 }
4586 }
4587 }
4588 #endif /* MPTCP */
4589
4590 tcp_tfo_rcv_ack(tp, th);
4591
4592 /*
4593 * If we have outstanding data (other than
4594 * a window probe), this is a completely
4595 * duplicate ack and the ack is the biggest we've seen.
4596 *
4597 * Need to accommodate a change in window on duplicate acks
4598 * to allow operating systems that update window during
4599 * recovery with SACK
4600 */
4601 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
4602 if (tlen == 0 && (tiwin == tp->snd_wnd ||
4603 (to.to_nsacks > 0 && sack_bytes_acked > 0))) {
4604 /*
4605 * If both ends send FIN at the same time,
4606 * then the ack will be a duplicate ack
4607 * but we have to process the FIN. Check
4608 * for this condition and process the FIN
4609 * instead of the dupack
4610 */
4611 if ((thflags & TH_FIN) &&
4612 !TCPS_HAVERCVDFIN(tp->t_state)) {
4613 break;
4614 }
4615 process_dupack:
4616 #if MPTCP
4617 /*
4618 * MPTCP options that are ignored must
4619 * not be treated as duplicate ACKs.
4620 */
4621 if (to.to_flags & TOF_MPTCP) {
4622 goto drop;
4623 }
4624
4625 if ((isconnected) && (tp->t_mpflags & TMPF_JOINED_FLOW)) {
4626 mptcplog((LOG_DEBUG, "MPTCP "
4627 "Sockets: bypass ack recovery\n"),
4628 MPTCP_SOCKET_DBG,
4629 MPTCP_LOGLVL_VERBOSE);
4630 break;
4631 }
4632 #endif /* MPTCP */
4633 /*
4634 * If a duplicate acknowledgement was seen
4635 * after ECN, it indicates packet loss in
4636 * addition to ECN. Reset INRECOVERY flag
4637 * so that we can process partial acks
4638 * correctly
4639 */
4640 if (tp->ecn_flags & TE_INRECOVERY) {
4641 tp->ecn_flags &= ~TE_INRECOVERY;
4642 }
4643
4644 tcpstat.tcps_rcvdupack++;
4645 ++tp->t_dupacks;
4646
4647 /*
4648 * Check if we need to reset the limit on
4649 * early retransmit
4650 */
4651 if (tp->t_early_rexmt_count > 0 &&
4652 TSTMP_GEQ(tcp_now,
4653 (tp->t_early_rexmt_win +
4654 TCP_EARLY_REXMT_WIN))) {
4655 tp->t_early_rexmt_count = 0;
4656 }
4657
4658 /*
4659 * Is early retransmit needed? We check for
4660 * this when the connection is waiting for
4661 * duplicate acks to enter fast recovery.
4662 */
4663 if (!IN_FASTRECOVERY(tp)) {
4664 tcp_early_rexmt_check(tp, th);
4665 }
4666
4667 /*
4668 * If we've seen exactly rexmt threshold
4669 * of duplicate acks, assume a packet
4670 * has been dropped and retransmit it.
4671 * Kludge snd_nxt & the congestion
4672 * window so we send only this one
4673 * packet.
4674 *
4675 * We know we're losing at the current
4676 * window size so do congestion avoidance
4677 * (set ssthresh to half the current window
4678 * and pull our congestion window back to
4679 * the new ssthresh).
4680 *
4681 * Dup acks mean that packets have left the
4682 * network (they're now cached at the receiver)
4683 * so bump cwnd by the amount in the receiver
4684 * to keep a constant cwnd packets in the
4685 * network.
4686 */
4687 if (tp->t_timer[TCPT_REXMT] == 0 ||
4688 (th->th_ack != tp->snd_una
4689 && sack_bytes_acked == 0)) {
4690 tp->t_dupacks = 0;
4691 tp->t_rexmtthresh = tcprexmtthresh;
4692 } else if (tp->t_dupacks > tp->t_rexmtthresh ||
4693 IN_FASTRECOVERY(tp)) {
4694 /*
4695 * If this connection was seeing packet
4696 * reordering, then recovery might be
4697 * delayed to disambiguate between
4698 * reordering and loss
4699 */
4700 if (SACK_ENABLED(tp) && !IN_FASTRECOVERY(tp) &&
4701 (tp->t_flagsext &
4702 (TF_PKTS_REORDERED | TF_DELAY_RECOVERY)) ==
4703 (TF_PKTS_REORDERED | TF_DELAY_RECOVERY)) {
4704 /*
4705 * Since the SACK information is already
4706 * updated, this ACK will be dropped
4707 */
4708 break;
4709 }
4710
4711 if (SACK_ENABLED(tp)
4712 && IN_FASTRECOVERY(tp)) {
4713 int awnd;
4714
4715 /*
4716 * Compute the amount of data in flight first.
4717 * We can inject new data into the pipe iff
4718 * we have less than 1/2 the original window's
4719 * worth of data in flight.
4720 */
4721 awnd = (tp->snd_nxt - tp->snd_fack) +
4722 tp->sackhint.sack_bytes_rexmit;
4723 if (awnd < tp->snd_ssthresh) {
4724 tp->snd_cwnd += tp->t_maxseg;
4725 if (tp->snd_cwnd > tp->snd_ssthresh) {
4726 tp->snd_cwnd = tp->snd_ssthresh;
4727 }
4728 }
4729 } else {
4730 tp->snd_cwnd += tp->t_maxseg;
4731 }
4732
4733 /* Process any window updates */
4734 if (tiwin > tp->snd_wnd) {
4735 tcp_update_window(tp, thflags,
4736 th, tiwin, tlen);
4737 }
4738 tcp_ccdbg_trace(tp, th,
4739 TCP_CC_IN_FASTRECOVERY);
4740
4741 (void) tcp_output(tp);
4742
4743 goto drop;
4744 } else if (tp->t_dupacks == tp->t_rexmtthresh) {
4745 tcp_seq onxt = tp->snd_nxt;
4746
4747 /*
4748 * If we're doing sack, check to
4749 * see if we're already in sack
4750 * recovery. If we're not doing sack,
4751 * check to see if we're in newreno
4752 * recovery.
4753 */
4754 if (SACK_ENABLED(tp)) {
4755 if (IN_FASTRECOVERY(tp)) {
4756 tp->t_dupacks = 0;
4757 break;
4758 } else if (tp->t_flagsext & TF_DELAY_RECOVERY) {
4759 break;
4760 }
4761 } else {
4762 if (SEQ_LEQ(th->th_ack,
4763 tp->snd_recover)) {
4764 tp->t_dupacks = 0;
4765 break;
4766 }
4767 }
4768 if (tp->t_flags & TF_SENTFIN) {
4769 tp->snd_recover = tp->snd_max - 1;
4770 } else {
4771 tp->snd_recover = tp->snd_max;
4772 }
4773 tp->t_timer[TCPT_PTO] = 0;
4774 tp->t_rtttime = 0;
4775
4776 /*
4777 * If the connection has seen pkt
4778 * reordering, delay recovery until
4779 * it is clear that the packet
4780 * was lost.
4781 */
4782 if (SACK_ENABLED(tp) &&
4783 (tp->t_flagsext &
4784 (TF_PKTS_REORDERED | TF_DELAY_RECOVERY))
4785 == TF_PKTS_REORDERED &&
4786 !IN_FASTRECOVERY(tp) &&
4787 tp->t_reorderwin > 0 &&
4788 (tp->t_state == TCPS_ESTABLISHED ||
4789 tp->t_state == TCPS_FIN_WAIT_1)) {
4790 tp->t_timer[TCPT_DELAYFR] =
4791 OFFSET_FROM_START(tp,
4792 tp->t_reorderwin);
4793 tp->t_flagsext |= TF_DELAY_RECOVERY;
4794 tcpstat.tcps_delay_recovery++;
4795 tcp_ccdbg_trace(tp, th,
4796 TCP_CC_DELAY_FASTRECOVERY);
4797 break;
4798 }
4799
4800 tcp_rexmt_save_state(tp);
4801 /*
4802 * If the current tcp cc module has
4803 * defined a hook for tasks to run
4804 * before entering FR, call it
4805 */
4806 if (CC_ALGO(tp)->pre_fr != NULL) {
4807 CC_ALGO(tp)->pre_fr(tp);
4808 }
4809 ENTER_FASTRECOVERY(tp);
4810 tp->t_timer[TCPT_REXMT] = 0;
4811 if (TCP_ECN_ENABLED(tp)) {
4812 tp->ecn_flags |= TE_SENDCWR;
4813 }
4814
4815 if (SACK_ENABLED(tp)) {
4816 tcpstat.tcps_sack_recovery_episode++;
4817 tp->t_sack_recovery_episode++;
4818 tp->sack_newdata = tp->snd_nxt;
4819 tp->snd_cwnd = tp->t_maxseg;
4820 tp->t_flagsext &=
4821 ~TF_CWND_NONVALIDATED;
4822
4823 /* Process any window updates */
4824 if (tiwin > tp->snd_wnd) {
4825 tcp_update_window(
4826 tp, thflags,
4827 th, tiwin, tlen);
4828 }
4829
4830 tcp_ccdbg_trace(tp, th,
4831 TCP_CC_ENTER_FASTRECOVERY);
4832 (void) tcp_output(tp);
4833 goto drop;
4834 }
4835 tp->snd_nxt = th->th_ack;
4836 tp->snd_cwnd = tp->t_maxseg;
4837
4838 /* Process any window updates */
4839 if (tiwin > tp->snd_wnd) {
4840 tcp_update_window(tp,
4841 thflags,
4842 th, tiwin, tlen);
4843 }
4844
4845 (void) tcp_output(tp);
4846 if (tp->t_flagsext & TF_CWND_NONVALIDATED) {
4847 tcp_cc_adjust_nonvalidated_cwnd(tp);
4848 } else {
4849 tp->snd_cwnd = tp->snd_ssthresh +
4850 tp->t_maxseg * tp->t_dupacks;
4851 }
4852 if (SEQ_GT(onxt, tp->snd_nxt)) {
4853 tp->snd_nxt = onxt;
4854 }
4855
4856 tcp_ccdbg_trace(tp, th,
4857 TCP_CC_ENTER_FASTRECOVERY);
4858 goto drop;
4859 } else if (limited_txmt &&
4860 ALLOW_LIMITED_TRANSMIT(tp) &&
4861 (!(SACK_ENABLED(tp)) || sack_bytes_acked > 0) &&
4862 (so->so_snd.sb_cc - (tp->snd_max - tp->snd_una)) > 0) {
4863 u_int32_t incr = (tp->t_maxseg * tp->t_dupacks);
4864
4865 /* Use Limited Transmit algorithm on the first two
4866 * duplicate acks when there is new data to transmit
4867 */
4868 tp->snd_cwnd += incr;
4869 tcpstat.tcps_limited_txt++;
4870 (void) tcp_output(tp);
4871
4872 tcp_ccdbg_trace(tp, th, TCP_CC_LIMITED_TRANSMIT);
4873
4874 /* Reset snd_cwnd back to normal */
4875 tp->snd_cwnd -= incr;
4876 }
4877 }
4878 break;
4879 }
4880 /*
4881 * If the congestion window was inflated to account
4882 * for the other side's cached packets, retract it.
4883 */
4884 if (IN_FASTRECOVERY(tp)) {
4885 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
4886 /*
4887 * If we received an ECE and entered
4888 * recovery, the subsequent ACKs should
4889 * not be treated as partial acks.
4890 */
4891 if (tp->ecn_flags & TE_INRECOVERY) {
4892 goto process_ACK;
4893 }
4894
4895 if (SACK_ENABLED(tp)) {
4896 tcp_sack_partialack(tp, th);
4897 } else {
4898 tcp_newreno_partial_ack(tp, th);
4899 }
4900 tcp_ccdbg_trace(tp, th, TCP_CC_PARTIAL_ACK);
4901 } else {
4902 EXIT_FASTRECOVERY(tp);
4903 if (CC_ALGO(tp)->post_fr != NULL) {
4904 CC_ALGO(tp)->post_fr(tp, th);
4905 }
4906 tp->t_pipeack = 0;
4907 tcp_clear_pipeack_state(tp);
4908 tcp_ccdbg_trace(tp, th,
4909 TCP_CC_EXIT_FASTRECOVERY);
4910 }
4911 } else if ((tp->t_flagsext &
4912 (TF_PKTS_REORDERED | TF_DELAY_RECOVERY))
4913 == (TF_PKTS_REORDERED | TF_DELAY_RECOVERY)) {
4914 /*
4915 * If the ack acknowledges upto snd_recover or if
4916 * it acknowledges all the snd holes, exit
4917 * recovery and cancel the timer. Otherwise,
4918 * this is a partial ack. Wait for recovery timer
4919 * to enter recovery. The snd_holes have already
4920 * been updated.
4921 */
4922 if (SEQ_GEQ(th->th_ack, tp->snd_recover) ||
4923 TAILQ_EMPTY(&tp->snd_holes)) {
4924 tp->t_timer[TCPT_DELAYFR] = 0;
4925 tp->t_flagsext &= ~TF_DELAY_RECOVERY;
4926 EXIT_FASTRECOVERY(tp);
4927 tcp_ccdbg_trace(tp, th,
4928 TCP_CC_EXIT_FASTRECOVERY);
4929 }
4930 } else {
4931 /*
4932 * We were not in fast recovery. Reset the
4933 * duplicate ack counter.
4934 */
4935 tp->t_dupacks = 0;
4936 tp->t_rexmtthresh = tcprexmtthresh;
4937 }
4938
4939
4940 /*
4941 * If we reach this point, ACK is not a duplicate,
4942 * i.e., it ACKs something we sent.
4943 */
4944 if (tp->t_flags & TF_NEEDSYN) {
4945 /*
4946 * T/TCP: Connection was half-synchronized, and our
4947 * SYN has been ACK'd (so connection is now fully
4948 * synchronized). Go to non-starred state,
4949 * increment snd_una for ACK of SYN, and check if
4950 * we can do window scaling.
4951 */
4952 tp->t_flags &= ~TF_NEEDSYN;
4953 tp->snd_una++;
4954 /* Do window scaling? */
4955 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
4956 tp->snd_scale = tp->requested_s_scale;
4957 tp->rcv_scale = tp->request_r_scale;
4958 }
4959 }
4960
4961 process_ACK:
4962 VERIFY(SEQ_GEQ(th->th_ack, tp->snd_una));
4963 acked = BYTES_ACKED(th, tp);
4964 tcpstat.tcps_rcvackpack++;
4965 tcpstat.tcps_rcvackbyte += acked;
4966
4967 /*
4968 * If the last packet was a retransmit, make sure
4969 * it was not spurious.
4970 *
4971 * This will also take care of congestion window
4972 * adjustment if a last packet was recovered due to a
4973 * tail loss probe.
4974 */
4975 tcp_bad_rexmt_check(tp, th, &to);
4976
4977 /* Recalculate the RTT */
4978 tcp_compute_rtt(tp, &to, th);
4979
4980 /*
4981 * If all outstanding data is acked, stop retransmit
4982 * timer and remember to restart (more output or persist).
4983 * If there is more data to be acked, restart retransmit
4984 * timer, using current (possibly backed-off) value.
4985 */
4986 TCP_RESET_REXMT_STATE(tp);
4987 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
4988 tp->t_rttmin, TCPTV_REXMTMAX,
4989 TCP_ADD_REXMTSLOP(tp));
4990 if (th->th_ack == tp->snd_max) {
4991 tp->t_timer[TCPT_REXMT] = 0;
4992 tp->t_timer[TCPT_PTO] = 0;
4993 needoutput = 1;
4994 } else if (tp->t_timer[TCPT_PERSIST] == 0) {
4995 tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp,
4996 tp->t_rxtcur);
4997 }
4998
4999 if ((prev_t_state == TCPS_SYN_SENT ||
5000 prev_t_state == TCPS_SYN_RECEIVED) &&
5001 tp->t_state == TCPS_ESTABLISHED) {
5002 TCP_LOG_RTT_INFO(tp);
5003 }
5004
5005 /*
5006 * If no data (only SYN) was ACK'd, skip rest of ACK
5007 * processing.
5008 */
5009 if (acked == 0) {
5010 goto step6;
5011 }
5012
5013 /*
5014 * When outgoing data has been acked (except the SYN+data), we
5015 * mark this connection as "sending good" for TFO.
5016 */
5017 if ((tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) &&
5018 !(tp->t_tfo_flags & TFO_F_NO_SNDPROBING) &&
5019 !(th->th_flags & TH_SYN)) {
5020 tp->t_tfo_flags |= TFO_F_NO_SNDPROBING;
5021 }
5022
5023 /*
5024 * If TH_ECE is received, make sure that ECN is enabled
5025 * on that connection and we have sent ECT on data packets.
5026 */
5027 if ((thflags & TH_ECE) != 0 && TCP_ECN_ENABLED(tp) &&
5028 (tp->ecn_flags & TE_SENDIPECT)) {
5029 /*
5030 * Reduce the congestion window if we haven't
5031 * done so.
5032 */
5033 if (!IN_FASTRECOVERY(tp)) {
5034 tcp_reduce_congestion_window(tp);
5035 tp->ecn_flags |= (TE_INRECOVERY | TE_SENDCWR);
5036 /*
5037 * Also note that the connection received
5038 * ECE atleast once
5039 */
5040 tp->ecn_flags |= TE_RECV_ECN_ECE;
5041 INP_INC_IFNET_STAT(inp, ecn_recv_ece);
5042 tcpstat.tcps_ecn_recv_ece++;
5043 tcp_ccdbg_trace(tp, th, TCP_CC_ECN_RCVD);
5044 }
5045 }
5046
5047 /*
5048 * When new data is acked, open the congestion window.
5049 * The specifics of how this is achieved are up to the
5050 * congestion control algorithm in use for this connection.
5051 *
5052 * The calculations in this function assume that snd_una is
5053 * not updated yet.
5054 */
5055 if (!IN_FASTRECOVERY(tp)) {
5056 if (CC_ALGO(tp)->ack_rcvd != NULL) {
5057 CC_ALGO(tp)->ack_rcvd(tp, th);
5058 }
5059 tcp_ccdbg_trace(tp, th, TCP_CC_ACK_RCVD);
5060 }
5061 if (acked > so->so_snd.sb_cc) {
5062 tp->snd_wnd -= so->so_snd.sb_cc;
5063 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
5064 if (so->so_flags & SOF_ENABLE_MSGS) {
5065 so->so_msg_state->msg_serial_bytes -=
5066 (int)so->so_snd.sb_cc;
5067 }
5068 ourfinisacked = 1;
5069 } else {
5070 sbdrop(&so->so_snd, acked);
5071 if (so->so_flags & SOF_ENABLE_MSGS) {
5072 so->so_msg_state->msg_serial_bytes -=
5073 acked;
5074 }
5075 tcp_sbsnd_trim(&so->so_snd);
5076 tp->snd_wnd -= acked;
5077 ourfinisacked = 0;
5078 }
5079 /* detect una wraparound */
5080 if (!IN_FASTRECOVERY(tp) &&
5081 SEQ_GT(tp->snd_una, tp->snd_recover) &&
5082 SEQ_LEQ(th->th_ack, tp->snd_recover)) {
5083 tp->snd_recover = th->th_ack - 1;
5084 }
5085
5086 if (IN_FASTRECOVERY(tp) &&
5087 SEQ_GEQ(th->th_ack, tp->snd_recover)) {
5088 EXIT_FASTRECOVERY(tp);
5089 }
5090
5091 tp->snd_una = th->th_ack;
5092
5093 if (SACK_ENABLED(tp)) {
5094 if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
5095 tp->snd_recover = tp->snd_una;
5096 }
5097 }
5098 if (SEQ_LT(tp->snd_nxt, tp->snd_una)) {
5099 tp->snd_nxt = tp->snd_una;
5100 }
5101 if (!SLIST_EMPTY(&tp->t_rxt_segments) &&
5102 !TCP_DSACK_SEQ_IN_WINDOW(tp, tp->t_dsack_lastuna,
5103 tp->snd_una)) {
5104 tcp_rxtseg_clean(tp);
5105 }
5106 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
5107 tp->t_bwmeas != NULL) {
5108 tcp_bwmeas_check(tp);
5109 }
5110
5111 write_wakeup = 1;
5112
5113 if (!SLIST_EMPTY(&tp->t_notify_ack)) {
5114 tcp_notify_acknowledgement(tp, so);
5115 }
5116
5117 switch (tp->t_state) {
5118 /*
5119 * In FIN_WAIT_1 STATE in addition to the processing
5120 * for the ESTABLISHED state if our FIN is now acknowledged
5121 * then enter FIN_WAIT_2.
5122 */
5123 case TCPS_FIN_WAIT_1:
5124 if (ourfinisacked) {
5125 /*
5126 * If we can't receive any more
5127 * data, then closing user can proceed.
5128 * Starting the TCPT_2MSL timer is contrary to the
5129 * specification, but if we don't get a FIN
5130 * we'll hang forever.
5131 */
5132 if (so->so_state & SS_CANTRCVMORE) {
5133 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
5134 TCP_CONN_MAXIDLE(tp));
5135 isconnected = FALSE;
5136 isdisconnected = TRUE;
5137 }
5138 DTRACE_TCP4(state__change, void, NULL,
5139 struct inpcb *, inp,
5140 struct tcpcb *, tp,
5141 int32_t, TCPS_FIN_WAIT_2);
5142 tp->t_state = TCPS_FIN_WAIT_2;
5143 /* fall through and make sure we also recognize
5144 * data ACKed with the FIN
5145 */
5146 }
5147 break;
5148
5149 /*
5150 * In CLOSING STATE in addition to the processing for
5151 * the ESTABLISHED state if the ACK acknowledges our FIN
5152 * then enter the TIME-WAIT state, otherwise ignore
5153 * the segment.
5154 */
5155 case TCPS_CLOSING:
5156 if (ourfinisacked) {
5157 DTRACE_TCP4(state__change, void, NULL,
5158 struct inpcb *, inp,
5159 struct tcpcb *, tp,
5160 int32_t, TCPS_TIME_WAIT);
5161 tp->t_state = TCPS_TIME_WAIT;
5162 tcp_canceltimers(tp);
5163 if (tp->t_flagsext & TF_NOTIMEWAIT) {
5164 tp->t_flags |= TF_CLOSING;
5165 } else {
5166 add_to_time_wait(tp, 2 * tcp_msl);
5167 }
5168 isconnected = FALSE;
5169 isdisconnected = TRUE;
5170 }
5171 break;
5172
5173 /*
5174 * In LAST_ACK, we may still be waiting for data to drain
5175 * and/or to be acked, as well as for the ack of our FIN.
5176 * If our FIN is now acknowledged, delete the TCB,
5177 * enter the closed state and return.
5178 */
5179 case TCPS_LAST_ACK:
5180 if (ourfinisacked) {
5181 tp = tcp_close(tp);
5182 goto drop;
5183 }
5184 break;
5185
5186 /*
5187 * In TIME_WAIT state the only thing that should arrive
5188 * is a retransmission of the remote FIN. Acknowledge
5189 * it and restart the finack timer.
5190 */
5191 case TCPS_TIME_WAIT:
5192 add_to_time_wait(tp, 2 * tcp_msl);
5193 goto dropafterack;
5194 }
5195
5196 /*
5197 * If there is a SACK option on the ACK and we
5198 * haven't seen any duplicate acks before, count
5199 * it as a duplicate ack even if the cumulative
5200 * ack is advanced. If the receiver delayed an
5201 * ack and detected loss afterwards, then the ack
5202 * will advance cumulative ack and will also have
5203 * a SACK option. So counting it as one duplicate
5204 * ack is ok.
5205 */
5206 if (sack_ackadv == 1 &&
5207 tp->t_state == TCPS_ESTABLISHED &&
5208 SACK_ENABLED(tp) && sack_bytes_acked > 0 &&
5209 to.to_nsacks > 0 && tp->t_dupacks == 0 &&
5210 SEQ_LEQ(th->th_ack, tp->snd_una) && tlen == 0 &&
5211 !(tp->t_flagsext & TF_PKTS_REORDERED)) {
5212 tcpstat.tcps_sack_ackadv++;
5213 goto process_dupack;
5214 }
5215 }
5216
5217 step6:
5218 /*
5219 * Update window information.
5220 */
5221 if (tcp_update_window(tp, thflags, th, tiwin, tlen)) {
5222 needoutput = 1;
5223 }
5224
5225 /*
5226 * Process segments with URG.
5227 */
5228 if ((thflags & TH_URG) && th->th_urp &&
5229 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5230 /*
5231 * This is a kludge, but if we receive and accept
5232 * random urgent pointers, we'll crash in
5233 * soreceive. It's hard to imagine someone
5234 * actually wanting to send this much urgent data.
5235 */
5236 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
5237 th->th_urp = 0; /* XXX */
5238 thflags &= ~TH_URG; /* XXX */
5239 goto dodata; /* XXX */
5240 }
5241 /*
5242 * If this segment advances the known urgent pointer,
5243 * then mark the data stream. This should not happen
5244 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5245 * a FIN has been received from the remote side.
5246 * In these states we ignore the URG.
5247 *
5248 * According to RFC961 (Assigned Protocols),
5249 * the urgent pointer points to the last octet
5250 * of urgent data. We continue, however,
5251 * to consider it to indicate the first octet
5252 * of data past the urgent section as the original
5253 * spec states (in one of two places).
5254 */
5255 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) {
5256 tp->rcv_up = th->th_seq + th->th_urp;
5257 so->so_oobmark = so->so_rcv.sb_cc +
5258 (tp->rcv_up - tp->rcv_nxt) - 1;
5259 if (so->so_oobmark == 0) {
5260 so->so_state |= SS_RCVATMARK;
5261 postevent(so, 0, EV_OOB);
5262 }
5263 sohasoutofband(so);
5264 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
5265 }
5266 /*
5267 * Remove out of band data so doesn't get presented to user.
5268 * This can happen independent of advancing the URG pointer,
5269 * but if two URG's are pending at once, some out-of-band
5270 * data may creep in... ick.
5271 */
5272 if (th->th_urp <= (u_int32_t)tlen
5273 #if SO_OOBINLINE
5274 && (so->so_options & SO_OOBINLINE) == 0
5275 #endif
5276 ) {
5277 tcp_pulloutofband(so, th, m,
5278 drop_hdrlen); /* hdr drop is delayed */
5279 }
5280 } else {
5281 /*
5282 * If no out of band data is expected,
5283 * pull receive urgent pointer along
5284 * with the receive window.
5285 */
5286 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) {
5287 tp->rcv_up = tp->rcv_nxt;
5288 }
5289 }
5290 dodata:
5291
5292 /* Set socket's connect or disconnect state correcly before doing data.
5293 * The following might unlock the socket if there is an upcall or a socket
5294 * filter.
5295 */
5296 if (isconnected) {
5297 soisconnected(so);
5298 } else if (isdisconnected) {
5299 soisdisconnected(so);
5300 }
5301
5302 /* Let's check the state of pcb just to make sure that it did not get closed
5303 * when we unlocked above
5304 */
5305 if (inp->inp_state == INPCB_STATE_DEAD) {
5306 /* Just drop the packet that we are processing and return */
5307 TCP_LOG_DROP_PCB(TCP_LOG_HDR, th, tp, false, "INPCB_STATE_DEAD");
5308 goto drop;
5309 }
5310
5311 /*
5312 * Process the segment text, merging it into the TCP sequencing queue,
5313 * and arranging for acknowledgment of receipt if necessary.
5314 * This process logically involves adjusting tp->rcv_wnd as data
5315 * is presented to the user (this happens in tcp_usrreq.c,
5316 * case PRU_RCVD). If a FIN has already been received on this
5317 * connection then we just ignore the text.
5318 *
5319 * If we are in SYN-received state and got a valid TFO cookie, we want
5320 * to process the data.
5321 */
5322 if ((tlen || (thflags & TH_FIN)) &&
5323 TCPS_HAVERCVDFIN(tp->t_state) == 0 &&
5324 (TCPS_HAVEESTABLISHED(tp->t_state) ||
5325 (tp->t_state == TCPS_SYN_RECEIVED &&
5326 (tp->t_tfo_flags & TFO_F_COOKIE_VALID)))) {
5327 tcp_seq save_start = th->th_seq;
5328 tcp_seq save_end = th->th_seq + tlen;
5329 m_adj(m, drop_hdrlen); /* delayed header drop */
5330 /*
5331 * Insert segment which includes th into TCP reassembly queue
5332 * with control block tp. Set thflags to whether reassembly now
5333 * includes a segment with FIN. This handles the common case
5334 * inline (segment is the next to be received on an established
5335 * connection, and the queue is empty), avoiding linkage into
5336 * and removal from the queue and repetition of various
5337 * conversions.
5338 * Set DELACK for segments received in order, but ack
5339 * immediately when segments are out of order (so
5340 * fast retransmit can work).
5341 */
5342 if (th->th_seq == tp->rcv_nxt && LIST_EMPTY(&tp->t_segq)) {
5343 TCP_INC_VAR(tp->t_unacksegs, nlropkts);
5344 /*
5345 * Calculate the RTT on the receiver only if the
5346 * connection is in streaming mode and the last
5347 * packet was not an end-of-write
5348 */
5349 if (tp->t_flags & TF_STREAMING_ON) {
5350 tcp_compute_rtt(tp, &to, th);
5351 }
5352
5353 if (DELAY_ACK(tp, th) &&
5354 ((tp->t_flags & TF_ACKNOW) == 0)) {
5355 if ((tp->t_flags & TF_DELACK) == 0) {
5356 tp->t_flags |= TF_DELACK;
5357 tp->t_timer[TCPT_DELACK] =
5358 OFFSET_FROM_START(tp, tcp_delack);
5359 }
5360 } else {
5361 tp->t_flags |= TF_ACKNOW;
5362 }
5363 tp->rcv_nxt += tlen;
5364 thflags = th->th_flags & TH_FIN;
5365 TCP_INC_VAR(tcpstat.tcps_rcvpack, nlropkts);
5366 tcpstat.tcps_rcvbyte += tlen;
5367 if (nstat_collect) {
5368 if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) {
5369 INP_ADD_STAT(inp, cell, wifi, wired,
5370 rxpackets, m->m_pkthdr.lro_npkts);
5371 } else {
5372 INP_ADD_STAT(inp, cell, wifi, wired,
5373 rxpackets, 1);
5374 }
5375 INP_ADD_STAT(inp, cell, wifi, wired,
5376 rxbytes, tlen);
5377 inp_set_activity_bitmap(inp);
5378 }
5379 tcp_sbrcv_grow(tp, &so->so_rcv, &to, tlen,
5380 TCP_AUTORCVBUF_MAX(ifp));
5381 so_recv_data_stat(so, m, drop_hdrlen);
5382
5383 if (isipv6) {
5384 memcpy(&saved_hdr, ip6, sizeof(struct ip6_hdr));
5385 ip6 = (struct ip6_hdr *)&saved_hdr[0];
5386 } else {
5387 memcpy(&saved_hdr, ip, ip->ip_hl << 2);
5388 ip = (struct ip *)&saved_hdr[0];
5389 }
5390 memcpy(&saved_tcphdr, th, sizeof(struct tcphdr));
5391 if (sbappendstream_rcvdemux(so, m,
5392 th->th_seq - (tp->irs + 1), 0)) {
5393 read_wakeup = 1;
5394 }
5395 th = &saved_tcphdr;
5396 } else {
5397 if (isipv6) {
5398 memcpy(&saved_hdr, ip6, sizeof(struct ip6_hdr));
5399 ip6 = (struct ip6_hdr *)&saved_hdr[0];
5400 } else {
5401 memcpy(&saved_hdr, ip, ip->ip_hl << 2);
5402 ip = (struct ip *)&saved_hdr[0];
5403 }
5404 memcpy(&saved_tcphdr, th, sizeof(struct tcphdr));
5405 thflags = tcp_reass(tp, th, &tlen, m, ifp, &read_wakeup);
5406 th = &saved_tcphdr;
5407 tp->t_flags |= TF_ACKNOW;
5408 }
5409
5410 if ((tlen > 0 || (th->th_flags & TH_FIN)) && SACK_ENABLED(tp)) {
5411 if (th->th_flags & TH_FIN) {
5412 save_end++;
5413 }
5414 tcp_update_sack_list(tp, save_start, save_end);
5415 }
5416
5417 tcp_adaptive_rwtimo_check(tp, tlen);
5418
5419 if (tlen > 0) {
5420 tcp_tfo_rcv_data(tp);
5421 }
5422
5423 if (tp->t_flags & TF_DELACK) {
5424 #if INET6
5425 if (isipv6) {
5426 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
5427 (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])),
5428 th->th_seq, th->th_ack, th->th_win);
5429 } else
5430 #endif
5431 {
5432 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
5433 (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)),
5434 th->th_seq, th->th_ack, th->th_win);
5435 }
5436 }
5437 } else {
5438 if ((so->so_flags & SOF_MP_SUBFLOW) && tlen == 0 &&
5439 (m->m_pkthdr.pkt_flags & PKTF_MPTCP_DFIN) &&
5440 (m->m_pkthdr.pkt_flags & PKTF_MPTCP)) {
5441 m_adj(m, drop_hdrlen); /* delayed header drop */
5442 mptcp_input(tptomptp(tp)->mpt_mpte, m);
5443 tp->t_flags |= TF_ACKNOW;
5444 } else {
5445 m_freem(m);
5446 }
5447 thflags &= ~TH_FIN;
5448 }
5449
5450 /*
5451 * If FIN is received ACK the FIN and let the user know
5452 * that the connection is closing.
5453 */
5454 if (thflags & TH_FIN) {
5455 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
5456 socantrcvmore(so);
5457 postevent(so, 0, EV_FIN);
5458 /*
5459 * If connection is half-synchronized
5460 * (ie NEEDSYN flag on) then delay ACK,
5461 * so it may be piggybacked when SYN is sent.
5462 * Otherwise, since we received a FIN then no
5463 * more input can be expected, send ACK now.
5464 */
5465 TCP_INC_VAR(tp->t_unacksegs, nlropkts);
5466 if (DELAY_ACK(tp, th) && (tp->t_flags & TF_NEEDSYN)) {
5467 if ((tp->t_flags & TF_DELACK) == 0) {
5468 tp->t_flags |= TF_DELACK;
5469 tp->t_timer[TCPT_DELACK] = OFFSET_FROM_START(tp, tcp_delack);
5470 }
5471 } else {
5472 tp->t_flags |= TF_ACKNOW;
5473 }
5474 tp->rcv_nxt++;
5475 }
5476 switch (tp->t_state) {
5477 /*
5478 * In SYN_RECEIVED and ESTABLISHED STATES
5479 * enter the CLOSE_WAIT state.
5480 */
5481 case TCPS_SYN_RECEIVED:
5482 tp->t_starttime = tcp_now;
5483 case TCPS_ESTABLISHED:
5484 DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp,
5485 struct tcpcb *, tp, int32_t, TCPS_CLOSE_WAIT);
5486 tp->t_state = TCPS_CLOSE_WAIT;
5487 break;
5488
5489 /*
5490 * If still in FIN_WAIT_1 STATE FIN has not been acked so
5491 * enter the CLOSING state.
5492 */
5493 case TCPS_FIN_WAIT_1:
5494 DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp,
5495 struct tcpcb *, tp, int32_t, TCPS_CLOSING);
5496 tp->t_state = TCPS_CLOSING;
5497 break;
5498
5499 /*
5500 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5501 * starting the time-wait timer, turning off the other
5502 * standard timers.
5503 */
5504 case TCPS_FIN_WAIT_2:
5505 DTRACE_TCP4(state__change, void, NULL,
5506 struct inpcb *, inp,
5507 struct tcpcb *, tp,
5508 int32_t, TCPS_TIME_WAIT);
5509 tp->t_state = TCPS_TIME_WAIT;
5510 tcp_canceltimers(tp);
5511 tp->t_flags |= TF_ACKNOW;
5512 if (tp->t_flagsext & TF_NOTIMEWAIT) {
5513 tp->t_flags |= TF_CLOSING;
5514 } else {
5515 add_to_time_wait(tp, 2 * tcp_msl);
5516 }
5517 soisdisconnected(so);
5518 break;
5519
5520 /*
5521 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5522 */
5523 case TCPS_TIME_WAIT:
5524 add_to_time_wait(tp, 2 * tcp_msl);
5525 break;
5526 }
5527 }
5528 #if TCPDEBUG
5529 if (so->so_options & SO_DEBUG) {
5530 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
5531 &tcp_savetcp, 0);
5532 }
5533 #endif
5534
5535 if (read_wakeup) {
5536 mptcp_handle_input(so);
5537 }
5538
5539 /*
5540 * Return any desired output.
5541 */
5542 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
5543 (void) tcp_output(tp);
5544 }
5545
5546 tcp_check_timer_state(tp);
5547
5548 tcp_handle_wakeup(so, read_wakeup, write_wakeup);
5549
5550 socket_unlock(so, 1);
5551 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
5552 return;
5553
5554 dropafterack:
5555 /*
5556 * Generate an ACK dropping incoming segment if it occupies
5557 * sequence space, where the ACK reflects our state.
5558 *
5559 * We can now skip the test for the RST flag since all
5560 * paths to this code happen after packets containing
5561 * RST have been dropped.
5562 *
5563 * In the SYN-RECEIVED state, don't send an ACK unless the
5564 * segment we received passes the SYN-RECEIVED ACK test.
5565 * If it fails send a RST. This breaks the loop in the
5566 * "LAND" DoS attack, and also prevents an ACK storm
5567 * between two listening ports that have been sent forged
5568 * SYN segments, each with the source address of the other.
5569 */
5570 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
5571 (SEQ_GT(tp->snd_una, th->th_ack) ||
5572 SEQ_GT(th->th_ack, tp->snd_max))) {
5573 rstreason = BANDLIM_RST_OPENPORT;
5574 IF_TCP_STATINC(ifp, dospacket);
5575 goto dropwithreset;
5576 }
5577 #if TCPDEBUG
5578 if (so->so_options & SO_DEBUG) {
5579 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
5580 &tcp_savetcp, 0);
5581 }
5582 #endif
5583 m_freem(m);
5584 tp->t_flags |= TF_ACKNOW;
5585
5586 (void) tcp_output(tp);
5587
5588 tcp_handle_wakeup(so, read_wakeup, write_wakeup);
5589
5590 /* Don't need to check timer state as we should have done it during tcp_output */
5591 socket_unlock(so, 1);
5592 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
5593 return;
5594 dropwithresetnosock:
5595 nosock = 1;
5596 dropwithreset:
5597 /*
5598 * Generate a RST, dropping incoming segment.
5599 * Make ACK acceptable to originator of segment.
5600 * Don't bother to respond if destination was broadcast/multicast.
5601 */
5602 if ((thflags & TH_RST) || m->m_flags & (M_BCAST | M_MCAST)) {
5603 goto drop;
5604 }
5605 #if INET6
5606 if (isipv6) {
5607 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
5608 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
5609 goto drop;
5610 }
5611 } else
5612 #endif /* INET6 */
5613 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
5614 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
5615 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
5616 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
5617 goto drop;
5618 }
5619 /* IPv6 anycast check is done at tcp6_input() */
5620
5621 /*
5622 * Perform bandwidth limiting.
5623 */
5624 #if ICMP_BANDLIM
5625 if (badport_bandlim(rstreason) < 0) {
5626 goto drop;
5627 }
5628 #endif
5629
5630 #if TCPDEBUG
5631 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) {
5632 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
5633 &tcp_savetcp, 0);
5634 }
5635 #endif
5636 bzero(&tra, sizeof(tra));
5637 tra.ifscope = ifscope;
5638 tra.awdl_unrestricted = 1;
5639 tra.intcoproc_allowed = 1;
5640 if (thflags & TH_ACK) {
5641 /* mtod() below is safe as long as hdr dropping is delayed */
5642 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
5643 TH_RST, &tra);
5644 } else {
5645 if (thflags & TH_SYN) {
5646 tlen++;
5647 }
5648 /* mtod() below is safe as long as hdr dropping is delayed */
5649 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq + tlen,
5650 (tcp_seq)0, TH_RST | TH_ACK, &tra);
5651 }
5652 /* destroy temporarily created socket */
5653 if (dropsocket) {
5654 (void) soabort(so);
5655 socket_unlock(so, 1);
5656 } else if ((inp != NULL) && (nosock == 0)) {
5657 tcp_handle_wakeup(so, read_wakeup, write_wakeup);
5658
5659 socket_unlock(so, 1);
5660 }
5661 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
5662 return;
5663 dropnosock:
5664 nosock = 1;
5665 drop:
5666 /*
5667 * Drop space held by incoming segment and return.
5668 */
5669 #if TCPDEBUG
5670 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) {
5671 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
5672 &tcp_savetcp, 0);
5673 }
5674 #endif
5675 m_freem(m);
5676 /* destroy temporarily created socket */
5677 if (dropsocket) {
5678 (void) soabort(so);
5679 socket_unlock(so, 1);
5680 } else if (nosock == 0) {
5681 tcp_handle_wakeup(so, read_wakeup, write_wakeup);
5682
5683 socket_unlock(so, 1);
5684 }
5685 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
5686 return;
5687 }
5688
5689 /*
5690 * Parse TCP options and place in tcpopt.
5691 */
5692 static void
5693 tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcphdr *th,
5694 struct tcpopt *to)
5695 {
5696 u_short mss = 0;
5697 int opt, optlen;
5698
5699 for (; cnt > 0; cnt -= optlen, cp += optlen) {
5700 opt = cp[0];
5701 if (opt == TCPOPT_EOL) {
5702 break;
5703 }
5704 if (opt == TCPOPT_NOP) {
5705 optlen = 1;
5706 } else {
5707 if (cnt < 2) {
5708 break;
5709 }
5710 optlen = cp[1];
5711 if (optlen < 2 || optlen > cnt) {
5712 break;
5713 }
5714 }
5715 switch (opt) {
5716 default:
5717 continue;
5718
5719 case TCPOPT_MAXSEG:
5720 if (optlen != TCPOLEN_MAXSEG) {
5721 continue;
5722 }
5723 if (!(th->th_flags & TH_SYN)) {
5724 continue;
5725 }
5726 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
5727 NTOHS(mss);
5728 to->to_mss = mss;
5729 to->to_flags |= TOF_MSS;
5730 break;
5731
5732 case TCPOPT_WINDOW:
5733 if (optlen != TCPOLEN_WINDOW) {
5734 continue;
5735 }
5736 if (!(th->th_flags & TH_SYN)) {
5737 continue;
5738 }
5739 to->to_flags |= TOF_SCALE;
5740 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
5741 break;
5742
5743 case TCPOPT_TIMESTAMP:
5744 if (optlen != TCPOLEN_TIMESTAMP) {
5745 continue;
5746 }
5747 to->to_flags |= TOF_TS;
5748 bcopy((char *)cp + 2,
5749 (char *)&to->to_tsval, sizeof(to->to_tsval));
5750 NTOHL(to->to_tsval);
5751 bcopy((char *)cp + 6,
5752 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
5753 NTOHL(to->to_tsecr);
5754 /* Re-enable sending Timestamps if we received them */
5755 if (!(tp->t_flags & TF_REQ_TSTMP) &&
5756 tcp_do_rfc1323 == 1) {
5757 tp->t_flags |= TF_REQ_TSTMP;
5758 }
5759 break;
5760 case TCPOPT_SACK_PERMITTED:
5761 if (!tcp_do_sack ||
5762 optlen != TCPOLEN_SACK_PERMITTED) {
5763 continue;
5764 }
5765 if (th->th_flags & TH_SYN) {
5766 to->to_flags |= TOF_SACK;
5767 }
5768 break;
5769 case TCPOPT_SACK:
5770 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) {
5771 continue;
5772 }
5773 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
5774 to->to_sacks = cp + 2;
5775 tcpstat.tcps_sack_rcv_blocks++;
5776
5777 break;
5778 case TCPOPT_FASTOPEN:
5779 if (optlen == TCPOLEN_FASTOPEN_REQ) {
5780 if (tp->t_state != TCPS_LISTEN) {
5781 continue;
5782 }
5783
5784 to->to_flags |= TOF_TFOREQ;
5785 } else {
5786 if (optlen < TCPOLEN_FASTOPEN_REQ ||
5787 (optlen - TCPOLEN_FASTOPEN_REQ) > TFO_COOKIE_LEN_MAX ||
5788 (optlen - TCPOLEN_FASTOPEN_REQ) < TFO_COOKIE_LEN_MIN) {
5789 continue;
5790 }
5791 if (tp->t_state != TCPS_LISTEN &&
5792 tp->t_state != TCPS_SYN_SENT) {
5793 continue;
5794 }
5795
5796 to->to_flags |= TOF_TFO;
5797 to->to_tfo = cp + 1;
5798 }
5799
5800 break;
5801 #if MPTCP
5802 case TCPOPT_MULTIPATH:
5803 tcp_do_mptcp_options(tp, cp, th, to, optlen);
5804 break;
5805 #endif /* MPTCP */
5806 }
5807 }
5808 }
5809
5810 static void
5811 tcp_finalize_options(struct tcpcb *tp, struct tcpopt *to, unsigned int ifscope)
5812 {
5813 if (to->to_flags & TOF_TS) {
5814 tp->t_flags |= TF_RCVD_TSTMP;
5815 tp->ts_recent = to->to_tsval;
5816 tp->ts_recent_age = tcp_now;
5817 }
5818 if (to->to_flags & TOF_MSS) {
5819 tcp_mss(tp, to->to_mss, ifscope);
5820 }
5821 if (SACK_ENABLED(tp)) {
5822 if (!(to->to_flags & TOF_SACK)) {
5823 tp->t_flagsext &= ~(TF_SACK_ENABLE);
5824 } else {
5825 tp->t_flags |= TF_SACK_PERMIT;
5826 }
5827 }
5828 if (to->to_flags & TOF_SCALE) {
5829 tp->t_flags |= TF_RCVD_SCALE;
5830 tp->requested_s_scale = to->to_requested_s_scale;
5831
5832 /* Re-enable window scaling, if the option is received */
5833 if (tp->request_r_scale > 0) {
5834 tp->t_flags |= TF_REQ_SCALE;
5835 }
5836 }
5837 }
5838
5839 /*
5840 * Pull out of band byte out of a segment so
5841 * it doesn't appear in the user's data queue.
5842 * It is still reflected in the segment length for
5843 * sequencing purposes.
5844 *
5845 * @param off delayed to be droped hdrlen
5846 */
5847 static void
5848 tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, int off)
5849 {
5850 int cnt = off + th->th_urp - 1;
5851
5852 while (cnt >= 0) {
5853 if (m->m_len > cnt) {
5854 char *cp = mtod(m, caddr_t) + cnt;
5855 struct tcpcb *tp = sototcpcb(so);
5856
5857 tp->t_iobc = *cp;
5858 tp->t_oobflags |= TCPOOB_HAVEDATA;
5859 bcopy(cp + 1, cp, (unsigned)(m->m_len - cnt - 1));
5860 m->m_len--;
5861 if (m->m_flags & M_PKTHDR) {
5862 m->m_pkthdr.len--;
5863 }
5864 return;
5865 }
5866 cnt -= m->m_len;
5867 m = m->m_next;
5868 if (m == 0) {
5869 break;
5870 }
5871 }
5872 panic("tcp_pulloutofband");
5873 }
5874
5875 uint32_t
5876 get_base_rtt(struct tcpcb *tp)
5877 {
5878 struct rtentry *rt = tp->t_inpcb->inp_route.ro_rt;
5879 return (rt == NULL) ? 0 : rt->rtt_min;
5880 }
5881
5882 /* Each value of RTT base represents the minimum RTT seen in a minute.
5883 * We keep upto N_RTT_BASE minutes worth of history.
5884 */
5885 void
5886 update_base_rtt(struct tcpcb *tp, uint32_t rtt)
5887 {
5888 u_int32_t base_rtt, i;
5889 struct rtentry *rt;
5890
5891 if ((rt = tp->t_inpcb->inp_route.ro_rt) == NULL) {
5892 return;
5893 }
5894 if (rt->rtt_expire_ts == 0) {
5895 RT_LOCK_SPIN(rt);
5896 if (rt->rtt_expire_ts != 0) {
5897 RT_UNLOCK(rt);
5898 goto update;
5899 }
5900 rt->rtt_expire_ts = tcp_now;
5901 rt->rtt_index = 0;
5902 rt->rtt_hist[0] = rtt;
5903 rt->rtt_min = rtt;
5904 RT_UNLOCK(rt);
5905 return;
5906 }
5907 update:
5908 #if TRAFFIC_MGT
5909 /*
5910 * If the recv side is being throttled, check if the
5911 * current RTT is closer to the base RTT seen in
5912 * first (recent) two slots. If so, unthrottle the stream.
5913 */
5914 if ((tp->t_flagsext & TF_RECV_THROTTLE) &&
5915 (int)(tcp_now - tp->t_recv_throttle_ts) >= TCP_RECV_THROTTLE_WIN) {
5916 base_rtt = rt->rtt_min;
5917 if (tp->t_rttcur <= (base_rtt + target_qdelay)) {
5918 tp->t_flagsext &= ~TF_RECV_THROTTLE;
5919 tp->t_recv_throttle_ts = 0;
5920 }
5921 }
5922 #endif /* TRAFFIC_MGT */
5923 if ((int)(tcp_now - rt->rtt_expire_ts) >=
5924 TCP_RTT_HISTORY_EXPIRE_TIME) {
5925 RT_LOCK_SPIN(rt);
5926 /* check the condition again to avoid race */
5927 if ((int)(tcp_now - rt->rtt_expire_ts) >=
5928 TCP_RTT_HISTORY_EXPIRE_TIME) {
5929 rt->rtt_index++;
5930 if (rt->rtt_index >= NRTT_HIST) {
5931 rt->rtt_index = 0;
5932 }
5933 rt->rtt_hist[rt->rtt_index] = rtt;
5934 rt->rtt_expire_ts = tcp_now;
5935 } else {
5936 rt->rtt_hist[rt->rtt_index] =
5937 min(rt->rtt_hist[rt->rtt_index], rtt);
5938 }
5939 /* forget the old value and update minimum */
5940 rt->rtt_min = 0;
5941 for (i = 0; i < NRTT_HIST; ++i) {
5942 if (rt->rtt_hist[i] != 0 &&
5943 (rt->rtt_min == 0 ||
5944 rt->rtt_hist[i] < rt->rtt_min)) {
5945 rt->rtt_min = rt->rtt_hist[i];
5946 }
5947 }
5948 RT_UNLOCK(rt);
5949 } else {
5950 rt->rtt_hist[rt->rtt_index] =
5951 min(rt->rtt_hist[rt->rtt_index], rtt);
5952 if (rt->rtt_min == 0) {
5953 rt->rtt_min = rtt;
5954 } else {
5955 rt->rtt_min = min(rt->rtt_min, rtt);
5956 }
5957 }
5958 }
5959
5960 /*
5961 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
5962 * present but transmit timer is running and timed sequence number was
5963 * acked, update smoothed RTT.
5964 *
5965 * If timestamps are supported, a receiver can update RTT even if
5966 * there is no outstanding data.
5967 *
5968 * Some boxes send broken timestamp replies during the SYN+ACK phase,
5969 * ignore timestamps of 0or we could calculate a huge RTT and blow up
5970 * the retransmit timer.
5971 */
5972 static void
5973 tcp_compute_rtt(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th)
5974 {
5975 int rtt = 0;
5976 VERIFY(to != NULL && th != NULL);
5977 if (tp->t_rtttime != 0 && SEQ_GT(th->th_ack, tp->t_rtseq)) {
5978 u_int32_t pipe_ack_val;
5979 rtt = tcp_now - tp->t_rtttime;
5980 /*
5981 * Compute pipe ack -- the amount of data acknowledged
5982 * in the last RTT
5983 */
5984 if (SEQ_GT(th->th_ack, tp->t_pipeack_lastuna)) {
5985 pipe_ack_val = th->th_ack - tp->t_pipeack_lastuna;
5986 /* Update the sample */
5987 tp->t_pipeack_sample[tp->t_pipeack_ind++] =
5988 pipe_ack_val;
5989 tp->t_pipeack_ind %= TCP_PIPEACK_SAMPLE_COUNT;
5990
5991 /* Compute the max of the pipeack samples */
5992 pipe_ack_val = tcp_get_max_pipeack(tp);
5993 tp->t_pipeack = (pipe_ack_val >
5994 TCP_CC_CWND_INIT_BYTES) ?
5995 pipe_ack_val : 0;
5996 }
5997 /* start another measurement */
5998 tp->t_rtttime = 0;
5999 }
6000 if (((to->to_flags & TOF_TS) != 0) &&
6001 (to->to_tsecr != 0) &&
6002 TSTMP_GEQ(tcp_now, to->to_tsecr)) {
6003 tcp_xmit_timer(tp, (tcp_now - to->to_tsecr),
6004 to->to_tsecr, th->th_ack);
6005 } else if (rtt > 0) {
6006 tcp_xmit_timer(tp, rtt, 0, th->th_ack);
6007 }
6008 }
6009
6010 /*
6011 * Collect new round-trip time estimate and update averages and
6012 * current timeout.
6013 */
6014 static void
6015 tcp_xmit_timer(struct tcpcb *tp, int rtt,
6016 u_int32_t tsecr, tcp_seq th_ack)
6017 {
6018 int delta;
6019 int old_srtt = tp->t_srtt;
6020 int old_rttvar = tp->t_rttvar;
6021 bool log_rtt = false;
6022
6023 /*
6024 * On AWDL interface, the initial RTT measurement on SYN
6025 * can be wrong due to peer caching. Avoid the first RTT
6026 * measurement as it might skew up the RTO.
6027 * <rdar://problem/28739046>
6028 */
6029 if (tp->t_inpcb->inp_last_outifp != NULL &&
6030 (tp->t_inpcb->inp_last_outifp->if_eflags & IFEF_AWDL) &&
6031 th_ack == tp->iss + 1) {
6032 return;
6033 }
6034
6035 if (tp->t_flagsext & TF_RECOMPUTE_RTT) {
6036 if (SEQ_GT(th_ack, tp->snd_una) &&
6037 SEQ_LEQ(th_ack, tp->snd_max) &&
6038 (tsecr == 0 ||
6039 TSTMP_GEQ(tsecr, tp->t_badrexmt_time))) {
6040 /*
6041 * We received a new ACk after a
6042 * spurious timeout. Adapt retransmission
6043 * timer as described in rfc 4015.
6044 */
6045 tp->t_flagsext &= ~(TF_RECOMPUTE_RTT);
6046 tp->t_badrexmt_time = 0;
6047 tp->t_srtt = max(tp->t_srtt_prev, rtt);
6048 tp->t_srtt = tp->t_srtt << TCP_RTT_SHIFT;
6049 tp->t_rttvar = max(tp->t_rttvar_prev, (rtt >> 1));
6050 tp->t_rttvar = tp->t_rttvar << TCP_RTTVAR_SHIFT;
6051
6052 if (tp->t_rttbest > (tp->t_srtt + tp->t_rttvar)) {
6053 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6054 }
6055
6056 goto compute_rto;
6057 } else {
6058 return;
6059 }
6060 }
6061
6062 tcpstat.tcps_rttupdated++;
6063 tp->t_rttupdated++;
6064
6065 if (rtt > 0) {
6066 tp->t_rttcur = rtt;
6067 update_base_rtt(tp, rtt);
6068 }
6069
6070 if (tp->t_srtt != 0) {
6071 /*
6072 * srtt is stored as fixed point with 5 bits after the
6073 * binary point (i.e., scaled by 32). The following magic
6074 * is equivalent to the smoothing algorithm in rfc793 with
6075 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
6076 * point).
6077 *
6078 * Freebsd adjusts rtt to origin 0 by subtracting 1
6079 * from the provided rtt value. This was required because
6080 * of the way t_rtttime was initiailised to 1 before.
6081 * Since we changed t_rtttime to be based on
6082 * tcp_now, this extra adjustment is not needed.
6083 */
6084 delta = (rtt << TCP_DELTA_SHIFT)
6085 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
6086
6087 if ((tp->t_srtt += delta) <= 0) {
6088 tp->t_srtt = 1;
6089 }
6090
6091 /*
6092 * We accumulate a smoothed rtt variance (actually, a
6093 * smoothed mean difference), then set the retransmit
6094 * timer to smoothed rtt + 4 times the smoothed variance.
6095 * rttvar is stored as fixed point with 4 bits after the
6096 * binary point (scaled by 16). The following is
6097 * equivalent to rfc793 smoothing with an alpha of .75
6098 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
6099 * rfc793's wired-in beta.
6100 */
6101 if (delta < 0) {
6102 delta = -delta;
6103 }
6104 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
6105 if ((tp->t_rttvar += delta) <= 0) {
6106 tp->t_rttvar = 1;
6107 }
6108 if (tp->t_rttbest == 0 ||
6109 tp->t_rttbest > (tp->t_srtt + tp->t_rttvar)) {
6110 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
6111 }
6112 } else {
6113 /*
6114 * No rtt measurement yet - use the unsmoothed rtt.
6115 * Set the variance to half the rtt (so our first
6116 * retransmit happens at 3*rtt).
6117 */
6118 tp->t_srtt = rtt << TCP_RTT_SHIFT;
6119 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
6120 }
6121
6122 compute_rto:
6123 nstat_route_rtt(tp->t_inpcb->inp_route.ro_rt, tp->t_srtt,
6124 tp->t_rttvar);
6125
6126 /*
6127 * the retransmit should happen at rtt + 4 * rttvar.
6128 * Because of the way we do the smoothing, srtt and rttvar
6129 * will each average +1/2 tick of bias. When we compute
6130 * the retransmit timer, we want 1/2 tick of rounding and
6131 * 1 extra tick because of +-1/2 tick uncertainty in the
6132 * firing of the timer. The bias will give us exactly the
6133 * 1.5 tick we need. But, because the bias is
6134 * statistical, we have to test that we don't drop below
6135 * the minimum feasible timer (which is 2 ticks).
6136 */
6137 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
6138 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX,
6139 TCP_ADD_REXMTSLOP(tp));
6140
6141 /*
6142 * We received an ack for a packet that wasn't retransmitted;
6143 * it is probably safe to discard any error indications we've
6144 * received recently. This isn't quite right, but close enough
6145 * for now (a route might have failed after we sent a segment,
6146 * and the return path might not be symmetrical).
6147 */
6148 tp->t_softerror = 0;
6149
6150 if (log_rtt) {
6151 TCP_LOG_RTT_INFO(tp);
6152 }
6153
6154 TCP_LOG_RTT_CHANGE(tp, old_srtt, old_rttvar);
6155 }
6156
6157 static inline unsigned int
6158 tcp_maxmtu(struct rtentry *rt)
6159 {
6160 unsigned int maxmtu;
6161 int interface_mtu = 0;
6162
6163 RT_LOCK_ASSERT_HELD(rt);
6164 interface_mtu = rt->rt_ifp->if_mtu;
6165
6166 if (rt_key(rt)->sa_family == AF_INET &&
6167 INTF_ADJUST_MTU_FOR_CLAT46(rt->rt_ifp)) {
6168 interface_mtu = IN6_LINKMTU(rt->rt_ifp);
6169 /* Further adjust the size for CLAT46 expansion */
6170 interface_mtu -= CLAT46_HDR_EXPANSION_OVERHD;
6171 }
6172
6173 if (rt->rt_rmx.rmx_mtu == 0) {
6174 maxmtu = interface_mtu;
6175 } else {
6176 maxmtu = MIN(rt->rt_rmx.rmx_mtu, interface_mtu);
6177 }
6178
6179 return maxmtu;
6180 }
6181
6182 #if INET6
6183 static inline unsigned int
6184 tcp_maxmtu6(struct rtentry *rt)
6185 {
6186 unsigned int maxmtu;
6187 struct nd_ifinfo *ndi = NULL;
6188
6189 RT_LOCK_ASSERT_HELD(rt);
6190 if ((ndi = ND_IFINFO(rt->rt_ifp)) != NULL && !ndi->initialized) {
6191 ndi = NULL;
6192 }
6193 if (ndi != NULL) {
6194 lck_mtx_lock(&ndi->lock);
6195 }
6196 if (rt->rt_rmx.rmx_mtu == 0) {
6197 maxmtu = IN6_LINKMTU(rt->rt_ifp);
6198 } else {
6199 maxmtu = MIN(rt->rt_rmx.rmx_mtu, IN6_LINKMTU(rt->rt_ifp));
6200 }
6201 if (ndi != NULL) {
6202 lck_mtx_unlock(&ndi->lock);
6203 }
6204
6205 return maxmtu;
6206 }
6207 #endif
6208
6209 unsigned int
6210 get_maxmtu(struct rtentry *rt)
6211 {
6212 unsigned int maxmtu = 0;
6213
6214 RT_LOCK_ASSERT_NOTHELD(rt);
6215
6216 RT_LOCK(rt);
6217
6218 if (rt_key(rt)->sa_family == AF_INET6) {
6219 maxmtu = tcp_maxmtu6(rt);
6220 } else {
6221 maxmtu = tcp_maxmtu(rt);
6222 }
6223
6224 RT_UNLOCK(rt);
6225
6226 return maxmtu;
6227 }
6228
6229 /*
6230 * Determine a reasonable value for maxseg size.
6231 * If the route is known, check route for mtu.
6232 * If none, use an mss that can be handled on the outgoing
6233 * interface without forcing IP to fragment; if bigger than
6234 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
6235 * to utilize large mbufs. If no route is found, route has no mtu,
6236 * or the destination isn't local, use a default, hopefully conservative
6237 * size (usually 512 or the default IP max size, but no more than the mtu
6238 * of the interface), as we can't discover anything about intervening
6239 * gateways or networks. We also initialize the congestion/slow start
6240 * window. While looking at the routing entry, we also initialize
6241 * other path-dependent parameters from pre-set or cached values
6242 * in the routing entry.
6243 *
6244 * Also take into account the space needed for options that we
6245 * send regularly. Make maxseg shorter by that amount to assure
6246 * that we can send maxseg amount of data even when the options
6247 * are present. Store the upper limit of the length of options plus
6248 * data in maxopd.
6249 *
6250 * NOTE that this routine is only called when we process an incoming
6251 * segment, for outgoing segments only tcp_mssopt is called.
6252 *
6253 */
6254 void
6255 tcp_mss(struct tcpcb *tp, int offer, unsigned int input_ifscope)
6256 {
6257 struct rtentry *rt;
6258 struct ifnet *ifp;
6259 int rtt, mss;
6260 u_int32_t bufsize;
6261 struct inpcb *inp;
6262 struct socket *so;
6263 struct rmxp_tao *taop;
6264 int origoffer = offer;
6265 u_int32_t sb_max_corrected;
6266 int isnetlocal = 0;
6267 #if INET6
6268 int isipv6;
6269 int min_protoh;
6270 #endif
6271
6272 inp = tp->t_inpcb;
6273
6274 so = inp->inp_socket;
6275 /*
6276 * Nothing left to send after the socket is defunct or TCP is in the closed state
6277 */
6278 if ((so->so_state & SS_DEFUNCT) || tp->t_state == TCPS_CLOSED) {
6279 return;
6280 }
6281
6282 #if INET6
6283 isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
6284 min_protoh = isipv6 ? sizeof(struct ip6_hdr) + sizeof(struct tcphdr)
6285 : sizeof(struct tcpiphdr);
6286 #else
6287 #define min_protoh (sizeof (struct tcpiphdr))
6288 #endif
6289
6290 #if INET6
6291 if (isipv6) {
6292 rt = tcp_rtlookup6(inp, input_ifscope);
6293 } else
6294 #endif /* INET6 */
6295 {
6296 rt = tcp_rtlookup(inp, input_ifscope);
6297 }
6298 isnetlocal = (tp->t_flags & TF_LOCAL);
6299
6300 if (rt == NULL) {
6301 tp->t_maxopd = tp->t_maxseg =
6302 #if INET6
6303 isipv6 ? tcp_v6mssdflt :
6304 #endif /* INET6 */
6305 tcp_mssdflt;
6306 return;
6307 }
6308 ifp = rt->rt_ifp;
6309 /*
6310 * Slower link window correction:
6311 * If a value is specificied for slowlink_wsize use it for
6312 * PPP links believed to be on a serial modem (speed <128Kbps).
6313 * Excludes 9600bps as it is the default value adversized
6314 * by pseudo-devices over ppp.
6315 */
6316 if (ifp->if_type == IFT_PPP && slowlink_wsize > 0 &&
6317 ifp->if_baudrate > 9600 && ifp->if_baudrate <= 128000) {
6318 tp->t_flags |= TF_SLOWLINK;
6319 }
6320
6321 taop = rmx_taop(rt->rt_rmx);
6322 /*
6323 * Offer == -1 means that we didn't receive SYN yet,
6324 * use cached value in that case;
6325 */
6326 if (offer == -1) {
6327 offer = taop->tao_mssopt;
6328 }
6329 /*
6330 * Offer == 0 means that there was no MSS on the SYN segment,
6331 * in this case we use tcp_mssdflt.
6332 */
6333 if (offer == 0) {
6334 offer =
6335 #if INET6
6336 isipv6 ? tcp_v6mssdflt :
6337 #endif /* INET6 */
6338 tcp_mssdflt;
6339 } else {
6340 /*
6341 * Prevent DoS attack with too small MSS. Round up
6342 * to at least minmss.
6343 */
6344 offer = max(offer, tcp_minmss);
6345 /*
6346 * Sanity check: make sure that maxopd will be large
6347 * enough to allow some data on segments even is the
6348 * all the option space is used (40bytes). Otherwise
6349 * funny things may happen in tcp_output.
6350 */
6351 offer = max(offer, 64);
6352 }
6353 taop->tao_mssopt = offer;
6354
6355 /*
6356 * While we're here, check if there's an initial rtt
6357 * or rttvar. Convert from the route-table units
6358 * to scaled multiples of the slow timeout timer.
6359 */
6360 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt) != 0) {
6361 tcp_getrt_rtt(tp, rt);
6362 } else {
6363 tp->t_rttmin = isnetlocal ? tcp_TCPTV_MIN : TCPTV_REXMTMIN;
6364 }
6365
6366 #if INET6
6367 mss = (isipv6 ? tcp_maxmtu6(rt) : tcp_maxmtu(rt));
6368 #else
6369 mss = tcp_maxmtu(rt);
6370 #endif
6371
6372 #if NECP
6373 // At this point, the mss is just the MTU. Adjust if necessary.
6374 mss = necp_socket_get_effective_mtu(inp, mss);
6375 #endif /* NECP */
6376
6377 mss -= min_protoh;
6378
6379 if (rt->rt_rmx.rmx_mtu == 0) {
6380 #if INET6
6381 if (isipv6) {
6382 if (!isnetlocal) {
6383 mss = min(mss, tcp_v6mssdflt);
6384 }
6385 } else
6386 #endif /* INET6 */
6387 if (!isnetlocal) {
6388 mss = min(mss, tcp_mssdflt);
6389 }
6390 }
6391
6392 mss = min(mss, offer);
6393 /*
6394 * maxopd stores the maximum length of data AND options
6395 * in a segment; maxseg is the amount of data in a normal
6396 * segment. We need to store this value (maxopd) apart
6397 * from maxseg, because now every segment carries options
6398 * and thus we normally have somewhat less data in segments.
6399 */
6400 tp->t_maxopd = mss;
6401
6402 /*
6403 * origoffer==-1 indicates, that no segments were received yet.
6404 * In this case we just guess.
6405 */
6406 if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
6407 (origoffer == -1 ||
6408 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) {
6409 mss -= TCPOLEN_TSTAMP_APPA;
6410 }
6411
6412 #if MPTCP
6413 mss -= mptcp_adj_mss(tp, FALSE);
6414 #endif /* MPTCP */
6415 tp->t_maxseg = mss;
6416
6417 /*
6418 * Calculate corrected value for sb_max; ensure to upgrade the
6419 * numerator for large sb_max values else it will overflow.
6420 */
6421 sb_max_corrected = (sb_max * (u_int64_t)MCLBYTES) / (MSIZE + MCLBYTES);
6422
6423 /*
6424 * If there's a pipesize (ie loopback), change the socket
6425 * buffer to that size only if it's bigger than the current
6426 * sockbuf size. Make the socket buffers an integral
6427 * number of mss units; if the mss is larger than
6428 * the socket buffer, decrease the mss.
6429 */
6430 #if RTV_SPIPE
6431 bufsize = rt->rt_rmx.rmx_sendpipe;
6432 if (bufsize < so->so_snd.sb_hiwat)
6433 #endif
6434 bufsize = so->so_snd.sb_hiwat;
6435 if (bufsize < mss) {
6436 mss = bufsize;
6437 } else {
6438 bufsize = (((bufsize + (u_int64_t)mss - 1) / (u_int64_t)mss) * (u_int64_t)mss);
6439 if (bufsize > sb_max_corrected) {
6440 bufsize = sb_max_corrected;
6441 }
6442 (void)sbreserve(&so->so_snd, bufsize);
6443 }
6444 tp->t_maxseg = mss;
6445
6446 ASSERT(tp->t_maxseg);
6447
6448 /*
6449 * Update MSS using recommendation from link status report. This is
6450 * temporary
6451 */
6452 tcp_update_mss_locked(so, ifp);
6453
6454 #if RTV_RPIPE
6455 bufsize = rt->rt_rmx.rmx_recvpipe;
6456 if (bufsize < so->so_rcv.sb_hiwat)
6457 #endif
6458 bufsize = so->so_rcv.sb_hiwat;
6459 if (bufsize > mss) {
6460 bufsize = (((bufsize + (u_int64_t)mss - 1) / (u_int64_t)mss) * (u_int64_t)mss);
6461 if (bufsize > sb_max_corrected) {
6462 bufsize = sb_max_corrected;
6463 }
6464 (void)sbreserve(&so->so_rcv, bufsize);
6465 }
6466
6467 set_tcp_stream_priority(so);
6468
6469 if (rt->rt_rmx.rmx_ssthresh) {
6470 /*
6471 * There's some sort of gateway or interface
6472 * buffer limit on the path. Use this to set
6473 * slow-start threshold, but set the threshold to
6474 * no less than 2*mss.
6475 */
6476 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
6477 tcpstat.tcps_usedssthresh++;
6478 } else {
6479 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
6480 }
6481
6482 /*
6483 * Set the slow-start flight size depending on whether this
6484 * is a local network or not.
6485 */
6486 if (CC_ALGO(tp)->cwnd_init != NULL) {
6487 CC_ALGO(tp)->cwnd_init(tp);
6488 }
6489
6490 tcp_ccdbg_trace(tp, NULL, TCP_CC_CWND_INIT);
6491
6492 /* Route locked during lookup above */
6493 RT_UNLOCK(rt);
6494 }
6495
6496 /*
6497 * Determine the MSS option to send on an outgoing SYN.
6498 */
6499 int
6500 tcp_mssopt(struct tcpcb *tp)
6501 {
6502 struct rtentry *rt;
6503 int mss;
6504 #if INET6
6505 int isipv6;
6506 int min_protoh;
6507 #endif
6508
6509 #if INET6
6510 isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
6511 min_protoh = isipv6 ? sizeof(struct ip6_hdr) + sizeof(struct tcphdr)
6512 : sizeof(struct tcpiphdr);
6513 #else
6514 #define min_protoh (sizeof (struct tcpiphdr))
6515 #endif
6516
6517 #if INET6
6518 if (isipv6) {
6519 rt = tcp_rtlookup6(tp->t_inpcb, IFSCOPE_NONE);
6520 } else
6521 #endif /* INET6 */
6522 rt = tcp_rtlookup(tp->t_inpcb, IFSCOPE_NONE);
6523 if (rt == NULL) {
6524 return
6525 #if INET6
6526 isipv6 ? tcp_v6mssdflt :
6527 #endif /* INET6 */
6528 tcp_mssdflt;
6529 }
6530 /*
6531 * Slower link window correction:
6532 * If a value is specificied for slowlink_wsize use it for PPP links
6533 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
6534 * it is the default value adversized by pseudo-devices over ppp.
6535 */
6536 if (rt->rt_ifp->if_type == IFT_PPP && slowlink_wsize > 0 &&
6537 rt->rt_ifp->if_baudrate > 9600 && rt->rt_ifp->if_baudrate <= 128000) {
6538 tp->t_flags |= TF_SLOWLINK;
6539 }
6540
6541 #if INET6
6542 mss = (isipv6 ? tcp_maxmtu6(rt) : tcp_maxmtu(rt));
6543 #else
6544 mss = tcp_maxmtu(rt);
6545 #endif
6546 /* Route locked during lookup above */
6547 RT_UNLOCK(rt);
6548
6549 #if NECP
6550 // At this point, the mss is just the MTU. Adjust if necessary.
6551 mss = necp_socket_get_effective_mtu(tp->t_inpcb, mss);
6552 #endif /* NECP */
6553
6554 return mss - min_protoh;
6555 }
6556
6557 /*
6558 * On a partial ack arrives, force the retransmission of the
6559 * next unacknowledged segment. Do not clear tp->t_dupacks.
6560 * By setting snd_nxt to th_ack, this forces retransmission timer to
6561 * be started again.
6562 */
6563 static void
6564 tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
6565 {
6566 tcp_seq onxt = tp->snd_nxt;
6567 u_int32_t ocwnd = tp->snd_cwnd;
6568 tp->t_timer[TCPT_REXMT] = 0;
6569 tp->t_timer[TCPT_PTO] = 0;
6570 tp->t_rtttime = 0;
6571 tp->snd_nxt = th->th_ack;
6572 /*
6573 * Set snd_cwnd to one segment beyond acknowledged offset
6574 * (tp->snd_una has not yet been updated when this function
6575 * is called)
6576 */
6577 tp->snd_cwnd = tp->t_maxseg + BYTES_ACKED(th, tp);
6578 tp->t_flags |= TF_ACKNOW;
6579 (void) tcp_output(tp);
6580 tp->snd_cwnd = ocwnd;
6581 if (SEQ_GT(onxt, tp->snd_nxt)) {
6582 tp->snd_nxt = onxt;
6583 }
6584 /*
6585 * Partial window deflation. Relies on fact that tp->snd_una
6586 * not updated yet.
6587 */
6588 if (tp->snd_cwnd > BYTES_ACKED(th, tp)) {
6589 tp->snd_cwnd -= BYTES_ACKED(th, tp);
6590 } else {
6591 tp->snd_cwnd = 0;
6592 }
6593 tp->snd_cwnd += tp->t_maxseg;
6594 }
6595
6596 /*
6597 * Drop a random TCP connection that hasn't been serviced yet and
6598 * is eligible for discard. There is a one in qlen chance that
6599 * we will return a null, saying that there are no dropable
6600 * requests. In this case, the protocol specific code should drop
6601 * the new request. This insures fairness.
6602 *
6603 * The listening TCP socket "head" must be locked
6604 */
6605 static int
6606 tcp_dropdropablreq(struct socket *head)
6607 {
6608 struct socket *so, *sonext;
6609 unsigned int i, j, qlen;
6610 static u_int32_t rnd = 0;
6611 static u_int64_t old_runtime;
6612 static unsigned int cur_cnt, old_cnt;
6613 u_int64_t now_sec;
6614 struct inpcb *inp = NULL;
6615 struct tcpcb *tp;
6616
6617 if ((head->so_options & SO_ACCEPTCONN) == 0) {
6618 return 0;
6619 }
6620
6621 if (TAILQ_EMPTY(&head->so_incomp)) {
6622 return 0;
6623 }
6624
6625 so_acquire_accept_list(head, NULL);
6626 socket_unlock(head, 0);
6627
6628 /*
6629 * Check if there is any socket in the incomp queue
6630 * that is closed because of a reset from the peer and is
6631 * waiting to be garbage collected. If so, pick that as
6632 * the victim
6633 */
6634 TAILQ_FOREACH_SAFE(so, &head->so_incomp, so_list, sonext) {
6635 inp = sotoinpcb(so);
6636 tp = intotcpcb(inp);
6637 if (tp != NULL && tp->t_state == TCPS_CLOSED &&
6638 so->so_head != NULL &&
6639 (so->so_state & (SS_INCOMP | SS_CANTSENDMORE | SS_CANTRCVMORE)) ==
6640 (SS_INCOMP | SS_CANTSENDMORE | SS_CANTRCVMORE)) {
6641 /*
6642 * The listen socket is already locked but we
6643 * can lock this socket here without lock ordering
6644 * issues because it is in the incomp queue and
6645 * is not visible to others.
6646 */
6647 if (socket_try_lock(so)) {
6648 so->so_usecount++;
6649 goto found_victim;
6650 } else {
6651 continue;
6652 }
6653 }
6654 }
6655
6656 so = TAILQ_FIRST(&head->so_incomp);
6657
6658 now_sec = net_uptime();
6659 if ((i = (now_sec - old_runtime)) != 0) {
6660 old_runtime = now_sec;
6661 old_cnt = cur_cnt / i;
6662 cur_cnt = 0;
6663 }
6664
6665 qlen = head->so_incqlen;
6666 if (rnd == 0) {
6667 rnd = RandomULong();
6668 }
6669
6670 if (++cur_cnt > qlen || old_cnt > qlen) {
6671 rnd = (314159 * rnd + 66329) & 0xffff;
6672 j = ((qlen + 1) * rnd) >> 16;
6673
6674 while (j-- && so) {
6675 so = TAILQ_NEXT(so, so_list);
6676 }
6677 }
6678 /* Find a connection that is not already closing (or being served) */
6679 while (so) {
6680 inp = (struct inpcb *)so->so_pcb;
6681
6682 sonext = TAILQ_NEXT(so, so_list);
6683
6684 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
6685 /*
6686 * Avoid the issue of a socket being accepted
6687 * by one input thread and being dropped by
6688 * another input thread. If we can't get a hold
6689 * on this mutex, then grab the next socket in
6690 * line.
6691 */
6692 if (socket_try_lock(so)) {
6693 so->so_usecount++;
6694 if ((so->so_usecount == 2) &&
6695 (so->so_state & SS_INCOMP) &&
6696 !(so->so_flags & SOF_INCOMP_INPROGRESS)) {
6697 break;
6698 } else {
6699 /*
6700 * don't use if being accepted or
6701 * used in any other way
6702 */
6703 in_pcb_checkstate(inp, WNT_RELEASE, 1);
6704 socket_unlock(so, 1);
6705 }
6706 } else {
6707 /*
6708 * do not try to lock the inp in
6709 * in_pcb_checkstate because the lock
6710 * is already held in some other thread.
6711 * Only drop the inp_wntcnt reference.
6712 */
6713 in_pcb_checkstate(inp, WNT_RELEASE, 1);
6714 }
6715 }
6716 so = sonext;
6717 }
6718 if (so == NULL) {
6719 socket_lock(head, 0);
6720 so_release_accept_list(head);
6721 return 0;
6722 }
6723
6724 /* Makes sure socket is still in the right state to be discarded */
6725
6726 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
6727 socket_unlock(so, 1);
6728 socket_lock(head, 0);
6729 so_release_accept_list(head);
6730 return 0;
6731 }
6732
6733 found_victim:
6734 if (so->so_usecount != 2 || !(so->so_state & SS_INCOMP)) {
6735 /* do not discard: that socket is being accepted */
6736 socket_unlock(so, 1);
6737 socket_lock(head, 0);
6738 so_release_accept_list(head);
6739 return 0;
6740 }
6741
6742 socket_lock(head, 0);
6743 TAILQ_REMOVE(&head->so_incomp, so, so_list);
6744 head->so_incqlen--;
6745 head->so_qlen--;
6746 so->so_state &= ~SS_INCOMP;
6747 so->so_flags |= SOF_OVERFLOW;
6748 so->so_head = NULL;
6749 so_release_accept_list(head);
6750 socket_unlock(head, 0);
6751
6752 socket_lock_assert_owned(so);
6753 tp = sototcpcb(so);
6754
6755 tcp_close(tp);
6756 if (inp->inp_wantcnt > 0 && inp->inp_wantcnt != WNT_STOPUSING) {
6757 /*
6758 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
6759 * doesn't require a lock, it could have happened while
6760 * we are holding the lock. This pcb will have to
6761 * be garbage collected later.
6762 * Release the reference held for so_incomp queue
6763 */
6764 VERIFY(so->so_usecount > 0);
6765 so->so_usecount--;
6766 socket_unlock(so, 1);
6767 } else {
6768 /*
6769 * Unlock this socket and leave the reference on.
6770 * We need to acquire the pcbinfo lock in order to
6771 * fully dispose it off
6772 */
6773 socket_unlock(so, 0);
6774
6775 lck_rw_lock_exclusive(tcbinfo.ipi_lock);
6776
6777 socket_lock(so, 0);
6778 /* Release the reference held for so_incomp queue */
6779 VERIFY(so->so_usecount > 0);
6780 so->so_usecount--;
6781
6782 if (so->so_usecount != 1 ||
6783 (inp->inp_wantcnt > 0 &&
6784 inp->inp_wantcnt != WNT_STOPUSING)) {
6785 /*
6786 * There is an extra wantcount or usecount
6787 * that must have been added when the socket
6788 * was unlocked. This socket will have to be
6789 * garbage collected later
6790 */
6791 socket_unlock(so, 1);
6792 } else {
6793 /* Drop the reference held for this function */
6794 VERIFY(so->so_usecount > 0);
6795 so->so_usecount--;
6796
6797 in_pcbdispose(inp);
6798 }
6799 lck_rw_done(tcbinfo.ipi_lock);
6800 }
6801 tcpstat.tcps_drops++;
6802
6803 socket_lock(head, 0);
6804 return 1;
6805 }
6806
6807 /* Set background congestion control on a socket */
6808 void
6809 tcp_set_background_cc(struct socket *so)
6810 {
6811 tcp_set_new_cc(so, TCP_CC_ALGO_BACKGROUND_INDEX);
6812 }
6813
6814 /* Set foreground congestion control on a socket */
6815 void
6816 tcp_set_foreground_cc(struct socket *so)
6817 {
6818 if (tcp_use_newreno) {
6819 tcp_set_new_cc(so, TCP_CC_ALGO_NEWRENO_INDEX);
6820 } else {
6821 tcp_set_new_cc(so, TCP_CC_ALGO_CUBIC_INDEX);
6822 }
6823 }
6824
6825 static void
6826 tcp_set_new_cc(struct socket *so, uint16_t cc_index)
6827 {
6828 struct inpcb *inp = sotoinpcb(so);
6829 struct tcpcb *tp = intotcpcb(inp);
6830 u_char old_cc_index = 0;
6831 if (tp->tcp_cc_index != cc_index) {
6832 old_cc_index = tp->tcp_cc_index;
6833
6834 if (CC_ALGO(tp)->cleanup != NULL) {
6835 CC_ALGO(tp)->cleanup(tp);
6836 }
6837 tp->tcp_cc_index = cc_index;
6838
6839 tcp_cc_allocate_state(tp);
6840
6841 if (CC_ALGO(tp)->switch_to != NULL) {
6842 CC_ALGO(tp)->switch_to(tp, old_cc_index);
6843 }
6844
6845 tcp_ccdbg_trace(tp, NULL, TCP_CC_CHANGE_ALGO);
6846 }
6847 }
6848
6849 void
6850 tcp_set_recv_bg(struct socket *so)
6851 {
6852 if (!IS_TCP_RECV_BG(so)) {
6853 so->so_flags1 |= SOF1_TRAFFIC_MGT_TCP_RECVBG;
6854 }
6855
6856 /* Unset Large Receive Offload on background sockets */
6857 so_set_lro(so, SO_TC_BK);
6858 }
6859
6860 void
6861 tcp_clear_recv_bg(struct socket *so)
6862 {
6863 if (IS_TCP_RECV_BG(so)) {
6864 so->so_flags1 &= ~(SOF1_TRAFFIC_MGT_TCP_RECVBG);
6865 }
6866
6867 /*
6868 * Set/unset use of Large Receive Offload depending on
6869 * the traffic class
6870 */
6871 so_set_lro(so, so->so_traffic_class);
6872 }
6873
6874 void
6875 inp_fc_unthrottle_tcp(struct inpcb *inp)
6876 {
6877 struct tcpcb *tp = inp->inp_ppcb;
6878 /*
6879 * Back off the slow-start threshold and enter
6880 * congestion avoidance phase
6881 */
6882 if (CC_ALGO(tp)->pre_fr != NULL) {
6883 CC_ALGO(tp)->pre_fr(tp);
6884 }
6885
6886 tp->snd_cwnd = tp->snd_ssthresh;
6887 tp->t_flagsext &= ~TF_CWND_NONVALIDATED;
6888 /*
6889 * Restart counting for ABC as we changed the
6890 * congestion window just now.
6891 */
6892 tp->t_bytes_acked = 0;
6893
6894 /* Reset retransmit shift as we know that the reason
6895 * for delay in sending a packet is due to flow
6896 * control on the outgoing interface. There is no need
6897 * to backoff retransmit timer.
6898 */
6899 TCP_RESET_REXMT_STATE(tp);
6900
6901 /*
6902 * Start the output stream again. Since we are
6903 * not retransmitting data, do not reset the
6904 * retransmit timer or rtt calculation.
6905 */
6906 tcp_output(tp);
6907 }
6908
6909 static int
6910 tcp_getstat SYSCTL_HANDLER_ARGS
6911 {
6912 #pragma unused(oidp, arg1, arg2)
6913
6914 int error;
6915 struct tcpstat *stat;
6916 stat = &tcpstat;
6917 #if !CONFIG_EMBEDDED
6918 struct tcpstat zero_stat;
6919
6920 if (tcp_disable_access_to_stats &&
6921 !kauth_cred_issuser(kauth_cred_get())) {
6922 bzero(&zero_stat, sizeof(zero_stat));
6923 stat = &zero_stat;
6924 }
6925
6926 #endif /* !CONFIG_EMBEDDED */
6927
6928 if (req->oldptr == 0) {
6929 req->oldlen = (size_t)sizeof(struct tcpstat);
6930 }
6931
6932 error = SYSCTL_OUT(req, stat, MIN(sizeof(tcpstat), req->oldlen));
6933
6934 return error;
6935 }
6936
6937 /*
6938 * Checksum extended TCP header and data.
6939 */
6940 int
6941 tcp_input_checksum(int af, struct mbuf *m, struct tcphdr *th, int off, int tlen)
6942 {
6943 struct ifnet *ifp = m->m_pkthdr.rcvif;
6944
6945 switch (af) {
6946 case AF_INET: {
6947 struct ip *ip = mtod(m, struct ip *);
6948 struct ipovly *ipov = (struct ipovly *)ip;
6949
6950 if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_DID_CSUM) {
6951 return 0;
6952 }
6953
6954 /* ip_stripoptions() must have been called before we get here */
6955 ASSERT((ip->ip_hl << 2) == sizeof(*ip));
6956
6957 if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
6958 (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
6959 (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
6960 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
6961 th->th_sum = m->m_pkthdr.csum_rx_val;
6962 } else {
6963 uint32_t sum = m->m_pkthdr.csum_rx_val;
6964 uint32_t start = m->m_pkthdr.csum_rx_start;
6965 int32_t trailer = (m_pktlen(m) - (off + tlen));
6966
6967 /*
6968 * Perform 1's complement adjustment of octets
6969 * that got included/excluded in the hardware-
6970 * calculated checksum value. Ignore cases
6971 * where the value already includes the entire
6972 * IP header span, as the sum for those octets
6973 * would already be 0 by the time we get here;
6974 * IP has already performed its header checksum
6975 * checks. If we do need to adjust, restore
6976 * the original fields in the IP header when
6977 * computing the adjustment value. Also take
6978 * care of any trailing bytes and subtract out
6979 * their partial sum.
6980 */
6981 ASSERT(trailer >= 0);
6982 if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
6983 ((start != 0 && start != off) || trailer)) {
6984 uint32_t swbytes = (uint32_t)trailer;
6985
6986 if (start < off) {
6987 ip->ip_len += sizeof(*ip);
6988 #if BYTE_ORDER != BIG_ENDIAN
6989 HTONS(ip->ip_len);
6990 HTONS(ip->ip_off);
6991 #endif /* BYTE_ORDER != BIG_ENDIAN */
6992 }
6993 /* callee folds in sum */
6994 sum = m_adj_sum16(m, start, off,
6995 tlen, sum);
6996 if (off > start) {
6997 swbytes += (off - start);
6998 } else {
6999 swbytes += (start - off);
7000 }
7001
7002 if (start < off) {
7003 #if BYTE_ORDER != BIG_ENDIAN
7004 NTOHS(ip->ip_off);
7005 NTOHS(ip->ip_len);
7006 #endif /* BYTE_ORDER != BIG_ENDIAN */
7007 ip->ip_len -= sizeof(*ip);
7008 }
7009
7010 if (swbytes != 0) {
7011 tcp_in_cksum_stats(swbytes);
7012 }
7013 if (trailer != 0) {
7014 m_adj(m, -trailer);
7015 }
7016 }
7017
7018 /* callee folds in sum */
7019 th->th_sum = in_pseudo(ip->ip_src.s_addr,
7020 ip->ip_dst.s_addr,
7021 sum + htonl(tlen + IPPROTO_TCP));
7022 }
7023 th->th_sum ^= 0xffff;
7024 } else {
7025 uint16_t ip_sum;
7026 int len;
7027 char b[9];
7028
7029 bcopy(ipov->ih_x1, b, sizeof(ipov->ih_x1));
7030 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7031 ip_sum = ipov->ih_len;
7032 ipov->ih_len = (u_short)tlen;
7033 #if BYTE_ORDER != BIG_ENDIAN
7034 HTONS(ipov->ih_len);
7035 #endif
7036 len = sizeof(struct ip) + tlen;
7037 th->th_sum = in_cksum(m, len);
7038 bcopy(b, ipov->ih_x1, sizeof(ipov->ih_x1));
7039 ipov->ih_len = ip_sum;
7040
7041 tcp_in_cksum_stats(len);
7042 }
7043 break;
7044 }
7045 #if INET6
7046 case AF_INET6: {
7047 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
7048
7049 if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_DID_CSUM) {
7050 return 0;
7051 }
7052
7053 if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
7054 (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
7055 (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
7056 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
7057 th->th_sum = m->m_pkthdr.csum_rx_val;
7058 } else {
7059 uint32_t sum = m->m_pkthdr.csum_rx_val;
7060 uint32_t start = m->m_pkthdr.csum_rx_start;
7061 int32_t trailer = (m_pktlen(m) - (off + tlen));
7062
7063 /*
7064 * Perform 1's complement adjustment of octets
7065 * that got included/excluded in the hardware-
7066 * calculated checksum value. Also take care
7067 * of any trailing bytes and subtract out their
7068 * partial sum.
7069 */
7070 ASSERT(trailer >= 0);
7071 if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
7072 (start != off || trailer != 0)) {
7073 uint16_t s = 0, d = 0;
7074 uint32_t swbytes = (uint32_t)trailer;
7075
7076 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
7077 s = ip6->ip6_src.s6_addr16[1];
7078 ip6->ip6_src.s6_addr16[1] = 0;
7079 }
7080 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
7081 d = ip6->ip6_dst.s6_addr16[1];
7082 ip6->ip6_dst.s6_addr16[1] = 0;
7083 }
7084
7085 /* callee folds in sum */
7086 sum = m_adj_sum16(m, start, off,
7087 tlen, sum);
7088 if (off > start) {
7089 swbytes += (off - start);
7090 } else {
7091 swbytes += (start - off);
7092 }
7093
7094 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
7095 ip6->ip6_src.s6_addr16[1] = s;
7096 }
7097 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
7098 ip6->ip6_dst.s6_addr16[1] = d;
7099 }
7100
7101 if (swbytes != 0) {
7102 tcp_in6_cksum_stats(swbytes);
7103 }
7104 if (trailer != 0) {
7105 m_adj(m, -trailer);
7106 }
7107 }
7108
7109 th->th_sum = in6_pseudo(
7110 &ip6->ip6_src, &ip6->ip6_dst,
7111 sum + htonl(tlen + IPPROTO_TCP));
7112 }
7113 th->th_sum ^= 0xffff;
7114 } else {
7115 tcp_in6_cksum_stats(tlen);
7116 th->th_sum = in6_cksum(m, IPPROTO_TCP, off, tlen);
7117 }
7118 break;
7119 }
7120 #endif /* INET6 */
7121 default:
7122 VERIFY(0);
7123 /* NOTREACHED */
7124 }
7125
7126 if (th->th_sum != 0) {
7127 tcpstat.tcps_rcvbadsum++;
7128 IF_TCP_STATINC(ifp, badformat);
7129 return -1;
7130 }
7131
7132 return 0;
7133 }
7134
7135
7136 SYSCTL_PROC(_net_inet_tcp, TCPCTL_STATS, stats,
7137 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, tcp_getstat,
7138 "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
7139
7140 static int
7141 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
7142 {
7143 #pragma unused(arg1, arg2)
7144
7145 int error, val = tcprexmtthresh;
7146
7147 error = sysctl_handle_int(oidp, &val, 0, req);
7148 if (error || !req->newptr) {
7149 return error;
7150 }
7151
7152 /*
7153 * Constrain the number of duplicate ACKs
7154 * to consider for TCP fast retransmit
7155 * to either 2 or 3
7156 */
7157
7158 if (val < 2 || val > 3) {
7159 return EINVAL;
7160 }
7161
7162 tcprexmtthresh = val;
7163
7164 return 0;
7165 }
7166
7167 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmt_thresh, CTLTYPE_INT | CTLFLAG_RW |
7168 CTLFLAG_LOCKED, &tcprexmtthresh, 0, &sysctl_rexmtthresh, "I",
7169 "Duplicate ACK Threshold for Fast Retransmit");