| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::{ |
| 8 |
cell::RefCell, |
| 9 |
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, |
| 10 |
rc::Rc, |
| 11 |
time::{Duration, Instant}, |
| 12 |
}; |
| 13 |
|
| 14 |
use neqo_common::{qdebug, Datagram, Decoder}; |
| 15 |
use test_fixture::{ |
| 16 |
assertions::{assert_v4_path, assert_v6_path}, |
| 17 |
fixture_init, new_neqo_qlog, now, DEFAULT_ADDR, DEFAULT_ADDR_V4, |
| 18 |
}; |
| 19 |
|
| 20 |
use super::{ |
| 21 |
super::{Connection, Output, State, StreamType}, |
| 22 |
connect_fail, connect_force_idle, connect_rtt_idle, default_client, default_server, |
| 23 |
maybe_authenticate, new_client, new_server, send_something, zero_len_cid_client, |
| 24 |
CountingConnectionIdGenerator, |
| 25 |
}; |
| 26 |
use crate::{ |
| 27 |
cid::LOCAL_ACTIVE_CID_LIMIT, |
| 28 |
connection::tests::{ |
| 29 |
assert_path_challenge_min_len, connect, send_something_paced, send_with_extra, |
| 30 |
}, |
| 31 |
frame::FrameType, |
| 32 |
packet::PacketBuilder, |
| 33 |
path::MAX_PATH_PROBES, |
| 34 |
pmtud::Pmtud, |
| 35 |
stats::FrameStats, |
| 36 |
tparams::{PreferredAddress, TransportParameter, TransportParameterId}, |
| 37 |
CloseReason, ConnectionId, ConnectionIdDecoder as _, ConnectionIdGenerator, ConnectionIdRef, |
| 38 |
ConnectionParameters, EmptyConnectionIdGenerator, Error, MIN_INITIAL_PACKET_SIZE, |
| 39 |
}; |
| 40 |
|
| 41 |
/// This should be a valid-seeming transport parameter. |
| 42 |
/// And it should have different values to `addr` and `addr_v4`. |
| 43 |
const SAMPLE_PREFERRED_ADDRESS: &[u8] = &[ |
| 44 |
0xc0, 0x00, 0x02, 0x02, 0x01, 0xbb, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0xbb, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, |
| 46 |
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 47 |
]; |
| 48 |
|
| 49 |
// These tests generally use two paths: |
| 50 |
// The connection is established on a path with the same IPv6 address on both ends. |
| 51 |
// Migrations move to a path with the same IPv4 address on both ends. |
| 52 |
// This simplifies validation as the same assertions can be used for client and server. |
| 53 |
// The risk is that there is a place where source/destination local/remote is inverted. |
| 54 |
|
| 55 |
const fn loopback() -> SocketAddr { |
| 56 |
SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 443) |
| 57 |
} |
| 58 |
|
| 59 |
fn change_path(d: &Datagram, a: SocketAddr) -> Datagram { |
| 60 |
Datagram::new(a, a, d.tos(), &d[..]) |
| 61 |
} |
| 62 |
|
| 63 |
const fn new_port(a: SocketAddr) -> SocketAddr { |
| 64 |
let (port, _) = a.port().overflowing_add(410); |
| 65 |
SocketAddr::new(a.ip(), port) |
| 66 |
} |
| 67 |
|
| 68 |
fn assert_path_challenge( |
| 69 |
c: &Connection, |
| 70 |
d: &Datagram, |
| 71 |
before: &FrameStats, |
| 72 |
dst: SocketAddr, |
| 73 |
padded: bool, |
| 74 |
) { |
| 75 |
let after = c.stats().frame_tx; |
| 76 |
assert_eq!(after.path_challenge, before.path_challenge + 1); |
| 77 |
assert_eq!(d.source(), DEFAULT_ADDR); |
| 78 |
assert_eq!(d.destination(), dst); |
| 79 |
if padded { |
| 80 |
assert!(d.len() >= MIN_INITIAL_PACKET_SIZE); |
| 81 |
} else { |
| 82 |
assert!(d.len() < MIN_INITIAL_PACKET_SIZE); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
fn assert_path_response(c: &Connection, d: &Datagram, before: &FrameStats) { |
| 87 |
let after = c.stats().frame_tx; |
| 88 |
assert_eq!(after.path_response, before.path_response + 1); |
| 89 |
assert_eq!(d.source(), DEFAULT_ADDR); |
| 90 |
assert_eq!(d.destination(), DEFAULT_ADDR); |
| 91 |
} |
| 92 |
|
| 93 |
fn local_address(c: &Connection) -> SocketAddr { |
| 94 |
c.paths.primary().unwrap().borrow().local_address() |
| 95 |
} |
| 96 |
|
| 97 |
fn rebind( |
| 98 |
client: &mut Connection, |
| 99 |
server: &mut Connection, |
| 100 |
cur_path: fn(&Datagram) -> Datagram, |
| 101 |
new_path: fn(&Datagram) -> Datagram, |
| 102 |
mut now: Instant, |
| 103 |
) -> Instant { |
| 104 |
qdebug!("Rebinding"); |
| 105 |
let c1 = send_something(client, now); |
| 106 |
let c1_new = new_path(&c1); |
| 107 |
qdebug!("Rebinding to {}", c1_new.source()); |
| 108 |
|
| 109 |
// Server will reply to modified datagram with a PATH_CHALLENGE. |
| 110 |
// Due to the amplification limit, this will not be padded to MIN_INITIAL_PACKET_SIZE. |
| 111 |
let before = server.stats().frame_tx; |
| 112 |
let s1 = server.process(Some(c1_new.clone()), now).dgram().unwrap(); |
| 113 |
assert_path_challenge(server, &s1, &before, c1_new.source(), false); |
| 114 |
|
| 115 |
// Restore the original source address, so to the client it looks like the path has not changed. |
| 116 |
let s1_reb = Datagram::new(s1.source(), local_address(client), s1.tos(), &s1[..]); |
| 117 |
|
| 118 |
// The client should respond to the PATH_CHALLENGE, without changing paths. |
| 119 |
let before = client.stats().frame_tx; |
| 120 |
let c2 = client.process(Some(s1_reb), now).dgram().unwrap(); |
| 121 |
assert_path_response(client, &c2, &before); |
| 122 |
|
| 123 |
// The server should now see the response on the new path. |
| 124 |
// It will send another PATH_CHALLENGE padded to MIN_INITIAL_PACKET_SIZE. |
| 125 |
let c2_new = new_path(&c2); |
| 126 |
let before = server.stats().frame_tx; |
| 127 |
let s2 = server.process(Some(c2_new.clone()), now).dgram().unwrap(); |
| 128 |
assert_path_challenge(server, &s2, &before, c2_new.source(), true); |
| 129 |
|
| 130 |
// Restore the original source address, so to the client it looks like the path has not changed. |
| 131 |
let s2_reb = Datagram::new(s2.source(), local_address(client), s2.tos(), &s2[..]); |
| 132 |
|
| 133 |
// The client should respond to the PATH_CHALLENGE, without changing paths. |
| 134 |
let before = client.stats().frame_tx; |
| 135 |
let c3 = client.process(Some(s2_reb.clone()), now).dgram().unwrap(); |
| 136 |
assert_path_response(client, &s2_reb, &before); |
| 137 |
|
| 138 |
// The server should now see the second response on the new path. |
| 139 |
// It will then try to probe the old path. |
| 140 |
let c3_new = new_path(&c3); |
| 141 |
let c3_cur = cur_path(&c3); |
| 142 |
let before = server.stats().frame_tx; |
| 143 |
let s3 = server.process(Some(c3_new.clone()), now).dgram().unwrap(); |
| 144 |
assert_path_challenge(server, &s3, &before, c3_cur.source(), true); |
| 145 |
|
| 146 |
// Do not deliver this probe to the client. |
| 147 |
|
| 148 |
// Server will now ACK on the new path. |
| 149 |
let before = server.stats().frame_tx; |
| 150 |
let s4 = server.process_output(now).dgram().unwrap(); |
| 151 |
let after = server.stats().frame_tx; |
| 152 |
assert_eq!(after.ack, before.ack + 1); |
| 153 |
assert_eq!(s4.source(), c3_new.destination()); |
| 154 |
assert_eq!(s4.destination(), c3_new.source()); |
| 155 |
|
| 156 |
// Restore the original source address, so to the client it looks like the path has not changed. |
| 157 |
let s4_reb = Datagram::new(s4.source(), local_address(client), s4.tos(), &s4[..]); |
| 158 |
|
| 159 |
// The client should process the ACK and go idle. |
| 160 |
let delay = client.process(Some(s4_reb), now).callback(); |
| 161 |
assert_eq!(delay, ConnectionParameters::default().get_idle_timeout()); |
| 162 |
|
| 163 |
let client_uses_zero_len_cid = client |
| 164 |
.paths |
| 165 |
.primary() |
| 166 |
.unwrap() |
| 167 |
.borrow() |
| 168 |
.local_cid() |
| 169 |
.unwrap() |
| 170 |
.is_empty(); |
| 171 |
let mut total_delay = Duration::new(0, 0); |
| 172 |
loop { |
| 173 |
let before = server.stats().frame_tx; |
| 174 |
match server.process_output(now) { |
| 175 |
Output::Callback(t) => { |
| 176 |
total_delay += t; |
| 177 |
if total_delay == ConnectionParameters::default().get_idle_timeout() { |
| 178 |
// Server should only hit the idle timeout here when the client uses a zero-len |
| 179 |
// CID. |
| 180 |
assert!(client_uses_zero_len_cid); |
| 181 |
break; |
| 182 |
} |
| 183 |
now += t; |
| 184 |
} |
| 185 |
Output::Datagram(sx) => { |
| 186 |
total_delay = Duration::new(0, 0); |
| 187 |
if sx.destination() == c3_cur.source() { |
| 188 |
// Old path gets path challenges. |
| 189 |
assert_path_challenge(server, &sx, &before, c3_cur.source(), true); |
| 190 |
// Don't deliver them. |
| 191 |
} else { |
| 192 |
let after = server.stats().frame_tx; |
| 193 |
// If the client uses a zero-len CID, the server will only PING. |
| 194 |
// Otherwise, it will PING or send a RETIRE_CONNECTION_ID. |
| 195 |
if client_uses_zero_len_cid { |
| 196 |
assert_eq!(after.ping, before.ping + 1); |
| 197 |
} else { |
| 198 |
assert!( |
| 199 |
after.retire_connection_id == before.retire_connection_id + 1 |
| 200 |
|| after.ping == before.ping + 1 |
| 201 |
); |
| 202 |
} |
| 203 |
// Restore the original source address, so to the client it looks like |
| 204 |
// the path has not changed. |
| 205 |
let sx_r = Datagram::new(sx.source(), local_address(client), sx.tos(), &sx[..]); |
| 206 |
let before = client.stats().frame_tx; |
| 207 |
let cx = client.process(Some(sx_r), now).dgram().unwrap(); |
| 208 |
let after = client.stats().frame_tx; |
| 209 |
assert_eq!(after.ack, before.ack + 1); |
| 210 |
// Also deliver the ACK. |
| 211 |
let cx_n = new_path(&cx); |
| 212 |
server.process_input(cx_n, now); |
| 213 |
if !client_uses_zero_len_cid |
| 214 |
&& after.new_connection_id == before.new_connection_id + 1 |
| 215 |
{ |
| 216 |
// Declare victory once the client has sent a new connection ID. |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
Output::None => panic!(), |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if !client_uses_zero_len_cid { |
| 226 |
// Eat up any delays before returning. |
| 227 |
now += client.process_output(now).callback(); |
| 228 |
now += server.process_output(now).callback(); |
| 229 |
} |
| 230 |
|
| 231 |
qdebug!("Rebinding done"); |
| 232 |
now |
| 233 |
} |
| 234 |
|
| 235 |
fn inc_port(port: u16, i: usize) -> u16 { |
| 236 |
port.overflowing_add(i.overflowing_mul(11).0.try_into().unwrap()) |
| 237 |
.0 |
| 238 |
} |
| 239 |
|
| 240 |
fn inc_addr(ip: IpAddr, i: usize) -> IpAddr { |
| 241 |
let inc: u8 = i.overflowing_mul(11).0.try_into().unwrap(); |
| 242 |
match ip { |
| 243 |
IpAddr::V4(ip) => IpAddr::V4(Ipv4Addr::from( |
| 244 |
ip.octets().map(|b| b.overflowing_add(inc).0), |
| 245 |
)), |
| 246 |
IpAddr::V6(ip) => IpAddr::V6(Ipv6Addr::from( |
| 247 |
ip.octets().map(|b| b.overflowing_add(inc).0), |
| 248 |
)), |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
fn change_source_port(d: &Datagram, i: usize) -> Datagram { |
| 253 |
Datagram::new( |
| 254 |
SocketAddr::new(d.source().ip(), inc_port(d.source().port(), i)), |
| 255 |
d.destination(), |
| 256 |
d.tos(), |
| 257 |
&d[..], |
| 258 |
) |
| 259 |
} |
| 260 |
|
| 261 |
fn change_source_address_and_port(d: &Datagram, i: usize) -> Datagram { |
| 262 |
Datagram::new( |
| 263 |
SocketAddr::new(inc_addr(d.source().ip(), i), inc_port(d.source().port(), i)), |
| 264 |
d.destination(), |
| 265 |
d.tos(), |
| 266 |
&d[..], |
| 267 |
) |
| 268 |
} |
| 269 |
|
| 270 |
fn rebind_port_with_client(client: &mut Connection) { |
| 271 |
let mut server = default_server(); |
| 272 |
connect_force_idle(client, &mut server); |
| 273 |
let mut now = now(); |
| 274 |
|
| 275 |
now = rebind( |
| 276 |
client, |
| 277 |
&mut server, |
| 278 |
|d| change_source_port(d, 0), |
| 279 |
|d| change_source_port(d, 1), |
| 280 |
now, |
| 281 |
); |
| 282 |
_ = rebind( |
| 283 |
client, |
| 284 |
&mut server, |
| 285 |
|d| change_source_port(d, 1), |
| 286 |
|d| change_source_port(d, 2), |
| 287 |
now, |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
fn rebind_address_and_port_with_client(client: &mut Connection) { |
| 292 |
let mut server = default_server(); |
| 293 |
connect_force_idle(client, &mut server); |
| 294 |
let mut now = now(); |
| 295 |
|
| 296 |
now = rebind( |
| 297 |
client, |
| 298 |
&mut server, |
| 299 |
|d| change_source_address_and_port(d, 0), |
| 300 |
|d| change_source_address_and_port(d, 1), |
| 301 |
now, |
| 302 |
); |
| 303 |
_ = rebind( |
| 304 |
client, |
| 305 |
&mut server, |
| 306 |
|d| change_source_address_and_port(d, 1), |
| 307 |
|d| change_source_address_and_port(d, 2), |
| 308 |
now, |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn rebind_port() { |
| 314 |
let mut client = default_client(); |
| 315 |
rebind_port_with_client(&mut client); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn rebind_port_zero_len_cid() { |
| 320 |
let mut client = zero_len_cid_client(DEFAULT_ADDR, DEFAULT_ADDR); |
| 321 |
rebind_port_with_client(&mut client); |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn rebind_address_and_port() { |
| 326 |
let mut client = default_client(); |
| 327 |
rebind_address_and_port_with_client(&mut client); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn rebind_address_and_port_zero_len_cid() { |
| 332 |
let mut client = zero_len_cid_client(DEFAULT_ADDR, DEFAULT_ADDR); |
| 333 |
rebind_address_and_port_with_client(&mut client); |
| 334 |
} |
| 335 |
|
| 336 |
/// This simulates an attack where a valid packet is forwarded on |
| 337 |
/// a different path. This shows how both paths are probed and the |
| 338 |
/// server eventually returns to the original path. |
| 339 |
#[test] |
| 340 |
fn path_forwarding_attack() { |
| 341 |
let mut client = default_client(); |
| 342 |
let mut server = default_server(); |
| 343 |
connect_force_idle(&mut client, &mut server); |
| 344 |
let now = now(); |
| 345 |
|
| 346 |
let dgram = send_something(&mut client, now); |
| 347 |
let dgram = change_path(&dgram, DEFAULT_ADDR_V4); |
| 348 |
server.process_input(dgram, now); |
| 349 |
|
| 350 |
// The server now probes the new (primary) path. |
| 351 |
let new_probe = server.process_output(now).dgram().unwrap(); |
| 352 |
assert_eq!(server.stats().frame_tx.path_challenge, 1); |
| 353 |
assert_path_challenge_min_len(&server, &new_probe, now); |
| 354 |
assert_v4_path(&new_probe, false); // Can't be padded. |
| 355 |
|
| 356 |
// The server also probes the old path. |
| 357 |
let old_probe = server.process_output(now).dgram().unwrap(); |
| 358 |
assert_path_challenge_min_len(&server, &old_probe, now); |
| 359 |
assert_eq!(server.stats().frame_tx.path_challenge, 2); |
| 360 |
assert_v6_path(&old_probe, true); |
| 361 |
|
| 362 |
// New data from the server is sent on the new path, but that is |
| 363 |
// now constrained by the amplification limit. |
| 364 |
let stream_id = server.stream_create(StreamType::UniDi).unwrap(); |
| 365 |
server.stream_close_send(stream_id).unwrap(); |
| 366 |
assert!(server.process_output(now).dgram().is_none()); |
| 367 |
|
| 368 |
// The client should respond to the challenge on the new path. |
| 369 |
// The server couldn't pad, so the client is also amplification limited. |
| 370 |
let new_resp = client.process(Some(new_probe), now).dgram().unwrap(); |
| 371 |
assert_eq!(client.stats().frame_rx.path_challenge, 1); |
| 372 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 373 |
assert_eq!(client.stats().frame_tx.path_response, 1); |
| 374 |
assert_v4_path(&new_resp, false); |
| 375 |
|
| 376 |
// The client also responds to probes on the old path. |
| 377 |
let old_resp = client.process(Some(old_probe), now).dgram().unwrap(); |
| 378 |
assert_eq!(client.stats().frame_rx.path_challenge, 2); |
| 379 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 380 |
assert_eq!(client.stats().frame_tx.path_response, 2); |
| 381 |
assert_v6_path(&old_resp, true); |
| 382 |
|
| 383 |
// But the client still sends data on the old path. |
| 384 |
let client_data1 = send_something(&mut client, now); |
| 385 |
assert_v6_path(&client_data1, false); // Just data. |
| 386 |
|
| 387 |
// Receiving the PATH_RESPONSE from the client opens the amplification |
| 388 |
// limit enough for the server to respond. |
| 389 |
// This is padded because it includes PATH_CHALLENGE. |
| 390 |
let server_data1 = server.process(Some(new_resp), now).dgram().unwrap(); |
| 391 |
assert_v4_path(&server_data1, true); |
| 392 |
assert_eq!(server.stats().frame_tx.path_challenge, 3); |
| 393 |
assert_path_challenge_min_len(&server, &server_data1, now); |
| 394 |
|
| 395 |
// The client responds to this probe on the new path. |
| 396 |
client.process_input(server_data1, now); |
| 397 |
let stream_before = client.stats().frame_tx.stream; |
| 398 |
let padded_resp = send_something(&mut client, now); |
| 399 |
assert_eq!(stream_before, client.stats().frame_tx.stream); |
| 400 |
assert_v4_path(&padded_resp, true); // This is padded! |
| 401 |
|
| 402 |
// But new data from the client stays on the old path. |
| 403 |
let client_data2 = client.process_output(now).dgram().unwrap(); |
| 404 |
assert_v6_path(&client_data2, false); |
| 405 |
|
| 406 |
// The server keeps sending on the new path. |
| 407 |
let server_data2 = send_something(&mut server, now); |
| 408 |
assert_v4_path(&server_data2, false); |
| 409 |
|
| 410 |
// Until new data is received from the client on the old path. |
| 411 |
server.process_input(client_data2, now); |
| 412 |
// The server sends a probe on the new path. |
| 413 |
let server_data3 = send_something(&mut server, now); |
| 414 |
assert_v4_path(&server_data3, true); |
| 415 |
// But switches data transmission to the old path. |
| 416 |
let server_data4 = server.process_output(now).dgram().unwrap(); |
| 417 |
assert_v6_path(&server_data4, false); |
| 418 |
} |
| 419 |
|
| 420 |
#[test] |
| 421 |
fn migrate_immediate() { |
| 422 |
let mut client = default_client(); |
| 423 |
let mut server = default_server(); |
| 424 |
connect_force_idle(&mut client, &mut server); |
| 425 |
let now = now(); |
| 426 |
|
| 427 |
client |
| 428 |
.migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), true, now) |
| 429 |
.unwrap(); |
| 430 |
|
| 431 |
let client1 = send_something(&mut client, now); |
| 432 |
assert_v4_path(&client1, true); // Contains PATH_CHALLENGE. |
| 433 |
assert_path_challenge_min_len(&client, &client1, now); |
| 434 |
|
| 435 |
let client2 = send_something(&mut client, now); |
| 436 |
assert_v4_path(&client2, false); // Doesn't. |
| 437 |
|
| 438 |
let server_delayed = send_something(&mut server, now); |
| 439 |
|
| 440 |
// The server accepts the first packet and migrates (but probes). |
| 441 |
let server1 = server.process(Some(client1), now).dgram().unwrap(); |
| 442 |
assert_v4_path(&server1, true); |
| 443 |
let server2 = server.process_output(now).dgram().unwrap(); |
| 444 |
assert_v6_path(&server2, true); |
| 445 |
|
| 446 |
// The second packet has no real effect, it just elicits an ACK. |
| 447 |
let all_before = server.stats().frame_tx.all(); |
| 448 |
let ack_before = server.stats().frame_tx.ack; |
| 449 |
let server3 = server.process(Some(client2), now).dgram(); |
| 450 |
assert!(server3.is_some()); |
| 451 |
assert_eq!(server.stats().frame_tx.all(), all_before + 1); |
| 452 |
assert_eq!(server.stats().frame_tx.ack, ack_before + 1); |
| 453 |
|
| 454 |
// Receiving a packet sent by the server before migration doesn't change path. |
| 455 |
client.process_input(server_delayed, now); |
| 456 |
// The client has sent two unpaced packets and this new path has no RTT estimate |
| 457 |
// so this might be paced. |
| 458 |
let (client3, _t) = send_something_paced(&mut client, now, true); |
| 459 |
assert_v4_path(&client3, false); |
| 460 |
} |
| 461 |
|
| 462 |
/// RTT estimates for paths should be preserved across migrations. |
| 463 |
#[test] |
| 464 |
fn migrate_rtt() { |
| 465 |
const RTT: Duration = Duration::from_millis(20); |
| 466 |
let mut client = default_client(); |
| 467 |
let mut server = default_server(); |
| 468 |
let now = connect_rtt_idle(&mut client, &mut server, RTT); |
| 469 |
|
| 470 |
client |
| 471 |
.migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), true, now) |
| 472 |
.unwrap(); |
| 473 |
// The RTT might be increased for the new path, so allow a little flexibility. |
| 474 |
let rtt = client.paths.rtt(); |
| 475 |
assert!(rtt > RTT); |
| 476 |
assert!(rtt < RTT * 2); |
| 477 |
} |
| 478 |
|
| 479 |
#[test] |
| 480 |
fn migrate_immediate_fail() { |
| 481 |
let mut client = default_client(); |
| 482 |
let mut server = default_server(); |
| 483 |
connect_force_idle(&mut client, &mut server); |
| 484 |
let mut now = now(); |
| 485 |
|
| 486 |
client |
| 487 |
.migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), true, now) |
| 488 |
.unwrap(); |
| 489 |
|
| 490 |
let probe = client.process_output(now).dgram().unwrap(); |
| 491 |
assert_v4_path(&probe, true); // Contains PATH_CHALLENGE. |
| 492 |
assert_path_challenge_min_len(&client, &probe, now); |
| 493 |
|
| 494 |
// -1 because first PATH_CHALLENGE already sent above |
| 495 |
for _ in 0..MAX_PATH_PROBES * 2 - 1 { |
| 496 |
let cb = client.process_output(now).callback(); |
| 497 |
assert_ne!(cb, Duration::new(0, 0)); |
| 498 |
now += cb; |
| 499 |
|
| 500 |
let before = client.stats().frame_tx; |
| 501 |
let probe = client.process_output(now).dgram().unwrap(); |
| 502 |
assert_v4_path(&probe, true); // Contains PATH_CHALLENGE. |
| 503 |
assert_path_challenge_min_len(&client, &probe, now); |
| 504 |
let after = client.stats().frame_tx; |
| 505 |
assert_eq!(after.path_challenge, before.path_challenge + 1); |
| 506 |
assert_eq!(after.padding, before.padding + 1); |
| 507 |
assert_eq!(after.all(), before.all() + 2); |
| 508 |
|
| 509 |
// This might be a PTO, which will result in sending a probe. |
| 510 |
if let Some(probe) = client.process_output(now).dgram() { |
| 511 |
assert_v4_path(&probe, false); // Contains PING. |
| 512 |
let after = client.stats().frame_tx; |
| 513 |
assert_eq!(after.path_challenge, before.path_challenge + 1); |
| 514 |
assert_eq!(after.ping, before.ping + 1); |
| 515 |
assert_eq!(after.all(), before.all() + 3); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
let pto = client.process_output(now).callback(); |
| 520 |
assert_ne!(pto, Duration::new(0, 0)); |
| 521 |
now += pto; |
| 522 |
|
| 523 |
// The client should fall back to the original path and retire the connection ID. |
| 524 |
let fallback = client.process_output(now).dgram(); |
| 525 |
assert_v6_path(&fallback.unwrap(), false); |
| 526 |
assert_eq!(client.stats().frame_tx.retire_connection_id, 1); |
| 527 |
} |
| 528 |
|
| 529 |
/// Migrating to the same path shouldn't do anything special, |
| 530 |
/// except that the path is probed. |
| 531 |
#[test] |
| 532 |
fn migrate_same() { |
| 533 |
let mut client = default_client(); |
| 534 |
let mut server = default_server(); |
| 535 |
connect_force_idle(&mut client, &mut server); |
| 536 |
let now = now(); |
| 537 |
|
| 538 |
client |
| 539 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), true, now) |
| 540 |
.unwrap(); |
| 541 |
|
| 542 |
let probe = client.process_output(now).dgram().unwrap(); |
| 543 |
assert_v6_path(&probe, true); // Contains PATH_CHALLENGE. |
| 544 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 545 |
assert_path_challenge_min_len(&client, &probe, now); |
| 546 |
|
| 547 |
let resp = server.process(Some(probe), now).dgram().unwrap(); |
| 548 |
assert_v6_path(&resp, true); |
| 549 |
assert_eq!(server.stats().frame_tx.path_response, 1); |
| 550 |
assert_eq!(server.stats().frame_tx.path_challenge, 0); |
| 551 |
|
| 552 |
// Everything continues happily. |
| 553 |
client.process_input(resp, now); |
| 554 |
let contd = send_something(&mut client, now); |
| 555 |
assert_v6_path(&contd, false); |
| 556 |
} |
| 557 |
|
| 558 |
/// Migrating to the same path, if it fails, causes the connection to fail. |
| 559 |
#[test] |
| 560 |
fn migrate_same_fail() { |
| 561 |
let mut client = default_client(); |
| 562 |
let mut server = default_server(); |
| 563 |
connect_force_idle(&mut client, &mut server); |
| 564 |
let mut now = now(); |
| 565 |
|
| 566 |
client |
| 567 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), true, now) |
| 568 |
.unwrap(); |
| 569 |
|
| 570 |
let probe = client.process_output(now).dgram().unwrap(); |
| 571 |
assert_v6_path(&probe, true); // Contains PATH_CHALLENGE. |
| 572 |
assert_path_challenge_min_len(&client, &probe, now); |
| 573 |
|
| 574 |
// -1 because first PATH_CHALLENGE already sent above |
| 575 |
for _ in 0..MAX_PATH_PROBES - 1 { |
| 576 |
let cb = client.process_output(now).callback(); |
| 577 |
assert_ne!(cb, Duration::new(0, 0)); |
| 578 |
now += cb; |
| 579 |
|
| 580 |
let before = client.stats().frame_tx; |
| 581 |
let probe = client.process_output(now).dgram().unwrap(); |
| 582 |
assert_v6_path(&probe, true); // Contains PATH_CHALLENGE. |
| 583 |
assert_path_challenge_min_len(&client, &probe, now); |
| 584 |
let after = client.stats().frame_tx; |
| 585 |
assert_eq!(after.path_challenge, before.path_challenge + 1); |
| 586 |
assert_eq!(after.padding, before.padding + 1); |
| 587 |
assert_eq!(after.all(), before.all() + 2); |
| 588 |
|
| 589 |
// This might be a PTO, which will result in sending a probe. |
| 590 |
if let Some(probe) = client.process_output(now).dgram() { |
| 591 |
assert_v6_path(&probe, false); // Contains PING. |
| 592 |
let after = client.stats().frame_tx; |
| 593 |
assert_eq!(after.path_challenge, before.path_challenge + 1); |
| 594 |
assert_eq!(after.ping, before.ping + 1); |
| 595 |
assert_eq!(after.all(), before.all() + 3); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
let pto = client.process_output(now).callback(); |
| 600 |
assert_ne!(pto, Duration::new(0, 0)); |
| 601 |
now += pto; |
| 602 |
|
| 603 |
// The client should mark this path as failed and close immediately. |
| 604 |
let res = client.process_output(now); |
| 605 |
assert!(matches!(res, Output::None)); |
| 606 |
assert!(matches!( |
| 607 |
client.state(), |
| 608 |
State::Closed(CloseReason::Transport(Error::NoAvailablePath)) |
| 609 |
)); |
| 610 |
} |
| 611 |
|
| 612 |
/// This gets the connection ID from a datagram using the default |
| 613 |
/// connection ID generator/decoder. |
| 614 |
pub fn get_cid(d: &Datagram) -> ConnectionIdRef { |
| 615 |
let gen = CountingConnectionIdGenerator::default(); |
| 616 |
assert_eq!(d[0] & 0x80, 0); // Only support short packets for now. |
| 617 |
gen.decode_cid(&mut Decoder::from(&d[1..])).unwrap() |
| 618 |
} |
| 619 |
|
| 620 |
fn migration(mut client: Connection) { |
| 621 |
let mut server = default_server(); |
| 622 |
connect_force_idle(&mut client, &mut server); |
| 623 |
let now = now(); |
| 624 |
|
| 625 |
client |
| 626 |
.migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), false, now) |
| 627 |
.unwrap(); |
| 628 |
|
| 629 |
let probe = client.process_output(now).dgram().unwrap(); |
| 630 |
assert_v4_path(&probe, true); // Contains PATH_CHALLENGE. |
| 631 |
assert_path_challenge_min_len(&client, &probe, now); |
| 632 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 633 |
let probe_cid = ConnectionId::from(get_cid(&probe)); |
| 634 |
|
| 635 |
let resp = server.process(Some(probe), now).dgram().unwrap(); |
| 636 |
assert_v4_path(&resp, true); |
| 637 |
assert_path_challenge_min_len(&server, &resp, now); |
| 638 |
assert_eq!(server.stats().frame_tx.path_response, 1); |
| 639 |
assert_eq!(server.stats().frame_tx.path_challenge, 1); |
| 640 |
|
| 641 |
// Data continues to be exchanged on the new path. |
| 642 |
let client_data = send_something(&mut client, now); |
| 643 |
assert_ne!(get_cid(&client_data), probe_cid); |
| 644 |
assert_v6_path(&client_data, false); |
| 645 |
server.process_input(client_data, now); |
| 646 |
let server_data = send_something(&mut server, now); |
| 647 |
assert_v6_path(&server_data, false); |
| 648 |
|
| 649 |
// Once the client receives the probe response, it migrates to the new path. |
| 650 |
client.process_input(resp, now); |
| 651 |
assert_eq!(client.stats().frame_rx.path_challenge, 1); |
| 652 |
let migrate_client = send_something(&mut client, now); |
| 653 |
assert_v4_path(&migrate_client, true); // Responds to server probe. |
| 654 |
|
| 655 |
// The server now sees the migration and will switch over. |
| 656 |
// However, it will probe the old path again, even though it has just |
| 657 |
// received a response to its last probe, because it needs to verify |
| 658 |
// that the migration is genuine. |
| 659 |
server.process_input(migrate_client, now); |
| 660 |
let stream_before = server.stats().frame_tx.stream; |
| 661 |
let probe_old_server = send_something(&mut server, now); |
| 662 |
// This is just the double-check probe; no STREAM frames. |
| 663 |
assert_v6_path(&probe_old_server, true); |
| 664 |
assert_path_challenge_min_len(&server, &probe_old_server, now); |
| 665 |
assert_eq!(server.stats().frame_tx.path_challenge, 2); |
| 666 |
assert_eq!(server.stats().frame_tx.stream, stream_before); |
| 667 |
|
| 668 |
// The server then sends data on the new path. |
| 669 |
let migrate_server = server.process_output(now).dgram().unwrap(); |
| 670 |
assert_v4_path(&migrate_server, false); |
| 671 |
assert_eq!(server.stats().frame_tx.path_challenge, 2); |
| 672 |
assert_eq!(server.stats().frame_tx.stream, stream_before + 1); |
| 673 |
|
| 674 |
// The client receives these checks and responds to the probe, but uses the new path. |
| 675 |
client.process_input(migrate_server, now); |
| 676 |
client.process_input(probe_old_server, now); |
| 677 |
let old_probe_resp = send_something(&mut client, now); |
| 678 |
assert_v6_path(&old_probe_resp, true); |
| 679 |
let client_confirmation = client.process_output(now).dgram().unwrap(); |
| 680 |
assert_v4_path(&client_confirmation, false); |
| 681 |
|
| 682 |
// The server has now sent 2 packets, so it is blocked on the pacer. Wait. |
| 683 |
let server_pacing = server.process_output(now).callback(); |
| 684 |
assert_ne!(server_pacing, Duration::new(0, 0)); |
| 685 |
// ... then confirm that the server sends on the new path still. |
| 686 |
let server_confirmation = send_something(&mut server, now + server_pacing); |
| 687 |
assert_v4_path(&server_confirmation, false); |
| 688 |
} |
| 689 |
|
| 690 |
#[test] |
| 691 |
fn migration_graceful() { |
| 692 |
migration(default_client()); |
| 693 |
} |
| 694 |
|
| 695 |
/// A client should be able to migrate when it has a zero-length connection ID. |
| 696 |
#[test] |
| 697 |
fn migration_client_empty_cid() { |
| 698 |
fixture_init(); |
| 699 |
let client = zero_len_cid_client(DEFAULT_ADDR, DEFAULT_ADDR); |
| 700 |
migration(client); |
| 701 |
} |
| 702 |
|
| 703 |
/// Drive the handshake in the most expeditious fashion. |
| 704 |
/// Returns the packet containing `HANDSHAKE_DONE` from the server. |
| 705 |
fn fast_handshake(client: &mut Connection, server: &mut Connection) -> Option<Datagram> { |
| 706 |
let dgram = client.process_output(now()).dgram(); |
| 707 |
let dgram2 = client.process_output(now()).dgram(); |
| 708 |
server.process_input(dgram.unwrap(), now()); |
| 709 |
let dgram = server.process(dgram2, now()).dgram(); |
| 710 |
let dgram = client.process(dgram, now()).dgram(); |
| 711 |
let dgram = server.process(dgram, now()).dgram(); |
| 712 |
client.process_input(dgram.unwrap(), now()); |
| 713 |
assert!(maybe_authenticate(client)); |
| 714 |
let dgram = client.process_output(now()).dgram(); |
| 715 |
server.process(dgram, now()).dgram() |
| 716 |
} |
| 717 |
|
| 718 |
fn preferred_address(hs_client: SocketAddr, hs_server: SocketAddr, preferred: SocketAddr) { |
| 719 |
let mtu = Pmtud::default_plpmtu(hs_client.ip()); |
| 720 |
let assert_orig_path = |d: &Datagram, full_mtu: bool| { |
| 721 |
assert_eq!( |
| 722 |
d.destination(), |
| 723 |
if d.source() == hs_client { |
| 724 |
hs_server |
| 725 |
} else if d.source() == hs_server { |
| 726 |
hs_client |
| 727 |
} else { |
| 728 |
panic!(); |
| 729 |
} |
| 730 |
); |
| 731 |
if full_mtu { |
| 732 |
assert_eq!(d.len(), mtu); |
| 733 |
} |
| 734 |
}; |
| 735 |
let assert_toward_spa = |d: &Datagram, full_mtu: bool| { |
| 736 |
assert_eq!(d.destination(), preferred); |
| 737 |
assert_eq!(d.source(), hs_client); |
| 738 |
if full_mtu { |
| 739 |
assert_eq!(d.len(), mtu); |
| 740 |
} |
| 741 |
}; |
| 742 |
let assert_from_spa = |d: &Datagram, full_mtu: bool| { |
| 743 |
assert_eq!(d.destination(), hs_client); |
| 744 |
assert_eq!(d.source(), preferred); |
| 745 |
if full_mtu { |
| 746 |
assert_eq!(d.len(), mtu); |
| 747 |
} |
| 748 |
}; |
| 749 |
|
| 750 |
fixture_init(); |
| 751 |
let (log, _contents) = new_neqo_qlog(); |
| 752 |
let mut client = zero_len_cid_client(hs_client, hs_server); |
| 753 |
client.set_qlog(log); |
| 754 |
let spa = match preferred { |
| 755 |
SocketAddr::V6(v6) => PreferredAddress::new(None, Some(v6)), |
| 756 |
SocketAddr::V4(v4) => PreferredAddress::new(Some(v4), None), |
| 757 |
}; |
| 758 |
let mut server = new_server(ConnectionParameters::default().preferred_address(spa)); |
| 759 |
|
| 760 |
let dgram = fast_handshake(&mut client, &mut server); |
| 761 |
|
| 762 |
// The client is about to process HANDSHAKE_DONE. |
| 763 |
// It should start probing toward the server's preferred address. |
| 764 |
let probe = client.process(dgram, now()).dgram().unwrap(); |
| 765 |
assert_toward_spa(&probe, true); |
| 766 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 767 |
assert_path_challenge_min_len(&client, &probe, now()); |
| 768 |
assert_ne!(client.process_output(now()).callback(), Duration::new(0, 0)); |
| 769 |
|
| 770 |
// Data continues on the main path for the client. |
| 771 |
let data = send_something(&mut client, now()); |
| 772 |
assert_orig_path(&data, false); |
| 773 |
|
| 774 |
// The server responds to the probe. |
| 775 |
let resp = server.process(Some(probe), now()).dgram().unwrap(); |
| 776 |
assert_from_spa(&resp, true); |
| 777 |
assert_eq!(server.stats().frame_tx.path_challenge, 1); |
| 778 |
assert_path_challenge_min_len(&server, &resp, now()); |
| 779 |
assert_eq!(server.stats().frame_tx.path_response, 1); |
| 780 |
|
| 781 |
// Data continues on the main path for the server. |
| 782 |
server.process_input(data, now()); |
| 783 |
let data = send_something(&mut server, now()); |
| 784 |
assert_orig_path(&data, false); |
| 785 |
|
| 786 |
// Client gets the probe response back and it migrates. |
| 787 |
client.process_input(resp, now()); |
| 788 |
client.process_input(data, now()); |
| 789 |
let data = send_something(&mut client, now()); |
| 790 |
assert_toward_spa(&data, true); |
| 791 |
assert_eq!(client.stats().frame_tx.stream, 2); |
| 792 |
assert_eq!(client.stats().frame_tx.path_response, 1); |
| 793 |
|
| 794 |
// The server sees the migration and probes the old path. |
| 795 |
let probe = server.process(Some(data), now()).dgram().unwrap(); |
| 796 |
assert_orig_path(&probe, true); |
| 797 |
assert_eq!(server.stats().frame_tx.path_challenge, 2); |
| 798 |
assert_path_challenge_min_len(&server, &probe, now()); |
| 799 |
|
| 800 |
// But data now goes on the new path. |
| 801 |
let data = send_something(&mut server, now()); |
| 802 |
assert_from_spa(&data, false); |
| 803 |
} |
| 804 |
|
| 805 |
/// Migration works for a new port number. |
| 806 |
#[test] |
| 807 |
fn preferred_address_new_port() { |
| 808 |
let a = DEFAULT_ADDR; |
| 809 |
preferred_address(a, a, new_port(a)); |
| 810 |
} |
| 811 |
|
| 812 |
/// Migration works for a new address too. |
| 813 |
#[test] |
| 814 |
fn preferred_address_new_address() { |
| 815 |
let mut preferred = DEFAULT_ADDR; |
| 816 |
preferred.set_ip(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2))); |
| 817 |
preferred_address(DEFAULT_ADDR, DEFAULT_ADDR, preferred); |
| 818 |
} |
| 819 |
|
| 820 |
/// Migration works for IPv4 addresses. |
| 821 |
#[test] |
| 822 |
fn preferred_address_new_port_v4() { |
| 823 |
let a = DEFAULT_ADDR_V4; |
| 824 |
preferred_address(a, a, new_port(a)); |
| 825 |
} |
| 826 |
|
| 827 |
/// Migrating to a loopback address is OK if we started there. |
| 828 |
#[test] |
| 829 |
fn preferred_address_loopback() { |
| 830 |
let a = loopback(); |
| 831 |
preferred_address(a, a, new_port(a)); |
| 832 |
} |
| 833 |
|
| 834 |
fn expect_no_migration(client: &mut Connection, server: &mut Connection) { |
| 835 |
let dgram = fast_handshake(client, server); |
| 836 |
|
| 837 |
// The client won't probe now, though it could; it remains idle. |
| 838 |
let out = client.process(dgram, now()); |
| 839 |
assert_ne!(out.callback(), Duration::new(0, 0)); |
| 840 |
|
| 841 |
// Data continues on the main path for the client. |
| 842 |
let data = send_something(client, now()); |
| 843 |
assert_v6_path(&data, false); |
| 844 |
assert_eq!(client.stats().frame_tx.path_challenge, 0); |
| 845 |
} |
| 846 |
|
| 847 |
fn preferred_address_ignored(spa: PreferredAddress) { |
| 848 |
let mut client = default_client(); |
| 849 |
let mut server = new_server(ConnectionParameters::default().preferred_address(spa)); |
| 850 |
|
| 851 |
expect_no_migration(&mut client, &mut server); |
| 852 |
} |
| 853 |
|
| 854 |
/// Using a loopback address in the preferred address is ignored. |
| 855 |
#[test] |
| 856 |
fn preferred_address_ignore_loopback() { |
| 857 |
preferred_address_ignored(PreferredAddress::new_any(None, Some(loopback()))); |
| 858 |
} |
| 859 |
|
| 860 |
/// A preferred address in the wrong address family is ignored. |
| 861 |
#[test] |
| 862 |
fn preferred_address_ignore_different_family() { |
| 863 |
preferred_address_ignored(PreferredAddress::new_any(Some(DEFAULT_ADDR_V4), None)); |
| 864 |
} |
| 865 |
|
| 866 |
/// Disabling preferred addresses at the client means that it ignores a perfectly |
| 867 |
/// good preferred address. |
| 868 |
#[test] |
| 869 |
fn preferred_address_disabled_client() { |
| 870 |
let mut client = new_client(ConnectionParameters::default().disable_preferred_address()); |
| 871 |
let mut preferred = DEFAULT_ADDR; |
| 872 |
preferred.set_ip(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 2))); |
| 873 |
let spa = PreferredAddress::new_any(None, Some(preferred)); |
| 874 |
let mut server = new_server(ConnectionParameters::default().preferred_address(spa)); |
| 875 |
|
| 876 |
expect_no_migration(&mut client, &mut server); |
| 877 |
} |
| 878 |
|
| 879 |
#[test] |
| 880 |
fn preferred_address_empty_cid() { |
| 881 |
fixture_init(); |
| 882 |
|
| 883 |
let spa = PreferredAddress::new_any(None, Some(new_port(DEFAULT_ADDR))); |
| 884 |
let res = Connection::new_server( |
| 885 |
test_fixture::DEFAULT_KEYS, |
| 886 |
test_fixture::DEFAULT_ALPN, |
| 887 |
Rc::new(RefCell::new(EmptyConnectionIdGenerator::default())), |
| 888 |
ConnectionParameters::default().preferred_address(spa), |
| 889 |
); |
| 890 |
assert_eq!(res.unwrap_err(), Error::ConnectionIdsExhausted); |
| 891 |
} |
| 892 |
|
| 893 |
/// A server cannot include a preferred address if it chooses an empty connection ID. |
| 894 |
#[test] |
| 895 |
fn preferred_address_server_empty_cid() { |
| 896 |
let mut client = default_client(); |
| 897 |
let mut server = Connection::new_server( |
| 898 |
test_fixture::DEFAULT_KEYS, |
| 899 |
test_fixture::DEFAULT_ALPN, |
| 900 |
Rc::new(RefCell::new(EmptyConnectionIdGenerator::default())), |
| 901 |
ConnectionParameters::default(), |
| 902 |
) |
| 903 |
.unwrap(); |
| 904 |
|
| 905 |
server |
| 906 |
.set_local_tparam( |
| 907 |
TransportParameterId::PreferredAddress, |
| 908 |
TransportParameter::Bytes(SAMPLE_PREFERRED_ADDRESS.to_vec()), |
| 909 |
) |
| 910 |
.unwrap(); |
| 911 |
|
| 912 |
connect_fail( |
| 913 |
&mut client, |
| 914 |
&mut server, |
| 915 |
Error::TransportParameterError, |
| 916 |
Error::PeerError(Error::TransportParameterError.code()), |
| 917 |
); |
| 918 |
} |
| 919 |
|
| 920 |
/// A client shouldn't send a preferred address transport parameter. |
| 921 |
#[test] |
| 922 |
fn preferred_address_client() { |
| 923 |
let mut client = default_client(); |
| 924 |
let mut server = default_server(); |
| 925 |
|
| 926 |
client |
| 927 |
.set_local_tparam( |
| 928 |
TransportParameterId::PreferredAddress, |
| 929 |
TransportParameter::Bytes(SAMPLE_PREFERRED_ADDRESS.to_vec()), |
| 930 |
) |
| 931 |
.unwrap(); |
| 932 |
|
| 933 |
connect_fail( |
| 934 |
&mut client, |
| 935 |
&mut server, |
| 936 |
Error::PeerError(Error::TransportParameterError.code()), |
| 937 |
Error::TransportParameterError, |
| 938 |
); |
| 939 |
} |
| 940 |
|
| 941 |
/// Test that migration isn't permitted if the connection isn't in the right state. |
| 942 |
#[test] |
| 943 |
fn migration_invalid_state() { |
| 944 |
let mut client = default_client(); |
| 945 |
assert!(client |
| 946 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), false, now()) |
| 947 |
.is_err()); |
| 948 |
|
| 949 |
let mut server = default_server(); |
| 950 |
assert!(server |
| 951 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), false, now()) |
| 952 |
.is_err()); |
| 953 |
connect_force_idle(&mut client, &mut server); |
| 954 |
|
| 955 |
assert!(server |
| 956 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), false, now()) |
| 957 |
.is_err()); |
| 958 |
|
| 959 |
client.close(now(), 0, "closing"); |
| 960 |
assert!(client |
| 961 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), false, now()) |
| 962 |
.is_err()); |
| 963 |
let close = client.process_output(now()).dgram(); |
| 964 |
|
| 965 |
let dgram = server.process(close, now()).dgram(); |
| 966 |
assert!(server |
| 967 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), false, now()) |
| 968 |
.is_err()); |
| 969 |
|
| 970 |
client.process_input(dgram.unwrap(), now()); |
| 971 |
assert!(client |
| 972 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), false, now()) |
| 973 |
.is_err()); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn migration_disabled() { |
| 978 |
let mut client = default_client(); |
| 979 |
let mut server = new_server(ConnectionParameters::default().disable_migration(true)); |
| 980 |
connect(&mut client, &mut server); |
| 981 |
assert_eq!( |
| 982 |
client |
| 983 |
.migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR), true, now()) |
| 984 |
.unwrap_err(), |
| 985 |
Error::InvalidMigration |
| 986 |
); |
| 987 |
} |
| 988 |
|
| 989 |
#[test] |
| 990 |
fn migration_invalid_address() { |
| 991 |
let mut client = default_client(); |
| 992 |
let mut server = default_server(); |
| 993 |
connect_force_idle(&mut client, &mut server); |
| 994 |
|
| 995 |
let mut cant_migrate = |local, remote| { |
| 996 |
assert_eq!( |
| 997 |
client.migrate(local, remote, true, now()).unwrap_err(), |
| 998 |
Error::InvalidMigration |
| 999 |
); |
| 1000 |
}; |
| 1001 |
|
| 1002 |
// Providing neither address is pointless and therefore an error. |
| 1003 |
cant_migrate(None, None); |
| 1004 |
|
| 1005 |
// Providing a zero port number isn't valid. |
| 1006 |
let mut zero_port = DEFAULT_ADDR; |
| 1007 |
zero_port.set_port(0); |
| 1008 |
cant_migrate(None, Some(zero_port)); |
| 1009 |
cant_migrate(Some(zero_port), None); |
| 1010 |
|
| 1011 |
// An unspecified remote address is bad. |
| 1012 |
let mut remote_unspecified = DEFAULT_ADDR; |
| 1013 |
remote_unspecified.set_ip(IpAddr::V6(Ipv6Addr::from(0))); |
| 1014 |
cant_migrate(None, Some(remote_unspecified)); |
| 1015 |
|
| 1016 |
// Mixed address families is bad. |
| 1017 |
cant_migrate(Some(DEFAULT_ADDR), Some(DEFAULT_ADDR_V4)); |
| 1018 |
cant_migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR)); |
| 1019 |
|
| 1020 |
// Loopback to non-loopback is bad. |
| 1021 |
cant_migrate(Some(DEFAULT_ADDR), Some(loopback())); |
| 1022 |
cant_migrate(Some(loopback()), Some(DEFAULT_ADDR)); |
| 1023 |
assert_eq!( |
| 1024 |
client |
| 1025 |
.migrate(Some(DEFAULT_ADDR), Some(loopback()), true, now()) |
| 1026 |
.unwrap_err(), |
| 1027 |
Error::InvalidMigration |
| 1028 |
); |
| 1029 |
assert_eq!( |
| 1030 |
client |
| 1031 |
.migrate(Some(loopback()), Some(DEFAULT_ADDR), true, now()) |
| 1032 |
.unwrap_err(), |
| 1033 |
Error::InvalidMigration |
| 1034 |
); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/// This inserts a frame into packets that provides a single new |
| 1038 |
/// connection ID and retires all others. |
| 1039 |
struct RetireAll { |
| 1040 |
cid_gen: Rc<RefCell<dyn ConnectionIdGenerator>>, |
| 1041 |
} |
| 1042 |
|
| 1043 |
impl crate::connection::test_internal::FrameWriter for RetireAll { |
| 1044 |
fn write_frames(&mut self, builder: &mut PacketBuilder) { |
| 1045 |
// Use a sequence number that is large enough that all existing values |
| 1046 |
// will be lower (so they get retired). As the code doesn't care about |
| 1047 |
// gaps in sequence numbers, this is safe, even though the gap might |
| 1048 |
// hint that there are more outstanding connection IDs that are allowed. |
| 1049 |
const SEQNO: u64 = 100; |
| 1050 |
let cid = self.cid_gen.borrow_mut().generate_cid().unwrap(); |
| 1051 |
builder |
| 1052 |
.encode_varint(FrameType::NewConnectionId) |
| 1053 |
.encode_varint(SEQNO) |
| 1054 |
.encode_varint(SEQNO) // Retire Prior To |
| 1055 |
.encode_vec(1, &cid) |
| 1056 |
.encode(&[0x7f; 16]); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
/// Test that forcing retirement of connection IDs forces retirement of all active |
| 1061 |
/// connection IDs and the use of of newer one. |
| 1062 |
#[test] |
| 1063 |
fn retire_all() { |
| 1064 |
let mut client = default_client(); |
| 1065 |
let cid_gen: Rc<RefCell<dyn ConnectionIdGenerator>> = |
| 1066 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())); |
| 1067 |
let mut server = Connection::new_server( |
| 1068 |
test_fixture::DEFAULT_KEYS, |
| 1069 |
test_fixture::DEFAULT_ALPN, |
| 1070 |
Rc::clone(&cid_gen), |
| 1071 |
ConnectionParameters::default(), |
| 1072 |
) |
| 1073 |
.unwrap(); |
| 1074 |
connect_force_idle(&mut client, &mut server); |
| 1075 |
|
| 1076 |
let original_cid = ConnectionId::from(get_cid(&send_something(&mut client, now()))); |
| 1077 |
|
| 1078 |
let ncid = send_with_extra(&mut server, RetireAll { cid_gen }, now()); |
| 1079 |
|
| 1080 |
let new_cid_before = client.stats().frame_rx.new_connection_id; |
| 1081 |
let retire_cid_before = client.stats().frame_tx.retire_connection_id; |
| 1082 |
client.process_input(ncid, now()); |
| 1083 |
let retire = send_something(&mut client, now()); |
| 1084 |
assert_eq!( |
| 1085 |
client.stats().frame_rx.new_connection_id, |
| 1086 |
new_cid_before + 1 |
| 1087 |
); |
| 1088 |
assert_eq!( |
| 1089 |
client.stats().frame_tx.retire_connection_id, |
| 1090 |
retire_cid_before + LOCAL_ACTIVE_CID_LIMIT |
| 1091 |
); |
| 1092 |
|
| 1093 |
assert_ne!(get_cid(&retire), original_cid); |
| 1094 |
} |
| 1095 |
|
| 1096 |
/// During a graceful migration, if the probed path can't get a new connection ID due |
| 1097 |
/// to being forced to retire the one it is using, the migration will fail. |
| 1098 |
#[test] |
| 1099 |
fn retire_prior_to_migration_failure() { |
| 1100 |
let mut client = default_client(); |
| 1101 |
let cid_gen: Rc<RefCell<dyn ConnectionIdGenerator>> = |
| 1102 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())); |
| 1103 |
let mut server = Connection::new_server( |
| 1104 |
test_fixture::DEFAULT_KEYS, |
| 1105 |
test_fixture::DEFAULT_ALPN, |
| 1106 |
Rc::clone(&cid_gen), |
| 1107 |
ConnectionParameters::default(), |
| 1108 |
) |
| 1109 |
.unwrap(); |
| 1110 |
connect_force_idle(&mut client, &mut server); |
| 1111 |
|
| 1112 |
let original_cid = ConnectionId::from(get_cid(&send_something(&mut client, now()))); |
| 1113 |
|
| 1114 |
client |
| 1115 |
.migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), false, now()) |
| 1116 |
.unwrap(); |
| 1117 |
|
| 1118 |
// The client now probes the new path. |
| 1119 |
let probe = client.process_output(now()).dgram().unwrap(); |
| 1120 |
assert_v4_path(&probe, true); |
| 1121 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 1122 |
assert_path_challenge_min_len(&client, &probe, now()); |
| 1123 |
let probe_cid = ConnectionId::from(get_cid(&probe)); |
| 1124 |
assert_ne!(original_cid, probe_cid); |
| 1125 |
|
| 1126 |
// Have the server receive the probe, but separately have it decide to |
| 1127 |
// retire all of the available connection IDs. |
| 1128 |
let retire_all = send_with_extra(&mut server, RetireAll { cid_gen }, now()); |
| 1129 |
|
| 1130 |
let resp = server.process(Some(probe), now()).dgram().unwrap(); |
| 1131 |
assert_v4_path(&resp, true); |
| 1132 |
assert_eq!(server.stats().frame_tx.path_response, 1); |
| 1133 |
assert_eq!(server.stats().frame_tx.path_challenge, 1); |
| 1134 |
assert_path_challenge_min_len(&server, &resp, now()); |
| 1135 |
|
| 1136 |
// Have the client receive the NEW_CONNECTION_ID with Retire Prior To. |
| 1137 |
client.process_input(retire_all, now()); |
| 1138 |
// This packet contains the probe response, which should be fine, but it |
| 1139 |
// also includes PATH_CHALLENGE for the new path, and the client can't |
| 1140 |
// respond without a connection ID. We treat this as a connection error. |
| 1141 |
client.process_input(resp, now()); |
| 1142 |
assert!(matches!( |
| 1143 |
client.state(), |
| 1144 |
State::Closing { |
| 1145 |
error: CloseReason::Transport(Error::InvalidMigration), |
| 1146 |
.. |
| 1147 |
} |
| 1148 |
)); |
| 1149 |
} |
| 1150 |
|
| 1151 |
/// The timing of when frames arrive can mean that the migration path can |
| 1152 |
/// get the last available connection ID. |
| 1153 |
#[test] |
| 1154 |
fn retire_prior_to_migration_success() { |
| 1155 |
let mut client = default_client(); |
| 1156 |
let cid_gen: Rc<RefCell<dyn ConnectionIdGenerator>> = |
| 1157 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())); |
| 1158 |
let mut server = Connection::new_server( |
| 1159 |
test_fixture::DEFAULT_KEYS, |
| 1160 |
test_fixture::DEFAULT_ALPN, |
| 1161 |
Rc::clone(&cid_gen), |
| 1162 |
ConnectionParameters::default(), |
| 1163 |
) |
| 1164 |
.unwrap(); |
| 1165 |
connect_force_idle(&mut client, &mut server); |
| 1166 |
|
| 1167 |
let original_cid = ConnectionId::from(get_cid(&send_something(&mut client, now()))); |
| 1168 |
|
| 1169 |
client |
| 1170 |
.migrate(Some(DEFAULT_ADDR_V4), Some(DEFAULT_ADDR_V4), false, now()) |
| 1171 |
.unwrap(); |
| 1172 |
|
| 1173 |
// The client now probes the new path. |
| 1174 |
let probe = client.process_output(now()).dgram().unwrap(); |
| 1175 |
assert_v4_path(&probe, true); |
| 1176 |
assert_eq!(client.stats().frame_tx.path_challenge, 1); |
| 1177 |
assert_path_challenge_min_len(&client, &probe, now()); |
| 1178 |
let probe_cid = ConnectionId::from(get_cid(&probe)); |
| 1179 |
assert_ne!(original_cid, probe_cid); |
| 1180 |
|
| 1181 |
// Have the server receive the probe, but separately have it decide to |
| 1182 |
// retire all of the available connection IDs. |
| 1183 |
let retire_all = send_with_extra(&mut server, RetireAll { cid_gen }, now()); |
| 1184 |
|
| 1185 |
let resp = server.process(Some(probe), now()).dgram().unwrap(); |
| 1186 |
assert_v4_path(&resp, true); |
| 1187 |
assert_eq!(server.stats().frame_tx.path_response, 1); |
| 1188 |
assert_eq!(server.stats().frame_tx.path_challenge, 1); |
| 1189 |
assert_path_challenge_min_len(&server, &resp, now()); |
| 1190 |
|
| 1191 |
// Have the client receive the NEW_CONNECTION_ID with Retire Prior To second. |
| 1192 |
// As this occurs in a very specific order, migration succeeds. |
| 1193 |
client.process_input(resp, now()); |
| 1194 |
client.process_input(retire_all, now()); |
| 1195 |
|
| 1196 |
// Migration succeeds and the new path gets the last connection ID. |
| 1197 |
let dgram = send_something(&mut client, now()); |
| 1198 |
assert_v4_path(&dgram, false); |
| 1199 |
assert_ne!(get_cid(&dgram), original_cid); |
| 1200 |
assert_ne!(get_cid(&dgram), probe_cid); |
| 1201 |
} |
| 1202 |
|
| 1203 |
struct GarbageWriter {} |
| 1204 |
|
| 1205 |
impl crate::connection::test_internal::FrameWriter for GarbageWriter { |
| 1206 |
fn write_frames(&mut self, builder: &mut PacketBuilder) { |
| 1207 |
// Not a valid frame type. |
| 1208 |
builder.encode_varint(u32::MAX); |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
/// Test the case that we run out of connection ID and receive an invalid frame |
| 1213 |
/// from a new path. |
| 1214 |
#[test] |
| 1215 |
fn error_on_new_path_with_no_connection_id() { |
| 1216 |
let mut client = default_client(); |
| 1217 |
let mut server = default_server(); |
| 1218 |
connect_force_idle(&mut client, &mut server); |
| 1219 |
|
| 1220 |
let cid_gen: Rc<RefCell<dyn ConnectionIdGenerator>> = |
| 1221 |
Rc::new(RefCell::new(CountingConnectionIdGenerator::default())); |
| 1222 |
let retire_all = send_with_extra(&mut server, RetireAll { cid_gen }, now()); |
| 1223 |
|
| 1224 |
client.process_input(retire_all, now()); |
| 1225 |
|
| 1226 |
let garbage = send_with_extra(&mut server, GarbageWriter {}, now()); |
| 1227 |
|
| 1228 |
let dgram = change_path(&garbage, DEFAULT_ADDR_V4); |
| 1229 |
client.process_input(dgram, now()); |
| 1230 |
|
| 1231 |
// See issue #1697. We had a crash when the client had a temporary path and |
| 1232 |
// process_output is called. |
| 1233 |
let closing_frames = client.stats().frame_tx.connection_close; |
| 1234 |
drop(client.process_output(now())); |
| 1235 |
assert!(matches!( |
| 1236 |
client.state(), |
| 1237 |
State::Closing { |
| 1238 |
error: CloseReason::Transport(Error::UnknownFrameType), |
| 1239 |
.. |
| 1240 |
} |
| 1241 |
)); |
| 1242 |
// Wait until the connection is closed. |
| 1243 |
let mut now = now(); |
| 1244 |
now += client.process_output(now).callback(); |
| 1245 |
_ = client.process_output(now); |
| 1246 |
// No closing frames should be sent, and the connection should be closed. |
| 1247 |
assert_eq!(client.stats().frame_tx.connection_close, closing_frames); |
| 1248 |
assert!(matches!( |
| 1249 |
client.state(), |
| 1250 |
State::Closed(CloseReason::Transport(Error::UnknownFrameType)) |
| 1251 |
)); |
| 1252 |
} |