Skip to content

Conversation

@kp-samuel-tam
Copy link
Member

@kp-samuel-tam kp-samuel-tam commented Dec 15, 2025

Description

This ensures correct route setup when Lightway client is connected to an IPv6 endpoint. The IPv4 tunnel stays unchanged.

Changes:

  • Use /128 prefix for IPv6 server routes (was incorrectly using /32)
  • Use /32 prefix for IPv4 server routes (unchanged)
  • Monitor IPv6 default route changes when server is IPv6, and IPv4 for IPv4
    • e.g. don't monitor IPv4 route changes when connected to an IPv6 server
  • Fix LAN mode to only use gateway if address family matches route
    • e.g. when connecting to IPv6 VPN server, don't do route add 192.168.0.0/16 via 2001:db8::1 (breaks)
  • Fix find_route to return proper error instead of panicking on None
    • nicer error, and don't assume a default route exists
  • Add mock IPv6 default route via loopback for test environments without IPv6
    • when testing environment has no IPv6 setup, just add a "mock" default route to ::1
    • we don't need a working default route, it works as long as the tests can look up a route for VPN server address
  • Add host_prefix_len() and same_ip_family() helper functions

Tests:

  • Add IPv6 and IPv4 test constants, update all tests to use them explicitly
  • Update create_test_setup() to accept server_ip parameter
  • Add test_ipv6_server_route_manager_creation()
  • Add test_privileged_ipv6_server_initialize_route_manager()
  • Add test_privileged_ipv6_server_route_update()
  • Mock route only looks for ::1 (not 127.0.0.1) when finding loopback

The tunnel interface remains IPv4-only (TUN_PEER_IP, TUN_DNS_IP), only the server connection endpoint supports IPv6.

Motivation and Context

Right now when connecting to an IPv6 endpoint, a route like 8321:8bec:666c:0305:9005:9f3e:e29f:3920/32 (which is functionally 8321:8bec::/32) will be established which is not correct; everything of 8321:8bec::/32 will leak. Only /128 should be correct.

How Has This Been Tested?

Wrote tests; if they work it works as intended.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • The correct base branch is being used, if not main

@kp-samuel-tam kp-samuel-tam requested a review from a team as a code owner December 15, 2025 11:04
@kp-samuel-tam kp-samuel-tam force-pushed the fix-ipv6-on-route-manager branch 4 times, most recently from cdd25a8 to 7d1c0c9 Compare December 16, 2025 05:54
@github-actions
Copy link

github-actions bot commented Dec 16, 2025

Code coverage summary for 2ec4f18:

Filename                                                     Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
lightway-app-utils/src/args/cipher.rs                              5                 5     0.00%           1                 1     0.00%           5                 5     0.00%           0                 0         -
lightway-app-utils/src/args/connection_type.rs                     5                 5     0.00%           1                 1     0.00%           5                 5     0.00%           0                 0         -
lightway-app-utils/src/args/duration.rs                           22                18    18.18%           5                 4    20.00%          15                12    20.00%           0                 0         -
lightway-app-utils/src/args/ip_map.rs                             19                19     0.00%           3                 3     0.00%          13                13     0.00%           0                 0         -
lightway-app-utils/src/args/logging.rs                            37                37     0.00%           3                 3     0.00%          31                31     0.00%           0                 0         -
lightway-app-utils/src/args/nonzero_duration.rs                   24                16    33.33%           4                 3    25.00%          16                10    37.50%           0                 0         -
lightway-app-utils/src/connection_ticker.rs                      236                24    89.83%          28                 4    85.71%         126                15    88.10%           0                 0         -
lightway-app-utils/src/dplpmtud_timer.rs                         212                16    92.45%          22                 4    81.82%         117                11    90.60%           0                 0         -
lightway-app-utils/src/event_stream.rs                            19                 0   100.00%           3                 0   100.00%          11                 0   100.00%           0                 0         -
lightway-app-utils/src/sockopt/ip_mtu_discover.rs                 78                78     0.00%           6                 6     0.00%          68                68     0.00%           0                 0         -
lightway-app-utils/src/sockopt/ip_pktinfo.rs                      14                14     0.00%           1                 1     0.00%          16                16     0.00%           0                 0         -
lightway-app-utils/src/tun.rs                                    281               281     0.00%          31                31     0.00%         175               175     0.00%           0                 0         -
lightway-app-utils/src/utils.rs                                   21                21     0.00%           1                 1     0.00%          11                11     0.00%           0                 0         -
lightway-client/src/args.rs                                       35                14    60.00%           2                 1    50.00%          17                 8    52.94%           0                 0         -
lightway-client/src/dns_manager.rs                                16                16     0.00%           3                 3     0.00%          11                11     0.00%           0                 0         -
lightway-client/src/io/inside/tun.rs                              78                78     0.00%          11                11     0.00%          57                57     0.00%           0                 0         -
lightway-client/src/io/outside/tcp.rs                             80                80     0.00%          11                11     0.00%          48                48     0.00%           0                 0         -
lightway-client/src/io/outside/udp.rs                            123               123     0.00%          13                13     0.00%          74                74     0.00%           0                 0         -
lightway-client/src/keepalive.rs                                 624                53    91.51%          55                 6    89.09%         330                25    92.42%           0                 0         -
lightway-client/src/lib.rs                                       796               660    17.09%          59                48    18.64%         550               460    16.36%           0                 0         -
lightway-client/src/main.rs                                      189               189     0.00%          12                12     0.00%         147               147     0.00%           0                 0         -
lightway-client/src/platform/linux/dns_manager.rs                144                64    55.56%          16                 6    62.50%          94                40    57.45%           0                 0         -
lightway-client/src/route_manager.rs                            1221               235    80.75%          77                 8    89.61%         671               124    81.52%           0                 0         -
lightway-core/src/borrowed_bytesmut.rs                           378                 1    99.74%          24                 0   100.00%         189                 1    99.47%           0                 0         -
lightway-core/src/builder_predicates.rs                           24                12    50.00%           4                 2    50.00%          24                12    50.00%           0                 0         -
lightway-core/src/cipher.rs                                       13                 0   100.00%           2                 0   100.00%          10                 0   100.00%           0                 0         -
lightway-core/src/connection.rs                                 1617               942    41.74%          70                30    57.14%        1127               625    44.54%           0                 0         -
lightway-core/src/connection/builders.rs                         247                49    80.16%          21                 8    61.90%         249                55    77.91%           0                 0         -
lightway-core/src/connection/dplpmtud.rs                        1755                95    94.59%          63                 0   100.00%         832                 9    98.92%           0                 0         -
lightway-core/src/connection/fragment_map.rs                     380                15    96.05%          25                 0   100.00%         258                 7    97.29%           0                 0         -
lightway-core/src/connection/io_adapter.rs                       529                26    95.09%          34                 5    85.29%         276                21    92.39%           0                 0         -
lightway-core/src/connection/key_update.rs                        34                13    61.76%           5                 0   100.00%          38                19    50.00%           0                 0         -
lightway-core/src/context.rs                                     202                51    74.75%          26                11    57.69%         213                64    69.95%           0                 0         -
lightway-core/src/context/ip_pool.rs                               8                 3    62.50%           1                 0   100.00%           5                 0   100.00%           0                 0         -
lightway-core/src/context/server_auth.rs                          35                27    22.86%           4                 3    25.00%          24                20    16.67%           0                 0         -
lightway-core/src/encoding_request_states.rs                       3                 0   100.00%           1                 0   100.00%           3                 0   100.00%           0                 0         -
lightway-core/src/io.rs                                           10                10     0.00%           3                 3     0.00%           9                 9     0.00%           0                 0         -
lightway-core/src/lib.rs                                           9                 0   100.00%           3                 0   100.00%           9                 0   100.00%           0                 0         -
lightway-core/src/metrics.rs                                      55                45    18.18%          21                17    19.05%          48                38    20.83%           0                 0         -
lightway-core/src/packet.rs                                       38                10    73.68%           4                 1    75.00%          30                 6    80.00%           0                 0         -
lightway-core/src/plugin.rs                                      304                13    95.72%          21                 3    85.71%         145                 7    95.17%           0                 0         -
lightway-core/src/utils.rs                                       339                31    90.86%          22                 2    90.91%         165                17    89.70%           0                 0         -
lightway-core/src/version.rs                                      94                 0   100.00%          17                 0   100.00%          82                 0   100.00%           0                 0         -
lightway-core/src/wire.rs                                        441                44    90.02%          28                 0   100.00%         233                10    95.71%           0                 0         -
lightway-core/src/wire/auth_failure.rs                            27                 1    96.30%           3                 0   100.00%          17                 0   100.00%           0                 0         -
lightway-core/src/wire/auth_request.rs                           472                12    97.46%          26                 0   100.00%         241                 0   100.00%           0                 0         -
lightway-core/src/wire/auth_success_with_config_ipv4.rs          223                 4    98.21%          11                 0   100.00%         124                 0   100.00%           0                 0         -
lightway-core/src/wire/data.rs                                    52                 1    98.08%           5                 0   100.00%          34                 0   100.00%           0                 0         -
lightway-core/src/wire/data_frag.rs                              131                 1    99.24%          14                 0   100.00%          82                 0   100.00%           0                 0         -
lightway-core/src/wire/encoding_request.rs                        82                 2    97.56%           6                 0   100.00%          42                 1    97.62%           0                 0         -
lightway-core/src/wire/encoding_response.rs                       82                 2    97.56%           6                 0   100.00%          42                 1    97.62%           0                 0         -
lightway-core/src/wire/expresslane_config.rs                     167                 2    98.80%           8                 0   100.00%          92                 0   100.00%           0                 0         -
lightway-core/src/wire/expresslane_data.rs                       938                71    92.43%          36                 7    80.56%         467                38    91.86%           0                 0         -
lightway-core/src/wire/ping.rs                                    96                 3    96.88%           7                 0   100.00%          59                 0   100.00%           0                 0         -
lightway-core/src/wire/pong.rs                                   110                 3    97.27%           8                 0   100.00%          72                 0   100.00%           0                 0         -
lightway-core/src/wire/server_config.rs                           69                 2    97.10%           4                 0   100.00%          37                 0   100.00%           0                 0         -
lightway-server/src/auth.rs                                      282                55    80.50%          22                 6    72.73%         171                28    83.63%           0                 0         -
lightway-server/src/connection.rs                                141               141     0.00%          11                11     0.00%         110               110     0.00%           0                 0         -
lightway-server/src/connection_manager.rs                        348               348     0.00%          41                41     0.00%         281               281     0.00%           0                 0         -
lightway-server/src/connection_manager/connection_map.rs         409                22    94.62%          21                 1    95.24%         228                 8    96.49%           0                 0         -
lightway-server/src/io/inside/tun.rs                              44                44     0.00%           9                 9     0.00%          30                30     0.00%           0                 0         -
lightway-server/src/io/outside/tcp.rs                             90                90     0.00%           9                 9     0.00%          65                65     0.00%           0                 0         -
lightway-server/src/io/outside/udp.rs                            320               320     0.00%          16                16     0.00%         205               205     0.00%           0                 0         -
lightway-server/src/io/outside/udp/cmsg.rs                       197                53    73.10%          14                 5    64.29%         158                43    72.78%           0                 0         -
lightway-server/src/ip_manager.rs                                550                58    89.45%          22                 4    81.82%         247                28    88.66%           0                 0         -
lightway-server/src/ip_manager/ip_pool.rs                        542                 1    99.82%          27                 0   100.00%         252                 0   100.00%           0                 0         -
lightway-server/src/lib.rs                                       260               260     0.00%          14                14     0.00%         169               169     0.00%           0                 0         -
lightway-server/src/main.rs                                      233               233     0.00%          11                11     0.00%         123               123     0.00%           0                 0         -
lightway-server/src/metrics.rs                                   257               253     1.56%          85                83     2.35%         215               211     1.86%           0                 0         -
lightway-server/src/statistics.rs                                138                62    55.07%           8                 4    50.00%          89                37    58.43%           0                 0         -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                          17054              5477    67.88%        1211               487    59.79%       10229              3664    64.18%           0                 0         -

