public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 pve-manager 2/4] fix #5255: node: wol: add optional bind interface
Date: Tue, 26 Mar 2024 10:16:57 +0100	[thread overview]
Message-ID: <20240326091659.63483-3-c.ebner@proxmox.com> (raw)
In-Reply-To: <20240326091659.63483-1-c.ebner@proxmox.com>

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>
---
changes since version 1:
- Adapted to use property string

 PVE/API2/Nodes.pm | 14 +++++++++++++-
 PVE/NodeConfig.pm |  7 +++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index 6e75cd5f..9ffe7494 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -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";
 
diff --git a/PVE/NodeConfig.pm b/PVE/NodeConfig.pm
index a09c9be1..ee316296 100644
--- a/PVE/NodeConfig.pm
+++ b/PVE/NodeConfig.pm
@@ -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} = {
-- 
2.39.2





  parent reply	other threads:[~2024-03-26  9:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-26  9:16 [pve-devel] [PATCH v2 pve-manager pve-docs 0/4] add optional WoL config options Christian Ebner
2024-03-26  9:16 ` [pve-devel] [PATCH v2 pve-manager 1/4] node: config: make wakeonlan a property string Christian Ebner
2024-03-26  9:16 ` Christian Ebner [this message]
2024-03-26  9:16 ` [pve-devel] [PATCH v2 pve-manager 3/4] fix #5255: node: wol: configurable broadcast address Christian Ebner
2024-03-26  9:16 ` [pve-devel] [PATCH v2 pve-docs 4/4] pvenode/wake-on-lan: mention optional config options Christian Ebner
2024-03-28 17:27 ` [pve-devel] applied-series: [PATCH v2 pve-manager pve-docs 0/4] add optional WoL " Thomas Lamprecht
2024-03-29  7:44   ` Christian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240326091659.63483-3-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal