📜 ⬆️ ⬇️

DD-WRT firmware on home router

DD-WRT firmware on home router


This is an overview article about what opportunities a user has of a regular home router, and how to take advantage of these opportunities.

Sooner or later, every Internet user buys his router home, as the number of devices that require the Internet grows. And many children grow up, who also require an Internet connection :) But it hurts me and insulting to see that people do not always understand what can and should be done to achieve maximum comfort.

A small disclaimer (I do not know how it is in Russian).
I will use the word “correctly” in the sense that I myself consider it right for myself. Having gone from “noob” in Linux to the level “I can advise others”, I consider that my advice is at least as good as other tips that can be found on various forums.

It will be about the firmware of the router based on the well-known distribution kit DD-WRT . This is a firmware that turns a router into a “normal” Linux server with very severe memory and speed limitations. Despite this, the functionality of such a server can be very rich.
')
Here are just some of the features:

- Let's start! - Said Walrus, sitting on the coastal stone. - It's time to talk about many things ... ( c )

1) Choice of equipment

The first and most important task is to choose the right router model, since this is a “one way ticket”. Evaluating your finances and your ambitions, it is important to choose a middle ground. A possible risk factor would be the fear of “banging up” an expensive router with its wrong actions. I myself, buying an expensive thing, am afraid to do something with it, especially if there is no experience. However, I can assure you: the chances of killing a router with new firmware are almost zero. Modern models have a recovery mode, and you need to very much want to ditch the router (and then not forever - you just need special equipment for resuscitation ).

I will name 2 "correct" models that I chose for myself.
  1. Linksys WRT-54GL , entry-level model, but unsurpassed in reliability. It was 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.
  2. Asus WL-520GU , a modern and very cheap router, 3 months in service, also no complaints about the work. Features: WiFi (b / g) + 4 LAN 10/100, 4Mb flash, 16Mb RAM, 1 USB.

The main criteria for selecting models are:

2) Router firmware

Firmware is not painful at all! It is in fact a simple operation, well, such as the right prostate gland removal ... No, really nothing complicated.
At the very first firmware change, it will take a little more effort, but each next one will go easier and easier.
Do not be afraid that the firmware change will occur regularly. By choosing for yourself a set of programs included in one of the ready-made firmware, you can calm down and enjoy life on it. If you need additional functionality - you can always install new programs separately, for this there is a special mechanism. But in the first stages it may happen that the choice of firmware will be unsuccessful, for example, the firmware will not have such a necessary opportunity as support for SAMBA. Do not worry, download another firmware, 2 mouse movements - voila!

Some models of routers (in particular, Asus) require special actions (flushing the memory or something like that) before flashing to DD-WRT. Carefully follow the instructions for a specific router model! Especially carefully you need to refer to the requirements of "wait 5 minutes after the end of the firmware." I was a little self-confident and, by reflashing to my colleague a router, I 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 an hourglass for 5 minutes and wait! This is serious.

I will not consider the process of flashing itself (tftp, changing the address to 192.168.0.1, interface, etc.) - this is all set forth in the DD-WRT forums .

But, finally, you are finished 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 connection settings to your provider (DHCP, PPTP etc), and you can already begin to surf the universe. This will be enough for many home users.
I will not describe the settings of security parameters, routing control and other standard “routers” for routers. Without this, no “normal” router can do anyway.
But you can do even better! Continue the process.

All additional programs are united by the general concept of Optware . (they are placed in the / opt directory, which is automatically created by the router and is always available for writing). 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 “disk”. And although the storages described do not have a disk shape in a geometrical sense (rather, they 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 smaller than the total size of the flash memory, then the remaining space can be used for hreneniya data. If you choose firmware with JFFS support, then depending on the version, you can get from 0.5 to 3Mb a “disk” right inside the router. Such a disk will be automatically mounted as / jffs at startup.

The main trouble when working with flash-drives is their rapid degradation during recording. If the number of overwrites 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 with the internal disk, it will really be bad. But if it is an external USB drive, then with a slight movement of the hand we replace the USB flash drive with a new one - and again into battle. After all, USB flash drives have long been consumable, is not it? Moreover, a 1 or 2Gb flash drive is sufficient. The main thing, as usual, in time to do backups.

USB

As you already understood, the main Optware installation site is an external USB drive. It is important to keep in mind that USB must be formatted in a certain way before installing it into a router. At a minimum, a primary partition must be present, formatted as ext2 or ext3 .

More “correct" is the following scheme:
  1. ext2 partition, ~ 400Mb size - the main Optware partition, mounted as / opt
  2. The swap section, size ~ 24Mb - for swapping, allows such “heavyweight” applications as RADIUS, MySQL, Apache to work ...
  3. ext2 partition, ~ 400Mb size - backup Optware partition (offline mirror)
  4. ext2 or fat32 partition, everything else is for user data

USB formatting is best done on a Linux machine by any available utilities, such as GParted.

CIFS

If the router does not have USB hardware, and you still want to install Optware, you can use the remarkable Linux opportunity to work with the disk via the network. For this we need 2 things:

If there are already computers in the house (and they are, because a router without computers looks somehow 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 further - no differences in the operation of Optware, except the speed of the initial download via the network. Of course, for the case of CIFS, it is better to use not WiFi, but Ethernet connection.

To make it work, it’s enough to specify the network address and login / password for access to the shared folder in the CIFS section of the router, and the next time you restart the router automatically mount the network drive to the / tmp / smbshare folder .

SD / MMC

Suppose that we have neither 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, you will have to work a little with the file.

For some models (among which the WRT-54GL) is available the so-called SD / MMC mod . It is enough to stick somewhere SD or MMC card and solder 6 wires, and you can get a place to install Optware and store user data.

It was this mod that I eventually did to get an autonomous access point for WiFi Hotspot Chilli from the old router. This is a topic for a separate post, maybe I’ll get together soon and even make a mini-site. If anyone else is interested in this topic, write in private.

4) Determination of mount point, startup, shutdown

Depending on the selected "disk" mount point will be different. For internal flash memory, this will be / jffs , for USB, this will be / mnt (if you enable auto-mount), for CIFS it will be / tmp / smbshare , for SD / MMC, this will be / mmc .

If the USB option is selected, then automatic mounting should be prohibited - we will do it ourselves in the “right” way.
As I said, Optware is installed in a strictly defined location - the / opt directory. This directory is already there, you only need to mount a real "disk" to it.

Mounting can (need) be done during the initial boot process of the router. To automate the router, there are several predefined scripts that are executed during startup and shutdown.

We will need at least 2 scripts:

The first script will mount the external "disk" and launch Optware, the second, respectively, stop Optware and unmount.

First you need to allow 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 set up authorization on a key file and not even enter a password every time you access it). And we will need access repeatedly to install programs and watch WTF why they don’t want to work properly.

