2015-01-14 14:21:10 +01:00
|
|
|
/*
|
|
|
|
* networkcommon_conf.c: network XML handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2014 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2006-2008 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "networkcommon_conf.h"
|
|
|
|
#include "virxml.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NETWORK
|
|
|
|
|
2021-03-11 08:16:13 +01:00
|
|
|
virNetDevIPRoute *
|
2016-06-14 13:40:04 -04:00
|
|
|
virNetDevIPRouteCreate(const char *errorDetail,
|
|
|
|
const char *family,
|
|
|
|
const char *address,
|
|
|
|
const char *netmask,
|
|
|
|
const char *gateway,
|
|
|
|
unsigned int prefix,
|
|
|
|
bool hasPrefix,
|
|
|
|
unsigned int metric,
|
|
|
|
bool hasMetric)
|
2015-01-14 14:21:10 +01:00
|
|
|
{
|
2021-02-25 12:51:56 +01:00
|
|
|
g_autoptr(virNetDevIPRoute) def = NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
virSocketAddr testAddr;
|
|
|
|
|
2020-10-07 21:15:50 +02:00
|
|
|
def = g_new0(virNetDevIPRoute, 1);
|
2015-01-14 14:21:10 +01:00
|
|
|
|
2019-10-20 13:49:46 +02:00
|
|
|
def->family = g_strdup(family);
|
2015-01-14 14:21:10 +01:00
|
|
|
|
|
|
|
def->prefix = prefix;
|
|
|
|
def->has_prefix = hasPrefix;
|
|
|
|
def->metric = metric;
|
|
|
|
def->has_metric = hasMetric;
|
|
|
|
|
|
|
|
/* Note: both network and gateway addresses must be specified */
|
|
|
|
|
|
|
|
if (!address) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Missing required address attribute in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gateway) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Missing required gateway attribute in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virSocketAddrParse(&def->address, address, AF_UNSPEC) < 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Bad network address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virSocketAddrParse(&def->gateway, gateway, AF_UNSPEC) < 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Bad gateway address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, gateway);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* validate network address, etc. for each family */
|
|
|
|
if ((def->family == NULL) || (STREQ(def->family, "ipv4"))) {
|
|
|
|
if (!(VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET) ||
|
|
|
|
VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_UNSPEC))) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
def->family == NULL ?
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: No family specified for non-IPv4 address '%2$s' in route definition") :
|
|
|
|
_("%1$s: IPv4 family specified for non-IPv4 address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->gateway, AF_INET)) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
def->family == NULL ?
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: No family specified for non-IPv4 gateway '%2$s' in route definition") :
|
|
|
|
_("%1$s: IPv4 family specified for non-IPv4 gateway '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (netmask) {
|
|
|
|
if (virSocketAddrParse(&def->netmask, netmask, AF_UNSPEC) < 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Bad netmask address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, netmask);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->netmask, AF_INET)) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Invalid netmask '%2$s' for address '%3$s' (both must be IPv4)"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, netmask, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (def->has_prefix) {
|
|
|
|
/* can't have both netmask and prefix at the same time */
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Route definition cannot have both a prefix and a netmask"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (def->prefix > 32) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Invalid prefix %2$u specified in route definition, must be 0 - 32"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, def->prefix);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else if (STREQ(def->family, "ipv6")) {
|
|
|
|
if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET6)) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: ipv6 family specified for non-IPv6 address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (netmask) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Specifying netmask invalid for IPv6 address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->gateway, AF_INET6)) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: ipv6 specified for non-IPv6 gateway address '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, gateway);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (def->prefix > 128) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Invalid prefix %2$u specified in route definition, must be 0 - 128"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, def->prefix);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Unrecognized family '%2$s' in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, def->family);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure the address is a network address */
|
|
|
|
if (netmask) {
|
|
|
|
if (virSocketAddrMask(&def->address, &def->netmask, &testAddr) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Error converting address '%2$s' with netmask '%3$s' to network-address in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address, netmask);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virSocketAddrMaskByPrefix(&def->address,
|
|
|
|
def->prefix, &testAddr) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Error converting address '%2$s' with prefix %3$u to network-address in route definition"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address, def->prefix);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!virSocketAddrEqual(&def->address, &testAddr)) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
2023-03-09 11:20:54 +01:00
|
|
|
_("%1$s: Address '%2$s' in route definition is not a network address"),
|
2015-01-14 14:21:10 +01:00
|
|
|
errorDetail, address);
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 12:51:56 +01:00
|
|
|
return g_steal_pointer(&def);
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 08:16:13 +01:00
|
|
|
virNetDevIPRoute *
|
2016-06-14 13:40:04 -04:00
|
|
|
virNetDevIPRouteParseXML(const char *errorDetail,
|
2022-10-05 14:33:57 +02:00
|
|
|
xmlNodePtr node)
|
2015-01-14 14:21:10 +01:00
|
|
|
{
|
2022-10-05 14:33:57 +02:00
|
|
|
g_autofree char *family = virXMLPropString(node, "family");
|
|
|
|
g_autofree char *address = virXMLPropString(node, "address");
|
|
|
|
g_autofree char *netmask = virXMLPropString(node, "netmask");
|
|
|
|
g_autofree char *gateway = virXMLPropString(node, "gateway");
|
|
|
|
unsigned int prefix = 0;
|
|
|
|
unsigned int metric = 0;
|
2015-01-14 14:21:10 +01:00
|
|
|
bool hasPrefix = false;
|
|
|
|
bool hasMetric = false;
|
2022-10-05 14:33:57 +02:00
|
|
|
int rc;
|
2015-01-14 14:21:10 +01:00
|
|
|
|
2022-10-05 14:33:57 +02:00
|
|
|
if ((rc = virXMLPropUInt(node, "prefix", 10, VIR_XML_PROP_NONE, &prefix)) < 0)
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2022-10-05 14:33:57 +02:00
|
|
|
|
|
|
|
if (rc == 1)
|
|
|
|
hasPrefix = true;
|
|
|
|
|
|
|
|
if ((rc = virXMLPropUInt(node, "metric", 10, VIR_XML_PROP_NONZERO, &metric)) < 0)
|
2021-02-25 12:51:57 +01:00
|
|
|
return NULL;
|
2022-10-05 14:33:57 +02:00
|
|
|
|
|
|
|
if (rc == 1)
|
2015-01-14 14:21:10 +01:00
|
|
|
hasMetric = true;
|
|
|
|
|
2021-02-25 12:51:57 +01:00
|
|
|
return virNetDevIPRouteCreate(errorDetail, family, address, netmask,
|
|
|
|
gateway, prefix, hasPrefix, metric,
|
|
|
|
hasMetric);
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-03-11 08:16:13 +01:00
|
|
|
virNetDevIPRouteFormat(virBuffer *buf,
|
2016-06-14 13:40:04 -04:00
|
|
|
const virNetDevIPRoute *def)
|
2015-01-14 14:21:10 +01:00
|
|
|
{
|
2021-02-25 12:51:55 +01:00
|
|
|
g_autofree char *address = NULL;
|
|
|
|
g_autofree char *netmask = NULL;
|
|
|
|
g_autofree char *gateway = NULL;
|
2015-01-14 14:21:10 +01:00
|
|
|
|
|
|
|
virBufferAddLit(buf, "<route");
|
|
|
|
|
|
|
|
if (def->family)
|
|
|
|
virBufferAsprintf(buf, " family='%s'", def->family);
|
|
|
|
|
2021-02-25 12:51:55 +01:00
|
|
|
if (!(address = virSocketAddrFormat(&def->address)))
|
2020-01-06 18:57:26 -03:00
|
|
|
return -1;
|
2021-02-25 12:51:55 +01:00
|
|
|
virBufferAsprintf(buf, " address='%s'", address);
|
2015-01-14 14:21:10 +01:00
|
|
|
|
|
|
|
if (VIR_SOCKET_ADDR_VALID(&def->netmask)) {
|
2021-02-25 12:51:55 +01:00
|
|
|
if (!(netmask = virSocketAddrFormat(&def->netmask)))
|
2020-01-06 18:57:26 -03:00
|
|
|
return -1;
|
2021-02-25 12:51:55 +01:00
|
|
|
virBufferAsprintf(buf, " netmask='%s'", netmask);
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|
|
|
|
if (def->has_prefix)
|
|
|
|
virBufferAsprintf(buf, " prefix='%u'", def->prefix);
|
|
|
|
|
2021-02-25 12:51:55 +01:00
|
|
|
if (!(gateway = virSocketAddrFormat(&def->gateway)))
|
2020-01-06 18:57:26 -03:00
|
|
|
return -1;
|
2021-02-25 12:51:55 +01:00
|
|
|
virBufferAsprintf(buf, " gateway='%s'", gateway);
|
2015-01-14 14:21:10 +01:00
|
|
|
|
|
|
|
if (def->has_metric && def->metric > 0)
|
|
|
|
virBufferAsprintf(buf, " metric='%u'", def->metric);
|
|
|
|
virBufferAddLit(buf, "/>\n");
|
|
|
|
|
2020-01-06 18:57:26 -03:00
|
|
|
return 0;
|
2015-01-14 14:21:10 +01:00
|
|
|
}
|