void dccp_set_state(struct sock *sk, const int state)
{
	const int oldstate = sk->sk_state;

	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
		      dccp_state_name(oldstate), dccp_state_name(state));
	WARN_ON(state == oldstate);

	switch (state) {
	case DCCP_OPEN:
		if (oldstate != DCCP_OPEN)
			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
		break;

	case DCCP_CLOSED:
		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
		    oldstate == DCCP_CLOSING)
			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);

		sk->sk_prot->unhash(sk);
		if (inet_csk(sk)->icsk_bind_hash != NULL &&
		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
			inet_put_port(sk);
		/* fall through */
	default:
		if (oldstate == DCCP_OPEN)
			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
	}

	/* Change state AFTER socket is unhashed to avoid closed
	 * socket sitting in hash tables.
	 */
	sk->sk_state = state;
}
Exemple #2
0
void dccp_set_state(struct sock *sk, const int state)
{
	const int oldstate = sk->sk_state;

	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
		      dccp_state_name(oldstate), dccp_state_name(state));
	WARN_ON(state == oldstate);

	switch (state) {
	case DCCP_OPEN:
		if (oldstate != DCCP_OPEN)
			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
		
		if (oldstate == DCCP_PARTOPEN)
			dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
		break;

	case DCCP_CLOSED:
		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
		    oldstate == DCCP_CLOSING)
			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);

		sk->sk_prot->unhash(sk);
		if (inet_csk(sk)->icsk_bind_hash != NULL &&
		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
			inet_put_port(sk);
		
	default:
		if (oldstate == DCCP_OPEN)
			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
	}

	
	sk->sk_state = state;
}
Exemple #3
0
/**
 * dccp_parse_options  -  Parse DCCP options present in @skb
 * @sk: client|server|listening dccp socket (when @dreq != NULL)
 * @dreq: request socket to use during connection setup, or NULL
 */
int dccp_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
                       struct sk_buff *skb)
{
    struct dccp_sock *dp = dccp_sk(sk);
    const struct dccp_hdr *dh = dccp_hdr(skb);
    const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
    unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
    unsigned char *opt_ptr = options;
    const unsigned char *opt_end = (unsigned char *)dh +
                                   (dh->dccph_doff * 4);
    struct dccp_options_received *opt_recv = &dp->dccps_options_received;
    unsigned char opt, len;
    unsigned char *uninitialized_var(value);
    u32 elapsed_time;
    __be32 opt_val;
    int rc;
    int mandatory = 0;

    memset(opt_recv, 0, sizeof(*opt_recv));

    opt = len = 0;
    while (opt_ptr != opt_end) {
        opt   = *opt_ptr++;
        len   = 0;
        value = NULL;

        /* Check if this isn't a single byte option */
        if (opt > DCCPO_MAX_RESERVED) {
            if (opt_ptr == opt_end)
                goto out_nonsensical_length;

            len = *opt_ptr++;
            if (len < 2)
                goto out_nonsensical_length;
            /*
             * Remove the type and len fields, leaving
             * just the value size
             */
            len	-= 2;
            value	= opt_ptr;
            opt_ptr += len;

            if (opt_ptr > opt_end)
                goto out_nonsensical_length;
        }

        /*
         * CCID-specific options are ignored during connection setup, as
         * negotiation may still be in progress (see RFC 4340, 10.3).
         * The same applies to Ack Vectors, as these depend on the CCID.
         */
        if (dreq != NULL && (opt >= DCCPO_MIN_RX_CCID_SPECIFIC ||
                             opt == DCCPO_ACK_VECTOR_0 || opt == DCCPO_ACK_VECTOR_1))
            goto ignore_option;

        switch (opt) {
        case DCCPO_PADDING:
            break;
        case DCCPO_MANDATORY:
            if (mandatory)
                goto out_invalid_option;
            if (pkt_type != DCCP_PKT_DATA)
                mandatory = 1;
            break;
        case DCCPO_NDP_COUNT:
            if (len > 6)
                goto out_invalid_option;

            opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
            dccp_pr_debug("%s opt: NDP count=%llu\n", dccp_role(sk),
                          (unsigned long long)opt_recv->dccpor_ndp);
            break;
        case DCCPO_CHANGE_L ... DCCPO_CONFIRM_R:
            if (pkt_type == DCCP_PKT_DATA)      /* RFC 4340, 6 */
                break;
            if (len == 0)
                goto out_invalid_option;
            rc = dccp_feat_parse_options(sk, dreq, mandatory, opt,
                                         *value, value + 1, len - 1);
            if (rc)
                goto out_featneg_failed;
            break;
        case DCCPO_TIMESTAMP:
            if (len != 4)
                goto out_invalid_option;
            /*
             * RFC 4340 13.1: "The precise time corresponding to
             * Timestamp Value zero is not specified". We use
             * zero to indicate absence of a meaningful timestamp.
             */
            opt_val = get_unaligned((__be32 *)value);
            if (unlikely(opt_val == 0)) {
                DCCP_WARN("Timestamp with zero value\n");
                break;
            }

            if (dreq != NULL) {
                dreq->dreq_timestamp_echo = ntohl(opt_val);
                dreq->dreq_timestamp_time = dccp_timestamp();
            } else {
                opt_recv->dccpor_timestamp =
                    dp->dccps_timestamp_echo = ntohl(opt_val);
                dp->dccps_timestamp_time = dccp_timestamp();
            }
            dccp_pr_debug("%s rx opt: TIMESTAMP=%u, ackno=%llu\n",
                          dccp_role(sk), ntohl(opt_val),
                          (unsigned long long)
                          DCCP_SKB_CB(skb)->dccpd_ack_seq);
            /* schedule an Ack in case this sender is quiescent */
            inet_csk_schedule_ack(sk);
            break;
        case DCCPO_TIMESTAMP_ECHO:
            if (len != 4 && len != 6 && len != 8)
                goto out_invalid_option;

            opt_val = get_unaligned((__be32 *)value);
            opt_recv->dccpor_timestamp_echo = ntohl(opt_val);

            dccp_pr_debug("%s rx opt: TIMESTAMP_ECHO=%u, len=%d, "
                          "ackno=%llu", dccp_role(sk),
                          opt_recv->dccpor_timestamp_echo,
                          len + 2,
                          (unsigned long long)
                          DCCP_SKB_CB(skb)->dccpd_ack_seq);

            value += 4;

            if (len == 4) {		/* no elapsed time included */
                dccp_pr_debug_cat("\n");
                break;
            }

            if (len == 6) {		/* 2-byte elapsed time */
                __be16 opt_val2 = get_unaligned((__be16 *)value);
                elapsed_time = ntohs(opt_val2);
            } else {		/* 4-byte elapsed time */
                opt_val = get_unaligned((__be32 *)value);
                elapsed_time = ntohl(opt_val);
            }

            dccp_pr_debug_cat(", ELAPSED_TIME=%u\n", elapsed_time);

            /* Give precedence to the biggest ELAPSED_TIME */
            if (elapsed_time > opt_recv->dccpor_elapsed_time)
                opt_recv->dccpor_elapsed_time = elapsed_time;
            break;
        case DCCPO_ELAPSED_TIME:
            if (dccp_packet_without_ack(skb))   /* RFC 4340, 13.2 */
                break;

            if (len == 2) {
                __be16 opt_val2 = get_unaligned((__be16 *)value);
                elapsed_time = ntohs(opt_val2);
            } else if (len == 4) {
                opt_val = get_unaligned((__be32 *)value);
                elapsed_time = ntohl(opt_val);
            } else {
                goto out_invalid_option;
            }

            if (elapsed_time > opt_recv->dccpor_elapsed_time)
                opt_recv->dccpor_elapsed_time = elapsed_time;

            dccp_pr_debug("%s rx opt: ELAPSED_TIME=%d\n",
                          dccp_role(sk), elapsed_time);
            break;
        case DCCPO_MIN_RX_CCID_SPECIFIC ... DCCPO_MAX_RX_CCID_SPECIFIC:
            if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
                                         pkt_type, opt, value, len))
                goto out_invalid_option;
            break;
        case DCCPO_ACK_VECTOR_0:
        case DCCPO_ACK_VECTOR_1:
            if (dccp_packet_without_ack(skb))   /* RFC 4340, 11.4 */
                break;
        /*
         * Ack vectors are processed by the TX CCID if it is
         * interested. The RX CCID need not parse Ack Vectors,
         * since it is only interested in clearing old state.
         * Fall through.
         */
        case DCCPO_MIN_TX_CCID_SPECIFIC ... DCCPO_MAX_TX_CCID_SPECIFIC:
            if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
                                         pkt_type, opt, value, len))
                goto out_invalid_option;
            break;
        default:
            DCCP_CRIT("DCCP(%p): option %d(len=%d) not "
                      "implemented, ignoring", sk, opt, len);
            break;
        }
