linux/include/net/rstreason.h

/* SPDX-License-Identifier: GPL-2.0-or-later */

#ifndef _LINUX_RSTREASON_H
#define _LINUX_RSTREASON_H
#include <net/dropreason-core.h>
#include <uapi/linux/mptcp.h>

#define DEFINE_RST_REASON(FN, FNe)	\
	FN(NOT_SPECIFIED)		\
	FN(NO_SOCKET)			\
	FN(TCP_INVALID_ACK_SEQUENCE)	\
	FN(TCP_RFC7323_PAWS)		\
	FN(TCP_TOO_OLD_ACK)		\
	FN(TCP_ACK_UNSENT_DATA)		\
	FN(TCP_FLAGS)			\
	FN(TCP_OLD_ACK)			\
	FN(TCP_ABORT_ON_DATA)		\
	FN(TCP_TIMEWAIT_SOCKET)		\
	FN(INVALID_SYN)			\
	FN(TCP_ABORT_ON_CLOSE)		\
	FN(TCP_ABORT_ON_LINGER)		\
	FN(TCP_ABORT_ON_MEMORY)		\
	FN(TCP_STATE)			\
	FN(TCP_KEEPALIVE_TIMEOUT)	\
	FN(TCP_DISCONNECT_WITH_DATA)	\
	FN(MPTCP_RST_EUNSPEC)		\
	FN(MPTCP_RST_EMPTCP)		\
	FN(MPTCP_RST_ERESOURCE)		\
	FN(MPTCP_RST_EPROHIBIT)		\
	FN(MPTCP_RST_EWQ2BIG)		\
	FN(MPTCP_RST_EBADPERF)		\
	FN(MPTCP_RST_EMIDDLEBOX)	\
	FN(ERROR)			\
	FNe(MAX)

/**
 * enum sk_rst_reason - the reasons of socket reset
 *
 * The reasons of sk reset, which are used in DCCP/TCP/MPTCP protocols.
 *
 * There are three parts in order:
 * 1) skb drop reasons: relying on drop reasons for such as passive reset
 * 2) independent reset reasons: such as active reset reasons
 * 3) reset reasons in MPTCP: only for MPTCP use
 */
enum sk_rst_reason {
	/* Refer to include/net/dropreason-core.h
	 * Rely on skb drop reasons because it indicates exactly why RST
	 * could happen.
	 */
	/** @SK_RST_REASON_NOT_SPECIFIED: reset reason is not specified */
	SK_RST_REASON_NOT_SPECIFIED,
	/** @SK_RST_REASON_NO_SOCKET: no valid socket that can be used */
	SK_RST_REASON_NO_SOCKET,
	/**
	 * @SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE: Not acceptable ACK SEQ
	 * field because ack sequence is not in the window between snd_una
	 * and snd_nxt
	 */
	SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE,
	/**
	 * @SK_RST_REASON_TCP_RFC7323_PAWS: PAWS check, corresponding to
	 * LINUX_MIB_PAWSESTABREJECTED, LINUX_MIB_PAWSACTIVEREJECTED
	 */
	SK_RST_REASON_TCP_RFC7323_PAWS,
	/** @SK_RST_REASON_TCP_TOO_OLD_ACK: TCP ACK is too old */
	SK_RST_REASON_TCP_TOO_OLD_ACK,
	/**
	 * @SK_RST_REASON_TCP_ACK_UNSENT_DATA: TCP ACK for data we haven't
	 * sent yet
	 */
	SK_RST_REASON_TCP_ACK_UNSENT_DATA,
	/** @SK_RST_REASON_TCP_FLAGS: TCP flags invalid */
	SK_RST_REASON_TCP_FLAGS,
	/** @SK_RST_REASON_TCP_OLD_ACK: TCP ACK is old, but in window */
	SK_RST_REASON_TCP_OLD_ACK,
	/**
	 * @SK_RST_REASON_TCP_ABORT_ON_DATA: abort on data
	 * corresponding to LINUX_MIB_TCPABORTONDATA
	 */
	SK_RST_REASON_TCP_ABORT_ON_DATA,

	/* Here start with the independent reasons */
	/** @SK_RST_REASON_TCP_TIMEWAIT_SOCKET: happen on the timewait socket */
	SK_RST_REASON_TCP_TIMEWAIT_SOCKET,
	/**
	 * @SK_RST_REASON_INVALID_SYN: receive bad syn packet
	 * RFC 793 says if the state is not CLOSED/LISTEN/SYN-SENT then
	 * "fourth, check the SYN bit,...If the SYN is in the window it is
	 * an error, send a reset"
	 */
	SK_RST_REASON_INVALID_SYN,
	/**
	 * @SK_RST_REASON_TCP_ABORT_ON_CLOSE: abort on close
	 * corresponding to LINUX_MIB_TCPABORTONCLOSE
	 */
	SK_RST_REASON_TCP_ABORT_ON_CLOSE,
	/**
	 * @SK_RST_REASON_TCP_ABORT_ON_LINGER: abort on linger
	 * corresponding to LINUX_MIB_TCPABORTONLINGER
	 */
	SK_RST_REASON_TCP_ABORT_ON_LINGER,
	/**
	 * @SK_RST_REASON_TCP_ABORT_ON_MEMORY: abort on memory
	 * corresponding to LINUX_MIB_TCPABORTONMEMORY
	 */
	SK_RST_REASON_TCP_ABORT_ON_MEMORY,
	/**
	 * @SK_RST_REASON_TCP_STATE: abort on tcp state
	 * Please see RFC 9293 for all possible reset conditions
	 */
	SK_RST_REASON_TCP_STATE,
	/**
	 * @SK_RST_REASON_TCP_KEEPALIVE_TIMEOUT: time to timeout
	 * When we have already run out of all the chances, which means
	 * keepalive timeout, we have to reset the connection
	 */
	SK_RST_REASON_TCP_KEEPALIVE_TIMEOUT,
	/**
	 * @SK_RST_REASON_TCP_DISCONNECT_WITH_DATA: disconnect when write
	 * queue is not empty
	 * It means user has written data into the write queue when doing
	 * disconnecting, so we have to send an RST.
	 */
	SK_RST_REASON_TCP_DISCONNECT_WITH_DATA,

