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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/netfilter/xt_string.sh
Extension
.sh
Size
2598 bytes
Lines
134
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

# return code to signal skipped test
ksft_skip=4
rc=0

source lib.sh

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

infile=$(mktemp)

cleanup()
{
	ip netns del "$netns"
	rm -f "$infile"
}

trap cleanup EXIT

setup_ns netns

ip -net "$netns" link add d0 type dummy
ip -net "$netns" link set d0 up
ip -net "$netns" addr add 10.1.2.1/24 dev d0

pattern="foo bar baz"
patlen=11
hdrlen=$((20 + 8)) # IPv4 + UDP

#ip netns exec "$netns" tcpdump -npXi d0 &
#tcpdump_pid=$!
#trap 'kill $tcpdump_pid; ip netns del $netns' EXIT

add_rule() { # (alg, from, to)
	ip netns exec "$netns" \
		iptables -A OUTPUT -o d0 -m string \
			--string "$pattern" --algo "$1" --from "$2" --to "$3"
}
showrules() { # ()
	ip netns exec "$netns" iptables -v -S OUTPUT | grep '^-A'
}
zerorules() {
	ip netns exec "$netns" iptables -Z OUTPUT
}
countrule() { # (pattern)
	showrules | grep -c -- "$*"
}
send() { # (offset)
	( for ((i = 0; i < $1 - hdrlen; i++)); do
		echo -n " "
	  done
	  echo -n "$pattern"
	) > "$infile"

	ip netns exec "$netns" socat -t 1 -u STDIN UDP-SENDTO:10.1.2.2:27374 < "$infile"
}

add_rule bm 1000 1500
add_rule bm 1400 1600
add_rule kmp 1000 1500
add_rule kmp 1400 1600

zerorules
send 0
send $((1000 - patlen))
if [ "$(countrule -c 0 0)" -ne 4 ]; then
	echo "FAIL: rules match data before --from"

Annotation

Implementation Notes