xref: /trafficserver/iocore/net/quic/QUICPadder.cc (revision a80d7794)
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 #include "QUICIntUtil.h"
25 #include "QUICPadder.h"
26 
27 static constexpr uint32_t MINIMUM_INITIAL_PACKET_SIZE = 1200;
28 static constexpr uint32_t MIN_PKT_PAYLOAD_LEN         = 3; ///< Minimum payload length for sampling for header protection
29 
30 void
request(QUICEncryptionLevel level)31 QUICPadder::request(QUICEncryptionLevel level)
32 {
33   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
34   ++this->_need_to_fire[static_cast<int>(level)];
35 }
36 
37 void
cancel(QUICEncryptionLevel level)38 QUICPadder::cancel(QUICEncryptionLevel level)
39 {
40   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
41   this->_need_to_fire[static_cast<int>(level)] = 0;
42 }
43 
44 uint64_t
count(QUICEncryptionLevel level)45 QUICPadder::count(QUICEncryptionLevel level)
46 {
47   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
48   return this->_need_to_fire[static_cast<int>(level)];
49 }
50 
51 bool
_will_generate_frame(QUICEncryptionLevel level,size_t current_packet_size,bool ack_eliciting)52 QUICPadder::_will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting)
53 {
54   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
55   // no extre padding packet
56   if (current_packet_size == 0 && this->_need_to_fire[static_cast<int>(level)] == 0) {
57     return false;
58   }
59 
60   // every packets need to be padded
61   return true;
62 }
63 
64 QUICFrame *
_generate_frame(uint8_t * buf,QUICEncryptionLevel level,uint64_t connection_credit,uint16_t maximum_frame_size,size_t current_packet_size)65 QUICPadder::_generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t connection_credit, uint16_t maximum_frame_size,
66                             size_t current_packet_size)
67 {
68   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
69   QUICFrame *frame = nullptr;
70 
71   uint64_t min_size = 0;
72   if (level == QUICEncryptionLevel::INITIAL && this->_context == NET_VCONNECTION_OUT) {
73     min_size = this->_minimum_quic_packet_size();
74     if (this->_av_token_len && min_size > (QUICVariableInt::size(this->_av_token_len) + this->_av_token_len)) {
75       min_size -= (QUICVariableInt::size(this->_av_token_len) + this->_av_token_len);
76     }
77   } else {
78     min_size = MIN_PKT_PAYLOAD_LEN;
79   }
80 
81   if (min_size > current_packet_size) { // ignore if we don't need to pad.
82     frame = QUICFrameFactory::create_padding_frame(
83       buf, std::min(min_size - current_packet_size, static_cast<uint64_t>(maximum_frame_size)));
84   }
85 
86   this->_need_to_fire[static_cast<int>(level)] = 0;
87   return frame;
88 }
89 
90 uint32_t
_minimum_quic_packet_size()91 QUICPadder::_minimum_quic_packet_size()
92 {
93   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
94   if (this->_context == NET_VCONNECTION_OUT) {
95     // FIXME Only the first packet need to be 1200 bytes at least
96     return MINIMUM_INITIAL_PACKET_SIZE;
97   } else {
98     // FIXME This size should be configurable and should have some randomness
99     // This is just for providing protection against packet analysis for protected packets
100     return 32 + (this->_rnd() & 0x3f); // 32 to 96
101   }
102 }
103 
104 void
set_av_token_len(uint32_t len)105 QUICPadder::set_av_token_len(uint32_t len)
106 {
107   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
108   this->_av_token_len = len;
109 }
110