]> git.proxmox.com Git - pve-manager.git/commitdiff
fix #5255: node: wol: add optional bind interface
authorChristian Ebner <c.ebner@proxmox.com>
Tue, 26 Mar 2024 09:16:57 +0000 (10:16 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 28 Mar 2024 16:27:47 +0000 (17:27 +0100)
Allows to optionally configure a local interface name to which to
bind to when sending a wake on lan packet to wake a remote node.

Default behaviour remains to send the packet via the interface for
the default gateway.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
PVE/API2/Nodes.pm
PVE/NodeConfig.pm

index 6e75cd5f33dc41331558b54d642905190aee92e0..9ffe7494530ea96f1cfee5037e04cfbb3c8e0f9f 100644 (file)
@@ -683,9 +683,10 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $node = $param->{node};
+       my $local_node = PVE::INotify::nodename();
 
        die "'$node' is local node, cannot wake my self!\n"
-           if $node eq 'localhost' || $node eq PVE::INotify::nodename();
+           if $node eq 'localhost' || $node eq $local_node;
 
        PVE::Cluster::check_node_exists($node);
 
@@ -696,6 +697,10 @@ __PACKAGE__->register_method({
            die "No wake on LAN MAC address defined for '$node'!\n";
        }
 
+       my $local_config = PVE::NodeConfig::load_config($local_node);
+       my $local_wol_config = PVE::NodeConfig::get_wakeonlan_config($local_config);
+       my $bind_iface = $local_wol_config->{'bind-interface'};
+
        $mac_addr =~ s/://g;
        my $packet = chr(0xff) x 6 . pack('H*', $mac_addr) x 16;
 
@@ -708,6 +713,13 @@ __PACKAGE__->register_method({
        setsockopt($sock, Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
            || die "Unable to set socket option: $!\n";
 
+       if (defined($bind_iface)) {
+           # Null terminated interface name
+           my $bind_iface_raw = pack('Z*', $bind_iface);
+           setsockopt($sock, Socket::SOL_SOCKET, Socket::SO_BINDTODEVICE, $bind_iface_raw)
+               || die "Unable to bind socket to interface '$bind_iface': $!\n";
+       }
+
        send($sock, $packet, 0, $to)
            || die "Unable to send packet: $!\n";
 
index a09c9be103a4a378351df80eafb5431069a07178..ee316296cef3f639a0cb94c4830b8a65b17ca5f3 100644 (file)
@@ -103,6 +103,13 @@ my $wakeonlan_desc = {
        format_description => 'MAC address',
        default_key => 1,
     },
+    'bind-interface' => {
+       type => 'string',
+       description => 'Bind to this interface when sending wake on LAN packet',
+       format => 'pve-iface',
+       format_description => 'bind interface',
+       optional => 1,
+    },
 };
 
 $confdesc->{wakeonlan} = {