xref: /linux/include/net/rstreason.h (revision 9410645520e9b820069761f3450ef6661418e279)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _LINUX_RSTREASON_H
4 #define _LINUX_RSTREASON_H
5 #include <net/dropreason-core.h>
6 #include <uapi/linux/mptcp.h>
7 
8 #define DEFINE_RST_REASON(FN, FNe)	\
9 	FN(NOT_SPECIFIED)		\
10 	FN(NO_SOCKET)			\
11 	FN(TCP_INVALID_ACK_SEQUENCE)	\
12 	FN(TCP_RFC7323_PAWS)		\
13 	FN(TCP_TOO_OLD_ACK)		\
14 	FN(TCP_ACK_UNSENT_DATA)		\
15 	FN(TCP_FLAGS)			\
16 	FN(TCP_OLD_ACK)			\
17 	FN(TCP_ABORT_ON_DATA)		\
18 	FN(TCP_TIMEWAIT_SOCKET)		\
19 	FN(INVALID_SYN)			\
20 	FN(TCP_ABORT_ON_CLOSE)		\
21 	FN(TCP_ABORT_ON_LINGER)		\
22 	FN(TCP_ABORT_ON_MEMORY)		\
23 	FN(TCP_STATE)			\
24 	FN(TCP_KEEPALIVE_TIMEOUT)	\
25 	FN(TCP_DISCONNECT_WITH_DATA)	\
26 	FN(MPTCP_RST_EUNSPEC)		\
27 	FN(MPTCP_RST_EMPTCP)		\
28 	FN(MPTCP_RST_ERESOURCE)		\
29 	FN(MPTCP_RST_EPROHIBIT)		\
30 	FN(MPTCP_RST_EWQ2BIG)		\
31 	FN(MPTCP_RST_EBADPERF)		\
32 	FN(MPTCP_RST_EMIDDLEBOX)	\
33 	FN(ERROR)			\
34 	FNe(MAX)
35 
36 /**
37  * enum sk_rst_reason - the reasons of socket reset
38  *
39  * The reasons of sk reset, which are used in DCCP/TCP/MPTCP protocols.
40  *
41  * There are three parts in order:
42  * 1) skb drop reasons: relying on drop reasons for such as passive reset
43  * 2) independent reset reasons: such as active reset reasons
44  * 3) reset reasons in MPTCP: only for MPTCP use
45  */
46 enum sk_rst_reason {
47 	/* Refer to include/net/dropreason-core.h
48 	 * Rely on skb drop reasons because it indicates exactly why RST
49 	 * could happen.
50 	 */
51 	/** @SK_RST_REASON_NOT_SPECIFIED: reset reason is not specified */
52 	SK_RST_REASON_NOT_SPECIFIED,
53 	/** @SK_RST_REASON_NO_SOCKET: no valid socket that can be used */
54 	SK_RST_REASON_NO_SOCKET,
55 	/**
56 	 * @SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE: Not acceptable ACK SEQ
57 	 * field because ack sequence is not in the window between snd_una
58 	 * and snd_nxt
59 	 */
60 	SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE,
61 	/**
62 	 * @SK_RST_REASON_TCP_RFC7323_PAWS: PAWS check, corresponding to
63 	 * LINUX_MIB_PAWSESTABREJECTED, LINUX_MIB_PAWSACTIVEREJECTED
64 	 */
65 	SK_RST_REASON_TCP_RFC7323_PAWS,
66 	/** @SK_RST_REASON_TCP_TOO_OLD_ACK: TCP ACK is too old */
67 	SK_RST_REASON_TCP_TOO_OLD_ACK,
68 	/**
69 	 * @SK_RST_REASON_TCP_ACK_UNSENT_DATA: TCP ACK for data we haven't
70 	 * sent yet
71 	 */
72 	SK_RST_REASON_TCP_ACK_UNSENT_DATA,
73 	/** @SK_RST_REASON_TCP_FLAGS: TCP flags invalid */
74 	SK_RST_REASON_TCP_FLAGS,
75 	/** @SK_RST_REASON_TCP_OLD_ACK: TCP ACK is old, but in window */
76 	SK_RST_REASON_TCP_OLD_ACK,
77 	/**
78 	 * @SK_RST_REASON_TCP_ABORT_ON_DATA: abort on data
79 	 * corresponding to LINUX_MIB_TCPABORTONDATA
80 	 */
81 	SK_RST_REASON_TCP_ABORT_ON_DATA,
82 
83 	/* Here start with the independent reasons */
84 	/** @SK_RST_REASON_TCP_TIMEWAIT_SOCKET: happen on the timewait socket */
85 	SK_RST_REASON_TCP_TIMEWAIT_SOCKET,
86 	/**
87 	 * @SK_RST_REASON_INVALID_SYN: receive bad syn packet
88 	 * RFC 793 says if the state is not CLOSED/LISTEN/SYN-SENT then
89 	 * "fourth, check the SYN bit,...If the SYN is in the window it is
90 	 * an error, send a reset"
91 	 */
92 	SK_RST_REASON_INVALID_SYN,
93 	/**
94 	 * @SK_RST_REASON_TCP_ABORT_ON_CLOSE: abort on close
95 	 * corresponding to LINUX_MIB_TCPABORTONCLOSE
96 	 */
97 	SK_RST_REASON_TCP_ABORT_ON_CLOSE,
98 	/**
99 	 * @SK_RST_REASON_TCP_ABORT_ON_LINGER: abort on linger
100 	 * corresponding to LINUX_MIB_TCPABORTONLINGER
101 	 */
102 	SK_RST_REASON_TCP_ABORT_ON_LINGER,
103 	/**
104 	 * @SK_RST_REASON_TCP_ABORT_ON_MEMORY: abort on memory
105 	 * corresponding to LINUX_MIB_TCPABORTONMEMORY
106 	 */
107 	SK_RST_REASON_TCP_ABORT_ON_MEMORY,
108 	/**
109 	 * @SK_RST_REASON_TCP_STATE: abort on tcp state
110 	 * Please see RFC 9293 for all possible reset conditions
111 	 */
112 	SK_RST_REASON_TCP_STATE,
113 	/**
114 	 * @SK_RST_REASON_TCP_KEEPALIVE_TIMEOUT: time to timeout
115 	 * When we have already run out of all the chances, which means
116 	 * keepalive timeout, we have to reset the connection
117 	 */
118 	SK_RST_REASON_TCP_KEEPALIVE_TIMEOUT,
119 	/**
120 	 * @SK_RST_REASON_TCP_DISCONNECT_WITH_DATA: disconnect when write
121 	 * queue is not empty
122 	 * It means user has written data into the write queue when doing
123 	 * disconnecting, so we have to send an RST.
124 	 */
125 	SK_RST_REASON_TCP_DISCONNECT_WITH_DATA,
126 
127 	/* Copy from include/uapi/linux/mptcp.h.
128 	 * These reset fields will not be changed since they adhere to
129 	 * RFC 8684. So do not touch them. I'm going to list each definition
130 	 * of them respectively.
131 	 */
132 	/**
133 	 * @SK_RST_REASON_MPTCP_RST_EUNSPEC: Unspecified error.
134 	 * This is the default error; it implies that the subflow is no
135 	 * longer available. The presence of this option shows that the
136 	 * RST was generated by an MPTCP-aware device.
137 	 */
138 	SK_RST_REASON_MPTCP_RST_EUNSPEC,
139 	/**
140 	 * @SK_RST_REASON_MPTCP_RST_EMPTCP: MPTCP-specific error.
141 	 * An error has been detected in the processing of MPTCP options.
142 	 * This is the usual reason code to return in the cases where a RST
143 	 * is being sent to close a subflow because of an invalid response.
144 	 */
145 	SK_RST_REASON_MPTCP_RST_EMPTCP,
146 	/**
147 	 * @SK_RST_REASON_MPTCP_RST_ERESOURCE: Lack of resources.
148 	 * This code indicates that the sending host does not have enough
149 	 * resources to support the terminated subflow.
150 	 */
151 	SK_RST_REASON_MPTCP_RST_ERESOURCE,
152 	/**
153 	 * @SK_RST_REASON_MPTCP_RST_EPROHIBIT: Administratively prohibited.
154 	 * This code indicates that the requested subflow is prohibited by
155 	 * the policies of the sending host.
156 	 */
157 	SK_RST_REASON_MPTCP_RST_EPROHIBIT,
158 	/**
159 	 * @SK_RST_REASON_MPTCP_RST_EWQ2BIG: Too much outstanding data.
160 	 * This code indicates that there is an excessive amount of data
161 	 * that needs to be transmitted over the terminated subflow while
162 	 * having already been acknowledged over one or more other subflows.
163 	 * This may occur if a path has been unavailable for a short period
164 	 * and it is more efficient to reset and start again than it is to
165 	 * retransmit the queued data.
166 	 */
167 	SK_RST_REASON_MPTCP_RST_EWQ2BIG,
168 	/**
169 	 * @SK_RST_REASON_MPTCP_RST_EBADPERF: Unacceptable performance.
170 	 * This code indicates that the performance of this subflow was
171 	 * too low compared to the other subflows of this Multipath TCP
172 	 * connection.
173 	 */
174 	SK_RST_REASON_MPTCP_RST_EBADPERF,
175 	/**
176 	 * @SK_RST_REASON_MPTCP_RST_EMIDDLEBOX: Middlebox interference.
177 	 * Middlebox interference has been detected over this subflow,
178 	 * making MPTCP signaling invalid. For example, this may be sent
179 	 * if the checksum does not validate.
180 	 */
181 	SK_RST_REASON_MPTCP_RST_EMIDDLEBOX,
182 
183 	/** @SK_RST_REASON_ERROR: unexpected error happens */
184 	SK_RST_REASON_ERROR,
185 
186 	/**
187 	 * @SK_RST_REASON_MAX: Maximum of socket reset reasons.
188 	 * It shouldn't be used as a real 'reason'.
189 	 */
190 	SK_RST_REASON_MAX,
191 };
192 
193 /* Convert skb drop reasons to enum sk_rst_reason type */
194 static inline enum sk_rst_reason
sk_rst_convert_drop_reason(enum skb_drop_reason reason)195 sk_rst_convert_drop_reason(enum skb_drop_reason reason)
196 {
197 	switch (reason) {
198 	case SKB_DROP_REASON_NOT_SPECIFIED:
199 		return SK_RST_REASON_NOT_SPECIFIED;
200 	case SKB_DROP_REASON_NO_SOCKET:
201 		return SK_RST_REASON_NO_SOCKET;
202 	case SKB_DROP_REASON_TCP_INVALID_ACK_SEQUENCE:
203 		return SK_RST_REASON_TCP_INVALID_ACK_SEQUENCE;
204 	case SKB_DROP_REASON_TCP_RFC7323_PAWS:
205 		return SK_RST_REASON_TCP_RFC7323_PAWS;
206 	case SKB_DROP_REASON_TCP_TOO_OLD_ACK:
207 		return SK_RST_REASON_TCP_TOO_OLD_ACK;
208 	case SKB_DROP_REASON_TCP_ACK_UNSENT_DATA:
209 		return SK_RST_REASON_TCP_ACK_UNSENT_DATA;
210 	case SKB_DROP_REASON_TCP_FLAGS:
211 		return SK_RST_REASON_TCP_FLAGS;
212 	case SKB_DROP_REASON_TCP_OLD_ACK:
213 		return SK_RST_REASON_TCP_OLD_ACK;
214 	case SKB_DROP_REASON_TCP_ABORT_ON_DATA:
215 		return SK_RST_REASON_TCP_ABORT_ON_DATA;
216 	default:
217 		/* If we don't have our own corresponding reason */
218 		return SK_RST_REASON_NOT_SPECIFIED;
219 	}
220 }
221 #endif
222