Wednesday, 24 July 2013

Part 1: P2P aka WiFi Direct - Introduction and Configuration of wpa_supplicant in Linux kernel version 3.8.3

What is P2P?

P2P alias Wi-Fi Direct builds upon the successful IEEE802.11 infrastructure mode and lets devices negotiate who will take over the AP-like functionalities. Thus, legacy Wi-Fi devices may seamlessly connect to Wi-Fi Direct devices . By taking this decision, Wi-Fi Direct immediately inherits all the enhanced QoS, power saving, and security mechanisms.


In a typical Wi-Fi network, clients discover and associate to WLANs, which are created and announced by Access Points (APs). In this way, a device unambiguously behaves either as an AP or as a client, each of these roles involving a different set of functionality. A major novelty of Wi-Fi Direct is that these roles are specified as dynamic, and hence a Wi-Fi Direct device has to implement both the role of a client and the role of an AP.


The device implementing AP-like functionality in the P2P Group is referred to as the P2P Group Owner (P2P GO), and devices acting as clients are known as P2P
Clients.Legacy clients can also communicate with the P2P GO, as long as they are not 802.11b-only devices and support the required security mechanisms.

For example, consider a laptop accessing the Internet through a legacy infrastructure AP while at the same time streaming content to a TV set by establishing a P2P Group, where the laptop acts as P2P GO.If the P2P GO leaves the P2P Group then the group is torn down, and has to be re-established using some of the specified procedures.

Wi-Fi Direct devices usually start by performing a traditional Wi-Fi scan (active or passive), by means of which they can discover existent P2P Groups and Wi-Fi networks.

After this scan, a new Discovery algorithm is executed, which we describe next. First, a P2P Device selects one of the so-called Social channels, namely channels 1, 6 or 11 in the 2.4 Ghz band, as its Listen channel.

Then, it alternates between two states: a search state, in which the device performs active scanning by sending Probe Requests in each of the social channels; and a listen state, in which the device listens for Probe Requests in its listen channel to respond with Probe Responses. The amount of time that a P2P Device spends on each state is randomly distributed, typically between 100 ms and 300 ms.


How to know if your device supports p2p? Use iw list command in terminal and look for the following field. If it contains p2p-client and p2p-GO, you can be assured that your device supports P2P.



Just like a client and server model, you need to run supplicant and p2p client.
Supplicant is a daemon running in the user space to provide authentication.

mac80211 and cfg80211 does not implement WPA feature. So you need a seperate program which provides supplicant functionality in Linux which is
wpa_supplicant.Install latest release of wpa_supplicant from http://hostap.epitest.fi/releases

I downloaded: wpa_supplicant-2.0.tar.gz


First thing you need to do is to create the .config file in the folder wpa_supplicant-2.0/wpa_supplicant from defconfig file.


