- 1 year ago
Windows IPv6 raw sockets - why don't I see incoming packets?
On Windows, by default, incoming IPv6 packets are not delivered to raw sockets due to security restrictions. This behavior is different from IPv4 raw sockets, where incoming packets are delivered to raw sockets by default.
To receive incoming IPv6 packets on raw sockets in Windows, you need to explicitly enable the reception of IPv6 packets for the raw socket using the IPV6_PROTECTION_LEVEL
socket option.
Here's how you can do it in C/C++:
- Create the raw socket with the
AF_INET6
address family andSOCK_RAW
socket type:
c#include <Winsock2.h>
#include <WS2tcpip.h>
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
// Error handling
return 1;
}
SOCKET rawSocket = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
if (rawSocket == INVALID_SOCKET)
{
// Error handling
WSACleanup();
return 1;
}
// Set the IPV6_PROTECTION_LEVEL socket option to RECEIVE_PACKET_INFORMATION
int level = IPPROTO_IPV6;
int option = IPV6_PROTECTION_LEVEL;
DWORD value = RECEIVE_PACKET_INFORMATION;
if (setsockopt(rawSocket, level, option, (const char*)&value, sizeof(value)) == SOCKET_ERROR)
{
// Error handling
closesocket(rawSocket);
WSACleanup();
return 1;
}
// Other processing using the raw socket
closesocket(rawSocket);
WSACleanup();
return 0;
}
With the IPV6_PROTECTION_LEVEL
set to RECEIVE_PACKET_INFORMATION
, the raw socket will now receive incoming IPv6 packets, and you should be able to see incoming packets.
Please note that using raw sockets requires administrative privileges, so you may need to run your application with elevated permissions (e.g., as an administrator). Additionally, handling raw sockets requires careful consideration of security and potential risks, as you have access to all incoming packets, including those that would otherwise be filtered or blocked by the operating system's network stack.