123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/net.h>
- #include <linux/nls.h>
- #include <linux/connector.h>
- #include <linux/workqueue.h>
- #include <linux/hyperv.h>
- #include "hyperv_vmbus.h"
- #include "hv_utils_transport.h"
- #define WS2008_SRV_MAJOR 1
- #define WS2008_SRV_MINOR 0
- #define WS2008_SRV_VERSION (WS2008_SRV_MAJOR << 16 | WS2008_SRV_MINOR)
- #define WIN7_SRV_MAJOR 3
- #define WIN7_SRV_MINOR 0
- #define WIN7_SRV_VERSION (WIN7_SRV_MAJOR << 16 | WIN7_SRV_MINOR)
- #define WIN8_SRV_MAJOR 4
- #define WIN8_SRV_MINOR 0
- #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
- static struct {
- int state;
- int recv_len;
- struct hv_kvp_msg *kvp_msg;
- struct vmbus_channel *recv_channel;
- u64 recv_req_id;
- } kvp_transaction;
- static int dm_reg_value;
- static void kvp_send_key(struct work_struct *dummy);
- static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
- static void kvp_timeout_func(struct work_struct *dummy);
- static void kvp_host_handshake_func(struct work_struct *dummy);
- static void kvp_register(int);
- static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
- static DECLARE_DELAYED_WORK(kvp_host_handshake_work, kvp_host_handshake_func);
- static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
- static const char kvp_devname[] = "vmbus/hv_kvp";
- static u8 *recv_buffer;
- static struct hvutil_transport *hvt;
- #define HV_DRV_VERSION "3.1"
- static void kvp_poll_wrapper(void *channel)
- {
-
- kvp_transaction.state = HVUTIL_READY;
- hv_kvp_onchannelcallback(channel);
- }
- static void kvp_register_done(void)
- {
-
- pr_debug("KVP: userspace daemon registered\n");
- cancel_delayed_work_sync(&kvp_host_handshake_work);
- hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
- }
- static void
- kvp_register(int reg_value)
- {
- struct hv_kvp_msg *kvp_msg;
- char *version;
- kvp_msg = kzalloc(sizeof(*kvp_msg), GFP_KERNEL);
- if (kvp_msg) {
- version = kvp_msg->body.kvp_register.version;
- kvp_msg->kvp_hdr.operation = reg_value;
- strcpy(version, HV_DRV_VERSION);
- hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg),
- kvp_register_done);
- kfree(kvp_msg);
- }
- }
- static void kvp_timeout_func(struct work_struct *dummy)
- {
-
- kvp_respond_to_host(NULL, HV_E_FAIL);
- hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
- }
- static void kvp_host_handshake_func(struct work_struct *dummy)
- {
- hv_poll_channel(kvp_transaction.recv_channel, hv_kvp_onchannelcallback);
- }
- static int kvp_handle_handshake(struct hv_kvp_msg *msg)
- {
- switch (msg->kvp_hdr.operation) {
- case KVP_OP_REGISTER:
- dm_reg_value = KVP_OP_REGISTER;
- pr_info("KVP: IP injection functionality not available\n");
- pr_info("KVP: Upgrade the KVP daemon\n");
- break;
- case KVP_OP_REGISTER1:
- dm_reg_value = KVP_OP_REGISTER1;
- break;
- default:
- pr_info("KVP: incompatible daemon\n");
- pr_info("KVP: KVP version: %d, Daemon version: %d\n",
- KVP_OP_REGISTER1, msg->kvp_hdr.operation);
- return -EINVAL;
- }
-
- pr_debug("KVP: userspace daemon ver. %d connected\n",
- msg->kvp_hdr.operation);
- kvp_register(dm_reg_value);
- return 0;
- }
- static int kvp_on_msg(void *msg, int len)
- {
- struct hv_kvp_msg *message = (struct hv_kvp_msg *)msg;
- struct hv_kvp_msg_enumerate *data;
- int error = 0;
- if (len < sizeof(*message))
- return -EINVAL;
-
- if (kvp_transaction.state < HVUTIL_READY) {
- return kvp_handle_handshake(message);
- }
-
- if (kvp_transaction.state < HVUTIL_USERSPACE_REQ)
- return -EINVAL;
- kvp_transaction.state = HVUTIL_USERSPACE_RECV;
-
- data = &message->body.kvp_enum_data;
- switch (dm_reg_value) {
- case KVP_OP_REGISTER:
-
- if (data->data.key[0] == 0)
- error = HV_S_CONT;
- break;
- case KVP_OP_REGISTER1:
-
- error = message->error;
- break;
- }
-
- if (cancel_delayed_work_sync(&kvp_timeout_work)) {
- kvp_respond_to_host(message, error);
- hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
- }
- return 0;
- }
- static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
- {
- struct hv_kvp_msg *in = in_msg;
- struct hv_kvp_ip_msg *out = out_msg;
- int len;
- switch (op) {
- case KVP_OP_GET_IP_INFO:
-
- len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
- strlen((char *)in->body.kvp_ip_val.ip_addr),
- UTF16_HOST_ENDIAN,
- (wchar_t *)out->kvp_ip_val.ip_addr,
- MAX_IP_ADDR_SIZE);
- if (len < 0)
- return len;
- len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
- strlen((char *)in->body.kvp_ip_val.sub_net),
- UTF16_HOST_ENDIAN,
- (wchar_t *)out->kvp_ip_val.sub_net,
- MAX_IP_ADDR_SIZE);
- if (len < 0)
- return len;
- len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
- strlen((char *)in->body.kvp_ip_val.gate_way),
- UTF16_HOST_ENDIAN,
- (wchar_t *)out->kvp_ip_val.gate_way,
- MAX_GATEWAY_SIZE);
- if (len < 0)
- return len;
- len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
- strlen((char *)in->body.kvp_ip_val.dns_addr),
- UTF16_HOST_ENDIAN,
- (wchar_t *)out->kvp_ip_val.dns_addr,
- MAX_IP_ADDR_SIZE);
- if (len < 0)
- return len;
- len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
- strlen((char *)in->body.kvp_ip_val.adapter_id),
- UTF16_HOST_ENDIAN,
- (wchar_t *)out->kvp_ip_val.adapter_id,
- MAX_IP_ADDR_SIZE);
- if (len < 0)
- return len;
- out->kvp_ip_val.dhcp_enabled =
- in->body.kvp_ip_val.dhcp_enabled;
- out->kvp_ip_val.addr_family =
- in->body.kvp_ip_val.addr_family;
- }
- return 0;
- }
- static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
- {
- struct hv_kvp_ip_msg *in = in_msg;
- struct hv_kvp_msg *out = out_msg;
- switch (op) {
- case KVP_OP_SET_IP_INFO:
-
- utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
- MAX_IP_ADDR_SIZE,
- UTF16_LITTLE_ENDIAN,
- (__u8 *)out->body.kvp_ip_val.ip_addr,
- MAX_IP_ADDR_SIZE);
- utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
- MAX_IP_ADDR_SIZE,
- UTF16_LITTLE_ENDIAN,
- (__u8 *)out->body.kvp_ip_val.sub_net,
- MAX_IP_ADDR_SIZE);
- utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
- MAX_GATEWAY_SIZE,
- UTF16_LITTLE_ENDIAN,
- (__u8 *)out->body.kvp_ip_val.gate_way,
- MAX_GATEWAY_SIZE);
- utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
- MAX_IP_ADDR_SIZE,
- UTF16_LITTLE_ENDIAN,
- (__u8 *)out->body.kvp_ip_val.dns_addr,
- MAX_IP_ADDR_SIZE);
- out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
- default:
- utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
- MAX_ADAPTER_ID_SIZE,
- UTF16_LITTLE_ENDIAN,
- (__u8 *)out->body.kvp_ip_val.adapter_id,
- MAX_ADAPTER_ID_SIZE);
- out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
- }
- }
- static void
- kvp_send_key(struct work_struct *dummy)
- {
- struct hv_kvp_msg *message;
- struct hv_kvp_msg *in_msg;
- __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation;
- __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool;
- __u32 val32;
- __u64 val64;
- int rc;
-
- if (kvp_transaction.state != HVUTIL_HOSTMSG_RECEIVED)
- return;
- message = kzalloc(sizeof(*message), GFP_KERNEL);
- if (!message)
- return;
- message->kvp_hdr.operation = operation;
- message->kvp_hdr.pool = pool;
- in_msg = kvp_transaction.kvp_msg;
-
- switch (message->kvp_hdr.operation) {
- case KVP_OP_SET_IP_INFO:
- process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
- break;
- case KVP_OP_GET_IP_INFO:
- process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
- break;
- case KVP_OP_SET:
- switch (in_msg->body.kvp_set.data.value_type) {
- case REG_SZ:
-
- message->body.kvp_set.data.value_size =
- utf16s_to_utf8s(
- (wchar_t *)in_msg->body.kvp_set.data.value,
- in_msg->body.kvp_set.data.value_size,
- UTF16_LITTLE_ENDIAN,
- message->body.kvp_set.data.value,
- HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1;
- break;
- case REG_U32:
-
- val32 = in_msg->body.kvp_set.data.value_u32;
- message->body.kvp_set.data.value_size =
- sprintf(message->body.kvp_set.data.value,
- "%d", val32) + 1;
- break;
- case REG_U64:
-
- val64 = in_msg->body.kvp_set.data.value_u64;
- message->body.kvp_set.data.value_size =
- sprintf(message->body.kvp_set.data.value,
- "%llu", val64) + 1;
- break;
- }
- case KVP_OP_GET:
- message->body.kvp_set.data.key_size =
- utf16s_to_utf8s(
- (wchar_t *)in_msg->body.kvp_set.data.key,
- in_msg->body.kvp_set.data.key_size,
- UTF16_LITTLE_ENDIAN,
- message->body.kvp_set.data.key,
- HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
- break;
- case KVP_OP_DELETE:
- message->body.kvp_delete.key_size =
- utf16s_to_utf8s(
- (wchar_t *)in_msg->body.kvp_delete.key,
- in_msg->body.kvp_delete.key_size,
- UTF16_LITTLE_ENDIAN,
- message->body.kvp_delete.key,
- HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1;
- break;
- case KVP_OP_ENUMERATE:
- message->body.kvp_enum_data.index =
- in_msg->body.kvp_enum_data.index;
- break;
- }
- kvp_transaction.state = HVUTIL_USERSPACE_REQ;
- rc = hvutil_transport_send(hvt, message, sizeof(*message), NULL);
- if (rc) {
- pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
- if (cancel_delayed_work_sync(&kvp_timeout_work)) {
- kvp_respond_to_host(message, HV_E_FAIL);
- kvp_transaction.state = HVUTIL_READY;
- }
- }
- kfree(message);
- return;
- }
- static void
- kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
- {
- struct hv_kvp_msg *kvp_msg;
- struct hv_kvp_exchg_msg_value *kvp_data;
- char *key_name;
- char *value;
- struct icmsg_hdr *icmsghdrp;
- int keylen = 0;
- int valuelen = 0;
- u32 buf_len;
- struct vmbus_channel *channel;
- u64 req_id;
- int ret;
-
- buf_len = kvp_transaction.recv_len;
- channel = kvp_transaction.recv_channel;
- req_id = kvp_transaction.recv_req_id;
- icmsghdrp = (struct icmsg_hdr *)
- &recv_buffer[sizeof(struct vmbuspipe_hdr)];
- if (channel->onchannel_callback == NULL)
-
- return;
- icmsghdrp->status = error;
-
- if (error) {
-
- goto response_done;
- }
- kvp_msg = (struct hv_kvp_msg *)
- &recv_buffer[sizeof(struct vmbuspipe_hdr) +
- sizeof(struct icmsg_hdr)];
- switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
- case KVP_OP_GET_IP_INFO:
- ret = process_ob_ipinfo(msg_to_host,
- (struct hv_kvp_ip_msg *)kvp_msg,
- KVP_OP_GET_IP_INFO);
- if (ret < 0)
- icmsghdrp->status = HV_E_FAIL;
- goto response_done;
- case KVP_OP_SET_IP_INFO:
- goto response_done;
- case KVP_OP_GET:
- kvp_data = &kvp_msg->body.kvp_get.data;
- goto copy_value;
- case KVP_OP_SET:
- case KVP_OP_DELETE:
- goto response_done;
- default:
- break;
- }
- kvp_data = &kvp_msg->body.kvp_enum_data.data;
- key_name = msg_to_host->body.kvp_enum_data.data.key;
-
- keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN,
- (wchar_t *) kvp_data->key,
- (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2);
- kvp_data->key_size = 2*(keylen + 1);
- copy_value:
- value = msg_to_host->body.kvp_enum_data.data.value;
- valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
- (wchar_t *) kvp_data->value,
- (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
- kvp_data->value_size = 2*(valuelen + 1);
-
- if ((keylen < 0) || (valuelen < 0))
- icmsghdrp->status = HV_E_FAIL;
- kvp_data->value_type = REG_SZ;
- response_done:
- icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
- vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
- VM_PKT_DATA_INBAND, 0);
- }
- void hv_kvp_onchannelcallback(void *context)
- {
- struct vmbus_channel *channel = context;
- u32 recvlen;
- u64 requestid;
- struct hv_kvp_msg *kvp_msg;
- struct icmsg_hdr *icmsghdrp;
- struct icmsg_negotiate *negop = NULL;
- int util_fw_version;
- int kvp_srv_version;
- static enum {NEGO_NOT_STARTED,
- NEGO_IN_PROGRESS,
- NEGO_FINISHED} host_negotiatied = NEGO_NOT_STARTED;
- if (kvp_transaction.state < HVUTIL_READY) {
-
- if (host_negotiatied == NEGO_NOT_STARTED) {
- host_negotiatied = NEGO_IN_PROGRESS;
- schedule_delayed_work(&kvp_host_handshake_work,
- HV_UTIL_NEGO_TIMEOUT * HZ);
- }
- return;
- }
- if (kvp_transaction.state > HVUTIL_READY)
- return;
- recheck:
- vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen,
- &requestid);
- if (recvlen > 0) {
- icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
- sizeof(struct vmbuspipe_hdr)];
- if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
-
- switch (vmbus_proto_version) {
- case (VERSION_WS2008):
- util_fw_version = UTIL_WS2K8_FW_VERSION;
- kvp_srv_version = WS2008_SRV_VERSION;
- break;
- case (VERSION_WIN7):
- util_fw_version = UTIL_FW_VERSION;
- kvp_srv_version = WIN7_SRV_VERSION;
- break;
- default:
- util_fw_version = UTIL_FW_VERSION;
- kvp_srv_version = WIN8_SRV_VERSION;
- }
- vmbus_prep_negotiate_resp(icmsghdrp, negop,
- recv_buffer, util_fw_version,
- kvp_srv_version);
- } else {
- kvp_msg = (struct hv_kvp_msg *)&recv_buffer[
- sizeof(struct vmbuspipe_hdr) +
- sizeof(struct icmsg_hdr)];
-
- kvp_transaction.recv_len = recvlen;
- kvp_transaction.recv_req_id = requestid;
- kvp_transaction.kvp_msg = kvp_msg;
- if (kvp_transaction.state < HVUTIL_READY) {
-
- kvp_respond_to_host(NULL, HV_E_FAIL);
- return;
- }
- kvp_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
-
- schedule_work(&kvp_sendkey_work);
- schedule_delayed_work(&kvp_timeout_work,
- HV_UTIL_TIMEOUT * HZ);
- return;
- }
- icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
- | ICMSGHDRFLAG_RESPONSE;
- vmbus_sendpacket(channel, recv_buffer,
- recvlen, requestid,
- VM_PKT_DATA_INBAND, 0);
- host_negotiatied = NEGO_FINISHED;
- goto recheck;
- }
- }
- static void kvp_on_reset(void)
- {
- if (cancel_delayed_work_sync(&kvp_timeout_work))
- kvp_respond_to_host(NULL, HV_E_FAIL);
- kvp_transaction.state = HVUTIL_DEVICE_INIT;
- }
- int
- hv_kvp_init(struct hv_util_service *srv)
- {
- recv_buffer = srv->recv_buffer;
- kvp_transaction.recv_channel = srv->channel;
-
- kvp_transaction.state = HVUTIL_DEVICE_INIT;
- hvt = hvutil_transport_init(kvp_devname, CN_KVP_IDX, CN_KVP_VAL,
- kvp_on_msg, kvp_on_reset);
- if (!hvt)
- return -EFAULT;
- return 0;
- }
- void hv_kvp_deinit(void)
- {
- kvp_transaction.state = HVUTIL_DEVICE_DYING;
- cancel_delayed_work_sync(&kvp_host_handshake_work);
- cancel_delayed_work_sync(&kvp_timeout_work);
- cancel_work_sync(&kvp_sendkey_work);
- hvutil_transport_destroy(hvt);
- }
|