ignore_option:
        if (opt != DCCPO_MANDATORY)
            mandatory = 0;
    }

    /* mandatory was the last byte in option list -> reset connection */
    if (mandatory)
        goto out_invalid_option;

out_nonsensical_length:
    /* RFC 4340, 5.8: ignore option and all remaining option space */
    return 0;

out_invalid_option:
    DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
    rc = DCCP_RESET_CODE_OPTION_ERROR;
out_featneg_failed:
    DCCP_WARN("DCCP(%p): Option %d (len=%d) error=%u\n", sk, opt, len, rc);
    DCCP_SKB_CB(skb)->dccpd_reset_code = rc;
    DCCP_SKB_CB(skb)->dccpd_reset_data[0] = opt;
    DCCP_SKB_CB(skb)->dccpd_reset_data[1] = len > 0 ? value[0] : 0;
    DCCP_SKB_CB(skb)->dccpd_reset_data[2] = len > 1 ? value[1] : 0;
    return -1;
}
int dccp_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
		       struct sk_buff *skb)
{
	struct dccp_sock *dp = dccp_sk(sk);
	const struct dccp_hdr *dh = dccp_hdr(skb);
	const u8 pkt_type = DCCP_SKB_CB(skb)->dccpd_type;
	unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
	unsigned char *opt_ptr = options;
	const unsigned char *opt_end = (unsigned char *)dh +
					(dh->dccph_doff * 4);
	struct dccp_options_received *opt_recv = &dp->dccps_options_received;
	unsigned char opt, len;
	unsigned char *uninitialized_var(value);
	u32 elapsed_time;
	__be32 opt_val;
	int rc;
	int mandatory = 0;

	memset(opt_recv, 0, sizeof(*opt_recv));

