提交 dd52444f 编写于 作者: D Daniel P. Berrangé

network: restrict usage of port management APIs

The port allocation APIs are currently called unconditionally for all
types of NIC, but (mostly) only do anything for NICs with type=network.

The exception is the port allocate API which does some validation even
for NICs with type!=network. Relying on this validation is flawed,
however, since the network driver may not even be installed. IOW virt
drivers must not delegate validation to the network driver for NICs
with type != network.

This change allows us to report errors when the virtual network driver
is not registered.
Reviewed-by: NCole Robinson <crobinso@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 545b0574
...@@ -30307,13 +30307,11 @@ int ...@@ -30307,13 +30307,11 @@ int
virDomainNetAllocateActualDevice(virDomainDefPtr dom, virDomainNetAllocateActualDevice(virDomainDefPtr dom,
virDomainNetDefPtr iface) virDomainNetDefPtr iface)
{ {
/* Just silently ignore if network driver isn't present. If something if (!netAllocate) {
* has tried to use a NIC with type=network, other code will already virReportError(VIR_ERR_NO_SUPPORT, "%s",
* cause an error. This ensures type=bridge doesn't break when _("Virtual networking driver is not available"));
* network driver is compiled out. return -1;
*/ }
if (!netAllocate)
return 0;
return netAllocate(dom, iface); return netAllocate(dom, iface);
} }
...@@ -30343,8 +30341,11 @@ bool ...@@ -30343,8 +30341,11 @@ bool
virDomainNetBandwidthChangeAllowed(virDomainNetDefPtr iface, virDomainNetBandwidthChangeAllowed(virDomainNetDefPtr iface,
virNetDevBandwidthPtr newBandwidth) virNetDevBandwidthPtr newBandwidth)
{ {
if (!netBandwidthChangeAllowed) if (!netBandwidthChangeAllowed) {
return 0; virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("Virtual networking driver is not available"));
return -1;
}
return netBandwidthChangeAllowed(iface, newBandwidth); return netBandwidthChangeAllowed(iface, newBandwidth);
} }
...@@ -30353,8 +30354,11 @@ int ...@@ -30353,8 +30354,11 @@ int
virDomainNetBandwidthUpdate(virDomainNetDefPtr iface, virDomainNetBandwidthUpdate(virDomainNetDefPtr iface,
virNetDevBandwidthPtr newBandwidth) virNetDevBandwidthPtr newBandwidth)
{ {
if (!netBandwidthUpdate) if (!netBandwidthUpdate) {
return 0; virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("Virtual networking driver is not available"));
return -1;
}
return netBandwidthUpdate(iface, newBandwidth); return netBandwidthUpdate(iface, newBandwidth);
} }
......
...@@ -904,6 +904,7 @@ libxlDomainCleanup(libxlDriverPrivatePtr driver, ...@@ -904,6 +904,7 @@ libxlDomainCleanup(libxlDriverPrivatePtr driver,
/* cleanup actual device */ /* cleanup actual device */
virDomainNetRemoveHostdev(vm->def, net); virDomainNetRemoveHostdev(vm->def, net);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, net); virDomainNetReleaseActualDevice(vm->def, net);
} }
} }
...@@ -1061,7 +1062,8 @@ libxlNetworkPrepareDevices(virDomainDefPtr def) ...@@ -1061,7 +1062,8 @@ libxlNetworkPrepareDevices(virDomainDefPtr def)
* network's pool of devices, or resolve bridge device name * network's pool of devices, or resolve bridge device name
* to the one defined in the network definition. * to the one defined in the network definition.
*/ */
if (virDomainNetAllocateActualDevice(def, net) < 0) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetAllocateActualDevice(def, net) < 0)
return -1; return -1;
actualType = virDomainNetGetActualType(net); actualType = virDomainNetGetActualType(net);
......
...@@ -3390,7 +3390,8 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver, ...@@ -3390,7 +3390,8 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver,
* network's pool of devices, or resolve bridge device name * network's pool of devices, or resolve bridge device name
* to the one defined in the network definition. * to the one defined in the network definition.
*/ */
if (virDomainNetAllocateActualDevice(vm->def, net) < 0) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetAllocateActualDevice(vm->def, net) < 0)
goto cleanup; goto cleanup;
actualType = virDomainNetGetActualType(net); actualType = virDomainNetGetActualType(net);
...@@ -3440,6 +3441,7 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver, ...@@ -3440,6 +3441,7 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver,
vm->def->nets[vm->def->nnets++] = net; vm->def->nets[vm->def->nnets++] = net;
} else { } else {
virDomainNetRemoveHostdev(vm->def, net); virDomainNetRemoveHostdev(vm->def, net);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, net); virDomainNetReleaseActualDevice(vm->def, net);
} }
virObjectUnref(cfg); virObjectUnref(cfg);
...@@ -3863,6 +3865,7 @@ libxlDomainDetachNetDevice(libxlDriverPrivatePtr driver, ...@@ -3863,6 +3865,7 @@ libxlDomainDetachNetDevice(libxlDriverPrivatePtr driver,
cleanup: cleanup:
libxl_device_nic_dispose(&nic); libxl_device_nic_dispose(&nic);
if (!ret) { if (!ret) {
if (detach->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, detach); virDomainNetReleaseActualDevice(vm->def, detach);
virDomainNetRemove(vm->def, detachidx); virDomainNetRemove(vm->def, detachidx);
} }
......
...@@ -3834,7 +3834,8 @@ lxcDomainAttachDeviceNetLive(virConnectPtr conn, ...@@ -3834,7 +3834,8 @@ lxcDomainAttachDeviceNetLive(virConnectPtr conn,
* network's pool of devices, or resolve bridge device name * network's pool of devices, or resolve bridge device name
* to the one defined in the network definition. * to the one defined in the network definition.
*/ */
if (virDomainNetAllocateActualDevice(vm->def, net) < 0) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetAllocateActualDevice(vm->def, net) < 0)
return -1; return -1;
actualType = virDomainNetGetActualType(net); actualType = virDomainNetGetActualType(net);
...@@ -4388,6 +4389,7 @@ lxcDomainDetachDeviceNetLive(virDomainObjPtr vm, ...@@ -4388,6 +4389,7 @@ lxcDomainDetachDeviceNetLive(virDomainObjPtr vm,
ret = 0; ret = 0;
cleanup: cleanup:
if (!ret) { if (!ret) {
if (detach->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, detach); virDomainNetReleaseActualDevice(vm->def, detach);
virDomainNetRemove(vm->def, detachidx); virDomainNetRemove(vm->def, detachidx);
virDomainNetDefFree(detach); virDomainNetDefFree(detach);
......
...@@ -224,6 +224,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver, ...@@ -224,6 +224,7 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
iface->ifname)); iface->ifname));
ignore_value(virNetDevVethDelete(iface->ifname)); ignore_value(virNetDevVethDelete(iface->ifname));
} }
if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, iface); virDomainNetReleaseActualDevice(vm->def, iface);
} }
...@@ -558,7 +559,8 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn, ...@@ -558,7 +559,8 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
if (virLXCProcessValidateInterface(net) < 0) if (virLXCProcessValidateInterface(net) < 0)
goto cleanup; goto cleanup;
if (virDomainNetAllocateActualDevice(def, net) < 0) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetAllocateActualDevice(def, net) < 0)
goto cleanup; goto cleanup;
type = virDomainNetGetActualType(net); type = virDomainNetGetActualType(net);
...@@ -637,6 +639,7 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn, ...@@ -637,6 +639,7 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
ignore_value(virNetDevOpenvswitchRemovePort( ignore_value(virNetDevOpenvswitchRemovePort(
virDomainNetGetActualBridgeName(iface), virDomainNetGetActualBridgeName(iface),
iface->ifname)); iface->ifname));
if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(def, iface); virDomainNetReleaseActualDevice(def, iface);
} }
} }
......
...@@ -4391,8 +4391,11 @@ networkAllocateActualDevice(virDomainDefPtr dom, ...@@ -4391,8 +4391,11 @@ networkAllocateActualDevice(virDomainDefPtr dom,
size_t i; size_t i;
int ret = -1; int ret = -1;
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
goto validate; virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
goto error;
}
virDomainActualNetDefFree(iface->data.network.actual); virDomainActualNetDefFree(iface->data.network.actual);
iface->data.network.actual = NULL; iface->data.network.actual = NULL;
...@@ -4711,7 +4714,6 @@ networkAllocateActualDevice(virDomainDefPtr dom, ...@@ -4711,7 +4714,6 @@ networkAllocateActualDevice(virDomainDefPtr dom,
if (virNetDevVPortProfileCheckComplete(virtport, true) < 0) if (virNetDevVPortProfileCheckComplete(virtport, true) < 0)
goto error; goto error;
validate:
/* make sure that everything now specified for the device is /* make sure that everything now specified for the device is
* actually supported on this type of network. NB: network, * actually supported on this type of network. NB: network,
* netdev, and iface->data.network.actual may all be NULL. * netdev, and iface->data.network.actual may all be NULL.
...@@ -4730,19 +4732,11 @@ networkAllocateActualDevice(virDomainDefPtr dom, ...@@ -4730,19 +4732,11 @@ networkAllocateActualDevice(virDomainDefPtr dom,
(actualType == VIR_DOMAIN_NET_TYPE_BRIDGE && (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE &&
virtport && virtport->virtPortType virtport && virtport->virtPortType
== VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH))) { == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH))) {
if (netdef) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("an interface connecting to network '%s' " _("an interface connecting to network '%s' "
"is requesting a vlan tag, but that is not " "is requesting a vlan tag, but that is not "
"supported for this type of network"), "supported for this type of network"),
netdef->name); netdef->name);
} else {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("an interface of type '%s' "
"is requesting a vlan tag, but that is not "
"supported for this type of connection"),
virDomainNetTypeToString(iface->type));
}
goto error; goto error;
} }
} }
...@@ -4758,7 +4752,6 @@ networkAllocateActualDevice(virDomainDefPtr dom, ...@@ -4758,7 +4752,6 @@ networkAllocateActualDevice(virDomainDefPtr dom,
} }
} }
if (netdef) {
netdef->connections++; netdef->connections++;
if (dev) if (dev)
dev->connections++; dev->connections++;
...@@ -4773,7 +4766,6 @@ networkAllocateActualDevice(virDomainDefPtr dom, ...@@ -4773,7 +4766,6 @@ networkAllocateActualDevice(virDomainDefPtr dom,
goto error; goto error;
} }
networkLogAllocation(netdef, actualType, dev, iface, true); networkLogAllocation(netdef, actualType, dev, iface, true);
}
ret = 0; ret = 0;
...@@ -4814,8 +4806,11 @@ networkNotifyActualDevice(virDomainDefPtr dom, ...@@ -4814,8 +4806,11 @@ networkNotifyActualDevice(virDomainDefPtr dom,
size_t i; size_t i;
char *master = NULL; char *master = NULL;
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
return; virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
goto error;
}
obj = virNetworkObjFindByName(driver->networks, iface->data.network.name); obj = virNetworkObjFindByName(driver->networks, iface->data.network.name);
if (!obj) { if (!obj) {
...@@ -5047,8 +5042,11 @@ networkReleaseActualDevice(virDomainDefPtr dom, ...@@ -5047,8 +5042,11 @@ networkReleaseActualDevice(virDomainDefPtr dom,
size_t i; size_t i;
int ret = -1; int ret = -1;
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
return 0; virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
goto error;
}
obj = virNetworkObjFindByName(driver->networks, iface->data.network.name); obj = virNetworkObjFindByName(driver->networks, iface->data.network.name);
if (!obj) { if (!obj) {
...@@ -5539,6 +5537,12 @@ networkBandwidthUpdate(virDomainNetDefPtr iface, ...@@ -5539,6 +5537,12 @@ networkBandwidthUpdate(virDomainNetDefPtr iface,
int plug_ret; int plug_ret;
int ret = -1; int ret = -1;
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
return -1;
}
if (!networkBandwidthGenericChecks(iface, newBandwidth)) if (!networkBandwidthGenericChecks(iface, newBandwidth))
return 0; return 0;
......
...@@ -11623,12 +11623,14 @@ qemuDomainSetInterfaceParameters(virDomainPtr dom, ...@@ -11623,12 +11623,14 @@ qemuDomainSetInterfaceParameters(virDomainPtr dom,
sizeof(*newBandwidth->out)); sizeof(*newBandwidth->out));
} }
if (!virDomainNetBandwidthChangeAllowed(net, newBandwidth)) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
!virDomainNetBandwidthChangeAllowed(net, newBandwidth))
goto endjob; goto endjob;
if (virNetDevBandwidthSet(net->ifname, newBandwidth, false, if (virNetDevBandwidthSet(net->ifname, newBandwidth, false,
!virDomainNetTypeSharesHostView(net)) < 0 || !virDomainNetTypeSharesHostView(net)) < 0 ||
virDomainNetBandwidthUpdate(net, newBandwidth) < 0) { (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetBandwidthUpdate(net, newBandwidth) < 0)) {
ignore_value(virNetDevBandwidthSet(net->ifname, ignore_value(virNetDevBandwidthSet(net->ifname,
net->bandwidth, net->bandwidth,
false, false,
......
...@@ -1385,7 +1385,8 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver, ...@@ -1385,7 +1385,8 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
* network's pool of devices, or resolve bridge device name * network's pool of devices, or resolve bridge device name
* to the one defined in the network definition. * to the one defined in the network definition.
*/ */
if (virDomainNetAllocateActualDevice(vm->def, net) < 0) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetAllocateActualDevice(vm->def, net) < 0)
goto cleanup; goto cleanup;
actualType = virDomainNetGetActualType(net); actualType = virDomainNetGetActualType(net);
...@@ -1689,6 +1690,7 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver, ...@@ -1689,6 +1690,7 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
virDomainNetRemoveHostdev(vm->def, net); virDomainNetRemoveHostdev(vm->def, net);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, net); virDomainNetReleaseActualDevice(vm->def, net);
} }
...@@ -4126,6 +4128,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver, ...@@ -4126,6 +4128,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver,
/* this function doesn't work with HOSTDEV networks yet, thus /* this function doesn't work with HOSTDEV networks yet, thus
* no need to change the pointer in the hostdev structure */ * no need to change the pointer in the hostdev structure */
if (olddev->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, olddev); virDomainNetReleaseActualDevice(vm->def, olddev);
virDomainNetDefFree(olddev); virDomainNetDefFree(olddev);
/* move newdev into the nets list, and NULL it out from the /* move newdev into the nets list, and NULL it out from the
...@@ -4157,7 +4160,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver, ...@@ -4157,7 +4160,7 @@ qemuDomainChangeNet(virQEMUDriverPtr driver,
* that the changes were minor enough that we didn't need to * that the changes were minor enough that we didn't need to
* replace the entire device object. * replace the entire device object.
*/ */
if (newdev) if (newdev && newdev->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, newdev); virDomainNetReleaseActualDevice(vm->def, newdev);
return ret; return ret;
...@@ -4750,6 +4753,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver, ...@@ -4750,6 +4753,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
virDomainHostdevDefFree(hostdev); virDomainHostdevDefFree(hostdev);
if (net) { if (net) {
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, net); virDomainNetReleaseActualDevice(vm->def, net);
virDomainNetDefFree(net); virDomainNetDefFree(net);
} }
...@@ -4852,6 +4856,7 @@ qemuDomainRemoveNetDevice(virQEMUDriverPtr driver, ...@@ -4852,6 +4856,7 @@ qemuDomainRemoveNetDevice(virQEMUDriverPtr driver,
qemuDomainNetDeviceVportRemove(net); qemuDomainNetDeviceVportRemove(net);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, net); virDomainNetReleaseActualDevice(vm->def, net);
virDomainNetDefFree(net); virDomainNetDefFree(net);
ret = 0; ret = 0;
......
...@@ -3288,6 +3288,7 @@ qemuProcessNotifyNets(virDomainDefPtr def) ...@@ -3288,6 +3288,7 @@ qemuProcessNotifyNets(virDomainDefPtr def)
if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT) if (virDomainNetGetActualType(net) == VIR_DOMAIN_NET_TYPE_DIRECT)
ignore_value(virNetDevMacVLanReserveName(net->ifname, false)); ignore_value(virNetDevMacVLanReserveName(net->ifname, false));
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetNotifyActualDevice(def, net); virDomainNetNotifyActualDevice(def, net);
} }
} }
...@@ -5474,7 +5475,8 @@ qemuProcessNetworkPrepareDevices(virDomainDefPtr def) ...@@ -5474,7 +5475,8 @@ qemuProcessNetworkPrepareDevices(virDomainDefPtr def)
* network's pool of devices, or resolve bridge device name * network's pool of devices, or resolve bridge device name
* to the one defined in the network definition. * to the one defined in the network definition.
*/ */
if (virDomainNetAllocateActualDevice(def, net) < 0) if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK &&
virDomainNetAllocateActualDevice(def, net) < 0)
goto cleanup; goto cleanup;
actualType = virDomainNetGetActualType(net); actualType = virDomainNetGetActualType(net);
...@@ -7321,6 +7323,7 @@ void qemuProcessStop(virQEMUDriverPtr driver, ...@@ -7321,6 +7323,7 @@ void qemuProcessStop(virQEMUDriverPtr driver,
/* kick the device out of the hostdev list too */ /* kick the device out of the hostdev list too */
virDomainNetRemoveHostdev(def, net); virDomainNetRemoveHostdev(def, net);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK)
virDomainNetReleaseActualDevice(vm->def, net); virDomainNetReleaseActualDevice(vm->def, net);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册