The SSH server is already present and works in all versions of the router firmware. You only need to create a pair of keys, one of which (public) is loaded into the router, and the other (private) is put on the laptop next to Putty and WinSCP. I will not write about the keys either, everywhere it is written in sufficient detail.

Let's create a .rc_startup script, which so far will only do one thing: mount an external "disk" in the / opt folder.
Options:
USBmount / dev / scsi / host0 / bus0 / target0 / lun0 / part1 / opt
Jffsmkdir -p / jffs / opt
mount -o bind / jffs / opt / opt
CIFSmount.cifs $ (nvram get samba_share) / jffs -o user = $ (nvram get
samba_user), pass = $ (nvram get samba_password)
mount -o bind / jffs / opt / opt

Save it in the router with the Save Start button, reboot the router (maybe it will reboot itself) - after that the router should have the / opt directory where you can now write files. Check?

telnet router
cd /opt
echo "OK" >opt.ok
cat opt.ok

if it turned out OK, then everything is OK, if not, then something is wrong ... We google , look for where the error is, repeat ...

5) Installing Optware - First Step

Well, everything is over, in our network - a router with the ability to install Optware. Let's get started

Because on my Windows laptops, I use Putty (telnet or SSH) to access the router console and WinSCP to copy and edit files.

Connect via WinSCP to the router and create the following script in the / tmp folder:
opt-inst.sh

Connect via Putty to the router and execute the script:

cd /tmp
chmod 740 ./optw-inst.sh
./optw-inst.sh

As a result of the script operation, the initial configuration of Optware and the installer program opt-inst will be created. Now you can begin to install all the other software packages.

6) Install the first packages

First, install some very simple package, for example rsync. It will be useful to us later to configure the backup.

cd /opt
ipkg-opt update
ipkg-opt install rsync

If everything went well and there is no swearing in the console, then you can continue the installation.

Among the packages of "first necessity" I would call the following:

Some packages will require the installation of others (through the mechanism of dependencies) - so be it, let them be put themselves. Usually, all that is required is to press "y" and agree.

7) Final Setup

It remains to configure the router so that the start, operation and shutdown of the necessary services occur fully automatically. To do this, you need to fix the scripts .rc_startup, .rc_shutdown, perhaps, run the cron process, and enjoy.

There should be some comments. For some reason, the procedure for starting and stopping is different for different firmware versions, and in a substantial part, namely, the launching of user scripts from init.d.

In the newer router (firmware version 13972) user scripts
/opt/etc/init.d/S* is automatically called after the system .rc_startup is shut down , and similarly, /opt/etc/init.d/K* before starting .rc_shutdown . Therefore, to launch and set up the required services, it is enough that their corresponding S * and K * scripts are located in the /opt/etc/init.d startup folder

However, even here (at least in my case) it was not without the “underwater rake” (someone’s apt expression). According to the laws of the genre, the start of the S * script should be with the start parameter, and the start of K * should be with the stop parameter. But DD-WRT for some reason does not transmit start at start, although it transmits stop at stop. I had to do some work-around:

The system script .rc_statup (created when running in / tmp and started by the init process) looks 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 approximately the following code:

if [ -n "$1" ]; then
OP=$1
else
OP=$(cat /tmp/.rc_stage)
fi

case "$OP" in
start) # bla-bla
;;
stop) # bla-bla
;;
esac


The system script .rc_shutdown (created when rebooting into / tmp and started with the reboot command) looks like this (some details are omitted, the full code is available here ):

# /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 ***


In an older router (firmware version 13064), the S * and K * scripts are not automatically invoked at all, so I had to do this:

The system script .rc_statup copies and starts (in the background) the optware startup script:

# /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


And the optware-script /opt/.startup already does the rest, namely, it runs the S * scripts.

The system script .rc_shutdown is similarly arranged: (it only starts /opt/.shutdown not in the background, but directly)

# /opt/.shutdown - Optware
CFG=/mmc
if [ -x $CFG/opt/.shutdown ]; then
cp $CFG/opt/.* /tmp
. /tmp/.shutdown stop $CFG
fi


That's all for now.

Source: https://habr.com/ru/post/98812/


All Articles