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 | 40 ++++++++++++++++++++++++++++++++++++++++
src/util/virsocketaddr.h | 3 +++
2 files changed, 43 insertions(+)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 4aeb19c..727565d 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -480,6 +480,35 @@ lxcContainerGetNetDef(virDomainDefPtr vmDef, const char *devName)
return NULL;
}
+static int
+lxcContainerAddDefaultRoute(const char *ifname,
+ const char *gateway,
+ int family)
+{
+ virSocketAddr address;
+ virSocketAddr network;
+
+ VIR_DEBUG("Adding default route via %s on dev %s", gateway, ifname);
+ if (virSocketAddrParse(&address, gateway, family) < 0)
+ return -1;
+
+ if (family == AF_INET) {
+ if (virSocketAddrParseIPv4(&network, VIR_SOCKET_ADDR_IPV4_ALL) < 0)
+ return -1;
+ } else {
+ if (virSocketAddrParseIPv6(&network, VIR_SOCKET_ADDR_IPV6_ALL) < 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 +568,17 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr
vmDef,
if (rc < 0)
goto error_out;
+ /* Set the gateways */
+ if (netDef->gateway_ipv4 &&
+ lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv4,
+ AF_INET) < 0)
+ goto error_out;
+
+ if (netDef->gateway_ipv6 &&
+ lxcContainerAddDefaultRoute(newname, netDef->gateway_ipv6,
+ AF_INET6) < 0)
+ goto error_out;
+
VIR_FREE(newname);
}
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 053855b..198b109 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -54,6 +54,9 @@ typedef struct {
# define VIR_SOCKET_ADDR_FAMILY(s) \
((s)->data.sa.sa_family)
+# define VIR_SOCKET_ADDR_IPV4_ALL "0.0.0.0"
+# define VIR_SOCKET_ADDR_IPV6_ALL "::"
+
typedef virSocketAddr *virSocketAddrPtr;
typedef struct _virSocketAddrRange virSocketAddrRange;
--
2.1.2