common/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::Context as _;
use fuchsia_async::{DurationExt as _, TimeoutExt as _};
use futures::io::{AsyncReadExt as _, AsyncWriteExt as _};
use futures::{FutureExt as _, StreamExt, TryFutureExt as _};
use net_declare::{fidl_mac, fidl_subnet};
use netemul::{RealmTcpListener as _, RealmTcpStream as _, RealmUdpSocket as _};
use netfilter::FidlReturn as _;
use netstack_testing_common::realms::{Netstack2, TestSandboxExt as _};
use netstack_testing_common::{
    ASYNC_EVENT_NEGATIVE_CHECK_TIMEOUT, ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT,
};
use std::ops::RangeInclusive;
use {fidl_fuchsia_net as fnet, fidl_fuchsia_net_filter_deprecated as fnetfilter};

pub const CLIENT_IPV4_SUBNET: fnet::Subnet = fidl_subnet!("192.168.0.2/24");
pub const SERVER_IPV4_SUBNET: fnet::Subnet = fidl_subnet!("192.168.0.1/24");
pub const CLIENT_MAC_ADDRESS: fnet::MacAddress = fidl_mac!("02:00:00:00:00:01");
pub const SERVER_MAC_ADDRESS: fnet::MacAddress = fidl_mac!("02:00:00:00:00:02");

pub const CLIENT_PORT: u16 = 1234;
pub const SERVER_PORT: u16 = 8080;

pub const CLIENT_PAYLOAD: &'static str = "Enjoy your food!";
pub const SERVER_PAYLOAD: &'static str = "Thanks, you too...";

#[derive(Copy, Clone)]
pub enum ExpectedTraffic {
    ClientToServerOnly,
    TwoWay,
}

pub struct Test {
    pub proto: fnetfilter::SocketProtocol,
    pub client_updates: Option<Vec<fnetfilter::Rule>>,
    pub server_updates: Option<Vec<fnetfilter::Rule>>,
    pub expected_traffic: ExpectedTraffic,
}

pub async fn run_udp_socket_test(
    server: &netemul::TestRealm<'_>,
    server_addr: fidl_fuchsia_net::IpAddress,
    server_port: u16,
    client: &netemul::TestRealm<'_>,
    client_addr: fidl_fuchsia_net::IpAddress,
    client_port: u16,
    expected_traffic: ExpectedTraffic,
) {
    let fidl_fuchsia_net_ext::IpAddress(client_addr) =
        fidl_fuchsia_net_ext::IpAddress::from(client_addr);
    let client_addr = std::net::SocketAddr::new(client_addr, client_port);

    let fidl_fuchsia_net_ext::IpAddress(server_addr) =
        fidl_fuchsia_net_ext::IpAddress::from(server_addr);
    let server_addr = std::net::SocketAddr::new(server_addr, server_port);

    let client_sock = fuchsia_async::net::UdpSocket::bind_in_realm(client, client_addr)
        .await
        .expect("failed to create client socket");
    let client_addr =
        client_sock.local_addr().expect("failed to get local address from client socket");

    let server_sock = fuchsia_async::net::UdpSocket::bind_in_realm(server, server_addr)
        .await
        .expect("failed to create server socket");

    let server_fut = async move {
        let mut buf = [0u8; 1024];
        let (r, from) = server_sock.recv_from(&mut buf[..]).await.expect("recvfrom failed");
        assert_eq!(r, CLIENT_PAYLOAD.as_bytes().len());
        assert_eq!(&buf[..r], CLIENT_PAYLOAD.as_bytes());
        assert_eq!(from, client_addr);
        let r = server_sock
            .send_to(SERVER_PAYLOAD.as_bytes(), client_addr)
            .await
            .expect("send to failed");
        assert_eq!(r, SERVER_PAYLOAD.as_bytes().len());
    };

    let client_fut = async move {
        let r = client_sock
            .send_to(CLIENT_PAYLOAD.as_bytes(), server_addr)
            .await
            .expect("sendto failed");
        assert_eq!(r, CLIENT_PAYLOAD.as_bytes().len());

        let mut buf = [0u8; 1024];
        match expected_traffic {
            ExpectedTraffic::ClientToServerOnly => {
                match client_sock
                    .recv_from(&mut buf[..])
                    .map_ok(Some)
                    .on_timeout(ASYNC_EVENT_NEGATIVE_CHECK_TIMEOUT.after_now(), || Ok(None))
                    .await
                    .expect("recvfrom failed")
                {
                    Some((r, from)) => {
                        panic!("unexpectedly received packet {:?} from {:?}", &buf[..r], from)
                    }
                    None => (),
                }
            }
            ExpectedTraffic::TwoWay => {
                let (r, from) = client_sock
                    .recv_from(&mut buf[..])
                    .map(|r| r.expect("recvfrom failed"))
                    .on_timeout(ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT.after_now(), || {
                        panic!(
                            "timed out waiting for packet from server after {:?}",
                            ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT
                        );
                    })
                    .await;
                assert_eq!(r, SERVER_PAYLOAD.as_bytes().len());
                assert_eq!(&buf[..r], SERVER_PAYLOAD.as_bytes());
                assert_eq!(from, server_addr);
            }
        }
    };
    let ((), ()) = futures::future::join(server_fut, client_fut).await;
}

