summaryrefslogtreecommitdiff
path: root/scripts/vyos-update-nhrp.pl
blob: c51aac0ec3112b98ef265cb042df8f44ddf9885b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/perl
#
# Module: vyos-update-nhrp.pl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyos maintainers.
# All Rights Reserved.
#
# Author: Kim Hagen
# Date: August 2014
# Description: Script to configure nhrp
#
# **** End License ****
#

use Getopt::Long;
use POSIX;
use File::Basename;
use File::Compare;
use NetAddr::IP;

use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
use Vyatta::Interface;

use strict;
use warnings;
my ($checkref, $set_nhrp, $set_ipsec, $get_esp_gr_names, $get_ike_gr_names, $set_iptables, $del_iptables, $tun);
my $conffile = '/etc/opennhrp/opennhrp.conf';
my $ipsecfile = '/etc/opennhrp/opennhrp.ipsec';

sub usage {
	print <<EOF;
Usage:
	$0 --set_nhrp --set_ipsec --get_esp_gr_names --get_ike_gr_names
EOF
	exit 1;
}

sub checkref {
	my $config_nhrp_tun = new Vyatta::Config;
   
	$config_nhrp_tun->setLevel("protocols nhrp tunnel");
	my @nhrp_tunnels = $config_nhrp_tun->listNodes();

	if (grep {$_ eq $tun} @nhrp_tunnels) {
		print ("WARNING: Can't delete tunnel $tun, it is in use by NHRP config.\n");
		exit 1;
	}
}

sub get_esp_groups {
	my $group_names = "";
	my $esp_groups = new Vyatta::Config;

	$esp_groups->setLevel("vpn ipsec esp-group");

	my @esp_groups = $esp_groups->listNodes();

	foreach my $group(@esp_groups) {
		$group_names = $group_names . " " . $group;
	}
	return $group_names;
}

sub get_ike_groups {
	my $group_names = "";
	my $ike_groups = new Vyatta::Config;

	$ike_groups->setLevel("vpn ipsec ike-group");

	my @ike_groups = $ike_groups->listNodes();

	foreach my $group(@ike_groups) {
		$group_names = $group_names . " " . $group;
	}
	return $group_names;
}

sub configure_nhrp_tunnels {
	my $cmd;
	my $config_tun = new Vyatta::Config;
	my $config_nhrp_tun = new Vyatta::Config;

	$config_tun->setLevel("interfaces tunnel");
	$config_nhrp_tun->setLevel("protocols nhrp tunnel");

	my @tunnels = $config_tun->listNodes();
	my @nhrp_tunnels = $config_nhrp_tun->listNodes();
	my $notun = 0;

	open (my $fh,">$conffile") or die("Can't open $conffile: $!\n");
	print $fh "";
	close $fh;

	if (@nhrp_tunnels) {
		foreach my $nhrp_tunnel(@nhrp_tunnels) {
			if (grep {$_ eq $nhrp_tunnel} @tunnels) {
				if ($config_tun->returnValue("$nhrp_tunnel encapsulation") eq "gre" && ! $config_tun->exists("$nhrp_tunnel remote")) {
					my @conf_file = tunnel_config($nhrp_tunnel);
					open (my $fh,">>$conffile");
					foreach (@conf_file) {
		       				print $fh "$_";
	       				}
					close $fh;
					$notun = 1;
				}
				else {
					print ("$nhrp_tunnel is not 'mGRE' tunnel'\n");
					exit 1;
				}
			}
			else {
				print ("Tunnel $nhrp_tunnel does not exist.\n");
				exit 1;
			}
		}
	}
	if ("$notun" eq "1" ) {
		$cmd = "/etc/init.d/opennhrp.init restart";
		system ($cmd);
	}
	else {
		$cmd = "/etc/init.d/opennhrp.init stop";
		system ($cmd);
	}
}

