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
// SPDX-License-Identifier: MIT

use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
    nla::{DefaultNla, Nla, NlaBuffer, NlasIterator},
    parsers::{parse_u16, parse_u32},
    traits::Parseable,
    DecodeError, Emitable,
};

const IFLA_BRIDGE_FLAGS: u16 = 0;
const IFLA_BRIDGE_MODE: u16 = 1;
const IFLA_BRIDGE_VLAN_INFO: u16 = 2;
const IFLA_BRIDGE_VLAN_TUNNEL_INFO: u16 = 3;
// const IFLA_BRIDGE_MRP: u16 = 4;
// const IFLA_BRIDGE_CFM: u16 = 5;
// const IFLA_BRIDGE_MST: u16 = 6;

#[derive(Clone, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum AfSpecBridge {
    Flags(BridgeFlag),
    Mode(BridgeMode),
    VlanInfo(BridgeVlanInfo),
    VlanTunnelInfo(Vec<BridgeVlanTunnelInfo>),
    Other(DefaultNla),
}

impl Nla for AfSpecBridge {
    fn value_len(&self) -> usize {
        match self {
            Self::Flags(_) => BridgeFlag::LENGTH,
            Self::Mode(_) => BridgeMode::LENGTH,
            Self::VlanInfo(_) => BridgeVlanInfo::LENGTH,
            Self::VlanTunnelInfo(s) => s.as_slice().buffer_len(),
            Self::Other(nla) => nla.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Flags(value) => {
                NativeEndian::write_u16(buffer, u16::from(*value))
            }
            Self::Mode(value) => {
                NativeEndian::write_u16(buffer, u16::from(*value))
            }
            Self::VlanInfo(info) => {
                buffer[..4].copy_from_slice(<[u8; 4]>::from(info).as_slice())
            }
            Self::VlanTunnelInfo(s) => s.as_slice().emit(buffer),
            Self::Other(nla) => nla.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            Self::Flags(_) => IFLA_BRIDGE_FLAGS,
            Self::Mode(_) => IFLA_BRIDGE_MODE,
            Self::VlanInfo(_) => IFLA_BRIDGE_VLAN_INFO,
            Self::VlanTunnelInfo(_) => IFLA_BRIDGE_VLAN_TUNNEL_INFO,
            Self::Other(nla) => nla.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecBridge {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();
        Ok(match buf.kind() {
            IFLA_BRIDGE_FLAGS => Self::Flags(
                parse_u16(payload)
                    .context("Invalid IFLA_BRIDGE_FLAGS value")?
                    .into(),
            ),
            IFLA_BRIDGE_MODE => Self::Mode(
                parse_u16(payload)
                    .context("Invalid IFLA_BRIDGE_MODE value")?
                    .into(),
            ),
            IFLA_BRIDGE_VLAN_INFO => Self::VlanInfo(
                BridgeVlanInfo::try_from(payload)
                    .context("Invalid IFLA_BRIDGE_VLAN_INFO value")?,
            ),
            IFLA_BRIDGE_VLAN_TUNNEL_INFO => {
                let mut nlas = Vec::new();
                for nla in NlasIterator::new(payload) {
                    let nla = &nla.context(format!(
                        "Invalid IFLA_BRIDGE_VLAN_TUNNEL_INFO for {payload:?}"
                    ))?;
                    let parsed = BridgeVlanTunnelInfo::parse(nla)?;
                    nlas.push(parsed);
                }
                Self::VlanTunnelInfo(nlas)
            }
            kind => Self::Other(
                DefaultNla::parse(buf)
                    .context(format!("Unknown NLA type {kind}"))?,
            ),
        })
    }
}

#[cfg(any(target_os = "linux", target_os = "fuchsia"))]
pub(crate) struct VecAfSpecBridge(pub(crate) Vec<AfSpecBridge>);

#[cfg(any(target_os = "linux", target_os = "fuchsia"))]
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
    for VecAfSpecBridge
{
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let mut nlas = vec![];
        let err = "Invalid AF_INET NLA for IFLA_AF_SPEC(AF_BRIDGE)";
        for nla in NlasIterator::new(buf.into_inner()) {
            let nla = nla.context(err)?;
            nlas.push(AfSpecBridge::parse(&nla).context(err)?);
        }
        Ok(Self(nlas))
    }
}

const BRIDGE_VLAN_INFO_CONTROLLER: u16 = 1 << 0;
const BRIDGE_VLAN_INFO_PVID: u16 = 1 << 1;
const BRIDGE_VLAN_INFO_UNTAGGED: u16 = 1 << 2;
const BRIDGE_VLAN_INFO_RANGE_BEGIN: u16 = 1 << 3;
const BRIDGE_VLAN_INFO_RANGE_END: u16 = 1 << 4;
const BRIDGE_VLAN_INFO_BRENTRY: u16 = 1 << 5;
const BRIDGE_VLAN_INFO_ONLY_OPTS: u16 = 1 << 6;

bitflags! {
    #[non_exhaustive]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct BridgeVlanInfoFlags: u16 {
        /// Operate on Bridge device as well
        const Controller = BRIDGE_VLAN_INFO_CONTROLLER;
        /// VLAN is PVID, ingress untagged
        const Pvid = BRIDGE_VLAN_INFO_PVID;
        /// VLAN egresses untagged
        const Untagged= BRIDGE_VLAN_INFO_UNTAGGED;
        /// VLAN is start of vlan range
        const RangeBegin = BRIDGE_VLAN_INFO_RANGE_BEGIN;
        /// VLAN is end of vlan range
        const RangeEnd = BRIDGE_VLAN_INFO_RANGE_END;
        /// Global bridge VLAN entry
        const Brentry = BRIDGE_VLAN_INFO_BRENTRY;
        /// Skip create/delete/flags
        const OnlyOpts= BRIDGE_VLAN_INFO_ONLY_OPTS;
        const _ = !0;
    }
}

