Skip to content
Snippets Groups Projects
tcp_ipv4.c 61.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    	if (!tcp_sk(sk)->md5sig_info) {
    		struct tcp_sock *tp = tcp_sk(sk);
    
    		struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
    
    
    		if (!p)
    			return -EINVAL;
    
    		tp->md5sig_info = p;
    
    		sk->sk_route_caps &= ~NETIF_F_GSO_MASK;
    
    	newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
    
    	if (!newkey)
    		return -ENOMEM;
    	return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
    				 newkey, cmd.tcpm_keylen);
    }
    
    static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
    				   __be32 saddr, __be32 daddr,
    				   struct tcphdr *th, int protocol,
    				   int tcplen)
    {
    	struct scatterlist sg[4];
    	__u16 data_len;
    	int block = 0;
    
    	struct tcp_md5sig_pool *hp;
    	struct tcp4_pseudohdr *bp;
    	struct hash_desc *desc;
    	int err;
    	unsigned int nbytes = 0;
    
    	/*
    	 * Okay, so RFC2385 is turned on for this connection,
    	 * so we need to generate the MD5 hash for the packet now.
    	 */
    
    	hp = tcp_get_md5sig_pool();
    	if (!hp)
    		goto clear_hash_noput;
    
    	bp = &hp->md5_blk.ip4;
    	desc = &hp->md5_desc;
    
    	/*
    	 * 1. the TCP pseudo-header (in the order: source IP address,
    	 * destination IP address, zero-padded protocol number, and
    	 * segment length)
    	 */
    	bp->saddr = saddr;
    	bp->daddr = daddr;
    	bp->pad = 0;
    	bp->protocol = protocol;
    	bp->len = htons(tcplen);
    
    	sg_set_buf(&sg[block++], bp, sizeof(*bp));
    	nbytes += sizeof(*bp);
    
    	/* 2. the TCP header, excluding options, and assuming a
    	 * checksum of zero/
    	 */
    	old_checksum = th->check;
    	th->check = 0;
    	sg_set_buf(&sg[block++], th, sizeof(struct tcphdr));
    	nbytes += sizeof(struct tcphdr);
    
    	/* 3. the TCP segment data (if any) */
    	data_len = tcplen - (th->doff << 2);
    	if (data_len > 0) {
    		unsigned char *data = (unsigned char *)th + (th->doff << 2);
    		sg_set_buf(&sg[block++], data, data_len);
    		nbytes += data_len;
    	}
    
    	/* 4. an independently-specified key or password, known to both
    	 * TCPs and presumably connection-specific
    	 */
    	sg_set_buf(&sg[block++], key->key, key->keylen);
    	nbytes += key->keylen;
    
    
    	sg_mark_end(&sg[block - 1]);
    
    	/* Now store the Hash into the packet */
    	err = crypto_hash_init(desc);
    	if (err)
    		goto clear_hash;
    	err = crypto_hash_update(desc, sg, nbytes);
    	if (err)
    		goto clear_hash;
    	err = crypto_hash_final(desc, md5_hash);
    	if (err)
    		goto clear_hash;
    
    	/* Reset header, and free up the crypto */
    	tcp_put_md5sig_pool();
    	th->check = old_checksum;
    
    out:
    	return 0;
    clear_hash:
    	tcp_put_md5sig_pool();
    clear_hash_noput:
    	memset(md5_hash, 0, 16);
    	goto out;
    }
    
    int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
    			 struct sock *sk,
    			 struct dst_entry *dst,
    			 struct request_sock *req,
    			 struct tcphdr *th, int protocol,
    			 int tcplen)
    {
    	__be32 saddr, daddr;
    
    	if (sk) {
    		saddr = inet_sk(sk)->saddr;
    		daddr = inet_sk(sk)->daddr;
    	} else {
    		struct rtable *rt = (struct rtable *)dst;
    		BUG_ON(!rt);
    		saddr = rt->rt_src;
    		daddr = rt->rt_dst;
    	}
    	return tcp_v4_do_calc_md5_hash(md5_hash, key,
    				       saddr, daddr,
    				       th, protocol, tcplen);
    }
    
    EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
    
    
    static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
    
    {
    	/*
    	 * This gets called for each TCP segment that arrives
    	 * so we want to be efficient.
    	 * We have 3 drop cases:
    	 * o No MD5 hash and one expected.
    	 * o MD5 hash and we're not expecting one.
    	 * o MD5 hash and its wrong.
    	 */
    	__u8 *hash_location = NULL;
    	struct tcp_md5sig_key *hash_expected;
    
    	const struct iphdr *iph = ip_hdr(skb);
    
    	struct tcphdr *th = tcp_hdr(skb);
    
    	int length = (th->doff << 2) - sizeof(struct tcphdr);
    
    	int genhash;
    	unsigned char *ptr;
    	unsigned char newhash[16];
    
    	hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
    
    	/*
    	 * If the TCP option length is less than the TCP_MD5SIG
    	 * option length, then we can shortcut
    	 */
    	if (length < TCPOLEN_MD5SIG) {
    		if (hash_expected)
    			return 1;
    		else
    			return 0;
    	}
    
    	/* Okay, we can't shortcut - we have to grub through the options */
    	ptr = (unsigned char *)(th + 1);
    	while (length > 0) {
    		int opcode = *ptr++;
    		int opsize;
    
    		switch (opcode) {
    		case TCPOPT_EOL:
    			goto done_opts;
    		case TCPOPT_NOP:
    			length--;
    			continue;
    		default:
    			opsize = *ptr++;
    			if (opsize < 2)
    				goto done_opts;
    			if (opsize > length)
    				goto done_opts;
    
    			if (opcode == TCPOPT_MD5SIG) {
    				hash_location = ptr;
    				goto done_opts;
    			}
    		}
    		ptr += opsize-2;
    		length -= opsize;
    	}
    done_opts:
    	/* We've parsed the options - do we have a hash? */
    	if (!hash_expected && !hash_location)
    		return 0;
    
    	if (hash_expected && !hash_location) {
    
    		LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
    
    			       "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
    
    			       NIPQUAD(iph->saddr), ntohs(th->source),
    			       NIPQUAD(iph->daddr), ntohs(th->dest));
    
    		return 1;
    	}
    
    	if (!hash_expected && hash_location) {
    
    		LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
    
    			       "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
    
    			       NIPQUAD(iph->saddr), ntohs(th->source),
    			       NIPQUAD(iph->daddr), ntohs(th->dest));
    
    		return 1;
    	}
    
    	/* Okay, so this is hash_expected and hash_location -
    	 * so we need to calculate the checksum.
    	 */
    	genhash = tcp_v4_do_calc_md5_hash(newhash,
    					  hash_expected,
    					  iph->saddr, iph->daddr,
    					  th, sk->sk_protocol,
    					  skb->len);
    
    	if (genhash || memcmp(hash_location, newhash, 16) != 0) {
    		if (net_ratelimit()) {
    			printk(KERN_INFO "MD5 Hash failed for "
    			       "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
    
    			       NIPQUAD(iph->saddr), ntohs(th->source),
    			       NIPQUAD(iph->daddr), ntohs(th->dest),
    
    			       genhash ? " tcp_v4_calc_md5_hash failed" : "");
    		}
    		return 1;
    	}
    	return 0;
    }
    
    #endif
    
    
    struct request_sock_ops tcp_request_sock_ops __read_mostly = {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	.family		=	PF_INET,
    
    	.obj_size	=	sizeof(struct tcp_request_sock),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	.rtx_syn_ack	=	tcp_v4_send_synack,
    
    	.send_ack	=	tcp_v4_reqsk_send_ack,
    	.destructor	=	tcp_v4_reqsk_destructor,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	.send_reset	=	tcp_v4_send_reset,
    };
    
    
    #ifdef CONFIG_TCP_MD5SIG
    
    static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
    
    	.md5_lookup	=	tcp_v4_reqsk_md5_lookup,
    };
    
    static struct timewait_sock_ops tcp_timewait_sock_ops = {
    	.twsk_obj_size	= sizeof(struct tcp_timewait_sock),
    	.twsk_unique	= tcp_twsk_unique,
    
    	.twsk_destructor= tcp_twsk_destructor,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct tcp_options_received tmp_opt;
    
    	struct request_sock *req;
    
    	__be32 saddr = ip_hdr(skb)->saddr;
    	__be32 daddr = ip_hdr(skb)->daddr;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	__u32 isn = TCP_SKB_CB(skb)->when;
    	struct dst_entry *dst = NULL;
    #ifdef CONFIG_SYN_COOKIES
    	int want_cookie = 0;
    #else
    #define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
    #endif
    
    	/* Never answer to SYNs send to broadcast or multicast */
    	if (((struct rtable *)skb->dst)->rt_flags &
    	    (RTCF_BROADCAST | RTCF_MULTICAST))
    		goto drop;
    
    	/* TW buckets are converted to open requests without
    	 * limitations, they conserve resources and peer is
    	 * evidently real one.
    	 */
    
    	if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #ifdef CONFIG_SYN_COOKIES
    		if (sysctl_tcp_syncookies) {
    			want_cookie = 1;
    		} else
    #endif
    		goto drop;
    	}
    
    	/* Accept backlog is full. If we have already queued enough
    	 * of warm entries in syn queue, drop request. It is better than
    	 * clogging syn queue with openreqs with exponentially increasing
    	 * timeout.
    	 */
    
    	if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto drop;
    
    
    	req = reqsk_alloc(&tcp_request_sock_ops);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!req)
    		goto drop;
    
    
    #ifdef CONFIG_TCP_MD5SIG
    	tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
    #endif
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	tcp_clear_options(&tmp_opt);
    	tmp_opt.mss_clamp = 536;
    	tmp_opt.user_mss  = tcp_sk(sk)->rx_opt.user_mss;
    
    	tcp_parse_options(skb, &tmp_opt, 0);
    
    	if (want_cookie) {
    		tcp_clear_options(&tmp_opt);
    		tmp_opt.saw_tstamp = 0;
    	}
    
    	if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
    		/* Some OSes (unknown ones, but I see them on web server, which
    		 * contains information interesting only for windows'
    		 * users) do not send their stamp in SYN. It is easy case.
    		 * We simply do not advertise TS support.
    		 */
    		tmp_opt.saw_tstamp = 0;
    		tmp_opt.tstamp_ok  = 0;
    	}
    	tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
    
    	tcp_openreq_init(req, &tmp_opt, skb);
    
    
    	if (security_inet_conn_request(sk, skb, req))
    		goto drop_and_free;
    
    
    	ireq = inet_rsk(req);
    	ireq->loc_addr = daddr;
    	ireq->rmt_addr = saddr;
    	ireq->opt = tcp_v4_save_options(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!want_cookie)
    
    		TCP_ECN_create_request(req, tcp_hdr(skb));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (want_cookie) {
    #ifdef CONFIG_SYN_COOKIES
    		syn_flood_warning(skb);
    #endif
    		isn = cookie_v4_init_sequence(sk, skb, &req->mss);
    	} else if (!isn) {
    		struct inet_peer *peer = NULL;
    
    		/* VJ's idea. We save last timestamp seen
    		 * from the destination in peer table, when entering
    		 * state TIME-WAIT, and check against it before
    		 * accepting new connection request.
    		 *
    		 * If "isn" is not zero, this request hit alive
    		 * timewait bucket, so that all the necessary checks
    		 * are made in the function processing timewait state.
    		 */
    		if (tmp_opt.saw_tstamp &&
    
    		    tcp_death_row.sysctl_tw_recycle &&
    
    		    (dst = inet_csk_route_req(sk, req)) != NULL &&
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		    (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
    		    peer->v4daddr == saddr) {
    
    			if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			    (s32)(peer->tcp_ts - req->ts_recent) >
    							TCP_PAWS_WINDOW) {
    				NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
    				dst_release(dst);
    				goto drop_and_free;
    			}
    		}
    		/* Kill the following clause, if you dislike this way. */
    		else if (!sysctl_tcp_syncookies &&
    
    			 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			  (sysctl_max_syn_backlog >> 2)) &&
    			 (!peer || !peer->tcp_ts_stamp) &&
    			 (!dst || !dst_metric(dst, RTAX_RTT))) {
    			/* Without syncookies last quarter of
    			 * backlog is filled with destinations,
    			 * proven to be alive.
    			 * It means that we continue to communicate
    			 * to destinations, already remembered
    			 * to the moment of synflood.
    			 */
    
    			LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
    				       "request from %u.%u.%u.%u/%u\n",
    				       NIPQUAD(saddr),
    
    				       ntohs(tcp_hdr(skb)->source));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			dst_release(dst);
    			goto drop_and_free;
    		}
    
    
    		isn = tcp_v4_init_sequence(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (tcp_v4_send_synack(sk, req, dst))
    		goto drop_and_free;
    
    	if (want_cookie) {
    
    		reqsk_free(req);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} else {
    
    		inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	return 0;
    
    drop_and_free:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    drop:
    	return 0;
    }
    
    
    /*
     * The three way handshake has completed - we got a valid synack -
     * now create the new socket.
     */
    struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
    
    				  struct request_sock *req,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				  struct dst_entry *dst)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct inet_sock *newinet;
    	struct tcp_sock *newtp;
    	struct sock *newsk;
    
    #ifdef CONFIG_TCP_MD5SIG
    	struct tcp_md5sig_key *key;
    #endif
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (sk_acceptq_is_full(sk))
    		goto exit_overflow;
    
    
    	if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto exit;
    
    	newsk = tcp_create_openreq_child(sk, req, skb);
    	if (!newsk)
    		goto exit;
    
    
    	newsk->sk_gso_type = SKB_GSO_TCPV4;
    
    	sk_setup_caps(newsk, dst);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	newtp		      = tcp_sk(newsk);
    	newinet		      = inet_sk(newsk);
    
    	ireq		      = inet_rsk(req);
    	newinet->daddr	      = ireq->rmt_addr;
    	newinet->rcv_saddr    = ireq->loc_addr;
    	newinet->saddr	      = ireq->loc_addr;
    	newinet->opt	      = ireq->opt;
    	ireq->opt	      = NULL;
    
    	newinet->mc_index     = inet_iif(skb);
    
    	newinet->mc_ttl	      = ip_hdr(skb)->ttl;
    
    	inet_csk(newsk)->icsk_ext_hdr_len = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (newinet->opt)
    
    		inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	newinet->id = newtp->write_seq ^ jiffies;
    
    
    John Heffner's avatar
    John Heffner committed
    	tcp_mtup_init(newsk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	tcp_sync_mss(newsk, dst_mtu(dst));
    	newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
    	tcp_initialize_rcv_mss(newsk);
    
    
    #ifdef CONFIG_TCP_MD5SIG
    	/* Copy over the MD5 key from the original socket */
    	if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
    		/*
    		 * We're using one, so create a matching key
    		 * on the newsk structure. If we fail to get
    		 * memory, then we end up not copying the key
    		 * across. Shucks.
    		 */
    
    		char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
    		if (newkey != NULL)
    
    			tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
    					  newkey, key->keylen);
    	}
    #endif
    
    
    	__inet_hash(&tcp_hashinfo, newsk, 0);
    
    	__inet_inherit_port(&tcp_hashinfo, sk, newsk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	return newsk;
    
    exit_overflow:
    	NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
    exit:
    	NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
    	dst_release(dst);
    	return NULL;
    }
    
    static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
    {
    
    	struct tcphdr *th = tcp_hdr(skb);
    
    	const struct iphdr *iph = ip_hdr(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct sock *nsk;
    
    	struct request_sock **prev;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Find possible connection requests. */
    
    	struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
    						       iph->saddr, iph->daddr);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (req)
    		return tcp_check_req(sk, skb, req, prev);
    
    
    	nsk = inet_lookup_established(&tcp_hashinfo, iph->saddr, th->source,
    				      iph->daddr, th->dest, inet_iif(skb));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (nsk) {
    		if (nsk->sk_state != TCP_TIME_WAIT) {
    			bh_lock_sock(nsk);
    			return nsk;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return NULL;
    	}
    
    #ifdef CONFIG_SYN_COOKIES
    	if (!th->rst && !th->syn && th->ack)
    		sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
    #endif
    	return sk;
    }
    
    
    static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	const struct iphdr *iph = ip_hdr(skb);
    
    
    	if (skb->ip_summed == CHECKSUM_COMPLETE) {
    
    		if (!tcp_v4_check(skb->len, iph->saddr,
    				  iph->daddr, skb->csum)) {
    
    			skb->ip_summed = CHECKSUM_UNNECESSARY;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
    
    				       skb->len, IPPROTO_TCP, 0);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (skb->len <= 76) {
    
    		return __skb_checksum_complete(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	return 0;
    }
    
    
    /* The socket must have it's spinlock held when we get
     * here.
     *
     * We have a potential double-lock case here, so even when
     * doing backlog processing we use the BH locking scheme.
     * This is because we cannot sleep with the original spinlock
     * held.
     */
    int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
    {
    
    	struct sock *rsk;
    #ifdef CONFIG_TCP_MD5SIG
    	/*
    	 * We really want to reject the packet as early as possible
    	 * if:
    	 *  o We're expecting an MD5'd packet and this is no MD5 tcp option
    	 *  o There is an MD5 option and we're not expecting one
    	 */
    
    	if (tcp_v4_inbound_md5_hash(sk, skb))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
    		TCP_CHECK_TIMER(sk);
    
    		if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			goto reset;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		TCP_CHECK_TIMER(sk);
    		return 0;
    	}
    
    
    	if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto csum_err;
    
    	if (sk->sk_state == TCP_LISTEN) {
    		struct sock *nsk = tcp_v4_hnd_req(sk, skb);
    		if (!nsk)
    			goto discard;
    
    		if (nsk != sk) {
    
    			if (tcp_child_process(sk, nsk, skb)) {
    				rsk = nsk;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				goto reset;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return 0;
    		}
    	}
    
    	TCP_CHECK_TIMER(sk);
    
    	if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto reset;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	TCP_CHECK_TIMER(sk);
    	return 0;
    
    reset:
    
    	tcp_v4_send_reset(rsk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    discard:
    	kfree_skb(skb);
    	/* Be careful here. If this function gets more complicated and
    	 * gcc suffers from register pressure on the x86, sk (in %ebx)
    	 * might be destroyed here. This current version compiles correctly,
    	 * but you have been warned.
    	 */
    	return 0;
    
    csum_err:
    	TCP_INC_STATS_BH(TCP_MIB_INERRS);
    	goto discard;
    }
    
    /*
     *	From tcp_input.c
     */
    
    int tcp_v4_rcv(struct sk_buff *skb)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct tcphdr *th;
    	struct sock *sk;
    	int ret;
    
    	if (skb->pkt_type != PACKET_HOST)
    		goto discard_it;
    
    	/* Count it even if it's bad */
    	TCP_INC_STATS_BH(TCP_MIB_INSEGS);
    
    	if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
    		goto discard_it;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (th->doff < sizeof(struct tcphdr) / 4)
    		goto bad_packet;
    	if (!pskb_may_pull(skb, th->doff * 4))
    		goto discard_it;
    
    	/* An explanation is required here, I think.
    	 * Packet length and doff are validated by header prediction,
    
    Stephen Hemminger's avatar
    Stephen Hemminger committed
    	 * provided case of th->doff==0 is eliminated.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 * So, we defer the checks. */
    
    	if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto bad_packet;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	TCP_SKB_CB(skb)->seq = ntohl(th->seq);
    	TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
    				    skb->len - th->doff * 4);
    	TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
    	TCP_SKB_CB(skb)->when	 = 0;
    
    	TCP_SKB_CB(skb)->flags	 = iph->tos;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	TCP_SKB_CB(skb)->sacked	 = 0;
    
    
    	sk = __inet_lookup(&tcp_hashinfo, iph->saddr, th->source,
    			   iph->daddr, th->dest, inet_iif(skb));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!sk)
    		goto no_tcp_socket;
    
    process:
    	if (sk->sk_state == TCP_TIME_WAIT)
    		goto do_time_wait;
    
    	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
    		goto discard_and_relse;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (sk_filter(sk, skb))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto discard_and_relse;
    
    	skb->dev = NULL;
    
    
    	bh_lock_sock_nested(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	ret = 0;
    	if (!sock_owned_by_user(sk)) {
    
    #ifdef CONFIG_NET_DMA
    		struct tcp_sock *tp = tcp_sk(sk);
    		if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
    			tp->ucopy.dma_chan = get_softnet_dma();
    		if (tp->ucopy.dma_chan)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			ret = tcp_v4_do_rcv(sk, skb);
    
    		else
    #endif
    		{
    			if (!tcp_prequeue(sk, skb))
    			ret = tcp_v4_do_rcv(sk, skb);
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} else
    		sk_add_backlog(sk, skb);
    	bh_unlock_sock(sk);
    
    	sock_put(sk);
    
    	return ret;
    
    no_tcp_socket:
    	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
    		goto discard_it;
    
    	if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
    bad_packet:
    		TCP_INC_STATS_BH(TCP_MIB_INERRS);
    	} else {
    
    		tcp_v4_send_reset(NULL, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    discard_it:
    	/* Discard frame. */
    	kfree_skb(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    discard_and_relse:
    	sock_put(sk);
    	goto discard_it;
    
    do_time_wait:
    	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto discard_it;
    	}
    
    	if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
    		TCP_INC_STATS_BH(TCP_MIB_INERRS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto discard_it;
    	}
    
    	switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	case TCP_TW_SYN: {
    
    		struct sock *sk2 = inet_lookup_listener(&tcp_hashinfo,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (sk2) {
    
    			inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
    			inet_twsk_put(inet_twsk(sk));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			sk = sk2;
    			goto process;
    		}
    		/* Fall through to ACK */
    	}
    	case TCP_TW_ACK:
    		tcp_v4_timewait_ack(sk, skb);
    		break;
    	case TCP_TW_RST:
    		goto no_tcp_socket;
    	case TCP_TW_SUCCESS:;
    	}
    	goto discard_it;
    }
    
    /* VJ's idea. Save last timestamp seen from this destination
     * and hold it at least for normal timewait interval to use for duplicate
     * segment detection in subsequent connections, before they enter synchronized
     * state.
     */
    
    int tcp_v4_remember_stamp(struct sock *sk)
    {
    	struct inet_sock *inet = inet_sk(sk);
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
    	struct inet_peer *peer = NULL;
    	int release_it = 0;
    
    	if (!rt || rt->rt_dst != inet->daddr) {
    		peer = inet_getpeer(inet->daddr, 1);
    		release_it = 1;
    	} else {
    		if (!rt->peer)
    			rt_bind_peer(rt, 1);
    		peer = rt->peer;
    	}
    
    	if (peer) {
    		if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
    
    		    (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		     peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
    			peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
    			peer->tcp_ts = tp->rx_opt.ts_recent;
    		}
    		if (release_it)
    			inet_putpeer(peer);
    		return 1;
    	}
    
    	return 0;
    }
    
    
    int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (peer) {
    
    		const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
    
    		if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
    
    		    (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() &&
    
    		     peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
    			peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
    			peer->tcp_ts	   = tcptw->tw_ts_recent;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    		inet_putpeer(peer);
    		return 1;
    	}
    
    	return 0;
    }
    
    
    struct inet_connection_sock_af_ops ipv4_specific = {
    
    	.queue_xmit	   = ip_queue_xmit,
    	.send_check	   = tcp_v4_send_check,
    	.rebuild_header	   = inet_sk_rebuild_header,
    	.conn_request	   = tcp_v4_conn_request,
    	.syn_recv_sock	   = tcp_v4_syn_recv_sock,
    	.remember_stamp	   = tcp_v4_remember_stamp,
    	.net_header_len	   = sizeof(struct iphdr),
    	.setsockopt	   = ip_setsockopt,
    	.getsockopt	   = ip_getsockopt,
    	.addr2sockaddr	   = inet_csk_addr2sockaddr,
    	.sockaddr_len	   = sizeof(struct sockaddr_in),
    
    #ifdef CONFIG_COMPAT
    
    	.compat_setsockopt = compat_ip_setsockopt,
    	.compat_getsockopt = compat_ip_getsockopt,
    
    #ifdef CONFIG_TCP_MD5SIG
    
    static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
    
    	.md5_lookup		= tcp_v4_md5_lookup,
    	.calc_md5_hash		= tcp_v4_calc_md5_hash,
    	.md5_add		= tcp_v4_md5_add_func,
    	.md5_parse		= tcp_v4_parse_md5_keys,
    };
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* NOTE: A lot of things set to zero explicitly by call to
     *       sk_alloc() so need not be done here.
     */
    static int tcp_v4_init_sock(struct sock *sk)
    {
    
    	struct inet_connection_sock *icsk = inet_csk(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	skb_queue_head_init(&tp->out_of_order_queue);
    	tcp_init_xmit_timers(sk);
    	tcp_prequeue_init(tp);
    
    
    	icsk->icsk_rto = TCP_TIMEOUT_INIT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	tp->mdev = TCP_TIMEOUT_INIT;
    
    	/* So many TCP implementations out there (incorrectly) count the
    	 * initial SYN frame in their delayed-ACK and congestion control
    	 * algorithms that we must have the following bandaid to talk
    	 * efficiently to them.  -DaveM
    	 */
    	tp->snd_cwnd = 2;
    
    	/* See draft-stevens-tcpca-spec-01 for discussion of the
    	 * initialization of these values.
    	 */
    	tp->snd_ssthresh = 0x7fffffff;	/* Infinity */
    	tp->snd_cwnd_clamp = ~0;
    
    	tp->mss_cache = 536;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	tp->reordering = sysctl_tcp_reordering;
    
    	icsk->icsk_ca_ops = &tcp_init_congestion_ops;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	sk->sk_state = TCP_CLOSE;
    
    	sk->sk_write_space = sk_stream_write_space;
    	sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
    
    
    	icsk->icsk_sync_mss = tcp_sync_mss;
    
    #ifdef CONFIG_TCP_MD5SIG
    	tp->af_specific = &tcp_sock_ipv4_specific;
    #endif
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	sk->sk_sndbuf = sysctl_tcp_wmem[1];
    	sk->sk_rcvbuf = sysctl_tcp_rmem[1];
    
    	atomic_inc(&tcp_sockets_allocated);
    
    	return 0;
    }
    
    int tcp_v4_destroy_sock(struct sock *sk)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	tcp_clear_xmit_timers(sk);
    
    
    	tcp_cleanup_congestion_control(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Cleanup up the write buffer. */
    
    	tcp_write_queue_purge(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Cleans up our, hopefully empty, out_of_order_queue. */
    
    	__skb_queue_purge(&tp->out_of_order_queue);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    #ifdef CONFIG_TCP_MD5SIG
    	/* Clean up the MD5 key list, if any */
    	if (tp->md5sig_info) {
    		tcp_v4_clear_md5_list(sk);
    		kfree(tp->md5sig_info);
    		tp->md5sig_info = NULL;
    	}
    #endif
    
    
    #ifdef CONFIG_NET_DMA
    	/* Cleans up our sk_async_wait_queue */
    
    	__skb_queue_purge(&sk->sk_async_wait_queue);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Clean prequeue, it must be empty really */
    	__skb_queue_purge(&tp->ucopy.prequeue);
    
    	/* Clean up a referenced TCP bind bucket. */
    
    	if (inet_csk(sk)->icsk_bind_hash)
    
    		inet_put_port(&tcp_hashinfo, sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/*
    	 * If sendmsg cached page exists, toss it.
    	 */
    	if (sk->sk_sndmsg_page) {
    		__free_page(sk->sk_sndmsg_page);
    		sk->sk_sndmsg_page = NULL;
    	}
    
    	atomic_dec(&tcp_sockets_allocated);
    
    	return 0;
    }
    
    EXPORT_SYMBOL(tcp_v4_destroy_sock);
    
    #ifdef CONFIG_PROC_FS
    /* Proc filesystem TCP sock list dumping. */
    
    
    static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	return hlist_empty(head) ? NULL :
    
    		list_entry(head->first, struct inet_timewait_sock, tw_node);
    
    static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	return tw->tw_node.next ?
    		hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
    }
    
    static void *listening_get_next(struct seq_file *seq, void *cur)
    {
    
    	struct inet_connection_sock *icsk;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct hlist_node *node;
    	struct sock *sk = cur;
    	struct tcp_iter_state* st = seq->private;
    
    	if (!sk) {
    		st->bucket = 0;
    
    		sk = sk_head(&tcp_hashinfo.listening_hash[0]);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto get_sk;
    	}
    
    	++st->num;
    
    	if (st->state == TCP_SEQ_STATE_OPENREQ) {
    
    		struct request_sock *req = cur;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		icsk = inet_csk(st->syn_wait_sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		req = req->dl_next;
    		while (1) {
    			while (req) {
    
    				if (req->rsk_ops->family == st->family) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					cur = req;
    					goto out;
    				}
    				req = req->dl_next;
    			}
    
    			if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				break;
    get_req:
    
    			req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    		sk	  = sk_next(st->syn_wait_sk);
    		st->state = TCP_SEQ_STATE_LISTENING;
    
    		read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} else {
    
    		icsk = inet_csk(sk);
    
    		read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
    		if (reqsk_queue_len(&icsk->icsk_accept_queue))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			goto start_req;