Skip to content

Commit

Permalink
Adding vxlan to pnet_packet
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedoyle committed Oct 18, 2023
1 parent 87f362d commit 4052801
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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[..]);
}

0 comments on commit 4052801

Please sign in to comment.