Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding vxlan to pnet_packet #654

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pnet_packet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default = ["std"]

[dev-dependencies]
hex = "0.4.3"
criterion = { version = "0.5.1", features = ["html_reports"] } #added HTML feature becuase of the annoying warnings when running the tests
criterion = { version = "0.5.1", features = ["html_reports"] } #added HTML feature because of the annoying warnings when running the tests

[build-dependencies]
glob = "0.3.1"
Expand Down
1 change: 1 addition & 0 deletions pnet_packet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ pub mod usbpcap;
pub mod vlan;
pub mod sll;
pub mod sll2;
pub mod vxlan;

pub mod util;
54 changes: 54 additions & 0 deletions pnet_packet/src/vxlan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2023 Stephen Doyle
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A VXLAN packet abstraction.

use alloc::vec::Vec;

use pnet_macros::packet;
use pnet_macros_support::types::*;

/// Virtual eXtensible Local Area Network (VXLAN)
///
/// See [RFC 7348](https://datatracker.ietf.org/doc/html/rfc7348)
///
/// VXLAN Header:
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |R|R|R|R|I|R|R|R| Reserved |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | VXLAN Network Identifier (VNI) | Reserved |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#[packet]
pub struct Vxlan {
pub flags: u8,
pub reserved1: u24be,
pub vni: u24be,
pub reserved2: u8,
#[payload]
pub payload: Vec<u8>,
}

#[test]
fn vxlan_packet_test() {
let mut packet = [0u8;8];
{
let mut vxlan_header = MutableVxlanPacket::new(&mut packet[..]).unwrap();
vxlan_header.set_flags(0x08);
assert_eq!(vxlan_header.get_flags(), 0x08);
vxlan_header.set_vni(0x123456);
assert_eq!(vxlan_header.get_vni(), 0x123456);
}

let ref_packet = [
0x08, // I flag
0x00, 0x00, 0x00, // Reserved
0x12, 0x34, 0x56, // VNI
0x00 // Reserved
];
assert_eq!(&ref_packet[..], &packet[..]);
}