]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
network: add netmask to dhcp range of dnsmasq conf file for IPv4
authorLaine Stump <laine@laine.org>
Mon, 18 Feb 2019 21:34:50 +0000 (16:34 -0500)
committerLaine Stump <laine@laine.org>
Thu, 21 Feb 2019 18:06:16 +0000 (13:06 -0500)
dnsmasq documentation says that the *IPv4* prefix/network
address/broadcast address sent to dhcp clients will be automatically
determined by dnsmasq by looking at the interface it's listening on,
so the original libvirt code did not add a netmask to the dnsmasq
commandline (or later, the dnsmasq conf file).

For *IPv6* however, dnsmasq apparently cannot automatically determine
the prefix (functionally the same as a netmask), and it must be
explicitly provided in the conf file (as a part of the dhcp-range
option). So many years after IPv4 DHCP support had been added, when
IPv6 dhcp support was added the prefix was included at the end of the
dhcp-range setting, but only for IPv6.

A user had reported a bug on a host where one of the interfaces was a
superset of the libvirt network where dhcp is needed (e.g., the host's
ethernet is 10.0.0.20/8, and the libvirt network is 10.10.0.1/24). For
some reason dnsmasq was supplying the netmask for the /8 network to
clients requesting an address on the /24 interface.

This seems like a bug in dnsmasq, but even if/when it gets fixed
there, it looks like there is no harm in just always adding the
netmask to all IPv4 dhcp-range options similar to how prefix is added
to all IPv6 dhcp-range options.

Signed-off-by: Laine Stump <laine@laine.org>
Reviewed-by: John Ferlan <jferlan@redhat.com>
12 files changed:
src/network/bridge_driver.c
tests/networkxml2confdata/dhcp6-nat-network.conf
tests/networkxml2confdata/isolated-network.conf
tests/networkxml2confdata/nat-network-dns-srv-record-minimal.conf
tests/networkxml2confdata/nat-network-dns-srv-record.conf
tests/networkxml2confdata/nat-network-dns-txt-record.conf
tests/networkxml2confdata/nat-network-mtu.conf
tests/networkxml2confdata/nat-network-name-with-quotes.conf
tests/networkxml2confdata/nat-network.conf
tests/networkxml2confdata/netboot-network.conf
tests/networkxml2confdata/netboot-proxy-network.conf
tests/networkxml2confdata/ptr-domains-auto.conf

index 6d80818e40215e15fa184d929a46604cdf7e1690..b3ca5b8a158a69d1cf08e6236ae0c5b599312e85 100644 (file)
@@ -1320,11 +1320,27 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                 !(eaddr = virSocketAddrFormat(&ipdef->ranges[r].end)))
                 goto cleanup;
 
-            virBufferAsprintf(&configbuf, "dhcp-range=%s,%s",
-                              saddr, eaddr);
-            if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6))
-                virBufferAsprintf(&configbuf, ",%d", prefix);
-            virBufferAddLit(&configbuf, "\n");
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) {
+               virBufferAsprintf(&configbuf, "dhcp-range=%s,%s,%d\n",
+                                 saddr, eaddr, prefix);
+            } else {
+                /* IPv4 - dnsmasq requires a netmask rather than prefix */
+                virSocketAddr netmask;
+                VIR_AUTOFREE(char *) netmaskStr = NULL;
+
+                if (virSocketAddrPrefixToNetmask(prefix, &netmask, AF_INET) < 0) {
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("Failed to translate bridge '%s' "
+                                     "prefix %d to netmask"),
+                                   def->bridge, prefix);
+                    goto cleanup;
+                }
+
+                if (!(netmaskStr = virSocketAddrFormat(&netmask)))
+                    goto cleanup;
+                virBufferAsprintf(&configbuf, "dhcp-range=%s,%s,%s\n",
+                                  saddr, eaddr, netmaskStr);
+            }
 
             VIR_FREE(saddr);
             VIR_FREE(eaddr);
