Blame common/vsock.c

Packit Service 3749ba
/*
Packit Service 3749ba
 * Copyright © 2020 Amazon.com, Inc. or its affiliates.
Packit Service 3749ba
 *
Packit Service 3749ba
 * Redistribution and use in source and binary forms, with or without
Packit Service 3749ba
 * modification, are permitted provided that the following conditions
Packit Service 3749ba
 * are met:
Packit Service 3749ba
 *
Packit Service 3749ba
 *     * Redistributions of source code must retain the above
Packit Service 3749ba
 *       copyright notice, this list of conditions and the
Packit Service 3749ba
 *       following disclaimer.
Packit Service 3749ba
 *     * Redistributions in binary form must reproduce the
Packit Service 3749ba
 *       above copyright notice, this list of conditions and
Packit Service 3749ba
 *       the following disclaimer in the documentation and/or
Packit Service 3749ba
 *       other materials provided with the distribution.
Packit Service 3749ba
 *     * The names of contributors to this software may not be
Packit Service 3749ba
 *       used to endorse or promote products derived from this
Packit Service 3749ba
 *       software without specific prior written permission.
Packit Service 3749ba
 *
Packit Service 3749ba
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 3749ba
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 3749ba
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 3749ba
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 3749ba
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 3749ba
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
Packit Service 3749ba
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Packit Service 3749ba
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Packit Service 3749ba
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit Service 3749ba
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
Packit Service 3749ba
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
Packit Service 3749ba
 * DAMAGE.
Packit Service 3749ba
 *
Packit Service 3749ba
 * Author: David Woodhouse <dwmw2@infradead.org>
Packit Service 3749ba
 */
Packit Service 3749ba
Packit Service 3749ba
#include "config.h"
Packit Service 3749ba
Packit Service 3749ba
#include "vsock.h"
Packit Service 3749ba
Packit Service 3749ba
#include <limits.h>
Packit Service 3749ba
#include <string.h>
Packit Service 3749ba
#include <stdlib.h>
Packit Service 3749ba
Packit Service 3749ba
#include <sys/types.h>
Packit Service 3749ba
#include <sys/stat.h>
Packit Service 3749ba
#include <fcntl.h>
Packit Service 3749ba
Packit Service 3749ba
#ifdef HAVE_VSOCK
Packit Service 3749ba
#include <sys/socket.h>
Packit Service 3749ba
#include <linux/vm_sockets.h>
Packit Service 3749ba
#include <sys/ioctl.h>
Packit Service 3749ba
#endif
Packit Service 3749ba
Packit Service 3749ba
/* This generic parsing utility doesn't actually require the
Packit Service 3749ba
 * vm_sockets.h header and thus doesn't require conditional
Packit Service 3749ba
 * compiliation... except for this one definition. */
Packit Service 3749ba
#ifndef VMADDR_CID_ANY
Packit Service 3749ba
#define VMADDR_CID_ANY -1U
Packit Service 3749ba
#endif
Packit Service 3749ba
Packit Service 3749ba
bool
Packit Service 3749ba
p11_vsock_parse_addr (const char *target,
Packit Service 3749ba
		      unsigned int *cid,
Packit Service 3749ba
		      unsigned int *port)
Packit Service 3749ba
{
Packit Service 3749ba
	bool cid_found = false;
Packit Service 3749ba
	bool port_found = false;
Packit Service 3749ba
	unsigned long val;
Packit Service 3749ba
	char *endptr;
Packit Service 3749ba
Packit Service 3749ba
	while (*target) {
Packit Service 3749ba
		if (strncmp (target, "cid=", 4) == 0) {
Packit Service 3749ba
			val = strtoul(target + 4, &endptr, 0);
Packit Service 3749ba
			if (val > UINT_MAX || endptr == target + 4)
Packit Service 3749ba
				return false;
Packit Service 3749ba
			*cid = val;
Packit Service 3749ba
			cid_found = true;
Packit Service 3749ba
		} else if (strncmp (target, "port=", 5) == 0) {
Packit Service 3749ba
			val = strtoul (target + 5, &endptr, 0);
Packit Service 3749ba
			if (val > UINT_MAX || endptr == target + 5)
Packit Service 3749ba
				return false;
Packit Service 3749ba
			*port = val;
Packit Service 3749ba
			port_found = true;
Packit Service 3749ba
		} else {
Packit Service 3749ba
			return false;
Packit Service 3749ba
		}
Packit Service 3749ba
Packit Service 3749ba
		target = endptr;
Packit Service 3749ba
		if (*target == ';')
Packit Service 3749ba
			target++;
Packit Service 3749ba
		else if (*target)
Packit Service 3749ba
			return false;
Packit Service 3749ba
	}
Packit Service 3749ba
Packit Service 3749ba
	/* Port is mandatory */
Packit Service 3749ba
	if (!port_found)
Packit Service 3749ba
		return false;
Packit Service 3749ba
Packit Service 3749ba
	/* CID is optional, defaulting to VMADDR_CID_ANY */
Packit Service 3749ba
	if (!cid_found)
Packit Service 3749ba
		*cid = VMADDR_CID_ANY;
Packit Service 3749ba
Packit Service 3749ba
	return true;
Packit Service 3749ba
}
Packit Service 3749ba
Packit Service 3749ba
bool
Packit Service 3749ba
p11_vsock_get_local_cid (unsigned int *cid)
Packit Service 3749ba
{
Packit Service 3749ba
#ifndef HAVE_VSOCK
Packit Service 3749ba
	return false;
Packit Service 3749ba
#else
Packit Service 3749ba
	int fd = open ("/dev/vsock", O_RDONLY);
Packit Service 3749ba
	int rc;
Packit Service 3749ba
Packit Service 3749ba
	if (fd == -1)
Packit Service 3749ba
		return false;
Packit Service 3749ba
Packit Service 3749ba
	rc = ioctl (fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, cid, sizeof(*cid));
Packit Service 3749ba
	close (fd);
Packit Service 3749ba
Packit Service 3749ba
	return (rc == 0);
Packit Service 3749ba
#endif
Packit Service 3749ba
}