Monday, 12 May 2014

Sharing one wifi adapter with many devices

Diagram of the network. The dotted lines are wifi connections, and the continuous line is an Ethernet cable

Why: The area where I live currently has free wifi internet for anyone to use, however the signal is very weak, and only one of my wifi devices (a TP-Link TL-WN722N USB wifi adapter) can receive the signal.
I want to be able to share the "free wifi" network with all of my devices.
This allows me to get rid of my terrible, overpriced, unreliable Telstra Bigpond ADSL.

How: I have the TP-Link TL-WN722N plugged into a Toshiba Tecra M5 laptop running Debian Stable. There are two network adapters connected to the laptop; the TP-Link TL-WN722N USB wifi adapter, which is labelled  "wlan2", and a built-in Ethernet port, labelled "eth0". The Ethernet port on the laptop is connected to the WAN port on my Belkin wireless router. The laptop is configured as a router which sends IP packets between the "wlan2" and "eth0" networks. This means that any device connected to the Belkin router (by a wired or wireless connection) will be able to access the internet through the "wlan2" adapter on the laptop.

The following instructions could serve as a basis for your own configuration.
The instructions should work on any Debian-based Linux distribution, but other distros may have things (such as the /etc/network/interfaces folder) configured differently.  Note that you should change "eth0" and "wlan2" to match your own network interface names. If you are confused about any of these steps, the list of useful links at the bottom of the page may help you.

The laptop is configured using the following steps:

1. Enable IP packet fowarding:
Open the file /etc/sysctl.conf in your preferred text editor, and add (or uncomment) the line net.ipv4.ip_forward = 1

2. Install dnsmasq:
You can do this by entering the following in a terminal: sudo apt-get install dnsmasq

3. Configure dnsmasq:
dnsmasq needs to be configured to serve IP addresses to a device conneced to the "eth0" network (in my case, the Belkin wireless router). To configure dnsmasq, add the following lines to /etc/dnsmasq.conf


4. Configure the "eth0" interface with a permanent IP address
This is done by adding the following lines to the file 

auto eth0
iface eth0 inet static

5. Reboot the laptop

6. Create an IP packet routing script:
The following script will tell iptables to do the packet routing between the "wlan2" and "eth0" networks. To create the script, create the file /etc/network/router_firewall and add the following:

# script for source Network Address Translation using iptables

iptables -F
iptables -t nat -F
iptables -X

iptables -N val_input
iptables -N val_output

# allow packets with NEW, ESTABLISHED and RELATED states
iptables -A val_input -m state --state NEW,ESTABLISHED,RELATED -i lo -j RETURN
iptables -A val_output -m state --state NEW,ESTABLISHED,RELATED -o lo -j RETURN

iptables -A val_input -m state --state NEW,ESTABLISHED,RELATED -i eth0 -j RETURN
iptables -A val_output -m state --state NEW,ESTABLISHED,RELATED -o eth0 -j RETURN

iptables -A val_input -m state --state NEW,ESTABLISHED,RELATED -i wlan2 -j RETURN
iptables -A val_output -m state --state NEW,ESTABLISHED,RELATED -o wlan2 -j RETURN

iptables -A val_input -j DROP
iptables -A val_output -j DROP

iptables -A INPUT -p tcp -j val_input
iptables -A OUTPUT -p tcp -j val_output

iptables -t nat -A POSTROUTING -o wlan2 -j MASQUERADE
# End of script

The script needs to be given executable permissions by entering the following into a terminal:  sudo chmod +x /etc/network/router_firewall

The script must be executed for the routing to work. This can be done manually by entering the following in a terminal: sudo /etc/network/router_firewall

7. Setting up DNS nameservers:
I changed the DNS nameserver settings on the Belkin router to "" and "", which are google's nameservers. 

Useful external links: 

No comments:

Post a Comment