index d1058df3b65e17e2345a35148b7c1385e9c5a4d9..536974e508f3c3cc086cbfc34d737b699c9b0496 100644 (file)
@@ -8,7 +8,7 @@ strict-order
 except-interface=lo
 bind-dynamic
 interface=virbr0
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-range=2001:db8:ac10:fd01::1:10,2001:db8:ac10:fd01::1:ff,64
index ce4a59f6c1a719feb0347f208181f543d9d73fe9..693a83d9a0fdb03d95dd12dec4814b2573d6fe15 100644 (file)
@@ -10,7 +10,7 @@ bind-interfaces
 listen-address=192.168.152.1
 dhcp-option=3
 no-resolv
-dhcp-range=192.168.152.2,192.168.152.254
+dhcp-range=192.168.152.2,192.168.152.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index f35ea1d5d42d7fa4c7472c08b6bf0a28694b6ca6..0b2ca6f5aab8468fa2a9cff0b02e77ca296d9fce 100644 (file)
@@ -13,7 +13,7 @@ listen-address=fc00:db8:ac10:fe01::1
 listen-address=fc00:db8:ac10:fd01::1
 listen-address=10.24.10.1
 srv-host=_name._tcp
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index af1ed707585154139baaa467bd1ef9c6948f9095..a18c09aaa7a10c0f94ba3fb6b6696781d50bac0e 100644 (file)
@@ -15,7 +15,7 @@ srv-host=_name4._tcp.test4.com,test4.example.com,4444
 srv-host=_name5._udp,test5.example.com,1,55,555
 srv-host=_name6._tcp.test6.com,test6.example.com,6666,0,666
 srv-host=_name7._tcp.test7.com,test7.example.com,1,0,777
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index 7f560fbb5c76ca8222817b30417e589425635d12..735c261c013a6839b5792c3af63c3c86256613a1 100644 (file)
@@ -9,7 +9,7 @@ except-interface=lo
 bind-dynamic
 interface=virbr0
 txt-record=example,example value
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index 91b574b964d296e2975f7a02604e6c36ab21e317..1dd4754f2ae174552b123b04098299ad4e092513 100644 (file)
@@ -8,7 +8,7 @@ strict-order
 except-interface=lo
 bind-dynamic
 interface=virbr0
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index 36e11d17b9906cc5ff75366fca150ecd7693de32..1b06de30663cbad39502738c87796e4c0a6840c7 100644 (file)
@@ -13,7 +13,7 @@ listen-address=fc00:db8:ac10:fe01::1
 listen-address=fc00:db8:ac10:fd01::1
 listen-address=10.24.10.1
 srv-host=_name._tcp
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index a3c8b102d302d13f097dc6b0be932a1fbaedf821..873a360accabceee29c14cf43abc26439e080cfa 100644 (file)
@@ -8,7 +8,7 @@ strict-order
 except-interface=lo
 bind-dynamic
 interface=virbr0
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253
index b554a5456c6ab0c831a400ffd3454deb8fb19631..99272b9d68f0cf9ddf22e9d425d981e3efa9fb2c 100644 (file)
@@ -10,7 +10,7 @@ expand-hosts
 except-interface=lo
 bind-interfaces
 listen-address=192.168.122.1
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 enable-tftp
index afb4033f7eda4e00e8ac8f59bc7f0b739fe9098e..fb0a20cff470a4016ac54e5486d3e55e9a9900c4 100644 (file)
@@ -10,7 +10,7 @@ expand-hosts
 except-interface=lo
 bind-interfaces
 listen-address=192.168.122.1
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-boot=pxeboot.img,,10.20.30.40
index 7f1a393dd5a24c164fcc0b6d1ce3fdd81c40e3d5..86701c4ddfe209f0045618927cd2458c8e825901 100644 (file)
@@ -10,7 +10,7 @@ local=/1.0.e.f.0.1.c.a.8.b.d.0.1.0.0.2.ip6.arpa/
 except-interface=lo
 bind-dynamic
 interface=virbr0
-dhcp-range=192.168.122.2,192.168.122.254
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
 dhcp-no-override
 dhcp-authoritative
 dhcp-lease-max=253