summaryrefslogtreecommitdiffstats
path: root/src/libknot/quic/quic_conn.h
blob: 49e063169f4742009a06873a9692f13b3c9b1589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*  Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*!
 * \file
 *
 * \brief QUIC connection management.
 *
 * \addtogroup quic
 * @{
 */

#pragma once

#include <stdbool.h>
#include <stdint.h>
#include <sys/uio.h>

#include "contrib/atomic.h"

#define MAX_STREAMS_PER_CONN 10 // this limits the number of un-finished streams per conn (i.e. if response has been recvd with FIN, it doesn't count)

struct gnutls_priority_st;
struct ngtcp2_cid; // declaration taken from wherever in ngtcp2
struct knot_creds;
struct knot_quic_reply;
struct knot_sweep_stats;

// those are equivalent to contrib/ucw/lists.h , just must not be included.
typedef struct knot_quic_ucw_node {
	struct knot_quic_ucw_node *next, *prev;
} knot_quic_ucw_node_t;
typedef struct knot_quic_ucw_list {
	knot_quic_ucw_node_t head, tail;
} knot_quic_ucw_list_t;

typedef struct {
	void *get_conn;
	void *user_data;
} nc_conn_ref_placeholder_t;

typedef struct {
	knot_quic_ucw_node_t node;
	size_t len;
	uint8_t buf[];
} knot_quic_obuf_t;

typedef struct {
	struct iovec inbuf;
	struct knot_tcp_inbufs_upd_res *inbufs;
	size_t firstib_consumed;
	knot_quic_ucw_list_t outbufs;
	size_t obufs_size;

	knot_quic_obuf_t *unsent_obuf;
	size_t first_offset;
	size_t unsent_offset;
} knot_quic_stream_t;

typedef enum {
	KNOT_QUIC_CONN_HANDSHAKE_DONE = (1 << 0),
	KNOT_QUIC_CONN_SESSION_TAKEN  = (1 << 1),
	KNOT_QUIC_CONN_BLOCKED        = (1 << 2),
} knot_quic_conn_flag_t;

typedef struct knot_quic_conn {
	int heap_node_placeholder; // MUST be first field of the struct
	uint64_t next_expiry;

	nc_conn_ref_placeholder_t conn_ref; // placeholder for internal struct ngtcp2_crypto_conn_ref

	struct ngtcp2_conn *conn;

	struct gnutls_session_int *tls_session;

	knot_quic_stream_t *streams;
	int16_t streams_count; // number of allocated streams structures
	int16_t stream_inprocess; // index of first stream that has complete incomming data to be processed (aka inbuf_fin)
	knot_quic_conn_flag_t flags;
	int qlog_fd;
	int64_t streams_first; // stream_id/4 of first allocated stream
	size_t ibufs_size;
	size_t obufs_size;

	struct knot_quic_table *quic_table;

	struct knot_quic_conn *next;
} knot_quic_conn_t;

typedef struct knot_quic_cid {
	uint8_t cid_placeholder[32];
	knot_quic_conn_t *conn;
	struct knot_quic_cid *next;
} knot_quic_cid_t;

typedef struct knot_quic_table {
	uint32_t flags; // unused yet
	size_t size;
	size_t usage;
	size_t pointers;
	size_t max_conns;
	size_t ibufs_max;
	size_t obufs_max;
	size_t ibufs_size;
	knot_atomic_size_t obufs_size;
	size_t udp_payload_limit; // for simplicity not distinguishing IPv4/6
	void (*log_cb)(const char *);
	const char *qlog_dir;
	uint64_t hash_secret[4];
	struct knot_creds *creds;
	struct gnutls_priority_st *priority;
	struct heap *expiry_heap;
	knot_quic_cid_t *conns[];
} knot_quic_table_t;

/*!
 * \brief Allocate QUIC connections hash table.
 *
 * \param max_conns    Maximum nuber of connections.
 * \param max_ibufs    Maximum size of buffers for fragmented incomming DNS msgs.
 * \param max_obufs    Maximum size of buffers for un-ACKed outgoing data.
 * \param udp_payload  Maximum UDP payload size (both IPv4 and 6).
 * \param creds        QUIC crypto context..
 *
 * \return Allocated table, or NULL.
 */
knot_quic_table_t *knot_quic_table_new(size_t max_conns, size_t max_ibufs, size_t max_obufs,
                                       size_t udp_payload, struct knot_creds *creds);

/*!
 * \brief Free QUIC table including its contents.
 *
 * \param table    Table to be freed.
 */
void knot_quic_table_free(knot_quic_table_t *table);

/*!
 * \brief Close timed out connections and some oldest ones if table full.
 *
 * \param table       QUIC table to be cleaned up.
 * \param sweep_reply Optional: reply structure to send sweep-initiated packets to the client.
 * \param stats       Out: sweep statistics.
 */
void knot_quic_table_sweep(knot_quic_table_t *table, struct knot_quic_reply *sweep_reply,
                           struct knot_sweep_stats *stats);

/*!
 * \brief Add new connection/CID link to table.
 *
 * \param conn    QUIC connection linked.
 * \param cid     New CID to be added.
 * \param table   QUIC table to be modified.
 *
 * \return Pointer on the CID reference in table, or NULL.
 */
knot_quic_cid_t **quic_table_insert(knot_quic_conn_t *conn,
                                    const struct ngtcp2_cid *cid,
                                    knot_quic_table_t *table);

/*!
 * \brief Add new connection to the table, allocating conn struct.
 *
 * \param ngconn    Ngtcp2 conn struct.
 * \param cid       CID to be linked (usually oscid for server).
 * \param table     QUIC table to be modified.
 *
 * \return Allocated (and linked) Knot conn struct, or NULL.
 */
knot_quic_conn_t *quic_table_add(struct ngtcp2_conn *ngconn,
                                 const struct ngtcp2_cid *cid,
                                 knot_quic_table_t *table);

/*!
 * \brief Lookup connection/CID link in table.
 *
 * \param cid      CID to be searched for.
 * \param table    QUIC table.
 *
 * \return Pointer on the CID reference in table, or NULL.
 */
knot_quic_cid_t **quic_table_lookup2(const struct ngtcp2_cid *cid,
                                     knot_quic_table_t *table);

/*!
 * \brief Lookup QUIC connection in table.
 *
 * \param cid      CID to be searched for.
 * \param table    QUIC table.
 *
 * \return Connection that the CID belongs to, or NULL.
 */
knot_quic_conn_t *quic_table_lookup(const struct ngtcp2_cid *cid,
                                    knot_quic_table_t *table);

/*!
 * \brief Re-schedule connection expiry timer.
 */
void quic_conn_mark_used(knot_quic_conn_t *conn, knot_quic_table_t *table);

/*!
 * \brief Remove connection/CID link from table.
 *
 * \param pcid     CID to be removed.
 * \param table    QUIC table.
 */
void quic_table_rem2(knot_quic_cid_t **pcid, knot_quic_table_t *table);

/*!
 * \brief Remove specified stream from QUIC connection, freeing all buffers.
 *
 * \param conn          QUIC connection to remove from.
 * \param stream_id     Stream QUIC ID.
 */
void knot_quic_conn_stream_free(knot_quic_conn_t *conn, int64_t stream_id);

/*!
 * \brief Remove and deinitialize connection completely.
 *
 * \param conn      Connection to be removed.
 * \param table     Table to remove from.
 */
void knot_quic_table_rem(knot_quic_conn_t *conn, knot_quic_table_t *table);

/*!
 * \brief Fetch or initialize a QUIC stream.
 *
 * \param conn           QUIC connection.
 * \param stream_id      Stream QUIC ID.
 * \param create         Trigger stream creation if not exists.
 *
 * \return Stream or NULL.
 */
knot_quic_stream_t *knot_quic_conn_get_stream(knot_quic_conn_t *conn,
                                              int64_t stream_id, bool create);

/*!
 * \brief Create a new, subsequent stream.
 */
knot_quic_stream_t *knot_quic_conn_new_stream(knot_quic_conn_t *conn);

/*!
 * \brief Process incomming stream data to stream structure.
 *
 * \param conn          QUIC connection that has received data.
 * \param stream_id     Stream QUIC ID of the incomming data.
 * \param data          Incomming payload data.
 * \param len           Incomming payload data length.
 * \param fin           FIN flag set for incomming data.
 *
 * \return KNOT_E*
 */
int knot_quic_stream_recv_data(knot_quic_conn_t *conn, int64_t stream_id,
                               const uint8_t *data, size_t len, bool fin);

/*!
 * \brief Get next stream which has pending incomming data to be processed.
 *
 * \param conn         QUIC connection.
 * \param stream_id    Out: strem QUIC ID of the returned stream.
 *
 * \return Stream with incomming data.
 */
knot_quic_stream_t *knot_quic_stream_get_process(knot_quic_conn_t *conn,
                                                 int64_t *stream_id);

/*!
 * \brief Add outgiong data to the stream for sending.
 *
 * \param conn          QUIC connection that shall send data.
 * \param stream_id     Stream ID for outgoing data.
 * \param data          Data payload.
 * \param len           Data payload length.
 *
 * \return NULL if error, or pinter at the data in outgiong buffer.
 */
uint8_t *knot_quic_stream_add_data(knot_quic_conn_t *conn, int64_t stream_id,
                                   uint8_t *data, size_t len);

/*!
 * \brief Mark outgiong data as acknowledged after ACK received.
 *
 * \param conn           QUIC connection that received ACK.
 * \param stream_id      Stream ID of ACKed data.
 * \param end_acked      Offset of ACKed data + ACKed length.
 * \param keep_stream    Don't free the stream even when ACKed all outgoing data.
 */
void knot_quic_stream_ack_data(knot_quic_conn_t *conn, int64_t stream_id,
                               size_t end_acked, bool keep_stream);

/*!
 * \brief Mark outgoing data as sent.
 *
 * \param conn           QUIC connection that sent data.
 * \param stream_id      Stream ID of sent data.
 * \param amount_sent    Length of sent data.
 */
void knot_quic_stream_mark_sent(knot_quic_conn_t *conn, int64_t stream_id,
                                size_t amount_sent);

/*!
 * \brief (Un)block the connection for incoming/outgoing traffic and sweep.
 */
void knot_quic_conn_block(knot_quic_conn_t *conn, bool block);

/*!
 * \brief Free rest of resources of closed conns.
 *
 * \param conns      Array with recently used conns (possibly NULLs).
 * \param n_conns    Size of the array.
 */
void knot_quic_cleanup(knot_quic_conn_t *conns[], size_t n_conns);

/*!
 * \brief Toggle sending Retry packet as a reaction to Initial packet of new connection.
 *
 * \param table       Connection table.
 *
 * \return True if instead of continuing handshake, Retry packet shall be sent
 *              to verify counterpart's address.
 */
bool quic_require_retry(knot_quic_table_t *table);

/*! @} */