Home | History | Annotate | Line # | Download | only in netinet
tcp_input.c revision 1.154
      1  1.154    itojun /*	$NetBSD: tcp_input.c,v 1.154 2002/09/05 23:02:18 itojun Exp $	*/
      2   1.83    itojun 
      3   1.83    itojun /*
      4   1.83    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.83    itojun  * All rights reserved.
      6  1.143    itojun  *
      7   1.83    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.83    itojun  * modification, are permitted provided that the following conditions
      9   1.83    itojun  * are met:
     10   1.83    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.83    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.83    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.83    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.83    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.83    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.83    itojun  *    may be used to endorse or promote products derived from this software
     17   1.83    itojun  *    without specific prior written permission.
     18  1.143    itojun  *
     19   1.83    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.83    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.83    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.83    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.83    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.83    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.83    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.83    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.83    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.83    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.83    itojun  * SUCH DAMAGE.
     30   1.83    itojun  */
     31   1.44   thorpej 
     32  1.134    itojun /*
     33  1.134    itojun  *      @(#)COPYRIGHT   1.1 (NRL) 17 January 1995
     34  1.143    itojun  *
     35  1.134    itojun  * NRL grants permission for redistribution and use in source and binary
     36  1.134    itojun  * forms, with or without modification, of the software and documentation
     37  1.134    itojun  * created at NRL provided that the following conditions are met:
     38  1.143    itojun  *
     39  1.134    itojun  * 1. Redistributions of source code must retain the above copyright
     40  1.134    itojun  *    notice, this list of conditions and the following disclaimer.
     41  1.134    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.134    itojun  *    notice, this list of conditions and the following disclaimer in the
     43  1.134    itojun  *    documentation and/or other materials provided with the distribution.
     44  1.134    itojun  * 3. All advertising materials mentioning features or use of this software
     45  1.134    itojun  *    must display the following acknowledgements:
     46  1.134    itojun  *      This product includes software developed by the University of
     47  1.134    itojun  *      California, Berkeley and its contributors.
     48  1.134    itojun  *      This product includes software developed at the Information
     49  1.134    itojun  *      Technology Division, US Naval Research Laboratory.
     50  1.134    itojun  * 4. Neither the name of the NRL nor the names of its contributors
     51  1.134    itojun  *    may be used to endorse or promote products derived from this software
     52  1.134    itojun  *    without specific prior written permission.
     53  1.143    itojun  *
     54  1.134    itojun  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
     55  1.134    itojun  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  1.134    itojun  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     57  1.134    itojun  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
     58  1.134    itojun  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     59  1.134    itojun  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     60  1.134    itojun  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     61  1.134    itojun  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     62  1.134    itojun  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     63  1.134    itojun  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     64  1.134    itojun  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     65  1.143    itojun  *
     66  1.134    itojun  * The views and conclusions contained in the software and documentation
     67  1.134    itojun  * are those of the authors and should not be interpreted as representing
     68  1.134    itojun  * official policies, either expressed or implied, of the US Naval
     69  1.134    itojun  * Research Laboratory (NRL).
     70  1.134    itojun  */
     71  1.134    itojun 
     72   1.44   thorpej /*-
     73  1.123   thorpej  * Copyright (c) 1997, 1998, 1999, 2001 The NetBSD Foundation, Inc.
     74   1.44   thorpej  * All rights reserved.
     75   1.44   thorpej  *
     76   1.44   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     77   1.44   thorpej  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     78   1.44   thorpej  * Facility, NASA Ames Research Center.
     79   1.44   thorpej  *
     80   1.44   thorpej  * Redistribution and use in source and binary forms, with or without
     81   1.44   thorpej  * modification, are permitted provided that the following conditions
     82   1.44   thorpej  * are met:
     83   1.44   thorpej  * 1. Redistributions of source code must retain the above copyright
     84   1.44   thorpej  *    notice, this list of conditions and the following disclaimer.
     85   1.44   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     86   1.44   thorpej  *    notice, this list of conditions and the following disclaimer in the
     87   1.44   thorpej  *    documentation and/or other materials provided with the distribution.
     88   1.44   thorpej  * 3. All advertising materials mentioning features or use of this software
     89   1.44   thorpej  *    must display the following acknowledgement:
     90   1.44   thorpej  *	This product includes software developed by the NetBSD
     91   1.44   thorpej  *	Foundation, Inc. and its contributors.
     92   1.44   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     93   1.44   thorpej  *    contributors may be used to endorse or promote products derived
     94   1.44   thorpej  *    from this software without specific prior written permission.
     95   1.44   thorpej  *
     96   1.44   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     97   1.44   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     98   1.44   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     99   1.44   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    100   1.44   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    101   1.44   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    102   1.44   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    103   1.44   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    104   1.44   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    105   1.44   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    106   1.44   thorpej  * POSSIBILITY OF SUCH DAMAGE.
    107   1.44   thorpej  */
    108   1.10       cgd 
    109    1.1       cgd /*
    110   1.39   thorpej  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
    111    1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
    112    1.1       cgd  *
    113    1.1       cgd  * Redistribution and use in source and binary forms, with or without
    114    1.1       cgd  * modification, are permitted provided that the following conditions
    115    1.1       cgd  * are met:
    116    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
    117    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
    118    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
    119    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
    120    1.1       cgd  *    documentation and/or other materials provided with the distribution.
    121    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
    122    1.1       cgd  *    must display the following acknowledgement:
    123    1.1       cgd  *	This product includes software developed by the University of
    124    1.1       cgd  *	California, Berkeley and its contributors.
    125    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
    126    1.1       cgd  *    may be used to endorse or promote products derived from this software
    127    1.1       cgd  *    without specific prior written permission.
    128    1.1       cgd  *
    129    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    130    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    131    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    132    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    133    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    134    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    135    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    136    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    137    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    138    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    139    1.1       cgd  * SUCH DAMAGE.
    140    1.1       cgd  *
    141   1.39   thorpej  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
    142    1.1       cgd  */
    143    1.1       cgd 
    144   1.29   thorpej /*
    145   1.29   thorpej  *	TODO list for SYN cache stuff:
    146   1.29   thorpej  *
    147   1.48   thorpej  *	Find room for a "state" field, which is needed to keep a
    148   1.48   thorpej  *	compressed state for TIME_WAIT TCBs.  It's been noted already
    149   1.48   thorpej  *	that this is fairly important for very high-volume web and
    150   1.48   thorpej  *	mail servers, which use a large number of short-lived
    151   1.48   thorpej  *	connections.
    152   1.29   thorpej  */
    153  1.133     lukem 
    154  1.133     lukem #include <sys/cdefs.h>
    155  1.154    itojun __KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.154 2002/09/05 23:02:18 itojun Exp $");
    156   1.29   thorpej 
    157   1.83    itojun #include "opt_inet.h"
    158   1.85   thorpej #include "opt_ipsec.h"
    159  1.125   thorpej #include "opt_inet_csum.h"
    160  1.127       abs #include "opt_tcp_debug.h"
    161   1.83    itojun 
    162    1.3   mycroft #include <sys/param.h>
    163    1.3   mycroft #include <sys/systm.h>
    164    1.3   mycroft #include <sys/malloc.h>
    165    1.3   mycroft #include <sys/mbuf.h>
    166    1.3   mycroft #include <sys/protosw.h>
    167    1.3   mycroft #include <sys/socket.h>
    168    1.3   mycroft #include <sys/socketvar.h>
    169    1.3   mycroft #include <sys/errno.h>
    170   1.52   thorpej #include <sys/syslog.h>
    171   1.63   thorpej #include <sys/pool.h>
    172   1.83    itojun #include <sys/domain.h>
    173  1.129   thorpej #include <sys/kernel.h>
    174    1.1       cgd 
    175    1.3   mycroft #include <net/if.h>
    176    1.3   mycroft #include <net/route.h>
    177   1.87    itojun #include <net/if_types.h>
    178    1.1       cgd 
    179    1.3   mycroft #include <netinet/in.h>
    180    1.3   mycroft #include <netinet/in_systm.h>
    181    1.3   mycroft #include <netinet/ip.h>
    182    1.3   mycroft #include <netinet/in_pcb.h>
    183    1.3   mycroft #include <netinet/ip_var.h>
    184   1.83    itojun 
    185   1.83    itojun #ifdef INET6
    186   1.83    itojun #ifndef INET
    187   1.83    itojun #include <netinet/in.h>
    188   1.83    itojun #endif
    189   1.83    itojun #include <netinet/ip6.h>
    190  1.112    itojun #include <netinet6/ip6_var.h>
    191   1.83    itojun #include <netinet6/in6_pcb.h>
    192   1.83    itojun #include <netinet6/ip6_var.h>
    193   1.83    itojun #include <netinet6/in6_var.h>
    194   1.83    itojun #include <netinet/icmp6.h>
    195   1.98    itojun #include <netinet6/nd6.h>
    196   1.83    itojun #endif
    197   1.83    itojun 
    198   1.99    itojun #ifdef PULLDOWN_TEST
    199   1.99    itojun #ifndef INET6
    200   1.99    itojun /* always need ip6.h for IP6_EXTHDR_GET */
    201   1.99    itojun #include <netinet/ip6.h>
    202   1.99    itojun #endif
    203   1.99    itojun #endif
    204   1.99    itojun 
    205    1.3   mycroft #include <netinet/tcp.h>
    206    1.3   mycroft #include <netinet/tcp_fsm.h>
    207    1.3   mycroft #include <netinet/tcp_seq.h>
    208    1.3   mycroft #include <netinet/tcp_timer.h>
    209    1.3   mycroft #include <netinet/tcp_var.h>
    210    1.3   mycroft #include <netinet/tcpip.h>
    211    1.3   mycroft #include <netinet/tcp_debug.h>
    212    1.1       cgd 
    213   1.23  christos #include <machine/stdarg.h>
    214   1.23  christos 
    215   1.83    itojun #ifdef IPSEC
    216   1.83    itojun #include <netinet6/ipsec.h>
    217   1.83    itojun #include <netkey/key.h>
    218   1.83    itojun #endif /*IPSEC*/
    219   1.88    itojun #ifdef INET6
    220   1.87    itojun #include "faith.h"
    221  1.124    itojun #if defined(NFAITH) && NFAITH > 0
    222  1.124    itojun #include <net/if_faith.h>
    223  1.124    itojun #endif
    224   1.88    itojun #endif
    225   1.87    itojun 
    226    1.1       cgd int	tcprexmtthresh = 3;
    227   1.82        ad int	tcp_log_refused;
    228    1.9   mycroft 
    229  1.116    itojun static int tcp_rst_ppslim_count = 0;
    230  1.116    itojun static struct timeval tcp_rst_ppslim_last;
    231  1.104   thorpej 
    232    1.9   mycroft #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
    233    1.9   mycroft 
    234    1.9   mycroft /* for modulo comparisons of timestamps */
    235    1.9   mycroft #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
    236    1.9   mycroft #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
    237    1.9   mycroft 
    238    1.1       cgd /*
    239   1.98    itojun  * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint.
    240   1.98    itojun  */
    241   1.98    itojun #ifdef INET6
    242   1.98    itojun #define ND6_HINT(tp) \
    243   1.98    itojun do { \
    244   1.98    itojun 	if (tp && tp->t_in6pcb && tp->t_family == AF_INET6 \
    245   1.98    itojun 	 && tp->t_in6pcb->in6p_route.ro_rt) { \
    246  1.112    itojun 		nd6_nud_hint(tp->t_in6pcb->in6p_route.ro_rt, NULL, 0); \
    247   1.98    itojun 	} \
    248   1.98    itojun } while (0)
    249   1.98    itojun #else
    250   1.98    itojun #define ND6_HINT(tp)
    251   1.98    itojun #endif
    252   1.98    itojun 
    253   1.98    itojun /*
    254   1.37   thorpej  * Macro to compute ACK transmission behavior.  Delay the ACK unless
    255   1.47   thorpej  * we have already delayed an ACK (must send an ACK every two segments).
    256   1.55   thorpej  * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
    257   1.55   thorpej  * option is enabled.
    258   1.37   thorpej  */
    259   1.83    itojun #define	TCP_SETUP_ACK(tp, th) \
    260   1.37   thorpej do { \
    261   1.55   thorpej 	if ((tp)->t_flags & TF_DELACK || \
    262   1.83    itojun 	    (tcp_ack_on_push && (th)->th_flags & TH_PUSH)) \
    263   1.37   thorpej 		tp->t_flags |= TF_ACKNOW; \
    264   1.37   thorpej 	else \
    265   1.38   thorpej 		TCP_SET_DELACK(tp); \
    266   1.37   thorpej } while (0)
    267   1.37   thorpej 
    268  1.103   thorpej /*
    269  1.103   thorpej  * Convert TCP protocol fields to host order for easier processing.
    270  1.103   thorpej  */
    271  1.103   thorpej #define	TCP_FIELDS_TO_HOST(th)						\
    272  1.103   thorpej do {									\
    273  1.103   thorpej 	NTOHL((th)->th_seq);						\
    274  1.103   thorpej 	NTOHL((th)->th_ack);						\
    275  1.103   thorpej 	NTOHS((th)->th_win);						\
    276  1.103   thorpej 	NTOHS((th)->th_urp);						\
    277  1.103   thorpej } while (0)
    278  1.103   thorpej 
    279  1.153   thorpej /*
    280  1.153   thorpej  * ... and reverse the above.
    281  1.153   thorpej  */
    282  1.153   thorpej #define	TCP_FIELDS_TO_NET(th)						\
    283  1.153   thorpej do {									\
    284  1.153   thorpej 	HTONL((th)->th_seq);						\
    285  1.153   thorpej 	HTONL((th)->th_ack);						\
    286  1.153   thorpej 	HTONS((th)->th_win);						\
    287  1.153   thorpej 	HTONS((th)->th_urp);						\
    288  1.153   thorpej } while (0)
    289  1.153   thorpej 
    290  1.125   thorpej #ifdef TCP_CSUM_COUNTERS
    291  1.125   thorpej #include <sys/device.h>
    292  1.125   thorpej 
    293  1.125   thorpej extern struct evcnt tcp_hwcsum_ok;
    294  1.125   thorpej extern struct evcnt tcp_hwcsum_bad;
    295  1.125   thorpej extern struct evcnt tcp_hwcsum_data;
    296  1.125   thorpej extern struct evcnt tcp_swcsum;
    297  1.125   thorpej 
    298  1.125   thorpej #define	TCP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    299  1.125   thorpej 
    300  1.125   thorpej #else
    301  1.125   thorpej 
    302  1.125   thorpej #define	TCP_CSUM_COUNTER_INCR(ev)	/* nothing */
    303  1.125   thorpej 
    304  1.125   thorpej #endif /* TCP_CSUM_COUNTERS */
    305  1.125   thorpej 
    306  1.141      matt #ifdef TCP_REASS_COUNTERS
    307  1.141      matt #include <sys/device.h>
    308  1.141      matt 
    309  1.141      matt extern struct evcnt tcp_reass_;
    310  1.141      matt extern struct evcnt tcp_reass_empty;
    311  1.141      matt extern struct evcnt tcp_reass_iteration[8];
    312  1.141      matt extern struct evcnt tcp_reass_prependfirst;
    313  1.141      matt extern struct evcnt tcp_reass_prepend;
    314  1.141      matt extern struct evcnt tcp_reass_insert;
    315  1.141      matt extern struct evcnt tcp_reass_inserttail;
    316  1.141      matt extern struct evcnt tcp_reass_append;
    317  1.141      matt extern struct evcnt tcp_reass_appendtail;
    318  1.141      matt extern struct evcnt tcp_reass_overlaptail;
    319  1.141      matt extern struct evcnt tcp_reass_overlapfront;
    320  1.141      matt extern struct evcnt tcp_reass_segdup;
    321  1.141      matt extern struct evcnt tcp_reass_fragdup;
    322  1.141      matt 
    323  1.141      matt #define	TCP_REASS_COUNTER_INCR(ev)	(ev)->ev_count++
    324  1.141      matt 
    325  1.141      matt #else
    326  1.141      matt 
    327  1.141      matt #define	TCP_REASS_COUNTER_INCR(ev)	/* nothing */
    328  1.141      matt 
    329  1.141      matt #endif /* TCP_REASS_COUNTERS */
    330  1.141      matt 
    331  1.145      yamt #ifdef INET
    332  1.145      yamt static void tcp4_log_refused __P((const struct ip *, const struct tcphdr *));
    333  1.145      yamt #endif
    334  1.145      yamt #ifdef INET6
    335  1.145      yamt static void tcp6_log_refused
    336  1.145      yamt     __P((const struct ip6_hdr *, const struct tcphdr *));
    337  1.145      yamt #endif
    338  1.145      yamt 
    339    1.5   mycroft int
    340   1.83    itojun tcp_reass(tp, th, m, tlen)
    341  1.106  augustss 	struct tcpcb *tp;
    342  1.106  augustss 	struct tcphdr *th;
    343    1.1       cgd 	struct mbuf *m;
    344   1.83    itojun 	int *tlen;
    345    1.1       cgd {
    346  1.106  augustss 	struct ipqent *p, *q, *nq, *tiqe = NULL;
    347   1.83    itojun 	struct socket *so = NULL;
    348   1.54      matt 	int pkt_flags;
    349   1.54      matt 	tcp_seq pkt_seq;
    350   1.54      matt 	unsigned pkt_len;
    351   1.54      matt 	u_long rcvpartdupbyte = 0;
    352   1.54      matt 	u_long rcvoobyte;
    353  1.141      matt #ifdef TCP_REASS_COUNTERS
    354  1.141      matt 	u_int count = 0;
    355  1.141      matt #endif
    356    1.1       cgd 
    357   1.83    itojun 	if (tp->t_inpcb)
    358   1.83    itojun 		so = tp->t_inpcb->inp_socket;
    359   1.83    itojun #ifdef INET6
    360   1.83    itojun 	else if (tp->t_in6pcb)
    361   1.83    itojun 		so = tp->t_in6pcb->in6p_socket;
    362   1.83    itojun #endif
    363   1.83    itojun 
    364   1.72   thorpej 	TCP_REASS_LOCK_CHECK(tp);
    365   1.72   thorpej 
    366    1.1       cgd 	/*
    367   1.83    itojun 	 * Call with th==0 after become established to
    368    1.1       cgd 	 * force pre-ESTABLISHED data up to user socket.
    369    1.1       cgd 	 */
    370   1.83    itojun 	if (th == 0)
    371    1.1       cgd 		goto present;
    372    1.1       cgd 
    373   1.83    itojun 	rcvoobyte = *tlen;
    374    1.1       cgd 	/*
    375   1.54      matt 	 * Copy these to local variables because the tcpiphdr
    376   1.54      matt 	 * gets munged while we are collapsing mbufs.
    377   1.20       cgd 	 */
    378   1.83    itojun 	pkt_seq = th->th_seq;
    379   1.83    itojun 	pkt_len = *tlen;
    380   1.83    itojun 	pkt_flags = th->th_flags;
    381  1.141      matt 
    382  1.141      matt 	TCP_REASS_COUNTER_INCR(&tcp_reass_);
    383  1.141      matt 
    384  1.141      matt 	if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) {
    385  1.141      matt 		/*
    386  1.141      matt 		 * When we miss a packet, the vast majority of time we get
    387  1.141      matt 		 * packets that follow it in order.  So optimize for that.
    388  1.141      matt 		 */
    389  1.141      matt 		if (pkt_seq == p->ipqe_seq + p->ipqe_len) {
    390  1.141      matt 			p->ipqe_len += pkt_len;
    391  1.141      matt 			p->ipqe_flags |= pkt_flags;
    392  1.141      matt 			m_cat(p->ipqe_m, m);
    393  1.141      matt 			tiqe = p;
    394  1.141      matt 			TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq);
    395  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail);
    396  1.141      matt 			goto skip_replacement;
    397  1.141      matt 		}
    398  1.141      matt 		/*
    399  1.141      matt 		 * While we're here, if the pkt is completely beyond
    400  1.141      matt 		 * anything we have, just insert it at the tail.
    401  1.141      matt 		 */
    402  1.141      matt 		if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) {
    403  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail);
    404  1.141      matt 			goto insert_it;
    405  1.141      matt 		}
    406  1.141      matt 	}
    407  1.141      matt 
    408  1.141      matt 	q = TAILQ_FIRST(&tp->segq);
    409  1.141      matt 
    410  1.141      matt 	if (q != NULL) {
    411  1.141      matt 		/*
    412  1.141      matt 		 * If this segment immediately precedes the first out-of-order
    413  1.141      matt 		 * block, simply slap the segment in front of it and (mostly)
    414  1.141      matt 		 * skip the complicated logic.
    415  1.141      matt 		 */
    416  1.141      matt 		if (pkt_seq + pkt_len == q->ipqe_seq) {
    417  1.141      matt 			q->ipqe_seq = pkt_seq;
    418  1.141      matt 			q->ipqe_len += pkt_len;
    419  1.141      matt 			q->ipqe_flags |= pkt_flags;
    420  1.141      matt 			m_cat(m, q->ipqe_m);
    421  1.141      matt 			q->ipqe_m = m;
    422  1.141      matt 			tiqe = q;
    423  1.141      matt 			TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    424  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst);
    425  1.141      matt 			goto skip_replacement;
    426  1.141      matt 		}
    427  1.141      matt 	} else {
    428  1.141      matt 		TCP_REASS_COUNTER_INCR(&tcp_reass_empty);
    429  1.141      matt 	}
    430  1.141      matt 
    431   1.20       cgd 	/*
    432    1.1       cgd 	 * Find a segment which begins after this one does.
    433    1.1       cgd 	 */
    434  1.141      matt 	for (p = NULL; q != NULL; q = nq) {
    435  1.141      matt 		nq = TAILQ_NEXT(q, ipqe_q);
    436  1.141      matt #ifdef TCP_REASS_COUNTERS
    437  1.141      matt 		count++;
    438  1.141      matt #endif
    439   1.54      matt 		/*
    440   1.54      matt 		 * If the received segment is just right after this
    441   1.54      matt 		 * fragment, merge the two together and then check
    442   1.54      matt 		 * for further overlaps.
    443   1.54      matt 		 */
    444   1.54      matt 		if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
    445   1.54      matt #ifdef TCPREASS_DEBUG
    446   1.54      matt 			printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n",
    447   1.54      matt 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    448   1.54      matt 			       q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len);
    449   1.54      matt #endif
    450   1.54      matt 			pkt_len += q->ipqe_len;
    451   1.54      matt 			pkt_flags |= q->ipqe_flags;
    452   1.54      matt 			pkt_seq = q->ipqe_seq;
    453   1.54      matt 			m_cat(q->ipqe_m, m);
    454   1.54      matt 			m = q->ipqe_m;
    455  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_append);
    456   1.54      matt 			goto free_ipqe;
    457   1.54      matt 		}
    458   1.54      matt 		/*
    459   1.54      matt 		 * If the received segment is completely past this
    460   1.54      matt 		 * fragment, we need to go the next fragment.
    461   1.54      matt 		 */
    462   1.54      matt 		if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
    463   1.54      matt 			p = q;
    464   1.54      matt 			continue;
    465   1.54      matt 		}
    466   1.54      matt 		/*
    467  1.143    itojun 		 * If the fragment is past the received segment,
    468   1.54      matt 		 * it (or any following) can't be concatenated.
    469   1.54      matt 		 */
    470  1.141      matt 		if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) {
    471  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_insert);
    472    1.1       cgd 			break;
    473  1.141      matt 		}
    474  1.141      matt 
    475   1.54      matt 		/*
    476   1.54      matt 		 * We've received all the data in this segment before.
    477   1.54      matt 		 * mark it as a duplicate and return.
    478   1.54      matt 		 */
    479   1.54      matt 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
    480   1.54      matt 		    SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    481   1.54      matt 			tcpstat.tcps_rcvduppack++;
    482   1.54      matt 			tcpstat.tcps_rcvdupbyte += pkt_len;
    483   1.54      matt 			m_freem(m);
    484   1.54      matt 			if (tiqe != NULL)
    485   1.71   thorpej 				pool_put(&ipqent_pool, tiqe);
    486  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_segdup);
    487   1.54      matt 			return (0);
    488   1.54      matt 		}
    489   1.54      matt 		/*
    490   1.54      matt 		 * Received segment completely overlaps this fragment
    491   1.54      matt 		 * so we drop the fragment (this keeps the temporal
    492   1.54      matt 		 * ordering of segments correct).
    493   1.54      matt 		 */
    494   1.54      matt 		if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
    495   1.54      matt 		    SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    496   1.54      matt 			rcvpartdupbyte += q->ipqe_len;
    497   1.54      matt 			m_freem(q->ipqe_m);
    498  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup);
    499   1.54      matt 			goto free_ipqe;
    500   1.54      matt 		}
    501   1.54      matt 		/*
    502   1.54      matt 		 * RX'ed segment extends past the end of the
    503   1.54      matt 		 * fragment.  Drop the overlapping bytes.  Then
    504   1.54      matt 		 * merge the fragment and segment then treat as
    505   1.54      matt 		 * a longer received packet.
    506   1.54      matt 		 */
    507   1.54      matt 		if (SEQ_LT(q->ipqe_seq, pkt_seq)
    508   1.54      matt 		    && SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq))  {
    509   1.54      matt 			int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
    510   1.54      matt #ifdef TCPREASS_DEBUG
    511   1.54      matt 			printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n",
    512   1.54      matt 			       tp, overlap,
    513   1.54      matt 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    514   1.54      matt #endif
    515   1.54      matt 			m_adj(m, overlap);
    516   1.54      matt 			rcvpartdupbyte += overlap;
    517   1.54      matt 			m_cat(q->ipqe_m, m);
    518   1.54      matt 			m = q->ipqe_m;
    519   1.54      matt 			pkt_seq = q->ipqe_seq;
    520   1.54      matt 			pkt_len += q->ipqe_len - overlap;
    521   1.54      matt 			rcvoobyte -= overlap;
    522  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail);
    523   1.54      matt 			goto free_ipqe;
    524   1.54      matt 		}
    525   1.54      matt 		/*
    526   1.54      matt 		 * RX'ed segment extends past the front of the
    527   1.54      matt 		 * fragment.  Drop the overlapping bytes on the
    528   1.54      matt 		 * received packet.  The packet will then be
    529   1.54      matt 		 * contatentated with this fragment a bit later.
    530   1.54      matt 		 */
    531   1.54      matt 		if (SEQ_GT(q->ipqe_seq, pkt_seq)
    532   1.54      matt 		    && SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len))  {
    533   1.54      matt 			int overlap = pkt_seq + pkt_len - q->ipqe_seq;
    534   1.54      matt #ifdef TCPREASS_DEBUG
    535   1.54      matt 			printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n",
    536   1.54      matt 			       tp, overlap,
    537   1.54      matt 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    538   1.54      matt #endif
    539   1.54      matt 			m_adj(m, -overlap);
    540   1.54      matt 			pkt_len -= overlap;
    541   1.54      matt 			rcvpartdupbyte += overlap;
    542  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront);
    543   1.54      matt 			rcvoobyte -= overlap;
    544   1.54      matt 		}
    545   1.54      matt 		/*
    546   1.54      matt 		 * If the received segment immediates precedes this
    547   1.54      matt 		 * fragment then tack the fragment onto this segment
    548   1.54      matt 		 * and reinsert the data.
    549   1.54      matt 		 */
    550   1.54      matt 		if (q->ipqe_seq == pkt_seq + pkt_len) {
    551   1.54      matt #ifdef TCPREASS_DEBUG
    552   1.54      matt 			printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n",
    553   1.54      matt 			       tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len,
    554   1.54      matt 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    555   1.54      matt #endif
    556   1.54      matt 			pkt_len += q->ipqe_len;
    557   1.54      matt 			pkt_flags |= q->ipqe_flags;
    558   1.54      matt 			m_cat(m, q->ipqe_m);
    559  1.141      matt 			TAILQ_REMOVE(&tp->segq, q, ipqe_q);
    560  1.141      matt 			TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    561   1.54      matt 			if (tiqe == NULL) {
    562   1.54      matt 			    tiqe = q;
    563   1.54      matt 			} else {
    564   1.71   thorpej 			    pool_put(&ipqent_pool, q);
    565    1.1       cgd 			}
    566  1.141      matt 			TCP_REASS_COUNTER_INCR(&tcp_reass_prepend);
    567   1.54      matt 			break;
    568   1.54      matt 		}
    569   1.54      matt 		/*
    570   1.54      matt 		 * If the fragment is before the segment, remember it.
    571   1.54      matt 		 * When this loop is terminated, p will contain the
    572   1.54      matt 		 * pointer to fragment that is right before the received
    573   1.54      matt 		 * segment.
    574   1.54      matt 		 */
    575   1.54      matt 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
    576   1.54      matt 			p = q;
    577   1.54      matt 
    578   1.54      matt 		continue;
    579   1.54      matt 
    580   1.54      matt 		/*
    581   1.54      matt 		 * This is a common operation.  It also will allow
    582   1.54      matt 		 * to save doing a malloc/free in most instances.
    583   1.54      matt 		 */
    584   1.54      matt 	  free_ipqe:
    585  1.141      matt 		TAILQ_REMOVE(&tp->segq, q, ipqe_q);
    586  1.141      matt 		TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    587   1.54      matt 		if (tiqe == NULL) {
    588   1.54      matt 		    tiqe = q;
    589   1.54      matt 		} else {
    590   1.71   thorpej 		    pool_put(&ipqent_pool, q);
    591    1.1       cgd 		}
    592    1.1       cgd 	}
    593    1.1       cgd 
    594  1.141      matt #ifdef TCP_REASS_COUNTERS
    595  1.141      matt 	if (count > 7)
    596  1.141      matt 		TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]);
    597  1.141      matt 	else if (count > 0)
    598  1.141      matt 		TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]);
    599  1.141      matt #endif
    600  1.141      matt 
    601  1.141      matt     insert_it:
    602  1.141      matt 
    603    1.1       cgd 	/*
    604   1.54      matt 	 * Allocate a new queue entry since the received segment did not
    605   1.54      matt 	 * collapse onto any other out-of-order block; thus we are allocating
    606   1.54      matt 	 * a new block.  If it had collapsed, tiqe would not be NULL and
    607   1.54      matt 	 * we would be reusing it.
    608   1.54      matt 	 * XXX If we can't, just drop the packet.  XXX
    609    1.1       cgd 	 */
    610   1.54      matt 	if (tiqe == NULL) {
    611   1.71   thorpej 		tiqe = pool_get(&ipqent_pool, PR_NOWAIT);
    612   1.54      matt 		if (tiqe == NULL) {
    613   1.54      matt 			tcpstat.tcps_rcvmemdrop++;
    614   1.54      matt 			m_freem(m);
    615   1.54      matt 			return (0);
    616   1.54      matt 		}
    617   1.54      matt 	}
    618   1.20       cgd 
    619   1.54      matt 	/*
    620   1.54      matt 	 * Update the counters.
    621   1.54      matt 	 */
    622   1.54      matt 	tcpstat.tcps_rcvoopack++;
    623   1.54      matt 	tcpstat.tcps_rcvoobyte += rcvoobyte;
    624   1.54      matt 	if (rcvpartdupbyte) {
    625   1.54      matt 	    tcpstat.tcps_rcvpartduppack++;
    626   1.54      matt 	    tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte;
    627    1.1       cgd 	}
    628    1.1       cgd 
    629   1.54      matt 	/*
    630   1.54      matt 	 * Insert the new fragment queue entry into both queues.
    631   1.54      matt 	 */
    632   1.20       cgd 	tiqe->ipqe_m = m;
    633   1.54      matt 	tiqe->ipqe_seq = pkt_seq;
    634   1.54      matt 	tiqe->ipqe_len = pkt_len;
    635   1.54      matt 	tiqe->ipqe_flags = pkt_flags;
    636   1.20       cgd 	if (p == NULL) {
    637  1.141      matt 		TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
    638   1.54      matt #ifdef TCPREASS_DEBUG
    639   1.54      matt 		if (tiqe->ipqe_seq != tp->rcv_nxt)
    640   1.54      matt 			printf("tcp_reass[%p]: insert %u:%u(%u) at front\n",
    641   1.54      matt 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len);
    642   1.54      matt #endif
    643   1.20       cgd 	} else {
    644  1.141      matt 		TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q);
    645   1.54      matt #ifdef TCPREASS_DEBUG
    646   1.54      matt 		printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n",
    647   1.54      matt 		       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    648   1.54      matt 		       p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len);
    649   1.54      matt #endif
    650   1.20       cgd 	}
    651    1.1       cgd 
    652  1.141      matt skip_replacement:
    653  1.141      matt 
    654  1.141      matt 	TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
    655   1.54      matt 
    656    1.1       cgd present:
    657    1.1       cgd 	/*
    658    1.1       cgd 	 * Present data to user, advancing rcv_nxt through
    659    1.1       cgd 	 * completed sequence space.
    660    1.1       cgd 	 */
    661   1.11   mycroft 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    662    1.1       cgd 		return (0);
    663  1.141      matt 	q = TAILQ_FIRST(&tp->segq);
    664   1.54      matt 	if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
    665    1.1       cgd 		return (0);
    666   1.54      matt 	if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
    667    1.1       cgd 		return (0);
    668   1.20       cgd 
    669   1.54      matt 	tp->rcv_nxt += q->ipqe_len;
    670   1.54      matt 	pkt_flags = q->ipqe_flags & TH_FIN;
    671   1.98    itojun 	ND6_HINT(tp);
    672   1.54      matt 
    673  1.141      matt 	TAILQ_REMOVE(&tp->segq, q, ipqe_q);
    674  1.141      matt 	TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    675   1.54      matt 	if (so->so_state & SS_CANTRCVMORE)
    676   1.54      matt 		m_freem(q->ipqe_m);
    677   1.54      matt 	else
    678  1.148   thorpej 		sbappendstream(&so->so_rcv, q->ipqe_m);
    679   1.71   thorpej 	pool_put(&ipqent_pool, q);
    680    1.1       cgd 	sorwakeup(so);
    681   1.54      matt 	return (pkt_flags);
    682    1.1       cgd }
    683    1.1       cgd 
    684  1.120    itojun #ifdef INET6
    685   1.83    itojun int
    686   1.83    itojun tcp6_input(mp, offp, proto)
    687   1.83    itojun 	struct mbuf **mp;
    688   1.83    itojun 	int *offp, proto;
    689   1.83    itojun {
    690   1.83    itojun 	struct mbuf *m = *mp;
    691   1.83    itojun 
    692   1.83    itojun 	/*
    693   1.83    itojun 	 * draft-itojun-ipv6-tcp-to-anycast
    694   1.83    itojun 	 * better place to put this in?
    695   1.83    itojun 	 */
    696   1.83    itojun 	if (m->m_flags & M_ANYCAST6) {
    697   1.98    itojun 		struct ip6_hdr *ip6;
    698   1.98    itojun 		if (m->m_len < sizeof(struct ip6_hdr)) {
    699   1.98    itojun 			if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    700   1.98    itojun 				tcpstat.tcps_rcvshort++;
    701   1.98    itojun 				return IPPROTO_DONE;
    702   1.98    itojun 			}
    703   1.98    itojun 		}
    704   1.98    itojun 		ip6 = mtod(m, struct ip6_hdr *);
    705  1.124    itojun 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
    706  1.124    itojun 		    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
    707   1.83    itojun 		return IPPROTO_DONE;
    708   1.83    itojun 	}
    709   1.83    itojun 
    710   1.83    itojun 	tcp_input(m, *offp, proto);
    711   1.83    itojun 	return IPPROTO_DONE;
    712   1.83    itojun }
    713   1.83    itojun #endif
    714   1.83    itojun 
    715  1.145      yamt #ifdef INET
    716  1.145      yamt static void
    717  1.145      yamt tcp4_log_refused(ip, th)
    718  1.145      yamt 	const struct ip *ip;
    719  1.145      yamt 	const struct tcphdr *th;
    720  1.145      yamt {
    721  1.145      yamt 	char src[4*sizeof "123"];
    722  1.145      yamt 	char dst[4*sizeof "123"];
    723  1.145      yamt 
    724  1.145      yamt 	if (ip) {
    725  1.145      yamt 		strcpy(src, inet_ntoa(ip->ip_src));
    726  1.145      yamt 		strcpy(dst, inet_ntoa(ip->ip_dst));
    727  1.145      yamt 	}
    728  1.145      yamt 	else {
    729  1.145      yamt 		strcpy(src, "(unknown)");
    730  1.145      yamt 		strcpy(dst, "(unknown)");
    731  1.145      yamt 	}
    732  1.145      yamt 	log(LOG_INFO,
    733  1.145      yamt 	    "Connection attempt to TCP %s:%d from %s:%d\n",
    734  1.145      yamt 	    dst, ntohs(th->th_dport),
    735  1.145      yamt 	    src, ntohs(th->th_sport));
    736  1.145      yamt }
    737  1.145      yamt #endif
    738  1.145      yamt 
    739  1.145      yamt #ifdef INET6
    740  1.145      yamt static void
    741  1.145      yamt tcp6_log_refused(ip6, th)
    742  1.145      yamt 	const struct ip6_hdr *ip6;
    743  1.145      yamt 	const struct tcphdr *th;
    744  1.145      yamt {
    745  1.145      yamt 	char src[INET6_ADDRSTRLEN];
    746  1.145      yamt 	char dst[INET6_ADDRSTRLEN];
    747  1.145      yamt 
    748  1.145      yamt 	if (ip6) {
    749  1.145      yamt 		strcpy(src, ip6_sprintf(&ip6->ip6_src));
    750  1.145      yamt 		strcpy(dst, ip6_sprintf(&ip6->ip6_dst));
    751  1.145      yamt 	}
    752  1.145      yamt 	else {
    753  1.145      yamt 		strcpy(src, "(unknown v6)");
    754  1.145      yamt 		strcpy(dst, "(unknown v6)");
    755  1.145      yamt 	}
    756  1.145      yamt 	log(LOG_INFO,
    757  1.145      yamt 	    "Connection attempt to TCP [%s]:%d from [%s]:%d\n",
    758  1.145      yamt 	    dst, ntohs(th->th_dport),
    759  1.145      yamt 	    src, ntohs(th->th_sport));
    760  1.145      yamt }
    761  1.145      yamt #endif
    762  1.145      yamt 
    763    1.1       cgd /*
    764    1.1       cgd  * TCP input routine, follows pages 65-76 of the
    765    1.1       cgd  * protocol specification dated September, 1981 very closely.
    766    1.1       cgd  */
    767    1.5   mycroft void
    768   1.23  christos #if __STDC__
    769   1.23  christos tcp_input(struct mbuf *m, ...)
    770   1.23  christos #else
    771   1.23  christos tcp_input(m, va_alist)
    772  1.106  augustss 	struct mbuf *m;
    773   1.23  christos #endif
    774    1.1       cgd {
    775   1.83    itojun 	int proto;
    776  1.106  augustss 	struct tcphdr *th;
    777   1.83    itojun 	struct ip *ip;
    778  1.106  augustss 	struct inpcb *inp;
    779   1.83    itojun #ifdef INET6
    780   1.83    itojun 	struct ip6_hdr *ip6;
    781  1.106  augustss 	struct in6pcb *in6p;
    782   1.83    itojun #endif
    783    1.9   mycroft 	caddr_t optp = NULL;
    784   1.23  christos 	int optlen = 0;
    785   1.83    itojun 	int len, tlen, toff, hdroptlen = 0;
    786  1.106  augustss 	struct tcpcb *tp = 0;
    787  1.106  augustss 	int tiflags;
    788   1.23  christos 	struct socket *so = NULL;
    789    1.1       cgd 	int todrop, acked, ourfinisacked, needoutput = 0;
    790   1.23  christos 	short ostate = 0;
    791    1.1       cgd 	int iss = 0;
    792   1.12       cgd 	u_long tiwin;
    793   1.29   thorpej 	struct tcp_opt_info opti;
    794   1.83    itojun 	int off, iphlen;
    795   1.23  christos 	va_list ap;
    796   1.83    itojun 	int af;		/* af on the wire */
    797   1.83    itojun 	struct mbuf *tcp_saveti = NULL;
    798   1.23  christos 
    799   1.23  christos 	va_start(ap, m);
    800   1.83    itojun 	toff = va_arg(ap, int);
    801   1.83    itojun 	proto = va_arg(ap, int);
    802   1.23  christos 	va_end(ap);
    803    1.1       cgd 
    804    1.1       cgd 	tcpstat.tcps_rcvtotal++;
    805   1.29   thorpej 
    806   1.83    itojun 	bzero(&opti, sizeof(opti));
    807   1.29   thorpej 	opti.ts_present = 0;
    808   1.29   thorpej 	opti.maxseg = 0;
    809   1.29   thorpej 
    810    1.1       cgd 	/*
    811  1.103   thorpej 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN.
    812  1.103   thorpej 	 *
    813  1.103   thorpej 	 * TCP is, by definition, unicast, so we reject all
    814  1.103   thorpej 	 * multicast outright.
    815  1.103   thorpej 	 *
    816  1.103   thorpej 	 * Note, there are additional src/dst address checks in
    817  1.103   thorpej 	 * the AF-specific code below.
    818  1.103   thorpej 	 */
    819  1.103   thorpej 	if (m->m_flags & (M_BCAST|M_MCAST)) {
    820  1.103   thorpej 		/* XXX stat */
    821  1.103   thorpej 		goto drop;
    822  1.103   thorpej 	}
    823  1.103   thorpej #ifdef INET6
    824  1.103   thorpej 	if (m->m_flags & M_ANYCAST6) {
    825  1.103   thorpej 		/* XXX stat */
    826  1.103   thorpej 		goto drop;
    827  1.103   thorpej 	}
    828  1.103   thorpej #endif
    829  1.103   thorpej 
    830  1.103   thorpej 	/*
    831    1.1       cgd 	 * Get IP and TCP header together in first mbuf.
    832    1.1       cgd 	 * Note: IP leaves IP header in first mbuf.
    833    1.1       cgd 	 */
    834   1.83    itojun 	ip = mtod(m, struct ip *);
    835   1.83    itojun #ifdef INET6
    836   1.83    itojun 	ip6 = NULL;
    837   1.83    itojun #endif
    838   1.83    itojun 	switch (ip->ip_v) {
    839  1.119    itojun #ifdef INET
    840   1.83    itojun 	case 4:
    841   1.83    itojun 		af = AF_INET;
    842   1.83    itojun 		iphlen = sizeof(struct ip);
    843   1.99    itojun #ifndef PULLDOWN_TEST
    844   1.83    itojun 		/* would like to get rid of this... */
    845   1.83    itojun 		if (toff > sizeof (struct ip)) {
    846   1.83    itojun 			ip_stripoptions(m, (struct mbuf *)0);
    847   1.83    itojun 			toff = sizeof(struct ip);
    848   1.83    itojun 		}
    849   1.83    itojun 		if (m->m_len < toff + sizeof (struct tcphdr)) {
    850   1.83    itojun 			if ((m = m_pullup(m, toff + sizeof (struct tcphdr))) == 0) {
    851   1.83    itojun 				tcpstat.tcps_rcvshort++;
    852   1.83    itojun 				return;
    853   1.83    itojun 			}
    854   1.83    itojun 		}
    855   1.83    itojun 		ip = mtod(m, struct ip *);
    856   1.83    itojun 		th = (struct tcphdr *)(mtod(m, caddr_t) + toff);
    857   1.99    itojun #else
    858   1.99    itojun 		ip = mtod(m, struct ip *);
    859   1.99    itojun 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
    860   1.99    itojun 			sizeof(struct tcphdr));
    861   1.99    itojun 		if (th == NULL) {
    862   1.99    itojun 			tcpstat.tcps_rcvshort++;
    863   1.99    itojun 			return;
    864   1.99    itojun 		}
    865   1.99    itojun #endif
    866  1.103   thorpej 		/* We do the checksum after PCB lookup... */
    867  1.150    itojun 		len = ntohs(ip->ip_len);
    868   1.83    itojun 		tlen = len - toff;
    869   1.83    itojun 		break;
    870  1.119    itojun #endif
    871   1.83    itojun #ifdef INET6
    872   1.83    itojun 	case 6:
    873   1.83    itojun 		ip = NULL;
    874   1.83    itojun 		iphlen = sizeof(struct ip6_hdr);
    875   1.83    itojun 		af = AF_INET6;
    876   1.99    itojun #ifndef PULLDOWN_TEST
    877   1.83    itojun 		if (m->m_len < toff + sizeof(struct tcphdr)) {
    878   1.83    itojun 			m = m_pullup(m, toff + sizeof(struct tcphdr));	/*XXX*/
    879   1.83    itojun 			if (m == NULL) {
    880   1.83    itojun 				tcpstat.tcps_rcvshort++;
    881   1.83    itojun 				return;
    882   1.83    itojun 			}
    883    1.1       cgd 		}
    884   1.83    itojun 		ip6 = mtod(m, struct ip6_hdr *);
    885   1.83    itojun 		th = (struct tcphdr *)(mtod(m, caddr_t) + toff);
    886   1.99    itojun #else
    887   1.99    itojun 		ip6 = mtod(m, struct ip6_hdr *);
    888   1.99    itojun 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
    889   1.99    itojun 			sizeof(struct tcphdr));
    890   1.99    itojun 		if (th == NULL) {
    891   1.99    itojun 			tcpstat.tcps_rcvshort++;
    892   1.99    itojun 			return;
    893   1.99    itojun 		}
    894   1.99    itojun #endif
    895  1.101    itojun 
    896  1.101    itojun 		/* Be proactive about malicious use of IPv4 mapped address */
    897  1.101    itojun 		if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    898  1.101    itojun 		    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    899  1.115    itojun 			/* XXX stat */
    900  1.115    itojun 			goto drop;
    901  1.115    itojun 		}
    902  1.115    itojun 
    903  1.115    itojun 		/*
    904  1.115    itojun 		 * Be proactive about unspecified IPv6 address in source.
    905  1.115    itojun 		 * As we use all-zero to indicate unbounded/unconnected pcb,
    906  1.115    itojun 		 * unspecified IPv6 address can be used to confuse us.
    907  1.115    itojun 		 *
    908  1.115    itojun 		 * Note that packets with unspecified IPv6 destination is
    909  1.115    itojun 		 * already dropped in ip6_input.
    910  1.115    itojun 		 */
    911  1.115    itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
    912  1.101    itojun 			/* XXX stat */
    913  1.101    itojun 			goto drop;
    914  1.101    itojun 		}
    915    1.1       cgd 
    916   1.83    itojun 		/*
    917  1.103   thorpej 		 * Make sure destination address is not multicast.
    918  1.103   thorpej 		 * Source address checked in ip6_input().
    919   1.83    itojun 		 */
    920  1.103   thorpej 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    921  1.103   thorpej 			/* XXX stat */
    922  1.103   thorpej 			goto drop;
    923  1.103   thorpej 		}
    924  1.103   thorpej 
    925  1.103   thorpej 		/* We do the checksum after PCB lookup... */
    926   1.83    itojun 		len = m->m_pkthdr.len;
    927   1.83    itojun 		tlen = len - toff;
    928   1.83    itojun 		break;
    929   1.83    itojun #endif
    930   1.83    itojun 	default:
    931   1.83    itojun 		m_freem(m);
    932   1.83    itojun 		return;
    933    1.1       cgd 	}
    934    1.1       cgd 
    935  1.146   thorpej 	KASSERT(TCP_HDR_ALIGNED_P(th));
    936  1.146   thorpej 
    937    1.1       cgd 	/*
    938    1.1       cgd 	 * Check that TCP offset makes sense,
    939    1.1       cgd 	 * pull out TCP options and adjust length.		XXX
    940    1.1       cgd 	 */
    941   1.83    itojun 	off = th->th_off << 2;
    942    1.1       cgd 	if (off < sizeof (struct tcphdr) || off > tlen) {
    943    1.1       cgd 		tcpstat.tcps_rcvbadoff++;
    944    1.1       cgd 		goto drop;
    945    1.1       cgd 	}
    946    1.1       cgd 	tlen -= off;
    947   1.83    itojun 
    948  1.143    itojun 	/*
    949   1.83    itojun 	 * tcp_input() has been modified to use tlen to mean the TCP data
    950   1.83    itojun 	 * length throughout the function.  Other functions can use
    951   1.83    itojun 	 * m->m_pkthdr.len as the basis for calculating the TCP data length.
    952   1.83    itojun 	 * rja
    953   1.83    itojun 	 */
    954   1.83    itojun 
    955    1.1       cgd 	if (off > sizeof (struct tcphdr)) {
    956   1.99    itojun #ifndef PULLDOWN_TEST
    957   1.83    itojun 		if (m->m_len < toff + off) {
    958   1.83    itojun 			if ((m = m_pullup(m, toff + off)) == 0) {
    959    1.1       cgd 				tcpstat.tcps_rcvshort++;
    960    1.1       cgd 				return;
    961    1.1       cgd 			}
    962   1.83    itojun 			switch (af) {
    963  1.119    itojun #ifdef INET
    964   1.83    itojun 			case AF_INET:
    965   1.83    itojun 				ip = mtod(m, struct ip *);
    966   1.83    itojun 				break;
    967  1.119    itojun #endif
    968   1.83    itojun #ifdef INET6
    969   1.83    itojun 			case AF_INET6:
    970   1.83    itojun 				ip6 = mtod(m, struct ip6_hdr *);
    971   1.83    itojun 				break;
    972   1.83    itojun #endif
    973   1.83    itojun 			}
    974   1.83    itojun 			th = (struct tcphdr *)(mtod(m, caddr_t) + toff);
    975    1.1       cgd 		}
    976   1.99    itojun #else
    977   1.99    itojun 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, off);
    978   1.99    itojun 		if (th == NULL) {
    979   1.99    itojun 			tcpstat.tcps_rcvshort++;
    980   1.99    itojun 			return;
    981   1.99    itojun 		}
    982   1.99    itojun 		/*
    983   1.99    itojun 		 * NOTE: ip/ip6 will not be affected by m_pulldown()
    984   1.99    itojun 		 * (as they're before toff) and we don't need to update those.
    985   1.99    itojun 		 */
    986   1.99    itojun #endif
    987  1.146   thorpej 		KASSERT(TCP_HDR_ALIGNED_P(th));
    988    1.9   mycroft 		optlen = off - sizeof (struct tcphdr);
    989   1.99    itojun 		optp = ((caddr_t)th) + sizeof(struct tcphdr);
    990  1.143    itojun 		/*
    991    1.9   mycroft 		 * Do quick retrieval of timestamp options ("options
    992    1.9   mycroft 		 * prediction?").  If timestamp is the only option and it's
    993    1.9   mycroft 		 * formatted as recommended in RFC 1323 appendix A, we
    994    1.9   mycroft 		 * quickly get the values now and not bother calling
    995    1.9   mycroft 		 * tcp_dooptions(), etc.
    996    1.9   mycroft 		 */
    997    1.9   mycroft 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
    998    1.9   mycroft 		     (optlen > TCPOLEN_TSTAMP_APPA &&
    999    1.9   mycroft 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
   1000   1.12       cgd 		     *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
   1001   1.83    itojun 		     (th->th_flags & TH_SYN) == 0) {
   1002   1.29   thorpej 			opti.ts_present = 1;
   1003   1.29   thorpej 			opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
   1004   1.29   thorpej 			opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
   1005    1.9   mycroft 			optp = NULL;	/* we've parsed the options */
   1006    1.1       cgd 		}
   1007    1.1       cgd 	}
   1008   1.83    itojun 	tiflags = th->th_flags;
   1009    1.1       cgd 
   1010    1.1       cgd 	/*
   1011    1.1       cgd 	 * Locate pcb for segment.
   1012    1.1       cgd 	 */
   1013    1.1       cgd findpcb:
   1014   1.83    itojun 	inp = NULL;
   1015   1.83    itojun #ifdef INET6
   1016   1.83    itojun 	in6p = NULL;
   1017   1.83    itojun #endif
   1018   1.83    itojun 	switch (af) {
   1019  1.119    itojun #ifdef INET
   1020   1.83    itojun 	case AF_INET:
   1021   1.83    itojun 		inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport,
   1022   1.83    itojun 		    ip->ip_dst, th->th_dport);
   1023   1.83    itojun 		if (inp == 0) {
   1024   1.83    itojun 			++tcpstat.tcps_pcbhashmiss;
   1025   1.83    itojun 			inp = in_pcblookup_bind(&tcbtable, ip->ip_dst, th->th_dport);
   1026   1.83    itojun 		}
   1027  1.120    itojun #ifdef INET6
   1028   1.21   mycroft 		if (inp == 0) {
   1029   1.83    itojun 			struct in6_addr s, d;
   1030   1.83    itojun 
   1031   1.83    itojun 			/* mapped addr case */
   1032   1.83    itojun 			bzero(&s, sizeof(s));
   1033   1.83    itojun 			s.s6_addr16[5] = htons(0xffff);
   1034   1.83    itojun 			bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src));
   1035   1.83    itojun 			bzero(&d, sizeof(d));
   1036   1.83    itojun 			d.s6_addr16[5] = htons(0xffff);
   1037   1.83    itojun 			bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst));
   1038   1.83    itojun 			in6p = in6_pcblookup_connect(&tcb6, &s, th->th_sport,
   1039   1.87    itojun 				&d, th->th_dport, 0);
   1040   1.83    itojun 			if (in6p == 0) {
   1041   1.83    itojun 				++tcpstat.tcps_pcbhashmiss;
   1042   1.87    itojun 				in6p = in6_pcblookup_bind(&tcb6, &d,
   1043   1.87    itojun 					th->th_dport, 0);
   1044   1.83    itojun 			}
   1045   1.83    itojun 		}
   1046   1.83    itojun #endif
   1047   1.83    itojun #ifndef INET6
   1048   1.83    itojun 		if (inp == 0)
   1049   1.83    itojun #else
   1050   1.83    itojun 		if (inp == 0 && in6p == 0)
   1051   1.83    itojun #endif
   1052   1.83    itojun 		{
   1053   1.21   mycroft 			++tcpstat.tcps_noport;
   1054   1.82        ad 			if (tcp_log_refused && (tiflags & TH_SYN)) {
   1055  1.145      yamt 				tcp4_log_refused(ip, th);
   1056   1.82        ad 			}
   1057  1.103   thorpej 			TCP_FIELDS_TO_HOST(th);
   1058  1.104   thorpej 			goto dropwithreset_ratelim;
   1059   1.21   mycroft 		}
   1060   1.83    itojun #ifdef IPSEC
   1061   1.83    itojun 		if (inp && ipsec4_in_reject(m, inp)) {
   1062   1.83    itojun 			ipsecstat.in_polvio++;
   1063   1.83    itojun 			goto drop;
   1064   1.83    itojun 		}
   1065   1.83    itojun #ifdef INET6
   1066   1.83    itojun 		else if (in6p && ipsec4_in_reject_so(m, in6p->in6p_socket)) {
   1067   1.83    itojun 			ipsecstat.in_polvio++;
   1068   1.83    itojun 			goto drop;
   1069   1.83    itojun 		}
   1070   1.83    itojun #endif
   1071   1.83    itojun #endif /*IPSEC*/
   1072   1.83    itojun 		break;
   1073  1.119    itojun #endif /*INET*/
   1074  1.120    itojun #ifdef INET6
   1075   1.83    itojun 	case AF_INET6:
   1076   1.87    itojun 	    {
   1077   1.87    itojun 		int faith;
   1078   1.87    itojun 
   1079   1.87    itojun #if defined(NFAITH) && NFAITH > 0
   1080  1.124    itojun 		faith = faithprefix(&ip6->ip6_dst);
   1081   1.87    itojun #else
   1082   1.87    itojun 		faith = 0;
   1083   1.87    itojun #endif
   1084   1.83    itojun 		in6p = in6_pcblookup_connect(&tcb6, &ip6->ip6_src, th->th_sport,
   1085   1.87    itojun 			&ip6->ip6_dst, th->th_dport, faith);
   1086   1.83    itojun 		if (in6p == NULL) {
   1087   1.83    itojun 			++tcpstat.tcps_pcbhashmiss;
   1088   1.87    itojun 			in6p = in6_pcblookup_bind(&tcb6, &ip6->ip6_dst,
   1089   1.87    itojun 				th->th_dport, faith);
   1090   1.83    itojun 		}
   1091   1.83    itojun 		if (in6p == NULL) {
   1092   1.83    itojun 			++tcpstat.tcps_noport;
   1093  1.136    itojun 			if (tcp_log_refused && (tiflags & TH_SYN)) {
   1094  1.145      yamt 				tcp6_log_refused(ip6, th);
   1095  1.136    itojun 			}
   1096  1.103   thorpej 			TCP_FIELDS_TO_HOST(th);
   1097  1.104   thorpej 			goto dropwithreset_ratelim;
   1098   1.83    itojun 		}
   1099   1.83    itojun #ifdef IPSEC
   1100   1.83    itojun 		if (ipsec6_in_reject(m, in6p)) {
   1101   1.83    itojun 			ipsec6stat.in_polvio++;
   1102   1.83    itojun 			goto drop;
   1103   1.83    itojun 		}
   1104   1.83    itojun #endif /*IPSEC*/
   1105   1.83    itojun 		break;
   1106   1.87    itojun 	    }
   1107   1.83    itojun #endif
   1108    1.1       cgd 	}
   1109    1.1       cgd 
   1110   1.24   mycroft 	/*
   1111   1.24   mycroft 	 * If the state is CLOSED (i.e., TCB does not exist) then
   1112   1.24   mycroft 	 * all data in the incoming segment is discarded.
   1113   1.24   mycroft 	 * If the TCB exists but is in CLOSED state, it is embryonic,
   1114   1.24   mycroft 	 * but should either do a listen or a connect soon.
   1115   1.24   mycroft 	 */
   1116   1.83    itojun 	tp = NULL;
   1117   1.83    itojun 	so = NULL;
   1118   1.83    itojun 	if (inp) {
   1119   1.83    itojun 		tp = intotcpcb(inp);
   1120   1.83    itojun 		so = inp->inp_socket;
   1121   1.83    itojun 	}
   1122   1.83    itojun #ifdef INET6
   1123   1.83    itojun 	else if (in6p) {
   1124   1.83    itojun 		tp = in6totcpcb(in6p);
   1125   1.83    itojun 		so = in6p->in6p_socket;
   1126   1.83    itojun 	}
   1127   1.83    itojun #endif
   1128   1.83    itojun 	if (tp == 0) {
   1129  1.103   thorpej 		TCP_FIELDS_TO_HOST(th);
   1130  1.104   thorpej 		goto dropwithreset_ratelim;
   1131   1.83    itojun 	}
   1132    1.1       cgd 	if (tp->t_state == TCPS_CLOSED)
   1133    1.1       cgd 		goto drop;
   1134  1.103   thorpej 
   1135  1.103   thorpej 	/*
   1136  1.103   thorpej 	 * Checksum extended TCP header and data.
   1137  1.103   thorpej 	 */
   1138  1.103   thorpej 	switch (af) {
   1139  1.119    itojun #ifdef INET
   1140  1.103   thorpej 	case AF_INET:
   1141  1.125   thorpej 		switch (m->m_pkthdr.csum_flags &
   1142  1.131   thorpej 			((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_TCPv4) |
   1143  1.125   thorpej 			 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
   1144  1.125   thorpej 		case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD:
   1145  1.125   thorpej 			TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad);
   1146  1.125   thorpej 			goto badcsum;
   1147  1.125   thorpej 
   1148  1.125   thorpej 		case M_CSUM_TCPv4|M_CSUM_DATA:
   1149  1.125   thorpej 			TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data);
   1150  1.125   thorpej 			if ((m->m_pkthdr.csum_data ^ 0xffff) != 0)
   1151  1.125   thorpej 				goto badcsum;
   1152  1.125   thorpej 			break;
   1153  1.125   thorpej 
   1154  1.125   thorpej 		case M_CSUM_TCPv4:
   1155  1.125   thorpej 			/* Checksum was okay. */
   1156  1.125   thorpej 			TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok);
   1157  1.125   thorpej 			break;
   1158  1.125   thorpej 
   1159  1.125   thorpej 		default:
   1160  1.125   thorpej 			/* Must compute it ourselves. */
   1161  1.125   thorpej 			TCP_CSUM_COUNTER_INCR(&tcp_swcsum);
   1162  1.103   thorpej #ifndef PULLDOWN_TEST
   1163  1.125   thorpej 		    {
   1164  1.125   thorpej 			struct ipovly *ipov;
   1165  1.125   thorpej 			ipov = (struct ipovly *)ip;
   1166  1.143    itojun 			bzero(ipov->ih_x1, sizeof ipov->ih_x1);
   1167  1.125   thorpej 			ipov->ih_len = htons(tlen + off);
   1168  1.125   thorpej 
   1169  1.125   thorpej 			if (in_cksum(m, len) != 0)
   1170  1.125   thorpej 				goto badcsum;
   1171  1.125   thorpej 		    }
   1172  1.103   thorpej #else
   1173  1.125   thorpej 			if (in4_cksum(m, IPPROTO_TCP, toff, tlen + off) != 0)
   1174  1.125   thorpej 				goto badcsum;
   1175  1.125   thorpej #endif /* ! PULLDOWN_TEST */
   1176  1.125   thorpej 			break;
   1177  1.103   thorpej 		}
   1178  1.103   thorpej 		break;
   1179  1.125   thorpej #endif /* INET4 */
   1180  1.103   thorpej 
   1181  1.103   thorpej #ifdef INET6
   1182  1.103   thorpej 	case AF_INET6:
   1183  1.125   thorpej 		if (in6_cksum(m, IPPROTO_TCP, toff, tlen + off) != 0)
   1184  1.125   thorpej 			goto badcsum;
   1185  1.103   thorpej 		break;
   1186  1.125   thorpej #endif /* INET6 */
   1187  1.103   thorpej 	}
   1188  1.103   thorpej 
   1189  1.103   thorpej 	TCP_FIELDS_TO_HOST(th);
   1190  1.103   thorpej 
   1191    1.9   mycroft 	/* Unscale the window into a 32-bit value. */
   1192    1.9   mycroft 	if ((tiflags & TH_SYN) == 0)
   1193   1.83    itojun 		tiwin = th->th_win << tp->snd_scale;
   1194    1.9   mycroft 	else
   1195   1.83    itojun 		tiwin = th->th_win;
   1196   1.83    itojun 
   1197   1.83    itojun #ifdef INET6
   1198   1.83    itojun 	/* save packet options if user wanted */
   1199   1.83    itojun 	if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) {
   1200   1.83    itojun 		if (in6p->in6p_options) {
   1201   1.83    itojun 			m_freem(in6p->in6p_options);
   1202   1.83    itojun 			in6p->in6p_options = 0;
   1203   1.83    itojun 		}
   1204   1.83    itojun 		ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m);
   1205   1.83    itojun 	}
   1206   1.83    itojun #endif
   1207    1.9   mycroft 
   1208    1.1       cgd 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
   1209   1.83    itojun 		union syn_cache_sa src;
   1210   1.83    itojun 		union syn_cache_sa dst;
   1211   1.83    itojun 
   1212   1.83    itojun 		bzero(&src, sizeof(src));
   1213   1.83    itojun 		bzero(&dst, sizeof(dst));
   1214   1.83    itojun 		switch (af) {
   1215  1.119    itojun #ifdef INET
   1216   1.83    itojun 		case AF_INET:
   1217   1.83    itojun 			src.sin.sin_len = sizeof(struct sockaddr_in);
   1218   1.83    itojun 			src.sin.sin_family = AF_INET;
   1219   1.83    itojun 			src.sin.sin_addr = ip->ip_src;
   1220   1.83    itojun 			src.sin.sin_port = th->th_sport;
   1221   1.83    itojun 
   1222   1.83    itojun 			dst.sin.sin_len = sizeof(struct sockaddr_in);
   1223   1.83    itojun 			dst.sin.sin_family = AF_INET;
   1224   1.83    itojun 			dst.sin.sin_addr = ip->ip_dst;
   1225   1.83    itojun 			dst.sin.sin_port = th->th_dport;
   1226   1.83    itojun 			break;
   1227  1.119    itojun #endif
   1228   1.83    itojun #ifdef INET6
   1229   1.83    itojun 		case AF_INET6:
   1230   1.83    itojun 			src.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1231   1.83    itojun 			src.sin6.sin6_family = AF_INET6;
   1232   1.83    itojun 			src.sin6.sin6_addr = ip6->ip6_src;
   1233   1.83    itojun 			src.sin6.sin6_port = th->th_sport;
   1234   1.83    itojun 
   1235   1.83    itojun 			dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1236   1.83    itojun 			dst.sin6.sin6_family = AF_INET6;
   1237   1.83    itojun 			dst.sin6.sin6_addr = ip6->ip6_dst;
   1238   1.83    itojun 			dst.sin6.sin6_port = th->th_dport;
   1239   1.83    itojun 			break;
   1240   1.83    itojun #endif /* INET6 */
   1241   1.83    itojun 		default:
   1242   1.83    itojun 			goto badsyn;	/*sanity*/
   1243   1.83    itojun 		}
   1244   1.83    itojun 
   1245    1.1       cgd 		if (so->so_options & SO_DEBUG) {
   1246    1.1       cgd 			ostate = tp->t_state;
   1247  1.109    itojun 
   1248  1.109    itojun 			tcp_saveti = NULL;
   1249  1.109    itojun 			if (iphlen + sizeof(struct tcphdr) > MHLEN)
   1250  1.109    itojun 				goto nosave;
   1251  1.109    itojun 
   1252  1.109    itojun 			if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) {
   1253  1.109    itojun 				tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT);
   1254  1.109    itojun 				if (!tcp_saveti)
   1255  1.109    itojun 					goto nosave;
   1256  1.109    itojun 			} else {
   1257  1.109    itojun 				MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER);
   1258  1.109    itojun 				if (!tcp_saveti)
   1259  1.109    itojun 					goto nosave;
   1260  1.109    itojun 				tcp_saveti->m_len = iphlen;
   1261  1.109    itojun 				m_copydata(m, 0, iphlen,
   1262  1.109    itojun 				    mtod(tcp_saveti, caddr_t));
   1263  1.109    itojun 			}
   1264  1.109    itojun 
   1265   1.83    itojun 			if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) {
   1266   1.83    itojun 				m_freem(tcp_saveti);
   1267   1.83    itojun 				tcp_saveti = NULL;
   1268   1.83    itojun 			} else {
   1269   1.83    itojun 				tcp_saveti->m_len += sizeof(struct tcphdr);
   1270   1.83    itojun 				bcopy(th, mtod(tcp_saveti, caddr_t) + iphlen,
   1271  1.109    itojun 				    sizeof(struct tcphdr));
   1272   1.83    itojun 			}
   1273   1.83    itojun 			if (tcp_saveti) {
   1274   1.83    itojun 				/*
   1275   1.83    itojun 				 * need to recover version # field, which was
   1276   1.83    itojun 				 * overwritten on ip_cksum computation.
   1277   1.83    itojun 				 */
   1278   1.83    itojun 				struct ip *sip;
   1279   1.83    itojun 				sip = mtod(tcp_saveti, struct ip *);
   1280   1.83    itojun 				switch (af) {
   1281  1.119    itojun #ifdef INET
   1282   1.83    itojun 				case AF_INET:
   1283   1.83    itojun 					sip->ip_v = 4;
   1284   1.83    itojun 					break;
   1285  1.119    itojun #endif
   1286   1.83    itojun #ifdef INET6
   1287   1.83    itojun 				case AF_INET6:
   1288   1.83    itojun 					sip->ip_v = 6;
   1289   1.83    itojun 					break;
   1290   1.83    itojun #endif
   1291   1.83    itojun 				}
   1292   1.83    itojun 			}
   1293  1.109    itojun 	nosave:;
   1294    1.1       cgd 		}
   1295    1.1       cgd 		if (so->so_options & SO_ACCEPTCONN) {
   1296  1.143    itojun 			if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
   1297   1.35   thorpej 				if (tiflags & TH_RST) {
   1298   1.83    itojun 					syn_cache_reset(&src.sa, &dst.sa, th);
   1299   1.35   thorpej 				} else if ((tiflags & (TH_ACK|TH_SYN)) ==
   1300   1.35   thorpej 				    (TH_ACK|TH_SYN)) {
   1301   1.35   thorpej 					/*
   1302   1.35   thorpej 					 * Received a SYN,ACK.  This should
   1303   1.35   thorpej 					 * never happen while we are in
   1304   1.35   thorpej 					 * LISTEN.  Send an RST.
   1305   1.35   thorpej 					 */
   1306   1.35   thorpej 					goto badsyn;
   1307   1.35   thorpej 				} else if (tiflags & TH_ACK) {
   1308   1.83    itojun 					so = syn_cache_get(&src.sa, &dst.sa,
   1309   1.83    itojun 						th, toff, tlen, so, m);
   1310   1.29   thorpej 					if (so == NULL) {
   1311   1.29   thorpej 						/*
   1312   1.29   thorpej 						 * We don't have a SYN for
   1313   1.29   thorpej 						 * this ACK; send an RST.
   1314   1.29   thorpej 						 */
   1315   1.35   thorpej 						goto badsyn;
   1316   1.29   thorpej 					} else if (so ==
   1317   1.29   thorpej 					    (struct socket *)(-1)) {
   1318   1.29   thorpej 						/*
   1319   1.29   thorpej 						 * We were unable to create
   1320   1.29   thorpej 						 * the connection.  If the
   1321   1.29   thorpej 						 * 3-way handshake was
   1322   1.67   mycroft 						 * completed, and RST has
   1323   1.29   thorpej 						 * been sent to the peer.
   1324   1.29   thorpej 						 * Since the mbuf might be
   1325   1.29   thorpej 						 * in use for the reply,
   1326   1.29   thorpej 						 * do not free it.
   1327   1.29   thorpej 						 */
   1328   1.29   thorpej 						m = NULL;
   1329   1.29   thorpej 					} else {
   1330   1.29   thorpej 						/*
   1331   1.29   thorpej 						 * We have created a
   1332   1.29   thorpej 						 * full-blown connection.
   1333   1.29   thorpej 						 */
   1334   1.83    itojun 						tp = NULL;
   1335   1.83    itojun 						inp = NULL;
   1336   1.83    itojun #ifdef INET6
   1337   1.83    itojun 						in6p = NULL;
   1338   1.83    itojun #endif
   1339   1.83    itojun 						switch (so->so_proto->pr_domain->dom_family) {
   1340  1.119    itojun #ifdef INET
   1341   1.83    itojun 						case AF_INET:
   1342   1.83    itojun 							inp = sotoinpcb(so);
   1343   1.83    itojun 							tp = intotcpcb(inp);
   1344   1.83    itojun 							break;
   1345  1.119    itojun #endif
   1346   1.83    itojun #ifdef INET6
   1347   1.83    itojun 						case AF_INET6:
   1348   1.83    itojun 							in6p = sotoin6pcb(so);
   1349   1.83    itojun 							tp = in6totcpcb(in6p);
   1350   1.83    itojun 							break;
   1351   1.83    itojun #endif
   1352   1.83    itojun 						}
   1353   1.83    itojun 						if (tp == NULL)
   1354   1.83    itojun 							goto badsyn;	/*XXX*/
   1355   1.29   thorpej 						tiwin <<= tp->snd_scale;
   1356   1.29   thorpej 						goto after_listen;
   1357   1.29   thorpej 					}
   1358  1.143    itojun 				} else {
   1359   1.66   mycroft 					/*
   1360   1.66   mycroft 					 * None of RST, SYN or ACK was set.
   1361   1.66   mycroft 					 * This is an invalid packet for a
   1362   1.66   mycroft 					 * TCB in LISTEN state.  Send a RST.
   1363   1.66   mycroft 					 */
   1364   1.66   mycroft 					goto badsyn;
   1365   1.66   mycroft 				}
   1366  1.143    itojun 			} else {
   1367   1.29   thorpej 				/*
   1368   1.35   thorpej 				 * Received a SYN.
   1369   1.35   thorpej 				 */
   1370   1.83    itojun 
   1371   1.83    itojun #ifdef INET6
   1372  1.151    itojun 				/*
   1373  1.151    itojun 				 * If deprecated address is forbidden, we do
   1374  1.151    itojun 				 * not accept SYN to deprecated interface
   1375  1.151    itojun 				 * address to prevent any new inbound
   1376  1.151    itojun 				 * connection from getting established.
   1377  1.152    itojun 				 * When we do not accept SYN, we send a TCP
   1378  1.152    itojun 				 * RST, with deprecated source address (instead
   1379  1.151    itojun 				 * of dropping it).  We compromise it as it is
   1380  1.152    itojun 				 * much better for peer to send a RST, and
   1381  1.152    itojun 				 * RST will be the final packet for the
   1382  1.152    itojun 				 * exchange.
   1383  1.151    itojun 				 *
   1384  1.152    itojun 				 * If we do not forbid deprecated addresses, we
   1385  1.152    itojun 				 * accept the SYN packet.  RFC2462 does not
   1386  1.152    itojun 				 * suggest dropping SYN in this case.
   1387  1.152    itojun 				 * If we decipher RFC2462 5.5.4, it says like
   1388  1.152    itojun 				 * this:
   1389  1.151    itojun 				 * 1. use of deprecated addr with existing
   1390  1.151    itojun 				 *    communication is okay - "SHOULD continue
   1391  1.151    itojun 				 *    to be used"
   1392  1.151    itojun 				 * 2. use of it with new communication:
   1393  1.151    itojun 				 *   (2a) "SHOULD NOT be used if alternate
   1394  1.151    itojun 				 *        address with sufficient scope is
   1395  1.151    itojun 				 *        available"
   1396  1.151    itojun 				 *   (2b) nothing mentioned otherwise.
   1397  1.151    itojun 				 * Here we fall into (2b) case as we have no
   1398  1.152    itojun 				 * choice in our source address selection - we
   1399  1.152    itojun 				 * must obey the peer.
   1400  1.151    itojun 				 *
   1401  1.151    itojun 				 * The wording in RFC2462 is confusing, and
   1402  1.151    itojun 				 * there are multiple description text for
   1403  1.151    itojun 				 * deprecated address handling - worse, they
   1404  1.152    itojun 				 * are not exactly the same.  I believe 5.5.4
   1405  1.152    itojun 				 * is the best one, so we follow 5.5.4.
   1406  1.151    itojun 				 */
   1407  1.152    itojun 				if (af == AF_INET6 && !ip6_use_deprecated) {
   1408  1.151    itojun 					struct in6_ifaddr *ia6;
   1409  1.151    itojun 					if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif,
   1410  1.151    itojun 					    &ip6->ip6_dst)) &&
   1411  1.151    itojun 					    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
   1412  1.151    itojun 						tp = NULL;
   1413  1.151    itojun 						goto dropwithreset;
   1414  1.152    itojun 					}
   1415  1.152    itojun 				}
   1416  1.152    itojun #endif
   1417  1.152    itojun 
   1418  1.152    itojun 				/*
   1419  1.152    itojun 				 * LISTEN socket received a SYN
   1420  1.152    itojun 				 * from itself?  This can't possibly
   1421  1.152    itojun 				 * be valid; drop the packet.
   1422  1.152    itojun 				 */
   1423  1.152    itojun 				if (th->th_sport == th->th_dport) {
   1424  1.152    itojun 					int i;
   1425  1.152    itojun 
   1426  1.152    itojun 					switch (af) {
   1427  1.152    itojun #ifdef INET
   1428  1.152    itojun 					case AF_INET:
   1429  1.152    itojun 						i = in_hosteq(ip->ip_src, ip->ip_dst);
   1430  1.152    itojun 						break;
   1431  1.152    itojun #endif
   1432  1.152    itojun #ifdef INET6
   1433  1.152    itojun 					case AF_INET6:
   1434  1.152    itojun 						i = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst);
   1435  1.152    itojun 						break;
   1436  1.152    itojun #endif
   1437  1.152    itojun 					default:
   1438  1.152    itojun 						i = 1;
   1439  1.152    itojun 					}
   1440  1.152    itojun 					if (i) {
   1441  1.152    itojun 						tcpstat.tcps_badsyn++;
   1442  1.152    itojun 						goto drop;
   1443   1.83    itojun 					}
   1444   1.35   thorpej 				}
   1445   1.83    itojun 
   1446   1.35   thorpej 				/*
   1447   1.35   thorpej 				 * SYN looks ok; create compressed TCP
   1448   1.35   thorpej 				 * state for it.
   1449   1.29   thorpej 				 */
   1450   1.29   thorpej 				if (so->so_qlen <= so->so_qlimit &&
   1451   1.83    itojun 				    syn_cache_add(&src.sa, &dst.sa, th, tlen,
   1452   1.83    itojun 						so, m, optp, optlen, &opti))
   1453   1.29   thorpej 					m = NULL;
   1454   1.29   thorpej 			}
   1455   1.29   thorpej 			goto drop;
   1456    1.1       cgd 		}
   1457    1.1       cgd 	}
   1458    1.1       cgd 
   1459   1.29   thorpej after_listen:
   1460   1.29   thorpej #ifdef DIAGNOSTIC
   1461   1.29   thorpej 	/*
   1462   1.29   thorpej 	 * Should not happen now that all embryonic connections
   1463   1.29   thorpej 	 * are handled with compressed state.
   1464   1.29   thorpej 	 */
   1465   1.29   thorpej 	if (tp->t_state == TCPS_LISTEN)
   1466   1.29   thorpej 		panic("tcp_input: TCPS_LISTEN");
   1467   1.29   thorpej #endif
   1468   1.29   thorpej 
   1469    1.1       cgd 	/*
   1470    1.1       cgd 	 * Segment received on connection.
   1471    1.1       cgd 	 * Reset idle time and keep-alive timer.
   1472    1.1       cgd 	 */
   1473  1.128   thorpej 	tp->t_rcvtime = tcp_now;
   1474   1.25   mycroft 	if (TCPS_HAVEESTABLISHED(tp->t_state))
   1475   1.58   thorpej 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
   1476    1.1       cgd 
   1477    1.1       cgd 	/*
   1478   1.29   thorpej 	 * Process options.
   1479    1.1       cgd 	 */
   1480   1.29   thorpej 	if (optp)
   1481   1.83    itojun 		tcp_dooptions(tp, optp, optlen, th, &opti);
   1482    1.9   mycroft 
   1483  1.143    itojun 	/*
   1484    1.1       cgd 	 * Header prediction: check for the two common cases
   1485    1.1       cgd 	 * of a uni-directional data xfer.  If the packet has
   1486    1.1       cgd 	 * no control flags, is in-sequence, the window didn't
   1487    1.1       cgd 	 * change and we're not retransmitting, it's a
   1488    1.1       cgd 	 * candidate.  If the length is zero and the ack moved
   1489    1.1       cgd 	 * forward, we're the sender side of the xfer.  Just
   1490    1.1       cgd 	 * free the data acked & wake any higher level process
   1491    1.1       cgd 	 * that was blocked waiting for space.  If the length
   1492    1.1       cgd 	 * is non-zero and the ack didn't move, we're the
   1493    1.1       cgd 	 * receiver side.  If we're getting packets in-order
   1494    1.1       cgd 	 * (the reassembly queue is empty), add the data to
   1495    1.1       cgd 	 * the socket buffer and note that we need a delayed ack.
   1496    1.1       cgd 	 */
   1497    1.1       cgd 	if (tp->t_state == TCPS_ESTABLISHED &&
   1498    1.1       cgd 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
   1499   1.29   thorpej 	    (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
   1500   1.83    itojun 	    th->th_seq == tp->rcv_nxt &&
   1501    1.9   mycroft 	    tiwin && tiwin == tp->snd_wnd &&
   1502    1.1       cgd 	    tp->snd_nxt == tp->snd_max) {
   1503    1.9   mycroft 
   1504  1.143    itojun 		/*
   1505    1.9   mycroft 		 * If last ACK falls within this segment's sequence numbers,
   1506    1.9   mycroft 		 *  record the timestamp.
   1507    1.9   mycroft 		 */
   1508   1.29   thorpej 		if (opti.ts_present &&
   1509   1.83    itojun 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
   1510   1.83    itojun 		    SEQ_LT(tp->last_ack_sent, th->th_seq + tlen)) {
   1511  1.123   thorpej 			tp->ts_recent_age = TCP_TIMESTAMP(tp);
   1512   1.29   thorpej 			tp->ts_recent = opti.ts_val;
   1513    1.9   mycroft 		}
   1514    1.9   mycroft 
   1515   1.83    itojun 		if (tlen == 0) {
   1516   1.83    itojun 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
   1517   1.83    itojun 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
   1518   1.15   mycroft 			    tp->snd_cwnd >= tp->snd_wnd &&
   1519   1.15   mycroft 			    tp->t_dupacks < tcprexmtthresh) {
   1520    1.1       cgd 				/*
   1521    1.1       cgd 				 * this is a pure ack for outstanding data.
   1522    1.1       cgd 				 */
   1523    1.9   mycroft 				++tcpstat.tcps_predack;
   1524   1.90   thorpej 				if (opti.ts_present && opti.ts_ecr)
   1525   1.29   thorpej 					tcp_xmit_timer(tp,
   1526  1.123   thorpej 					  TCP_TIMESTAMP(tp) - opti.ts_ecr + 1);
   1527  1.128   thorpej 				else if (tp->t_rtttime &&
   1528   1.83    itojun 				    SEQ_GT(th->th_ack, tp->t_rtseq))
   1529  1.128   thorpej 					tcp_xmit_timer(tp,
   1530  1.128   thorpej 					tcp_now - tp->t_rtttime);
   1531   1.83    itojun 				acked = th->th_ack - tp->snd_una;
   1532    1.1       cgd 				tcpstat.tcps_rcvackpack++;
   1533    1.1       cgd 				tcpstat.tcps_rcvackbyte += acked;
   1534   1.98    itojun 				ND6_HINT(tp);
   1535    1.1       cgd 				sbdrop(&so->so_snd, acked);
   1536   1.91   thorpej 				/*
   1537   1.91   thorpej 				 * We want snd_recover to track snd_una to
   1538   1.91   thorpej 				 * avoid sequence wraparound problems for
   1539   1.91   thorpej 				 * very large transfers.
   1540   1.91   thorpej 				 */
   1541   1.91   thorpej 				tp->snd_una = tp->snd_recover = th->th_ack;
   1542    1.1       cgd 				m_freem(m);
   1543    1.1       cgd 
   1544    1.1       cgd 				/*
   1545    1.1       cgd 				 * If all outstanding data are acked, stop
   1546    1.1       cgd 				 * retransmit timer, otherwise restart timer
   1547    1.1       cgd 				 * using current (possibly backed-off) value.
   1548    1.1       cgd 				 * If process is waiting for space,
   1549    1.1       cgd 				 * wakeup/selwakeup/signal.  If data
   1550    1.1       cgd 				 * are ready to send, let tcp_output
   1551    1.1       cgd 				 * decide between more output or persist.
   1552    1.1       cgd 				 */
   1553    1.1       cgd 				if (tp->snd_una == tp->snd_max)
   1554   1.58   thorpej 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1555   1.58   thorpej 				else if (TCP_TIMER_ISARMED(tp,
   1556   1.58   thorpej 				    TCPT_PERSIST) == 0)
   1557   1.58   thorpej 					TCP_TIMER_ARM(tp, TCPT_REXMT,
   1558   1.58   thorpej 					    tp->t_rxtcur);
   1559    1.1       cgd 
   1560   1.54      matt 				sowwakeup(so);
   1561    1.1       cgd 				if (so->so_snd.sb_cc)
   1562    1.1       cgd 					(void) tcp_output(tp);
   1563   1.89    itojun 				if (tcp_saveti)
   1564   1.89    itojun 					m_freem(tcp_saveti);
   1565    1.1       cgd 				return;
   1566    1.1       cgd 			}
   1567   1.83    itojun 		} else if (th->th_ack == tp->snd_una &&
   1568  1.141      matt 		    TAILQ_FIRST(&tp->segq) == NULL &&
   1569   1.83    itojun 		    tlen <= sbspace(&so->so_rcv)) {
   1570    1.1       cgd 			/*
   1571    1.1       cgd 			 * this is a pure, in-sequence data packet
   1572    1.1       cgd 			 * with nothing on the reassembly queue and
   1573    1.1       cgd 			 * we have enough buffer space to take it.
   1574    1.1       cgd 			 */
   1575    1.9   mycroft 			++tcpstat.tcps_preddat;
   1576   1.83    itojun 			tp->rcv_nxt += tlen;
   1577    1.1       cgd 			tcpstat.tcps_rcvpack++;
   1578   1.83    itojun 			tcpstat.tcps_rcvbyte += tlen;
   1579   1.98    itojun 			ND6_HINT(tp);
   1580    1.1       cgd 			/*
   1581    1.9   mycroft 			 * Drop TCP, IP headers and TCP options then add data
   1582    1.9   mycroft 			 * to socket buffer.
   1583    1.1       cgd 			 */
   1584  1.154    itojun 			if (so->so_state & SS_CANTRCVMORE)
   1585  1.154    itojun 				m_freem(m);
   1586  1.154    itojun 			else {
   1587  1.154    itojun 				m_adj(m, toff + off);
   1588  1.154    itojun 				sbappendstream(&so->so_rcv, m);
   1589  1.154    itojun 			}
   1590    1.1       cgd 			sorwakeup(so);
   1591   1.83    itojun 			TCP_SETUP_ACK(tp, th);
   1592   1.37   thorpej 			if (tp->t_flags & TF_ACKNOW)
   1593   1.37   thorpej 				(void) tcp_output(tp);
   1594   1.89    itojun 			if (tcp_saveti)
   1595   1.89    itojun 				m_freem(tcp_saveti);
   1596    1.1       cgd 			return;
   1597    1.1       cgd 		}
   1598    1.1       cgd 	}
   1599    1.1       cgd 
   1600    1.1       cgd 	/*
   1601   1.97    itojun 	 * Compute mbuf offset to TCP data segment.
   1602    1.1       cgd 	 */
   1603   1.97    itojun 	hdroptlen = toff + off;
   1604    1.1       cgd 
   1605    1.1       cgd 	/*
   1606    1.1       cgd 	 * Calculate amount of space in receive window,
   1607    1.1       cgd 	 * and then do TCP input processing.
   1608    1.1       cgd 	 * Receive window is amount of space in rcv queue,
   1609    1.1       cgd 	 * but not less than advertised window.
   1610    1.1       cgd 	 */
   1611    1.1       cgd 	{ int win;
   1612    1.1       cgd 
   1613    1.1       cgd 	win = sbspace(&so->so_rcv);
   1614    1.1       cgd 	if (win < 0)
   1615    1.1       cgd 		win = 0;
   1616   1.28   thorpej 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
   1617    1.1       cgd 	}
   1618    1.1       cgd 
   1619    1.1       cgd 	switch (tp->t_state) {
   1620  1.140  christos 	case TCPS_LISTEN:
   1621  1.140  christos 		/*
   1622  1.140  christos 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
   1623  1.140  christos 		 */
   1624  1.140  christos 		if (m->m_flags & (M_BCAST|M_MCAST))
   1625  1.140  christos 			goto drop;
   1626  1.140  christos 		switch (af) {
   1627  1.140  christos #ifdef INET6
   1628  1.140  christos 		case AF_INET6:
   1629  1.140  christos 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
   1630  1.140  christos 				goto drop;
   1631  1.140  christos 			break;
   1632  1.140  christos #endif /* INET6 */
   1633  1.140  christos 		case AF_INET:
   1634  1.140  christos 			if (IN_MULTICAST(ip->ip_dst.s_addr) ||
   1635  1.140  christos 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
   1636  1.140  christos 				goto drop;
   1637  1.140  christos 			break;
   1638  1.140  christos 		}
   1639  1.140  christos 		break;
   1640    1.1       cgd 
   1641    1.1       cgd 	/*
   1642    1.1       cgd 	 * If the state is SYN_SENT:
   1643    1.1       cgd 	 *	if seg contains an ACK, but not for our SYN, drop the input.
   1644    1.1       cgd 	 *	if seg contains a RST, then drop the connection.
   1645    1.1       cgd 	 *	if seg does not contain SYN, then drop it.
   1646    1.1       cgd 	 * Otherwise this is an acceptable SYN segment
   1647    1.1       cgd 	 *	initialize tp->rcv_nxt and tp->irs
   1648    1.1       cgd 	 *	if seg contains ack then advance tp->snd_una
   1649    1.1       cgd 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
   1650    1.1       cgd 	 *	arrange for segment to be acked (eventually)
   1651    1.1       cgd 	 *	continue processing rest of data/controls, beginning with URG
   1652    1.1       cgd 	 */
   1653    1.1       cgd 	case TCPS_SYN_SENT:
   1654    1.1       cgd 		if ((tiflags & TH_ACK) &&
   1655   1.83    itojun 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
   1656   1.83    itojun 		     SEQ_GT(th->th_ack, tp->snd_max)))
   1657    1.1       cgd 			goto dropwithreset;
   1658    1.1       cgd 		if (tiflags & TH_RST) {
   1659    1.1       cgd 			if (tiflags & TH_ACK)
   1660    1.1       cgd 				tp = tcp_drop(tp, ECONNREFUSED);
   1661    1.1       cgd 			goto drop;
   1662    1.1       cgd 		}
   1663    1.1       cgd 		if ((tiflags & TH_SYN) == 0)
   1664    1.1       cgd 			goto drop;
   1665    1.1       cgd 		if (tiflags & TH_ACK) {
   1666   1.91   thorpej 			tp->snd_una = tp->snd_recover = th->th_ack;
   1667    1.1       cgd 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   1668    1.1       cgd 				tp->snd_nxt = tp->snd_una;
   1669  1.107      matt 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1670    1.1       cgd 		}
   1671   1.83    itojun 		tp->irs = th->th_seq;
   1672    1.1       cgd 		tcp_rcvseqinit(tp);
   1673    1.1       cgd 		tp->t_flags |= TF_ACKNOW;
   1674   1.32   thorpej 		tcp_mss_from_peer(tp, opti.maxseg);
   1675   1.46   thorpej 
   1676   1.46   thorpej 		/*
   1677   1.46   thorpej 		 * Initialize the initial congestion window.  If we
   1678   1.46   thorpej 		 * had to retransmit the SYN, we must initialize cwnd
   1679   1.62   thorpej 		 * to 1 segment (i.e. the Loss Window).
   1680   1.46   thorpej 		 */
   1681   1.62   thorpej 		if (tp->t_flags & TF_SYN_REXMT)
   1682   1.62   thorpej 			tp->snd_cwnd = tp->t_peermss;
   1683   1.62   thorpej 		else
   1684   1.62   thorpej 			tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
   1685   1.62   thorpej 			    tp->t_peermss);
   1686   1.46   thorpej 
   1687   1.32   thorpej 		tcp_rmx_rtt(tp);
   1688  1.108      matt 		if (tiflags & TH_ACK) {
   1689    1.1       cgd 			tcpstat.tcps_connects++;
   1690    1.1       cgd 			soisconnected(so);
   1691   1.32   thorpej 			tcp_established(tp);
   1692    1.9   mycroft 			/* Do window scaling on this connection? */
   1693    1.9   mycroft 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   1694    1.9   mycroft 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
   1695    1.9   mycroft 				tp->snd_scale = tp->requested_s_scale;
   1696    1.9   mycroft 				tp->rcv_scale = tp->request_r_scale;
   1697    1.9   mycroft 			}
   1698   1.72   thorpej 			TCP_REASS_LOCK(tp);
   1699   1.83    itojun 			(void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
   1700   1.72   thorpej 			TCP_REASS_UNLOCK(tp);
   1701    1.1       cgd 			/*
   1702    1.1       cgd 			 * if we didn't have to retransmit the SYN,
   1703    1.1       cgd 			 * use its rtt as our initial srtt & rtt var.
   1704    1.1       cgd 			 */
   1705  1.128   thorpej 			if (tp->t_rtttime)
   1706  1.128   thorpej 				tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
   1707    1.1       cgd 		} else
   1708    1.1       cgd 			tp->t_state = TCPS_SYN_RECEIVED;
   1709    1.1       cgd 
   1710    1.1       cgd 		/*
   1711   1.83    itojun 		 * Advance th->th_seq to correspond to first data byte.
   1712    1.1       cgd 		 * If data, trim to stay within window,
   1713    1.1       cgd 		 * dropping FIN if necessary.
   1714    1.1       cgd 		 */
   1715   1.83    itojun 		th->th_seq++;
   1716   1.83    itojun 		if (tlen > tp->rcv_wnd) {
   1717   1.83    itojun 			todrop = tlen - tp->rcv_wnd;
   1718    1.1       cgd 			m_adj(m, -todrop);
   1719   1.83    itojun 			tlen = tp->rcv_wnd;
   1720    1.1       cgd 			tiflags &= ~TH_FIN;
   1721    1.1       cgd 			tcpstat.tcps_rcvpackafterwin++;
   1722    1.1       cgd 			tcpstat.tcps_rcvbyteafterwin += todrop;
   1723    1.1       cgd 		}
   1724   1.83    itojun 		tp->snd_wl1 = th->th_seq - 1;
   1725   1.83    itojun 		tp->rcv_up = th->th_seq;
   1726    1.1       cgd 		goto step6;
   1727   1.29   thorpej 
   1728   1.29   thorpej 	/*
   1729   1.29   thorpej 	 * If the state is SYN_RECEIVED:
   1730   1.29   thorpej 	 *	If seg contains an ACK, but not for our SYN, drop the input
   1731   1.29   thorpej 	 *	and generate an RST.  See page 36, rfc793
   1732   1.29   thorpej 	 */
   1733   1.29   thorpej 	case TCPS_SYN_RECEIVED:
   1734   1.29   thorpej 		if ((tiflags & TH_ACK) &&
   1735   1.83    itojun 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
   1736   1.83    itojun 		     SEQ_GT(th->th_ack, tp->snd_max)))
   1737   1.29   thorpej 			goto dropwithreset;
   1738   1.29   thorpej 		break;
   1739    1.1       cgd 	}
   1740    1.1       cgd 
   1741    1.1       cgd 	/*
   1742    1.1       cgd 	 * States other than LISTEN or SYN_SENT.
   1743    1.9   mycroft 	 * First check timestamp, if present.
   1744  1.143    itojun 	 * Then check that at least some bytes of segment are within
   1745    1.1       cgd 	 * receive window.  If segment begins before rcv_nxt,
   1746    1.1       cgd 	 * drop leading data (and SYN); if nothing left, just ack.
   1747  1.143    itojun 	 *
   1748    1.9   mycroft 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
   1749    1.9   mycroft 	 * and it's less than ts_recent, drop it.
   1750    1.1       cgd 	 */
   1751   1.29   thorpej 	if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
   1752   1.29   thorpej 	    TSTMP_LT(opti.ts_val, tp->ts_recent)) {
   1753    1.9   mycroft 
   1754    1.9   mycroft 		/* Check to see if ts_recent is over 24 days old.  */
   1755  1.123   thorpej 		if ((int)(TCP_TIMESTAMP(tp) - tp->ts_recent_age) >
   1756  1.123   thorpej 		    TCP_PAWS_IDLE) {
   1757    1.9   mycroft 			/*
   1758    1.9   mycroft 			 * Invalidate ts_recent.  If this segment updates
   1759    1.9   mycroft 			 * ts_recent, the age will be reset later and ts_recent
   1760    1.9   mycroft 			 * will get a valid value.  If it does not, setting
   1761    1.9   mycroft 			 * ts_recent to zero will at least satisfy the
   1762    1.9   mycroft 			 * requirement that zero be placed in the timestamp
   1763    1.9   mycroft 			 * echo reply when ts_recent isn't valid.  The
   1764    1.9   mycroft 			 * age isn't reset until we get a valid ts_recent
   1765    1.9   mycroft 			 * because we don't want out-of-order segments to be
   1766    1.9   mycroft 			 * dropped when ts_recent is old.
   1767    1.9   mycroft 			 */
   1768    1.9   mycroft 			tp->ts_recent = 0;
   1769    1.9   mycroft 		} else {
   1770    1.9   mycroft 			tcpstat.tcps_rcvduppack++;
   1771   1.83    itojun 			tcpstat.tcps_rcvdupbyte += tlen;
   1772    1.9   mycroft 			tcpstat.tcps_pawsdrop++;
   1773    1.9   mycroft 			goto dropafterack;
   1774    1.9   mycroft 		}
   1775    1.9   mycroft 	}
   1776    1.9   mycroft 
   1777   1.83    itojun 	todrop = tp->rcv_nxt - th->th_seq;
   1778    1.1       cgd 	if (todrop > 0) {
   1779    1.1       cgd 		if (tiflags & TH_SYN) {
   1780    1.1       cgd 			tiflags &= ~TH_SYN;
   1781   1.83    itojun 			th->th_seq++;
   1782  1.143    itojun 			if (th->th_urp > 1)
   1783   1.83    itojun 				th->th_urp--;
   1784   1.24   mycroft 			else {
   1785    1.1       cgd 				tiflags &= ~TH_URG;
   1786   1.83    itojun 				th->th_urp = 0;
   1787   1.24   mycroft 			}
   1788    1.1       cgd 			todrop--;
   1789    1.1       cgd 		}
   1790   1.83    itojun 		if (todrop > tlen ||
   1791   1.83    itojun 		    (todrop == tlen && (tiflags & TH_FIN) == 0)) {
   1792    1.1       cgd 			/*
   1793   1.42   mycroft 			 * Any valid FIN must be to the left of the window.
   1794   1.42   mycroft 			 * At this point the FIN must be a duplicate or
   1795   1.42   mycroft 			 * out of sequence; drop it.
   1796    1.7   mycroft 			 */
   1797    1.7   mycroft 			tiflags &= ~TH_FIN;
   1798    1.7   mycroft 			/*
   1799   1.42   mycroft 			 * Send an ACK to resynchronize and drop any data.
   1800   1.42   mycroft 			 * But keep on processing for RST or ACK.
   1801    1.1       cgd 			 */
   1802    1.7   mycroft 			tp->t_flags |= TF_ACKNOW;
   1803   1.83    itojun 			todrop = tlen;
   1804   1.42   mycroft 			tcpstat.tcps_rcvdupbyte += todrop;
   1805    1.7   mycroft 			tcpstat.tcps_rcvduppack++;
   1806    1.1       cgd 		} else {
   1807    1.1       cgd 			tcpstat.tcps_rcvpartduppack++;
   1808    1.1       cgd 			tcpstat.tcps_rcvpartdupbyte += todrop;
   1809    1.1       cgd 		}
   1810   1.97    itojun 		hdroptlen += todrop;	/*drop from head afterwards*/
   1811   1.83    itojun 		th->th_seq += todrop;
   1812   1.83    itojun 		tlen -= todrop;
   1813   1.83    itojun 		if (th->th_urp > todrop)
   1814   1.83    itojun 			th->th_urp -= todrop;
   1815    1.1       cgd 		else {
   1816    1.1       cgd 			tiflags &= ~TH_URG;
   1817   1.83    itojun 			th->th_urp = 0;
   1818    1.1       cgd 		}
   1819    1.1       cgd 	}
   1820    1.1       cgd 
   1821    1.1       cgd 	/*
   1822    1.1       cgd 	 * If new data are received on a connection after the
   1823    1.1       cgd 	 * user processes are gone, then RST the other end.
   1824    1.1       cgd 	 */
   1825    1.1       cgd 	if ((so->so_state & SS_NOFDREF) &&
   1826   1.83    itojun 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
   1827    1.1       cgd 		tp = tcp_close(tp);
   1828    1.1       cgd 		tcpstat.tcps_rcvafterclose++;
   1829    1.1       cgd 		goto dropwithreset;
   1830    1.1       cgd 	}
   1831    1.1       cgd 
   1832    1.1       cgd 	/*
   1833    1.1       cgd 	 * If segment ends after window, drop trailing data
   1834    1.1       cgd 	 * (and PUSH and FIN); if nothing left, just ACK.
   1835    1.1       cgd 	 */
   1836   1.83    itojun 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd);
   1837    1.1       cgd 	if (todrop > 0) {
   1838    1.1       cgd 		tcpstat.tcps_rcvpackafterwin++;
   1839   1.83    itojun 		if (todrop >= tlen) {
   1840   1.83    itojun 			tcpstat.tcps_rcvbyteafterwin += tlen;
   1841    1.1       cgd 			/*
   1842    1.1       cgd 			 * If a new connection request is received
   1843    1.1       cgd 			 * while in TIME_WAIT, drop the old connection
   1844    1.1       cgd 			 * and start over if the sequence numbers
   1845    1.1       cgd 			 * are above the previous ones.
   1846  1.153   thorpej 			 *
   1847  1.153   thorpej 			 * NOTE: We will checksum the packet again, and
   1848  1.153   thorpej 			 * so we need to put the header fields back into
   1849  1.153   thorpej 			 * network order!
   1850  1.153   thorpej 			 * XXX This kind of sucks, but we don't expect
   1851  1.153   thorpej 			 * XXX this to happen very often, so maybe it
   1852  1.153   thorpej 			 * XXX doesn't matter so much.
   1853    1.1       cgd 			 */
   1854    1.1       cgd 			if (tiflags & TH_SYN &&
   1855    1.1       cgd 			    tp->t_state == TCPS_TIME_WAIT &&
   1856   1.83    itojun 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
   1857  1.123   thorpej 				iss = tcp_new_iss(tp, tp->snd_nxt);
   1858    1.1       cgd 				tp = tcp_close(tp);
   1859  1.153   thorpej 				TCP_FIELDS_TO_NET(th);
   1860    1.1       cgd 				goto findpcb;
   1861    1.1       cgd 			}
   1862    1.1       cgd 			/*
   1863    1.1       cgd 			 * If window is closed can only take segments at
   1864    1.1       cgd 			 * window edge, and have to drop data and PUSH from
   1865    1.1       cgd 			 * incoming segments.  Continue processing, but
   1866    1.1       cgd 			 * remember to ack.  Otherwise, drop segment
   1867    1.1       cgd 			 * and ack.
   1868    1.1       cgd 			 */
   1869   1.83    itojun 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
   1870    1.1       cgd 				tp->t_flags |= TF_ACKNOW;
   1871    1.1       cgd 				tcpstat.tcps_rcvwinprobe++;
   1872    1.1       cgd 			} else
   1873    1.1       cgd 				goto dropafterack;
   1874    1.1       cgd 		} else
   1875    1.1       cgd 			tcpstat.tcps_rcvbyteafterwin += todrop;
   1876    1.1       cgd 		m_adj(m, -todrop);
   1877   1.83    itojun 		tlen -= todrop;
   1878    1.1       cgd 		tiflags &= ~(TH_PUSH|TH_FIN);
   1879    1.1       cgd 	}
   1880    1.1       cgd 
   1881    1.1       cgd 	/*
   1882    1.9   mycroft 	 * If last ACK falls within this segment's sequence numbers,
   1883   1.53   thorpej 	 * and the timestamp is newer, record it.
   1884    1.9   mycroft 	 */
   1885   1.53   thorpej 	if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
   1886   1.83    itojun 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
   1887   1.83    itojun 	    SEQ_LT(tp->last_ack_sent, th->th_seq + tlen +
   1888    1.9   mycroft 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
   1889  1.123   thorpej 		tp->ts_recent_age = TCP_TIMESTAMP(tp);
   1890   1.29   thorpej 		tp->ts_recent = opti.ts_val;
   1891    1.9   mycroft 	}
   1892    1.9   mycroft 
   1893    1.9   mycroft 	/*
   1894    1.1       cgd 	 * If the RST bit is set examine the state:
   1895    1.1       cgd 	 *    SYN_RECEIVED STATE:
   1896    1.1       cgd 	 *	If passive open, return to LISTEN state.
   1897    1.1       cgd 	 *	If active open, inform user that connection was refused.
   1898    1.1       cgd 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
   1899    1.1       cgd 	 *	Inform user that connection was reset, and close tcb.
   1900    1.1       cgd 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
   1901    1.1       cgd 	 *	Close the tcb.
   1902    1.1       cgd 	 */
   1903    1.1       cgd 	if (tiflags&TH_RST) switch (tp->t_state) {
   1904    1.1       cgd 
   1905    1.1       cgd 	case TCPS_SYN_RECEIVED:
   1906    1.1       cgd 		so->so_error = ECONNREFUSED;
   1907    1.1       cgd 		goto close;
   1908    1.1       cgd 
   1909    1.1       cgd 	case TCPS_ESTABLISHED:
   1910    1.1       cgd 	case TCPS_FIN_WAIT_1:
   1911    1.1       cgd 	case TCPS_FIN_WAIT_2:
   1912    1.1       cgd 	case TCPS_CLOSE_WAIT:
   1913    1.1       cgd 		so->so_error = ECONNRESET;
   1914    1.1       cgd 	close:
   1915    1.1       cgd 		tp->t_state = TCPS_CLOSED;
   1916    1.1       cgd 		tcpstat.tcps_drops++;
   1917    1.1       cgd 		tp = tcp_close(tp);
   1918    1.1       cgd 		goto drop;
   1919    1.1       cgd 
   1920    1.1       cgd 	case TCPS_CLOSING:
   1921    1.1       cgd 	case TCPS_LAST_ACK:
   1922    1.1       cgd 	case TCPS_TIME_WAIT:
   1923    1.1       cgd 		tp = tcp_close(tp);
   1924    1.1       cgd 		goto drop;
   1925    1.1       cgd 	}
   1926    1.1       cgd 
   1927    1.1       cgd 	/*
   1928    1.1       cgd 	 * If a SYN is in the window, then this is an
   1929    1.1       cgd 	 * error and we send an RST and drop the connection.
   1930    1.1       cgd 	 */
   1931    1.1       cgd 	if (tiflags & TH_SYN) {
   1932    1.1       cgd 		tp = tcp_drop(tp, ECONNRESET);
   1933    1.1       cgd 		goto dropwithreset;
   1934    1.1       cgd 	}
   1935    1.1       cgd 
   1936    1.1       cgd 	/*
   1937    1.1       cgd 	 * If the ACK bit is off we drop the segment and return.
   1938    1.1       cgd 	 */
   1939   1.78       kml 	if ((tiflags & TH_ACK) == 0) {
   1940   1.78       kml 		if (tp->t_flags & TF_ACKNOW)
   1941   1.78       kml 			goto dropafterack;
   1942   1.78       kml 		else
   1943   1.78       kml 			goto drop;
   1944   1.78       kml 	}
   1945  1.143    itojun 
   1946    1.1       cgd 	/*
   1947    1.1       cgd 	 * Ack processing.
   1948    1.1       cgd 	 */
   1949    1.1       cgd 	switch (tp->t_state) {
   1950    1.1       cgd 
   1951    1.1       cgd 	/*
   1952    1.1       cgd 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
   1953    1.1       cgd 	 * ESTABLISHED state and continue processing, otherwise
   1954    1.1       cgd 	 * send an RST.
   1955    1.1       cgd 	 */
   1956    1.1       cgd 	case TCPS_SYN_RECEIVED:
   1957   1.83    itojun 		if (SEQ_GT(tp->snd_una, th->th_ack) ||
   1958   1.83    itojun 		    SEQ_GT(th->th_ack, tp->snd_max))
   1959    1.1       cgd 			goto dropwithreset;
   1960    1.1       cgd 		tcpstat.tcps_connects++;
   1961    1.1       cgd 		soisconnected(so);
   1962   1.32   thorpej 		tcp_established(tp);
   1963    1.9   mycroft 		/* Do window scaling? */
   1964    1.9   mycroft 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   1965    1.9   mycroft 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
   1966    1.9   mycroft 			tp->snd_scale = tp->requested_s_scale;
   1967    1.9   mycroft 			tp->rcv_scale = tp->request_r_scale;
   1968    1.9   mycroft 		}
   1969   1.72   thorpej 		TCP_REASS_LOCK(tp);
   1970   1.83    itojun 		(void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
   1971   1.72   thorpej 		TCP_REASS_UNLOCK(tp);
   1972   1.83    itojun 		tp->snd_wl1 = th->th_seq - 1;
   1973    1.1       cgd 		/* fall into ... */
   1974    1.1       cgd 
   1975    1.1       cgd 	/*
   1976    1.1       cgd 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
   1977    1.1       cgd 	 * ACKs.  If the ack is in the range
   1978   1.83    itojun 	 *	tp->snd_una < th->th_ack <= tp->snd_max
   1979   1.83    itojun 	 * then advance tp->snd_una to th->th_ack and drop
   1980    1.1       cgd 	 * data from the retransmission queue.  If this ACK reflects
   1981    1.1       cgd 	 * more up to date window information we update our window information.
   1982    1.1       cgd 	 */
   1983    1.1       cgd 	case TCPS_ESTABLISHED:
   1984    1.1       cgd 	case TCPS_FIN_WAIT_1:
   1985    1.1       cgd 	case TCPS_FIN_WAIT_2:
   1986    1.1       cgd 	case TCPS_CLOSE_WAIT:
   1987    1.1       cgd 	case TCPS_CLOSING:
   1988    1.1       cgd 	case TCPS_LAST_ACK:
   1989    1.1       cgd 	case TCPS_TIME_WAIT:
   1990    1.1       cgd 
   1991   1.83    itojun 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
   1992   1.83    itojun 			if (tlen == 0 && tiwin == tp->snd_wnd) {
   1993    1.1       cgd 				tcpstat.tcps_rcvdupack++;
   1994    1.1       cgd 				/*
   1995    1.1       cgd 				 * If we have outstanding data (other than
   1996    1.1       cgd 				 * a window probe), this is a completely
   1997    1.1       cgd 				 * duplicate ack (ie, window info didn't
   1998    1.1       cgd 				 * change), the ack is the biggest we've
   1999    1.1       cgd 				 * seen and we've seen exactly our rexmt
   2000    1.1       cgd 				 * threshhold of them, assume a packet
   2001    1.1       cgd 				 * has been dropped and retransmit it.
   2002    1.1       cgd 				 * Kludge snd_nxt & the congestion
   2003    1.1       cgd 				 * window so we send only this one
   2004    1.1       cgd 				 * packet.
   2005    1.1       cgd 				 *
   2006    1.1       cgd 				 * We know we're losing at the current
   2007    1.1       cgd 				 * window size so do congestion avoidance
   2008    1.1       cgd 				 * (set ssthresh to half the current window
   2009    1.1       cgd 				 * and pull our congestion window back to
   2010    1.1       cgd 				 * the new ssthresh).
   2011    1.1       cgd 				 *
   2012    1.1       cgd 				 * Dup acks mean that packets have left the
   2013  1.143    itojun 				 * network (they're now cached at the receiver)
   2014    1.1       cgd 				 * so bump cwnd by the amount in the receiver
   2015    1.1       cgd 				 * to keep a constant cwnd packets in the
   2016    1.1       cgd 				 * network.
   2017    1.1       cgd 				 */
   2018   1.58   thorpej 				if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
   2019   1.83    itojun 				    th->th_ack != tp->snd_una)
   2020    1.1       cgd 					tp->t_dupacks = 0;
   2021    1.1       cgd 				else if (++tp->t_dupacks == tcprexmtthresh) {
   2022    1.1       cgd 					tcp_seq onxt = tp->snd_nxt;
   2023    1.1       cgd 					u_int win =
   2024  1.143    itojun 					    min(tp->snd_wnd, tp->snd_cwnd) /
   2025   1.34       kml 					    2 /	tp->t_segsz;
   2026   1.94   thorpej 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
   2027   1.91   thorpej 					    tp->snd_recover)) {
   2028   1.68      matt 						/*
   2029   1.68      matt 						 * False fast retransmit after
   2030   1.68      matt 						 * timeout.  Do not cut window.
   2031   1.68      matt 						 */
   2032   1.68      matt 						tp->snd_cwnd += tp->t_segsz;
   2033   1.68      matt 						tp->t_dupacks = 0;
   2034   1.68      matt 						(void) tcp_output(tp);
   2035   1.68      matt 						goto drop;
   2036   1.68      matt 					}
   2037    1.1       cgd 
   2038    1.1       cgd 					if (win < 2)
   2039    1.1       cgd 						win = 2;
   2040   1.34       kml 					tp->snd_ssthresh = win * tp->t_segsz;
   2041   1.68      matt 					tp->snd_recover = tp->snd_max;
   2042   1.58   thorpej 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
   2043  1.128   thorpej 					tp->t_rtttime = 0;
   2044   1.83    itojun 					tp->snd_nxt = th->th_ack;
   2045   1.34       kml 					tp->snd_cwnd = tp->t_segsz;
   2046    1.1       cgd 					(void) tcp_output(tp);
   2047    1.1       cgd 					tp->snd_cwnd = tp->snd_ssthresh +
   2048   1.34       kml 					       tp->t_segsz * tp->t_dupacks;
   2049    1.1       cgd 					if (SEQ_GT(onxt, tp->snd_nxt))
   2050    1.1       cgd 						tp->snd_nxt = onxt;
   2051    1.1       cgd 					goto drop;
   2052    1.1       cgd 				} else if (tp->t_dupacks > tcprexmtthresh) {
   2053   1.34       kml 					tp->snd_cwnd += tp->t_segsz;
   2054    1.1       cgd 					(void) tcp_output(tp);
   2055    1.1       cgd 					goto drop;
   2056    1.1       cgd 				}
   2057    1.1       cgd 			} else
   2058    1.1       cgd 				tp->t_dupacks = 0;
   2059    1.1       cgd 			break;
   2060    1.1       cgd 		}
   2061    1.1       cgd 		/*
   2062    1.1       cgd 		 * If the congestion window was inflated to account
   2063    1.1       cgd 		 * for the other side's cached packets, retract it.
   2064    1.1       cgd 		 */
   2065   1.94   thorpej 		if (tcp_do_newreno == 0) {
   2066   1.69      matt 			if (tp->t_dupacks >= tcprexmtthresh &&
   2067   1.69      matt 			    tp->snd_cwnd > tp->snd_ssthresh)
   2068   1.69      matt 				tp->snd_cwnd = tp->snd_ssthresh;
   2069   1.69      matt 			tp->t_dupacks = 0;
   2070   1.94   thorpej 		} else if (tp->t_dupacks >= tcprexmtthresh &&
   2071   1.94   thorpej 			   tcp_newreno(tp, th) == 0) {
   2072    1.1       cgd 			tp->snd_cwnd = tp->snd_ssthresh;
   2073   1.68      matt 			/*
   2074   1.68      matt 			 * Window inflation should have left us with approx.
   2075  1.143    itojun 			 * snd_ssthresh outstanding data.  But in case we
   2076   1.68      matt 			 * would be inclined to send a burst, better to do
   2077   1.68      matt 			 * it via the slow start mechanism.
   2078   1.68      matt 			 */
   2079   1.83    itojun 			if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
   2080   1.83    itojun 				tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
   2081   1.94   thorpej 				    + tp->t_segsz;
   2082   1.68      matt 			tp->t_dupacks = 0;
   2083   1.68      matt 		}
   2084   1.83    itojun 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
   2085    1.1       cgd 			tcpstat.tcps_rcvacktoomuch++;
   2086    1.1       cgd 			goto dropafterack;
   2087    1.1       cgd 		}
   2088   1.83    itojun 		acked = th->th_ack - tp->snd_una;
   2089    1.1       cgd 		tcpstat.tcps_rcvackpack++;
   2090    1.1       cgd 		tcpstat.tcps_rcvackbyte += acked;
   2091    1.1       cgd 
   2092    1.1       cgd 		/*
   2093    1.9   mycroft 		 * If we have a timestamp reply, update smoothed
   2094    1.9   mycroft 		 * round trip time.  If no timestamp is present but
   2095    1.9   mycroft 		 * transmit timer is running and timed sequence
   2096    1.1       cgd 		 * number was acked, update smoothed round trip time.
   2097    1.1       cgd 		 * Since we now have an rtt measurement, cancel the
   2098    1.1       cgd 		 * timer backoff (cf., Phil Karn's retransmit alg.).
   2099    1.1       cgd 		 * Recompute the initial retransmit timer.
   2100    1.1       cgd 		 */
   2101   1.90   thorpej 		if (opti.ts_present && opti.ts_ecr)
   2102  1.123   thorpej 			tcp_xmit_timer(tp, TCP_TIMESTAMP(tp) - opti.ts_ecr + 1);
   2103  1.128   thorpej 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
   2104  1.128   thorpej 			tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
   2105    1.1       cgd 
   2106    1.1       cgd 		/*
   2107    1.1       cgd 		 * If all outstanding data is acked, stop retransmit
   2108    1.1       cgd 		 * timer and remember to restart (more output or persist).
   2109    1.1       cgd 		 * If there is more data to be acked, restart retransmit
   2110    1.1       cgd 		 * timer, using current (possibly backed-off) value.
   2111    1.1       cgd 		 */
   2112   1.83    itojun 		if (th->th_ack == tp->snd_max) {
   2113   1.58   thorpej 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   2114    1.1       cgd 			needoutput = 1;
   2115   1.58   thorpej 		} else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
   2116   1.58   thorpej 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   2117    1.1       cgd 		/*
   2118    1.1       cgd 		 * When new data is acked, open the congestion window.
   2119    1.1       cgd 		 * If the window gives us less than ssthresh packets
   2120   1.34       kml 		 * in flight, open exponentially (segsz per packet).
   2121   1.34       kml 		 * Otherwise open linearly: segsz per window
   2122   1.34       kml 		 * (segsz^2 / cwnd per packet), plus a constant
   2123   1.34       kml 		 * fraction of a packet (segsz/8) to help larger windows
   2124    1.1       cgd 		 * open quickly enough.
   2125    1.1       cgd 		 */
   2126    1.1       cgd 		{
   2127  1.106  augustss 		u_int cw = tp->snd_cwnd;
   2128  1.106  augustss 		u_int incr = tp->t_segsz;
   2129    1.1       cgd 
   2130    1.1       cgd 		if (cw > tp->snd_ssthresh)
   2131   1.16   mycroft 			incr = incr * incr / cw;
   2132   1.94   thorpej 		if (tcp_do_newreno == 0 || SEQ_GEQ(th->th_ack, tp->snd_recover))
   2133   1.94   thorpej 			tp->snd_cwnd = min(cw + incr,
   2134   1.94   thorpej 			    TCP_MAXWIN << tp->snd_scale);
   2135    1.1       cgd 		}
   2136   1.98    itojun 		ND6_HINT(tp);
   2137    1.1       cgd 		if (acked > so->so_snd.sb_cc) {
   2138    1.1       cgd 			tp->snd_wnd -= so->so_snd.sb_cc;
   2139    1.1       cgd 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
   2140    1.1       cgd 			ourfinisacked = 1;
   2141    1.1       cgd 		} else {
   2142    1.1       cgd 			sbdrop(&so->so_snd, acked);
   2143    1.1       cgd 			tp->snd_wnd -= acked;
   2144    1.1       cgd 			ourfinisacked = 0;
   2145    1.1       cgd 		}
   2146   1.54      matt 		sowwakeup(so);
   2147   1.91   thorpej 		/*
   2148   1.91   thorpej 		 * We want snd_recover to track snd_una to
   2149   1.91   thorpej 		 * avoid sequence wraparound problems for
   2150   1.91   thorpej 		 * very large transfers.
   2151   1.91   thorpej 		 */
   2152   1.91   thorpej 		tp->snd_una = tp->snd_recover = th->th_ack;
   2153    1.1       cgd 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   2154    1.1       cgd 			tp->snd_nxt = tp->snd_una;
   2155    1.1       cgd 
   2156    1.1       cgd 		switch (tp->t_state) {
   2157    1.1       cgd 
   2158    1.1       cgd 		/*
   2159    1.1       cgd 		 * In FIN_WAIT_1 STATE in addition to the processing
   2160    1.1       cgd 		 * for the ESTABLISHED state if our FIN is now acknowledged
   2161    1.1       cgd 		 * then enter FIN_WAIT_2.
   2162    1.1       cgd 		 */
   2163    1.1       cgd 		case TCPS_FIN_WAIT_1:
   2164    1.1       cgd 			if (ourfinisacked) {
   2165    1.1       cgd 				/*
   2166    1.1       cgd 				 * If we can't receive any more
   2167    1.1       cgd 				 * data, then closing user can proceed.
   2168    1.1       cgd 				 * Starting the timer is contrary to the
   2169    1.1       cgd 				 * specification, but if we don't get a FIN
   2170    1.1       cgd 				 * we'll hang forever.
   2171    1.1       cgd 				 */
   2172    1.1       cgd 				if (so->so_state & SS_CANTRCVMORE) {
   2173    1.1       cgd 					soisdisconnected(so);
   2174   1.65     mouse 					if (tcp_maxidle > 0)
   2175   1.65     mouse 						TCP_TIMER_ARM(tp, TCPT_2MSL,
   2176   1.65     mouse 						    tcp_maxidle);
   2177    1.1       cgd 				}
   2178    1.1       cgd 				tp->t_state = TCPS_FIN_WAIT_2;
   2179    1.1       cgd 			}
   2180    1.1       cgd 			break;
   2181    1.1       cgd 
   2182    1.1       cgd 	 	/*
   2183    1.1       cgd 		 * In CLOSING STATE in addition to the processing for
   2184    1.1       cgd 		 * the ESTABLISHED state if the ACK acknowledges our FIN
   2185    1.1       cgd 		 * then enter the TIME-WAIT state, otherwise ignore
   2186    1.1       cgd 		 * the segment.
   2187    1.1       cgd 		 */
   2188    1.1       cgd 		case TCPS_CLOSING:
   2189    1.1       cgd 			if (ourfinisacked) {
   2190    1.1       cgd 				tp->t_state = TCPS_TIME_WAIT;
   2191    1.1       cgd 				tcp_canceltimers(tp);
   2192   1.58   thorpej 				TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2193    1.1       cgd 				soisdisconnected(so);
   2194    1.1       cgd 			}
   2195    1.1       cgd 			break;
   2196    1.1       cgd 
   2197    1.1       cgd 		/*
   2198    1.1       cgd 		 * In LAST_ACK, we may still be waiting for data to drain
   2199    1.1       cgd 		 * and/or to be acked, as well as for the ack of our FIN.
   2200    1.1       cgd 		 * If our FIN is now acknowledged, delete the TCB,
   2201    1.1       cgd 		 * enter the closed state and return.
   2202    1.1       cgd 		 */
   2203    1.1       cgd 		case TCPS_LAST_ACK:
   2204    1.1       cgd 			if (ourfinisacked) {
   2205    1.1       cgd 				tp = tcp_close(tp);
   2206    1.1       cgd 				goto drop;
   2207    1.1       cgd 			}
   2208    1.1       cgd 			break;
   2209    1.1       cgd 
   2210    1.1       cgd 		/*
   2211    1.1       cgd 		 * In TIME_WAIT state the only thing that should arrive
   2212    1.1       cgd 		 * is a retransmission of the remote FIN.  Acknowledge
   2213    1.1       cgd 		 * it and restart the finack timer.
   2214    1.1       cgd 		 */
   2215    1.1       cgd 		case TCPS_TIME_WAIT:
   2216   1.58   thorpej 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2217    1.1       cgd 			goto dropafterack;
   2218    1.1       cgd 		}
   2219    1.1       cgd 	}
   2220    1.1       cgd 
   2221    1.1       cgd step6:
   2222    1.1       cgd 	/*
   2223    1.1       cgd 	 * Update window information.
   2224    1.1       cgd 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
   2225    1.1       cgd 	 */
   2226   1.83    itojun 	if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) ||
   2227   1.83    itojun 	    (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) ||
   2228   1.83    itojun 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) {
   2229    1.1       cgd 		/* keep track of pure window updates */
   2230   1.83    itojun 		if (tlen == 0 &&
   2231   1.83    itojun 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
   2232    1.1       cgd 			tcpstat.tcps_rcvwinupd++;
   2233    1.9   mycroft 		tp->snd_wnd = tiwin;
   2234   1.83    itojun 		tp->snd_wl1 = th->th_seq;
   2235   1.83    itojun 		tp->snd_wl2 = th->th_ack;
   2236    1.1       cgd 		if (tp->snd_wnd > tp->max_sndwnd)
   2237    1.1       cgd 			tp->max_sndwnd = tp->snd_wnd;
   2238    1.1       cgd 		needoutput = 1;
   2239    1.1       cgd 	}
   2240    1.1       cgd 
   2241    1.1       cgd 	/*
   2242    1.1       cgd 	 * Process segments with URG.
   2243    1.1       cgd 	 */
   2244   1.83    itojun 	if ((tiflags & TH_URG) && th->th_urp &&
   2245    1.1       cgd 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2246    1.1       cgd 		/*
   2247    1.1       cgd 		 * This is a kludge, but if we receive and accept
   2248    1.1       cgd 		 * random urgent pointers, we'll crash in
   2249    1.1       cgd 		 * soreceive.  It's hard to imagine someone
   2250    1.1       cgd 		 * actually wanting to send this much urgent data.
   2251    1.1       cgd 		 */
   2252   1.83    itojun 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
   2253   1.83    itojun 			th->th_urp = 0;			/* XXX */
   2254    1.1       cgd 			tiflags &= ~TH_URG;		/* XXX */
   2255    1.1       cgd 			goto dodata;			/* XXX */
   2256    1.1       cgd 		}
   2257    1.1       cgd 		/*
   2258    1.1       cgd 		 * If this segment advances the known urgent pointer,
   2259    1.1       cgd 		 * then mark the data stream.  This should not happen
   2260    1.1       cgd 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
   2261  1.143    itojun 		 * a FIN has been received from the remote side.
   2262    1.1       cgd 		 * In these states we ignore the URG.
   2263    1.1       cgd 		 *
   2264    1.1       cgd 		 * According to RFC961 (Assigned Protocols),
   2265    1.1       cgd 		 * the urgent pointer points to the last octet
   2266    1.1       cgd 		 * of urgent data.  We continue, however,
   2267    1.1       cgd 		 * to consider it to indicate the first octet
   2268  1.143    itojun 		 * of data past the urgent section as the original
   2269    1.1       cgd 		 * spec states (in one of two places).
   2270    1.1       cgd 		 */
   2271   1.83    itojun 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
   2272   1.83    itojun 			tp->rcv_up = th->th_seq + th->th_urp;
   2273    1.1       cgd 			so->so_oobmark = so->so_rcv.sb_cc +
   2274    1.1       cgd 			    (tp->rcv_up - tp->rcv_nxt) - 1;
   2275    1.1       cgd 			if (so->so_oobmark == 0)
   2276    1.1       cgd 				so->so_state |= SS_RCVATMARK;
   2277    1.1       cgd 			sohasoutofband(so);
   2278    1.1       cgd 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
   2279    1.1       cgd 		}
   2280    1.1       cgd 		/*
   2281    1.1       cgd 		 * Remove out of band data so doesn't get presented to user.
   2282    1.1       cgd 		 * This can happen independent of advancing the URG pointer,
   2283    1.1       cgd 		 * but if two URG's are pending at once, some out-of-band
   2284    1.1       cgd 		 * data may creep in... ick.
   2285    1.1       cgd 		 */
   2286   1.83    itojun 		if (th->th_urp <= (u_int16_t) tlen
   2287    1.1       cgd #ifdef SO_OOBINLINE
   2288    1.1       cgd 		     && (so->so_options & SO_OOBINLINE) == 0
   2289    1.1       cgd #endif
   2290    1.1       cgd 		     )
   2291   1.97    itojun 			tcp_pulloutofband(so, th, m, hdroptlen);
   2292    1.1       cgd 	} else
   2293    1.1       cgd 		/*
   2294    1.1       cgd 		 * If no out of band data is expected,
   2295    1.1       cgd 		 * pull receive urgent pointer along
   2296    1.1       cgd 		 * with the receive window.
   2297    1.1       cgd 		 */
   2298    1.1       cgd 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
   2299    1.1       cgd 			tp->rcv_up = tp->rcv_nxt;
   2300    1.1       cgd dodata:							/* XXX */
   2301    1.1       cgd 
   2302    1.1       cgd 	/*
   2303    1.1       cgd 	 * Process the segment text, merging it into the TCP sequencing queue,
   2304   1.95    simonb 	 * and arranging for acknowledgement of receipt if necessary.
   2305    1.1       cgd 	 * This process logically involves adjusting tp->rcv_wnd as data
   2306    1.1       cgd 	 * is presented to the user (this happens in tcp_usrreq.c,
   2307    1.1       cgd 	 * case PRU_RCVD).  If a FIN has already been received on this
   2308    1.1       cgd 	 * connection then we just ignore the text.
   2309    1.1       cgd 	 */
   2310   1.83    itojun 	if ((tlen || (tiflags & TH_FIN)) &&
   2311    1.1       cgd 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2312   1.83    itojun 		/*
   2313   1.83    itojun 		 * Insert segment ti into reassembly queue of tcp with
   2314   1.83    itojun 		 * control block tp.  Return TH_FIN if reassembly now includes
   2315   1.83    itojun 		 * a segment with FIN.  The macro form does the common case
   2316   1.83    itojun 		 * inline (segment is the next to be received on an
   2317   1.83    itojun 		 * established connection, and the queue is empty),
   2318   1.83    itojun 		 * avoiding linkage into and removal from the queue and
   2319   1.83    itojun 		 * repetition of various conversions.
   2320   1.83    itojun 		 * Set DELACK for segments received in order, but ack
   2321   1.83    itojun 		 * immediately when segments are out of order
   2322   1.83    itojun 		 * (so fast retransmit can work).
   2323   1.83    itojun 		 */
   2324   1.83    itojun 		/* NOTE: this was TCP_REASS() macro, but used only once */
   2325   1.83    itojun 		TCP_REASS_LOCK(tp);
   2326   1.83    itojun 		if (th->th_seq == tp->rcv_nxt &&
   2327  1.141      matt 		    TAILQ_FIRST(&tp->segq) == NULL &&
   2328   1.83    itojun 		    tp->t_state == TCPS_ESTABLISHED) {
   2329   1.83    itojun 			TCP_SETUP_ACK(tp, th);
   2330   1.83    itojun 			tp->rcv_nxt += tlen;
   2331   1.83    itojun 			tiflags = th->th_flags & TH_FIN;
   2332   1.97    itojun 			tcpstat.tcps_rcvpack++;
   2333   1.97    itojun 			tcpstat.tcps_rcvbyte += tlen;
   2334   1.98    itojun 			ND6_HINT(tp);
   2335  1.154    itojun 			if (so->so_state & SS_CANTRCVMORE)
   2336  1.154    itojun 				m_freem(m);
   2337  1.154    itojun 			else {
   2338  1.154    itojun 				m_adj(m, hdroptlen);
   2339  1.154    itojun 				sbappendstream(&(so)->so_rcv, m);
   2340  1.154    itojun 			}
   2341   1.83    itojun 			sorwakeup(so);
   2342   1.83    itojun 		} else {
   2343   1.97    itojun 			m_adj(m, hdroptlen);
   2344   1.83    itojun 			tiflags = tcp_reass(tp, th, m, &tlen);
   2345   1.83    itojun 			tp->t_flags |= TF_ACKNOW;
   2346   1.83    itojun 		}
   2347   1.83    itojun 		TCP_REASS_UNLOCK(tp);
   2348   1.83    itojun 
   2349    1.1       cgd 		/*
   2350    1.1       cgd 		 * Note the amount of data that peer has sent into
   2351    1.1       cgd 		 * our window, in order to estimate the sender's
   2352    1.1       cgd 		 * buffer size.
   2353    1.1       cgd 		 */
   2354    1.1       cgd 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
   2355    1.1       cgd 	} else {
   2356    1.1       cgd 		m_freem(m);
   2357   1.83    itojun 		m = NULL;
   2358    1.1       cgd 		tiflags &= ~TH_FIN;
   2359    1.1       cgd 	}
   2360    1.1       cgd 
   2361    1.1       cgd 	/*
   2362    1.1       cgd 	 * If FIN is received ACK the FIN and let the user know
   2363   1.22   mycroft 	 * that the connection is closing.  Ignore a FIN received before
   2364   1.22   mycroft 	 * the connection is fully established.
   2365    1.1       cgd 	 */
   2366   1.22   mycroft 	if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
   2367    1.1       cgd 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2368    1.1       cgd 			socantrcvmore(so);
   2369    1.1       cgd 			tp->t_flags |= TF_ACKNOW;
   2370    1.1       cgd 			tp->rcv_nxt++;
   2371    1.1       cgd 		}
   2372    1.1       cgd 		switch (tp->t_state) {
   2373    1.1       cgd 
   2374    1.1       cgd 	 	/*
   2375   1.22   mycroft 		 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
   2376    1.1       cgd 		 */
   2377    1.1       cgd 		case TCPS_ESTABLISHED:
   2378    1.1       cgd 			tp->t_state = TCPS_CLOSE_WAIT;
   2379    1.1       cgd 			break;
   2380    1.1       cgd 
   2381    1.1       cgd 	 	/*
   2382    1.1       cgd 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
   2383    1.1       cgd 		 * enter the CLOSING state.
   2384    1.1       cgd 		 */
   2385    1.1       cgd 		case TCPS_FIN_WAIT_1:
   2386    1.1       cgd 			tp->t_state = TCPS_CLOSING;
   2387    1.1       cgd 			break;
   2388    1.1       cgd 
   2389    1.1       cgd 	 	/*
   2390    1.1       cgd 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
   2391  1.143    itojun 		 * starting the time-wait timer, turning off the other
   2392    1.1       cgd 		 * standard timers.
   2393    1.1       cgd 		 */
   2394    1.1       cgd 		case TCPS_FIN_WAIT_2:
   2395    1.1       cgd 			tp->t_state = TCPS_TIME_WAIT;
   2396    1.1       cgd 			tcp_canceltimers(tp);
   2397   1.58   thorpej 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2398    1.1       cgd 			soisdisconnected(so);
   2399    1.1       cgd 			break;
   2400    1.1       cgd 
   2401    1.1       cgd 		/*
   2402    1.1       cgd 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
   2403    1.1       cgd 		 */
   2404    1.1       cgd 		case TCPS_TIME_WAIT:
   2405   1.58   thorpej 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2406    1.1       cgd 			break;
   2407    1.1       cgd 		}
   2408    1.1       cgd 	}
   2409  1.127       abs #ifdef TCP_DEBUG
   2410  1.127       abs 	if (so->so_options & SO_DEBUG)
   2411   1.83    itojun 		tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0);
   2412  1.127       abs #endif
   2413    1.1       cgd 
   2414    1.1       cgd 	/*
   2415    1.1       cgd 	 * Return any desired output.
   2416    1.1       cgd 	 */
   2417    1.1       cgd 	if (needoutput || (tp->t_flags & TF_ACKNOW))
   2418    1.1       cgd 		(void) tcp_output(tp);
   2419   1.89    itojun 	if (tcp_saveti)
   2420   1.89    itojun 		m_freem(tcp_saveti);
   2421    1.1       cgd 	return;
   2422   1.35   thorpej 
   2423   1.35   thorpej badsyn:
   2424   1.35   thorpej 	/*
   2425   1.35   thorpej 	 * Received a bad SYN.  Increment counters and dropwithreset.
   2426   1.35   thorpej 	 */
   2427   1.35   thorpej 	tcpstat.tcps_badsyn++;
   2428   1.35   thorpej 	tp = NULL;
   2429   1.35   thorpej 	goto dropwithreset;
   2430    1.1       cgd 
   2431    1.1       cgd dropafterack:
   2432    1.1       cgd 	/*
   2433    1.1       cgd 	 * Generate an ACK dropping incoming segment if it occupies
   2434    1.1       cgd 	 * sequence space, where the ACK reflects our state.
   2435    1.1       cgd 	 */
   2436    1.1       cgd 	if (tiflags & TH_RST)
   2437    1.1       cgd 		goto drop;
   2438    1.1       cgd 	m_freem(m);
   2439    1.1       cgd 	tp->t_flags |= TF_ACKNOW;
   2440    1.1       cgd 	(void) tcp_output(tp);
   2441   1.89    itojun 	if (tcp_saveti)
   2442   1.89    itojun 		m_freem(tcp_saveti);
   2443    1.1       cgd 	return;
   2444    1.1       cgd 
   2445  1.104   thorpej dropwithreset_ratelim:
   2446  1.104   thorpej 	/*
   2447  1.104   thorpej 	 * We may want to rate-limit RSTs in certain situations,
   2448  1.104   thorpej 	 * particularly if we are sending an RST in response to
   2449  1.104   thorpej 	 * an attempt to connect to or otherwise communicate with
   2450  1.104   thorpej 	 * a port for which we have no socket.
   2451  1.104   thorpej 	 */
   2452  1.116    itojun 	if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count,
   2453  1.116    itojun 	    tcp_rst_ppslim) == 0) {
   2454  1.104   thorpej 		/* XXX stat */
   2455  1.104   thorpej 		goto drop;
   2456  1.104   thorpej 	}
   2457  1.104   thorpej 	/* ...fall into dropwithreset... */
   2458  1.104   thorpej 
   2459    1.1       cgd dropwithreset:
   2460    1.1       cgd 	/*
   2461    1.1       cgd 	 * Generate a RST, dropping incoming segment.
   2462    1.1       cgd 	 * Make ACK acceptable to originator of segment.
   2463    1.1       cgd 	 */
   2464  1.103   thorpej 	if (tiflags & TH_RST)
   2465  1.137  christos 		goto drop;
   2466  1.140  christos 
   2467  1.140  christos 	switch (af) {
   2468  1.140  christos #ifdef INET6
   2469  1.140  christos 	case AF_INET6:
   2470  1.140  christos 		/* For following calls to tcp_respond */
   2471  1.140  christos 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
   2472  1.140  christos 			goto drop;
   2473  1.140  christos 		break;
   2474  1.140  christos #endif /* INET6 */
   2475  1.140  christos 	case AF_INET:
   2476  1.140  christos 		if (IN_MULTICAST(ip->ip_dst.s_addr) ||
   2477  1.140  christos 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
   2478  1.140  christos 			goto drop;
   2479  1.140  christos 	}
   2480  1.137  christos 
   2481   1.83    itojun     {
   2482   1.83    itojun 	/*
   2483   1.83    itojun 	 * need to recover version # field, which was overwritten on
   2484   1.83    itojun 	 * ip_cksum computation.
   2485   1.83    itojun 	 */
   2486   1.83    itojun 	struct ip *sip;
   2487   1.83    itojun 	sip = mtod(m, struct ip *);
   2488   1.83    itojun 	switch (af) {
   2489  1.119    itojun #ifdef INET
   2490   1.83    itojun 	case AF_INET:
   2491   1.83    itojun 		sip->ip_v = 4;
   2492   1.83    itojun 		break;
   2493  1.119    itojun #endif
   2494   1.83    itojun #ifdef INET6
   2495   1.83    itojun 	case AF_INET6:
   2496   1.83    itojun 		sip->ip_v = 6;
   2497   1.83    itojun 		break;
   2498   1.83    itojun #endif
   2499   1.83    itojun 	}
   2500   1.83    itojun     }
   2501    1.1       cgd 	if (tiflags & TH_ACK)
   2502   1.86    itojun 		(void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
   2503    1.1       cgd 	else {
   2504    1.1       cgd 		if (tiflags & TH_SYN)
   2505   1.83    itojun 			tlen++;
   2506   1.86    itojun 		(void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0,
   2507    1.1       cgd 		    TH_RST|TH_ACK);
   2508    1.1       cgd 	}
   2509   1.89    itojun 	if (tcp_saveti)
   2510   1.89    itojun 		m_freem(tcp_saveti);
   2511    1.1       cgd 	return;
   2512    1.1       cgd 
   2513  1.125   thorpej badcsum:
   2514  1.125   thorpej 	tcpstat.tcps_rcvbadsum++;
   2515    1.1       cgd drop:
   2516    1.1       cgd 	/*
   2517    1.1       cgd 	 * Drop space held by incoming segment and return.
   2518    1.1       cgd 	 */
   2519   1.83    itojun 	if (tp) {
   2520   1.83    itojun 		if (tp->t_inpcb)
   2521   1.83    itojun 			so = tp->t_inpcb->inp_socket;
   2522   1.83    itojun #ifdef INET6
   2523   1.83    itojun 		else if (tp->t_in6pcb)
   2524   1.83    itojun 			so = tp->t_in6pcb->in6p_socket;
   2525   1.83    itojun #endif
   2526   1.83    itojun 		else
   2527   1.83    itojun 			so = NULL;
   2528  1.127       abs #ifdef TCP_DEBUG
   2529   1.89    itojun 		if (so && (so->so_options & SO_DEBUG) != 0)
   2530   1.83    itojun 			tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0);
   2531  1.127       abs #endif
   2532   1.83    itojun 	}
   2533   1.89    itojun 	if (tcp_saveti)
   2534   1.89    itojun 		m_freem(tcp_saveti);
   2535    1.1       cgd 	m_freem(m);
   2536    1.1       cgd 	return;
   2537    1.1       cgd }
   2538    1.1       cgd 
   2539    1.5   mycroft void
   2540   1.83    itojun tcp_dooptions(tp, cp, cnt, th, oi)
   2541    1.1       cgd 	struct tcpcb *tp;
   2542    1.9   mycroft 	u_char *cp;
   2543    1.9   mycroft 	int cnt;
   2544   1.83    itojun 	struct tcphdr *th;
   2545   1.29   thorpej 	struct tcp_opt_info *oi;
   2546    1.1       cgd {
   2547   1.12       cgd 	u_int16_t mss;
   2548    1.9   mycroft 	int opt, optlen;
   2549    1.1       cgd 
   2550    1.1       cgd 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   2551    1.1       cgd 		opt = cp[0];
   2552    1.1       cgd 		if (opt == TCPOPT_EOL)
   2553    1.1       cgd 			break;
   2554    1.1       cgd 		if (opt == TCPOPT_NOP)
   2555    1.1       cgd 			optlen = 1;
   2556    1.1       cgd 		else {
   2557  1.113    itojun 			if (cnt < 2)
   2558  1.113    itojun 				break;
   2559    1.1       cgd 			optlen = cp[1];
   2560  1.113    itojun 			if (optlen < 2 || optlen > cnt)
   2561    1.1       cgd 				break;
   2562    1.1       cgd 		}
   2563    1.1       cgd 		switch (opt) {
   2564    1.1       cgd 
   2565    1.1       cgd 		default:
   2566    1.1       cgd 			continue;
   2567    1.1       cgd 
   2568    1.1       cgd 		case TCPOPT_MAXSEG:
   2569    1.9   mycroft 			if (optlen != TCPOLEN_MAXSEG)
   2570    1.1       cgd 				continue;
   2571   1.83    itojun 			if (!(th->th_flags & TH_SYN))
   2572    1.1       cgd 				continue;
   2573   1.29   thorpej 			bcopy(cp + 2, &mss, sizeof(mss));
   2574   1.29   thorpej 			oi->maxseg = ntohs(mss);
   2575    1.1       cgd 			break;
   2576    1.9   mycroft 
   2577    1.9   mycroft 		case TCPOPT_WINDOW:
   2578    1.9   mycroft 			if (optlen != TCPOLEN_WINDOW)
   2579    1.9   mycroft 				continue;
   2580   1.83    itojun 			if (!(th->th_flags & TH_SYN))
   2581    1.9   mycroft 				continue;
   2582    1.9   mycroft 			tp->t_flags |= TF_RCVD_SCALE;
   2583   1.52   thorpej 			tp->requested_s_scale = cp[2];
   2584   1.52   thorpej 			if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
   2585   1.83    itojun #if 0	/*XXX*/
   2586   1.83    itojun 				char *p;
   2587   1.83    itojun 
   2588   1.83    itojun 				if (ip)
   2589   1.83    itojun 					p = ntohl(ip->ip_src);
   2590   1.83    itojun #ifdef INET6
   2591   1.83    itojun 				else if (ip6)
   2592   1.83    itojun 					p = ip6_sprintf(&ip6->ip6_src);
   2593   1.83    itojun #endif
   2594   1.83    itojun 				else
   2595   1.83    itojun 					p = "(unknown)";
   2596   1.83    itojun 				log(LOG_ERR, "TCP: invalid wscale %d from %s, "
   2597   1.83    itojun 				    "assuming %d\n",
   2598   1.83    itojun 				    tp->requested_s_scale, p,
   2599   1.83    itojun 				    TCP_MAX_WINSHIFT);
   2600   1.83    itojun #else
   2601   1.83    itojun 				log(LOG_ERR, "TCP: invalid wscale %d, "
   2602   1.83    itojun 				    "assuming %d\n",
   2603   1.52   thorpej 				    tp->requested_s_scale,
   2604   1.52   thorpej 				    TCP_MAX_WINSHIFT);
   2605   1.83    itojun #endif
   2606   1.52   thorpej 				tp->requested_s_scale = TCP_MAX_WINSHIFT;
   2607   1.52   thorpej 			}
   2608    1.9   mycroft 			break;
   2609    1.9   mycroft 
   2610    1.9   mycroft 		case TCPOPT_TIMESTAMP:
   2611    1.9   mycroft 			if (optlen != TCPOLEN_TIMESTAMP)
   2612    1.9   mycroft 				continue;
   2613   1.32   thorpej 			oi->ts_present = 1;
   2614   1.29   thorpej 			bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
   2615   1.29   thorpej 			NTOHL(oi->ts_val);
   2616   1.29   thorpej 			bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
   2617   1.29   thorpej 			NTOHL(oi->ts_ecr);
   2618    1.9   mycroft 
   2619  1.143    itojun 			/*
   2620    1.9   mycroft 			 * A timestamp received in a SYN makes
   2621    1.9   mycroft 			 * it ok to send timestamp requests and replies.
   2622    1.9   mycroft 			 */
   2623   1.83    itojun 			if (th->th_flags & TH_SYN) {
   2624    1.9   mycroft 				tp->t_flags |= TF_RCVD_TSTMP;
   2625   1.29   thorpej 				tp->ts_recent = oi->ts_val;
   2626  1.123   thorpej 				tp->ts_recent_age = TCP_TIMESTAMP(tp);
   2627   1.54      matt 			}
   2628   1.54      matt 			break;
   2629   1.54      matt 		case TCPOPT_SACK_PERMITTED:
   2630   1.54      matt 			if (optlen != TCPOLEN_SACK_PERMITTED)
   2631   1.54      matt 				continue;
   2632   1.83    itojun 			if (!(th->th_flags & TH_SYN))
   2633   1.54      matt 				continue;
   2634   1.54      matt 			tp->t_flags &= ~TF_CANT_TXSACK;
   2635   1.54      matt 			break;
   2636   1.54      matt 
   2637   1.54      matt 		case TCPOPT_SACK:
   2638   1.54      matt 			if (tp->t_flags & TF_IGNR_RXSACK)
   2639   1.54      matt 				continue;
   2640   1.54      matt 			if (optlen % 8 != 2 || optlen < 10)
   2641   1.54      matt 				continue;
   2642   1.54      matt 			cp += 2;
   2643   1.54      matt 			optlen -= 2;
   2644   1.54      matt 			for (; optlen > 0; cp -= 8, optlen -= 8) {
   2645   1.54      matt 				tcp_seq lwe, rwe;
   2646   1.54      matt 				bcopy((char *)cp, (char *) &lwe, sizeof(lwe));
   2647   1.54      matt 				NTOHL(lwe);
   2648   1.54      matt 				bcopy((char *)cp, (char *) &rwe, sizeof(rwe));
   2649   1.54      matt 				NTOHL(rwe);
   2650   1.54      matt 				/* tcp_mark_sacked(tp, lwe, rwe); */
   2651    1.9   mycroft 			}
   2652    1.9   mycroft 			break;
   2653    1.1       cgd 		}
   2654    1.1       cgd 	}
   2655    1.1       cgd }
   2656    1.1       cgd 
   2657    1.1       cgd /*
   2658    1.1       cgd  * Pull out of band byte out of a segment so
   2659    1.1       cgd  * it doesn't appear in the user's data queue.
   2660    1.1       cgd  * It is still reflected in the segment length for
   2661    1.1       cgd  * sequencing purposes.
   2662    1.1       cgd  */
   2663    1.5   mycroft void
   2664   1.97    itojun tcp_pulloutofband(so, th, m, off)
   2665    1.1       cgd 	struct socket *so;
   2666   1.83    itojun 	struct tcphdr *th;
   2667  1.106  augustss 	struct mbuf *m;
   2668   1.97    itojun 	int off;
   2669    1.1       cgd {
   2670   1.97    itojun 	int cnt = off + th->th_urp - 1;
   2671  1.143    itojun 
   2672    1.1       cgd 	while (cnt >= 0) {
   2673    1.1       cgd 		if (m->m_len > cnt) {
   2674    1.1       cgd 			char *cp = mtod(m, caddr_t) + cnt;
   2675    1.1       cgd 			struct tcpcb *tp = sototcpcb(so);
   2676    1.1       cgd 
   2677    1.1       cgd 			tp->t_iobc = *cp;
   2678    1.1       cgd 			tp->t_oobflags |= TCPOOB_HAVEDATA;
   2679    1.1       cgd 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
   2680    1.1       cgd 			m->m_len--;
   2681    1.1       cgd 			return;
   2682    1.1       cgd 		}
   2683    1.1       cgd 		cnt -= m->m_len;
   2684    1.1       cgd 		m = m->m_next;
   2685    1.1       cgd 		if (m == 0)
   2686    1.1       cgd 			break;
   2687    1.1       cgd 	}
   2688    1.1       cgd 	panic("tcp_pulloutofband");
   2689    1.1       cgd }
   2690    1.1       cgd 
   2691    1.1       cgd /*
   2692    1.1       cgd  * Collect new round-trip time estimate
   2693    1.1       cgd  * and update averages and current timeout.
   2694    1.1       cgd  */
   2695    1.5   mycroft void
   2696    1.9   mycroft tcp_xmit_timer(tp, rtt)
   2697  1.106  augustss 	struct tcpcb *tp;
   2698  1.128   thorpej 	uint32_t rtt;
   2699    1.1       cgd {
   2700  1.128   thorpej 	int32_t delta;
   2701    1.1       cgd 
   2702    1.1       cgd 	tcpstat.tcps_rttupdated++;
   2703    1.1       cgd 	if (tp->t_srtt != 0) {
   2704    1.1       cgd 		/*
   2705    1.1       cgd 		 * srtt is stored as fixed point with 3 bits after the
   2706    1.1       cgd 		 * binary point (i.e., scaled by 8).  The following magic
   2707    1.1       cgd 		 * is equivalent to the smoothing algorithm in rfc793 with
   2708    1.1       cgd 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
   2709    1.9   mycroft 		 * point).  Adjust rtt to origin 0.
   2710    1.1       cgd 		 */
   2711   1.16   mycroft 		delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
   2712    1.1       cgd 		if ((tp->t_srtt += delta) <= 0)
   2713   1.27   mycroft 			tp->t_srtt = 1 << 2;
   2714    1.1       cgd 		/*
   2715    1.1       cgd 		 * We accumulate a smoothed rtt variance (actually, a
   2716    1.1       cgd 		 * smoothed mean difference), then set the retransmit
   2717    1.1       cgd 		 * timer to smoothed rtt + 4 times the smoothed variance.
   2718    1.1       cgd 		 * rttvar is stored as fixed point with 2 bits after the
   2719    1.1       cgd 		 * binary point (scaled by 4).  The following is
   2720    1.1       cgd 		 * equivalent to rfc793 smoothing with an alpha of .75
   2721    1.1       cgd 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
   2722    1.1       cgd 		 * rfc793's wired-in beta.
   2723    1.1       cgd 		 */
   2724    1.1       cgd 		if (delta < 0)
   2725    1.1       cgd 			delta = -delta;
   2726    1.1       cgd 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
   2727    1.1       cgd 		if ((tp->t_rttvar += delta) <= 0)
   2728   1.27   mycroft 			tp->t_rttvar = 1 << 2;
   2729    1.1       cgd 	} else {
   2730  1.143    itojun 		/*
   2731    1.1       cgd 		 * No rtt measurement yet - use the unsmoothed rtt.
   2732    1.1       cgd 		 * Set the variance to half the rtt (so our first
   2733    1.9   mycroft 		 * retransmit happens at 3*rtt).
   2734    1.1       cgd 		 */
   2735   1.16   mycroft 		tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
   2736   1.16   mycroft 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
   2737    1.1       cgd 	}
   2738  1.128   thorpej 	tp->t_rtttime = 0;
   2739    1.1       cgd 	tp->t_rxtshift = 0;
   2740    1.1       cgd 
   2741    1.1       cgd 	/*
   2742    1.1       cgd 	 * the retransmit should happen at rtt + 4 * rttvar.
   2743    1.1       cgd 	 * Because of the way we do the smoothing, srtt and rttvar
   2744    1.1       cgd 	 * will each average +1/2 tick of bias.  When we compute
   2745    1.1       cgd 	 * the retransmit timer, we want 1/2 tick of rounding and
   2746    1.1       cgd 	 * 1 extra tick because of +-1/2 tick uncertainty in the
   2747    1.1       cgd 	 * firing of the timer.  The bias will give us exactly the
   2748    1.1       cgd 	 * 1.5 tick we need.  But, because the bias is
   2749    1.1       cgd 	 * statistical, we have to test that we don't drop below
   2750    1.1       cgd 	 * the minimum feasible timer (which is 2 ticks).
   2751    1.1       cgd 	 */
   2752  1.128   thorpej 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
   2753  1.128   thorpej 	    max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
   2754  1.143    itojun 
   2755    1.1       cgd 	/*
   2756    1.1       cgd 	 * We received an ack for a packet that wasn't retransmitted;
   2757    1.1       cgd 	 * it is probably safe to discard any error indications we've
   2758    1.1       cgd 	 * received recently.  This isn't quite right, but close enough
   2759    1.1       cgd 	 * for now (a route might have failed after we sent a segment,
   2760    1.1       cgd 	 * and the return path might not be symmetrical).
   2761    1.1       cgd 	 */
   2762    1.1       cgd 	tp->t_softerror = 0;
   2763    1.1       cgd }
   2764   1.68      matt 
   2765   1.68      matt /*
   2766   1.68      matt  * Checks for partial ack.  If partial ack arrives, force the retransmission
   2767   1.68      matt  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
   2768   1.91   thorpej  * 1.  By setting snd_nxt to th_ack, this forces retransmission timer to
   2769   1.68      matt  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
   2770   1.68      matt  */
   2771   1.68      matt int
   2772   1.83    itojun tcp_newreno(tp, th)
   2773   1.68      matt 	struct tcpcb *tp;
   2774   1.83    itojun 	struct tcphdr *th;
   2775   1.68      matt {
   2776   1.91   thorpej 	tcp_seq onxt = tp->snd_nxt;
   2777   1.91   thorpej 	u_long ocwnd = tp->snd_cwnd;
   2778   1.91   thorpej 
   2779   1.83    itojun 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
   2780   1.91   thorpej 		/*
   2781   1.91   thorpej 		 * snd_una has not yet been updated and the socket's send
   2782   1.91   thorpej 		 * buffer has not yet drained off the ACK'd data, so we
   2783   1.91   thorpej 		 * have to leave snd_una as it was to get the correct data
   2784   1.91   thorpej 		 * offset in tcp_output().
   2785   1.91   thorpej 		 */
   2786   1.68      matt 		TCP_TIMER_DISARM(tp, TCPT_REXMT);
   2787  1.128   thorpej 	        tp->t_rtttime = 0;
   2788   1.83    itojun 	        tp->snd_nxt = th->th_ack;
   2789   1.91   thorpej 		/*
   2790   1.91   thorpej 		 * Set snd_cwnd to one segment beyond ACK'd offset.  snd_una
   2791   1.91   thorpej 		 * is not yet updated when we're called.
   2792   1.91   thorpej 		 */
   2793   1.91   thorpej 		tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una);
   2794   1.68      matt 	        (void) tcp_output(tp);
   2795   1.68      matt 	        tp->snd_cwnd = ocwnd;
   2796   1.68      matt 	        if (SEQ_GT(onxt, tp->snd_nxt))
   2797   1.68      matt 	                tp->snd_nxt = onxt;
   2798   1.68      matt 	        /*
   2799   1.68      matt 	         * Partial window deflation.  Relies on fact that tp->snd_una
   2800   1.68      matt 	         * not updated yet.
   2801   1.68      matt 	         */
   2802   1.83    itojun 	        tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_segsz);
   2803   1.68      matt 	        return 1;
   2804   1.68      matt 	}
   2805   1.68      matt 	return 0;
   2806   1.68      matt }
   2807   1.68      matt 
   2808    1.1       cgd 
   2809    1.1       cgd /*
   2810   1.29   thorpej  * TCP compressed state engine.  Currently used to hold compressed
   2811   1.29   thorpej  * state for SYN_RECEIVED.
   2812   1.29   thorpej  */
   2813   1.29   thorpej 
   2814   1.29   thorpej u_long	syn_cache_count;
   2815   1.29   thorpej u_int32_t syn_hash1, syn_hash2;
   2816   1.29   thorpej 
   2817   1.29   thorpej #define SYN_HASH(sa, sp, dp) \
   2818   1.29   thorpej 	((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
   2819   1.49   thorpej 				     ((u_int32_t)(sp)))^syn_hash2)))
   2820   1.83    itojun #ifndef INET6
   2821   1.83    itojun #define	SYN_HASHALL(hash, src, dst) \
   2822   1.83    itojun do {									\
   2823   1.83    itojun 	hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr,	\
   2824   1.83    itojun 		((struct sockaddr_in *)(src))->sin_port,		\
   2825   1.83    itojun 		((struct sockaddr_in *)(dst))->sin_port);		\
   2826   1.83    itojun } while (0)
   2827   1.83    itojun #else
   2828   1.83    itojun #define SYN_HASH6(sa, sp, dp) \
   2829   1.83    itojun 	((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
   2830   1.83    itojun 	  (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
   2831   1.83    itojun 	 & 0x7fffffff)
   2832   1.83    itojun 
   2833   1.83    itojun #define SYN_HASHALL(hash, src, dst) \
   2834   1.83    itojun do {									\
   2835   1.83    itojun 	switch ((src)->sa_family) {					\
   2836   1.83    itojun 	case AF_INET:							\
   2837   1.83    itojun 		hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \
   2838   1.83    itojun 			((struct sockaddr_in *)(src))->sin_port,	\
   2839   1.83    itojun 			((struct sockaddr_in *)(dst))->sin_port);	\
   2840   1.83    itojun 		break;							\
   2841   1.83    itojun 	case AF_INET6:							\
   2842   1.83    itojun 		hash = SYN_HASH6(&((struct sockaddr_in6 *)(src))->sin6_addr, \
   2843   1.83    itojun 			((struct sockaddr_in6 *)(src))->sin6_port,	\
   2844   1.83    itojun 			((struct sockaddr_in6 *)(dst))->sin6_port);	\
   2845   1.83    itojun 		break;							\
   2846   1.83    itojun 	default:							\
   2847   1.83    itojun 		hash = 0;						\
   2848   1.83    itojun 	}								\
   2849  1.130   thorpej } while (/*CONSTCOND*/0)
   2850   1.83    itojun #endif /* INET6 */
   2851   1.29   thorpej 
   2852   1.80   thorpej #define	SYN_CACHE_RM(sc)						\
   2853   1.59   thorpej do {									\
   2854  1.130   thorpej 	TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket,	\
   2855  1.130   thorpej 	    (sc), sc_bucketq);						\
   2856   1.93    itojun 	(sc)->sc_tp = NULL;						\
   2857   1.93    itojun 	LIST_REMOVE((sc), sc_tpq);					\
   2858   1.80   thorpej 	tcp_syn_cache[(sc)->sc_bucketidx].sch_length--;			\
   2859  1.130   thorpej 	callout_stop(&(sc)->sc_timer);					\
   2860   1.29   thorpej 	syn_cache_count--;						\
   2861  1.130   thorpej } while (/*CONSTCOND*/0)
   2862   1.59   thorpej 
   2863   1.75   thorpej #define	SYN_CACHE_PUT(sc)						\
   2864   1.75   thorpej do {									\
   2865   1.75   thorpej 	if ((sc)->sc_ipopts)						\
   2866   1.75   thorpej 		(void) m_free((sc)->sc_ipopts);				\
   2867   1.83    itojun 	if ((sc)->sc_route4.ro_rt != NULL)				\
   2868   1.83    itojun 		RTFREE((sc)->sc_route4.ro_rt);				\
   2869   1.75   thorpej 	pool_put(&syn_cache_pool, (sc));				\
   2870  1.130   thorpej } while (/*CONSTCOND*/0)
   2871   1.75   thorpej 
   2872   1.63   thorpej struct pool syn_cache_pool;
   2873   1.63   thorpej 
   2874   1.80   thorpej /*
   2875   1.80   thorpej  * We don't estimate RTT with SYNs, so each packet starts with the default
   2876  1.130   thorpej  * RTT and each timer step has a fixed timeout value.
   2877   1.80   thorpej  */
   2878   1.80   thorpej #define	SYN_CACHE_TIMER_ARM(sc)						\
   2879   1.80   thorpej do {									\
   2880   1.80   thorpej 	TCPT_RANGESET((sc)->sc_rxtcur,					\
   2881   1.80   thorpej 	    TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN,	\
   2882   1.80   thorpej 	    TCPTV_REXMTMAX);						\
   2883  1.130   thorpej 	callout_reset(&(sc)->sc_timer,					\
   2884  1.130   thorpej 	    (sc)->sc_rxtcur * (hz / PR_SLOWHZ), syn_cache_timer, (sc));	\
   2885  1.130   thorpej } while (/*CONSTCOND*/0)
   2886   1.80   thorpej 
   2887  1.123   thorpej #define	SYN_CACHE_TIMESTAMP(sc)	(tcp_now - (sc)->sc_timebase)
   2888  1.123   thorpej 
   2889   1.59   thorpej void
   2890   1.59   thorpej syn_cache_init()
   2891   1.59   thorpej {
   2892   1.59   thorpej 	int i;
   2893   1.59   thorpej 
   2894   1.80   thorpej 	/* Initialize the hash buckets. */
   2895   1.59   thorpej 	for (i = 0; i < tcp_syn_cache_size; i++)
   2896  1.130   thorpej 		TAILQ_INIT(&tcp_syn_cache[i].sch_bucket);
   2897   1.63   thorpej 
   2898   1.63   thorpej 	/* Initialize the syn cache pool. */
   2899   1.63   thorpej 	pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
   2900  1.135   thorpej 	    "synpl", NULL);
   2901   1.29   thorpej }
   2902   1.29   thorpej 
   2903   1.29   thorpej void
   2904   1.93    itojun syn_cache_insert(sc, tp)
   2905   1.29   thorpej 	struct syn_cache *sc;
   2906   1.93    itojun 	struct tcpcb *tp;
   2907   1.29   thorpej {
   2908   1.80   thorpej 	struct syn_cache_head *scp;
   2909   1.29   thorpej 	struct syn_cache *sc2;
   2910  1.130   thorpej 	int s;
   2911   1.29   thorpej 
   2912   1.59   thorpej 	/*
   2913   1.59   thorpej 	 * If there are no entries in the hash table, reinitialize
   2914   1.59   thorpej 	 * the hash secrets.
   2915   1.59   thorpej 	 */
   2916   1.29   thorpej 	if (syn_cache_count == 0) {
   2917   1.29   thorpej 		struct timeval tv;
   2918   1.29   thorpej 		microtime(&tv);
   2919  1.142    itojun 		syn_hash1 = arc4random() ^ (u_long)&sc;
   2920  1.142    itojun 		syn_hash2 = arc4random() ^ tv.tv_usec;
   2921   1.29   thorpej 	}
   2922   1.29   thorpej 
   2923   1.83    itojun 	SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
   2924   1.80   thorpej 	sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
   2925   1.80   thorpej 	scp = &tcp_syn_cache[sc->sc_bucketidx];
   2926   1.29   thorpej 
   2927   1.29   thorpej 	/*
   2928   1.29   thorpej 	 * Make sure that we don't overflow the per-bucket
   2929   1.29   thorpej 	 * limit or the total cache size limit.
   2930   1.29   thorpej 	 */
   2931   1.29   thorpej 	s = splsoftnet();
   2932   1.29   thorpej 	if (scp->sch_length >= tcp_syn_bucket_limit) {
   2933   1.29   thorpej 		tcpstat.tcps_sc_bucketoverflow++;
   2934   1.59   thorpej 		/*
   2935   1.80   thorpej 		 * The bucket is full.  Toss the oldest element in the
   2936  1.130   thorpej 		 * bucket.  This will be the first entry in the bucket.
   2937  1.130   thorpej 		 */
   2938  1.130   thorpej 		sc2 = TAILQ_FIRST(&scp->sch_bucket);
   2939   1.80   thorpej #ifdef DIAGNOSTIC
   2940   1.80   thorpej 		/*
   2941   1.80   thorpej 		 * This should never happen; we should always find an
   2942   1.80   thorpej 		 * entry in our bucket.
   2943   1.59   thorpej 		 */
   2944  1.130   thorpej 		if (sc2 == NULL)
   2945  1.130   thorpej 			panic("syn_cache_insert: bucketoverflow: impossible");
   2946   1.80   thorpej #endif
   2947  1.130   thorpej 		SYN_CACHE_RM(sc2);
   2948  1.130   thorpej 		SYN_CACHE_PUT(sc2);
   2949   1.29   thorpej 	} else if (syn_cache_count >= tcp_syn_cache_limit) {
   2950  1.130   thorpej 		struct syn_cache_head *scp2, *sce;
   2951  1.130   thorpej 
   2952   1.29   thorpej 		tcpstat.tcps_sc_overflowed++;
   2953   1.29   thorpej 		/*
   2954   1.80   thorpej 		 * The cache is full.  Toss the oldest entry in the
   2955  1.130   thorpej 		 * first non-empty bucket we can find.
   2956  1.130   thorpej 		 *
   2957  1.130   thorpej 		 * XXX We would really like to toss the oldest
   2958  1.130   thorpej 		 * entry in the cache, but we hope that this
   2959  1.130   thorpej 		 * condition doesn't happen very often.
   2960  1.130   thorpej 		 */
   2961  1.130   thorpej 		scp2 = scp;
   2962  1.130   thorpej 		if (TAILQ_EMPTY(&scp2->sch_bucket)) {
   2963  1.130   thorpej 			sce = &tcp_syn_cache[tcp_syn_cache_size];
   2964  1.130   thorpej 			for (++scp2; scp2 != scp; scp2++) {
   2965  1.130   thorpej 				if (scp2 >= sce)
   2966  1.130   thorpej 					scp2 = &tcp_syn_cache[0];
   2967  1.130   thorpej 				if (! TAILQ_EMPTY(&scp2->sch_bucket))
   2968  1.130   thorpej 					break;
   2969  1.130   thorpej 			}
   2970   1.80   thorpej #ifdef DIAGNOSTIC
   2971  1.130   thorpej 			/*
   2972  1.130   thorpej 			 * This should never happen; we should always find a
   2973  1.130   thorpej 			 * non-empty bucket.
   2974  1.130   thorpej 			 */
   2975  1.130   thorpej 			if (scp2 == scp)
   2976  1.130   thorpej 				panic("syn_cache_insert: cacheoverflow: "
   2977  1.130   thorpej 				    "impossible");
   2978   1.80   thorpej #endif
   2979  1.130   thorpej 		}
   2980  1.130   thorpej 		sc2 = TAILQ_FIRST(&scp2->sch_bucket);
   2981  1.130   thorpej 		SYN_CACHE_RM(sc2);
   2982  1.130   thorpej 		SYN_CACHE_PUT(sc2);
   2983   1.29   thorpej 	}
   2984   1.29   thorpej 
   2985   1.80   thorpej 	/*
   2986   1.80   thorpej 	 * Initialize the entry's timer.
   2987   1.80   thorpej 	 */
   2988   1.80   thorpej 	sc->sc_rxttot = 0;
   2989   1.80   thorpej 	sc->sc_rxtshift = 0;
   2990   1.80   thorpej 	SYN_CACHE_TIMER_ARM(sc);
   2991   1.59   thorpej 
   2992   1.93    itojun 	/* Link it from tcpcb entry */
   2993   1.93    itojun 	LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
   2994   1.93    itojun 
   2995   1.59   thorpej 	/* Put it into the bucket. */
   2996  1.130   thorpej 	TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq);
   2997   1.80   thorpej 	scp->sch_length++;
   2998   1.59   thorpej 	syn_cache_count++;
   2999   1.29   thorpej 
   3000   1.59   thorpej 	tcpstat.tcps_sc_added++;
   3001   1.29   thorpej 	splx(s);
   3002   1.29   thorpej }
   3003   1.29   thorpej 
   3004   1.29   thorpej /*
   3005   1.80   thorpej  * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
   3006   1.80   thorpej  * If we have retransmitted an entry the maximum number of times, expire
   3007   1.80   thorpej  * that entry.
   3008   1.29   thorpej  */
   3009   1.29   thorpej void
   3010  1.130   thorpej syn_cache_timer(void *arg)
   3011   1.29   thorpej {
   3012  1.130   thorpej 	struct syn_cache *sc = arg;
   3013  1.130   thorpej 	int s;
   3014   1.29   thorpej 
   3015   1.29   thorpej 	s = splsoftnet();
   3016   1.80   thorpej 
   3017  1.130   thorpej 	if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) {
   3018  1.130   thorpej 		/* Drop it -- too many retransmissions. */
   3019  1.130   thorpej 		goto dropit;
   3020  1.130   thorpej 	}
   3021  1.130   thorpej 
   3022   1.80   thorpej 	/*
   3023  1.130   thorpej 	 * Compute the total amount of time this entry has
   3024  1.130   thorpej 	 * been on a queue.  If this entry has been on longer
   3025  1.130   thorpej 	 * than the keep alive timer would allow, expire it.
   3026   1.80   thorpej 	 */
   3027  1.130   thorpej 	sc->sc_rxttot += sc->sc_rxtcur;
   3028  1.130   thorpej 	if (sc->sc_rxttot >= TCPTV_KEEP_INIT)
   3029  1.130   thorpej 		goto dropit;
   3030   1.80   thorpej 
   3031  1.130   thorpej 	tcpstat.tcps_sc_retransmitted++;
   3032  1.130   thorpej 	(void) syn_cache_respond(sc, NULL);
   3033   1.80   thorpej 
   3034  1.130   thorpej 	/* Advance the timer back-off. */
   3035  1.130   thorpej 	sc->sc_rxtshift++;
   3036  1.130   thorpej 	SYN_CACHE_TIMER_ARM(sc);
   3037   1.80   thorpej 
   3038  1.130   thorpej 	splx(s);
   3039  1.130   thorpej 	return;
   3040   1.80   thorpej 
   3041  1.130   thorpej  dropit:
   3042  1.130   thorpej 	tcpstat.tcps_sc_timed_out++;
   3043  1.130   thorpej 	SYN_CACHE_RM(sc);
   3044  1.130   thorpej 	SYN_CACHE_PUT(sc);
   3045   1.29   thorpej 	splx(s);
   3046   1.29   thorpej }
   3047   1.29   thorpej 
   3048   1.29   thorpej /*
   3049   1.93    itojun  * Remove syn cache created by the specified tcb entry,
   3050   1.93    itojun  * because this does not make sense to keep them
   3051   1.93    itojun  * (if there's no tcb entry, syn cache entry will never be used)
   3052   1.93    itojun  */
   3053   1.93    itojun void
   3054   1.93    itojun syn_cache_cleanup(tp)
   3055   1.93    itojun 	struct tcpcb *tp;
   3056   1.93    itojun {
   3057   1.93    itojun 	struct syn_cache *sc, *nsc;
   3058   1.93    itojun 	int s;
   3059   1.93    itojun 
   3060   1.93    itojun 	s = splsoftnet();
   3061   1.93    itojun 
   3062   1.93    itojun 	for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
   3063   1.93    itojun 		nsc = LIST_NEXT(sc, sc_tpq);
   3064   1.93    itojun 
   3065   1.93    itojun #ifdef DIAGNOSTIC
   3066   1.93    itojun 		if (sc->sc_tp != tp)
   3067   1.93    itojun 			panic("invalid sc_tp in syn_cache_cleanup");
   3068   1.93    itojun #endif
   3069   1.93    itojun 		SYN_CACHE_RM(sc);
   3070   1.93    itojun 		SYN_CACHE_PUT(sc);
   3071   1.93    itojun 	}
   3072   1.93    itojun 	/* just for safety */
   3073   1.93    itojun 	LIST_INIT(&tp->t_sc);
   3074   1.93    itojun 
   3075   1.93    itojun 	splx(s);
   3076   1.93    itojun }
   3077   1.93    itojun 
   3078   1.93    itojun /*
   3079   1.29   thorpej  * Find an entry in the syn cache.
   3080   1.29   thorpej  */
   3081   1.29   thorpej struct syn_cache *
   3082   1.83    itojun syn_cache_lookup(src, dst, headp)
   3083   1.83    itojun 	struct sockaddr *src;
   3084   1.83    itojun 	struct sockaddr *dst;
   3085   1.29   thorpej 	struct syn_cache_head **headp;
   3086   1.29   thorpej {
   3087   1.59   thorpej 	struct syn_cache *sc;
   3088   1.59   thorpej 	struct syn_cache_head *scp;
   3089   1.29   thorpej 	u_int32_t hash;
   3090   1.29   thorpej 	int s;
   3091   1.29   thorpej 
   3092   1.83    itojun 	SYN_HASHALL(hash, src, dst);
   3093   1.29   thorpej 
   3094   1.59   thorpej 	scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
   3095   1.59   thorpej 	*headp = scp;
   3096   1.29   thorpej 	s = splsoftnet();
   3097  1.130   thorpej 	for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL;
   3098  1.130   thorpej 	     sc = TAILQ_NEXT(sc, sc_bucketq)) {
   3099   1.29   thorpej 		if (sc->sc_hash != hash)
   3100   1.29   thorpej 			continue;
   3101   1.83    itojun 		if (!bcmp(&sc->sc_src, src, src->sa_len) &&
   3102   1.83    itojun 		    !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
   3103   1.29   thorpej 			splx(s);
   3104   1.29   thorpej 			return (sc);
   3105   1.29   thorpej 		}
   3106   1.29   thorpej 	}
   3107   1.29   thorpej 	splx(s);
   3108   1.29   thorpej 	return (NULL);
   3109   1.29   thorpej }
   3110   1.29   thorpej 
   3111   1.29   thorpej /*
   3112   1.29   thorpej  * This function gets called when we receive an ACK for a
   3113   1.29   thorpej  * socket in the LISTEN state.  We look up the connection
   3114   1.29   thorpej  * in the syn cache, and if its there, we pull it out of
   3115   1.29   thorpej  * the cache and turn it into a full-blown connection in
   3116   1.29   thorpej  * the SYN-RECEIVED state.
   3117   1.29   thorpej  *
   3118   1.29   thorpej  * The return values may not be immediately obvious, and their effects
   3119   1.29   thorpej  * can be subtle, so here they are:
   3120   1.29   thorpej  *
   3121   1.29   thorpej  *	NULL	SYN was not found in cache; caller should drop the
   3122   1.29   thorpej  *		packet and send an RST.
   3123   1.29   thorpej  *
   3124   1.29   thorpej  *	-1	We were unable to create the new connection, and are
   3125   1.29   thorpej  *		aborting it.  An ACK,RST is being sent to the peer
   3126   1.29   thorpej  *		(unless we got screwey sequence numbners; see below),
   3127   1.29   thorpej  *		because the 3-way handshake has been completed.  Caller
   3128   1.29   thorpej  *		should not free the mbuf, since we may be using it.  If
   3129   1.29   thorpej  *		we are not, we will free it.
   3130   1.29   thorpej  *
   3131   1.29   thorpej  *	Otherwise, the return value is a pointer to the new socket
   3132   1.29   thorpej  *	associated with the connection.
   3133   1.29   thorpej  */
   3134   1.29   thorpej struct socket *
   3135   1.83    itojun syn_cache_get(src, dst, th, hlen, tlen, so, m)
   3136   1.83    itojun 	struct sockaddr *src;
   3137   1.83    itojun 	struct sockaddr *dst;
   3138   1.83    itojun 	struct tcphdr *th;
   3139   1.83    itojun 	unsigned int hlen, tlen;
   3140   1.29   thorpej 	struct socket *so;
   3141   1.29   thorpej 	struct mbuf *m;
   3142   1.29   thorpej {
   3143   1.59   thorpej 	struct syn_cache *sc;
   3144   1.59   thorpej 	struct syn_cache_head *scp;
   3145  1.106  augustss 	struct inpcb *inp = NULL;
   3146   1.83    itojun #ifdef INET6
   3147  1.106  augustss 	struct in6pcb *in6p = NULL;
   3148   1.83    itojun #endif
   3149  1.106  augustss 	struct tcpcb *tp = 0;
   3150   1.29   thorpej 	struct mbuf *am;
   3151   1.29   thorpej 	int s;
   3152   1.83    itojun 	struct socket *oso;
   3153   1.29   thorpej 
   3154   1.29   thorpej 	s = splsoftnet();
   3155   1.83    itojun 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3156   1.29   thorpej 		splx(s);
   3157   1.29   thorpej 		return (NULL);
   3158   1.29   thorpej 	}
   3159   1.29   thorpej 
   3160   1.29   thorpej 	/*
   3161   1.75   thorpej 	 * Verify the sequence and ack numbers.  Try getting the correct
   3162   1.75   thorpej 	 * response again.
   3163   1.29   thorpej 	 */
   3164   1.83    itojun 	if ((th->th_ack != sc->sc_iss + 1) ||
   3165   1.83    itojun 	    SEQ_LEQ(th->th_seq, sc->sc_irs) ||
   3166   1.83    itojun 	    SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
   3167   1.80   thorpej 		(void) syn_cache_respond(sc, m);
   3168   1.29   thorpej 		splx(s);
   3169   1.29   thorpej 		return ((struct socket *)(-1));
   3170   1.29   thorpej 	}
   3171   1.29   thorpej 
   3172   1.29   thorpej 	/* Remove this cache entry */
   3173   1.80   thorpej 	SYN_CACHE_RM(sc);
   3174   1.29   thorpej 	splx(s);
   3175   1.29   thorpej 
   3176   1.29   thorpej 	/*
   3177   1.29   thorpej 	 * Ok, create the full blown connection, and set things up
   3178   1.29   thorpej 	 * as they would have been set up if we had created the
   3179   1.29   thorpej 	 * connection when the SYN arrived.  If we can't create
   3180   1.29   thorpej 	 * the connection, abort it.
   3181   1.29   thorpej 	 */
   3182   1.83    itojun 	/*
   3183   1.83    itojun 	 * inp still has the OLD in_pcb stuff, set the
   3184   1.83    itojun 	 * v6-related flags on the new guy, too.   This is
   3185   1.83    itojun 	 * done particularly for the case where an AF_INET6
   3186   1.83    itojun 	 * socket is bound only to a port, and a v4 connection
   3187   1.83    itojun 	 * comes in on that port.
   3188  1.143    itojun 	 * we also copy the flowinfo from the original pcb
   3189   1.83    itojun 	 * to the new one.
   3190   1.83    itojun 	 */
   3191   1.83    itojun     {
   3192   1.83    itojun 	struct inpcb *parentinpcb;
   3193   1.83    itojun 
   3194   1.83    itojun 	parentinpcb = (struct inpcb *)so->so_pcb;
   3195   1.83    itojun 
   3196   1.83    itojun 	oso = so;
   3197   1.29   thorpej 	so = sonewconn(so, SS_ISCONNECTED);
   3198   1.29   thorpej 	if (so == NULL)
   3199   1.29   thorpej 		goto resetandabort;
   3200   1.29   thorpej 
   3201   1.83    itojun 	switch (so->so_proto->pr_domain->dom_family) {
   3202  1.119    itojun #ifdef INET
   3203   1.83    itojun 	case AF_INET:
   3204   1.83    itojun 		inp = sotoinpcb(so);
   3205   1.83    itojun 		break;
   3206  1.119    itojun #endif
   3207   1.83    itojun #ifdef INET6
   3208   1.83    itojun 	case AF_INET6:
   3209   1.83    itojun 		in6p = sotoin6pcb(so);
   3210   1.83    itojun 		break;
   3211   1.83    itojun #endif
   3212   1.83    itojun 	}
   3213   1.83    itojun     }
   3214   1.83    itojun 	switch (src->sa_family) {
   3215  1.119    itojun #ifdef INET
   3216   1.83    itojun 	case AF_INET:
   3217   1.83    itojun 		if (inp) {
   3218   1.83    itojun 			inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
   3219   1.83    itojun 			inp->inp_lport = ((struct sockaddr_in *)dst)->sin_port;
   3220   1.83    itojun 			inp->inp_options = ip_srcroute();
   3221   1.83    itojun 			in_pcbstate(inp, INP_BOUND);
   3222   1.83    itojun 			if (inp->inp_options == NULL) {
   3223   1.83    itojun 				inp->inp_options = sc->sc_ipopts;
   3224   1.83    itojun 				sc->sc_ipopts = NULL;
   3225   1.83    itojun 			}
   3226   1.83    itojun 		}
   3227   1.83    itojun #ifdef INET6
   3228   1.83    itojun 		else if (in6p) {
   3229   1.83    itojun 			/* IPv4 packet to AF_INET6 socket */
   3230   1.83    itojun 			bzero(&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
   3231   1.83    itojun 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
   3232   1.83    itojun 			bcopy(&((struct sockaddr_in *)dst)->sin_addr,
   3233   1.83    itojun 				&in6p->in6p_laddr.s6_addr32[3],
   3234   1.83    itojun 				sizeof(((struct sockaddr_in *)dst)->sin_addr));
   3235   1.83    itojun 			in6p->in6p_lport = ((struct sockaddr_in *)dst)->sin_port;
   3236   1.83    itojun 			in6totcpcb(in6p)->t_family = AF_INET;
   3237   1.83    itojun 		}
   3238   1.83    itojun #endif
   3239   1.83    itojun 		break;
   3240  1.119    itojun #endif
   3241   1.83    itojun #ifdef INET6
   3242   1.83    itojun 	case AF_INET6:
   3243   1.83    itojun 		if (in6p) {
   3244   1.83    itojun 			in6p->in6p_laddr = ((struct sockaddr_in6 *)dst)->sin6_addr;
   3245   1.83    itojun 			in6p->in6p_lport = ((struct sockaddr_in6 *)dst)->sin6_port;
   3246   1.83    itojun #if 0
   3247   1.83    itojun 			in6p->in6p_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
   3248   1.83    itojun 			/*inp->inp_options = ip6_srcroute();*/ /* soon. */
   3249   1.83    itojun #endif
   3250   1.83    itojun 		}
   3251   1.83    itojun 		break;
   3252   1.83    itojun #endif
   3253   1.83    itojun 	}
   3254   1.83    itojun #ifdef INET6
   3255   1.83    itojun 	if (in6p && in6totcpcb(in6p)->t_family == AF_INET6 && sotoinpcb(oso)) {
   3256   1.83    itojun 		struct in6pcb *oin6p = sotoin6pcb(oso);
   3257   1.83    itojun 		/* inherit socket options from the listening socket */
   3258   1.83    itojun 		in6p->in6p_flags |= (oin6p->in6p_flags & IN6P_CONTROLOPTS);
   3259   1.83    itojun 		if (in6p->in6p_flags & IN6P_CONTROLOPTS) {
   3260   1.83    itojun 			m_freem(in6p->in6p_options);
   3261   1.83    itojun 			in6p->in6p_options = 0;
   3262   1.83    itojun 		}
   3263   1.83    itojun 		ip6_savecontrol(in6p, &in6p->in6p_options,
   3264   1.83    itojun 			mtod(m, struct ip6_hdr *), m);
   3265   1.83    itojun 	}
   3266   1.83    itojun #endif
   3267   1.83    itojun 
   3268   1.83    itojun #ifdef IPSEC
   3269  1.102    itojun 	/*
   3270  1.102    itojun 	 * we make a copy of policy, instead of sharing the policy,
   3271  1.102    itojun 	 * for better behavior in terms of SA lookup and dead SA removal.
   3272  1.102    itojun 	 */
   3273   1.83    itojun 	if (inp) {
   3274  1.102    itojun 		/* copy old policy into new socket's */
   3275  1.144    itojun 		if (ipsec_copy_pcbpolicy(sotoinpcb(oso)->inp_sp, inp->inp_sp))
   3276   1.83    itojun 			printf("tcp_input: could not copy policy\n");
   3277   1.83    itojun 	}
   3278   1.83    itojun #ifdef INET6
   3279   1.83    itojun 	else if (in6p) {
   3280  1.102    itojun 		/* copy old policy into new socket's */
   3281  1.144    itojun 		if (ipsec_copy_pcbpolicy(sotoin6pcb(oso)->in6p_sp,
   3282  1.144    itojun 		    in6p->in6p_sp))
   3283   1.83    itojun 			printf("tcp_input: could not copy policy\n");
   3284   1.50   thorpej 	}
   3285   1.83    itojun #endif
   3286   1.83    itojun #endif
   3287   1.29   thorpej 
   3288   1.75   thorpej 	/*
   3289   1.75   thorpej 	 * Give the new socket our cached route reference.
   3290   1.75   thorpej 	 */
   3291   1.83    itojun 	if (inp)
   3292   1.83    itojun 		inp->inp_route = sc->sc_route4;		/* struct assignment */
   3293   1.83    itojun #ifdef INET6
   3294   1.83    itojun 	else
   3295   1.83    itojun 		in6p->in6p_route = sc->sc_route6;
   3296   1.83    itojun #endif
   3297   1.83    itojun 	sc->sc_route4.ro_rt = NULL;
   3298   1.75   thorpej 
   3299   1.29   thorpej 	am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
   3300   1.40    mellon 	if (am == NULL)
   3301   1.29   thorpej 		goto resetandabort;
   3302   1.83    itojun 	am->m_len = src->sa_len;
   3303   1.83    itojun 	bcopy(src, mtod(am, caddr_t), src->sa_len);
   3304   1.83    itojun 	if (inp) {
   3305   1.83    itojun 		if (in_pcbconnect(inp, am)) {
   3306   1.83    itojun 			(void) m_free(am);
   3307   1.83    itojun 			goto resetandabort;
   3308   1.83    itojun 		}
   3309   1.83    itojun 	}
   3310   1.83    itojun #ifdef INET6
   3311   1.83    itojun 	else if (in6p) {
   3312   1.83    itojun 		if (src->sa_family == AF_INET) {
   3313   1.83    itojun 			/* IPv4 packet to AF_INET6 socket */
   3314   1.83    itojun 			struct sockaddr_in6 *sin6;
   3315   1.83    itojun 			sin6 = mtod(am, struct sockaddr_in6 *);
   3316   1.83    itojun 			am->m_len = sizeof(*sin6);
   3317   1.83    itojun 			bzero(sin6, sizeof(*sin6));
   3318   1.83    itojun 			sin6->sin6_family = AF_INET6;
   3319   1.83    itojun 			sin6->sin6_len = sizeof(*sin6);
   3320   1.83    itojun 			sin6->sin6_port = ((struct sockaddr_in *)src)->sin_port;
   3321   1.83    itojun 			sin6->sin6_addr.s6_addr16[5] = htons(0xffff);
   3322   1.83    itojun 			bcopy(&((struct sockaddr_in *)src)->sin_addr,
   3323   1.83    itojun 				&sin6->sin6_addr.s6_addr32[3],
   3324   1.83    itojun 				sizeof(sin6->sin6_addr.s6_addr32[3]));
   3325   1.83    itojun 		}
   3326   1.83    itojun 		if (in6_pcbconnect(in6p, am)) {
   3327   1.83    itojun 			(void) m_free(am);
   3328   1.83    itojun 			goto resetandabort;
   3329   1.83    itojun 		}
   3330   1.83    itojun 	}
   3331   1.83    itojun #endif
   3332   1.83    itojun 	else {
   3333   1.29   thorpej 		(void) m_free(am);
   3334   1.29   thorpej 		goto resetandabort;
   3335   1.29   thorpej 	}
   3336   1.29   thorpej 	(void) m_free(am);
   3337   1.29   thorpej 
   3338   1.83    itojun 	if (inp)
   3339   1.83    itojun 		tp = intotcpcb(inp);
   3340   1.83    itojun #ifdef INET6
   3341   1.83    itojun 	else if (in6p)
   3342   1.83    itojun 		tp = in6totcpcb(in6p);
   3343   1.83    itojun #endif
   3344   1.83    itojun 	else
   3345   1.83    itojun 		tp = NULL;
   3346  1.149  wrstuden 	tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
   3347   1.29   thorpej 	if (sc->sc_request_r_scale != 15) {
   3348   1.29   thorpej 		tp->requested_s_scale = sc->sc_requested_s_scale;
   3349   1.29   thorpej 		tp->request_r_scale = sc->sc_request_r_scale;
   3350   1.29   thorpej 		tp->snd_scale = sc->sc_requested_s_scale;
   3351   1.29   thorpej 		tp->rcv_scale = sc->sc_request_r_scale;
   3352   1.29   thorpej 		tp->t_flags |= TF_RCVD_SCALE;
   3353   1.29   thorpej 	}
   3354   1.49   thorpej 	if (sc->sc_flags & SCF_TIMESTAMP)
   3355   1.29   thorpej 		tp->t_flags |= TF_RCVD_TSTMP;
   3356  1.123   thorpej 	tp->ts_timebase = sc->sc_timebase;
   3357   1.29   thorpej 
   3358   1.29   thorpej 	tp->t_template = tcp_template(tp);
   3359   1.29   thorpej 	if (tp->t_template == 0) {
   3360   1.29   thorpej 		tp = tcp_drop(tp, ENOBUFS);	/* destroys socket */
   3361   1.29   thorpej 		so = NULL;
   3362   1.29   thorpej 		m_freem(m);
   3363   1.29   thorpej 		goto abort;
   3364   1.29   thorpej 	}
   3365   1.29   thorpej 
   3366   1.29   thorpej 	tp->iss = sc->sc_iss;
   3367   1.29   thorpej 	tp->irs = sc->sc_irs;
   3368   1.29   thorpej 	tcp_sendseqinit(tp);
   3369   1.29   thorpej 	tcp_rcvseqinit(tp);
   3370   1.29   thorpej 	tp->t_state = TCPS_SYN_RECEIVED;
   3371   1.58   thorpej 	TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
   3372   1.29   thorpej 	tcpstat.tcps_accepts++;
   3373   1.29   thorpej 
   3374   1.32   thorpej 	/* Initialize tp->t_ourmss before we deal with the peer's! */
   3375   1.32   thorpej 	tp->t_ourmss = sc->sc_ourmaxseg;
   3376   1.32   thorpej 	tcp_mss_from_peer(tp, sc->sc_peermaxseg);
   3377   1.46   thorpej 
   3378   1.46   thorpej 	/*
   3379   1.46   thorpej 	 * Initialize the initial congestion window.  If we
   3380   1.46   thorpej 	 * had to retransmit the SYN,ACK, we must initialize cwnd
   3381   1.62   thorpej 	 * to 1 segment (i.e. the Loss Window).
   3382   1.46   thorpej 	 */
   3383   1.80   thorpej 	if (sc->sc_rxtshift)
   3384   1.62   thorpej 		tp->snd_cwnd = tp->t_peermss;
   3385   1.62   thorpej 	else
   3386   1.62   thorpej 		tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_peermss);
   3387   1.46   thorpej 
   3388   1.32   thorpej 	tcp_rmx_rtt(tp);
   3389   1.29   thorpej 	tp->snd_wl1 = sc->sc_irs;
   3390   1.29   thorpej 	tp->rcv_up = sc->sc_irs + 1;
   3391   1.29   thorpej 
   3392   1.29   thorpej 	/*
   3393   1.29   thorpej 	 * This is what whould have happened in tcp_ouput() when
   3394   1.29   thorpej 	 * the SYN,ACK was sent.
   3395   1.29   thorpej 	 */
   3396   1.29   thorpej 	tp->snd_up = tp->snd_una;
   3397   1.29   thorpej 	tp->snd_max = tp->snd_nxt = tp->iss+1;
   3398   1.58   thorpej 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   3399   1.80   thorpej 	if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
   3400   1.80   thorpej 		tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
   3401   1.29   thorpej 	tp->last_ack_sent = tp->rcv_nxt;
   3402   1.29   thorpej 
   3403   1.29   thorpej 	tcpstat.tcps_sc_completed++;
   3404   1.75   thorpej 	SYN_CACHE_PUT(sc);
   3405   1.29   thorpej 	return (so);
   3406   1.29   thorpej 
   3407   1.29   thorpej resetandabort:
   3408   1.86    itojun 	(void) tcp_respond(NULL, m, m, th,
   3409   1.83    itojun 			   th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK);
   3410   1.29   thorpej abort:
   3411   1.29   thorpej 	if (so != NULL)
   3412   1.29   thorpej 		(void) soabort(so);
   3413   1.75   thorpej 	SYN_CACHE_PUT(sc);
   3414   1.29   thorpej 	tcpstat.tcps_sc_aborted++;
   3415   1.29   thorpej 	return ((struct socket *)(-1));
   3416   1.29   thorpej }
   3417   1.29   thorpej 
   3418   1.29   thorpej /*
   3419   1.29   thorpej  * This function is called when we get a RST for a
   3420  1.126       wiz  * non-existent connection, so that we can see if the
   3421   1.29   thorpej  * connection is in the syn cache.  If it is, zap it.
   3422   1.29   thorpej  */
   3423   1.29   thorpej 
   3424   1.29   thorpej void
   3425   1.83    itojun syn_cache_reset(src, dst, th)
   3426   1.83    itojun 	struct sockaddr *src;
   3427   1.83    itojun 	struct sockaddr *dst;
   3428   1.83    itojun 	struct tcphdr *th;
   3429   1.29   thorpej {
   3430   1.59   thorpej 	struct syn_cache *sc;
   3431   1.59   thorpej 	struct syn_cache_head *scp;
   3432   1.29   thorpej 	int s = splsoftnet();
   3433   1.29   thorpej 
   3434   1.83    itojun 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3435   1.29   thorpej 		splx(s);
   3436   1.29   thorpej 		return;
   3437   1.29   thorpej 	}
   3438   1.83    itojun 	if (SEQ_LT(th->th_seq, sc->sc_irs) ||
   3439   1.83    itojun 	    SEQ_GT(th->th_seq, sc->sc_irs+1)) {
   3440   1.29   thorpej 		splx(s);
   3441   1.29   thorpej 		return;
   3442   1.29   thorpej 	}
   3443   1.80   thorpej 	SYN_CACHE_RM(sc);
   3444   1.29   thorpej 	splx(s);
   3445   1.29   thorpej 	tcpstat.tcps_sc_reset++;
   3446   1.75   thorpej 	SYN_CACHE_PUT(sc);
   3447   1.29   thorpej }
   3448   1.29   thorpej 
   3449   1.29   thorpej void
   3450   1.83    itojun syn_cache_unreach(src, dst, th)
   3451   1.83    itojun 	struct sockaddr *src;
   3452   1.83    itojun 	struct sockaddr *dst;
   3453   1.29   thorpej 	struct tcphdr *th;
   3454   1.29   thorpej {
   3455   1.59   thorpej 	struct syn_cache *sc;
   3456   1.59   thorpej 	struct syn_cache_head *scp;
   3457   1.29   thorpej 	int s;
   3458   1.29   thorpej 
   3459   1.29   thorpej 	s = splsoftnet();
   3460   1.83    itojun 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3461   1.29   thorpej 		splx(s);
   3462   1.29   thorpej 		return;
   3463   1.29   thorpej 	}
   3464   1.29   thorpej 	/* If the sequence number != sc_iss, then it's a bogus ICMP msg */
   3465   1.29   thorpej 	if (ntohl (th->th_seq) != sc->sc_iss) {
   3466   1.29   thorpej 		splx(s);
   3467   1.29   thorpej 		return;
   3468   1.29   thorpej 	}
   3469   1.64   thorpej 
   3470   1.64   thorpej 	/*
   3471   1.64   thorpej 	 * If we've rertransmitted 3 times and this is our second error,
   3472   1.64   thorpej 	 * we remove the entry.  Otherwise, we allow it to continue on.
   3473   1.64   thorpej 	 * This prevents us from incorrectly nuking an entry during a
   3474   1.64   thorpej 	 * spurious network outage.
   3475   1.64   thorpej 	 *
   3476   1.64   thorpej 	 * See tcp_notify().
   3477   1.64   thorpej 	 */
   3478   1.80   thorpej 	if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
   3479   1.64   thorpej 		sc->sc_flags |= SCF_UNREACH;
   3480   1.64   thorpej 		splx(s);
   3481   1.64   thorpej 		return;
   3482   1.64   thorpej 	}
   3483   1.64   thorpej 
   3484   1.80   thorpej 	SYN_CACHE_RM(sc);
   3485   1.29   thorpej 	splx(s);
   3486   1.29   thorpej 	tcpstat.tcps_sc_unreach++;
   3487   1.75   thorpej 	SYN_CACHE_PUT(sc);
   3488   1.29   thorpej }
   3489   1.29   thorpej 
   3490   1.29   thorpej /*
   3491   1.29   thorpej  * Given a LISTEN socket and an inbound SYN request, add
   3492   1.29   thorpej  * this to the syn cache, and send back a segment:
   3493   1.29   thorpej  *	<SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
   3494   1.29   thorpej  * to the source.
   3495   1.32   thorpej  *
   3496   1.61   thorpej  * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
   3497   1.61   thorpej  * Doing so would require that we hold onto the data and deliver it
   3498   1.61   thorpej  * to the application.  However, if we are the target of a SYN-flood
   3499   1.61   thorpej  * DoS attack, an attacker could send data which would eventually
   3500   1.61   thorpej  * consume all available buffer space if it were ACKed.  By not ACKing
   3501   1.61   thorpej  * the data, we avoid this DoS scenario.
   3502   1.29   thorpej  */
   3503   1.29   thorpej 
   3504   1.29   thorpej int
   3505   1.83    itojun syn_cache_add(src, dst, th, hlen, so, m, optp, optlen, oi)
   3506   1.83    itojun 	struct sockaddr *src;
   3507   1.83    itojun 	struct sockaddr *dst;
   3508   1.83    itojun 	struct tcphdr *th;
   3509   1.83    itojun 	unsigned int hlen;
   3510   1.29   thorpej 	struct socket *so;
   3511   1.29   thorpej 	struct mbuf *m;
   3512   1.29   thorpej 	u_char *optp;
   3513   1.29   thorpej 	int optlen;
   3514   1.29   thorpej 	struct tcp_opt_info *oi;
   3515   1.29   thorpej {
   3516   1.32   thorpej 	struct tcpcb tb, *tp;
   3517   1.29   thorpej 	long win;
   3518   1.59   thorpej 	struct syn_cache *sc;
   3519   1.29   thorpej 	struct syn_cache_head *scp;
   3520   1.50   thorpej 	struct mbuf *ipopts;
   3521   1.29   thorpej 
   3522   1.32   thorpej 	tp = sototcpcb(so);
   3523   1.29   thorpej 
   3524   1.29   thorpej 	/*
   3525   1.29   thorpej 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
   3526  1.103   thorpej 	 *
   3527  1.103   thorpej 	 * Note this check is performed in tcp_input() very early on.
   3528   1.29   thorpej 	 */
   3529   1.29   thorpej 
   3530   1.29   thorpej 	/*
   3531   1.29   thorpej 	 * Initialize some local state.
   3532   1.29   thorpej 	 */
   3533   1.29   thorpej 	win = sbspace(&so->so_rcv);
   3534   1.29   thorpej 	if (win > TCP_MAXWIN)
   3535   1.29   thorpej 		win = TCP_MAXWIN;
   3536   1.29   thorpej 
   3537  1.119    itojun 	switch (src->sa_family) {
   3538  1.119    itojun #ifdef INET
   3539  1.119    itojun 	case AF_INET:
   3540   1.83    itojun 		/*
   3541   1.83    itojun 		 * Remember the IP options, if any.
   3542   1.83    itojun 		 */
   3543   1.83    itojun 		ipopts = ip_srcroute();
   3544  1.119    itojun 		break;
   3545  1.119    itojun #endif
   3546  1.119    itojun 	default:
   3547   1.83    itojun 		ipopts = NULL;
   3548  1.119    itojun 	}
   3549   1.50   thorpej 
   3550   1.29   thorpej 	if (optp) {
   3551   1.29   thorpej 		tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
   3552   1.83    itojun 		tcp_dooptions(&tb, optp, optlen, th, oi);
   3553   1.29   thorpej 	} else
   3554   1.29   thorpej 		tb.t_flags = 0;
   3555   1.29   thorpej 
   3556   1.29   thorpej 	/*
   3557   1.29   thorpej 	 * See if we already have an entry for this connection.
   3558   1.80   thorpej 	 * If we do, resend the SYN,ACK.  We do not count this
   3559   1.80   thorpej 	 * as a retransmission (XXX though maybe we should).
   3560   1.29   thorpej 	 */
   3561   1.83    itojun 	if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
   3562   1.29   thorpej 		tcpstat.tcps_sc_dupesyn++;
   3563   1.50   thorpej 		if (ipopts) {
   3564   1.50   thorpej 			/*
   3565   1.50   thorpej 			 * If we were remembering a previous source route,
   3566   1.50   thorpej 			 * forget it and use the new one we've been given.
   3567   1.50   thorpej 			 */
   3568   1.50   thorpej 			if (sc->sc_ipopts)
   3569   1.50   thorpej 				(void) m_free(sc->sc_ipopts);
   3570   1.50   thorpej 			sc->sc_ipopts = ipopts;
   3571   1.50   thorpej 		}
   3572   1.80   thorpej 		sc->sc_timestamp = tb.ts_recent;
   3573   1.80   thorpej 		if (syn_cache_respond(sc, m) == 0) {
   3574   1.29   thorpej 			tcpstat.tcps_sndacks++;
   3575   1.29   thorpej 			tcpstat.tcps_sndtotal++;
   3576   1.29   thorpej 		}
   3577   1.29   thorpej 		return (1);
   3578   1.29   thorpej 	}
   3579   1.29   thorpej 
   3580   1.63   thorpej 	sc = pool_get(&syn_cache_pool, PR_NOWAIT);
   3581  1.143    itojun 	if (sc == NULL) {
   3582   1.50   thorpej 		if (ipopts)
   3583   1.50   thorpej 			(void) m_free(ipopts);
   3584   1.29   thorpej 		return (0);
   3585   1.50   thorpej 	}
   3586   1.50   thorpej 
   3587   1.29   thorpej 	/*
   3588   1.50   thorpej 	 * Fill in the cache, and put the necessary IP and TCP
   3589   1.29   thorpej 	 * options into the reply.
   3590   1.29   thorpej 	 */
   3591  1.130   thorpej 	callout_init(&sc->sc_timer);
   3592   1.83    itojun 	bzero(sc, sizeof(struct syn_cache));
   3593   1.83    itojun 	bcopy(src, &sc->sc_src, src->sa_len);
   3594   1.83    itojun 	bcopy(dst, &sc->sc_dst, dst->sa_len);
   3595   1.46   thorpej 	sc->sc_flags = 0;
   3596   1.50   thorpej 	sc->sc_ipopts = ipopts;
   3597   1.83    itojun 	sc->sc_irs = th->th_seq;
   3598  1.123   thorpej 	switch (src->sa_family) {
   3599  1.123   thorpej #ifdef INET
   3600  1.123   thorpej 	case AF_INET:
   3601  1.123   thorpej 	    {
   3602  1.123   thorpej 		struct sockaddr_in *srcin = (void *) src;
   3603  1.123   thorpej 		struct sockaddr_in *dstin = (void *) dst;
   3604  1.123   thorpej 
   3605  1.123   thorpej 		sc->sc_iss = tcp_new_iss1(&dstin->sin_addr,
   3606  1.123   thorpej 		    &srcin->sin_addr, dstin->sin_port,
   3607  1.123   thorpej 		    srcin->sin_port, sizeof(dstin->sin_addr), 0);
   3608  1.123   thorpej 		break;
   3609  1.123   thorpej 	    }
   3610  1.123   thorpej #endif /* INET */
   3611  1.123   thorpej #ifdef INET6
   3612  1.123   thorpej 	case AF_INET6:
   3613  1.123   thorpej 	    {
   3614  1.123   thorpej 		struct sockaddr_in6 *srcin6 = (void *) src;
   3615  1.123   thorpej 		struct sockaddr_in6 *dstin6 = (void *) dst;
   3616  1.123   thorpej 
   3617  1.123   thorpej 		sc->sc_iss = tcp_new_iss1(&dstin6->sin6_addr,
   3618  1.123   thorpej 		    &srcin6->sin6_addr, dstin6->sin6_port,
   3619  1.123   thorpej 		    srcin6->sin6_port, sizeof(dstin6->sin6_addr), 0);
   3620  1.123   thorpej 		break;
   3621  1.123   thorpej 	    }
   3622  1.123   thorpej #endif /* INET6 */
   3623  1.123   thorpej 	}
   3624   1.29   thorpej 	sc->sc_peermaxseg = oi->maxseg;
   3625   1.51       kml 	sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
   3626   1.96    itojun 						m->m_pkthdr.rcvif : NULL,
   3627   1.96    itojun 						sc->sc_src.sa.sa_family);
   3628   1.80   thorpej 	sc->sc_win = win;
   3629  1.123   thorpej 	sc->sc_timebase = tcp_now;	/* see tcp_newtcpcb() */
   3630   1.80   thorpej 	sc->sc_timestamp = tb.ts_recent;
   3631   1.49   thorpej 	if (tcp_do_rfc1323 && (tb.t_flags & TF_RCVD_TSTMP))
   3632   1.49   thorpej 		sc->sc_flags |= SCF_TIMESTAMP;
   3633   1.29   thorpej 	if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   3634   1.29   thorpej 	    (TF_RCVD_SCALE|TF_REQ_SCALE)) {
   3635   1.29   thorpej 		sc->sc_requested_s_scale = tb.requested_s_scale;
   3636   1.29   thorpej 		sc->sc_request_r_scale = 0;
   3637   1.29   thorpej 		while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
   3638   1.29   thorpej 		    TCP_MAXWIN << sc->sc_request_r_scale <
   3639   1.29   thorpej 		    so->so_rcv.sb_hiwat)
   3640   1.29   thorpej 			sc->sc_request_r_scale++;
   3641   1.29   thorpej 	} else {
   3642   1.29   thorpej 		sc->sc_requested_s_scale = 15;
   3643   1.29   thorpej 		sc->sc_request_r_scale = 15;
   3644   1.29   thorpej 	}
   3645   1.93    itojun 	sc->sc_tp = tp;
   3646   1.80   thorpej 	if (syn_cache_respond(sc, m) == 0) {
   3647   1.93    itojun 		syn_cache_insert(sc, tp);
   3648   1.29   thorpej 		tcpstat.tcps_sndacks++;
   3649   1.29   thorpej 		tcpstat.tcps_sndtotal++;
   3650   1.29   thorpej 	} else {
   3651   1.75   thorpej 		SYN_CACHE_PUT(sc);
   3652   1.29   thorpej 		tcpstat.tcps_sc_dropped++;
   3653   1.29   thorpej 	}
   3654   1.29   thorpej 	return (1);
   3655   1.29   thorpej }
   3656   1.29   thorpej 
   3657   1.29   thorpej int
   3658   1.80   thorpej syn_cache_respond(sc, m)
   3659   1.29   thorpej 	struct syn_cache *sc;
   3660   1.29   thorpej 	struct mbuf *m;
   3661   1.29   thorpej {
   3662   1.97    itojun 	struct route *ro;
   3663   1.29   thorpej 	u_int8_t *optp;
   3664   1.75   thorpej 	int optlen, error;
   3665   1.75   thorpej 	u_int16_t tlen;
   3666   1.83    itojun 	struct ip *ip = NULL;
   3667   1.83    itojun #ifdef INET6
   3668   1.83    itojun 	struct ip6_hdr *ip6 = NULL;
   3669   1.83    itojun #endif
   3670   1.83    itojun 	struct tcphdr *th;
   3671   1.83    itojun 	u_int hlen;
   3672   1.83    itojun 
   3673   1.83    itojun 	switch (sc->sc_src.sa.sa_family) {
   3674   1.83    itojun 	case AF_INET:
   3675   1.83    itojun 		hlen = sizeof(struct ip);
   3676   1.97    itojun 		ro = &sc->sc_route4;
   3677   1.83    itojun 		break;
   3678   1.83    itojun #ifdef INET6
   3679   1.83    itojun 	case AF_INET6:
   3680   1.83    itojun 		hlen = sizeof(struct ip6_hdr);
   3681   1.97    itojun 		ro = (struct route *)&sc->sc_route6;
   3682   1.83    itojun 		break;
   3683   1.83    itojun #endif
   3684   1.83    itojun 	default:
   3685   1.83    itojun 		if (m)
   3686   1.83    itojun 			m_freem(m);
   3687   1.83    itojun 		return EAFNOSUPPORT;
   3688   1.83    itojun 	}
   3689   1.75   thorpej 
   3690   1.75   thorpej 	/* Compute the size of the TCP options. */
   3691   1.75   thorpej 	optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
   3692   1.75   thorpej 	    ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
   3693   1.75   thorpej 
   3694   1.83    itojun 	tlen = hlen + sizeof(struct tcphdr) + optlen;
   3695   1.29   thorpej 
   3696   1.29   thorpej 	/*
   3697  1.109    itojun 	 * Create the IP+TCP header from scratch.
   3698   1.29   thorpej 	 */
   3699  1.109    itojun 	if (m)
   3700  1.109    itojun 		m_freem(m);
   3701  1.114    itojun #ifdef DIAGNOSTIC
   3702  1.114    itojun 	if (max_linkhdr + tlen > MCLBYTES)
   3703  1.114    itojun 		return (ENOBUFS);
   3704  1.114    itojun #endif
   3705  1.109    itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   3706  1.109    itojun 	if (m && tlen > MHLEN) {
   3707  1.109    itojun 		MCLGET(m, M_DONTWAIT);
   3708  1.111   thorpej 		if ((m->m_flags & M_EXT) == 0) {
   3709  1.109    itojun 			m_freem(m);
   3710  1.109    itojun 			m = NULL;
   3711  1.109    itojun 		}
   3712   1.29   thorpej 	}
   3713  1.109    itojun 	if (m == NULL)
   3714  1.109    itojun 		return (ENOBUFS);
   3715   1.29   thorpej 
   3716   1.75   thorpej 	/* Fixup the mbuf. */
   3717   1.75   thorpej 	m->m_data += max_linkhdr;
   3718   1.75   thorpej 	m->m_len = m->m_pkthdr.len = tlen;
   3719   1.83    itojun #ifdef IPSEC
   3720   1.93    itojun 	if (sc->sc_tp) {
   3721   1.93    itojun 		struct tcpcb *tp;
   3722   1.93    itojun 		struct socket *so;
   3723   1.93    itojun 
   3724   1.93    itojun 		tp = sc->sc_tp;
   3725   1.93    itojun 		if (tp->t_inpcb)
   3726   1.93    itojun 			so = tp->t_inpcb->inp_socket;
   3727   1.93    itojun #ifdef INET6
   3728   1.93    itojun 		else if (tp->t_in6pcb)
   3729   1.93    itojun 			so = tp->t_in6pcb->in6p_socket;
   3730   1.93    itojun #endif
   3731   1.93    itojun 		else
   3732   1.93    itojun 			so = NULL;
   3733   1.93    itojun 		/* use IPsec policy on listening socket, on SYN ACK */
   3734  1.122    itojun 		if (ipsec_setsocket(m, so) != 0) {
   3735  1.122    itojun 			m_freem(m);
   3736  1.122    itojun 			return ENOBUFS;
   3737  1.122    itojun 		}
   3738   1.93    itojun 	}
   3739   1.83    itojun #endif
   3740  1.109    itojun 	m->m_pkthdr.rcvif = NULL;
   3741   1.83    itojun 	memset(mtod(m, u_char *), 0, tlen);
   3742   1.75   thorpej 
   3743   1.83    itojun 	switch (sc->sc_src.sa.sa_family) {
   3744   1.83    itojun 	case AF_INET:
   3745   1.83    itojun 		ip = mtod(m, struct ip *);
   3746   1.83    itojun 		ip->ip_dst = sc->sc_src.sin.sin_addr;
   3747   1.83    itojun 		ip->ip_src = sc->sc_dst.sin.sin_addr;
   3748   1.83    itojun 		ip->ip_p = IPPROTO_TCP;
   3749   1.83    itojun 		th = (struct tcphdr *)(ip + 1);
   3750   1.83    itojun 		th->th_dport = sc->sc_src.sin.sin_port;
   3751   1.83    itojun 		th->th_sport = sc->sc_dst.sin.sin_port;
   3752   1.83    itojun 		break;
   3753   1.83    itojun #ifdef INET6
   3754   1.83    itojun 	case AF_INET6:
   3755   1.83    itojun 		ip6 = mtod(m, struct ip6_hdr *);
   3756   1.83    itojun 		ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
   3757   1.83    itojun 		ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
   3758   1.83    itojun 		ip6->ip6_nxt = IPPROTO_TCP;
   3759   1.83    itojun 		/* ip6_plen will be updated in ip6_output() */
   3760   1.83    itojun 		th = (struct tcphdr *)(ip6 + 1);
   3761   1.83    itojun 		th->th_dport = sc->sc_src.sin6.sin6_port;
   3762   1.83    itojun 		th->th_sport = sc->sc_dst.sin6.sin6_port;
   3763   1.83    itojun 		break;
   3764   1.83    itojun #endif
   3765   1.84    itojun 	default:
   3766   1.84    itojun 		th = NULL;
   3767   1.83    itojun 	}
   3768   1.75   thorpej 
   3769   1.83    itojun 	th->th_seq = htonl(sc->sc_iss);
   3770   1.83    itojun 	th->th_ack = htonl(sc->sc_irs + 1);
   3771   1.83    itojun 	th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
   3772   1.83    itojun 	th->th_flags = TH_SYN|TH_ACK;
   3773   1.83    itojun 	th->th_win = htons(sc->sc_win);
   3774   1.83    itojun 	/* th_sum already 0 */
   3775   1.83    itojun 	/* th_urp already 0 */
   3776   1.75   thorpej 
   3777   1.75   thorpej 	/* Tack on the TCP options. */
   3778   1.83    itojun 	optp = (u_int8_t *)(th + 1);
   3779   1.75   thorpej 	*optp++ = TCPOPT_MAXSEG;
   3780   1.75   thorpej 	*optp++ = 4;
   3781   1.75   thorpej 	*optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
   3782   1.75   thorpej 	*optp++ = sc->sc_ourmaxseg & 0xff;
   3783   1.29   thorpej 
   3784   1.29   thorpej 	if (sc->sc_request_r_scale != 15) {
   3785   1.75   thorpej 		*((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
   3786   1.29   thorpej 		    TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
   3787   1.29   thorpej 		    sc->sc_request_r_scale);
   3788   1.75   thorpej 		optp += 4;
   3789   1.29   thorpej 	}
   3790   1.29   thorpej 
   3791   1.49   thorpej 	if (sc->sc_flags & SCF_TIMESTAMP) {
   3792   1.75   thorpej 		u_int32_t *lp = (u_int32_t *)(optp);
   3793   1.29   thorpej 		/* Form timestamp option as shown in appendix A of RFC 1323. */
   3794   1.29   thorpej 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
   3795  1.123   thorpej 		*lp++ = htonl(SYN_CACHE_TIMESTAMP(sc));
   3796   1.80   thorpej 		*lp   = htonl(sc->sc_timestamp);
   3797   1.75   thorpej 		optp += TCPOLEN_TSTAMP_APPA;
   3798   1.29   thorpej 	}
   3799   1.29   thorpej 
   3800   1.75   thorpej 	/* Compute the packet's checksum. */
   3801   1.83    itojun 	switch (sc->sc_src.sa.sa_family) {
   3802   1.83    itojun 	case AF_INET:
   3803   1.83    itojun 		ip->ip_len = htons(tlen - hlen);
   3804   1.83    itojun 		th->th_sum = 0;
   3805   1.83    itojun 		th->th_sum = in_cksum(m, tlen);
   3806   1.83    itojun 		break;
   3807   1.83    itojun #ifdef INET6
   3808   1.83    itojun 	case AF_INET6:
   3809   1.83    itojun 		ip6->ip6_plen = htons(tlen - hlen);
   3810   1.83    itojun 		th->th_sum = 0;
   3811   1.83    itojun 		th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
   3812   1.83    itojun 		break;
   3813   1.83    itojun #endif
   3814   1.83    itojun 	}
   3815   1.75   thorpej 
   3816   1.29   thorpej 	/*
   3817   1.75   thorpej 	 * Fill in some straggling IP bits.  Note the stack expects
   3818   1.75   thorpej 	 * ip_len to be in host order, for convenience.
   3819   1.29   thorpej 	 */
   3820   1.83    itojun 	switch (sc->sc_src.sa.sa_family) {
   3821  1.119    itojun #ifdef INET
   3822   1.83    itojun 	case AF_INET:
   3823  1.150    itojun 		ip->ip_len = htons(tlen);
   3824   1.83    itojun 		ip->ip_ttl = ip_defttl;
   3825   1.83    itojun 		/* XXX tos? */
   3826   1.83    itojun 		break;
   3827  1.119    itojun #endif
   3828   1.83    itojun #ifdef INET6
   3829   1.83    itojun 	case AF_INET6:
   3830  1.100    itojun 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   3831  1.100    itojun 		ip6->ip6_vfc |= IPV6_VERSION;
   3832   1.83    itojun 		ip6->ip6_plen = htons(tlen - hlen);
   3833   1.99    itojun 		/* ip6_hlim will be initialized afterwards */
   3834   1.83    itojun 		/* XXX flowlabel? */
   3835   1.83    itojun 		break;
   3836   1.83    itojun #endif
   3837   1.83    itojun 	}
   3838   1.29   thorpej 
   3839   1.83    itojun 	switch (sc->sc_src.sa.sa_family) {
   3840  1.119    itojun #ifdef INET
   3841   1.83    itojun 	case AF_INET:
   3842  1.118   thorpej 		error = ip_output(m, sc->sc_ipopts, ro,
   3843  1.118   thorpej 		    (ip_mtudisc ? IP_MTUDISC : 0),
   3844  1.118   thorpej 		    NULL);
   3845   1.83    itojun 		break;
   3846  1.119    itojun #endif
   3847   1.83    itojun #ifdef INET6
   3848   1.83    itojun 	case AF_INET6:
   3849   1.99    itojun 		ip6->ip6_hlim = in6_selecthlim(NULL,
   3850   1.99    itojun 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   3851   1.99    itojun 
   3852   1.83    itojun 		error = ip6_output(m, NULL /*XXX*/, (struct route_in6 *)ro,
   3853   1.99    itojun 			0, NULL, NULL);
   3854   1.83    itojun 		break;
   3855   1.83    itojun #endif
   3856   1.84    itojun 	default:
   3857   1.84    itojun 		error = EAFNOSUPPORT;
   3858   1.84    itojun 		break;
   3859   1.83    itojun 	}
   3860   1.75   thorpej 	return (error);
   3861    1.1       cgd }
   3862