1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2014,2016 Microsoft Corp. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _KVP_H 32 #define _KVP_H 33 /* 34 * An implementation of HyperV key value pair (KVP) functionality for FreeBSD 35 * 36 */ 37 38 /* 39 * Maximum value size - used for both key names and value data, and includes 40 * any applicable NULL terminators. 41 * 42 * Note: This limit is somewhat arbitrary, but falls easily within what is 43 * supported for all native guests (back to Win 2000) and what is reasonable 44 * for the IC KVP exchange functionality. Note that Windows Me/98/95 are 45 * limited to 255 character key names. 46 * 47 * MSDN recommends not storing data values larger than 2048 bytes in the 48 * registry. 49 * 50 * Note: This value is used in defining the KVP exchange message - this value 51 * cannot be modified without affecting the message size and compatibility. 52 */ 53 54 /* 55 * bytes, including any null terminators 56 */ 57 #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE (2048) 58 59 60 /* 61 * Maximum key size - the registry limit for the length of an entry name 62 * is 256 characters, including the null terminator 63 */ 64 #define HV_KVP_EXCHANGE_MAX_KEY_SIZE (512) 65 66 67 /* 68 * In FreeBSD, we implement the KVP functionality in two components: 69 * 1) The kernel component which is packaged as part of the hv_utils driver 70 * is responsible for communicating with the host and responsible for 71 * implementing the host/guest protocol. 2) A user level daemon that is 72 * responsible for data gathering. 73 * 74 * Host/Guest Protocol: The host iterates over an index and expects the guest 75 * to assign a key name to the index and also return the value corresponding to 76 * the key. The host will have atmost one KVP transaction outstanding at any 77 * given point in time. The host side iteration stops when the guest returns 78 * an error. Microsoft has specified the following mapping of key names to 79 * host specified index: 80 * 81 * Index Key Name 82 * 0 FullyQualifiedDomainName 83 * 1 IntegrationServicesVersion 84 * 2 NetworkAddressIPv4 85 * 3 NetworkAddressIPv6 86 * 4 OSBuildNumber 87 * 5 OSName 88 * 6 OSMajorVersion 89 * 7 OSMinorVersion 90 * 8 OSVersion 91 * 9 ProcessorArchitecture 92 * 93 * The Windows host expects the Key Name and Key Value to be encoded in utf16. 94 * 95 * Guest Kernel/KVP Daemon Protocol: As noted earlier, we implement all of the 96 * data gathering functionality in a user mode daemon. The user level daemon 97 * is also responsible for binding the key name to the index as well. The 98 * kernel and user-level daemon communicate using a connector channel. 99 * 100 * The user mode component first registers with the 101 * the kernel component. Subsequently, the kernel component requests, data 102 * for the specified keys. In response to this message the user mode component 103 * fills in the value corresponding to the specified key. We overload the 104 * sequence field in the cn_msg header to define our KVP message types. 105 * 106 * 107 * The kernel component simply acts as a conduit for communication between the 108 * Windows host and the user-level daemon. The kernel component passes up the 109 * index received from the Host to the user-level daemon. If the index is 110 * valid (supported), the corresponding key as well as its 111 * value (both are strings) is returned. If the index is invalid 112 * (not supported), a NULL key string is returned. 113 */ 114 115 116 /* 117 * Registry value types. 118 */ 119 #define HV_REG_SZ 1 120 #define HV_REG_U32 4 121 #define HV_REG_U64 8 122 123 124 /* 125 * Daemon code supporting IP injection. 126 */ 127 #define HV_KVP_OP_REGISTER 4 128 129 130 enum hv_kvp_exchg_op { 131 HV_KVP_OP_GET = 0, 132 HV_KVP_OP_SET, 133 HV_KVP_OP_DELETE, 134 HV_KVP_OP_ENUMERATE, 135 HV_KVP_OP_GET_IP_INFO, 136 HV_KVP_OP_SET_IP_INFO, 137 HV_KVP_OP_COUNT /* Number of operations, must be last. */ 138 }; 139 140 enum hv_kvp_exchg_pool { 141 HV_KVP_POOL_EXTERNAL = 0, 142 HV_KVP_POOL_GUEST, 143 HV_KVP_POOL_AUTO, 144 HV_KVP_POOL_AUTO_EXTERNAL, 145 HV_KVP_POOL_AUTO_INTERNAL, 146 HV_KVP_POOL_COUNT /* Number of pools, must be last. */ 147 }; 148 149 #define ADDR_FAMILY_NONE 0x00 150 #define ADDR_FAMILY_IPV4 0x01 151 #define ADDR_FAMILY_IPV6 0x02 152 153 #define MAX_ADAPTER_ID_SIZE 128 154 #define MAX_IP_ADDR_SIZE 1024 155 #define MAX_GATEWAY_SIZE 512 156 157 158 struct hv_kvp_ipaddr_value { 159 uint16_t adapter_id[MAX_ADAPTER_ID_SIZE]; 160 uint8_t addr_family; 161 uint8_t dhcp_enabled; 162 uint16_t ip_addr[MAX_IP_ADDR_SIZE]; 163 uint16_t sub_net[MAX_IP_ADDR_SIZE]; 164 uint16_t gate_way[MAX_GATEWAY_SIZE]; 165 uint16_t dns_addr[MAX_IP_ADDR_SIZE]; 166 }__attribute__((packed)); 167 168 struct hv_kvp_hdr { 169 uint8_t operation; 170 uint8_t pool; 171 uint16_t pad; 172 } __attribute__((packed)); 173 174 struct hv_kvp_exchg_msg_value { 175 uint32_t value_type; 176 uint32_t key_size; 177 uint32_t value_size; 178 uint8_t key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; 179 union { 180 uint8_t value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; 181 uint32_t value_u32; 182 uint64_t value_u64; 183 } msg_value; 184 } __attribute__((packed)); 185 186 struct hv_kvp_msg_enumerate { 187 uint32_t index; 188 struct hv_kvp_exchg_msg_value data; 189 } __attribute__((packed)); 190 191 struct hv_kvp_msg_get { 192 struct hv_kvp_exchg_msg_value data; 193 } __attribute__((packed)); 194 195 struct hv_kvp_msg_set { 196 struct hv_kvp_exchg_msg_value data; 197 } __attribute__((packed)); 198 199 struct hv_kvp_msg_delete { 200 uint32_t key_size; 201 uint8_t key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; 202 } __attribute__((packed)); 203 204 struct hv_kvp_register { 205 uint8_t version[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; 206 } __attribute__((packed)); 207 208 struct hv_kvp_msg { 209 union { 210 struct hv_kvp_hdr kvp_hdr; 211 uint32_t error; 212 } hdr; 213 union { 214 struct hv_kvp_msg_get kvp_get; 215 struct hv_kvp_msg_set kvp_set; 216 struct hv_kvp_msg_delete kvp_delete; 217 struct hv_kvp_msg_enumerate kvp_enum_data; 218 struct hv_kvp_ipaddr_value kvp_ip_val; 219 struct hv_kvp_register kvp_register; 220 } body; 221 } __attribute__((packed)); 222 223 struct hv_kvp_ip_msg { 224 uint8_t operation; 225 uint8_t pool; 226 struct hv_kvp_ipaddr_value kvp_ip_val; 227 } __attribute__((packed)); 228 229 #endif /* _KVP_H */ 230