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

use std::net::{Ipv4Addr, Ipv6Addr};

use anyhow::Context;
use byteorder::{BigEndian, ByteOrder, NativeEndian};
use netlink_packet_utils::{
    nla::{DefaultNla, Nla, NlaBuffer},
    parsers::{parse_u16_be, parse_u32, parse_u8},
    traits::Parseable,
    DecodeError,
};

const IFLA_VXLAN_ID: u16 = 1;
const IFLA_VXLAN_GROUP: u16 = 2;
const IFLA_VXLAN_LINK: u16 = 3;
const IFLA_VXLAN_LOCAL: u16 = 4;
const IFLA_VXLAN_TTL: u16 = 5;
const IFLA_VXLAN_TOS: u16 = 6;
const IFLA_VXLAN_LEARNING: u16 = 7;
const IFLA_VXLAN_AGEING: u16 = 8;
const IFLA_VXLAN_LIMIT: u16 = 9;
const IFLA_VXLAN_PORT_RANGE: u16 = 10;
const IFLA_VXLAN_PROXY: u16 = 11;
const IFLA_VXLAN_RSC: u16 = 12;
const IFLA_VXLAN_L2MISS: u16 = 13;
const IFLA_VXLAN_L3MISS: u16 = 14;
const IFLA_VXLAN_PORT: u16 = 15;
const IFLA_VXLAN_GROUP6: u16 = 16;
const IFLA_VXLAN_LOCAL6: u16 = 17;
const IFLA_VXLAN_UDP_CSUM: u16 = 18;
const IFLA_VXLAN_UDP_ZERO_CSUM6_TX: u16 = 19;
const IFLA_VXLAN_UDP_ZERO_CSUM6_RX: u16 = 20;
const IFLA_VXLAN_REMCSUM_TX: u16 = 21;
const IFLA_VXLAN_REMCSUM_RX: u16 = 22;
const IFLA_VXLAN_GBP: u16 = 23;
const IFLA_VXLAN_REMCSUM_NOPARTIAL: u16 = 24;
const IFLA_VXLAN_COLLECT_METADATA: u16 = 25;
const IFLA_VXLAN_LABEL: u16 = 26;
const IFLA_VXLAN_GPE: u16 = 27;
const IFLA_VXLAN_TTL_INHERIT: u16 = 28;
const IFLA_VXLAN_DF: u16 = 29;
const IFLA_VXLAN_VNIFILTER: u16 = 30;
const IFLA_VXLAN_LOCALBYPASS: u16 = 31;

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum InfoVxlan {
    Id(u32),
    Group(Ipv4Addr),
    Group6(Ipv6Addr),
    Link(u32),
    Local(Ipv4Addr),
    Local6(Ipv6Addr),
    Tos(u8),
    Ttl(u8),
    Label(u32),
    Learning(bool),
    Ageing(u32),
    Limit(u32),
    PortRange((u16, u16)),
    Proxy(bool),
    Rsc(bool),
    L2Miss(bool),
    L3Miss(bool),
    CollectMetadata(bool),
    Port(u16),
    UDPCsum(bool),
    UDPZeroCsumTX(bool),
    UDPZeroCsumRX(bool),
    RemCsumTX(bool),
    RemCsumRX(bool),
    Gbp(bool),
    Gpe(bool),
    RemCsumNoPartial(bool),
    TtlInherit(bool),
    Df(u8),
    Vnifilter(bool),
    Localbypass(bool),
    Other(DefaultNla),
}

impl Nla for InfoVxlan {
    fn value_len(&self) -> usize {
        match self {
            Self::Tos(_)
            | Self::Ttl(_)
            | Self::Learning(_)
            | Self::Proxy(_)
            | Self::Rsc(_)
            | Self::L2Miss(_)
            | Self::L3Miss(_)
            | Self::CollectMetadata(_)
            | Self::UDPCsum(_)
            | Self::UDPZeroCsumTX(_)
            | Self::UDPZeroCsumRX(_)
            | Self::RemCsumTX(_)
            | Self::RemCsumRX(_)
            | Self::TtlInherit(_)
            | Self::Df(_)
            | Self::Vnifilter(_)
            | Self::Localbypass(_) => 1,
            Self::Gbp(_) | Self::Gpe(_) | Self::RemCsumNoPartial(_) => 0,
            Self::Port(_) => 2,
            Self::Id(_)
            | Self::Label(_)
            | Self::Link(_)
            | Self::Ageing(_)
            | Self::Limit(_)
            | Self::PortRange(_)
            | Self::Group(_)
            | Self::Local(_) => 4,
            Self::Group6(_) | Self::Local6(_) => 16,
            Self::Other(nla) => nla.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Id(value)
            | Self::Label(value)
            | Self::Link(value)
            | Self::Ageing(value)
            | Self::Limit(value) => NativeEndian::write_u32(buffer, *value),
            Self::Gbp(_value)
            | Self::Gpe(_value)
            | Self::RemCsumNoPartial(_value) => (),
            Self::Tos(value) | Self::Ttl(value) | Self::Df(value) => {
                buffer[0] = *value
            }
            Self::Vnifilter(value)
            | Self::Localbypass(value)
            | Self::Learning(value)
            | Self::Proxy(value)
            | Self::Rsc(value)
            | Self::L2Miss(value)
            | Self::L3Miss(value)
            | Self::CollectMetadata(value)
            | Self::UDPCsum(value)
            | Self::UDPZeroCsumTX(value)
            | Self::UDPZeroCsumRX(value)
            | Self::RemCsumTX(value)
            | Self::RemCsumRX(value)
            | Self::TtlInherit(value) => buffer[0] = *value as u8,
            Self::Group(value) | Self::Local(value) => {
                buffer.copy_from_slice(&value.octets())
            }
            Self::Group6(value) | Self::Local6(value) => {
                buffer.copy_from_slice(&value.octets())
            }
            Self::Port(value) => BigEndian::write_u16(buffer, *value),
            Self::PortRange(range) => {
                BigEndian::write_u16(buffer, range.0);
                BigEndian::write_u16(&mut buffer[2..], range.1)
            }
            Self::Other(nla) => nla.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            Self::Id(_) => IFLA_VXLAN_ID,
            Self::Group(_) => IFLA_VXLAN_GROUP,
            Self::Group6(_) => IFLA_VXLAN_GROUP6,
            Self::Link(_) => IFLA_VXLAN_LINK,
            Self::Local(_) => IFLA_VXLAN_LOCAL,
            Self::Local6(_) => IFLA_VXLAN_LOCAL6,
            Self::Tos(_) => IFLA_VXLAN_TOS,
            Self::Ttl(_) => IFLA_VXLAN_TTL,
            Self::Label(_) => IFLA_VXLAN_LABEL,
            Self::Learning(_) => IFLA_VXLAN_LEARNING,
            Self::Ageing(_) => IFLA_VXLAN_AGEING,
            Self::Limit(_) => IFLA_VXLAN_LIMIT,
            Self::PortRange(_) => IFLA_VXLAN_PORT_RANGE,
            Self::Proxy(_) => IFLA_VXLAN_PROXY,
            Self::Rsc(_) => IFLA_VXLAN_RSC,
            Self::L2Miss(_) => IFLA_VXLAN_L2MISS,
            Self::L3Miss(_) => IFLA_VXLAN_L3MISS,
            Self::CollectMetadata(_) => IFLA_VXLAN_COLLECT_METADATA,
            Self::Port(_) => IFLA_VXLAN_PORT,
            Self::UDPCsum(_) => IFLA_VXLAN_UDP_CSUM,
            Self::UDPZeroCsumTX(_) => IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
            Self::UDPZeroCsumRX(_) => IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
            Self::RemCsumTX(_) => IFLA_VXLAN_REMCSUM_TX,
            Self::RemCsumRX(_) => IFLA_VXLAN_REMCSUM_RX,
            Self::Gbp(_) => IFLA_VXLAN_GBP,
            Self::Gpe(_) => IFLA_VXLAN_GPE,
            Self::RemCsumNoPartial(_) => IFLA_VXLAN_REMCSUM_NOPARTIAL,
            Self::TtlInherit(_) => IFLA_VXLAN_TTL_INHERIT,
            Self::Df(_) => IFLA_VXLAN_DF,
            Self::Vnifilter(_) => IFLA_VXLAN_VNIFILTER,
            Self::Localbypass(_) => IFLA_VXLAN_LOCALBYPASS,
            Self::Other(nla) => nla.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoVxlan {
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();
        Ok(match buf.kind() {
            IFLA_VXLAN_ID => {
                Self::Id(parse_u32(payload).context("invalid IFLA_VXLAN_ID value")?)
            }
            IFLA_VXLAN_GROUP => {
                if payload.len() == 4 {
                    let mut data = [0u8; 4];
                    data.copy_from_slice(&payload[0..4]);
                    Self::Group(Ipv4Addr::from(data))
                } else {
                    return Err(DecodeError::from(format!(
                        "Invalid IFLA_VXLAN_GROUP, got unexpected length \
                        of IPv4 address payload {:?}",
                        payload
                    )));
                }
            }
            IFLA_VXLAN_LOCAL => {
                if payload.len() == 4 {
                    let mut data = [0u8; 4];
                    data.copy_from_slice(&payload[0..4]);
                    Self::Local(Ipv4Addr::from(data))
                } else {
                    return Err(DecodeError::from(format!(
                        "Invalid IFLA_VXLAN_LOCAL, got unexpected length \
                        of IPv4 address payload {:?}",
                        payload
                    )));
                }
            }
            IFLA_VXLAN_GROUP6 => {
                if payload.len() == 16 {
                    let mut data = [0u8; 16];
                    data.copy_from_slice(&payload[0..16]);
                    Self::Group6(Ipv6Addr::from(data))
                } else {
                    return Err(DecodeError::from(format!(
                        "Invalid IFLA_VXLAN_GROUP6, got unexpected length \
                        of IPv6 address payload {:?}",
                        payload
                    )));
                }
            },
            IFLA_VXLAN_LOCAL6 => {
                if payload.len() == 16 {
                    let mut data = [0u8; 16];
                    data.copy_from_slice(&payload[0..16]);
                    Self::Local6(Ipv6Addr::from(data))
                } else {
                    return Err(DecodeError::from(format!(
                        "Invalid IFLA_VXLAN_LOCAL6, got unexpected length \
                        of IPv6 address payload {:?}",
                        payload
                    )));
                }
            },
            IFLA_VXLAN_LINK => Self::Link(
                parse_u32(payload).context("invalid IFLA_VXLAN_LINK value")?,
            ),
            IFLA_VXLAN_TOS => {
                Self::Tos(parse_u8(payload)
                    .context("invalid IFLA_VXLAN_TOS value")?)
            }
            IFLA_VXLAN_TTL => {
                Self::Ttl(parse_u8(payload)
                    .context("invalid IFLA_VXLAN_TTL value")?)
            }
            IFLA_VXLAN_LABEL => Self::Label(
                parse_u32(payload).context("invalid IFLA_VXLAN_LABEL value")?,
            ),
            IFLA_VXLAN_LEARNING => Self::Learning(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_LEARNING value")? > 0,
            ),
            IFLA_VXLAN_AGEING => Self::Ageing(
                parse_u32(payload)
                    .context("invalid IFLA_VXLAN_AGEING value")?,
            ),
            IFLA_VXLAN_LIMIT => Self::Limit(
                parse_u32(payload).context("invalid IFLA_VXLAN_LIMIT value")?,
            ),
            IFLA_VXLAN_PROXY => Self::Proxy(
                parse_u8(payload).context("invalid IFLA_VXLAN_PROXY value")? > 0,
            ),
            IFLA_VXLAN_RSC => {
                Self::Rsc(parse_u8(payload)
                    .context("invalid IFLA_VXLAN_RSC value")?> 0)
            }
            IFLA_VXLAN_L2MISS => Self::L2Miss(
                parse_u8(payload).context("invalid IFLA_VXLAN_L2MISS value")? > 0,
            ),
            IFLA_VXLAN_L3MISS => Self::L3Miss(
                parse_u8(payload).context("invalid IFLA_VXLAN_L3MISS value")? > 0,
            ),
            IFLA_VXLAN_COLLECT_METADATA => Self::CollectMetadata(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_COLLECT_METADATA value")? >0,
            ),
            IFLA_VXLAN_PORT_RANGE => {
                let err = "invalid IFLA_VXLAN_PORT value";
                if payload.len() != 4 {
                    return Err(err.into());
                }
                let low = parse_u16_be(&payload[0..2]).context(err)?;
                let high = parse_u16_be(&payload[2..]).context(err)?;
                Self::PortRange((low, high))
            }
            IFLA_VXLAN_PORT => Self::Port(
                parse_u16_be(payload)
                    .context("invalid IFLA_VXLAN_PORT value")?,
            ),
            IFLA_VXLAN_UDP_CSUM => Self::UDPCsum(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_UDP_CSUM value")? > 0,
            ),
            IFLA_VXLAN_UDP_ZERO_CSUM6_TX => Self::UDPZeroCsumTX(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_UDP_ZERO_CSUM6_TX value")? > 0,
            ),
            IFLA_VXLAN_UDP_ZERO_CSUM6_RX => Self::UDPZeroCsumRX(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_UDP_ZERO_CSUM6_RX value")? > 0,
            ),
            IFLA_VXLAN_REMCSUM_TX => Self::RemCsumTX(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_REMCSUM_TX value")? > 0,
            ),
            IFLA_VXLAN_REMCSUM_RX => Self::RemCsumRX(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_REMCSUM_RX value")? > 0,
            ),
            IFLA_VXLAN_DF => {
                Self::Df(parse_u8(payload).context("invalid IFLA_VXLAN_DF value")?)
            }
            IFLA_VXLAN_GBP => {
                Self::Gbp(true)
            }
            IFLA_VXLAN_GPE => {
                Self::Gpe(true)
            }
            IFLA_VXLAN_REMCSUM_NOPARTIAL => Self::RemCsumNoPartial(true),
            IFLA_VXLAN_TTL_INHERIT => Self::TtlInherit(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_TTL_INHERIT value")? > 0,
            ),
            IFLA_VXLAN_VNIFILTER => Self::Vnifilter(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_VNIFILTER value")? > 0,
            ),
            IFLA_VXLAN_LOCALBYPASS => Self::Localbypass(
                parse_u8(payload)
                    .context("invalid IFLA_VXLAN_LOCALBYPASS value")? > 0,
            ),
            unknown_kind => Self::Other(DefaultNla::parse(buf).context(format!(
                "Failed to parse IFLA_INFO_DATA(vxlan) NLA type: {unknown_kind} as DefaultNla"
            ))?),
        })
    }
}