#!/usr/bin/perl

$HOSTS="11 12 13 21 22 23 31 32 33 41 43 51 52 53 61 62 63 71 72 73 81 82 83 91 92 93 101 102 103";

$X = 10;
$Y = 10;

$GW = "10.42.1.1";
$GW_NET = "42.1.1.0/24";

#system("/sbin/ip addr add $GW/8 dev eth0");

# Set policy database

system ("/sbin/ip xfrm policy flush");
system ("/sbin/ip xfrm state flush");

foreach $B (split(/ /, $HOSTS)) {
	for ($i = 1; $i <= $X; $i++) {
		for ($j = 1; $j <= $Y; $j++) {
			system "/sbin/ip xfrm policy add dir out src $GW_NET dst $B.$i.$j.0/24 proto icmp tmpl src $GW dst 10.$B.$i.$j proto esp mode tunnel level required";
			system "/sbin/ip xfrm policy add dir in dst $B.$i.$j.0/24 src $GW_NET proto icmp tmpl dst 10.$B.$i.$j src $GW proto esp mode tunnel level required";
		}
	}
}