pub async fn run_tcp_socket_test(
    server: &netemul::TestRealm<'_>,
    server_addr: fidl_fuchsia_net::IpAddress,
    server_port: u16,
    client: &netemul::TestRealm<'_>,
    client_addr: fidl_fuchsia_net::IpAddress,
    client_port: u16,
    expected_traffic: ExpectedTraffic,
) {
    let fidl_fuchsia_net_ext::IpAddress(client_addr) =
        fidl_fuchsia_net_ext::IpAddress::from(client_addr);
    let client_addr = std::net::SocketAddr::new(client_addr, client_port);

    let fidl_fuchsia_net_ext::IpAddress(server_addr) =
        fidl_fuchsia_net_ext::IpAddress::from(server_addr);
    let server_addr = std::net::SocketAddr::new(server_addr, server_port);

    let listener =
        fuchsia_async::net::TcpListener::listen_in_realm_with(server, server_addr, |sock| {
            sock.set_reuse_port(true).context("failed to set reuse port")
        })
        .await
        .expect("failed to create server socket");

    let server_fut = async move {
        match expected_traffic {
            ExpectedTraffic::ClientToServerOnly => {
                match listener
                    .accept()
                    .map_ok(Some)
                    .on_timeout(ASYNC_EVENT_NEGATIVE_CHECK_TIMEOUT.after_now(), || Ok(None))
                    .await
                    .expect("failed to accept a connection")
                {
                    Some(_stream) => panic!("unexpectedly connected successfully"),
                    None => (),
                }
            }
            ExpectedTraffic::TwoWay => {
                let (_listener, mut stream, from) = listener
                    .accept()
                    .map(|r| r.expect("accept failed"))
                    .on_timeout(ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT.after_now(), || {
                        panic!(
                            "timed out waiting for a connection after {:?}",
                            ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT
                        );
                    })
                    .await;

                let mut buf = [0u8; 1024];
                let read_count =
                    stream.read(&mut buf).await.expect("read from tcp server stream failed");

                assert_eq!(from.ip(), client_addr.ip());
                assert_eq!(read_count, CLIENT_PAYLOAD.as_bytes().len());
                assert_eq!(&buf[..read_count], CLIENT_PAYLOAD.as_bytes());

                let write_count = stream
                    .write(SERVER_PAYLOAD.as_bytes())
                    .await
                    .expect("write to tcp server stream failed");
                assert_eq!(write_count, SERVER_PAYLOAD.as_bytes().len());
            }
        }
    };

    let client_fut = async move {
        match expected_traffic {
            ExpectedTraffic::ClientToServerOnly => {
                match fuchsia_async::net::TcpStream::connect_in_realm(client, server_addr)
                    .map_ok(Some)
                    .on_timeout(ASYNC_EVENT_NEGATIVE_CHECK_TIMEOUT.after_now(), || Ok(None))
                    .await
                    .expect("failed to create client socket")
                {
                    Some(_stream) => panic!("unexpectedly connected successfully"),
                    None => (),
                }
            }
            ExpectedTraffic::TwoWay => {
                let mut stream =
                    fuchsia_async::net::TcpStream::connect_in_realm(client, server_addr)
                        .map(|r| r.expect("connect_in_realm failed"))
                        .on_timeout(ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT.after_now(), || {
                            panic!(
                                "timed out waiting for a connection after {:?}",
                                ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT
                            );
                        })
                        .await;

                let write_count = stream
                    .write(CLIENT_PAYLOAD.as_bytes())
                    .await
                    .expect("write to tcp client stream failed");

                assert_eq!(write_count, CLIENT_PAYLOAD.as_bytes().len());

                let mut buf = [0u8; 1024];
                let read_count =
                    stream.read(&mut buf).await.expect("read from tcp client stream failed");

                assert_eq!(read_count, SERVER_PAYLOAD.as_bytes().len());
                assert_eq!(&buf[..read_count], SERVER_PAYLOAD.as_bytes());
            }
        }
    };

    let ((), ()) = futures::future::join(client_fut, server_fut).await;
}

