|
| 1 | +#!/bin/bash |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | +# Copyright (c) 2025 Anthony Harivel |
| 4 | +# Test IPv4 fragmentation |
| 5 | + |
| 6 | +. $(dirname $0)/_init.sh |
| 7 | + |
| 8 | +p0=${run_id}0 |
| 9 | +p1=${run_id}1 |
| 10 | + |
| 11 | +grcli interface add port $p0 devargs net_tap0,iface=$p0 mac f0:0d:ac:dc:00:00 |
| 12 | +grcli interface add port $p1 devargs net_tap1,iface=$p1 mac f0:0d:ac:dc:00:01 |
| 13 | +grcli address add 10.0.0.1/24 iface $p0 |
| 14 | +grcli address add 10.1.0.1/24 iface $p1 |
| 15 | + |
| 16 | +# Keep default MTU on p0 (1500) |
| 17 | +# Set smaller MTU on p1 (egress) to force fragmentation |
| 18 | +# Note: Minimum MTU in grout is 1280 (IPv6 requirement) |
| 19 | +grcli interface set port $p1 mtu 1280 |
| 20 | + |
| 21 | +# Setup network namespaces |
| 22 | +for n in 0 1; do |
| 23 | + p=$run_id$n |
| 24 | + netns_add $p |
| 25 | + ip link set $p mtu 1500 |
| 26 | + ip link set $p netns $p |
| 27 | + ip -n $p link set $p address ba:d0:ca:ca:00:0$n |
| 28 | + ip -n $p link set $p up |
| 29 | + ip -n $p link set lo up |
| 30 | + ip -n $p addr add 10.$n.0.2/24 dev $p |
| 31 | + ip -n $p route add default via 10.$n.0.1 |
| 32 | + # Clear PMTU cache to ensure kernel uses interface MTU |
| 33 | + ip -n $p route flush cache |
| 34 | +done |
| 35 | + |
| 36 | +# Test 1: Ping with default packet size (should work without fragmentation) |
| 37 | +ip netns exec $p0 ping -i0.01 -c3 -n 10.1.0.2 |
| 38 | + |
| 39 | +# Test 2: Large packet with DF flag set (should get ICMP fragmentation needed error) |
| 40 | +# Send 1260-byte packet with DF=1 (Don't Fragment) |
| 41 | +# Packet size: 1260 + 8 (ICMP) + 20 (IP) = 1288 bytes |
| 42 | +# Fits in p0 MTU (1500) but exceeds p1 MTU (1280) |
| 43 | +# Expected: ICMP Type 3 Code 4 (Fragmentation Needed and DF Set) |
| 44 | +ip netns exec $p0 ping -i0.01 -c3 -s 1260 -M do -n 10.1.0.2 && fail "ping with DF flag should have failed" |
| 45 | + |
| 46 | +# Test 3: Large packet without DF flag (should fragment and succeed) |
| 47 | +# Send 1260-byte packet with DF=0 (fragmentation allowed) |
| 48 | +# Packet size: 1260 + 8 (ICMP) + 20 (IP) = 1288 bytes |
| 49 | +# Fits in p0 MTU (1500) but needs fragmentation for p1 MTU (1280) |
| 50 | +# Expected: Packet is fragmented into 2 fragments (1276 + 32 bytes) and ping succeeds |
| 51 | +ip netns exec $p0 ip route flush cache |
| 52 | +ip netns exec $p0 ping -i0.01 -c3 -s 1260 -M dont -n 10.1.0.2 |
0 commit comments