2

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 ?
Aditya Pawade
  • 866
  • 9
  • 19

2 Answers2

1

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.

user2699113
  • 4,262
  • 3
  • 25
  • 43
  • Ok. Now I get it. So this interface can't read any packets from External Networks. It just exists virtually for the userspace programs to send or receive packets. So this can't help me read packets from wlan. So how do commands like 'airmon-ng start wlan0' or 'iw dev wlan0 interface add mon0 type monitor' create a virtual mon0 interface which captures all packets from the hardware interfaces ? – Aditya Pawade Nov 09 '13 at 10:22
  • I guess that mon0 interface is created by madwifi drivers on kernel level as a special kind of device which allows to capture all packets from real hardware interface. I suggest you to take a look at source of "iw" command how such interface is requested by user-space. User space is only a requester for madwifi drivers in such case. All things like copying packets to your mon0 interface is doing by madwifi I think. – user2699113 Nov 09 '13 at 10:27
0

i don't know about any existing libraries for this. If your only intention is to get this working from your code, why don't you follow this.

system("iw phy phy0 interface add mySta1 type station");

Its obviously ugly, but it does its purpose.

joe
  • 1,136
  • 9
  • 17
  • That is my last resort but I wanted to know if there was a library or command which would help me do the same. Also, the tun device doesn't get attached to the iw created interfaces . – Aditya Pawade Nov 09 '13 at 07:30
  • i just gave a search, there are already folks asked this question about C interface for TUN IOCTL. Looks like it isn't available. You might need to go with your last resort :-) – joe Nov 09 '13 at 07:39
  • Even after creating the interface through iw command, I couldn't find a way to attach my tun listener to the interface. That's the reason I was searching for a wireless tun method. Thanks for your help though :) – Aditya Pawade Nov 09 '13 at 08:09