doc/Routing.md
Reaching a remote network or tunneling all the internet traffic via n2n are two common tasks which require a proper routing setup. n2n supports routing needs providing options for packet forwarding including broadcasts as well as modifying the routing table.
In this context, the server is the edge node which provides access to the remote network/internet, whereas the client is the connecting edge node.
In order to enable routing, the server must be configured as follows:
-r option to the edge options to enable routingsudo sysctl -w net.ipv4.ip_forward=1sudo iptables -t nat -A POSTROUTING -j MASQUERADEOn the client side, the easiest way to configure routing is via the tools/n2n-route utility. For example:
tools/n2n-route 10.0.0.1192.168.100.0/24, use tools/n2n-route -n 192.168.100.0/24 10.0.0.110.0.0.1 is the IP address of the gateway to use to route the specified network. It should correspond to the IP address of the server within n2n. Multiple -n options can be specified.
The utility connects to the local edge's management port to receive information about peers and supernodes. It currently works on Linux only but certainly can be ported to other OS (the route handling code is quite OS dependant).
As an alternative to the tools/n2n-route utility, the ip route linux command can be manually used. See the n2n-gateway.sh script for an example. See also the following description of other use cases and in depth explanation.
echo 1 > /proc/sys/net/ipv4/ip_forward or sysctl -w net.ipv4.ip_forward=1. To
make this setting persistent over reboot, a file containing the line
net.ipv4.ip_forward=1 could be added in /etc/sysctl.d/ – your distro
may vary.-r option on their command line. All other regular network
interfaces usually already allow packet forwarding and thus do not need
any further configuration.ip route add 192.168.1.0/24 via 10.99.99.60 dev n2n0 src 10.11.12.5.ip route add 10.11.12.5 via 10.99.99.50 dev n2n0 src 192.168.1.6.oscar and hickory should now be able to exchange packets by using just their regular (non-n2n) IP addresses 10.11.12.5 and 192.168.1.6. To make the complete office network aware of how packets or answers are sent to hickory, one more step is required:
ip route add 10.11.12.5 via 192.168.1.5 dev eth0 src 192.168.1.123.iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o n2n0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i n2n0 -o eth0 -j ACCEPT
There is a major drawback with this solution which thus is the least
recommended: Connections between hickory and the office network
will only work if initiated by hickory – not the other way 'round.
By the way, in case iptables are messed up, they can be flushed by:
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t raw -F
iptables -t raw -X
iptables -t security -F
iptables -t security -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPTThis is easy:
This is not too complicated either. Basically, follow the given example above and apply the following changes:
ip route add 10.11.12.0/24 via 10.99.99.50 dev n2n0 src 192.168.1.6.This scenario could be considered a n2n-tunneled VPN connection which also would work for travelling users on their laptop. All external internet traffic will appear to originate from oscar and the office network.
First, one of the setups described above needs to be in place, with the following change:
NAT on oscar (see the three iptables commands above) must be
enabled. It will not work without because the office router 192.168.1.1
usually denies forwarding to packets not originating from its own
network. It could be in addition to the eventually installed static
routes for 10.11.12.0/24 in the router 192.168.1.1 or on other office
computers – it will not interfere. However, oscar definitely needs
the route given above: ip route add 10.11.12.5 via 10.99.99.50 dev n2n0 src 192.168.1.6.
To have hickory's complete internet traffic going through the n2n
tunnel, its default route needs to be changed:
ip route del default
ip route add default via 10.99.99.60 dev n2n0 src 10.11.12.5
hickory's home network should still be reachable as usually,
eth0 and the associated network 10.11.12.0/24 get their very own
route. If not, i.e. it was only covered by default route before, it
needs to be added: ip route add 10.11.12.0/24 dev eth0 src 10.11.12.5.
Unfortunately (unless the supernode is on hickory's local
network), n2n supernode becomes unreachable for hickory. To fix it:
ip route add <supernode IP address> via 10.11.12.1 dev eth0 src 10.11.12.5
The supernode's IP address needs to be known to have this work. However, if the supernode's IP needs to be resolved from some domain name (FQDN), e.g. in case of using dynamic domain name services, a DNS server needs to remain reachable, too. Either the reachable home network router 10.11.12.1 is good enough for that (if it offers DNS) or another route could to be added and hickory's DNS settings might be set accordingly, maybe to Google's 8.8.8.8.
If DNS leaks do not matter, this setup is complete.
Otherwise, there is more to it: Without changes, all future DNS queries go through the home router 10.11.12.1 to the ISP's servers or directly to Google (via the home router 10.11.12.1 along the configured route for 8.8.8.8 and not through the n2n tunnel) while the remaining traffic ships through the n2n tunnel.
To prevent such a DNS leak, the supernode's IP address must be
determined independently from hickory's DNS configuration, e.g. by
digesting dig +short mysupernode.myfavoritednsservice.com @8.8.8.8's
output in the n2n-edge's setup script for both, the edge node command
line as well as the static route mentioned above. Without further
additional work, dynamic address changes remain undetected. A static
route to 8.8.8.8 is still required. hickory's regular DNS
configuration should query a different DNS server for its regular DNS
needs, e.g. 9.9.9.9 or 1.1.1.1 or maybe the office DNS server, maybe
192.168.1.1. This guarantees the regular DNS queries also to get sent
through the n2n tunnel.
A test for DNS leaks can be found here.