| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/crypto/strike_register.h" | 5 #include "net/quic/crypto/strike_register.h" |
| 6 | 6 |
| 7 #include <limits> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 | 10 |
| 11 using std::make_pair; |
| 12 using std::max; |
| 9 using std::min; | 13 using std::min; |
| 10 using std::pair; | 14 using std::pair; |
| 11 using std::set; | 15 using std::set; |
| 12 using std::vector; | 16 using std::vector; |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 15 | 19 |
| 16 namespace { | 20 namespace { |
| 17 | 21 |
| 18 uint32 GetInitialHorizon(uint32 current_time_internal, | 22 uint32 GetInitialHorizon(uint32 current_time_internal, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // <24 bits> left child | 74 // <24 bits> left child |
| 71 // <8 bits> crit-byte | 75 // <8 bits> crit-byte |
| 72 // <24 bits> right child | 76 // <24 bits> right child |
| 73 // <8 bits> other-bits | 77 // <8 bits> other-bits |
| 74 uint32 data_[2]; | 78 uint32 data_[2]; |
| 75 }; | 79 }; |
| 76 | 80 |
| 77 // kCreationTimeFromInternalEpoch contains the number of seconds between the | 81 // kCreationTimeFromInternalEpoch contains the number of seconds between the |
| 78 // start of the internal epoch and the creation time. This allows us | 82 // start of the internal epoch and the creation time. This allows us |
| 79 // to consider times that are before the creation time. | 83 // to consider times that are before the creation time. |
| 80 static const uint32 kCreationTimeFromInternalEpoch = 63115200.0; // 2 years. | 84 static const uint32 kCreationTimeFromInternalEpoch = 63115200; // 2 years. |
| 81 | 85 |
| 82 void StrikeRegister::ValidateStrikeRegisterConfig(unsigned max_entries) { | 86 void StrikeRegister::ValidateStrikeRegisterConfig(unsigned max_entries) { |
| 83 // We only have 23 bits of index available. | 87 // We only have 23 bits of index available. |
| 84 CHECK_LT(max_entries, 1u << 23); | 88 CHECK_LT(max_entries, 1u << 23); |
| 85 CHECK_GT(max_entries, 1u); // There must be at least two entries. | 89 CHECK_GT(max_entries, 1u); // There must be at least two entries. |
| 86 CHECK_EQ(sizeof(InternalNode), 8u); // in case of compiler changes. | 90 CHECK_EQ(sizeof(InternalNode), 8u); // in case of compiler changes. |
| 87 } | 91 } |
| 88 | 92 |
| 89 StrikeRegister::StrikeRegister(unsigned max_entries, | 93 StrikeRegister::StrikeRegister(unsigned max_entries, |
| 90 uint32 current_time, | 94 uint32 current_time, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 120 external_node_free_head_ = 0; | 124 external_node_free_head_ = 0; |
| 121 for (unsigned i = 0; i < max_entries_ - 1; i++) | 125 for (unsigned i = 0; i < max_entries_ - 1; i++) |
| 122 external_node_next_ptr(i) = i + 1; | 126 external_node_next_ptr(i) = i + 1; |
| 123 external_node_next_ptr(max_entries_ - 1) = kNil; | 127 external_node_next_ptr(max_entries_ - 1) = kNil; |
| 124 | 128 |
| 125 // This is the root of the tree. | 129 // This is the root of the tree. |
| 126 internal_node_head_ = kNil; | 130 internal_node_head_ = kNil; |
| 127 } | 131 } |
| 128 | 132 |
| 129 bool StrikeRegister::Insert(const uint8 nonce[32], | 133 bool StrikeRegister::Insert(const uint8 nonce[32], |
| 130 const uint32 current_time_external) { | 134 uint32 current_time_external) { |
| 131 // Make space for the insertion if the strike register is full. | 135 // Make space for the insertion if the strike register is full. |
| 132 while (external_node_free_head_ == kNil || | 136 while (external_node_free_head_ == kNil || |
| 133 internal_node_free_head_ == kNil) { | 137 internal_node_free_head_ == kNil) { |
| 134 DropOldestNode(); | 138 DropOldestNode(); |
| 135 } | 139 } |
| 136 | 140 |
| 137 const uint32 current_time = ExternalTimeToInternal(current_time_external); | 141 const uint32 current_time = ExternalTimeToInternal(current_time_external); |
| 138 | 142 |
| 139 // Check to see if the orbit is correct. | 143 // Check to see if the orbit is correct. |
| 140 if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) { | 144 if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) { |
| 141 return false; | 145 return false; |
| 142 } | 146 } |
| 147 |
| 143 const uint32 nonce_time = ExternalTimeToInternal(TimeFromBytes(nonce)); | 148 const uint32 nonce_time = ExternalTimeToInternal(TimeFromBytes(nonce)); |
| 144 // We have dropped one or more nonces with a time value of |horizon_ - 1|, so | 149 |
| 145 // we have to reject anything with a timestamp less than or equal to that. | 150 // Check that the timestamp is in the valid range. |
| 146 if (nonce_time < horizon_) { | 151 pair<uint32, uint32> valid_range = |
| 152 StrikeRegister::GetValidRange(current_time); |
| 153 if (nonce_time < valid_range.first || nonce_time > valid_range.second) { |
| 147 return false; | 154 return false; |
| 148 } | 155 } |
| 149 | 156 |
| 150 // Check that the timestamp is in the current window. | |
| 151 if ((current_time > window_secs_ && | |
| 152 nonce_time < (current_time - window_secs_)) || | |
| 153 nonce_time > (current_time + window_secs_)) { | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 // We strip the orbit out of the nonce. | 157 // We strip the orbit out of the nonce. |
| 158 uint8 value[24]; | 158 uint8 value[24]; |
| 159 memcpy(value, nonce, sizeof(nonce_time)); | 159 memcpy(value, nonce, sizeof(nonce_time)); |
| 160 memcpy(value + sizeof(nonce_time), | 160 memcpy(value + sizeof(nonce_time), |
| 161 nonce + sizeof(nonce_time) + sizeof(orbit_), | 161 nonce + sizeof(nonce_time) + sizeof(orbit_), |
| 162 sizeof(value) - sizeof(nonce_time)); | 162 sizeof(value) - sizeof(nonce_time)); |
| 163 | 163 |
| 164 // Find the best match to |value| in the crit-bit tree. The best match is | 164 // Find the best match to |value| in the crit-bit tree. The best match is |
| 165 // simply the value which /could/ match |value|, if any does, so we still | 165 // simply the value which /could/ match |value|, if any does, so we still |
| 166 // need a memcmp to check. | 166 // need a memcmp to check. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 *where_index = (*where_index & 0xff) | (internal_node_index << 8); | 263 *where_index = (*where_index & 0xff) | (internal_node_index << 8); |
| 264 | 264 |
| 265 DCHECK_LE(horizon_, nonce_time); | 265 DCHECK_LE(horizon_, nonce_time); |
| 266 return true; | 266 return true; |
| 267 } | 267 } |
| 268 | 268 |
| 269 const uint8* StrikeRegister::orbit() const { | 269 const uint8* StrikeRegister::orbit() const { |
| 270 return orbit_; | 270 return orbit_; |
| 271 } | 271 } |
| 272 | 272 |
| 273 uint32 StrikeRegister::EffectiveWindowSecs( | 273 uint32 StrikeRegister::GetCurrentValidWindowSecs( |
| 274 const uint32 current_time_external) const { | 274 uint32 current_time_external) const { |
| 275 const uint32 future_horizon = | 275 uint32 current_time = ExternalTimeToInternal(current_time_external); |
| 276 ExternalTimeToInternal(current_time_external) + window_secs_; | 276 pair<uint32, uint32> valid_range = StrikeRegister::GetValidRange( |
| 277 | 277 current_time); |
| 278 if (horizon_ >= future_horizon) { | 278 if (valid_range.second >= valid_range.first) { |
| 279 return valid_range.second - current_time + 1; |
| 280 } else { |
| 279 return 0; | 281 return 0; |
| 280 } | 282 } |
| 281 return min(future_horizon - horizon_, 2 * window_secs_); | |
| 282 } | 283 } |
| 283 | 284 |
| 284 void StrikeRegister::Validate() { | 285 void StrikeRegister::Validate() { |
| 285 set<uint32> free_internal_nodes; | 286 set<uint32> free_internal_nodes; |
| 286 for (uint32 i = internal_node_free_head_; i != kNil; | 287 for (uint32 i = internal_node_free_head_; i != kNil; |
| 287 i = internal_nodes_[i].next()) { | 288 i = internal_nodes_[i].next()) { |
| 288 CHECK_LT(i, max_entries_); | 289 CHECK_LT(i, max_entries_); |
| 289 CHECK_EQ(free_internal_nodes.count(i), 0u); | 290 CHECK_EQ(free_internal_nodes.count(i), 0u); |
| 290 free_internal_nodes.insert(i); | 291 free_internal_nodes.insert(i); |
| 291 } | 292 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 311 } | 312 } |
| 312 | 313 |
| 313 // static | 314 // static |
| 314 uint32 StrikeRegister::TimeFromBytes(const uint8 d[4]) { | 315 uint32 StrikeRegister::TimeFromBytes(const uint8 d[4]) { |
| 315 return static_cast<uint32>(d[0]) << 24 | | 316 return static_cast<uint32>(d[0]) << 24 | |
| 316 static_cast<uint32>(d[1]) << 16 | | 317 static_cast<uint32>(d[1]) << 16 | |
| 317 static_cast<uint32>(d[2]) << 8 | | 318 static_cast<uint32>(d[2]) << 8 | |
| 318 static_cast<uint32>(d[3]); | 319 static_cast<uint32>(d[3]); |
| 319 } | 320 } |
| 320 | 321 |
| 322 pair<uint32, uint32> StrikeRegister::GetValidRange( |
| 323 uint32 current_time_internal) const { |
| 324 if (current_time_internal < horizon_) { |
| 325 // Empty valid range. |
| 326 return make_pair(std::numeric_limits<uint32>::max(), 0); |
| 327 } |
| 328 |
| 329 uint32 lower_bound; |
| 330 if (current_time_internal >= window_secs_) { |
| 331 lower_bound = max(horizon_, current_time_internal - window_secs_); |
| 332 } else { |
| 333 lower_bound = horizon_; |
| 334 } |
| 335 |
| 336 // Also limit the upper range based on horizon_. This makes the |
| 337 // strike register reject inserts that are far in the future and |
| 338 // would consume strike register resources for a long time. This |
| 339 // allows the strike server to degrade optimally in cases where the |
| 340 // insert rate exceeds |max_entries_ / (2 * window_secs_)| entries |
| 341 // per second. |
| 342 uint32 upper_bound = |
| 343 current_time_internal + min(current_time_internal - horizon_, |
| 344 window_secs_); |
| 345 |
| 346 return make_pair(lower_bound, upper_bound); |
| 347 } |
| 348 |
| 321 uint32 StrikeRegister::ExternalTimeToInternal(uint32 external_time) const { | 349 uint32 StrikeRegister::ExternalTimeToInternal(uint32 external_time) const { |
| 322 return external_time - internal_epoch_; | 350 return external_time - internal_epoch_; |
| 323 } | 351 } |
| 324 | 352 |
| 325 uint32 StrikeRegister::BestMatch(const uint8 v[24]) const { | 353 uint32 StrikeRegister::BestMatch(const uint8 v[24]) const { |
| 326 if (internal_node_head_ == kNil) { | 354 if (internal_node_head_ == kNil) { |
| 327 return kNil; | 355 return kNil; |
| 328 } | 356 } |
| 329 | 357 |
| 330 uint32 next = internal_node_head_ >> 8; | 358 uint32 next = internal_node_head_ >> 8; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 CHECK_EQ(free_internal_nodes.count(inter), 0u); | 511 CHECK_EQ(free_internal_nodes.count(inter), 0u); |
| 484 CHECK_EQ(used_internal_nodes->count(inter), 0u); | 512 CHECK_EQ(used_internal_nodes->count(inter), 0u); |
| 485 used_internal_nodes->insert(inter); | 513 used_internal_nodes->insert(inter); |
| 486 ValidateTree(inter, bit, bits, free_internal_nodes, free_external_nodes, | 514 ValidateTree(inter, bit, bits, free_internal_nodes, free_external_nodes, |
| 487 used_internal_nodes, used_external_nodes); | 515 used_internal_nodes, used_external_nodes); |
| 488 } | 516 } |
| 489 } | 517 } |
| 490 } | 518 } |
| 491 | 519 |
| 492 } // namespace net | 520 } // namespace net |
| OLD | NEW |