I have an Ubuntu server with two network interfaces - an ethernet and a WiFi network let’s call eth0 and wlan0. So far I’ve been able to set it up as a router by enabling packet forwarding and then doing some iptables trickery. These are my iptable commands:

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

If I’m understanding correctly, the first command says “if you receive packets from a device, do NAT and then forward them with your IP”, the second one says to forward packets from eth0 to eth0, and the last line says “if you get packets back, only accept them if a connection has already been previously established”. This Ubuntu server is connected to a router which is connected to a modem that actually has internet access. I’ve set it up so that my router uses my Ubuntu server as the default gateway during DHCP requests. This works fine, I’m able to use devices to connect to the internet, and if I do a trace route, it first goes to the Ubuntu server, then to the router, then out into the great beyond.

Now, I’ve run:

iptables -D FORWARD -i eth0 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

Which, if I’m understanding correctly, should forward packets through to the WiFi interface instead, but it isn’t working. I’m still able to access other devices on the network but not the open internet. I also tried doing iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE which as far as I can tell is unnecessary, but that didn’t do anything. When I do trace route this time, it is able to get to the Ubuntu server but no further. I’ve also tried doing iptables -L -v but neither the wlan0 -> eth0 rule or the reverse have any packet count. I also tried doing iptables -A FORWARD -i lan0 -o wlan0 -j LOG --log-prefix "FORWARD: " to just log it first, but nothing shows up in /var/log/syslog even if I try to connect to the internet from a device.

I’m at a loss here so any help even debugging or if I’m going about this wrong would be greatly appreciated. My ultimate goal is to set up a failover so that if the LAN interface doesn’t have a connection, it’ll start sending packets through the WiFi interface which will be connected to a different internet connection.

  • Max-P
    link
    fedilink
    English
    91 year ago

    Those rules only allows forwarding, it doesn’t cause forwarding. That is dictated by the routing table: that’s why we put that in POSTROUTING as well.

    You need to make it so the packers are also routed to the right place. This can be done by changing the default gateway to be wlan0, or creating a secondary routing table, adding a rule to route eth0 packets through it. By default there’s only one, so if your server is configured to use eth0 as the default gateway, it’s where things will be masqueraded to.

    This may help: http://www.allgoodbits.org/articles/view/24

    • @jcgOPA
      link
      English
      11 year ago

      Thanks, this is very helpful!

  • Starfer
    link
    fedilink
    English
    41 year ago

    Any reason you don’t want to use something like OPNsense?

    • @jcgOPA
      link
      English
      01 year ago

      Well, my Ubuntu server is already running a whole bunch of things so I don’t want to take it down and rebuild it on a different OS.

      • Starfer
        link
        fedilink
        English
        21 year ago

        Not really sure what your use case is here, but from a security perspective (and speaking as a network engineer), I would highly suggest you run a firewall/router on a separate device.