Wednesday, February 27, 2013

Using a spare router to extend wifi coverage (and an upstart init script to control routing)

Or, "How i learned to route eth0 through wlan0 and script it".

First off, a disclaimer

This is not about using my spare router as a signal amplifier. I didn't find a way to do that with my Sweex LW150. Also note that, as the title unfortunately omits, this story also needs a spare laptop. Or something else that has wifi and an ethernet port, and can run iptables.

The house is not too big, the signal is too weak.

The use case: Wireless router is at one end of the house, near the stables. Large parts of the house, including the salon and the servants' quarters, are out of reach. And halfway, in the music room, there's this small laptop that's always on when i'm at home, and only runs Spotify and Guayadeque (on Lubuntu 12.10).

Iptables, and scripting it

So i hooked up the spare router to the laptop (which by now runs a dhcp server). To make traffic pass from the spare router (eth0) to the internet connection (wlan0), there's some iptables stuff to do. To make sure the routing restarts when the laptop has gone down, i created an upstart configuration. This also enables me to turn routing off and on quickly. Here's how:
  1. Create /etc/init/route-eth0-wlan0.conf:
    # route-eth0-wlan0 - Route traffic from eth0 to internet on wlan0
    #
    # We have internet on our machine's wlan0 interface, an unrelated wireless
    # router in eth0, and want to route that router's traffic.
    # The router on eth0 will then be an extra access point for our internet,
    # extending its range.

    description "Route eth0 through wlan0"

    # Make sure we start before an interface receives traffic
    start on (starting network-interface
              or starting network-manager
              or starting networking)

    stop on runlevel [!023456]

    console output

    pre-start script
        # Set up IP FORWARDing and Masquerading (NAT)
        iptables --table nat --append POSTROUTING --out-interface wlan0 -j MASQUERADE
        iptables --append FORWARD --in-interface eth0 -j ACCEPT
        # enable IP forwarding
        echo 1 > /proc/sys/net/ipv4/ip_forward
    end script

    post-stop script
        # disable IP forwarding
        echo 0 > /proc/sys/net/ipv4/ip_forward
        # delete old configuration, if any
        # Flush all the rules in filter and nat tables
        iptables --flush               
        iptables --table nat --flush
        # delete all chains that are not in default filter and nat table, if any
        iptables --delete-chain    
        iptables --table nat --delete-chain
    end script
  2. Symlink /etc/init.d/route-eth0-wlan0 -> /lib/init/upstart-job
Now i can start and stop the routing by running sudo service route-eth0-wlan0 (start|stop).

Update 2013-03-27: The motto "because we can" applies to all of the above. After, i applied "use the right tool for the job" and bought a wireless repeater from Amazon (Anker Kompakt, 26 Euros, works great).

No comments: