1
0
mirror of https://github.com/SoftEtherVPN/SoftEtherVPN.git synced 2025-07-07 00:04:57 +03:00

Merge pull request #1443 from domosekai/win32

Add IPv6 route management for Windows client
This commit is contained in:
Yihong Wu
2021-09-18 22:12:27 +08:00
committed by GitHub
3 changed files with 263 additions and 330 deletions

View File

@ -162,7 +162,6 @@ void RouteTrackingMain(SESSION *s)
char ip_str2[64];
Copy(&e->DestIP, &nat_t_ip, sizeof(IP));
e->Metric = e->OldIfMetric;
IPToStr(ip_str, sizeof(ip_str), &e->DestIP);
IPToStr(ip_str2, sizeof(ip_str2), &e->GatewayIP);
@ -190,9 +189,12 @@ void RouteTrackingMain(SESSION *s)
{
UINT i;
bool route_to_server_erased = true;
bool is_vlan_want_to_be_default_gateway = false;
UINT vlan_default_gateway_metric = 0;
UINT other_if_default_gateway_metric_min = INFINITE;
bool is_vlan_want_to_be_default_gateway_v4 = false;
bool is_vlan_want_to_be_default_gateway_v6 = false;
UINT vlan_default_gateway_metric_v4 = 0;
UINT vlan_default_gateway_metric_v6 = 0;
UINT other_if_default_gateway_metric_min_v4 = INFINITE;
UINT other_if_default_gateway_metric_min_v6 = INFINITE;
// Get whether the routing table have been changed
if (t->LastRoutingTableHash != table->HashedValue)
@ -224,59 +226,77 @@ void RouteTrackingMain(SESSION *s)
}
// Search for the default gateway
if (IPToUINT(&e->DestIP) == 0 &&
IPToUINT(&e->DestMask) == 0)
if (IsZeroIP(&e->DestIP) && IsZeroIP(&e->DestMask))
{
Debug("e->InterfaceID = %u, t->VLanInterfaceId = %u\n",
e->InterfaceID, t->VLanInterfaceId);
if (e->InterfaceID == t->VLanInterfaceId)
{
// The virtual LAN card think that he want to be a default gateway
is_vlan_want_to_be_default_gateway = true;
vlan_default_gateway_metric = e->Metric;
if (vlan_default_gateway_metric >= 2 &&
t->OldDefaultGatewayMetric == (vlan_default_gateway_metric - 1))
if (IsIP4(&e->DestIP))
{
// Restore because the PPP server rewrites
// the routing table selfishly
DeleteRouteEntry(e);
e->Metric--;
AddRouteEntry(e);
Debug("** Restore metric destroyed by PPP.\n");
// The virtual LAN card think that he want to be a default gateway
is_vlan_want_to_be_default_gateway_v4 = true;
vlan_default_gateway_metric_v4 = e->Metric;
any_modified = true;
// PPP route fix
if (vlan_default_gateway_metric_v4 >= 2 &&
t->OldDefaultGatewayMetric == (vlan_default_gateway_metric_v4 - 1))
{
// Restore because the PPP server rewrites
// the routing table selfishly
DeleteRouteEntry(e);
e->Metric--;
AddRouteEntry(e);
Debug("** Restore metric destroyed by PPP.\n");
any_modified = true;
}
// Keep this entry
if (t->DefaultGatewayByVLan != NULL)
{
// Delete if there is one added last time
FreeRouteEntry(t->DefaultGatewayByVLan);
}
t->DefaultGatewayByVLan = ZeroMalloc(sizeof(ROUTE_ENTRY));
Copy(t->DefaultGatewayByVLan, e, sizeof(ROUTE_ENTRY));
t->OldDefaultGatewayMetric = vlan_default_gateway_metric_v4;
}
// Keep this entry
if (t->DefaultGatewayByVLan != NULL)
else
{
// Delete if there is one added last time
FreeRouteEntry(t->DefaultGatewayByVLan);
is_vlan_want_to_be_default_gateway_v6 = true;
vlan_default_gateway_metric_v6 = e->Metric;
}
t->DefaultGatewayByVLan = ZeroMalloc(sizeof(ROUTE_ENTRY));
Copy(t->DefaultGatewayByVLan, e, sizeof(ROUTE_ENTRY));
t->OldDefaultGatewayMetric = vlan_default_gateway_metric;
}
else
{
// There are default gateway other than the virtual LAN card
// Save the metric value of the default gateway
if (other_if_default_gateway_metric_min > e->Metric)
if (IsIP4(&e->DestIP))
{
// Ignore the metric value of all PPP connection in the case of Windows Vista
if (e->PPPConnection == false)
// There are default gateway other than the virtual LAN card
// Save the metric value of the default gateway
if (other_if_default_gateway_metric_min_v4 > e->Metric)
{
other_if_default_gateway_metric_min = e->Metric;
// Ignore the metric value of all PPP connection in the case of Windows Vista
if (e->PPPConnection == false)
{
other_if_default_gateway_metric_min_v4 = e->Metric;
}
else
{
// a PPP is used to Connect to the network
// in using Windows Vista
t->VistaAndUsingPPP = true;
}
}
else
}
else
{
if (other_if_default_gateway_metric_min_v6 > e->Metric)
{
// a PPP is used to Connect to the network
// in using Windows Vista
t->VistaAndUsingPPP = true;
other_if_default_gateway_metric_min_v6 = e->Metric;
}
}
}
@ -287,7 +307,7 @@ void RouteTrackingMain(SESSION *s)
{
if (t->DefaultGatewayByVLan != NULL)
{
if (is_vlan_want_to_be_default_gateway)
if (is_vlan_want_to_be_default_gateway_v4)
{
if (t->VistaOldDefaultGatewayByVLan == NULL || Cmp(t->VistaOldDefaultGatewayByVLan, t->DefaultGatewayByVLan, sizeof(ROUTE_ENTRY)) != 0)
{
@ -362,8 +382,9 @@ void RouteTrackingMain(SESSION *s)
// to elect the virtual LAN card as the default gateway
// Debug("is_vlan_want_to_be_default_gateway = %u, rs = %u, route_to_server_erased = %u, other_if_default_gateway_metric_min = %u, vlan_default_gateway_metric = %u\n",
// is_vlan_want_to_be_default_gateway, rs, route_to_server_erased, other_if_default_gateway_metric_min, vlan_default_gateway_metric);
if (is_vlan_want_to_be_default_gateway && (rs != NULL && route_to_server_erased == false) &&
other_if_default_gateway_metric_min >= vlan_default_gateway_metric)
if ((is_vlan_want_to_be_default_gateway_v4 && other_if_default_gateway_metric_min_v4 >= vlan_default_gateway_metric_v4 ||
is_vlan_want_to_be_default_gateway_v6 && other_if_default_gateway_metric_min_v6 >= vlan_default_gateway_metric_v6)
&& rs != NULL && route_to_server_erased == false)
{
// Scan the routing table again
for (i = 0;i < table->NumEntry;i++)
@ -372,8 +393,7 @@ void RouteTrackingMain(SESSION *s)
if (e->InterfaceID != t->VLanInterfaceId)
{
if (IPToUINT(&e->DestIP) == 0 &&
IPToUINT(&e->DestMask) == 0)
if (IsZeroIP(&e->DestIP) && IsZeroIP(&e->DestMask))
{
char str[64];
// Default gateway is found
@ -486,8 +506,6 @@ void RouteTrackingStart(SESSION *s)
Debug("GetBestRouteEntry() Succeed. [Gateway: %s]\n", tmp);
// Add a route
e->Metric = e->OldIfMetric;
if (AddRouteEntryEx(e, &already_exists) == false)
{
FreeRouteEntry(e);
@ -549,8 +567,6 @@ void RouteTrackingStart(SESSION *s)
else
{
// Add a route
dns->Metric = dns->OldIfMetric;
if (AddRouteEntry(dns) == false)
{
FreeRouteEntry(dns);
@ -569,8 +585,6 @@ void RouteTrackingStart(SESSION *s)
if (route_to_real_server_global != NULL)
{
route_to_real_server_global->Metric = route_to_real_server_global->OldIfMetric;
if (AddRouteEntry(route_to_real_server_global) == false)
{
FreeRouteEntry(route_to_real_server_global);
@ -633,11 +647,6 @@ void RouteTrackingStart(SESSION *s)
MsFreeAdapter(a);
}
}
else
{
// For Win9x
Win32RenewDhcp9x(if_id);
}
// Clear the DNS cache
Win32FlushDnsCache();
@ -782,12 +791,12 @@ void RouteTrackingStop(SESSION *s, ROUTE_TRACKING *t)
// If the restoring routing entry is a default gateway and
// the existing routing table contains another default gateway
// on the interface, give up restoring the entry
if (IPToUINT(&e->DestIP) == 0 && IPToUINT(&e->DestMask) == 0)
if (IsZeroIP(&e->DestIP) && IsZeroIP(&e->DestMask))
{
for (i = 0;i < table->NumEntry;i++)
{
ROUTE_ENTRY *r = table->Entry[i];
if (IPToUINT(&r->DestIP) == 0 && IPToUINT(&r->DestMask) == 0)
if (IsZeroIP(&r->DestIP) && IsZeroIP(&r->DestMask))
{
if (r->InterfaceID == e->InterfaceID)
{