#!/usr/bin/env atf-sh1#-2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2020 Alexander V. Chernikov5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#27#2829. $(atf_get_srcdir)/../common/vnet.subr3031atf_test_case "valid_redirect" "cleanup"32valid_redirect_head() {3334atf_set descr 'Test valid IPv4 redirect'35atf_set require.user root36atf_set require.progs python3 scapy37}3839valid_redirect_body() {4041ids=6553342id=`printf "%x" ${ids}`43if [ $$ -gt 65535 ]; then44xl=`printf "%x" $(($$ - 65535))`45yl="1"46else47xl=`printf "%x" $$`48yl=""49fi5051vnet_init5253ip4a="192.0.2.1"54ip4b="192.0.2.2"5556net4="198.51.100.0/24"57dst_addr4="198.51.100.42"5859# remote_rtr60remote_rtr_ip="192.0.2.3"61remote_rtr_mac="00:00:5E:00:53:42"6263new_rtr_ip="192.0.2.4"6465script_name="redirect.py"6667epair=$(vnet_mkepair)68ifconfig ${epair}a up69ifconfig ${epair}a inet ${ip4a}/247071jname="v4t-${id}-${yl}-${xl}"72vnet_mkjail ${jname} ${epair}b73jexec ${jname} ifconfig ${epair}b up74jexec ${jname} ifconfig ${epair}b inet ${ip4b}/247576# Setup static entry for the remote router77jexec ${jname} arp -s ${remote_rtr_ip} ${remote_rtr_mac}78# setup prefix reachable via router79jexec ${jname} route add -4 -net ${net4} ${remote_rtr_ip}8081local_ip=${ip4b}82local_mac=`jexec ${jname} ifconfig ${epair}b ether | awk '$1~/ether/{print$2}'`8384# echo "LOCAL: ${local_ip} ${local_mac}"85# echo "REMOTE: ${remote_rtr_ip} ${remote_rtr_mac}"8687atf_check -s exit:0 $(atf_get_srcdir)/${script_name} \88--smac ${remote_rtr_mac} --dmac ${local_mac} \89--sip ${remote_rtr_ip} --dip ${local_ip} \90--route ${dst_addr4} --gw ${new_rtr_ip} \91--iface ${epair}a9293atf_check -o match:"destination: ${dst_addr4}\$" jexec ${jname} route -n get -4 ${dst_addr4}94atf_check -o match:'flags: <UP,GATEWAY,HOST,DYNAMIC,DONE>' jexec ${jname} route -n get -4 ${dst_addr4}95}9697valid_redirect_cleanup() {9899vnet_cleanup100}101102atf_init_test_cases()103{104105atf_add_test_case "valid_redirect"106}107108# end109110111112