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

# ip addr add 192.168.0.1/24 dev eth0
# ping -c 1 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.051 ms

--- 192.168.0.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.051/0.051/0.051/0.000 ms
#

B: Adding 192.168.0.1/32 and adding a /24 route

# ip addr add 192.168.0.1/32 dev eth0
# # 192.168.0.2 should not be reachable.
# ping -c 1 192.168.0.2
ping: connect: Network is unreachable
# # But after adding a route, it is.
# ip route add 192.168.0.0/24 dev eth0
# ping -c 1 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.053 ms

--- 192.168.0.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.053/0.053/0.053/0.000 ms
#

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:

# ip addr add 192.168.0.1/24 dev eth0
# ip route add 192.168.1.0/24 dev eth0
# # Finish the config on Node B
# nc 192.168.1.1 8080 <<< "Message from 192.168.0.1"
Response from 192.168.1.1

Node 2:

# ip addr add 192.168.1.1/24 dev eth0
# ip route add 192.168.0.0/24 dev eth0
# # Finish the config on Node A
# nc -l 0.0.0.0 8080 <<< "Response from 192.168.1.1"
Message from 192.168.0.1
  • Markaos@lemmy.one
    link
    fedilink
    arrow-up
    11
    ·
    edit-2
    7 months ago

    Or is there any functional difference between the two methods?

    Can’t test right now, but I have a strong suspicion you will have trouble getting IP broadcast to work. Normally broadcast address is calculated by setting all bits after the network prefix to 1, but your computer believes to be in a /32 “network”. It won’t broadcast over routes that are not part of its network.

    And even if you calculate the broadcast address successfully (maybe the software you use has /24 hardcoded for whatever reason), no computer configured with a /32 address will receive it - 192.168.0.255 is not within the 192.168.0.1/32 network, so it will probably get forwarded according to your routes if you have forwarding enabled (except it shouldn’t in this case with one network interface, because you never send packets back the way they came from)

    • akash_rawal@lemmy.worldOP
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      7 months ago

      Just did some basic testing on broadcast addresses using socat, broadcast is not working at all with /32 addresses. With /24 addresses, broadcast only reaches nodes that share a subnet. Nodes that don’t share the subnet aren’t reachable by broadcast even when they’re reachable via unicast.

      Edit1: Did more testing, it seems like broadcast traffic ignores routing tables.

      On 192.168.0.2, I am running socat -u udp-recv:8000,reuseaddr - to print UDP messages.

      Case 1: add 192.168.0.1/24

      # ip addr add 192.168.0.1/24 dev eth0
      # # Testing unicast
      # socat - udp-sendto:192.168.0.2:8000 <<< "Message"
      # # Worked
      # socat - udp-sendto:192.168.0.255:8000,broadcast <<< "Message"
      # # Worked
      

      Case 2: Same as above but delete 192.168.0.0/24 route

      # ip addr add 192.168.0.1/24 dev eth0
      # ip route del 192.168.0.0/24 dev eth0
      # # Testing unicast
      # socat - udp-sendto:192.168.0.2:8000 <<< "Message"
      2024/02/13 22:00:23 socat[90844] E sendto(5, 0x5d3cdaa2b000, 8, 0, AF=2 192.168.0.2:8000, 16): Network is unreachable
      # # Testing broadcast
      # socat - udp-sendto:192.168.0.255:8000,broadcast <<< "Message"
      # # Worked
      
      • NaN@lemmy.sdf.org
        link
        fedilink
        English
        arrow-up
        9
        ·
        7 months ago

        One of the functions of a router is splitting broadcast domains. You would not expect a broadcast to reach a different subnet.