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