/src/openssl/ssl/quic/qlog_event_helpers.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include "internal/qlog_event_helpers.h"  | 
11  |  | #include "internal/common.h"  | 
12  |  | #include "internal/packet.h"  | 
13  |  | #include "internal/quic_channel.h"  | 
14  |  | #include "internal/quic_error.h"  | 
15  |  |  | 
16  |  | void ossl_qlog_event_connectivity_connection_started(QLOG *qlog,  | 
17  |  |                                                      const QUIC_CONN_ID *init_dcid)  | 
18  | 0  | { | 
19  | 0  | #ifndef OPENSSL_NO_QLOG  | 
20  | 0  |     QLOG_EVENT_BEGIN(qlog, connectivity, connection_started)  | 
21  | 0  |         QLOG_STR("protocol", "quic"); | 
22  | 0  |         QLOG_CID("dst_cid", init_dcid); | 
23  | 0  |     QLOG_EVENT_END()  | 
24  | 0  | #endif  | 
25  | 0  | }  | 
26  |  |  | 
27  |  | #ifndef OPENSSL_NO_QLOG  | 
28  |  | static const char *map_state_to_qlog(uint32_t state,  | 
29  |  |                                      int handshake_complete,  | 
30  |  |                                      int handshake_confirmed)  | 
31  | 0  | { | 
32  | 0  |     switch (state) { | 
33  | 0  |     default:  | 
34  | 0  |     case QUIC_CHANNEL_STATE_IDLE:  | 
35  | 0  |         return NULL;  | 
36  |  |  | 
37  | 0  |     case QUIC_CHANNEL_STATE_ACTIVE:  | 
38  | 0  |         if (handshake_confirmed)  | 
39  | 0  |             return "handshake_confirmed";  | 
40  | 0  |         else if (handshake_complete)  | 
41  | 0  |             return "handshake_complete";  | 
42  | 0  |         else  | 
43  | 0  |             return "attempted";  | 
44  |  |  | 
45  | 0  |     case QUIC_CHANNEL_STATE_TERMINATING_CLOSING:  | 
46  | 0  |         return "closing";  | 
47  |  |  | 
48  | 0  |     case QUIC_CHANNEL_STATE_TERMINATING_DRAINING:  | 
49  | 0  |         return "draining";  | 
50  |  |  | 
51  | 0  |     case QUIC_CHANNEL_STATE_TERMINATED:  | 
52  | 0  |         return "closed";  | 
53  | 0  |     }  | 
54  | 0  | }  | 
55  |  | #endif  | 
56  |  |  | 
57  |  | void ossl_qlog_event_connectivity_connection_state_updated(QLOG *qlog,  | 
58  |  |                                                            uint32_t old_state,  | 
59  |  |                                                            uint32_t new_state,  | 
60  |  |                                                            int handshake_complete,  | 
61  |  |                                                            int handshake_confirmed)  | 
62  | 0  | { | 
63  | 0  | #ifndef OPENSSL_NO_QLOG  | 
64  | 0  |     const char *state_s;  | 
65  |  | 
  | 
66  | 0  |     QLOG_EVENT_BEGIN(qlog, connectivity, connection_state_updated)  | 
67  | 0  |         state_s = map_state_to_qlog(new_state,  | 
68  | 0  |                                     handshake_complete,  | 
69  | 0  |                                     handshake_confirmed);  | 
70  |  | 
  | 
71  | 0  |         if (state_s != NULL)  | 
72  | 0  |             QLOG_STR("state", state_s); | 
73  | 0  |     QLOG_EVENT_END()  | 
74  | 0  | #endif  | 
75  | 0  | }  | 
76  |  |  | 
77  |  | #ifndef OPENSSL_NO_QLOG  | 
78  |  | static const char *quic_err_to_qlog(uint64_t error_code)  | 
79  | 0  | { | 
80  | 0  |     switch (error_code) { | 
81  | 0  |     case OSSL_QUIC_ERR_INTERNAL_ERROR:  | 
82  | 0  |         return "internal_error";  | 
83  | 0  |     case OSSL_QUIC_ERR_CONNECTION_REFUSED:  | 
84  | 0  |         return "connection_refused";  | 
85  | 0  |     case OSSL_QUIC_ERR_FLOW_CONTROL_ERROR:  | 
86  | 0  |         return "flow_control_error";  | 
87  | 0  |     case OSSL_QUIC_ERR_STREAM_LIMIT_ERROR:  | 
88  | 0  |         return "stream_limit_error";  | 
89  | 0  |     case OSSL_QUIC_ERR_STREAM_STATE_ERROR:  | 
90  | 0  |         return "stream_state_error";  | 
91  | 0  |     case OSSL_QUIC_ERR_FINAL_SIZE_ERROR:  | 
92  | 0  |         return "final_size_error";  | 
93  | 0  |     case OSSL_QUIC_ERR_FRAME_ENCODING_ERROR:  | 
94  | 0  |         return "frame_encoding_error";  | 
95  | 0  |     case OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR:  | 
96  | 0  |         return "transport_parameter_error";  | 
97  | 0  |     case OSSL_QUIC_ERR_CONNECTION_ID_LIMIT_ERROR:  | 
98  | 0  |         return "connection_id_limit_error";  | 
99  | 0  |     case OSSL_QUIC_ERR_PROTOCOL_VIOLATION:  | 
100  | 0  |         return "protocol_violation";  | 
101  | 0  |     case OSSL_QUIC_ERR_INVALID_TOKEN:  | 
102  | 0  |         return "invalid_token";  | 
103  | 0  |     case OSSL_QUIC_ERR_APPLICATION_ERROR:  | 
104  | 0  |         return "application_error";  | 
105  | 0  |     case OSSL_QUIC_ERR_CRYPTO_BUFFER_EXCEEDED:  | 
106  | 0  |         return "crypto_buffer_exceeded";  | 
107  | 0  |     case OSSL_QUIC_ERR_KEY_UPDATE_ERROR:  | 
108  | 0  |         return "key_update_error";  | 
109  | 0  |     case OSSL_QUIC_ERR_AEAD_LIMIT_REACHED:  | 
110  | 0  |         return "aead_limit_reached";  | 
111  | 0  |     case OSSL_QUIC_ERR_NO_VIABLE_PATH:  | 
112  | 0  |         return "no_viable_path";  | 
113  | 0  |     default:  | 
114  | 0  |         return NULL;  | 
115  | 0  |     }  | 
116  | 0  | }  | 
117  |  | #endif  | 
118  |  |  | 
119  |  | void ossl_qlog_event_connectivity_connection_closed(QLOG *qlog,  | 
120  |  |                                                     const QUIC_TERMINATE_CAUSE *tcause)  | 
121  | 0  | { | 
122  | 0  | #ifndef OPENSSL_NO_QLOG  | 
123  | 0  |     QLOG_EVENT_BEGIN(qlog, connectivity, connection_closed)  | 
124  | 0  |         QLOG_STR("owner", tcause->remote ? "remote" : "local"); | 
125  | 0  |         if (tcause->app) { | 
126  | 0  |             QLOG_U64("application_code", tcause->error_code); | 
127  | 0  |         } else { | 
128  | 0  |             const char *m = quic_err_to_qlog(tcause->error_code);  | 
129  | 0  |             char ce[32];  | 
130  |  | 
  | 
131  | 0  |             if (tcause->error_code >= OSSL_QUIC_ERR_CRYPTO_ERR_BEGIN  | 
132  | 0  |                 && tcause->error_code <= OSSL_QUIC_ERR_CRYPTO_ERR_END) { | 
133  | 0  |                 BIO_snprintf(ce, sizeof(ce), "crypto_error_0x%03llx",  | 
134  | 0  |                              (unsigned long long)tcause->error_code);  | 
135  | 0  |                 m = ce;  | 
136  | 0  |             }  | 
137  |  |             /* TODO(QLOG FUTURE): Consider adding ERR information in the output. */  | 
138  |  | 
  | 
139  | 0  |             if (m != NULL)  | 
140  | 0  |                 QLOG_STR("connection_code", m); | 
141  | 0  |             else  | 
142  | 0  |                 QLOG_U64("connection_code", tcause->error_code); | 
143  | 0  |         }  | 
144  |  | 
  | 
145  | 0  |         QLOG_STR_LEN("reason", tcause->reason, tcause->reason_len); | 
146  | 0  |     QLOG_EVENT_END()  | 
147  | 0  | #endif  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | #ifndef OPENSSL_NO_QLOG  | 
151  |  | static const char *quic_pkt_type_to_qlog(uint32_t pkt_type)  | 
152  | 0  | { | 
153  | 0  |     switch (pkt_type) { | 
154  | 0  |     case QUIC_PKT_TYPE_INITIAL:  | 
155  | 0  |         return "initial";  | 
156  | 0  |     case QUIC_PKT_TYPE_HANDSHAKE:  | 
157  | 0  |         return "handshake";  | 
158  | 0  |     case QUIC_PKT_TYPE_0RTT:  | 
159  | 0  |         return "0RTT";  | 
160  | 0  |     case QUIC_PKT_TYPE_1RTT:  | 
161  | 0  |         return "1RTT";  | 
162  | 0  |     case QUIC_PKT_TYPE_VERSION_NEG:  | 
163  | 0  |         return "version_negotiation";  | 
164  | 0  |     case QUIC_PKT_TYPE_RETRY:  | 
165  | 0  |         return "retry";  | 
166  | 0  |     default:  | 
167  | 0  |         return "unknown";  | 
168  | 0  |     }  | 
169  | 0  | }  | 
170  |  | #endif  | 
171  |  |  | 
172  |  | void ossl_qlog_event_recovery_packet_lost(QLOG *qlog,  | 
173  |  |                                           const QUIC_TXPIM_PKT *tpkt)  | 
174  | 0  | { | 
175  | 0  | #ifndef OPENSSL_NO_QLOG  | 
176  | 0  |     QLOG_EVENT_BEGIN(qlog, recovery, packet_lost)  | 
177  | 0  |         QLOG_BEGIN("header") | 
178  | 0  |             QLOG_STR("packet_type", quic_pkt_type_to_qlog(tpkt->pkt_type)); | 
179  | 0  |             if (ossl_quic_pkt_type_has_pn(tpkt->pkt_type))  | 
180  | 0  |                 QLOG_U64("packet_number", tpkt->ackm_pkt.pkt_num); | 
181  | 0  |         QLOG_END()  | 
182  | 0  |     QLOG_EVENT_END()  | 
183  | 0  | #endif  | 
184  | 0  | }  | 
185  |  |  | 
186  |  | #ifndef OPENSSL_NO_QLOG  | 
187  |  | # define MAX_ACK_RANGES 32  | 
188  |  |  | 
189  | 0  | static void ignore_res(int x) {} | 
190  |  |  | 
191  |  | /*  | 
192  |  |  * For logging received packets, we need to parse all the frames in the packet  | 
193  |  |  * to log them. We should do this separately to the RXDP code because we want to  | 
194  |  |  * log the packet and its contents before we start to actually process it in  | 
195  |  |  * case it causes an error. We also in general don't want to do other  | 
196  |  |  * non-logging related work in the middle of an event logging transaction.  | 
197  |  |  * Reparsing packet data allows us to meet these needs while avoiding the need  | 
198  |  |  * to keep around bookkeeping data on what frames were in a packet, etc.  | 
199  |  |  *  | 
200  |  |  * For logging transmitted packets, we actually reuse the same code and reparse  | 
201  |  |  * the outgoing packet's payload. This again has the advantage that we only log  | 
202  |  |  * a packet when it is actually queued for transmission (and not if something  | 
203  |  |  * goes wrong before then) while avoiding the need to keep around bookkeeping  | 
204  |  |  * data on what frames it contained.  | 
205  |  |  */  | 
206  |  | static int log_frame_actual(QLOG *qlog_instance, PACKET *pkt,  | 
207  |  |                             size_t *need_skip)  | 
208  | 0  | { | 
209  | 0  |     uint64_t frame_type;  | 
210  | 0  |     OSSL_QUIC_FRAME_ACK ack;  | 
211  | 0  |     OSSL_QUIC_ACK_RANGE ack_ranges[MAX_ACK_RANGES];  | 
212  | 0  |     uint64_t num_ranges, total_ranges;  | 
213  | 0  |     size_t i;  | 
214  | 0  |     PACKET orig_pkt = *pkt;  | 
215  |  | 
  | 
216  | 0  |     if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, NULL)) { | 
217  | 0  |         *need_skip = SIZE_MAX;  | 
218  | 0  |         return 0;  | 
219  | 0  |     }  | 
220  |  |  | 
221  |  |     /*  | 
222  |  |      * If something goes wrong decoding a frame we cannot log it as that frame  | 
223  |  |      * as we need to know how to decode it in order to be able to do so, but in  | 
224  |  |      * that case we log it as an unknown frame to assist with diagnosis.  | 
225  |  |      */  | 
226  | 0  |     switch (frame_type) { | 
227  | 0  |     case OSSL_QUIC_FRAME_TYPE_PADDING:  | 
228  | 0  |         QLOG_STR("frame_type", "padding"); | 
229  | 0  |         QLOG_U64("payload_length", | 
230  | 0  |                  ossl_quic_wire_decode_padding(pkt));  | 
231  | 0  |         break;  | 
232  | 0  |     case OSSL_QUIC_FRAME_TYPE_PING:  | 
233  | 0  |         if (!ossl_quic_wire_decode_frame_ping(pkt))  | 
234  | 0  |             goto unknown;  | 
235  |  |  | 
236  | 0  |         QLOG_STR("frame_type", "ping"); | 
237  | 0  |         break;  | 
238  | 0  |     case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:  | 
239  | 0  |     case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:  | 
240  | 0  |         if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &num_ranges))  | 
241  | 0  |             goto unknown;  | 
242  |  |  | 
243  | 0  |         ack.ack_ranges      = ack_ranges;  | 
244  | 0  |         ack.num_ack_ranges  = OSSL_NELEM(ack_ranges);  | 
245  | 0  |         if (!ossl_quic_wire_decode_frame_ack(pkt, 3, &ack, &total_ranges))  | 
246  | 0  |             goto unknown;  | 
247  |  |  | 
248  | 0  |         QLOG_STR("frame_type", "ack"); | 
249  | 0  |         QLOG_U64("ack_delay", ossl_time2ms(ack.delay_time)); | 
250  | 0  |         if (ack.ecn_present) { | 
251  | 0  |             QLOG_U64("ect1", ack.ect0); | 
252  | 0  |             QLOG_U64("ect0", ack.ect1); | 
253  | 0  |             QLOG_U64("ce", ack.ecnce); | 
254  | 0  |         }  | 
255  | 0  |         QLOG_BEGIN_ARRAY("acked_ranges"); | 
256  | 0  |         for (i = 0; i < ack.num_ack_ranges; ++i) { | 
257  | 0  |             QLOG_BEGIN_ARRAY(NULL)  | 
258  | 0  |                 QLOG_U64(NULL, ack.ack_ranges[i].start);  | 
259  | 0  |                 if (ack.ack_ranges[i].end != ack.ack_ranges[i].start)  | 
260  | 0  |                     QLOG_U64(NULL, ack.ack_ranges[i].end);  | 
261  | 0  |             QLOG_END_ARRAY()  | 
262  | 0  |         }  | 
263  | 0  |         QLOG_END_ARRAY()  | 
264  | 0  |         break;  | 
265  | 0  |     case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:  | 
266  | 0  |         { | 
267  | 0  |             OSSL_QUIC_FRAME_RESET_STREAM f;  | 
268  |  | 
  | 
269  | 0  |             if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &f))  | 
270  | 0  |                 goto unknown;  | 
271  |  |  | 
272  | 0  |             QLOG_STR("frame_type", "reset_stream"); | 
273  | 0  |             QLOG_U64("stream_id", f.stream_id); | 
274  | 0  |             QLOG_U64("error_code", f.app_error_code); | 
275  | 0  |             QLOG_U64("final_size", f.final_size); | 
276  | 0  |         }  | 
277  | 0  |         break;  | 
278  | 0  |     case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:  | 
279  | 0  |         { | 
280  | 0  |             OSSL_QUIC_FRAME_STOP_SENDING f;  | 
281  |  | 
  | 
282  | 0  |             if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &f))  | 
283  | 0  |                 goto unknown;  | 
284  |  |  | 
285  | 0  |             QLOG_STR("frame_type", "stop_sending"); | 
286  | 0  |             QLOG_U64("stream_id", f.stream_id); | 
287  | 0  |             QLOG_U64("error_code", f.app_error_code); | 
288  | 0  |         }  | 
289  | 0  |         break;  | 
290  | 0  |     case OSSL_QUIC_FRAME_TYPE_CRYPTO:  | 
291  | 0  |         { | 
292  | 0  |             OSSL_QUIC_FRAME_CRYPTO f;  | 
293  |  | 
  | 
294  | 0  |             if (!ossl_quic_wire_decode_frame_crypto(pkt, 1, &f))  | 
295  | 0  |                 goto unknown;  | 
296  |  |  | 
297  | 0  |             QLOG_STR("frame_type", "crypto"); | 
298  | 0  |             QLOG_U64("offset", f.offset); | 
299  | 0  |             QLOG_U64("payload_length", f.len); | 
300  | 0  |             *need_skip += (size_t)f.len;  | 
301  | 0  |         }  | 
302  | 0  |         break;  | 
303  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM:  | 
304  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:  | 
305  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:  | 
306  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:  | 
307  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:  | 
308  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:  | 
309  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:  | 
310  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:  | 
311  | 0  |         { | 
312  | 0  |             OSSL_QUIC_FRAME_STREAM f;  | 
313  |  | 
  | 
314  | 0  |             if (!ossl_quic_wire_decode_frame_stream(pkt, 1, &f))  | 
315  | 0  |                 goto unknown;  | 
316  |  |  | 
317  | 0  |             QLOG_STR("frame_type", "stream"); | 
318  | 0  |             QLOG_U64("stream_id", f.stream_id); | 
319  | 0  |             QLOG_U64("offset", f.offset); | 
320  | 0  |             QLOG_U64("payload_length", f.len); | 
321  | 0  |             QLOG_BOOL("explicit_length", f.has_explicit_len); | 
322  | 0  |             if (f.is_fin)  | 
323  | 0  |                 QLOG_BOOL("fin", 1); | 
324  | 0  |             *need_skip = f.has_explicit_len  | 
325  | 0  |                 ? *need_skip + (size_t)f.len : SIZE_MAX;  | 
326  | 0  |         }  | 
327  | 0  |         break;  | 
328  | 0  |     case OSSL_QUIC_FRAME_TYPE_MAX_DATA:  | 
329  | 0  |         { | 
330  | 0  |             uint64_t x;  | 
331  |  | 
  | 
332  | 0  |             if (!ossl_quic_wire_decode_frame_max_data(pkt, &x))  | 
333  | 0  |                 goto unknown;  | 
334  |  |  | 
335  | 0  |             QLOG_STR("frame_type", "max_data"); | 
336  | 0  |             QLOG_U64("maximum", x); | 
337  | 0  |         }  | 
338  | 0  |         break;  | 
339  | 0  |     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:  | 
340  | 0  |     case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:  | 
341  | 0  |         { | 
342  | 0  |             uint64_t x;  | 
343  |  | 
  | 
344  | 0  |             if (!ossl_quic_wire_decode_frame_max_streams(pkt, &x))  | 
345  | 0  |                 goto unknown;  | 
346  |  |  | 
347  | 0  |             QLOG_STR("frame_type", "max_streams"); | 
348  | 0  |             QLOG_STR("stream_type", | 
349  | 0  |                      frame_type == OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI  | 
350  | 0  |                         ? "bidirectional" : "unidirectional");  | 
351  | 0  |             QLOG_U64("maximum", x); | 
352  | 0  |         }  | 
353  | 0  |         break;  | 
354  | 0  |     case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:  | 
355  | 0  |         { | 
356  | 0  |             uint64_t stream_id, max_data;  | 
357  |  | 
  | 
358  | 0  |             if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,  | 
359  | 0  |                                                              &max_data))  | 
360  | 0  |                 goto unknown;  | 
361  |  |  | 
362  | 0  |             QLOG_STR("frame_type", "max_stream_data"); | 
363  | 0  |             QLOG_U64("stream_id", stream_id); | 
364  | 0  |             QLOG_U64("maximum", max_data); | 
365  | 0  |         }  | 
366  | 0  |         break;  | 
367  | 0  |     case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:  | 
368  | 0  |         { | 
369  | 0  |             uint64_t challenge;  | 
370  |  | 
  | 
371  | 0  |             if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge))  | 
372  | 0  |                 goto unknown;  | 
373  |  |  | 
374  | 0  |             QLOG_STR("frame_type", "path_challenge"); | 
375  | 0  |         }  | 
376  | 0  |         break;  | 
377  | 0  |     case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:  | 
378  | 0  |         { | 
379  | 0  |             uint64_t challenge;  | 
380  |  | 
  | 
381  | 0  |             if (!ossl_quic_wire_decode_frame_path_response(pkt, &challenge))  | 
382  | 0  |                 goto unknown;  | 
383  |  |  | 
384  | 0  |             QLOG_STR("frame_type", "path_response"); | 
385  | 0  |         }  | 
386  | 0  |         break;  | 
387  | 0  |     case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:  | 
388  | 0  |     case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:  | 
389  | 0  |         { | 
390  | 0  |             OSSL_QUIC_FRAME_CONN_CLOSE f;  | 
391  |  | 
  | 
392  | 0  |             if (!ossl_quic_wire_decode_frame_conn_close(pkt, &f))  | 
393  | 0  |                 goto unknown;  | 
394  |  |  | 
395  | 0  |             QLOG_STR("frame_type", "connection_close"); | 
396  | 0  |             QLOG_STR("error_space", f.is_app ? "application" : "transport"); | 
397  | 0  |             QLOG_U64("error_code_value", f.error_code); | 
398  | 0  |             if (f.is_app)  | 
399  | 0  |                 QLOG_U64("error_code", f.error_code); | 
400  | 0  |             if (!f.is_app && f.frame_type != 0)  | 
401  | 0  |                 QLOG_U64("trigger_frame_type", f.frame_type); | 
402  | 0  |             QLOG_STR_LEN("reason", f.reason, f.reason_len); | 
403  | 0  |         }  | 
404  | 0  |         break;  | 
405  | 0  |     case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:  | 
406  | 0  |         { | 
407  | 0  |             if (!ossl_quic_wire_decode_frame_handshake_done(pkt))  | 
408  | 0  |                 goto unknown;  | 
409  |  |  | 
410  | 0  |             QLOG_STR("frame_type", "handshake_done"); | 
411  | 0  |         }  | 
412  | 0  |         break;  | 
413  | 0  |     case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:  | 
414  | 0  |         { | 
415  | 0  |             OSSL_QUIC_FRAME_NEW_CONN_ID f;  | 
416  |  | 
  | 
417  | 0  |             if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &f))  | 
418  | 0  |                 goto unknown;  | 
419  |  |  | 
420  | 0  |             QLOG_STR("frame_type", "new_connection_id"); | 
421  | 0  |             QLOG_U64("sequence_number", f.seq_num); | 
422  | 0  |             QLOG_U64("retire_prior_to", f.retire_prior_to); | 
423  | 0  |             QLOG_CID("connection_id", &f.conn_id); | 
424  | 0  |             QLOG_BIN("stateless_reset_token", | 
425  | 0  |                      f.stateless_reset.token,  | 
426  | 0  |                      sizeof(f.stateless_reset.token));  | 
427  | 0  |         }  | 
428  | 0  |         break;  | 
429  | 0  |     case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:  | 
430  | 0  |         { | 
431  | 0  |             uint64_t seq_num;  | 
432  |  | 
  | 
433  | 0  |             if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num))  | 
434  | 0  |                 goto unknown;  | 
435  |  |  | 
436  | 0  |             QLOG_STR("frame_type", "retire_connection_id"); | 
437  | 0  |             QLOG_U64("sequence_number", seq_num); | 
438  | 0  |         }  | 
439  | 0  |         break;  | 
440  | 0  |     case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:  | 
441  | 0  |         { | 
442  | 0  |             uint64_t x;  | 
443  |  | 
  | 
444  | 0  |             if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &x))  | 
445  | 0  |                 goto unknown;  | 
446  |  |  | 
447  | 0  |             QLOG_STR("frame_type", "data_blocked"); | 
448  | 0  |             QLOG_U64("limit", x); | 
449  | 0  |         }  | 
450  | 0  |         break;  | 
451  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:  | 
452  | 0  |         { | 
453  | 0  |             uint64_t stream_id, x;  | 
454  |  | 
  | 
455  | 0  |             if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt,  | 
456  | 0  |                                                                  &stream_id,  | 
457  | 0  |                                                                  &x))  | 
458  | 0  |                 goto unknown;  | 
459  |  |  | 
460  | 0  |             QLOG_STR("frame_type", "stream_data_blocked"); | 
461  | 0  |             QLOG_U64("stream_id", stream_id); | 
462  | 0  |             QLOG_U64("limit", x); | 
463  | 0  |         }  | 
464  | 0  |         break;  | 
465  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:  | 
466  | 0  |     case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:  | 
467  | 0  |         { | 
468  | 0  |             uint64_t x;  | 
469  |  | 
  | 
470  | 0  |             if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &x))  | 
471  | 0  |                 goto unknown;  | 
472  |  |  | 
473  | 0  |             QLOG_STR("frame_type", "streams_blocked"); | 
474  | 0  |             QLOG_STR("stream_type", | 
475  | 0  |                      frame_type == OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI  | 
476  | 0  |                         ? "bidirectional" : "unidirectional");  | 
477  | 0  |             QLOG_U64("limit", x); | 
478  | 0  |         }  | 
479  | 0  |         break;  | 
480  | 0  |     case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:  | 
481  | 0  |         { | 
482  | 0  |             const unsigned char *token;  | 
483  | 0  |             size_t token_len;  | 
484  |  | 
  | 
485  | 0  |             if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len))  | 
486  | 0  |                 goto unknown;  | 
487  |  |  | 
488  | 0  |             QLOG_STR("frame_type", "new_token"); | 
489  | 0  |             QLOG_BEGIN("token"); | 
490  | 0  |                 QLOG_BEGIN("raw"); | 
491  | 0  |                     QLOG_BIN("data", token, token_len); | 
492  | 0  |                 QLOG_END();  | 
493  | 0  |             QLOG_END();  | 
494  | 0  |         }  | 
495  | 0  |         break;  | 
496  | 0  |     default:  | 
497  | 0  | unknown:  | 
498  | 0  |         QLOG_STR("frame_type", "unknown"); | 
499  | 0  |         QLOG_U64("frame_type_value", frame_type); | 
500  |  |  | 
501  |  |         /*  | 
502  |  |          * Can't continue scanning for frames in this case as the frame length  | 
503  |  |          * is unknown. We log the entire body of the rest of the packet payload  | 
504  |  |          * as the raw data of the frame.  | 
505  |  |          */  | 
506  | 0  |         QLOG_BEGIN("raw"); | 
507  | 0  |             QLOG_BIN("data", PACKET_data(&orig_pkt), | 
508  | 0  |                      PACKET_remaining(&orig_pkt));  | 
509  | 0  |         QLOG_END();  | 
510  | 0  |         ignore_res(PACKET_forward(pkt, PACKET_remaining(pkt)));  | 
511  | 0  |         break;  | 
512  | 0  |     }  | 
513  |  |  | 
514  | 0  |     return 1;  | 
515  | 0  | }  | 
516  |  |  | 
517  |  | static void log_frame(QLOG *qlog_instance, PACKET *pkt,  | 
518  |  |                       size_t *need_skip)  | 
519  | 0  | { | 
520  | 0  |     size_t rem_before, rem_after;  | 
521  |  | 
  | 
522  | 0  |     rem_before = PACKET_remaining(pkt);  | 
523  |  | 
  | 
524  | 0  |     if (!log_frame_actual(qlog_instance, pkt, need_skip))  | 
525  | 0  |         return;  | 
526  |  |  | 
527  | 0  |     rem_after = PACKET_remaining(pkt);  | 
528  | 0  |     QLOG_U64("length", rem_before - rem_after); | 
529  | 0  | }  | 
530  |  |  | 
531  |  | static int log_frames(QLOG *qlog_instance,  | 
532  |  |                       const OSSL_QTX_IOVEC *iovec,  | 
533  |  |                       size_t num_iovec)  | 
534  | 0  | { | 
535  | 0  |     size_t i;  | 
536  | 0  |     PACKET pkt;  | 
537  | 0  |     size_t need_skip = 0;  | 
538  |  | 
  | 
539  | 0  |     for (i = 0; i < num_iovec; ++i) { | 
540  | 0  |         if (!PACKET_buf_init(&pkt, iovec[i].buf, iovec[i].buf_len))  | 
541  | 0  |             return 0;  | 
542  |  |  | 
543  | 0  |         while (PACKET_remaining(&pkt) > 0) { | 
544  | 0  |             if (need_skip > 0) { | 
545  | 0  |                 size_t adv = need_skip;  | 
546  |  | 
  | 
547  | 0  |                 if (adv > PACKET_remaining(&pkt))  | 
548  | 0  |                     adv = PACKET_remaining(&pkt);  | 
549  |  | 
  | 
550  | 0  |                 if (!PACKET_forward(&pkt, adv))  | 
551  | 0  |                     return 0;  | 
552  |  |  | 
553  | 0  |                 need_skip -= adv;  | 
554  | 0  |                 continue;  | 
555  | 0  |             }  | 
556  |  |  | 
557  | 0  |             QLOG_BEGIN(NULL)  | 
558  | 0  |             { | 
559  | 0  |                 log_frame(qlog_instance, &pkt, &need_skip);  | 
560  | 0  |             }  | 
561  | 0  |             QLOG_END()  | 
562  | 0  |         }  | 
563  | 0  |     }  | 
564  |  |  | 
565  | 0  |     return 1;  | 
566  | 0  | }  | 
567  |  |  | 
568  |  | static void log_packet(QLOG *qlog_instance,  | 
569  |  |                        const QUIC_PKT_HDR *hdr,  | 
570  |  |                        QUIC_PN pn,  | 
571  |  |                        const OSSL_QTX_IOVEC *iovec,  | 
572  |  |                        size_t num_iovec,  | 
573  |  |                        uint64_t datagram_id)  | 
574  | 0  | { | 
575  | 0  |     const char *type_s;  | 
576  |  | 
  | 
577  | 0  |     QLOG_BEGIN("header") | 
578  | 0  |         type_s = quic_pkt_type_to_qlog(hdr->type);  | 
579  | 0  |         if (type_s == NULL)  | 
580  | 0  |             type_s = "unknown";  | 
581  |  | 
  | 
582  | 0  |         QLOG_STR("packet_type", type_s); | 
583  | 0  |         if (ossl_quic_pkt_type_has_pn(hdr->type))  | 
584  | 0  |             QLOG_U64("packet_number", pn); | 
585  |  | 
  | 
586  | 0  |         QLOG_CID("dcid", &hdr->dst_conn_id); | 
587  | 0  |         if (ossl_quic_pkt_type_has_scid(hdr->type))  | 
588  | 0  |             QLOG_CID("scid", &hdr->src_conn_id); | 
589  |  | 
  | 
590  | 0  |         if (hdr->token_len > 0) { | 
591  | 0  |             QLOG_BEGIN("token") | 
592  | 0  |                 QLOG_BEGIN("raw") | 
593  | 0  |                     QLOG_BIN("data", hdr->token, hdr->token_len); | 
594  | 0  |                 QLOG_END()  | 
595  | 0  |             QLOG_END()  | 
596  | 0  |         }  | 
597  |  |         /* TODO(QLOG FUTURE): flags, length */  | 
598  | 0  |     QLOG_END()  | 
599  | 0  |     QLOG_U64("datagram_id", datagram_id); | 
600  |  | 
  | 
601  | 0  |     if (ossl_quic_pkt_type_is_encrypted(hdr->type)) { | 
602  | 0  |         QLOG_BEGIN_ARRAY("frames") | 
603  | 0  |             log_frames(qlog_instance, iovec, num_iovec);  | 
604  | 0  |         QLOG_END_ARRAY()  | 
605  | 0  |     }  | 
606  | 0  | }  | 
607  |  |  | 
608  |  | #endif  | 
609  |  |  | 
610  |  | void ossl_qlog_event_transport_packet_sent(QLOG *qlog,  | 
611  |  |                                            const QUIC_PKT_HDR *hdr,  | 
612  |  |                                            QUIC_PN pn,  | 
613  |  |                                            const OSSL_QTX_IOVEC *iovec,  | 
614  |  |                                            size_t num_iovec,  | 
615  |  |                                            uint64_t datagram_id)  | 
616  | 0  | { | 
617  | 0  | #ifndef OPENSSL_NO_QLOG  | 
618  | 0  |     QLOG_EVENT_BEGIN(qlog, transport, packet_sent)  | 
619  | 0  |         log_packet(qlog, hdr, pn, iovec, num_iovec, datagram_id);  | 
620  | 0  |     QLOG_EVENT_END()  | 
621  | 0  | #endif  | 
622  | 0  | }  | 
623  |  |  | 
624  |  | void ossl_qlog_event_transport_packet_received(QLOG *qlog,  | 
625  |  |                                                const QUIC_PKT_HDR *hdr,  | 
626  |  |                                                QUIC_PN pn,  | 
627  |  |                                                const OSSL_QTX_IOVEC *iovec,  | 
628  |  |                                                size_t num_iovec,  | 
629  |  |                                                uint64_t datagram_id)  | 
630  | 0  | { | 
631  | 0  | #ifndef OPENSSL_NO_QLOG  | 
632  | 0  |     QLOG_EVENT_BEGIN(qlog, transport, packet_received)  | 
633  | 0  |         log_packet(qlog, hdr, pn, iovec, num_iovec, datagram_id);  | 
634  | 0  |     QLOG_EVENT_END()  | 
635  | 0  | #endif  | 
636  | 0  | }  |