	/* Copy from include/uapi/linux/mptcp.h.
	 * These reset fields will not be changed since they adhere to
	 * RFC 8684. So do not touch them. I'm going to list each definition
	 * of them respectively.
	 */
	/**
	 * @SK_RST_REASON_MPTCP_RST_EUNSPEC: Unspecified error.
	 * This is the default error; it implies that the subflow is no
	 * longer available. The presence of this option shows that the
	 * RST was generated by an MPTCP-aware device.
	 */
	SK_RST_REASON_MPTCP_RST_EUNSPEC,
	/**
	 * @SK_RST_REASON_MPTCP_RST_EMPTCP: MPTCP-specific error.
	 * An error has been detected in the processing of MPTCP options.
	 * This is the usual reason code to return in the cases where a RST
	 * is being sent to close a subflow because of an invalid response.
	 */
	SK_RST_REASON_MPTCP_RST_EMPTCP,
	/**
	 * @SK_RST_REASON_MPTCP_RST_ERESOURCE: Lack of resources.
	 * This code indicates that the sending host does not have enough
	 * resources to support the terminated subflow.
	 */
	SK_RST_REASON_MPTCP_RST_ERESOURCE,
	/**
	 * @SK_RST_REASON_MPTCP_RST_EPROHIBIT: Administratively prohibited.
	 * This code indicates that the requested subflow is prohibited by
	 * the policies of the sending host.
	 */
	SK_RST_REASON_MPTCP_RST_EPROHIBIT,
	/**
	 * @SK_RST_REASON_MPTCP_RST_EWQ2BIG: Too much outstanding data.
	 * This code indicates that there is an excessive amount of data
	 * that needs to be transmitted over the terminated subflow while
	 * having already been acknowledged over one or more other subflows.
	 * This may occur if a path has been unavailable for a short period
	 * and it is more efficient to reset and start again than it is to
	 * retransmit the queued data.
	 */
	SK_RST_REASON_MPTCP_RST_EWQ2BIG,
	/**
	 * @SK_RST_REASON_MPTCP_RST_EBADPERF: Unacceptable performance.
	 * This code indicates that the performance of this subflow was
	 * too low compared to the other subflows of this Multipath TCP
	 * connection.
	 */
	SK_RST_REASON_MPTCP_RST_EBADPERF,
	/**
	 * @SK_RST_REASON_MPTCP_RST_EMIDDLEBOX: Middlebox interference.
	 * Middlebox interference has been detected over this subflow,
	 * making MPTCP signaling invalid. For example, this may be sent
	 * if the checksum does not validate.
	 */
	SK_RST_REASON_MPTCP_RST_EMIDDLEBOX,

	/** @SK_RST_REASON_ERROR: unexpected error happens */
	SK_RST_REASON_ERROR,

	/**
	 * @SK_RST_REASON_MAX: Maximum of socket reset reasons.
	 * It shouldn't be used as a real 'reason'.
	 */
	SK_RST_REASON_MAX,
};

/* Convert skb drop reasons to enum sk_rst_reason type */
static inline enum sk_rst_reason
sk_rst_convert_drop_reason(enum skb_drop_reason reason)
{
	switch (reason) {
	case SKB_DROP_REASON_NOT_SPECIFIED:
		return SK_RST_REASON_NOT_SPECIFIED;
	case SKB_DROP_REASON_NO_SOCKET:
		return SK_RST_REASON_NO_SOCKET;
	case SKB_DROP_REASON_TCP_INVALID_ACK_SEQUENCE:
		return SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE;
	case SKB_DROP_REASON_TCP_RFC7323_PAWS:
		return SK_RST_REASON_TCP_RFC7323_PAWS;
	case SKB_DROP_REASON_TCP_TOO_OLD_ACK:
		return SK_RST_REASON_TCP_TOO_OLD_ACK;
	case SKB_DROP_REASON_TCP_ACK_UNSENT_DATA:
		return SK_RST_REASON_TCP_ACK_UNSENT_DATA;
	case SKB_DROP_REASON_TCP_FLAGS:
		return SK_RST_REASON_TCP_FLAGS;
	case SKB_DROP_REASON_TCP_OLD_ACK:
		return SK_RST_REASON_TCP_OLD_ACK;
	case SKB_DROP_REASON_TCP_ABORT_ON_DATA:
		return SK_RST_REASON_TCP_ABORT_ON_DATA;
	default:
		/* If we don't have our own corresponding reason */
		return SK_RST_REASON_NOT_SPECIFIED;
	}
}
#endif