Skip to content

Commit c86090f

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 c86090f

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/route/builder.rs

Lines changed: 16 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,13 @@ impl RouteMessageBuilder<Ipv4Addr> {
168168
.push(RouteAttribute::Gateway(RouteAddress::Inet(addr)));
169169
self
170170
}
171+
172+
pub fn gateway6(mut self, addr: Ipv6Addr) -> Self {
173+
self.message
174+
.attributes
175+
.push(RouteAttribute::Via(RouteVia::Inet6(addr)));
176+
self
177+
}
171178
}
172179

173180
impl Default for RouteMessageBuilder<Ipv4Addr> {
@@ -360,25 +367,15 @@ impl RouteMessageBuilder<IpAddr> {
360367
mut self,
361368
addr: IpAddr,
362369
) -> 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-
};
370+
use AddressFamily::*;
371+
let attr = match (self.message.header.address_family, addr) {
372+
(Inet, addr @ IpAddr::V4(_)) | (Inet6, addr @ IpAddr::V6(_)) => {
373+
RouteAttribute::Gateway(addr.into())
369374
}
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()));
375+
(Inet, IpAddr::V6(v6)) => RouteAttribute::Via(RouteVia::Inet6(v6)),
376+
(af, _) => return Err(InvalidRouteMessage::AddressFamily(af)),
377+
};
378+
self.message.attributes.push(attr);
382379
Ok(self)
383380
}
384381

0 commit comments

Comments
 (0)