Deleted Added
full compact
tcp_input.c (253571) tcp_input.c (254889)
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2007-2008,2010
5 * Swinburne University of Technology, Melbourne, Australia.
6 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
7 * Copyright (c) 2010 The FreeBSD Foundation
8 * Copyright (c) 2010-2011 Juniper Networks, Inc.

--- 34 unchanged lines hidden (view full) ---

43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
48 */
49
50#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2007-2008,2010
5 * Swinburne University of Technology, Melbourne, Australia.
6 * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
7 * Copyright (c) 2010 The FreeBSD Foundation
8 * Copyright (c) 2010-2011 Juniper Networks, Inc.

--- 34 unchanged lines hidden (view full) ---

43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
48 */
49
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD: head/sys/netinet/tcp_input.c 253571 2013-07-23 14:14:24Z ae $");
51__FBSDID("$FreeBSD: head/sys/netinet/tcp_input.c 254889 2013-08-25 21:54:41Z markj $");
52
53#include "opt_ipfw.h" /* for ipfw_fwd */
54#include "opt_inet.h"
55#include "opt_inet6.h"
56#include "opt_ipsec.h"
52
53#include "opt_ipfw.h" /* for ipfw_fwd */
54#include "opt_inet.h"
55#include "opt_inet6.h"
56#include "opt_ipsec.h"
57#include "opt_kdtrace.h"
57#include "opt_tcpdebug.h"
58
59#include <sys/param.h>
60#include <sys/kernel.h>
61#include <sys/hhook.h>
62#include <sys/malloc.h>
63#include <sys/mbuf.h>
64#include <sys/proc.h> /* for proc0 declaration */
65#include <sys/protosw.h>
58#include "opt_tcpdebug.h"
59
60#include <sys/param.h>
61#include <sys/kernel.h>
62#include <sys/hhook.h>
63#include <sys/malloc.h>
64#include <sys/mbuf.h>
65#include <sys/proc.h> /* for proc0 declaration */
66#include <sys/protosw.h>
67#include <sys/sdt.h>
66#include <sys/signalvar.h>
67#include <sys/socket.h>
68#include <sys/socketvar.h>
69#include <sys/sysctl.h>
70#include <sys/syslog.h>
71#include <sys/systm.h>
72
73#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
74
75#include <vm/uma.h>
76
77#include <net/if.h>
78#include <net/route.h>
79#include <net/vnet.h>
80
81#define TCPSTATES /* for logging */
82
83#include <netinet/cc.h>
84#include <netinet/in.h>
68#include <sys/signalvar.h>
69#include <sys/socket.h>
70#include <sys/socketvar.h>
71#include <sys/sysctl.h>
72#include <sys/syslog.h>
73#include <sys/systm.h>
74
75#include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
76
77#include <vm/uma.h>
78
79#include <net/if.h>
80#include <net/route.h>
81#include <net/vnet.h>
82
83#define TCPSTATES /* for logging */
84
85#include <netinet/cc.h>
86#include <netinet/in.h>
87#include <netinet/in_kdtrace.h>
85#include <netinet/in_pcb.h>
86#include <netinet/in_systm.h>
87#include <netinet/in_var.h>
88#include <netinet/ip.h>
89#include <netinet/ip_icmp.h> /* required for icmp_var.h */
90#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
91#include <netinet/ip_var.h>
92#include <netinet/ip_options.h>

--- 590 unchanged lines hidden (view full) ---

683
684 /*
685 * Checksum extended TCP header and data.
686 */
687 len = off0 + tlen;
688 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
689 ipov->ih_len = htons(tlen);
690 th->th_sum = in_cksum(m, len);
88#include <netinet/in_pcb.h>
89#include <netinet/in_systm.h>
90#include <netinet/in_var.h>
91#include <netinet/ip.h>
92#include <netinet/ip_icmp.h> /* required for icmp_var.h */
93#include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
94#include <netinet/ip_var.h>
95#include <netinet/ip_options.h>

