all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH pve-network 6/7] dnsmasq: fix ipv6 support
Date: Sat, 18 Nov 2023 15:13:13 +0100	[thread overview]
Message-ID: <20231118141314.2785997-7-aderumier@odiso.com> (raw)
In-Reply-To: <20231118141314.2785997-1-aderumier@odiso.com>

ether file should have 1 line by mac address with ip4+ip6

ip6 address should be in braced [ip6]

for now: don't update ip6 through bus as it seem to be incorrect

Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
---
 src/PVE/Network/SDN/Dhcp/Dnsmasq.pm | 64 +++++++++++++++++------------
 1 file changed, 37 insertions(+), 27 deletions(-)

diff --git a/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm b/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
index b469272..0dd6436 100644
--- a/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
+++ b/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
@@ -25,44 +25,54 @@ sub add_ip_mapping {
     my $ethers_file = "$DNSMASQ_CONFIG_ROOT/$dhcpid/ethers";
     my $ethers_tmp_file = "$ethers_file.tmp";
 
-    my $change = undef;
-    my $match4 = undef;
-    my $match6 = undef;
+    my $reload = undef;
 
     my $appendFn = sub {
 	open(my $in, '<', $ethers_file) or die "Could not open file '$ethers_file' $!\n";
 	open(my $out, '>', $ethers_tmp_file) or die "Could not open file '$ethers_tmp_file' $!\n";
 
-        while (my $line = <$in>) {
+	my $match = undef;
+
+ 	while (my $line = <$in>) {
 	    chomp($line);
-	    my ($parsed_mac, $parsed_ip) = split(/,/, $line);
-	    #delete removed mac
-	    if (!defined($macdb->{macs}->{$parsed_mac})) {
-		$change = 1;
-		next;
+	    my $parsed_ip4 = undef;
+	    my $parsed_ip6 = undef;
+	    my ($parsed_mac, $parsed_ip1, $parsed_ip2) = split(/,/, $line);
+
+	    if ($parsed_ip2) {
+		$parsed_ip4 = $parsed_ip1;
+		$parsed_ip6 = $parsed_ip2;
+	    } elsif (Net::IP::ip_is_ipv4($parsed_ip1)) {
+		$parsed_ip4 = $parsed_ip1;
+	    } else {
+		$parsed_ip6 = $parsed_ip1;
+	    }
+	    $parsed_ip6 = $1 if $parsed_ip6 && $parsed_ip6 =~ m/\[(\S+)\]/;
+
+	    #delete changed
+	    if (!defined($macdb->{macs}->{$parsed_mac}) ||
+		($parsed_ip4 && $macdb->{macs}->{$parsed_mac}->{'ip4'} && $macdb->{macs}->{$parsed_mac}->{'ip4'} ne $parsed_ip4) ||
+		($parsed_ip6 && $macdb->{macs}->{$parsed_mac}->{'ip6'} && $macdb->{macs}->{$parsed_mac}->{'ip6'} ne $parsed_ip6)) {
+                    $reload = 1;
+		    next;
 	    }
 
-	    #delete changed ip
-	    my $ipversion = Net::IP::ip_is_ipv4($parsed_ip) ? "ip4" : "ip6";
-	    if ($macdb->{macs}->{$parsed_mac}->{$ipversion} && $macdb->{macs}->{$parsed_mac}->{$ipversion} ne $parsed_ip) {
-		$change = 1;
-		next;
+	    if ($parsed_mac eq $mac) {
+		$match = 1 if $ip4 && $parsed_ip4 && $ip4;
+		$match = 1 if $ip6 && $parsed_ip6 && $ip6;
 	    }
-	    print $out "$parsed_mac,$parsed_ip\n";
-	    #check if mac/ip already exist
-	    $match4 = 1 if $parsed_mac eq $mac && $macdb->{macs}->{$mac}->{'ip4'} && $macdb->{macs}->{$mac}->{'ip4'} eq $ip4;
-	    $match6 = 1 if $parsed_mac eq $mac && $macdb->{macs}->{$mac}->{'ip6'} && $macdb->{macs}->{$mac}->{'ip6'} eq $ip6;
-	}
 
-	if(!$match4 && $ip4) {
-	    print $out "$mac,$ip4\n";
-	    $change = 1;
+	    print $out "$line\n";
 	}
 
-	if(!$match6 && $ip6) {
-	    print $out "$mac,$ip6\n";
-	    $change = 1;
+	if(!$match) {
+	    my $reservation = $mac;
+	    $reservation .= ",$ip4" if $ip4;
+	    $reservation .= ",[$ip6]" if $ip6;
+	    print $out "$reservation\n";
+	    $reload = 1;
 	}
+
 	close $in;
 	close $out;
 	move $ethers_tmp_file, $ethers_file;
@@ -77,7 +87,7 @@ sub add_ip_mapping {
     }
 
     my $service_name = "dnsmasq\@$dhcpid";
-    PVE::Tools::run_command(['systemctl', 'reload', $service_name]) if $change;
+    PVE::Tools::run_command(['systemctl', 'reload', $service_name]) if $reload;
 
     #update lease as ip could still be associated to an old removed mac
     my $bus = Net::DBus->system();
@@ -86,7 +96,7 @@ sub add_ip_mapping {
 
     my @hostname = unpack("C*", "*");
     $manager->AddDhcpLease($ip4, $mac, \@hostname, undef, 0, 0, 0) if $ip4;
-    $manager->AddDhcpLease($ip6, $mac, \@hostname, undef, 0, 0, 0) if $ip6;
+#    $manager->AddDhcpLease($ip6, $mac, \@hostname, undef, 0, 0, 0) if $ip6;
 
 }
 
-- 
2.39.2




  parent reply	other threads:[~2023-11-18 14:14 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-18 14:13 [pve-devel] [PATCH pve-network 0/7] dhcp v4 Alexandre Derumier
2023-11-18 14:13 ` [pve-devel] [PATCH pve-network 1/7] fix subnet tests Alexandre Derumier
2023-11-18 14:13 ` [pve-devel] [PATCH pve-network 2/7] api2: ipam : remove add|del_mapping in mac/ip management Alexandre Derumier
2023-11-18 14:13 ` [pve-devel] [PATCH pve-network 3/7] dhcp : remove del_ip_mapping Alexandre Derumier
2023-11-18 14:13 ` [pve-devel] [PATCH pve-network 4/7] dnsmasq: configure static range for each subnet Alexandre Derumier
2023-11-18 14:13 ` [pve-devel] [PATCH pve-network 5/7] dnsmasq: enable dbus && purge old ip lease on reservation Alexandre Derumier
2023-11-18 14:13 ` Alexandre Derumier [this message]
2023-11-18 14:13 ` [pve-devel] [PATCH pve-network 7/7] dhcp : dnsmasq: generate dbus policy Alexandre Derumier
2023-11-20 16:42 ` [pve-devel] applied-series: [PATCH pve-network 0/7] dhcp v4 Thomas Lamprecht

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=20231118141314.2785997-7-aderumier@odiso.com \
    --to=aderumier@odiso.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal