Using Hostapd with dnsmasq to create Virtual Wifi Access Point in Linux

NOTE: This is an alternative branch off from my previous Hostapd guide, which I really recommend going through before this.

In my previous hostapd guide, I used dhcpd to assign IP addresses to the clients connecting to the access point. While this works fine for most scenarios, it is an overkill to use dhcpd for such situations where normally the number of clients is 2-3, or around 20 at max. For such cases, dnsmasq is a better option.

Installing

Install dnsmasq from somewhere

# Arch Linux
sudo pacman -S dnsmasq
# Ubuntu
sudo apt-get install dnsmasq

Configuring dnsmasq

The main reason I am recommending dnsmasq over dhcpd is the ease in configuring it. Less hassle in configuration means less problems and better troubleshooting. Most of the problems users faced in my previous guide was dhcpd related.

The default /etc/dnsmasq.conf explains all its configuration options pretty well, so I will jump straight to what your /etc/dnsmasq.conf should look like.

Just append the following to the /etc/dnsmasq.conf

# disables dnsmasq reading any other files like /etc/resolv.conf for nameservers
no-resolv
# Interface to bind to
interface=wlan0
# Specify starting_range,end_range,lease_time
dhcp-range=10.0.0.3,10.0.0.20,12h
# dns addresses to send to the clients
server=8.8.8.8
server=8.8.4.4

Simple, isn’t it?

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 dnsmasq and hostapd in the BASH script below
Let the name of this file be initSoftAP
Copy the content below to the file initSoftAP  (Perform changes if required)

#!/bin/bash
#Initial wifi interface configuration
ifconfig $1 up 10.0.0.1 netmask 255.255.255.0
sleep 2

###########Start dnsmasq, modify if required##########
if [ -z "$(ps -e | grep dnsmasq)" ]
then
 dnsmasq
fi
###########

#Enable NAT
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface $2 -j MASQUERADE
iptables --append FORWARD --in-interface $1 -j ACCEPT

#Thanks to lorenzo
#Uncomment the line below if facing problems while sharing PPPoE, see lorenzo's comment for more details
#iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

sysctl -w net.ipv4.ip_forward=1

#start hostapd
hostapd /etc/hostapd/hostapd.conf 1> /dev/null
killall dnsmasq

It might be more convenient to use hostapd -B /etc/hostapd/hostapd.conf which runs hostapd in background, but take care of the ‘killall dnsmasq’ if you choose this option. (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

chmod +x initSoftAP
./initSoftAP wlan0 eth0  # And there you go

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 ppp0. (These values need not be same for everyone)
You may see available network interfaces by

ifconfig -a

That’s all folks!
Problems, Errors, Feedback or any alternatives? Feel free to reply.

Downgrading Packages in Arch Linux: The Worst Case Scenario

Nonsense Part (You may Skip it)

Downgrading packages is a thing i don’t often encounter while using Arch. If something breaks, i wait for it to get fixed and the update again. But this time, the case was different. Late night, after a kernel (or libnl, i don’t remember) upgrade, my hostapd stopped working, complaining about shared libraries. Shockingly, i discovered that my hostapd was still at 0.7 while the latest stable version was at 2.0!(Reason provided at end) So i did an pacman -Sc to remove older hostapd versions from cache, which didn’t change anything, then made some correct changes and upgraded hostapd to 2.0. New problems in hostapd popped up, about nl80211 being unable to set my wlan to master mode. Now ahead of me was option of spending the rest of night desperately debugging the problem or study for the exam coming morning. Hostapd not working is a more serious issue for me.
Read more of this post

Steam on Linux: First Impressions

Got a steam linux beta invite few days ago via e-mail and thus decided to give it a try.

Steam currently only has official Ubuntu 12.04 LTS Support. But there are ways to run it on different distros. Luckily, there is a PKGBUILD in AUR for steam which makes installation pretty easier. Installation is quick with a 1.4 MB steam.deb download. After starting steam, it updates itself after a heavy >100 MB download.

Steam in Linux starts with a familiar “Connecting Steam Account” box.

Steam Starting

Steam Starting


Read more of this post

Hostapd Python Client

Earlier i had written a guide about setting up and running hostapd. To hone my python skills and make using and configuring hostapd simpler, i wrote a python client for hostapd. I also intend this project to serve as a base for a connectify-like GUI hostapd client, which i will start working on in a few days. This project is nearly complete, with proper logging system yet to implement and fix any unknown bugs.

Getting and Installing hostapd-python-script

Just paste this on your command line.


git clone https://github.com/nims11/hostapd-python-script.git

cd hostapd-python-script

Read the README.textile file, it will get you well covered.

Contribute

Help me improve it by reporting bugs, suggestions and pull requests. I have only tested it well enough under my Arch Linux installation with ath9k wifi driver, so reporting working/non-working environments will help a lot. I believe the sourcecode is simple enough to understand and i have documented most part of it. Will document the rest in few days as well.

Thank you :)

Redirect TCP connections with rinetd

rinetd is a simple little redirection server which can redirect TCP connections from one IP:port to another IP:port. rinetd is simple enough to set up and run. I came across this while searching for a solution to make clients on my LAN to connect to my DC hub server running inside a virtual machine.
Installation is straightforward, download the source from here
Read more of this post

Nested X sessions with Xephyr

I was searching for a way to establish a multi-seat setup for my laptop display and a monitor using a single graphics card when i came across Xephyr. (Thanks to bodhi.zazen’s post in ubuntuforums )

In this post, I have used Arch Linux with openbox running on my Lenovo Z560. The process should, however, work with all Linux Distros.

KDE session nested inside an openbox session through Xephyr

Read more of this post

Hostapd : The Linux Way to create Virtual Wifi Access Point

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.
Read more of this post

BrainFuck : An eight-command awesome esoteric programming language

I was going through the SPOJ (SPhere Online Judge) challenge problems when i saw this problem in which the aim was to print a line using brainf**k with minimum source code size. If other well known languages were allowed, this problem would have been ridiculous. So i decided to try my hands on this esoteric language. Given its weird nature and limited features, playing with it is quite fun and the best part about it is that it takes you only around 10 minutes to learn this language. Read more of this post

Conversion among Data Types in C++

As a Programmer, i have quite often encountered situations where i need to convert one data type to another, especially conversions between int/float to strings. Be it solving problems on online judge or during implementation of mathematical parser or data type to hold BIG INTEGERS (bigger than what a fundamental data type can hold), these kind of conversions are quite useful. Here, i discuss few methods i use to achieve it. Read more of this post

Bootable flash Drives

Bootable optical discs are a thing of past (atleast for me). With usb flash drives packed with few gigs of storage becoming cheaper, its convenient to have a fully equipped pen drive in your pocket rather than carrying CDs/DVDs around. In this article, i will discuss about bootable USB drives and how to make one using various tools. Read more of this post