--- 590 unchanged lines hidden (view full) ---

686
687 /*
688 * Checksum extended TCP header and data.
689 */
690 len = off0 + tlen;
691 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
692 ipov->ih_len = htons(tlen);
693 th->th_sum = in_cksum(m, len);
694 /* Reset length for SDT probes. */
695 ip->ip_len = htons(tlen + off0);
691 }
696 }
697
692 if (th->th_sum) {
693 TCPSTAT_INC(tcps_rcvbadsum);
694 goto drop;
695 }
696 /* Re-initialization for later version check */
697 ip->ip_v = IPVERSION;
698 }
699#endif /* INET */

--- 679 unchanged lines hidden (view full) ---

1379 if ((thflags & TH_RST) == 0 ||
1380 (tp->t_state == TCPS_SYN_SENT) == 0)
1381 goto dropunlock;
1382 }
1383 sig_checked = 1;
1384 }
1385#endif
1386
698 if (th->th_sum) {
699 TCPSTAT_INC(tcps_rcvbadsum);
700 goto drop;
701 }
702 /* Re-initialization for later version check */
703 ip->ip_v = IPVERSION;
704 }
705#endif /* INET */

--- 679 unchanged lines hidden (view full) ---

1385 if ((thflags & TH_RST) == 0 ||
1386 (tp->t_state == TCPS_SYN_SENT) == 0)
1387 goto dropunlock;
1388 }
1389 sig_checked = 1;
1390 }
1391#endif
1392
1393 TCP_PROBE5(receive, NULL, tp, m->m_data, tp, th);
1394
1387 /*
1388 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
1389 * state. tcp_do_segment() always consumes the mbuf chain, unlocks
1390 * the inpcb, and unlocks pcbinfo.
1391 */
1392 tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1393 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1394 return;
1395
1396dropwithreset:
1395 /*
1396 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
1397 * state. tcp_do_segment() always consumes the mbuf chain, unlocks
1398 * the inpcb, and unlocks pcbinfo.
1399 */
1400 tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1401 INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1402 return;
1403
1404dropwithreset:
1405 TCP_PROBE5(receive, NULL, tp, m->m_data, tp, th);
1406
1397 if (ti_locked == TI_WLOCKED) {
1398 INP_INFO_WUNLOCK(&V_tcbinfo);
1399 ti_locked = TI_UNLOCKED;
1400 }
1401#ifdef INVARIANTS
1402 else {
1403 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1404 "ti_locked: %d", __func__, ti_locked));

--- 5 unchanged lines hidden (view full) ---

1410 tcp_dropwithreset(m, th, tp, tlen, rstreason);
1411 INP_WUNLOCK(inp);
1412 } else
1413 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1414 m = NULL; /* mbuf chain got consumed. */
1415 goto drop;
1416
1417dropunlock:
1407 if (ti_locked == TI_WLOCKED) {
1408 INP_INFO_WUNLOCK(&V_tcbinfo);
1409 ti_locked = TI_UNLOCKED;
1410 }
1411#ifdef INVARIANTS
1412 else {
1413 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1414 "ti_locked: %d", __func__, ti_locked));

--- 5 unchanged lines hidden (view full) ---

1420 tcp_dropwithreset(m, th, tp, tlen, rstreason);
1421 INP_WUNLOCK(inp);
1422 } else
1423 tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1424 m = NULL; /* mbuf chain got consumed. */
1425 goto drop;
1426
1427dropunlock:
1428 if (m != NULL)
1429 TCP_PROBE5(receive, NULL, tp, m->m_data, tp, th);
1430
1418 if (ti_locked == TI_WLOCKED) {
1419 INP_INFO_WUNLOCK(&V_tcbinfo);
1420 ti_locked = TI_UNLOCKED;
1421 }
1422#ifdef INVARIANTS
1423 else {
1424 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1425 "ti_locked: %d", __func__, ti_locked));

--- 479 unchanged lines hidden (view full) ---

