Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of key value pair (KVP) functionality for Linux. |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2010, Novell, Inc. |
| 6 | * Author : K. Y. Srinivasan <ksrinivasan@novell.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 15 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 16 | * details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/nls.h> |
| 27 | #include <linux/connector.h> |
| 28 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 30 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 31 | |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Pre win8 version numbers used in ws2008 and ws 2008 r2 (win7) |
| 34 | */ |
| 35 | #define WIN7_SRV_MAJOR 3 |
| 36 | #define WIN7_SRV_MINOR 0 |
| 37 | #define WIN7_SRV_MAJOR_MINOR (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR) |
| 38 | |
| 39 | #define WIN8_SRV_MAJOR 4 |
| 40 | #define WIN8_SRV_MINOR 0 |
| 41 | #define WIN8_SRV_MAJOR_MINOR (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Global state maintained for transaction that is being processed. |
| 45 | * Note that only one transaction can be active at any point in time. |
| 46 | * |
| 47 | * This state is set when we receive a request from the host; we |
| 48 | * cleanup this state when the transaction is completed - when we respond |
| 49 | * to the host with the key value. |
| 50 | */ |
| 51 | |
| 52 | static struct { |
| 53 | bool active; /* transaction status - active or not */ |
| 54 | int recv_len; /* number of bytes received. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 55 | struct hv_kvp_msg *kvp_msg; /* current message */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 56 | struct vmbus_channel *recv_channel; /* chn we got the request */ |
| 57 | u64 recv_req_id; /* request ID. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 58 | void *kvp_context; /* for the channel callback */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 59 | } kvp_transaction; |
| 60 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Before we can accept KVP messages from the host, we need |
| 63 | * to handshake with the user level daemon. This state tracks |
| 64 | * if we are in the handshake phase. |
| 65 | */ |
| 66 | static bool in_hand_shake = true; |
| 67 | |
| 68 | /* |
| 69 | * This state maintains the version number registered by the daemon. |
| 70 | */ |
| 71 | static int dm_reg_value; |
| 72 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 73 | static void kvp_send_key(struct work_struct *dummy); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 74 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 75 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 76 | static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 77 | static void kvp_work_func(struct work_struct *dummy); |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 78 | static void kvp_register(int); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 79 | |
| 80 | static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 81 | static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 82 | |
| 83 | static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL }; |
| 84 | static const char kvp_name[] = "kvp_kernel_module"; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 85 | static u8 *recv_buffer; |
| 86 | /* |
| 87 | * Register the kernel component with the user-level daemon. |
| 88 | * As part of this registration, pass the LIC version number. |
Olaf Hering | cfc2599 | 2013-05-29 11:29:07 +0200 | [diff] [blame] | 89 | * This number has no meaning, it satisfies the registration protocol. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 90 | */ |
Olaf Hering | cfc2599 | 2013-05-29 11:29:07 +0200 | [diff] [blame] | 91 | #define HV_DRV_VERSION "3.1" |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 92 | |
| 93 | static void |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 94 | kvp_register(int reg_value) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 95 | { |
| 96 | |
| 97 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 98 | struct hv_kvp_msg *kvp_msg; |
| 99 | char *version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 100 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 101 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 102 | |
| 103 | if (msg) { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 104 | kvp_msg = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame] | 105 | version = kvp_msg->body.kvp_register.version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 106 | msg->id.idx = CN_KVP_IDX; |
| 107 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 108 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 109 | kvp_msg->kvp_hdr.operation = reg_value; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 110 | strcpy(version, HV_DRV_VERSION); |
| 111 | msg->len = sizeof(struct hv_kvp_msg); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 112 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 113 | kfree(msg); |
| 114 | } |
| 115 | } |
| 116 | static void |
| 117 | kvp_work_func(struct work_struct *dummy) |
| 118 | { |
| 119 | /* |
| 120 | * If the timer fires, the user-mode component has not responded; |
| 121 | * process the pending transaction. |
| 122 | */ |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 123 | kvp_respond_to_host(NULL, HV_E_FAIL); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 126 | static int kvp_handle_handshake(struct hv_kvp_msg *msg) |
| 127 | { |
| 128 | int ret = 1; |
| 129 | |
| 130 | switch (msg->kvp_hdr.operation) { |
| 131 | case KVP_OP_REGISTER: |
| 132 | dm_reg_value = KVP_OP_REGISTER; |
| 133 | pr_info("KVP: IP injection functionality not available\n"); |
| 134 | pr_info("KVP: Upgrade the KVP daemon\n"); |
| 135 | break; |
| 136 | case KVP_OP_REGISTER1: |
| 137 | dm_reg_value = KVP_OP_REGISTER1; |
| 138 | break; |
| 139 | default: |
| 140 | pr_info("KVP: incompatible daemon\n"); |
| 141 | pr_info("KVP: KVP version: %d, Daemon version: %d\n", |
| 142 | KVP_OP_REGISTER1, msg->kvp_hdr.operation); |
| 143 | ret = 0; |
| 144 | } |
| 145 | |
| 146 | if (ret) { |
| 147 | /* |
| 148 | * We have a compatible daemon; complete the handshake. |
| 149 | */ |
| 150 | pr_info("KVP: user-mode registering done.\n"); |
| 151 | kvp_register(dm_reg_value); |
| 152 | kvp_transaction.active = false; |
| 153 | if (kvp_transaction.kvp_context) |
| 154 | hv_kvp_onchannelcallback(kvp_transaction.kvp_context); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 160 | /* |
| 161 | * Callback when data is received from user mode. |
| 162 | */ |
| 163 | |
| 164 | static void |
| 165 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
| 166 | { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 167 | struct hv_kvp_msg *message; |
| 168 | struct hv_kvp_msg_enumerate *data; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 169 | int error = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 170 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 171 | message = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * If we are negotiating the version information |
| 175 | * with the daemon; handle that first. |
| 176 | */ |
| 177 | |
| 178 | if (in_hand_shake) { |
| 179 | if (kvp_handle_handshake(message)) |
| 180 | in_hand_shake = false; |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Based on the version of the daemon, we propagate errors from the |
| 186 | * daemon differently. |
| 187 | */ |
| 188 | |
| 189 | data = &message->body.kvp_enum_data; |
| 190 | |
| 191 | switch (dm_reg_value) { |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 192 | case KVP_OP_REGISTER: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 193 | /* |
| 194 | * Null string is used to pass back error condition. |
| 195 | */ |
| 196 | if (data->data.key[0] == 0) |
| 197 | error = HV_S_CONT; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 198 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 199 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 200 | case KVP_OP_REGISTER1: |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 201 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 202 | * We use the message header information from |
| 203 | * the user level daemon to transmit errors. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 204 | */ |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 205 | error = message->error; |
| 206 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 207 | } |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Complete the transaction by forwarding the key value |
| 211 | * to the host. But first, cancel the timeout. |
| 212 | */ |
| 213 | if (cancel_delayed_work_sync(&kvp_work)) |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 214 | kvp_respond_to_host(message, error); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 215 | } |
| 216 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 217 | |
| 218 | static int process_ob_ipinfo(void *in_msg, void *out_msg, int op) |
| 219 | { |
| 220 | struct hv_kvp_msg *in = in_msg; |
| 221 | struct hv_kvp_ip_msg *out = out_msg; |
| 222 | int len; |
| 223 | |
| 224 | switch (op) { |
| 225 | case KVP_OP_GET_IP_INFO: |
| 226 | /* |
| 227 | * Transform all parameters into utf16 encoding. |
| 228 | */ |
| 229 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr, |
| 230 | strlen((char *)in->body.kvp_ip_val.ip_addr), |
| 231 | UTF16_HOST_ENDIAN, |
| 232 | (wchar_t *)out->kvp_ip_val.ip_addr, |
| 233 | MAX_IP_ADDR_SIZE); |
| 234 | if (len < 0) |
| 235 | return len; |
| 236 | |
| 237 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net, |
| 238 | strlen((char *)in->body.kvp_ip_val.sub_net), |
| 239 | UTF16_HOST_ENDIAN, |
| 240 | (wchar_t *)out->kvp_ip_val.sub_net, |
| 241 | MAX_IP_ADDR_SIZE); |
| 242 | if (len < 0) |
| 243 | return len; |
| 244 | |
| 245 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way, |
| 246 | strlen((char *)in->body.kvp_ip_val.gate_way), |
| 247 | UTF16_HOST_ENDIAN, |
| 248 | (wchar_t *)out->kvp_ip_val.gate_way, |
| 249 | MAX_GATEWAY_SIZE); |
| 250 | if (len < 0) |
| 251 | return len; |
| 252 | |
| 253 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr, |
| 254 | strlen((char *)in->body.kvp_ip_val.dns_addr), |
| 255 | UTF16_HOST_ENDIAN, |
| 256 | (wchar_t *)out->kvp_ip_val.dns_addr, |
| 257 | MAX_IP_ADDR_SIZE); |
| 258 | if (len < 0) |
| 259 | return len; |
| 260 | |
| 261 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id, |
| 262 | strlen((char *)in->body.kvp_ip_val.adapter_id), |
| 263 | UTF16_HOST_ENDIAN, |
| 264 | (wchar_t *)out->kvp_ip_val.adapter_id, |
| 265 | MAX_IP_ADDR_SIZE); |
| 266 | if (len < 0) |
| 267 | return len; |
| 268 | |
| 269 | out->kvp_ip_val.dhcp_enabled = |
| 270 | in->body.kvp_ip_val.dhcp_enabled; |
K. Y. Srinivasan | a500e0e | 2012-09-04 17:54:13 -0700 | [diff] [blame] | 271 | out->kvp_ip_val.addr_family = |
| 272 | in->body.kvp_ip_val.addr_family; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static void process_ib_ipinfo(void *in_msg, void *out_msg, int op) |
| 279 | { |
| 280 | struct hv_kvp_ip_msg *in = in_msg; |
| 281 | struct hv_kvp_msg *out = out_msg; |
| 282 | |
| 283 | switch (op) { |
| 284 | case KVP_OP_SET_IP_INFO: |
| 285 | /* |
| 286 | * Transform all parameters into utf8 encoding. |
| 287 | */ |
| 288 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr, |
| 289 | MAX_IP_ADDR_SIZE, |
| 290 | UTF16_LITTLE_ENDIAN, |
| 291 | (__u8 *)out->body.kvp_ip_val.ip_addr, |
| 292 | MAX_IP_ADDR_SIZE); |
| 293 | |
| 294 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net, |
| 295 | MAX_IP_ADDR_SIZE, |
| 296 | UTF16_LITTLE_ENDIAN, |
| 297 | (__u8 *)out->body.kvp_ip_val.sub_net, |
| 298 | MAX_IP_ADDR_SIZE); |
| 299 | |
| 300 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way, |
| 301 | MAX_GATEWAY_SIZE, |
| 302 | UTF16_LITTLE_ENDIAN, |
| 303 | (__u8 *)out->body.kvp_ip_val.gate_way, |
| 304 | MAX_GATEWAY_SIZE); |
| 305 | |
| 306 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr, |
| 307 | MAX_IP_ADDR_SIZE, |
| 308 | UTF16_LITTLE_ENDIAN, |
| 309 | (__u8 *)out->body.kvp_ip_val.dns_addr, |
| 310 | MAX_IP_ADDR_SIZE); |
| 311 | |
| 312 | out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled; |
| 313 | |
| 314 | default: |
| 315 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id, |
| 316 | MAX_ADAPTER_ID_SIZE, |
| 317 | UTF16_LITTLE_ENDIAN, |
| 318 | (__u8 *)out->body.kvp_ip_val.adapter_id, |
| 319 | MAX_ADAPTER_ID_SIZE); |
| 320 | |
| 321 | out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 328 | static void |
| 329 | kvp_send_key(struct work_struct *dummy) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 330 | { |
| 331 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 332 | struct hv_kvp_msg *message; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 333 | struct hv_kvp_msg *in_msg; |
| 334 | __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation; |
| 335 | __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool; |
| 336 | __u32 val32; |
| 337 | __u64 val64; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 338 | |
| 339 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 340 | if (!msg) |
| 341 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 342 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 343 | msg->id.idx = CN_KVP_IDX; |
| 344 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 345 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 346 | message = (struct hv_kvp_msg *)msg->data; |
| 347 | message->kvp_hdr.operation = operation; |
| 348 | message->kvp_hdr.pool = pool; |
| 349 | in_msg = kvp_transaction.kvp_msg; |
| 350 | |
| 351 | /* |
| 352 | * The key/value strings sent from the host are encoded in |
| 353 | * in utf16; convert it to utf8 strings. |
| 354 | * The host assures us that the utf16 strings will not exceed |
| 355 | * the max lengths specified. We will however, reserve room |
| 356 | * for the string terminating character - in the utf16s_utf8s() |
| 357 | * function we limit the size of the buffer where the converted |
| 358 | * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee |
| 359 | * that the strings can be properly terminated! |
| 360 | */ |
| 361 | |
| 362 | switch (message->kvp_hdr.operation) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 363 | case KVP_OP_SET_IP_INFO: |
| 364 | process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO); |
| 365 | break; |
| 366 | case KVP_OP_GET_IP_INFO: |
| 367 | process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO); |
| 368 | break; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 369 | case KVP_OP_SET: |
| 370 | switch (in_msg->body.kvp_set.data.value_type) { |
| 371 | case REG_SZ: |
| 372 | /* |
| 373 | * The value is a string - utf16 encoding. |
| 374 | */ |
| 375 | message->body.kvp_set.data.value_size = |
| 376 | utf16s_to_utf8s( |
| 377 | (wchar_t *)in_msg->body.kvp_set.data.value, |
| 378 | in_msg->body.kvp_set.data.value_size, |
| 379 | UTF16_LITTLE_ENDIAN, |
| 380 | message->body.kvp_set.data.value, |
| 381 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1; |
| 382 | break; |
| 383 | |
| 384 | case REG_U32: |
| 385 | /* |
| 386 | * The value is a 32 bit scalar. |
| 387 | * We save this as a utf8 string. |
| 388 | */ |
| 389 | val32 = in_msg->body.kvp_set.data.value_u32; |
| 390 | message->body.kvp_set.data.value_size = |
| 391 | sprintf(message->body.kvp_set.data.value, |
| 392 | "%d", val32) + 1; |
| 393 | break; |
| 394 | |
| 395 | case REG_U64: |
| 396 | /* |
| 397 | * The value is a 64 bit scalar. |
| 398 | * We save this as a utf8 string. |
| 399 | */ |
| 400 | val64 = in_msg->body.kvp_set.data.value_u64; |
| 401 | message->body.kvp_set.data.value_size = |
| 402 | sprintf(message->body.kvp_set.data.value, |
| 403 | "%llu", val64) + 1; |
| 404 | break; |
| 405 | |
| 406 | } |
| 407 | case KVP_OP_GET: |
| 408 | message->body.kvp_set.data.key_size = |
| 409 | utf16s_to_utf8s( |
| 410 | (wchar_t *)in_msg->body.kvp_set.data.key, |
| 411 | in_msg->body.kvp_set.data.key_size, |
| 412 | UTF16_LITTLE_ENDIAN, |
| 413 | message->body.kvp_set.data.key, |
| 414 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 415 | break; |
| 416 | |
| 417 | case KVP_OP_DELETE: |
| 418 | message->body.kvp_delete.key_size = |
| 419 | utf16s_to_utf8s( |
| 420 | (wchar_t *)in_msg->body.kvp_delete.key, |
| 421 | in_msg->body.kvp_delete.key_size, |
| 422 | UTF16_LITTLE_ENDIAN, |
| 423 | message->body.kvp_delete.key, |
| 424 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 425 | break; |
| 426 | |
| 427 | case KVP_OP_ENUMERATE: |
| 428 | message->body.kvp_enum_data.index = |
| 429 | in_msg->body.kvp_enum_data.index; |
| 430 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 431 | } |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 432 | |
| 433 | msg->len = sizeof(struct hv_kvp_msg); |
| 434 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 435 | kfree(msg); |
| 436 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 437 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Send a response back to the host. |
| 442 | */ |
| 443 | |
| 444 | static void |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 445 | kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 446 | { |
| 447 | struct hv_kvp_msg *kvp_msg; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 448 | struct hv_kvp_exchg_msg_value *kvp_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 449 | char *key_name; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 450 | char *value; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 451 | struct icmsg_hdr *icmsghdrp; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 452 | int keylen = 0; |
| 453 | int valuelen = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 454 | u32 buf_len; |
| 455 | struct vmbus_channel *channel; |
| 456 | u64 req_id; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 457 | int ret; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * If a transaction is not active; log and return. |
| 461 | */ |
| 462 | |
| 463 | if (!kvp_transaction.active) { |
| 464 | /* |
| 465 | * This is a spurious call! |
| 466 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 467 | pr_warn("KVP: Transaction not active\n"); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | /* |
| 471 | * Copy the global state for completing the transaction. Note that |
| 472 | * only one transaction can be active at a time. |
| 473 | */ |
| 474 | |
| 475 | buf_len = kvp_transaction.recv_len; |
| 476 | channel = kvp_transaction.recv_channel; |
| 477 | req_id = kvp_transaction.recv_req_id; |
| 478 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 479 | kvp_transaction.active = false; |
| 480 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 481 | icmsghdrp = (struct icmsg_hdr *) |
| 482 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; |
| 483 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 484 | if (channel->onchannel_callback == NULL) |
| 485 | /* |
| 486 | * We have raced with util driver being unloaded; |
| 487 | * silently return. |
| 488 | */ |
| 489 | return; |
| 490 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 491 | icmsghdrp->status = error; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 492 | |
| 493 | /* |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 494 | * If the error parameter is set, terminate the host's enumeration |
| 495 | * on this pool. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 496 | */ |
| 497 | if (error) { |
| 498 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 499 | * Something failed or we have timedout; |
| 500 | * terminate the current host-side iteration. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 501 | */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 502 | goto response_done; |
| 503 | } |
| 504 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 505 | kvp_msg = (struct hv_kvp_msg *) |
| 506 | &recv_buffer[sizeof(struct vmbuspipe_hdr) + |
| 507 | sizeof(struct icmsg_hdr)]; |
| 508 | |
| 509 | switch (kvp_transaction.kvp_msg->kvp_hdr.operation) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 510 | case KVP_OP_GET_IP_INFO: |
| 511 | ret = process_ob_ipinfo(msg_to_host, |
| 512 | (struct hv_kvp_ip_msg *)kvp_msg, |
| 513 | KVP_OP_GET_IP_INFO); |
| 514 | if (ret < 0) |
| 515 | icmsghdrp->status = HV_E_FAIL; |
| 516 | |
| 517 | goto response_done; |
| 518 | case KVP_OP_SET_IP_INFO: |
| 519 | goto response_done; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 520 | case KVP_OP_GET: |
| 521 | kvp_data = &kvp_msg->body.kvp_get.data; |
| 522 | goto copy_value; |
| 523 | |
| 524 | case KVP_OP_SET: |
| 525 | case KVP_OP_DELETE: |
| 526 | goto response_done; |
| 527 | |
| 528 | default: |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | kvp_data = &kvp_msg->body.kvp_enum_data.data; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 533 | key_name = msg_to_host->body.kvp_enum_data.data.key; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 534 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 535 | /* |
| 536 | * The windows host expects the key/value pair to be encoded |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 537 | * in utf16. Ensure that the key/value size reported to the host |
| 538 | * will be less than or equal to the MAX size (including the |
| 539 | * terminating character). |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 540 | */ |
Alan Stern | 0720a06 | 2011-11-17 16:42:19 -0500 | [diff] [blame] | 541 | keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN, |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 542 | (wchar_t *) kvp_data->key, |
| 543 | (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2); |
| 544 | kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 545 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 546 | copy_value: |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 547 | value = msg_to_host->body.kvp_enum_data.data.value; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 548 | valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, |
| 549 | (wchar_t *) kvp_data->value, |
| 550 | (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2); |
| 551 | kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */ |
| 552 | |
| 553 | /* |
| 554 | * If the utf8s to utf16s conversion failed; notify host |
| 555 | * of the error. |
| 556 | */ |
| 557 | if ((keylen < 0) || (valuelen < 0)) |
| 558 | icmsghdrp->status = HV_E_FAIL; |
| 559 | |
| 560 | kvp_data->value_type = REG_SZ; /* all our values are strings */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 561 | |
| 562 | response_done: |
| 563 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; |
| 564 | |
| 565 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 566 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 567 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
| 571 | * This callback is invoked when we get a KVP message from the host. |
| 572 | * The host ensures that only one KVP transaction can be active at a time. |
| 573 | * KVP implementation in Linux needs to forward the key to a user-mde |
| 574 | * component to retrive the corresponding value. Consequently, we cannot |
| 575 | * respond to the host in the conext of this callback. Since the host |
| 576 | * guarantees that at most only one transaction can be active at a time, |
| 577 | * we stash away the transaction state in a set of global variables. |
| 578 | */ |
| 579 | |
| 580 | void hv_kvp_onchannelcallback(void *context) |
| 581 | { |
| 582 | struct vmbus_channel *channel = context; |
| 583 | u32 recvlen; |
| 584 | u64 requestid; |
| 585 | |
| 586 | struct hv_kvp_msg *kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 587 | |
| 588 | struct icmsg_hdr *icmsghdrp; |
| 589 | struct icmsg_negotiate *negop = NULL; |
| 590 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 591 | if (kvp_transaction.active) { |
| 592 | /* |
| 593 | * We will defer processing this callback once |
| 594 | * the current transaction is complete. |
| 595 | */ |
| 596 | kvp_transaction.kvp_context = context; |
| 597 | return; |
| 598 | } |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 599 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 600 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen, |
| 601 | &requestid); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 602 | |
| 603 | if (recvlen > 0) { |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 604 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
| 605 | sizeof(struct vmbuspipe_hdr)]; |
| 606 | |
| 607 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 608 | /* |
| 609 | * We start with win8 version and if the host cannot |
| 610 | * support that we use the previous version. |
| 611 | */ |
| 612 | if (vmbus_prep_negotiate_resp(icmsghdrp, negop, |
| 613 | recv_buffer, UTIL_FW_MAJOR_MINOR, |
| 614 | WIN8_SRV_MAJOR_MINOR)) |
| 615 | goto done; |
| 616 | |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 617 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 618 | recv_buffer, UTIL_FW_MAJOR_MINOR, |
| 619 | WIN7_SRV_MAJOR_MINOR); |
| 620 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 621 | } else { |
| 622 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ |
| 623 | sizeof(struct vmbuspipe_hdr) + |
| 624 | sizeof(struct icmsg_hdr)]; |
| 625 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 626 | /* |
| 627 | * Stash away this global state for completing the |
| 628 | * transaction; note transactions are serialized. |
| 629 | */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 630 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 631 | kvp_transaction.recv_len = recvlen; |
| 632 | kvp_transaction.recv_channel = channel; |
| 633 | kvp_transaction.recv_req_id = requestid; |
| 634 | kvp_transaction.active = true; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 635 | kvp_transaction.kvp_msg = kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * Get the information from the |
| 639 | * user-mode component. |
| 640 | * component. This transaction will be |
| 641 | * completed when we get the value from |
| 642 | * the user-mode component. |
| 643 | * Set a timeout to deal with |
| 644 | * user-mode not responding. |
| 645 | */ |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 646 | schedule_work(&kvp_sendkey_work); |
| 647 | schedule_delayed_work(&kvp_work, 5*HZ); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 648 | |
| 649 | return; |
| 650 | |
| 651 | } |
K. Y. Srinivasan | 6741335 | 2013-07-02 10:31:30 -0700 | [diff] [blame] | 652 | done: |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 653 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 654 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 655 | | ICMSGHDRFLAG_RESPONSE; |
| 656 | |
| 657 | vmbus_sendpacket(channel, recv_buffer, |
| 658 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 659 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | } |
| 663 | |
| 664 | int |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 665 | hv_kvp_init(struct hv_util_service *srv) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 666 | { |
| 667 | int err; |
| 668 | |
| 669 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); |
| 670 | if (err) |
| 671 | return err; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 672 | recv_buffer = srv->recv_buffer; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 673 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 674 | /* |
| 675 | * When this driver loads, the user level daemon that |
| 676 | * processes the host requests may not yet be running. |
| 677 | * Defer processing channel callbacks until the daemon |
| 678 | * has registered. |
| 679 | */ |
| 680 | kvp_transaction.active = true; |
| 681 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | void hv_kvp_deinit(void) |
| 686 | { |
| 687 | cn_del_callback(&kvp_id); |
| 688 | cancel_delayed_work_sync(&kvp_work); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 689 | cancel_work_sync(&kvp_sendkey_work); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 690 | } |