Line data Source code
1 : #ifndef HEADER_fd_src_waltz_quic_fd_quic_conn_h
2 : #define HEADER_fd_src_waltz_quic_fd_quic_conn_h
3 :
4 : #include "fd_quic.h"
5 : #include "fd_quic_common.h"
6 : #include "fd_quic_ack_tx.h"
7 : #include "fd_quic_conn_id.h"
8 : #include "crypto/fd_quic_crypto_suites.h"
9 : #include "fd_quic_pkt_meta.h"
10 : #include "fd_quic_svc_q.h"
11 : #include "../fd_rtt_est.h"
12 :
13 0 : #define FD_QUIC_CONN_STATE_INVALID 0 /* dead object / freed */
14 0 : #define FD_QUIC_CONN_STATE_HANDSHAKE 1 /* currently doing handshaking with peer */
15 0 : #define FD_QUIC_CONN_STATE_HANDSHAKE_COMPLETE 2 /* handshake complete, confirming with peer */
16 0 : #define FD_QUIC_CONN_STATE_ACTIVE 3 /* connection established - data may be transferred */
17 0 : #define FD_QUIC_CONN_STATE_PEER_CLOSE 4 /* peer requested close */
18 0 : #define FD_QUIC_CONN_STATE_ABORT 5 /* connection terminating due to error */
19 0 : #define FD_QUIC_CONN_STATE_CLOSE_PENDING 6 /* connection is closing */
20 0 : #define FD_QUIC_CONN_STATE_DEAD 7 /* connection about to be freed */
21 : #define FD_QUIC_CONN_STATE_CNT 8
22 :
23 : FD_STATIC_ASSERT( FD_QUIC_CONN_STATE_CNT == sizeof(((fd_quic_metrics_t*)0)->conn_state_cnt)/sizeof(((fd_quic_metrics_t*)0)->conn_state_cnt[0]),
24 : "metrics conn_state_cnt is the wrong size" );
25 :
26 : #define FD_QUIC_REASON_CODES(X,SEP) \
27 0 : X(NO_ERROR , 0x00 , "No error" ) SEP \
28 0 : X(INTERNAL_ERROR , 0x01 , "Implementation error" ) SEP \
29 0 : X(CONNECTION_REFUSED , 0x02 , "Server refuses a connection" ) SEP \
30 0 : X(FLOW_CONTROL_ERROR , 0x03 , "Flow control error" ) SEP \
31 0 : X(STREAM_LIMIT_ERROR , 0x04 , "Too many streams opened" ) SEP \
32 0 : X(STREAM_STATE_ERROR , 0x05 , "Frame received in invalid stream state" ) SEP \
33 0 : X(FINAL_SIZE_ERROR , 0x06 , "Change to final size" ) SEP \
34 0 : X(FRAME_ENCODING_ERROR , 0x07 , "Frame encoding error" ) SEP \
35 0 : X(TRANSPORT_PARAMETER_ERROR , 0x08 , "Error in transport parameters" ) SEP \
36 0 : X(CONNECTION_ID_LIMIT_ERROR , 0x09 , "Too many connection IDs received" ) SEP \
37 0 : X(PROTOCOL_VIOLATION , 0x0a , "Generic protocol violation" ) SEP \
38 0 : X(INVALID_TOKEN , 0x0b , "Invalid Token received" ) SEP \
39 0 : X(APPLICATION_ERROR , 0x0c , "Application error" ) SEP \
40 0 : X(CRYPTO_BUFFER_EXCEEDED , 0x0d , "CRYPTO data buffer overflowed" ) SEP \
41 0 : X(KEY_UPDATE_ERROR , 0x0e , "Invalid packet protection update" ) SEP \
42 0 : X(AEAD_LIMIT_REACHED , 0x0f , "Excessive use of packet protection keys" ) SEP \
43 0 : X(NO_VIABLE_PATH , 0x10 , "No viable network path exists" ) SEP \
44 0 : X(CRYPTO_BASE , 0x100 , "0x0100-0x01ff CRYPTO_ERROR TLS alert code" ) SEP \
45 0 : X(HANDSHAKE_FAILURE , 0x128 , "Handshake failed" )
46 :
47 : enum {
48 : # define COMMA ,
49 : # define _(NAME,CODE,DESC) \
50 : FD_QUIC_CONN_REASON_##NAME = CODE
51 : FD_QUIC_REASON_CODES(_,COMMA)
52 : # undef _
53 : # undef COMMA
54 : };
55 :
56 : char const *
57 : fd_quic_conn_reason_name( uint reason );
58 :
59 :
60 : struct fd_quic_conn_stream_rx {
61 : ulong rx_hi_stream_id; /* highest RX stream ID sent by peer + 4 */
62 : ulong rx_sup_stream_id; /* highest allowed RX stream ID + 4 */
63 :
64 : ulong rx_max_data; /* the limit on the number of bytes the peer is allowed to send to us */
65 : ulong rx_tot_data; /* total of all bytes received across all streams and including implied bytes */
66 : ulong rx_max_data_ackd; /* max max_data acked by peer */
67 :
68 : ulong rx_max_streams_unidir_ackd; /* value of MAX_STREAMS acked for UNIDIR */
69 :
70 : long rx_streams_active; /* FIXME: This is a user scratch field, not in use by fd_quic */
71 :
72 : /* FIXME add a TLB */
73 : };
74 :
75 : typedef struct fd_quic_conn_stream_rx fd_quic_conn_stream_rx_t;
76 :
77 : struct fd_quic_conn {
78 : /* 'PERSISTENT' means field should survive a conn_clear */
79 : uint conn_idx; /* connection index - PERSISTENT */
80 : /* connections are sized at runtime */
81 : /* storing the index avoids a division */
82 : uint conn_gen; /* generation of this connection slot - PERSISTENT */
83 :
84 : fd_quic_t * quic; /* PERSISTENT */
85 : void * context; /* user context */
86 :
87 : uint server : 1; /* role from self POV: 0=client, 1=server */
88 : uint established : 1; /* used by clients to determine whether to
89 : switch the destination conn id used */
90 : uint transport_params_set : 1;
91 : uint called_conn_new : 1; /* whether we need to call conn_final on teardown */
92 : uint visited : 1; /* scratch bit, no strict definition */
93 : uint key_phase : 1;
94 : uint key_update : 1;
95 : uint key_phase_updated : 1; /* completed at least one key update? */
96 : uint key_phase_ack_sent : 1; /* sent a 1-RTT ACK since the last key update? */
97 :
98 : /* metadata used by service queue */
99 : fd_quic_svc_timers_conn_meta_t svc_meta;
100 :
101 : /* Dlist membership */
102 : uint free_conn_next;
103 :
104 : ulong our_conn_id;
105 :
106 : /* Save original retry_source_connection_id
107 : * This is used by clients to compare against the retry_source_connection_id
108 : * in the transport parameters as specified in rfc 9000 7.3 */
109 : fd_quic_conn_id_t retry_src_conn_id;
110 :
111 : /* Host network endpoint. Used to determine src address and port */
112 : fd_quic_net_endpoint_t host;
113 :
114 : /* Peer network endpoints – have multiple connection ids and ip:port */
115 : /* TODO: footprint allows specifying conn_id_cnt but hardcoded limit used here */
116 : fd_quic_net_endpoint_t peer[1];
117 : fd_quic_conn_id_t peer_cids[1]; /* FIXME support new/retire conn ID */
118 :
119 : /* initial source connection id */
120 : ulong initial_source_conn_id;
121 :
122 : uint tx_max_datagram_sz; /* max UDP datagram sz allowed by peer */
123 : ulong tx_max_datagram_frame_sz; /* max RFC 9221 QUIC datagram sz allowed by peer */
124 :
125 : /* handshake members */
126 : uint handshake_complete : 1; /* have we completed a successful handshake? */
127 : uint handshake_done_send : 1; /* do we need to send handshake-done to peer? */
128 : uint handshake_done_ackd : 1; /* was handshake_done ack'ed? */
129 : fd_quic_tls_hs_t * tls_hs;
130 :
131 : /* amount of handshake data already sent from head of queue */
132 : ulong hs_sent_bytes[4];
133 :
134 : /* amount of handshake data ack'ed by peer counted from head of queue */
135 : ulong hs_ackd_bytes[4];
136 :
137 : /* Keys for header and packet protection
138 : secrets: Contains 'master' secrets used to derive other keys
139 : keys[e][d]: Current pair of keys for each encryption level (e)
140 : and direction (d==0 is incoming, d==1 is outgoing)
141 : new_keys[e]: App keys to use for the next key update. Once app
142 : keys are available these are always kept up-to-date
143 : keys_avail: Bit set of available keys, LSB indexed by enc level */
144 : fd_quic_crypto_secrets_t secrets;
145 : fd_quic_crypto_keys_t keys[FD_QUIC_NUM_ENC_LEVELS][2];
146 : fd_quic_crypto_keys_t new_keys[2];
147 : uint keys_avail;
148 :
149 : ulong tx_sup_stream_id; /* highest allowed TX stream ID + 4 */
150 :
151 : /* packet number info
152 : each encryption level maps to a packet number space
153 : 0-RTT and 1-RTT both map to APPLICATION
154 : exp_pkt_number[j] is one past the highest packet number received */
155 : ulong exp_pkt_number[3]; /* different packet number spaces:
156 : INITIAL, HANDSHAKE and APPLICATION */
157 : ulong pkt_number[3]; /* tx packet number by pn space */
158 :
159 : /* Incrementally skip unsupported NewSessionTickets in application
160 : CRYPTO data, retaining enough framing to reject TLS KeyUpdate. */
161 : ulong post_hs_rx_off;
162 : uint post_hs_remaining;
163 : uint post_hs_length;
164 : uchar post_hs_hdr_used;
165 : ulong rx_pn_mask[3]; /* bit i set if exp_pkt_number-1-i was received */
166 : ulong highest_acked[3]; /* highest packet number acked (meaningless if 0) */
167 :
168 : ushort ipv4_id; /* ipv4 id field */
169 :
170 : /* buffer to send next */
171 : /* must be at least FD_QUIC_MAX_UDP_PAYLOAD_SZ */
172 : uchar tx_buf_conn[2048];
173 : uchar * tx_ptr; /* ptr to free space in tx_buf_conn */
174 :
175 : uint state; /* PERSISTENT to keep state counters correct */
176 : uint reason; /* quic reason for closing. see FD_QUIC_CONN_REASON_* */
177 : uint app_reason; /* application reason for closing */
178 :
179 : fd_quic_ack_gen_t ack_gen[1];
180 : ulong unacked_sz; /* Number of received stream frame payload bytes pending ACK */
181 : /* Resets to zero when conn is rescheduled or ACKs are sent */
182 :
183 : fd_quic_pkt_meta_tracker_t pkt_meta_tracker;
184 :
185 : /* flow control */
186 : ulong tx_max_data; /* the limit on the number of bytes we are allowed
187 : to send to the peer across all streams */
188 : /* even if a bytes on a stream are not received,
189 : higher offsets received imply the usage of those bytes,
190 : and they count against the max */
191 :
192 : uint flags;
193 0 : # define FD_QUIC_CONN_FLAGS_MAX_DATA (1u<<0u)
194 0 : # define FD_QUIC_CONN_FLAGS_CLOSE_SENT (1u<<1u)
195 0 : # define FD_QUIC_CONN_FLAGS_MAX_STREAMS_UNIDIR (1u<<2u)
196 0 : # define FD_QUIC_CONN_FLAGS_PING (1u<<4u)
197 0 : # define FD_QUIC_CONN_FLAGS_PING_SENT (1u<<5u)
198 :
199 : /* max stream data per stream type */
200 : ulong tx_initial_max_stream_data_uni;
201 :
202 : /* last tx packet num with max_data frame referring to this stream
203 : set to next_pkt_number to indicate a new max_data frame should be sent
204 : if we time out this packet (or possibly a later packet) we resend the frame
205 : and update this value */
206 : ulong upd_pkt_number;
207 :
208 : /* idle timeout arguments */
209 : long idle_timeout_ns;
210 : long last_activity;
211 : long last_ack;
212 : long let_die_time_ns; /* stop keep-alive after this time */
213 :
214 : /* round trip time related members */
215 : fd_rtt_estimate_t rtt[1];
216 : float rtt_period_ns; /* bound on time between RTT measurements */
217 : float peer_ack_delay_scale; /* convert ACK delay units to nanoseconds */
218 : float peer_max_ack_delay_ns; /* peer max ack delay in nanoseconds */
219 : uint pto_count; /* consecutive PTO expirations without a new ACK */
220 : long pto_expiry; /* earliest next PTO after backing off */
221 :
222 : ulong token_len;
223 : uchar token[ FD_QUIC_RETRY_MAX_TOKEN_SZ ];
224 :
225 : fd_quic_conn_stream_rx_t srx[1];
226 :
227 : ulong used_pkt_meta;
228 : };
229 :
230 : FD_PROTOTYPES_BEGIN
231 :
232 : FD_FN_CONST static inline ulong
233 0 : fd_quic_conn_uid( fd_quic_conn_t const * conn ) {
234 0 : return ( (ulong)conn->conn_idx << 32UL ) | ( (ulong)conn->conn_gen );
235 0 : }
236 :
237 : FD_FN_CONST static inline uint
238 0 : fd_quic_conn_uid_idx( ulong conn_uid ) {
239 0 : return (uint)( conn_uid >> 32UL );
240 0 : }
241 :
242 : FD_FN_CONST static inline uint
243 0 : fd_quic_conn_uid_gen( ulong conn_uid ) {
244 0 : return (uint)( conn_uid & 0xffffffffUL );
245 0 : }
246 :
247 : /* returns the alignment requirement of fd_quic_conn_t */
248 : FD_FN_CONST ulong
249 : fd_quic_conn_align( void );
250 :
251 : /* returns the footprint of the connection object for given limits */
252 : ulong
253 : fd_quic_conn_footprint( fd_quic_limits_t const * );
254 :
255 : /* called by fd_quic_new to initialize the connection objects
256 : used by fd_quic */
257 : fd_quic_conn_t *
258 : fd_quic_conn_new( void * mem,
259 : fd_quic_t * quic,
260 : fd_quic_limits_t const * limits );
261 :
262 : /* clears all non-persistent members of the connection object */
263 : static inline void
264 0 : fd_quic_conn_clear( fd_quic_conn_t * conn ) {
265 0 : fd_quic_t * quic = conn->quic;
266 0 : uint conn_idx = conn->conn_idx;
267 0 : uint conn_gen = conn->conn_gen;
268 0 : uint conn_state = conn->state;
269 :
270 0 : fd_memset( conn, 0, sizeof(fd_quic_conn_t) );
271 :
272 0 : conn->quic = quic;
273 0 : conn->conn_idx = conn_idx;
274 0 : conn->conn_gen = conn_gen;
275 0 : conn->state = conn_state;
276 0 : }
277 :
278 : /* set the user-defined context value on the connection */
279 : void
280 : fd_quic_conn_set_context( fd_quic_conn_t * conn, void * context );
281 :
282 : /* get the user-defined context value from a connection */
283 : void *
284 : fd_quic_conn_get_context( fd_quic_conn_t * conn );
285 :
286 :
287 : /* set all conns to not visited, used for validation */
288 : void
289 : fd_quic_conn_validate_init( fd_quic_t * quic );
290 :
291 : FD_PROTOTYPES_END
292 :
293 : #endif /* HEADER_fd_src_waltz_quic_fd_quic_conn_h */
|