8

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?

Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99
Vijaya
  • 81
  • 1
  • 2
  • As for me, i can read IP packets out of the TUN interface, but my problem is that, when i forward those packets and write to tun0 on my ubuntu server, i get the packet with `ETHERNET FRAME CHECK SEQUENCE NOT CORRECT` as response. In order for you to be able to read IP packets, after setting p2p like you did, you need to do some routing: `route add mask 255.255.255.255 ` `route add 128.0.0.0 mask 128.0.0.0 ` `route add 0.0.0.0 mask 128.0.0.0 ` – JoeAdeoye Sep 02 '16 at 15:30
  • 1
    You should look at openvpn source code. I see in the openvpn source code the constants which is belong to tun mode. I mean TAP_WIN_IOCTL_CONFIG_TUN for instance. – vadim_hr Aug 30 '18 at 08:54
  • `TAP_WIN_IOCTL_CONFIG_TUN` is defined at https://github.com/OpenVPN/tap-windows6/blob/8e437cbc8a38362710aeccd9aea57bef662c2812/src/tap-windows.h#L52 – Top-Master Mar 06 '19 at 05:00

1 Answers1

6

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.

user3235994
  • 61
  • 1
  • 2