pub async fn run_socket_test(
    proto: fnetfilter::SocketProtocol,
    server: &netemul::TestRealm<'_>,
    server_addr: fidl_fuchsia_net::IpAddress,
    server_port: u16,
    client: &netemul::TestRealm<'_>,
    client_addr: fidl_fuchsia_net::IpAddress,
    client_port: u16,
    expected_traffic: ExpectedTraffic,
) {
    match proto {
        fnetfilter::SocketProtocol::Udp => {
            run_udp_socket_test(
                server,
                server_addr,
                server_port,
                client,
                client_addr,
                client_port,
                expected_traffic,
            )
            .await
        }
        fnetfilter::SocketProtocol::Tcp => {
            run_tcp_socket_test(
                server,
                server_addr,
                server_port,
                client,
                client_addr,
                client_port,
                expected_traffic,
            )
            .await
        }
        proto => panic!("unexpected protocol {:?}", proto),
    }
}

pub async fn test_filter(name: &str, test: Test) {
    let sandbox = netemul::TestSandbox::new().expect("failed to create sandbox");
    let net = sandbox.create_network("net").await.expect("failed to create network");

    let client = sandbox
        .create_netstack_realm::<Netstack2, _>(format!("{}_client", name))
        .expect("failed to create client realm");
    let client_ep = client
        .join_network_with(
            &net,
            "client",
            netemul::new_endpoint_config(netemul::DEFAULT_MTU, Some(CLIENT_MAC_ADDRESS)),
            Default::default(),
        )
        .await
        .expect("client failed to join network");
    client_ep.add_address_and_subnet_route(CLIENT_IPV4_SUBNET).await.expect("configure address");
    let client_filter = client
        .connect_to_protocol::<fnetfilter::FilterMarker>()
        .expect("client failed to connect to filter service");

    let server = sandbox
        .create_netstack_realm::<Netstack2, _>(format!("{}_server", name))
        .expect("failed to create server realm");
    let server_ep = server
        .join_network_with(
            &net,
            "server",
            netemul::new_endpoint_config(netemul::DEFAULT_MTU, Some(SERVER_MAC_ADDRESS)),
            Default::default(),
        )
        .await
        .expect("server failed to join network");
    server_ep.add_address_and_subnet_route(SERVER_IPV4_SUBNET).await.expect("configure address");

    // Put client and server in each other's neighbor table. We've observed
    // flakes in CQ due to ARP timeouts and ARP resolution is immaterial to the
    // tests we run here.
    let () = futures::stream::iter([
        (&server, &server_ep, CLIENT_IPV4_SUBNET.addr, CLIENT_MAC_ADDRESS),
        (&client, &client_ep, SERVER_IPV4_SUBNET.addr, SERVER_MAC_ADDRESS),
    ])
    .for_each_concurrent(None, |(realm, ep, addr, mac)| {
        realm.add_neighbor_entry(ep.id(), addr, mac).map(|r| r.expect("add_neighbor_entry"))
    })
    .await;

    let server_filter = server
        .connect_to_protocol::<fnetfilter::FilterMarker>()
        .expect("server failed to connect to filter service");

    let Test { proto, client_updates, server_updates, expected_traffic } = test;

    // Initial check (no filters set).
    let () = client_filter
        .enable_interface(client_ep.id())
        .await
        .transform_result()
        .expect("error enabling filter on client");
    let () = server_filter
        .enable_interface(server_ep.id())
        .await
        .transform_result()
        .expect("error enabling filter on server");
    let () = run_socket_test(
        proto,
        &server,
        SERVER_IPV4_SUBNET.addr,
        SERVER_PORT,
        &client,
        CLIENT_IPV4_SUBNET.addr,
        CLIENT_PORT,
        ExpectedTraffic::TwoWay,
    )
    .await;

    // Set the filters and do the test.
    let (_rules, mut server_generation) =
        server_filter.get_rules().await.expect("failed to get server's filter rules");
    let (_rules, mut client_generation) =
        client_filter.get_rules().await.expect("failed to get client's filter rules");
    if let Some(updates) = client_updates {
        let () = client_filter
            .update_rules(&updates, client_generation)
            .await
            .transform_result()
            .expect("failed to update client's filter rules");
        client_generation += 1;
    }
    if let Some(updates) = server_updates {
        let () = server_filter
            .update_rules(&updates, server_generation)
            .await
            .transform_result()
            .expect("failed to update server's filter rules");
        server_generation += 1;
    }
    let () = run_socket_test(
        proto,
        &server,
        SERVER_IPV4_SUBNET.addr,
        SERVER_PORT,
        &client,
        CLIENT_IPV4_SUBNET.addr,
        CLIENT_PORT,
        expected_traffic,
    )
    .await;

    // Disable the filters on the interface and expect full connectivity.
    let () = client_filter
        .disable_interface(client_ep.id())
        .await
        .transform_result()
        .expect("error disabling filter on client");
    let () = server_filter
        .disable_interface(server_ep.id())
        .await
        .transform_result()
        .expect("error disabling filter on server");
    let () = run_socket_test(
        proto,
        &server,
        SERVER_IPV4_SUBNET.addr,
        SERVER_PORT,
        &client,
        CLIENT_IPV4_SUBNET.addr,
        CLIENT_PORT,
        ExpectedTraffic::TwoWay,
    )
    .await;

    // Reset and enable filters and expect full connectivity.
    let () = client_filter
        .enable_interface(client_ep.id())
        .await
        .transform_result()
        .expect("error re-enabling filter on client");
    let () = server_filter
        .enable_interface(server_ep.id())
        .await
        .transform_result()
        .expect("error re-enabling filter on server");
    let () = server_filter
        .update_rules(&[], server_generation)
        .await
        .transform_result()
        .expect("failed to reset client's filter rules");
    let () = client_filter
        .update_rules(&[], client_generation)
        .await
        .transform_result()
        .expect("failed to reset client's filter rules");
    run_socket_test(
        proto,
        &server,
        SERVER_IPV4_SUBNET.addr,
        SERVER_PORT,
        &client,
        CLIENT_IPV4_SUBNET.addr,
        CLIENT_PORT,
        ExpectedTraffic::TwoWay,
    )
    .await
}