sub configure_nhrp_ipsec {
	my $cmd;
	my $config_nhrp_tun = new Vyatta::Config;
	my $config_ipsec = new Vyatta::Config;

	$config_nhrp_tun->setLevel("protocols nhrp tunnel");
	$config_ipsec->setLevel("vpn ipsec profile");

	my @nhrp_tunnels = $config_nhrp_tun->listNodes();
	my @ipsec_profiles = $config_ipsec->listNodes();
	my $isipsec = 0;
	
	open(my $fh,">$ipsecfile") or die("Can't open $ipsecfile: $!\n");
	print $fh "";
	close $fh;

	if (@ipsec_profiles) {
		foreach my $ipsec_profile(@ipsec_profiles) {
			my $config_ipsec_profile_tun = new Vyatta::Config;

			$config_ipsec_profile_tun->setLevel("vpn ipsec profile $ipsec_profile bind tunnel");

			my @ipsec_profile_tunnels = $config_ipsec_profile_tun->listNodes();

			foreach my $ipsec_profile_tunnel(@ipsec_profile_tunnels) {
				if (grep {$_ eq $ipsec_profile_tunnel} @nhrp_tunnels) {
					my @conf_file = ipsec_config($ipsec_profile, $ipsec_profile_tunnel);
					open (my $fh,">>$ipsecfile");
					foreach (@conf_file) {
						print $fh "$_";
					}
					close $fh;
					$isipsec = 1;
				}
			}
		}
	}
	if ("$isipsec" eq "1" ) {
		$cmd = "/etc/init.d/opennhrp.init restart";
		system ($cmd);
	}
}

sub tunnel_config {
	my $tunnel_Config = new Vyatta::Config;
	my $tunnel_ID = $_[0];

	$tunnel_Config->setLevel("protocols nhrp tunnel");

	my @conf_file = ();
	my $type = "#hub";

	push(@conf_file, "interface $tunnel_ID $type\n");
	if ( $tunnel_Config->exists("$tunnel_ID map")) {
		$type = "#spoke";
		my @maps = $tunnel_Config->listNodes("$tunnel_ID map");
		shift(@conf_file);
		unshift(@conf_file, "interface $tunnel_ID $type\n");
		foreach my $map (@maps) {
			push(@conf_file, " map", " $map");
			push(@conf_file, " ", $tunnel_Config->returnValue("$tunnel_ID map $map nbma-address"));

			if ($tunnel_Config->exists("$tunnel_ID map $map register")) {
				push(@conf_file, " register");
			}
			if ($tunnel_Config->exists("$tunnel_ID map $map cisco")) {
				push(@conf_file, " cisco");
			}
			push(@conf_file, "\n");
		}
	}
	if ( $tunnel_Config->exists("$tunnel_ID dynamic-map")) {
		$type = "#spoke";
		my @dynmaps = $tunnel_Config->listNodes("$tunnel_ID dynamic-map");
		shift(@conf_file);
		unshift(@conf_file, "interface $tunnel_ID $type\n");
		foreach my $dynmap (@dynmaps) {
			push(@conf_file, " dynamic-map", " $dynmap");
			push(@conf_file, " ", $tunnel_Config->returnValue("$tunnel_ID dynamic-map $dynmap nbma-domain-name"));
			push(@conf_file, "\n");
		}
	}
	if ( $tunnel_Config->exists("$tunnel_ID shortcut-target")) {
		my @starget = $tunnel_Config->listNodes("$tunnel_ID shortcut-target");
		my $starget = $starget[0];
		push(@conf_file, " shortcut-target", " $starget");
		push(@conf_file, " ", $tunnel_Config->returnValue("$tunnel_ID shortcut-target $starget holding-time"));
		shift(@conf_file);
		unshift(@conf_file, "interface $tunnel_ID $type\n");
		push(@conf_file, "\n");
	}
	if ($tunnel_Config->returnValue("$tunnel_ID cisco-authentication") ne "") {
		push(@conf_file, " cisco-authentication ", $tunnel_Config->returnValue("$tunnel_ID cisco-authentication") , "\n"); 
	}
	if ( $tunnel_Config->exists("$tunnel_ID holding-time") && ($tunnel_Config->returnValue("$tunnel_ID holding-time") ne "")) {
		push(@conf_file, " holding-time", " ", $tunnel_Config->returnValue("$tunnel_ID holding-time") , "\n");
	}
	if ($tunnel_Config->exists("$tunnel_ID shortcut")) {
		push(@conf_file, " shortcut\n");
	}
	if ($tunnel_Config->exists("$tunnel_ID non-caching")) {
		push(@conf_file, " non-caching\n");
	}
	if ($tunnel_Config->exists("$tunnel_ID multicast")) {
		push(@conf_file, " multicast ", $tunnel_Config->returnValue("$tunnel_ID multicast") ,"\n");
	}	
	if ($tunnel_Config->exists("$tunnel_ID redirect")) {
		push(@conf_file, " redirect\n");
	}
	if ($tunnel_Config->exists("$tunnel_ID shortcut-destination")) {
		push(@conf_file, " shortcut-destination\n");
	}
	push(@conf_file, "\n");

	return @conf_file;
}

sub ipsec_config {
	my $new_rule = "";
	my ($ipsec_profile, $ipsec_tun) = @_;
	my $config_prot = new Vyatta::Config;
	my $config_ipsec = new Vyatta::Config;

	$config_ipsec->setLevel("vpn ipsec profile $ipsec_profile");

	my $config_tun = new Vyatta::Config;	

	$config_tun->setLevel("interfaces tunnel $ipsec_tun");

	my @conf_file = ();
	my ($esp_group, $ike_group, @tun_ip) = undef;

	$esp_group = $config_ipsec->returnValue("esp-group");
	$ike_group = $config_ipsec->returnValue("ike-group");	
	@tun_ip = $config_tun->returnValues('address');
	
	if (@tun_ip) {
		for my $ip (@tun_ip) {
			push(@conf_file, "interface $ipsec_tun\n");
			push(@conf_file, "$ip\n");
			if ($esp_group) {
				$config_prot->setLevel("vpn ipsec esp-group $esp_group");

				my @proposals = $config_prot->listNodes("proposal");
				my $x = 0;
				foreach my $e (@proposals) {
					$x ++;
				}
				my $y = 0;
				push(@conf_file, " --esp", " ");
				foreach my $proposal (@proposals) {
					if ($y != 0 && $y <= $x ) {
						push(@conf_file, ",");
					}
					if ($config_prot->exists("proposal $proposal encryption")) {
						push(@conf_file, $config_prot->returnValue("proposal $proposal encryption"));
					}
					else {
						push(@conf_file, "aes128");
					}
					if ($config_prot->exists("proposal $proposal hash")) {
						push(@conf_file, "-", $config_prot->returnValue("proposal $proposal hash"));
					}
					else {
						push(@conf_file, "-sha1");
					}
					if ($config_prot->exists("pfs")) {
						my $pfs = $config_prot->returnValue("pfs");
						if ($pfs eq 'dh-group2') {
							push(@conf_file, "-modp1024");
						}
						elsif ($pfs eq 'dh-group5') {
							push(@conf_file, "-modp1536");
						}
						elsif ($pfs eq 'dh-group14') {
							push(@conf_file, "-modp2048");
						}
						elsif ($pfs eq 'dh-group15') {
							push(@conf_file, "-modp3072");
						}
						elsif ($pfs eq 'dh-group16') {
							push(@conf_file, "-modp4096");
						}
						elsif ($pfs eq 'dh-group17') {
							push(@conf_file, "-modp6144");
						}
						elsif ($pfs eq 'dh-group18') {
							push(@conf_file, "-modp8192");
						}
						elsif ($pfs eq 'dh-group19') {
							push(@conf_file, "-ecp256");
						}
						elsif ($pfs eq 'dh-group20') {
							push(@conf_file, "-ecp384");
						}
						elsif ($pfs eq 'dh-group21') {
							push(@conf_file, "-ecp521");
						}
						elsif ($pfs eq 'dh-group22') {
							push(@conf_file, "-modp1024s160");
						}
						elsif ($pfs eq 'dh-group23') {
							push(@conf_file, "-modp2048s224");
						}
						elsif ($pfs eq 'dh-group24') {
							push(@conf_file, "-modp2048s256");
						}
						elsif ($pfs eq 'dh-group25') {
							push(@conf_file, "-ecp192");
						}
						elsif ($pfs eq 'dh-group26') {
							push(@conf_file, "-ecp224");
						}
					}
					++$y;
				}
				push(@conf_file, "\n");
			}
		
			if ($ike_group) {
				$config_prot->setLevel("vpn ipsec ike-group $ike_group");
				my @proposals = $config_prot->listNodes("proposal");
				my $x = 0;
				foreach my $e (@proposals) {
					$x ++;
				}
				my $y = 0;
				push(@conf_file, " --ike", " ");
				foreach my $proposal (@proposals) {
					if ($y != 0 && $y <= $x ) {
						push(@conf_file, ",");
					}
					if ($config_prot->exists("proposal $proposal encryption")) {
						push(@conf_file, $config_prot->returnValue("proposal $proposal encryption"));
					}
					else {
						push(@conf_file, "aes128");
					}
					if ($config_prot->exists("proposal $proposal hash")) {
						push(@conf_file, "-", $config_prot->returnValue("proposal $proposal hash"));
					}
					else {
						push(@conf_file, "-sha1");
					}
					if ($config_prot->exists("proposal $proposal dh-group")) {
						my $pfs = $config_prot->returnValue("proposal $proposal dh-group");
						if ($pfs eq '2') {
							push(@conf_file, "-modp1024");
						}
						elsif ($pfs eq '5') {
							push(@conf_file, "-modp1536");
						}
						elsif ($pfs eq '14') {
							push(@conf_file, "-modp2048");
						}
						elsif ($pfs eq '15') {
							push(@conf_file, "-modp3072");
						}
						elsif ($pfs eq '16') {
							push(@conf_file, "-modp4096");
						}
						elsif ($pfs eq '17') {
							push(@conf_file, "-modp6144");
						}
						elsif ($pfs eq '18') {
							push(@conf_file, "-modp8192");
						}
						elsif ($pfs eq '19') {
							push(@conf_file, "-ecp256");
						}
						elsif ($pfs eq '20') {
							push(@conf_file, "-ecp384");
						}
						elsif ($pfs eq '21') {
							push(@conf_file, "-ecp521");
						}
						elsif ($pfs eq '22') {
							push(@conf_file, "-modp1024s160");
						}
						elsif ($pfs eq '23') {
							push(@conf_file, "-modp2048s224");
						}
						elsif ($pfs eq '24') {
							push(@conf_file, "-modp2048s256");
						}
						elsif ($pfs eq '25') {
							push(@conf_file, "-ecp192");
						}
						elsif ($pfs eq '26') {
							push(@conf_file, "-ecp224");
						}
					}
					++$y;
				}
				if ($config_prot->exists("dead-peer-detection action")) {
					push(@conf_file, " ");
					push(@conf_file, "--dpdaction");
					push(@conf_file, " ");
					push(@conf_file, $config_prot->returnValue("dead-peer-detection action"));
					push(@conf_file, " ");
					push(@conf_file, "--dpdtimeout");
					push(@conf_file, " ");
					push(@conf_file, $config_prot->returnValue("dead-peer-detection timeout"));
					push(@conf_file, " ");
					push(@conf_file, "--dpddelay");
					push(@conf_file, " ");
					push(@conf_file, $config_prot->returnValue("dead-peer-detection interval"));
				}
				push(@conf_file, "\n");
			}
		}
	}
	push(@conf_file, "\n");

	return @conf_file;
}

sub create_nhrp_iptables {
	my $config_tun = new Vyatta::Config;
	
	$config_tun->setLevel("interfaces tunnel");
	
	if ( $config_tun->exists("$tun source-address")) {
		if ( $config_tun->exists("$tun remote")) {
			print ("$tun is not 'mGRE' tunnel'\n");
			exit 1;
		}
		my $local_ip = $config_tun->returnValue("$tun source-address");

		system ("sudo iptables -N VYOS_NHRP_${tun}_OUT_HOOK") == 0 or die "System call failed: $!";
		system ("sudo iptables -A VYOS_NHRP_${tun}_OUT_HOOK -p gre -s ${local_ip} -d 224.0.0.0/4 -j DROP") == 0 or die "System call failed: $!";
		system ("sudo iptables -A VYOS_NHRP_${tun}_OUT_HOOK -j RETURN") == 0 or die "System call failed: $!";
		system ("sudo iptables -I OUTPUT 2 -j VYOS_NHRP_${tun}_OUT_HOOK") == 0 or die "System call failed: $!";
	}
}

sub delete_nhrp_iptables {
	system ("sudo iptables -D OUTPUT -j VYOS_NHRP_${tun}_OUT_HOOK") == 0 or die "System call failed: $!";
	system ("sudo iptables -D VYOS_NHRP_${tun}_OUT_HOOK 1") == 0 or die "System call failed: $!";
	system ("sudo iptables -D VYOS_NHRP_${tun}_OUT_HOOK 1") == 0 or die "System call failed: $!";
	system ("sudo iptables -X VYOS_NHRP_${tun}_OUT_HOOK") == 0 or die "System call failed: $!";
}

#
# main
#

GetOptions (
	"checkref"			=> \$checkref,
	"set_ipsec"			=> \$set_ipsec,
	"set_nhrp"			=> \$set_nhrp,
	"get_esp_gr_names"	=> \$get_esp_gr_names,
	"get_ike_gr_names"	=> \$get_ike_gr_names,
	"set_iptables"		=> \$set_iptables,
	"del_iptables"		=> \$del_iptables,
	"tun=s"     		=> \$tun
) or usage ();

checkref() if $checkref;
print get_esp_groups() if $get_esp_gr_names;
print get_ike_groups() if $get_ike_gr_names;
configure_nhrp_ipsec() if $set_ipsec;
configure_nhrp_tunnels() if $set_nhrp;
create_nhrp_iptables() if $set_iptables;
delete_nhrp_iptables() if $del_iptables;

# end of file