Monday, September 24, 2018

Routing packets across the internet, or across different subnets in a LAN is done using a routing table.

On Linux systems this is handled by the kernel and is configured by file(s) corresponding to each network intrerface stored in /etc/sysconfig/network-scripts directory.

For example if your interface name is enp7s1 the path would look like this:  /etc/sysconfig/network-scripts/route-enp7s1.

Each interface has at least one route called the default route, which translates to a network address of 0.0.0.0 with a subnet mask of 0.0.0.0 or the entire tcp/ip address range including the "internet" (publicly routable addresses) and the private IP ranges:

192.168.0.0 - 192.168.255.255 (65,536 IP addresses) noted as 192.168.0.0/16 (255.255.0.0)
172.16.0.0 - 172.31.255.255 (1,048,576 IP addresses) noted as 172.16.0.0/12 (255.240.0.0)
10.0.0.0 - 10.255.255.255 (16,777,216 IP addresses) noted as 10.0.0.0/8 (255.0.0.0)

Most of the time these ranges are broken up into smaller subnets especially for the private ranges. for example 192.168.0.0/24 (255.255.255.0) is extremely common for home and SOHO networks.

Additional routes may be present, typically at least one for the local network, be that a subnet of the publicly routed address space or a subnet of one of the private address spaces. Also, if the machine is acting as a router itself one route for each subnet is present, for example:

$ sudo route -n
Kernel IP routing table
Destination     Gateway             Genmask         Flags  Metric Ref    Use   Iface
0.0.0.0            192.168.0.254     0.0.0.0             UG    100       0        0      enp7s1
192.168.0.0    0.0.0.0                 255.255.255.0 U       100       0        0      enp7s1

Here we have the default route 0.0.0.0 along with a gateway (for addresses not in the local subnet), this is typically used for routing packets across the internet through an ISP connection on that interface.

And then the second route is being used here for the private IP space of the local network.

A more complicated example:

$ sudo route -n
Kernel IP routing table
Destination         Gateway             Genmask               Flags    Metric Ref    Use     Iface
10.1.0.0             0.0.0.0                  255.255.0.0           U          0      0            0        eth0
192.168.0.0         0.0.0.0                255.255.255.0       U          0      0            0        eth1
192.168.25.0       0.0.0.0                255.255.255.0       U          0      0            0        eth2
0.0.0.0                 192.168.1.25      0.0.0.0                   UG       0      0            0        eth0

Here we have four interfaces connected to different subnets in both the 10.1.0.0 and 192.168.0.0 ranges. The file basically looks at the range for a match to the destination range 10.1.0.0 range up to 10.0.255.254 (mask 255.255.0.0) then try the next until we reach the default route, which forwards any unmatched subnets to the gateway at 192.168.1.25 via eth0.

It's important to note that if you are using the machine to bridge multiple subnets on different interfaces and forward those packets between ranges ip forwarding must be enabled. Otherwise the machine itself will be able to reach all the destinations in the routing table, however machines connected to it with the intent that it will bridge/route for them will not.


No comments: