Difference between revisions of "Ad-hoc networking"

From ArchWiki
Jump to: navigation, search
(Requirements: Add link to compatible drivers.)
 
(38 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Category:Networking]]
+
[[Category:Wireless networking]]
[[Category:Wireless Networking]]
+
[[ja:アドホックネットワーク]]
 
[[zh-CN:Ad-hoc networking]]
 
[[zh-CN:Ad-hoc networking]]
{{stub}}
+
{{Related articles start}}
You can setup ad-hoc [[network]]ing to make two computers communicate or share an internet connection. The following scripts serve as an example. '''You will have to change the interface names for this to work on your system'''.
+
{{Related|Network configuration}}
 +
{{Related|Wireless network configuration}}
 +
{{Related|Software access point}}
 +
{{Related|Internet sharing}}
 +
{{Related articles end}}
 +
An IBSS (Independent Basic Service Set) network, often called an ad-hoc network, is a way to have a group of devices talk to each other wirelessly, without a central controller. It is an example of a peer-to-peer network, in which all devices talk directly to each other, with no inherent relaying.
  
== On the server ==
+
For example, ad-hoc networking may be used to [[Internet sharing|share an internet connection]].
The server script works for a wireless card with an atheros driver. To make it work with a different card is trivial. More info there: https://help.ubuntu.com/community/WifiDocs/Adhoc.
+
If you just want to connect two computers with no sharing of internet connection, you can leave out the iptables bit in the server script.
+
  
#!/bin/bash
+
== Requirements ==
####### this part should change or simply be removed depending on the network card
+
#recreates the ath0 interface in ad hoc mode
+
modprobe -r ath_pci
+
modprobe ath_pci autocreate=adhoc
+
wlanconfig ath0 destroy
+
wlanconfig ath create wlandev wifi0 wlanmode adhoc
+
#######
+
+
#starts the ad hoc server
+
ifconfig ath0 down
+
iwconfig ath0 mode ad-hoc
+
iwconfig ath0 channel 4
+
iwconfig ath0 essid 'mine'
+
ifconfig ath0 10.0.0.1 up
+
+
#forwards the ad hoc network to the router
+
iptables -A FORWARD -i ath0 -o eth0 -s 10.0.0.0/24 -m state --state NEW -j ACCEPT
+
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
+
iptables -A POSTROUTING -t nat -j MASQUERADE
+
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
+
  