/// Tests how the filter on server drops the outgoing traffic from server.
pub fn server_outgoing_drop_test(
    proto: fnetfilter::SocketProtocol,
    src_subnet: Option<fnet::Subnet>,
    src_subnet_invert_match: bool,
    dst_subnet: Option<fnet::Subnet>,
    dst_subnet_invert_match: bool,
    src_port_range: RangeInclusive<u16>,
    dst_port_range: RangeInclusive<u16>,
    expected_traffic: ExpectedTraffic,
) -> Test {
    Test {
        proto,
        client_updates: None,
        server_updates: Some(vec![fnetfilter::Rule {
            action: fnetfilter::Action::Drop,
            direction: fnetfilter::Direction::Outgoing,
            proto,
            src_subnet: src_subnet.map(Box::new),
            src_subnet_invert_match,
            dst_subnet: dst_subnet.map(Box::new),
            dst_subnet_invert_match,
            src_port_range: fnetfilter::PortRange {
                start: *src_port_range.start(),
                end: *src_port_range.end(),
            },
            dst_port_range: fnetfilter::PortRange {
                start: *dst_port_range.start(),
                end: *dst_port_range.end(),
            },
            nic: 0,
            log: false,
            keep_state: false,
            device_class: fnetfilter::DeviceClass::Any(fnetfilter::Empty {}),
        }]),
        expected_traffic,
    }
}

/// Tests if the filter on client drops the incoming traffic to client.
pub fn client_incoming_drop_test(
    proto: fnetfilter::SocketProtocol,
    src_subnet: Option<fnet::Subnet>,
    src_subnet_invert_match: bool,
    dst_subnet: Option<fnet::Subnet>,
    dst_subnet_invert_match: bool,
    src_port_range: RangeInclusive<u16>,
    dst_port_range: RangeInclusive<u16>,
    expected_traffic: ExpectedTraffic,
) -> Test {
    Test {
        proto,
        client_updates: Some(vec![fnetfilter::Rule {
            action: fnetfilter::Action::Drop,
            direction: fnetfilter::Direction::Incoming,
            proto,
            src_subnet: src_subnet.map(Box::new),
            src_subnet_invert_match,
            dst_subnet: dst_subnet.map(Box::new),
            dst_subnet_invert_match,
            src_port_range: fnetfilter::PortRange {
                start: *src_port_range.start(),
                end: *src_port_range.end(),
            },
            dst_port_range: fnetfilter::PortRange {
                start: *dst_port_range.start(),
                end: *dst_port_range.end(),
            },
            nic: 0,
            log: false,
            keep_state: false,
            device_class: fnetfilter::DeviceClass::Any(fnetfilter::Empty {}),
        }]),
        server_updates: None,
        expected_traffic,
    }
}

pub fn subnet_with_offset(
    fnet::Subnet { mut addr, prefix_len }: fnet::Subnet,
    offset: u8,
) -> fnet::Subnet {
    let last_mut = match addr {
        fnet::IpAddress::Ipv4(fnet::Ipv4Address { ref mut addr }) => addr.last_mut(),
        fnet::IpAddress::Ipv6(fnet::Ipv6Address { ref mut addr }) => addr.last_mut(),
    };
    *last_mut.expect("should have at least 1 byte in addresses") += offset;

    fnet::Subnet { addr, prefix_len }
}