2018-05-10 13:30:42 +00:00
|
|
|
/*
|
|
|
|
* virnwfilterbindingdef.c: network filter binding XML processing
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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 "viralloc.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "nwfilter_params.h"
|
|
|
|
#include "virnwfilterbindingdef.h"
|
2018-05-10 12:37:53 +00:00
|
|
|
#include "viruuid.h"
|
2018-05-10 13:30:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NWFILTER
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virNWFilterBindingDefFree(virNWFilterBindingDef *def)
|
2018-05-10 13:30:42 +00:00
|
|
|
{
|
|
|
|
if (!def)
|
|
|
|
return;
|
|
|
|
|
2021-01-30 19:05:50 +00:00
|
|
|
g_free(def->ownername);
|
|
|
|
g_free(def->portdevname);
|
|
|
|
g_free(def->linkdevname);
|
|
|
|
g_free(def->filter);
|
2021-11-30 13:13:28 +00:00
|
|
|
g_clear_pointer(&def->filterparams, g_hash_table_unref);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
2021-01-30 19:05:50 +00:00
|
|
|
g_free(def);
|
2018-05-10 13:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virNWFilterBindingDef *
|
|
|
|
virNWFilterBindingDefCopy(virNWFilterBindingDef *src)
|
2018-05-10 13:30:42 +00:00
|
|
|
{
|
2021-07-06 08:11:19 +00:00
|
|
|
g_autoptr(virNWFilterBindingDef) ret = g_new0(virNWFilterBindingDef, 1);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
ret->ownername = g_strdup(src->ownername);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
|
|
|
memcpy(ret->owneruuid, src->owneruuid, sizeof(ret->owneruuid));
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
ret->portdevname = g_strdup(src->portdevname);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
ret->linkdevname = g_strdup(src->linkdevname);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
|
|
|
ret->mac = src->mac;
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
ret->filter = g_strdup(src->filter);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
2021-07-06 08:07:39 +00:00
|
|
|
ret->filterparams = virHashNew(virNWFilterVarValueHashFree);
|
2018-05-10 13:30:42 +00:00
|
|
|
|
|
|
|
if (virNWFilterHashTablePutAll(src->filterparams, ret->filterparams) < 0)
|
2021-07-06 11:45:47 +00:00
|
|
|
return NULL;
|
2018-05-10 13:30:42 +00:00
|
|
|
|
2021-07-06 08:11:19 +00:00
|
|
|
return g_steal_pointer(&ret);
|
2018-05-10 13:30:42 +00:00
|
|
|
}
|
2018-05-10 12:37:53 +00:00
|
|
|
|
|
|
|
|
2022-09-22 13:47:40 +00:00
|
|
|
virNWFilterBindingDef *
|
2018-05-10 12:37:53 +00:00
|
|
|
virNWFilterBindingDefParseXML(xmlXPathContextPtr ctxt)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virNWFilterBindingDef *ret;
|
2018-05-10 12:37:53 +00:00
|
|
|
char *uuid = NULL;
|
|
|
|
char *mac = NULL;
|
|
|
|
xmlNodePtr node;
|
|
|
|
|
2020-10-07 19:15:50 +00:00
|
|
|
ret = g_new0(virNWFilterBindingDef, 1);
|
2018-05-10 12:37:53 +00:00
|
|
|
|
|
|
|
ret->portdevname = virXPathString("string(./portdev/@name)", ctxt);
|
|
|
|
if (!ret->portdevname) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("filter binding has no port dev name"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virXPathNode("./linkdev", ctxt)) {
|
|
|
|
ret->linkdevname = virXPathString("string(./linkdev/@name)", ctxt);
|
|
|
|
if (!ret->linkdevname) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("filter binding has no link dev name"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->ownername = virXPathString("string(./owner/name)", ctxt);
|
|
|
|
if (!ret->ownername) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("filter binding has no owner name"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid = virXPathString("string(./owner/uuid)", ctxt);
|
|
|
|
if (!uuid) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("filter binding has no owner UUID"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virUUIDParse(uuid, ret->owneruuid) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse UUID '%s'"), uuid);
|
|
|
|
VIR_FREE(uuid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
VIR_FREE(uuid);
|
|
|
|
|
|
|
|
mac = virXPathString("string(./mac/@address)", ctxt);
|
|
|
|
if (!mac) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("filter binding has no MAC address"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virMacAddrParse(mac, &ret->mac) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse MAC '%s'"), mac);
|
|
|
|
VIR_FREE(mac);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
VIR_FREE(mac);
|
|
|
|
|
|
|
|
ret->filter = virXPathString("string(./filterref/@filter)", ctxt);
|
|
|
|
if (!ret->filter) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("filter binding has no filter reference"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = virXPathNode("./filterref", ctxt);
|
|
|
|
if (node &&
|
|
|
|
!(ret->filterparams = virNWFilterParseParamAttributes(node)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virNWFilterBindingDefFree(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-22 13:56:32 +00:00
|
|
|
virNWFilterBindingDef *
|
2018-05-10 12:37:53 +00:00
|
|
|
virNWFilterBindingDefParse(const char *xmlStr,
|
2021-08-26 12:32:33 +00:00
|
|
|
const char *filename,
|
|
|
|
unsigned int flags)
|
2018-05-10 12:37:53 +00:00
|
|
|
{
|
2021-08-11 11:57:18 +00:00
|
|
|
g_autoptr(xmlDoc) xml = NULL;
|
2022-09-22 13:47:40 +00:00
|
|
|
g_autoptr(xmlXPathContext) ctxt = NULL;
|
2022-09-22 12:30:53 +00:00
|
|
|
bool validate = flags & VIR_NWFILTER_BINDING_CREATE_VALIDATE;
|
2018-05-10 12:37:53 +00:00
|
|
|
|
2022-09-22 13:47:40 +00:00
|
|
|
if (!(xml = virXMLParse(filename, xmlStr, _("(nwfilterbinding_definition)"),
|
|
|
|
"filterbinding", &ctxt, "nwfilterbinding.rng", validate)))
|
|
|
|
return NULL;
|
2018-05-10 12:37:53 +00:00
|
|
|
|
2022-09-22 13:47:40 +00:00
|
|
|
return virNWFilterBindingDefParseXML(ctxt);
|
2018-05-10 12:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
virNWFilterBindingDefFormat(const virNWFilterBindingDef *def)
|
|
|
|
{
|
2020-07-03 02:19:01 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2018-05-10 12:37:53 +00:00
|
|
|
|
2020-07-03 02:19:01 +00:00
|
|
|
if (virNWFilterBindingDefFormatBuf(&buf, def) < 0)
|
2018-05-10 12:37:53 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virNWFilterBindingDefFormatBuf(virBuffer *buf,
|
2018-05-10 12:37:53 +00:00
|
|
|
const virNWFilterBindingDef *def)
|
|
|
|
{
|
|
|
|
char uuid[VIR_UUID_STRING_BUFLEN];
|
|
|
|
char mac[VIR_MAC_STRING_BUFLEN];
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "<filterbinding>\n");
|
|
|
|
|
|
|
|
virBufferAdjustIndent(buf, 2);
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "<owner>\n");
|
|
|
|
virBufferAdjustIndent(buf, 2);
|
|
|
|
virBufferEscapeString(buf, "<name>%s</name>\n", def->ownername);
|
|
|
|
virUUIDFormat(def->owneruuid, uuid);
|
|
|
|
virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuid);
|
|
|
|
virBufferAdjustIndent(buf, -2);
|
|
|
|
virBufferAddLit(buf, "</owner>\n");
|
|
|
|
|
|
|
|
virBufferEscapeString(buf, "<portdev name='%s'/>\n", def->portdevname);
|
|
|
|
if (def->linkdevname)
|
|
|
|
virBufferEscapeString(buf, "<linkdev name='%s'/>\n", def->linkdevname);
|
|
|
|
|
|
|
|
virMacAddrFormat(&def->mac, mac);
|
|
|
|
virBufferAsprintf(buf, "<mac address='%s'/>\n", mac);
|
|
|
|
|
|
|
|
if (virNWFilterFormatParamAttributes(buf, def->filterparams, def->filter) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virBufferAdjustIndent(buf, -2);
|
|
|
|
virBufferAddLit(buf, "</filterbinding>\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|