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
// Copyright 2023 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.

//! Device link layer configuration types.

use net_types::ip::Ip;
use netstack3_base::{Device, DeviceIdContext};
use netstack3_ip::nud::{NudUserConfig, NudUserConfigUpdate};

/// Device ARP configuration.
#[derive(Clone, Debug)]
pub struct ArpConfiguration {
    /// NUD over ARP configuration.
    pub nud: NudUserConfig,
}

/// Device NDP configuration.
#[derive(Clone, Debug)]
pub struct NdpConfiguration {
    /// NUD over NDP configuration.
    pub nud: NudUserConfig,
}

/// Device link layer configuration.
#[derive(Clone, Debug)]
pub struct DeviceConfiguration {
    /// ARP configurations.
    ///
    /// Only present if the device supports ARP.
    pub arp: Option<ArpConfiguration>,
    /// NDP configurations.
    ///
    /// Only present if the device supports NDP.
    pub ndp: Option<NdpConfiguration>,
}
/// An update to apply to ARP configurations.
///
/// Only fields with variant `Some` are requested to be updated.
#[derive(Clone, Debug, Default)]
pub struct ArpConfigurationUpdate {
    /// NUD over ARP configuration update.
    pub nud: Option<NudUserConfigUpdate>,
}

/// An update to apply to NDP configurations.
///
/// Only fields with variant `Some` are requested to be updated.
#[derive(Clone, Debug, Default)]
pub struct NdpConfigurationUpdate {
    /// NUD over NDP configuration update.
    pub nud: Option<NudUserConfigUpdate>,
}

/// An update to apply to device configurations.
///
/// Only fields with variant `Some` are requested to be updated.
#[derive(Clone, Debug, Default)]
pub struct DeviceConfigurationUpdate {
    /// ARP configuration update.
    pub arp: Option<ArpConfigurationUpdate>,
    /// NDP configuration update.
    pub ndp: Option<NdpConfigurationUpdate>,
}

/// Errors observed updating device configuration.
#[derive(Debug)]
pub enum DeviceConfigurationUpdateError {
    /// ARP is not supported for the requested device.
    ArpNotSupported,
    /// NDP is not supported for the requested device.
    NdpNotSupported,
}

/// A trait abstracting device configuration.
///
/// This trait allows the device API to perform device confiuration at the
/// device layer.
pub trait DeviceConfigurationContext<D: Device>: DeviceIdContext<D> {
    /// Calls the callback with a mutable reference to the NUD user
    /// configuration for IP version `I`.
    ///
    /// If the device does not support NUD, the callback is called with `None`,
    fn with_nud_config<I: Ip, O, F: FnOnce(Option<&NudUserConfig>) -> O>(
        &mut self,
        device_id: &Self::DeviceId,
        f: F,
    ) -> O;

    /// Calls the callback with a mutable reference to the NUD user
    /// configuration for IP version `I`.
    ///
    /// If the device does not support NUD, the callback is called with `None`,
    fn with_nud_config_mut<I: Ip, O, F: FnOnce(Option<&mut NudUserConfig>) -> O>(
        &mut self,
        device_id: &Self::DeviceId,
        f: F,
    ) -> O;
}