1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /****************************************************************************
25 
26   QUICNetVConnection.h
27 
28   This file implements an I/O Processor for network I/O.
29 
30 
31  ****************************************************************************/
32 #pragma once
33 
34 #include "tscore/ink_platform.h"
35 #include "P_Net.h"
36 #include "P_EventSystem.h"
37 #include "P_UnixNetVConnection.h"
38 #include "P_UnixNet.h"
39 #include "P_UDPNet.h"
40 #include "P_ALPNSupport.h"
41 #include "TLSSessionResumptionSupport.h"
42 #include "tscore/ink_apidefs.h"
43 #include "tscore/List.h"
44 
45 #include "quic/QUICConfig.h"
46 #include "quic/QUICConnection.h"
47 #include "quic/QUICConnectionTable.h"
48 #include "quic/QUICResetTokenTable.h"
49 #include "quic/QUICVersionNegotiator.h"
50 #include "quic/QUICPacket.h"
51 #include "quic/QUICPacketFactory.h"
52 #include "quic/QUICFrame.h"
53 #include "quic/QUICFrameDispatcher.h"
54 #include "quic/QUICApplication.h"
55 #include "quic/QUICStream.h"
56 #include "quic/QUICHandshakeProtocol.h"
57 #include "quic/QUICAckFrameCreator.h"
58 #include "quic/QUICPinger.h"
59 #include "quic/QUICPadder.h"
60 #include "quic/QUICLossDetector.h"
61 #include "quic/QUICStreamManager.h"
62 #include "quic/QUICAltConnectionManager.h"
63 #include "quic/QUICPathValidator.h"
64 #include "quic/QUICPathManager.h"
65 #include "quic/QUICApplicationMap.h"
66 #include "quic/QUICPacketReceiveQueue.h"
67 #include "quic/QUICPacketHeaderProtector.h"
68 #include "quic/QUICAddrVerifyState.h"
69 #include "quic/QUICPacketProtectionKeyInfo.h"
70 #include "quic/QUICContext.h"
71 #include "quic/QUICTokenCreator.h"
72 #include "quic/qlog/QLogListener.h"
73 
74 // Size of connection ids for debug log : e.g. aaaaaaaa-bbbbbbbb\0
75 static constexpr size_t MAX_CIDS_SIZE = 8 + 1 + 8 + 1;
76 
77 //////////////////////////////////////////////////////////////////
78 //
79 //  class NetVConnection
80 //
81 //  A VConnection for a network socket.
82 //
83 //////////////////////////////////////////////////////////////////
84 
85 class QUICPacketHandler;
86 class QUICLossDetector;
87 class QUICHandshake;
88 
89 class SSLNextProtocolSet;
90 
91 /**
92  * @class QUICNetVConnection
93  * @brief A NetVConnection for a QUIC network socket
94  * @detail
95  *
96  * state_pre_handshake()
97  *  | READ:
98  *  |   Do nothing
99  *  | WRITE:
100  *  |   _state_common_send_packet()
101  *  v
102  * state_handshake()
103  *  | READ:
104  *  |   _state_handshake_process_packet()
105  *  |   _state_handshake_process_initial_packet()
106  *  |   _state_handshake_process_retry_packet()
107  *  |   _state_handshake_process_handshake_packet()
108  *  |   _state_handshake_process_zero_rtt_protected_packet()
109  *  | WRITE:
110  *  |   _state_common_send_packet()
111  *  |   or
112  *  |   _state_handshake_send_retry_packet()
113  *  v
114  * state_connection_established()
115  *  | READ:
116  *  |   _state_connection_established_receive_packet()
117  *  |   _state_connection_established_process_protected_packet()
118  *  | WRITE:
119  *  |   _state_common_send_packet()
120  *  v
121  * state_connection_closing() (If closing actively)
122  *  | READ:
123  *  |   _state_closing_receive_packet()
124  *  | WRITE:
125  *  |   _state_closing_send_packet()
126  *  v
127  * state_connection_draining() (If closing passively)
128  *  | READ:
129  *  |   _state_draining_receive_packet()
130  *  | WRITE:
131  *  |   Do nothing
132  *  v
133  * state_connection_close()
134  *    READ:
135  *      Do nothing
136  *    WRITE:
137  *      Do nothing
138  **/
139 class QUICNetVConnection : public UnixNetVConnection,
140                            public QUICConnection,
141                            public RefCountObj,
142                            public ALPNSupport,
143                            public TLSSessionResumptionSupport
144 {
145   using super = UnixNetVConnection; ///< Parent type.
146 
147 public:
148   QUICNetVConnection();
149   ~QUICNetVConnection();
150   void init(QUICVersion version, QUICConnectionId peer_cid, QUICConnectionId original_cid, UDPConnection *, QUICPacketHandler *,
151             QUICResetTokenTable *rtable);
152   void init(QUICVersion version, QUICConnectionId peer_cid, QUICConnectionId original_cid, QUICConnectionId first_cid,
153             QUICConnectionId retry_cid, UDPConnection *, QUICPacketHandler *, QUICResetTokenTable *rtable,
154             QUICConnectionTable *ctable);
155 
156   // accept new conn_id
157   int acceptEvent(int event, Event *e);
158 
159   // NetVConnection
160   void set_local_addr() override;
161 
162   // UnixNetVConnection
163   void reenable(VIO *vio) override;
164   VIO *do_io_read(Continuation *c, int64_t nbytes, MIOBuffer *buf) override;
165   VIO *do_io_write(Continuation *c, int64_t nbytes, IOBufferReader *buf, bool owner = false) override;
166   int connectUp(EThread *t, int fd) override;
167 
168   // QUICNetVConnection
169   int startEvent(int event, Event *e);
170   int state_pre_handshake(int event, Event *data);
171   int state_handshake(int event, Event *data);
172   int state_connection_established(int event, Event *data);
173   int state_connection_closing(int event, Event *data);
174   int state_connection_draining(int event, Event *data);
175   int state_connection_closed(int event, Event *data);
176   void start();
177   void remove_connection_ids();
178   void free(EThread *t) override;
179   void free() override;
180   void destroy(EThread *t);
181 
182   UDPConnection *get_udp_con();
183   virtual void net_read_io(NetHandler *nh, EThread *lthread) override;
184   virtual int64_t load_buffer_and_write(int64_t towrite, MIOBufferAccessor &buf, int64_t &total_written, int &needs) override;
185 
186   int populate_protocol(std::string_view *results, int n) const override;
187   const char *protocol_contains(std::string_view tag) const override;
188 
189   int select_next_protocol(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in,
190                            unsigned inlen) const override;
191 
192   // QUICConnection
193   QUICStreamManager *stream_manager() override;
194   void close_quic_connection(QUICConnectionErrorUPtr error) override;
195   void reset_quic_connection() override;
196   void handle_received_packet(UDPPacket *packet) override;
197   void ping() override;
198 
199   // QUICConnection (QUICConnectionInfoProvider)
200   QUICConnectionId peer_connection_id() const override;
201   QUICConnectionId original_connection_id() const override;
202   QUICConnectionId first_connection_id() const override;
203   QUICConnectionId retry_source_connection_id() const override;
204   QUICConnectionId initial_source_connection_id() const override;
205   QUICConnectionId connection_id() const override;
206   std::string_view cids() const override;
207   const QUICFiveTuple five_tuple() const override;
208   uint32_t pmtu() const override;
209   NetVConnectionContext_t direction() const override;
210   QUICVersion negotiated_version() const override;
211   std::string_view negotiated_application_name() const override;
212   bool is_closed() const override;
213   bool is_at_anti_amplification_limit() const override;
214   bool is_address_validation_completed() const override;
215   bool is_handshake_completed() const override;
216   bool has_keys_for(QUICPacketNumberSpace space) const override;
217 
218   // QUICConnection (QUICFrameHandler)
219   std::vector<QUICFrameType> interests() override;
220   QUICConnectionErrorUPtr handle_frame(QUICEncryptionLevel level, const QUICFrame &frame) override;
221 
222   int in_closed_queue = 0;
223 
224   bool shouldDestroy();
225 
226   LINK(QUICNetVConnection, closed_link);
227   SLINK(QUICNetVConnection, closed_alink);
228 
229 protected:
230   const IpEndpoint &_getLocalEndpoint() override;
231 
232 private:
233   std::random_device _rnd;
234 
235   QUICConfig::scoped_config _quic_config;
236 
237   QUICConnectionId _peer_quic_connection_id;      // dst cid in local
238   QUICConnectionId _peer_old_quic_connection_id;  // dst previous cid in local
239   QUICConnectionId _original_quic_connection_id;  // dst cid of initial packet from client
240   QUICConnectionId _first_quic_connection_id;     // dst cid of initial packet from client that doesn't have retry token
241   QUICConnectionId _retry_source_connection_id;   // src cid used for sending Retry packet
242   QUICConnectionId _initial_source_connection_id; // src cid used for Initial packet
243   QUICConnectionId _quic_connection_id;           // src cid in local
244   QUICFiveTuple _five_tuple;
245   bool _connection_migration_initiated = false;
246 
247   char _cids_data[MAX_CIDS_SIZE] = {0};
248   std::string_view _cids;
249 
250   QUICVersion _initial_version;
251   UDPConnection *_udp_con = nullptr;
252   QUICPacketProtectionKeyInfo _pp_key_info;
253   QUICPacketHandler *_packet_handler = nullptr;
254   QUICPacketFactory _packet_factory;
255   QUICFrameFactory _frame_factory;
256   QUICAckFrameManager _ack_frame_manager;
257   QUICPacketHeaderProtector _ph_protector;
258   QUICRTTMeasure _rtt_measure;
259   QUICApplicationMap *_application_map = nullptr;
260 
261   uint32_t _pmtu = 1280;
262 
263   // TODO: use custom allocator and make them std::unique_ptr or std::shared_ptr
264   // or make them just member variables.
265   QUICPinger *_pinger                               = nullptr;
266   QUICPadder *_padder                               = nullptr;
267   QUICHandshake *_handshake_handler                 = nullptr;
268   QUICHandshakeProtocol *_hs_protocol               = nullptr;
269   QUICLossDetector *_loss_detector                  = nullptr;
270   QUICFrameDispatcher *_frame_dispatcher            = nullptr;
271   QUICStreamManager *_stream_manager                = nullptr;
272   QUICCongestionController *_congestion_controller  = nullptr;
273   QUICRemoteFlowController *_remote_flow_controller = nullptr;
274   QUICLocalFlowController *_local_flow_controller   = nullptr;
275   QUICResetTokenTable *_rtable                      = nullptr;
276   QUICConnectionTable *_ctable                      = nullptr;
277   QUICAltConnectionManager *_alt_con_manager        = nullptr;
278   QUICPathValidator *_path_validator                = nullptr;
279   QUICPathManager *_path_manager                    = nullptr;
280   QUICTokenCreator *_token_creator                  = nullptr;
281 
282   QUICFrameGeneratorManager _frame_generators;
283 
284   QUICPacketReceiveQueue _packet_recv_queue = {this->_packet_factory, this->_ph_protector};
285 
286   QUICConnectionErrorUPtr _connection_error  = nullptr;
287   uint32_t _state_closing_recv_packet_count  = 0;
288   uint32_t _state_closing_recv_packet_window = 1;
289   uint64_t _flow_control_buffer_size         = 1024;
290 
291   void _init_submodules();
292 
293   void _schedule_packet_write_ready(bool delay = false);
294   void _unschedule_packet_write_ready();
295   void _close_packet_write_ready(Event *data);
296   Event *_packet_write_ready = nullptr;
297 
298   void _schedule_closing_timeout(ink_hrtime interval);
299   void _unschedule_closing_timeout();
300   void _close_closing_timeout(Event *data);
301   Event *_closing_timeout = nullptr;
302 
303   void _schedule_closed_event();
304   void _unschedule_closed_event();
305   void _close_closed_event(Event *data);
306   Event *_closed_event = nullptr;
307 
308   void _schedule_ack_manager_periodic(ink_hrtime interval);
309   void _unschedule_ack_manager_periodic();
310   Event *_ack_manager_periodic = nullptr;
311 
312   QUICEncryptionLevel _minimum_encryption_level = QUICEncryptionLevel::INITIAL;
313 
314   QUICPacketNumber _largest_acked_packet_number(QUICEncryptionLevel level) const;
315   uint32_t _maximum_quic_packet_size() const;
316   uint32_t _minimum_quic_packet_size();
317   uint64_t _maximum_stream_frame_data_size();
318 
319   Ptr<IOBufferBlock> _store_frame(Ptr<IOBufferBlock> parent_block, size_t &size_added, uint64_t &max_frame_size, QUICFrame &frame,
320                                   std::vector<QUICSentPacketInfo::FrameInfo> &frames);
321   QUICPacketUPtr _packetize_frames(uint8_t *packet_buf, QUICEncryptionLevel level, uint64_t max_packet_size,
322                                    std::vector<QUICSentPacketInfo::FrameInfo> &frames);
323   void _packetize_closing_frame();
324   QUICPacketUPtr _build_packet(uint8_t *packet_buf, QUICEncryptionLevel level, const Ptr<IOBufferBlock> &parent_block,
325                                bool retransmittable, bool probing, bool crypto);
326 
327   QUICConnectionErrorUPtr _recv_and_ack(const QUICPacketR &packet, bool *has_non_probing_frame = nullptr);
328 
329   QUICConnectionErrorUPtr _state_handshake_process_packet(const QUICPacket &packet);
330   QUICConnectionErrorUPtr _state_handshake_process_version_negotiation_packet(const QUICVersionNegotiationPacketR &packet);
331   QUICConnectionErrorUPtr _state_handshake_process_initial_packet(const QUICInitialPacketR &packet);
332   QUICConnectionErrorUPtr _state_handshake_process_retry_packet(const QUICRetryPacketR &packet);
333   QUICConnectionErrorUPtr _state_handshake_process_handshake_packet(const QUICHandshakePacketR &packet);
334   QUICConnectionErrorUPtr _state_handshake_process_zero_rtt_protected_packet(const QUICZeroRttPacketR &packet);
335   QUICConnectionErrorUPtr _state_connection_established_receive_packet();
336   QUICConnectionErrorUPtr _state_connection_established_process_protected_packet(const QUICShortHeaderPacketR &packet);
337   QUICConnectionErrorUPtr _state_connection_established_migrate_connection(const QUICPacketR &packet);
338   QUICConnectionErrorUPtr _state_connection_established_initiate_connection_migration();
339   QUICConnectionErrorUPtr _state_closing_receive_packet();
340   QUICConnectionErrorUPtr _state_draining_receive_packet();
341   QUICConnectionErrorUPtr _state_common_send_packet();
342   QUICConnectionErrorUPtr _state_handshake_send_retry_packet();
343   QUICConnectionErrorUPtr _state_closing_send_packet();
344 
345   Ptr<ProxyMutex> _packet_transmitter_mutex;
346 
347   void _init_flow_control_params(const std::shared_ptr<const QUICTransportParameters> &local_tp,
348                                  const std::shared_ptr<const QUICTransportParameters> &remote_tp);
349   void _handle_error(QUICConnectionErrorUPtr error);
350   QUICPacketUPtr _dequeue_recv_packet(uint8_t *packet_buf, QUICPacketCreationResult &result);
351   void _validate_new_path(const QUICPath &path);
352 
353   bool _handshake_completed = false;
354   int _complete_handshake_if_possible();
355 
356   void _switch_to_handshake_state();
357   void _switch_to_established_state();
358   void _switch_to_closing_state(QUICConnectionErrorUPtr error);
359   void _switch_to_draining_state(QUICConnectionErrorUPtr error);
360   void _switch_to_close_state();
361 
362   bool _application_started = false;
363   void _start_application();
364 
365   void _handle_periodic_ack_event();
366   void _handle_idle_timeout();
367   void _handle_active_timeout();
368 
369   QUICConnectionErrorUPtr _handle_frame(const QUICNewConnectionIdFrame &frame);
370 
371   void _update_cids();
372   void _update_peer_cid(const QUICConnectionId &new_cid);
373   void _update_local_cid(const QUICConnectionId &new_cid);
374   void _rerandomize_original_cid();
375 
376   QUICHandshakeProtocol *_setup_handshake_protocol(const shared_SSL_CTX &ctx);
377 
378   QUICPacketUPtr _the_final_packet = QUICPacketFactory::create_null_packet();
379   uint8_t _final_packet_buf[QUICPacket::MAX_INSTANCE_SIZE];
380   QUICStatelessResetToken _reset_token;
381 
382   ats_unique_buf _av_token = {nullptr};
383   size_t _av_token_len     = 0;
384 
385   uint64_t _stream_frames_sent = 0;
386   uint32_t _seq_num            = 0;
387 
388   // TODO: Source addresses verification through an address validation token
389   QUICAddrVerifyState _verified_state;
390 
391   std::unique_ptr<QUICContext> _context;
392 
393   std::shared_ptr<QLog::QLogListener> _qlog;
394 };
395 
396 typedef int (QUICNetVConnection::*QUICNetVConnHandler)(int, void *);
397 
398 extern ClassAllocator<QUICNetVConnection> quicNetVCAllocator;
399