Skip to content

Commit ea13ce2

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 30aa30f commit ea13ce2

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/route/builder.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use netlink_packet_route::{
99
route::{
1010
RouteAddress, RouteAttribute, RouteFlags, RouteHeader, RouteMessage,
11-
RouteProtocol, RouteScope, RouteType,
11+
RouteProtocol, RouteScope, RouteType, RouteVia,
1212
},
1313
AddressFamily,
1414
};
@@ -168,6 +168,14 @@ impl RouteMessageBuilder<Ipv4Addr> {
168168
.push(RouteAttribute::Gateway(RouteAddress::Inet(addr)));
169169
self
170170
}
171+
172+
/// Sets the IPv6 gateway (via) address.
173+
pub fn via(mut self, addr: Ipv6Addr) -> Self {
174+
self.message
175+
.attributes
176+
.push(RouteAttribute::Via(RouteVia::Inet6(addr)));
177+
self
178+
}
171179
}
172180

173181
impl Default for RouteMessageBuilder<Ipv4Addr> {
@@ -360,25 +368,15 @@ impl RouteMessageBuilder<IpAddr> {
360368
mut self,
361369
addr: IpAddr,
362370
) -> Result<Self, InvalidRouteMessage> {
363-
self.set_address_family_from_ip_addr(addr);
364-
match self.message.header.address_family {
365-
AddressFamily::Inet => {
366-
if addr.is_ipv6() {
367-
return Err(InvalidRouteMessage::Gateway(addr));
368-
};
371+
use AddressFamily::*;
372+
let attr = match (self.message.header.address_family, addr) {
373+
(Inet, addr @ IpAddr::V4(_)) | (Inet6, addr @ IpAddr::V6(_)) => {
374+
RouteAttribute::Gateway(addr.into())
369375
}
370-
AddressFamily::Inet6 => {
371-
if addr.is_ipv4() {
372-
return Err(InvalidRouteMessage::Gateway(addr));
373-
};
374-
}
375-
af => {
376-
return Err(InvalidRouteMessage::AddressFamily(af));
377-
}
378-
}
379-
self.message
380-
.attributes
381-
.push(RouteAttribute::Gateway(addr.into()));
376+
(Inet, IpAddr::V6(v6)) => RouteAttribute::Via(RouteVia::Inet6(v6)),
377+
(af, _) => return Err(InvalidRouteMessage::AddressFamily(af)),
378+
};
379+
self.message.attributes.push(attr);
382380
Ok(self)
383381
}
384382

0 commit comments

Comments
 (0)