-
Notifications
You must be signed in to change notification settings - Fork 63
Add support for LWTUNNEL_ENCAP_IP6 #148
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
use netlink_packet_core::{ | ||
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST, | ||
}; | ||
use netlink_packet_route::{route::RouteMessage, RouteNetlinkMessage}; | ||
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr}; | ||
|
||
fn main() { | ||
let mut socket = Socket::new(NETLINK_ROUTE).unwrap(); | ||
let _port_number = socket.bind_auto().unwrap().port_number(); | ||
socket.connect(&SocketAddr::new(0, 0)).unwrap(); | ||
|
||
let mut nl_hdr = NetlinkHeader::default(); | ||
nl_hdr.flags = NLM_F_REQUEST | NLM_F_DUMP; | ||
let mut packet = NetlinkMessage::new( | ||
nl_hdr, | ||
NetlinkPayload::from(RouteNetlinkMessage::GetRoute( | ||
RouteMessage::default(), | ||
)), | ||
); | ||
|
||
packet.finalize(); | ||
|
||
let mut buf = vec![0; packet.header.length as usize]; | ||
|
||
// Before calling serialize, it is important to check that the buffer in | ||
// which we're emitting is big enough for the packet, other | ||
// `serialize()` panics. | ||
|
||
assert!(buf.len() == packet.buffer_len()); | ||
|
||
packet.serialize(&mut buf[..]); | ||
|
||
println!(">>> {packet:?}"); | ||
if let Err(e) = socket.send(&buf[..], 0) { | ||
println!("SEND ERROR {e}"); | ||
} | ||
|
||
let mut receive_buffer = vec![0; 4096]; | ||
let mut offset = 0; | ||
|
||
// we set the NLM_F_DUMP flag so we expect a multipart rx_packet in | ||
// response. | ||
while let Ok(size) = socket.recv(&mut &mut receive_buffer[..], 0) { | ||
loop { | ||
let bytes = &receive_buffer[offset..]; | ||
let rx_packet = | ||
<NetlinkMessage<RouteNetlinkMessage>>::deserialize(bytes) | ||
.unwrap(); | ||
println!("<<< {rx_packet:?}"); | ||
|
||
if matches!(rx_packet.payload, NetlinkPayload::Done(_)) { | ||
println!("Done!"); | ||
return; | ||
} | ||
|
||
offset += rx_packet.header.length as usize; | ||
if offset == size || rx_packet.header.length == 0 { | ||
offset = 0; | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
use std::str::FromStr; | ||
|
||
use netlink_packet_utils::{Emitable, Parseable}; | ||
|
||
use crate::route::lwtunnel::RouteIp6TunnelFlags; | ||
use crate::route::{ | ||
RouteAttribute, RouteFlags, RouteHeader, RouteIp6Tunnel, RouteLwEnCapType, | ||
RouteLwTunnelEncap, RouteMessage, RouteMessageBuffer, RouteProtocol, | ||
RouteScope, RouteType, | ||
}; | ||
use crate::AddressFamily; | ||
|
||
// Setup: | ||
// ip link add dummy1 type dummy | ||
// ip link set dummy1 up | ||
// ip route add 192.0.2.0/24 encap ip6 \ | ||
// dst 2001:db8:1::1 src 2001:db8:1::2 \ | ||
// id 100 tc 7 hoplimit 253 csum dev dummy1 | ||
// wireshark capture(netlink message header removed) of nlmon against command: | ||
// ip route show dev dummy1 | ||
#[test] | ||
fn test_ip6_tunnel() { | ||
let raw = vec![ | ||
0x02, 0x18, 0x00, 0x00, 0xfe, 0x03, 0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, | ||
0x08, 0x00, 0x0f, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x00, | ||
0xc0, 0x00, 0x02, 0x00, 0x08, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, | ||
0x50, 0x00, 0x16, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x64, 0x14, 0x00, 0x02, 0x00, 0x20, 0x01, 0x0d, 0xb8, | ||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | ||
0x14, 0x00, 0x03, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x05, 0x00, 0x05, 0x00, | ||
0x07, 0x00, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, 0xfd, 0x00, 0x00, 0x00, | ||
0x06, 0x00, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x06, 0x00, 0x15, 0x00, | ||
0x04, 0x00, 0x00, 0x00, | ||
]; | ||
|
||
let expected = RouteMessage { | ||
header: RouteHeader { | ||
address_family: AddressFamily::Inet, | ||
destination_prefix_length: 24, | ||
source_prefix_length: 0, | ||
tos: 0, | ||
table: 254, | ||
protocol: RouteProtocol::Boot, | ||
scope: RouteScope::Link, | ||
kind: RouteType::Unicast, | ||
flags: RouteFlags::empty(), | ||
}, | ||
attributes: vec![ | ||
RouteAttribute::Table(254), | ||
RouteAttribute::Destination( | ||
Ipv4Addr::from_str("192.0.2.0").unwrap().into(), | ||
), | ||
RouteAttribute::Oif(8), | ||
RouteAttribute::Encap(vec![ | ||
RouteLwTunnelEncap::Ip6(RouteIp6Tunnel::Id(100)), | ||
RouteLwTunnelEncap::Ip6(RouteIp6Tunnel::Destination( | ||
Ipv6Addr::from_str("2001:db8:1::1").unwrap(), | ||
)), | ||
RouteLwTunnelEncap::Ip6(RouteIp6Tunnel::Source( | ||
Ipv6Addr::from_str("2001:db8:1::2").unwrap(), | ||
)), | ||
RouteLwTunnelEncap::Ip6(RouteIp6Tunnel::Tc(7)), | ||
RouteLwTunnelEncap::Ip6(RouteIp6Tunnel::Hoplimit(253)), | ||
RouteLwTunnelEncap::Ip6(RouteIp6Tunnel::Flags( | ||
RouteIp6TunnelFlags::Checksum, | ||
)), | ||
]), | ||
RouteAttribute::EncapType(RouteLwEnCapType::Ip6), | ||
], | ||
}; | ||
|
||
assert_eq!( | ||
expected, | ||
RouteMessage::parse(&RouteMessageBuffer::new(&raw)).unwrap() | ||
); | ||
|
||
let mut buf = vec![0; expected.buffer_len()]; | ||
|
||
expected.emit(&mut buf); | ||
|
||
assert_eq!(buf, raw); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.