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