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