Skip to content
Snippets Groups Projects
tcp_input.c 175 KiB
Newer Older
  • Learn to ignore specific revisions
  • 					tcp_mtup_init(sk);
    					tcp_init_buffer_space(sk);
    					tp->copied_seq = tp->rcv_nxt;
    				}
    
    				smp_mb();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				tcp_set_state(sk, TCP_ESTABLISHED);
    				sk->sk_state_change(sk);
    
    				/* 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);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    				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);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    				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 retranmitting any data
    					 * sooner based on when they were 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);
    			} else {
    				return 1;
    			}
    			break;
    
    		case TCP_FIN_WAIT1:
    
    			/* 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);
    			}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (tp->snd_una == tp->write_seq) {
    
    				struct dst_entry *dst;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				tcp_set_state(sk, TCP_FIN_WAIT2);
    				sk->sk_shutdown |= SEND_SHUTDOWN;
    
    
    				dst = __sk_dst_get(sk);
    				if (dst)
    					dst_confirm(dst);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    				if (!sock_flag(sk, SOCK_DEAD))
    					/* Wake up lingering close() */
    					sk->sk_state_change(sk);
    				else {
    					int tmo;
    
    					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);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    						return 1;
    					}
    
    
    					tmo = tcp_fin_time(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					if (tmo > TCP_TIMEWAIT_LEN) {
    
    						inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					} 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);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					} else {
    						tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
    						goto discard;
    					}
    				}
    			}
    			break;
    
    		case TCP_CLOSING:
    			if (tp->snd_una == tp->write_seq) {
    				tcp_time_wait(sk, TCP_TIME_WAIT, 0);
    				goto discard;
    			}
    			break;
    
    		case TCP_LAST_ACK:
    			if (tp->snd_una == tp->write_seq) {
    				tcp_update_metrics(sk);
    				tcp_done(sk);
    				goto discard;
    			}
    			break;
    		}
    	} else
    		goto discard;
    
    
    	/* ts_recent update must be made after we are sure that the packet
    	 * is in window.
    	 */
    	tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
    
    
    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);