WiFi to Ethernet Raspberry Pi Router

The WiFi access point is quite far from my desk so the signal is very unstable. It is a bit tricky, but possible to setup an intermediate access point with Raspberry Pi and another router.

My Raspberry Pi 2 does not have WiFi onboard so I have equiped it with two WiFi adapters:

$ lsusb
Bus 001 Device 005: ID 7392:a812 Edimax Technology Co., Ltd 
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter

Edimax adapter requires installing additional driver[1] and I've done it already. The Ralink adapter was configered to be an access point[2] so I can connect and manage my RPI.

I've connected the Lincsys E1200 router to RPI via Ethernet (Internet socket on the router is connected to Ethernet socket on RPI) and configured its wireless settings. Nothing surprising here. The connectivity is set to Automatic - DHCP.

The configuration files are the following:

/etc/dnsmasq.conf
-----------------
no-resolv
server=8.8.8.8
interface=wlan1
interface=eth0
no-dhcp-interface=wlan0
dhcp-range=wlan1,192.168.5.20,192.168.5.100,255.255.255.0,24h
dhcp-range=eth0,192.168.6.20,192.168.6.100,255.255.255.0,24h


/etc/dhcpcd.conf, at the very end:
----------------------------------

# Do not run DHCP on wlan1, it is access point.
denyinterfaces wlan1

# Do not run DHCP on eth0, it is DHCP server.
denyinterfaces eth0

/etc/network/interfaces
-----------------------
auto lo
iface lo inet loopback

auto wlan0
iface wlan0 inet dhcp
  metric 10
  wpa-ssid "VM0XXXXXXX"
  wpa-psk "XXXXXXXXXXXXXXX"

allow-hotplug wlan1
iface wlan1 inet static
  metric 100
  address 192.168.5.1
  netmask 255.255.255.0
  network 192.168.5.0

auto eth0
iface eth0 inet static
  metric 100
  address 192.168.6.1
  netmask 255.255.255.0
  network 192.168.6.0
  
/etc/hostapd/hostapd.conf
-------------------------
interface=wlan1
ssid=RPI
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=XXXXXXXXXXXX
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

After powering everything up the router successfully gets its IP address. the google.com is reachable from the RPI. The tricky part is to link wlan0 and eth0. The bridging approach from the official documentation[3] does not work in my case (looks like the some kind of incompatibility) so I end up using the traditional NAT/masquerade approach.

First, enable ipv4 forwarding. Run sudo sysctl -w net.ipv4.ip_forward=1 and uncomment net.ipv4.ip_forward=1 in /etc/sysctl.conf to make this change permanent.

Then add iptables instructions to the interfaces file:

auto eth0
iface eth0 inet static
  metric 100
  address 192.168.6.1
  netmask 255.255.255.0
  network 192.168.6.0

  ## remove rules and chains first
  up /sbin/iptables -F
  up /sbin/iptables -X
  up /sbin/iptables -t nat -F

  ## mask eth0, activate port forwarding and nat
  up /sbin/iptables -A FORWARD -o wlan0 -i eth0 -s 192.168.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
  up /sbin/iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  up /sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

Reboot RPI or run the iptable commands to enable NAT for wlan0/eth0.


  1. Using RTL8812 Wi-Fi Adapter with Raspberry Pi, https://alexatnet.com/using-rtl8812-wi-fi-adapter-with-raspberry-pi/ ↩︎

  2. Access Point on Raspberry Pi, https://alexatnet.com/raspberry-pi-acces-point/ ↩︎

  3. SETTING UP A RASPBERRY PI AS AN ACCESS POINT IN A STANDALONE NETWORK, https://www.raspberrypi.org/documentation/configuration/wireless/access-point.md ↩︎