DD-WRT firmware on a home router
DD-WRT firmware on a home router
This is an overview article about what features a user has with a regular home router, and how to take advantage of these features.
Sooner or later, every Internet user buys a router at home, as the number of devices that require the Internet is growing. And many have children who also require an Internet connection :) But it hurts and offends me to see that people do not always understand what can and should be done to achieve maximum comfort.
A small disclaimer (I don’t know how it is in Russian).
I will use the word “right” in the sense that I myself consider it right for myself. Having passed the path from “noob” in Linux to the level “I can advise others,” I believe that my advice is at least no worse than other advice that can be found in various forums.
It will be about the firmware of the router based on the well-known DD-WRT distribution . This is the firmware that turns the router into a “regular” Linux server with very strict memory and performance limitations. Despite this, the functionality of such a server can turn out to be very rich.
Here are just a few of the features:
- Ssh
- FTP server
- HTTP server
- Openvpn
- QoS management
- Transmission
- Php
- MySQL
- RADIUS
- Asterisk
- etc etc
- Let's get started! Said the walrus, perching on a coastal stone. - It's time to talk about many things ... ( c )
1) Equipment selection
The first and most important task is to choose the right router model, as it is a one-way ticket. When evaluating your finances and your ambitions, it is important to choose a middle ground. A possible risk factor could be the fear of “messing up” the expensive router with its wrong actions. When buying an expensive thing, I myself am afraid to do something with it, especially if there is no experience. However, I can assure you: the chances of killing the router with new firmware are almost zero. Modern models have a recovery mode, and you need to really want to ditch the router (and then not forever - you just need special equipment for resuscitation ).
I will name 2 “correct” models that I have chosen for myself.
- The Linksys WRT-54GL is an entry-level model, but unrivaled in reliability. Bought more than 3 years ago, still in service, "not a single gap" ... Features: WiFi (b / g) + 4 LAN 10/100, 4Mb flash, 16Mb RAM, no USB.
- Asus WL-520GU , a modern and very cheap router, 3 months in service, also no complaints about work. Features: WiFi (b / g) + 4 LAN 10/100, 4Mb flash, 16Mb RAM, 1 USB.
The main criteria for the selection of models are:
- Compatible data models with firmware DD-WRT. The possibilities of “native” firmware were not considered at all: why choose the color of the wallpaper in the new apartment, if there is an overwhelming repair ahead?
- RAM memory capacity. Almost all modern routers have 16Mb RAM. This is just enough for normal operation, but, as you know, each program seeks to capture the entire available amount of memory. If you want to get the most out of your router or have extra money, it’s better to find a model with 32Mb of memory.
- The presence of USB ports, because all the advanced functionality is most conveniently installed on an external medium, because The flash memory of the router will not be able to accommodate all new programs. My first router did not have USB, so I had to go a little bit and “work with a file”.
2) Router firmware
Firmware - it doesn’t hurt at all! This is actually a simple operation, such as perrectal removal of the tonsils ... No, the truth is nothing complicated.
The very first change of firmware will require a little more effort, but each next one will go easier and easier.
You should not be afraid that the change of firmware will occur regularly. Choosing for yourself a set of programs included in one of the ready-made firmware, you can calm down on this and enjoy life. If you need additional functionality, you can always install new programs separately, for this there is a special mechanism. But at the first stages it may happen that the choice of firmware is unsuccessful, for example, the firmware will not have such a necessary opportunity as SAMBA support. It doesn’t matter, download another firmware, 2 mouse movements - voila!
Some models of routers (in particular Asus) require special actions (flashing memory or something like that) before flashing to DD-WRT. Carefully follow the instructions for a specific router model! Particular attention should be paid to the requirements “wait 5 minutes after the end of the firmware”. I was a little self-confident and, flashing my router to my colleague, slightly hurried to turn off the power after the end of the firmware. The result is a recovery procedure and everything is new.
If it says “wait 5 minutes” - take the hourglass for 5 minutes and wait! This is serious.
I will not consider the flashing process itself (tftp, change of address to 192.168.0.1, interface, etc.) - all this is described on the DD-WRT forums .
But, finally, you are done with the firmware, and in front of you is a wonderful new WEB-interface of your router. What's next?
3) Preparing to install OPTWARE
You have already received the basic functionality by selecting the firmware version. It remains to configure the settings for connecting to your provider (DHCP, PPTP etc), and you can already begin to surf the open spaces of the universe. For many home users this will already be enough.
Security settings, routing control, and other standard “chips” for routers, I will not describe. Anyway, not a single “normal” router can do without it.
But you can do even better! We continue the process.
All additional programs are united by the common concept of Optware . (they are placed in the / opt directory, which is automatically created by the router and is always writable). However, the / opt directory itself is empty and is actually located in RAM - that is, there is very little space and everything that gets there will be lost upon reboot. What to do?
The solution is as follows: we need a permanent place on some kind of “disk”. And although the storage facilities described do not have a disk shape in the geometric sense (rather, these are silicon crystals in plastic cases with metal leads), we will still call them disks.
Jffs
If the flash-memory of the router is large enough, and the size of the firmware is less than the total size of the flash-memory, then the remaining space can be used for data storage. If you select firmware with JFFS support, then depending on the version, you can get from 0.5 to 3Mb “drive” directly inside the router. Such a disk will be automatically mounted as / jffs at startup.
The main nuisance when working with flash-disks is their rapid degradation during recording. If the number of rewrites of the “disk” sector exceeds a certain threshold (from 10 to 100 thousand cycles), then the disk will come to an end. Everything is bad… :(
If this happens to the internal drive, it will really be bad. But if it is an external USB drive, then with a flick of the wrist we replace the USB flash drive with a new one - and again into battle. After all, USB-sticks have long been consumables, right? Moreover, a flash drive with a volume of 1 or 2Gb is sufficient. The main thing, as usual, is to make backups on time.
USB
As you already understood, the main place to install Optware is an external USB drive. It is important to keep in mind that USB must be formatted in a certain way before installation in a router. At a minimum, there must be a primary partition formatted as ext2 or ext3 .
More “correct” is the following scheme:
- ext2 partition, size ~ 400Mb - the main Optware partition, mounted as / opt
- section swap, size ~ 24Mb - for swapping, allows you to work with such "heavy" applications like RADIUS, MySQL, Apache ...
- ext2 partition, size ~ 400Mb - Optware backup partition (offline mirror)
- ext2 or fat32 partition, everything else is for user data
USB formatting is best done on a Linux machine using any available utilities, such as GParted.
Cifs
If the router does not have hardware USB, but you still want to install Optware, then you can take advantage of Linux’s remarkable ability to work with the drive through the network. To do this, we need 2 things:
- constantly working somewhere on the network server (or NAS, or just a desktop machine, even a laptop) with SAMBA support
- firmware with support for CIFS / SAMBA
If the house already has computers (and they are, because a router without computers looks ridiculous), then one of the home computers will act as a “network drive” for the router.
When booting, the router will mount the shared folder as / opt , and then there will be no differences in the operation of Optware, except for the initial boot speed via the network. Of course, for the case of CIFS it is better to use not WiFi, but an Ethernet connection.
To make it work, you just need to specify the network address and login / password in the CIFS section of the router settings to access the shared folder, and the next time you reboot, the router will automatically mount the network drive to the / tmp / smbshare folder .
SD / MMC
Suppose that we have no free space in the flash memory of the router, nor USB, nor a server. And we really need to install Optware. What to do?
As usual, “we had with us” (this is my favorite motto). If there is nothing at all, then you will have to work a bit with a file.
For some models (including the WRT-54GL) the so-called SD / MMC mod is available . Just stick an SD or MMC card somewhere and solder 6 wires, and you can get a place to install Optware and store user data.
In the end, I did just that kind of mod in order to get an autonomous access point for WiFi from the old router Hotspot Chilli. This is the topic of a separate post, maybe I will get together soon and even make a mini-site. If anyone else is interested in this topic, write in private.
4) Mount point definition, startup, shutdown
Depending on the selected “disk”, the mount point will be different. For internal flash-memory it will be / jffs , for USB it will be / mnt (if you enable automatic mounting), for CIFS it will be / tmp / smbshare , for SD / MMC it will be / mmc .
If the USB option is selected, then automatic mounting should be prohibited - we ourselves will do this in the “correct” way.
As I said, Optware is installed in a strictly defined place - the / opt directory . This directory already exists, you only need to mount a real "disk" to it.
Mounting can (be) done during the initial boot of the router. For automation in the router, there are several predefined scripts that are executed at startup and shutdown.
We need at least 2 scripts:
- .rc_startup
- .rc_shutdown
The first script will mount the external “drive” and start Optware, the second, respectively, will stop Optware and unmount.
First you need to enable access to the router via telnet or SSH protocol.
I recommend setting up access via SSH, as it is safer and more convenient (you can configure authorization by a key file and do not even have to enter a password each time you access). And we will need access several times to install programs and watch, WTF, why they do not want to work correctly.
The SSH server is already present and works in all versions of the router firmware. You only need to create a key pair, one of which (public) is uploaded to the router, and the other (private) is put on the laptop next to Putty and WinSCP. I won’t write about the keys either, everywhere it signs in sufficient detail.
Let's create the .rc_startup script , which so far will only do one thing: mount the external “drive” in the / opt folder .
Options:
USB | mount / dev / scsi / host0 / bus0 / target0 / lun0 / part1 / opt |
Jffs | mkdir -p / jffs / opt mount -o bind / jffs / opt / opt |
Cifs | mount.cifs $ (nvram get samba_share) / jffs -o user = $ (nvram get samba_user), pass = $ (nvram get samba_password) mount -o bind / jffs / opt / opt |
We will save it in the router using the Save startup button , reboot the router (maybe it will reboot itself) - after that, the router should have the / opt directory , where now you can write files. Check it out? if it turned out "OK" - then everything is OK, if not - then something is wrong ... Google , look for where the error is, we repeat ...
telnet router
cd /opt
echo "OK" >opt.ok
cat opt.ok
5) Installing Optware - The First Step
Well, everything is behind us, on our network there is a router with the option of installing Optware. Let's get started.
Because on my Windows laptops, I use Putty (telnet or SSH) to access the console of the router and WinSCP to copy and edit files.
We connect via WinSCP to the router and create the following script in the / tmp folder: Connect via Putty to the router and execute the script: As a result of the script, the initial Optware configuration and the opt-inst installer will be created. Now you can proceed with the installation of all other software packages.
opt-inst.sh
cd /tmp
chmod 740 ./optw-inst.sh
./optw-inst.sh
6) Installing the first packages
First, install some very simple package, for example rsync. It will be useful to us in the future for setting up backups. If everything went well and there is no abuse in the console, then you can continue the installation. Among the "essentials" packages, I would name the following:
cd /opt
ipkg-opt update
ipkg-opt install rsync
- mc - Midnight Commander
- tcpdump - collection and analysis of network traffic
- busybox - extended versions of the main Linux utilities
Some packages will require the installation of others (through the dependency mechanism) - this is the way to go, let them be installed by themselves. Usually all that is required is to press y and agree.
7) Final setup
It remains to configure the router so that the launch, operation and shutdown of the necessary services occurs completely automatically. To do this, you need to fix the scripts .rc_startup, .rc_shutdown, perhaps start the cron process, and enjoy.
Some comments should be made here. For different firmware versions, the start and stop procedure is different for some reason, and in a substantial part, namely the launch of user scripts from init.d.
In a newer router (firmware version 13972), user scripts
/opt/etc/init.d/S* are automatically called after the system .rc_startup completes , and similarly, /opt/etc/init.d/K* before running .rc_shutdown. Therefore, to start and base the necessary services, it is enough that their respective S * and K * scripts are in the launch folder /opt/etc/init.d
However, here (at least in my case) could not do without an "underwater rake" (whose apt expression). According to the laws of the genre, the start of the S * script must be with the start parameter , and the start of K * with the stop parameter . But DD-WRT for some reason does not pass start at start, although it does pass stop when it stops. I had to do some work-around: The
system script .rc_statup (created when running in / tmp and launched by the init process) looks something like this (some details are omitted, the full code is available here ):
# /tmp/.rc_startup:
# 1: Монтировать "диск" optware в папку /opt
DST=`ls /dev/scsi/host?/bus?/target?/lun?/part1`
if [ -n "$DST" ]; then
/bin/mount -t ext3 -o noatime,nodiratime $DST /opt
echo "mount USB part1 -> /opt"
fi
# 2: Разрешить swapfile с раздела swap
DST=`ls /dev/scsi/host?/bus?/target?/lun?/part2`
if [ -n "$DST" ]; then
/opt/bin/busybox swapon $DST
echo "mount USB part2 -> swap"
fi
# 3: Создать окружение для запуска S* скриптов Optware
echo "start">/tmp/.rc_stage
# 4: Закончить работу, далее запустятся скрипты /opt/etc/init.d/S*
exit
Each S * and K * script contains something like this: The system script .rc_shutdown (created when rebooting in / tmp and launched by the reboot command) looks something like this (some details are omitted, the full code is available here ): In an older router (firmware version 13064) S * and K * scripts are not automatically called at all, so I had to do this: The system script .rc_statup copies and starts (in the background) the optware startup script: And the optware script /opt/.startup does the rest, namely - runs S * scripts. The system script .rc_shutdown is similarly arranged : (only starts
if [ -n "$1" ]; then
OP=$1
else
OP=$(cat /tmp/.rc_stage)
fi
case "$OP" in
start) # bla-bla
;;
stop) # bla-bla
;;
esac
# /tmp/.rc_shutdown:
# 1: Скрипты /opt/etc/init.d/K* уже отработали
# 2: Запретить swapfile
DST=`ls /dev/scsi/host?/bus?/target?/lun?/part2`
if [ -n "$DST" ]; then
/opt/bin/busybox swapoff $DST
echo "swap disabled"
fi
# 3: Отмонтировать optware
DST=`ls /dev/scsi/host?/bus?/target?/lun?/part1`
if [ -n "$DST" ]; then
/bin/umount /opt
echo "/opt umounted"
fi
# 4: Закончить работу, далее будет перезагрузка роутера
echo "*** Shutdown ***
# /opt/.startup - скрипт запуска Optware
CFG=/mmc # или /mnt, или /jffs, или /tmp/smbshare
if [ -x $CFG/opt/.startup ]; then
cp $CFG/opt/.* /tmp
echo "startup: switch to optware" >>/tmp/log
/tmp/.startup start $CFG &
else
echo "startup: optware not found" >>/tmp/log
fi
/opt/.shutdown not in the background, but directly) That's all for now.
# /opt/.shutdown - скрипт останова Optware
CFG=/mmc
if [ -x $CFG/opt/.shutdown ]; then
cp $CFG/opt/.* /tmp
. /tmp/.shutdown stop $CFG
fi