impl BridgeVlanInfoFlags {
    pub const LENGTH: usize = 2;
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct BridgeVlanInfo {
    pub flags: BridgeVlanInfoFlags,
    pub vid: u16,
}

impl BridgeVlanInfo {
    pub const LENGTH: usize = 4;
}

impl From<&BridgeVlanInfo> for [u8; 4] {
    fn from(d: &BridgeVlanInfo) -> Self {
        let mut ret = [0u8; 4];
        NativeEndian::write_u16(&mut ret[0..2], d.flags.bits());
        NativeEndian::write_u16(&mut ret[2..4], d.vid);
        ret
    }
}

impl TryFrom<&[u8]> for BridgeVlanInfo {
    type Error = DecodeError;
    fn try_from(raw: &[u8]) -> Result<Self, DecodeError> {
        if raw.len() == 4 {
            Ok(Self {
                flags: BridgeVlanInfoFlags::from_bits_retain(
                    parse_u16(&raw[0..2]).context(format!(
                        "Invalid IFLA_BRIDGE_VLAN_INFO value: {raw:?}"
                    ))?,
                ),
                vid: parse_u16(&raw[2..4]).context(format!(
                    "Invalid IFLA_BRIDGE_VLAN_INFO value: {raw:?}"
                ))?,
            })
        } else {
            Err(DecodeError::from(format!(
                "Invalid IFLA_BRIDGE_VLAN_INFO value, expecting [u8;4], \
                but got {raw:?}"
            )))
        }
    }
}

// kernel constant name is BRIDGE_FLAGS_MASTER
const BRIDGE_FLAGS_CONTROLLER: u16 = 1;
const BRIDGE_FLAGS_SELF: u16 = 2;

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum BridgeFlag {
    /// Bridge command to/from controller
    Controller,
    /// Bridge command to/from lowerdev
    LowerDev,
    Other(u16),
}

impl From<u16> for BridgeFlag {
    fn from(d: u16) -> Self {
        match d {
            BRIDGE_FLAGS_CONTROLLER => Self::Controller,
            BRIDGE_FLAGS_SELF => Self::LowerDev,
            _ => Self::Other(d),
        }
    }
}

impl From<BridgeFlag> for u16 {
    fn from(v: BridgeFlag) -> u16 {
        match v {
            BridgeFlag::Controller => BRIDGE_FLAGS_CONTROLLER,
            BridgeFlag::LowerDev => BRIDGE_FLAGS_SELF,
            BridgeFlag::Other(d) => d,
        }
    }
}

impl BridgeFlag {
    pub const LENGTH: usize = 2;
}

const BRIDGE_MODE_VEB: u16 = 0;
const BRIDGE_MODE_VEPA: u16 = 1;

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum BridgeMode {
    /// Default loopback mode
    Veb,
    /// 802.1Qbg defined VEPA mode
    Vepa,
    Other(u16),
}

impl From<u16> for BridgeMode {
    fn from(d: u16) -> Self {
        match d {
            BRIDGE_MODE_VEB => Self::Veb,
            BRIDGE_MODE_VEPA => Self::Vepa,
            _ => Self::Other(d),
        }
    }
}

impl From<BridgeMode> for u16 {
    fn from(v: BridgeMode) -> u16 {
        match v {
            BridgeMode::Veb => BRIDGE_MODE_VEB,
            BridgeMode::Vepa => BRIDGE_MODE_VEPA,
            BridgeMode::Other(d) => d,
        }
    }
}

impl BridgeMode {
    pub const LENGTH: usize = 2;
}

const IFLA_BRIDGE_VLAN_TUNNEL_ID: u16 = 1;
const IFLA_BRIDGE_VLAN_TUNNEL_VID: u16 = 2;
const IFLA_BRIDGE_VLAN_TUNNEL_FLAGS: u16 = 3;

#[derive(Clone, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub enum BridgeVlanTunnelInfo {
    Id(u32),
    Vid(u16),
    Flags(BridgeVlanInfoFlags),
    Other(DefaultNla),
}