Here is a [[WPA Supplicant]] config file WPA based adhoc network.
+
* A [https://wireless.wiki.kernel.org/en/users/drivers| nl80211 compatible] wireless device (e.g. ath9k) on all devices which will connect to the network
  ctrl_interface=/var/run/wpa_supplicant
+
 
ctrl_interface_group=wheel
+
== Wifi link layer ==
ap_scan=2
+
 
network={
+
Since IBSS network is a peer-to-peer network, the steps necessary to set up the wifi link layer should be the same on all devices.
     ssid="my-wifi"
+
 
 +
{{Tip|It is possible to create complex network topologies, see [http://wireless.kernel.org/en/users/Documentation/iw/vif Linux Wireless documentation] for advanced examples.}}
 +
 
 +
=== Manual method ===
 +
 
 +
{{Warning|This method creates '''unencrypted''' ad-hoc network. See [[#WPA supplicant]] for method using WPA encryption.}}
 +
 
 +
See [[Wireless network configuration#Manual setup]] for a better explanation of the following commands. Make sure that {{Pkg|iw}} is [[pacman|installed]].
 +
 
 +
Set the operation mode to ibss:
 +
 
 +
  # iw ''interface'' set type ibss
 +
 
 +
Bring the interface up (an additional step like {{ic|rfkill unblock wifi}} might be needed):
 +
 
 +
# ip link set ''interface'' up
 +
 
 +
Now you can create an ad-hoc network. Replace ''your_ssid'' with the name of the network and ''frequency'' with the frequency in MHz, depending on which channel you want to use. See the Wikipedia page [[Wikipedia:List_of_WLAN_channels#Interference_Concerns|List of WLAN channels]] for a table showing frequencies of individual channels.
 +
 
 +
# iw ''interface'' ibss join ''your_ssid'' ''frequency''
 +
 
 +
=== WPA supplicant ===
 +
 
 +
{{Note|This method creates ad-hoc network using WPA encryption. WPA2 is currently not supported (August 2013).}}
 +
 
 +
Ensure that {{Pkg|wpa_supplicant}} is [[pacman|installed]], and create a configuration file for it (see [[WPA supplicant]] for details).
 +
 
 +
{{hc|/etc/wpa_supplicant-adhoc.conf|<nowiki>
 +
ctrl_interface=DIR=/run/wpa_supplicant GROUP=wheel
 +
 
 +
# use 'ap_scan=2' on all devices connected to the network
 +
ap_scan=2
 +
 
 +
network={
 +
     ssid="MySSID"
 
     mode=1
 
     mode=1
     frequency=2412
+
     frequency=2432
 
     proto=WPA
 
     proto=WPA
     key_mgmt=NONE
+
     key_mgmt=WPA-NONE
 
     pairwise=NONE
 
     pairwise=NONE
 
     group=TKIP
 
     group=TKIP
     psk="s0m3k3y$"
+
     psk="secret passphrase"
}
+
}
 +
</nowiki>}}
 +
 
 +
Run ''wpa_supplicant'' on all devices connected to the network with the following command:
 +
 
 +
# wpa_supplicant -B -i ''interface'' -c /etc/wpa_supplicant-adhoc.conf -D nl80211,wext
 +
 
 +
== Network configuration ==
  
== On the client ==
+
The final step is to assign an IP address to all devices in the network. There are multiple ways to do this:
Run the following on the client. If you want multiple clients, you will have to change the IP address to be unique.
+
#!/bin/bash
+
iwconfig eth1 essid mine
+
iwconfig eth1 mode Ad-Hoc
+
ifconfig eth1 10.0.0.2 up
+
route add default gw 10.0.0.1
+
  
To share an internet connection, you need to tell the clients about the DNS servers. See the /etc/resolv.conf file on the server.
+
* Assign static IP addresses. See [[Network configuration#Static IP address]] for details.
 +
* Running DHCP server on one device. See [[dhcpd]] or [[dnsmasq]] for details.
 +
* Running ''avahi-autoipd''. See [[Avahi#Obtaining IPv4LL IP address]] for details.
  
== Using NetworkManager ==
+
If you want to share an internet connection to the ad-hoc network, see [[Internet sharing]].
  
Install and set-up [[NetworkManager]] on both the server and the client.
+
== Tips and tricks ==
  
=== On the Server ===
+
=== Using NetworkManager ===
Plug the wireless adapter into the computer which you wish to use as the server. Click on the nm-applet in the dock and choose "Create New Wireless Network".
+
  
With the dialog box, type in your SSID and choose your security type and password, then click "Create".
+
If you use [[NetworkManager]], you can use ''nm-applet'' for ad-hoc network configuration instead of the manual method described above. See [[NetworkManager#Ad-hoc]] for details.
  
Afterwards, right-click on the nm-applet in the dock and choose "Edit Connections". Click on the "Wireless" tab and select the SSID for the network you just created. Click on "Edit".
+
{{Note|NetworkManager does not support WPA encryption in ad-hoc mode.}}
  
With the dialog box which pops up, make sure "Mode" is set to "Ad-hoc".
+
=== Custom systemd service (with wpa_supplicant and static IP) ===
  
In "IP4 Settings", change "Method" to "Manual" and click on "Add" next to the "Addresses" box. It then should highlight the box to the left where you have to enter three IP addresses.
+
You can use the following templates to enable wireless ad-hoc networking:
  
For "Address" choose a value between 192.168.0.0 to 192.168.255.255; "Netmask", 255.255.255.0; "Gateway", 1.1.1.1. then click save.
+
{{hc|/etc/conf.d/network-wireless-adhoc@<interface>|<nowiki>
 +
addr=192.168.0.2
 +
mask=24
 +
</nowiki>}}
  
Finally, click on the nm-applet in the tray again and select "Connect to Hidden Wireless Network"; then in the "Connection" drop-down box select the network which you just created. It should then connect.
+
{{hc|/etc/systemd/system/network-wireless-adhoc@.service|<nowiki>
 +
[Unit]
 +
Description=Ad-hoc wireless network connectivity (%i)
 +
Wants=network.target
 +
Before=network.target
 +
BindsTo=sys-subsystem-net-devices-%i.device
 +
After=sys-subsystem-net-devices-%i.device
  
=== On the Client ===
+
[Service]
 +
Type=oneshot
 +
RemainAfterExit=yes
 +
EnvironmentFile=/etc/conf.d/network-wireless-adhoc@%i
  
On the nm-applet in the tray, click on the wireless network you created and connect to it; afterwards, right-click and choose "Edit Connections". Again, click on the "Wireless" tab and select the SSID for your network and choose "Edit".
+
# perhaps rfkill is not needed for you
 +
ExecStart=/usr/bin/rfkill unblock wifi
 +
ExecStart=/usr/bin/ip link set %i up
 +
ExecStart=/usr/bin/wpa_supplicant -B -i %i -D nl80211,wext -c /etc/wpa_supplicant-adhoc.conf
 +
ExecStart=/usr/bin/ip addr add ${addr}/${mask} dev %i
  
In the "IP4 Settings" again change the "Method" to "Manual" and click on "Add" next to the "Addresses" box.
+
ExecStop=/usr/bin/ip addr flush dev %i
 +
ExecStop=/usr/bin/ip link set %i down
  
For the "Address" choose a value between 192.168.0.0 to 192.168.255.255 '''but make sure it is different to the one you selected for the server'''; afterwards fill in "Netmask", 255.255.255.0; "Gateway", 1.1.1.1. then click save.
+
[Install]
 +
WantedBy=multi-user.target
 +
</nowiki>}}
  
It should then allow you to connect to the server (try pinging the server to ensure this).
+
== See also ==
  
== Other Resources ==
+
* [https://help.ubuntu.com/community/WifiDocs/Adhoc Ubuntu community wiki WifiDocs/Adhoc]
* [http://www.ubuntugeek.com/creating-an-adhoc-host-with-ubuntu.html Creating an Ad-Hoc Host with Ubuntu]
+
* [http://www.ubuntugeek.com/creating-an-adhoc-host-with-ubuntu.html Manual about creating an Ad-Hoc network on UbuntuGeek]
 +
* [http://go2linux.garron.me/linux/2011/03/share-your-3g-internet-connection-over-wifi-linux-ipod-touch-925 Share your 3G Internet connection over wifi]

Latest revision as of 09:02, 19 November 2015

An IBSS (Independent Basic Service Set) network, often called an ad-hoc network, is a way to have a group of devices talk to each other wirelessly, without a central controller. It is an example of a peer-to-peer network, in which all devices talk directly to each other, with no inherent relaying.

For example, ad-hoc networking may be used to share an internet connection.

Requirements

  • A nl80211 compatible wireless device (e.g. ath9k) on all devices which will connect to the network

Wifi link layer

Since IBSS network is a peer-to-peer network, the steps necessary to set up the wifi link layer should be the same on all devices.

Tip: It is possible to create complex network topologies, see Linux Wireless documentation for advanced examples.

Manual method

Warning: This method creates unencrypted ad-hoc network. See #WPA supplicant for method using WPA encryption.

See Wireless network configuration#Manual setup for a better explanation of the following commands. Make sure that iw is installed.

Set the operation mode to ibss:

# iw interface set type ibss

Bring the interface up (an additional step like rfkill unblock wifi might be needed):

# ip link set interface up

Now you can create an ad-hoc network. Replace your_ssid with the name of the network and frequency with the frequency in MHz, depending on which channel you want to use. See the Wikipedia page List of WLAN channels for a table showing frequencies of individual channels.

# iw interface ibss join your_ssid frequency

WPA supplicant

Note: This method creates ad-hoc network using WPA encryption. WPA2 is currently not supported (August 2013).

Ensure that wpa_supplicant is installed, and create a configuration file for it (see WPA supplicant for details).

/etc/wpa_supplicant-adhoc.conf
ctrl_interface=DIR=/run/wpa_supplicant GROUP=wheel

# use 'ap_scan=2' on all devices connected to the network
ap_scan=2

network={
    ssid="MySSID"
    mode=1
    frequency=2432
    proto=WPA
    key_mgmt=WPA-NONE
    pairwise=NONE
    group=TKIP
    psk="secret passphrase"
}

Run wpa_supplicant on all devices connected to the network with the following command:

# wpa_supplicant -B -i interface -c /etc/wpa_supplicant-adhoc.conf -D nl80211,wext

Network configuration

The final step is to assign an IP address to all devices in the network. There are multiple ways to do this:

If you want to share an internet connection to the ad-hoc network, see Internet sharing.

Tips and tricks

Using NetworkManager

If you use NetworkManager, you can use nm-applet for ad-hoc network configuration instead of the manual method described above. See NetworkManager#Ad-hoc for details.

Note: NetworkManager does not support WPA encryption in ad-hoc mode.

Custom systemd service (with wpa_supplicant and static IP)

You can use the following templates to enable wireless ad-hoc networking:

/etc/conf.d/network-wireless-adhoc@<interface>
addr=192.168.0.2
mask=24
/etc/systemd/system/network-wireless-adhoc@.service
[Unit]
Description=Ad-hoc wireless network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/network-wireless-adhoc@%i

# perhaps rfkill is not needed for you
ExecStart=/usr/bin/rfkill unblock wifi
ExecStart=/usr/bin/ip link set %i up
ExecStart=/usr/bin/wpa_supplicant -B -i %i -D nl80211,wext -c /etc/wpa_supplicant-adhoc.conf
ExecStart=/usr/bin/ip addr add ${addr}/${mask} dev %i

ExecStop=/usr/bin/ip addr flush dev %i
ExecStop=/usr/bin/ip link set %i down

[Install]
WantedBy=multi-user.target

See also