NOTE: Although this guide should work in most cases, it is not flawless and still requires few minor modifications to make the process bug-free. Please do point out corrections and changes.
(After you are done with this post, please do checkout my Python Hostapd Client)
I was recently looking into ways to use my laptop’s wifi adapter as a wireless access point to enable my phone (Nokia E63) and playstation portable to connect to the internet through it. Ad-hoc feature may be used to share internet through wifi, but it doesn’t work with many phones and my PSP. I found connectify and virtual router for Windows which served this purpose, unsatisfactorily. Other than the reasons like Virtual Router not detecting my 3g modem and Connectify (free version) not allowing me to set desired ssid for my virtual access point, the biggest issue with these two was the limited modes available for the access point. Both the programs offered only WPA2-PSK encryption for infrastructure mode and WEP and open encryption for ad-hoc modes. Many devices connect only through infrastructure mode and support for WPA2-PSK is absent in few devices (including the PSP). Also, since I am a Linux user, I needed something else.
This is where hostapd kicks in.
HOSTAPD
“hostapd is a user space daemon for access point and authentication servers. “
In simple words, hostapd allows you to create software wifi access points allowing decent amount of configuration options. In rest of this post, I will show how to create a software access point in Linux using hostapd and share your internet to the devices through it. I have used my Lenovo Z560 with ath9k wifi driver under Arch Linux and have also tested it under Ubuntu 11.10. But the method is also applicable for other Linux distros and supported hardware.
If the method works/doesn’t work for a non-Atheros wifi card, please do comment.
REQUIREMENTS
- Supported Wireless Card (ie. supports master mode)
- An internet connection you want to share. (not strictly a neccessity)
- A linux distro
CHECKING WIFI CARD SUPPORT
First of all, you will need to find if your wireless card is supported by hostapd.
To check what kernel driver is in use for your wireless card, type the follwing in the terminal
1 | lspci -k | grep -A 3 -i "network" |
Look for the section in the output which corresponds to your Wireless controller. In my case, it is
06:00.0 Network controller: Atheros Communications Inc. AR9285 Wireless Network Adapter (PCI-Express) (rev 01)
Subsystem: Lenovo Device 30a1
Kernel driver in use: ath9k
The Bold part is my kernel driver in use. It will vary depending on your wifi card and driver you are using.
Now get the interface details of your wireless driver by
1 | modinfo ath9k | grep 'depend' |
replace ath9k by your wifi kernel driver you determined in the last step. In my case, the output was
depends: ath9k_hw,mac80211,ath9k_common,ath,cfg80211
modinfo says my Kernel driver supports mac80211 interface which is supported by hostapd which implies that my wifi card is compatible with hostapd.
Supported wireless cards/drivers
- Linux mac80211 drivers
- Host AP driver for Prism2/2.5/3
- madwifi (Atheros ar521x)
- BSD net80211 layer (e.g., Atheros driver) (FreeBSD 6-CURRENT)
INSTALLING HOSTAPD
Install Hostapd from your distro’s repo
sudo apt-get update && sudo apt-get install hostapd |
Or Download Hostapd here and compile it.
CONFIGURING HOSTAPD
The /etc/hostapd/hostapd.conf is the main configuration which you need to deal with in order to set up a SoftAP.
This is the minimal configuration setting which will let you test if hostapd is working. Create a file ~/hostapd-test.confwith the following content:
start hostapd by
1 | sudo hostapd ~/hostapd- test .conf |
Use a wifi device to check if the access point is being detected. You won’t be able to connect to it at present.
Once hostapd is working fine, its time to configure hostapd with more options.
Here is a brief overview of some of its options:
Once hostapd is working fine, its time to configure hostapd with more options.
Here is a brief overview of some of its options:
06 | ssid=dontMessWithVincentValentine |
14 | ignore_broadcast_ssid=0 |
27 | wpa_passphrase=KeePGuessinG |
So, here is my complete /etc/hostapd/hostapd.conf with WPA authentication options.
03 | ssid=dontMessWithVincentValentine |
08 | ignore_broadcast_ssid=0 |
10 | wpa_passphrase=KeePGuessinG |
SETTING UP THE DHCP SERVER
Now that hostapd is running fine, you need to setup a DHCP server to run along with hostapd in order to assign ip address to the devices connecting to the access point. Setting up a dhcp server is quite straightforward.
Install dhcp server from your distro’s repo.
sudo apt-get update && sudo apt-get install dhcp3-server |
edit /etc/dhcpd.conf (for arch linux) or /etc/dhcp/dhcpd.conf (for Ubuntu) to
01 | ddns-update-style none; |
04 | option local -wpad code 252 = text; |
07 | 10.0.0.0 netmask 255.255.255.0 { |
15 | option broadcast-address |
18 | option domain-name-servers |
19 | 10.0.0.1, 8.8.8.8, 8.8.4.4; |
22 | range 10.0.0.3 10.0.0.13; |
23 | default-lease- time 1209600; |
24 | max-lease- time 1814400; |
options are easy to understand and you may change it according to your needs (if required).
FINAL STEPS
The final steps involves enabling NAT to share internet in one network interface with the clients connected through hostapd.
I have included all the steps to configure wlan interface, enable NAT, start DHCP server and hostapd in the BASH script below
Let the name of this file be initSoftAP.Copy the BASH file below to the file initSoftAP.(and make changes to file according to your system)
03 | ifconfig $1 up 10.0.0.1 netmask 255.255.255.0 |
08 | if [ "$(ps -e | grep dhcpd)" == "" ]; then |
14 | iptables --table nat --flush |
15 | iptables --delete-chain |
16 | iptables --table nat --delete-chain |
17 | iptables --table nat --append POSTROUTING --out-interface $2 -j MASQUERADE |
18 | iptables --append FORWARD -- in -interface $1 -j ACCEPT |
24 | sysctl -w net.ipv4.ip_forward=1 |
26 | hostapd /etc/hostapd/hostapd.conf 1>/dev/null |
Script Changes (12/9/12) : Added check for already running dhcpd process (Thanks to Panji), Added an optional line to fix issues related to PPPoE connection sharing (See lorenzo’s comment)
It might be more convenient to use hostapd -B /etc/hostapd/hostapd.conf which runs hostapd in background. (Thanks to Enda for pointing out)
Make this file executable, and run it. The syntax for executing it is
./initSoftAP wifi_card_interface interface_with_internet
The “wifi_card_interface” will be wlan0 most of the cases. For “interface_with_internet“, since I want to share internet from my ethernet network interface, I used eth0. If I ever want to share internet from my 3g modem, I use ppo0. (These values need not be same for everyone)
You may see available network interfaces by
You may see available network interfaces by
Note: If dhcpd is failing to start and throwing errors like No subnet declaration for wlan0, take a look at these comments by Maheshand Charlie.
Source : http://nims11.wordpress.com/2012/04/27/hostapd-the-linux-way-to-create-virtual-wifi-access-point/
0 comments:
Post a Comment