1905 */
1906 case TCPS_SYN_SENT:
1907 if ((thflags & TH_ACK) &&
1908 (SEQ_LEQ(th->th_ack, tp->iss) ||
1909 SEQ_GT(th->th_ack, tp->snd_max))) {
1910 rstreason = BANDLIM_UNLIMITED;
1911 goto dropwithreset;
1912 }
1431 if (ti_locked == TI_WLOCKED) {
1432 INP_INFO_WUNLOCK(&V_tcbinfo);
1433 ti_locked = TI_UNLOCKED;
1434 }
1435#ifdef INVARIANTS
1436 else {
1437 KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1438 "ti_locked: %d", __func__, ti_locked));

--- 479 unchanged lines hidden (view full) ---

1918 */
1919 case TCPS_SYN_SENT:
1920 if ((thflags & TH_ACK) &&
1921 (SEQ_LEQ(th->th_ack, tp->iss) ||
1922 SEQ_GT(th->th_ack, tp->snd_max))) {
1923 rstreason = BANDLIM_UNLIMITED;
1924 goto dropwithreset;
1925 }
1913 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST))
1926 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1927 TCP_PROBE5(connect_refused, NULL, tp, m->m_data, tp,
1928 th);
1914 tp = tcp_drop(tp, ECONNREFUSED);
1929 tp = tcp_drop(tp, ECONNREFUSED);
1930 }
1915 if (thflags & TH_RST)
1916 goto drop;
1917 if (!(thflags & TH_SYN))
1918 goto drop;
1919
1920 tp->irs = th->th_seq;
1921 tcp_rcvseqinit(tp);
1922 if (thflags & TH_ACK) {

--- 28 unchanged lines hidden (view full) ---

1951 /*
1952 * Received <SYN,ACK> in SYN_SENT[*] state.
1953 * Transitions:
1954 * SYN_SENT --> ESTABLISHED
1955 * SYN_SENT* --> FIN_WAIT_1
1956 */
1957 tp->t_starttime = ticks;
1958 if (tp->t_flags & TF_NEEDFIN) {
1931 if (thflags & TH_RST)
1932 goto drop;
1933 if (!(thflags & TH_SYN))
1934 goto drop;
1935
1936 tp->irs = th->th_seq;
1937 tcp_rcvseqinit(tp);
1938 if (thflags & TH_ACK) {

--- 28 unchanged lines hidden (view full) ---

1967 /*
1968 * Received <SYN,ACK> in SYN_SENT[*] state.
1969 * Transitions:
1970 * SYN_SENT --> ESTABLISHED
1971 * SYN_SENT* --> FIN_WAIT_1
1972 */
1973 tp->t_starttime = ticks;
1974 if (tp->t_flags & TF_NEEDFIN) {
1959 tp->t_state = TCPS_FIN_WAIT_1;
1975 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1960 tp->t_flags &= ~TF_NEEDFIN;
1961 thflags &= ~TH_SYN;
1962 } else {
1976 tp->t_flags &= ~TF_NEEDFIN;
1977 thflags &= ~TH_SYN;
1978 } else {
1963 tp->t_state = TCPS_ESTABLISHED;
1979 tcp_state_change(tp, TCPS_ESTABLISHED);
1980 TCP_PROBE5(connect_established, NULL, tp,
1981 m->m_data, tp, th);
1964 cc_conn_init(tp);
1965 tcp_timer_activate(tp, TT_KEEP,
1966 TP_KEEPIDLE(tp));
1967 }
1968 } else {
1969 /*
1970 * Received initial SYN in SYN-SENT[*] state =>
1971 * simultaneous open. If segment contains CC option
1972 * and there is a cached CC, apply TAO test.
1973 * If it succeeds, connection is * half-synchronized.
1974 * Otherwise, do 3-way handshake:
1975 * SYN-SENT -> SYN-RECEIVED
1976 * SYN-SENT* -> SYN-RECEIVED*
1977 * If there was no CC option, clear cached CC value.
1978 */
1979 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1980 tcp_timer_activate(tp, TT_REXMT, 0);
1982 cc_conn_init(tp);
1983 tcp_timer_activate(tp, TT_KEEP,
1984 TP_KEEPIDLE(tp));
1985 }
1986 } else {
1987 /*
1988 * Received initial SYN in SYN-SENT[*] state =>
1989 * simultaneous open. If segment contains CC option
1990 * and there is a cached CC, apply TAO test.
1991 * If it succeeds, connection is * half-synchronized.
1992 * Otherwise, do 3-way handshake:
1993 * SYN-SENT -> SYN-RECEIVED
1994 * SYN-SENT* -> SYN-RECEIVED*
1995 * If there was no CC option, clear cached CC value.
1996 */
1997 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1998 tcp_timer_activate(tp, TT_REXMT, 0);
1981 tp->t_state = TCPS_SYN_RECEIVED;
1999 tcp_state_change(tp, TCPS_SYN_RECEIVED);
1982 }
1983
1984 KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
1985 "ti_locked %d", __func__, ti_locked));
1986 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1987 INP_WLOCK_ASSERT(tp->t_inpcb);
1988
1989 /*

--- 121 unchanged lines hidden (view full) ---

2111 case TCPS_CLOSE_WAIT:
2112 so->so_error = ECONNRESET;
2113 close:
2114 KASSERT(ti_locked == TI_WLOCKED,
2115 ("tcp_do_segment: TH_RST 1 ti_locked %d",
2116 ti_locked));
2117 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2118
2000 }
2001
2002 KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: "
2003 "ti_locked %d", __func__, ti_locked));
2004 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2005 INP_WLOCK_ASSERT(tp->t_inpcb);
2006
2007 /*

--- 121 unchanged lines hidden (view full) ---

2129 case TCPS_CLOSE_WAIT:
2130 so->so_error = ECONNRESET;
2131 close:
2132 KASSERT(ti_locked == TI_WLOCKED,
2133 ("tcp_do_segment: TH_RST 1 ti_locked %d",
2134 ti_locked));
2135 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
2136
2119 tp->t_state = TCPS_CLOSED;
2137 tcp_state_change(tp, TCPS_CLOSED);
2120 TCPSTAT_INC(tcps_drops);
2121 tp = tcp_close(tp);
2122 break;
2123
2124 case TCPS_CLOSING:
2125 case TCPS_LAST_ACK:
2126 KASSERT(ti_locked == TI_WLOCKED,
2127 ("tcp_do_segment: TH_RST 2 ti_locked %d",

--- 228 unchanged lines hidden (view full) ---

2356 }
2357 /*
2358 * Make transitions:
2359 * SYN-RECEIVED -> ESTABLISHED
2360 * SYN-RECEIVED* -> FIN-WAIT-1
2361 */
2362 tp->t_starttime = ticks;
2363 if (tp->t_flags & TF_NEEDFIN) {
2138 TCPSTAT_INC(tcps_drops);
2139 tp = tcp_close(tp);
2140 break;
2141
2142 case TCPS_CLOSING:
2143 case TCPS_LAST_ACK:
2144 KASSERT(ti_locked == TI_WLOCKED,
2145 ("tcp_do_segment: TH_RST 2 ti_locked %d",

--- 228 unchanged lines hidden (view full) ---

2374 }
2375 /*
2376 * Make transitions:
2377 * SYN-RECEIVED -> ESTABLISHED
2378 * SYN-RECEIVED* -> FIN-WAIT-1
2379 */
2380 tp->t_starttime = ticks;
2381 if (tp->t_flags & TF_NEEDFIN) {
2364 tp->t_state = TCPS_FIN_WAIT_1;
2382 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2365 tp->t_flags &= ~TF_NEEDFIN;
2366 } else {
2383 tp->t_flags &= ~TF_NEEDFIN;
2384 } else {
2367 tp->t_state = TCPS_ESTABLISHED;
2385 tcp_state_change(tp, TCPS_ESTABLISHED);
2386 TCP_PROBE5(accept_established, NULL, tp, m->m_data, tp,
2387 th);
2368 cc_conn_init(tp);
2369 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
2370 }
2371 /*
2372 * If segment contains data or ACK, will call tcp_reass()
2373 * later; if not, do so now to pass queued data to user.
2374 */
2375 if (tlen == 0 && (thflags & TH_FIN) == 0)

--- 371 unchanged lines hidden (view full) ---

2747 */
2748 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2749 soisdisconnected(so);
2750 tcp_timer_activate(tp, TT_2MSL,
2751 (tcp_fast_finwait2_recycle ?
2752 tcp_finwait2_timeout :
2753 TP_MAXIDLE(tp)));
2754 }
2388 cc_conn_init(tp);
2389 tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
2390 }
2391 /*
2392 * If segment contains data or ACK, will call tcp_reass()
2393 * later; if not, do so now to pass queued data to user.
2394 */
2395 if (tlen == 0 && (thflags & TH_FIN) == 0)

