Skip to content

Commit ddabf52

Browse files
committed
route: allow IPv6 gateway in IPv4 route
Linux supports it by using RTA_VIA instead of RTA_GATEWAY. Since it essentially acts as gateway, we extend the current API to allow it.
1 parent e1184c0 commit ddabf52

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/route/builder.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ impl RouteMessageBuilder<Ipv4Addr> {
211211
.push(RouteAttribute::Gateway(RouteAddress::Inet(addr)));
212212
self
213213
}
214+
215+
/// Sets the IPv6 gateway (via) address.
216+
pub fn via(mut self, addr: Ipv6Addr) -> Self {
217+
self.message
218+
.attributes
219+
.push(RouteAttribute::Via(RouteVia::Inet6(addr)));
220+
self
221+
}
214222
}
215223

216224
impl Default for RouteMessageBuilder<Ipv4Addr> {
@@ -403,25 +411,15 @@ impl RouteMessageBuilder<IpAddr> {
403411
mut self,
404412
addr: IpAddr,
405413
) -> Result<Self, InvalidRouteMessage> {
406-
self.set_address_family_from_ip_addr(addr);
407-
match self.message.header.address_family {
408-
AddressFamily::Inet => {
409-
if addr.is_ipv6() {
410-
return Err(InvalidRouteMessage::Gateway(addr));
411-
};
412-
}
413-
AddressFamily::Inet6 => {
414-
if addr.is_ipv4() {
415-
return Err(InvalidRouteMessage::Gateway(addr));
416-
};
417-
}
418-
af => {
419-
return Err(InvalidRouteMessage::AddressFamily(af));
414+
use AddressFamily::*;
415+
let attr = match (self.message.header.address_family, addr) {
416+
(Inet, addr @ IpAddr::V4(_)) | (Inet6, addr @ IpAddr::V6(_)) => {
417+
RouteAttribute::Gateway(addr.into())
420418
}
421-
}
422-
self.message
423-
.attributes
424-
.push(RouteAttribute::Gateway(addr.into()));
419+
(Inet, IpAddr::V6(v6)) => RouteAttribute::Via(RouteVia::Inet6(v6)),
420+
(af, _) => return Err(InvalidRouteMessage::AddressFamily(af)),
421+
};
422+
self.message.attributes.push(attr);
425423
Ok(self)
426424
}
427425

0 commit comments

Comments
 (0)