Skip to content

Commit

Permalink
Merge pull request #984 from thvdveld/use-own-src-address
Browse files Browse the repository at this point in the history
Use own source address for ARP and NDISC Solicitations
  • Loading branch information
Dirbaio authored Sep 11, 2024
2 parents bc22929 + c76f32f commit 8025752
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
30 changes: 10 additions & 20 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,6 @@ impl InterfaceInner {
fn lookup_hardware_addr<Tx>(
&mut self,
tx_token: Tx,
src_addr: &IpAddress,
dst_addr: &IpAddress,
fragmenter: &mut Fragmenter,
) -> Result<(HardwareAddress, Tx), DispatchError>
Expand Down Expand Up @@ -962,11 +961,9 @@ impl InterfaceInner {
_ => (), // XXX
}

match (src_addr, dst_addr) {
match dst_addr {
#[cfg(all(feature = "medium-ethernet", feature = "proto-ipv4"))]
(&IpAddress::Ipv4(src_addr), IpAddress::Ipv4(dst_addr))
if matches!(self.caps.medium, Medium::Ethernet) =>
{
IpAddress::Ipv4(dst_addr) if matches!(self.caps.medium, Medium::Ethernet) => {
net_debug!(
"address {} not in neighbor cache, sending ARP request",
dst_addr
Expand All @@ -976,7 +973,9 @@ impl InterfaceInner {
let arp_repr = ArpRepr::EthernetIpv4 {
operation: ArpOperation::Request,
source_hardware_addr: src_hardware_addr,
source_protocol_addr: src_addr,
source_protocol_addr: self
.get_source_address_ipv4(&dst_addr)
.ok_or(DispatchError::NoRoute)?,
target_hardware_addr: EthernetAddress::BROADCAST,
target_protocol_addr: dst_addr,
};
Expand All @@ -995,7 +994,7 @@ impl InterfaceInner {
}

#[cfg(feature = "proto-ipv6")]
(&IpAddress::Ipv6(src_addr), IpAddress::Ipv6(dst_addr)) => {
IpAddress::Ipv6(dst_addr) => {
net_debug!(
"address {} not in neighbor cache, sending Neighbor Solicitation",
dst_addr
Expand All @@ -1008,7 +1007,7 @@ impl InterfaceInner {

let packet = Packet::new_ipv6(
Ipv6Repr {
src_addr,
src_addr: self.get_source_address_ipv6(&dst_addr),
dst_addr: dst_addr.solicited_node(),
next_header: IpProtocol::Icmpv6,
payload_len: solicit.buffer_len(),
Expand Down Expand Up @@ -1055,12 +1054,8 @@ impl InterfaceInner {

#[cfg(feature = "medium-ieee802154")]
if matches!(self.caps.medium, Medium::Ieee802154) {
let (addr, tx_token) = self.lookup_hardware_addr(
tx_token,
&ip_repr.src_addr(),
&ip_repr.dst_addr(),
frag,
)?;
let (addr, tx_token) =
self.lookup_hardware_addr(tx_token, &ip_repr.dst_addr(), frag)?;
let addr = addr.ieee802154_or_panic();

self.dispatch_ieee802154(addr, tx_token, meta, packet, frag);
Expand All @@ -1087,12 +1082,7 @@ impl InterfaceInner {
#[cfg(feature = "medium-ethernet")]
let (dst_hardware_addr, mut tx_token) = match self.caps.medium {
Medium::Ethernet => {
match self.lookup_hardware_addr(
tx_token,
&ip_repr.src_addr(),
&ip_repr.dst_addr(),
frag,
)? {
match self.lookup_hardware_addr(tx_token, &ip_repr.dst_addr(), frag)? {
(HardwareAddress::Ethernet(addr), tx_token) => (addr, tx_token),
(_, _) => unreachable!(),
}
Expand Down
3 changes: 0 additions & 3 deletions src/iface/interface/tests/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ fn test_handle_valid_arp_request(#[case] medium: Medium) {
assert_eq!(
iface.inner.lookup_hardware_addr(
MockTxToken,
&IpAddress::Ipv4(local_ip_addr),
&IpAddress::Ipv4(remote_ip_addr),
&mut iface.fragmenter,
),
Expand Down Expand Up @@ -505,7 +504,6 @@ fn test_handle_other_arp_request(#[case] medium: Medium) {
assert_eq!(
iface.inner.lookup_hardware_addr(
MockTxToken,
&IpAddress::Ipv4(Ipv4Address([0x7f, 0x00, 0x00, 0x01])),
&IpAddress::Ipv4(remote_ip_addr),
&mut iface.fragmenter,
),
Expand Down Expand Up @@ -564,7 +562,6 @@ fn test_arp_flush_after_update_ip(#[case] medium: Medium) {
assert_eq!(
iface.inner.lookup_hardware_addr(
MockTxToken,
&IpAddress::Ipv4(local_ip_addr),
&IpAddress::Ipv4(remote_ip_addr),
&mut iface.fragmenter,
),
Expand Down
1 change: 0 additions & 1 deletion src/iface/interface/tests/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,6 @@ fn test_handle_valid_ndisc_request(#[case] medium: Medium) {
assert_eq!(
iface.inner.lookup_hardware_addr(
MockTxToken,
&IpAddress::Ipv6(local_ip_addr),
&IpAddress::Ipv6(remote_ip_addr),
&mut iface.fragmenter,
),
Expand Down

0 comments on commit 8025752

Please sign in to comment.