Search code examples
cnetwork-programmingwirelesspacket-capturetun

Listening on Virtual Interface


My objective is to create a to create a virtual wireless interface and to attach a listener on that interface. I am trying to do this using a C program.

So far I have been able to create the virtual interface by the following methods :

  • Using the linux iw commands : `

sudo iw phy phy0 interface add mySta1 type station

but I was unable to find the library to do the same using C program.

char tun_name[IFNAMSIZ];
strcpy(tun_name, "MyTun");
tunfd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI);

where tun_alloc is my function which uses ioctl to create the interface

ioctl(fd, TUNSETIFF, (void *) &ifr)

but there is no wireless extensions for this interface

For listening on the interface :

nread = read(tunfd,buffer,sizeof(buffer));

But this only works on tun devices which I create using my program or programs like openvpn. When used with any other interface like wlan0, ioctl gives the error 'Invalid argument'

Is my approach to the problem correct ? Is there any other way to achieve this ? I want to know the following :

  1. Is there a way of attaching the tun device on devices created by 'iw add interface' commands ?
  2. Is there a way of making a virtual wireless tun/tap interface or changing the type of an existing tun interface to wireless ?

Solution

  • If you want to achieve "iw phy..." in C program - just look at "iw" program sources. Probably it is achieved by some ioctls or netlink.

    Answers to your questions:

    1. Virtual TUN/TAP device means that this device is independent of any hardware. It it only software solution. You cannot easily in userspace attach TUN/TAP device to wireless interface.

    2. No, as I wrote in answer 1. the TUN/TAP is only virtual device and it cannot be converted to wifi device type.