summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorCarl Byington <carl@five-ten-sg.com>2015-05-04 15:54:41 -0700
committerDaniil Baturin <daniil@baturin.org>2015-06-13 19:01:46 +0200
commit0dcde1edee1b6c84feb7a4dc0b690909a7b5d498 (patch)
tree0d80ab1094a21be43955638f02f02a24e010fb5c /scripts
parent593881742424f3499f32cc75d86786e7f948b2a1 (diff)
downloadvyatta-cfg-system-0dcde1edee1b6c84feb7a4dc0b690909a7b5d498.tar.gz
vyatta-cfg-system-0dcde1edee1b6c84feb7a4dc0b690909a7b5d498.zip
allow dhcp interface for the local end of a tunnel
Signed-off-by: Daniil Baturin <daniil@baturin.org>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/vyatta-dhcp-helper.pl59
-rw-r--r--scripts/vyatta-tunnel-dhcp.pl25
-rw-r--r--scripts/vyatta-update-tunnel.pl27
3 files changed, 111 insertions, 0 deletions
diff --git a/scripts/vyatta-dhcp-helper.pl b/scripts/vyatta-dhcp-helper.pl
new file mode 100644
index 00000000..40291654
--- /dev/null
+++ b/scripts/vyatta-dhcp-helper.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+use Getopt::Long;
+use strict;
+use lib "/opt/vyatta/share/perl5";
+use Vyatta::Misc;
+
+my ($iface, $want);
+GetOptions("interface=s" => \$iface,
+ "want=s" => \$want);
+
+# Return the current router address from an interface that is
+# configured via dhcp. Return 127.0.0.1 for all errors.
+# This address will be used for the next hop address for static routes.
+
+sub get_dhcp_router {
+ my $dhcp_iface = pop(@_);
+ if (!Vyatta::Misc::is_dhcp_enabled($dhcp_iface,0)) {
+ return "127.0.0.1";
+ }
+ my $lease = "/var/lib/dhcp3/dhclient_${dhcp_iface}_lease";
+ my $router = `grep new_routers= $lease | cut -d"'" -f2`;
+ my @r = split(/,/, $router);
+ $router = $r[0];
+ if ($router eq "") {
+ return "127.0.0.1";
+ }
+ return $router;
+}
+
+
+# Return the current ipv4 address from an interface that is
+# configured via dhcp. Return 127.0.0.1 for all errors.
+# This address will be used for the local-ip for tunnels,
+
+sub get_dhcp_addr {
+ my $dhcp_iface = pop(@_);
+ if (!Vyatta::Misc::is_dhcp_enabled($dhcp_iface,0)) {
+ return "127.0.0.1";
+ }
+ my @dhcp_addr = Vyatta::Misc::getIP($dhcp_iface,4);
+ my $addr = pop(@dhcp_addr);
+ if (!defined($addr)) {
+ return "127.0.0.1";
+ }
+ @dhcp_addr = split(/\//, $addr);
+ $addr = $dhcp_addr[0];
+ return $addr;
+}
+
+
+if ($want eq 'local') {
+ print get_dhcp_addr($iface);
+}
+else {
+ print get_dhcp_router($iface);
+}
+exit 0;
+
diff --git a/scripts/vyatta-tunnel-dhcp.pl b/scripts/vyatta-tunnel-dhcp.pl
new file mode 100644
index 00000000..4283b30a
--- /dev/null
+++ b/scripts/vyatta-tunnel-dhcp.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use Getopt::Long;
+use strict;
+
+my ($iface, $dhcp, $tunnel, $nip, $oip, $reason);
+GetOptions("interface=s" => \$iface,
+ "dhcp=s" => \$dhcp,
+ "tunnel=s" => \$tunnel,
+ "new_ip=s" => \$nip,
+ "old_ip=s" => \$oip,
+ "reason=s" => \$reason);
+
+# check if an update is needed
+exit(0) if (($iface ne $dhcp) || ($oip eq $nip) || ($reason ne "BOUND"));
+logger("DHCP address on $iface updated to $nip from $oip: Updating tunnel $tunnel configuration.");
+system("sudo ip tunnel change $tunnel local $nip");
+
+sub logger {
+ my $msg = pop(@_);
+ my $FACILITY = "daemon";
+ my $LEVEL = "notice";
+ my $TAG = "tunnel-dhclient-hook";
+ my $LOGCMD = "logger -t $TAG -p $FACILITY.$LEVEL";
+ system("$LOGCMD $msg");
+}
diff --git a/scripts/vyatta-update-tunnel.pl b/scripts/vyatta-update-tunnel.pl
new file mode 100644
index 00000000..d4c652d0
--- /dev/null
+++ b/scripts/vyatta-update-tunnel.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+use Getopt::Long;
+use strict;
+use lib "/opt/vyatta/share/perl5";
+use Vyatta::Config;
+
+my ($iface, $tunnel, $option);
+GetOptions("interface=s" => \$iface,
+ "tunnel=s" => \$tunnel,
+ "option=s" => \$option
+ );
+my $FILE_DHCP_HOOK = "/etc/dhcp3/dhclient-exit-hooks.d/tunnel-$tunnel";
+my $dhcp_hook = '';
+if ($option eq 'create') {
+ $dhcp_hook =<<EOS;
+#!/bin/sh
+/opt/vyatta/bin/sudo-users/vyatta-tunnel-dhcp.pl --interface=\"\$interface\" --dhcp=\"$iface\" --tunnel=\"$tunnel\" --new_ip=\"\$new_ip_address\" --old_ip=\"\$old_ip_address\" --reason=\"\$reason\"
+EOS
+}
+
+open my $dhcp_hook_file, '>', $FILE_DHCP_HOOK
+ or die "cannot open $FILE_DHCP_HOOK";
+print ${dhcp_hook_file} $dhcp_hook;
+close $dhcp_hook_file;
+exit 0;
+