--- 371 unchanged lines hidden (view full) ---

2767 */
2768 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2769 soisdisconnected(so);
2770 tcp_timer_activate(tp, TT_2MSL,
2771 (tcp_fast_finwait2_recycle ?
2772 tcp_finwait2_timeout :
2773 TP_MAXIDLE(tp)));
2774 }
2755 tp->t_state = TCPS_FIN_WAIT_2;
2775 tcp_state_change(tp, TCPS_FIN_WAIT_2);
2756 }
2757 break;
2758
2759 /*
2760 * In CLOSING STATE in addition to the processing for
2761 * the ESTABLISHED state if the ACK acknowledges our FIN
2762 * then enter the TIME-WAIT state, otherwise ignore
2763 * the segment.

--- 209 unchanged lines hidden (view full) ---

2973 /*
2974 * In SYN_RECEIVED and ESTABLISHED STATES
2975 * enter the CLOSE_WAIT state.
2976 */
2977 case TCPS_SYN_RECEIVED:
2978 tp->t_starttime = ticks;
2979 /* FALLTHROUGH */
2980 case TCPS_ESTABLISHED:
2776 }
2777 break;
2778
2779 /*
2780 * In CLOSING STATE in addition to the processing for
2781 * the ESTABLISHED state if the ACK acknowledges our FIN
2782 * then enter the TIME-WAIT state, otherwise ignore
2783 * the segment.

--- 209 unchanged lines hidden (view full) ---

2993 /*
2994 * In SYN_RECEIVED and ESTABLISHED STATES
2995 * enter the CLOSE_WAIT state.
2996 */
2997 case TCPS_SYN_RECEIVED:
2998 tp->t_starttime = ticks;
2999 /* FALLTHROUGH */
3000 case TCPS_ESTABLISHED:
2981 tp->t_state = TCPS_CLOSE_WAIT;
3001 tcp_state_change(tp, TCPS_CLOSE_WAIT);
2982 break;
2983
2984 /*
2985 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2986 * enter the CLOSING state.
2987 */
2988 case TCPS_FIN_WAIT_1:
3002 break;
3003
3004 /*
3005 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3006 * enter the CLOSING state.
3007 */
3008 case TCPS_FIN_WAIT_1:
2989 tp->t_state = TCPS_CLOSING;
3009 tcp_state_change(tp, TCPS_CLOSING);
2990 break;
2991
2992 /*
2993 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2994 * starting the time-wait timer, turning off the other
2995 * standard timers.
2996 */
2997 case TCPS_FIN_WAIT_2:

--- 715 unchanged lines hidden ---
3010 break;
3011
3012 /*
3013 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3014 * starting the time-wait timer, turning off the other
3015 * standard timers.
3016 */
3017 case TCPS_FIN_WAIT_2:

--- 715 unchanged lines hidden ---