[pve-devel] [PATCH manager 1/3] fix #5255: node: wol: add optional bind interface

Christian Ebner c.ebner at proxmox.com
Tue Mar 5 13:54:29 CET 2024


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 at proxmox.com>
---
 PVE/API2/Nodes.pm | 13 ++++++++++++-
 PVE/NodeConfig.pm |  6 ++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index cc5ee65e..620dac1c 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -682,9 +682,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);
 
@@ -694,6 +695,9 @@ __PACKAGE__->register_method({
 	    die "No wake on LAN MAC address defined for '$node'!\n";
 	}
 
+	my $local_config = PVE::NodeConfig::load_config($local_node);
+	my $bind_iface = $local_config->{'wakeonlan-bind-interface'};
+
 	$mac_addr =~ s/://g;
 	my $packet = chr(0xff) x 6 . pack('H*', $mac_addr) x 16;
 
@@ -706,6 +710,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";
 
diff --git a/PVE/NodeConfig.pm b/PVE/NodeConfig.pm
index 941e6009..5450ab2f 100644
--- a/PVE/NodeConfig.pm
+++ b/PVE/NodeConfig.pm
@@ -91,6 +91,12 @@ my $confdesc = {
 	format => 'mac-addr',
 	optional => 1,
     },
+    'wakeonlan-bind-interface' => {
+	type => 'string',
+	description => 'Bind to this interface when sending wake on LAN packet',
+	format => 'pve-iface',
+	optional => 1,
+    },
     'startall-onboot-delay' => {
 	description => 'Initial delay in seconds, before starting all the Virtual Guests with on-boot enabled.',
 	type => 'integer',
-- 
2.39.2





More information about the pve-devel mailing list