Preface
When multiple routes are available to a specific destination, such as
when multiple default gateways are configured, the IP protocol is designed
to output traffic over the route with lowest metric. This article describes
the configuration required to define routes based on IP address and port.
Why would you route traffic over a non-preferred GW?
Assume the scenario where you have a private network with one or more hosts
providing services such as HTTP, HTTPS, etc. The ISP might only provide a
dynamic IP or no public IP, so forwarding services is not possible. We can
use a VPN to obtain a public IP, then assign the VPN GW as preferred, and
forward a few ports to make our LAN servers available on the Internet.
This configuration however has one limitation:
All traffic from all hosts on the LAN will go through the VPN, which may be
slower than the direct Internet connection.
Routing traffic based on source IP or port:
To override the global routing, we define IP routing tables. Each table
specify source, target, gateway, and metric. Next we define IP rules, which
assign a table based on custom rules such as source IP address or firewall
mark. When routing traffic based on source IP address, it is sufficient to
create an IP rule which assigns a table to that source address.
Since rules operate on IP traffic, they cannot apply more precise filtering
based on the source port. This limitation can be overcome by using firewall
marks: in this case the rule specifies that packets with a specific mark
should be assigned a particular routing table. The final step is to create
precise filters which mark packets using iptables. iptables can mark packets
based on source and destination IP and port, protocol, etc.
Example: output from router
Assume we have a private network
NET_LAN='192.168.192.0/24', Internet
connection is provided over NET_WAN='172.20.10.0/24',
and the VPN network is NET_VPN='5.5.5.0/24'.
If we want PORT_VPN_WAN='80' on the router
to be accessible on the Internet over the VPN, we can mark the traffic
using the following command:
|