#!/usr/bin/perl
#
# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
# Yokogawa Electric Corporation.
# 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.
# 
# 5. The copyrighters, the project and the contributors may prohibit
#    the use of this software at any time.
# 
# 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.
#
# $Id: IKEv2-SGW-R-1-2-7-1-A.seq,v 1.4 2009/06/16 05:38:53 doo Exp $
#
################################################################

BEGIN
{
	push(@INC, '..');
	$ENV{'V6EVAL_WITH_KOI'} = 'TRUE';
}

END {}



use V6evalTool;
use IKEv2v6eval;
use IKEv2;
use strict;



my $remote	= 'common_remote_index';
my $selector	= 'common_selector_index_outbound';

my $session	= undef;
my $ike_sa_init_req_param	= undef;
my $ike_sa_init_resp_param	= undef;
my $ike_auth_req_param	= undef;
my $ike_auth_resp_param	= undef;
my $create_child_sa_req_param	= undef;
my $create_child_sa_resp_param	= undef;

my $material	= undef;
my $child_sa_material = undef;
my $ret	= undef;
my $str = undef;

my @local_ike_auth_resp = undef;
my @local_create_child_sa_req = undef;
my @local_create_child_sa_resp = undef;

kLogHTML('<FONT COLOR="#FF0000" SIZE="+1"><U><B>TEST SETUP</B></U></FONT>');



#------------------------------#
# register IKEv2.pm            #
#------------------------------#
unless(IKEv2initialize('SGW', 'SGW')) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 module initializing failure</FONT>');
}



#---------------------------------#
# switch Support or Not Support   #
#---------------------------------#
unless ($config_pl->{'advanced_support_creating_new_child_sa'}) {
	IKEv2exitNS('Not support ' . "'advanced_support_creating_new_child_sa'");
}



#--------------------------------------#
# prepare IKE_AUTH request             #
#--------------------------------------#
my $common_gen_ike_auth_req = $gen_ike_auth_req->{'common_remote_index'};
my @local_ike_auth_req = @{$common_gen_ike_auth_req};
# remove N(USE_TRANSPORT_MODE)
@local_ike_auth_req = grep {
	my $ret = 0;
	if ($_->{'self'} ne 'N') { $ret = 1; }
	if ($_->{'type'} ne 'USE_TRANSPORT_MODE') { $ret = 1; }
	$ret;
} @local_ike_auth_req;

foreach my $payload (@local_ike_auth_req) {
	if ($payload->{'self'} eq 'TSi') {
		foreach my $selector (@{$payload->{'selectors'}}) {
			if (defined($selector->{'protocol'})) {
				$selector->{'saddr'} = $addresses{'th2_global_linkY'};
				$selector->{'eaddr'} = $addresses{'th2_global_linkY'};
			}
		}
	}

	if ($payload->{'self'} eq 'TSr') {
		foreach my $selector (@{$payload->{'selectors'}}) {
			if (defined($selector->{'protocol'})) {
				$selector->{'saddr'} = $addresses{'th1_global_linkB'};
				$selector->{'eaddr'} = $addresses{'th1_global_linkB'};
			}
		}
	}
}
$gen_ike_auth_req->{'SGW_R_IKE_AUTH_REQ'} = \@local_ike_auth_req;



