Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TUN driver in windows

Tags:

tun

I have installed the TAP-WIN32 Adapter V9 on my Windows 7 machine. I want to use TUN to read the IP packets out of the interface. I followed the C# sample from http://www.varsanofiev.com/inside/using_tuntap_under_windows.htm

I modified the code like below

IntPtr ptun = Marshal.AllocHGlobal(8);       
Marshal.WriteInt32(ptun, 0, 0x0a030001);
Marshal.WriteInt32(ptun, 4, unchecked((int)0x00ffffFF));
bool val = DeviceIoControl (ptr, TAP_CONTROL_CODE (5, METHOD_BUFFERED) /*POINT TO POINT */, ptun, 8,ptun, 8, out len, IntPtr.Zero);

However, I still don't seem to get IP packets. I ran tcpdump on the other end and it complains "Wrong link layer encapsulation".

Is this the correct way of trying to get IP Packets out of TUN interface?

like image 524
Vijaya Avatar asked Sep 20 '12 13:09

Vijaya


People also ask

What is a TUN driver?

The TUN is Virtual Point-to-Point network device. TUN driver was designed as low level kernel support for. IP tunneling. It provides to userland application.

How does a TUN device work?

TUN, namely network TUNnel, simulates a network layer device and operates in layer 3 carrying IP packets. TAP, namely network TAP, simulates a link layer device and operates in layer 2 carrying Ethernet frames. TUN is used with routing. TAP can be used to create a user space network bridge.

What is a TUN adapter?

A TUN interface is a virtual IP Point-to-Point interface and a TAP interface is a virtual Ethernet interface. That means the user program can only read/write IP packets from/to a TUN interface and Ethernet frames from/to a TAP interface.

How do I enable the tap adapter in Windows 10?

Solution 1: re-enable the adapterOpen Control Panel > Network and Internet > Network and Sharing Center > Change adapter settings. Check description of network adapters to find the Tap-Windows Adapter. Right click on it > Disable. Right click again > Enable.


1 Answers

No, it is not the correct way. It's a pity, but tap/tun driver works only in tap mode in windows. It means that you can receive Ethernet packet, not IP. Let's consider the situation when you have some IP packet for destination address X. Before sending this packet on ethernet layer, OS creates an ARP query, hey, IP X, what is your MAC? In this case you have to implement an ARP layer. After ARP negotiations, system will send the Ethernet-encapsulated IP paket.

like image 145
user3235994 Avatar answered Oct 14 '22 05:10

user3235994