Sometimes you think a problem is going to be really hard to solve, but it turns out to be pretty easy. A few weeks ago we started implementing a new feature for Hypernodes: Dedicated IP’s. This would make migrating and upgrading a lot easier for our customers because their node IP won’t change anymore. One problem we had to tackle whilst implementing this feature was that pretty much all cloud providers handled dedicated IP management in a different way. For example AWS calls them Elastic IP’s, while DigitalOcean calls them Floating IP’s. Of course this can all be coded into the Hypernode logic, so no problem, you’d say.

However, it didn’t take long for us to find out that mail traffic over the Dedicated IP didn’t work properly with DigitalOcean’s Floating IP’s. It turned out that this cloud provider was blocking port 25 and 587 outbound for all floating IP’s. DigitalOcean responded that this was an acknowledged new feature on their product roadmap, but with low priority due to the perceived “very rare instances where a customer deliberately configures their route to use the floating IP address in this way”. So now we were faced with the problem that we could only fully implement Floating IP’s without standard mail connectivity. Not an ideal situation to say the least. A solution was needed.

The concept of Floating IP’s is that the actual public IP is bound on a gateway somewhere and that the traffic is translated to the internal node IP. Some cloud providers always use this setup even without Floating IP’s and some bind the original public node IP directly on the node. The latter was also the case for the DigitalOcean Floating IP’s we were having problems with. So a possible solution was to route outbound mail traffic over the original node gateway. Only problem was that the internal IP (used for the floating IP) and the public node IP are bound on the same interface. So how do you route traffic destined for ports 25 and 587 to another gateway, while not affecting all other outbound traffic on the same interface?

We thought that this was next to impossible to achieve. But after some extensive googling it turned out to be pretty easy. I found this dusty stackexchange article which looked very promising. And it turned out this approach actually works. But what are we actually doing here? We have to go deeper.

This is the part we were particularly interested in:

  • First we would have to create a extra routing table, turns out the linux kernel support this by default (link).
  • Then add a default route to this table that uses the original node gateway IP.
  • Then add the IP rule specified above to route packets with an (hexadecimal) fwmark of 1 to this routing table.
  • Then add an fwmark of 1 to all packets to destination ports 25 and 587 using iptables.
  • Finally add the SNAT to give this traffic the appropriate source IP using iptables. (The original node IP.)

This will result in the following bash commands:

Pretty similar to the solution from the stackexchange article and guess what: it works. Now only these two ports will be routed over the original gateway instead of the floating IP. The visual representation of what we just did looks something like this:

Solution for floating IP on Hypernode

Conclusion: Linux is awesome 🙂

Since we had a hard time believing that our case is “a rare instance”, we decided to share this information with everybody who would ever want to use DigitalOcean’s Floating IP’s for outbound mail. Hoping that one day we can save somebody from the headache we had.