Skip to content

Commit 927bdd7

Browse files
maiquebcathay4t
authored andcommitted
vxlan: fix port attribute endianness
We need to use big endian to encode the port number, just like done in golang's netlink implementation - [0]. In [1] you can see the host to network function implementation, which is used by [0]. [0] - https://github.com/vishvananda/netlink/blob/1b5637395dd8baed87f2fef5c2ad4b771fa32fd9/link_linux.go#L1169 [1] - https://github.com/vishvananda/netlink/blob/a3f0be63522b2d1827372f2510eb6e53477960c4/order.go#L22 Signed-off-by: Miguel Duarte Barroso <[email protected]>
1 parent 0241ea4 commit 927bdd7

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/rtnl/link/nlas/link_infos.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22

33
use anyhow::Context;
4-
use byteorder::{ByteOrder, NativeEndian};
4+
use byteorder::{BigEndian, ByteOrder, NativeEndian};
55
use netlink_packet_utils::{
66
nla::{DefaultNla, Nla, NlaBuffer, NlasIterator},
77
parsers::{
@@ -779,7 +779,7 @@ impl Nla for InfoVxlan {
779779
| Group6(ref value)
780780
| Local6(ref value)
781781
=> buffer.copy_from_slice(value.as_slice()),
782-
Port(ref value) => NativeEndian::write_u16(buffer, *value),
782+
Port(ref value) => BigEndian::write_u16(buffer, *value),
783783
PortRange(ref range) => {
784784
NativeEndian::write_u16(buffer, range.0);
785785
NativeEndian::write_u16(buffer, range.1)
@@ -2450,4 +2450,52 @@ mod tests {
24502450
nlas.as_slice().emit(&mut vec);
24512451
assert_eq!(&vec[..], &VLAN[..]);
24522452
}
2453+
2454+
#[rustfmt::skip]
2455+
static VXLAN: [u8; 32] = [
2456+
0x0a, 0x00, // length = 10
2457+
0x01, 0x00, // type = 1 = IFLA_INFO_KIND
2458+
0x76, 0x78, 0x6C, 0x61, 0x6E, // V = "vxlan\0"
2459+
0x00, 0x00, 0x00, // padding
2460+
0x14, 0x00, // length = 20
2461+
0x02, 0x00, // type = 2 = IFLA_INFO_DATA
2462+
0x08, 0x00, // length - 8
2463+
0x01, 0x00, // type = 1 = IFLA_VXLAN_ID
2464+
0x0A, 0x00, // id = 0x0A = 10
2465+
0x00, 0x00, // padding
2466+
0x06, 0x00, // length = 6
2467+
0x0F, 0x00, // type = 15 = IFLA_VXLAN_PORT
2468+
0x12, 0xB5, // port = 4789
2469+
0x00, 0x00 // padding
2470+
];
2471+
2472+
lazy_static! {
2473+
static ref VXLAN_INFO: Vec<InfoVxlan> =
2474+
vec![InfoVxlan::Id(10), InfoVxlan::Port(4789),];
2475+
}
2476+
2477+
#[test]
2478+
fn parse_info_vxlan() {
2479+
let nla = NlaBuffer::new_checked(&VXLAN[..]).unwrap();
2480+
let parsed = VecInfo::parse(&nla).unwrap().0;
2481+
let expected = vec![
2482+
Info::Kind(InfoKind::Vxlan),
2483+
Info::Data(InfoData::Vxlan(VXLAN_INFO.clone())),
2484+
];
2485+
assert_eq!(expected, parsed);
2486+
}
2487+
2488+
#[test]
2489+
fn emit_info_vxlan() {
2490+
let nlas = vec![
2491+
Info::Kind(InfoKind::Vxlan),
2492+
Info::Data(InfoData::Vxlan(VXLAN_INFO.clone())),
2493+
];
2494+
2495+
assert_eq!(nlas.as_slice().buffer_len(), VXLAN.len());
2496+
2497+
let mut vec = vec![0xff; VXLAN.len()];
2498+
nlas.as_slice().emit(&mut vec);
2499+
assert_eq!(&vec[..], &VXLAN[..]);
2500+
}
24532501
}

0 commit comments

Comments
 (0)