-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmctpd.c
More file actions
5219 lines (4457 loc) · 126 KB
/
mctpd.c
File metadata and controls
5219 lines (4457 loc) · 126 KB
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
*/
#define _GNU_SOURCE
#include "config.h"
#include <assert.h>
#include <systemd/sd-bus-vtable.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <systemd/sd-event.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-id128.h>
#include "toml.h"
#include "mctp.h"
#include "mctp-util.h"
#include "mctp-netlink.h"
#include "mctp-control-spec.h"
#include "mctp-ops.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MCTP_DBUS_PATH "/au/com/codeconstruct/mctp1"
#define MCTP_DBUS_PATH_NETWORKS "/au/com/codeconstruct/mctp1/networks"
#define MCTP_DBUS_PATH_LINKS "/au/com/codeconstruct/mctp1/interfaces"
#define CC_MCTP_DBUS_IFACE_BUSOWNER "au.com.codeconstruct.MCTP.BusOwner1"
#define CC_MCTP_DBUS_IFACE_ENDPOINT "au.com.codeconstruct.MCTP.Endpoint1"
#define CC_MCTP_DBUS_IFACE_BRIDGE "au.com.codeconstruct.MCTP.Bridge1"
#define CC_MCTP_DBUS_IFACE_TESTING "au.com.codeconstruct.MCTPTesting"
#define MCTP_DBUS_NAME "au.com.codeconstruct.MCTP1"
#define MCTP_DBUS_IFACE_ENDPOINT "xyz.openbmc_project.MCTP.Endpoint"
#define OPENBMC_IFACE_COMMON_UUID "xyz.openbmc_project.Common.UUID"
#define CC_MCTP_DBUS_IFACE_INTERFACE "au.com.codeconstruct.MCTP.Interface1"
#define CC_MCTP_DBUS_NETWORK_INTERFACE "au.com.codeconstruct.MCTP.Network1"
// an arbitrary constant for use with sd_id128_get_machine_app_specific()
static const char *mctpd_appid = "67369c05-4b97-4b7e-be72-65cfd8639f10";
static const char *conf_file_default = MCTPD_CONF_FILE_DEFAULT;
static const mctp_eid_t eid_alloc_min = 0x08;
static const mctp_eid_t eid_alloc_max = 0xfe;
// arbitrary sanity
static size_t MAX_PEER_SIZE = 1000000;
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 {
struct ctx *ctx;
uint32_t net;
// EID mappings, NULL is unused.
struct peer *peers[256];
sd_bus_slot *slot;
char *path;
};
struct ctx;
// all local peers have the same phys
static const dest_phys local_phys = { .ifindex = 0 };
enum endpoint_role {
ENDPOINT_ROLE_UNKNOWN,
ENDPOINT_ROLE_BUS_OWNER,
ENDPOINT_ROLE_ENDPOINT,
};
struct role {
enum endpoint_role role;
const char *conf_val;
const char *dbus_val;
};
static const struct role roles[] = {
[ENDPOINT_ROLE_UNKNOWN] = {
.role = ENDPOINT_ROLE_UNKNOWN,
.conf_val = "unknown",
.dbus_val = "Unknown",
},
[ENDPOINT_ROLE_BUS_OWNER] = {
.role = ENDPOINT_ROLE_BUS_OWNER,
.conf_val = "bus-owner",
.dbus_val = "BusOwner",
},
[ENDPOINT_ROLE_ENDPOINT] = {
.role = ENDPOINT_ROLE_ENDPOINT,
.conf_val = "endpoint",
.dbus_val = "Endpoint",
},
};
enum discovery_state {
DISCOVERY_UNSUPPORTED,
DISCOVERY_DISCOVERED,
DISCOVERY_UNDISCOVERED,
};
struct link {
bool published;
int ifindex;
enum endpoint_role role;
char *path;
sd_bus_slot *slot_iface;
sd_bus_slot *slot_busowner;
struct {
enum discovery_state flag;
sd_event_source *notify_source;
dest_phys notify_dest;
uint64_t notify_retry_delay;
uint8_t notify_tries_left;
} discovery;
struct ctx *ctx;
};
struct peer {
uint32_t 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 {
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;
sd_bus_slot *slot_obmc_endpoint;
sd_bus_slot *slot_cc_endpoint;
sd_bus_slot *slot_bridge;
sd_bus_slot *slot_uuid;
char *path;
bool have_neigh;
bool have_route;
// MTU for the route. Set to the interface's minimum MTU initially,
// or changed by .SetMTU method
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;
// Pool size
uint8_t pool_size;
uint8_t pool_start;
};
struct msg_type_support {
uint8_t msg_type;
uint32_t *versions;
size_t num_versions;
sd_bus_track *source_peer;
};
struct ctx {
sd_event *event;
sd_bus *bus;
// Configuration
char *config_filename;
mctp_nl *nl;
// Default BMC role in All of MCTP medium interface
enum endpoint_role default_role;
// An allocated array of peers, changes address (reallocated) during runtime
struct peer **peers;
size_t num_peers;
struct net **nets;
size_t num_nets;
// the range we allocate any dynamic EIDs from
mctp_eid_t dyn_eid_min;
mctp_eid_t dyn_eid_max;
// Timeout in usecs for a MCTP response
uint64_t mctp_timeout;
// Next IID to use
uint8_t iid;
uint8_t uuid[16];
// Supported message types and their versions
struct msg_type_support *supported_msg_types;
size_t num_supported_msg_types;
// Verbose logging
bool verbose;
// maximum pool size for assumed MCTP Bridge
uint8_t max_pool_size;
};
static int emit_endpoint_added(const struct peer *peer);
static int emit_endpoint_removed(const struct peer *peer);
static int emit_interface_added(struct link *link);
static int emit_interface_removed(struct link *link);
static int emit_net_added(struct ctx *ctx, struct net *net);
static int emit_net_removed(struct ctx *ctx, struct net *net);
static int add_peer(struct ctx *ctx, const dest_phys *dest, mctp_eid_t eid,
uint32_t net, struct peer **ret_peer, bool allow_bridged);
static int add_peer_from_addr(struct ctx *ctx,
const struct sockaddr_mctp_ext *addr,
struct peer **ret_peer);
static int remove_peer(struct peer *peer);
static int query_peer_properties(struct peer *peer);
static int setup_added_peer(struct peer *peer);
static void add_peer_route(struct peer *peer);
static int publish_peer(struct peer *peer);
static int unpublish_peer(struct peer *peer);
static int peer_route_update(struct peer *peer, uint16_t type);
static int peer_neigh_update(struct peer *peer, uint16_t type);
static int add_interface_local(struct ctx *ctx, int ifindex);
static int del_interface(struct link *link);
static int rename_interface(struct ctx *ctx, struct link *link, int ifindex);
static int change_net_interface(struct ctx *ctx, int ifindex, uint32_t old_net);
static int add_local_eid(struct ctx *ctx, uint32_t net, int eid);
static int del_local_eid(struct ctx *ctx, uint32_t net, int eid);
static int add_net(struct ctx *ctx, uint32_t net);
static void del_net(struct net *net);
static int add_interface(struct ctx *ctx, int ifindex);
static int endpoint_allocate_eids(struct peer *peer);
static const sd_bus_vtable bus_endpoint_obmc_vtable[];
static const sd_bus_vtable bus_endpoint_cc_vtable[];
static const sd_bus_vtable bus_endpoint_bridge[];
static const sd_bus_vtable bus_endpoint_uuid_vtable[];
__attribute__((format(printf, 1, 2))) static void bug_warn(const char *fmt, ...)
{
char *bug_fmt = NULL;
va_list ap;
int rc;
rc = asprintf(&bug_fmt, "BUG: %s", fmt);
if (rc < 0)
return;
va_start(ap, fmt);
mctp_ops.bug_warn(bug_fmt, ap);
va_end(ap);
free(bug_fmt);
}
mctp_eid_t local_addr(const struct 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 struct net *lookup_net(struct ctx *ctx, uint32_t 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 struct peer *find_peer_by_phys(struct ctx *ctx, const dest_phys *dest)
{
for (size_t i = 0; i < ctx->num_peers; i++) {
struct peer *peer = ctx->peers[i];
if (peer->state != REMOTE)
continue;
if (match_phys(&peer->phys, dest))
return peer;
}
return NULL;
}
static struct peer *find_peer_by_addr(struct ctx *ctx, mctp_eid_t eid,
uint32_t net)
{
struct net *n = lookup_net(ctx, net);
if (eid != 0 && n && n->peers[eid])
return n->peers[eid];
return NULL;
}
static int find_local_eids_by_net(struct net *net, size_t *local_eid_cnt,
mctp_eid_t *ret_eids)
{
size_t local_count = 0;
struct peer *peer;
*local_eid_cnt = 0;
for (size_t t = 0; t < 256; t++) {
peer = net->peers[t];
if (!peer)
continue;
if (peer && (peer->state == LOCAL))
ret_eids[local_count++] = t;
}
*local_eid_cnt = local_count;
return 0;
}
/* 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 %u 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 struct 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 %u phys %s state %d", peer->eid,
peer->net, dest_phys_tostr(&peer->phys), peer->state);
return dfree(str);
}
static const char *peer_tostr_short(const struct peer *peer)
{
size_t l = 30;
char *str = NULL;
str = malloc(l);
if (!str) {
return "Out of memory";
}
snprintf(str, l, "%u:%d", peer->net, peer->eid);
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 = mctp_ops.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 const char *path_from_peer(const struct peer *peer)
{
if (!peer->published) {
bug_warn("%s on peer %s", __func__, peer_tostr(peer));
return NULL;
}
return peer->path;
}
static int get_role(const char *mode, struct role *role)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(roles); i++) {
if (roles[i].dbus_val &&
(strcmp(roles[i].dbus_val, mode) == 0)) {
memcpy(role, &roles[i], sizeof(struct role));
return 0;
}
}
return -1;
}
/* Returns the message from a socket.
ret_buf is allocated, should be freed by the caller */
static int read_message(struct 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 = mctp_ops.mctp.recvfrom(sd, NULL, 0, MSG_PEEK | MSG_TRUNC, NULL,
0);
if (len < 0) {
rc = -errno;
goto out;
}
if (len == 0) {
*ret_buf = NULL;
*ret_buf_size = 0;
rc = 0;
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 = mctp_ops.mctp.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) {
bug_warn("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 physical address */
static int reply_message_phys(struct ctx *ctx, int sd, const void *resp,
size_t resp_len,
const struct sockaddr_mctp_ext *addr)
{
ssize_t len;
struct sockaddr_mctp_ext reply_addr = *addr;
reply_addr.smctp_base.smctp_tag &= ~MCTP_TAG_OWNER;
len = mctp_ops.mctp.sendto(sd, resp, resp_len, 0,
(struct sockaddr *)&reply_addr,
sizeof(reply_addr));
if (len < 0) {
return -errno;
}
if ((size_t)len != resp_len) {
bug_warn("short sendto %zd, expected %zu", len, resp_len);
return -EPROTO;
}
return 0;
}
/* Replies to a real EID, not physical addressing */
static int reply_message(struct 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) {
bug_warn("reply_message can't take EID %d",
reply_addr.smctp_addr.s_addr);
return -EPROTO;
}
len = mctp_ops.mctp.sendto(sd, resp, resp_len, 0,
(struct sockaddr *)&reply_addr,
sizeof(reply_addr));
if (len < 0) {
return -errno;
}
if ((size_t)len != resp_len) {
bug_warn("short sendto %zd, expected %zu", len, resp_len);
return -EPROTO;
}
return 0;
}
/// Clear interface local addresses and remote cached peers
static void clear_interface_addrs(struct ctx *ctx, int ifindex)
{
mctp_eid_t *addrs;
size_t addrs_num;
size_t i;
int rc;
// Remove all addresses on this interface
addrs = mctp_nl_addrs_byindex(ctx->nl, ifindex, &addrs_num);
if (addrs) {
for (i = 0; i < addrs_num; i++) {
rc = mctp_nl_addr_del(ctx->nl, addrs[i], ifindex);
if (rc < 0) {
errx(rc,
"ERR: cannot remove local eid %d ifindex %d",
addrs[i], ifindex);
}
}
free(addrs);
}
// Remove all peers on this interface
for (i = 0; i < ctx->num_peers; i++) {
struct peer *p = ctx->peers[i];
if (p->state == REMOTE && p->phys.ifindex == ifindex) {
remove_peer(p);
}
}
}
/// Handles new Incoming Set Endpoint ID request
///
/// This currently handles two cases: Top-most bus owner and Endpoint. No bridge
/// support yet.
///
///
/// # References
///
/// The DSP0236 1.3.3 specification describes Set Endpoint ID in the following
/// sections:
///
/// - 8.18 Endpoint ID assignment and endpoint ID pools
///
/// > A non-bridge device that is connected to multiple different buses
/// > will have one EID for each bus it is attached to.
///
/// - 9.1.3 EID options for MCTP bridge
///
/// > There are three general options:
/// > - The bridge uses a single MCTP endpoint
/// > - The bridge uses an MCTP endpoint for each bus that connects to a bus owner
/// > - The bridge uses an MCTP endpoint for every bus to which it connects
///
/// - 12.4 Set Endpoint ID
///
/// [the whole section]
///
static int handle_control_set_endpoint_id(struct 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;
struct link *link_data;
struct peer *peer;
size_t resp_len;
int rc;
if (buf_size < sizeof(*req)) {
bug_warn("short Set Endpoint ID message");
return -ENOMSG;
}
req = (void *)buf;
link_data = mctp_nl_get_link_userdata(ctx->nl, addr->smctp_ifindex);
if (!link_data) {
bug_warn("unconfigured interface %d", addr->smctp_ifindex);
return -ENOENT;
}
mctp_ctrl_msg_hdr_init_resp(&respi.ctrl_hdr, req->ctrl_hdr);
resp->completion_code = MCTP_CTRL_CC_SUCCESS;
resp_len = sizeof(struct mctp_ctrl_resp_set_eid);
// reject if we are bus owner
if (link_data->role == ENDPOINT_ROLE_BUS_OWNER) {
warnx("Rejected set EID %d request from (%s) because we are the bus owner",
req->eid, ext_addr_tostr(addr));
resp->completion_code = MCTP_CTRL_CC_ERROR_UNSUPPORTED_CMD;
resp_len = sizeof(struct mctp_ctrl_resp);
return reply_message(ctx, sd, resp, resp_len, addr);
}
// error if EID is invalid
if (req->eid < 0x08 || req->eid == 0xFF) {
warnx("Rejected invalid EID %d", req->eid);
resp->completion_code = MCTP_CTRL_CC_ERROR_INVALID_DATA;
resp_len = sizeof(struct mctp_ctrl_resp);
return reply_message(ctx, sd, resp, resp_len, addr);
}
switch (GET_MCTP_SET_EID_OPERATION(req->operation)) {
case MCTP_SET_EID_SET:
// TODO: for bridges, only accept EIDs from originator bus
//
// We currently only support endpoints, which require separate
// EIDs on interfaces (see function comment). For bridges, we
// might need to support sharing a single EID for multiple
// interfaces. We will need to:
// - track the first bus assigned the EID.
// - policy for propagating EID to other interfaces (see bridge
// EID options in function comment above)
// fallthrough
case MCTP_SET_EID_FORCE:
fprintf(stderr, "setting EID to %d\n", req->eid);
// When we are assigned a new EID, assume our world view of the
// network reachable from this interface has been stale. Reset
// everything.
clear_interface_addrs(ctx, addr->smctp_ifindex);
rc = mctp_nl_addr_add(ctx->nl, req->eid, addr->smctp_ifindex);
if (rc < 0) {
warnx("ERR: cannot add local eid %d to ifindex %d",
req->eid, addr->smctp_ifindex);
resp->completion_code = MCTP_CTRL_CC_ERROR_NOT_READY;
}
rc = add_peer_from_addr(ctx, addr, &peer);
if (rc == 0) {
rc = setup_added_peer(peer);
}
if (rc < 0) {
warnx("ERR: cannot add bus owner to object lists");
}
if (link_data->discovery.flag != DISCOVERY_UNSUPPORTED) {
link_data->discovery.flag = DISCOVERY_DISCOVERED;
}
resp->status =
SET_MCTP_EID_ASSIGNMENT_STATUS(MCTP_SET_EID_ACCEPTED) |
SET_MCTP_EID_ALLOCATION_STATUS(MCTP_SET_EID_POOL_NONE);
resp->eid_set = req->eid;
resp->eid_pool_size = 0;
fprintf(stderr, "Accepted set eid %d\n", req->eid);
return reply_message(ctx, sd, resp, resp_len, addr);
case MCTP_SET_EID_DISCOVERED:
if (link_data->discovery.flag == DISCOVERY_UNSUPPORTED) {
resp->completion_code = MCTP_CTRL_CC_ERROR_INVALID_DATA;
resp_len = sizeof(struct mctp_ctrl_resp);
return reply_message(ctx, sd, resp, resp_len, addr);
}
link_data->discovery.flag = DISCOVERY_DISCOVERED;
resp->status =
SET_MCTP_EID_ASSIGNMENT_STATUS(MCTP_SET_EID_REJECTED) |
SET_MCTP_EID_ALLOCATION_STATUS(MCTP_SET_EID_POOL_NONE);
resp->eid_set = req->eid;
resp->eid_pool_size = 0;
return reply_message(ctx, sd, resp, resp_len, addr);
case MCTP_SET_EID_RESET:
// unsupported
resp->completion_code = MCTP_CTRL_CC_ERROR_INVALID_DATA;
return reply_message(ctx, sd, resp, resp_len, addr);
default:
bug_warn("unreachable Set EID operation code");
return -EINVAL;
}
}
static int
handle_control_get_version_support(struct ctx *ctx, int sd,
const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_resp_get_mctp_ver_support *resp = NULL;
struct mctp_ctrl_cmd_get_mctp_ver_support *req = NULL;
size_t resp_len, i, ver_count = 0, ver_bytes_count;
uint32_t *versions = NULL;
uint8_t *respbuf = NULL;
ssize_t ver_idx = -1;
int rc;
if (buf_size < sizeof(struct mctp_ctrl_cmd_get_mctp_ver_support)) {
warnx("short Get Version Support message");
return -ENOMSG;
}
req = (void *)buf;
if (req->msg_type_number == 0xFF) {
// use same version for base spec and control protocol
req->msg_type_number = 0;
}
for (i = 0; i < ctx->num_supported_msg_types; i++) {
if (ctx->supported_msg_types[i].msg_type ==
req->msg_type_number) {
ver_idx = i;
break;
}
}
if (ver_idx < 0) {
respbuf = malloc(sizeof(struct mctp_ctrl_resp));
if (!respbuf) {
warnx("Failed to allocate response buffer");
return -ENOMEM;
}
resp = (void *)respbuf;
// Nobody registered yet as responder for this type
resp->completion_code =
MCTP_CTRL_CC_GET_MCTP_VER_SUPPORT_UNSUPPORTED_TYPE;
resp_len = sizeof(struct mctp_ctrl_resp);
} else {
ver_count = ctx->supported_msg_types[ver_idx].num_versions;
ver_bytes_count = ver_count * sizeof(uint32_t);
respbuf = malloc(sizeof(*resp) + ver_bytes_count);
if (!respbuf) {
warnx("Failed to allocate response buffer for versions");
return -ENOMEM;
}
resp = (void *)respbuf;
resp->number_of_entries = ver_count;
versions = (void *)(resp + 1);
memcpy(versions, ctx->supported_msg_types[ver_idx].versions,
ver_bytes_count);
resp->completion_code = MCTP_CTRL_CC_SUCCESS;
resp_len = sizeof(*resp) + ver_bytes_count;
}
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
rc = reply_message(ctx, sd, resp, resp_len, addr);
free(respbuf);
return rc;
}
static int handle_control_get_endpoint_id(struct 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;
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
resp->eid = local_addr(ctx, addr->smctp_ifindex);
resp->eid_type = 0;
if (ctx->default_role == ENDPOINT_ROLE_BUS_OWNER)
resp->eid_type |= SET_ENDPOINT_TYPE(MCTP_BUS_OWNER_BRIDGE);
resp->eid_type |=
SET_ENDPOINT_ID_TYPE(MCTP_STATIC_EID_MATCHING_PRESENT);
// TODO: medium specific information
// Get Endpoint ID is typically send and reply using physical addressing.
return reply_message_phys(ctx, sd, resp, sizeof(*resp), addr);
}
static int
handle_control_get_endpoint_uuid(struct 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;
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
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(
struct ctx *ctx, int sd, const struct sockaddr_mctp_ext *addr,
const uint8_t *buf, const size_t buf_size)
{
struct mctp_ctrl_resp_get_msg_type_support *resp = NULL;
struct mctp_ctrl_cmd_get_msg_type_support *req = NULL;
size_t i, resp_len, type_count;
uint8_t *resp_buf, *msg_types;
int rc;
if (buf_size < sizeof(*req)) {
warnx("short Get Message Type Support message");
return -ENOMSG;
}
req = (void *)buf;
type_count = ctx->num_supported_msg_types;
// Allocate extra space for the message types
resp_len = sizeof(*resp) + type_count;
resp_buf = malloc(resp_len);
if (!resp_buf) {
warnx("Failed to allocate response buffer");
return -ENOMEM;
}
resp = (void *)resp_buf;
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
resp->completion_code = MCTP_CTRL_CC_SUCCESS;
resp->msg_type_count = type_count;
// Append message types after msg_type_count
msg_types = (uint8_t *)(resp + 1);
for (i = 0; i < type_count; i++) {
msg_types[i] = ctx->supported_msg_types[i].msg_type;