#!/usr/bin/expect # # Pedit Main Test 5 # # Purpose: # To test the pedit/munge/set capabilities. # # What it does: # -Run tcpdump beforehand, and capture a hex dump of a packet. # -Install a rule stating that any packet that arrives on loopback # (ingress) and is destined for 127.0.0.1 will be edited. Offset 0 # will be rewritten to 0x12345678 # -Run tcpdump afterwards, and check the differences between packets # # Expected result: # -The ping will fail, and offset 0 of the packet will contain 0x12345678 # # Set description for use with libtest set libtest_description "Pedit action: setting a u32 range at offset 0." # Source common test procedures and run set-up commands source ./libtest.tcl # Delete any ingress qdiscs associated with loopback in order to clear state send "tc qdisc del dev lo ingress\r" # Send initial pings to establish a base case send "sleep 1; ping 127.0.0.1 -c 3 &\r " # Get a tcpdump hex output for a packet send "tcpdump -i lo -s 96 -vvv -x host 127.0.0.1 and icmp\[icmptype\]=icmp-echo -c 1\r" if [expect_tcpdump_results before_list] { output_test_failure "Error while expecting tcpdump results" return -1 } # Add an ingress qdisc send "tc qdisc add dev lo ingress\r" # Match IP destination 127.0.0.1, and edit these packets to set the 4 octets # at offset 0 to be 0x12345678 send "tc filter add dev lo parent ffff: protocol ip prio 10 u32 match ip dst 127.0.0.1/32 flowid 1:1 action pedit munge offset 0 u32 set 0x12345678 pipe action mirred egress redirect dev dummy0\r" # Send a few pings to 127.0.0.1 while tcpdump is running on dummy0 and capture # the data. expect -re ".*" send "sleep 1; ping 127.0.0.1 -c 2 &\r" # Get a tcpdump hex output for a packet send "tcpdump -i dummy0 -s 96 -vvv -x -c 1\r" if [expect_tcpdump_results after_list] { output_test_failure "Error while expecting tcpdump results" return -1 } # Extra logging output_test_message "Before: $before_list" output_test_message "After: $after_list" # Check the results. In this case, data at offsets 0-3 was changed. # Offsets 0-1 should be 0x1234 if {[lindex $before_list 0] == [lindex $after_list 0]} { output_test_failure "Test failed at offsets 0-1" return -1 } else { if {[lindex $after_list 0] != "1234"} { output_test_failure "Test failed at offsets 0-1." return -1 } else { output_test_message "Offsets 0-1 changed to 0x1234 (success)" } } # Offsets 2-3 should be 0x5678 if {[lindex $before_list 1] == [lindex $after_list 1]} { output_test_failure "Test failed at offsets 2-3" return -1 } else { if {[lindex $after_list 1] != "5678"} { output_test_failure "Test failed at offsets 2-3" return -1 } else { output_test_message "Offsets 2-3 changed to 0x5678 (success)" } } # Ensure that all expected matching portions of the packet are identical set exemptions [concat $DEFAULT_TCPDUMP_ICMP_EXEMPTION 0 1] if [compare_lists $before_list $after_list $exemptions $DEFAULT_TCPDUMP_LENGTH mismatch_index] { output_test_failure "Test failed at $mismatch_index" return -1 } expect -re ".*" output_test_success return 0