2013-01-09 15:11:50 +00:00
|
|
|
/*
|
|
|
|
* virportallocator.c: Allocate & track TCP port allocations
|
|
|
|
*
|
2014-06-24 11:34:17 +00:00
|
|
|
* Copyright (C) 2013-2014 Red Hat, Inc.
|
2013-01-09 15:11:50 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2020-02-23 22:11:34 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-08 12:11:16 +00:00
|
|
|
#include "virsocket.h"
|
2013-01-09 15:11:50 +00:00
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virbitmap.h"
|
|
|
|
#include "virportallocator.h"
|
|
|
|
#include "virthread.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
2013-10-31 11:46:28 +00:00
|
|
|
#include "virstring.h"
|
2020-02-23 22:11:34 +00:00
|
|
|
#include "virutil.h"
|
2013-01-09 15:11:50 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2018-06-14 07:17:10 +00:00
|
|
|
#define VIR_PORT_ALLOCATOR_NUM_PORTS 65536
|
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
typedef struct _virPortAllocator virPortAllocator;
|
2013-01-09 15:11:50 +00:00
|
|
|
struct _virPortAllocator {
|
|
|
|
virObjectLockable parent;
|
2021-03-11 07:16:13 +00:00
|
|
|
virBitmap *bitmap;
|
2018-02-06 09:09:06 +00:00
|
|
|
};
|
2013-01-09 15:11:50 +00:00
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
struct _virPortAllocatorRange {
|
2013-10-31 11:46:28 +00:00
|
|
|
char *name;
|
|
|
|
|
2013-01-09 15:11:50 +00:00
|
|
|
unsigned short start;
|
|
|
|
unsigned short end;
|
|
|
|
};
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virClass *virPortAllocatorClass;
|
|
|
|
static virPortAllocator *virPortAllocatorInstance;
|
2013-01-09 15:11:50 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
virPortAllocatorDispose(void *obj)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocator *pa = obj;
|
2013-01-09 15:11:50 +00:00
|
|
|
|
|
|
|
virBitmapFree(pa->bitmap);
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virPortAllocator *
|
2018-02-06 09:09:06 +00:00
|
|
|
virPortAllocatorNew(void)
|
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocator *pa;
|
2018-02-06 09:09:06 +00:00
|
|
|
|
|
|
|
if (!(pa = virObjectLockableNew(virPortAllocatorClass)))
|
|
|
|
return NULL;
|
|
|
|
|
2020-10-01 15:42:11 +00:00
|
|
|
pa->bitmap = virBitmapNew(VIR_PORT_ALLOCATOR_NUM_PORTS);
|
2018-02-06 09:09:06 +00:00
|
|
|
|
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virPortAllocatorOnceInit(void)
|
2013-01-09 15:11:50 +00:00
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virPortAllocator, virClassForObjectLockable()))
|
2013-01-09 15:11:50 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
if (!(virPortAllocatorInstance = virPortAllocatorNew()))
|
|
|
|
return -1;
|
|
|
|
|
2013-01-09 15:11:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virPortAllocator);
|
2013-01-09 15:11:50 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocatorRange *
|
2018-02-06 09:09:06 +00:00
|
|
|
virPortAllocatorRangeNew(const char *name,
|
|
|
|
unsigned short start,
|
2018-02-06 09:09:09 +00:00
|
|
|
unsigned short end)
|
2013-01-09 15:11:50 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocatorRange *range;
|
2013-01-09 15:11:50 +00:00
|
|
|
|
|
|
|
if (start >= end) {
|
|
|
|
virReportInvalidArg(start, "start port %d must be less than end port %d",
|
|
|
|
start, end);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:13:12 +00:00
|
|
|
range = g_new0(virPortAllocatorRange, 1);
|
2013-01-09 15:11:50 +00:00
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
range->start = start;
|
|
|
|
range->end = end;
|
2019-10-20 11:49:46 +00:00
|
|
|
range->name = g_strdup(name);
|
2013-01-09 15:11:50 +00:00
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocatorRangeFree(virPortAllocatorRange *range)
|
2018-02-06 09:09:06 +00:00
|
|
|
{
|
|
|
|
if (!range)
|
|
|
|
return;
|
|
|
|
|
2021-02-03 19:32:34 +00:00
|
|
|
g_free(range->name);
|
|
|
|
g_free(range);
|
2013-01-09 15:11:50 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
static int
|
|
|
|
virPortAllocatorBindToPort(bool *used,
|
|
|
|
unsigned short port,
|
|
|
|
int family)
|
2013-10-31 14:14:15 +00:00
|
|
|
{
|
2013-10-18 11:52:03 +00:00
|
|
|
struct sockaddr_in6 addr6 = {
|
|
|
|
.sin6_family = AF_INET6,
|
|
|
|
.sin6_port = htons(port),
|
2014-02-20 09:04:30 +00:00
|
|
|
.sin6_addr = in6addr_any
|
2013-10-18 11:52:03 +00:00
|
|
|
};
|
|
|
|
struct sockaddr_in addr4 = {
|
2013-10-31 14:14:15 +00:00
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(port),
|
|
|
|
.sin_addr.s_addr = htonl(INADDR_ANY)
|
|
|
|
};
|
2013-10-18 11:52:03 +00:00
|
|
|
struct sockaddr* addr;
|
|
|
|
size_t addrlen;
|
|
|
|
int v6only = 1;
|
2013-10-31 14:14:15 +00:00
|
|
|
int ret = -1;
|
|
|
|
int fd = -1;
|
2013-10-18 11:52:03 +00:00
|
|
|
bool ipv6 = false;
|
|
|
|
|
|
|
|
if (family == AF_INET6) {
|
|
|
|
addr = (struct sockaddr*)&addr6;
|
|
|
|
addrlen = sizeof(addr6);
|
|
|
|
ipv6 = true;
|
|
|
|
} else if (family == AF_INET) {
|
|
|
|
addr = (struct sockaddr*)&addr4;
|
|
|
|
addrlen = sizeof(addr4);
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, _("Unknown family %d"), family);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-10-31 14:14:15 +00:00
|
|
|
|
|
|
|
*used = false;
|
|
|
|
|
2013-10-18 11:52:03 +00:00
|
|
|
fd = socket(family, SOCK_STREAM, 0);
|
2013-10-31 14:14:15 +00:00
|
|
|
if (fd < 0) {
|
2013-10-18 11:52:03 +00:00
|
|
|
if (errno == EAFNOSUPPORT)
|
|
|
|
return 0;
|
2013-10-31 14:14:15 +00:00
|
|
|
virReportSystemError(errno, "%s", _("Unable to open test socket"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-09-07 15:05:03 +00:00
|
|
|
if (virSetSockReuseAddr(fd, true) < 0)
|
2013-10-31 14:14:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-10-18 11:52:03 +00:00
|
|
|
if (ipv6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&v6only,
|
|
|
|
sizeof(v6only)) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to set IPV6_V6ONLY flag"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(fd, addr, addrlen) < 0) {
|
2013-10-31 14:14:15 +00:00
|
|
|
if (errno == EADDRINUSE) {
|
|
|
|
*used = true;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
virReportSystemError(errno, _("Unable to bind to port %d"), port);
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2020-01-08 12:11:16 +00:00
|
|
|
if (fd != -1)
|
|
|
|
closesocket(fd);
|
2013-10-31 14:14:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virPortAllocator *
|
2018-02-06 09:09:06 +00:00
|
|
|
virPortAllocatorGet(void)
|
|
|
|
{
|
|
|
|
if (virPortAllocatorInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virPortAllocatorInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-02-06 09:09:11 +00:00
|
|
|
virPortAllocatorAcquire(const virPortAllocatorRange *range,
|
2018-02-06 09:09:06 +00:00
|
|
|
unsigned short *port)
|
2013-01-09 15:11:50 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocator *pa = virPortAllocatorGet();
|
2013-01-09 15:11:50 +00:00
|
|
|
|
|
|
|
*port = 0;
|
2018-02-06 09:09:06 +00:00
|
|
|
|
|
|
|
if (!pa)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-09 15:11:50 +00:00
|
|
|
virObjectLock(pa);
|
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
for (i = range->start; i <= range->end && !*port; i++) {
|
2013-10-18 11:52:03 +00:00
|
|
|
bool used = false, v6used = false;
|
2013-01-09 15:11:50 +00:00
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
if (virBitmapIsBitSet(pa->bitmap, i))
|
2013-01-09 15:11:50 +00:00
|
|
|
continue;
|
|
|
|
|
2018-02-06 09:09:09 +00:00
|
|
|
if (virPortAllocatorBindToPort(&v6used, i, AF_INET6) < 0 ||
|
|
|
|
virPortAllocatorBindToPort(&used, i, AF_INET) < 0)
|
|
|
|
goto cleanup;
|
2013-01-09 15:11:50 +00:00
|
|
|
|
2013-10-18 11:52:03 +00:00
|
|
|
if (!used && !v6used) {
|
2013-01-09 15:11:50 +00:00
|
|
|
/* Add port to bitmap of reserved ports */
|
2018-02-06 09:09:06 +00:00
|
|
|
if (virBitmapSetBit(pa->bitmap, i) < 0) {
|
2013-01-09 15:11:50 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
_("Failed to reserve port %zu"), i);
|
2013-01-09 15:11:50 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
*port = i;
|
2013-10-31 11:49:04 +00:00
|
|
|
ret = 0;
|
2013-01-09 15:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-31 11:49:04 +00:00
|
|
|
if (*port == 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find an unused port in range '%s' (%d-%d)"),
|
2018-02-06 09:09:06 +00:00
|
|
|
range->name, range->start, range->end);
|
2013-10-31 11:49:04 +00:00
|
|
|
}
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-09 15:11:50 +00:00
|
|
|
virObjectUnlock(pa);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
int
|
2018-02-06 09:09:08 +00:00
|
|
|
virPortAllocatorRelease(unsigned short port)
|
2013-01-09 15:11:50 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocator *pa = virPortAllocatorGet();
|
2018-02-06 09:09:06 +00:00
|
|
|
|
|
|
|
if (!pa)
|
|
|
|
return -1;
|
2013-07-04 19:16:57 +00:00
|
|
|
|
|
|
|
if (!port)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-09 15:11:50 +00:00
|
|
|
virObjectLock(pa);
|
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
if (virBitmapClearBit(pa->bitmap, port) < 0) {
|
2013-01-09 15:11:50 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to release port %d"),
|
|
|
|
port);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-09 15:11:50 +00:00
|
|
|
virObjectUnlock(pa);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-24 11:34:17 +00:00
|
|
|
|
2018-02-06 09:09:06 +00:00
|
|
|
int
|
2018-02-06 09:09:10 +00:00
|
|
|
virPortAllocatorSetUsed(unsigned short port)
|
2014-06-24 11:34:17 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2021-03-11 07:16:13 +00:00
|
|
|
virPortAllocator *pa = virPortAllocatorGet();
|
2018-02-06 09:09:06 +00:00
|
|
|
|
|
|
|
if (!pa)
|
|
|
|
return -1;
|
2014-06-24 11:34:17 +00:00
|
|
|
|
2019-01-21 14:49:23 +00:00
|
|
|
if (!port)
|
|
|
|
return 0;
|
|
|
|
|
2014-06-24 11:34:17 +00:00
|
|
|
virObjectLock(pa);
|
|
|
|
|
2018-02-06 09:09:10 +00:00
|
|
|
if (virBitmapIsBitSet(pa->bitmap, port) ||
|
|
|
|
virBitmapSetBit(pa->bitmap, port) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to reserve port %d"), port);
|
|
|
|
goto cleanup;
|
2014-06-24 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(pa);
|
|
|
|
return ret;
|
|
|
|
}
|