@@ -4,7 +4,7 @@ use std::io::Write;
44
55use crate :: network:: dhcpv6:: * ;
66use futures:: stream:: TryStreamExt ;
7- use log:: { debug, info, warn} ;
7+ use log:: { debug, error , info, warn} ;
88use rtnetlink:: { new_connection, Handle , IpVersion } ;
99use std:: net:: Ipv6Addr ;
1010use tokio:: fs:: { read_link, OpenOptions } ;
@@ -22,9 +22,9 @@ use pnet::packet::udp::UdpPacket;
2222use pnet:: packet:: Packet ;
2323
2424use netlink_packet_route:: neighbour:: * ;
25- use netlink_packet_route:: route:: { RouteAddress , RouteAttribute } ;
25+ use netlink_packet_route:: route:: { RouteAddress , RouteAttribute , RouteType } ;
2626
27- const INTERFACE_NAME : & str = "eth0" ;
27+ pub const INTERFACE_NAME : & str = "eth0" ;
2828
2929pub async fn configure_network_devices ( ) -> Result < Option < ( Ipv6Addr , u8 ) > , String > {
3030 let ignore_ra_flag = true ; // Till the RA has the correct flags (O or M), ignore the flag
@@ -124,6 +124,19 @@ pub async fn configure_network_devices() -> Result<Option<(Ipv6Addr, u8)>, Strin
124124 delegated_prefix, prefix_length
125125 ) ;
126126 delegated_prefix_option = Some ( ( delegated_prefix, prefix_length) ) ;
127+ if let Err ( e) = add_ipv6_route (
128+ & handle,
129+ INTERFACE_NAME ,
130+ delegated_prefix,
131+ prefix_length,
132+ None ,
133+ 1024 ,
134+ RouteType :: Unreachable ,
135+ )
136+ . await
137+ {
138+ error ! ( "Failed to add unreachable IPv6 route: {}" , e) ;
139+ }
127140 } else {
128141 info ! ( "No prefix delegation received." ) ;
129142 }
@@ -227,36 +240,35 @@ async fn _print_ipv6_routes(
227240 }
228241 }
229242
230- RouteAttribute :: Gateway ( gw) => {
231- match gw {
232- RouteAddress :: Inet6 ( addr) => {
233- gateway = Some ( addr. to_string ( ) ) ;
234- debug ! ( "Parsed IPv6 Gateway: {}" , addr) ;
235- }
236- RouteAddress :: Other ( v) => {
237- // Some other form of gateway, handle if needed
238- if v. is_empty ( ) {
239- debug ! ( "Parsed Empty Gateway" ) ;
240- } else {
241- let hex_str = v
242- . iter ( )
243- . map ( |b| format ! ( "{:02x}" , b) )
244- . collect :: < Vec < String > > ( )
245- . join ( ":" ) ;
246- gateway = Some ( format ! ( "unknown({})" , hex_str) ) ;
247- debug ! ( "Parsed Unknown Gateway: {}" , hex_str) ;
248- }
249- }
250- _ => {
251- debug ! ( "Unhandled Gateway variant" ) ;
243+ RouteAttribute :: Gateway ( gw) => match gw {
244+ RouteAddress :: Inet6 ( addr) => {
245+ gateway = Some ( addr. to_string ( ) ) ;
246+ debug ! ( "Parsed IPv6 Gateway: {}" , addr) ;
247+ }
248+ RouteAddress :: Other ( v) => {
249+ if v. is_empty ( ) {
250+ debug ! ( "Parsed Empty Gateway" ) ;
251+ } else {
252+ let hex_str = v
253+ . iter ( )
254+ . map ( |b| format ! ( "{:02x}" , b) )
255+ . collect :: < Vec < String > > ( )
256+ . join ( ":" ) ;
257+ gateway = Some ( format ! ( "unknown({})" , hex_str) ) ;
258+ debug ! ( "Parsed Unknown Gateway: {}" , hex_str) ;
252259 }
253260 }
254- }
261+ _ => {
262+ debug ! ( "Unhandled Gateway variant" ) ;
263+ }
264+ } ,
255265 _ => { }
256266 }
257267 }
258268
259- if oif != Some ( iface_index) {
269+ let is_unreachable = route_msg. header . kind == RouteType :: Unreachable ;
270+
271+ if !is_unreachable && oif != Some ( iface_index) {
260272 debug ! (
261273 "Skipping route not associated with interface '{}'" ,
262274 interface_name
@@ -269,12 +281,29 @@ async fn _print_ipv6_routes(
269281 debug ! ( "Default route detected (no destination attribute)" ) ;
270282 }
271283
272- let dest_str = destination. unwrap_or_else ( || "unknown" . to_string ( ) ) ;
284+ let dest_str = destination. unwrap_or_else ( || {
285+ if is_unreachable {
286+ "unreachable" . to_string ( )
287+ } else {
288+ "unknown" . to_string ( )
289+ }
290+ } ) ;
291+
273292 let mut route_str = dest_str. to_string ( ) ;
293+
274294 if let Some ( gw) = gateway {
275295 route_str. push_str ( & format ! ( " via {}" , gw) ) ;
276296 }
277- route_str. push_str ( & format ! ( " dev {}" , interface_name) ) ;
297+
298+ if oif. is_some ( ) {
299+ if is_unreachable {
300+ route_str. push_str ( & format ! ( " dev {} [unreachable]" , interface_name) ) ;
301+ } else {
302+ route_str. push_str ( & format ! ( " dev {}" , interface_name) ) ;
303+ }
304+ } else if is_unreachable {
305+ route_str. push_str ( " [unreachable]" ) ;
306+ }
278307
279308 info ! ( "- {}" , route_str) ;
280309 }
0 commit comments