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