Introduction

It is easy to setup ad-hoc wifi router in Linux. The problem is, many devices e.g Android (currently) can not connect to it. Network Manager 0.9.8 brings AP-mode Hotspot. However, since it is not integrated into nm-applet or other easy tools, we will still have to wait some time for a seemless experience. Until then, we will have to rely on hostapd software.

Is your hardware supported ?

According to linuxwireless.org

As far as Linux is concerned, out of the old drivers you can only use HostAP, madwifi, prism54 drivers with hostapd. All new mac80211 based drivers that implement AP functionality are supported with hostapd’s nl80211 driver. Userspace programs like hostapd now use netlink (the nl80211 driver) to create a master mode interface for your traffic and a monitor mode interface for receiving and transmitting management frames.

Enough theory. You can find out if your card falls under one of these categories using following command.

$ lspci -k | grep -A 3 -i "network" 02:00.0 Network controller: Intel Corporation Centrino Wireless-N 1030 [Rainbow Peak] (rev 34) Subsystem: Intel Corporation Centrino Wireless-N 1030 BGN Kernel driver in use: iwlwifi

As you can see, the driver used in this case is iwlwifi.

Let us find out more information about this driver’s module.

$ modinfo iwlwifi | grep 'depend' depends: cfg80211

To find out if this driver is supported by hostapd, I just do is search the term “cfg80211 hostapd” on google. In this limited post I cannot list all supported drivers.

Installation

You will need to install two software (hostapd and dhcp3-server). On ubuntu installing these software would require following command.

$ sudo apt-get install hostapd dhcp3-server

Hostapd is used to configure our wireless driver to be used as a router, and dhcp server provides ip addresses to connecting devices.

Configuration

This part might look like a lot of work, but it is the most easiest part of the whole tutorial. You just need to copy and paste following texts to necessary files.

a. Create /etc/hostapd/hostapd.conf file and paste the following text.

interface=wlan0 driver=nl80211 ssid=testhostapd channel=1 hw_mode=g auth_algs=1 wpa=3 wpa_passphrase=mypassword wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP CCMP rsn_pairwise=CCMP

Note: Please change some of these values to match your setting. In a laptop most often the wireless interface is wlan0, but to be sure run ifconfig command and find out.

$ ifconfig wlan0 Link encap:Ethernet ...

You might also want to change the driver (as discussed above), router name (ssid), wireless password (wpa_passphrase) etc.

b. Edit /etc/dhcp/dhcpd.conf file to look like this

ddns-update-style none; default-lease-time 600; max-lease-time 7200; log-facility local7; # option definitions common to all supported networks… # option domain-name “example.org”; # option domain-name-servers ns1.example.org, ns2.example.org; option domain-name-servers 208.67.220.220,208.67.222.222 ; subnet 10.42.43.0 netmask 255.255.255.0 { range 10.42.43.50 10.42.43.70; option subnet-mask 255.255.255.0; option broadcast-address 10.42.43.255; option routers 10.42.43.1; }

What we did above is, create a dhcp server configuration, commented option lines and left other lines untouched. Lastly added dhcp ip range we want our devices to use.

c. Finally create a file (any name you like) in your home directory. In this example I will create a file called connecthostapd in bin directory of home (~/bin/connecthostapd) with following text.

#!/bin/bash rmmod iwlwifi modprobe iwlwifi iwconfig wlan0 mode Master freq 2.42G essid testhostapd sleep 2 ifconfig wlan0 10.42.43.1/24 iptables -t nat -A POSTROUTING -s 10.42.43.0/24 -o ppp0 -j MASQUERADE iptables -A FORWARD -s 10.42.43.0/24 -o ppp0 -j ACCEPT iptables -A FORWARD -d 10.42.43.0/24 -m state --state ESTABLISHED,RELATED -i ppp0 -j ACCEPT echo 1 >/proc/sys/net/ipv4/conf/all/forwarding echo 'INTERFACES=wlan0' >/etc/default/dhcp dhcpd wlan0 hostapd -d /etc/hostapd/hostapd.conf

In this particular example, I used DSL internet available through ppp0 as the source. If you have some other devices or interface that you want to use (e.g eth0 in case of direct connection) as the internet source, change ppp0 to appropriate device interface in all three places in above file. Make sure the essid matches the name that we used in /etc/hostapd/hostapd.conf, and finally the kernel module we discovered above for wifi (iwlwifi in this case) .

Note: Don’t forget to make this file executable using “chmod +x command”

$ chmod +x ~/bin/connecthostapd

For the sake of completion (dhcp), I suggest you restart your computer this one time.

From now on, you just have to remember only the last file. If you have been browsing the internet using wifi, and decide to use it as a router, you just need to disconnect wifi (don’t turn off), and run above file as root. Which would be something like this.

$ sudo ~/bin/connecthostapd

You can now connect to this device as a normal wifi. To disconnect, simply type “Ctrl + c ” in the terminal where connecthostapd is currently running.

Note: Due to its limitations (in my case) on failing to keep the connection for long time, this is not going to be a replacement for wifi router, in some of your cases. However in situations where wifi does not work, and you want to update some internet related services on devices that depend on wifi (e.g Android) you can do it easily, with above setup.

Conclusion

Sharing internet through wifi has recently got much attention. As a result, most newer hardware support this feature. Hope it works for you. One problem I have to note though is, the performance is rather limited than a dedicated wifi router.

References

1. http://projects.gnome.org/NetworkManager/

2. http://www.linux.org.ru/forum/general/9047756

3. http://www.phoronix.com/scan.php?page=news_item&px=MTMwNzY

4. http://linuxwireless.org/en/users/Documentation/hostapd