1 /* 2 * QEMU Random Number Generator Backend 3 * 4 * Copyright IBM, Corp. 2012 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "sysemu/rng.h" 15 #include "sysemu/char.h" 16 #include "qapi/qmp/qerror.h" 17 #include "hw/qdev.h" /* just for DEFINE_PROP_CHR */ 18 19 #define TYPE_RNG_EGD "rng-egd" 20 #define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD) 21 22 typedef struct RngEgd 23 { 24 RngBackend parent; 25 26 CharDriverState *chr; 27 char *chr_name; 28 29 GSList *requests; 30 } RngEgd; 31 32 typedef struct RngRequest 33 { 34 EntropyReceiveFunc *receive_entropy; 35 uint8_t *data; 36 void *opaque; 37 size_t offset; 38 size_t size; 39 } RngRequest; 40 41 static void rng_egd_request_entropy(RngBackend *b, size_t size, 42 EntropyReceiveFunc *receive_entropy, 43 void *opaque) 44 { 45 RngEgd *s = RNG_EGD(b); 46 RngRequest *req; 47 48 req = g_malloc(sizeof(*req)); 49 50 req->offset = 0; 51 req->size = size; 52 req->receive_entropy = receive_entropy; 53 req->opaque = opaque; 54 req->data = g_malloc(req->size); 55 56 while (size > 0) { 57 uint8_t header[2]; 58 uint8_t len = MIN(size, 255); 59 60 /* synchronous entropy request */ 61 header[0] = 0x02; 62 header[1] = len; 63 64 qemu_chr_fe_write(s->chr, header, sizeof(header)); 65 66 size -= len; 67 } 68 69 s->requests = g_slist_append(s->requests, req); 70 } 71 72 static void rng_egd_free_request(RngRequest *req) 73 { 74 g_free(req->data); 75 g_free(req); 76 } 77 78 static int rng_egd_chr_can_read(void *opaque) 79 { 80 RngEgd *s = RNG_EGD(opaque); 81 GSList *i; 82 int size = 0; 83 84 for (i = s->requests; i; i = i->next) { 85 RngRequest *req = i->data; 86 size += req->size - req->offset; 87 } 88 89 return size; 90 } 91 92 static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size) 93 { 94 RngEgd *s = RNG_EGD(opaque); 95 size_t buf_offset = 0; 96 97 while (size > 0 && s->requests) { 98 RngRequest *req = s->requests->data; 99 int len = MIN(size, req->size - req->offset); 100 101 memcpy(req->data + req->offset, buf + buf_offset, len); 102 buf_offset += len; 103 req->offset += len; 104 size -= len; 105 106 if (req->offset == req->size) { 107 s->requests = g_slist_remove_link(s->requests, s->requests); 108 109 req->receive_entropy(req->opaque, req->data, req->size); 110 111 rng_egd_free_request(req); 112 } 113 } 114 } 115 116 static void rng_egd_free_requests(RngEgd *s) 117 { 118 GSList *i; 119 120 for (i = s->requests; i; i = i->next) { 121 rng_egd_free_request(i->data); 122 } 123 124 g_slist_free(s->requests); 125 s->requests = NULL; 126 } 127 128 static void rng_egd_cancel_requests(RngBackend *b) 129 { 130 RngEgd *s = RNG_EGD(b); 131 132 /* We simply delete the list of pending requests. If there is data in the 133 * queue waiting to be read, this is okay, because there will always be 134 * more data than we requested originally 135 */ 136 rng_egd_free_requests(s); 137 } 138 139 static void rng_egd_opened(RngBackend *b, Error **errp) 140 { 141 RngEgd *s = RNG_EGD(b); 142 143 if (s->chr_name == NULL) { 144 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 145 "chardev", "a valid character device"); 146 return; 147 } 148 149 s->chr = qemu_chr_find(s->chr_name); 150 if (s->chr == NULL) { 151 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 152 "Device '%s' not found", s->chr_name); 153 return; 154 } 155 156 if (qemu_chr_fe_claim(s->chr) != 0) { 157 error_setg(errp, QERR_DEVICE_IN_USE, s->chr_name); 158 return; 159 } 160 161 /* FIXME we should resubmit pending requests when the CDS reconnects. */ 162 qemu_chr_add_handlers(s->chr, rng_egd_chr_can_read, rng_egd_chr_read, 163 NULL, s); 164 } 165 166 static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp) 167 { 168 RngBackend *b = RNG_BACKEND(obj); 169 RngEgd *s = RNG_EGD(b); 170 171 if (b->opened) { 172 error_setg(errp, QERR_PERMISSION_DENIED); 173 } else { 174 g_free(s->chr_name); 175 s->chr_name = g_strdup(value); 176 } 177 } 178 179 static char *rng_egd_get_chardev(Object *obj, Error **errp) 180 { 181 RngEgd *s = RNG_EGD(obj); 182 183 if (s->chr && s->chr->label) { 184 return g_strdup(s->chr->label); 185 } 186 187 return NULL; 188 } 189 190 static void rng_egd_init(Object *obj) 191 { 192 object_property_add_str(obj, "chardev", 193 rng_egd_get_chardev, rng_egd_set_chardev, 194 NULL); 195 } 196 197 static void rng_egd_finalize(Object *obj) 198 { 199 RngEgd *s = RNG_EGD(obj); 200 201 if (s->chr) { 202 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL); 203 qemu_chr_fe_release(s->chr); 204 } 205 206 g_free(s->chr_name); 207 208 rng_egd_free_requests(s); 209 } 210 211 static void rng_egd_class_init(ObjectClass *klass, void *data) 212 { 213 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); 214 215 rbc->request_entropy = rng_egd_request_entropy; 216 rbc->cancel_requests = rng_egd_cancel_requests; 217 rbc->opened = rng_egd_opened; 218 } 219 220 static const TypeInfo rng_egd_info = { 221 .name = TYPE_RNG_EGD, 222 .parent = TYPE_RNG_BACKEND, 223 .instance_size = sizeof(RngEgd), 224 .class_init = rng_egd_class_init, 225 .instance_init = rng_egd_init, 226 .instance_finalize = rng_egd_finalize, 227 }; 228 229 static void register_types(void) 230 { 231 type_register_static(&rng_egd_info); 232 } 233 234 type_init(register_types); 235