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