Add bzip2 to the bootstrap tools list. The compat libs (if enabled in
[dragonfly.git] / sys / netinet / tcp_input.c
CommitLineData
984263bc 1/*
66d6c637
JH
2 * Copyright (c) 2002, 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2002, 2003, 2004 The DragonFly Project. All rights reserved.
95b22adf 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
95b22adf 7 *
66d6c637
JH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
95b22adf 19 *
66d6c637
JH
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 * Copyright (c) 2002, 2003, 2004 Jeffrey M. Hsu. All rights reserved.
36 *
37 * License terms: all terms for the DragonFly license above plus the following:
38 *
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 *
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
44 *
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
48 * apply.
49 */
50
51/*
984263bc
MD
52 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
53 * The Regents of the University of California. All rights reserved.
54 *
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
57 * are met:
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
70 *
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81 * SUCH DAMAGE.
82 *
83 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
84 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.38 2003/05/21 04:46:41 cjc Exp $
2b1ce38a 85 * $DragonFly: src/sys/netinet/tcp_input.c,v 1.33 2004/08/03 00:04:13 dillon Exp $
984263bc
MD
86 */
87
88#include "opt_ipfw.h" /* for ipfw_fwd */
89#include "opt_inet6.h"
90#include "opt_ipsec.h"
91#include "opt_tcpdebug.h"
92#include "opt_tcp_input.h"
93
94#include <sys/param.h>
95#include <sys/systm.h>
96#include <sys/kernel.h>
97#include <sys/sysctl.h>
98#include <sys/malloc.h>
99#include <sys/mbuf.h>
100#include <sys/proc.h> /* for proc0 declaration */
101#include <sys/protosw.h>
102#include <sys/socket.h>
103#include <sys/socketvar.h>
104#include <sys/syslog.h>
3f9db7f8 105#include <sys/in_cksum.h>
984263bc
MD
106
107#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
a00138cb 108#include <machine/stdarg.h>
984263bc
MD
109
110#include <net/if.h>
111#include <net/route.h>
112
113#include <netinet/in.h>
114#include <netinet/in_systm.h>
115#include <netinet/ip.h>
95b22adf 116#include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
984263bc 117#include <netinet/in_var.h>
95b22adf 118#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
984263bc
MD
119#include <netinet/in_pcb.h>
120#include <netinet/ip_var.h>
121#include <netinet/ip6.h>
122#include <netinet/icmp6.h>
123#include <netinet6/nd6.h>
124#include <netinet6/ip6_var.h>
125#include <netinet6/in6_pcb.h>
126#include <netinet/tcp.h>
127#include <netinet/tcp_fsm.h>
128#include <netinet/tcp_seq.h>
129#include <netinet/tcp_timer.h>
130#include <netinet/tcp_var.h>
131#include <netinet6/tcp6_var.h>
132#include <netinet/tcpip.h>
95b22adf 133
984263bc
MD
134#ifdef TCPDEBUG
135#include <netinet/tcp_debug.h>
136
95b22adf 137u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
984263bc 138struct tcphdr tcp_savetcp;
95b22adf 139#endif
984263bc
MD
140
141#ifdef FAST_IPSEC
142#include <netipsec/ipsec.h>
143#include <netipsec/ipsec6.h>
144#endif
145
146#ifdef IPSEC
147#include <netinet6/ipsec.h>
148#include <netinet6/ipsec6.h>
d2438d69 149#include <netproto/key/key.h>
95b22adf 150#endif
984263bc 151
984263bc
MD
152MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
153
154static const int tcprexmtthresh = 3;
155tcp_cc tcp_ccgen;
984263bc 156static int log_in_vain = 0;
d24ce1dc 157SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
984263bc
MD
158 &log_in_vain, 0, "Log all incoming TCP connections");
159
160static int blackhole = 0;
161SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
162 &blackhole, 0, "Do not send RST when dropping refused connections");
163
164int tcp_delack_enabled = 1;
d24ce1dc
JH
165SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
166 &tcp_delack_enabled, 0,
984263bc
MD
167 "Delay ACK to try and piggyback it onto a data packet");
168
169#ifdef TCP_DROP_SYNFIN
170static int drop_synfin = 0;
171SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
172 &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
173#endif
174
33abdd1c
MD
175static int tcp_do_limitedtransmit = 1;
176SYSCTL_INT(_net_inet_tcp, OID_AUTO, limitedtransmit, CTLFLAG_RW,
177 &tcp_do_limitedtransmit, 0, "Enable RFC 3042 (Limited Transmit)");
178
8819433a
JH
179static int tcp_do_early_retransmit = 0;
180SYSCTL_INT(_net_inet_tcp, OID_AUTO, earlyretransmit, CTLFLAG_RW,
181 &tcp_do_early_retransmit, 0, "Early retransmit");
182
4b52d5ee
JH
183static int tcp_do_rfc3390 = 1;
184SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
185 &tcp_do_rfc3390, 0,
186 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
187
efd4b327
JH
188static int tcp_do_eifel_detect = 1;
189SYSCTL_INT(_net_inet_tcp, OID_AUTO, eifel, CTLFLAG_RW,
190 &tcp_do_eifel_detect, 0, "Eifel detection algorithm (RFC 3522)");
191
3edf7c37
RG
192SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
193 "TCP Segment Reassembly Queue");
194
195int tcp_reass_maxseg = 0;
196SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RD,
197 &tcp_reass_maxseg, 0,
198 "Global maximum number of TCP Segments in Reassembly Queue");
199
200int tcp_reass_qsize = 0;
201SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
202 &tcp_reass_qsize, 0,
203 "Global number of TCP Segments currently in Reassembly Queue");
204
205static int tcp_reass_overflows = 0;
206SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
207 &tcp_reass_overflows, 0,
208 "Global number of TCP Segment Reassembly Queue Overflows");
209
95b22adf 210static void tcp_dooptions(struct tcpopt *, u_char *, int, boolean_t);
984263bc
MD
211static void tcp_pulloutofband(struct socket *,
212 struct tcphdr *, struct mbuf *, int);
213static int tcp_reass(struct tcpcb *, struct tcphdr *, int *,
214 struct mbuf *);
215static void tcp_xmit_timer(struct tcpcb *, int);
216static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
217
218/* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
219#ifdef INET6
220#define ND6_HINT(tp) \
221do { \
222 if ((tp) && (tp)->t_inpcb && \
223 ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
224 (tp)->t_inpcb->in6p_route.ro_rt) \
225 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
226} while (0)
227#else
228#define ND6_HINT(tp)
229#endif
230
231/*
232 * Indicate whether this ack should be delayed. We can delay the ack if
233 * - delayed acks are enabled and
234 * - there is no delayed ack timer in progress and
235 * - our last ack wasn't a 0-sized window. We never want to delay
236 * the ack that opens up a 0-sized window.
237 */
238#define DELAY_ACK(tp) \
239 (tcp_delack_enabled && !callout_pending(tp->tt_delack) && \
240 (tp->t_flags & TF_RXWIN0SENT) == 0)
241
242static int
95b22adf 243tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
984263bc
MD
244{
245 struct tseg_qent *q;
246 struct tseg_qent *p = NULL;
247 struct tseg_qent *nq;
248 struct tseg_qent *te;
249 struct socket *so = tp->t_inpcb->inp_socket;
250 int flags;
251
252 /*
253 * Call with th==0 after become established to
254 * force pre-ESTABLISHED data up to user socket.
255 */
256 if (th == 0)
257 goto present;
258
3edf7c37
RG
259 /*
260 * Limit the number of segments in the reassembly queue to prevent
261 * holding on to too many segments (and thus running out of mbufs).
262 * Make sure to let the missing segment through which caused this
263 * queue. Always keep one global queue entry spare to be able to
264 * process the missing segment.
265 */
266 if (th->th_seq != tp->rcv_nxt &&
267 tcp_reass_qsize + 1 >= tcp_reass_maxseg) {
268 tcp_reass_overflows++;
269 tcpstat.tcps_rcvmemdrop++;
270 m_freem(m);
271 return (0);
272 }
273
ba4e3dbe 274 /* Allocate a new queue entry. */
984263bc 275 MALLOC(te, struct tseg_qent *, sizeof(struct tseg_qent), M_TSEGQ,
ba4e3dbe 276 M_INTWAIT | M_NULLOK);
984263bc
MD
277 if (te == NULL) {
278 tcpstat.tcps_rcvmemdrop++;
279 m_freem(m);
280 return (0);
281 }
3edf7c37 282 tcp_reass_qsize++;
984263bc
MD
283
284 /*
285 * Find a segment which begins after this one does.
286 */
287 LIST_FOREACH(q, &tp->t_segq, tqe_q) {
288 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
289 break;
290 p = q;
291 }
292
293 /*
294 * If there is a preceding segment, it may provide some of
295 * our data already. If so, drop the data from the incoming
296 * segment. If it provides all of our data, drop us.
297 */
298 if (p != NULL) {
2256ba69 299 int i;
984263bc
MD
300 /* conversion to int (in i) handles seq wraparound */
301 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
302 if (i > 0) {
303 if (i >= *tlenp) {
304 tcpstat.tcps_rcvduppack++;
305 tcpstat.tcps_rcvdupbyte += *tlenp;
306 m_freem(m);
307 free(te, M_TSEGQ);
3edf7c37 308 tcp_reass_qsize--;
984263bc
MD
309 /*
310 * Try to present any queued data
311 * at the left window edge to the user.
312 * This is needed after the 3-WHS
313 * completes.
314 */
315 goto present; /* ??? */
316 }
317 m_adj(m, i);
318 *tlenp -= i;
319 th->th_seq += i;
320 }
321 }
322 tcpstat.tcps_rcvoopack++;
323 tcpstat.tcps_rcvoobyte += *tlenp;
324
325 /*
326 * While we overlap succeeding segments trim them or,
327 * if they are completely covered, dequeue them.
328 */
329 while (q) {
2256ba69 330 int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
984263bc
MD
331 if (i <= 0)
332 break;
333 if (i < q->tqe_len) {
334 q->tqe_th->th_seq += i;
335 q->tqe_len -= i;
336 m_adj(q->tqe_m, i);
337 break;
338 }
339
340 nq = LIST_NEXT(q, tqe_q);
341 LIST_REMOVE(q, tqe_q);
342 m_freem(q->tqe_m);
343 free(q, M_TSEGQ);
3edf7c37 344 tcp_reass_qsize--;
984263bc
MD
345 q = nq;
346 }
347
348 /* Insert the new segment queue entry into place. */
349 te->tqe_m = m;
350 te->tqe_th = th;
351 te->tqe_len = *tlenp;
352
353 if (p == NULL) {
354 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
355 } else {
356 LIST_INSERT_AFTER(p, te, tqe_q);
357 }
358
359present:
360 /*
361 * Present data to user, advancing rcv_nxt through
362 * completed sequence space.
363 */
364 if (!TCPS_HAVEESTABLISHED(tp->t_state))
365 return (0);
366 q = LIST_FIRST(&tp->t_segq);
367 if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
368 return (0);
369 do {
370 tp->rcv_nxt += q->tqe_len;
371 flags = q->tqe_th->th_flags & TH_FIN;
372 nq = LIST_NEXT(q, tqe_q);
373 LIST_REMOVE(q, tqe_q);
374 if (so->so_state & SS_CANTRCVMORE)
375 m_freem(q->tqe_m);
376 else
377 sbappend(&so->so_rcv, q->tqe_m);
378 free(q, M_TSEGQ);
3edf7c37 379 tcp_reass_qsize--;
984263bc
MD
380 q = nq;
381 } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
382 ND6_HINT(tp);
383 sorwakeup(so);
384 return (flags);
385}
386
387/*
388 * TCP input routine, follows pages 65-76 of the
389 * protocol specification dated September, 1981 very closely.
390 */
391#ifdef INET6
392int
95b22adf 393tcp6_input(struct mbuf **mp, int *offp, int proto)
984263bc 394{
2256ba69 395 struct mbuf *m = *mp;
984263bc
MD
396 struct in6_ifaddr *ia6;
397
398 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
399
400 /*
401 * draft-itojun-ipv6-tcp-to-anycast
402 * better place to put this in?
403 */
404 ia6 = ip6_getdstifaddr(m);
d24ce1dc 405 if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
984263bc
MD
406 struct ip6_hdr *ip6;
407
408 ip6 = mtod(m, struct ip6_hdr *);
409 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
410 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
d24ce1dc 411 return (IPPROTO_DONE);
984263bc
MD
412 }
413
414 tcp_input(m, *offp, proto);
d24ce1dc 415 return (IPPROTO_DONE);
984263bc
MD
416}
417#endif
418
419void
a00138cb 420tcp_input(struct mbuf *m, ...)
984263bc 421{
a00138cb
JS
422 __va_list ap;
423 int off0, proto;
2256ba69
RG
424 struct tcphdr *th;
425 struct ip *ip = NULL;
426 struct ipovly *ipov;
427 struct inpcb *inp = NULL;
984263bc
MD
428 u_char *optp = NULL;
429 int optlen = 0;
430 int len, tlen, off;
431 int drop_hdrlen;
2256ba69
RG
432 struct tcpcb *tp = NULL;
433 int thflags;
984263bc
MD
434 struct socket *so = 0;
435 int todrop, acked, ourfinisacked, needoutput = 0;
436 u_long tiwin;
437 struct tcpopt to; /* options in this segment */
438 struct rmxp_tao *taop; /* pointer to our TAO cache entry */
439 struct rmxp_tao tao_noncached; /* in case there's no cached entry */
440 struct sockaddr_in *next_hop = NULL;
441 int rstreason; /* For badport_bandlim accounting purposes */
d371a63a 442 int cpu;
984263bc
MD
443 struct ip6_hdr *ip6 = NULL;
444#ifdef INET6
d24ce1dc 445 boolean_t isipv6;
984263bc 446#else
d24ce1dc 447 const boolean_t isipv6 = FALSE;
984263bc
MD
448#endif
449#ifdef TCPDEBUG
450 short ostate = 0;
451#endif
452
a00138cb
JS
453 __va_start(ap, m);
454 off0 = __va_arg(ap, int);
455 proto = __va_arg(ap, int);
456 __va_end(ap);
457
d24ce1dc
JH
458 tcpstat.tcps_rcvtotal++;
459
460 /* Grab info from and strip MT_TAG mbufs prepended to the chain. */
461 while (m->m_type == MT_TAG) {
984263bc
MD
462 if (m->_m_tag_id == PACKET_TAG_IPFORWARD)
463 next_hop = (struct sockaddr_in *)m->m_hdr.mh_data;
d24ce1dc 464 m = m->m_next;
984263bc 465 }
d24ce1dc 466
984263bc 467#ifdef INET6
d24ce1dc 468 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? TRUE : FALSE;
984263bc 469#endif
984263bc
MD
470
471 if (isipv6) {
472 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
473 ip6 = mtod(m, struct ip6_hdr *);
474 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
475 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
476 tcpstat.tcps_rcvbadsum++;
477 goto drop;
478 }
479 th = (struct tcphdr *)((caddr_t)ip6 + off0);
480
481 /*
482 * Be proactive about unspecified IPv6 address in source.
483 * As we use all-zero to indicate unbounded/unconnected pcb,
484 * unspecified IPv6 address can be used to confuse us.
485 *
486 * Note that packets with unspecified IPv6 destination is
487 * already dropped in ip6_input.
488 */
489 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
490 /* XXX stat */
491 goto drop;
492 }
493 } else {
494 /*
495 * Get IP and TCP header together in first mbuf.
496 * Note: IP leaves IP header in first mbuf.
497 */
498 if (off0 > sizeof(struct ip)) {
bddf0751 499 ip_stripoptions(m);
984263bc
MD
500 off0 = sizeof(struct ip);
501 }
55d829f8
JH
502 /* already checked and pulled up in ip_demux() */
503 KASSERT(m->m_len >= sizeof(struct tcpiphdr),
504 ("TCP header not in one mbuf"));
984263bc
MD
505 ip = mtod(m, struct ip *);
506 ipov = (struct ipovly *)ip;
507 th = (struct tcphdr *)((caddr_t)ip + off0);
508 tlen = ip->ip_len;
509
510 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
511 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
512 th->th_sum = m->m_pkthdr.csum_data;
513 else
514 th->th_sum = in_pseudo(ip->ip_src.s_addr,
515 ip->ip_dst.s_addr,
516 htonl(m->m_pkthdr.csum_data +
517 ip->ip_len +
518 IPPROTO_TCP));
519 th->th_sum ^= 0xffff;
520 } else {
521 /*
522 * Checksum extended TCP header and data.
523 */
524 len = sizeof(struct ip) + tlen;
525 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
526 ipov->ih_len = (u_short)tlen;
527 ipov->ih_len = htons(ipov->ih_len);
528 th->th_sum = in_cksum(m, len);
529 }
530 if (th->th_sum) {
531 tcpstat.tcps_rcvbadsum++;
532 goto drop;
533 }
534#ifdef INET6
535 /* Re-initialization for later version check */
536 ip->ip_v = IPVERSION;
537#endif
538 }
539
540 /*
541 * Check that TCP offset makes sense,
542 * pull out TCP options and adjust length. XXX
543 */
544 off = th->th_off << 2;
55d829f8
JH
545 /* already checked and pulled up in ip_demux() */
546 KASSERT(off >= sizeof(struct tcphdr) && off <= tlen,
547 ("bad TCP data offset"));
984263bc
MD
548 tlen -= off; /* tlen is used instead of ti->ti_len */
549 if (off > sizeof(struct tcphdr)) {
550 if (isipv6) {
551 IP6_EXTHDR_CHECK(m, off0, off, );
552 ip6 = mtod(m, struct ip6_hdr *);
553 th = (struct tcphdr *)((caddr_t)ip6 + off0);
554 } else {
55d829f8
JH
555 /* already pulled up in ip_demux() */
556 KASSERT(m->m_len >= sizeof(struct ip) + off,
557 ("TCP header and options not in one mbuf"));
984263bc
MD
558 }
559 optlen = off - sizeof(struct tcphdr);
560 optp = (u_char *)(th + 1);
561 }
562 thflags = th->th_flags;
563
564#ifdef TCP_DROP_SYNFIN
565 /*
566 * If the drop_synfin option is enabled, drop all packets with
567 * both the SYN and FIN bits set. This prevents e.g. nmap from
568 * identifying the TCP/IP stack.
569 *
570 * This is a violation of the TCP specification.
571 */
572 if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
573 goto drop;
574#endif
575
576 /*
577 * Convert TCP protocol specific fields to host format.
578 */
579 th->th_seq = ntohl(th->th_seq);
580 th->th_ack = ntohl(th->th_ack);
581 th->th_win = ntohs(th->th_win);
582 th->th_urp = ntohs(th->th_urp);
583
584 /*
d24ce1dc 585 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
984263bc
MD
586 * until after ip6_savecontrol() is called and before other functions
587 * which don't want those proto headers.
588 * Because ip6_savecontrol() is going to parse the mbuf to
589 * search for data to be passed up to user-land, it wants mbuf
590 * parameters to be unchanged.
591 * XXX: the call of ip6_savecontrol() has been obsoleted based on
592 * latest version of the advanced API (20020110).
593 */
594 drop_hdrlen = off0 + off;
595
596 /*
597 * Locate pcb for segment.
598 */
599findpcb:
600 /* IPFIREWALL_FORWARD section */
d24ce1dc 601 if (next_hop != NULL && !isipv6) { /* IPv6 support is not there yet */
984263bc
MD
602 /*
603 * Transparently forwarded. Pretend to be the destination.
d24ce1dc 604 * already got one like this?
984263bc 605 */
6ca1a1cd 606 cpu = mycpu->gd_cpuid;
6ca1a1cd 607 inp = in_pcblookup_hash(&tcbinfo[cpu],
d371a63a 608 ip->ip_src, th->th_sport,
984263bc
MD
609 ip->ip_dst, th->th_dport,
610 0, m->m_pkthdr.rcvif);
611 if (!inp) {
83be63fe
JH
612 /*
613 * It's new. Try to find the ambushing socket.
614 */
615
616 /*
617 * The rest of the ipfw code stores the port in
618 * host order. XXX
619 * (The IP address is still in network order.)
620 */
621 in_port_t dport = next_hop->sin_port ?
622 htons(next_hop->sin_port) :
623 th->th_dport;
624
d371a63a 625 cpu = tcp_addrcpu(ip->ip_src.s_addr, th->th_sport,
83be63fe 626 next_hop->sin_addr.s_addr, dport);
d371a63a 627 inp = in_pcblookup_hash(&tcbinfo[cpu],
984263bc 628 ip->ip_src, th->th_sport,
83be63fe 629 next_hop->sin_addr, dport,
984263bc
MD
630 1, m->m_pkthdr.rcvif);
631 }
632 } else {
6ca1a1cd 633 if (isipv6) {
d371a63a 634 inp = in6_pcblookup_hash(&tcbinfo[0],
984263bc
MD
635 &ip6->ip6_src, th->th_sport,
636 &ip6->ip6_dst, th->th_dport,
637 1, m->m_pkthdr.rcvif);
6ca1a1cd 638 } else {
6ca1a1cd 639 cpu = mycpu->gd_cpuid;
6ca1a1cd 640 inp = in_pcblookup_hash(&tcbinfo[cpu],
984263bc
MD
641 ip->ip_src, th->th_sport,
642 ip->ip_dst, th->th_dport,
643 1, m->m_pkthdr.rcvif);
6ca1a1cd 644 }
984263bc
MD
645 }
646
647#ifdef IPSEC
648 if (isipv6) {
649 if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
650 ipsec6stat.in_polvio++;
651 goto drop;
652 }
653 } else {
654 if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
655 ipsecstat.in_polvio++;
656 goto drop;
657 }
658 }
659#endif
660#ifdef FAST_IPSEC
661 if (isipv6) {
662 if (inp != NULL && ipsec6_in_reject(m, inp)) {
663 goto drop;
664 }
665 } else {
666 if (inp != NULL && ipsec4_in_reject(m, inp)) {
667 goto drop;
668 }
669 }
670#endif
671
672 /*
673 * If the state is CLOSED (i.e., TCB does not exist) then
674 * all data in the incoming segment is discarded.
675 * If the TCB exists but is in CLOSED state, it is embryonic,
676 * but should either do a listen or a connect soon.
677 */
678 if (inp == NULL) {
679 if (log_in_vain) {
680#ifdef INET6
681 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
682#else
d24ce1dc 683 char dbuf[4 * sizeof "123"], sbuf[4 * sizeof "123"];
984263bc
MD
684#endif
685 if (isipv6) {
686 strcpy(dbuf, "[");
984263bc 687 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
984263bc 688 strcat(dbuf, "]");
95b22adf
JH
689 strcpy(sbuf, "[");
690 strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
984263bc
MD
691 strcat(sbuf, "]");
692 } else {
693 strcpy(dbuf, inet_ntoa(ip->ip_dst));
694 strcpy(sbuf, inet_ntoa(ip->ip_src));
695 }
696 switch (log_in_vain) {
697 case 1:
698 if ((thflags & TH_SYN) == 0)
699 break;
700 case 2:
701 log(LOG_INFO,
702 "Connection attempt to TCP %s:%d "
703 "from %s:%d flags:0x%02x\n",
704 dbuf, ntohs(th->th_dport), sbuf,
705 ntohs(th->th_sport), thflags);
706 break;
707 default:
708 break;
709 }
710 }
d24ce1dc 711 if (blackhole) {
984263bc
MD
712 switch (blackhole) {
713 case 1:
714 if (thflags & TH_SYN)
715 goto drop;
716 break;
717 case 2:
718 goto drop;
719 default:
720 goto drop;
721 }
722 }
723 rstreason = BANDLIM_RST_CLOSEDPORT;
724 goto dropwithreset;
725 }
726 tp = intotcpcb(inp);
727 if (tp == NULL) {
728 rstreason = BANDLIM_RST_CLOSEDPORT;
729 goto dropwithreset;
730 }
731 if (tp->t_state == TCPS_CLOSED)
732 goto drop;
733
734 /* Unscale the window into a 32-bit value. */
95b22adf 735 if (!(thflags & TH_SYN))
984263bc
MD
736 tiwin = th->th_win << tp->snd_scale;
737 else
738 tiwin = th->th_win;
739
740 so = inp->inp_socket;
47654766 741
984263bc 742#ifdef TCPDEBUG
47654766
JH
743 if (so->so_options & SO_DEBUG) {
744 ostate = tp->t_state;
745 if (isipv6)
95b22adf 746 bcopy(ip6, tcp_saveipgen, sizeof(*ip6));
47654766 747 else
95b22adf 748 bcopy(ip, tcp_saveipgen, sizeof(*ip));
47654766
JH
749 tcp_savetcp = *th;
750 }
984263bc 751#endif
47654766 752
95b22adf 753 bzero(&to, sizeof(to));
d24ce1dc 754
47654766
JH
755 if (so->so_options & SO_ACCEPTCONN) {
756 struct in_conninfo inc;
757
984263bc 758#ifdef INET6
d24ce1dc 759 inc.inc_isipv6 = (isipv6 == TRUE);
984263bc
MD
760#endif
761 if (isipv6) {
762 inc.inc6_faddr = ip6->ip6_src;
763 inc.inc6_laddr = ip6->ip6_dst;
764 inc.inc6_route.ro_rt = NULL; /* XXX */
765 } else {
766 inc.inc_faddr = ip->ip_src;
767 inc.inc_laddr = ip->ip_dst;
768 inc.inc_route.ro_rt = NULL; /* XXX */
769 }
770 inc.inc_fport = th->th_sport;
771 inc.inc_lport = th->th_dport;
772
773 /*
774 * If the state is LISTEN then ignore segment if it contains
775 * a RST. If the segment contains an ACK then it is bad and
776 * send a RST. If it does not contain a SYN then it is not
777 * interesting; drop it.
778 *
779 * If the state is SYN_RECEIVED (syncache) and seg contains
780 * an ACK, but not for our SYN/ACK, send a RST. If the seg
781 * contains a RST, check the sequence number to see if it
782 * is a valid reset segment.
783 */
784 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
785 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
786 if (!syncache_expand(&inc, th, &so, m)) {
787 /*
788 * No syncache entry, or ACK was not
789 * for our SYN/ACK. Send a RST.
790 */
791 tcpstat.tcps_badsyn++;
792 rstreason = BANDLIM_RST_OPENPORT;
793 goto dropwithreset;
794 }
795 if (so == NULL)
796 /*
797 * Could not complete 3-way handshake,
798 * connection is being closed down, and
799 * syncache will free mbuf.
800 */
801 return;
802 /*
803 * Socket is created in state SYN_RECEIVED.
804 * Continue processing segment.
805 */
806 inp = sotoinpcb(so);
807 tp = intotcpcb(inp);
808 /*
809 * This is what would have happened in
810 * tcp_output() when the SYN,ACK was sent.
811 */
812 tp->snd_up = tp->snd_una;
813 tp->snd_max = tp->snd_nxt = tp->iss + 1;
814 tp->last_ack_sent = tp->rcv_nxt;
815/*
816 * XXX possible bug - it doesn't appear that tp->snd_wnd is unscaled
817 * until the _second_ ACK is received:
818 * rcv SYN (set wscale opts) --> send SYN/ACK, set snd_wnd = window.
819 * rcv ACK, calculate tiwin --> process SYN_RECEIVED, determine wscale,
820 * move to ESTAB, set snd_wnd to tiwin.
d24ce1dc 821 */
984263bc
MD
822 tp->snd_wnd = tiwin; /* unscaled */
823 goto after_listen;
824 }
825 if (thflags & TH_RST) {
826 syncache_chkrst(&inc, th);
827 goto drop;
828 }
829 if (thflags & TH_ACK) {
830 syncache_badack(&inc);
831 tcpstat.tcps_badsyn++;
832 rstreason = BANDLIM_RST_OPENPORT;
833 goto dropwithreset;
834 }
835 goto drop;
836 }
837
838 /*
839 * Segment's flags are (SYN) or (SYN|FIN).
840 */
841#ifdef INET6
842 /*
843 * If deprecated address is forbidden,
844 * we do not accept SYN to deprecated interface
845 * address to prevent any new inbound connection from
846 * getting established.
847 * When we do not accept SYN, we send a TCP RST,
848 * with deprecated source address (instead of dropping
849 * it). We compromise it as it is much better for peer
850 * to send a RST, and RST will be the final packet
851 * for the exchange.
852 *
853 * If we do not forbid deprecated addresses, we accept
854 * the SYN packet. RFC2462 does not suggest dropping
855 * SYN in this case.
856 * If we decipher RFC2462 5.5.4, it says like this:
857 * 1. use of deprecated addr with existing
858 * communication is okay - "SHOULD continue to be
859 * used"
860 * 2. use of it with new communication:
861 * (2a) "SHOULD NOT be used if alternate address
862 * with sufficient scope is available"
863 * (2b) nothing mentioned otherwise.
864 * Here we fall into (2b) case as we have no choice in
865 * our source address selection - we must obey the peer.
866 *
867 * The wording in RFC2462 is confusing, and there are
868 * multiple description text for deprecated address
869 * handling - worse, they are not exactly the same.
870 * I believe 5.5.4 is the best one, so we follow 5.5.4.
871 */
872 if (isipv6 && !ip6_use_deprecated) {
873 struct in6_ifaddr *ia6;
874
875 if ((ia6 = ip6_getdstifaddr(m)) &&
876 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
877 tp = NULL;
878 rstreason = BANDLIM_RST_OPENPORT;
879 goto dropwithreset;
880 }
881 }
882#endif
883 /*
884 * If it is from this socket, drop it, it must be forged.
885 * Don't bother responding if the destination was a broadcast.
886 */
887 if (th->th_dport == th->th_sport) {
888 if (isipv6) {
889 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
890 &ip6->ip6_src))
891 goto drop;
892 } else {
893 if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
894 goto drop;
895 }
896 }
897 /*
898 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
899 *
900 * Note that it is quite possible to receive unicast
901 * link-layer packets with a broadcast IP address. Use
902 * in_broadcast() to find them.
903 */
904 if (m->m_flags & (M_BCAST|M_MCAST))
905 goto drop;
906 if (isipv6) {
907 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
908 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
909 goto drop;
910 } else {
911 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
912 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
913 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
914 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
915 goto drop;
916 }
917 /*
918 * SYN appears to be valid; create compressed TCP state
919 * for syncache, or perform t/tcp connection.
920 */
921 if (so->so_qlen <= so->so_qlimit) {
95b22adf 922 tcp_dooptions(&to, optp, optlen, TRUE);
984263bc
MD
923 if (!syncache_add(&inc, &to, th, &so, m))
924 goto drop;
925 if (so == NULL)
926 /*
927 * Entry added to syncache, mbuf used to
928 * send SYN,ACK packet.
929 */
930 return;
931 /*
932 * Segment passed TAO tests.
933 */
934 inp = sotoinpcb(so);
935 tp = intotcpcb(inp);
936 tp->snd_wnd = tiwin;
937 tp->t_starttime = ticks;
938 tp->t_state = TCPS_ESTABLISHED;
939
940 /*
941 * If there is a FIN, or if there is data and the
942 * connection is local, then delay SYN,ACK(SYN) in
943 * the hope of piggy-backing it on a response
944 * segment. Otherwise must send ACK now in case
945 * the other side is slow starting.
946 */
947 if (DELAY_ACK(tp) &&
948 ((thflags & TH_FIN) ||
949 (tlen != 0 &&
950 ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
951 (!isipv6 && in_localaddr(inp->inp_faddr)))))) {
d24ce1dc
JH
952 callout_reset(tp->tt_delack, tcp_delacktime,
953 tcp_timer_delack, tp);
984263bc 954 tp->t_flags |= TF_NEEDSYN;
d24ce1dc 955 } else
984263bc
MD
956 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
957
958 tcpstat.tcps_connects++;
959 soisconnected(so);
960 goto trimthenstep6;
961 }
962 goto drop;
963 }
964after_listen:
965
966/* XXX temp debugging */
967 /* should not happen - syncache should pick up these connections */
968 if (tp->t_state == TCPS_LISTEN)
969 panic("tcp_input: TCPS_LISTEN");
970
971 /*
972 * Segment received on connection.
973 * Reset idle time and keep-alive timer.
974 */
975 tp->t_rcvtime = ticks;
976 if (TCPS_HAVEESTABLISHED(tp->t_state))
977 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
978
979 /*
980 * Process options.
981 * XXX this is tradtitional behavior, may need to be cleaned up.
982 */
95b22adf 983 tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) != 0);
984263bc
MD
984 if (thflags & TH_SYN) {
985 if (to.to_flags & TOF_SCALE) {
986 tp->t_flags |= TF_RCVD_SCALE;
987 tp->requested_s_scale = to.to_requested_s_scale;
988 }
989 if (to.to_flags & TOF_TS) {
990 tp->t_flags |= TF_RCVD_TSTMP;
991 tp->ts_recent = to.to_tsval;
992 tp->ts_recent_age = ticks;
993 }
994 if (to.to_flags & (TOF_CC|TOF_CCNEW))
995 tp->t_flags |= TF_RCVD_CC;
996 if (to.to_flags & TOF_MSS)
997 tcp_mss(tp, to.to_mss);
998 }
999
1000 /*
1001 * Header prediction: check for the two common cases
1002 * of a uni-directional data xfer. If the packet has
1003 * no control flags, is in-sequence, the window didn't
1004 * change and we're not retransmitting, it's a
1005 * candidate. If the length is zero and the ack moved
1006 * forward, we're the sender side of the xfer. Just
1007 * free the data acked & wake any higher level process
1008 * that was blocked waiting for space. If the length
1009 * is non-zero and the ack didn't move, we're the
1010 * receiver side. If we're getting packets in-order
1011 * (the reassembly queue is empty), add the data to
1012 * the socket buffer and note that we need a delayed ack.
1013 * Make sure that the hidden state-flags are also off.
1014 * Since we check for TCPS_ESTABLISHED above, it can only
1015 * be TH_NEEDSYN.
1016 */
1017 if (tp->t_state == TCPS_ESTABLISHED &&
1018 (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1019 ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1020 ((to.to_flags & TOF_TS) == 0 ||
1021 TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1022 /*
1023 * Using the CC option is compulsory if once started:
1024 * the segment is OK if no T/TCP was negotiated or
1025 * if the segment has a CC option equal to CCrecv
1026 */
1027 ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
1028 ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
1029 th->th_seq == tp->rcv_nxt &&
1030 tiwin && tiwin == tp->snd_wnd &&
1031 tp->snd_nxt == tp->snd_max) {
1032
1033 /*
1034 * If last ACK falls within this segment's sequence numbers,
1035 * record the timestamp.
1036 * NOTE that the test is modified according to the latest
1037 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1038 */
1039 if ((to.to_flags & TOF_TS) != 0 &&
1040 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1041 tp->ts_recent_age = ticks;
1042 tp->ts_recent = to.to_tsval;
1043 }
1044
1045 if (tlen == 0) {
1046 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1047 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1048 tp->snd_cwnd >= tp->snd_wnd &&
95b22adf 1049 !IN_FASTRECOVERY(tp)) {
984263bc
MD
1050 /*
1051 * this is a pure ack for outstanding data.
1052 */
1053 ++tcpstat.tcps_predack;
1054 /*
1055 * "bad retransmit" recovery
bfdb979e
JH
1056 *
1057 * If Eifel detection applies, then
1058 * it is deterministic, so use it
1059 * unconditionally over the old heuristic.
1060 * Otherwise, fall back to the old heuristic.
984263bc 1061 */
bfdb979e
JH
1062 if (tcp_do_eifel_detect &&
1063 (to.to_flags & TOF_TS) && to.to_tsecr &&
1064 (tp->t_flags & TF_FIRSTACCACK)) {
1065 /* Eifel detection applicable. */
1066 if (to.to_tsecr < tp->t_rexmtTS) {
1067 tcp_revert_congestion_state(tp);
1068 ++tcpstat.tcps_eifeldetected;
1069 }
1070 } else if (tp->t_rxtshift == 1 &&
1071 ticks < tp->t_badrxtwin) {
1072 tcp_revert_congestion_state(tp);
1073 ++tcpstat.tcps_rttdetected;
984263bc 1074 }
8819433a
JH
1075 tp->t_flags &= ~(TF_FIRSTACCACK |
1076 TF_FASTREXMT | TF_EARLYREXMT);
984263bc
MD
1077 /*
1078 * Recalculate the retransmit timer / rtt.
1079 *
d24ce1dc 1080 * Some machines (certain windows boxes)
984263bc
MD
1081 * send broken timestamp replies during the
1082 * SYN+ACK phase, ignore timestamps of 0.
1083 */
95b22adf 1084 if ((to.to_flags & TOF_TS) && to.to_tsecr) {
984263bc 1085 tcp_xmit_timer(tp,
95b22adf 1086 ticks - to.to_tsecr + 1);
984263bc 1087 } else if (tp->t_rtttime &&
95b22adf 1088 SEQ_GT(th->th_ack, tp->t_rtseq)) {
984263bc
MD
1089 tcp_xmit_timer(tp,
1090 ticks - tp->t_rtttime);
1091 }
1092 tcp_xmit_bandwidth_limit(tp, th->th_ack);
1093 acked = th->th_ack - tp->snd_una;
1094 tcpstat.tcps_rcvackpack++;
1095 tcpstat.tcps_rcvackbyte += acked;
1096 sbdrop(&so->so_snd, acked);
cfb3f3f4 1097 tp->snd_recover = th->th_ack - 1;
9845754e 1098 tp->snd_una = th->th_ack;
984263bc
MD
1099 tp->t_dupacks = 0;
1100 m_freem(m);
1101 ND6_HINT(tp); /* some progress has been done */
1102
1103 /*
1104 * If all outstanding data are acked, stop
1105 * retransmit timer, otherwise restart timer
1106 * using current (possibly backed-off) value.
1107 * If process is waiting for space,
1108 * wakeup/selwakeup/signal. If data
1109 * are ready to send, let tcp_output
1110 * decide between more output or persist.
1111 */
1112 if (tp->snd_una == tp->snd_max)
1113 callout_stop(tp->tt_rexmt);
1114 else if (!callout_active(tp->tt_persist))
d24ce1dc 1115 callout_reset(tp->tt_rexmt,
984263bc
MD
1116 tp->t_rxtcur,
1117 tcp_timer_rexmt, tp);
1118
1119 sowwakeup(so);
1120 if (so->so_snd.sb_cc)
1121 (void) tcp_output(tp);
1122 return;
1123 }
1124 } else if (th->th_ack == tp->snd_una &&
1125 LIST_EMPTY(&tp->t_segq) &&
1126 tlen <= sbspace(&so->so_rcv)) {
1127 /*
1128 * this is a pure, in-sequence data packet
1129 * with nothing on the reassembly queue and
1130 * we have enough buffer space to take it.
1131 */
1132 ++tcpstat.tcps_preddat;
1133 tp->rcv_nxt += tlen;
1134 tcpstat.tcps_rcvpack++;
1135 tcpstat.tcps_rcvbyte += tlen;
1136 ND6_HINT(tp); /* some progress has been done */
1137 /*
1138 * Add data to socket buffer.
1139 */
1140 if (so->so_state & SS_CANTRCVMORE) {
1141 m_freem(m);
1142 } else {
09cf73e0 1143 m_adj(m, drop_hdrlen); /* delayed header drop */
984263bc
MD
1144 sbappend(&so->so_rcv, m);
1145 }
1146 sorwakeup(so);
2b1ce38a
MD
1147
1148 /*
1149 * This code is responsible for most of the ACKs
1150 * the TCP stack sends back after receiving a data
1151 * packet. Note that the DELAY_ACK check fails if
1152 * the delack timer is already running, which results
1153 * in an ack being sent every other packet (which is
1154 * what we want).
1155 */
984263bc
MD
1156 if (DELAY_ACK(tp)) {
1157 callout_reset(tp->tt_delack, tcp_delacktime,
1158 tcp_timer_delack, tp);
1159 } else {
1160 tp->t_flags |= TF_ACKNOW;
2b1ce38a
MD
1161 if ((tp->t_flags & TF_ONOUTPUTQ) == 0) {
1162 tp->t_flags |= TF_ONOUTPUTQ;
1163 tp->tt_cpu = mycpu->gd_cpuid;
1164 TAILQ_INSERT_TAIL(
1165 &tcpcbackq[tp->tt_cpu],
1166 tp, t_outputq);
1167 }
984263bc
MD
1168 }
1169 return;
1170 }
1171 }
1172
1173 /*
1174 * Calculate amount of space in receive window,
1175 * and then do TCP input processing.
1176 * Receive window is amount of space in rcv queue,
1177 * but not less than advertised window.
1178 */
1179 { int win;
1180
1181 win = sbspace(&so->so_rcv);
1182 if (win < 0)
1183 win = 0;
1184 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1185 }
1186
1187 switch (tp->t_state) {
1188
1189 /*
1190 * If the state is SYN_RECEIVED:
1191 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1192 */
1193 case TCPS_SYN_RECEIVED:
1194 if ((thflags & TH_ACK) &&
1195 (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1196 SEQ_GT(th->th_ack, tp->snd_max))) {
1197 rstreason = BANDLIM_RST_OPENPORT;
1198 goto dropwithreset;
1199 }
1200 break;
1201
1202 /*
1203 * If the state is SYN_SENT:
1204 * if seg contains an ACK, but not for our SYN, drop the input.
1205 * if seg contains a RST, then drop the connection.
1206 * if seg does not contain SYN, then drop it.
1207 * Otherwise this is an acceptable SYN segment
1208 * initialize tp->rcv_nxt and tp->irs
1209 * if seg contains ack then advance tp->snd_una
1210 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1211 * arrange for segment to be acked (eventually)
1212 * continue processing rest of data/controls, beginning with URG
1213 */
1214 case TCPS_SYN_SENT:
1215 if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) {
1216 taop = &tao_noncached;
1217 bzero(taop, sizeof(*taop));
1218 }
1219
1220 if ((thflags & TH_ACK) &&
1221 (SEQ_LEQ(th->th_ack, tp->iss) ||
1222 SEQ_GT(th->th_ack, tp->snd_max))) {
1223 /*
1224 * If we have a cached CCsent for the remote host,
1225 * hence we haven't just crashed and restarted,
1226 * do not send a RST. This may be a retransmission
1227 * from the other side after our earlier ACK was lost.
1228 * Our new SYN, when it arrives, will serve as the
1229 * needed ACK.
1230 */
1231 if (taop->tao_ccsent != 0)
1232 goto drop;
1233 else {
1234 rstreason = BANDLIM_UNLIMITED;
1235 goto dropwithreset;
1236 }
1237 }
1238 if (thflags & TH_RST) {
1239 if (thflags & TH_ACK)
1240 tp = tcp_drop(tp, ECONNREFUSED);
1241 goto drop;
1242 }
1243 if ((thflags & TH_SYN) == 0)
1244 goto drop;
1245 tp->snd_wnd = th->th_win; /* initial send window */
1246 tp->cc_recv = to.to_cc; /* foreign CC */
1247
1248 tp->irs = th->th_seq;
1249 tcp_rcvseqinit(tp);
1250 if (thflags & TH_ACK) {
1251 /*
1252 * Our SYN was acked. If segment contains CC.ECHO
1253 * option, check it to make sure this segment really
1254 * matches our SYN. If not, just drop it as old
1255 * duplicate, but send an RST if we're still playing
1256 * by the old rules. If no CC.ECHO option, make sure
1257 * we don't get fooled into using T/TCP.
1258 */
1259 if (to.to_flags & TOF_CCECHO) {
1260 if (tp->cc_send != to.to_ccecho) {
1261 if (taop->tao_ccsent != 0)
1262 goto drop;
1263 else {
1264 rstreason = BANDLIM_UNLIMITED;
1265 goto dropwithreset;
1266 }
1267 }
1268 } else
1269 tp->t_flags &= ~TF_RCVD_CC;
1270 tcpstat.tcps_connects++;
1271 soisconnected(so);
1272 /* Do window scaling on this connection? */
1273 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1274 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1275 tp->snd_scale = tp->requested_s_scale;
1276 tp->rcv_scale = tp->request_r_scale;
1277 }
1278 /* Segment is acceptable, update cache if undefined. */
1279 if (taop->tao_ccsent == 0)
1280 taop->tao_ccsent = to.to_ccecho;
1281
1282 tp->rcv_adv += tp->rcv_wnd;
1283 tp->snd_una++; /* SYN is acked */
1284 /*
1285 * If there's data, delay ACK; if there's also a FIN
1286 * ACKNOW will be turned on later.
1287 */
1288 if (DELAY_ACK(tp) && tlen != 0)
d24ce1dc
JH
1289 callout_reset(tp->tt_delack, tcp_delacktime,
1290 tcp_timer_delack, tp);
984263bc
MD
1291 else
1292 tp->t_flags |= TF_ACKNOW;
1293 /*
1294 * Received <SYN,ACK> in SYN_SENT[*] state.
1295 * Transitions:
1296 * SYN_SENT --> ESTABLISHED
1297 * SYN_SENT* --> FIN_WAIT_1
1298 */
1299 tp->t_starttime = ticks;
1300 if (tp->t_flags & TF_NEEDFIN) {
1301 tp->t_state = TCPS_FIN_WAIT_1;
1302 tp->t_flags &= ~TF_NEEDFIN;
1303 thflags &= ~TH_SYN;
1304 } else {
1305 tp->t_state = TCPS_ESTABLISHED;
1306 callout_reset(tp->tt_keep, tcp_keepidle,
1307 tcp_timer_keep, tp);
1308 }
1309 } else {
1310 /*
95b22adf
JH
1311 * Received initial SYN in SYN-SENT[*] state =>
1312 * simultaneous open. If segment contains CC option
1313 * and there is a cached CC, apply TAO test.
1314 * If it succeeds, connection is * half-synchronized.
1315 * Otherwise, do 3-way handshake:
1316 * SYN-SENT -> SYN-RECEIVED
1317 * SYN-SENT* -> SYN-RECEIVED*
1318 * If there was no CC option, clear cached CC value.
1319 */
984263bc
MD
1320 tp->t_flags |= TF_ACKNOW;
1321 callout_stop(tp->tt_rexmt);
1322 if (to.to_flags & TOF_CC) {
1323 if (taop->tao_cc != 0 &&
1324 CC_GT(to.to_cc, taop->tao_cc)) {
1325 /*
1326 * update cache and make transition:
1327 * SYN-SENT -> ESTABLISHED*
1328 * SYN-SENT* -> FIN-WAIT-1*
1329 */
1330 taop->tao_cc = to.to_cc;
1331 tp->t_starttime = ticks;
1332 if (tp->t_flags & TF_NEEDFIN) {
1333 tp->t_state = TCPS_FIN_WAIT_1;
1334 tp->t_flags &= ~TF_NEEDFIN;
1335 } else {
1336 tp->t_state = TCPS_ESTABLISHED;
1337 callout_reset(tp->tt_keep,
1338 tcp_keepidle,
1339 tcp_timer_keep,
1340 tp);
1341 }
1342 tp->t_flags |= TF_NEEDSYN;
1343 } else
1344 tp->t_state = TCPS_SYN_RECEIVED;
1345 } else {
1346 /* CC.NEW or no option => invalidate cache */
1347 taop->tao_cc = 0;
1348 tp->t_state = TCPS_SYN_RECEIVED;
1349 }
1350 }
1351
1352trimthenstep6:
1353 /*
1354 * Advance th->th_seq to correspond to first data byte.
1355 * If data, trim to stay within window,
1356 * dropping FIN if necessary.
1357 */
1358 th->th_seq++;
1359 if (tlen > tp->rcv_wnd) {
1360 todrop = tlen - tp->rcv_wnd;
1361 m_adj(m, -todrop);
1362 tlen = tp->rcv_wnd;
1363 thflags &= ~TH_FIN;
1364 tcpstat.tcps_rcvpackafterwin++;
1365 tcpstat.tcps_rcvbyteafterwin += todrop;
1366 }
1367 tp->snd_wl1 = th->th_seq - 1;
1368 tp->rcv_up = th->th_seq;
1369 /*
1370 * Client side of transaction: already sent SYN and data.
1371 * If the remote host used T/TCP to validate the SYN,
1372 * our data will be ACK'd; if so, enter normal data segment
1373 * processing in the middle of step 5, ack processing.
1374 * Otherwise, goto step 6.
1375 */
95b22adf 1376 if (thflags & TH_ACK)
984263bc
MD
1377 goto process_ACK;
1378
1379 goto step6;
1380
1381 /*
1382 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1383 * if segment contains a SYN and CC [not CC.NEW] option:
1384 * if state == TIME_WAIT and connection duration > MSL,
1385 * drop packet and send RST;
1386 *
1387 * if SEG.CC > CCrecv then is new SYN, and can implicitly
1388 * ack the FIN (and data) in retransmission queue.
1389 * Complete close and delete TCPCB. Then reprocess
1390 * segment, hoping to find new TCPCB in LISTEN state;
1391 *
1392 * else must be old SYN; drop it.
1393 * else do normal processing.
1394 */
1395 case TCPS_LAST_ACK:
1396 case TCPS_CLOSING:
1397 case TCPS_TIME_WAIT:
1398 if ((thflags & TH_SYN) &&
1399 (to.to_flags & TOF_CC) && tp->cc_recv != 0) {
1400 if (tp->t_state == TCPS_TIME_WAIT &&
1401 (ticks - tp->t_starttime) > tcp_msl) {
1402 rstreason = BANDLIM_UNLIMITED;
1403 goto dropwithreset;
1404 }
1405 if (CC_GT(to.to_cc, tp->cc_recv)) {
1406 tp = tcp_close(tp);
1407 goto findpcb;
1408 }
1409 else
1410 goto drop;
1411 }
95b22adf 1412 break; /* continue normal processing */
984263bc
MD
1413 }
1414
1415 /*
1416 * States other than LISTEN or SYN_SENT.
1417 * First check the RST flag and sequence number since reset segments
1418 * are exempt from the timestamp and connection count tests. This
1419 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1420 * below which allowed reset segments in half the sequence space
1421 * to fall though and be processed (which gives forged reset
1422 * segments with a random sequence number a 50 percent chance of
1423 * killing a connection).
1424 * Then check timestamp, if present.
1425 * Then check the connection count, if present.
1426 * Then check that at least some bytes of segment are within
1427 * receive window. If segment begins before rcv_nxt,
1428 * drop leading data (and SYN); if nothing left, just ack.
1429 *
1430 *
1431 * If the RST bit is set, check the sequence number to see
1432 * if this is a valid reset segment.
1433 * RFC 793 page 37:
1434 * In all states except SYN-SENT, all reset (RST) segments
1435 * are validated by checking their SEQ-fields. A reset is
1436 * valid if its sequence number is in the window.
1437 * Note: this does not take into account delayed ACKs, so
1438 * we should test against last_ack_sent instead of rcv_nxt.
1439 * The sequence number in the reset segment is normally an
1440 * echo of our outgoing acknowlegement numbers, but some hosts
1441 * send a reset with the sequence number at the rightmost edge
1442 * of our receive window, and we have to handle this case.
1443 * If we have multiple segments in flight, the intial reset
1444 * segment sequence numbers will be to the left of last_ack_sent,
1445 * but they will eventually catch up.
1446 * In any case, it never made sense to trim reset segments to
1447 * fit the receive window since RFC 1122 says:
1448 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1449 *
1450 * A TCP SHOULD allow a received RST segment to include data.
1451 *
1452 * DISCUSSION
1453 * It has been suggested that a RST segment could contain
1454 * ASCII text that encoded and explained the cause of the
1455 * RST. No standard has yet been established for such
1456 * data.
1457 *
1458 * If the reset segment passes the sequence number test examine
1459 * the state:
1460 * SYN_RECEIVED STATE:
1461 * If passive open, return to LISTEN state.
1462 * If active open, inform user that connection was refused.
1463 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1464 * Inform user that connection was reset, and close tcb.
1465 * CLOSING, LAST_ACK STATES:
1466 * Close the tcb.
1467 * TIME_WAIT STATE:
1468 * Drop the segment - see Stevens, vol. 2, p. 964 and
1469 * RFC 1337.
1470 */
1471 if (thflags & TH_RST) {
1472 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1473 SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
1474 switch (tp->t_state) {
1475
1476 case TCPS_SYN_RECEIVED:
1477 so->so_error = ECONNREFUSED;
1478 goto close;
1479
1480 case TCPS_ESTABLISHED:
1481 case TCPS_FIN_WAIT_1:
1482 case TCPS_FIN_WAIT_2:
1483 case TCPS_CLOSE_WAIT:
1484 so->so_error = ECONNRESET;
1485 close:
1486 tp->t_state = TCPS_CLOSED;
1487 tcpstat.tcps_drops++;
1488 tp = tcp_close(tp);
1489 break;
1490
1491 case TCPS_CLOSING:
1492 case TCPS_LAST_ACK:
1493 tp = tcp_close(tp);
1494 break;
1495
1496 case TCPS_TIME_WAIT:
1497 break;
1498 }
1499 }
1500 goto drop;
1501 }
1502
1503 /*
1504 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1505 * and it's less than ts_recent, drop it.
1506 */
1507 if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
1508 TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1509
1510 /* Check to see if ts_recent is over 24 days old. */
1511 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1512 /*
1513 * Invalidate ts_recent. If this segment updates
1514 * ts_recent, the age will be reset later and ts_recent
1515 * will get a valid value. If it does not, setting
1516 * ts_recent to zero will at least satisfy the
1517 * requirement that zero be placed in the timestamp
1518 * echo reply when ts_recent isn't valid. The
1519 * age isn't reset until we get a valid ts_recent
1520 * because we don't want out-of-order segments to be
1521 * dropped when ts_recent is old.
1522 */
1523 tp->ts_recent = 0;
1524 } else {
1525 tcpstat.tcps_rcvduppack++;
1526 tcpstat.tcps_rcvdupbyte += tlen;
1527 tcpstat.tcps_pawsdrop++;
1528 if (tlen)
1529 goto dropafterack;
1530 goto drop;
1531 }
1532 }
1533
1534 /*
1535 * T/TCP mechanism
1536 * If T/TCP was negotiated and the segment doesn't have CC,
1537 * or if its CC is wrong then drop the segment.
1538 * RST segments do not have to comply with this.
1539 */
1540 if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1541 ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
95b22adf 1542 goto dropafterack;
984263bc
MD
1543
1544 /*
1545 * In the SYN-RECEIVED state, validate that the packet belongs to
1546 * this connection before trimming the data to fit the receive
1547 * window. Check the sequence number versus IRS since we know
1548 * the sequence numbers haven't wrapped. This is a partial fix
1549 * for the "LAND" DoS attack.
1550 */
1551 if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1552 rstreason = BANDLIM_RST_OPENPORT;
1553 goto dropwithreset;
1554 }
1555
1556 todrop = tp->rcv_nxt - th->th_seq;
1557 if (todrop > 0) {
1558 if (thflags & TH_SYN) {
1559 thflags &= ~TH_SYN;
1560 th->th_seq++;
1561 if (th->th_urp > 1)
1562 th->th_urp--;
1563 else
1564 thflags &= ~TH_URG;
1565 todrop--;
1566 }
1567 /*
1568 * Following if statement from Stevens, vol. 2, p. 960.
1569 */
1570 if (todrop > tlen
1571 || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1572 /*
1573 * Any valid FIN must be to the left of the window.
1574 * At this point the FIN must be a duplicate or out
1575 * of sequence; drop it.
1576 */
1577 thflags &= ~TH_FIN;
1578
1579 /*
1580 * Send an ACK to resynchronize and drop any data.
1581 * But keep on processing for RST or ACK.
1582 */
1583 tp->t_flags |= TF_ACKNOW;
1584 todrop = tlen;
1585 tcpstat.tcps_rcvduppack++;
1586 tcpstat.tcps_rcvdupbyte += todrop;
1587 } else {
1588 tcpstat.tcps_rcvpartduppack++;
1589 tcpstat.tcps_rcvpartdupbyte += todrop;
1590 }
1591 drop_hdrlen += todrop; /* drop from the top afterwards */
1592 th->th_seq += todrop;
1593 tlen -= todrop;
1594 if (th->th_urp > todrop)
1595 th->th_urp -= todrop;
1596 else {
1597 thflags &= ~TH_URG;
1598 th->th_urp = 0;
1599 }
1600 }
1601
1602 /*
1603 * If new data are received on a connection after the
1604 * user processes are gone, then RST the other end.
1605 */
1606 if ((so->so_state & SS_NOFDREF) &&
1607 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1608 tp = tcp_close(tp);
1609 tcpstat.tcps_rcvafterclose++;
1610 rstreason = BANDLIM_UNLIMITED;
1611 goto dropwithreset;
1612 }
1613
1614 /*
1615 * If segment ends after window, drop trailing data
1616 * (and PUSH and FIN); if nothing left, just ACK.
1617 */
1618 todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1619 if (todrop > 0) {
1620 tcpstat.tcps_rcvpackafterwin++;
1621 if (todrop >= tlen) {
1622 tcpstat.tcps_rcvbyteafterwin += tlen;
1623 /*
1624 * If a new connection request is received
1625 * while in TIME_WAIT, drop the old connection
1626 * and start over if the sequence numbers
1627 * are above the previous ones.
1628 */
1629 if (thflags & TH_SYN &&
1630 tp->t_state == TCPS_TIME_WAIT &&
1631 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1632 tp = tcp_close(tp);
1633 goto findpcb;
1634 }
1635 /*
1636 * If window is closed can only take segments at
1637 * window edge, and have to drop data and PUSH from
1638 * incoming segments. Continue processing, but
1639 * remember to ack. Otherwise, drop segment
1640 * and ack.
1641 */
1642 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1643 tp->t_flags |= TF_ACKNOW;
1644 tcpstat.tcps_rcvwinprobe++;
1645 } else
1646 goto dropafterack;
1647 } else
1648 tcpstat.tcps_rcvbyteafterwin += todrop;
1649 m_adj(m, -todrop);
1650 tlen -= todrop;
1651 thflags &= ~(TH_PUSH|TH_FIN);
1652 }
1653
1654 /*
1655 * If last ACK falls within this segment's sequence numbers,
1656 * record its timestamp.
1657 * NOTE that the test is modified according to the latest
1658 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1659 */
1660 if ((to.to_flags & TOF_TS) != 0 &&
1661 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1662 tp->ts_recent_age = ticks;
1663 tp->ts_recent = to.to_tsval;
1664 }
1665
1666 /*
1667 * If a SYN is in the window, then this is an
1668 * error and we send an RST and drop the connection.
1669 */
1670 if (thflags & TH_SYN) {
1671 tp = tcp_drop(tp, ECONNRESET);
1672 rstreason = BANDLIM_UNLIMITED;
1673 goto dropwithreset;
1674 }
1675
1676 /*
1677 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1678 * flag is on (half-synchronized state), then queue data for
1679 * later processing; else drop segment and return.
1680 */
1681 if ((thflags & TH_ACK) == 0) {
1682 if (tp->t_state == TCPS_SYN_RECEIVED ||
1683 (tp->t_flags & TF_NEEDSYN))
1684 goto step6;
1685 else
1686 goto drop;
1687 }
1688
1689 /*
1690 * Ack processing.
1691 */
1692 switch (tp->t_state) {
1693
1694 /*
1695 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1696 * ESTABLISHED state and continue processing.
1697 * The ACK was checked above.
1698 */
1699 case TCPS_SYN_RECEIVED:
1700
1701 tcpstat.tcps_connects++;
1702 soisconnected(so);
1703 /* Do window scaling? */
1704 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1705 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1706 tp->snd_scale = tp->requested_s_scale;
1707 tp->rcv_scale = tp->request_r_scale;
1708 }
1709 /*
1710 * Upon successful completion of 3-way handshake,
1711 * update cache.CC if it was undefined, pass any queued
1712 * data to the user, and advance state appropriately.
1713 */
1714 if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL &&
1715 taop->tao_cc == 0)
1716 taop->tao_cc = tp->cc_recv;
1717
1718 /*
1719 * Make transitions:
1720 * SYN-RECEIVED -> ESTABLISHED
1721 * SYN-RECEIVED* -> FIN-WAIT-1
1722 */
1723 tp->t_starttime = ticks;
1724 if (tp->t_flags & TF_NEEDFIN) {
1725 tp->t_state = TCPS_FIN_WAIT_1;
1726 tp->t_flags &= ~TF_NEEDFIN;
1727 } else {
1728 tp->t_state = TCPS_ESTABLISHED;
d24ce1dc 1729 callout_reset(tp->tt_keep, tcp_keepidle,
984263bc
MD
1730 tcp_timer_keep, tp);
1731 }
1732 /*
1733 * If segment contains data or ACK, will call tcp_reass()
1734 * later; if not, do so now to pass queued data to user.
1735 */
1736 if (tlen == 0 && (thflags & TH_FIN) == 0)
1737 (void) tcp_reass(tp, (struct tcphdr *)0, 0,
1738 (struct mbuf *)0);
1739 tp->snd_wl1 = th->th_seq - 1;
1740 /* fall into ... */
1741
1742 /*
1743 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1744 * ACKs. If the ack is in the range
1745 * tp->snd_una < th->th_ack <= tp->snd_max
1746 * then advance tp->snd_una to th->th_ack and drop
1747 * data from the retransmission queue. If this ACK reflects
1748 * more up to date window information we update our window information.
1749 */
1750 case TCPS_ESTABLISHED:
1751 case TCPS_FIN_WAIT_1:
1752 case TCPS_FIN_WAIT_2:
1753 case TCPS_CLOSE_WAIT:
1754 case TCPS_CLOSING:
1755 case TCPS_LAST_ACK:
1756 case TCPS_TIME_WAIT:
1757
1758 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
95b22adf
JH
1759 if (tlen != 0 || tiwin != tp->snd_wnd) {
1760 tp->t_dupacks = 0;
1761 break;
1762 }
1763 tcpstat.tcps_rcvdupack++;
1764 /*
1765 * If we have outstanding data (other than
1766 * a window probe), this is a completely
1767 * duplicate ack (ie, window info didn't
1768 * change), the ack is the biggest we've
1769 * seen and we've seen exactly our rexmt
1770 * threshhold of them, assume a packet
1771 * has been dropped and retransmit it.
1772 * Kludge snd_nxt & the congestion
1773 * window so we send only this one
1774 * packet.
1775 *
1776 * We know we're losing at the current
1777 * window size so do congestion avoidance
1778 * (set ssthresh to half the current window
1779 * and pull our congestion window back to
1780 * the new ssthresh).
1781 *
1782 * Dup acks mean that packets have left the
1783 * network (they're now cached at the receiver)
1784 * so bump cwnd by the amount in the receiver
1785 * to keep a constant cwnd packets in the
1786 * network.
1787 */
1788 if (!callout_active(tp->tt_rexmt) ||
1789 th->th_ack != tp->snd_una) {
1790 tp->t_dupacks = 0;
1791 break;
1792 }
1793 if (IN_FASTRECOVERY(tp)) {
1794 tp->snd_cwnd += tp->t_maxseg;
1795 (void) tcp_output(tp);
1796 } else if (++tp->t_dupacks == tcprexmtthresh) {
1797 tcp_seq onxt;
1798 u_int win;
1799
1800 if (SEQ_LEQ(th->th_ack, tp->snd_recover)) {
984263bc 1801 tp->t_dupacks = 0;
95b22adf
JH
1802 break;
1803 }
8819433a 1804fastretransmit:
95b22adf
JH
1805 if (tcp_do_eifel_detect &&
1806 (tp->t_flags & TF_RCVD_TSTMP)) {
1807 tcp_save_congestion_state(tp);
1808 tp->t_flags |= TF_FASTREXMT;
1809 }
1810 win = min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1811 tp->t_maxseg;
1812 if (win < 2)
1813 win = 2;
1814 tp->snd_ssthresh = win * tp->t_maxseg;
1815 ENTER_FASTRECOVERY(tp);
1816 tp->snd_recover = tp->snd_max;
1817 callout_stop(tp->tt_rexmt);
1818 tp->t_rtttime = 0;
1819 onxt = tp->snd_nxt;
1820 tp->snd_nxt = th->th_ack;
1821 tp->snd_cwnd = tp->t_maxseg;
1822 (void) tcp_output(tp);
1823 ++tcpstat.tcps_sndfastrexmit;
1824 KASSERT(tp->snd_limited <= 2,
1825 ("tp->snd_limited too big"));
1826 tp->snd_cwnd = tp->snd_ssthresh +
1827 (tp->t_maxseg *
1828 (tp->t_dupacks - tp->snd_limited));
1829 if (SEQ_GT(onxt, tp->snd_nxt))
1830 tp->snd_nxt = onxt;
1831 } else if (tcp_do_limitedtransmit) {
1832 u_long oldcwnd = tp->snd_cwnd;
1833 tcp_seq oldsndmax = tp->snd_max;
1834 /* outstanding data */
1835 uint32_t ownd = tp->snd_max - tp->snd_una;
1836 u_int sent;
ca94b4a6 1837
8819433a
JH
1838#define iceildiv(n, d) (((n)+(d)-1) / (d))
1839
95b22adf
JH
1840 KASSERT(tp->t_dupacks == 1 ||
1841 tp->t_dupacks == 2,
1842 ("dupacks not 1 or 2"));
1843 if (tp->t_dupacks == 1)
1844 tp->snd_limited = 0;
1845 tp->snd_cwnd = ownd +
1846 (tp->t_dupacks - tp->snd_limited) *
1847 tp->t_maxseg;
1848 (void) tcp_output(tp);
1849 tp->snd_cwnd = oldcwnd;
1850 sent = tp->snd_max - oldsndmax;
1851 if (sent > tp->t_maxseg) {
1852 KASSERT((tp->t_dupacks == 2 &&
1853 tp->snd_limited == 0) ||
1854 (sent == tp->t_maxseg + 1 &&
1855 tp->t_flags & TF_SENTFIN),
1856 ("sent too much"));
1857 KASSERT(sent <= tp->t_maxseg * 2,
1858 ("sent too many segments"));
1859 tp->snd_limited = 2;
1860 tcpstat.tcps_sndlimited += 2;
1861 } else if (sent > 0) {
1862 ++tp->snd_limited;
1863 ++tcpstat.tcps_sndlimited;
1864 } else if (tcp_do_early_retransmit &&
1865 (tcp_do_eifel_detect &&
1866 (tp->t_flags & TF_RCVD_TSTMP)) &&
1867 tp->t_dupacks + 1 >=
1868 iceildiv(ownd, tp->t_maxseg)) {
1869 ++tcpstat.tcps_sndearlyrexmit;
1870 tp->t_flags |= TF_EARLYREXMT;
1871 goto fastretransmit;
984263bc 1872 }
95b22adf
JH
1873 }
1874 goto drop;
984263bc
MD
1875 }
1876
1877 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
1878
1879 /*
1880 * If the congestion window was inflated to account
1881 * for the other side's cached packets, retract it.
1882 */
95b22adf
JH
1883 if (IN_FASTRECOVERY(tp)) {
1884 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
1885 tcp_newreno_partial_ack(tp, th);
1886 } else {
1887 /*
1888 * Window inflation should have left us
1889 * with approximately snd_ssthresh
1890 * outstanding data.
1891 * But in case we would be inclined to
1892 * send a burst, better to do it via
1893 * the slow start mechanism.
1894 */
1895 if (SEQ_GT(th->th_ack + tp->snd_ssthresh,
1896 tp->snd_max))
1897 tp->snd_cwnd = tp->snd_max -
1898 th->th_ack + tp->t_maxseg;
1899 else
1900 tp->snd_cwnd = tp->snd_ssthresh;
984263bc 1901 }
95b22adf 1902 }
984263bc
MD
1903 tp->t_dupacks = 0;
1904 if (SEQ_GT(th->th_ack, tp->snd_max)) {
5a274421
JH
1905 /*
1906 * Detected optimistic ACK attack.
1907 * Force slow-start to de-synchronize attack.
1908 */
1909 tp->snd_cwnd = tp->t_maxseg;
1910
984263bc
MD
1911 tcpstat.tcps_rcvacktoomuch++;
1912 goto dropafterack;
1913 }
1914 /*
1915 * If we reach this point, ACK is not a duplicate,
1916 * i.e., it ACKs something we sent.
1917 */
1918 if (tp->t_flags & TF_NEEDSYN) {
1919 /*
1920 * T/TCP: Connection was half-synchronized, and our
1921 * SYN has been ACK'd (so connection is now fully
1922 * synchronized). Go to non-starred state,
1923 * increment snd_una for ACK of SYN, and check if
1924 * we can do window scaling.
1925 */
1926 tp->t_flags &= ~TF_NEEDSYN;
1927 tp->snd_una++;
1928 /* Do window scaling? */
1929 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
95b22adf 1930 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
984263bc
MD
1931 tp->snd_scale = tp->requested_s_scale;
1932 tp->rcv_scale = tp->request_r_scale;
1933 }
1934 }
1935
1936process_ACK:
1937 acked = th->th_ack - tp->snd_una;
1938 tcpstat.tcps_rcvackpack++;
1939 tcpstat.tcps_rcvackbyte += acked;
1940
1941 /*
1942 * If we just performed our first retransmit, and the ACK
1943 * arrives within our recovery window, then it was a mistake
1944 * to do the retransmit in the first place. Recover our
1945 * original cwnd and ssthresh, and proceed to transmit where
1946 * we left off.
1947 */
bfdb979e 1948 if (tcp_do_eifel_detect && acked &&
95b22adf 1949 (to.to_flags & TOF_TS) && (to.to_tsecr != 0) &&
bfdb979e
JH
1950 (tp->t_flags & TF_FIRSTACCACK)) {
1951 /* Eifel detection applicable. */
1952 if (to.to_tsecr < tp->t_rexmtTS) {
bfdb979e 1953 ++tcpstat.tcps_eifeldetected;
8819433a
JH
1954 tcp_revert_congestion_state(tp);
1955 if (tp->t_rxtshift == 1 &&
1956 ticks >= tp->t_badrxtwin)
1957 ++tcpstat.tcps_rttcantdetect;
bfdb979e
JH
1958 }
1959 } else if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
1960 tcp_revert_congestion_state(tp);
1961 ++tcpstat.tcps_rttdetected;
984263bc
MD
1962 }
1963
1964 /*
1965 * If we have a timestamp reply, update smoothed
1966 * round trip time. If no timestamp is present but
1967 * transmit timer is running and timed sequence
1968 * number was acked, update smoothed round trip time.
1969 * Since we now have an rtt measurement, cancel the
1970 * timer backoff (cf., Phil Karn's retransmit alg.).
1971 * Recompute the initial retransmit timer.
1972 *
1973 * Some machines (certain windows boxes) send broken
d24ce1dc 1974 * timestamp replies during the SYN+ACK phase, ignore
984263bc
MD
1975 * timestamps of 0.
1976 */
95b22adf 1977 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0))
984263bc 1978 tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
95b22adf 1979 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
984263bc 1980 tcp_xmit_timer(tp, ticks - tp->t_rtttime);
984263bc
MD
1981 tcp_xmit_bandwidth_limit(tp, th->th_ack);
1982
1983 /*
1984 * If all outstanding data is acked, stop retransmit
1985 * timer and remember to restart (more output or persist).
1986 * If there is more data to be acked, restart retransmit
1987 * timer, using current (possibly backed-off) value.
1988 */
1989 if (th->th_ack == tp->snd_max) {
1990 callout_stop(tp->tt_rexmt);
1991 needoutput = 1;
1992 } else if (!callout_active(tp->tt_persist))
1993 callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1994 tcp_timer_rexmt, tp);
1995
1996 /*
1997 * If no data (only SYN) was ACK'd,
1998 * skip rest of ACK processing.
1999 */
2000 if (acked == 0)
2001 goto step6;
2002
efd4b327 2003 /* Stop looking for an acceptable ACK since one was received. */
8819433a 2004 tp->t_flags &= ~(TF_FIRSTACCACK | TF_FASTREXMT | TF_EARLYREXMT);
efd4b327 2005
984263bc
MD
2006 /*
2007 * When new data is acked, open the congestion window.
2008 * If the window gives us less than ssthresh packets
2009 * in flight, open exponentially (maxseg per packet).
2010 * Otherwise open linearly: maxseg per window
2011 * (maxseg^2 / cwnd per packet).
2012 */
95b22adf 2013 if (!IN_FASTRECOVERY(tp)) {
2256ba69
RG
2014 u_int cw = tp->snd_cwnd;
2015 u_int incr = tp->t_maxseg;
95b22adf 2016
984263bc
MD
2017 if (cw > tp->snd_ssthresh)
2018 incr = incr * incr / cw;
2019 tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2020 }
2021 if (acked > so->so_snd.sb_cc) {
2022 tp->snd_wnd -= so->so_snd.sb_cc;
2023 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
2024 ourfinisacked = 1;
2025 } else {
2026 sbdrop(&so->so_snd, acked);
2027 tp->snd_wnd -= acked;
2028 ourfinisacked = 0;
2029 }
2030 sowwakeup(so);
95b22adf
JH
2031 if (IN_FASTRECOVERY(tp)) {
2032 if (SEQ_GEQ(th->th_ack, tp->snd_recover))
2033 EXIT_FASTRECOVERY(tp);
2034 } else {
2035 tp->snd_recover = th->th_ack - 1;
cfb3f3f4 2036 }
984263bc
MD
2037 tp->snd_una = th->th_ack;
2038 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2039 tp->snd_nxt = tp->snd_una;
2040
2041 switch (tp->t_state) {
2042
2043 /*
2044 * In FIN_WAIT_1 STATE in addition to the processing
2045 * for the ESTABLISHED state if our FIN is now acknowledged
2046 * then enter FIN_WAIT_2.
2047 */
2048 case TCPS_FIN_WAIT_1:
2049 if (ourfinisacked) {
2050 /*
2051 * If we can't receive any more
2052 * data, then closing user can proceed.
2053 * Starting the timer is contrary to the
2054 * specification, but if we don't get a FIN
2055 * we'll hang forever.
2056 */
2057 if (so->so_state & SS_CANTRCVMORE) {
2058 soisdisconnected(so);
2059 callout_reset(tp->tt_2msl, tcp_maxidle,
2060 tcp_timer_2msl, tp);
2061 }
2062 tp->t_state = TCPS_FIN_WAIT_2;
2063 }
2064 break;
2065
95b22adf 2066 /*
984263bc
MD
2067 * In CLOSING STATE in addition to the processing for
2068 * the ESTABLISHED state if the ACK acknowledges our FIN
2069 * then enter the TIME-WAIT state, otherwise ignore
2070 * the segment.
2071 */
2072 case TCPS_CLOSING:
2073 if (ourfinisacked) {
2074 tp->t_state = TCPS_TIME_WAIT;
2075 tcp_canceltimers(tp);
2076 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2077 if (tp->cc_recv != 0 &&
2078 (ticks - tp->t_starttime) < tcp_msl)
2079 callout_reset(tp->tt_2msl,
2080 tp->t_rxtcur *
2081 TCPTV_TWTRUNC,
2082 tcp_timer_2msl, tp);
2083 else
2084 callout_reset(tp->tt_2msl, 2 * tcp_msl,
2085 tcp_timer_2msl, tp);
2086 soisdisconnected(so);
2087 }
2088 break;
2089
2090 /*
2091 * In LAST_ACK, we may still be waiting for data to drain
2092 * and/or to be acked, as well as for the ack of our FIN.
2093 * If our FIN is now acknowledged, delete the TCB,
2094 * enter the closed state and return.
2095 */
2096 case TCPS_LAST_ACK:
2097 if (ourfinisacked) {
2098 tp = tcp_close(tp);
2099 goto drop;
2100 }
2101 break;
2102
2103 /*
2104 * In TIME_WAIT state the only thing that should arrive
2105 * is a retransmission of the remote FIN. Acknowledge
2106 * it and restart the finack timer.
2107 */
2108 case TCPS_TIME_WAIT:
2109 callout_reset(tp->tt_2msl, 2 * tcp_msl,
2110 tcp_timer_2msl, tp);
2111 goto dropafterack;
2112 }
2113 }
2114
2115step6:
2116 /*
2117 * Update window information.
2118 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2119 */
2120 if ((thflags & TH_ACK) &&
2121 (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2122 (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2123 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2124 /* keep track of pure window updates */
2125 if (tlen == 0 &&
2126 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2127 tcpstat.tcps_rcvwinupd++;
2128 tp->snd_wnd = tiwin;
2129 tp->snd_wl1 = th->th_seq;
2130 tp->snd_wl2 = th->th_ack;
2131 if (tp->snd_wnd > tp->max_sndwnd)
2132 tp->max_sndwnd = tp->snd_wnd;
2133 needoutput = 1;
2134 }
2135
2136 /*
2137 * Process segments with URG.
2138 */
2139 if ((thflags & TH_URG) && th->th_urp &&
95b22adf 2140 !TCPS_HAVERCVDFIN(tp->t_state)) {
984263bc
MD
2141 /*
2142 * This is a kludge, but if we receive and accept
2143 * random urgent pointers, we'll crash in
2144 * soreceive. It's hard to imagine someone
2145 * actually wanting to send this much urgent data.
2146 */
2147 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2148 th->th_urp = 0; /* XXX */
2149 thflags &= ~TH_URG; /* XXX */
2150 goto dodata; /* XXX */
2151 }
2152 /*
2153 * If this segment advances the known urgent pointer,
2154 * then mark the data stream. This should not happen
2155 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2156 * a FIN has been received from the remote side.
2157 * In these states we ignore the URG.
2158 *
2159 * According to RFC961 (Assigned Protocols),
2160 * the urgent pointer points to the last octet
2161 * of urgent data. We continue, however,
2162 * to consider it to indicate the first octet
2163 * of data past the urgent section as the original
2164 * spec states (in one of two places).
2165 */
2166 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2167 tp->rcv_up = th->th_seq + th->th_urp;
2168 so->so_oobmark = so->so_rcv.sb_cc +
2169 (tp->rcv_up - tp->rcv_nxt) - 1;
2170 if (so->so_oobmark == 0)
2171 so->so_state |= SS_RCVATMARK;
2172 sohasoutofband(so);
2173 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2174 }
2175 /*
2176 * Remove out of band data so doesn't get presented to user.
2177 * This can happen independent of advancing the URG pointer,
2178 * but if two URG's are pending at once, some out-of-band
2179 * data may creep in... ick.
2180 */
2181 if (th->th_urp <= (u_long)tlen
2182#ifdef SO_OOBINLINE
2183 && (so->so_options & SO_OOBINLINE) == 0
2184#endif
2185 )
2186 tcp_pulloutofband(so, th, m,
2187 drop_hdrlen); /* hdr drop is delayed */
2188 } else {
2189 /*
2190 * If no out of band data is expected,
2191 * pull receive urgent pointer along
2192 * with the receive window.
2193 */
2194 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2195 tp->rcv_up = tp->rcv_nxt;
2196 }
2197dodata: /* XXX */
2198
2199 /*
2200 * Process the segment text, merging it into the TCP sequencing queue,
2201 * and arranging for acknowledgment of receipt if necessary.
2202 * This process logically involves adjusting tp->rcv_wnd as data
2203 * is presented to the user (this happens in tcp_usrreq.c,
2204 * case PRU_RCVD). If a FIN has already been received on this
2205 * connection then we just ignore the text.
2206 */
95b22adf 2207 if ((tlen || (thflags & TH_FIN)) && !TCPS_HAVERCVDFIN(tp->t_state)) {
984263bc
MD
2208 m_adj(m, drop_hdrlen); /* delayed header drop */
2209 /*
2210 * Insert segment which includes th into TCP reassembly queue
2211 * with control block tp. Set thflags to whether reassembly now
2212 * includes a segment with FIN. This handles the common case
2213 * inline (segment is the next to be received on an established
2214 * connection, and the queue is empty), avoiding linkage into
2215 * and removal from the queue and repetition of various
2216 * conversions.
2217 * Set DELACK for segments received in order, but ack
2218 * immediately when segments are out of order (so
2219 * fast retransmit can work).
2220 */
2221 if (th->th_seq == tp->rcv_nxt &&
2222 LIST_EMPTY(&tp->t_segq) &&
2223 TCPS_HAVEESTABLISHED(tp->t_state)) {
2224 if (DELAY_ACK(tp))
2225 callout_reset(tp->tt_delack, tcp_delacktime,
2226 tcp_timer_delack, tp);
2227 else
2228 tp->t_flags |= TF_ACKNOW;
2229 tp->rcv_nxt += tlen;
2230 thflags = th->th_flags & TH_FIN;
2231 tcpstat.tcps_rcvpack++;
2232 tcpstat.tcps_rcvbyte += tlen;
2233 ND6_HINT(tp);
2234 if (so->so_state & SS_CANTRCVMORE)
2235 m_freem(m);
2236 else
2237 sbappend(&so->so_rcv, m);
2238 sorwakeup(so);
2239 } else {
2240 thflags = tcp_reass(tp, th, &tlen, m);
2241 tp->t_flags |= TF_ACKNOW;
2242 }
2243
2244 /*
2245 * Note the amount of data that peer has sent into
2246 * our window, in order to estimate the sender's
2247 * buffer size.
2248 */
2249 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2250 } else {
2251 m_freem(m);
2252 thflags &= ~TH_FIN;
2253 }
2254
2255 /*
2256 * If FIN is received ACK the FIN and let the user know
2257 * that the connection is closing.
2258 */
2259 if (thflags & TH_FIN) {
95b22adf 2260 if (!TCPS_HAVERCVDFIN(tp->t_state)) {
984263bc
MD
2261 socantrcvmore(so);
2262 /*
2263 * If connection is half-synchronized
2264 * (ie NEEDSYN flag on) then delay ACK,
2265 * so it may be piggybacked when SYN is sent.
2266 * Otherwise, since we received a FIN then no
2267 * more input can be expected, send ACK now.
2268 */
2269 if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
d24ce1dc
JH
2270 callout_reset(tp->tt_delack, tcp_delacktime,
2271 tcp_timer_delack, tp);
984263bc
MD
2272 else
2273 tp->t_flags |= TF_ACKNOW;
2274 tp->rcv_nxt++;
2275 }
2276 switch (tp->t_state) {
2277
95b22adf 2278 /*
984263bc
MD
2279 * In SYN_RECEIVED and ESTABLISHED STATES
2280 * enter the CLOSE_WAIT state.
2281 */
2282 case TCPS_SYN_RECEIVED:
2283 tp->t_starttime = ticks;
2284 /*FALLTHROUGH*/
2285 case TCPS_ESTABLISHED:
2286 tp->t_state = TCPS_CLOSE_WAIT;
2287 break;
2288
95b22adf 2289 /*
984263bc
MD
2290 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2291 * enter the CLOSING state.
2292 */
2293 case TCPS_FIN_WAIT_1:
2294 tp->t_state = TCPS_CLOSING;
2295 break;
2296
95b22adf 2297 /*
984263bc
MD
2298 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2299 * starting the time-wait timer, turning off the other
2300 * standard timers.
2301 */
2302 case TCPS_FIN_WAIT_2:
2303 tp->t_state = TCPS_TIME_WAIT;
2304 tcp_canceltimers(tp);
2305 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2306 if (tp->cc_recv != 0 &&
2307 (ticks - tp->t_starttime) < tcp_msl) {
2308 callout_reset(tp->tt_2msl,
2309 tp->t_rxtcur * TCPTV_TWTRUNC,
2310 tcp_timer_2msl, tp);
2311 /* For transaction client, force ACK now. */
2312 tp->t_flags |= TF_ACKNOW;
2313 }
2314 else
2315 callout_reset(tp->tt_2msl, 2 * tcp_msl,
2316 tcp_timer_2msl, tp);
2317 soisdisconnected(so);
2318 break;
2319
2320 /*
2321 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2322 */
2323 case TCPS_TIME_WAIT:
2324 callout_reset(tp->tt_2msl, 2 * tcp_msl,
2325 tcp_timer_2msl, tp);
2326 break;
2327 }
2328 }
2329#ifdef TCPDEBUG
2330 if (so->so_options & SO_DEBUG)
2331 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2332 &tcp_savetcp, 0);
2333#endif
2334
2335 /*
2336 * Return any desired output.
2337 */
2338 if (needoutput || (tp->t_flags & TF_ACKNOW))
2339 (void) tcp_output(tp);
2340 return;
2341
2342dropafterack:
2343 /*
2344 * Generate an ACK dropping incoming segment if it occupies
2345 * sequence space, where the ACK reflects our state.
2346 *
2347 * We can now skip the test for the RST flag since all
2348 * paths to this code happen after packets containing
2349 * RST have been dropped.
2350 *
2351 * In the SYN-RECEIVED state, don't send an ACK unless the
2352 * segment we received passes the SYN-RECEIVED ACK test.
2353 * If it fails send a RST. This breaks the loop in the
2354 * "LAND" DoS attack, and also prevents an ACK storm
2355 * between two listening ports that have been sent forged
2356 * SYN segments, each with the source address of the other.
2357 */
2358 if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2359 (SEQ_GT(tp->snd_una, th->th_ack) ||
2360 SEQ_GT(th->th_ack, tp->snd_max)) ) {
2361 rstreason = BANDLIM_RST_OPENPORT;
2362 goto dropwithreset;
2363 }
2364#ifdef TCPDEBUG
2365 if (so->so_options & SO_DEBUG)
2366 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2367 &tcp_savetcp, 0);
2368#endif
2369 m_freem(m);
2370 tp->t_flags |= TF_ACKNOW;
2371 (void) tcp_output(tp);
2372 return;
2373
2374dropwithreset:
2375 /*
2376 * Generate a RST, dropping incoming segment.
2377 * Make ACK acceptable to originator of segment.
2378 * Don't bother to respond if destination was broadcast/multicast.
2379 */
2380 if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2381 goto drop;
2382 if (isipv6) {
2383 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2384 IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2385 goto drop;
2386 } else {
2387 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2388 IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
95b22adf
JH
2389 ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2390 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
984263bc
MD
2391 goto drop;
2392 }
2393 /* IPv6 anycast check is done at tcp6_input() */
2394
2395 /*
2396 * Perform bandwidth limiting.
2397 */
2398#ifdef ICMP_BANDLIM
2399 if (badport_bandlim(rstreason) < 0)
2400 goto drop;
2401#endif
2402
2403#ifdef TCPDEBUG
2404 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2405 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2406 &tcp_savetcp, 0);
2407#endif
2408 if (thflags & TH_ACK)
2409 /* mtod() below is safe as long as hdr dropping is delayed */
2410 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2411 TH_RST);
2412 else {
2413 if (thflags & TH_SYN)
2414 tlen++;
2415 /* mtod() below is safe as long as hdr dropping is delayed */
2416 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2417 (tcp_seq)0, TH_RST|TH_ACK);
2418 }
2419 return;
2420
2421drop:
2422 /*
2423 * Drop space held by incoming segment and return.
2424 */
2425#ifdef TCPDEBUG
2426 if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2427 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2428 &tcp_savetcp, 0);
2429#endif
2430 m_freem(m);
2431 return;
2432}
2433
2434/*
2435 * Parse TCP options and place in tcpopt.
2436 */
2437static void
95b22adf 2438tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, boolean_t is_syn)
984263bc
MD
2439{
2440 int opt, optlen;
2441
2442 to->to_flags = 0;
2443 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2444 opt = cp[0];
2445 if (opt == TCPOPT_EOL)
2446 break;
2447 if (opt == TCPOPT_NOP)
2448 optlen = 1;
2449 else {
2450 if (cnt < 2)
2451 break;
2452 optlen = cp[1];
2453 if (optlen < 2 || optlen > cnt)
2454 break;
2455 }
2456 switch (opt) {
2457 case TCPOPT_MAXSEG:
2458 if (optlen != TCPOLEN_MAXSEG)
2459 continue;
2460 if (!is_syn)
2461 continue;
2462 to->to_flags |= TOF_MSS;
95b22adf 2463 bcopy(cp + 2, &to->to_mss, sizeof(to->to_mss));
984263bc
MD
2464 to->to_mss = ntohs(to->to_mss);
2465 break;
2466 case TCPOPT_WINDOW:
2467 if (optlen != TCPOLEN_WINDOW)
2468 continue;
95b22adf 2469 if (!is_syn)
984263bc
MD
2470 continue;
2471 to->to_flags |= TOF_SCALE;
2472 to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2473 break;
2474 case TCPOPT_TIMESTAMP:
2475 if (optlen != TCPOLEN_TIMESTAMP)
2476 continue;
2477 to->to_flags |= TOF_TS;
95b22adf 2478 bcopy(cp + 2, &to->to_tsval, sizeof(to->to_tsval));
984263bc 2479 to->to_tsval = ntohl(to->to_tsval);
95b22adf 2480 bcopy(cp + 6, &to->to_tsecr, sizeof(to->to_tsecr));
984263bc
MD
2481 to->to_tsecr = ntohl(to->to_tsecr);
2482 break;
2483 case TCPOPT_CC:
2484 if (optlen != TCPOLEN_CC)
2485 continue;
2486 to->to_flags |= TOF_CC;
95b22adf 2487 bcopy(cp + 2, &to->to_cc, sizeof(to->to_cc));
984263bc
MD
2488 to->to_cc = ntohl(to->to_cc);
2489 break;
2490 case TCPOPT_CCNEW:
2491 if (optlen != TCPOLEN_CC)
2492 continue;
2493 if (!is_syn)
2494 continue;
2495 to->to_flags |= TOF_CCNEW;
95b22adf 2496 bcopy(cp + 2, &to->to_cc, sizeof(to->to_cc));
984263bc
MD
2497 to->to_cc = ntohl(to->to_cc);
2498 break;
2499 case TCPOPT_CCECHO:
2500 if (optlen != TCPOLEN_CC)
2501 continue;
2502 if (!is_syn)
2503 continue;
2504 to->to_flags |= TOF_CCECHO;
95b22adf 2505 bcopy(cp + 2, &to->to_ccecho, sizeof(to->to_ccecho));
984263bc
MD
2506 to->to_ccecho = ntohl(to->to_ccecho);
2507 break;
2508 default:
2509 continue;
2510 }
2511 }
2512}
2513
2514/*
2515 * Pull out of band byte out of a segment so
2516 * it doesn't appear in the user's data queue.
2517 * It is still reflected in the segment length for
2518 * sequencing purposes.
95b22adf 2519 * "off" is the delayed to be dropped hdrlen.
984263bc
MD
2520 */
2521static void
95b22adf 2522tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, int off)
984263bc
MD
2523{
2524 int cnt = off + th->th_urp - 1;
2525
2526 while (cnt >= 0) {
2527 if (m->m_len > cnt) {
2528 char *cp = mtod(m, caddr_t) + cnt;
2529 struct tcpcb *tp = sototcpcb(so);
2530
2531 tp->t_iobc = *cp;
2532 tp->t_oobflags |= TCPOOB_HAVEDATA;
95b22adf 2533 bcopy(cp + 1, cp, m->m_len - cnt - 1);
984263bc
MD
2534 m->m_len--;
2535 if (m->m_flags & M_PKTHDR)
2536 m->m_pkthdr.len--;
2537 return;
2538 }
2539 cnt -= m->m_len;
2540 m = m->m_next;
2541 if (m == 0)
2542 break;
2543 }
2544 panic("tcp_pulloutofband");
2545}
2546
2547/*
2548 * Collect new round-trip time estimate
2549 * and update averages and current timeout.
2550 */
2551static void
95b22adf 2552tcp_xmit_timer(struct tcpcb *tp, int rtt)
984263bc 2553{
2256ba69 2554 int delta;
984263bc
MD
2555
2556 tcpstat.tcps_rttupdated++;
2557 tp->t_rttupdated++;
2558 if (tp->t_srtt != 0) {
2559 /*
2560 * srtt is stored as fixed point with 5 bits after the
2561 * binary point (i.e., scaled by 8). The following magic
2562 * is equivalent to the smoothing algorithm in rfc793 with
2563 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2564 * point). Adjust rtt to origin 0.
2565 */
2566 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2567 - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2568
2569 if ((tp->t_srtt += delta) <= 0)
2570 tp->t_srtt = 1;
2571
2572 /*
2573 * We accumulate a smoothed rtt variance (actually, a
2574 * smoothed mean difference), then set the retransmit
2575 * timer to smoothed rtt + 4 times the smoothed variance.
2576 * rttvar is stored as fixed point with 4 bits after the
2577 * binary point (scaled by 16). The following is
2578 * equivalent to rfc793 smoothing with an alpha of .75
2579 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2580 * rfc793's wired-in beta.
2581 */
2582 if (delta < 0)
2583 delta = -delta;
2584 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2585 if ((tp->t_rttvar += delta) <= 0)
2586 tp->t_rttvar = 1;
2587 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
2588 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2589 } else {
2590 /*
2591 * No rtt measurement yet - use the unsmoothed rtt.
2592 * Set the variance to half the rtt (so our first
2593 * retransmit happens at 3*rtt).
2594 */
2595 tp->t_srtt = rtt << TCP_RTT_SHIFT;
2596 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2597 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2598 }
2599 tp->t_rtttime = 0;
2600 tp->t_rxtshift = 0;
2601
2602 /*
2603 * the retransmit should happen at rtt + 4 * rttvar.
2604 * Because of the way we do the smoothing, srtt and rttvar
2605 * will each average +1/2 tick of bias. When we compute
2606 * the retransmit timer, we want 1/2 tick of rounding and
2607 * 1 extra tick because of +-1/2 tick uncertainty in the
2608 * firing of the timer. The bias will give us exactly the
2609 * 1.5 tick we need. But, because the bias is
2610 * statistical, we have to test that we don't drop below
2611 * the minimum feasible timer (which is 2 ticks).
2612 */
2613 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2614 max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2615
2616 /*
2617 * We received an ack for a packet that wasn't retransmitted;
2618 * it is probably safe to discard any error indications we've
2619 * received recently. This isn't quite right, but close enough
2620 * for now (a route might have failed after we sent a segment,
2621 * and the return path might not be symmetrical).
2622 */
2623 tp->t_softerror = 0;
2624}
2625
2626/*
2627 * Determine a reasonable value for maxseg size.
2628 * If the route is known, check route for mtu.
2629 * If none, use an mss that can be handled on the outgoing
2630 * interface without forcing IP to fragment; if bigger than
2631 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2632 * to utilize large mbufs. If no route is found, route has no mtu,
2633 * or the destination isn't local, use a default, hopefully conservative
2634 * size (usually 512 or the default IP max size, but no more than the mtu
2635 * of the interface), as we can't discover anything about intervening
2636 * gateways or networks. We also initialize the congestion/slow start
2637 * window to be a single segment if the destination isn't local.
2638 * While looking at the routing entry, we also initialize other path-dependent
2639 * parameters from pre-set or cached values in the routing entry.
2640 *
2641 * Also take into account the space needed for options that we
2642 * send regularly. Make maxseg shorter by that amount to assure
2643 * that we can send maxseg amount of data even when the options
2644 * are present. Store the upper limit of the length of options plus
2645 * data in maxopd.
2646 *
2647 * NOTE that this routine is only called when we process an incoming
2648 * segment, for outgoing segments only tcp_mssopt is called.
2649 *
2650 * In case of T/TCP, we call this routine during implicit connection
2651 * setup as well (offer = -1), to initialize maxseg from the cached
2652 * MSS of our peer.
2653 */
2654void
95b22adf 2655tcp_mss(struct tcpcb *tp, int offer)
984263bc 2656{
2256ba69 2657 struct rtentry *rt;
984263bc 2658 struct ifnet *ifp;
2256ba69 2659 int rtt, mss;
984263bc
MD
2660 u_long bufsize;
2661 struct inpcb *inp = tp->t_inpcb;
2662 struct socket *so;
2663 struct rmxp_tao *taop;
2664 int origoffer = offer;
2665#ifdef INET6
d24ce1dc 2666 boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE);
984263bc
MD
2667 size_t min_protoh = isipv6 ?
2668 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
2669 sizeof(struct tcpiphdr);
2670#else
d24ce1dc 2671 const boolean_t isipv6 = FALSE;
984263bc
MD
2672 const size_t min_protoh = sizeof(struct tcpiphdr);
2673#endif
2674
2675 if (isipv6)
2676 rt = tcp_rtlookup6(&inp->inp_inc);
2677 else
2678 rt = tcp_rtlookup(&inp->inp_inc);
2679 if (rt == NULL) {
2680 tp->t_maxopd = tp->t_maxseg =
d24ce1dc 2681 (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
984263bc
MD
2682 return;
2683 }
2684 ifp = rt->rt_ifp;
2685 so = inp->inp_socket;
2686
2687 taop = rmx_taop(rt->rt_rmx);
2688 /*
2689 * Offer == -1 means that we didn't receive SYN yet,
2690 * use cached value in that case;
2691 */
2692 if (offer == -1)
2693 offer = taop->tao_mssopt;
2694 /*
2695 * Offer == 0 means that there was no MSS on the SYN segment,
2696 * in this case we use tcp_mssdflt.
2697 */
2698 if (offer == 0)
d24ce1dc 2699 offer = (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
984263bc
MD
2700 else
2701 /*
2702 * Sanity check: make sure that maxopd will be large
2703 * enough to allow some data on segments even is the
2704 * all the option space is used (40bytes). Otherwise
2705 * funny things may happen in tcp_output.
2706 */
2707 offer = max(offer, 64);
2708 taop->tao_mssopt = offer;
2709
2710 /*
2711 * While we're here, check if there's an initial rtt
2712 * or rttvar. Convert from the route-table units
2713 * to scaled multiples of the slow timeout timer.
2714 */
2715 if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2716 /*
2717 * XXX the lock bit for RTT indicates that the value
2718 * is also a minimum value; this is subject to time.
2719 */
2720 if (rt->rt_rmx.rmx_locks & RTV_RTT)
2721 tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
2722 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2723 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
2724 tcpstat.tcps_usedrtt++;
2725 if (rt->rt_rmx.rmx_rttvar) {
2726 tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2727 (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2728 tcpstat.tcps_usedrttvar++;
2729 } else {
2730 /* default variation is +- 1 rtt */
2731 tp->t_rttvar =
2732 tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2733 }
2734 TCPT_RANGESET(tp->t_rxtcur,
2735 ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2736 tp->t_rttmin, TCPTV_REXMTMAX);
2737 }
2738 /*
2739 * if there's an mtu associated with the route, use it
2740 * else, use the link mtu.
2741 */
2742 if (rt->rt_rmx.rmx_mtu)
2743 mss = rt->rt_rmx.rmx_mtu - min_protoh;
2744 else {
2745 if (isipv6) {
2746 mss = nd_ifinfo[rt->rt_ifp->if_index].linkmtu -
2747 min_protoh;
2748 if (!in6_localaddr(&inp->in6p_faddr))
2749 mss = min(mss, tcp_v6mssdflt);
2750 } else {
2751 mss = ifp->if_mtu - min_protoh;
2752 if (!in_localaddr(inp->inp_faddr))
2753 mss = min(mss, tcp_mssdflt);
2754 }
2755 }
2756 mss = min(mss, offer);
2757 /*
2758 * maxopd stores the maximum length of data AND options
2759 * in a segment; maxseg is the amount of data in a normal
2760 * segment. We need to store this value (maxopd) apart
2761 * from maxseg, because now every segment carries options
2762 * and thus we normally have somewhat less data in segments.
2763 */
2764 tp->t_maxopd = mss;
2765
2766 /*
2767 * In case of T/TCP, origoffer==-1 indicates, that no segments
2768 * were received yet. In this case we just guess, otherwise
2769 * we do the same as before T/TCP.
2770 */
95b22adf 2771 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
984263bc
MD
2772 (origoffer == -1 ||
2773 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2774 mss -= TCPOLEN_TSTAMP_APPA;
95b22adf 2775 if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
984263bc
MD
2776 (origoffer == -1 ||
2777 (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2778 mss -= TCPOLEN_CC_APPA;
2779
2780#if (MCLBYTES & (MCLBYTES - 1)) == 0
2781 if (mss > MCLBYTES)
2782 mss &= ~(MCLBYTES-1);
2783#else
2784 if (mss > MCLBYTES)
2785 mss = mss / MCLBYTES * MCLBYTES;
2786#endif
2787 /*
2788 * If there's a pipesize, change the socket buffer
2789 * to that size. Make the socket buffers an integral
2790 * number of mss units; if the mss is larger than
2791 * the socket buffer, decrease the mss.
2792 */
2793#ifdef RTV_SPIPE
2794 if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2795#endif
2796 bufsize = so->so_snd.sb_hiwat;
2797 if (bufsize < mss)
2798 mss = bufsize;
2799 else {
2800 bufsize = roundup(bufsize, mss);
2801 if (bufsize > sb_max)
2802 bufsize = sb_max;
2803 if (bufsize > so->so_snd.sb_hiwat)
2804 (void)sbreserve(&so->so_snd, bufsize, so, NULL);
2805 }
2806 tp->t_maxseg = mss;
2807
2808#ifdef RTV_RPIPE
2809 if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2810#endif
2811 bufsize = so->so_rcv.sb_hiwat;
2812 if (bufsize > mss) {
2813 bufsize = roundup(bufsize, mss);
2814 if (bufsize > sb_max)
2815 bufsize = sb_max;
2816 if (bufsize > so->so_rcv.sb_hiwat)
2817 (void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2818 }
2819
2820 /*
2821 * Set the slow-start flight size depending on whether this
2822 * is a local network or not.
2823 */
4b52d5ee
JH
2824 if (tcp_do_rfc3390)
2825 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
2826 else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2827 (!isipv6 && in_localaddr(inp->inp_faddr)))
984263bc 2828 tp->snd_cwnd = mss * ss_fltsz_local;
4b52d5ee 2829 else
984263bc
MD
2830 tp->snd_cwnd = mss * ss_fltsz;
2831
2832 if (rt->rt_rmx.rmx_ssthresh) {
2833 /*
2834 * There's some sort of gateway or interface
2835 * buffer limit on the path. Use this to set
2836 * the slow start threshhold, but set the
2837 * threshold to no less than 2*mss.
2838 */
2839 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2840 tcpstat.tcps_usedssthresh++;
2841 }
2842}
2843
2844/*
2845 * Determine the MSS option to send on an outgoing SYN.
2846 */
2847int
95b22adf 2848tcp_mssopt(struct tcpcb *tp)
984263bc
MD
2849{
2850 struct rtentry *rt;
2851#ifdef INET6
d24ce1dc
JH
2852 boolean_t isipv6 =
2853 ((tp->t_inpcb->inp_vflag & INP_IPV6) ? TRUE : FALSE);
984263bc
MD
2854 int min_protoh = isipv6 ?
2855 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
2856 sizeof(struct tcpiphdr);
2857#else
d24ce1dc 2858 const boolean_t isipv6 = FALSE;
984263bc
MD
2859 const size_t min_protoh = sizeof(struct tcpiphdr);
2860#endif
2861
2862 if (isipv6)
2863 rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc);
2864 else
2865 rt = tcp_rtlookup(&tp->t_inpcb->inp_inc);
2866 if (rt == NULL)
2867 return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
2868
2869 return (rt->rt_ifp->if_mtu - min_protoh);
2870}
2871
2872
2873/*
2874 * When a partial ack arrives, force the retransmission of the
2875 * next unacknowledged segment. Do not clear tp->t_dupacks.
2876 * By setting snd_nxt to ti_ack, this forces retransmission timer to
2877 * be started again.
2878 */
2879static void
95b22adf 2880tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
984263bc
MD
2881{
2882 tcp_seq onxt = tp->snd_nxt;
2883 u_long ocwnd = tp->snd_cwnd;
2884
2885 callout_stop(tp->tt_rexmt);
2886 tp->t_rtttime = 0;
2887 tp->snd_nxt = th->th_ack;
2888 /*
2889 * Set snd_cwnd to one segment beyond acknowledged offset
2890 * (tp->snd_una has not yet been updated when this function is called.)
2891 */
2892 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
2893 tp->t_flags |= TF_ACKNOW;
2894 (void) tcp_output(tp);
2895 tp->snd_cwnd = ocwnd;
2896 if (SEQ_GT(onxt, tp->snd_nxt))
2897 tp->snd_nxt = onxt;
2898 /*
2899 * Partial window deflation. Relies on fact that tp->snd_una
2900 * not updated yet.
2901 */
2902 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
2903}