tools/testing/selftests/net/netfilter/nf_nat_edemux.sh

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/netfilter/nf_nat_edemux.sh

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/netfilter/nf_nat_edemux.sh
Extension
.sh
Size
3484 bytes
Lines
122
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Test NAT source port clash resolution
#

source lib.sh
ret=0
socatpid=0

cleanup()
{
	[ "$socatpid" -gt 0 ] && kill "$socatpid"

	cleanup_all_ns
}

checktool "socat -h" "run test without socat"
checktool "iptables --version" "run test without iptables"
checktool "conntrack --version" "run test without conntrack"

trap cleanup EXIT

connect_done()
{
	local ns="$1"
	local port="$2"

	ip netns exec "$ns" ss -nt -o state established "dport = :$port" | grep -q "$port"
}

check_ctstate()
{
	local ns="$1"
	local dp="$2"

	if ! ip netns exec "$ns" conntrack --get -s 192.168.1.2 -d 192.168.1.1 -p tcp \
	     --sport 10000 --dport "$dp" --state ESTABLISHED > /dev/null 2>&1;then
		echo "FAIL: Did not find expected state for dport $2"
		ip netns exec "$ns" bash -c 'conntrack -L; conntrack -S; ss -nt'
		ret=1
	fi
}

setup_ns ns1 ns2

# Connect the namespaces using a veth pair
ip link add name veth2 type veth peer name veth1
ip link set netns "$ns1" dev veth1
ip link set netns "$ns2" dev veth2

ip netns exec "$ns1" ip link set up dev lo
ip netns exec "$ns1" ip link set up dev veth1
ip netns exec "$ns1" ip addr add 192.168.1.1/24 dev veth1

ip netns exec "$ns2" ip link set up dev lo
ip netns exec "$ns2" ip link set up dev veth2
ip netns exec "$ns2" ip addr add 192.168.1.2/24 dev veth2

# Create a server in one namespace
ip netns exec "$ns1" socat -u TCP-LISTEN:5201,fork OPEN:/dev/null,wronly=1 &
socatpid=$!

# Restrict source port to just one so we don't have to exhaust
# all others.
ip netns exec "$ns2" sysctl -q net.ipv4.ip_local_port_range="10000 10000"

# add a virtual IP using DNAT
ip netns exec "$ns2" iptables -t nat -A OUTPUT -d 10.96.0.1/32 -p tcp --dport 443 -j DNAT --to-destination 192.168.1.1:5201 || exit 1

Annotation

Implementation Notes