diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/vyatta-address | 45 | ||||
-rwxr-xr-x | scripts/vyatta-dhcpv6-client.pl | 98 | ||||
-rwxr-xr-x | scripts/vyatta-interfaces.pl | 17 |
3 files changed, 106 insertions, 54 deletions
diff --git a/scripts/vyatta-address b/scripts/vyatta-address new file mode 100644 index 00000000..2e521c26 --- /dev/null +++ b/scripts/vyatta-address @@ -0,0 +1,45 @@ +#! /bin/bash +# +# Wrapper around ip link command that handles IPv4, IPv6 and DHCP +# This is done in shell rather than perl to avoid the overhead of recompilation + +if [ $# -ne 3 ]; then + echo "Usage: $0 {add|delete} interface address" + exit 1 +fi + +case $1 in + add) + if [[ "$3" = "dhcp" ]] + then + exec /opt/vyatta/sbin/vyatta-interfaces.pl --dev="$2" --dhcp=start + elif [[ "$3" = "dhcpv6" ]] + then + exec /opt/vyatta/sbin/vyatta-dhcpv6-client.pl --start -ifname "$2" + elif [[ "$3" =~ ":" ]] + then # Ipv6 address + exec ip -6 addr add "$3" dev "$2" + else + exec ip addr add "$3" broadcast + dev "$2" + fi ;; + + delete) + if [ ! -d "/sys/class/net/$2" ] + then # device is already gone + exit 0 + elif [[ "$3" = "dhcp" ]] + then + exec /opt/vyatta/sbin/vyatta-interfaces.pl --dev="$2" --dhcp=stop + elif [[ "$3" = "dhcpv6" ]] + then + exec /opt/vyatta/sbin/vyatta-dhcpv6-client.pl --stop --ifname $ifname + elif [[ "$3" =~ ":" ]] + then + exec ip -6 addr del "$3" dev "$2" + else + exec ip addr del "$3" dev "$2" + fi ;; + *) + echo "Unknown option $1" + exit 1 ;; +esac diff --git a/scripts/vyatta-dhcpv6-client.pl b/scripts/vyatta-dhcpv6-client.pl index 3f01a677..283b6d2b 100755 --- a/scripts/vyatta-dhcpv6-client.pl +++ b/scripts/vyatta-dhcpv6-client.pl @@ -26,7 +26,7 @@ use strict; use lib "/opt/vyatta/share/perl5/"; -use FileHandle; +use Sys::hostname; use Vyatta::Config; use Getopt::Long; @@ -34,26 +34,17 @@ my $start_flag; # Start the daemon my $stop_flag; # Stop the daemon and delete all config files my $release_flag; # Stop the daemon, but leave config file my $renew_flag; # Re-start the daemon. Functionally same as start_flag -my $temp_flag; -my $params_only_flag; my $ifname; - sub gen_conf_file { my ($conffile, $ifname) = @_; + my $FD_WR; - my $FD_WR = new FileHandle; + open($FD_WR, '>', $conffile) + or die "Can't write config file: $conffile : $!\n"; - if (!open($FD_WR, ">$conffile")) { - printf("Can't write config file: $conffile\n"); - exit 1; - } - my $date = `date`; - my $user = `id -un`; - my $hostname = `hostname`; - chomp($date); - chomp($user); - chomp($hostname); + my $date = localtime; + my $user = getpwuid($<); print $FD_WR "# This file was auto-generated by the Vyatta\n"; print $FD_WR "# configuration sub-system. Do not edit it.\n"; @@ -61,11 +52,37 @@ sub gen_conf_file { print $FD_WR "# Generated on $date by $user\n"; print $FD_WR "#\n"; print $FD_WR "interface \"$ifname\" {\n"; +# my $hostname = hostname; # print $FD_WR " send host-name \"$hostname\";\n"; # print $FD_WR " send dhcp6.oro 1, 2, 7, 12, 13, 23, 24, 39;\n"; print $FD_WR "}\n"; + close $FD_WR; } +sub usage { + print "Usage: $0 --ifname=ethX --{start|stop|renew|release}\n"; + exit 1; +} + +sub dhcpv6_options { + my $ifname = shift; + my $intf = new Vyatta::Interface($name); + + die "Unknown interface type for $ifname" unless $intf; + + my $config = new Vyatta::Config; + $config->setLevel($intf->path()); + + my $args = ""; + + $args .= " -T" + if ($config->exists("dhcpv6-options/temporary")); + + $args .= " -S" + if ($config->exists("dhcpv6-options/parameters-only")); + + return $args; +} # # Main Section @@ -75,46 +92,31 @@ GetOptions("start" => \$start_flag, "stop" => \$stop_flag, "release" => \$release_flag, "renew" => \$renew_flag, - "temporary" => \$temp_flag, - "parameters-only" => \$params_only_flag, "ifname=s" => \$ifname, - ); + ) or usage(); -if ((defined $temp_flag) && (defined $params_only_flag)) { - printf("Error: --temporary and --parameters-only flags are mutually exclusive.\n"); - exit 1; -} - -if (!defined $ifname) { - printf("Error: Interface name must be specified with --ifname parameter.\n"); - exit 1; -} +die "Error: Interface name must be specified with --ifname parameter.\n" + unless $ifname; my $pidfile = "/var/lib/dhcp3/dhclient_v6_$ifname.pid"; my $leasefile = "/var/lib/dhcp3/dhclient_v6_$ifname.leases"; my $conffile = "/var/lib/dhcp3/dhclient_v6_$ifname.conf"; my $cmdname = "/sbin/dhclient"; -if (defined $release_flag) { - if (! -e $conffile) { - printf("DHCPv6 client is not configured on interface $ifname.\n"); - exit 1; - } +if ($release_flag) { + die "DHCPv6 client is not configured on interface $ifname.\n" + unless (-e $conffile); - if (! -e $pidfile) { - printf("DHCPv6 client is already released on interface $ifname.\n"); - exit 1; - } + die "DHCPv6 client is already released on interface $ifname.\n" + unless (-e $pidfile); } -if (defined $renew_flag) { - if (! -e $conffile) { - printf("DHCPv6 client is not configured on interface $ifname.\n"); - exit 1; - } +if ($renew_flag) { + die "DHCPv6 client is not configured on interface $ifname.\n" + unless (-e $conffile); } -if (defined $stop_flag || defined $release_flag) { +if (defined($stop_flag)|| defined ($release_flag)) { # Stop dhclient -6 on $ifname printf("Stopping daemon...\n"); @@ -131,7 +133,8 @@ if (defined $stop_flag || defined $release_flag) { } } -if (defined $start_flag || defined $renew_flag) { +if (defined($start_flag) || defined ($renew_flag)) { + # Generate the DHCP client config file... gen_conf_file($conffile, $ifname); @@ -142,14 +145,7 @@ if (defined $start_flag || defined $renew_flag) { printf($output); # start "dhclient -6" on $ifname - - my $args = ""; - if (defined $temp_flag) { - $args .= " -T"; - } - if (defined $params_only_flag) { - $args .= " -S"; - } + my $args = dhcpv6_options($ifname); printf("Starting new daemon...\n"); my $output=`$cmdname -6 -nw -cf $conffile -pf $pidfile -lf $leasefile $args $ifname`; diff --git a/scripts/vyatta-interfaces.pl b/scripts/vyatta-interfaces.pl index d625d262..a7384eea 100755 --- a/scripts/vyatta-interfaces.pl +++ b/scripts/vyatta-interfaces.pl @@ -486,10 +486,10 @@ sub dhcp { my ($request, $intf) = @_; die "$intf is not using DHCP to get an IP address\n" - unless is_dhcp_enabled($intf); + unless ($request eq 'start' || is_dhcp_enabled($intf)); - die "$intf is disabled. Unable to release/renew lease\n" - if is_intf_down($intf); + die "$intf is disabled.\n" + if ($request ne 'stop' && is_intf_down($intf)); my $tmp_dhclient_dir = '/var/run/vyatta/dhclient/'; my $release_file = $tmp_dhclient_dir . 'dhclient_release_' . $intf; @@ -505,6 +505,17 @@ sub dhcp { print "Renewing DHCP lease on $intf ...\n"; run_dhclient($intf); unlink ($release_file); + } elsif ($request eq "start") { + print "Starting DHCP client on $intf ...\n"; + touch("/var/lib/dhcp3/$intf"); + run_dhclient($intf); + } elsif ($request eq "stop") { + print "Stopping DHCP client on $intf ...\n"; + stop_dhclient($intf); + unlink("/var/lib/dhcp3/dhclient_$intf\_lease"); + unlink("/var/lib/dhcp3/$intf"); + unlink("/var/run/vyatta/dhclient/dhclient_release_$intf"); + unlink("/var/lib/dhcp3/dhclient_$intf\.conf"); } else { die "Unknown DHCP request: $request\n"; } |