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