Linux WiFi Ad-hoc mode

Ad-hoc, or IBSS mode allows you to create a wireless network without a central access point. This guide demonstrates a simple Ad-hoc network using one of our adapters and some basic tools found in most modern Linux distributions.

  • At the present time, IBSS may suffer from high levels of packet loss due to an issue with the driver.

Prerequisites

Here is a non exhaustive list of requirements:

  • An LM006 or LM820/1 WiFi module (RTL8188CUS based).
  • At the time of writing: a very recent version of the Linux Kernel (4.0.0+).
  • The iw and ip user space tools.
  • Optionally, the wpa_supplicant daemon.

Other LM products

  • The LM products listed here are not the only ones compatible with Ad-hoc mode, please contact us if you are interested in using another LM product in this way.

Starting position

We shall assume that the adapter is configured and working under station mode (the default), but hasn’t been assigned an IP address.

~ %  iw dev
phy#0
    Interface wls160u2u1
        ifindex 4
        wdev 0x1
        addr 00:02:72:b7:a2:0f
        type managed
~ %  ip addr show wls160u2u1
4: wls160u2u1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 00:02:72:b7:a2:0f brd ff:ff:ff:ff:ff:f

From the above you can see that I have a wireless interface called wls160u2u1 which is currently not in use.

More detail on getting started

  • On some systems, getting to this point could be difficult due to some pitfalls which we have not covered here. Our article on AP mode has a section on preparing your system which is mostly relevant to Ad-hoc mode as well; Linux WiFi Access Point (Wireless AP). The most common problem is interference from a network manager which will need to be disabled.

Ad-hoc interface configuration

Lets start with the steps to configure an Ad-hoc interface.

1/ First we add a second virtual interface called ibs0 to our physical adapter phy0.

~ %  sudo iw phy phy0 interface add ibs0 type ibss
~ %  iw dev
phy#0
    Interface ibs0
        ifindex 5
        wdev 0x2
        addr 00:02:72:b7:a2:0f
        type IBSS
    Interface wls160u2u1
        ifindex 4
        wdev 0x1
        addr 00:02:72:b7:a2:0f
        type managed

The new interface is of the IBSS type which stands for Independant Basic Service Set and essentially means the same thing as Ad-hoc.
2/ Next we need to join or start an Ad-hoc network.

~ %  sudo ip link set ibs0 up
~ %  sudo iw dev ibs0 ibss join ibstest 2412 key d:1:5chrs
~ %  iw dev
phy#0
    Interface ibs0
        ifindex 5
        wdev 0x2
        addr 00:02:72:b7:a2:0f
        ssid ibstest
        type IBSS
        channel 1 (2412 MHz), width: 20 MHz (no HT), center1: 2412 MHz
    Interface wls160u2u1
        ifindex 4
        wdev 0x1
        addr 00:02:72:b7:a2:0f
        type managed

This will start or join a network with the name/SSID ibstest on channel one (Frequency 2412) with the WEP key ‘5chrs’.

3/ Do the same again on a different computer.

4/ Now we can check that a connection has been made.

~ %  iw dev ibs0 link
Joined IBSS d2:fa:ad:02:83:24 (on ibs0)
    SSID: ibstest
    freq: 2412

WEP

  • At the time of writing the iw tool only appears to support WEP for encryption which can easily be brocken. If you have any security requirements at all you should investigate using wpa_supplicant instead, which can configure the interface with better encryption.

The wpa_supplicant alternative

To get WPA2 encryption we can use wpa_supplicant instead. This method requires a bit more configuration than the iw tool which we shall gloss over.

1/ Do step one from above to create the virtual interface ibs0.
2/ Create a config file with the following contents.

ctrl_interface=/var/run/wpa_supplicant
ap_scan=2
# IBSS/ad-hoc network with RSN
network={
    ssid="ibss-rsn"
    key_mgmt=WPA-PSK
    proto=RSN
    psk="12345678"
    mode=1
    frequency=2412
    pairwise=CCMP
    group=CCMP
}

3/ Start wpa_supplicant using this configuration.

~ %  sudo wpa_supplicant -Dnl80211 -iibs0 -c/etc/wpa_supplicant/ibss-test.conf
Successfully initialized wpa_supplicant
ibs0: Trying to associate with SSID 'ibss-rsn'
ibs0: Associated with 12:f6:e6:ba:ba:c7
ibs0: CTRL-EVENT-CONNECTED - Connection to 12:f6:e6:ba:ba:c7 completed [id=0 id_str=]
ibs0: IBSS-RSN-COMPLETED 5c:f3:70:1a:89:fd
ibs0: WPA: Key negotiation completed with 5c:f3:70:1a:89:fd [PTK=CCMP GTK=CCMP]
ibs0: CTRL-EVENT-EAP-FAILURE EAP authentication failed
ibs0: WPA: Group rekeying completed with 5c:f3:70:1a:89:fd [GTK=CCMP]
...

4/ See steps three and four of the iw method.

Basic IP configuration

We will just use static IP addresses for the purpose of this demonstration, for a more complicated way of doing things, see the routingsection of the Host AP article.

1/ Add a static IP to the ibs0 interface and route all traffic to the network 10.254.239.xxx through the interface ibs0.

~ %  sudo ip addr add 10.254.239.1/24 broadcast 10.254.239.255 dev ibs0
~ %  sudo ip route add 10.254.239.0/24 dev ibs0

2/ Do the same on a different computer, but with the address 10.254.239.2/24 (or anything else between 10.254.239.2 and 10.254.239.254).

3/ Now we can ping

~ %  ping 10.254.239.1 -c 10
PING 10.254.239.1 (10.254.239.1) 56(84) bytes of data.
64 bytes from 10.254.239.1: icmp_seq=1 ttl=64 time=2.68 ms
64 bytes from 10.254.239.1: icmp_seq=2 ttl=64 time=206 ms
64 bytes from 10.254.239.1: icmp_seq=3 ttl=64 time=3.48 ms
64 bytes from 10.254.239.1: icmp_seq=4 ttl=64 time=1.66 ms
64 bytes from 10.254.239.1: icmp_seq=5 ttl=64 time=1.71 ms
64 bytes from 10.254.239.1: icmp_seq=6 ttl=64 time=1.44 ms
64 bytes from 10.254.239.1: icmp_seq=7 ttl=64 time=5.23 ms
64 bytes from 10.254.239.1: icmp_seq=8 ttl=64 time=12.6 ms
64 bytes from 10.254.239.1: icmp_seq=9 ttl=64 time=1.86 ms
64 bytes from 10.254.239.1: icmp_seq=10 ttl=64 time=1.54 ms
--- 10.254.239.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9019ms
rtt min/avg/max/mdev = 1.447/23.889/206.669/61.011 ms