impl Nla for BridgeVlanTunnelInfo {
    fn value_len(&self) -> usize {
        match self {
            Self::Id(_) => 4,
            Self::Vid(_) => 2,
            Self::Flags(_) => BridgeVlanInfoFlags::LENGTH,
            Self::Other(nla) => nla.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Id(v) => NativeEndian::write_u32(buffer, *v),
            Self::Vid(v) => NativeEndian::write_u16(buffer, *v),
            Self::Flags(value) => NativeEndian::write_u16(buffer, value.bits()),
            Self::Other(nla) => nla.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            Self::Id(_) => IFLA_BRIDGE_VLAN_TUNNEL_ID,
            Self::Vid(_) => IFLA_BRIDGE_VLAN_TUNNEL_VID,
            Self::Flags(_) => IFLA_BRIDGE_VLAN_TUNNEL_FLAGS,
            Self::Other(nla) => nla.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
    for BridgeVlanTunnelInfo
{
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();
        Ok(match buf.kind() {
            IFLA_BRIDGE_VLAN_TUNNEL_ID => {
                Self::Id(parse_u32(payload).context(format!(
                    "Invalid IFLA_BRIDGE_VLAN_TUNNEL_ID {payload:?}"
                ))?)
            }
            IFLA_BRIDGE_VLAN_TUNNEL_VID => {
                Self::Vid(parse_u16(payload).context(format!(
                    "Invalid IFLA_BRIDGE_VLAN_TUNNEL_VID {payload:?}"
                ))?)
            }
            IFLA_BRIDGE_VLAN_TUNNEL_FLAGS => {
                Self::Flags(BridgeVlanInfoFlags::from_bits_retain(
                    parse_u16(payload).context(format!(
                        "Invalid IFLA_BRIDGE_VLAN_TUNNEL_VID {payload:?}"
                    ))?,
                ))
            }
            _ => {
                Self::Other(DefaultNla::parse(buf).context("Unknown NLA type")?)
            }
        })
    }
}