Skip to content

Commit

Permalink
DEV9: Avoid using a netmask of 255.255.255.255
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastRar committed Nov 28, 2024
1 parent 4059211 commit 53048f2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
28 changes: 24 additions & 4 deletions pcsx2/DEV9/InternalServers/DHCP_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,34 @@ namespace InternalServers
* Some VPN adapters will present a subnet mask of 255.255.255.255 and omit setting a gateway.
* This is used for point-point links where the destination device handles routing out of the network.
* PS2 software, howver, expects a valid gateway for packets leaving the network.
* As a hackfix, we set the gateway to the PS2 IP, which is enough to allow such software to pregress.
* A side effect of this is that outbound packets will have the src and dst mac be identical.
* A possible hackfix was to set the gateway to the PS2 IP, however, some software rejects this.
* Thus the only solution is to expand the netmask and add a fake gateway using the other IP.
* This is a mostly PCAP exclusive issue, I've only seen such networks with VPN devices,
* which don't like being bridged, preventing TAP from being used with them.
* Sockets (currently) uses its own internal network, and thus would be unaffected.
*/
else if (netmask == IP_Address{{{255, 255, 255, 255}}})
gateway = ps2IP;
{
// Expand the netmask to allow for a gateway
netmask = {{{255, 255, 255, 252}}};

// Need to ensure our IP isn't the broadcast IP
while (true)
{
// Shift is busted
const IP_Address bc = ps2IP | ~netmask;
if (ps2IP == bc)
netmask.integer = htonl(ntohl(netmask.integer) << 1);
else
break;
}

// Pick a free IP for our gateway
gateway = (ps2IP & netmask);
gateway.integer = htonl(ntohl(gateway.integer) + 1);
while (gateway == ps2IP)
gateway.integer = htonl(ntohl(gateway.integer) + 1);
}
}

#ifdef _WIN32
Expand Down Expand Up @@ -192,7 +212,7 @@ namespace InternalServers

void DHCP_Server::AutoBroadcast(IP_Address parPS2IP, IP_Address parNetmask)
{
if (parNetmask.integer != 0 && parNetmask != IP_Address{{{255, 255, 255, 255}}})
if (parNetmask.integer != 0)
{
for (int i = 0; i < 4; i++)
broadcastIP.bytes[i] = ((parPS2IP.bytes[i]) | (~parNetmask.bytes[i]));
Expand Down
25 changes: 25 additions & 0 deletions pcsx2/DEV9/PacketReader/IP/IP_Address.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,30 @@ namespace PacketReader::IP

bool operator==(const IP_Address& other) const { return this->integer == other.integer; }
bool operator!=(const IP_Address& other) const { return this->integer != other.integer; }

IP_Address operator~() const
{
IP_Address ret;
ret.integer = ~this->integer;
return ret;
}
IP_Address operator&(const IP_Address& other) const
{
IP_Address ret;
ret.integer = this->integer & other.integer;
return ret;
}
IP_Address operator|(const IP_Address& other) const
{
IP_Address ret;
ret.integer = this->integer | other.integer;
return ret;
}
IP_Address operator^(const IP_Address& other) const
{
IP_Address ret;
ret.integer = this->integer ^ other.integer;
return ret;
}
};
} // namespace PacketReader::IP

0 comments on commit 53048f2

Please sign in to comment.