Path: blob/main/tests/sys/netpfil/pf/fragmentation_pass.sh
39507 views
#1# SPDX-License-Identifier: BSD-2-Clause2#3# Copyright (c) 2017 Kristof Provost <[email protected]>4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24# SUCH DAMAGE.2526. $(atf_get_srcdir)/utils.subr2728common_dir=$(atf_get_srcdir)/../common2930atf_test_case "too_many_fragments" "cleanup"3132too_many_fragments_head()33{34atf_set descr 'IPv4 fragment limitation test'35atf_set require.user root36}3738too_many_fragments_body()39{40pft_init4142epair=$(vnet_mkepair)43vnet_mkjail alcatraz ${epair}a4445ifconfig ${epair}b inet 192.0.2.1/24 up46jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up4748ifconfig ${epair}b mtu 20049jexec alcatraz ifconfig ${epair}a mtu 2005051jexec alcatraz pfctl -e52pft_set_rules alcatraz \53"set reassemble yes" \54"pass keep state"5556# So we know pf is limiting things57jexec alcatraz sysctl net.inet.ip.maxfragsperpacket=10245859# Sanity check60atf_check -s exit:0 -o ignore ping -c 1 192.0.2.26162# We can ping with < 64 fragments63atf_check -s exit:0 -o ignore ping -c 1 -s 800 192.0.2.26465# Too many fragments should fail66atf_check -s exit:2 -o ignore ping -c 1 -s 20000 192.0.2.267}6869too_many_fragments_cleanup()70{71pft_cleanup72}7374atf_test_case "v6" "cleanup"75v6_head()76{77atf_set descr 'IPv6 fragmentation test'78atf_set require.user root79atf_set require.progs python3 scapy80}8182v6_body()83{84pft_init8586epair_send=$(vnet_mkepair)87epair_link=$(vnet_mkepair)8889vnet_mkjail alcatraz ${epair_send}b ${epair_link}a90vnet_mkjail singsing ${epair_link}b9192ifconfig ${epair_send}a inet6 2001:db8:42::1/64 no_dad up9394jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 no_dad up95jexec alcatraz ifconfig ${epair_link}a inet6 2001:db8:43::2/64 no_dad up96jexec alcatraz sysctl net.inet6.ip6.forwarding=19798jexec singsing ifconfig ${epair_link}b inet6 2001:db8:43::3/64 no_dad up99jexec singsing route add -6 2001:db8:42::/64 2001:db8:43::2100route add -6 2001:db8:43::/64 2001:db8:42::2101102jexec alcatraz ifconfig ${epair_send}b inet6 -ifdisabled103jexec alcatraz ifconfig ${epair_link}a inet6 -ifdisabled104jexec singsing ifconfig ${epair_link}b inet6 -ifdisabled105ifconfig ${epair_send}a inet6 -ifdisabled106107ifconfig ${epair_send}a108jexec alcatraz ifconfig ${epair_send}b109lladdr=$(jexec alcatraz ifconfig ${epair_send}b | awk '/ scopeid / { print($2); }' | cut -f 1 -d %)110111jexec alcatraz pfctl -e112pft_set_rules alcatraz \113"set reassemble yes" \114"pass keep state" \115"block in" \116"pass in inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \117"pass in inet6 proto icmp6 icmp6-type { echoreq, echorep }" \118"set skip on lo"119120# Host test121atf_check -s exit:0 -o ignore \122ping -6 -c 1 2001:db8:42::2123124atf_check -s exit:0 -o ignore \125ping -6 -c 1 -s 4500 2001:db8:42::2126127atf_check -s exit:0 -o ignore\128ping -6 -c 1 -b 70000 -s 65000 2001:db8:42::2129130# Force an NDP lookup131ping -6 -c 1 ${lladdr}%${epair_send}a132133atf_check -s exit:0 -o ignore\134ping -6 -c 1 -b 70000 -s 65000 ${lladdr}%${epair_send}a135136# Forwarding test137atf_check -s exit:0 -o ignore \138ping -6 -c 1 2001:db8:43::3139140atf_check -s exit:0 -o ignore \141ping -6 -c 1 -s 4500 2001:db8:43::3142143atf_check -s exit:0 -o ignore\144ping -6 -c 1 -b 70000 -s 65000 2001:db8:43::3145146$(atf_get_srcdir)/CVE-2019-5597.py \147${epair_send}a \1482001:db8:42::1 \1492001:db8:43::3150}151152v6_cleanup()153{154pft_cleanup155}156157atf_test_case "v6_route_to" "cleanup"158v6_route_to_head()159{160atf_set descr 'Test IPv6 reassembly combined with route-to'161atf_set require.user root162}163164v6_route_to_body()165{166pft_init167168epair_send=$(vnet_mkepair)169epair_link=$(vnet_mkepair)170171vnet_mkjail alcatraz ${epair_send}b ${epair_link}a172vnet_mkjail singsing ${epair_link}b173174ifconfig ${epair_send}a inet6 2001:db8:42::1/64 no_dad up175176jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 no_dad up177jexec alcatraz ifconfig ${epair_link}a inet6 2001:db8:43::2/64 no_dad up178jexec alcatraz sysctl net.inet6.ip6.forwarding=1179180jexec singsing ifconfig ${epair_link}b inet6 2001:db8:43::3/64 no_dad up181jexec singsing route add -6 2001:db8:42::/64 2001:db8:43::2182route add -6 2001:db8:43::/64 2001:db8:42::2183184jexec alcatraz ifconfig ${epair_send}b inet6 -ifdisabled185jexec alcatraz ifconfig ${epair_link}a inet6 -ifdisabled186jexec singsing ifconfig ${epair_link}b inet6 -ifdisabled187ifconfig ${epair_send}a inet6 -ifdisabled188189jexec alcatraz pfctl -e190pft_set_rules alcatraz \191"set reassemble yes" \192"pass" \193"pass in route-to (${epair_link}a 2001:db8:43::3) inet6 proto icmp6 from any to 2001:db8:43::3 keep state"194195# Forwarding test196atf_check -s exit:0 -o ignore \197ping -6 -c 1 2001:db8:43::3198199atf_check -s exit:0 -o ignore \200ping -6 -c 1 -s 4500 2001:db8:43::3201202atf_check -s exit:0 -o ignore\203ping -6 -c 1 -b 70000 -s 65000 2001:db8:43::3204205# Now test this without fragmentation206pft_set_rules alcatraz \207"set reassemble no" \208"pass" \209"pass in route-to (${epair_link}a 2001:db8:43::3) inet6 proto icmp6 from any to 2001:db8:43::3 keep state"210211atf_check -s exit:0 -o ignore \212ping -6 -c 1 2001:db8:43::3213214atf_check -s exit:0 -o ignore \215ping -6 -c 1 -s 4500 2001:db8:43::3216217atf_check -s exit:0 -o ignore\218ping -6 -c 1 -b 70000 -s 65000 2001:db8:43::3219}220221v6_route_to_cleanup()222{223pft_cleanup224}225226atf_test_case "mtu_diff" "cleanup"227mtu_diff_head()228{229atf_set descr 'Test reassembly across different MTUs, PR #255432'230atf_set require.user root231}232233mtu_diff_body()234{235pft_init236237epair_small=$(vnet_mkepair)238epair_large=$(vnet_mkepair)239240vnet_mkjail first ${epair_small}b ${epair_large}a241vnet_mkjail second ${epair_large}b242243ifconfig ${epair_small}a 192.0.2.1/25 up244jexec first ifconfig ${epair_small}b 192.0.2.2/25 up245246jexec first sysctl net.inet.ip.forwarding=1247jexec first ifconfig ${epair_large}a 192.0.2.130/25 up248jexec first ifconfig ${epair_large}a mtu 9000249jexec second ifconfig ${epair_large}b 192.0.2.131/25 up250jexec second ifconfig ${epair_large}b mtu 9000251jexec second route add default 192.0.2.130252253route add 192.0.2.128/25 192.0.2.2254255jexec first pfctl -e256pft_set_rules first \257"set reassemble yes" \258"pass keep state"259260# Sanity checks261atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2262atf_check -s exit:0 -o ignore ping -c 1 192.0.2.130263atf_check -s exit:0 -o ignore ping -c 1 192.0.2.131264265# Large packet that'll get reassembled and sent out in one on the large266# epair267atf_check -s exit:0 -o ignore ping -c 1 -s 8000 192.0.2.131268}269270mtu_diff_cleanup()271{272pft_cleanup273}274275frag_common()276{277name=$1278279pft_init280281epair=$(vnet_mkepair)282vnet_mkjail alcatraz ${epair}a283284ifconfig ${epair}b inet 192.0.2.1/24 up285jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up286287jexec alcatraz pfctl -e288pft_set_rules alcatraz \289"set reassemble yes" \290"pass keep state"291292# Sanity check293atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2294295atf_check -s exit:0 -o ignore $(atf_get_srcdir)/frag-${1}.py \296--to 192.0.2.2 \297--fromaddr 192.0.2.1 \298--sendif ${epair}b \299--recvif ${epair}b300}301302atf_test_case "overreplace" "cleanup"303overreplace_head()304{305atf_set descr 'ping fragment that overlaps fragment at index boundary and replace it'306atf_set require.user root307atf_set require.progs python3 scapy308}309310overreplace_body()311{312frag_common overreplace313}314315overreplace_cleanup()316{317pft_cleanup318}319320atf_test_case "overindex" "cleanup"321overindex_head()322{323atf_set descr 'ping fragment that overlaps the first fragment at index boundary'324atf_set require.user root325atf_set require.progs python3 scapy326}327328overindex_body()329{330frag_common overindex331}332333overindex_cleanup()334{335pft_cleanup336}337338atf_test_case "overlimit" "cleanup"339overlimit_head()340{341atf_set descr 'ping fragment at index boundary that cannot be requeued'342atf_set require.user root343atf_set require.progs python3 scapy344}345346overlimit_body()347{348frag_common overlimit349}350351overlimit_cleanup()352{353pft_cleanup354}355356atf_test_case "overhole" "cleanup"357overhole_head()358{359atf_set descr 'ping fragment at index boundary which modifies pf hole counter'360atf_set require.user root361atf_set require.progs python3 scapy362}363364overhole_body()365{366frag_common overhole367}368369overhole_cleanup()370{371pft_cleanup372}373374atf_test_case "adjhole" "cleanup"375adjhole_head()376{377atf_set descr 'overlapping ping fragments which modifies pf hole counter'378atf_set require.user root379atf_set require.progs python3 scapy380}381382adjhole_body()383{384frag_common adjhole385}386387adjhole_cleanup()388{389pft_cleanup390}391392atf_test_case "reassemble" "cleanup"393reassemble_head()394{395atf_set descr 'Test reassembly'396atf_set require.user root397}398399reassemble_body()400{401pft_init402403epair=$(vnet_mkepair)404vnet_mkjail alcatraz ${epair}a405406ifconfig ${epair}b inet 192.0.2.1/24 up407jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up408409# Sanity check410atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2411412jexec alcatraz pfctl -e413pft_set_rules alcatraz \414"pass out" \415"block in" \416"pass in inet proto icmp all icmp-type echoreq"417418# Single fragment passes419atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2420421# But a fragmented ping does not422atf_check -s exit:2 -o ignore ping -c 1 -s 2000 192.0.2.2423424pft_set_rules alcatraz \425"set reassemble yes" \426"pass out" \427"block in" \428"pass in inet proto icmp all icmp-type echoreq"429430# Both single packet & fragmented pass when we scrub431atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2432atf_check -s exit:0 -o ignore ping -c 1 -s 2000 192.0.2.2433}434435reassemble_cleanup()436{437pft_cleanup438}439440atf_test_case "no_df" "cleanup"441no_df_head()442{443atf_set descr 'Test removing of DF flag'444atf_set require.user root445atf_set require.progs python3 scapy446}447448no_df_body()449{450setup_router_server_ipv4451452# Tester can send long packets which will get fragmented by the router.453# Replies from server will come in fragments which might get454# reassembled resulting in a long reply packet sent back to tester.455ifconfig ${epair_tester}a mtu 9000456jexec router ifconfig ${epair_tester}b mtu 9000457jexec router ifconfig ${epair_server}a mtu 1500458jexec server ifconfig ${epair_server}b mtu 1500459460# Sanity check.461ping_server_check_reply exit:0 --ping-type=icmp462463# Enable packet reassembly with clearing of the no-df flag.464pft_set_rules router \465"scrub all fragment reassemble no-df" \466"block" \467"pass inet proto icmp all icmp-type echoreq"468# Ping with non-fragmentable packets.469# pf will strip the DF flag resulting in fragmentation and packets470# getting properly forwarded.471ping_server_check_reply exit:0 --ping-type=icmp --send-length=2000 --send-flags DF472}473474no_df_cleanup()475{476pft_cleanup477}478479atf_test_case "reassemble_slowpath" "cleanup"480reassemble_slowpath_head()481{482atf_set descr 'Test reassembly on the slow path'483atf_set require.user root484atf_set require.progs python3 scapy485}486487reassemble_slowpath_body()488{489if ! sysctl -q kern.features.ipsec >/dev/null ; then490atf_skip "This test requires ipsec"491fi492493setup_router_server_ipv4494495# Now define an ipsec policy so we end up taking the slow path.496# We don't actually need the traffic to go through ipsec, we just don't497# want to go through ip_tryforward().498echo "flush;499spdflush;500spdadd 203.0.113.1/32 203.0.113.2/32 any -P out ipsec esp/transport//require;501add 203.0.113.1 203.0.113.2 esp 0x1001 -E aes-gcm-16 \"12345678901234567890\";" \502| jexec router setkey -c503504# Sanity check.505ping_server_check_reply exit:0 --ping-type=icmp506507# Enable packet reassembly with clearing of the no-df flag.508pft_set_rules router \509"scrub in on ${epair_tester}b fragment no reassemble" \510"scrub on ${epair_server}a fragment reassemble" \511"pass"512513# Ensure that the packet makes it through the slow path514atf_check -s exit:0 -o ignore \515ping -c 1 -s 2000 198.51.100.2516}517518reassemble_slowpath_cleanup()519{520pft_cleanup521}522523atf_test_case "dummynet" "cleanup"524dummynet_head()525{526atf_set descr 'dummynet + reassembly test'527atf_set require.user root528}529530dummynet_body()531{532pft_init533dummynet_init534535epair=$(vnet_mkepair)536vnet_mkjail alcatraz ${epair}a537538ifconfig ${epair}b inet 192.0.2.1/24 up539jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up540541# Sanity check542atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2543544jexec alcatraz dnctl pipe 1 config bw 600Byte/s545jexec alcatraz dnctl pipe 2 config bw 700Byte/s546547jexec alcatraz pfctl -e548pft_set_rules alcatraz \549"set reassemble yes" \550"block" \551"pass inet proto icmp all icmp-type echoreq dnpipe (1, 2)"552553atf_check -s exit:0 -o ignore ping -s 2000 -c 1 192.0.2.2554}555556dummynet_cleanup()557{558pft_cleanup559}560561atf_test_case "dummynet_nat" "cleanup"562dummynet_nat_head()563{564atf_set descr 'Test dummynet on NATed fragmented traffic'565atf_set require.user root566}567568dummynet_nat_body()569{570pft_init571dummynet_init572573epair_one=$(vnet_mkepair)574ifconfig ${epair_one}a 192.0.2.1/24 up575576epair_two=$(vnet_mkepair)577578vnet_mkjail alcatraz ${epair_one}b ${epair_two}a579jexec alcatraz ifconfig ${epair_one}b 192.0.2.2/24 up580jexec alcatraz ifconfig ${epair_two}a 198.51.100.1/24 up581jexec alcatraz sysctl net.inet.ip.forwarding=1582583vnet_mkjail singsing ${epair_two}b584jexec singsing ifconfig ${epair_two}b 198.51.100.2/24 up585jexec singsing route add default 198.51.100.1586587route add 198.51.100.0/24 192.0.2.2588589jexec alcatraz dnctl pipe 1 config bw 1600Byte/s590jexec alcatraz dnctl pipe 2 config bw 1700Byte/s591592jexec alcatraz pfctl -e593pft_set_rules alcatraz \594"set reassemble yes" \595"nat on ${epair_two}a from 192.0.2.0/24 -> (${epair_two}a)" \596"block in" \597"pass in inet proto icmp all icmp-type echoreq dnpipe (1, 2)"598599atf_check -s exit:0 -o ignore ping -c 1 198.51.100.2600atf_check -s exit:0 -o ignore ping -c 1 -s 2000 198.51.100.2601}602603dummynet_nat_cleanup()604{605pft_cleanup606}607608atf_test_case "dummynet_fragmented" "cleanup"609dummynet_fragmented_head()610{611atf_set descr 'Test dummynet on NATed fragmented traffic'612atf_set require.user root613atf_set require.progs python3 scapy614}615616dummynet_fragmented_body()617{618pft_init619dummynet_init620621# No test for IPv6. IPv6 fragment reassembly can't be disabled.622setup_router_dummy_ipv4623624jexec router dnctl pipe 1 config delay 1625626pft_set_rules router \627"set reassemble no" \628"block" \629"pass inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \630"pass in on ${epair_tester}b inet proto udp dnpipe (1, 1)" \631"pass out on ${epair_server}a inet proto udp" \632633ping_dummy_check_request exit:0 --ping-type=udp --send-length=10000 --send-frag-length=1280634635rules=$(mktemp) || exit 1636jexec router pfctl -qvsr | normalize_pfctl_s > $rules637638# Count that fragmented packets have hit the rule only once and that639# they have not created states. There is no stateful firewall support640# for fragmented packets.641grep -qE 'pass in on epair0b inet proto udp all keep state dnpipe\(1, 1\) .* Packets: 8 Bytes: 10168 States: 0 ' $rules ||642atf_fail "Fragmented packets not counted correctly"643}644645dummynet_fragmented_cleanup()646{647pft_cleanup648}649650atf_test_case "counters" "cleanup"651counters_head()652{653atf_set descr 'Test fragment counters'654atf_set require.user root655}656657counters_body()658{659pft_init660661epair=$(vnet_mkepair)662vnet_mkjail alcatraz ${epair}a663664ifconfig ${epair}b inet 192.0.2.1/24 up665jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up666667jexec alcatraz pfctl -e668pft_set_rules alcatraz \669"set reassemble yes" \670"pass keep state"671672# All fragment counters are zero673counters=$(jexec alcatraz pfctl -si -v | grep -A 4 '^Fragments')674atf_check -s exit:0 -o match:"current entries[[:space:]]+0" \675echo $counters676atf_check -s exit:0 -o match:"searches[[:space:]]+0" \677echo $counters678atf_check -s exit:0 -o match:"inserts[[:space:]]+0" \679echo $counters680atf_check -s exit:0 -o match:"removals[[:space:]]+0" \681echo $counters682683# They remain zero after we've seen non-fragmented traffic684atf_check -s exit:0 -o ignore \685ping -c 1 192.0.2.2686counters=$(jexec alcatraz pfctl -si -v | grep -A 4 '^Fragments')687atf_check -s exit:0 -o match:"current entries[[:space:]]+0" \688echo $counters689atf_check -s exit:0 -o match:"searches[[:space:]]+0" \690echo $counters691atf_check -s exit:0 -o match:"inserts[[:space:]]+0" \692echo $counters693atf_check -s exit:0 -o match:"removals[[:space:]]+0" \694echo $counters695696# But once we've reassembled they're no longer zero697# (Count is 2, because in + out)698atf_check -s exit:0 -o ignore \699ping -c 1 -s 2000 192.0.2.2700counters=$(jexec alcatraz pfctl -si -v | grep -A 4 '^Fragments')701atf_check -s exit:0 -o match:"current entries[[:space:]]+0" \702echo $counters703atf_check -s exit:0 -o match:"searches[[:space:]]+2" \704echo $counters705atf_check -s exit:0 -o match:"inserts[[:space:]]+2" \706echo $counters707atf_check -s exit:0 -o match:"removals[[:space:]]+2" \708echo $counters709}710711counters_cleanup()712{713pft_cleanup714}715716atf_init_test_cases()717{718atf_add_test_case "too_many_fragments"719atf_add_test_case "v6"720atf_add_test_case "v6_route_to"721atf_add_test_case "mtu_diff"722atf_add_test_case "overreplace"723atf_add_test_case "overindex"724atf_add_test_case "overlimit"725atf_add_test_case "overhole"726atf_add_test_case "adjhole"727atf_add_test_case "reassemble"728atf_add_test_case "no_df"729atf_add_test_case "reassemble_slowpath"730atf_add_test_case "dummynet"731atf_add_test_case "dummynet_nat"732atf_add_test_case "dummynet_fragmented"733atf_add_test_case "counters"734}735736737