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