Linksys WRT54GL Hacks

I plan on documenting some of the changes I made to my Linksys WRT54-GL, a very hackable Linux equipped WLAN Router that is quite reasonably priced as well…


There are a number of free and Open Source replacements for the default firmware available that add powerful new features such as WDS, Client Mode, Private Hotspot, VPN and Bandwith Measurement to the device.

Firmware Tomato (Manual)
Version 1.01.0918
Mode WDS AP Mode



Some Screenshots from the Tomato Firmware


Long Term Bandwith graph  Realtime Bandwith graph  Daily Bandwith Use - The impact of the Burning Crusade beta can be clearly seen



Why Tomato?

I experimented with a other third party firmware releases for the router before deciding on Tomato :


DD-WRT is an extremely capable firmware which offers some great features, including private hotspot support, Kaid (Open Xbox Live server), VPN. It also has a great web admin interface and third party scripts 1)

OpenWrt is extremely powerful but requires quite a bit of linux knowledge to get around, not neccessarily the best choice if you're geek level is too low ;-)

X-Wrt is an extension for OpenWrt and looks like it is shaping up to become the solution with the most userfriendly UI. Doesn't seem to be quite there just yet.
There's also a firmware called Alchemy from a company called Sveasoft http://en.wikipedia.org/wiki/Sveasoft, which requires a paid subscription - something that seems rather unneccessary given the excellent quality and great community support for the free firmware solutions available.

In the end I decided to go with Tomato because of it's bandwith metering features, the very extensive and well presented QoS features and because it had the most responsive admin interface. Additionally, and this is totally subjective, the router seemed to respond a bit faster when browsing the net on Tomato. My second choice is DD-Wrt (currently running my the router I use distribute wireless in the living room), which works just fine in WDS mode with the Tomato gateway.


FTP backup for the Tomato bandwith history

The current version of tomato only supports backup to RAM (not very useful when the power goes out), NVRAM (not recommended as NVRAM has limited write cycles) and CIFS (unreliable). The backup frequency is also limited to once per hour and it does not backup the bandwith graph.

I created a few scripts to perform FTP backup for the bandwith history in Tomato instead:
The following scripts will back up your current bandwith data to an FTP server, restore it every time your WAN comes up (usually router reboot). It restores daily and monthly history, the long term and the short term bandwith graph.


A word of caution

Use a local ftp/webserver in your network that is not accessible from the internet, otherwise someone who gets access to the ftp server will be able to download a malicious script to your router.
If you absolutely must use an external server, make the wan_up script verifies the md5 sum of the downloaded scripts (ftp.sh / ftp_setup.sh) against a hash stored in a NVRAM variable on the router.


Requirements

  • A local ftp and webserver with access to the ftp root directory(if you don't want to use a webserver, replace wget calls in the following scripts with ftpget)
  • WRT54GL Tomato 1.x
  • About 5 minutes to install the scripts


How does it work

  1. When the router starts, the wan_up script which fires when the WAN interface comes up downloads a setup script from the server (I chose this method so I don't have to use up valuable RAM on the router with the setup script)
  2. The setup script halts rstats (the statistics tool tomato uses for bandwith history) and cleans all existing data
  3. It downloads the ftp backup script from the webserver and installs it into crontab
  4. It then restarts rstats
  5. Every (n) minutes (default 15) the backup scripts uploads the current working data for rstats to the ftp server.


1. The Router WAN UP Script

The script uses default values, replace them with values valid for your network:

  • 192.168.0.3 – The FTP/Webserver to backup to
(put into tomato's WAN UP script slot)
### wan_up.sh
### Downloads the ftp backup script from the 
### see http://gulbsoft.de/ for details
cd /tmp;                              # create a directory on the ramdisk 
mkdir ftpbackup;
cd ftpbackup;
wget http://192.168.0.3/ftp_setup.sh; # change for ftpget if you do not have a webserver.
chmod a+x ftp_setup.sh;               # set executable flag
./ftp_setup.sh;                       # run script


2. ftp_setup.sh

This 'setup' script stops rstats, downloads the backup from the ftp server, downloads the crontab script and restarts rstats.

The script uses default value, replace them with values valid for your network:

  • 192.168.0.3 – Your local ftp / webserver
### ftp_setup.sh
### Sets up the scheduled FTP backup on the router
### see http://gulbsoft.de/ for details
cru d boot;                 # remove previously existing cron entry for this script 
killall rstats;             # kill rstats
rm ftp.sh;                  # remove previous ftp.sh 
chmod a+x ftp.sh;           # make it executable
## crontab: run the following script every 15 minutes...
cru a boot "2,15,30,45 * * * * /tmp/ftpbackup/ftp.sh"; minutes                   
cd /tmp/var/lib/misc;                             # directory where rstats keeps its data
wget http://192.168.0.3/rstats-history.gz; # restore history file (the important one)
wget http://192.168.0.3/rstats-speed.gz;   # restore speed file 
wget http://192.168.0.3/rstats-stime;      # restore last backup time
wget http://192.168.0.3/rstats-source;     # restore source file
cd /tmp/ftpbackup;
rstats;                    # restart rstats.


3. ftp.sh

This script handles uploading of the data to the FTP server. Replace the following values with your own setup:

  • user – your ftp user account.
  • password – your ftp password.
  • 192.168.0.3 – your ftp and webserver server


### ftp.sh
### this script is run by crontab to backup your ftp data
 
## upload 
ftpput  -u user -p password 192.168.0.3 stats-history.gz /tmp/var/lib/misc/rstats-history.gz;
ftpput  -u user -p password 192.168.0.3 rstats-speed.gz /tmp/var/lib/misc/rstats-speed.gz;
ftpput  -u user -p password 192.168.0.3 rstats-stime /tmp/var/lib/misc/rstats-stime;
ftpput  -u user -p password 192.168.0.3 rstats-source /tmp/var/lib/misc/rstats-source;



Notes / Issues / Comments

Notes:

  • I am using the WAN_UP instead of STARTUP script hook to give the ftp server a bit more time to come up in case of a power outage. Getting a DHCP lease from my ISP seems to take quite a while, which gives my ftp box sufficient time to come up after a power failure.
  • You can do a manual backup using the admin interface and save the resulting file as rstats-history.gz on the ftp server if you need to for some reason.
  • The bandwith metering display reveals some interesting facts - such as that there seems to be constant 4-5KB/s down traffic of junk from my ISP - even if no local client is turned on. This adds up to quite a bit of traffic over the month and people on a limited bandwith plan (thankfully I'm not) might want to take not of such behavior..



The script as is has a few issues:

  • In case your router reboots and finds your FTP server not accessible at startup, it would restart it's bandwith history. Should the ftp server come up at some point in the future, the history on the server would be overwritten


Fix: run a crontab script on the FTP server to frequently back up your data or change the script avoid uploading if backup could not be restored from the FTP server


  • As mentioned above, a local webserver is necessary for this solution to be secure. If you need to use an external server, make sure to compare md5 hashes for the downloaded scripts against a variable stored in NVRAM.
Alternate Fix: Just put everything into the startup script and make it create the crontab script on the fly.
1) e.g. to add bandwith tracking