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 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" |
57#include "opt_kdtrace.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> |
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> |
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); |
696 } |
697 |
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 |
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 |
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 |
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 } |
1926 if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) { 1927 TCP_PROBE5(connect_refused, NULL, tp, m->m_data, tp, 1928 th); |
1929 tp = tcp_drop(tp, ECONNREFUSED); |
1930 } |
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) { |
1975 tcp_state_change(tp, TCPS_FIN_WAIT_1); |
1976 tp->t_flags &= ~TF_NEEDFIN; 1977 thflags &= ~TH_SYN; 1978 } else { |
1979 tcp_state_change(tp, TCPS_ESTABLISHED); 1980 TCP_PROBE5(connect_established, NULL, tp, 1981 m->m_data, tp, th); |
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); |
1999 tcp_state_change(tp, TCPS_SYN_RECEIVED); |
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 |
2137 tcp_state_change(tp, TCPS_CLOSED); |
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) { |
2382 tcp_state_change(tp, TCPS_FIN_WAIT_1); |
2383 tp->t_flags &= ~TF_NEEDFIN; 2384 } else { |
2385 tcp_state_change(tp, TCPS_ESTABLISHED); 2386 TCP_PROBE5(accept_established, NULL, tp, m->m_data, tp, 2387 th); |
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 } |
2775 tcp_state_change(tp, TCPS_FIN_WAIT_2); |
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: |
3001 tcp_state_change(tp, TCPS_CLOSE_WAIT); |
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: |
3009 tcp_state_change(tp, TCPS_CLOSING); |
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 --- |