xref: /freebsd/sys/dev/hyperv/utilities/hv_kvp.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 
29 #ifndef _KVP_H
30 #define _KVP_H
31 /*
32  * An implementation of HyperV key value pair (KVP) functionality for FreeBSD
33  *
34  */
35 
36 /*
37  * Maximum value size - used for both key names and value data, and includes
38  * any applicable NULL terminators.
39  *
40  * Note:  This limit is somewhat arbitrary, but falls easily within what is
41  * supported for all native guests (back to Win 2000) and what is reasonable
42  * for the IC KVP exchange functionality.  Note that Windows Me/98/95 are
43  * limited to 255 character key names.
44  *
45  * MSDN recommends not storing data values larger than 2048 bytes in the
46  * registry.
47  *
48  * Note:  This value is used in defining the KVP exchange message - this value
49  * cannot be modified without affecting the message size and compatibility.
50  */
51 
52 /*
53  * bytes, including any null terminators
54  */
55 #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE    (2048)
56 
57 
58 /*
59  * Maximum key size - the registry limit for the length of an entry name
60  * is 256 characters, including the null terminator
61  */
62 #define HV_KVP_EXCHANGE_MAX_KEY_SIZE    (512)
63 
64 
65 /*
66  * In FreeBSD, we implement the KVP functionality in two components:
67  * 1) The kernel component which is packaged as part of the hv_utils driver
68  * is responsible for communicating with the host and responsible for
69  * implementing the host/guest protocol. 2) A user level daemon that is
70  * responsible for data gathering.
71  *
72  * Host/Guest Protocol: The host iterates over an index and expects the guest
73  * to assign a key name to the index and also return the value corresponding to
74  * the key. The host will have atmost one KVP transaction outstanding at any
75  * given point in time. The host side iteration stops when the guest returns
76  * an error. Microsoft has specified the following mapping of key names to
77  * host specified index:
78  *
79  *  Index		Key Name
80  *	0		FullyQualifiedDomainName
81  *	1		IntegrationServicesVersion
82  *	2		NetworkAddressIPv4
83  *	3		NetworkAddressIPv6
84  *	4		OSBuildNumber
85  *	5		OSName
86  *	6		OSMajorVersion
87  *	7		OSMinorVersion
88  *	8		OSVersion
89  *	9		ProcessorArchitecture
90  *
91  * The Windows host expects the Key Name and Key Value to be encoded in utf16.
92  *
93  * Guest Kernel/KVP Daemon Protocol: As noted earlier, we implement all of the
94  * data gathering functionality in a user mode daemon. The user level daemon
95  * is also responsible for binding the key name to the index as well. The
96  * kernel and user-level daemon communicate using a connector channel.
97  *
98  * The user mode component first registers with the
99  * the kernel component. Subsequently, the kernel component requests, data
100  * for the specified keys. In response to this message the user mode component
101  * fills in the value corresponding to the specified key. We overload the
102  * sequence field in the cn_msg header to define our KVP message types.
103  *
104  *
105  * The kernel component simply acts as a conduit for communication between the
106  * Windows host and the user-level daemon. The kernel component passes up the
107  * index received from the Host to the user-level daemon. If the index is
108  * valid (supported), the corresponding key as well as its
109  * value (both are strings) is returned. If the index is invalid
110  * (not supported), a NULL key string is returned.
111  */
112 
113 
114 /*
115  * Registry value types.
116  */
117 #define HV_REG_SZ     1
118 #define HV_REG_U32    4
119 #define HV_REG_U64    8
120 
121 
122 /*
123  * Daemon code supporting IP injection.
124  */
125 #define HV_KVP_OP_REGISTER    4
126 
127 
128 enum hv_kvp_exchg_op {
129 	HV_KVP_OP_GET = 0,
130 	HV_KVP_OP_SET,
131 	HV_KVP_OP_DELETE,
132 	HV_KVP_OP_ENUMERATE,
133 	HV_KVP_OP_GET_IP_INFO,
134 	HV_KVP_OP_SET_IP_INFO,
135 	HV_KVP_OP_COUNT /* Number of operations, must be last. */
136 };
137 
138 enum hv_kvp_exchg_pool {
139 	HV_KVP_POOL_EXTERNAL = 0,
140 	HV_KVP_POOL_GUEST,
141 	HV_KVP_POOL_AUTO,
142 	HV_KVP_POOL_AUTO_EXTERNAL,
143 	HV_KVP_POOL_AUTO_INTERNAL,
144 	HV_KVP_POOL_COUNT /* Number of pools, must be last. */
145 };
146 
147 #define ADDR_FAMILY_NONE                 0x00
148 #define ADDR_FAMILY_IPV4                 0x01
149 #define ADDR_FAMILY_IPV6                 0x02
150 
151 #define MAX_ADAPTER_ID_SIZE              128
152 #define MAX_IP_ADDR_SIZE                 1024
153 #define MAX_GATEWAY_SIZE                 512
154 
155 
156 struct hv_kvp_ipaddr_value {
157 	uint16_t adapter_id[MAX_ADAPTER_ID_SIZE];
158 	uint8_t  addr_family;
159 	uint8_t  dhcp_enabled;
160 	uint16_t ip_addr[MAX_IP_ADDR_SIZE];
161 	uint16_t sub_net[MAX_IP_ADDR_SIZE];
162 	uint16_t gate_way[MAX_GATEWAY_SIZE];
163 	uint16_t dns_addr[MAX_IP_ADDR_SIZE];
164 }__attribute__((packed));
165 
166 struct hv_kvp_hdr {
167 	uint8_t                 operation;
168 	uint8_t                 pool;
169 	uint16_t                pad;
170 } __attribute__((packed));
171 
172 struct hv_kvp_exchg_msg_value {
173 	uint32_t value_type;
174 	uint32_t key_size;
175 	uint32_t value_size;
176 	uint8_t  key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
177 	union {
178 		uint8_t  value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
179 		uint32_t value_u32;
180 		uint64_t value_u64;
181 	} msg_value;
182 } __attribute__((packed));
183 
184 struct hv_kvp_msg_enumerate {
185 	uint32_t index;
186 	struct hv_kvp_exchg_msg_value data;
187 } __attribute__((packed));
188 
189 struct hv_kvp_msg_get {
190 	struct hv_kvp_exchg_msg_value data;
191 } __attribute__((packed));
192 
193 struct hv_kvp_msg_set {
194 	struct hv_kvp_exchg_msg_value data;
195 } __attribute__((packed));
196 
197 struct hv_kvp_msg_delete {
198 	uint32_t key_size;
199 	uint8_t key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
200 } __attribute__((packed));
201 
202 struct hv_kvp_register {
203 	uint8_t version[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
204 } __attribute__((packed));
205 
206 struct hv_kvp_msg {
207 	union {
208 		struct hv_kvp_hdr kvp_hdr;
209 		uint32_t error;
210 	} hdr;
211 	union {
212 		struct hv_kvp_msg_get		kvp_get;
213 		struct hv_kvp_msg_set		kvp_set;
214 		struct hv_kvp_msg_delete	kvp_delete;
215 		struct hv_kvp_msg_enumerate	kvp_enum_data;
216 		struct hv_kvp_ipaddr_value	kvp_ip_val;
217 		struct hv_kvp_register		kvp_register;
218 	} body;
219 } __attribute__((packed));
220 
221 struct hv_kvp_ip_msg {
222 	uint8_t operation;
223 	uint8_t pool;
224 	struct hv_kvp_ipaddr_value      kvp_ip_val;
225 } __attribute__((packed));
226 
227 #endif /* _KVP_H */
228