-
Notifications
You must be signed in to change notification settings - Fork 20
/
mctpd.c
3553 lines (3044 loc) · 81.2 KB
/
mctpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: GPL-2.0 */
/*
* mctpd: bus owner for MCTP using Linux kernel
*
* Copyright (c) 2021 Code Construct
* Copyright (c) 2021 Google
*/
#include "config.h"
#include <assert.h>
#include <bits/time.h>
#include <systemd/sd-bus-vtable.h>
#define _GNU_SOURCE
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <systemd/sd-event.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-id128.h>
#include "mctp.h"
#include "mctp-util.h"
#include "mctp-netlink.h"
#include "mctp-control-spec.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MCTP_DBUS_PATH "/xyz/openbmc_project/mctp"
#define CC_MCTP_DBUS_IFACE "au.com.CodeConstruct.MCTP"
#define CC_MCTP_DBUS_IFACE_ENDPOINT "au.com.CodeConstruct.MCTP.Endpoint"
#define CC_MCTP_DBUS_IFACE_TESTING "au.com.CodeConstruct.MCTPTesting"
#define MCTP_DBUS_IFACE "xyz.openbmc_project.MCTP"
#define MCTP_DBUS_IFACE_ENDPOINT "xyz.openbmc_project.MCTP.Endpoint"
#define OPENBMC_IFACE_COMMON_UUID "xyz.openbmc_project.Common.UUID"
// an arbitrary constant for use with sd_id128_get_machine_app_specific()
static const char* mctpd_appid = "67369c05-4b97-4b7e-be72-65cfd8639f10";
static mctp_eid_t eid_alloc_min = 0x08;
static mctp_eid_t eid_alloc_max = 0xfe;
// arbitrary sanity
static size_t MAX_PEER_SIZE = 1000000;
static const uint8_t RQDI_REQ = 1<<7;
static const uint8_t RQDI_RESP = 0x0;
struct dest_phys {
int ifindex;
uint8_t hwaddr[MAX_ADDR_LEN];
size_t hwaddr_len;
};
typedef struct dest_phys dest_phys;
/* Table of per-network details */
struct net_det {
int net;
// EID mappings, an index into ctx->peers. Value -1 is unused.
ssize_t peeridx[256];
};
typedef struct net_det net_det;
struct ctx;
// all local peers have the same phys
static const dest_phys local_phys = { .ifindex = 0 };
struct peer {
int net;
mctp_eid_t eid;
// multiple local interfaces can have the same eid,
// so we store a refcount to use when removing peers.
int local_count;
// Only set for .state == REMOTE
dest_phys phys;
enum {
UNUSED = 0,
REMOTE,
// Local address. Note that multiple interfaces
// in a network may have the same local address.
LOCAL,
} state;
// visible to dbus, set by publish/unpublish_peer()
bool published;
bool have_neigh;
bool have_route;
// set by SetMTU method, 0 otherwise
uint32_t mtu;
// malloc()ed list of supported message types, from Get Message Type
uint8_t *message_types;
size_t num_message_types;
// From Get Endpoint ID
uint8_t endpoint_type;
uint8_t medium_spec;
// From Get Endpoint UUID. A malloced 16 bytes */
uint8_t *uuid;
// Stuff the ctx pointer into peer for tidier parameter passing
struct ctx *ctx;
// Connectivity state
bool degraded;
struct {
uint64_t delay;
sd_event_source *source;
int npolls;
mctp_eid_t eid;
uint8_t endpoint_type;
uint8_t medium_spec;
} recovery;
};
typedef struct peer peer;
struct ctx {
sd_event *event;
sd_bus *bus;
// Main instance for link/address state and listening for updates
mctp_nl *nl;
// Second instance for sending mctp socket requests. State is unused.
mctp_nl *nl_query;
// Whether we are running as the bus owner
bool bus_owner;
// An allocated array of peers, changes address (reallocated) during runtime
peer *peers;
size_t size_peers;
struct net_det *nets;
size_t num_nets;
// Timeout in usecs for a MCTP response
uint64_t mctp_timeout;
uint8_t uuid[16];
// Verbose logging
bool verbose;
bool testing;
};
typedef struct ctx ctx;
static int emit_endpoint_added(const peer *peer);
static int emit_endpoint_removed(const peer *peer);
static int emit_net_added(ctx *ctx, int net);
static int emit_net_removed(ctx *ctx, int net);
static int query_peer_properties(peer *peer);
static int setup_added_peer(peer *peer);
static void add_peer_route(peer *peer);
static int publish_peer(peer *peer, bool add_route);
static int unpublish_peer(peer *peer);
static int peer_route_update(peer *peer, uint16_t type);
static int peer_neigh_update(peer *peer, uint16_t type);
static int add_interface_local(ctx *ctx, int ifindex);
static int del_interface(ctx *ctx, int old_ifindex);
static int change_net_interface(ctx *ctx, int ifindex, int old_net);
static int add_local_eid(ctx *ctx, int net, int eid);
static int del_local_eid(ctx *ctx, int net, int eid);
static int add_net(ctx *ctx, int net);
mctp_eid_t local_addr(const ctx *ctx, int ifindex) {
mctp_eid_t *eids, ret = 0;
size_t num;
eids = mctp_nl_addrs_byindex(ctx->nl, ifindex, &num);
if (num)
ret = eids[0];
free(eids);
return ret;
}
static void* dfree(void* ptr);
static net_det *lookup_net(ctx *ctx, int net)
{
size_t i;
for (i = 0; i < ctx->num_nets; i++)
if (ctx->nets[i].net == net)
return &ctx->nets[i];
return NULL;
}
static bool match_phys(const dest_phys *d1, const dest_phys *d2) {
return d1->ifindex == d2->ifindex &&
d1->hwaddr_len == d2->hwaddr_len &&
(d2->hwaddr_len == 0
|| !memcmp(d1->hwaddr, d2->hwaddr, d1->hwaddr_len));
}
static peer * find_peer_by_phys(ctx *ctx, const dest_phys *dest)
{
for (size_t i = 0; i < ctx->size_peers; i++) {
peer *peer = &ctx->peers[i];
if (peer->state != REMOTE)
continue;
if (match_phys(&peer->phys, dest))
return peer;
}
return NULL;
}
static peer * find_peer_by_addr(ctx *ctx, mctp_eid_t eid, int net)
{
net_det *n = lookup_net(ctx, net);
if (eid != 0 && n && n->peeridx[eid] >= 0)
return &ctx->peers[n->peeridx[eid]];
return NULL;
}
/* Returns a deferred free pointer */
static const char* dest_phys_tostr(const dest_phys *dest)
{
char hex[MAX_ADDR_LEN*4];
char* buf;
size_t l = 50 + sizeof(hex);
buf = malloc(l);
if (!buf) {
return "Out of memory";
}
write_hex_addr(dest->hwaddr, dest->hwaddr_len, hex, sizeof(hex));
snprintf(buf, l, "physaddr if %d hw len %zu 0x%s", dest->ifindex, dest->hwaddr_len, hex);
return dfree(buf);
}
static const char* ext_addr_tostr(const struct sockaddr_mctp_ext *addr)
{
char hex[MAX_ADDR_LEN*4];
char* buf;
size_t l = 256;
buf = malloc(l);
if (!buf) {
return "Out of memory";
}
write_hex_addr(addr->smctp_haddr, addr->smctp_halen, hex, sizeof(hex));
snprintf(buf, l, "sockaddr_mctp_ext eid %d net %d type 0x%02x if %d hw len %hhu 0x%s",
addr->smctp_base.smctp_addr.s_addr,
addr->smctp_base.smctp_network,
addr->smctp_base.smctp_type,
addr->smctp_ifindex,
addr->smctp_halen, hex);
return dfree(buf);
}
static const char* peer_tostr(const peer *peer)
{
size_t l = 300;
char *str = NULL;
str = malloc(l);
if (!str) {
return "Out of memory";
}
snprintf(str, l, "peer eid %d net %d phys %s state %d",
peer->eid, peer->net, dest_phys_tostr(&peer->phys),
peer->state);
return dfree(str);
}
static int defer_free_handler(sd_event_source *s, void *userdata)
{
free(userdata);
sd_event_source_unref(s);
return 0;
}
/* Returns ptr, frees it on the next default event loop cycle (defer)*/
static void* dfree(void* ptr)
{
sd_event *e = NULL;
int rc;
if (!ptr)
return NULL;
rc = sd_event_default(&e);
if (rc < 0) {
warnx("defer_free no event loop");
goto out;
}
rc = sd_event_add_defer(e, NULL, defer_free_handler, ptr);
if (rc < 0) {
warnx("defer_free failed adding");
goto out;
}
out:
if (e)
sd_event_unref(e);
return ptr;
}
static int cb_exit_loop_io(sd_event_source *s, int fd, uint32_t revents, void *userdata)
{
sd_event_exit(sd_event_source_get_event(s), 0);
return 0;
}
static int cb_exit_loop_timeout(sd_event_source *s, uint64_t usec, void *userdata)
{
sd_event_exit(sd_event_source_get_event(s), -ETIMEDOUT);
return 0;
}
/* Events are EPOLLIN, EPOLLOUT etc.
Returns 0 on ready, negative on error. -ETIMEDOUT on timeout */
static int wait_fd_timeout(int fd, short events, uint64_t timeout_usec)
{
int rc;
sd_event *ev = NULL;
// Create a new event loop just for the event+timeout
rc = sd_event_new(&ev);
if (rc < 0)
goto out;
rc = sd_event_add_time_relative(ev, NULL, CLOCK_MONOTONIC,
timeout_usec, 0, cb_exit_loop_timeout, NULL);
if (rc < 0)
goto out;
rc = sd_event_add_io(ev, NULL, fd, events, cb_exit_loop_io, NULL);
if (rc < 0)
goto out;
// TODO: maybe need to break the loop on SIGINT event too?
rc = sd_event_loop(ev);
out:
if (ev)
sd_event_unref(ev);
return rc;
}
static int peer_from_path(ctx *ctx, const char* path, peer **ret_peer)
{
char *netstr = NULL, *eidstr = NULL;
uint32_t tmp, net;
mctp_eid_t eid;
int rc;
*ret_peer = NULL;
rc = sd_bus_path_decode_many(path, MCTP_DBUS_PATH "/%/%",
&netstr, &eidstr);
if (rc == 0)
return -ENOENT;
if (rc < 0)
return rc;
dfree(netstr);
dfree(eidstr);
if (parse_uint32(eidstr, &tmp) < 0 || tmp > 0xff)
return -EINVAL;
eid = tmp & 0xff;
if (parse_uint32(netstr, &net) < 0)
return -EINVAL;
*ret_peer = find_peer_by_addr(ctx, eid, net);
if (!*ret_peer)
return -ENOENT;
return 0;
}
static int path_from_peer(const peer *peer, char ** ret_path) {
size_t l;
char* buf;
if (!peer->published) {
warnx("BUG: %s on peer %s", __func__, peer_tostr(peer));
return -EPROTO;
}
l = strlen(MCTP_DBUS_PATH) + 60;
buf = malloc(l);
if (!buf)
return -ENOMEM;
/* can't use sd_bus_path_encode_many() since it escapes
leading digits */
snprintf(buf, l, "%s/%d/%d", MCTP_DBUS_PATH,
peer->net, peer->eid);
*ret_path = buf;
return 0;
}
/* Returns the message from a socket.
ret_buf is allocated, should be freed by the caller */
static int read_message(ctx *ctx, int sd, uint8_t **ret_buf, size_t *ret_buf_size,
struct sockaddr_mctp_ext *ret_addr)
{
int rc;
socklen_t addrlen;
ssize_t len;
uint8_t* buf = NULL;
size_t buf_size;
len = recvfrom(sd, NULL, 0, MSG_PEEK | MSG_TRUNC, NULL, 0);
if (len < 0) {
rc = -errno;
goto out;
}
buf_size = len;
buf = malloc(buf_size);
if (!buf) {
rc = -ENOMEM;
goto out;
}
addrlen = sizeof(struct sockaddr_mctp_ext);
memset(ret_addr, 0x0, addrlen);
len = recvfrom(sd, buf, buf_size, MSG_TRUNC, (struct sockaddr *)ret_addr,
&addrlen);
if (len < 0) {
rc = -errno;
goto out;
}
if ((size_t)len != buf_size) {
warnx("BUG: incorrect recvfrom %zd, expected %zu", len, buf_size);
rc = -EPROTO;
goto out;
}
if (addrlen != sizeof(struct sockaddr_mctp_ext)) {
warnx("Unexpected address size %u.", addrlen);
rc = -EPROTO;
goto out;
}
if (ctx->verbose) {
warnx("read_message got from %s len %zu",
ext_addr_tostr(ret_addr),
buf_size);
}
*ret_buf = buf;
*ret_buf_size = buf_size;
rc = 0;
out:
if (rc < 0) {
if (ctx->verbose) {
warnx("read_message returned error: %s", strerror(-rc));
}
free(buf);
}
return rc;
}
/* Replies to a real EID, not physical addressing */
static int reply_message(ctx *ctx, int sd, const void *resp, size_t resp_len,
const struct sockaddr_mctp_ext *addr)
{
ssize_t len;
struct sockaddr_mctp reply_addr;
memcpy(&reply_addr, &addr->smctp_base, sizeof(reply_addr));
reply_addr.smctp_tag &= ~MCTP_TAG_OWNER;
if (reply_addr.smctp_addr.s_addr == 0 ||
reply_addr.smctp_addr.s_addr == 0xff) {
warnx("BUG: reply_message can't take EID %d",
reply_addr.smctp_addr.s_addr);
return -EPROTO;
}
len = sendto(sd, resp, resp_len, 0,
(struct sockaddr*)&reply_addr, sizeof(reply_addr));
if (len < 0) {
return -errno;
}
if ((size_t)len != resp_len) {
warnx("BUG: short sendto %zd, expected %zu", len, resp_len);
return -EPROTO;
}
return 0;
}
// Handles new Incoming Set Endpoint ID request
static int handle_control_set_endpoint_id(ctx *ctx,
int sd, struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_cmd_set_eid *req = NULL;
struct mctp_ctrl_resp_set_eid respi = {0}, *resp = &respi;
size_t resp_len;
if (buf_size < sizeof(*req)) {
warnx("short Set Endpoint ID message");
return -ENOMSG;
}
req = (void*)buf;
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
resp->completion_code = 0;
resp->status = 0x01 << 4; // Already assigned, TODO
resp->eid_set = local_addr(ctx, addr->smctp_ifindex);
resp->eid_pool_size = 0;
resp_len = sizeof(struct mctp_ctrl_resp_set_eid);
// TODO: learn busowner route and neigh
return reply_message(ctx, sd, resp, resp_len, addr);
}
static int handle_control_get_version_support(ctx *ctx,
int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_cmd_get_mctp_ver_support *req = NULL;
struct mctp_ctrl_resp_get_mctp_ver_support *resp = NULL;
uint32_t *versions = NULL;
// space for 4 versions
uint8_t respbuf[sizeof(*resp) + 4*sizeof(*versions)];
size_t resp_len;
if (buf_size < sizeof(struct mctp_ctrl_cmd_get_mctp_ver_support)) {
warnx("short Get Version Support message");
return -ENOMSG;
}
req = (void*)buf;
resp = (void*)respbuf;
versions = (void*)(resp+1);
switch (req->msg_type_number) {
case 0xff: // Base Protocol
case 0x00: // Control protocol
// from DSP0236 1.3.1 section 12.6.2. Big endian.
versions[0] = htonl(0xF1F0FF00);
versions[1] = htonl(0xF1F1FF00);
versions[2] = htonl(0xF1F2FF00);
versions[3] = htonl(0xF1F3F100);
resp->number_of_entries = 4;
resp->completion_code = 0x00;
resp_len = sizeof(*resp) + 4*sizeof(*versions);
break;
default:
// Unsupported message type
resp->completion_code = 0x80;
resp_len = sizeof(*resp);
}
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
return reply_message(ctx, sd, resp, resp_len, addr);
}
static int handle_control_get_endpoint_id(ctx *ctx,
int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_cmd_get_eid *req = NULL;
struct mctp_ctrl_resp_get_eid respi = {0}, *resp = &respi;
if (buf_size < sizeof(*req)) {
warnx("short Get Endpoint ID message");
return -ENOMSG;
}
req = (void*)buf;
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
resp->eid = local_addr(ctx, addr->smctp_ifindex);
if (ctx->bus_owner)
SET_ENDPOINT_TYPE(resp->eid_type, MCTP_BUS_OWNER_BRIDGE);
// 10b = 2 = static EID supported, matches currently assigned.
SET_ENDPOINT_ID_TYPE(resp->eid_type, 2);
// TODO: medium specific information
return reply_message(ctx, sd, resp, sizeof(*resp), addr);
}
static int handle_control_get_endpoint_uuid(ctx *ctx,
int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_cmd_get_uuid *req = NULL;;
struct mctp_ctrl_resp_get_uuid respi = {0}, *resp = &respi;
if (buf_size < sizeof(*req)) {
warnx("short Get Endpoint UUID message");
return -ENOMSG;
}
req = (void*)buf;
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
memcpy(resp->uuid, ctx->uuid, sizeof(resp->uuid));
return reply_message(ctx, sd, resp, sizeof(*resp), addr);
}
static int handle_control_get_message_type_support(ctx *ctx,
int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_cmd_get_msg_type_support *req = NULL;;
struct mctp_ctrl_resp_get_msg_type_support *resp = NULL;
uint8_t resp_buf[sizeof(*resp) + 1];
if (buf_size < sizeof(*req)) {
warnx("short Get Message Type Support message");
return -ENOMSG;
}
req = (void*)buf;
resp = (void*)resp_buf;
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
// Only control messages supported
resp->msg_type_count = 1;
*((uint8_t*)(resp+1)) = MCTP_CTRL_HDR_MSG_TYPE;
return reply_message(ctx, sd, resp, sizeof(*resp), addr);
}
static int handle_control_resolve_endpoint_id(ctx *ctx,
int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_cmd_resolve_endpoint_id *req = NULL;
struct mctp_ctrl_resp_resolve_endpoint_id *resp = NULL;
uint8_t resp_buf[sizeof(*resp) + MAX_ADDR_LEN];
size_t resp_len;
peer *peer = NULL;
if (buf_size < sizeof(*req)) {
warnx("short Resolve Endpoint ID message");
return -ENOMSG;
}
req = (void*)buf;
resp = (void*)resp_buf;
memset(resp, 0x0, sizeof(*resp));
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
peer = find_peer_by_addr(ctx, req->eid,
addr->smctp_base.smctp_network);
if (!peer) {
resp->completion_code = 1;
resp_len = sizeof(*resp);
} else {
// TODO: bridging
resp->eid = req->eid;
memcpy((void*)(resp+1),
peer->phys.hwaddr, peer->phys.hwaddr_len);
resp_len = sizeof(*resp) + peer->phys.hwaddr_len;
}
printf("resp_len %zu ... 0x%02x 0x%02x\n", resp_len,
((uint8_t*)resp)[resp_len-2],
((uint8_t*)resp)[resp_len-1]);
return reply_message(ctx, sd, resp, resp_len, addr);
}
static int handle_control_unsupported(ctx *ctx,
int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_msg_hdr *req = NULL;
struct mctp_ctrl_generic {
struct mctp_ctrl_msg_hdr ctrl_hdr;
uint8_t completion_code;
} __attribute__((__packed__));
struct mctp_ctrl_generic respi = {0}, *resp = &respi;
if (buf_size < sizeof(*req)) {
warnx("short unsupported control message");
return -ENOMSG;
}
req = (void*)buf;
resp->ctrl_hdr.command_code = req->command_code;
resp->ctrl_hdr.rq_dgram_inst = RQDI_RESP;
resp->completion_code = MCTP_CTRL_CC_ERROR_UNSUPPORTED_CMD;
return reply_message(ctx, sd, resp, sizeof(*resp), addr);
}
static int cb_listen_control_msg(sd_event_source *s, int sd, uint32_t revents,
void *userdata)
{
struct sockaddr_mctp_ext addr = {0};
ctx *ctx = userdata;
uint8_t *buf = NULL;
size_t buf_size;
struct mctp_ctrl_msg_hdr *ctrl_msg = NULL;
int rc;
rc = read_message(ctx, sd, &buf, &buf_size, &addr);
if (rc < 0)
goto out;
if (addr.smctp_base.smctp_type != MCTP_CTRL_HDR_MSG_TYPE) {
warnx("BUG: Wrong message type for listen socket");
rc = -EINVAL;
goto out;
}
if (buf_size < sizeof(struct mctp_ctrl_msg_hdr)) {
warnx("Short message %zu bytes from %s",
buf_size, ext_addr_tostr(&addr));
rc = -EINVAL;
goto out;
}
ctrl_msg = (void*)buf;
if (ctx->verbose) {
warnx("Got control request command code %hhd",
ctrl_msg->command_code);
}
switch (ctrl_msg->command_code) {
case MCTP_CTRL_CMD_GET_VERSION_SUPPORT:
rc = handle_control_get_version_support(ctx,
sd, &addr, buf, buf_size);
break;
case MCTP_CTRL_CMD_SET_ENDPOINT_ID:
rc = handle_control_set_endpoint_id(ctx,
sd, &addr, buf, buf_size);
break;
case MCTP_CTRL_CMD_GET_ENDPOINT_ID:
rc = handle_control_get_endpoint_id(ctx,
sd, &addr, buf, buf_size);
break;
case MCTP_CTRL_CMD_GET_ENDPOINT_UUID:
rc = handle_control_get_endpoint_uuid(ctx,
sd, &addr, buf, buf_size);
break;
case MCTP_CTRL_CMD_GET_MESSAGE_TYPE_SUPPORT:
rc = handle_control_get_message_type_support(ctx,
sd, &addr, buf, buf_size);
break;
case MCTP_CTRL_CMD_RESOLVE_ENDPOINT_ID:
rc = handle_control_resolve_endpoint_id(ctx,
sd, &addr, buf, buf_size);
break;
default:
if (ctx->verbose) {
warnx("Ignoring unsupported command code 0x%02x",
ctrl_msg->command_code);
rc = -ENOTSUP;
}
rc = handle_control_unsupported(ctx,
sd, &addr, buf, buf_size);
}
if (ctx->verbose && rc < 0) {
warnx("Error handling command code %02x from %s: %s",
ctrl_msg->command_code, ext_addr_tostr(&addr),
strerror(-rc));
}
out:
free(buf);
return 0;
}
static int listen_control_msg(ctx *ctx, int net)
{
struct sockaddr_mctp addr = { 0 };
int rc, sd = -1, val;
sd = socket(AF_MCTP, SOCK_DGRAM, 0);
if (sd < 0) {
rc = -errno;
warn("%s: socket() failed", __func__);
goto out;
}
addr.smctp_family = AF_MCTP;
addr.smctp_network = net;
addr.smctp_addr.s_addr = MCTP_ADDR_ANY;
addr.smctp_type = MCTP_CTRL_HDR_MSG_TYPE;
addr.smctp_tag = MCTP_TAG_OWNER;
rc = bind(sd, (struct sockaddr *)&addr, sizeof(addr));
if (rc < 0) {
rc = -errno;
warn("%s: bind() failed", __func__);
goto out;
}
val = 1;
rc = setsockopt(sd, SOL_MCTP, MCTP_OPT_ADDR_EXT, &val, sizeof(val));
if (rc < 0) {
rc = -errno;
warn("Kernel does not support MCTP extended addressing");
goto out;
}
rc = sd_event_add_io(ctx->event, NULL, sd, EPOLLIN,
cb_listen_control_msg, ctx);
return rc;
out:
if (rc < 0) {
close(sd);
}
return rc;
}
static int cb_listen_monitor(sd_event_source *s, int sd, uint32_t revents,
void *userdata)
{
ctx *ctx = userdata;
mctp_nl_change *changes = NULL;
size_t num_changes;
int rc;
bool any_error = false;
rc = mctp_nl_handle_monitor(ctx->nl, &changes, &num_changes);
if (rc < 0) {
warnx("Error handling update from netlink, link state may now be outdated. %s",
strerror(-rc));
return rc;
}
for (size_t i = 0; i < num_changes; i++) {
struct mctp_nl_change *c = &changes[i];
switch (c->op) {
case MCTP_NL_ADD_LINK:
{
rc = add_interface_local(ctx, c->ifindex);
any_error |= (rc < 0);
}
break;
case MCTP_NL_DEL_LINK:
{
// Local addresses have already been deleted with DEL_EID
rc = del_interface(ctx, c->ifindex);
any_error |= (rc < 0);
}
break;
case MCTP_NL_CHANGE_NET:
{
// Local addresses have already been deleted with DEL_EID
rc = add_interface_local(ctx, c->ifindex);
any_error |= (rc < 0);
// Move remote endpoints
rc = change_net_interface(ctx, c->ifindex, c->old_net);
any_error |= (rc < 0);
}
break;
case MCTP_NL_ADD_EID:
{
int net = mctp_nl_net_byindex(ctx->nl, c->ifindex);
rc = add_local_eid(ctx, net, c->eid);
any_error |= (rc < 0);
}
break;
case MCTP_NL_DEL_EID:
{
rc = del_local_eid(ctx, c->old_net, c->eid);
any_error |= (rc < 0);
}
break;
case MCTP_NL_CHANGE_UP:
{
// 'up' state is currently unused
}
break;
}
}
if (ctx->verbose && any_error) {
printf("Error handling netlink update\n");
mctp_nl_changes_dump(ctx->nl, changes, num_changes);
mctp_nl_linkmap_dump(ctx->nl);
}
free(changes);
return 0;
}
static int listen_monitor(ctx *ctx)
{
int rc, sd;
sd = mctp_nl_monitor(ctx->nl, true);
if (sd < 0) {
return sd;
}
rc = sd_event_add_io(ctx->event, NULL, sd, EPOLLIN,
cb_listen_monitor, ctx);
return rc;
}
/* Use endpoint_query_peer() or endpoint_query_phys() instead.
*
* resp buffer is allocated, caller to free.
* Extended addressing is used optionally, depending on ext_addr arg. */
static int endpoint_query_addr(ctx *ctx,
const struct sockaddr_mctp_ext *req_addr, bool ext_addr,
const void* req, size_t req_len,
uint8_t **resp, size_t *resp_len, struct sockaddr_mctp_ext *resp_addr)
{
size_t req_addr_len;
int sd = -1, val;
ssize_t rc;
size_t buf_size;
uint8_t* buf = NULL;
*resp = NULL;
*resp_len = 0;
sd = socket(AF_MCTP, SOCK_DGRAM, 0);
if (sd < 0) {
warn("socket");
rc = -errno;
goto out;
}
// We want extended addressing on all received messages
val = 1;
rc = setsockopt(sd, SOL_MCTP, MCTP_OPT_ADDR_EXT, &val, sizeof(val));
if (rc < 0) {
rc = -errno;
warn("Kernel does not support MCTP extended addressing");
goto out;
}
if (ext_addr) {
req_addr_len = sizeof(struct sockaddr_mctp_ext);
} else {
req_addr_len = sizeof(struct sockaddr_mctp);
}
if (req_len == 0) {
warnx("BUG: zero length request");
rc = -EPROTO;
goto out;
}
rc = sendto(sd, req, req_len, 0, (struct sockaddr*)req_addr, req_addr_len);
if (rc < 0) {
rc = -errno;
if (ctx->verbose) {
warnx("%s: sendto(%s) %zu bytes failed. %s", __func__,
ext_addr_tostr(req_addr), req_len,
strerror(-rc));
}
goto out;
}
if ((size_t)rc != req_len) {
warnx("BUG: incorrect sendto %zd, expected %zu", rc, req_len);
rc = -EPROTO;
goto out;
}
rc = wait_fd_timeout(sd, EPOLLIN, ctx->mctp_timeout);
if (rc < 0) {
if (rc == -ETIMEDOUT && ctx->verbose) {
warnx("%s: receive timed out from %s", __func__,
ext_addr_tostr(req_addr));
}
goto out;
}
rc = read_message(ctx, sd, &buf, &buf_size, resp_addr);
if (rc < 0) {
goto out;
}
if (resp_addr->smctp_base.smctp_type != req_addr->smctp_base.smctp_type) {
warnx("Mismatching response type %d for request type %d. dest %s",
resp_addr->smctp_base.smctp_type,
req_addr->smctp_base.smctp_type,
ext_addr_tostr(req_addr));