#--------------------------------------#
# prepare IKE_AUTH response            #
#--------------------------------------#
my $common_exp_ike_auth_resp = $exp_ike_auth_resp->{'common_remote_index'};
my @local_ike_auth_resp = @{$common_exp_ike_auth_resp};
# remove N(USE_TRANSPORT_MODE)
foreach my $payload (@local_ike_auth_resp) {
	if ($payload->{'self'} eq 'E') {
		my @payloads = grep {
			my $ret = 0;
			if ($_->{'self'} ne 'N') { $ret = 1; }
			if ($_->{'type'} ne 'USE_TRANSPORT_MODE') { $ret = 1; }
			$ret;
		} @{$payload->{'innerPayloads'}};

		$payload->{'innerPayloads'} = \@payloads;
	}
}
# set correct nexttype
for (my $i = 0; $i < scalar(@local_ike_auth_resp); $i++) {
	my $payload = $local_ike_auth_resp[$i];

	if ($payload->{'self'} eq 'E') {
		foreach my $payload2 (@{$payload->{'innerPayloads'}}) {
			if ($payload2->{'self'} eq 'TSi') {
				foreach my $selector (@{$payload2->{'selectors'}}) {
					if (defined($selector->{'protocol'})) {
						$selector->{'saddr'} = $addresses{'th2_global_linkY'};
						$selector->{'eaddr'} = $addresses{'th2_global_linkY'};
					}
				}
			}

			if ($payload2->{'self'} eq 'TSr') {
				foreach my $selector (@{$payload2->{'selectors'}}) {
					if (defined($selector->{'protocol'})) {
						$selector->{'saddr'} = $addresses{'th1_global_linkB'};
						$selector->{'eaddr'} = $addresses{'th1_global_linkB'};
					}
				}
			}
		}

		for (my $j = 0; $j < scalar(@{$payload->{'innerPayloads'}}); $j++) {
			my $cur = $payload->{'innerPayloads'}->[$j];
			my $next = $payload->{'innerPayloads'}->[$j+1];
			unless (defined($next)) {
				$cur->{'nexttype'} = '0';
				next;
			}
			if ($cur->{'nexttype'} eq $next->{'self'}) {
				next;
			}
			$cur->{'nexttype'} = $next->{'self'};
		}

		next;
	}
}
$exp_ike_auth_resp->{'SGW_R_IKE_AUTH_RESP'} = \@local_ike_auth_resp;



#--------------------------------------#
# prepare CREATE_CHILD_SA request      #
#--------------------------------------#
my $common_gen_create_child_sa_req = $gen_create_child_sa_req->{'common_remote_index'};
my @local_create_child_sa_req = @{$common_gen_create_child_sa_req};
# remove N(USE_TRANSPORT_MODE)
@local_create_child_sa_req = grep {
	my $ret = 0;
	if ($_->{'self'} ne 'N') { $ret = 1; }
	if ($_->{'type'} ne 'USE_TRANSPORT_MODE') { $ret = 1; }
	$ret;
} @local_create_child_sa_req;

foreach my $payload (@local_create_child_sa_req) {
	if ($payload->{'self'} eq 'TSi') {
		foreach my $selector (@{$payload->{'selectors'}}) {
			if (defined($selector->{'protocol'})) {
				$selector->{'saddr'} = $addresses{'th3_global_linkY'};
				$selector->{'eaddr'} = $addresses{'th3_global_linkY'};
			}
		}
	}

	if ($payload->{'self'} eq 'TSr') {
		foreach my $selector (@{$payload->{'selectors'}}) {
			if (defined($selector->{'protocol'})) {
				$selector->{'saddr'} = $addresses{'th1_global_linkB'};
				$selector->{'eaddr'} = $addresses{'th1_global_linkB'};
			}
		}
	}
}
$gen_create_child_sa_req->{'SGW_R_CREATE_CHILD_SA_REQ'} = \@local_create_child_sa_req;



#--------------------------------------#
# prepare CREATE_CHILD_SA response     #
#--------------------------------------#
my $common_exp_create_child_sa_resp = $exp_create_child_sa_resp->{'common_remote_index'};
my @local_create_child_sa_resp = @{$common_exp_create_child_sa_resp};
# remove N(USE_TRANSPORT_MODE)
foreach my $payload (@local_create_child_sa_resp) {
	if ($payload->{'self'} eq 'E') {
		my @payloads = grep {
			my $ret = 0;
			if ($_->{'self'} ne 'N') { $ret = 1; }
			if ($_->{'type'} ne 'USE_TRANSPORT_MODE') { $ret = 1; }
			$ret;
		} @{$payload->{'innerPayloads'}};
		$payload->{'innerPayloads'} = \@payloads;
	}
}
# set correct nexttype
for (my $i = 0; $i < scalar(@local_create_child_sa_resp); $i++) {
	my $payload = $local_create_child_sa_resp[$i];

	if ($payload->{'self'} eq 'E') {
		foreach my $payload2 (@{$payload->{'innerPayloads'}}) {
			if ($payload2->{'self'} eq 'TSi') {
				foreach my $selector (@{$payload2->{'selectors'}}) {
					if (defined($selector->{'protocol'})) {
						$selector->{'saddr'} = $addresses{'th3_global_linkY'};
						$selector->{'eaddr'} = $addresses{'th3_global_linkY'};
					}
				}
			}

			if ($payload2->{'self'} eq 'TSr') {
				foreach my $selector (@{$payload2->{'selectors'}}) {
					if (defined($selector->{'protocol'})) {
						$selector->{'saddr'} = $addresses{'th1_global_linkB'};
						$selector->{'eaddr'} = $addresses{'th1_global_linkB'};
					}
				}
			}
		}

		$payload->{'innerType'} = @{$payload->{'innerPayloads'}}[0]->{'self'};

		for (my $j = 0; $j < scalar(@{$payload->{'innerPayloads'}}); $j++) {
			my $cur = $payload->{'innerPayloads'}->[$j];
			my $next = $payload->{'innerPayloads'}->[$j+1];
			unless (defined($next)) {
				$cur->{'nexttype'} = '0';
				next;
			}
			if ($cur->{'nexttype'} eq $next->{'self'}) {
				next;
			}
			$cur->{'nexttype'} = $next->{'self'};
		}

		next;
	}
}
$exp_create_child_sa_resp->{'SGW_R_CREATE_CHILD_SA_RESP'} = \@local_create_child_sa_resp;



#------------------------------#
# configure TN                 #
#------------------------------#
unless(IKEv2setupTN()) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">TN setup failure</FONT>');
}



#------------------------------#
# configure NUT                #
#------------------------------#
unless(IKEv2setupNUT()) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">NUT setup failure</FONT>');
}



kLogHTML('<FONT COLOR="#FF0000" SIZE="+1"><U><B>TEST PROCEDURE</B></U></FONT>');


#------------------------------#
# prepare session handler      #
#------------------------------#
$session = IKEv2create_session($remote);
unless(defined($session)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 session creation failure</FONT>');
}



#--------------------------------------#
# send IKE_SA_INIT request to NUT      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| IKE_SA_INIT request (HDR, SAi1, KEi, Ni)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);



$ike_sa_init_req_param = devel_IKEv2send_IKE_SA_INIT_request($session,
							     $remote);
unless(defined($ike_sa_init_req_param)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send IKE_SA_INIT request.</FONT>');
}



#--------------------------------------#
# receive IKE_SA_INIT response by NUT  #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |-------------->| IKE_SA_INIT response (HDR, SAr1, KEr, Nr)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);



$ike_sa_init_resp_param = devel_IKEv2receive_IKE_SA_INIT_response($session,
								  $remote,
								  $ike_sa_init_req_param);
unless(defined($ike_sa_init_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe IKE_SA_INIT response.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# generate keying material             #
#--------------------------------------#
$material = devel_IKEv2gen_keymat(1, $ike_sa_init_req_param, $ike_sa_init_resp_param);
unless(defined($material)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t generate keying material.</FONT>');
}



#--------------------------------------#
# send IKE_AUTH request to NUT         #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| IKE_AUTH request (HDR, SK {IDi, AUTH, SAi2, TSi, TSr})\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);



$ike_auth_req_param = devel_IKEv2send_IKE_AUTH_request($session,
						       'SGW_R_IKE_AUTH_REQ',
						       $ike_sa_init_req_param,
						       $ike_sa_init_resp_param,
						       $material);
unless(defined($ike_auth_req_param)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send IKE_AUTH request.</FONT>');
}



#--------------------------------------#
# receive IKE_AUTH response by NUT     #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |-------------->| IKE_AUTH response (HDR, SK {IDr, AUTH, SAr2, TSi, TSr)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);



$ike_auth_resp_param = devel_IKEv2receive_IKE_AUTH_response($session,
							    'SGW_R_IKE_AUTH_RESP',
							    $ike_sa_init_req_param,
							    $ike_sa_init_resp_param,
							    $ike_auth_req_param,
							    $material);
unless(defined($ike_auth_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe IKE_AUTH response.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# prepare ICMP session handler         #
#--------------------------------------#
my $icmp_session_th2 = ICMPcreate_session('TH2sendtoTH1');
unless(defined($icmp_session_th2)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">ICMP session creation failure</FONT>');
}

my $icmp_session_th1_th2 = ICMPcreate_session('TH1sendtoTH2');
unless(defined($icmp_session_th1_th2)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">ICMP session creation failure</FONT>');
}



#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2\n";
$str .= "     |               |               |               |\n";
$str .= "     |<--------------+===============+---------------| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |\n";
$str .= "     V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend($icmp_session_th2, 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th1_th2);
unless(defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe ICMP packet.</FONT>');
	IKEv2exitFail();
}

#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2\n";
$str .= "     |               |               |               |\n";
$str .= "     |---------------+===============+-------------->| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |\n";
$str .= "     V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend_v6eval('TH1sendtoTH2', 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th2);
unless(defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe ICMP packet.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# prepare ICMP session handler         #
#--------------------------------------#
my $icmp_session_th3 = ICMPcreate_session('TH3sendtoTH1');
unless(defined($icmp_session_th3)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">ICMP session creation failure</FONT>');
}

my $icmp_session_th1_th3 = ICMPcreate_session('TH1sendtoTH3');
unless(defined($icmp_session_th1_th3)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">ICMP session creation failure</FONT>');
}



#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2             TH3\n";
$str .= "     |               |               |               |               |\n";
$str .= "     |       X-------+===============+-------------------------------| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |               |\n";
$str .= "     V               V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend($icmp_session_th3, 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th1_th3);
if (defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can observe ICMP packet.</FONT>');
	IKEv2exitFail();
}

#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2             TH3\n";
$str .= "     |               |               |               |               |\n";
$str .= "     |---------------+=======X       |               |               | IPsec {Echo Reply}\n";
$str .= "     |               |               |               |               |\n";
$str .= "     V               V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend_v6eval('TH1sendtoTH3', 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th3);
if (defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can observe ICMP packet.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# send CREATE_CHILD_SA request         #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| CREATE_CHILD_SA request (HDR, SK {N(REKEY_SA), SA, Ni, TSi, TSr})\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);

$create_child_sa_req_param = devel_IKEv2send_CREATE_CHILD_SA_request($session,
								     'SGW_R_CREATE_CHILD_SA_REQ',
								     $ike_sa_init_req_param,
								     $ike_sa_init_resp_param,
								     $ike_auth_req_param,
								     $material);
unless (defined($create_child_sa_req_param)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send CREATE_CHILD_SA request.</FONT>');
}



#--------------------------------------#
# receive CREATE_CHILD_SA response     #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |-------------->| CREATE_CHILD_SA response (HDR, SK {SA, Nr, TSi, TSr})\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);



$create_child_sa_resp_param = devel_IKEv2receive_CREATE_CHILD_SA_response($session,
									  'SGW_R_CREATE_CHILD_SA_RESP',
									  $ike_sa_init_req_param,
									  $ike_sa_init_resp_param,
									  $create_child_sa_resp_param,
									  $material);
unless(defined($create_child_sa_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe CREATE_CHILD_SA response.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2\n";
$str .= "     |               |               |               |\n";
$str .= "     |<--------------+===============+---------------| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |\n";
$str .= "     V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend($icmp_session_th2, 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th1_th2);
unless(defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe ICMP packet.</FONT>');
	IKEv2exitFail();
}

#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2\n";
$str .= "     |               |               |               |\n";
$str .= "     |---------------+===============+-------------->| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |\n";
$str .= "     V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend_v6eval('TH1sendtoTH2', 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th2);
unless(defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe ICMP packet.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2             TH3\n";
$str .= "     |               |               |               |               |\n";
$str .= "     |<--------------+===============+-------------------------------| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |               |\n";
$str .= "     V               V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend($icmp_session_th3, 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th1_th3);
unless(defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe ICMP packet.</FONT>');
	IKEv2exitFail();
}

#--------------------------------------#
# send Echo Reply                      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "                    (I)             (R)\n";
$str .= "    TH1             NUT             TN1             TH2             TH3\n";
$str .= "     |               |               |               |               |\n";
$str .= "     |---------------+===============+---------------|-------------->| IPsec {Echo Reply}\n";
$str .= "     |               |               |               |               |\n";
$str .= "     V               V               V               V               V";
$str .= '</PRE>';

kLogHTML($str);

$ret = ICMPsend_v6eval('TH1sendtoTH3', 'echo_reply');
unless (defined($ret)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send ICMP packet.</FONT>');
}



#--------------------------------------#
# receive Echo Reply                   #
#--------------------------------------#
$ret = ICMPreceive($icmp_session_th3);
unless(defined($ret)) {
	my $strerror = kDump_Common_Error();
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe ICMP packet.</FONT>');
	IKEv2exitFail();
}



#--------------------------------------#
# exit with cleanup                    #
#--------------------------------------#
IKEv2exitPass();



#
# perldoc
#
########################################################################
__END__

=head1 Title

  Test IKEv2.SGW.R.1.2.7.1: Receipt of cryptographically protected message on the new SA
  Part A: (ADVANCED)

=head1 Purpose

  To verify an IKEv2 device properly recognizes the lifetime of CHILD_SAs. 

=head1 References

  * [RFC 4306] - Sections 2.8

=head1 Test Setup

  * Network Topology
      Connect the devices according to the Common Topology.
  * Configuration
      In each part, configure the devices according to the Common Configuration.
  * Pre-Sequence and Cleanup Sequence
      IKEv2 on the NUT is disabled after each part.

=head1 Procedure

=begin html

<BLOCKQUOTE>
<PRE>

   TH1            NUT           TN1           TH2           TH3
 (HOST)          (SGW)         (SGW)         (Host)        (Host)
    |              |             |             |             |
    |              |<------------|             |             | IKE_SA_INIT request (HDR, SAi1, KEi, Ni)
    |              |             |             |             | (Packet #1)
    |              |------------>|             |             | IKE_SA_INIT Response (HDR, SAr1, KEr, Nr)
    |              |             |             |             | (Judgement #1)
    |              |             |             |             |
    |              |<------------|             |             | IKE_AUTH request (HDR, SK {IDi, AUTH, SAi2, TSi, TSr})
    |              |             |             |             | (Packet #2)
    |              |------------>|             |             | IKE_AUTH Response (HDR, SK {IDr, AUTH, SAr2, TSi, TSr})
    |              |             |             |             | (Judgement #2)
    |              |             |             |             |
    |<-------------+=============+-------------|             | IPSec {Echo Request}
    |              |             |             |             | (Packet #3) (Judgement #3)
    |--------------+=============+------------>|             | IPSec {Echo Reply}
    |              |             |             |             | (Packet #4) (Judgement #4)
    |              |             |             |             |
    |       X------+=============+-------------+-------------| IPSec {Echo Request}
    |              |             |             |             | (Packet #5) (Judgement #5)
    |--------------+=====X       |             |             | IPSec {Echo Reply}
    |              |             |             |             | (Packet #6) (Judgement #6)
    |              |             |             |             |
    |              |<------------|             |             | CREATE_CHILD_SA request (HDR, SK {SA, Ni, TSi, TSr})
    |              |             |             |             | (Packet #7)
    |              |------------>|             |             | CREATE_CHILD_SA response (HDR, SK {SA, Nr, TSi, TSr})
    |              |             |             |             | (Judgement #8)
    |              |             |             |             |
    |<-------------+=============+-------------|             | IPSec {Echo Request}
    |              |             |             |             | (Packet #8) (Judgement #8)
    |--------------+=============+------------>|             | IPSec {Echo Reply}
    |              |             |             |             | (Packet #9) (Judgement #9)
    |              |             |             |             |
    |<-------------+=============+-------------+-------------| IPSec {Echo Request}
    |              |             |             |             | (Packet #10) (Judgement #10)
    |--------------+=============+-------------+------------>| IPSec {Echo Reply}
    |              |             |             |             | (Packet #11) (Judgement #11)
    |              |             |             |             |
    V              V             V             V             V

</PRE>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD><TT>Packet #1</TT></TD>
<TD><TT>See Common Packet #1</TT></TD>
</TR>
<TR>
<TD><TT>Packet #2</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
<TR>
<TD><TT>Packet #3</TT></TD>
<TD><TT>See Common Packet #21</TT></TD>
</TR>
<TR>
<TD><TT>Packet #4</TT></TD>
<TD><TT>See Common Packet #25</TT></TD>
</TR>
<TR>
<TD><TT>Packet #5</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
<TR>
<TD><TT>Packet #6</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
<TR>
<TD><TT>Packet #7</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
<TR>
<TD><TT>Packet #8</TT></TD>
<TD><TT>See Common Packet #21</TT></TD>
</TR>
<TR>
<TD><TT>Packet #9</TT></TD>
<TD><TT>See Common Packet #25</TT></TD>
</TR>
<TR>
<TD><TT>Packet #10</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
<TR>
<TD><TT>Packet #11</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
</TABLE>
</BLOCKQUOTE>

<BLOCKQUOTE>
Packet #2: IKE_AUTH request
<TABLE BORDER>
<TR>
<TD>IPv6 Header</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>UDP Header</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>IKEv2 Header</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>E Payload</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>IDi Payload</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>AUTH Payload</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>N Payload</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD>SA Payload</TD>
<TD colspan="2">Same as the Common Packet #5</TD>
</TR>
<TR>
<TD rowspan="2">TSi Payload</TD>
<TD colspan="2">Other fields are same as the Common Packet #5</TD>
</TR>
<TR>
<TD>Traffic Selectors</TD>
<TD>See below</TD>
</TR>
<TR>
<TD rowspan="2">TSr Payload</TD>
<TD colspan="2">Other fields are same as the Common Packet #5</TD>
</TR>
<TR>
<TD>Traffic Selectors</TD>
<TD>See below</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD rowspan="7">TSi Payload</TD>
<TD rowspan="7">Traffic Selector</TD>
<TD>TS Type</TD>
<TD>8 (IPV6_ADDR_RANGE)</TD>
</TR>
<TR>
<TD>IP Protocol ID</TD>
<TD>0 (any)</TD>
</TR>
<TR>
<TD>Selector Length</TD>
<TD>40</TD>
</TR>
<TR>
<TD>Start Port</TD>
<TD>0</TD>
</TR>
<TR>
<TD>End Port</TD>
<TD>65535</TD>
</TR>
<TR>
<TD>Starting Address</TD>
<TD>TH2's Global Address on Link B</TD>
</TR>
<TR>
<TD>Ending Address</TD>
<TD>TH2's Global Address on Link B</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD rowspan="7">TSr Payload</TD>
<TD rowspan="7">Traffic Selector</TD>
<TD>TS Type</TD>
<TD>8 (IPV6_ADDR_RANGE)</TD>
</TR>
<TR>
<TD>IP Protocol ID</TD>
<TD>0 (any)</TD>
</TR>
<TR>
<TD>Selector Length</TD>
<TD>40</TD>
</TR>
<TR>
<TD>Start Port</TD>
<TD>0</TD>
</TR>
<TR>
<TD>End Port</TD>
<TD>65535</TD>
</TR>
<TR>
<TD>Starting Address</TD>
<TD>TH1's Global Address on Link Y</TD>
</TR>
<TR>
<TD>Ending Address</TD>
<TD>TH1's Global Address on Link Y</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

<BLOCKQUOTE>
Packet #5: Echo Request
<TABLE BORDER>
<TR>
<TD rowspan="2">IPv6 Header</TD>
<TD>Source Address</TD>
<TD>TN1's Global Address on Link X</TD>
</TR>
<TR>
<TD>Destination Address</TD>
<TD>NUT's Global Address on Link A</TD>
</TR>
<TR>
<TD rowspan="7">ESP</TD>
<TD>Security Parameter Index</TD>
<TD>CHILD_SA's SPI value used by this message</TD>
</TR>
<TR>
<TD>Sequence Number</TD>
<TD>The value incremented the previous encrypted packet's Sequence Number by one.</TD>
</TR>
<TR>
<TD>Payload Data</TD>
<TD>Subsequent data encrypted by underlying encryption algorithm</TD>
</TR>
<TR>
<TD>Padding</TD>
<TD>Any value which to be a multiple of the encryption block size</TD>
</TR>
<TR>
<TD>Pad Length</TD>
<TD>The length of the Padding field</TD>
</TR>
<TR>
<TD>Next Header</TD>
<TD>41 (IPv6)</TD>
</TR>
<TR>
<TD>Integrity Check Value</TD>
<TD>The checksum must be valid by calculation according to the manner described in RFC.</TD>
</TR>
<TR>
<TD rowspan="2">IPv6 Header</TD>
<TD>Source Port</TD>
<TD>TH3's Global Address</TD>
</TR>
<TR>
<TD>Destination Port</TD>
<TD>TH1's Global Address</TD>
</TR>
<TR>
<TD rowspan="5">ICMPv6 Header</TD>
<TD>Type</TD>
<TD>128</TD>
</TR>
<TR>
<TD>Code</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Identifier</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Sequence Number</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Payload Data</TD>
<TD>0x0000000000000000</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

<BLOCKQUOTE>
Packet #6: Echo Request
<TABLE BORDER>
<TR>
<TD rowspan="2">IPv6 Header</TD>
<TD>Source Address</TD>
<TD>TH1's Global Address</TD>
</TR>
<TR>
<TD>Destination Address</TD>
<TD>TH3's Global Address</TD>
</TR>
<TR>
<TD rowspan="5">ICMPv6 Header</TD>
<TD>Type</TD>
<TD>128</TD>
</TR>
<TR>
<TD>Code</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Identifier</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Sequence Number</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Payload Data</TD>
<TD>0x0000000000000000</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

<BLOCKQUOTE>
Packet #7: CREATE_CHILD_SA request
<TABLE BORDER>
<TR>
<TD>IPv6 Header</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>UDP Header</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>IKEv2 Header</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>E Payload</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>IDi Payload</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>AUTH Payload</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>N Payload</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD>SA Payload</TD>
<TD colspan="2">Same as the Common Packet #4</TD>
</TR>
<TR>
<TD rowspan="2">TSi Payload</TD>
<TD colspan="2">Other fields are same as the Common Packet #4</TD>
</TR>
<TR>
<TD>Traffic Selectors</TD>
<TD>See below</TD>
</TR>
<TR>
<TD rowspan="2">TSr Payload</TD>
<TD colspan="2">Other fields are same as the Common Packet #4</TD>
</TR>
<TR>
<TD>Traffic Selectors</TD>
<TD>See below</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD rowspan="7">TSi Payload</TD>
<TD rowspan="7">Traffic Selector</TD>
<TD>TS Type</TD>
<TD>8 (IPV6_ADDR_RANGE)</TD>
</TR>
<TR>
<TD>IP Protocol ID</TD>
<TD>0 (any)</TD>
</TR>
<TR>
<TD>Selector Length</TD>
<TD>40</TD>
</TR>
<TR>
<TD>Start Port</TD>
<TD>0</TD>
</TR>
<TR>
<TD>End Port</TD>
<TD>65535</TD>
</TR>
<TR>
<TD>Starting Address</TD>
<TD>TH3's Global Address on Link B</TD>
</TR>
<TR>
<TD>Ending Address</TD>
<TD>TH3's Global Address on Link B</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD rowspan="7">TSr Payload</TD>
<TD rowspan="7">Traffic Selector</TD>
<TD>TS Type</TD>
<TD>8 (IPV6_ADDR_RANGE)</TD>
</TR>
<TR>
<TD>IP Protocol ID</TD>
<TD>0 (any)</TD>
</TR>
<TR>
<TD>Selector Length</TD>
<TD>40</TD>
</TR>
<TR>
<TD>Start Port</TD>
<TD>0</TD>
</TR>
<TR>
<TD>End Port</TD>
<TD>65535</TD>
</TR>
<TR>
<TD>Starting Address</TD>
<TD>TH1's Global Address on Link Y</TD>
</TR>
<TR>
<TD>Ending Address</TD>
<TD>TH1's Global Address on Link Y</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

<BLOCKQUOTE>
Packet #10: Echo Request
<TABLE BORDER>
<TR>
<TD rowspan="2">IPv6 Header</TD>
<TD>Source Address</TD>
<TD>TN1's Global Address on Link X</TD>
</TR>
<TR>
<TD>Destination Address</TD>
<TD>NUT's Global Address on Link A</TD>
</TR>
<TR>
<TD rowspan="7">ESP</TD>
<TD>Security Parameter Index</TD>
<TD>CHILD_SA's SPI value used by this message</TD>
</TR>
<TR>
<TD>Sequence Number</TD>
<TD>The value incremented the previous encrypted packet's Sequence Number by one.</TD>
</TR>
<TR>
<TD>Payload Data</TD>
<TD>Subsequent data encrypted by underlying encryption algorithm</TD>
</TR>
<TR>
<TD>Padding</TD>
<TD>Any value which to be a multiple of the encryption block size</TD>
</TR>
<TR>
<TD>Pad Length</TD>
<TD>The length of the Padding field</TD>
</TR>
<TR>
<TD>Next Header</TD>
<TD>41 (IPv6)</TD>
</TR>
<TR>
<TD>Integrity Check Value</TD>
<TD>The checksum must be valid by calculation according to the manner described in RFC.</TD>
</TR>
<TR>
<TD rowspan="2">IPv6 Header</TD>
<TD>Source Port</TD>
<TD>TH3's Global Address</TD>
</TR>
<TR>
<TD>Destination Port</TD>
<TD>TH1's Global Address</TD>
</TR>
<TR>
<TD rowspan="5">ICMPv6 Header</TD>
<TD>Type</TD>
<TD>128</TD>
</TR>
<TR>
<TD>Code</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Identifier</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Sequence Number</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Payload Data</TD>
<TD>0x0000000000000000</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

<BLOCKQUOTE>
Packet #11: Echo Reply
<TABLE BORDER>
<TR>
<TD rowspan="2">IPv6 Header</TD>
<TD>Source Address</TD>
<TD>TH1's Global Address</TD>
</TR>
<TR>
<TD>Destination Address</TD>
<TD>TH3's Global Address</TD>
</TR>
<TR>
<TD rowspan="5">ICMPv6 Header</TD>
<TD>Type</TD>
<TD>129</TD>
</TR>
<TR>
<TD>Code</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Identifier</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Sequence Number</TD>
<TD>any</TD>
</TR>
<TR>
<TD>Payload Data</TD>
<TD>0x0000000000000000</TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

=end html

  Part A: (ADVANCED)
     1. TN1 starts to negotiate with NUT by sending IKE_SA_INIT request.
     2. Observe the messages transmitted on Link B.
     3. After reception of IKE_SA_INIT response from the NUT, TN1 transmits an IKE_AUTH
        request to the NUT.
     4. Observe the messages transmitted on Link B.
     5. TH2 transmits an Echo Request packet to TH1.
     6. Observe the messages transmitted on Link A.
     7. TH1 transmits an Echo Reply packet to TH2.
     8. Observe the messages transmitted on Link B.
     9. TH3 transmits an Echo Request packet to TH1.
    10. Observe the messages transmitted on Link A.
    11. TH1 transmits an Echo Request packet to TH3.
    12. Observe the messages transmitted on Link B.
    13. TN1 starts to negotiate new CHILD_SA with the NUT by sending CREATE_CHILD_SA
        request.
    14. Observe the messages transmitted on Link B.
    15. TH2 transmits an Echo Request packet to TH1.
    16. Observe the messages transmitted on Link A.
    17. TH1 transmits an Echo Reply packet to TH2.
    18. Observe the messages transmitted on Link B.
    19. TH3 transmits an Echo Request packet to TH1.
    20. Observe the messages transmitted on Link A.
    21. TH1 transmits an Echo Reply packet to TH3.
    22. Observe the messages transmitted on Link B.

=head1 Observable Result

  Part A
       Step 2: Judgment #1
       The NUT transmits an IKE_SA_INIT response including "ENCR_3DES",
       "PRF_HMAC_SHA1", "AUTH_HMAC_SHA1_96" and "D-H group 2" as accepted
       algorithms.

       Step 4: Judgment #2
       The NUT transmits an IKE_AUTH response including "ENCR_3DES",
       "AUTH_HMAC_SHA1_96" and "No Extended Sequence Numbers" as accepted algorithms.

       Step 6: Judgment #3
       The NUT forwards an Echo Request.

       Step 8: Judgment #4
       The NUT forwards an Echo Reply with IPsec ESP using the first negotiated algorithms.

       Step 10: Judgment #5
       The NUT never forwards an Echo Request.

       Step 12: Judgment #6
       The NUT never forwards an Echo Reequest with IPsec ESP using the first negotiated
       algorithms.

       Step 14: Judgment #7
       The NUT transmits a CREATE_CHILD_SA response including "ENCR_3DES",
       "AUTH_HMAC_SHA1_96" and "No Extended Sequence Numbers" as proposed algorithms.

       Step 16: Judgment #8
       The NUT forwards an Echo Request.

       Step 18: Judgment #9
       The NUT forwards an Echo Reply with IPsec ESP using the first negotiated algorithms.

       Step 20: Judgment #10
       The NUT forwards an Echo Request.

       Step 22: Judgment #11
       The NUT forwards an Echo Reply with IPsec ESP using the second negotiated algorithms.

=head1 Possible Problems

  * None.

=cut


