| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/ioctl.h> |
| 5 |
|
| 6 |
#include "sd-hwdb.h" |
| 7 |
|
| 8 |
#include "fd-util.h" |
| 9 |
#include "fileio.h" |
| 10 |
#include "log.h" |
| 11 |
#include "parse-util.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "vsock-util.h" |
| 14 |
|
| 15 |
static int smbios_get_modalias(char **ret) { |
| 16 |
int r; |
| 17 |
|
| 18 |
assert(ret); |
| 19 |
|
| 20 |
_cleanup_free_ char *modalias = NULL; |
| 21 |
r = read_virtual_file("/sys/devices/virtual/dmi/id/modalias", SIZE_MAX, &modalias, /* ret_size= */ NULL); |
| 22 |
if (r < 0) |
| 23 |
return r; |
| 24 |
|
| 25 |
truncate_nl(modalias); |
| 26 |
|
| 27 |
*ret = TAKE_PTR(modalias); |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
static int smbios_get_accepts_any(void) { |
| 32 |
static int accepts_any = -1; |
| 33 |
int r; |
| 34 |
|
| 35 |
if (accepts_any >= 0) |
| 36 |
return accepts_any; |
| 37 |
|
| 38 |
_cleanup_free_ char *modalias = NULL; |
| 39 |
r = smbios_get_modalias(&modalias); |
| 40 |
if (r == -ENOENT) |
| 41 |
return (accepts_any = false); |
| 42 |
if (r < 0) |
| 43 |
return log_debug_errno(r, "Failed to read DMI modalias: %m"); |
| 44 |
|
| 45 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 46 |
r = sd_hwdb_new(&hwdb); |
| 47 |
if (r < 0) |
| 48 |
return log_debug_errno(r, "Failed to open hwdb: %m"); |
| 49 |
|
| 50 |
const char *value; |
| 51 |
r = sd_hwdb_get(hwdb, modalias, "VSOCK_ACCEPT_VMADDR_CID_ANY", &value); |
| 52 |
if (r < 0) { |
| 53 |
if (r != -ENOENT) |
| 54 |
log_debug_errno(r, "Failed to get VSOCK_ACCEPT_VMADDR_CID_ANY, ignoring: %m"); |
| 55 |
return (accepts_any = false); |
| 56 |
} |
| 57 |
log_debug("VSOCK_ACCEPT_VMADDR_CID_ANY: %s", value); |
| 58 |
|
| 59 |
r = parse_boolean(value); |
| 60 |
if (r < 0) { |
| 61 |
log_debug_errno(r, "Failed to parse VSOCK_ACCEPT_VMADDR_CID_ANY, ignoring: %m"); |
| 62 |
return (accepts_any = false); |
| 63 |
} |
| 64 |
|
| 65 |
return (accepts_any = r); |
| 66 |
} |
| 67 |
|
| 68 |
int vsock_get_local_cid(unsigned *ret) { |
| 69 |
_cleanup_close_ int vsock_fd = -EBADF; |
| 70 |
|
| 71 |
vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC); |
| 72 |
if (vsock_fd < 0) |
| 73 |
return log_debug_errno(errno, "Failed to open %s: %m", "/dev/vsock"); |
| 74 |
|
| 75 |
unsigned tmp; |
| 76 |
if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &tmp) < 0) |
| 77 |
return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m"); |
| 78 |
log_debug("Local AF_VSOCK CID: %u", tmp); |
| 79 |
|
| 80 |
/* If ret == NULL, we just want to check if AF_VSOCK is available, so accept any |
| 81 |
* address. Otherwise, filter out special addresses that cannot be used to identify |
| 82 |
* _this_ machine from the outside. */ |
| 83 |
if (ret && |
| 84 |
(IN_SET(tmp, VMADDR_CID_LOCAL, VMADDR_CID_HOST) || |
| 85 |
(tmp == VMADDR_CID_ANY && smbios_get_accepts_any() <= 0))) |
| 86 |
return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), |
| 87 |
"IOCTL_VM_SOCKETS_GET_LOCAL_CID returned special value (%u), ignoring.", tmp); |
| 88 |
|
| 89 |
if (ret) |
| 90 |
*ret = tmp; |
| 91 |
return 0; |
| 92 |
} |