When a gateway is set on a network device, a new default route via this
gateway through the devoce will be added in the container.
---
src/lxc/lxc_container.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 62e9d76..4a373cb 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -479,6 +479,33 @@ lxcContainerGetNetDef(virDomainDefPtr vmDef, const char *devName)
return NULL;
}
+static int
+lxcContainerAddDefaultRoute(const char *ifname, const char *gateway)
+{
+ virSocketAddr address;
+ virSocketAddr network;
+
+ VIR_DEBUG("Adding default route via %s on dev %s", gateway, ifname);
+ if (virSocketAddrParse(&address, gateway, AF_UNSPEC) < 0)
+ return -1;
+
+ if (!strchr(gateway, ':')) {
+ if (virSocketAddrParseIPv4(&network, "0.0.0.0") < 0)
+ return -1;
+ } else {
+ if (virSocketAddrParseIPv6(&network, "::") < 0)
+ return -1;
+ }
+
+ if (virNetDevAddRoute(ifname, &network, 0, &address, 0) < 0) {
+ virReportError(VIR_ERR_SYSTEM_ERROR,
+ _("Failed adding default route via %s on dev %s"),
+ gateway, ifname);
+ return -1;
+ }
+ return 0;
+}
+
/**
* lxcContainerRenameAndEnableInterfaces:
* @nveths: number of interfaces
@@ -539,6 +566,15 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr
vmDef,
if (rc < 0)
goto error_out;
+ /* Set the gateways */
+ if (netDef->gateway_ipv4 &&
+ lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv4) < 0)
+ goto error_out;
+
+ if (netDef->gateway_ipv6 &&
+ lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv6) < 0)
+ goto error_out;
+
VIR_FREE(newname);
}
--
1.8.4.5