#!/usr/local/bin/perl
#
# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
# Yokogawa Electric Corporation, YDC Corporation,
# IPA (Information-technology Promotion Agency, Japan).
# All rights reserved.
# 
# Redistribution and use of this software in source and binary forms, with 
# or without modification, are permitted provided that the following 
# conditions and disclaimer are agreed and accepted by the user:
# 
# 1. Redistributions of source code must retain the above copyright 
# notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright 
# notice, this list of conditions and the following disclaimer in the 
# documentation and/or other materials provided with the distribution.
# 
# 3. Neither the names of the copyrighters, the name of the project which 
# is related to this software (hereinafter referred to as "project") nor 
# the names of the contributors may be used to endorse or promote products 
# derived from this software without specific prior written permission.
# 
# 4. No merchantable use may be permitted without prior written 
# notification to the copyrighters. However, using this software for the 
# purpose of testing or evaluating any products including merchantable 
# products may be permitted without any notification to the copyrighters.
# 
# 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHTERS, THE PROJECT AND 
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING 
# BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.  IN NO EVENT SHALL THE 
# COPYRIGHTERS, THE PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT,STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
# THE POSSIBILITY OF SUCH DAMAGE.
#
# $TAHI: v6eval/sample_stddef/aes-ctr/aes-ctr.seq,v 1.2 2007/03/07 05:05:39 akisada Exp $
#

use V6evalTool;

my $IF = 'Link0';

vCapture($IF);



#----------------------------------------------------------------------#
# initialization                                                       #
#                                                                      #
#                                                                      #
#        TH0                                                           #
#         |                                                            #
# ----+---+--- L1                                                      #
#     |                                                                #
#    TR0                                                               #
#     |                                                                #
# ----+---+--- L0                                                      #
#         |                                                            #
#        HUT                                                           #
#                                                                      #
#     L0:                                                              #
#         3ffe:501:ffff:100::/64                                       #
#     L1:                                                              #
#         3ffe:501:ffff:101::/64                                       #
#                                                                      #
#----------------------------------------------------------------------#

if(vRemote('ipsecEnable.rmt')) {
	vLog('NG');
	exit($V6evalTool::exitFatal);
}

if(vRemote('ipsecClearAll.rmt')) {
	vLog('NG');
	exit($V6evalTool::exitFatal);
}

# if(vRemote('reboot.rmt')) {
# 	vLog('NG');
# 	exit($V6evalTool::exitFatal);
# }

vSend($IF, 'ra');

vSleep(3);



#----------------------------------------------------------------------#
# collection of TN/NUT address                                         #
#----------------------------------------------------------------------#
vClear($IF);

vSend($IF, 'ereq');

my %ret = vRecv($IF, 5, 0, 0, 'ns', 'erep');
if($ret{status}) {
	vLog('NG'); 
	exit($V6evalTool::exitFail);
}

if($ret{recvFrame} eq 'ns') {
	vSend($IF, 'na');

	%ret=vRecv($IF, 5, 0, 0, 'erep');
	if($ret{status}) {
		vLog('NG'); 
		exit($V6evalTool::exitFail);
	}
}

if($ret{recvFrame} ne 'erep') {
	vLog('NG');
	exit($V6evalTool::exitFail);
}

my $base	= 'Frame_Ether.Packet_IPv6.Hdr_IPv6.';
my $tnaddr	= $ret{$base. 'DestinationAddress'};
my $nutaddr	= $ret{$base. 'SourceAddress'};



#----------------------------------------------------------------------#
# SAD/SPD configuration                                                #
#----------------------------------------------------------------------#
if(vRemote(
	'ipsecSetSAD.rmt',
	"src=$tnaddr",
	"dst=$nutaddr",
	'protocol=esp',
	'spi=0x1000',
	'mode=transport',
	'ealgo=aes-ctr',
	'ealgokey="ipv6readylogaescin01"',
	'eauth=hmac-sha1',
	'eauthkey="ipv6readylogsha1in01"',
	'nodump'
)) {
	vLog('NG');
	exit($V6evalTool::exitFatal);
}

#	'ealgo=3des-cbc',
#	'ealgokey="ipv6readylogo3descbcin01"',

if(vRemote(
	'ipsecSetSAD.rmt',
	"src=$nutaddr",
	"dst=$tnaddr",
	'protocol=esp',
	'spi=0x2000',
	'mode=transport',
	'ealgo=aes-ctr',
	'ealgokey="ipv6readylogaescout1"',
	'eauth=hmac-sha1',
	'eauthkey="ipv6readylogsha1out1"',
	'nodump'
)) {
	vLog('NG');
	exit($V6evalTool::exitFatal);
}

if(vRemote(
	'ipsecSetSPD.rmt',
	"src=$tnaddr",
	"dst=$nutaddr",
	'upperspec=any',
	'direction=in',
	'policy=ipsec',
	'protocol=esp',
	'mode=transport',
	'level=require',
	'ommit',
	'nodump'
)) {
	vLog('NG');
	exit($V6evalTool::exitFatal);
}

if(vRemote(
	'ipsecSetSPD.rmt',
	"src=$nutaddr",
	"dst=$tnaddr",
	'upperspec=any',
	'direction=out',
	'policy=ipsec',
	'protocol=esp',
	'mode=transport',
	'level=require',
	'ommit'
)) {
	vLog('NG');
	exit($V6evalTool::exitFatal);
}



vClear($IF);

vSend($IF, 'ereq_esp');
# vSend($IF, 'ereq');

%ret = vRecv($IF, 5, 0, 0, 'ns', 'erep_esp');
if($ret{status}) {
	vLog('NG'); 
	exit($V6evalTool::exitFail);
}

if($ret{recvFrame} eq 'ns') {
	vSend($IF, 'na');

	%ret=vRecv($IF, 5, 0, 0, 'erep_esp');
	if($ret{status}) {
		vLog('NG'); 
		exit($V6evalTool::exitFail);
	}
}

if($ret{recvFrame} ne 'erep_esp') {
	vLog('NG');
	exit($V6evalTool::exitFail);
}



vStop($IF);

vLog('OK');
exit $V6evalTool::exitPass;
