Monday, August 27, 2012

TP-Link TL-MR3020 as AP and router with OpenWrt

Just got a travel router TL-MR3020 and immediately flashed it with OpenWrt. By default, it will be act as an AP. Here is how to add configuration to switch between AP and router modes.

Before we start, make sure the fail safe mode is working: with the device powered off, change the switch to AP mode. Press and hold the WPS button. Turn the power on. Wait for 2 seconds until the WPS light starts to flash slowly. Immediately switch the mode to 3G while still holding the WPS button.

Now the device has IP of and you can connect to it via a LAN cable with your computer configured with IP 192.168.1.x

With fail safe mode working, reboot the device normally. Go to /etc/config and make the following changes.

Define a new wifi network. Make copies of the file network and name them network.ap and network.router. Edit network.router and remove the lan section and add the following:
config interface 'wan'
        option ifname 'eth0'
        option proto 'dhcp'

config interface 'wifi'
        option proto 'static'
        option ipaddr ''
        option netmask ''

Change the wireless settings. Make copies of the file wireless and name them wireless.ap and wireless.router. Edit wireless.router and change the wifi-iface section to suit your need. Here I use WPA2
config wifi-iface
        option device   radio0
        option network  wifi
        option mode     ap
        option ssid     put_your_wireless_ssid_here
        #option encryption none
        option encryption psk2
        option key put_your_wireless_password_here

In router mode, the device withh assign IP to others via wifi. So we need to create a new DHCP pool. Make copies of the file dhcp and name them as dhcp.ap and dhcp.router. Add the following section to dhcp.router:
config dhcp wifi
        option interface        wifi
        option start            100
        option limit            150
        option leasetime        12h

Finally, we will modify the firewall rules to allow traffic from wifi to WAN. Make copies of the file firewall and name them as firewall.ap and firewall.router. Edit firewall.router and add the following sections:
config zone
        option name             wifi
        option input            ACCEPT
        option output           ACCEPT
        option forward          REJECT

config forwarding
        option src              wifi
        option dest             wan

Now we are all set. To switch between AP and router modes, just copy the above modified files to the original file and restart wifi, firewall and dhcp services:
ifup wifi
/etc/init.d/firewall restart
/etc/init.d/dnsmasq restart

However, we can do better than that. We can map the mode switch on TL-MR3020 to auto execute the commands for us.

Create a file name /sbin/sw_ap_mode
/bin/cp -f /etc/config/dhcp.ap /etc/config/dhcp
/bin/cp -f /etc/config/firewall.ap /etc/config/firewall
/bin/cp -f /etc/config/network.ap /etc/config/network
/bin/cp -f /etc/config/wireless.ap /etc/config/wireless
/sbin/ifup wifi
/etc/init.d/firewall restart
/etc/init.d/dnsmasq restart

Create a file name /sbin/sw_router_mode
/bin/cp -f /etc/config/dhcp.router /etc/config/dhcp
/bin/cp -f /etc/config/firewall.router /etc/config/firewall
/bin/cp -f /etc/config/network.router /etc/config/network
/bin/cp -f /etc/config/wireless.router /etc/config/wireless
/sbin/ifup wan
/sbin/ifup wifi
/etc/init.d/firewall restart
/etc/init.d/dnsmasq restart

Following the instructions on to edit /etc/hotplug.d/button. Then create /etc/hotplug.d/button/00-button. Execute the following commands:
uci add system button
uci set system.@button[-1].button=BTN_0
uci set system.@button[-1].action=released
uci set system.@button[-1].handler="/sbin/sw_router_mode"
uci commit system

uci add system button
uci set system.@button[-1].button=BTN_0
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler="/sbin/sw_ap_mode"
uci commit system


Bobby Simmons said...

Great tutorial. Unfortunately, I've not been able to get the AP to give me an IP through DHCP. I've tried multiple configurations to no avail. I have the ver 1.7 and I believe there may be some hardware limitations. The manual for the device states in AP mode DHCP is disabled.


linuxtechie said...

I have MR3020 v1 and I ended up with following:

root@OpenWrt:~# cat /etc/hotplug.d/button/00-button

if [ "$ACTION" = "pressed" ]; then
if [ "$BUTTON" = "BTN_0" ]; then
logger "Router Mode"
elif [ "$BUTTON" = "BTN_1" ]; then
logger "AP Mode"

The uci commands never really worked for me :(

Abhay Ghatpande said...

Hello Clarence,

Thank you for your excellent write-up. I was successfully able to configure the MR3020.

Just one addition might prove useful to other readers in case they face the same issue as I did when trying the ssh / Luci once in AP mode from the LAN/WAN port. Change the firewall setting as given here:

I'm not sure if it is necessary, but I also added a forwarding rule from wan to wifi.

Else I have to be on the WiFi network to be able to access the LuCi or ssh into the router.

Best regards,