Skip to content

Commit afb6018

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 afb6018

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

src/route/builder.rs

Lines changed: 9 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
};
@@ -360,25 +360,15 @@ impl RouteMessageBuilder<IpAddr> {
360360
mut self,
361361
addr: IpAddr,
362362
) -> 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-
};
369-
}
370-
AddressFamily::Inet6 => {
371-
if addr.is_ipv4() {
372-
return Err(InvalidRouteMessage::Gateway(addr));
373-
};
374-
}
375-
af => {
376-
return Err(InvalidRouteMessage::AddressFamily(af));
363+
use AddressFamily::*;
364+
let attr = match (self.message.header.address_family, addr) {
365+
(Inet, addr @ IpAddr::V4(_)) | (Inet6, addr @ IpAddr::V6(_)) => {
366+
RouteAttribute::Gateway(addr.into())
377367
}
378-
}
379-
self.message
380-
.attributes
381-
.push(RouteAttribute::Gateway(addr.into()));
368+
(Inet, IpAddr::V6(v6)) => RouteAttribute::Via(RouteVia::Inet6(v6)),
369+
(af, _) => return Err(InvalidRouteMessage::AddressFamily(af)),
370+
};
371+
self.message.attributes.push(attr);
382372
Ok(self)
383373
}
384374

0 commit comments

Comments
 (0)