How exactly does linux use prefix length assigned to network interface?
I was exploring direct links between machines, and basically failed to break something.
I assigned IP address 192.168.0.1/24
to eth0
in two ways.
A. Adding 192.168.0.1/24
as usual
<span style="color:#323232;"># ip addr add 192.168.0.1/24 dev eth0
</span><span style="color:#323232;"># ping -c 1 192.168.0.2
</span><span style="color:#323232;">PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
</span><span style="color:#323232;">64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.051 ms
</span><span style="color:#323232;">
</span><span style="color:#323232;">--- 192.168.0.2 ping statistics ---
</span><span style="color:#323232;">1 packets transmitted, 1 received, 0% packet loss, time 0ms
</span><span style="color:#323232;">rtt min/avg/max/mdev = 0.051/0.051/0.051/0.000 ms
</span><span style="color:#323232;">#
</span>
B: Adding 192.168.0.1/32
and adding a /24 route
<span style="color:#323232;"># ip addr add 192.168.0.1/32 dev eth0
</span><span style="color:#323232;"># # 192.168.0.2 should not be reachable.
</span><span style="color:#323232;"># ping -c 1 192.168.0.2
</span><span style="color:#323232;">ping: connect: Network is unreachable
</span><span style="color:#323232;"># # But after adding a route, it is.
</span><span style="color:#323232;"># ip route add 192.168.0.0/24 dev eth0
</span><span style="color:#323232;"># ping -c 1 192.168.0.2
</span><span style="color:#323232;">PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
</span><span style="color:#323232;">64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.053 ms
</span><span style="color:#323232;">
</span><span style="color:#323232;">--- 192.168.0.2 ping statistics ---
</span><span style="color:#323232;">1 packets transmitted, 1 received, 0% packet loss, time 0ms
</span><span style="color:#323232;">rtt min/avg/max/mdev = 0.053/0.053/0.053/0.000 ms
</span><span style="color:#323232;">#
</span>
Does this mean that adding an IP address with prefix is just a shorthand for adding the IP address with /32 prefix and adding a route afterwards? That is, does the prefix length has no meaning and the real work is done by the route entries?
Or is there any functional difference between the two methods?
Here is another case, these two nodes can reach each other via direct connection (no router in between) but don’t share a subnet.
Node 1:
<span style="color:#323232;"># ip addr add 192.168.0.1/24 dev eth0
</span><span style="color:#323232;"># ip route add 192.168.1.0/24 dev eth0
</span><span style="color:#323232;"># # Finish the config on Node B
</span><span style="color:#323232;"># nc 192.168.1.1 8080 <<< "Message from 192.168.0.1"
</span><span style="color:#323232;">Response from 192.168.1.1
</span>
Node 2:
<span style="color:#323232;"># ip addr add 192.168.1.1/24 dev eth0
</span><span style="color:#323232;"># ip route add 192.168.0.0/24 dev eth0
</span><span style="color:#323232;"># # Finish the config on Node A
</span><span style="color:#323232;"># nc -l 0.0.0.0 8080 <<< "Response from 192.168.1.1"
</span><span style="color:#323232;">Message from 192.168.0.1
</span>
Add comment