✅ Region coverage 67% passes
✅ Line coverage 64% passes

@kp-samuel-tam kp-samuel-tam force-pushed the fix-ipv6-on-route-manager branch 2 times, most recently from 4e02561 to 2f55f24 Compare December 16, 2025 07:57
#[cfg(windows)]
let server_route = server_route.with_metric(0);

// For NoExec mode, store the route but don't actually add it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear, why do we need to move this.
Please add changes like this in a separate commit with clear description on why it is needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood the code wrong and wrote a wrong internal state check for NoExec. It should be fixed now, thanks!

// and routed networks (host systems with gateways)
let server_route = Route::new(server_ip, 32).with_if_index(default_interface_index);
// Use /32 for IPv4, /128 for IPv6
let prefix = match server_ip {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is repeated. Better to move this to a separate function.
And use https://doc.rust-lang.org/std/net/struct.Ipv6Addr.html#associatedconstant.BITS for prefix

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, for this and checking same family as well.

@kp-samuel-tam kp-samuel-tam force-pushed the fix-ipv6-on-route-manager branch 5 times, most recently from 117d4bc to 74026a6 Compare December 16, 2025 10:23
This ensures correct route setup when Lightway client is connected to
an IPv6 endpoint. The IPv4 tunnel stays unchanged.

Changes:
- Use /128 prefix for IPv6 server routes (was incorrectly using /32)
- Use /32 prefix for IPv4 server routes (unchanged)
- Monitor IPv6 default route changes when server is IPv6
- Monitor IPv4 default route changes when server is IPv4
- Fix LAN mode to only use gateway if address family matches route
- Fix find_route to return proper error instead of panicking on None
- Add mock IPv6 default route via loopback for test environments without IPv6
- Add host_prefix_len() and same_ip_family() helper functions

Tests:
- Add IPv6 and IPv4 test constants, update all tests to use them explicitly
- Update create_test_setup() to accept server_ip parameter
- Add test_ipv6_server_route_manager_creation()
- Add test_privileged_ipv6_server_initialize_route_manager()
- Add test_privileged_ipv6_server_route_update()
- Mock route only looks for ::1 (not 127.0.0.1) when finding loopback

The tunnel interface remains IPv4-only (TUN_PEER_IP, TUN_DNS_IP),
only the server connection endpoint supports IPv6.
@kp-samuel-tam kp-samuel-tam force-pushed the fix-ipv6-on-route-manager branch from 74026a6 to 2ec4f18 Compare December 16, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants