Skip to content
Snippets Groups Projects
tcp_input.c 163 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
     *	- Unexpected TCP option.
     *
    
     *	When these conditions are not satisfied it drops into a standard
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *	receive procedure patterned after RFC793 to handle all cases.
     *	The first three cases are guaranteed by proper pred_flags setting,
    
     *	the rest is checked inline. Fast processing is turned on in
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *	tcp_data_queue when everything is OK.
     */
    int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
    
    			const struct tcphdr *th, unsigned int len)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    
    	if (unlikely(sk->sk_rx_dst == NULL))
    		inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/*
    	 *	Header prediction.
    
    	 *	The code loosely follows the one in the famous
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 *	"30 instruction TCP receive" Van Jacobson mail.
    
    	 *
    	 *	Van's trick is to deposit buffers into socket queue
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 *	on a device interrupt, to call tcp_recv function
    	 *	on the receive process context and checksum and copy
    	 *	the buffer to user space. smart...
    	 *
    
    	 *	Our current scheme is not silly either but we take the
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 *	extra cost of the net_bh soft interrupt processing...
    	 *	We do checksum and copy also but from device to kernel.
    	 */
    
    	tp->rx_opt.saw_tstamp = 0;
    
    	/*	pred_flags is 0xS?10 << 16 + snd_wnd
    
    Stephen Hemminger's avatar
    Stephen Hemminger committed
    	 *	if header_prediction is to be made
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 *	'S' will always be tp->tcp_header_len >> 2
    	 *	'?' will be 0 for the fast path, otherwise pred_flags is 0 to
    
    	 *  turn it off	(when there are holes in the receive
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 *	 space for instance)
    	 *	PSH flag is ignored.
    	 */
    
    	if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
    
    	    TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
    	    !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		int tcp_header_len = tp->tcp_header_len;
    
    		/* Timestamp header prediction: tcp_header_len
    		 * is automatically equal to th->doff*4 due to pred_flags
    		 * match.
    		 */
    
    		/* Check timestamp */
    		if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
    			/* No? Slow path! */
    
    			if (!tcp_parse_aligned_timestamp(tp, th))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				goto slow_path;
    
    			/* If PAWS failed, check it more carefully in slow path */
    			if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
    				goto slow_path;
    
    			/* DO NOT update ts_recent here, if checksum fails
    			 * and timestamp was corrupted part, it will result
    			 * in a hung connection since we will drop all
    			 * future packets due to the PAWS test.
    			 */
    		}
    
    		if (len <= tcp_header_len) {
    			/* Bulk data transfer: sender */
    			if (len == tcp_header_len) {
    				/* Predicted packet is in window by definition.
    				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
    				 * Hence, check seq<=rcv_wup reduces to:
    				 */
    				if (tcp_header_len ==
    				    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
    				    tp->rcv_nxt == tp->rcv_wup)
    					tcp_store_ts_recent(tp);
    
    				/* We know that such packets are checksummed
    				 * on entry.
    				 */
    				tcp_ack(sk, skb, 0);
    
    				__kfree_skb(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				return 0;
    			} else { /* Header too small */
    
    				TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				goto discard;
    			}
    		} else {
    			int eaten = 0;
    
    			int copied_early = 0;
    
    			bool fragstolen = false;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			if (tp->copied_seq == tp->rcv_nxt &&
    			    len - tcp_header_len <= tp->ucopy.len) {
    #ifdef CONFIG_NET_DMA
    
    				if (tp->ucopy.task == current &&
    				    sock_owned_by_user(sk) &&
    				    tcp_dma_try_early_copy(sk, skb, tcp_header_len)) {
    
    					copied_early = 1;
    					eaten = 1;
    				}
    #endif
    
    				if (tp->ucopy.task == current &&
    				    sock_owned_by_user(sk) && !copied_early) {
    
    					__set_current_state(TASK_RUNNING);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    					if (!tcp_copy_to_iovec(sk, skb, tcp_header_len))
    						eaten = 1;
    				}
    				if (eaten) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					/* Predicted packet is in window by definition.
    					 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
    					 * Hence, check seq<=rcv_wup reduces to:
    					 */
    					if (tcp_header_len ==
    					    (sizeof(struct tcphdr) +
    					     TCPOLEN_TSTAMP_ALIGNED) &&
    					    tp->rcv_nxt == tp->rcv_wup)
    						tcp_store_ts_recent(tp);
    
    
    					tcp_rcv_rtt_measure_ts(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    					__skb_pull(skb, tcp_header_len);
    					tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
    
    					NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				}
    
    				if (copied_early)
    					tcp_cleanup_rbuf(sk, skb->len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    			if (!eaten) {
    				if (tcp_checksum_complete_user(sk, skb))
    					goto csum_error;
    
    
    				if ((int)skb->truesize > sk->sk_forward_alloc)
    					goto step5;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				/* Predicted packet is in window by definition.
    				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
    				 * Hence, check seq<=rcv_wup reduces to:
    				 */
    				if (tcp_header_len ==
    				    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
    				    tp->rcv_nxt == tp->rcv_wup)
    					tcp_store_ts_recent(tp);
    
    
    				tcp_rcv_rtt_measure_ts(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    				/* Bulk data transfer: receiver */
    
    				eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
    						      &fragstolen);
    
    			tcp_event_data_recv(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    			if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
    				/* Well, only one small jumplet in fast path... */
    				tcp_ack(sk, skb, FLAG_DATA);
    
    				if (!inet_csk_ack_scheduled(sk))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					goto no_ack;
    			}
    
    
    			if (!copied_early || tp->rcv_nxt != tp->rcv_wup)
    				__tcp_ack_snd_check(sk, 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    no_ack:
    
    #ifdef CONFIG_NET_DMA
    			if (copied_early)
    				__skb_queue_tail(&sk->sk_async_wait_queue, skb);
    			else
    #endif
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (eaten)
    
    				kfree_skb_partial(skb, fragstolen);
    
    			sk->sk_data_ready(sk, 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return 0;
    		}
    	}
    
    slow_path:
    
    	if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto csum_error;
    
    
    	if (!th->ack && !th->rst)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/*
    	 *	Standard slow path.
    	 */
    
    
    	if (!tcp_validate_incoming(sk, skb, th, 1))
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    step5:
    
    	if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	tcp_rcv_rtt_measure_ts(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Process urgent data. */
    	tcp_urg(sk, skb, th);
    
    	/* step 7: process the segment text */
    	tcp_data_queue(sk, skb);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	tcp_ack_snd_check(sk);
    	return 0;
    
    csum_error:
    
    	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS);
    
    	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    discard:
    	__kfree_skb(skb);
    	return 0;
    }
    
    EXPORT_SYMBOL(tcp_rcv_established);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Pavel Emelyanov's avatar
    Pavel Emelyanov committed
    void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct inet_connection_sock *icsk = inet_csk(sk);
    
    	tcp_set_state(sk, TCP_ESTABLISHED);
    
    
    	if (skb != NULL) {
    
    		icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
    
    Pavel Emelyanov's avatar
    Pavel Emelyanov committed
    		security_inet_conn_established(sk, skb);
    
    Pavel Emelyanov's avatar
    Pavel Emelyanov committed
    
    	/* Make sure socket is routed, for correct metrics.  */
    	icsk->icsk_af_ops->rebuild_header(sk);
    
    	tcp_init_metrics(sk);
    
    	tcp_init_congestion_control(sk);
    
    	/* Prevent spurious tcp_cwnd_restart() on first data
    	 * packet.
    	 */
    	tp->lsndtime = tcp_time_stamp;
    
    	tcp_init_buffer_space(sk);
    
    	if (sock_flag(sk, SOCK_KEEPOPEN))
    		inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
    
    	if (!tp->rx_opt.snd_wscale)
    		__tcp_fast_path_on(tp, tp->snd_wnd);
    	else
    		tp->pred_flags = 0;
    
    	if (!sock_flag(sk, SOCK_DEAD)) {
    		sk->sk_state_change(sk);
    		sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
    	}
    }
    
    
    static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
    				    struct tcp_fastopen_cookie *cookie)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL;
    
    	u16 mss = tp->rx_opt.mss_clamp;
    
    
    	if (mss == tp->rx_opt.user_mss) {
    		struct tcp_options_received opt;
    
    		/* Get original SYNACK MSS value if user MSS sets mss_clamp */
    		tcp_clear_options(&opt);
    		opt.user_mss = opt.mss_clamp = 0;
    
    Christoph Paasch's avatar
    Christoph Paasch committed
    		tcp_parse_options(synack, &opt, 0, NULL);
    
    	if (!tp->syn_fastopen)  /* Ignore an unsolicited cookie */
    		cookie->len = -1;
    
    
    	/* The SYN-ACK neither has cookie nor acknowledges the data. Presumably
    	 * the remote receives only the retransmitted (regular) SYNs: either
    	 * the original SYN-data or the corresponding SYN-ACK is lost.
    	 */
    
    	syn_drop = (cookie->len <= 0 && data && tp->total_retrans);
    
    
    	tcp_fastopen_cache_set(sk, mss, cookie, syn_drop);
    
    
    	if (data) { /* Retransmit unacked data in SYN */
    
    		tcp_for_write_queue_from(data, sk) {
    			if (data == tcp_send_head(sk) ||
    			    __tcp_retransmit_skb(sk, data))
    				break;
    		}
    
    		tcp_rearm_rto(sk);
    		return true;
    	}
    
    	tp->syn_data_acked = tp->syn_data;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
    
    					 const struct tcphdr *th, unsigned int len)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct inet_connection_sock *icsk = inet_csk(sk);
    
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	struct tcp_fastopen_cookie foc = { .len = -1 };
    
    	int saved_clamp = tp->rx_opt.mss_clamp;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Christoph Paasch's avatar
    Christoph Paasch committed
    	tcp_parse_options(skb, &tp->rx_opt, 0, &foc);
    
    	if (tp->rx_opt.saw_tstamp)
    		tp->rx_opt.rcv_tsecr -= tp->tsoffset;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (th->ack) {
    		/* rfc793:
    		 * "If the state is SYN-SENT then
    		 *    first check the ACK bit
    		 *      If the ACK bit is set
    		 *	  If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
    		 *        a reset (unless the RST bit is set, if so drop
    		 *        the segment and return)"
    		 */
    
    		if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
    		    after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			goto reset_and_undo;
    
    		if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
    		    !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
    			     tcp_time_stamp)) {
    
    			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			goto reset_and_undo;
    		}
    
    		/* Now ACK is acceptable.
    		 *
    		 * "If the RST bit is set
    		 *    If the ACK was acceptable then signal the user "error:
    		 *    connection reset", drop the segment, enter CLOSED state,
    		 *    delete TCB, and return."
    		 */
    
    		if (th->rst) {
    			tcp_reset(sk);
    			goto discard;
    		}
    
    		/* rfc793:
    		 *   "fifth, if neither of the SYN or RST bits is set then
    		 *    drop the segment and return."
    		 *
    		 *    See note below!
    		 *                                        --ANK(990513)
    		 */
    		if (!th->syn)
    			goto discard_and_undo;
    
    		/* rfc793:
    		 *   "If the SYN bit is on ...
    		 *    are acceptable then ...
    		 *    (our SYN has been ACKed), change the connection
    		 *    state to ESTABLISHED..."
    		 */
    
    		TCP_ECN_rcv_synack(tp, th);
    
    
    		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		tcp_ack(sk, skb, FLAG_SLOWPATH);
    
    		/* Ok.. it's good. Set up sequence numbers and
    		 * move to established.
    		 */
    		tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
    		tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
    
    		/* RFC1323: The window in SYN & SYN/ACK segments is
    		 * never scaled.
    		 */
    		tp->snd_wnd = ntohs(th->window);
    
    		if (!tp->rx_opt.wscale_ok) {
    			tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
    			tp->window_clamp = min(tp->window_clamp, 65535U);
    		}
    
    		if (tp->rx_opt.saw_tstamp) {
    			tp->rx_opt.tstamp_ok	   = 1;
    			tp->tcp_header_len =
    				sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
    			tp->advmss	    -= TCPOLEN_TSTAMP_ALIGNED;
    			tcp_store_ts_recent(tp);
    		} else {
    			tp->tcp_header_len = sizeof(struct tcphdr);
    		}
    
    
    		if (tcp_is_sack(tp) && sysctl_tcp_fack)
    			tcp_enable_fack(tp);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    John Heffner's avatar
    John Heffner committed
    		tcp_mtup_init(sk);
    
    		tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		tcp_initialize_rcv_mss(sk);
    
    		/* Remember, tcp_poll() does not lock socket!
    		 * Change state from SYN-SENT only after copied_seq
    		 * is initialized. */
    		tp->copied_seq = tp->rcv_nxt;
    
    		smp_mb();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Pavel Emelyanov's avatar
    Pavel Emelyanov committed
    		tcp_finish_connect(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if ((tp->syn_fastopen || tp->syn_data) &&
    		    tcp_rcv_fastopen_synack(sk, skb, &foc))
    
    		if (sk->sk_write_pending ||
    		    icsk->icsk_accept_queue.rskq_defer_accept ||
    		    icsk->icsk_ack.pingpong) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			/* Save one ACK. Data will be ready after
    			 * several ticks, if write_pending is set.
    			 *
    			 * It may be deleted, but with this feature tcpdumps
    			 * look so _wonderfully_ clever, that I was not able
    			 * to stand against the temptation 8)     --ANK
    			 */
    
    			inet_csk_schedule_ack(sk);
    
    			icsk->icsk_ack.lrcvtime = tcp_time_stamp;
    
    			tcp_enter_quickack_mode(sk);
    
    			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
    						  TCP_DELACK_MAX, TCP_RTO_MAX);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    discard:
    			__kfree_skb(skb);
    			return 0;
    		} else {
    			tcp_send_ack(sk);
    		}
    		return -1;
    	}
    
    	/* No ACK in the segment */
    
    	if (th->rst) {
    		/* rfc793:
    		 * "If the RST bit is set
    		 *
    		 *      Otherwise (no ACK) drop the segment and return."
    		 */
    
    		goto discard_and_undo;
    	}
    
    	/* PAWS check. */
    
    	if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
    
    	    tcp_paws_reject(&tp->rx_opt, 0))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto discard_and_undo;
    
    	if (th->syn) {
    		/* We see SYN without ACK. It is attempt of
    		 * simultaneous connect with crossed SYNs.
    		 * Particularly, it can be connect to self.
    		 */
    		tcp_set_state(sk, TCP_SYN_RECV);
    
    		if (tp->rx_opt.saw_tstamp) {
    			tp->rx_opt.tstamp_ok = 1;
    			tcp_store_ts_recent(tp);
    			tp->tcp_header_len =
    				sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
    		} else {
    			tp->tcp_header_len = sizeof(struct tcphdr);
    		}
    
    		tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
    		tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
    
    		/* RFC1323: The window in SYN & SYN/ACK segments is
    		 * never scaled.
    		 */
    		tp->snd_wnd    = ntohs(th->window);
    		tp->snd_wl1    = TCP_SKB_CB(skb)->seq;
    		tp->max_window = tp->snd_wnd;
    
    		TCP_ECN_rcv_syn(tp, th);
    
    
    John Heffner's avatar
    John Heffner committed
    		tcp_mtup_init(sk);
    
    		tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		tcp_initialize_rcv_mss(sk);
    
    		tcp_send_synack(sk);
    #if 0
    		/* Note, we could accept data and URG from this segment.
    
    		 * There are no obstacles to make this (except that we must
    		 * either change tcp_recvmsg() to prevent it from returning data
    		 * before 3WHS completes per RFC793, or employ TCP Fast Open).
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		 *
    		 * However, if we ignore data in ACKless segments sometimes,
    		 * we have no reasons to accept it sometimes.
    		 * Also, seems the code doing it in step6 of tcp_rcv_state_process
    		 * is not flawless. So, discard packet for sanity.
    		 * Uncomment this return to process the data.
    		 */
    		return -1;
    #else
    		goto discard;
    #endif
    	}
    	/* "fifth, if neither of the SYN or RST bits is set then
    	 * drop the segment and return."
    	 */
    
    discard_and_undo:
    	tcp_clear_options(&tp->rx_opt);
    	tp->rx_opt.mss_clamp = saved_clamp;
    	goto discard;
    
    reset_and_undo:
    	tcp_clear_options(&tp->rx_opt);
    	tp->rx_opt.mss_clamp = saved_clamp;
    	return 1;
    }
    
    /*
     *	This function implements the receiving procedure of RFC 793 for
    
     *	all states except ESTABLISHED and TIME_WAIT.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *	It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
     *	address independent.
     */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
    
    			  const struct tcphdr *th, unsigned int len)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	struct inet_connection_sock *icsk = inet_csk(sk);
    
    	struct request_sock *req;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int queued = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	tp->rx_opt.saw_tstamp = 0;
    
    	switch (sk->sk_state) {
    	case TCP_CLOSE:
    		goto discard;
    
    	case TCP_LISTEN:
    
    		if (th->ack)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return 1;
    
    
    		if (th->rst)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			goto discard;
    
    
    		if (th->syn) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    			if (th->fin)
    				goto discard;
    
    			if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				return 1;
    
    
    			/* Now we have several options: In theory there is
    			 * nothing else in the frame. KA9Q has an option to
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			 * send data with the syn, BSD accepts data with the
    
    			 * syn up to the [to be] advertised window and
    			 * Solaris 2.1 gives you a protocol error. For now
    			 * we just ignore it, that fits the spec precisely
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			 * and avoids incompatibilities. It would be nice in
    			 * future to drop through and process the data.
    			 *
    
    			 * Now that TTCP is starting to be used we ought to
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			 * queue this data.
    			 * But, this leaves one open to an easy denial of
    
    			 * service attack, and SYN cookies can't defend
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			 * against this problem. So, we drop the data
    
    			 * in the interest of security over speed unless
    			 * it's still in use.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			 */
    
    			kfree_skb(skb);
    			return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    		goto discard;
    
    	case TCP_SYN_SENT:
    		queued = tcp_rcv_synsent_state_process(sk, skb, th, len);
    		if (queued >= 0)
    			return queued;
    
    		/* Do step6 onward by hand. */
    		tcp_urg(sk, skb, th);
    		__kfree_skb(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return 0;
    	}
    
    
    	req = tp->fastopen_rsk;
    	if (req != NULL) {
    
    		WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
    
    		    sk->sk_state != TCP_FIN_WAIT1);
    
    		if (tcp_check_req(sk, skb, req, NULL, true) == NULL)
    			goto discard;
    
    	if (!th->ack && !th->rst)
    
    	if (!tcp_validate_incoming(sk, skb, th, 0))
    
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* step 5: check the ACK field */
    
    	acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
    				      FLAG_UPDATE_TS_RECENT) > 0;
    
    	switch (sk->sk_state) {
    	case TCP_SYN_RECV:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		/* Once we leave TCP_SYN_RECV, we no longer need req
    		 * so release it.
    		 */
    		if (req) {
    			tp->total_retrans = req->num_retrans;
    			reqsk_fastopen_remove(sk, req, false);
    		} else {
    			/* Make sure socket is routed, for correct metrics. */
    			icsk->icsk_af_ops->rebuild_header(sk);
    			tcp_init_congestion_control(sk);
    
    			tcp_mtup_init(sk);
    			tcp_init_buffer_space(sk);
    			tp->copied_seq = tp->rcv_nxt;
    		}
    		smp_mb();
    		tcp_set_state(sk, TCP_ESTABLISHED);
    		sk->sk_state_change(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		/* Note, that this wakeup is only for marginal crossed SYN case.
    		 * Passively open sockets are not waked up, because
    		 * sk->sk_sleep == NULL and sk->sk_socket == NULL.
    		 */
    		if (sk->sk_socket)
    			sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
    
    		tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
    		tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
    		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
    
    		tcp_synack_rtt_meas(sk, req);
    
    
    		if (tp->rx_opt.tstamp_ok)
    			tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
    
    		if (req) {
    			/* Re-arm the timer because data may have been sent out.
    			 * This is similar to the regular data transmission case
    			 * when new data has just been ack'ed.
    			 *
    			 * (TFO) - we could try to be more aggressive and
    			 * retransmitting any data sooner based on when they
    			 * are sent out.
    
    			tcp_rearm_rto(sk);
    		} else
    			tcp_init_metrics(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		/* Prevent spurious tcp_cwnd_restart() on first data packet */
    		tp->lsndtime = tcp_time_stamp;
    
    		tcp_initialize_rcv_mss(sk);
    		tcp_fast_path_on(tp);
    
    	case TCP_FIN_WAIT1: {
    		struct dst_entry *dst;
    		int tmo;
    
    
    		/* If we enter the TCP_FIN_WAIT1 state and we are a
    		 * Fast Open socket and this is the first acceptable
    		 * ACK we have received, this would have acknowledged
    		 * our SYNACK so stop the SYNACK timer.
    		 */
    		if (req != NULL) {
    			/* Return RST if ack_seq is invalid.
    			 * Note that RFC793 only says to generate a
    			 * DUPACK for it but for TCP Fast Open it seems
    			 * better to treat this case like TCP_SYN_RECV
    			 * above.
    
    			if (!acceptable)
    				return 1;
    			/* We no longer need the request sock. */
    			reqsk_fastopen_remove(sk, req, false);
    			tcp_rearm_rto(sk);
    		}
    
    		if (tp->snd_una != tp->write_seq)
    			break;
    
    		tcp_set_state(sk, TCP_FIN_WAIT2);
    		sk->sk_shutdown |= SEND_SHUTDOWN;
    
    		dst = __sk_dst_get(sk);
    		if (dst)
    			dst_confirm(dst);
    
    		if (!sock_flag(sk, SOCK_DEAD)) {
    			/* Wake up lingering close() */
    			sk->sk_state_change(sk);
    			break;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if (tp->linger2 < 0 ||
    		    (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
    		     after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
    			tcp_done(sk);
    			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
    			return 1;
    		}
    
    		tmo = tcp_fin_time(sk);
    		if (tmo > TCP_TIMEWAIT_LEN) {
    			inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
    		} else if (th->fin || sock_owned_by_user(sk)) {
    			/* Bad case. We could lose such FIN otherwise.
    			 * It is not a big problem, but it looks confusing
    			 * and not so rare event. We still can lose it now,
    			 * if it spins in bh_lock_sock(), but it is really
    			 * marginal case.
    			 */
    			inet_csk_reset_keepalive_timer(sk, tmo);
    		} else {
    			tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
    			goto discard;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case TCP_CLOSING:
    		if (tp->snd_una == tp->write_seq) {
    			tcp_time_wait(sk, TCP_TIME_WAIT, 0);
    			goto discard;
    		}
    		break;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case TCP_LAST_ACK:
    		if (tp->snd_una == tp->write_seq) {
    			tcp_update_metrics(sk);
    			tcp_done(sk);
    			goto discard;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* step 6: check the URG bit */
    	tcp_urg(sk, skb, th);
    
    	/* step 7: process the segment text */
    	switch (sk->sk_state) {
    	case TCP_CLOSE_WAIT:
    	case TCP_CLOSING:
    	case TCP_LAST_ACK:
    		if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
    			break;
    	case TCP_FIN_WAIT1:
    	case TCP_FIN_WAIT2:
    		/* RFC 793 says to queue data in these states,
    
    		 * RFC 1122 says we MUST send a reset.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		 * BSD 4.4 also does reset.
    		 */
    		if (sk->sk_shutdown & RCV_SHUTDOWN) {
    			if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
    			    after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
    
    				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				tcp_reset(sk);
    				return 1;
    			}
    		}
    		/* Fall through */
    
    	case TCP_ESTABLISHED:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		tcp_data_queue(sk, skb);
    		queued = 1;
    		break;
    	}
    
    	/* tcp_data could move socket to TIME-WAIT */
    	if (sk->sk_state != TCP_CLOSE) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		tcp_ack_snd_check(sk);
    	}
    
    
    	if (!queued) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    discard:
    		__kfree_skb(skb);
    	}
    	return 0;
    }
    EXPORT_SYMBOL(tcp_rcv_state_process);