[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 1/3] network driver: Start dnsmasq even if no dhcp ranges/hosts are specified.

This fixes a regression introduced in commit ad48df, and reported on
the libvirt-users list:


The problem in that commit was that we began searching a list of ip
address definitions (rather than just having one) to look for a dhcp
range or static host; when we didn't find any, our pointer (ipdef) was
left at NULL, and when ipdef was NULL, we returned without starting up

Previously dnsmasq was started even without any dhcp ranges or static
entries, because it's still useful for DNS services.

Another problem I noticed while investigating was that, if there are
IPv6 addresses, but no IPv4 addresses of any kind, we would jump out
at an ever higher level in the call chain.

This patch does the following:

1) networkBuildDnsmasqArgv() = all uses of ipdef are protected from
   NULL dereference. (this patch doesn't change indentation, to make
   review easier. The next patch will change just the
   indentation). ipdef is intended to point to the first IPv4 address
   with DHCP info (or the first IPv4 address if none of them have any
   dhcp info).

2) networkStartDhcpDaemon() = if the loop looking for an ipdef with
   DHCP info comes up empty, we then grab the first IPv4 def from the
   list. Also, instead of returning if there are no IPv4 defs, we just
   return if there are no IP defs at all (either v4 or v6). This way a
   network that is IPv6-only will still get dnsmasq listening for DNS

3) in networkStartNetworkDaemon() - we will startup dhcp not just if there
   are any IPv4 addresses, but also if there are any IPv6 addresses.
 src/network/bridge_driver.c |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index e666cbb..e3156fa 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -440,12 +440,9 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
                         virCommandPtr cmd) {
     int r, ret = -1;
     int nbleases = 0;
-    char *bridgeaddr;
     int ii;
     virNetworkIpDefPtr tmpipdef;
-    if (!(bridgeaddr = virSocketFormatAddr(&ipdef->address)))
-        goto cleanup;
      * NB, be careful about syntax for dnsmasq options in long format.
@@ -501,6 +498,7 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
+    if (ipdef) {
     for (r = 0 ; r < ipdef->nranges ; r++) {
         char *saddr = virSocketFormatAddr(&ipdef->ranges[r].start);
         if (!saddr)
@@ -524,8 +522,12 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
      * dnsmasq.
     if (!ipdef->nranges && ipdef->nhosts) {
+        char *bridgeaddr = virSocketFormatAddr(&ipdef->address);
+        if (!bridgeaddr)
+            goto cleanup;
         virCommandAddArg(cmd, "--dhcp-range");
         virCommandAddArgFormat(cmd, "%s,static", bridgeaddr);
+        VIR_FREE(bridgeaddr);
     if (ipdef->nranges > 0) {
@@ -569,10 +571,10 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
             virCommandAddArg(cmd, ipdef->bootfile);
+    }
     ret = 0;
-    VIR_FREE(bridgeaddr);
     return ret;
@@ -594,7 +596,16 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
         if (ipdef->nranges || ipdef->nhosts)
+    /* If no IPv4 addresses had dhcp info, pick the first (if there were any). */
     if (!ipdef)
+        ipdef = virNetworkDefGetIpByIndex(network->def, AF_INET, 0);
+    /* If there are no IP addresses at all (v4 or v6), return now, since
+     * there won't be any address for dnsmasq to listen on anyway.
+     * If there are any addresses, even if no dhcp ranges or static entries,
+     * we should continue and run dnsmasq, just for the DNS capabilities.
+     */
+    if (!virNetworkDefGetIpByIndex(network->def, AF_UNSPEC, 0))
         return 0;
     if ((err = virFileMakePath(NETWORK_PID_DIR)) != 0) {
@@ -1678,7 +1689,7 @@ networkStartNetworkDaemon(struct network_driver *driver,
     /* start dnsmasq if there are any IPv4 addresses */
-    if (v4present && networkStartDhcpDaemon(network) < 0)
+    if ((v4present || v6present) && networkStartDhcpDaemon(network) < 0)
         goto err3;
     /* start radvd if there are any ipv6 addresses */

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]