]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_input.c
xnu-1486.2.11.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_input.c
CommitLineData
1c79356b 1/*
c910b4d9 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
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.
8f6c56a5 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>
76#include <sys/proc.h> /* for proc0 declaration */
77#include <sys/protosw.h>
78#include <sys/socket.h>
79#include <sys/socketvar.h>
80#include <sys/syslog.h>
81
82#include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
83
b0d623f7
A
84#include <machine/endian.h>
85
1c79356b 86#include <net/if.h>
d12e1678 87#include <net/if_types.h>
1c79356b
A
88#include <net/route.h>
89
90#include <netinet/in.h>
91#include <netinet/in_systm.h>
92#include <netinet/ip.h>
9bccf70c 93#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
1c79356b 94#include <netinet/in_var.h>
9bccf70c 95#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
1c79356b 96#include <netinet/in_pcb.h>
9bccf70c 97#include <netinet/ip_var.h>
1c79356b
A
98#if INET6
99#include <netinet/ip6.h>
100#include <netinet/icmp6.h>
101#include <netinet6/nd6.h>
102#include <netinet6/ip6_var.h>
103#include <netinet6/in6_pcb.h>
104#endif
105#include <netinet/tcp.h>
106#include <netinet/tcp_fsm.h>
107#include <netinet/tcp_seq.h>
108#include <netinet/tcp_timer.h>
109#include <netinet/tcp_var.h>
9bccf70c
A
110#if INET6
111#include <netinet6/tcp6_var.h>
112#endif
1c79356b
A
113#include <netinet/tcpip.h>
114#if TCPDEBUG
115#include <netinet/tcp_debug.h>
9bccf70c 116u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
1c79356b
A
117struct tcphdr tcp_savetcp;
118#endif /* TCPDEBUG */
119
120#if IPSEC
121#include <netinet6/ipsec.h>
9bccf70c
A
122#if INET6
123#include <netinet6/ipsec6.h>
124#endif
1c79356b
A
125#include <netkey/key.h>
126#endif /*IPSEC*/
127
2d21ac55
A
128#if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
129#include <security/mac_framework.h>
130#endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
131
1c79356b
A
132#include <sys/kdebug.h>
133
9bccf70c
A
134#ifndef __APPLE__
135MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
136#endif
137
1c79356b
A
138#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
139#define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
140#define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
141#define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
142
2d21ac55 143static int tcprexmtthresh = 2;
1c79356b
A
144tcp_cc tcp_ccgen;
145
9bccf70c
A
146#if IPSEC
147extern int ipsec_bypass;
148#endif
149
1c79356b 150struct tcpstat tcpstat;
1c79356b 151
9bccf70c 152static int log_in_vain = 0;
1c79356b 153SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
9bccf70c
A
154 &log_in_vain, 0, "Log all incoming TCP connections");
155
156static int blackhole = 0;
157SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
158 &blackhole, 0, "Do not send RST when dropping refused connections");
1c79356b 159
743b1565 160int tcp_delack_enabled = 3;
1c79356b 161SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
9bccf70c
A
162 &tcp_delack_enabled, 0,
163 "Delay ACK to try and piggyback it onto a data packet");
164
165int tcp_lq_overflow = 1;
166SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_lq_overflow, CTLFLAG_RW,
167 &tcp_lq_overflow, 0,
168 "Listen Queue Overflow");
169
170#if TCP_DROP_SYNFIN
55e303ae 171static int drop_synfin = 1;
9bccf70c
A
172SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
173 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
174#endif
1c79356b 175
2d21ac55 176SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW|CTLFLAG_LOCKED, 0,
e5568f75
A
177 "TCP Segment Reassembly Queue");
178
179__private_extern__ int tcp_reass_maxseg = 0;
180SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RW,
181 &tcp_reass_maxseg, 0,
182 "Global maximum number of TCP Segments in Reassembly Queue");
183
184__private_extern__ int tcp_reass_qsize = 0;
185SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
186 &tcp_reass_qsize, 0,
187 "Global number of TCP Segments currently in Reassembly Queue");
188
189static int tcp_reass_overflows = 0;
190SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
191 &tcp_reass_overflows, 0,
192 "Global number of TCP Segment Reassembly Queue Overflows");
193
194
d12e1678
A
195__private_extern__ int slowlink_wsize = 8192;
196SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowlink_wsize, CTLFLAG_RW,
197 &slowlink_wsize, 0, "Maximum advertised window size for slowlink");
198
2d21ac55
A
199static int maxseg_unacked = 8;
200SYSCTL_INT(_net_inet_tcp, OID_AUTO, maxseg_unacked, CTLFLAG_RW,
201 &maxseg_unacked, 0, "Maximum number of outstanding segments left unacked");
202
203static int tcp_do_rfc3465 = 1;
204SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW,
205 &tcp_do_rfc3465, 0, "");
b0d623f7
A
206
207static int tcp_do_rfc3465_lim2 = 1;
208SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465_lim2, CTLFLAG_RW,
209 &tcp_do_rfc3465_lim2, 0, "Appropriate bytes counting w/ L=2*SMSS");
210
211#if CONFIG_IFEF_NOWINDOWSCALE
212int tcp_obey_ifef_nowindowscale = 0;
213SYSCTL_INT(_net_inet_tcp, OID_AUTO, obey_ifef_nowindowscale, CTLFLAG_RW,
214 &tcp_obey_ifef_nowindowscale, 0, "");
215#endif
216
2d21ac55 217extern int tcp_TCPTV_MIN;
d12e1678 218
b0d623f7 219u_int32_t tcp_now;
2d21ac55 220
1c79356b
A
221struct inpcbhead tcb;
222#define tcb6 tcb /* for KAME src sync over BSD*'s */
223struct inpcbinfo tcbinfo;
224
91447636 225static void tcp_dooptions(struct tcpcb *,
c910b4d9 226 u_char *, int, struct tcphdr *, struct tcpopt *, unsigned int);
91447636
A
227static void tcp_pulloutofband(struct socket *,
228 struct tcphdr *, struct mbuf *, int);
229static int tcp_reass(struct tcpcb *, struct tcphdr *, int *,
230 struct mbuf *);
231static void tcp_xmit_timer(struct tcpcb *, int);
2d21ac55
A
232static inline unsigned int tcp_maxmtu(struct rtentry *);
233#if INET6
234static inline unsigned int tcp_maxmtu6(struct rtentry *);
235#endif
1c79356b 236
9bccf70c 237/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
1c79356b 238#if INET6
9bccf70c
A
239#define ND6_HINT(tp) \
240do { \
241 if ((tp) && (tp)->t_inpcb && \
242 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
243 (tp)->t_inpcb->in6p_route.ro_rt) \
244 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
245} while (0)
1c79356b 246#else
9bccf70c 247#define ND6_HINT(tp)
1c79356b 248#endif
9bccf70c 249
b0d623f7 250extern u_int32_t *delack_bitmask;
1c79356b 251
2d21ac55
A
252extern void add_to_time_wait(struct tcpcb *);
253extern void postevent(struct socket *, struct sockbuf *, int);
254
255extern void ipfwsyslog( int level, const char *format,...);
91447636
A
256extern int ChkAddressOK( __uint32_t dstaddr, __uint32_t srcaddr );
257extern int fw_verbose;
2d21ac55
A
258__private_extern__ int tcp_sockthreshold;
259__private_extern__ int tcp_win_scale;
91447636 260
2d21ac55 261#if IPFIREWALL
91447636
A
262#define log_in_vain_log( a ) { \
263 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
264 ipfwsyslog a ; \
265 } \
266 else log a ; \
267}
2d21ac55
A
268#else
269#define log_in_vain_log( a ) { log a; }
270#endif
271
91447636 272
1c79356b 273/*
91447636
A
274 * Indicate whether this ack should be delayed.
275 * We can delay the ack if:
276 * - delayed acks are enabled (set to 1) and
9bccf70c 277 * - our last ack wasn't a 0-sized window. We never want to delay
91447636
A
278 * the ack that opens up a 0-sized window.
279 * - delayed acks are enabled (set to 2, "more compatible") and
280 * - our last ack wasn't a 0-sized window.
281 * - if the peer hasn't sent us a TH_PUSH data packet (this solves 3649245)
282 * - the peer hasn't sent us a TH_PUSH data packet, if he did, take this as a clue that we
283 * need to ACK with no delay. This helps higher level protocols who won't send
284 * us more data even if the window is open because their last "segment" hasn't been ACKed
743b1565 285 * - delayed acks are enabled (set to 3, "streaming detection") and
2d21ac55
A
286 * - if we receive more than "maxseg_unacked" full packets per second on this socket
287 * - if we don't have more than "maxseg_unacked" delayed so far
288 * - if those criteria aren't met, acts like "2". Allowing faster acking while browsing for example.
91447636 289 *
1c79356b 290 */
9bccf70c 291#define DELAY_ACK(tp) \
91447636
A
292 (((tcp_delack_enabled == 1) && ((tp->t_flags & TF_RXWIN0SENT) == 0)) || \
293 (((tcp_delack_enabled == 2) && (tp->t_flags & TF_RXWIN0SENT) == 0) && \
743b1565
A
294 ((thflags & TH_PUSH) == 0) && ((tp->t_flags & TF_DELACK) == 0)) || \
295 (((tcp_delack_enabled == 3) && (tp->t_flags & TF_RXWIN0SENT) == 0) && \
2d21ac55
A
296 (tp->t_rcvtime == 0) && ((thflags & TH_PUSH) == 0) && \
297 (((tp->t_unacksegs == 0)) || \
298 ((tp->rcv_byps > (maxseg_unacked * tp->t_maxseg)) && (tp->t_unacksegs < maxseg_unacked)))))
91447636 299
2d21ac55 300static int tcp_dropdropablreq(struct socket *head);
8ad349bb 301static void tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th);
9bccf70c
A
302
303
304static int
305tcp_reass(tp, th, tlenp, m)
1c79356b
A
306 register struct tcpcb *tp;
307 register struct tcphdr *th;
9bccf70c 308 int *tlenp;
1c79356b 309 struct mbuf *m;
1c79356b 310{
9bccf70c
A
311 struct tseg_qent *q;
312 struct tseg_qent *p = NULL;
313 struct tseg_qent *nq;
8ad349bb 314 struct tseg_qent *te = NULL;
1c79356b
A
315 struct socket *so = tp->t_inpcb->inp_socket;
316 int flags;
91447636 317 int dowakeup = 0;
1c79356b
A
318
319 /*
320 * Call with th==0 after become established to
321 * force pre-ESTABLISHED data up to user socket.
322 */
8ad349bb 323 if (th == NULL)
1c79356b
A
324 goto present;
325
e5568f75
A
326 /*
327 * Limit the number of segments in the reassembly queue to prevent
328 * holding on to too many segments (and thus running out of mbufs).
329 * Make sure to let the missing segment through which caused this
330 * queue. Always keep one global queue entry spare to be able to
331 * process the missing segment.
332 */
333 if (th->th_seq != tp->rcv_nxt &&
334 tcp_reass_qsize + 1 >= tcp_reass_maxseg) {
335 tcp_reass_overflows++;
336 tcpstat.tcps_rcvmemdrop++;
337 m_freem(m);
2d21ac55 338 *tlenp = 0;
e5568f75
A
339 return (0);
340 }
341
9bccf70c
A
342 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
343 MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
344 M_NOWAIT);
345 if (te == NULL) {
1c79356b
A
346 tcpstat.tcps_rcvmemdrop++;
347 m_freem(m);
348 return (0);
349 }
e5568f75 350 tcp_reass_qsize++;
1c79356b
A
351
352 /*
353 * Find a segment which begins after this one does.
354 */
9bccf70c
A
355 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
356 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
1c79356b 357 break;
9bccf70c
A
358 p = q;
359 }
1c79356b
A
360
361 /*
362 * If there is a preceding segment, it may provide some of
363 * our data already. If so, drop the data from the incoming
364 * segment. If it provides all of our data, drop us.
365 */
366 if (p != NULL) {
1c79356b 367 register int i;
1c79356b 368 /* conversion to int (in i) handles seq wraparound */
9bccf70c 369 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
1c79356b 370 if (i > 0) {
9bccf70c 371 if (i >= *tlenp) {
1c79356b 372 tcpstat.tcps_rcvduppack++;
9bccf70c 373 tcpstat.tcps_rcvdupbyte += *tlenp;
1c79356b 374 m_freem(m);
9bccf70c 375 FREE(te, M_TSEGQ);
e5568f75 376 tcp_reass_qsize--;
1c79356b
A
377 /*
378 * Try to present any queued data
379 * at the left window edge to the user.
380 * This is needed after the 3-WHS
381 * completes.
382 */
383 goto present; /* ??? */
1c79356b
A
384 }
385 m_adj(m, i);
9bccf70c 386 *tlenp -= i;
1c79356b
A
387 th->th_seq += i;
388 }
389 }
390 tcpstat.tcps_rcvoopack++;
9bccf70c 391 tcpstat.tcps_rcvoobyte += *tlenp;
1c79356b
A
392
393 /*
394 * While we overlap succeeding segments trim them or,
395 * if they are completely covered, dequeue them.
396 */
397 while (q) {
9bccf70c 398 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
1c79356b
A
399 if (i <= 0)
400 break;
9bccf70c
A
401 if (i < q->tqe_len) {
402 q->tqe_th->th_seq += i;
403 q->tqe_len -= i;
404 m_adj(q->tqe_m, i);
1c79356b
A
405 break;
406 }
9bccf70c
A
407
408 nq = LIST_NEXT(q, tqe_q);
409 LIST_REMOVE(q, tqe_q);
410 m_freem(q->tqe_m);
411 FREE(q, M_TSEGQ);
e5568f75 412 tcp_reass_qsize--;
1c79356b
A
413 q = nq;
414 }
415
9bccf70c
A
416 /* Insert the new segment queue entry into place. */
417 te->tqe_m = m;
418 te->tqe_th = th;
419 te->tqe_len = *tlenp;
1c79356b 420
1c79356b 421 if (p == NULL) {
9bccf70c 422 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
1c79356b 423 } else {
9bccf70c 424 LIST_INSERT_AFTER(p, te, tqe_q);
1c79356b
A
425 }
426
427present:
428 /*
429 * Present data to user, advancing rcv_nxt through
430 * completed sequence space.
431 */
432 if (!TCPS_HAVEESTABLISHED(tp->t_state))
433 return (0);
9bccf70c
A
434 q = LIST_FIRST(&tp->t_segq);
435 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
1c79356b 436 return (0);
1c79356b 437 do {
9bccf70c
A
438 tp->rcv_nxt += q->tqe_len;
439 flags = q->tqe_th->th_flags & TH_FIN;
440 nq = LIST_NEXT(q, tqe_q);
441 LIST_REMOVE(q, tqe_q);
1c79356b 442 if (so->so_state & SS_CANTRCVMORE)
9bccf70c 443 m_freem(q->tqe_m);
91447636 444 else {
2d21ac55 445 if (sbappendstream(&so->so_rcv, q->tqe_m))
91447636
A
446 dowakeup = 1;
447 }
9bccf70c 448 FREE(q, M_TSEGQ);
e5568f75 449 tcp_reass_qsize--;
1c79356b 450 q = nq;
9bccf70c
A
451 } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
452 ND6_HINT(tp);
453
1c79356b 454#if INET6
9bccf70c
A
455 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) {
456
457 KERNEL_DEBUG(DBG_LAYER_BEG,
458 ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport),
459 (((tp->t_inpcb->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
460 (tp->t_inpcb->in6p_faddr.s6_addr16[0] & 0xffff)),
461 0,0,0);
462 }
463 else
1c79356b 464#endif
9bccf70c
A
465 {
466 KERNEL_DEBUG(DBG_LAYER_BEG,
467 ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport),
468 (((tp->t_inpcb->inp_laddr.s_addr & 0xffff) << 16) |
469 (tp->t_inpcb->inp_faddr.s_addr & 0xffff)),
470 0,0,0);
91447636
A
471 }
472 if (dowakeup)
473 sorwakeup(so); /* done with socket lock held */
1c79356b 474 return (flags);
9bccf70c 475
1c79356b
A
476}
477
2d21ac55
A
478/*
479 * Reduce congestion window.
480 */
481static void
482tcp_reduce_congestion_window(
483 struct tcpcb *tp)
484{
485 u_int win;
486
487 win = min(tp->snd_wnd, tp->snd_cwnd) /
488 2 / tp->t_maxseg;
489 if (win < 2)
490 win = 2;
491 tp->snd_ssthresh = win * tp->t_maxseg;
492 ENTER_FASTRECOVERY(tp);
493 tp->snd_recover = tp->snd_max;
494 tp->t_timer[TCPT_REXMT] = 0;
495 tp->t_rtttime = 0;
496 tp->ecn_flags |= TE_SENDCWR;
497 tp->snd_cwnd = tp->snd_ssthresh +
498 tp->t_maxseg * tcprexmtthresh;
499}
500
9bccf70c 501
1c79356b
A
502/*
503 * TCP input routine, follows pages 65-76 of the
504 * protocol specification dated September, 1981 very closely.
505 */
506#if INET6
507int
55e303ae 508tcp6_input(mp, offp)
1c79356b 509 struct mbuf **mp;
55e303ae 510 int *offp;
1c79356b 511{
9bccf70c
A
512 register struct mbuf *m = *mp;
513 struct in6_ifaddr *ia6;
514
91447636 515 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), return IPPROTO_DONE);
9bccf70c
A
516
517 /*
518 * draft-itojun-ipv6-tcp-to-anycast
519 * better place to put this in?
520 */
521 ia6 = ip6_getdstifaddr(m);
522 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
523 struct ip6_hdr *ip6;
524
525 ip6 = mtod(m, struct ip6_hdr *);
526 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
527 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
528 return IPPROTO_DONE;
529 }
530
531 tcp_input(m, *offp);
1c79356b
A
532 return IPPROTO_DONE;
533}
534#endif
535
536void
9bccf70c 537tcp_input(m, off0)
1c79356b 538 struct mbuf *m;
9bccf70c 539 int off0;
1c79356b
A
540{
541 register struct tcphdr *th;
542 register struct ip *ip = NULL;
543 register struct ipovly *ipov;
544 register struct inpcb *inp;
545 u_char *optp = NULL;
546 int optlen = 0;
9bccf70c
A
547 int len, tlen, off;
548 int drop_hdrlen;
1c79356b
A
549 register struct tcpcb *tp = 0;
550 register int thflags;
551 struct socket *so = 0;
552 int todrop, acked, ourfinisacked, needoutput = 0;
553 struct in_addr laddr;
9bccf70c 554#if INET6
1c79356b
A
555 struct in6_addr laddr6;
556#endif
557 int dropsocket = 0;
558 int iss = 0;
91447636 559 int nosock = 0;
b0d623f7 560 u_int32_t tiwin;
1c79356b 561 struct tcpopt to; /* options in this segment */
91447636 562 struct sockaddr_in *next_hop = NULL;
1c79356b
A
563#if TCPDEBUG
564 short ostate = 0;
565#endif
91447636 566 struct m_tag *fwd_tag;
2d21ac55 567 u_char ip_ecn = IPTOS_ECN_NOTECT;
c910b4d9
A
568 unsigned int ifscope;
569
570 /*
571 * Record the interface where this segment arrived on; this does not
572 * affect normal data output (for non-detached TCP) as it provides a
573 * hint about which route and interface to use for sending in the
574 * absence of a PCB, when scoped routing (and thus source interface
575 * selection) are enabled.
576 */
577 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.rcvif != NULL)
578 ifscope = m->m_pkthdr.rcvif->if_index;
579 else
580 ifscope = IFSCOPE_NONE;
91447636
A
581
582 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
b0d623f7
A
583 if (!SLIST_EMPTY(&m->m_pkthdr.tags)) {
584 fwd_tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
585 KERNEL_TAG_TYPE_IPFORWARD, NULL);
586 } else {
587 fwd_tag = NULL;
588 }
91447636
A
589 if (fwd_tag != NULL) {
590 struct ip_fwd_tag *ipfwd_tag = (struct ip_fwd_tag *)(fwd_tag+1);
591
592 next_hop = ipfwd_tag->next_hop;
593 m_tag_delete(m, fwd_tag);
594 }
595
1c79356b
A
596#if INET6
597 struct ip6_hdr *ip6 = NULL;
9bccf70c 598 int isipv6;
1c79356b 599#endif /* INET6 */
9bccf70c 600 int rstreason; /* For badport_bandlim accounting purposes */
1c79356b 601 struct proc *proc0=current_proc();
9bccf70c 602
1c79356b
A
603 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_START,0,0,0,0,0);
604
9bccf70c
A
605#if INET6
606 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
607#endif
1c79356b
A
608 bzero((char *)&to, sizeof(to));
609
610 tcpstat.tcps_rcvtotal++;
1c79356b 611
1c79356b 612
1c79356b
A
613
614#if INET6
615 if (isipv6) {
9bccf70c 616 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
1c79356b 617 ip6 = mtod(m, struct ip6_hdr *);
9bccf70c
A
618 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
619 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
1c79356b 620 tcpstat.tcps_rcvbadsum++;
91447636 621 goto dropnosock;
1c79356b 622 }
9bccf70c
A
623 th = (struct tcphdr *)((caddr_t)ip6 + off0);
624
625 KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport),
626 (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])),
627 th->th_seq, th->th_ack, th->th_win);
1c79356b 628 /*
9bccf70c
A
629 * Be proactive about unspecified IPv6 address in source.
630 * As we use all-zero to indicate unbounded/unconnected pcb,
631 * unspecified IPv6 address can be used to confuse us.
632 *
633 * Note that packets with unspecified IPv6 destination is
634 * already dropped in ip6_input.
1c79356b 635 */
9bccf70c
A
636 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
637 /* XXX stat */
91447636 638 goto dropnosock;
1c79356b 639 }
9bccf70c
A
640 } else
641#endif /* INET6 */
ac5ea4a9 642 {
9bccf70c
A
643 /*
644 * Get IP and TCP header together in first mbuf.
645 * Note: IP leaves IP header in first mbuf.
646 */
647 if (off0 > sizeof (struct ip)) {
648 ip_stripoptions(m, (struct mbuf *)0);
649 off0 = sizeof(struct ip);
650 if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
651 m->m_pkthdr.csum_flags = 0; /* invalidate hwcksuming */
652
653 }
654 if (m->m_len < sizeof (struct tcpiphdr)) {
655 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
656 tcpstat.tcps_rcvshort++;
657 return;
0b4e3aa0 658 }
9bccf70c
A
659 }
660 ip = mtod(m, struct ip *);
661 ipov = (struct ipovly *)ip;
662 th = (struct tcphdr *)((caddr_t)ip + off0);
663 tlen = ip->ip_len;
0b4e3aa0 664
9bccf70c
A
665 KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport),
666 (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)),
667 th->th_seq, th->th_ack, th->th_win);
668
669 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
2d21ac55 670 if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) {
9bccf70c 671 u_short pseudo;
ac5ea4a9
A
672 char b[9];
673 *(uint32_t*)&b[0] = *(uint32_t*)&ipov->ih_x1[0];
674 *(uint32_t*)&b[4] = *(uint32_t*)&ipov->ih_x1[4];
675 *(uint8_t*)&b[8] = *(uint8_t*)&ipov->ih_x1[8];
676
9bccf70c
A
677 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
678 ipov->ih_len = (u_short)tlen;
b0d623f7
A
679
680#if BYTE_ORDER != BIG_ENDIAN
9bccf70c 681 HTONS(ipov->ih_len);
b0d623f7
A
682#endif
683
9bccf70c 684 pseudo = in_cksum(m, sizeof (struct ip));
ac5ea4a9
A
685
686 *(uint32_t*)&ipov->ih_x1[0] = *(uint32_t*)&b[0];
687 *(uint32_t*)&ipov->ih_x1[4] = *(uint32_t*)&b[4];
688 *(uint8_t*)&ipov->ih_x1[8] = *(uint8_t*)&b[8];
689
9bccf70c
A
690 th->th_sum = in_addword(pseudo, (m->m_pkthdr.csum_data & 0xFFFF));
691 } else {
692 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
693 th->th_sum = m->m_pkthdr.csum_data;
694 else
695 th->th_sum = in_pseudo(ip->ip_src.s_addr,
696 ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data +
697 ip->ip_len + IPPROTO_TCP));
1c79356b 698 }
9bccf70c
A
699 th->th_sum ^= 0xffff;
700 } else {
ac5ea4a9 701 char b[9];
9bccf70c
A
702 /*
703 * Checksum extended TCP header and data.
704 */
ac5ea4a9
A
705 *(uint32_t*)&b[0] = *(uint32_t*)&ipov->ih_x1[0];
706 *(uint32_t*)&b[4] = *(uint32_t*)&ipov->ih_x1[4];
707 *(uint8_t*)&b[8] = *(uint8_t*)&ipov->ih_x1[8];
708
9bccf70c
A
709 len = sizeof (struct ip) + tlen;
710 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
711 ipov->ih_len = (u_short)tlen;
b0d623f7
A
712
713#if BYTE_ORDER != BIG_ENDIAN
9bccf70c 714 HTONS(ipov->ih_len);
b0d623f7
A
715#endif
716
9bccf70c 717 th->th_sum = in_cksum(m, len);
ac5ea4a9
A
718
719 *(uint32_t*)&ipov->ih_x1[0] = *(uint32_t*)&b[0];
720 *(uint32_t*)&ipov->ih_x1[4] = *(uint32_t*)&b[4];
721 *(uint8_t*)&ipov->ih_x1[8] = *(uint8_t*)&b[8];
2d21ac55
A
722
723 tcp_in_cksum_stats(len);
9bccf70c
A
724 }
725 if (th->th_sum) {
726 tcpstat.tcps_rcvbadsum++;
91447636 727 goto dropnosock;
1c79356b 728 }
9bccf70c
A
729#if INET6
730 /* Re-initialization for later version check */
731 ip->ip_v = IPVERSION;
732#endif
2d21ac55 733 ip_ecn = (ip->ip_tos & IPTOS_ECN_MASK);
ac5ea4a9 734 }
1c79356b
A
735
736 /*
737 * Check that TCP offset makes sense,
738 * pull out TCP options and adjust length. XXX
739 */
9bccf70c
A
740 off = th->th_off << 2;
741 if (off < sizeof (struct tcphdr) || off > tlen) {
1c79356b 742 tcpstat.tcps_rcvbadoff++;
91447636 743 goto dropnosock;
1c79356b 744 }
9bccf70c
A
745 tlen -= off; /* tlen is used instead of ti->ti_len */
746 if (off > sizeof (struct tcphdr)) {
1c79356b
A
747#if INET6
748 if (isipv6) {
91447636 749 IP6_EXTHDR_CHECK(m, off0, off, return);
1c79356b 750 ip6 = mtod(m, struct ip6_hdr *);
9bccf70c 751 th = (struct tcphdr *)((caddr_t)ip6 + off0);
1c79356b
A
752 } else
753#endif /* INET6 */
2d21ac55
A
754 {
755 if (m->m_len < sizeof(struct ip) + off) {
756 if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
757 tcpstat.tcps_rcvshort++;
758 return;
759 }
760 ip = mtod(m, struct ip *);
761 ipov = (struct ipovly *)ip;
762 th = (struct tcphdr *)((caddr_t)ip + off0);
1c79356b
A
763 }
764 }
9bccf70c 765 optlen = off - sizeof (struct tcphdr);
1c79356b
A
766 optp = (u_char *)(th + 1);
767 /*
768 * Do quick retrieval of timestamp options ("options
769 * prediction?"). If timestamp is the only option and it's
770 * formatted as recommended in RFC 1323 appendix A, we
771 * quickly get the values now and not bother calling
772 * tcp_dooptions(), etc.
773 */
774 if ((optlen == TCPOLEN_TSTAMP_APPA ||
2d21ac55 775 (optlen > TCPOLEN_TSTAMP_APPA &&
1c79356b 776 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
2d21ac55
A
777 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
778 (th->th_flags & TH_SYN) == 0) {
8ad349bb 779 to.to_flags |= TOF_TS;
1c79356b
A
780 to.to_tsval = ntohl(*(u_int32_t *)(optp + 4));
781 to.to_tsecr = ntohl(*(u_int32_t *)(optp + 8));
782 optp = NULL; /* we've parsed the options */
783 }
784 }
785 thflags = th->th_flags;
786
9bccf70c
A
787#if TCP_DROP_SYNFIN
788 /*
789 * If the drop_synfin option is enabled, drop all packets with
790 * both the SYN and FIN bits set. This prevents e.g. nmap from
791 * identifying the TCP/IP stack.
792 *
8ad349bb 793 * This is a violation of the TCP specification.
9bccf70c
A
794 */
795 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
91447636 796 goto dropnosock;
9bccf70c
A
797#endif
798
1c79356b
A
799 /*
800 * Convert TCP protocol specific fields to host format.
801 */
b0d623f7
A
802
803#if BYTE_ORDER != BIG_ENDIAN
1c79356b
A
804 NTOHL(th->th_seq);
805 NTOHL(th->th_ack);
806 NTOHS(th->th_win);
807 NTOHS(th->th_urp);
b0d623f7 808#endif
1c79356b
A
809
810 /*
8ad349bb 811 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
9bccf70c
A
812 * until after ip6_savecontrol() is called and before other functions
813 * which don't want those proto headers.
814 * Because ip6_savecontrol() is going to parse the mbuf to
815 * search for data to be passed up to user-land, it wants mbuf
816 * parameters to be unchanged.
1c79356b 817 */
9bccf70c 818 drop_hdrlen = off0 + off;
1c79356b
A
819
820 /*
821 * Locate pcb for segment.
822 */
823findpcb:
824#if IPFIREWALL_FORWARD
91447636 825 if (next_hop != NULL
1c79356b 826#if INET6
2d21ac55 827 && isipv6 == 0 /* IPv6 support is not yet */
1c79356b
A
828#endif /* INET6 */
829 ) {
830 /*
831 * Diverted. Pretend to be the destination.
832 * already got one like this?
833 */
834 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
835 ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
836 if (!inp) {
837 /*
838 * No, then it's new. Try find the ambushing socket
839 */
91447636 840 if (!next_hop->sin_port) {
1c79356b 841 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
91447636 842 th->th_sport, next_hop->sin_addr,
1c79356b
A
843 th->th_dport, 1, m->m_pkthdr.rcvif);
844 } else {
845 inp = in_pcblookup_hash(&tcbinfo,
846 ip->ip_src, th->th_sport,
91447636
A
847 next_hop->sin_addr,
848 ntohs(next_hop->sin_port), 1,
1c79356b
A
849 m->m_pkthdr.rcvif);
850 }
851 }
1c79356b
A
852 } else
853#endif /* IPFIREWALL_FORWARD */
9bccf70c 854 {
1c79356b
A
855#if INET6
856 if (isipv6)
857 inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
858 &ip6->ip6_dst, th->th_dport, 1,
859 m->m_pkthdr.rcvif);
860 else
861#endif /* INET6 */
862 inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
863 ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
9bccf70c 864 }
1c79356b 865
c910b4d9
A
866 /*
867 * Use the interface scope information from the PCB for outbound
868 * segments. If the PCB isn't present and if scoped routing is
869 * enabled, tcp_respond will use the scope of the interface where
870 * the segment arrived on.
871 */
872 if (inp != NULL && (inp->inp_flags & INP_BOUND_IF))
873 ifscope = inp->inp_boundif;
1c79356b 874#if IPSEC
91447636 875 if (ipsec_bypass == 0) {
1c79356b 876#if INET6
91447636
A
877 if (isipv6) {
878 if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
2d21ac55 879 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
b0d623f7
A
880 if (in_pcb_checkstate(inp, WNT_RELEASE, 0) == WNT_STOPUSING)
881 inp = NULL; // pretend we didn't find it
91447636
A
882 goto dropnosock;
883 }
884 } else
1c79356b 885#endif /* INET6 */
91447636 886 if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
b0d623f7
A
887 IPSEC_STAT_INCREMENT(ipsecstat.in_polvio);
888 if (in_pcb_checkstate(inp, WNT_RELEASE, 0) == WNT_STOPUSING)
889 inp = NULL; // pretend we didn't find it
91447636
A
890 goto dropnosock;
891 }
1c79356b 892 }
1c79356b
A
893#endif /*IPSEC*/
894
895 /*
896 * If the state is CLOSED (i.e., TCB does not exist) then
897 * all data in the incoming segment is discarded.
898 * If the TCB exists but is in CLOSED state, it is embryonic,
899 * but should either do a listen or a connect soon.
900 */
901 if (inp == NULL) {
9bccf70c 902 if (log_in_vain) {
1c79356b 903#if INET6
91447636 904 char dbuf[MAX_IPv6_STR_LEN], sbuf[MAX_IPv6_STR_LEN];
1c79356b 905#else /* INET6 */
91447636 906 char dbuf[MAX_IPv4_STR_LEN], sbuf[MAX_IPv4_STR_LEN];
1c79356b
A
907#endif /* INET6 */
908
909#if INET6
910 if (isipv6) {
91447636
A
911 inet_ntop(AF_INET6, &ip6->ip6_dst, dbuf, sizeof(dbuf));
912 inet_ntop(AF_INET6, &ip6->ip6_src, sbuf, sizeof(sbuf));
9bccf70c 913 } else
1c79356b 914#endif
91447636
A
915 {
916 inet_ntop(AF_INET, &ip->ip_dst, dbuf, sizeof(dbuf));
917 inet_ntop(AF_INET, &ip->ip_src, sbuf, sizeof(sbuf));
918 }
9bccf70c
A
919 switch (log_in_vain) {
920 case 1:
921 if(thflags & TH_SYN)
922 log(LOG_INFO,
91447636
A
923 "Connection attempt to TCP %s:%d from %s:%d\n",
924 dbuf, ntohs(th->th_dport),
925 sbuf,
926 ntohs(th->th_sport));
9bccf70c
A
927 break;
928 case 2:
929 log(LOG_INFO,
91447636
A
930 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
931 dbuf, ntohs(th->th_dport), sbuf,
932 ntohs(th->th_sport), thflags);
933 break;
934 case 3:
935 if ((thflags & TH_SYN) &&
936 !(m->m_flags & (M_BCAST | M_MCAST)) &&
937#if INET6
938 ((isipv6 && !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) ||
939 (!isipv6 && ip->ip_dst.s_addr != ip->ip_src.s_addr))
940#else
941 ip->ip_dst.s_addr != ip->ip_src.s_addr
942#endif
943 )
944 log_in_vain_log((LOG_INFO,
945 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
946 dbuf, ntohs(th->th_dport),
947 sbuf,
948 ntohs(th->th_sport)));
9bccf70c
A
949 break;
950 default:
951 break;
1c79356b 952 }
1c79356b 953 }
9bccf70c 954 if (blackhole) {
91447636
A
955 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type != IFT_LOOP)
956 switch (blackhole) {
957 case 1:
958 if (thflags & TH_SYN)
959 goto dropnosock;
960 break;
961 case 2:
962 goto dropnosock;
963 default:
964 goto dropnosock;
965 }
9bccf70c
A
966 }
967 rstreason = BANDLIM_RST_CLOSEDPORT;
91447636
A
968 goto dropwithresetnosock;
969 }
970 so = inp->inp_socket;
971 if (so == NULL) {
b0d623f7
A
972 /* This case shouldn't happen as the socket shouldn't be null
973 * if inp_state isn't set to INPCB_STATE_DEAD
974 * But just in case, we pretend we didn't find the socket if we hit this case
975 * as this isn't cause for a panic (the socket might be leaked however)...
976 */
977 inp = NULL;
91447636 978#if TEMPDEBUG
b0d623f7 979 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp);
91447636
A
980#endif
981 goto dropnosock;
1c79356b 982 }
8ad349bb 983
b0d623f7 984 tcp_lock(so, 1, (void *)2);
91447636 985 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
b0d623f7 986 tcp_unlock(so, 1, (void *)2);
91447636
A
987 inp = NULL; // pretend we didn't find it
988 goto dropnosock;
989 }
990
1c79356b 991 tp = intotcpcb(inp);
9bccf70c
A
992 if (tp == 0) {
993 rstreason = BANDLIM_RST_CLOSEDPORT;
1c79356b 994 goto dropwithreset;
9bccf70c 995 }
1c79356b
A
996 if (tp->t_state == TCPS_CLOSED)
997 goto drop;
9bccf70c 998
1c79356b
A
999 /* Unscale the window into a 32-bit value. */
1000 if ((thflags & TH_SYN) == 0)
1001 tiwin = th->th_win << tp->snd_scale;
1002 else
1003 tiwin = th->th_win;
1004
2d21ac55
A
1005#if CONFIG_MACF_NET
1006 if (mac_inpcb_check_deliver(inp, m, AF_INET, SOCK_STREAM))
1007 goto drop;
1008#endif
1009
1c79356b
A
1010 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
1011#if TCPDEBUG
1012 if (so->so_options & SO_DEBUG) {
1013 ostate = tp->t_state;
1014#if INET6
1015 if (isipv6)
9bccf70c
A
1016 bcopy((char *)ip6, (char *)tcp_saveipgen,
1017 sizeof(*ip6));
1c79356b 1018 else
1c79356b 1019#endif /* INET6 */
9bccf70c 1020 bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1c79356b
A
1021 tcp_savetcp = *th;
1022 }
1023#endif
1024 if (so->so_options & SO_ACCEPTCONN) {
91447636 1025 register struct tcpcb *tp0 = tp;
1c79356b 1026 struct socket *so2;
1c79356b 1027 struct socket *oso;
91447636 1028 struct sockaddr_storage from;
1c79356b
A
1029#if INET6
1030 struct inpcb *oinp = sotoinpcb(so);
1031#endif /* INET6 */
c910b4d9
A
1032 unsigned int head_ifscope;
1033
1034 /* Get listener's bound-to-interface, if any */
1035 head_ifscope = (inp->inp_flags & INP_BOUND_IF) ?
1036 inp->inp_boundif : IFSCOPE_NONE;
1c79356b 1037
9bccf70c 1038 /*
7e4a7d39
A
1039 * If the state is LISTEN then ignore segment if it contains an RST.
1040 * If the segment contains an ACK then it is bad and send a RST.
1041 * If it does not contain a SYN then it is not interesting; drop it.
1042 * If it is from this socket, drop it, it must be forged.
9bccf70c 1043 */
1c79356b 1044 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
7e4a7d39
A
1045 if (thflags & TH_RST) {
1046 goto drop;
1047 }
1c79356b 1048 if (thflags & TH_ACK) {
7e4a7d39 1049 tp = NULL;
1c79356b 1050 tcpstat.tcps_badsyn++;
9bccf70c 1051 rstreason = BANDLIM_RST_OPENPORT;
1c79356b
A
1052 goto dropwithreset;
1053 }
7e4a7d39
A
1054
1055 /* We come here if there is no SYN set */
1056 tcpstat.tcps_badsyn++;
1c79356b
A
1057 goto drop;
1058 }
1c79356b 1059 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_START,0,0,0,0,0);
7e4a7d39
A
1060 if (th->th_dport == th->th_sport) {
1061#if INET6
1062 if (isipv6) {
1063 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1064 &ip6->ip6_src))
1065 goto drop;
1066 } else
1067#endif /* INET6 */
1068 if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
1069 goto drop;
1070 }
1071 /*
1072 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1073 * in_broadcast() should never return true on a received
1074 * packet with M_BCAST not set.
1075 *
1076 * Packets with a multicast source address should also
1077 * be discarded.
1078 */
1079 if (m->m_flags & (M_BCAST|M_MCAST))
1080 goto drop;
1081#if INET6
1082 if (isipv6) {
1083 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1084 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
1085 goto drop;
1086 } else
1087#endif
1088 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1089 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
1090 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1091 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
1092 goto drop;
1093
1c79356b 1094
9bccf70c
A
1095#if INET6
1096 /*
1097 * If deprecated address is forbidden,
1098 * we do not accept SYN to deprecated interface
1099 * address to prevent any new inbound connection from
1100 * getting established.
1101 * When we do not accept SYN, we send a TCP RST,
1102 * with deprecated source address (instead of dropping
1103 * it). We compromise it as it is much better for peer
1104 * to send a RST, and RST will be the final packet
1105 * for the exchange.
1106 *
1107 * If we do not forbid deprecated addresses, we accept
1108 * the SYN packet. RFC2462 does not suggest dropping
1109 * SYN in this case.
1110 * If we decipher RFC2462 5.5.4, it says like this:
1111 * 1. use of deprecated addr with existing
1112 * communication is okay - "SHOULD continue to be
1113 * used"
1114 * 2. use of it with new communication:
1115 * (2a) "SHOULD NOT be used if alternate address
1116 * with sufficient scope is available"
1117 * (2b) nothing mentioned otherwise.
1118 * Here we fall into (2b) case as we have no choice in
1119 * our source address selection - we must obey the peer.
1120 *
1121 * The wording in RFC2462 is confusing, and there are
1122 * multiple description text for deprecated address
1123 * handling - worse, they are not exactly the same.
1124 * I believe 5.5.4 is the best one, so we follow 5.5.4.
1125 */
1126 if (isipv6 && !ip6_use_deprecated) {
1127 struct in6_ifaddr *ia6;
1128
1129 if ((ia6 = ip6_getdstifaddr(m)) &&
1130 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1131 tp = NULL;
1132 rstreason = BANDLIM_RST_OPENPORT;
1133 goto dropwithreset;
1134 }
1135 }
1136#endif
91447636 1137 if (so->so_filt) {
2d21ac55 1138#if INET6
91447636
A
1139 if (isipv6) {
1140 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&from;
1141
1142 sin6->sin6_len = sizeof(*sin6);
1143 sin6->sin6_family = AF_INET6;
1144 sin6->sin6_port = th->th_sport;
1145 sin6->sin6_flowinfo = 0;
1146 sin6->sin6_addr = ip6->ip6_src;
1147 sin6->sin6_scope_id = 0;
2d21ac55
A
1148 }
1149 else
1150#endif
1151 {
91447636
A
1152 struct sockaddr_in *sin = (struct sockaddr_in*)&from;
1153
1154 sin->sin_len = sizeof(*sin);
1155 sin->sin_family = AF_INET;
1156 sin->sin_port = th->th_sport;
1157 sin->sin_addr = ip->ip_src;
1158 }
1159 so2 = sonewconn(so, 0, (struct sockaddr*)&from);
1160 } else {
1161 so2 = sonewconn(so, 0, NULL);
1162 }
1c79356b
A
1163 if (so2 == 0) {
1164 tcpstat.tcps_listendrop++;
2d21ac55 1165 if (tcp_dropdropablreq(so)) {
91447636
A
1166 if (so->so_filt)
1167 so2 = sonewconn(so, 0, (struct sockaddr*)&from);
1168 else
1169 so2 = sonewconn(so, 0, NULL);
1c79356b 1170 }
91447636 1171 if (!so2)
1c79356b
A
1172 goto drop;
1173 }
b0d623f7
A
1174
1175 /* Point "inp" and "tp" in tandem to new socket */
1176 inp = (struct inpcb *)so2->so_pcb;
1177 tp = intotcpcb(inp);
91447636 1178
1c79356b 1179 oso = so;
91447636
A
1180 tcp_unlock(so, 0, 0); /* Unlock but keep a reference on listener for now */
1181
1c79356b 1182 so = so2;
91447636 1183 tcp_lock(so, 1, 0);
1c79356b 1184 /*
1c79356b
A
1185 * Mark socket as temporary until we're
1186 * committed to keeping it. The code at
1187 * ``drop'' and ``dropwithreset'' check the
1188 * flag dropsocket to see if the temporary
1189 * socket created here should be discarded.
1190 * We mark the socket as discardable until
1191 * we're committed to it below in TCPS_LISTEN.
7e4a7d39
A
1192 * There are some error conditions in which we
1193 * have to drop the temporary socket.
1c79356b
A
1194 */
1195 dropsocket++;
c910b4d9
A
1196 /*
1197 * Inherit INP_BOUND_IF from listener; testing if
1198 * head_ifscope is non-zero is sufficient, since it
1199 * can only be set to a non-zero value earlier if
1200 * the listener has such a flag set.
1201 */
1202#if INET6
1203 if (head_ifscope != IFSCOPE_NONE && !isipv6) {
1204#else
1205 if (head_ifscope != IFSCOPE_NONE) {
1206#endif /* INET6 */
1207 inp->inp_flags |= INP_BOUND_IF;
1208 inp->inp_boundif = head_ifscope;
1209 }
1c79356b
A
1210#if INET6
1211 if (isipv6)
1212 inp->in6p_laddr = ip6->ip6_dst;
1213 else {
9bccf70c
A
1214 inp->inp_vflag &= ~INP_IPV6;
1215 inp->inp_vflag |= INP_IPV4;
1c79356b 1216#endif /* INET6 */
7e4a7d39 1217 inp->inp_laddr = ip->ip_dst;
1c79356b
A
1218#if INET6
1219 }
1220#endif /* INET6 */
1c79356b 1221 inp->inp_lport = th->th_dport;
91447636 1222 if (in_pcbinshash(inp, 0) != 0) {
1c79356b 1223 /*
9bccf70c
A
1224 * Undo the assignments above if we failed to
1225 * put the PCB on the hash lists.
1c79356b
A
1226 */
1227#if INET6
1228 if (isipv6)
1229 inp->in6p_laddr = in6addr_any;
1230 else
1231#endif /* INET6 */
1232 inp->inp_laddr.s_addr = INADDR_ANY;
1233 inp->inp_lport = 0;
91447636
A
1234 tcp_lock(oso, 0, 0); /* release ref on parent */
1235 tcp_unlock(oso, 1, 0);
1c79356b
A
1236 goto drop;
1237 }
1c79356b
A
1238#if INET6
1239 if (isipv6) {
9bccf70c
A
1240 /*
1241 * Inherit socket options from the listening
1242 * socket.
1243 * Note that in6p_inputopts are not (even
1244 * should not be) copied, since it stores
1c79356b 1245 * previously received options and is used to
9bccf70c
A
1246 * detect if each new option is different than
1247 * the previous one and hence should be passed
1248 * to a user.
1249 * If we copied in6p_inputopts, a user would
1250 * not be able to receive options just after
1251 * calling the accept system call.
1252 */
1c79356b
A
1253 inp->inp_flags |=
1254 oinp->inp_flags & INP_CONTROLOPTS;
9bccf70c
A
1255 if (oinp->in6p_outputopts)
1256 inp->in6p_outputopts =
1257 ip6_copypktopts(oinp->in6p_outputopts,
1258 M_NOWAIT);
1c79356b
A
1259 } else
1260#endif /* INET6 */
1261 inp->inp_options = ip_srcroute();
91447636 1262 tcp_lock(oso, 0, 0);
1c79356b
A
1263#if IPSEC
1264 /* copy old policy into new socket's */
9bccf70c
A
1265 if (sotoinpcb(oso)->inp_sp)
1266 {
1267 int error = 0;
1268 /* Is it a security hole here to silently fail to copy the policy? */
1269 if (inp->inp_sp != NULL)
1270 error = ipsec_init_policy(so, &inp->inp_sp);
1271 if (error != 0 || ipsec_copy_policy(sotoinpcb(oso)->inp_sp, inp->inp_sp))
1272 printf("tcp_input: could not copy policy\n");
1273 }
1c79356b 1274#endif
b0d623f7 1275 /* inherit states from the listener */
1c79356b 1276 tp->t_state = TCPS_LISTEN;
9bccf70c 1277 tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT|TF_NODELAY);
b0d623f7 1278 tp->t_keepinit = tp0->t_keepinit;
91447636 1279 tp->t_inpcb->inp_ip_ttl = tp0->t_inpcb->inp_ip_ttl;
b0d623f7
A
1280
1281 /* now drop the reference on the listener */
1282 tcp_unlock(oso, 1, 0);
1283
1c79356b 1284 /* Compute proper scaling value from buffer space */
2d21ac55
A
1285 if (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold) {
1286 tp->request_r_scale = max(tcp_win_scale, tp->request_r_scale);
b0d623f7 1287 so->so_rcv.sb_hiwat = imin(TCP_MAXWIN << tp->request_r_scale, (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES);
2d21ac55
A
1288 }
1289 else {
1290 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1291 TCP_MAXWIN << tp->request_r_scale <
1292 so->so_rcv.sb_hiwat)
1293 tp->request_r_scale++;
1294 }
1c79356b
A
1295
1296 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_END,0,0,0,0,0);
1297 }
1298 }
1299
91447636
A
1300#if 1
1301 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1302#endif
1303 /*
1304 * Radar 3529618
1305 * This is the second part of the MSS DoS prevention code (after
1306 * minmss on the sending side) and it deals with too many too small
1307 * tcp packets in a too short timeframe (1 second).
1308 *
1309 * For every full second we count the number of received packets
1310 * and bytes. If we get a lot of packets per second for this connection
1311 * (tcp_minmssoverload) we take a closer look at it and compute the
1312 * average packet size for the past second. If that is less than
1313 * tcp_minmss we get too many packets with very small payload which
1314 * is not good and burdens our system (and every packet generates
1315 * a wakeup to the process connected to our socket). We can reasonable
1316 * expect this to be small packet DoS attack to exhaust our CPU
1317 * cycles.
1318 *
1319 * Care has to be taken for the minimum packet overload value. This
1320 * value defines the minimum number of packets per second before we
1321 * start to worry. This must not be too low to avoid killing for
1322 * example interactive connections with many small packets like
1323 * telnet or SSH.
1324 *
2d21ac55
A
1325 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
1326 * this check.
91447636
A
1327 *
1328 * Account for packet if payload packet, skip over ACK, etc.
743b1565
A
1329 *
1330 * The packet per second count is done all the time and is also used
1331 * by "DELAY_ACK" to detect streaming situations.
1332 *
91447636 1333 */
743b1565 1334 if (tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
91447636
A
1335 if (tp->rcv_reset > tcp_now) {
1336 tp->rcv_pps++;
1337 tp->rcv_byps += tlen + off;
2d21ac55
A
1338 if (tp->rcv_byps > tp->rcv_maxbyps)
1339 tp->rcv_maxbyps = tp->rcv_byps;
743b1565
A
1340 /*
1341 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
1342 * the check.
1343 */
1344 if (tcp_minmss && tcp_minmssoverload && tp->rcv_pps > tcp_minmssoverload) {
91447636
A
1345 if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
1346 char ipstrbuf[MAX_IPv6_STR_LEN];
1347 printf("too many small tcp packets from "
b0d623f7 1348 "%s:%u, av. %ubyte/packet, "
91447636 1349 "dropping connection\n",
2d21ac55 1350#if INET6
91447636
A
1351 isipv6 ?
1352 inet_ntop(AF_INET6, &inp->in6p_faddr, ipstrbuf,
1353 sizeof(ipstrbuf)) :
1354#endif
1355 inet_ntop(AF_INET, &inp->inp_faddr, ipstrbuf,
1356 sizeof(ipstrbuf)),
1357 inp->inp_fport,
1358 tp->rcv_byps / tp->rcv_pps);
1359 tp = tcp_drop(tp, ECONNRESET);
1360/* tcpstat.tcps_minmssdrops++; */
1361 goto drop;
1362 }
1363 }
1364 } else {
2d21ac55 1365 tp->rcv_reset = tcp_now + TCP_RETRANSHZ;
91447636
A
1366 tp->rcv_pps = 1;
1367 tp->rcv_byps = tlen + off;
1368 }
1369 }
1370
2d21ac55
A
1371#if TRAFFIC_MGT
1372 if (so->so_traffic_mgt_flags & TRAFFIC_MGT_SO_BACKGROUND) {
1373 tcpstat.tcps_bg_rcvtotal++;
1374
1375 /* Take snapshots of pkts recv;
1376 * tcpcb should have been initialized to 0 when allocated,
1377 * so if 0 then this is the first time we're doing this
1378 */
1379 if (!tp->tot_recv_snapshot) {
1380 tp->tot_recv_snapshot = tcpstat.tcps_rcvtotal;
1381 }
1382 if (!tp->bg_recv_snapshot) {
1383 tp->bg_recv_snapshot = tcpstat.tcps_bg_rcvtotal;
1384 }
1385 }
1386#endif /* TRAFFIC_MGT */
1387
1388 /*
1389 Explicit Congestion Notification - Flag that we need to send ECT if
1390 + The IP Congestion experienced flag was set.
1391 + Socket is in established state
1392 + We negotiated ECN in the TCP setup
1393 + This isn't a pure ack (tlen > 0)
1394 + The data is in the valid window
1395
1396 TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
1397 */
1398 if (ip_ecn == IPTOS_ECN_CE && tp->t_state == TCPS_ESTABLISHED &&
1399 (tp->ecn_flags & (TE_SETUPSENT | TE_SETUPRECEIVED)) ==
1400 (TE_SETUPSENT | TE_SETUPRECEIVED) && tlen > 0 &&
1401 SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1402 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
1403 tp->ecn_flags |= TE_SENDECE;
1404 }
1405
1406 /*
1407 Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
1408 bother doing extensive checks for state and whatnot.
1409 */
1410 if ((thflags & TH_CWR) == TH_CWR) {
1411 tp->ecn_flags &= ~TE_SENDECE;
1412 }
1413
1c79356b
A
1414 /*
1415 * Segment received on connection.
1416 * Reset idle time and keep-alive timer.
1417 */
9bccf70c 1418 tp->t_rcvtime = 0;
1c79356b 1419 if (TCPS_HAVEESTABLISHED(tp->t_state))
55e303ae 1420 tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp);
1c79356b
A
1421
1422 /*
1423 * Process options if not in LISTEN state,
1424 * else do it below (after getting remote address).
1425 */
1426 if (tp->t_state != TCPS_LISTEN && optp)
c910b4d9 1427 tcp_dooptions(tp, optp, optlen, th, &to, ifscope);
1c79356b 1428
8ad349bb
A
1429 if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1430 if (to.to_flags & TOF_SCALE) {
1431 tp->t_flags |= TF_RCVD_SCALE;
1432 tp->requested_s_scale = to.to_requested_s_scale;
2d21ac55
A
1433 tp->snd_wnd = th->th_win << tp->snd_scale;
1434 tiwin = tp->snd_wnd;
8ad349bb
A
1435 }
1436 if (to.to_flags & TOF_TS) {
1437 tp->t_flags |= TF_RCVD_TSTMP;
1438 tp->ts_recent = to.to_tsval;
1439 tp->ts_recent_age = tcp_now;
1440 }
1441 if (to.to_flags & TOF_MSS)
c910b4d9 1442 tcp_mss(tp, to.to_mss, ifscope);
8ad349bb
A
1443 if (tp->sack_enable) {
1444 if (!(to.to_flags & TOF_SACK))
1445 tp->sack_enable = 0;
1446 else
1447 tp->t_flags |= TF_SACK_PERMIT;
1448 }
1449 }
1450
1c79356b
A
1451 /*
1452 * Header prediction: check for the two common cases
1453 * of a uni-directional data xfer. If the packet has
1454 * no control flags, is in-sequence, the window didn't
1455 * change and we're not retransmitting, it's a
1456 * candidate. If the length is zero and the ack moved
1457 * forward, we're the sender side of the xfer. Just
1458 * free the data acked & wake any higher level process
1459 * that was blocked waiting for space. If the length
1460 * is non-zero and the ack didn't move, we're the
1461 * receiver side. If we're getting packets in-order
1462 * (the reassembly queue is empty), add the data to
1463 * the socket buffer and note that we need a delayed ack.
1464 * Make sure that the hidden state-flags are also off.
1465 * Since we check for TCPS_ESTABLISHED above, it can only
1466 * be TH_NEEDSYN.
1467 */
1468 if (tp->t_state == TCPS_ESTABLISHED &&
2d21ac55 1469 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK|TH_ECE)) == TH_ACK &&
1c79356b 1470 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
8ad349bb 1471 ((to.to_flags & TOF_TS) == 0 ||
1c79356b 1472 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1c79356b
A
1473 th->th_seq == tp->rcv_nxt &&
1474 tiwin && tiwin == tp->snd_wnd &&
1475 tp->snd_nxt == tp->snd_max) {
1476
1477 /*
1478 * If last ACK falls within this segment's sequence numbers,
1479 * record the timestamp.
1480 * NOTE that the test is modified according to the latest
1481 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1482 */
8ad349bb 1483 if ((to.to_flags & TOF_TS) != 0 &&
1c79356b
A
1484 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1485 tp->ts_recent_age = tcp_now;
1486 tp->ts_recent = to.to_tsval;
1487 }
1488
c910b4d9
A
1489 /* Force acknowledgment if we received a FIN */
1490
1491 if (thflags & TH_FIN)
1492 tp->t_flags |= TF_ACKNOW;
1493
9bccf70c 1494 if (tlen == 0) {
1c79356b
A
1495 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1496 SEQ_LEQ(th->th_ack, tp->snd_max) &&
2d21ac55 1497 tp->snd_cwnd >= tp->snd_ssthresh &&
8ad349bb
A
1498 ((!tcp_do_newreno && !tp->sack_enable &&
1499 tp->t_dupacks < tcprexmtthresh) ||
1500 ((tcp_do_newreno || tp->sack_enable) &&
1501 !IN_FASTRECOVERY(tp) && to.to_nsacks == 0 &&
1502 TAILQ_EMPTY(&tp->snd_holes)))) {
1c79356b
A
1503 /*
1504 * this is a pure ack for outstanding data.
1505 */
1506 ++tcpstat.tcps_predack;
9bccf70c
A
1507 /*
1508 * "bad retransmit" recovery
1509 */
1510 if (tp->t_rxtshift == 1 &&
2d21ac55
A
1511 tcp_now < tp->t_badrxtwin) {
1512 ++tcpstat.tcps_sndrexmitbad;
9bccf70c
A
1513 tp->snd_cwnd = tp->snd_cwnd_prev;
1514 tp->snd_ssthresh =
1515 tp->snd_ssthresh_prev;
8ad349bb
A
1516 tp->snd_recover = tp->snd_recover_prev;
1517 if (tp->t_flags & TF_WASFRECOVERY)
1518 ENTER_FASTRECOVERY(tp);
9bccf70c
A
1519 tp->snd_nxt = tp->snd_max;
1520 tp->t_badrxtwin = 0;
1521 }
2d21ac55
A
1522 /*
1523 * Recalculate the transmit timer / rtt.
1524 *
1525 * Some boxes send broken timestamp replies
1526 * during the SYN+ACK phase, ignore
1527 * timestamps of 0 or we could calculate a
1528 * huge RTT and blow up the retransmit timer.
1529 */
1530 if (((to.to_flags & TOF_TS) != 0) && (to.to_tsecr != 0)) { /* Makes sure we already have a TS */
1531 if (!tp->t_rttlow ||
1532 tp->t_rttlow > tcp_now - to.to_tsecr)
1533 tp->t_rttlow = tcp_now - to.to_tsecr;
1c79356b 1534 tcp_xmit_timer(tp,
2d21ac55
A
1535 tcp_now - to.to_tsecr);
1536 } else if (tp->t_rtttime &&
1537 SEQ_GT(th->th_ack, tp->t_rtseq)) {
1538 if (!tp->t_rttlow ||
1539 tp->t_rttlow > tcp_now - tp->t_rtttime)
1540 tp->t_rttlow = tcp_now - tp->t_rtttime;
9bccf70c 1541 tcp_xmit_timer(tp, tp->t_rtttime);
2d21ac55 1542 }
1c79356b
A
1543 acked = th->th_ack - tp->snd_una;
1544 tcpstat.tcps_rcvackpack++;
1545 tcpstat.tcps_rcvackbyte += acked;
2d21ac55
A
1546 /*
1547 * Grow the congestion window, if the
1548 * connection is cwnd bound.
1549 */
cf7d32b8
A
1550 if (tp->snd_cwnd < tp->snd_wnd) {
1551 tp->t_bytes_acked += acked;
1552 if (tp->t_bytes_acked > tp->snd_cwnd) {
1553 tp->t_bytes_acked -= tp->snd_cwnd;
1554 tp->snd_cwnd += tp->t_maxseg;
1555 }
1556 }
1c79356b 1557 sbdrop(&so->so_snd, acked);
8ad349bb
A
1558 if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
1559 SEQ_LEQ(th->th_ack, tp->snd_recover))
1560 tp->snd_recover = th->th_ack - 1;
1c79356b 1561 tp->snd_una = th->th_ack;
8ad349bb
A
1562 /*
1563 * pull snd_wl2 up to prevent seq wrap relative
1564 * to th_ack.
1565 */
1566 tp->snd_wl2 = th->th_ack;
1567 tp->t_dupacks = 0;
1c79356b 1568 m_freem(m);
9bccf70c 1569 ND6_HINT(tp); /* some progress has been done */
1c79356b
A
1570
1571 /*
1572 * If all outstanding data are acked, stop
1573 * retransmit timer, otherwise restart timer
1574 * using current (possibly backed-off) value.
1575 * If process is waiting for space,
1576 * wakeup/selwakeup/signal. If data
1577 * are ready to send, let tcp_output
1578 * decide between more output or persist.
1579 */
1580 if (tp->snd_una == tp->snd_max)
1581 tp->t_timer[TCPT_REXMT] = 0;
1582 else if (tp->t_timer[TCPT_PERSIST] == 0)
1583 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1584
91447636 1585 sowwakeup(so); /* has to be done with socket lock held */
2d21ac55
A
1586 if ((so->so_snd.sb_cc) || (tp->t_flags & TF_ACKNOW)) {
1587 tp->t_unacksegs = 0;
1c79356b 1588 (void) tcp_output(tp);
2d21ac55 1589 }
91447636 1590 tcp_unlock(so, 1, 0);
1c79356b
A
1591 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
1592 return;
1593 }
1594 } else if (th->th_ack == tp->snd_una &&
9bccf70c 1595 LIST_EMPTY(&tp->t_segq) &&
2d21ac55 1596 tlen <= tcp_sbspace(tp)) {
1c79356b
A
1597 /*
1598 * this is a pure, in-sequence data packet
1599 * with nothing on the reassembly queue and
1600 * we have enough buffer space to take it.
1601 */
8ad349bb
A
1602 /* Clean receiver SACK report if present */
1603 if (tp->sack_enable && tp->rcv_numsacks)
1604 tcp_clean_sackreport(tp);
1c79356b 1605 ++tcpstat.tcps_preddat;
9bccf70c 1606 tp->rcv_nxt += tlen;
8ad349bb
A
1607 /*
1608 * Pull snd_wl1 up to prevent seq wrap relative to
1609 * th_seq.
1610 */
1611 tp->snd_wl1 = th->th_seq;
1612 /*
1613 * Pull rcv_up up to prevent seq wrap relative to
1614 * rcv_nxt.
1615 */
1616 tp->rcv_up = tp->rcv_nxt;
1c79356b 1617 tcpstat.tcps_rcvpack++;
9bccf70c
A
1618 tcpstat.tcps_rcvbyte += tlen;
1619 ND6_HINT(tp); /* some progress has been done */
1620 /*
1621 * Add data to socket buffer.
1622 */
1623 m_adj(m, drop_hdrlen); /* delayed header drop */
2d21ac55 1624 if (sbappendstream(&so->so_rcv, m))
91447636 1625 sorwakeup(so);
9bccf70c
A
1626#if INET6
1627 if (isipv6) {
1628 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
1629 (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])),
1630 th->th_seq, th->th_ack, th->th_win);
1631 }
1632 else
1633#endif
1634 {
1635 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
1636 (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)),
1637 th->th_seq, th->th_ack, th->th_win);
1638 }
91447636 1639 if (DELAY_ACK(tp)) {
2d21ac55
A
1640 tp->t_flags |= TF_DELACK;
1641 tp->t_unacksegs++;
1c79356b 1642 } else {
2d21ac55 1643 tp->t_unacksegs = 0;
1c79356b
A
1644 tp->t_flags |= TF_ACKNOW;
1645 tcp_output(tp);
1646 }
91447636 1647 tcp_unlock(so, 1, 0);
1c79356b
A
1648 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
1649 return;
1650 }
1651 }
1652
1653 /*
1654 * Calculate amount of space in receive window,
1655 * and then do TCP input processing.
1656 * Receive window is amount of space in rcv queue,
1657 * but not less than advertised window.
1658 */
91447636
A
1659#if 1
1660 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1661#endif
1c79356b
A
1662 { int win;
1663
2d21ac55
A
1664 win = tcp_sbspace(tp);
1665
1c79356b
A
1666 if (win < 0)
1667 win = 0;
d12e1678
A
1668 else { /* clip rcv window to 4K for modems */
1669 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0)
1670 win = min(win, slowlink_wsize);
1671 }
1c79356b
A
1672 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1673 }
1674
1675 switch (tp->t_state) {
1676
1677 /*
7e4a7d39 1678 * Initialize tp->rcv_nxt, and tp->irs, select an initial
1c79356b
A
1679 * tp->iss, and send a segment:
1680 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1681 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1682 * Fill in remote peer address fields if not previously specified.
1683 * Enter SYN_RECEIVED state, and process any other fields of this
1684 * segment in this state.
1685 */
1686 case TCPS_LISTEN: {
1687 register struct sockaddr_in *sin;
9bccf70c 1688#if INET6
1c79356b
A
1689 register struct sockaddr_in6 *sin6;
1690#endif
1691
91447636
A
1692#if 1
1693 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1694#endif
9bccf70c
A
1695#if INET6
1696 if (isipv6) {
1c79356b
A
1697 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
1698 M_SONAME, M_NOWAIT);
1699 if (sin6 == NULL)
1700 goto drop;
1701 bzero(sin6, sizeof(*sin6));
1702 sin6->sin6_family = AF_INET6;
1703 sin6->sin6_len = sizeof(*sin6);
1704 sin6->sin6_addr = ip6->ip6_src;
1705 sin6->sin6_port = th->th_sport;
1706 laddr6 = inp->in6p_laddr;
1707 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1708 inp->in6p_laddr = ip6->ip6_dst;
1709 if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
9bccf70c 1710 proc0)) {
1c79356b
A
1711 inp->in6p_laddr = laddr6;
1712 FREE(sin6, M_SONAME);
1713 goto drop;
1714 }
1715 FREE(sin6, M_SONAME);
9bccf70c 1716 } else
1c79356b 1717#endif
9bccf70c 1718 {
2d21ac55 1719#if 0
91447636
A
1720 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1721#endif
1c79356b 1722 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
9bccf70c 1723 M_NOWAIT);
1c79356b
A
1724 if (sin == NULL)
1725 goto drop;
1726 sin->sin_family = AF_INET;
1727 sin->sin_len = sizeof(*sin);
1728 sin->sin_addr = ip->ip_src;
1729 sin->sin_port = th->th_sport;
1730 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
1731 laddr = inp->inp_laddr;
1732 if (inp->inp_laddr.s_addr == INADDR_ANY)
1733 inp->inp_laddr = ip->ip_dst;
9bccf70c 1734 if (in_pcbconnect(inp, (struct sockaddr *)sin, proc0)) {
1c79356b
A
1735 inp->inp_laddr = laddr;
1736 FREE(sin, M_SONAME);
1737 goto drop;
1738 }
1739 FREE(sin, M_SONAME);
1c79356b 1740 }
8ad349bb 1741
c910b4d9 1742 tcp_dooptions(tp, optp, optlen, th, &to, ifscope);
8ad349bb
A
1743
1744 if (tp->sack_enable) {
1745 if (!(to.to_flags & TOF_SACK))
1746 tp->sack_enable = 0;
1747 else
1748 tp->t_flags |= TF_SACK_PERMIT;
1749 }
1750
1c79356b
A
1751 if (iss)
1752 tp->iss = iss;
0b4e3aa0 1753 else {
9bccf70c
A
1754 tp->iss = tcp_new_isn(tp);
1755 }
1c79356b
A
1756 tp->irs = th->th_seq;
1757 tcp_sendseqinit(tp);
1758 tcp_rcvseqinit(tp);
9bccf70c 1759 tp->snd_recover = tp->snd_una;
1c79356b
A
1760 /*
1761 * Initialization of the tcpcb for transaction;
1762 * set SND.WND = SEG.WND,
1763 * initialize CCsend and CCrecv.
1764 */
1765 tp->snd_wnd = tiwin; /* initial send-window */
1c79356b 1766 tp->t_flags |= TF_ACKNOW;
2d21ac55 1767 tp->t_unacksegs = 0;
1c79356b 1768 tp->t_state = TCPS_SYN_RECEIVED;
b0d623f7 1769 tp->t_timer[TCPT_KEEP] = tp->t_keepinit ? tp->t_keepinit : tcp_keepinit;
1c79356b
A
1770 dropsocket = 0; /* committed to socket */
1771 tcpstat.tcps_accepts++;
2d21ac55
A
1772 if ((thflags & (TH_ECE | TH_CWR)) == (TH_ECE | TH_CWR)) {
1773 /* ECN-setup SYN */
1774 tp->ecn_flags |= (TE_SETUPRECEIVED | TE_SENDIPECT);
1775 }
b0d623f7
A
1776#if CONFIG_IFEF_NOWINDOWSCALE
1777 if (tcp_obey_ifef_nowindowscale && m->m_pkthdr.rcvif != NULL &&
1778 (m->m_pkthdr.rcvif->if_eflags & IFEF_NOWINDOWSCALE)) {
1779 /* Window scaling is not enabled on this interface */
1780 tp->t_flags &= ~TF_REQ_SCALE;
593a1d5f
A
1781 }
1782#endif
1c79356b
A
1783 goto trimthenstep6;
1784 }
1785
1786 /*
1787 * If the state is SYN_RECEIVED:
1788 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1789 */
1790 case TCPS_SYN_RECEIVED:
1791 if ((thflags & TH_ACK) &&
1792 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
9bccf70c
A
1793 SEQ_GT(th->th_ack, tp->snd_max))) {
1794 rstreason = BANDLIM_RST_OPENPORT;
1c79356b 1795 goto dropwithreset;
9bccf70c 1796 }
1c79356b
A
1797 break;
1798
1799 /*
1800 * If the state is SYN_SENT:
1801 * if seg contains an ACK, but not for our SYN, drop the input.
1802 * if seg contains a RST, then drop the connection.
1803 * if seg does not contain SYN, then drop it.
1804 * Otherwise this is an acceptable SYN segment
1805 * initialize tp->rcv_nxt and tp->irs
1806 * if seg contains ack then advance tp->snd_una
1807 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1808 * arrange for segment to be acked (eventually)
1809 * continue processing rest of data/controls, beginning with URG
1810 */
1811 case TCPS_SYN_SENT:
1c79356b
A
1812 if ((thflags & TH_ACK) &&
1813 (SEQ_LEQ(th->th_ack, tp->iss) ||
1814 SEQ_GT(th->th_ack, tp->snd_max))) {
8ad349bb
A
1815 rstreason = BANDLIM_UNLIMITED;
1816 goto dropwithreset;
1c79356b
A
1817 }
1818 if (thflags & TH_RST) {
2d21ac55 1819 if ((thflags & TH_ACK) != 0) {
1c79356b
A
1820 tp = tcp_drop(tp, ECONNREFUSED);
1821 postevent(so, 0, EV_RESET);
2d21ac55 1822 }
1c79356b
A
1823 goto drop;
1824 }
1825 if ((thflags & TH_SYN) == 0)
1826 goto drop;
1827 tp->snd_wnd = th->th_win; /* initial send window */
1c79356b
A
1828
1829 tp->irs = th->th_seq;
1830 tcp_rcvseqinit(tp);
1831 if (thflags & TH_ACK) {
1c79356b 1832 tcpstat.tcps_connects++;
2d21ac55
A
1833
1834 if ((thflags & (TH_ECE | TH_CWR)) == (TH_ECE)) {
1835 /* ECN-setup SYN-ACK */
1836 tp->ecn_flags |= TE_SETUPRECEIVED;
1837 }
1838 else {
1839 /* non-ECN-setup SYN-ACK */
1840 tp->ecn_flags &= ~TE_SENDIPECT;
1841 }
1842
2d21ac55
A
1843#if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
1844 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
1845 mac_socketpeer_label_associate_mbuf(m, so);
1846 /* XXXMAC: SOCK_UNLOCK(so); */
1847#endif
1c79356b
A
1848 /* Do window scaling on this connection? */
1849 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1850 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1851 tp->snd_scale = tp->requested_s_scale;
1852 tp->rcv_scale = tp->request_r_scale;
1853 }
1c79356b
A
1854 tp->rcv_adv += tp->rcv_wnd;
1855 tp->snd_una++; /* SYN is acked */
1856 /*
1857 * If there's data, delay ACK; if there's also a FIN
1858 * ACKNOW will be turned on later.
1859 */
91447636 1860 if (DELAY_ACK(tp) && tlen != 0) {
1c79356b 1861 tp->t_flags |= TF_DELACK;
2d21ac55 1862 tp->t_unacksegs++;
1c79356b 1863 }
91447636 1864 else {
1c79356b 1865 tp->t_flags |= TF_ACKNOW;
2d21ac55 1866 tp->t_unacksegs = 0;
91447636 1867 }
1c79356b
A
1868 /*
1869 * Received <SYN,ACK> in SYN_SENT[*] state.
1870 * Transitions:
1871 * SYN_SENT --> ESTABLISHED
1872 * SYN_SENT* --> FIN_WAIT_1
1873 */
8ad349bb 1874 tp->t_starttime = 0;
1c79356b
A
1875 if (tp->t_flags & TF_NEEDFIN) {
1876 tp->t_state = TCPS_FIN_WAIT_1;
1877 tp->t_flags &= ~TF_NEEDFIN;
1878 thflags &= ~TH_SYN;
1879 } else {
1c79356b 1880 tp->t_state = TCPS_ESTABLISHED;
55e303ae 1881 tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp);
1c79356b 1882 }
4a3eedf9
A
1883 /* soisconnected may lead to socket_unlock in case of upcalls,
1884 * make sure this is done when everything is setup.
1885 */
1886 soisconnected(so);
1c79356b
A
1887 } else {
1888 /*
1889 * Received initial SYN in SYN-SENT[*] state => simul-
1890 * taneous open. If segment contains CC option and there is
1891 * a cached CC, apply TAO test; if it succeeds, connection is
1892 * half-synchronized. Otherwise, do 3-way handshake:
1893 * SYN-SENT -> SYN-RECEIVED
1894 * SYN-SENT* -> SYN-RECEIVED*
1c79356b
A
1895 */
1896 tp->t_flags |= TF_ACKNOW;
1897 tp->t_timer[TCPT_REXMT] = 0;
8ad349bb
A
1898 tp->t_state = TCPS_SYN_RECEIVED;
1899
1c79356b
A
1900 }
1901
1902trimthenstep6:
1903 /*
1904 * Advance th->th_seq to correspond to first data byte.
1905 * If data, trim to stay within window,
1906 * dropping FIN if necessary.
1907 */
1908 th->th_seq++;
9bccf70c
A
1909 if (tlen > tp->rcv_wnd) {
1910 todrop = tlen - tp->rcv_wnd;
1c79356b 1911 m_adj(m, -todrop);
9bccf70c 1912 tlen = tp->rcv_wnd;
1c79356b
A
1913 thflags &= ~TH_FIN;
1914 tcpstat.tcps_rcvpackafterwin++;
1915 tcpstat.tcps_rcvbyteafterwin += todrop;
1916 }
1917 tp->snd_wl1 = th->th_seq - 1;
1918 tp->rcv_up = th->th_seq;
1919 /*
1920 * Client side of transaction: already sent SYN and data.
1921 * If the remote host used T/TCP to validate the SYN,
1922 * our data will be ACK'd; if so, enter normal data segment
1923 * processing in the middle of step 5, ack processing.
1924 * Otherwise, goto step 6.
1925 */
1926 if (thflags & TH_ACK)
1927 goto process_ACK;
1928 goto step6;
1929 /*
1930 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
8ad349bb 1931 * do normal processing.
1c79356b 1932 *
8ad349bb 1933 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
1c79356b
A
1934 */
1935 case TCPS_LAST_ACK:
1936 case TCPS_CLOSING:
1937 case TCPS_TIME_WAIT:
1c79356b 1938 break; /* continue normal processing */
55e303ae
A
1939
1940 /* Received a SYN while connection is already established.
1941 * This is a "half open connection and other anomalies" described
1942 * in RFC793 page 34, send an ACK so the remote reset the connection
1943 * or recovers by adjusting its sequence numberering
1944 */
1945 case TCPS_ESTABLISHED:
1946 if (thflags & TH_SYN)
1947 goto dropafterack;
1948 break;
1c79356b
A
1949 }
1950
1951 /*
1952 * States other than LISTEN or SYN_SENT.
1953 * First check the RST flag and sequence number since reset segments
1954 * are exempt from the timestamp and connection count tests. This
1955 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1956 * below which allowed reset segments in half the sequence space
1957 * to fall though and be processed (which gives forged reset
1958 * segments with a random sequence number a 50 percent chance of
1959 * killing a connection).
1960 * Then check timestamp, if present.
1961 * Then check the connection count, if present.
1962 * Then check that at least some bytes of segment are within
1963 * receive window. If segment begins before rcv_nxt,
1964 * drop leading data (and SYN); if nothing left, just ack.
1965 *
1966 *
1967 * If the RST bit is set, check the sequence number to see
1968 * if this is a valid reset segment.
1969 * RFC 793 page 37:
1970 * In all states except SYN-SENT, all reset (RST) segments
1971 * are validated by checking their SEQ-fields. A reset is
1972 * valid if its sequence number is in the window.
1973 * Note: this does not take into account delayed ACKs, so
1974 * we should test against last_ack_sent instead of rcv_nxt.
9bccf70c
A
1975 * The sequence number in the reset segment is normally an
1976 * echo of our outgoing acknowlegement numbers, but some hosts
1977 * send a reset with the sequence number at the rightmost edge
1978 * of our receive window, and we have to handle this case.
8ad349bb
A
1979 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
1980 * that brute force RST attacks are possible. To combat this,
1981 * we use a much stricter check while in the ESTABLISHED state,
1982 * only accepting RSTs where the sequence number is equal to
1983 * last_ack_sent. In all other states (the states in which a
1984 * RST is more likely), the more permissive check is used.
1c79356b
A
1985 * If we have multiple segments in flight, the intial reset
1986 * segment sequence numbers will be to the left of last_ack_sent,
1987 * but they will eventually catch up.
1988 * In any case, it never made sense to trim reset segments to
1989 * fit the receive window since RFC 1122 says:
1990 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1991 *
1992 * A TCP SHOULD allow a received RST segment to include data.
1993 *
1994 * DISCUSSION
1995 * It has been suggested that a RST segment could contain
1996 * ASCII text that encoded and explained the cause of the
1997 * RST. No standard has yet been established for such
1998 * data.
1999 *
2000 * If the reset segment passes the sequence number test examine
2001 * the state:
2002 * SYN_RECEIVED STATE:
2003 * If passive open, return to LISTEN state.
2004 * If active open, inform user that connection was refused.
8ad349bb 2005 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1c79356b 2006 * Inform user that connection was reset, and close tcb.
9bccf70c 2007 * CLOSING, LAST_ACK STATES:
1c79356b 2008 * Close the tcb.
9bccf70c 2009 * TIME_WAIT STATE:
1c79356b
A
2010 * Drop the segment - see Stevens, vol. 2, p. 964 and
2011 * RFC 1337.
0c530ab8 2012 *
2d21ac55
A
2013 * Radar 4803931: Allows for the case where we ACKed the FIN but
2014 * there is already a RST in flight from the peer.
2015 * In that case, accept the RST for non-established
2016 * state if it's one off from last_ack_sent.
2017
1c79356b
A
2018 */
2019 if (thflags & TH_RST) {
8ad349bb
A
2020 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
2021 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
0c530ab8 2022 (tp->rcv_wnd == 0 &&
2d21ac55 2023 ((tp->last_ack_sent == th->th_seq) || ((tp->last_ack_sent -1) == th->th_seq)))) {
1c79356b
A
2024 switch (tp->t_state) {
2025
2026 case TCPS_SYN_RECEIVED:
2027 so->so_error = ECONNREFUSED;
2028 goto close;
2029
2030 case TCPS_ESTABLISHED:
8ad349bb 2031 if (tp->last_ack_sent != th->th_seq) {
2d21ac55 2032 tcpstat.tcps_badrst++;
8ad349bb
A
2033 goto drop;
2034 }
1c79356b
A
2035 case TCPS_FIN_WAIT_1:
2036 case TCPS_CLOSE_WAIT:
1c79356b
A
2037 /*
2038 Drop through ...
2039 */
2040 case TCPS_FIN_WAIT_2:
2041 so->so_error = ECONNRESET;
2042 close:
2043 postevent(so, 0, EV_RESET);
2044 tp->t_state = TCPS_CLOSED;
2045 tcpstat.tcps_drops++;
2046 tp = tcp_close(tp);
2047 break;
2048
2049 case TCPS_CLOSING:
2050 case TCPS_LAST_ACK:
1c79356b
A
2051 tp = tcp_close(tp);
2052 break;
2053
2054 case TCPS_TIME_WAIT:
2055 break;
2056 }
2057 }
2058 goto drop;
2059 }
2060
2d21ac55 2061#if 0
91447636
A
2062 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
2063#endif
2d21ac55 2064
1c79356b
A
2065 /*
2066 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2067 * and it's less than ts_recent, drop it.
2068 */
8ad349bb 2069 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
1c79356b
A
2070 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2071
2072 /* Check to see if ts_recent is over 24 days old. */
2073 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
2074 /*
2075 * Invalidate ts_recent. If this segment updates
2076 * ts_recent, the age will be reset later and ts_recent
2077 * will get a valid value. If it does not, setting
2078 * ts_recent to zero will at least satisfy the
2079 * requirement that zero be placed in the timestamp
2080 * echo reply when ts_recent isn't valid. The
2081 * age isn't reset until we get a valid ts_recent
2082 * because we don't want out-of-order segments to be
2083 * dropped when ts_recent is old.
2084 */
2085 tp->ts_recent = 0;
2086 } else {
2087 tcpstat.tcps_rcvduppack++;
9bccf70c 2088 tcpstat.tcps_rcvdupbyte += tlen;
1c79356b 2089 tcpstat.tcps_pawsdrop++;
8ad349bb
A
2090 if (tlen)
2091 goto dropafterack;
2092 goto drop;
1c79356b
A
2093 }
2094 }
2095
1c79356b
A
2096 /*
2097 * In the SYN-RECEIVED state, validate that the packet belongs to
2098 * this connection before trimming the data to fit the receive
2099 * window. Check the sequence number versus IRS since we know
2100 * the sequence numbers haven't wrapped. This is a partial fix
2101 * for the "LAND" DoS attack.
2102 */
9bccf70c
A
2103 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2104 rstreason = BANDLIM_RST_OPENPORT;
1c79356b 2105 goto dropwithreset;
9bccf70c 2106 }
1c79356b
A
2107
2108 todrop = tp->rcv_nxt - th->th_seq;
2109 if (todrop > 0) {
2110 if (thflags & TH_SYN) {
2111 thflags &= ~TH_SYN;
2112 th->th_seq++;
2113 if (th->th_urp > 1)
2114 th->th_urp--;
2115 else
2116 thflags &= ~TH_URG;
2117 todrop--;
2118 }
2119 /*
2120 * Following if statement from Stevens, vol. 2, p. 960.
2121 */
9bccf70c
A
2122 if (todrop > tlen
2123 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1c79356b
A
2124 /*
2125 * Any valid FIN must be to the left of the window.
2126 * At this point the FIN must be a duplicate or out
2127 * of sequence; drop it.
2128 */
2129 thflags &= ~TH_FIN;
2130
2131 /*
2132 * Send an ACK to resynchronize and drop any data.
2133 * But keep on processing for RST or ACK.
2134 */
2135 tp->t_flags |= TF_ACKNOW;
2d21ac55 2136 tp->t_unacksegs = 0;
9bccf70c 2137 todrop = tlen;
1c79356b
A
2138 tcpstat.tcps_rcvduppack++;
2139 tcpstat.tcps_rcvdupbyte += todrop;
2140 } else {
2141 tcpstat.tcps_rcvpartduppack++;
2142 tcpstat.tcps_rcvpartdupbyte += todrop;
2143 }
9bccf70c 2144 drop_hdrlen += todrop; /* drop from the top afterwards */
1c79356b 2145 th->th_seq += todrop;
9bccf70c 2146 tlen -= todrop;
1c79356b
A
2147 if (th->th_urp > todrop)
2148 th->th_urp -= todrop;
2149 else {
2150 thflags &= ~TH_URG;
2151 th->th_urp = 0;
2152 }
2153 }
2154
2155 /*
2156 * If new data are received on a connection after the
2157 * user processes are gone, then RST the other end.
2158 */
2159 if ((so->so_state & SS_NOFDREF) &&
9bccf70c 2160 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1c79356b
A
2161 tp = tcp_close(tp);
2162 tcpstat.tcps_rcvafterclose++;
9bccf70c 2163 rstreason = BANDLIM_UNLIMITED;
1c79356b
A
2164 goto dropwithreset;
2165 }
2166
2167 /*
2168 * If segment ends after window, drop trailing data
2169 * (and PUSH and FIN); if nothing left, just ACK.
2170 */
9bccf70c 2171 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1c79356b
A
2172 if (todrop > 0) {
2173 tcpstat.tcps_rcvpackafterwin++;
9bccf70c
A
2174 if (todrop >= tlen) {
2175 tcpstat.tcps_rcvbyteafterwin += tlen;
1c79356b
A
2176 /*
2177 * If a new connection request is received
2178 * while in TIME_WAIT, drop the old connection
2179 * and start over if the sequence numbers
2180 * are above the previous ones.
2181 */
2182 if (thflags & TH_SYN &&
2183 tp->t_state == TCPS_TIME_WAIT &&
2184 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
9bccf70c 2185 iss = tcp_new_isn(tp);
1c79356b 2186 tp = tcp_close(tp);
91447636 2187 tcp_unlock(so, 1, 0);
1c79356b
A
2188 goto findpcb;
2189 }
2190 /*
2191 * If window is closed can only take segments at
2192 * window edge, and have to drop data and PUSH from
2193 * incoming segments. Continue processing, but
2194 * remember to ack. Otherwise, drop segment
2195 * and ack.
2196 */
2197 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2198 tp->t_flags |= TF_ACKNOW;
2d21ac55 2199 tp->t_unacksegs = 0;
1c79356b
A
2200 tcpstat.tcps_rcvwinprobe++;
2201 } else
2202 goto dropafterack;
2203 } else
2204 tcpstat.tcps_rcvbyteafterwin += todrop;
2205 m_adj(m, -todrop);
9bccf70c 2206 tlen -= todrop;
1c79356b
A
2207 thflags &= ~(TH_PUSH|TH_FIN);
2208 }
2209
2210 /*
2211 * If last ACK falls within this segment's sequence numbers,
2212 * record its timestamp.
8ad349bb
A
2213 * NOTE:
2214 * 1) That the test incorporates suggestions from the latest
2215 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2216 * 2) That updating only on newer timestamps interferes with
2217 * our earlier PAWS tests, so this check should be solely
2218 * predicated on the sequence space of this segment.
2219 * 3) That we modify the segment boundary check to be
2220 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
2221 * instead of RFC1323's
2222 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
2223 * This modified check allows us to overcome RFC1323's
2224 * limitations as described in Stevens TCP/IP Illustrated
2225 * Vol. 2 p.869. In such cases, we can still calculate the
2226 * RTT correctly when RCV.NXT == Last.ACK.Sent.
1c79356b 2227 */
8ad349bb
A
2228 if ((to.to_flags & TOF_TS) != 0 &&
2229 SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2230 SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2231 ((thflags & (TH_SYN|TH_FIN)) != 0))) {
1c79356b
A
2232 tp->ts_recent_age = tcp_now;
2233 tp->ts_recent = to.to_tsval;
2234 }
2235
2236 /*
2237 * If a SYN is in the window, then this is an
2238 * error and we send an RST and drop the connection.
2239 */
2240 if (thflags & TH_SYN) {
2241 tp = tcp_drop(tp, ECONNRESET);
9bccf70c 2242 rstreason = BANDLIM_UNLIMITED;
1c79356b
A
2243 postevent(so, 0, EV_RESET);
2244 goto dropwithreset;
2245 }
2246
2247 /*
2248 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
2249 * flag is on (half-synchronized state), then queue data for
2250 * later processing; else drop segment and return.
2251 */
2252 if ((thflags & TH_ACK) == 0) {
2253 if (tp->t_state == TCPS_SYN_RECEIVED ||
2254 (tp->t_flags & TF_NEEDSYN))
2255 goto step6;
2d21ac55
A
2256 else if (tp->t_flags & TF_ACKNOW)
2257 goto dropafterack;
1c79356b
A
2258 else
2259 goto drop;
2260 }
2261
2262 /*
2263 * Ack processing.
2264 */
2265 switch (tp->t_state) {
2266
2267 /*
2268 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2269 * ESTABLISHED state and continue processing.
2270 * The ACK was checked above.
2271 */
2272 case TCPS_SYN_RECEIVED:
2273
2274 tcpstat.tcps_connects++;
1c79356b
A
2275
2276 /* Do window scaling? */
2277 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2278 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2279 tp->snd_scale = tp->requested_s_scale;
2280 tp->rcv_scale = tp->request_r_scale;
2281 }
1c79356b
A
2282 /*
2283 * Make transitions:
2284 * SYN-RECEIVED -> ESTABLISHED
2285 * SYN-RECEIVED* -> FIN-WAIT-1
2286 */
8ad349bb 2287 tp->t_starttime = 0;
1c79356b
A
2288 if (tp->t_flags & TF_NEEDFIN) {
2289 tp->t_state = TCPS_FIN_WAIT_1;
2290 tp->t_flags &= ~TF_NEEDFIN;
2291 } else {
2292 tp->t_state = TCPS_ESTABLISHED;
55e303ae 2293 tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp);
1c79356b
A
2294 }
2295 /*
2296 * If segment contains data or ACK, will call tcp_reass()
2297 * later; if not, do so now to pass queued data to user.
2298 */
9bccf70c 2299 if (tlen == 0 && (thflags & TH_FIN) == 0)
2d21ac55 2300 (void) tcp_reass(tp, (struct tcphdr *)0, &tlen,
9bccf70c 2301 (struct mbuf *)0);
1c79356b 2302 tp->snd_wl1 = th->th_seq - 1;
4a3eedf9 2303
8ad349bb 2304 /* FALLTHROUGH */
1c79356b 2305
4a3eedf9
A
2306 /* soisconnected may lead to socket_unlock in case of upcalls,
2307 * make sure this is done when everything is setup.
2308 */
2309 soisconnected(so);
2310
1c79356b
A
2311 /*
2312 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2313 * ACKs. If the ack is in the range
2314 * tp->snd_una < th->th_ack <= tp->snd_max
2315 * then advance tp->snd_una to th->th_ack and drop
2316 * data from the retransmission queue. If this ACK reflects
2317 * more up to date window information we update our window information.
2318 */
2319 case TCPS_ESTABLISHED:
2320 case TCPS_FIN_WAIT_1:
2321 case TCPS_FIN_WAIT_2:
2322 case TCPS_CLOSE_WAIT:
2323 case TCPS_CLOSING:
2324 case TCPS_LAST_ACK:
2325 case TCPS_TIME_WAIT:
8ad349bb
A
2326 if (SEQ_GT(th->th_ack, tp->snd_max)) {
2327 tcpstat.tcps_rcvacktoomuch++;
2328 goto dropafterack;
2329 }
2330 if (tp->sack_enable &&
2331 (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes)))
2332 tcp_sack_doack(tp, &to, th->th_ack);
1c79356b 2333 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
9bccf70c 2334 if (tlen == 0 && tiwin == tp->snd_wnd) {
1c79356b
A
2335 tcpstat.tcps_rcvdupack++;
2336 /*
2337 * If we have outstanding data (other than
2338 * a window probe), this is a completely
2339 * duplicate ack (ie, window info didn't
2340 * change), the ack is the biggest we've
2341 * seen and we've seen exactly our rexmt
2342 * threshhold of them, assume a packet
2343 * has been dropped and retransmit it.
2344 * Kludge snd_nxt & the congestion
2345 * window so we send only this one
2346 * packet.
2347 *
2348 * We know we're losing at the current
2349 * window size so do congestion avoidance
2350 * (set ssthresh to half the current window
2351 * and pull our congestion window back to
2352 * the new ssthresh).
2353 *
2354 * Dup acks mean that packets have left the
2355 * network (they're now cached at the receiver)
2356 * so bump cwnd by the amount in the receiver
2357 * to keep a constant cwnd packets in the
2358 * network.
2359 */
2360 if (tp->t_timer[TCPT_REXMT] == 0 ||
2361 th->th_ack != tp->snd_una)
2362 tp->t_dupacks = 0;
8ad349bb
A
2363 else if (++tp->t_dupacks > tcprexmtthresh ||
2364 ((tcp_do_newreno || tp->sack_enable) &&
2365 IN_FASTRECOVERY(tp))) {
2d21ac55 2366 if (tp->sack_enable && IN_FASTRECOVERY(tp)) {
8ad349bb
A
2367 int awnd;
2368
2369 /*
2370 * Compute the amount of data in flight first.
2371 * We can inject new data into the pipe iff
2372 * we have less than 1/2 the original window's
2373 * worth of data in flight.
9bccf70c 2374 */
8ad349bb
A
2375 awnd = (tp->snd_nxt - tp->snd_fack) +
2376 tp->sackhint.sack_bytes_rexmit;
2377 if (awnd < tp->snd_ssthresh) {
2378 tp->snd_cwnd += tp->t_maxseg;
2379 if (tp->snd_cwnd > tp->snd_ssthresh)
2380 tp->snd_cwnd = tp->snd_ssthresh;
2381 }
2382 } else
9bccf70c 2383 tp->snd_cwnd += tp->t_maxseg;
2d21ac55 2384 tp->t_unacksegs = 0;
8ad349bb
A
2385 (void) tcp_output(tp);
2386 goto drop;
2387 } else if (tp->t_dupacks == tcprexmtthresh) {
2388 tcp_seq onxt = tp->snd_nxt;
2389 u_int win;
2390
2391 /*
2392 * If we're doing sack, check to
2393 * see if we're already in sack
2394 * recovery. If we're not doing sack,
2395 * check to see if we're in newreno
2396 * recovery.
2397 */
2398 if (tp->sack_enable) {
2399 if (IN_FASTRECOVERY(tp)) {
2400 tp->t_dupacks = 0;
2401 break;
2402 }
2403 } else if (tcp_do_newreno) {
2404 if (SEQ_LEQ(th->th_ack,
2405 tp->snd_recover)) {
2406 tp->t_dupacks = 0;
2407 break;
2408 }
9bccf70c 2409 }
8ad349bb
A
2410 win = min(tp->snd_wnd, tp->snd_cwnd) /
2411 2 / tp->t_maxseg;
1c79356b
A
2412 if (win < 2)
2413 win = 2;
2414 tp->snd_ssthresh = win * tp->t_maxseg;
8ad349bb 2415 ENTER_FASTRECOVERY(tp);
9bccf70c 2416 tp->snd_recover = tp->snd_max;
1c79356b 2417 tp->t_timer[TCPT_REXMT] = 0;
9bccf70c 2418 tp->t_rtttime = 0;
2d21ac55 2419 tp->ecn_flags |= TE_SENDCWR;
8ad349bb
A
2420 if (tp->sack_enable) {
2421 tcpstat.tcps_sack_recovery_episode++;
2422 tp->sack_newdata = tp->snd_nxt;
2423 tp->snd_cwnd = tp->t_maxseg;
2d21ac55 2424 tp->t_unacksegs = 0;
8ad349bb
A
2425 (void) tcp_output(tp);
2426 goto drop;
2427 }
1c79356b
A
2428 tp->snd_nxt = th->th_ack;
2429 tp->snd_cwnd = tp->t_maxseg;
2d21ac55 2430 tp->t_unacksegs = 0;
1c79356b
A
2431 (void) tcp_output(tp);
2432 tp->snd_cwnd = tp->snd_ssthresh +
8ad349bb 2433 tp->t_maxseg * tp->t_dupacks;
1c79356b
A
2434 if (SEQ_GT(onxt, tp->snd_nxt))
2435 tp->snd_nxt = onxt;
2436 goto drop;
1c79356b
A
2437 }
2438 } else
2439 tp->t_dupacks = 0;
2440 break;
2441 }
b0d623f7
A
2442 /*
2443 * If the congestion window was inflated to account
2444 * for the other side's cached packets, retract it.
2445 */
593a1d5f
A
2446 if (!IN_FASTRECOVERY(tp)) {
2447 /*
2448 * We were not in fast recovery. Reset the duplicate ack
2449 * counter.
2450 */
2451 tp->t_dupacks = 0;
2452 }
1c79356b
A
2453 /*
2454 * If the congestion window was inflated to account
2455 * for the other side's cached packets, retract it.
2456 */
593a1d5f
A
2457 else {
2458 if (tcp_do_newreno || tp->sack_enable) {
8ad349bb
A
2459 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2460 if (tp->sack_enable)
2461 tcp_sack_partialack(tp, th);
2462 else
593a1d5f
A
2463 tcp_newreno_partial_ack(tp, th);
2464 }
2465 else {
2466 if (tcp_do_newreno) {
b0d623f7 2467 int32_t ss = tp->snd_max - th->th_ack;
593a1d5f
A
2468
2469 /*
2470 * Complete ack. Inflate the congestion window to
2471 * ssthresh and exit fast recovery.
2472 *
2473 * Window inflation should have left us with approx.
2474 * snd_ssthresh outstanding data. But in case we
2475 * would be inclined to send a burst, better to do
2476 * it via the slow start mechanism.
2477 */
2478 if (ss < tp->snd_ssthresh)
2479 tp->snd_cwnd = ss + tp->t_maxseg;
2480 else
2481 tp->snd_cwnd = tp->snd_ssthresh;
2482 }
2483 else {
2484 /*
2485 * Clamp the congestion window to the crossover point
2486 * and exit fast recovery.
2487 */
2488 if (tp->snd_cwnd > tp->snd_ssthresh)
2489 tp->snd_cwnd = tp->snd_ssthresh;
2490 }
2491
2492 EXIT_FASTRECOVERY(tp);
2493 tp->t_dupacks = 0;
2494 tp->t_bytes_acked = 0;
8ad349bb
A
2495 }
2496 }
593a1d5f
A
2497 else {
2498 /*
2499 * Clamp the congestion window to the crossover point
2500 * and exit fast recovery in non-newreno and non-SACK case.
2501 */
2502 if (tp->snd_cwnd > tp->snd_ssthresh)
2503 tp->snd_cwnd = tp->snd_ssthresh;
2504 EXIT_FASTRECOVERY(tp);
2505 tp->t_dupacks = 0;
2506 tp->t_bytes_acked = 0;
2507 }
1c79356b 2508 }
593a1d5f
A
2509
2510
1c79356b 2511 /*
8ad349bb 2512 * If we reach this point, ACK is not a duplicate,
1c79356b
A
2513 * i.e., it ACKs something we sent.
2514 */
2515 if (tp->t_flags & TF_NEEDSYN) {
2516 /*
2517 * T/TCP: Connection was half-synchronized, and our
2518 * SYN has been ACK'd (so connection is now fully
2519 * synchronized). Go to non-starred state,
2520 * increment snd_una for ACK of SYN, and check if
2521 * we can do window scaling.
2522 */
2523 tp->t_flags &= ~TF_NEEDSYN;
2524 tp->snd_una++;
2525 /* Do window scaling? */
2526 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2527 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
2528 tp->snd_scale = tp->requested_s_scale;
2529 tp->rcv_scale = tp->request_r_scale;
2530 }
2531 }
2532
2533process_ACK:
2534 acked = th->th_ack - tp->snd_una;
2535 tcpstat.tcps_rcvackpack++;
2536 tcpstat.tcps_rcvackbyte += acked;
2537
9bccf70c
A
2538 /*
2539 * If we just performed our first retransmit, and the ACK
2540 * arrives within our recovery window, then it was a mistake
2541 * to do the retransmit in the first place. Recover our
2542 * original cwnd and ssthresh, and proceed to transmit where
2543 * we left off.
2544 */
2545 if (tp->t_rxtshift == 1 && tcp_now < tp->t_badrxtwin) {
2d21ac55 2546 ++tcpstat.tcps_sndrexmitbad;
9bccf70c
A
2547 tp->snd_cwnd = tp->snd_cwnd_prev;
2548 tp->snd_ssthresh = tp->snd_ssthresh_prev;
8ad349bb
A
2549 tp->snd_recover = tp->snd_recover_prev;
2550 if (tp->t_flags & TF_WASFRECOVERY)
2551 ENTER_FASTRECOVERY(tp);
9bccf70c
A
2552 tp->snd_nxt = tp->snd_max;
2553 tp->t_badrxtwin = 0; /* XXX probably not required */
2554 }
2555
1c79356b
A
2556 /*
2557 * If we have a timestamp reply, update smoothed
2558 * round trip time. If no timestamp is present but
2559 * transmit timer is running and timed sequence
2560 * number was acked, update smoothed round trip time.
2561 * Since we now have an rtt measurement, cancel the
2562 * timer backoff (cf., Phil Karn's retransmit alg.).
2563 * Recompute the initial retransmit timer.
143cc14e 2564 * Also makes sure we have a valid time stamp in hand
2d21ac55
A
2565 *
2566 * Some boxes send broken timestamp replies
2567 * during the SYN+ACK phase, ignore
2568 * timestamps of 0 or we could calculate a
2569 * huge RTT and blow up the retransmit timer.
1c79356b 2570 */
2d21ac55
A
2571 if (((to.to_flags & TOF_TS) != 0) && (to.to_tsecr != 0)) {
2572 if (!tp->t_rttlow || tp->t_rttlow > tcp_now - to.to_tsecr)
2573 tp->t_rttlow = tcp_now - to.to_tsecr;
2574 tcp_xmit_timer(tp, tcp_now - to.to_tsecr);
2575 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2576 if (!tp->t_rttlow || tp->t_rttlow > tcp_now - tp->t_rtttime)
2577 tp->t_rttlow = tcp_now - tp->t_rtttime;
9bccf70c 2578 tcp_xmit_timer(tp, tp->t_rtttime);
2d21ac55 2579 }
1c79356b
A
2580
2581 /*
2582 * If all outstanding data is acked, stop retransmit
2583 * timer and remember to restart (more output or persist).
2584 * If there is more data to be acked, restart retransmit
2585 * timer, using current (possibly backed-off) value.
2586 */
2587 if (th->th_ack == tp->snd_max) {
2588 tp->t_timer[TCPT_REXMT] = 0;
2589 needoutput = 1;
2590 } else if (tp->t_timer[TCPT_PERSIST] == 0)
2591 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
2592
2593 /*
2594 * If no data (only SYN) was ACK'd,
2595 * skip rest of ACK processing.
2596 */
2597 if (acked == 0)
2598 goto step6;
2599
2600 /*
2601 * When new data is acked, open the congestion window.
1c79356b 2602 */
2d21ac55
A
2603 if ((thflags & TH_ECE) != 0 &&
2604 (tp->ecn_flags & TE_SETUPSENT) != 0) {
2605 /*
2606 * Reduce the congestion window if we haven't done so.
2607 */
2608 if (!(tp->sack_enable && IN_FASTRECOVERY(tp)) &&
2609 !(tcp_do_newreno && SEQ_LEQ(th->th_ack, tp->snd_recover))) {
2610 tcp_reduce_congestion_window(tp);
2611 }
2612 } else if ((!tcp_do_newreno && !tp->sack_enable) ||
8ad349bb 2613 !IN_FASTRECOVERY(tp)) {
2d21ac55
A
2614 /*
2615 * RFC 3465 - Appropriate Byte Counting.
2616 *
2617 * If the window is currently less than ssthresh,
2618 * open the window by the number of bytes ACKed by
2619 * the last ACK, however clamp the window increase
2620 * to an upper limit "L".
2621 *
2622 * In congestion avoidance phase, open the window by
2623 * one segment each time "bytes_acked" grows to be
2624 * greater than or equal to the congestion window.
2625 */
2626
8ad349bb
A
2627 register u_int cw = tp->snd_cwnd;
2628 register u_int incr = tp->t_maxseg;
2d21ac55 2629
b0d623f7
A
2630 if (tcp_do_rfc3465) {
2631
2632 if (cw >= tp->snd_ssthresh) {
2633 tp->t_bytes_acked += acked;
2634 if (tp->t_bytes_acked >= cw) {
2d21ac55 2635 /* Time to increase the window. */
b0d623f7
A
2636 tp->t_bytes_acked -= cw;
2637 } else {
cf7d32b8 2638 /* No need to increase yet. */
b0d623f7
A
2639 incr = 0;
2640 }
2641 } else {
2642 /*
2643 * If the user explicitly enables RFC3465
2644 * use 2*SMSS for the "L" param. Otherwise
2645 * use the more conservative 1*SMSS.
2646 *
2647 * (See RFC 3465 2.3 Choosing the Limit)
2648 */
2649 u_int abc_lim;
2650
2651 abc_lim = (tcp_do_rfc3465_lim2 &&
2652 tp->snd_nxt == tp->snd_max) ? incr * 2 : incr;
2653
2654 incr = lmin(acked, abc_lim);
2d21ac55 2655 }
b0d623f7
A
2656 }
2657 else {
2d21ac55 2658 /*
b0d623f7
A
2659 * If the window gives us less than ssthresh packets
2660 * in flight, open exponentially (segsz per packet).
2661 * Otherwise open linearly: segsz per window
2662 * (segsz^2 / cwnd per packet).
2d21ac55 2663 */
b0d623f7
A
2664
2665 if (cw >= tp->snd_ssthresh) {
2666 incr = max((incr * incr / cw), 1);
2667 }
cf7d32b8 2668 }
4a3eedf9 2669
b0d623f7 2670
8ad349bb 2671 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
1c79356b
A
2672 }
2673 if (acked > so->so_snd.sb_cc) {
2674 tp->snd_wnd -= so->so_snd.sb_cc;
2675 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
2676 ourfinisacked = 1;
2677 } else {
2678 sbdrop(&so->so_snd, acked);
2679 tp->snd_wnd -= acked;
2680 ourfinisacked = 0;
2681 }
91447636 2682 /* detect una wraparound */
8ad349bb
A
2683 if ((tcp_do_newreno || tp->sack_enable) &&
2684 !IN_FASTRECOVERY(tp) &&
2685 SEQ_GT(tp->snd_una, tp->snd_recover) &&
2686 SEQ_LEQ(th->th_ack, tp->snd_recover))
2687 tp->snd_recover = th->th_ack - 1;
2688 if ((tcp_do_newreno || tp->sack_enable) &&
2689 IN_FASTRECOVERY(tp) &&
2690 SEQ_GEQ(th->th_ack, tp->snd_recover))
2691 EXIT_FASTRECOVERY(tp);
1c79356b 2692 tp->snd_una = th->th_ack;
8ad349bb
A
2693 if (tp->sack_enable) {
2694 if (SEQ_GT(tp->snd_una, tp->snd_recover))
2695 tp->snd_recover = tp->snd_una;
2696 }
1c79356b
A
2697 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2698 tp->snd_nxt = tp->snd_una;
4a3eedf9
A
2699
2700 /*
2701 * sowwakeup must happen after snd_una, et al. are updated so that
2702 * the sequence numbers are in sync with so_snd
2703 */
2704 sowwakeup(so);
1c79356b
A
2705
2706 switch (tp->t_state) {
2707
2708 /*
2709 * In FIN_WAIT_1 STATE in addition to the processing
2710 * for the ESTABLISHED state if our FIN is now acknowledged
2711 * then enter FIN_WAIT_2.
2712 */
2713 case TCPS_FIN_WAIT_1:
2714 if (ourfinisacked) {
2715 /*
2716 * If we can't receive any more
2717 * data, then closing user can proceed.
2718 * Starting the timer is contrary to the
2719 * specification, but if we don't get a FIN
2720 * we'll hang forever.
2721 */
2722 if (so->so_state & SS_CANTRCVMORE) {
1c79356b 2723 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
2d21ac55 2724 add_to_time_wait(tp);
4a3eedf9 2725 soisdisconnected(so);
1c79356b 2726 }
1c79356b 2727 tp->t_state = TCPS_FIN_WAIT_2;
c910b4d9 2728 /* fall through and make sure we also recognize data ACKed with the FIN */
1c79356b 2729 }
c910b4d9 2730 tp->t_flags |= TF_ACKNOW;
1c79356b
A
2731 break;
2732
2733 /*
2734 * In CLOSING STATE in addition to the processing for
2735 * the ESTABLISHED state if the ACK acknowledges our FIN
2736 * then enter the TIME-WAIT state, otherwise ignore
2737 * the segment.
2738 */
2739 case TCPS_CLOSING:
2740 if (ourfinisacked) {
2741 tp->t_state = TCPS_TIME_WAIT;
2742 tcp_canceltimers(tp);
2743 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2744 if (tp->cc_recv != 0 &&
b0d623f7 2745 tp->t_starttime < (u_int32_t)tcp_msl)
1c79356b
A
2746 tp->t_timer[TCPT_2MSL] =
2747 tp->t_rxtcur * TCPTV_TWTRUNC;
2748 else
9bccf70c 2749 tp->t_timer[TCPT_2MSL] = 2 * tcp_msl;
1c79356b 2750 add_to_time_wait(tp);
1c79356b
A
2751 soisdisconnected(so);
2752 }
c910b4d9 2753 tp->t_flags |= TF_ACKNOW;
1c79356b
A
2754 break;
2755
2756 /*
2757 * In LAST_ACK, we may still be waiting for data to drain
2758 * and/or to be acked, as well as for the ack of our FIN.
2759 * If our FIN is now acknowledged, delete the TCB,
2760 * enter the closed state and return.
2761 */
2762 case TCPS_LAST_ACK:
2763 if (ourfinisacked) {
2764 tp = tcp_close(tp);
2765 goto drop;
2766 }
2767 break;
2768
2769 /*
2770 * In TIME_WAIT state the only thing that should arrive
2771 * is a retransmission of the remote FIN. Acknowledge
2772 * it and restart the finack timer.
2773 */
2774 case TCPS_TIME_WAIT:
9bccf70c 2775 tp->t_timer[TCPT_2MSL] = 2 * tcp_msl;
1c79356b
A
2776 add_to_time_wait(tp);
2777 goto dropafterack;
2778 }
2779 }
2780
2781step6:
2782 /*
2783 * Update window information.
2784 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2785 */
2786 if ((thflags & TH_ACK) &&
2787 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2788 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2789 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2790 /* keep track of pure window updates */
9bccf70c 2791 if (tlen == 0 &&
1c79356b
A
2792 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2793 tcpstat.tcps_rcvwinupd++;
2794 tp->snd_wnd = tiwin;
2795 tp->snd_wl1 = th->th_seq;
2796 tp->snd_wl2 = th->th_ack;
2797 if (tp->snd_wnd > tp->max_sndwnd)
2798 tp->max_sndwnd = tp->snd_wnd;
2799 needoutput = 1;
2800 }
2801
2802 /*
2803 * Process segments with URG.
2804 */
2805 if ((thflags & TH_URG) && th->th_urp &&
2806 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2807 /*
2808 * This is a kludge, but if we receive and accept
2809 * random urgent pointers, we'll crash in
2810 * soreceive. It's hard to imagine someone
2811 * actually wanting to send this much urgent data.
2812 */
2813 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2814 th->th_urp = 0; /* XXX */
2815 thflags &= ~TH_URG; /* XXX */
2816 goto dodata; /* XXX */
2817 }
2818 /*
2819 * If this segment advances the known urgent pointer,
2820 * then mark the data stream. This should not happen
2821 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2822 * a FIN has been received from the remote side.
2823 * In these states we ignore the URG.
2824 *
2825 * According to RFC961 (Assigned Protocols),
2826 * the urgent pointer points to the last octet
2827 * of urgent data. We continue, however,
2828 * to consider it to indicate the first octet
2829 * of data past the urgent section as the original
2830 * spec states (in one of two places).
2831 */
2832 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2833 tp->rcv_up = th->th_seq + th->th_urp;
2834 so->so_oobmark = so->so_rcv.sb_cc +
2835 (tp->rcv_up - tp->rcv_nxt) - 1;
2836 if (so->so_oobmark == 0) {
2837 so->so_state |= SS_RCVATMARK;
2838 postevent(so, 0, EV_OOB);
2839 }
2840 sohasoutofband(so);
2841 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2842 }
2843 /*
2844 * Remove out of band data so doesn't get presented to user.
2845 * This can happen independent of advancing the URG pointer,
2846 * but if two URG's are pending at once, some out-of-band
2847 * data may creep in... ick.
2848 */
b0d623f7 2849 if (th->th_urp <= (u_int32_t)tlen
1c79356b
A
2850#if SO_OOBINLINE
2851 && (so->so_options & SO_OOBINLINE) == 0
2852#endif
2853 )
9bccf70c
A
2854 tcp_pulloutofband(so, th, m,
2855 drop_hdrlen); /* hdr drop is delayed */
1c79356b
A
2856 } else
2857 /*
2858 * If no out of band data is expected,
2859 * pull receive urgent pointer along
2860 * with the receive window.
2861 */
2862 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2863 tp->rcv_up = tp->rcv_nxt;
2864dodata: /* XXX */
2865
2866 /*
2867 * Process the segment text, merging it into the TCP sequencing queue,
2868 * and arranging for acknowledgment of receipt if necessary.
2869 * This process logically involves adjusting tp->rcv_wnd as data
2870 * is presented to the user (this happens in tcp_usrreq.c,
2871 * case PRU_RCVD). If a FIN has already been received on this
2872 * connection then we just ignore the text.
2873 */
c910b4d9 2874 if ((tlen || (thflags & TH_FIN)) &&
1c79356b 2875 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
8ad349bb
A
2876 tcp_seq save_start = th->th_seq;
2877 tcp_seq save_end = th->th_seq + tlen;
9bccf70c
A
2878 m_adj(m, drop_hdrlen); /* delayed header drop */
2879 /*
8ad349bb
A
2880 * Insert segment which includes th into TCP reassembly queue
2881 * with control block tp. Set thflags to whether reassembly now
2882 * includes a segment with FIN. This handles the common case
2883 * inline (segment is the next to be received on an established
2884 * connection, and the queue is empty), avoiding linkage into
2885 * and removal from the queue and repetition of various
2886 * conversions.
2887 * Set DELACK for segments received in order, but ack
2888 * immediately when segments are out of order (so
2889 * fast retransmit can work).
9bccf70c
A
2890 */
2891 if (th->th_seq == tp->rcv_nxt &&
2892 LIST_EMPTY(&tp->t_segq) &&
2893 TCPS_HAVEESTABLISHED(tp->t_state)) {
91447636 2894 if (DELAY_ACK(tp) && ((tp->t_flags & TF_ACKNOW) == 0)) {
9bccf70c 2895 tp->t_flags |= TF_DELACK;
2d21ac55 2896 tp->t_unacksegs++;
9bccf70c 2897 }
91447636 2898 else {
2d21ac55 2899 tp->t_unacksegs = 0;
9bccf70c 2900 tp->t_flags |= TF_ACKNOW;
91447636 2901 }
9bccf70c
A
2902 tp->rcv_nxt += tlen;
2903 thflags = th->th_flags & TH_FIN;
2904 tcpstat.tcps_rcvpack++;
2905 tcpstat.tcps_rcvbyte += tlen;
2906 ND6_HINT(tp);
2d21ac55 2907 if (sbappendstream(&so->so_rcv, m))
91447636 2908 sorwakeup(so);
9bccf70c
A
2909 } else {
2910 thflags = tcp_reass(tp, th, &tlen, m);
2911 tp->t_flags |= TF_ACKNOW;
2d21ac55 2912 tp->t_unacksegs = 0;
9bccf70c 2913 }
1c79356b 2914
8ad349bb
A
2915 if (tlen > 0 && tp->sack_enable)
2916 tcp_update_sack_list(tp, save_start, save_end);
2917
1c79356b
A
2918 if (tp->t_flags & TF_DELACK)
2919 {
9bccf70c
A
2920#if INET6
2921 if (isipv6) {
2922 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
2923 (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])),
2924 th->th_seq, th->th_ack, th->th_win);
2925 }
2926 else
2927#endif
2928 {
2929 KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport),
2930 (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)),
2931 th->th_seq, th->th_ack, th->th_win);
2932 }
2933
1c79356b
A
2934 }
2935 /*
2936 * Note the amount of data that peer has sent into
2937 * our window, in order to estimate the sender's
2938 * buffer size.
2939 */
2d21ac55
A
2940 len = (u_int)(so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt));
2941 if (len > so->so_rcv.sb_maxused)
2942 so->so_rcv.sb_maxused = len;
1c79356b
A
2943 } else {
2944 m_freem(m);
2945 thflags &= ~TH_FIN;
2946 }
2947
2948 /*
2949 * If FIN is received ACK the FIN and let the user know
2950 * that the connection is closing.
2951 */
2952 if (thflags & TH_FIN) {
2953 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2954 socantrcvmore(so);
2955 postevent(so, 0, EV_FIN);
2956 /*
2957 * If connection is half-synchronized
2958 * (ie NEEDSYN flag on) then delay ACK,
8ad349bb
A
2959 * If connection is half-synchronized
2960 * (ie NEEDSYN flag on) then delay ACK,
2961 * so it may be piggybacked when SYN is sent.
2962 * Otherwise, since we received a FIN then no
2963 * more input can be expected, send ACK now.
1c79356b 2964 */
91447636 2965 if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN)) {
1c79356b 2966 tp->t_flags |= TF_DELACK;
2d21ac55 2967 tp->t_unacksegs++;
1c79356b 2968 }
91447636 2969 else {
1c79356b 2970 tp->t_flags |= TF_ACKNOW;
2d21ac55 2971 tp->t_unacksegs = 0;
91447636 2972 }
1c79356b
A
2973 tp->rcv_nxt++;
2974 }
2975 switch (tp->t_state) {
2976
2977 /*
2978 * In SYN_RECEIVED and ESTABLISHED STATES
2979 * enter the CLOSE_WAIT state.
2980 */
2981 case TCPS_SYN_RECEIVED:
8ad349bb 2982 tp->t_starttime = 0;
1c79356b
A
2983 case TCPS_ESTABLISHED:
2984 tp->t_state = TCPS_CLOSE_WAIT;
2985 break;
2986
2987 /*
2988 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2989 * enter the CLOSING state.
2990 */
2991 case TCPS_FIN_WAIT_1:
2992 tp->t_state = TCPS_CLOSING;
2993 break;
2994
2995 /*
2996 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2997 * starting the time-wait timer, turning off the other
2998 * standard timers.
2999 */
3000 case TCPS_FIN_WAIT_2:
3001 tp->t_state = TCPS_TIME_WAIT;
3002 tcp_canceltimers(tp);
3003 /* Shorten TIME_WAIT [RFC-1644, p.28] */
3004 if (tp->cc_recv != 0 &&
b0d623f7 3005 tp->t_starttime < (u_int32_t)tcp_msl) {
1c79356b
A
3006 tp->t_timer[TCPT_2MSL] =
3007 tp->t_rxtcur * TCPTV_TWTRUNC;
3008 /* For transaction client, force ACK now. */
3009 tp->t_flags |= TF_ACKNOW;
2d21ac55 3010 tp->t_unacksegs = 0;
1c79356b
A
3011 }
3012 else
9bccf70c 3013 tp->t_timer[TCPT_2MSL] = 2 * tcp_msl;
1c79356b
A
3014
3015 add_to_time_wait(tp);
3016 soisdisconnected(so);
3017 break;
3018
3019 /*
3020 * In TIME_WAIT state restart the 2 MSL time_wait timer.
3021 */
3022 case TCPS_TIME_WAIT:
9bccf70c 3023 tp->t_timer[TCPT_2MSL] = 2 * tcp_msl;
1c79356b
A
3024 add_to_time_wait(tp);
3025 break;
3026 }
3027 }
3028#if TCPDEBUG
9bccf70c
A
3029 if (so->so_options & SO_DEBUG)
3030 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
1c79356b 3031 &tcp_savetcp, 0);
1c79356b
A
3032#endif
3033
3034 /*
3035 * Return any desired output.
3036 */
2d21ac55
A
3037 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
3038 tp->t_unacksegs = 0;
1c79356b 3039 (void) tcp_output(tp);
2d21ac55 3040 }
91447636 3041 tcp_unlock(so, 1, 0);
1c79356b
A
3042 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
3043 return;
3044
3045dropafterack:
3046 /*
3047 * Generate an ACK dropping incoming segment if it occupies
3048 * sequence space, where the ACK reflects our state.
3049 *
3050 * We can now skip the test for the RST flag since all
3051 * paths to this code happen after packets containing
3052 * RST have been dropped.
3053 *
3054 * In the SYN-RECEIVED state, don't send an ACK unless the
3055 * segment we received passes the SYN-RECEIVED ACK test.
3056 * If it fails send a RST. This breaks the loop in the
3057 * "LAND" DoS attack, and also prevents an ACK storm
3058 * between two listening ports that have been sent forged
3059 * SYN segments, each with the source address of the other.
3060 */
3061 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3062 (SEQ_GT(tp->snd_una, th->th_ack) ||
9bccf70c
A
3063 SEQ_GT(th->th_ack, tp->snd_max)) ) {
3064 rstreason = BANDLIM_RST_OPENPORT;
1c79356b 3065 goto dropwithreset;
9bccf70c 3066 }
1c79356b 3067#if TCPDEBUG
9bccf70c
A
3068 if (so->so_options & SO_DEBUG)
3069 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
1c79356b 3070 &tcp_savetcp, 0);
1c79356b
A
3071#endif
3072 m_freem(m);
3073 tp->t_flags |= TF_ACKNOW;
2d21ac55 3074 tp->t_unacksegs = 0;
1c79356b 3075 (void) tcp_output(tp);
91447636 3076 tcp_unlock(so, 1, 0);
1c79356b
A
3077 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
3078 return;
91447636
A
3079dropwithresetnosock:
3080 nosock = 1;
1c79356b
A
3081dropwithreset:
3082 /*
3083 * Generate a RST, dropping incoming segment.
3084 * Make ACK acceptable to originator of segment.
3085 * Don't bother to respond if destination was broadcast/multicast.
3086 */
3087 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3088 goto drop;
3089#if INET6
3090 if (isipv6) {
9bccf70c
A
3091 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3092 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3093 goto drop;
1c79356b
A
3094 } else
3095#endif /* INET6 */
14353aa8
A
3096 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3097 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3098 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3099 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
1c79356b 3100 goto drop;
9bccf70c
A
3101 /* IPv6 anycast check is done at tcp6_input() */
3102
3103 /*
3104 * Perform bandwidth limiting.
3105 */
3106#if ICMP_BANDLIM
3107 if (badport_bandlim(rstreason) < 0)
3108 goto drop;
3109#endif
3110
1c79356b 3111#if TCPDEBUG
9bccf70c
A
3112 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3113 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
1c79356b 3114 &tcp_savetcp, 0);
1c79356b
A
3115#endif
3116 if (thflags & TH_ACK)
9bccf70c
A
3117 /* mtod() below is safe as long as hdr dropping is delayed */
3118 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
c910b4d9 3119 TH_RST, ifscope);
1c79356b
A
3120 else {
3121 if (thflags & TH_SYN)
9bccf70c
A
3122 tlen++;
3123 /* mtod() below is safe as long as hdr dropping is delayed */
3124 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
c910b4d9 3125 (tcp_seq)0, TH_RST|TH_ACK, ifscope);
1c79356b
A
3126 }
3127 /* destroy temporarily created socket */
91447636
A
3128 if (dropsocket) {
3129 (void) soabort(so);
3130 tcp_unlock(so, 1, 0);
3131 }
3132 else
3133 if ((inp != NULL) && (nosock == 0))
3134 tcp_unlock(so, 1, 0);
1c79356b
A
3135 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
3136 return;
91447636
A
3137dropnosock:
3138 nosock = 1;
1c79356b
A
3139drop:
3140 /*
3141 * Drop space held by incoming segment and return.
3142 */
3143#if TCPDEBUG
9bccf70c
A
3144 if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3145 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
1c79356b 3146 &tcp_savetcp, 0);
1c79356b
A
3147#endif
3148 m_freem(m);
1c79356b 3149 /* destroy temporarily created socket */
91447636
A
3150 if (dropsocket) {
3151 (void) soabort(so);
3152 tcp_unlock(so, 1, 0);
3153 }
3154 else
3155 if (nosock == 0)
3156 tcp_unlock(so, 1, 0);
1c79356b
A
3157 KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0);
3158 return;
3159}
3160
3161static void
c910b4d9 3162tcp_dooptions(tp, cp, cnt, th, to, input_ifscope)
8ad349bb
A
3163/*
3164 * Parse TCP options and place in tcpopt.
3165 */
1c79356b
A
3166 struct tcpcb *tp;
3167 u_char *cp;
3168 int cnt;
3169 struct tcphdr *th;
3170 struct tcpopt *to;
c910b4d9 3171 unsigned int input_ifscope;
1c79356b
A
3172{
3173 u_short mss = 0;
3174 int opt, optlen;
3175
3176 for (; cnt > 0; cnt -= optlen, cp += optlen) {
3177 opt = cp[0];
3178 if (opt == TCPOPT_EOL)
3179 break;
3180 if (opt == TCPOPT_NOP)
3181 optlen = 1;
3182 else {
9bccf70c
A
3183 if (cnt < 2)
3184 break;
1c79356b 3185 optlen = cp[1];
9bccf70c 3186 if (optlen < 2 || optlen > cnt)
1c79356b
A
3187 break;
3188 }
3189 switch (opt) {
3190
3191 default:
3192 continue;
3193
3194 case TCPOPT_MAXSEG:
3195 if (optlen != TCPOLEN_MAXSEG)
3196 continue;
3197 if (!(th->th_flags & TH_SYN))
3198 continue;
3199 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
b0d623f7
A
3200
3201#if BYTE_ORDER != BIG_ENDIAN
9bccf70c 3202 NTOHS(mss);
b0d623f7
A
3203#endif
3204
1c79356b
A
3205 break;
3206
3207 case TCPOPT_WINDOW:
3208 if (optlen != TCPOLEN_WINDOW)
3209 continue;
3210 if (!(th->th_flags & TH_SYN))
3211 continue;
3212 tp->t_flags |= TF_RCVD_SCALE;
3213 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
3214 break;
3215
3216 case TCPOPT_TIMESTAMP:
3217 if (optlen != TCPOLEN_TIMESTAMP)
3218 continue;
8ad349bb 3219 to->to_flags |= TOF_TS;
1c79356b
A
3220 bcopy((char *)cp + 2,
3221 (char *)&to->to_tsval, sizeof(to->to_tsval));
b0d623f7
A
3222
3223#if BYTE_ORDER != BIG_ENDIAN
1c79356b 3224 NTOHL(to->to_tsval);
b0d623f7
A
3225#endif
3226
1c79356b
A
3227 bcopy((char *)cp + 6,
3228 (char *)&to->to_tsecr, sizeof(to->to_tsecr));
b0d623f7
A
3229
3230#if BYTE_ORDER != BIG_ENDIAN
1c79356b 3231 NTOHL(to->to_tsecr);
b0d623f7 3232#endif
1c79356b
A
3233
3234 /*
3235 * A timestamp received in a SYN makes
3236 * it ok to send timestamp requests and replies.
3237 */
3238 if (th->th_flags & TH_SYN) {
3239 tp->t_flags |= TF_RCVD_TSTMP;
3240 tp->ts_recent = to->to_tsval;
3241 tp->ts_recent_age = tcp_now;
3242 }
3243 break;
8ad349bb
A
3244 case TCPOPT_SACK_PERMITTED:
3245 if (!tcp_do_sack ||
3246 optlen != TCPOLEN_SACK_PERMITTED)
1c79356b 3247 continue;
1c79356b 3248 if (th->th_flags & TH_SYN)
8ad349bb 3249 to->to_flags |= TOF_SACK;
1c79356b 3250 break;
8ad349bb
A
3251 case TCPOPT_SACK:
3252 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
1c79356b 3253 continue;
8ad349bb
A
3254 to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
3255 to->to_sacks = cp + 2;
3256 tcpstat.tcps_sack_rcv_blocks++;
3257
1c79356b
A
3258 break;
3259 }
3260 }
9bccf70c 3261 if (th->th_flags & TH_SYN)
c910b4d9 3262 tcp_mss(tp, mss, input_ifscope); /* sets t_maxseg */
1c79356b
A
3263}
3264
3265/*
3266 * Pull out of band byte out of a segment so
3267 * it doesn't appear in the user's data queue.
3268 * It is still reflected in the segment length for
3269 * sequencing purposes.
3270 */
3271static void
9bccf70c 3272tcp_pulloutofband(so, th, m, off)
1c79356b
A
3273 struct socket *so;
3274 struct tcphdr *th;
3275 register struct mbuf *m;
9bccf70c 3276 int off; /* delayed to be droped hdrlen */
1c79356b 3277{
9bccf70c 3278 int cnt = off + th->th_urp - 1;
1c79356b
A
3279
3280 while (cnt >= 0) {
3281 if (m->m_len > cnt) {
3282 char *cp = mtod(m, caddr_t) + cnt;
3283 struct tcpcb *tp = sototcpcb(so);
3284
3285 tp->t_iobc = *cp;
3286 tp->t_oobflags |= TCPOOB_HAVEDATA;
3287 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3288 m->m_len--;
9bccf70c
A
3289 if (m->m_flags & M_PKTHDR)
3290 m->m_pkthdr.len--;
1c79356b
A
3291 return;
3292 }
3293 cnt -= m->m_len;
3294 m = m->m_next;
3295 if (m == 0)
3296 break;
3297 }
3298 panic("tcp_pulloutofband");
3299}
3300
3301/*
3302 * Collect new round-trip time estimate
3303 * and update averages and current timeout.
3304 */
3305static void
3306tcp_xmit_timer(tp, rtt)
3307 register struct tcpcb *tp;
9bccf70c 3308 int rtt;
1c79356b
A
3309{
3310 register int delta;
3311
3312 tcpstat.tcps_rttupdated++;
3313 tp->t_rttupdated++;
3314 if (tp->t_srtt != 0) {
3315 /*
3316 * srtt is stored as fixed point with 5 bits after the
3317 * binary point (i.e., scaled by 8). The following magic
3318 * is equivalent to the smoothing algorithm in rfc793 with
3319 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3320 * point). Adjust rtt to origin 0.
3321 */
3322 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3323 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3324
3325 if ((tp->t_srtt += delta) <= 0)
3326 tp->t_srtt = 1;
3327
3328 /*
3329 * We accumulate a smoothed rtt variance (actually, a
3330 * smoothed mean difference), then set the retransmit
3331 * timer to smoothed rtt + 4 times the smoothed variance.
3332 * rttvar is stored as fixed point with 4 bits after the
3333 * binary point (scaled by 16). The following is
3334 * equivalent to rfc793 smoothing with an alpha of .75
3335 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
3336 * rfc793's wired-in beta.
3337 */
3338 if (delta < 0)
3339 delta = -delta;
3340 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3341 if ((tp->t_rttvar += delta) <= 0)
3342 tp->t_rttvar = 1;
2d21ac55
A
3343 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
3344 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
1c79356b
A
3345 } else {
3346 /*
3347 * No rtt measurement yet - use the unsmoothed rtt.
3348 * Set the variance to half the rtt (so our first
3349 * retransmit happens at 3*rtt).
3350 */
3351 tp->t_srtt = rtt << TCP_RTT_SHIFT;
3352 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2d21ac55 3353 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
1c79356b 3354 }
9bccf70c 3355 tp->t_rtttime = 0;
1c79356b
A
3356 tp->t_rxtshift = 0;
3357
3358 /*
3359 * the retransmit should happen at rtt + 4 * rttvar.
3360 * Because of the way we do the smoothing, srtt and rttvar
3361 * will each average +1/2 tick of bias. When we compute
3362 * the retransmit timer, we want 1/2 tick of rounding and
3363 * 1 extra tick because of +-1/2 tick uncertainty in the
3364 * firing of the timer. The bias will give us exactly the
3365 * 1.5 tick we need. But, because the bias is
3366 * statistical, we have to test that we don't drop below
3367 * the minimum feasible timer (which is 2 ticks).
3368 */
3369 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
3370 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3371
3372 /*
3373 * We received an ack for a packet that wasn't retransmitted;
3374 * it is probably safe to discard any error indications we've
3375 * received recently. This isn't quite right, but close enough
3376 * for now (a route might have failed after we sent a segment,
3377 * and the return path might not be symmetrical).
3378 */
3379 tp->t_softerror = 0;
3380}
3381
2d21ac55
A
3382static inline unsigned int
3383tcp_maxmtu(struct rtentry *rt)
3384{
3385 unsigned int maxmtu;
3386
b0d623f7 3387 RT_LOCK_ASSERT_HELD(rt);
2d21ac55
A
3388 if (rt->rt_rmx.rmx_mtu == 0)
3389 maxmtu = rt->rt_ifp->if_mtu;
3390 else
3391 maxmtu = MIN(rt->rt_rmx.rmx_mtu, rt->rt_ifp->if_mtu);
3392
3393 return (maxmtu);
3394}
3395
3396#if INET6
3397static inline unsigned int
3398tcp_maxmtu6(struct rtentry *rt)
3399{
3400 unsigned int maxmtu;
3401
b0d623f7
A
3402 RT_LOCK_ASSERT_HELD(rt);
3403 lck_rw_lock_shared(nd_if_rwlock);
2d21ac55
A
3404 if (rt->rt_rmx.rmx_mtu == 0)
3405 maxmtu = IN6_LINKMTU(rt->rt_ifp);
3406 else
3407 maxmtu = MIN(rt->rt_rmx.rmx_mtu, IN6_LINKMTU(rt->rt_ifp));
b0d623f7 3408 lck_rw_done(nd_if_rwlock);
2d21ac55
A
3409
3410 return (maxmtu);
3411}
3412#endif
3413
1c79356b
A
3414/*
3415 * Determine a reasonable value for maxseg size.
3416 * If the route is known, check route for mtu.
3417 * If none, use an mss that can be handled on the outgoing
3418 * interface without forcing IP to fragment; if bigger than
3419 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3420 * to utilize large mbufs. If no route is found, route has no mtu,
3421 * or the destination isn't local, use a default, hopefully conservative
3422 * size (usually 512 or the default IP max size, but no more than the mtu
3423 * of the interface), as we can't discover anything about intervening
3424 * gateways or networks. We also initialize the congestion/slow start
3425 * window to be a single segment if the destination isn't local.
3426 * While looking at the routing entry, we also initialize other path-dependent
3427 * parameters from pre-set or cached values in the routing entry.
3428 *
3429 * Also take into account the space needed for options that we
3430 * send regularly. Make maxseg shorter by that amount to assure
3431 * that we can send maxseg amount of data even when the options
3432 * are present. Store the upper limit of the length of options plus
3433 * data in maxopd.
3434 *
3435 * NOTE that this routine is only called when we process an incoming
3436 * segment, for outgoing segments only tcp_mssopt is called.
3437 *
1c79356b
A
3438 */
3439void
c910b4d9 3440tcp_mss(tp, offer, input_ifscope)
1c79356b
A
3441 struct tcpcb *tp;
3442 int offer;
c910b4d9 3443 unsigned int input_ifscope;
1c79356b
A
3444{
3445 register struct rtentry *rt;
3446 struct ifnet *ifp;
3447 register int rtt, mss;
b0d623f7 3448 u_int32_t bufsize;
1c79356b
A
3449 struct inpcb *inp;
3450 struct socket *so;
3451 struct rmxp_tao *taop;
3452 int origoffer = offer;
b0d623f7 3453 u_int32_t sb_max_corrected;
2d21ac55 3454 int isnetlocal = 0;
1c79356b 3455#if INET6
9bccf70c
A
3456 int isipv6;
3457 int min_protoh;
3458#endif
1c79356b
A
3459
3460 inp = tp->t_inpcb;
9bccf70c
A
3461#if INET6
3462 isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3463 min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
3464 : sizeof (struct tcpiphdr);
3465#else
3466#define min_protoh (sizeof (struct tcpiphdr))
3467#endif
b0d623f7 3468
1c79356b 3469#if INET6
2d21ac55 3470 if (isipv6) {
1c79356b 3471 rt = tcp_rtlookup6(inp);
b0d623f7
A
3472 if (rt != NULL &&
3473 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
3474 IN6_IS_ADDR_LINKLOCAL(&inp->in6p_faddr) ||
3475 rt->rt_gateway->sa_family == AF_LINK))
3476 isnetlocal = TRUE;
2d21ac55 3477 }
1c79356b
A
3478 else
3479#endif /* INET6 */
2d21ac55 3480 {
c910b4d9 3481 rt = tcp_rtlookup(inp, input_ifscope);
b0d623f7
A
3482 if (rt != NULL &&
3483 (rt->rt_gateway->sa_family == AF_LINK ||
3484 rt->rt_ifp->if_flags & IFF_LOOPBACK))
2d21ac55
A
3485 isnetlocal = TRUE;
3486 }
1c79356b
A
3487 if (rt == NULL) {
3488 tp->t_maxopd = tp->t_maxseg =
3489#if INET6
3490 isipv6 ? tcp_v6mssdflt :
3491#endif /* INET6 */
3492 tcp_mssdflt;
3493 return;
3494 }
3495 ifp = rt->rt_ifp;
d12e1678
A
3496 /*
3497 * Slower link window correction:
3498 * If a value is specificied for slowlink_wsize use it for PPP links
3499 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
3500 * it is the default value adversized by pseudo-devices over ppp.
3501 */
3502 if (ifp->if_type == IFT_PPP && slowlink_wsize > 0 &&
3503 ifp->if_baudrate > 9600 && ifp->if_baudrate <= 128000) {
3504 tp->t_flags |= TF_SLOWLINK;
3505 }
1c79356b
A
3506 so = inp->inp_socket;
3507
3508 taop = rmx_taop(rt->rt_rmx);
3509 /*
3510 * Offer == -1 means that we didn't receive SYN yet,
3511 * use cached value in that case;
3512 */
3513 if (offer == -1)
3514 offer = taop->tao_mssopt;
3515 /*
3516 * Offer == 0 means that there was no MSS on the SYN segment,
3517 * in this case we use tcp_mssdflt.
3518 */
3519 if (offer == 0)
3520 offer =
3521#if INET6
3522 isipv6 ? tcp_v6mssdflt :
3523#endif /* INET6 */
3524 tcp_mssdflt;
e5568f75
A
3525 else {
3526 /*
3527 * Prevent DoS attack with too small MSS. Round up
3528 * to at least minmss.
3529 */
3530 offer = max(offer, tcp_minmss);
1c79356b
A
3531 /*
3532 * Sanity check: make sure that maxopd will be large
3533 * enough to allow some data on segments even is the
3534 * all the option space is used (40bytes). Otherwise
3535 * funny things may happen in tcp_output.
3536 */
3537 offer = max(offer, 64);
e5568f75 3538 }
1c79356b
A
3539 taop->tao_mssopt = offer;
3540
3541 /*
3542 * While we're here, check if there's an initial rtt
3543 * or rttvar. Convert from the route-table units
3544 * to scaled multiples of the slow timeout timer.
3545 */
3546 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
3547 /*
3548 * XXX the lock bit for RTT indicates that the value
3549 * is also a minimum value; this is subject to time.
3550 */
3551 if (rt->rt_rmx.rmx_locks & RTV_RTT)
2d21ac55
A
3552 tp->t_rttmin = rtt / (RTM_RTTUNIT / TCP_RETRANSHZ);
3553 else
3554 tp->t_rttmin = isnetlocal ? tcp_TCPTV_MIN : TCP_RETRANSHZ;
3555 tp->t_srtt = rtt / (RTM_RTTUNIT / (TCP_RETRANSHZ * TCP_RTT_SCALE));
1c79356b
A
3556 tcpstat.tcps_usedrtt++;
3557 if (rt->rt_rmx.rmx_rttvar) {
3558 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2d21ac55 3559 (RTM_RTTUNIT / (TCP_RETRANSHZ * TCP_RTTVAR_SCALE));
1c79356b
A
3560 tcpstat.tcps_usedrttvar++;
3561 } else {
3562 /* default variation is +- 1 rtt */
3563 tp->t_rttvar =
3564 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
3565 }
3566 TCPT_RANGESET(tp->t_rxtcur,
9bccf70c
A
3567 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
3568 tp->t_rttmin, TCPTV_REXMTMAX);
1c79356b 3569 }
1c79356b 3570 else
2d21ac55
A
3571 tp->t_rttmin = isnetlocal ? tcp_TCPTV_MIN : TCP_RETRANSHZ;
3572
9bccf70c 3573#if INET6
2d21ac55
A
3574 mss = (isipv6 ? tcp_maxmtu6(rt) : tcp_maxmtu(rt));
3575#else
3576 mss = tcp_maxmtu(rt);
9bccf70c 3577#endif
2d21ac55
A
3578 mss -= min_protoh;
3579
3580 if (rt->rt_rmx.rmx_mtu == 0) {
1c79356b
A
3581#if INET6
3582 if (isipv6) {
2d21ac55 3583 if (!isnetlocal)
1c79356b
A
3584 mss = min(mss, tcp_v6mssdflt);
3585 } else
3586#endif /* INET6 */
2d21ac55 3587 if (!isnetlocal)
1c79356b
A
3588 mss = min(mss, tcp_mssdflt);
3589 }
2d21ac55 3590
1c79356b
A
3591 mss = min(mss, offer);
3592 /*
3593 * maxopd stores the maximum length of data AND options
3594 * in a segment; maxseg is the amount of data in a normal
3595 * segment. We need to store this value (maxopd) apart
3596 * from maxseg, because now every segment carries options
3597 * and thus we normally have somewhat less data in segments.
3598 */
3599 tp->t_maxopd = mss;
3600
3601 /*
8ad349bb
A
3602 * origoffer==-1 indicates, that no segments were received yet.
3603 * In this case we just guess.
1c79356b 3604 */
8ad349bb 3605 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1c79356b
A
3606 (origoffer == -1 ||
3607 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3608 mss -= TCPOLEN_TSTAMP_APPA;
8ad349bb 3609 tp->t_maxseg = mss;
1c79356b 3610
2d21ac55
A
3611 /*
3612 * Calculate corrected value for sb_max; ensure to upgrade the
3613 * numerator for large sb_max values else it will overflow.
3614 */
3615 sb_max_corrected = (sb_max * (u_int64_t)MCLBYTES) / (MSIZE + MCLBYTES);
3616
1c79356b 3617 /*
55e303ae
A
3618 * If there's a pipesize (ie loopback), change the socket
3619 * buffer to that size only if it's bigger than the current
3620 * sockbuf size. Make the socket buffers an integral
1c79356b
A
3621 * number of mss units; if the mss is larger than
3622 * the socket buffer, decrease the mss.
3623 */
3624#if RTV_SPIPE
55e303ae
A
3625 bufsize = rt->rt_rmx.rmx_sendpipe;
3626 if (bufsize < so->so_snd.sb_hiwat)
1c79356b
A
3627#endif
3628 bufsize = so->so_snd.sb_hiwat;
3629 if (bufsize < mss)
3630 mss = bufsize;
3631 else {
2d21ac55
A
3632 bufsize = (((bufsize + (u_int64_t)mss - 1) / (u_int64_t)mss) * (u_int64_t)mss);
3633 if (bufsize > sb_max_corrected)
3634 bufsize = sb_max_corrected;
1c79356b
A
3635 (void)sbreserve(&so->so_snd, bufsize);
3636 }
3637 tp->t_maxseg = mss;
3638
3639#if RTV_RPIPE
55e303ae
A
3640 bufsize = rt->rt_rmx.rmx_recvpipe;
3641 if (bufsize < so->so_rcv.sb_hiwat)
1c79356b
A
3642#endif
3643 bufsize = so->so_rcv.sb_hiwat;
3644 if (bufsize > mss) {
2d21ac55
A
3645 bufsize = (((bufsize + (u_int64_t)mss - 1) / (u_int64_t)mss) * (u_int64_t)mss);
3646 if (bufsize > sb_max_corrected)
3647 bufsize = sb_max_corrected;
1c79356b
A
3648 (void)sbreserve(&so->so_rcv, bufsize);
3649 }
9bccf70c 3650
1c79356b 3651 /*
9bccf70c
A
3652 * Set the slow-start flight size depending on whether this
3653 * is a local network or not.
1c79356b 3654 */
2d21ac55 3655 if (isnetlocal)
9bccf70c
A
3656 tp->snd_cwnd = mss * ss_fltsz_local;
3657 else
3658 tp->snd_cwnd = mss * ss_fltsz;
1c79356b
A
3659
3660 if (rt->rt_rmx.rmx_ssthresh) {
3661 /*
3662 * There's some sort of gateway or interface
3663 * buffer limit on the path. Use this to set
3664 * the slow start threshhold, but set the
3665 * threshold to no less than 2*mss.
3666 */
3667 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
3668 tcpstat.tcps_usedssthresh++;
b0d623f7 3669 } else {
cf7d32b8 3670 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
b0d623f7 3671 }
cf7d32b8 3672
b0d623f7
A
3673 /* Route locked during lookup above */
3674 RT_UNLOCK(rt);
1c79356b
A
3675}
3676
3677/*
3678 * Determine the MSS option to send on an outgoing SYN.
3679 */
3680int
9bccf70c 3681tcp_mssopt(tp)
1c79356b 3682 struct tcpcb *tp;
1c79356b
A
3683{
3684 struct rtentry *rt;
8ad349bb 3685 int mss;
1c79356b 3686#if INET6
9bccf70c
A
3687 int isipv6;
3688 int min_protoh;
3689#endif
1c79356b 3690
9bccf70c
A
3691#if INET6
3692 isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3693 min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
3694 : sizeof (struct tcpiphdr);
3695#else
3696#define min_protoh (sizeof (struct tcpiphdr))
3697#endif
b0d623f7 3698
1c79356b
A
3699#if INET6
3700 if (isipv6)
3701 rt = tcp_rtlookup6(tp->t_inpcb);
3702 else
3703#endif /* INET6 */
c910b4d9 3704 rt = tcp_rtlookup(tp->t_inpcb, IFSCOPE_NONE);
2d21ac55 3705 if (rt == NULL) {
2d21ac55 3706 return (
1c79356b
A
3707#if INET6
3708 isipv6 ? tcp_v6mssdflt :
3709#endif /* INET6 */
2d21ac55
A
3710 tcp_mssdflt);
3711 }
d12e1678
A
3712 /*
3713 * Slower link window correction:
3714 * If a value is specificied for slowlink_wsize use it for PPP links
3715 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
3716 * it is the default value adversized by pseudo-devices over ppp.
3717 */
3718 if (rt->rt_ifp->if_type == IFT_PPP && slowlink_wsize > 0 &&
3719 rt->rt_ifp->if_baudrate > 9600 && rt->rt_ifp->if_baudrate <= 128000) {
3720 tp->t_flags |= TF_SLOWLINK;
3721 }
1c79356b 3722
8ad349bb 3723#if INET6
2d21ac55
A
3724 mss = (isipv6 ? tcp_maxmtu6(rt) : tcp_maxmtu(rt));
3725#else
3726 mss = tcp_maxmtu(rt);
8ad349bb 3727#endif
b0d623f7
A
3728 /* Route locked during lookup above */
3729 RT_UNLOCK(rt);
8ad349bb 3730 return (mss - min_protoh);
9bccf70c
A
3731}
3732
9bccf70c 3733/*
8ad349bb
A
3734 * On a partial ack arrives, force the retransmission of the
3735 * next unacknowledged segment. Do not clear tp->t_dupacks.
3736 * By setting snd_nxt to ti_ack, this forces retransmission timer to
3737 * be started again.
9bccf70c 3738 */
8ad349bb
A
3739static void
3740tcp_newreno_partial_ack(tp, th)
9bccf70c
A
3741 struct tcpcb *tp;
3742 struct tcphdr *th;
3743{
9bccf70c 3744 tcp_seq onxt = tp->snd_nxt;
b0d623f7 3745 u_int32_t ocwnd = tp->snd_cwnd;
9bccf70c 3746 tp->t_timer[TCPT_REXMT] = 0;
9bccf70c
A
3747 tp->t_rtttime = 0;
3748 tp->snd_nxt = th->th_ack;
3749 /*
3750 * Set snd_cwnd to one segment beyond acknowledged offset
3751 * (tp->snd_una has not yet been updated when this function
3752 * is called)
3753 */
3754 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
91447636 3755 tp->t_flags |= TF_ACKNOW;
2d21ac55 3756 tp->t_unacksegs = 0;
9bccf70c
A
3757 (void) tcp_output(tp);
3758 tp->snd_cwnd = ocwnd;
3759 if (SEQ_GT(onxt, tp->snd_nxt))
3760 tp->snd_nxt = onxt;
3761 /*
3762 * Partial window deflation. Relies on fact that tp->snd_una
3763 * not updated yet.
3764 */
2d21ac55
A
3765 if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3766 tp->snd_cwnd -= th->th_ack - tp->snd_una;
3767 else
3768 tp->snd_cwnd = 0;
3769 tp->snd_cwnd += tp->t_maxseg;
3770
1c79356b 3771}
91447636
A
3772
3773/*
3774 * Drop a random TCP connection that hasn't been serviced yet and
3775 * is eligible for discard. There is a one in qlen chance that
3776 * we will return a null, saying that there are no dropable
3777 * requests. In this case, the protocol specific code should drop
3778 * the new request. This insures fairness.
3779 *
3780 * The listening TCP socket "head" must be locked
3781 */
3782static int
2d21ac55 3783tcp_dropdropablreq(struct socket *head)
91447636 3784{
2d21ac55 3785 struct socket *so, *sonext;
91447636
A
3786 unsigned int i, j, qlen;
3787 static int rnd;
3788 static struct timeval old_runtime;
3789 static unsigned int cur_cnt, old_cnt;
3790 struct timeval tv;
3791 struct inpcb *inp = NULL;
3a60a9f5 3792 struct tcpcb *tp;
2d21ac55
A
3793
3794 if ((head->so_options & SO_ACCEPTCONN) == 0)
3795 return 0;
3796
3797 so = TAILQ_FIRST(&head->so_incomp);
3798 if (!so)
3799 return 0;
3800
91447636
A
3801 microtime(&tv);
3802 if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) {
3803 old_runtime = tv;
3804 old_cnt = cur_cnt / i;
3805 cur_cnt = 0;
3806 }
3807
91447636
A
3808
3809 qlen = head->so_incqlen;
3810 if (++cur_cnt > qlen || old_cnt > qlen) {
3811 rnd = (314159 * rnd + 66329) & 0xffff;
3812 j = ((qlen + 1) * rnd) >> 16;
3813
3814 while (j-- && so)
3815 so = TAILQ_NEXT(so, so_list);
3816 }
2d21ac55 3817 /* Find a connection that is not already closing (or being served) */
91447636
A
3818 while (so) {
3819 inp = (struct inpcb *)so->so_pcb;
3820
2d21ac55
A
3821 sonext = TAILQ_NEXT(so, so_list);
3822
3823 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
3824 /* Avoid the issue of a socket being accepted by one input thread
3825 * and being dropped by another input thread.
3826 * If we can't get a hold on this mutex, then grab the next socket in line.
3827 */
3828 if (lck_mtx_try_lock(inp->inpcb_mtx)) {
3829 so->so_usecount++;
3830 if ((so->so_usecount == 2) && so->so_state & SS_INCOMP)
3831 break;
3832 else {/* don't use if beeing accepted or used in any other way */
3833 in_pcb_checkstate(inp, WNT_RELEASE, 1);
3834 tcp_unlock(so, 1, 0);
3835 }
3836 }
b0d623f7
A
3837 else {
3838 /* do not try to lock the inp in in_pcb_checkstate
3839 * because the lock is already held in some other thread.
3840 * Only drop the inp_wntcnt reference.
3841 */
3842 in_pcb_checkstate(inp, WNT_RELEASE, 1);
3843 }
2d21ac55
A
3844 }
3845 so = sonext;
91447636 3846
91447636
A
3847 }
3848 if (!so)
3849 return 0;
2d21ac55 3850
ff6e181a
A
3851 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3852 tcp_unlock(head, 0, 0);
3853
2d21ac55
A
3854 /* Makes sure socket is still in the right state to be discarded */
3855
91447636
A
3856 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
3857 tcp_unlock(so, 1, 0);
2d21ac55 3858 tcp_lock(head, 0, 0);
91447636
A
3859 return 0;
3860 }
2d21ac55
A
3861
3862 if (so->so_usecount != 2 || !(so->so_state & SS_INCOMP)) {
3863 /* do not discard: that socket is beeing accepted */
3864 tcp_unlock(so, 1, 0);
3865 tcp_lock(head, 0, 0);
3866 return 0;
3867 }
3868
3a60a9f5 3869 so->so_head = NULL;
91447636 3870
3a60a9f5
A
3871 /*
3872 * We do not want to lose track of the PCB right away in case we receive
3873 * more segments from the peer
3874 */
3875 tp = sototcpcb(so);
2d21ac55
A
3876 so->so_flags |= SOF_OVERFLOW;
3877 tp->t_state = TCPS_TIME_WAIT;
3878 (void) tcp_close(tp);
3879 tp->t_unacksegs = 0;
3a60a9f5 3880 tcpstat.tcps_drops++;
3a60a9f5
A
3881 tcp_canceltimers(tp);
3882 add_to_time_wait(tp);
3883
91447636 3884 tcp_unlock(so, 1, 0);
3a60a9f5 3885 tcp_lock(head, 0, 0);
2d21ac55
A
3886 head->so_incqlen--;
3887 head->so_qlen--;
91447636 3888 return 1;
91447636
A
3889}
3890
8ad349bb
A
3891static int
3892tcp_getstat SYSCTL_HANDLER_ARGS
3893{
2d21ac55 3894#pragma unused(oidp, arg1, arg2)
8ad349bb
A
3895
3896 int error;
3897
3898 if (req->oldptr == 0) {
3899 req->oldlen= (size_t)sizeof(struct tcpstat);
3900 }
3901
2d21ac55 3902 error = SYSCTL_OUT(req, &tcpstat, MIN(sizeof (tcpstat), req->oldlen));
8ad349bb
A
3903
3904 return (error);
3905
3906}
3907
3908SYSCTL_PROC(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD, 0, 0,
3909 tcp_getstat, "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
3910
2d21ac55
A
3911static int
3912sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
3913{
3914#pragma unused(arg1, arg2)
3915
3916 int error, val = tcprexmtthresh;
3917
3918 error = sysctl_handle_int(oidp, &val, 0, req);
3919 if (error || !req->newptr)
3920 return (error);
3921
3922 /*
3923 * Constrain the number of duplicate ACKs
3924 * to consider for TCP fast retransmit
3925 * to either 2 or 3
3926 */
3927
3928 if (val < 2 || val > 3)
3929 return (EINVAL);
3930
3931 tcprexmtthresh = val;
3932
3933 return (0);
3934}
91447636 3935
2d21ac55
A
3936SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmt_thresh, CTLTYPE_INT|CTLFLAG_RW,
3937 &tcprexmtthresh, 0, &sysctl_rexmtthresh, "I", "Duplicate ACK Threshold for Fast Retransmit");