| 1 | /* |
| 2 | * ngtcp2 |
| 3 | * |
| 4 | * Copyright (c) 2025 ngtcp2 contributors |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | #ifndef NGTCP2_DCIDTR_H |
| 26 | #define NGTCP2_DCIDTR_H |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif /* defined(HAVE_CONFIG_H) */ |
| 31 | |
| 32 | #include <ngtcp2/ngtcp2.h> |
| 33 | |
| 34 | #include "ngtcp2_cid.h" |
| 35 | #include "ngtcp2_ringbuf.h" |
| 36 | |
| 37 | /* NGTCP2_DCIDTR_MAX_BOUND_DCID_SIZE is the maximum number of |
| 38 | Destination Connection ID which has been bound to a particular |
| 39 | path, but not yet used as primary path, and path validation is not |
| 40 | performed from the local endpoint. It must be the power of 2. */ |
| 41 | #define NGTCP2_DCIDTR_MAX_BOUND_DCID_SIZE 4 |
| 42 | /* NGTCP2_DCIDTR_MAX_UNUSED_DCID_SIZE is the maximum number of |
| 43 | Destination Connection ID the remote endpoint provides to store. |
| 44 | It must be the power of 2. */ |
| 45 | #define NGTCP2_DCIDTR_MAX_UNUSED_DCID_SIZE 8 |
| 46 | /* NGTCP2_DCIDTR_MAX_RETIRED_DCID_SIZE is the maximum number of |
| 47 | retired Destination Connection ID kept to catch in-flight packet on |
| 48 | a retired path. It must be the power of 2. */ |
| 49 | #define NGTCP2_DCIDTR_MAX_RETIRED_DCID_SIZE 2 |
| 50 | |
| 51 | ngtcp2_static_ringbuf_def(dcid_bound, NGTCP2_DCIDTR_MAX_BOUND_DCID_SIZE, |
| 52 | sizeof(ngtcp2_dcid)) |
| 53 | ngtcp2_static_ringbuf_def(dcid_unused, NGTCP2_DCIDTR_MAX_UNUSED_DCID_SIZE, |
| 54 | sizeof(ngtcp2_dcid)) |
| 55 | ngtcp2_static_ringbuf_def(dcid_retired, NGTCP2_DCIDTR_MAX_RETIRED_DCID_SIZE, |
| 56 | sizeof(ngtcp2_dcid)) |
| 57 | |
| 58 | /* |
| 59 | * ngtcp2_dcidtr stores unused, bound, and retired Destination |
| 60 | * Connection IDs. |
| 61 | */ |
| 62 | typedef struct ngtcp2_dcidtr { |
| 63 | /* unused is a set of unused Destination Connection ID received from |
| 64 | a remote endpoint. They are considered inactive. */ |
| 65 | ngtcp2_static_ringbuf_dcid_unused unused; |
| 66 | /* bound is a set of Destination Connection IDs which are bound to |
| 67 | particular paths. These paths are not validated yet. They are |
| 68 | considered inactive. */ |
| 69 | ngtcp2_static_ringbuf_dcid_bound bound; |
| 70 | /* retired is a set of Destination Connection ID retired by local |
| 71 | endpoint. Keep them in 3*PTO to catch packets in flight along |
| 72 | the old path. They are considered active. */ |
| 73 | ngtcp2_static_ringbuf_dcid_retired retired; |
| 74 | struct { |
| 75 | /* seqs contains sequence number of Destination Connection ID |
| 76 | whose retirement is not acknowledged by the remote endpoint |
| 77 | yet. */ |
| 78 | uint64_t seqs[NGTCP2_DCIDTR_MAX_UNUSED_DCID_SIZE * 2]; |
| 79 | /* len is the number of sequence numbers that seq contains. */ |
| 80 | size_t len; |
| 81 | } retire_unacked; |
| 82 | } ngtcp2_dcidtr; |
| 83 | |
| 84 | typedef int (*ngtcp2_dcidtr_cb)(const ngtcp2_dcid *dcid, void *user_data); |
| 85 | |
| 86 | /* |
| 87 | * ngtcp2_dcidtr_init initializes |dtr|. |
| 88 | */ |
| 89 | void ngtcp2_dcidtr_init(ngtcp2_dcidtr *dtr); |
| 90 | |
| 91 | /* |
| 92 | * ngtcp2_dcidtr_track_retired_seq tracks the sequence number |seq| of |
| 93 | * unacknowledged retiring Destination Connection ID. |
| 94 | * |
| 95 | * This function returns 0 if it succeeds, or one of the following |
| 96 | * negative error codes: |
| 97 | * |
| 98 | * NGTCP2_ERR_CONNECTION_ID_LIMIT |
| 99 | * The number of unacknowledged retirement exceeds the limit. |
| 100 | */ |
| 101 | int ngtcp2_dcidtr_track_retired_seq(ngtcp2_dcidtr *dtr, uint64_t seq); |
| 102 | |
| 103 | /* |
| 104 | * ngtcp2_dcidtr_untrack_retired_seq deletes the sequence number |seq| |
| 105 | * of unacknowledged retiring Destination Connection ID. It is fine |
| 106 | * if such sequence number is not found. |
| 107 | */ |
| 108 | void ngtcp2_dcidtr_untrack_retired_seq(ngtcp2_dcidtr *dtr, uint64_t seq); |
| 109 | |
| 110 | /* |
| 111 | * ngtcp2_dcidtr_check_retired_seq_tracked returns nonzero if |seq| |
| 112 | * has already been tracked. |
| 113 | */ |
| 114 | int ngtcp2_dcidtr_check_retired_seq_tracked(const ngtcp2_dcidtr *dtr, |
| 115 | uint64_t seq); |
| 116 | |
| 117 | /* |
| 118 | * ngtcp2_dcidtr_find_bound_dcid returns the pointer to ngtcp2_dcid |
| 119 | * that bound to |path|. It returns NULL if there is no such |
| 120 | * ngtcp2_dcid. |
| 121 | */ |
| 122 | ngtcp2_dcid *ngtcp2_dcidtr_find_bound_dcid(const ngtcp2_dcidtr *dtr, |
| 123 | const ngtcp2_path *path); |
| 124 | |
| 125 | /* |
| 126 | * ngtcp2_dcidtr_bind_zerolen_dcid binds zero-length Destination |
| 127 | * Connection ID to |path|, and returns the pointer to the bound |
| 128 | * ngtcp2_dcid. |
| 129 | */ |
| 130 | ngtcp2_dcid *ngtcp2_dcidtr_bind_zerolen_dcid(ngtcp2_dcidtr *dtr, |
| 131 | const ngtcp2_path *path); |
| 132 | |
| 133 | /* |
| 134 | * ngtcp2_dcidtr_bind_dcid binds non-zero Destination Connection ID to |
| 135 | * |path|. |ts| is the current timestamp. The buffer space of bound |
| 136 | * Destination Connection ID is limited. If it is full, the earliest |
| 137 | * one is removed. |on_retire|, if specified, is called for the |
| 138 | * removed ngtcp2_dcid with |user_data|. This function assigns the |
| 139 | * pointer to bound ngtcp2_dcid to |*pdest|. |
| 140 | * |
| 141 | * This function returns 0 if it succeeds, or negative error code that |
| 142 | * |on_retire| returns. |
| 143 | */ |
| 144 | int ngtcp2_dcidtr_bind_dcid(ngtcp2_dcidtr *dtr, ngtcp2_dcid **pdest, |
| 145 | const ngtcp2_path *path, ngtcp2_tstamp ts, |
| 146 | ngtcp2_dcidtr_cb on_retire, void *user_data); |
| 147 | |
| 148 | /* |
| 149 | * ngtcp2_dcidtr_verify_stateless_reset verifies the stateless reset |
| 150 | * token |token| received from |path|. It returns 0 if it succeeds, |
| 151 | * or one of the following negative error codes: |
| 152 | * |
| 153 | * NGTCP2_ERR_INVALID_ARGUMENT |
| 154 | * There is no Destination Connection ID that matches the given |
| 155 | * |path| and |token|. |
| 156 | */ |
| 157 | int ngtcp2_dcidtr_verify_stateless_reset(const ngtcp2_dcidtr *dtr, |
| 158 | const ngtcp2_path *path, |
| 159 | const uint8_t *token); |
| 160 | |
| 161 | /* |
| 162 | * ngtcp2_dcidtr_verify_token_uniqueness verifies that the uniqueness |
| 163 | * of the combination of |seq|, |cid|, and |token| against the exiting |
| 164 | * Destination Connection IDs. That is: |
| 165 | * |
| 166 | * - If they do not share the same seq, then their Connection IDs must |
| 167 | * be different. |
| 168 | * |
| 169 | * - If they share the same seq, then their Connection IDs and tokens |
| 170 | * must be the same. |
| 171 | * |
| 172 | * If this function succeeds, and there is Destination Connection ID |
| 173 | * which shares |seq|, |cid|, and |token|, |*pfound| is set to |
| 174 | * nonzero. |
| 175 | * |
| 176 | * This function returns 0 if it succeeds, or one of the following |
| 177 | * negative error codes: |
| 178 | * |
| 179 | * NGTCP2_ERR_PROTO |
| 180 | * The given combination of values does not satisfy the above |
| 181 | * conditions. |
| 182 | */ |
| 183 | int ngtcp2_dcidtr_verify_token_uniqueness(const ngtcp2_dcidtr *dtr, int *pfound, |
| 184 | uint64_t seq, const ngtcp2_cid *cid, |
| 185 | const uint8_t *token); |
| 186 | |
| 187 | /* |
| 188 | * ngtcp2_dcidtr_retire_inactive_dcid_prior_to retires inactive |
| 189 | * Destination Connection IDs (unused or bound) whose seq is less than |
| 190 | * |seq|. For each retired ngtcp2_dcid, |on_retire|, if specified, is |
| 191 | * called with |user_data|. |
| 192 | * |
| 193 | * This function returns 0 if it succeeds, or negative error code that |
| 194 | * |on_retire| returns. |
| 195 | */ |
| 196 | int ngtcp2_dcidtr_retire_inactive_dcid_prior_to(ngtcp2_dcidtr *dtr, |
| 197 | uint64_t seq, |
| 198 | ngtcp2_dcidtr_cb on_retire, |
| 199 | void *user_data); |
| 200 | |
| 201 | /* |
| 202 | * ngtcp2_dcidtr_retire_active_dcid adds an active |dcid| to the |
| 203 | * retired Destination Connection ID buffer. The buffer space of |
| 204 | * retired Destination Connection ID is limited. If it is full, the |
| 205 | * earliest one is removed. |on_deactivate| is called for the removed |
| 206 | * ngtcp2_dcid with |user_data|. |
| 207 | * |
| 208 | * This function returns 0 if it succeeds, or negative error code that |
| 209 | * |on_deactivate| returns. |
| 210 | */ |
| 211 | int ngtcp2_dcidtr_retire_active_dcid(ngtcp2_dcidtr *dtr, |
| 212 | const ngtcp2_dcid *dcid, ngtcp2_tstamp ts, |
| 213 | ngtcp2_dcidtr_cb on_deactivate, |
| 214 | void *user_data); |
| 215 | |
| 216 | /* |
| 217 | * ngtcp2_dcidtr_retire_stale_bound_dcid retires stale bound |
| 218 | * Destination Connection ID. For each retired ngtcp2_dcid, |
| 219 | * |on_retire|, if specified, is called with |user_data|. |
| 220 | */ |
| 221 | int ngtcp2_dcidtr_retire_stale_bound_dcid(ngtcp2_dcidtr *dtr, |
| 222 | ngtcp2_duration timeout, |
| 223 | ngtcp2_tstamp ts, |
| 224 | ngtcp2_dcidtr_cb on_retire, |
| 225 | void *user_data); |
| 226 | |
| 227 | /* |
| 228 | * ngtcp2_dcidtr_remove_stale_retired_dcid removes stale retired |
| 229 | * Destination Connection ID. For each removed ngtcp2_dcid, |
| 230 | * |on_deactivate| is called with |user_data|. |
| 231 | * |
| 232 | * This function returns 0 if it succeeds, or negative error code that |
| 233 | * |on_deactivate| returns. |
| 234 | */ |
| 235 | int ngtcp2_dcidtr_remove_stale_retired_dcid(ngtcp2_dcidtr *dtr, |
| 236 | ngtcp2_duration timeout, |
| 237 | ngtcp2_tstamp ts, |
| 238 | ngtcp2_dcidtr_cb on_deactivate, |
| 239 | void *user_data); |
| 240 | |
| 241 | /* |
| 242 | * ngtcp2_dcidtr_pop_bound_dcid removes Destination Connection ID that |
| 243 | * is bound to |path|, and copies it into the object pointed by |
| 244 | * |dest|. |
| 245 | * |
| 246 | * This function returns 0 if it succeeds, or one of the following |
| 247 | * negative error codes: |
| 248 | * |
| 249 | * NGTCP2_ERR_INVALID_ARGUMENT |
| 250 | * No ngtcp2_dcid bound to |path| found. |
| 251 | */ |
| 252 | int ngtcp2_dcidtr_pop_bound_dcid(ngtcp2_dcidtr *dtr, ngtcp2_dcid *dest, |
| 253 | const ngtcp2_path *path); |
| 254 | |
| 255 | /* |
| 256 | * ngtcp2_dcidtr_earliest_bound_ts returns earliest timestamp when a |
| 257 | * Destination Connection ID is bound. If there is no bound |
| 258 | * Destination Connection ID, this function returns UINT64_MAX. |
| 259 | */ |
| 260 | ngtcp2_tstamp ngtcp2_dcidtr_earliest_bound_ts(const ngtcp2_dcidtr *dtr); |
| 261 | |
| 262 | /* |
| 263 | * ngtcp2_dcidtr_earliest_retired_ts returns earliest timestamp when a |
| 264 | * Destination Connection ID is retired. If there is no retired |
| 265 | * Destination Connection ID, this function returns UINT64_MAX. |
| 266 | */ |
| 267 | ngtcp2_tstamp ngtcp2_dcidtr_earliest_retired_ts(const ngtcp2_dcidtr *dtr); |
| 268 | |
| 269 | /* |
| 270 | * ngtcp2_dcidtr_push_unused adds new Destination Connection ID to the |
| 271 | * unused buffer. |seq| is its sequence number, |cid| is its |
| 272 | * Connection ID, and |token| is its stateless reset token. If the |
| 273 | * buffer space is full, the earliest ngtcp2_dcid is removed. |
| 274 | */ |
| 275 | void ngtcp2_dcidtr_push_unused(ngtcp2_dcidtr *dtr, uint64_t seq, |
| 276 | const ngtcp2_cid *cid, const uint8_t *token); |
| 277 | |
| 278 | /* |
| 279 | * ngtcp2_dcidtr_pop_unused_cid_token removes an unused Destination |
| 280 | * Connection ID, and copies it into the object pointed by |dcid| with |
| 281 | * ngtcp2_dcid_copy_cid_token. This function assumes that there is at |
| 282 | * least one unused Destination Connection ID. |
| 283 | */ |
| 284 | void ngtcp2_dcidtr_pop_unused_cid_token(ngtcp2_dcidtr *dtr, ngtcp2_dcid *dcid); |
| 285 | |
| 286 | /* |
| 287 | * ngtcp2_dcidtr_pop_unused removes an unused Destination Connection |
| 288 | * ID, and copies it into the object pointed by |dcid| with |
| 289 | * ngtcp2_dcid_copy. This function assumes that there is at least one |
| 290 | * unused Destination Connection ID. |
| 291 | */ |
| 292 | void ngtcp2_dcidtr_pop_unused(ngtcp2_dcidtr *dtr, ngtcp2_dcid *dcid); |
| 293 | |
| 294 | /* |
| 295 | * ngtcp2_dcidtr_check_path_retired returns nonzero if |path| is |
| 296 | * included in retired Destination Connection IDs. |
| 297 | */ |
| 298 | int ngtcp2_dcidtr_check_path_retired(const ngtcp2_dcidtr *dtr, |
| 299 | const ngtcp2_path *path); |
| 300 | |
| 301 | /* |
| 302 | * ngtcp2_dcidtr_unused_len returns the number of unused Destination |
| 303 | * Connection ID. |
| 304 | */ |
| 305 | size_t ngtcp2_dcidtr_unused_len(const ngtcp2_dcidtr *dtr); |
| 306 | |
| 307 | /* |
| 308 | * ngtcp2_dcidtr_bound_len returns the number of bound Destination |
| 309 | * Connection ID. |
| 310 | */ |
| 311 | size_t ngtcp2_dcidtr_bound_len(const ngtcp2_dcidtr *dtr); |
| 312 | |
| 313 | /* |
| 314 | * ngtcp2_dcidtr_retired_len returns the number of retired Destination |
| 315 | * Connection ID. |
| 316 | */ |
| 317 | size_t ngtcp2_dcidtr_retired_len(const ngtcp2_dcidtr *dtr); |
| 318 | |
| 319 | /* |
| 320 | * ngtcp2_dcidtr_inactive_len returns the number of unused and bound |
| 321 | * Destination Connection ID. |
| 322 | */ |
| 323 | size_t ngtcp2_dcidtr_inactive_len(const ngtcp2_dcidtr *dtr); |
| 324 | |
| 325 | /* |
| 326 | * ngtcp2_dcidtr_unused_full returns nonzero if the buffer of unused |
| 327 | * Destination Connection ID is full. |
| 328 | */ |
| 329 | int ngtcp2_dcidtr_unused_full(const ngtcp2_dcidtr *dtr); |
| 330 | |
| 331 | /* |
| 332 | * ngtcp2_dcidtr_unused_empty returns nonzero if the buffer of unused |
| 333 | * Destination Connection ID is empty. |
| 334 | */ |
| 335 | int ngtcp2_dcidtr_unused_empty(const ngtcp2_dcidtr *dtr); |
| 336 | |
| 337 | /* |
| 338 | * ngtcp2_dcidtr_bound_full returns nonzero if the buffer of bound |
| 339 | * Destination Connection ID is full. |
| 340 | */ |
| 341 | int ngtcp2_dcidtr_bound_full(const ngtcp2_dcidtr *dtr); |
| 342 | |
| 343 | #endif /* !defined(NGTCP2_DCIDTR_H) */ |