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