	opt = len = 0;
	while (opt_ptr != opt_end) {
		opt   = *opt_ptr++;
		len   = 0;
		value = NULL;

		
		if (opt > DCCPO_MAX_RESERVED) {
			if (opt_ptr == opt_end)
				goto out_nonsensical_length;

			len = *opt_ptr++;
			if (len < 2)
				goto out_nonsensical_length;
			len	-= 2;
			value	= opt_ptr;
			opt_ptr += len;

			if (opt_ptr > opt_end)
				goto out_nonsensical_length;
		}

		if (dreq != NULL && (opt >= DCCPO_MIN_RX_CCID_SPECIFIC ||
		    opt == DCCPO_ACK_VECTOR_0 || opt == DCCPO_ACK_VECTOR_1))
			goto ignore_option;

		switch (opt) {
		case DCCPO_PADDING:
			break;
		case DCCPO_MANDATORY:
			if (mandatory)
				goto out_invalid_option;
			if (pkt_type != DCCP_PKT_DATA)
				mandatory = 1;
			break;
		case DCCPO_NDP_COUNT:
			if (len > 6)
				goto out_invalid_option;

			opt_recv->dccpor_ndp = dccp_decode_value_var(value, len);
			dccp_pr_debug("%s opt: NDP count=%llu\n", dccp_role(sk),
				      (unsigned long long)opt_recv->dccpor_ndp);
			break;
		case DCCPO_CHANGE_L ... DCCPO_CONFIRM_R:
			if (pkt_type == DCCP_PKT_DATA)      
				break;
			if (len == 0)
				goto out_invalid_option;
			rc = dccp_feat_parse_options(sk, dreq, mandatory, opt,
						    *value, value + 1, len - 1);
			if (rc)
				goto out_featneg_failed;
			break;
		case DCCPO_TIMESTAMP:
			if (len != 4)
				goto out_invalid_option;
			opt_val = get_unaligned((__be32 *)value);
			if (unlikely(opt_val == 0)) {
				DCCP_WARN("Timestamp with zero value\n");
				break;
			}

			if (dreq != NULL) {
				dreq->dreq_timestamp_echo = ntohl(opt_val);
				dreq->dreq_timestamp_time = dccp_timestamp();
			} else {
				opt_recv->dccpor_timestamp =
					dp->dccps_timestamp_echo = ntohl(opt_val);
				dp->dccps_timestamp_time = dccp_timestamp();
			}
			dccp_pr_debug("%s rx opt: TIMESTAMP=%u, ackno=%llu\n",
				      dccp_role(sk), ntohl(opt_val),
				      (unsigned long long)
				      DCCP_SKB_CB(skb)->dccpd_ack_seq);
			
			inet_csk_schedule_ack(sk);
			break;
		case DCCPO_TIMESTAMP_ECHO:
			if (len != 4 && len != 6 && len != 8)
				goto out_invalid_option;

			opt_val = get_unaligned((__be32 *)value);
			opt_recv->dccpor_timestamp_echo = ntohl(opt_val);

			dccp_pr_debug("%s rx opt: TIMESTAMP_ECHO=%u, len=%d, "
				      "ackno=%llu", dccp_role(sk),
				      opt_recv->dccpor_timestamp_echo,
				      len + 2,
				      (unsigned long long)
				      DCCP_SKB_CB(skb)->dccpd_ack_seq);

			value += 4;

			if (len == 4) {		
				dccp_pr_debug_cat("\n");
				break;
			}

			if (len == 6) {		
				__be16 opt_val2 = get_unaligned((__be16 *)value);
				elapsed_time = ntohs(opt_val2);
			} else {		
				opt_val = get_unaligned((__be32 *)value);
				elapsed_time = ntohl(opt_val);
			}

			dccp_pr_debug_cat(", ELAPSED_TIME=%u\n", elapsed_time);

			
			if (elapsed_time > opt_recv->dccpor_elapsed_time)
				opt_recv->dccpor_elapsed_time = elapsed_time;
			break;
		case DCCPO_ELAPSED_TIME:
			if (dccp_packet_without_ack(skb))   
				break;

			if (len == 2) {
				__be16 opt_val2 = get_unaligned((__be16 *)value);
				elapsed_time = ntohs(opt_val2);
			} else if (len == 4) {
				opt_val = get_unaligned((__be32 *)value);
				elapsed_time = ntohl(opt_val);
			} else {
				goto out_invalid_option;
			}

			if (elapsed_time > opt_recv->dccpor_elapsed_time)
				opt_recv->dccpor_elapsed_time = elapsed_time;

			dccp_pr_debug("%s rx opt: ELAPSED_TIME=%d\n",
				      dccp_role(sk), elapsed_time);
			break;
		case DCCPO_MIN_RX_CCID_SPECIFIC ... DCCPO_MAX_RX_CCID_SPECIFIC:
			if (ccid_hc_rx_parse_options(dp->dccps_hc_rx_ccid, sk,
						     pkt_type, opt, value, len))
				goto out_invalid_option;
			break;
		case DCCPO_ACK_VECTOR_0:
		case DCCPO_ACK_VECTOR_1:
			if (dccp_packet_without_ack(skb))   
				break;
		case DCCPO_MIN_TX_CCID_SPECIFIC ... DCCPO_MAX_TX_CCID_SPECIFIC:
			if (ccid_hc_tx_parse_options(dp->dccps_hc_tx_ccid, sk,
						     pkt_type, opt, value, len))
				goto out_invalid_option;
			break;
		default:
			DCCP_CRIT("DCCP(%p): option %d(len=%d) not "
				  "implemented, ignoring", sk, opt, len);
			break;
		}
ignore_option:
		if (opt != DCCPO_MANDATORY)
			mandatory = 0;
	}

	
	if (mandatory)
		goto out_invalid_option;

out_nonsensical_length:
	
	return 0;

out_invalid_option:
	DCCP_INC_STATS_BH(DCCP_MIB_INVALIDOPT);
	rc = DCCP_RESET_CODE_OPTION_ERROR;
out_featneg_failed:
	DCCP_WARN("DCCP(%p): Option %d (len=%d) error=%u\n", sk, opt, len, rc);
	DCCP_SKB_CB(skb)->dccpd_reset_code = rc;
	DCCP_SKB_CB(skb)->dccpd_reset_data[0] = opt;
	DCCP_SKB_CB(skb)->dccpd_reset_data[1] = len > 0 ? value[0] : 0;
	DCCP_SKB_CB(skb)->dccpd_reset_data[2] = len > 1 ? value[1] : 0;
	return -1;
}