Enable following options in defconfig file (uncommenting is removing # in defconfig file)


CONFIG_CTRL_IFACE=y
CONFIG_DRIVER_NL80211=y 
CONFIG_WPS=y
CONFIG_WPS2=y
CONFIG_P2P=y
CONFIG_AP=y

Give cp defconfig .config command and then make command in wpa_supplicant 
folder. Now there are two binaries created wpa_supplicant and wpa_cli. In order to run wpa_supplicant we need a config file. Traditionally it is created in /etc folder. Here is a sample config file for p2p. (/etc/p2p.conf)
ctrl_interface=/var/run/wpa_supplicant
update_config=1
device_name=PRIYA-P2P
device_type=2-0050F204-1
p2p_listen_reg_class=81
p2p_listen_channel=1
p2p_oper_reg_class=81
p2p_oper_channel=1
p2p_go_intent=1
p2p_no_group_iface=1



Find out the wireless interface using iwconfig command and then give the following command in wpa_supplicant

./wpa_supplicant -i wlan2 -c /etc/p2p.conf -Dnl80211

You will see a msg "Successfully initialized wpa_supplicant" which means that supplicant has started running.In another window run the wpa client using ./wpa_cli

This opens an interactive mode in which commands can be given.
Interactive mode
>


(Tip: Use killall wpa_supplicant if you get error message like: 

ctrl_iface exists and seems to be in use - cannot override it Delete '/var/run/wpa_supplicant/wlan2' manually if it is not used anymore Failed to initialize control interface '/var/run/wpa_supplicant'. You may have another wpa_supplicant process already running or the file was left by an unclean termination of wpa_supplicant in which case you will need to manually remove this file before starting wpa_supplicant again.  )




39 comments:

  1. Hi,
    Your blog has been very informative. However I would like to know if you had made any changes to menuconfig while compiling linux kernel for supporting wifi p2p. Also may i know which wireless chipset do you have on your laptop ?

    ReplyDelete
  2. Forgot to ask one quick question... Can we have a wireless dongles connected to usb on laptop if i need to test wifi p2p incase wireless card not avaiable. Anything additional need to be done for wpa_supplicant/linux driver side to set wifi p2p network ?

    -Thanks,
    Jane

    ReplyDelete
    Replies
    1. Hi.. sorry for the delayed response! Just check if you have enabled the support for cfg80211 and mac80211 in network support-> wireless configuration in make menuconfig. I was using Atheros chipset (Netgear WG111T usb dongle). You can use usb dongle, but make sure that it has P2P capability by checking "iw list" result. Just try out the procedures in the blog and if you have any trouble setting up the network, feel free to ask me.

      Delete
    2. Thanks Ramapriya .. saw your reply very late ... so do you mean that configure wifi direct Netgear WG111T usb wireless dongle ? will connecting it create an wlan interface in ifconfig output over which we can configure wifi direct by running WPA supplicant ?

      Delete
    3. Yes, connecting the dongle creates a wireless interface which you can view in ifconfig/iwconfig

      Delete
  3. Hi rama,
    Here i'm using Ath9 series of chips for WiFi-BT, when i disable wifi-direct feature on Freescale Android JB.4.2.2.1 version, wiFi feature works and when i enable, it fails with following error.

    D/CommandListener( 2171): Setting iface cfg

    D/CommandListener( 2171): Trying to bring down wlan0

    E/WifiHW ( 2547): wifi_start_supplicant p2p=1

    E/WifiHW ( 2547): Wi-Fi will ensure config file exist

    E/WifiHW ( 2547): start supplicant cmd=p2p_supplicant

    I/wpa_supplicant( 3178): Successfully initialized wpa_supplicant

    I/wpa_supplicant( 3178): rfkill: Cannot open RFKILL control device

    E/wpa_supplicant( 3178): nl80211: Could not configure driver to use managed mode

    E/wpa_supplicant( 3178): Could not read interface p2p0 flags: No such device

    E/wpa_supplicant( 3178): p2p0: Failed to initialize driver interface

    E/WifiStateMachine( 2547): Failed to start supplicant!

    W/Netd ( 2171): No subsystem found in netlink event

    D/NetlinkEvent( 2171): Unexpected netlink message. type=0x11



    Fails to enable P2P interface,

    Please guide me regarding this issue.

    ReplyDelete
  4. Verify if you are using the correct driver for your chipset. Try installing the driver again.What is the output for the "rfkill list" command? What is the kernel version?

    ReplyDelete
  5. i configure RFKILL from kernel defconfig and from wpa_supplicant created p2p0 interface, then able configure p2p0 interface but fail to configure wlan0 interface. fails with following error:
    E/wpa_supplicant( 3411): nl80211: Register frame match - hexdump(len=2): 0a 07
    E/wpa_supplicant( 3411): Could not set interface wlan0 flags (UP): Name not unique on network
    E/wpa_supplicant( 3411): nl80211: Could not set interface 'wlan0' UP
    E/wpa_supplicant( 3411): nl80211: Set mode ifindex 9 iftype 2 (STATION)
    E/wpa_supplicant( 3411): nl80211: Unsubscribe mgmt frames handle 0x407360e8 (mode change)
    E/wpa_supplicant( 3411): wlan0: Failed to initialize driver interface
    E/wpa_supplicant( 3411): Failed to add interface wlan0
    E/wpa_supplicant( 3411): wlan0: Cancelling authentication timeout
    E/wpa_supplicant( 3411): p2p0: Removing interface p2p0

    Both wlan0 and p2p0 interfaces showing same MAC address. do you think MAC address is the issue?

    ReplyDelete
  6. Hello Ramapriya Rjamani.
    Thanks for the tutorial.
    I got a few questions to ask about how to setup 2 STA (dell, hp laptops with Linux 12.04 on it) with direct wifi.
    First of all, I have tp link tl-wn722n which is a usb dongle. (I think it supports P2P since I checked it with 'iw list') I have downloaded the same wpa_supplicant as you mentioned on both STA. Then I started wpa_supplicant. On another terminal, I got to interactive mode with > hanging. However, I am not sure why CTRL-EVENT-SCAN-RESULTS is being spammed on both STA1 and STA2 screens.

    Also, I am wondering how to connect STA1 to STA2 and transmit data.

    Could you give me a little bit of help here?
    Thanks in advance.

    Best regards,
    Spencer.

    ReplyDelete
  7. Hi Spencer,

    Open another terminal and run ./wpa_cli.
    1. Use "p2p_find" to find peer's MAC address

    2. "p2p_connect [peer MAC addresss] [intent value(range 0 to 15)]"

    In one device give intent value as 15 (for the device to become group owner) and in another device use a value as 4 (for the device to become client)

    Note:
    You may try different intent values. This is just given as an example.

    Pls let me know if you face any issues.
    Regards,
    Ramapriya

    ReplyDelete
  8. Hi,
    Could some one suggest me Wireless USB adapter with P2P-client and P2P-GO compatibility. Please share your thoughts.
    -Anand Moon

    ReplyDelete
    Replies
    1. my suggestion is to go for netgear wireless dongle

      Delete
  9. p2p find sends probe request in the social channels. If it didn't find any responding device it might fail.

    ReplyDelete
  10. Hi,
    Thanks for your tutorial. It is very useful. I have followed your tutorial and it is working for netgear dongle wna1100. Now i want P2P working in WNDA3100v2 dongle. It is windows based. so i have installed bcm43xx driver using ndiswrapper command in ubuntu. After installed driver, wlan0 interface is created. but P2P is not working. iw list command returns nothing. when running wpa_supplicant it throws like
    $ sudo ./wpa_supplicant -Dnl80211 -iwlan0 -c/etc/p2p.conf -B
    Successfully initialized wpa_supplicant
    nl80211: Could not configure driver to use managed mode
    wlan0: Failed to initialize driver interface

    Please help me to solve this.

    ReplyDelete
    Replies
    1. Looks like driver issue..Try installing latest software from http://downloadcenter.netgear.com/en/product/WNDA3100v2#searchResults

      Delete
    2. Hi,

      I have a Netgear wna1100 usb wlan adaptor. I have followed above tutorial and installed the drivers for this on ubuntu. I am using ath9k_htc driver version 1.3.
      With iw list the device shows that it supports AP,P2P modes.
      But when I am trying to run the following command,

      ./wpa_supplicant -i wlan1 -c /etc/wpa_supplicant/wpa_supplicant.conf -Dnl80211

      output:
      Successfully initialized wpa_supplicant
      nl80211: Could not configure driver mode
      nl80211: deinit ifname=wlan1 disabled_11b_rates=0
      wlan1: Failed to initialize driver interface.

      And after this the led (present on the USB adaptor) which was ON before executing this command will turn OFF.

      I have to use service hostapd restart to start that LED again(to start wifi dongle).

      Could you please help me with this?

      Thanks

      Delete
  11. Hi,
    Could you help me to do the testing on power save modes ops and noa using wpa_supplicant?. i cant find any related documents to that.

    ReplyDelete
  12. Given 2 or more p2p driver connect with each other. So after connection how will the connected nodes get IP. Do we need DHCP server running in the background.

    ReplyDelete
    Replies
    1. Yes, pls run DHCP server in Group owner

      Delete
    2. My guess is that the Group owner is dynamically decided between the 2 or more p2p client. Is this correct.

      Delete
    3. Yes, you may want to read http://riya80211.blogspot.in/2013/08/part-3-p2p-group-owner-negotiation.html for further details.

      Delete
  13. Wow is just the simple word that may explain that how much I liked it. It was nicely stuffed with the material I was looking for. It’s great to be here though by chance.
    Buy Laptops

    ReplyDelete
  14. Hi im using OpenWRT for "Freescale i.MX 6" target platform. I have enabled the following using menuconfig
    'wpa-supplicant'
    'wpa-cli'
    'Add rfkill support'
    'wpa-supplicant-p2p'
    'wpad'
    also
    'kmod-ipt-iface'
    'iptables-mod-iface'
    'iptables-mod-ipp2p'
    'kmod-ipt-ipp2p'
    'olsrd-mod-p2pd'
    and some P2P
    'amule'
    'opendchub'
    'peerguardian'
    'uhub'
    But I haven't enabled 'mutella' since it was giving some compilation error.
    Now in my target board im facing an issue on starting the wpa_supplicant. I have created config file in the target as u mentioned and tried to start the wpa_supplicant, im receiving the following error,

    root@OpenWrt:/# wpa_supplicant -i wlan0 -c /etc/p2p.conf -Dnl80211
    Successfully initialized wpa_supplicant
    Line 6: unknown global field 'p2p_listen_reg_class=81'.
    Line 6: Invalid configuration line 'p2p_listen_reg_class=81'.
    Line 7: unknown global field 'p2p_listen_channel=1'.
    Line 7: Invalid configuration line 'p2p_listen_channel=1'.
    Line 8: unknown global field 'p2p_oper_reg_class=81'.
    Line 8: Invalid configuration line 'p2p_oper_reg_class=81'.
    Line 9: unknown global field 'p2p_oper_channel=1'.
    Line 9: Invalid configuration line 'p2p_oper_channel=1'.
    Line 10: unknown global field 'p2p_go_intent=1'.
    Line 10: Invalid configuration line 'p2p_go_intent=1'.
    Line 11: unknown global field 'p2p_no_group_iface=1'.
    Line 11: Invalid configuration line 'p2p_no_group_iface=1'.
    Failed to read or parse configuration '/etc/p2p.conf'.

    Is this happens due to unavailability of "mutella". Is this required? Or this failure happens due to some other reason?

    ReplyDelete
    Replies
    1. Can you pls check if wlan0 is up? If it is up, enable -ddt option (./wpa_supplicant -i wlan0 -c /etc/p2p.conf -Dnl80211 -ddt) for the debug and you may get additional info.

      Delete
    2. OMG, thanks for ur super fast reply.
      I have made wifi up by giving "ifconfig wlan0 up"
      I could the available AP using "iw wlan0 scan"

      root@OpenWrt:/#
      root@OpenWrt:/# wpa_supplicant -i wlan0 -c /etc/p2p.conf -Dnl80211 -ddt
      1435126267.331293: Successfully initialized wpa_supplicant
      1435126267.331813: Line 6: unknown global field 'p2p_listen_reg_class=81'.
      1435126267.331875: Line 6: Invalid configuration line 'p2p_listen_reg_class=81'.
      1435126267.331919: Line 7: unknown global field 'p2p_listen_channel=1'.
      1435126267.331951: Line 7: Invalid configuration line 'p2p_listen_channel=1'.
      1435126267.331991: Line 8: unknown global field 'p2p_oper_reg_class=81'.
      1435126267.332022: Line 8: Invalid configuration line 'p2p_oper_reg_class=81'.
      1435126267.332060: Line 9: unknown global field 'p2p_oper_channel=1'.
      1435126267.332090: Line 9: Invalid configuration line 'p2p_oper_channel=1'.
      1435126267.332131: Line 10: unknown global field 'p2p_go_intent=1'.
      1435126267.332162: Line 10: Invalid configuration line 'p2p_go_intent=1'.
      1435126267.332201: Line 11: unknown global field 'p2p_no_group_iface=1'.
      1435126267.332231: Line 11: Invalid configuration line 'p2p_no_group_iface=1'.
      1435126267.332312: Failed to read or parse configuration '/etc/p2p.conf'.
      root@OpenWrt:/#
      root@OpenWrt:/#


      Im still getting the same problem. Is this due to the unavailability of "mutella" or any other reason?

      Delete
    3. Problem is not with mutella. The problem you mention does not occur in Linux platform. Check freescale forums for device specific problems

      Delete
    4. Ramapriya it's really worthy information I am not getting ssid as direct- after creating in station mode in probe response packet plss explain me

      Delete
    5. Do you mean P2P client or station?

      Delete
  15. Hi Guys,
    I need your help to resolve "p2p_find" FAIL message. Below is the complete description of my setup.

    I want to establish wi-fi direct connection between two Dell Laptops running on Ubuntu 14.04.3 LTS. Wi-fi chip is Broadcomm BCM4313. p2p_find command fails. This is the code flow: wpa_cli_cmd_p2p_find() -> wpa_cli_cmd() -> wpa_ctrl_command() ->_wpa_ctrl_command() -> recv(). recv() call receives "FAIL" as reply. Running wpa_supplicant and wpa_cli on both laptops,


    1. wpa_supplicant-2.4,



    a. .config file in wpa_supplicant-2.4 has below configurations,

    CONFIG_DRIVER_NL80211=y

    # optional, depending on libnl version you want to use:

    # CONFIG_LIBNL20=y



    CONFIG_CTRL_IFACE=y

    CONFIG_WPS=y

    CONFIG_WPS2=y

    CONFIG_P2P=y

    CONFIG_AP=y



    # and maybe DBus



    b. cp wpa_supplicant.conf /etc/wpa_supplicant.conf

    c. sudo make && sudo make install

    d. p2p.conf file



    ctrl_interface=/var/run/wpa_supplicant

    ap_scan=1



    device_name=my-device-name

    device_type=1-0050F204-1



    # If you need to modify the group owner intent, 0-15, the higher

    # number indicates preference to become the GO. You can also set

    # this on p2p_connect commands.

    #p2p_go_intent=15



    # optional, can be useful for monitoring, forces

    # wpa_supplicant to use only channel 1 rather than

    # 1, 6 and 11:

    #p2p_listen_reg_class=81

    #p2p_listen_channel=1

    #p2p_oper_reg_class=81

    #p2p_oper_channel=1



    e. Start wpa supplicant,

    ./wpa_supplicant -Dnl80211 -c /path/to/p2p.conf -i wlan0 -dt

    f. ./wpa_cli

    >p2p_find

    FAIL



    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    lsmod output



    bcma 52096 3 b43,brcmsmac


    cordic 12574 1 brcmsmac

    brcmutil 15618 1 brcmsmac

    b43 387371 0

    mac80211 630728 2 b43,brcmsmac

    cfg80211 484040 3 b43,brcmsmac,mac80211



    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



    iw list output



    * P2P-client: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0

    * P2P-GO: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0

    * P2P-client: 0x40 0xd0

    * P2P-GO: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0



    * IBSS

    * IBSS: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0

    * IBSS: 0x40 0xb0 0xc0 0xd0

    Device supports RSN-IBSS.

    Device supports HT-IBSS.



    * managed

    * managed: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0

    * managed: 0x40 0xd0



    * monitor

    * monitor



    * AP

    * AP/VLAN

    * AP/VLAN

    * AP: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0

    * AP/VLAN: 0x00 0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80 0x90 0xa0 0xb0 0xc0 0xd0 0xe0 0xf0

    * AP: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0

    * AP/VLAN: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0





    Regards,

    Gangadhar

    ReplyDelete
  16. Hi,

    I have a Netgear wna1100 usb wlan adaptor. I have followed above tutorial and installed the drivers for this on ubuntu. I am using ath9k_htc driver version 1.3.
    With iw list the device shows that it supports AP,P2P modes.
    But when I am trying to run the following command,

    ./wpa_supplicant -i wlan1 -c /etc/wpa_supplicant/wpa_supplicant.conf -Dnl80211

    output:
    Successfully initialized wpa_supplicant
    nl80211: Could not configure driver mode
    nl80211: deinit ifname=wlan1 disabled_11b_rates=0
    wlan1: Failed to initialize driver interface.

    And after this the led (present on the USB adaptor) which was ON before executing this command will turn OFF.

    I have to use service hostapd restart to start that LED again(to start wifi dongle).

    Could you please help me with this?

    Thanks

    ReplyDelete
    Replies
    1. Check if any other instance of wpa_supplicant or hostapd is running. killall wpa_supplicant and killall hostapd before you run wpa_suuplicant. Also run wpa_supplicant with -dddt option to get to know more information

      Delete
  17. Thanks for your informative post. I am amazed that in four years there has been no advance to make Wifi Direct easier to setup.

    I am using the ath9k_htc driver, and everything goes well following your instructions, but the very last step.

    When running P2P_find in wpa_cli I get CONTROL-EVENT-SCAN-STARTED multiple times.

    I don't have an SSID published either...

    Are you still available to help on this? Do you know of a newer more effective way of getting Wifi Direct going?

    All the best,

    Dave

    ReplyDelete