]>
git.proxmox.com Git - pve-network.git/blob - src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
1 package PVE
::Network
::SDN
::Dhcp
::Dnsmasq
;
6 use base
qw(PVE::Network::SDN::Dhcp::Plugin);
9 use PVE
::Tools
qw(file_set_contents run_command lock_file);
14 my $DNSMASQ_CONFIG_ROOT = '/etc/dnsmasq.d';
15 my $DNSMASQ_DEFAULT_ROOT = '/etc/default';
16 my $DNSMASQ_LEASE_ROOT = '/var/lib/misc';
23 my ($class, $dhcpid, $macdb, $mac, $ip4, $ip6) = @_;
25 my $ethers_file = "$DNSMASQ_CONFIG_ROOT/$dhcpid/ethers";
26 my $ethers_tmp_file = "$ethers_file.tmp";
33 open(my $in, '<', $ethers_file) or die "Could not open file '$ethers_file' $!\n";
34 open(my $out, '>', $ethers_tmp_file) or die "Could not open file '$ethers_tmp_file' $!\n";
36 while (my $line = <$in>) {
38 my ($parsed_mac, $parsed_ip) = split(/,/, $line);
40 if (!defined($macdb->{macs
}->{$parsed_mac})) {
46 my $ipversion = Net
::IP
::ip_is_ipv4
($parsed_ip) ?
"ip4" : "ip6";
47 if ($macdb->{macs
}->{$parsed_mac}->{$ipversion} && $macdb->{macs
}->{$parsed_mac}->{$ipversion} ne $parsed_ip) {
51 print $out "$parsed_mac,$parsed_ip\n";
52 #check if mac/ip already exist
53 $match4 = 1 if $parsed_mac eq $mac && $macdb->{macs
}->{$mac}->{'ip4'} && $macdb->{macs
}->{$mac}->{'ip4'} eq $ip4;
54 $match6 = 1 if $parsed_mac eq $mac && $macdb->{macs
}->{$mac}->{'ip6'} && $macdb->{macs
}->{$mac}->{'ip6'} eq $ip6;
57 if(!$match4 && $ip4) {
58 print $out "$mac,$ip4\n";
62 if(!$match6 && $ip6) {
63 print $out "$mac,$ip6\n";
68 move
$ethers_tmp_file, $ethers_file;
69 chmod 0644, $ethers_file;
72 PVE
::Tools
::lock_file
($ethers_file, 10, $appendFn);
75 warn "Unable to add $mac to the dnsmasq configuration: $@\n";
79 my $service_name = "dnsmasq\@$dhcpid";
80 PVE
::Tools
::run_command
(['systemctl', 'reload', $service_name]) if $change;
82 #update lease as ip could still be associated to an old removed mac
83 my $bus = Net
::DBus-
>system();
84 my $dnsmasq = $bus->get_service("uk.org.thekelleys.dnsmasq.$dhcpid");
85 my $manager = $dnsmasq->get_object("/uk/org/thekelleys/dnsmasq","uk.org.thekelleys.dnsmasq.$dhcpid");
87 my @hostname = unpack("C*", "*");
88 $manager->AddDhcpLease($ip4, $mac, \
@hostname, undef, 0, 0, 0) if $ip4;
89 $manager->AddDhcpLease($ip6, $mac, \
@hostname, undef, 0, 0, 0) if $ip6;
93 sub configure_subnet
{
94 my ($class, $dhcpid, $subnet_config) = @_;
96 die "No gateway defined for subnet $subnet_config->{id}"
97 if !$subnet_config->{gateway
};
99 my $tag = $subnet_config->{id
};
101 my @dnsmasq_config = (
102 "listen-address=$subnet_config->{gateway}",
106 if (ip_is_ipv6
($subnet_config->{network
})) {
107 $option_string = 'option6';
108 push @dnsmasq_config, "enable-ra";
110 $option_string = 'option';
111 push @dnsmasq_config, "dhcp-option=tag:$tag,$option_string:router,$subnet_config->{gateway}";
114 push @dnsmasq_config, "dhcp-option=tag:$tag,$option_string:dns-server,$subnet_config->{'dhcp-dns-server'}"
115 if $subnet_config->{'dhcp-dns-server'};
117 PVE
::Tools
::file_set_contents
(
118 "$DNSMASQ_CONFIG_ROOT/$dhcpid/10-$subnet_config->{id}.conf",
119 join("\n", @dnsmasq_config) . "\n"
123 sub configure_range
{
124 my ($class, $dhcpid, $subnet_config, $range_config) = @_;
126 my $subnet_file = "$DNSMASQ_CONFIG_ROOT/$dhcpid/10-$subnet_config->{id}.conf";
127 my $tag = $subnet_config->{id
};
129 my ($zone, $network, $mask) = split(/-/, $tag);
131 if (Net
::IP
::ip_is_ipv4
($network)) {
132 $mask = (2 ** $mask - 1) << (32 - $mask);
133 $mask = join( '.', unpack( "C4", pack( "N", $mask ) ) );
136 open(my $fh, '>>', $subnet_file) or die "Could not open file '$subnet_file' $!\n";
137 print $fh "dhcp-range=set:$tag,$network,static,$mask,infinite\n";
141 sub before_configure
{
142 my ($class, $dhcpid) = @_;
144 my $config_directory = "$DNSMASQ_CONFIG_ROOT/$dhcpid";
146 mkdir($config_directory, 755) if !-d
$config_directory;
148 my $default_config = <<CFG;
149 CONFIG_DIR='$config_directory,\*.conf'
150 DNSMASQ_OPTS="--conf-file=/dev/null --enable-dbus=uk.org.thekelleys.dnsmasq.$dhcpid"
153 PVE
::Tools
::file_set_contents
(
154 "$DNSMASQ_DEFAULT_ROOT/dnsmasq.$dhcpid",
158 my $default_dnsmasq_config = <<CFG;
163 dhcp-leasefile=$DNSMASQ_LEASE_ROOT/dnsmasq.$dhcpid.leases
164 dhcp-hostsfile=$config_directory/ethers
165 dhcp-ignore=tag:!known
167 # Send an empty WPAD option. This may be REQUIRED to get windows 7 to behave.
168 dhcp-option=252,"\\n"
170 # Send microsoft-specific option to tell windows to release the DHCP lease
171 # when it shuts down. Note the "i" flag, to tell dnsmasq to send the
172 # value as a four-byte integer - that's what microsoft wants.
173 dhcp-option=vendor:MSFT,2,1i
175 # If a DHCP client claims that its name is "wpad", ignore that.
176 # This fixes a security hole. see CERT Vulnerability VU#598349
177 dhcp-name-match=set:wpad-ignore,wpad
178 dhcp-ignore-names=tag:wpad-ignore
181 PVE
::Tools
::file_set_contents
(
182 "$config_directory/00-default.conf",
183 $default_dnsmasq_config
186 unlink glob "$config_directory/10-*.conf";
189 sub after_configure
{
190 my ($class, $dhcpid) = @_;
192 my $service_name = "dnsmasq\@$dhcpid";
194 PVE
::Tools
::run_command
(['systemctl', 'enable', $service_name]);
195 PVE
::Tools
::run_command
(['systemctl', 'restart', $service_name]);
198 sub before_regenerate
{
201 PVE
::Tools
::run_command
(['systemctl', 'stop', "dnsmasq@*"]);
202 PVE
::Tools
::run_command
(['systemctl', 'disable', 'dnsmasq@']);
205 sub after_regenerate
{