Linux, peer addresses and routing between 2 network namespaces

1

2

I'm trying to understand something about routing in Linux. I've following devices (veth pairs):

veth pair: [ns1-1] <10.8.0.1 peer 10.8.0.2> [ns1-2]
veth pair: [ns2-1] <10.8.0.5 peer 10.8.0.6> [ns2-2]

And I added IPs to ns*-1 devices like this:

[ns1-1] <10.8.0.3 peer 10.8.0.4> [ns2-1]

Each ns*-2 device is in separate network namespace. From each namespace I'm trying to ping all 6 IPs. I'm doing all in a simple script I attached. The output of this script is:

# failed to ping 10.8.0.6 from ns1
# failed to ping 10.8.0.2 from ns2

Note that I've also did

echo 0 > /proc/sys/net/ipv4/ip_forward

Writing 1 there makes all IPs pingable but I'm not simply looking for solution to a problem. I'd like understand how the routing works.

Why did the ping failed in 2 cases above?

When I ping 10.8.0.5 (ns2-1) from namespace ns1, why don't I see any packets in tcpdump -i ns2-1 (outside namespace) even though the ping succeeds?

Why can I ping 10.8.0.5 from namespace ns1 even though forwarding is disabled?

Can I make all involved IPs accessible from any namespace involved without forwarding enabled?

Thank you.

Here's the script:

#!/bin/sh

#path=(/sbin $path)
PATH=/sbin:$PATH

_ns() { ip netns exec "$@"; }

create_ns()
{
    ns=$1
    a1=$2 a2=$3
    l1=$ns-1 l2=$ns-2
    ip netns add $ns
    ip link add name $l1 type veth peer name $l2
    ip link set dev $l2 netns $ns
    _ns $ns ip link set dev lo up
    ip addr add $a1 peer $a2 dev $l1
    _ns $ns ip addr add $a2 peer $a1 dev $l2
    ip link set dev $l1 up
    _ns $ns ip link set dev $l2 up
    _ns $ns ip route add default via $a1
}

close_ns()
{
    ip link del $1-1
    ip netns del $1
}

test_ping()
{
    if ! _ns $1 ping -W 1 -c 1 $2 >/dev/null 2>&1; then
        echo "failed to ping $2 from $1"
    fi
}

close_ns ns1
close_ns ns2

create_ns ns1 10.8.0.1 10.8.0.2
create_ns ns2 10.8.0.5 10.8.0.6
ip addr add 10.8.0.3 peer 10.8.0.4 dev ns1-1
ip addr add 10.8.0.4 peer 10.8.0.3 dev ns2-1

test_ping ns1 10.8.0.1
test_ping ns1 10.8.0.2
test_ping ns1 10.8.0.3
test_ping ns1 10.8.0.4
test_ping ns1 10.8.0.5
test_ping ns1 10.8.0.6

test_ping ns2 10.8.0.1
test_ping ns2 10.8.0.2
test_ping ns2 10.8.0.3
test_ping ns2 10.8.0.4
test_ping ns2 10.8.0.5
test_ping ns2 10.8.0.6

# failed to ping 10.8.0.6 from ns1
# failed to ping 10.8.0.2 from ns2

sqxmn

Posted 2015-01-13T19:47:04.900

Reputation: 11

No answers