blob: 25bd9acadbcd1d7ee1a99f0aab0a2e39251a5629 [file] [log] [blame]
Cyril Bur1aa096a2015-10-28 15:24:52 +11001/* Copyright 2015 IBM
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <errno.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <sys/stat.h>
24#include <sys/mman.h>
25#include <sys/timerfd.h>
26#include <syslog.h>
27#include <poll.h>
28#include <limits.h>
29#include <getopt.h>
30#include <time.h>
31#include <assert.h>
32
33#include <linux/bt-host.h>
34
35#include <systemd/sd-bus.h>
36
37#define PREFIX "[BTBRIDGED] "
38
39#define BT_HOST_PATH "/dev/bt-host"
40#define BT_HOST_TIMEOUT_SEC 1
41#define BT_MAX_MESSAGE 64
42
43#define DBUS_NAME "org.openbmc.HostIpmi"
44#define OBJ_NAME "/org/openbmc/HostIpmi/1"
45
46#define SD_BUS_FD 0
47#define BT_FD 1
48#define TIMER_FD 2
49#define TOTAL_FDS 3
50
51#define MSG_OUT(f_, ...) do { if (verbose) { printf(PREFIX); printf((f_), ##__VA_ARGS__); } } while(0)
52#define MSG_ERR(f_, ...) do { \
53 if (verbose) { \
54 fprintf(stderr, PREFIX); \
55 fprintf(stderr, (f_), ##__VA_ARGS__); \
56 } \
57 } while(0)
58
59struct ipmi_msg {
60 uint8_t netfn;
61 uint8_t lun;
62 uint8_t seq;
63 uint8_t cmd;
64 uint8_t cc; /* Only used on responses */
65 uint8_t *data;
66 size_t data_len;
67};
68
69struct bt_queue {
70 struct ipmi_msg req;
71 struct ipmi_msg rsp;
72 struct timespec start;
73 int expired;
74 sd_bus_message *call;
75 struct bt_queue *next;
76};
77
78struct btbridged_context{
79 int debug;
Cyril Bur1aa096a2015-10-28 15:24:52 +110080 struct pollfd fds[TOTAL_FDS];
81 struct sd_bus *bus;
82 struct bt_queue *bt_q;
83};
84
85static int running = 1;
86static int verbose;
87
88static struct bt_queue *bt_q_get_head(struct btbridged_context *context)
89{
90 return context ? context->bt_q : NULL;
91}
92
93static struct bt_queue *bt_q_get_seq(struct btbridged_context *context, uint8_t seq)
94{
95 struct bt_queue *t;
96
97 assert(context);
98
99 t = context->bt_q;
100
101 while (t && t->req.seq != seq)
102 t = t->next;
103
104 return t;
105}
106
107static struct bt_queue *bt_q_get_msg(struct btbridged_context *context)
108{
109 struct bt_queue *t;
110
111 assert(context);
112
113 t = context->bt_q;
114
115 while (t && (!t->call && !t->expired))
116 t = t->next;
117
118 return t;
119}
120
121static struct bt_queue *bt_q_enqueue(struct btbridged_context *context, uint8_t *bt_data)
122{
123 struct bt_queue *n;
124 struct bt_queue *bt_q;
125 int len;
126
127 assert(context && bt_data);
128
129 /*
130 * len here is the length of the array.
131 * Helpfully BT doesn't count the length byte
132 */
133 len = bt_data[0] + 1;
134 if (len < 4) {
135 MSG_ERR("Trying to queue a BT message with a short length (%d)\n", len);
136 return NULL;
137 }
138
139 bt_q = context->bt_q;
140
141 n = calloc(1, sizeof(struct bt_queue));
142 if (!n)
143 return NULL;
144
145 if (context->debug) {
146 n->req.data = malloc(len - 4);
147 if (n->req.data)
148 n->req.data = memcpy(n->req.data, bt_data + 4, len - 4);
149 }
150 n->req.data_len = len - 4;
151 /* Don't count the lenfn/ln, seq and command */
152 n->req.netfn = bt_data[1] >> 2;
153 n->req.lun = bt_data[1] & 0x3;
154 n->req.seq = bt_data[2];
155 n->req.cmd = bt_data[3];
156 if (clock_gettime(CLOCK_MONOTONIC, &n->start) == -1) {
157 MSG_ERR("Couldn't clock_gettime(): %s\n", strerror(errno));
158 free(n);
159 return NULL;
160 }
161 if (!bt_q) {
162 context->bt_q = n;
163 } else {
164 struct bt_queue *t = bt_q;
165
166 while (t->next)
167 t = t->next;
168
169 t->next = n;
170 }
171
172 return n;
173}
174
175static void bt_q_free(struct bt_queue *bt_q)
176{
177 if (!bt_q)
178 return;
179
180 /* Unrefing sd_bus_message should free(rsp.data) */
181 if (bt_q->call)
182 sd_bus_message_unref(bt_q->call);
183
184 free(bt_q->req.data);
185 free(bt_q);
186}
187
188static struct bt_queue *bt_q_drop(struct btbridged_context *context, struct bt_queue *element)
189{
190 struct bt_queue *r;
191 struct bt_queue *bt_q;
192
193 assert(context);
194
195 bt_q = context->bt_q;
196 if (!element || !bt_q)
197 return NULL;
198
199
200 r = bt_q;
201 if (r == bt_q) {
202 context->bt_q = bt_q->next;
203 } else {
204 while (r && r->next != element)
205 r = r->next;
206
207 if (!r) {
208 MSG_ERR("Didn't find element %p in queue\n", element);
209 bt_q_free(element);
210 return NULL;
211 }
212 r->next = r->next->next;
213 }
214 bt_q_free(element);
215
216 return context->bt_q;
217}
218
219static struct bt_queue *bt_q_dequeue(struct btbridged_context *context)
220{
221 struct bt_queue *r;
222 struct bt_queue *bt_q;
223
224 assert(context);
225
226 bt_q = context->bt_q;
227 if (!bt_q) {
228 MSG_ERR("Dequeuing empty queue!\n");
229 return NULL;
230 }
231
232 r = bt_q->next;
233 bt_q_free(bt_q);
234 context->bt_q = r;
235
236 return r;
237}
238
239static int method_send_sms_atn(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
240{
241 int r, bt_fd = *(int *)userdata;
242 MSG_OUT("Sending SMS_ATN ioctl() to %s\n", BT_HOST_PATH);
243 r = ioctl(bt_fd, BT_HOST_IOCTL_SMS_ATN);
244 if (r == -1) {
245 r = errno;
246 MSG_ERR("Couldn't ioctl() to %s: %s\n", BT_HOST_PATH, strerror(r));
247 return sd_bus_reply_method_errno(msg, errno, ret_error);
248 }
249
250 r = 0;
251 return sd_bus_reply_method_return(msg, "x", r);
252}
253
254static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
255{
256 struct btbridged_context *context;
257 struct bt_queue *bt_msg;
258 uint8_t *data, *all_data;
259 size_t data_sz, all_data_sz;
260 uint8_t netfn, lun, seq, cmd, cc;
261 uint64_t cookie;
262 /*
263 * Doesn't say it anywhere explicitly but it looks like returning 0 or
264 * negative is BAD...
265 */
266 int r = 1;
267
268 context = (struct btbridged_context *)userdata;
269 if (!context) {
270 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Internal error");
271 r = 0;
272 goto out;
273 }
274 r = sd_bus_message_read(msg, "yyyyy", &netfn, &lun, &seq, &cmd, &cc);
275 if (r < 0) {
276 MSG_ERR("Couldn't parse leading bytes of message: %s\n", strerror(-r));
277 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message");
278 r = -EINVAL;
279 goto out;
280 }
281 r = sd_bus_message_read_array(msg, 'y', (const void **)&data, &data_sz);
282 if (r < 0) {
283 MSG_ERR("Couldn't parse data bytes of message: %s\n", strerror(-r));
284 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message data");
285 r = -EINVAL;
286 goto out;
287 }
288
289 bt_msg = bt_q_get_seq(context, seq);
290 if (!bt_msg) {
291 sd_bus_error_set_const(ret_error, "org.openbmc.error", "No matching request");
292 MSG_ERR("Failed to find matching request for dbus method with seq: 0x%02x\n", seq);
293 r = -EINVAL;
294 goto out;
295 }
Cyril Bur104200b2015-10-30 14:34:58 +1100296 MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100297 bt_msg->call = sd_bus_message_ref(msg);
298 bt_msg->rsp.netfn = netfn;
299 bt_msg->rsp.lun = lun;
300 bt_msg->rsp.seq = seq;
301 bt_msg->rsp.cmd = cmd;
302 bt_msg->rsp.cc = cc;
303 bt_msg->rsp.data_len = data_sz;
304 /* Because we've ref'ed the msg, I hope we don't need to memcpy data */
305 bt_msg->rsp.data = data;
306
307 /* Now that we have a response */
308 context->fds[BT_FD].events |= POLLOUT;
309
310out:
311 return r;
312}
313
314static int bt_host_write(struct btbridged_context *context, struct bt_queue *bt_msg)
315{
316 struct bt_queue *head;
317 uint8_t data[BT_MAX_MESSAGE] = { 0 };
318 sd_bus_message *msg = NULL;
319 int r, len;
320
321 assert(context);
322
323 if (!bt_msg)
324 return -EINVAL;
325
326 head = bt_q_get_head(context);
327 if (bt_msg == head) {
328 struct itimerspec ts;
329 /* Need to adjust the timer */
330 ts.it_interval.tv_sec = 0;
331 ts.it_interval.tv_nsec = 0;
332 if (head->next) {
333 ts.it_value = head->next->start;
334 ts.it_value.tv_sec += BT_HOST_TIMEOUT_SEC;
335 MSG_OUT("Adjusting timer for next element\n");
336 } else {
337 ts.it_value.tv_nsec = 0;
338 ts.it_value.tv_sec = 0;
339 MSG_OUT("Disabling timer, no elements remain in queue\n");
340 }
341 r = timerfd_settime(context->fds[TIMER_FD].fd, TFD_TIMER_ABSTIME, &ts, NULL);
342 if (r == -1)
343 MSG_ERR("Couldn't set timerfd\n");
344 }
345 data[1] = (bt_msg->rsp.netfn << 2) | (bt_msg->rsp.lun & 0x3);
346 data[2] = bt_msg->rsp.seq;
347 data[3] = bt_msg->rsp.cmd;
348 data[4] = bt_msg->rsp.cc;
349 if (bt_msg->rsp.data_len > sizeof(data) - 5) {
350 MSG_ERR("Response message size (%d) too big, truncating\n", bt_msg->rsp.data_len);
351 bt_msg->rsp.data_len = sizeof(data) - 5;
352 }
353 /* netfn/len + seq + cmd + cc = 4 */
354 data[0] = bt_msg->rsp.data_len + 4;
355 if (bt_msg->rsp.data_len)
356 memcpy(data + 5, bt_msg->rsp.data, bt_msg->rsp.data_len);
357 /* Count the data[0] byte */
358 len = write(context->fds[BT_FD].fd, data, data[0] + 1);
359 if (r == -1) {
360 r = errno;
361 MSG_ERR("Error writing to %s: %s\n", BT_HOST_PATH, strerror(errno));
362 if (bt_msg->call) {
363 r = sd_bus_message_new_method_errno(bt_msg->call, &msg, r, NULL);
364 if (r < 0)
365 MSG_ERR("Couldn't create response error\n");
366 }
367 goto out;
368 } else {
369 if (len != data[0] + 1)
370 MSG_ERR("Possible short write to %s, desired len: %d, written len: %d\n", BT_HOST_PATH, data[0] + 1, len);
371 else
372 MSG_OUT("Successfully wrote %d of %d bytes to %s\n", len, data[0] + 1, BT_HOST_PATH);
373
374 if (bt_msg->call) {
375 r = sd_bus_message_new_method_return(bt_msg->call, &msg);
376 if (r < 0) {
377 MSG_ERR("Couldn't create response message\n");
378 goto out;
379 }
380 r = 0; /* Just to be explicit about what we're sending back */
381 r = sd_bus_message_append(msg, "x", r);
382 if (r < 0) {
383 MSG_ERR("Couldn't append result to response\n");
384 goto out;
385 }
386
387 }
388 }
389
390out:
391 if (bt_msg->call) {
392 if (sd_bus_send(context->bus, msg, NULL) < 0)
393 MSG_ERR("Couldn't send response message\n");
394 sd_bus_message_unref(msg);
395 }
396 bt_q_drop(context, bt_msg);
397
398 /*
399 * There isn't another message ready to be sent so turn off POLLOUT
400 */
401 if (!bt_q_get_msg(context)) {
402 MSG_OUT("Turning off POLLOUT for the BT in poll()\n");
403 context->fds[BT_FD].events = POLLIN;
404 }
405
406 return r;
407}
408
409static int dispatch_timer(struct btbridged_context *context)
410{
411 int r = 0;
412 if (context->fds[TIMER_FD].revents & POLLIN) {
413 sd_bus_message *msg;
414 struct bt_queue *head;
415
416 head = bt_q_get_head(context);
417 if (!head) {
418 /* Odd, timer expired but we didn't have a message to timeout */
419 MSG_ERR("No message found to send timeout\n");
420 return 0;
421 }
422 if (head->call) {
423 r = sd_bus_message_new_method_errno(head->call, &msg, ETIMEDOUT, NULL);
424 if (r < 0) {
425 MSG_ERR("Couldn't create response error\n");
426 } else {
427 if (sd_bus_send(context->bus, msg, NULL) < 0)
428 MSG_ERR("Couldn't send response message\n");
429 sd_bus_message_unref(msg);
430 }
431 MSG_ERR("Message with seq 0x%02x is being timed out despite "
432 "appearing to have been responded to. Slow BT?\n", head->rsp.seq);
433 }
434
435 /* Set expiry */
436 head->expired = 1;
437 head->rsp.seq = head->req.seq;
438 head->rsp.netfn = head->req.netfn + 1;
439 head->rsp.lun = head->req.lun;
440 head->rsp.cc = 0xce; /* Command response could not be provided */
441 head->rsp.cmd = head->req.cmd;
442 /* These should already be zeroed but best to be sure */
443 head->rsp.data_len = 0;
444 head->rsp.data = NULL;
445 head->call = NULL;
446 MSG_OUT("Timeout on msg with seq: 0x%02x\n", head->rsp.seq);
447 /* Turn on POLLOUT so we'll write this one next */
448 context->fds[BT_FD].events |= POLLOUT;
449 }
450
451 return 0;
452}
453
454static int dispatch_sd_bus(struct btbridged_context *context)
455{
456 int r = 0;
457 if (context->fds[SD_BUS_FD].revents) {
458 r = sd_bus_process(context->bus, NULL);
459 if (r > 0)
460 MSG_OUT("Processed %d dbus events\n", r);
461 }
462
463 return r;
464}
465
466static int dispatch_bt(struct btbridged_context *context)
467{
468 int err = 0;
469 int r;
470
471 assert(context);
472
473 if (context->fds[BT_FD].revents & POLLIN) {
474 int data_len;
475 struct bt_queue *new;
476 uint8_t data[BT_MAX_MESSAGE] = { 0 };
477
478 r = read(context->fds[BT_FD].fd, data, sizeof(data));
479 if (r < 0) {
480 MSG_ERR("Couldn't read from bt: %s\n", strerror(-r));
481 goto out1;
482 }
483 if (r < data[0] + 1) {
484 MSG_ERR("Short read from bt (%d)\n", r);
485 r = -EINVAL;
486 goto out1;
487 }
488
Cyril Bur1aa096a2015-10-28 15:24:52 +1100489 new = bt_q_enqueue(context, data);
490 if (!new) {
491 r = -ENOMEM;
492 goto out1;
493 }
494 if (new == bt_q_get_head(context)) {
495 struct itimerspec ts;
496 /*
497 * Enqueued onto an empty list, setup a timer for sending a
498 * timeout
499 */
500 ts.it_interval.tv_sec = 0;
501 ts.it_interval.tv_nsec = 0;
502 ts.it_value.tv_nsec = 0;
503 ts.it_value.tv_sec = BT_HOST_TIMEOUT_SEC;
504 r = timerfd_settime(context->fds[TIMER_FD].fd, 0, &ts, NULL);
505 if (r == -1)
506 MSG_ERR("Couldn't set timerfd\n");
507 }
508
509 MSG_OUT("Sending dbus signal with netfn 0x%02x, lun 0x%02x, seq 0x%02x, cmd 0x%02x\n",
510 new->req.netfn, new->req.lun, new->req.seq, new->req.cmd);
511 /* Note we only actually keep the request data in the queue when debugging */
Cyril Bur104200b2015-10-30 14:34:58 +1100512 r = sd_bus_emit_signal(context->bus, OBJ_NAME, DBUS_NAME, "ReceivedMessage", "yyyyay",
Cyril Bur1aa096a2015-10-28 15:24:52 +1100513 new->req.netfn, new->req.lun, new->req.seq, new->req.cmd,
514 new->req.data_len, data+4);
515 if (r < 0) {
516 MSG_ERR("Couldn't emit dbus signal: %s\n", strerror(-r));
517 goto out1;
518 }
519 r = 0;
520out1:
521 err = r;
522 }
523
524 if (context->fds[BT_FD].revents & POLLOUT) {
525 struct bt_queue *bt_msg;
526 bt_msg = bt_q_get_msg(context);
527 if (!bt_msg) {
528 /* Odd, this shouldn't really happen */
529 MSG_ERR("Got a POLLOUT on %s but no message is ready to be written\n");
530 r = 0;
531 goto out;
532 }
533 r = bt_host_write(context, bt_msg);
534 if (r < 0)
535 MSG_ERR("Problem putting request with netfn 0x%02x, lun 0x%02x, seq 0x%02x, cmd 0x%02x, cc 0x%02x\n"
536 "out to %s", BT_HOST_PATH, bt_msg->rsp.netfn, bt_msg->rsp.lun, bt_msg->rsp.seq,
537 bt_msg->rsp.cmd, bt_msg->rsp.cc);
538 else
539 MSG_OUT("Completed request with netfn 0x%02x, lun 0x%02x, seq 0x%02x, cmd 0x%02x, cc 0x%02x\n",
540 bt_msg->rsp.netfn, bt_msg->rsp.lun, bt_msg->rsp.seq, bt_msg->rsp.cmd, bt_msg->rsp.cc);
541 }
542out:
543 return err ? err : r;
544}
545
546static void usage(const char *name)
547{
548 fprintf(stderr, "Usage %s\n", name);
549 fprintf(stderr, "\t--verbose\t Be verbose\n\n");
550}
551
552static const sd_bus_vtable ipmid_vtable[] = {
553 SD_BUS_VTABLE_START(0),
554 SD_BUS_METHOD("sendMessage", "yyyyyay", "x", &method_send_message, SD_BUS_VTABLE_UNPRIVILEGED),
555 SD_BUS_METHOD("setAttention", "", "x", &method_send_sms_atn, SD_BUS_VTABLE_UNPRIVILEGED),
Cyril Bur104200b2015-10-30 14:34:58 +1100556 SD_BUS_SIGNAL("ReceivedMessage", "yyyyay", 0),
Cyril Bur1aa096a2015-10-28 15:24:52 +1100557 SD_BUS_VTABLE_END
558};
559
560int main(int argc, char *argv[]) {
561 struct btbridged_context context;
562 struct bt_queue *bt_q;
563 const char *path;
564 const char *name = argv[0];
565 int opt, polled, r, daemonise;
566 uint8_t buf[BT_MAX_MESSAGE];
567
568 static struct option long_options[] = {
569 {"verbose", no_argument, &verbose, 1},
570 {0, 0, 0, 0}
571 };
572
Cyril Bur1aa096a2015-10-28 15:24:52 +1100573 context.debug = 0;
574 context.bt_q = NULL;
575
576 while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
577 switch (opt) {
578 case 0:
579 MSG_OUT("Found verbosity flag\n");
580 break;
581 default:
582 usage(name);
583 exit(EXIT_FAILURE);
584 }
585 }
586
587 MSG_OUT("Starting\n");
588 r = sd_bus_default_system(&context.bus);
589 if (r < 0) {
590 MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r));
591 goto finish;
592 }
593
594 MSG_OUT("Registering dbus methods/signals\n");
595 r = sd_bus_add_object_vtable(context.bus,
596 NULL,
597 OBJ_NAME,
598 DBUS_NAME,
599 ipmid_vtable,
600 &context);
601 if (r < 0) {
602 MSG_ERR("Failed to issue method call: %s\n", strerror(-r));
603 goto finish;
604 }
605
606 MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME);
607 r = sd_bus_request_name(context.bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
608 if (r < 0) {
609 MSG_ERR("Failed to acquire service name: %s\n", strerror(-r));
610 goto finish;
611 }
612
613 MSG_OUT("Getting dbus file descriptors\n");
614 context.fds[SD_BUS_FD].fd = sd_bus_get_fd(context.bus);
615 if (context.fds[SD_BUS_FD].fd < 0) {
616 r = -errno;
617 MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno));
618 goto finish;
619 }
620
621 MSG_OUT("Opening %s\n", BT_HOST_PATH);
622 context.fds[BT_FD].fd = open(BT_HOST_PATH, O_RDWR | O_NONBLOCK);
623 if (context.fds[BT_FD].fd < 0) {
624 r = -errno;
625 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_HOST_PATH, strerror(errno));
626 goto finish;
627 }
628
629 MSG_OUT("Creating timer fd\n");
630 context.fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0);
631 if (context.fds[TIMER_FD].fd < 0) {
632 r = -errno;
633 MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno));
634 goto finish;
635 }
636 context.fds[SD_BUS_FD].events = POLLIN;
637 context.fds[BT_FD].events = POLLIN;
638 context.fds[TIMER_FD].events = POLLIN;
639
640 MSG_OUT("Entering polling loop\n");
641
642 while (running) {
643 polled = poll(context.fds, TOTAL_FDS, 1000);
644 if (polled == 0)
645 continue;
646 if (polled < 0) {
647 r = -errno;
648 MSG_ERR("Error from poll(): %s\n", strerror(errno));
649 goto finish;
650 }
651 r = dispatch_sd_bus(&context);
652 if (r < 0) {
653 MSG_ERR("Error handling dbus event: %s\n", strerror(-r));
654 goto finish;
655 }
656 r = dispatch_bt(&context);
657 if (r < 0) {
658 MSG_ERR("Error handling BT event: %s\n", strerror(-r));
659 goto finish;
660 }
661 r = dispatch_timer(&context);
662 if (r < 0) {
663 MSG_ERR("Error handling timer event: %s\n", strerror(-r));
664 goto finish;
665 }
666 }
667
668finish:
669 if (bt_q_get_head(&context)) {
670 MSG_ERR("Unresponded BT Message!\n");
671 while (bt_q_dequeue(&context));
672 }
673 close(context.fds[BT_FD].fd);
674 close(context.fds[TIMER_FD].fd);
675 sd_bus_unref(context.bus);
676
677 return r;
678}
679